• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef _MSC_VER
24 #define _CRT_SECURE_NO_WARNINGS
25 #endif
26 
27 #include <elfio/elfio.hpp>
28 #include <cstring>
29 
30 using namespace ELFIO;
31 
32 #include "elfio_c_wrapper.h"
33 
34 //-----------------------------------------------------------------------------
35 // elfio
36 //-----------------------------------------------------------------------------
elfio_new()37 pelfio_t elfio_new() { return new elfio; }
38 
elfio_delete(pelfio_t pelfio)39 void elfio_delete( pelfio_t pelfio ) { delete (elfio*)pelfio; }
40 
elfio_create(pelfio_t pelfio,unsigned char file_class,unsigned char encoding)41 void elfio_create( pelfio_t      pelfio,
42                    unsigned char file_class,
43                    unsigned char encoding )
44 {
45     pelfio->create( file_class, encoding );
46 }
47 
elfio_load(pelfio_t pelfio,const char * file_name)48 bool elfio_load( pelfio_t pelfio, const char* file_name )
49 {
50     return pelfio->load( file_name );
51 }
52 
elfio_save(pelfio_t pelfio,const char * file_name)53 bool elfio_save( pelfio_t pelfio, const char* file_name )
54 {
55     return pelfio->save( file_name );
56 }
57 
58 ELFIO_C_HEADER_ACCESS_GET_IMPL( unsigned char, class );
59 ELFIO_C_HEADER_ACCESS_GET_IMPL( unsigned char, elf_version );
60 ELFIO_C_HEADER_ACCESS_GET_IMPL( unsigned char, encoding );
61 ELFIO_C_HEADER_ACCESS_GET_IMPL( Elf_Word, version );
62 ELFIO_C_HEADER_ACCESS_GET_IMPL( Elf_Half, header_size );
63 ELFIO_C_HEADER_ACCESS_GET_IMPL( Elf_Half, section_entry_size );
64 ELFIO_C_HEADER_ACCESS_GET_IMPL( Elf_Half, segment_entry_size );
65 
66 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( unsigned char, os_abi );
67 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( unsigned char, abi_version );
68 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( Elf_Half, type );
69 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( Elf_Half, machine );
70 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( Elf_Word, flags );
71 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( Elf64_Addr, entry );
72 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( Elf64_Off, sections_offset );
73 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( Elf64_Off, segments_offset );
74 ELFIO_C_HEADER_ACCESS_GET_SET_IMPL( Elf_Half, section_name_str_index );
75 
elfio_get_sections_num(pelfio_t pelfio)76 Elf_Half elfio_get_sections_num( pelfio_t pelfio )
77 {
78     return pelfio->sections.size();
79 }
80 
elfio_get_section_by_index(pelfio_t pelfio,int index)81 psection_t elfio_get_section_by_index( pelfio_t pelfio, int index )
82 {
83     return pelfio->sections[index];
84 }
85 
elfio_get_section_by_name(pelfio_t pelfio,char * name)86 psection_t elfio_get_section_by_name( pelfio_t pelfio, char* name )
87 {
88     return pelfio->sections[name];
89 }
90 
elfio_add_section(pelfio_t pelfio,char * name)91 psection_t elfio_add_section( pelfio_t pelfio, char* name )
92 {
93     return pelfio->sections.add( name );
94 }
95 
elfio_get_segments_num(pelfio_t pelfio)96 Elf_Half elfio_get_segments_num( pelfio_t pelfio )
97 {
98     return pelfio->segments.size();
99 }
100 
elfio_get_segment_by_index(pelfio_t pelfio,int index)101 psegment_t elfio_get_segment_by_index( pelfio_t pelfio, int index )
102 {
103     return pelfio->segments[index];
104 }
105 
elfio_add_segment(pelfio_t pelfio)106 psegment_t elfio_add_segment( pelfio_t pelfio )
107 {
108     return pelfio->segments.add();
109 }
110 
elfio_validate(pelfio_t pelfio,char * msg,int msg_len)111 bool elfio_validate( pelfio_t pelfio, char* msg, int msg_len )
112 {
113     std::string error = pelfio->validate();
114 
115     if ( msg != nullptr && msg_len > 0 ) {
116         strncpy( msg, error.c_str(), (size_t)msg_len - 1 );
117     }
118 
119     return error.empty();
120 }
121 
122 //-----------------------------------------------------------------------------
123 // section
124 //-----------------------------------------------------------------------------
125 ELFIO_C_GET_ACCESS_IMPL( section, Elf_Half, index );
126 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf_Word, type );
127 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf_Xword, flags );
128 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf_Word, info );
129 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf_Word, link );
130 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf_Xword, addr_align );
131 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf_Xword, entry_size );
132 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf64_Addr, address );
133 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf_Xword, size );
134 ELFIO_C_GET_SET_ACCESS_IMPL( section, Elf_Word, name_string_offset );
135 ELFIO_C_GET_ACCESS_IMPL( section, Elf64_Off, offset );
136 
elfio_section_get_name(psection_t psection,char * buffer,int len)137 void elfio_section_get_name( psection_t psection, char* buffer, int len )
138 {
139     strncpy( buffer, psection->get_name().c_str(), (size_t)len - 1 );
140 }
141 
elfio_section_set_name(psection_t psection,char * buffer)142 void elfio_section_set_name( psection_t psection, char* buffer )
143 {
144     psection->set_name( buffer );
145 }
146 
elfio_section_get_data(psection_t psection)147 char* elfio_section_get_data( psection_t psection )
148 {
149     return (char*)psection->get_data();
150 }
151 
elfio_section_set_data(psection_t psection,const char * pData,Elf_Word size)152 void elfio_section_set_data( psection_t  psection,
153                              const char* pData,
154                              Elf_Word    size )
155 {
156     psection->set_data( pData, size );
157 }
158 
elfio_section_append_data(psection_t psection,const char * pData,Elf_Word size)159 void elfio_section_append_data( psection_t  psection,
160                                 const char* pData,
161                                 Elf_Word    size )
162 {
163     psection->append_data( pData, size );
164 }
165 
166 //-----------------------------------------------------------------------------
167 // segment
168 //-----------------------------------------------------------------------------
169 ELFIO_C_GET_ACCESS_IMPL( segment, Elf_Half, index );
170 ELFIO_C_GET_SET_ACCESS_IMPL( segment, Elf_Word, type );
171 ELFIO_C_GET_SET_ACCESS_IMPL( segment, Elf_Word, flags );
172 ELFIO_C_GET_SET_ACCESS_IMPL( segment, Elf_Xword, align );
173 ELFIO_C_GET_SET_ACCESS_IMPL( segment, Elf_Xword, memory_size );
174 ELFIO_C_GET_SET_ACCESS_IMPL( segment, Elf64_Addr, virtual_address );
175 ELFIO_C_GET_SET_ACCESS_IMPL( segment, Elf64_Addr, physical_address );
176 ELFIO_C_GET_SET_ACCESS_IMPL( segment, Elf_Xword, file_size );
177 ELFIO_C_GET_ACCESS_IMPL( segment, Elf64_Off, offset );
178 
elfio_segment_get_data(psegment_t psegment)179 char* elfio_segment_get_data( psegment_t psegment )
180 {
181     return (char*)psegment->get_data();
182 }
183 
elfio_segment_add_section_index(psegment_t psegment,Elf_Half index,Elf_Xword addr_align)184 Elf_Half elfio_segment_add_section_index( psegment_t psegment,
185                                           Elf_Half   index,
186                                           Elf_Xword  addr_align )
187 {
188     return psegment->add_section_index( index, addr_align );
189 }
190 
elfio_segment_get_sections_num(psegment_t psegment)191 Elf_Half elfio_segment_get_sections_num( psegment_t psegment )
192 {
193     return psegment->get_sections_num();
194 }
195 
elfio_segment_get_section_index_at(psegment_t psegment,Elf_Half num)196 Elf_Half elfio_segment_get_section_index_at( psegment_t psegment, Elf_Half num )
197 {
198     return psegment->get_section_index_at( num );
199 }
200 
elfio_segment_is_offset_initialized(psegment_t psegment)201 bool elfio_segment_is_offset_initialized( psegment_t psegment )
202 {
203     return psegment->is_offset_initialized();
204 }
205 
206 //-----------------------------------------------------------------------------
207 // symbol
208 //-----------------------------------------------------------------------------
elfio_symbol_section_accessor_new(pelfio_t pelfio,psection_t psection)209 psymbol_t elfio_symbol_section_accessor_new( pelfio_t   pelfio,
210                                              psection_t psection )
211 {
212     return new symbol_section_accessor( *pelfio, psection );
213 }
214 
elfio_symbol_section_accessor_delete(psymbol_t psymbol)215 void elfio_symbol_section_accessor_delete( psymbol_t psymbol )
216 {
217     delete psymbol;
218 }
219 
elfio_symbol_get_symbols_num(psymbol_t psymbol)220 Elf_Xword elfio_symbol_get_symbols_num( psymbol_t psymbol )
221 {
222     return psymbol->get_symbols_num();
223 }
224 
elfio_symbol_get_symbol(psymbol_t psymbol,Elf_Xword index,char * name,int name_len,Elf64_Addr * value,Elf_Xword * size,unsigned char * bind,unsigned char * type,Elf_Half * section_index,unsigned char * other)225 bool elfio_symbol_get_symbol( psymbol_t      psymbol,
226                               Elf_Xword      index,
227                               char*          name,
228                               int            name_len,
229                               Elf64_Addr*    value,
230                               Elf_Xword*     size,
231                               unsigned char* bind,
232                               unsigned char* type,
233                               Elf_Half*      section_index,
234                               unsigned char* other )
235 {
236     std::string name_param;
237     bool ret = psymbol->get_symbol( index, name_param, *value, *size, *bind,
238                                     *type, *section_index, *other );
239     strncpy( name, name_param.c_str(), (size_t)name_len - 1 );
240 
241     return ret;
242 }
243 
elfio_symbol_add_symbol(psymbol_t psymbol,Elf_Word name,Elf64_Addr value,Elf_Xword size,unsigned char info,unsigned char other,Elf_Half shndx)244 Elf_Word elfio_symbol_add_symbol( psymbol_t     psymbol,
245                                   Elf_Word      name,
246                                   Elf64_Addr    value,
247                                   Elf_Xword     size,
248                                   unsigned char info,
249                                   unsigned char other,
250                                   Elf_Half      shndx )
251 {
252     return psymbol->add_symbol( name, value, size, info, other, shndx );
253 }
254 
elfio_symbol_arrange_local_symbols(psymbol_t psymbol,void (* func)(Elf_Xword first,Elf_Xword second))255 Elf_Xword elfio_symbol_arrange_local_symbols(
256     psymbol_t psymbol, void ( *func )( Elf_Xword first, Elf_Xword second ) )
257 {
258     return psymbol->arrange_local_symbols( func );
259 }
260 
261 //-----------------------------------------------------------------------------
262 // relocation
263 //-----------------------------------------------------------------------------
elfio_relocation_section_accessor_new(pelfio_t pelfio,psection_t psection)264 prelocation_t elfio_relocation_section_accessor_new( pelfio_t   pelfio,
265                                                      psection_t psection )
266 {
267     return new relocation_section_accessor( *pelfio, psection );
268 }
269 
elfio_relocation_section_accessor_delete(prelocation_t prelocation)270 void elfio_relocation_section_accessor_delete( prelocation_t prelocation )
271 {
272     delete prelocation;
273 }
274 
elfio_relocation_get_entries_num(prelocation_t prelocation)275 Elf_Xword elfio_relocation_get_entries_num( prelocation_t prelocation )
276 {
277     return prelocation->get_entries_num();
278 }
279 
elfio_relocation_get_entry(prelocation_t prelocation,Elf_Xword index,Elf64_Addr * offset,Elf_Word * symbol,Elf_Word * type,Elf_Sxword * addend)280 bool elfio_relocation_get_entry( prelocation_t prelocation,
281                                  Elf_Xword     index,
282                                  Elf64_Addr*   offset,
283                                  Elf_Word*     symbol,
284                                  Elf_Word*     type,
285                                  Elf_Sxword*   addend )
286 {
287     return prelocation->get_entry( index, *offset, *symbol, *type, *addend );
288 }
289 
elfio_relocation_set_entry(prelocation_t prelocation,Elf_Xword index,Elf64_Addr offset,Elf_Word symbol,Elf_Word type,Elf_Sxword addend)290 bool elfio_relocation_set_entry( prelocation_t prelocation,
291                                  Elf_Xword     index,
292                                  Elf64_Addr    offset,
293                                  Elf_Word      symbol,
294                                  Elf_Word      type,
295                                  Elf_Sxword    addend )
296 {
297     return prelocation->set_entry( index, offset, symbol, type, addend );
298 }
299 
elfio_relocation_add_entry(prelocation_t prelocation,Elf64_Addr offset,Elf_Word symbol,unsigned char type,Elf_Sxword addend)300 void elfio_relocation_add_entry( prelocation_t prelocation,
301                                  Elf64_Addr    offset,
302                                  Elf_Word      symbol,
303                                  unsigned char type,
304                                  Elf_Sxword    addend )
305 {
306     return prelocation->add_entry( offset, symbol, type, addend );
307 }
308 
elfio_relocation_swap_symbols(prelocation_t prelocation,Elf_Xword first,Elf_Xword second)309 void elfio_relocation_swap_symbols( prelocation_t prelocation,
310                                     Elf_Xword     first,
311                                     Elf_Xword     second )
312 {
313     prelocation->swap_symbols( first, second );
314 }
315 
316 //-----------------------------------------------------------------------------
317 // string
318 //-----------------------------------------------------------------------------
elfio_string_section_accessor_new(psection_t psection)319 pstring_t elfio_string_section_accessor_new( psection_t psection )
320 {
321     return new string_section_accessor( psection );
322 }
323 
elfio_string_section_accessor_delete(pstring_t pstring)324 void elfio_string_section_accessor_delete( pstring_t pstring )
325 {
326     delete pstring;
327 }
328 
elfio_string_get_string(pstring_t pstring,Elf_Word index)329 const char* elfio_string_get_string( pstring_t pstring, Elf_Word index )
330 {
331     return pstring->get_string( index );
332 }
333 
elfio_string_add_string(pstring_t pstring,char * str)334 Elf_Word elfio_string_add_string( pstring_t pstring, char* str )
335 {
336     return pstring->add_string( str );
337 }
338 
339 //-----------------------------------------------------------------------------
340 // note
341 //-----------------------------------------------------------------------------
elfio_note_section_accessor_new(pelfio_t pelfio,psection_t psection)342 pnote_t elfio_note_section_accessor_new( pelfio_t pelfio, psection_t psection )
343 {
344     return new note_section_accessor( *pelfio, psection );
345 }
346 
elfio_note_section_accessor_delete(pnote_t pnote)347 void elfio_note_section_accessor_delete( pnote_t pnote ) { delete pnote; }
348 
elfio_note_get_notes_num(pnote_t pnote)349 Elf_Word elfio_note_get_notes_num( pnote_t pnote )
350 {
351     return pnote->get_notes_num();
352 }
353 
elfio_note_get_note(pnote_t pnote,Elf_Word index,Elf_Word * type,char * name,int name_len,void ** desc,Elf_Word * descSize)354 bool elfio_note_get_note( pnote_t   pnote,
355                           Elf_Word  index,
356                           Elf_Word* type,
357                           char*     name,
358                           int       name_len,
359                           void**    desc,
360                           Elf_Word* descSize )
361 {
362     std::string name_str;
363     bool ret = pnote->get_note( index, *type, name_str, *desc, *descSize );
364     strncpy( name, name_str.c_str(), (size_t)name_len - 1 );
365 
366     return ret;
367 }
368 
elfio_note_add_note(pnote_t pnote,Elf_Word type,const char * name,const void * desc,Elf_Word descSize)369 void elfio_note_add_note( pnote_t     pnote,
370                           Elf_Word    type,
371                           const char* name,
372                           const void* desc,
373                           Elf_Word    descSize )
374 {
375     pnote->add_note( type, name, desc, descSize );
376 }
377 
378 //-----------------------------------------------------------------------------
379 // modinfo
380 //-----------------------------------------------------------------------------
elfio_modinfo_section_accessor_new(psection_t psection)381 pmodinfo_t elfio_modinfo_section_accessor_new( psection_t psection )
382 {
383     return new modinfo_section_accessor( psection );
384 }
385 
elfio_modinfo_section_accessor_delete(pmodinfo_t pmodinfo)386 void elfio_modinfo_section_accessor_delete( pmodinfo_t pmodinfo )
387 {
388     delete pmodinfo;
389 }
390 
elfio_modinfo_get_attribute_num(pmodinfo_t pmodinfo)391 Elf_Word elfio_modinfo_get_attribute_num( pmodinfo_t pmodinfo )
392 {
393     return pmodinfo->get_attribute_num();
394 }
395 
elfio_modinfo_get_attribute(pmodinfo_t pmodinfo,Elf_Word no,char * field,int field_len,char * value,int value_len)396 bool elfio_modinfo_get_attribute( pmodinfo_t pmodinfo,
397                                   Elf_Word   no,
398                                   char*      field,
399                                   int        field_len,
400                                   char*      value,
401                                   int        value_len )
402 {
403     std::string field_str;
404     std::string value_str;
405     bool        ret = pmodinfo->get_attribute( no, field_str, value_str );
406     strncpy( field, field_str.c_str(), (size_t)field_len - 1 );
407     strncpy( value, value_str.c_str(), (size_t)value_len - 1 );
408 
409     return ret;
410 }
411 
elfio_modinfo_get_attribute_by_name(pmodinfo_t pmodinfo,char * field_name,char * value,int value_len)412 bool elfio_modinfo_get_attribute_by_name( pmodinfo_t pmodinfo,
413                                           char*      field_name,
414                                           char*      value,
415                                           int        value_len )
416 {
417     std::string value_str;
418     bool        ret = pmodinfo->get_attribute( value_str, value_str );
419     strncpy( value, value_str.c_str(), (size_t)value_len - 1 );
420 
421     return ret;
422 }
423 
424 Elf_Word
elfio_modinfo_add_attribute(pmodinfo_t pmodinfo,char * field,char * value)425 elfio_modinfo_add_attribute( pmodinfo_t pmodinfo, char* field, char* value )
426 {
427     return pmodinfo->add_attribute( field, value );
428 }
429 
430 //-----------------------------------------------------------------------------
431 // dynamic
432 //-----------------------------------------------------------------------------
elfio_dynamic_section_accessor_new(pelfio_t pelfio,psection_t psection)433 pdynamic_t elfio_dynamic_section_accessor_new( pelfio_t   pelfio,
434                                                psection_t psection )
435 {
436     return new dynamic_section_accessor( *pelfio, psection );
437 }
438 
elfio_dynamic_section_accessor_delete(pdynamic_t pdynamic)439 void elfio_dynamic_section_accessor_delete( pdynamic_t pdynamic )
440 {
441     delete pdynamic;
442 }
443 
elfio_dynamic_get_entries_num(pdynamic_t pdynamic)444 Elf_Xword elfio_dynamic_get_entries_num( pdynamic_t pdynamic )
445 {
446     return pdynamic->get_entries_num();
447 }
448 
elfio_dynamic_get_entry(pdynamic_t pdynamic,Elf_Xword index,Elf_Xword * tag,Elf_Xword * value,char * str,int str_len)449 bool elfio_dynamic_get_entry( pdynamic_t pdynamic,
450                               Elf_Xword  index,
451                               Elf_Xword* tag,
452                               Elf_Xword* value,
453                               char*      str,
454                               int        str_len )
455 {
456     std::string str_str;
457     bool        ret = pdynamic->get_entry( index, *tag, *value, str_str );
458     strncpy( str, str_str.c_str(), (size_t)str_len - 1 );
459 
460     return ret;
461 }
462 
elfio_dynamic_add_entry(pdynamic_t pdynamic,Elf_Xword tag,Elf_Xword value)463 void elfio_dynamic_add_entry( pdynamic_t pdynamic,
464                               Elf_Xword  tag,
465                               Elf_Xword  value )
466 {
467     pdynamic->add_entry( tag, value );
468 }
469 
470 //-----------------------------------------------------------------------------
471 // array
472 //-----------------------------------------------------------------------------
elfio_array_section_accessor_new(pelfio_t pelfio,psection_t psection)473 parray_t elfio_array_section_accessor_new( pelfio_t   pelfio,
474                                            psection_t psection )
475 {
476     return new array_section_accessor( *pelfio, psection );
477 }
478 
elfio_array_section_accessor_delete(parray_t parray)479 void elfio_array_section_accessor_delete( parray_t parray ) { delete parray; }
480 
elfio_array_get_entries_num(parray_t parray)481 Elf_Xword elfio_array_get_entries_num( parray_t parray )
482 {
483     return parray->get_entries_num();
484 }
485 
elfio_array_get_entry(parray_t parray,Elf_Xword index,Elf64_Addr * paddress)486 bool elfio_array_get_entry( parray_t    parray,
487                             Elf_Xword   index,
488                             Elf64_Addr* paddress )
489 {
490     bool ret = parray->get_entry( index, *paddress );
491 
492     return ret;
493 }
494 
elfio_array_add_entry(parray_t parray,Elf64_Addr address)495 void elfio_array_add_entry( parray_t parray, Elf64_Addr address )
496 {
497     parray->add_entry( address );
498 }
499