1 /* 2 Copyright (C) 2001-present by Serge Lamikhov-Center 3 4 Permission is hereby granted, free of charge, to any person obtaining a copy 5 of this software and associated documentation files (the "Software"), to deal 6 in the Software without restriction, including without limitation the rights 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 copies of the Software, and to permit persons to whom the Software is 9 furnished to do so, subject to the following conditions: 10 11 The above copyright notice and this permission notice shall be included in 12 all copies or substantial portions of the Software. 13 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 THE SOFTWARE. 21 */ 22 23 #ifndef ELFIO_C_WRAPPER_H 24 #define ELFIO_C_WRAPPER_H 25 26 #define ELFIO_C_HEADER_ACCESS_GET( TYPE, FNAME ) \ 27 TYPE elfio_get_##FNAME( pelfio_t pelfio ); 28 29 #define ELFIO_C_HEADER_ACCESS_GET_SET( TYPE, FNAME ) \ 30 TYPE elfio_get_##FNAME( pelfio_t pelfio ); \ 31 void elfio_set_##FNAME( pelfio_t pelfio, TYPE val ); 32 33 #define ELFIO_C_HEADER_ACCESS_GET_IMPL( TYPE, FNAME ) \ 34 TYPE elfio_get_##FNAME( pelfio_t pelfio ) { return pelfio->get_##FNAME(); } 35 36 #define ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( TYPE, FNAME ) \ 37 TYPE elfio_get_##FNAME( pelfio_t pelfio ) \ 38 { \ 39 return pelfio->get_##FNAME(); \ 40 } \ 41 void elfio_set_##FNAME( pelfio_t pelfio, TYPE val ) \ 42 { \ 43 pelfio->set_##FNAME( val ); \ 44 } 45 46 #define ELFIO_C_GET_ACCESS_IMPL( CLASS, TYPE, NAME ) \ 47 TYPE elfio_##CLASS##_get_##NAME( p##CLASS##_t p##CLASS ) \ 48 { \ 49 return p##CLASS->get_##NAME(); \ 50 } 51 52 #define ELFIO_C_SET_ACCESS_IMPL( CLASS, TYPE, NAME ) \ 53 void elfio_##CLASS##_set_##NAME( p##CLASS##_t p##CLASS, TYPE value ) \ 54 { \ 55 p##CLASS->set_##NAME( value ); \ 56 } 57 58 #define ELFIO_C_GET_SET_ACCESS_IMPL( CLASS, TYPE, NAME ) \ 59 TYPE elfio_##CLASS##_get_##NAME( p##CLASS##_t p##CLASS ) \ 60 { \ 61 return p##CLASS->get_##NAME(); \ 62 } \ 63 void elfio_##CLASS##_set_##NAME( p##CLASS##_t p##CLASS, TYPE value ) \ 64 { \ 65 p##CLASS->set_##NAME( value ); \ 66 } 67 68 #define ELFIO_C_GET_ACCESS( CLASS, TYPE, NAME ) \ 69 TYPE elfio_##CLASS##_get_##NAME( p##CLASS##_t p##CLASS ); 70 71 #define ELFIO_C_SET_ACCESS( CLASS, TYPE, NAME ) \ 72 void elfio_##CLASS##_set_##NAME( p##CLASS##_t p##CLASS, TYPE value ); 73 74 #define ELFIO_C_GET_SET_ACCESS( CLASS, TYPE, NAME ) \ 75 TYPE elfio_##CLASS##_get_##NAME( p##CLASS##_t p##CLASS ); \ 76 void elfio_##CLASS##_set_##NAME( p##CLASS##_t p##CLASS, TYPE value ) 77 78 #ifdef __cplusplus 79 typedef ELFIO::elfio* pelfio_t; 80 typedef ELFIO::section* psection_t; 81 typedef ELFIO::segment* psegment_t; 82 typedef ELFIO::symbol_section_accessor* psymbol_t; 83 typedef ELFIO::relocation_section_accessor* prelocation_t; 84 typedef ELFIO::string_section_accessor* pstring_t; 85 typedef ELFIO::note_section_accessor* pnote_t; 86 typedef ELFIO::modinfo_section_accessor* pmodinfo_t; 87 typedef ELFIO::dynamic_section_accessor* pdynamic_t; 88 typedef ELFIO::array_section_accessor<Elf64_Word>* parray_t; 89 90 extern "C" 91 { 92 #else 93 typedef void* pelfio_t; 94 typedef void* psection_t; 95 typedef void* psegment_t; 96 typedef void* psymbol_t; 97 typedef void* prelocation_t; 98 typedef void* pstring_t; 99 typedef void* pnote_t; 100 typedef void* pmodinfo_t; 101 typedef void* pdynamic_t; 102 typedef void* parray_t; 103 typedef int bool; 104 #endif 105 106 //----------------------------------------------------------------------------- 107 // elfio 108 //----------------------------------------------------------------------------- 109 pelfio_t elfio_new(); 110 void elfio_delete( pelfio_t pelfio ); 111 void elfio_create( pelfio_t pelfio, 112 unsigned char file_class, 113 unsigned char encoding ); 114 bool elfio_load( pelfio_t pelfio, const char* file_name ); 115 bool elfio_save( pelfio_t pelfio, const char* file_name ); 116 ELFIO_C_HEADER_ACCESS_GET( unsigned char, class ); 117 ELFIO_C_HEADER_ACCESS_GET( unsigned char, elf_version ); 118 ELFIO_C_HEADER_ACCESS_GET( unsigned char, encoding ); 119 ELFIO_C_HEADER_ACCESS_GET( Elf_Word, version ); 120 ELFIO_C_HEADER_ACCESS_GET( Elf_Half, header_size ); 121 ELFIO_C_HEADER_ACCESS_GET( Elf_Half, section_entry_size ); 122 ELFIO_C_HEADER_ACCESS_GET( Elf_Half, segment_entry_size ); 123 ELFIO_C_HEADER_ACCESS_GET_SET( unsigned char, os_abi ); 124 ELFIO_C_HEADER_ACCESS_GET_SET( unsigned char, abi_version ); 125 ELFIO_C_HEADER_ACCESS_GET_SET( Elf_Half, type ); 126 ELFIO_C_HEADER_ACCESS_GET_SET( Elf_Half, machine ); 127 ELFIO_C_HEADER_ACCESS_GET_SET( Elf_Word, flags ); 128 ELFIO_C_HEADER_ACCESS_GET_SET( Elf64_Addr, entry ); 129 ELFIO_C_HEADER_ACCESS_GET_SET( Elf64_Off, sections_offset ); 130 ELFIO_C_HEADER_ACCESS_GET_SET( Elf64_Off, segments_offset ); 131 ELFIO_C_HEADER_ACCESS_GET_SET( Elf_Half, section_name_str_index ); 132 Elf_Half elfio_get_sections_num( pelfio_t pelfio ); 133 psection_t elfio_get_section_by_index( pelfio_t pelfio, int index ); 134 psection_t elfio_get_section_by_name( pelfio_t pelfio, char* name ); 135 psection_t elfio_add_section( pelfio_t pelfio, char* name ); 136 Elf_Half elfio_get_segments_num( pelfio_t pelfio ); 137 psegment_t elfio_get_segment_by_index( pelfio_t pelfio, int index ); 138 psegment_t elfio_add_segment( pelfio_t pelfio ); 139 bool elfio_validate( pelfio_t pelfio, char* msg, int msg_len ); 140 141 //----------------------------------------------------------------------------- 142 // section 143 //----------------------------------------------------------------------------- 144 ELFIO_C_GET_ACCESS( section, Elf_Half, index ); 145 ELFIO_C_GET_SET_ACCESS( section, Elf_Word, type ); 146 ELFIO_C_GET_SET_ACCESS( section, Elf_Xword, flags ); 147 ELFIO_C_GET_SET_ACCESS( section, Elf_Word, info ); 148 ELFIO_C_GET_SET_ACCESS( section, Elf_Word, link ); 149 ELFIO_C_GET_SET_ACCESS( section, Elf_Xword, addr_align ); 150 ELFIO_C_GET_SET_ACCESS( section, Elf_Xword, entry_size ); 151 ELFIO_C_GET_SET_ACCESS( section, Elf64_Addr, address ); 152 ELFIO_C_GET_SET_ACCESS( section, Elf_Xword, size ); 153 ELFIO_C_GET_SET_ACCESS( section, Elf_Word, name_string_offset ); 154 ELFIO_C_GET_ACCESS( section, Elf64_Off, offset ); 155 void elfio_section_get_name( psection_t psection, char* buffer, int len ); 156 void elfio_section_set_name( psection_t psection, char* buffer ); 157 char* elfio_section_get_data( psection_t psection ); 158 void elfio_section_set_data( psection_t psection, 159 const char* pData, 160 Elf_Word size ); 161 void elfio_section_append_data( psection_t psection, 162 const char* pData, 163 Elf_Word size ); 164 165 //----------------------------------------------------------------------------- 166 // segment 167 //----------------------------------------------------------------------------- 168 ELFIO_C_GET_ACCESS( segment, Elf_Half, index ); 169 ELFIO_C_GET_SET_ACCESS( segment, Elf_Word, type ); 170 ELFIO_C_GET_SET_ACCESS( segment, Elf_Word, flags ); 171 ELFIO_C_GET_SET_ACCESS( segment, Elf_Xword, align ); 172 ELFIO_C_GET_SET_ACCESS( segment, Elf_Xword, memory_size ); 173 ELFIO_C_GET_SET_ACCESS( segment, Elf64_Addr, virtual_address ); 174 ELFIO_C_GET_SET_ACCESS( segment, Elf64_Addr, physical_address ); 175 ELFIO_C_GET_SET_ACCESS( segment, Elf_Xword, file_size ); 176 ELFIO_C_GET_ACCESS( segment, Elf64_Off, offset ); 177 char* elfio_segment_get_data( psegment_t psegment ); 178 Elf_Half elfio_segment_add_section_index( psegment_t psegment, 179 Elf_Half index, 180 Elf_Xword addr_align ); 181 Elf_Half elfio_segment_get_sections_num( psegment_t psegment ); 182 Elf_Half elfio_segment_get_section_index_at( psegment_t psegment, 183 Elf_Half num ); 184 bool elfio_segment_is_offset_initialized( psegment_t psegment ); 185 186 //----------------------------------------------------------------------------- 187 // symbol 188 //----------------------------------------------------------------------------- 189 psymbol_t elfio_symbol_section_accessor_new( pelfio_t pelfio, 190 psection_t psection ); 191 void elfio_symbol_section_accessor_delete( psymbol_t psymbol ); 192 Elf_Xword elfio_symbol_get_symbols_num( psymbol_t psymbol ); 193 bool elfio_symbol_get_symbol( psymbol_t psymbol, 194 Elf_Xword index, 195 char* name, 196 int name_len, 197 Elf64_Addr* value, 198 Elf_Xword* size, 199 unsigned char* bind, 200 unsigned char* type, 201 Elf_Half* section_index, 202 unsigned char* other ); 203 Elf_Word elfio_symbol_add_symbol( psymbol_t psymbol, 204 Elf_Word name, 205 Elf64_Addr value, 206 Elf_Xword size, 207 unsigned char info, 208 unsigned char other, 209 Elf_Half shndx ); 210 Elf_Xword elfio_symbol_arrange_local_symbols( 211 psymbol_t psymbol, 212 void ( *func )( Elf_Xword first, Elf_Xword second ) ); 213 214 //----------------------------------------------------------------------------- 215 // relocation 216 //----------------------------------------------------------------------------- 217 prelocation_t elfio_relocation_section_accessor_new( pelfio_t pelfio, 218 psection_t psection ); 219 void elfio_relocation_section_accessor_delete( prelocation_t prelocation ); 220 Elf_Xword elfio_relocation_get_entries_num( prelocation_t prelocation ); 221 bool elfio_relocation_get_entry( prelocation_t prelocation, 222 Elf_Xword index, 223 Elf64_Addr* offset, 224 Elf_Word* symbol, 225 Elf_Word* type, 226 Elf_Sxword* addend ); 227 bool elfio_relocation_set_entry( prelocation_t prelocation, 228 Elf_Xword index, 229 Elf64_Addr offset, 230 Elf_Word symbol, 231 Elf_Word type, 232 Elf_Sxword addend ); 233 void elfio_relocation_add_entry( prelocation_t prelocation, 234 Elf64_Addr offset, 235 Elf_Word symbol, 236 unsigned char type, 237 Elf_Sxword addend ); 238 void elfio_relocation_swap_symbols( prelocation_t prelocation, 239 Elf_Xword first, 240 Elf_Xword second ); 241 242 //----------------------------------------------------------------------------- 243 // string 244 //----------------------------------------------------------------------------- 245 pstring_t elfio_string_section_accessor_new( psection_t psection ); 246 void elfio_string_section_accessor_delete( pstring_t pstring ); 247 const char* elfio_string_get_string( pstring_t pstring, Elf_Word index ); 248 Elf_Word elfio_string_add_string( pstring_t pstring, char* str ); 249 250 //----------------------------------------------------------------------------- 251 // note 252 //----------------------------------------------------------------------------- 253 pnote_t elfio_note_section_accessor_new( pelfio_t pelfio, 254 psection_t psection ); 255 void elfio_note_section_accessor_delete( pnote_t pstring ); 256 Elf_Word elfio_note_get_notes_num( pnote_t pnote ); 257 bool elfio_note_get_note( pnote_t pnote, 258 Elf_Word index, 259 Elf_Word* type, 260 char* name, 261 int name_len, 262 void** desc, 263 Elf_Word* descSize ); 264 void elfio_note_add_note( pnote_t pnote, 265 Elf_Word type, 266 const char* name, 267 const void* desc, 268 Elf_Word descSize ); 269 270 //----------------------------------------------------------------------------- 271 // modinfo 272 //----------------------------------------------------------------------------- 273 pmodinfo_t elfio_modinfo_section_accessor_new( psection_t psection ); 274 void elfio_modinfo_section_accessor_delete( pmodinfo_t pmodinfo ); 275 Elf_Word elfio_modinfo_get_attribute_num( pmodinfo_t pmodinfo ); 276 bool elfio_modinfo_get_attribute( pmodinfo_t pmodinfo, 277 Elf_Word no, 278 char* field, 279 int field_len, 280 char* value, 281 int value_len ); 282 bool elfio_modinfo_get_attribute_by_name( pmodinfo_t pmodinfo, 283 char* field_name, 284 char* value, 285 int value_len ); 286 Elf_Word elfio_modinfo_add_attribute( pmodinfo_t pmodinfo, 287 char* field, 288 char* value ); 289 290 //----------------------------------------------------------------------------- 291 // dynamic 292 //----------------------------------------------------------------------------- 293 pdynamic_t elfio_dynamic_section_accessor_new( pelfio_t pelfio, 294 psection_t psection ); 295 void elfio_dynamic_section_accessor_delete( pdynamic_t pdynamic ); 296 Elf_Xword elfio_dynamic_get_entries_num( pdynamic_t pdynamic ); 297 bool elfio_dynamic_get_entry( pdynamic_t pdynamic, 298 Elf_Xword index, 299 Elf_Xword* tag, 300 Elf_Xword* value, 301 char* str, 302 int str_len ); 303 void elfio_dynamic_add_entry( pdynamic_t pdynamic, 304 Elf_Xword tag, 305 Elf_Xword value ); 306 307 //----------------------------------------------------------------------------- 308 // array 309 //----------------------------------------------------------------------------- 310 parray_t elfio_array_section_accessor_new( pelfio_t pelfio, 311 psection_t psection ); 312 void elfio_array_section_accessor_delete( parray_t parray ); 313 Elf_Xword elfio_array_get_entries_num( parray_t parray ); 314 bool elfio_array_get_entry( parray_t parray, 315 Elf_Xword index, 316 Elf64_Addr* paddress ); 317 void elfio_array_add_entry( parray_t parray, Elf64_Addr address ); 318 319 #ifdef __cplusplus 320 } 321 #endif 322 323 #endif // ELFIO_C_WRAPPER_H 324