165042b18Sopenharmony_ci/* 265042b18Sopenharmony_ciCopyright (C) 2001-present by Serge Lamikhov-Center 365042b18Sopenharmony_ci 465042b18Sopenharmony_ciPermission is hereby granted, free of charge, to any person obtaining a copy 565042b18Sopenharmony_ciof this software and associated documentation files (the "Software"), to deal 665042b18Sopenharmony_ciin the Software without restriction, including without limitation the rights 765042b18Sopenharmony_cito use, copy, modify, merge, publish, distribute, sublicense, and/or sell 865042b18Sopenharmony_cicopies of the Software, and to permit persons to whom the Software is 965042b18Sopenharmony_cifurnished to do so, subject to the following conditions: 1065042b18Sopenharmony_ci 1165042b18Sopenharmony_ciThe above copyright notice and this permission notice shall be included in 1265042b18Sopenharmony_ciall copies or substantial portions of the Software. 1365042b18Sopenharmony_ci 1465042b18Sopenharmony_ciTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1565042b18Sopenharmony_ciIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1665042b18Sopenharmony_ciFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1765042b18Sopenharmony_ciAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1865042b18Sopenharmony_ciLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1965042b18Sopenharmony_ciOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 2065042b18Sopenharmony_ciTHE SOFTWARE. 2165042b18Sopenharmony_ci*/ 2265042b18Sopenharmony_ci 2365042b18Sopenharmony_ci#include <stdio.h> 2465042b18Sopenharmony_ci#include <string.h> 2565042b18Sopenharmony_ci 2665042b18Sopenharmony_ci#include <elfio/elf_types.hpp> 2765042b18Sopenharmony_ci#include "elfio_c_wrapper.h" 2865042b18Sopenharmony_ci 2965042b18Sopenharmony_ciint main( int argc, char* argv[] ) 3065042b18Sopenharmony_ci{ 3165042b18Sopenharmony_ci pelfio_t pelfio = elfio_new(); 3265042b18Sopenharmony_ci bool ret; 3365042b18Sopenharmony_ci 3465042b18Sopenharmony_ci if ( argc == 1 ) 3565042b18Sopenharmony_ci ret = elfio_load( pelfio, argv[0] ); 3665042b18Sopenharmony_ci else 3765042b18Sopenharmony_ci ret = elfio_load( pelfio, argv[1] ); 3865042b18Sopenharmony_ci 3965042b18Sopenharmony_ci if ( !ret ) { 4065042b18Sopenharmony_ci printf( "Can't load ELF file\n" ); 4165042b18Sopenharmony_ci return 1; 4265042b18Sopenharmony_ci } 4365042b18Sopenharmony_ci 4465042b18Sopenharmony_ci char msg[128]; 4565042b18Sopenharmony_ci ret = elfio_validate( pelfio, msg, 128 ); 4665042b18Sopenharmony_ci 4765042b18Sopenharmony_ci if ( !ret ) { 4865042b18Sopenharmony_ci printf( "Validation errors:\n" ); 4965042b18Sopenharmony_ci printf( "%s\n", msg ); 5065042b18Sopenharmony_ci return 2; 5165042b18Sopenharmony_ci } 5265042b18Sopenharmony_ci 5365042b18Sopenharmony_ci //----------------------------------------------------------------------------- 5465042b18Sopenharmony_ci // elfio 5565042b18Sopenharmony_ci //----------------------------------------------------------------------------- 5665042b18Sopenharmony_ci printf( "Header size : %d\n", elfio_get_header_size( pelfio ) ); 5765042b18Sopenharmony_ci printf( "Version : %d\n", elfio_get_version( pelfio ) ); 5865042b18Sopenharmony_ci printf( "Section Entry : %d\n", elfio_get_section_entry_size( pelfio ) ); 5965042b18Sopenharmony_ci printf( "Segment Entry : %d\n", elfio_get_segment_entry_size( pelfio ) ); 6065042b18Sopenharmony_ci 6165042b18Sopenharmony_ci /* Uncomment a printf block of the interest */ 6265042b18Sopenharmony_ci 6365042b18Sopenharmony_ci //----------------------------------------------------------------------------- 6465042b18Sopenharmony_ci // section 6565042b18Sopenharmony_ci //----------------------------------------------------------------------------- 6665042b18Sopenharmony_ci int secno = elfio_get_sections_num( pelfio ); 6765042b18Sopenharmony_ci printf( "Sections No : %d\n", secno ); 6865042b18Sopenharmony_ci 6965042b18Sopenharmony_ci for ( int i = 0; i < secno; i++ ) { 7065042b18Sopenharmony_ci psection_t psection = elfio_get_section_by_index( pelfio, i ); 7165042b18Sopenharmony_ci char buff[128]; 7265042b18Sopenharmony_ci elfio_section_get_name( psection, buff, 100 ); 7365042b18Sopenharmony_ci // printf( " [%02d] %s\n", i, buff ); 7465042b18Sopenharmony_ci // printf( " %08lx : %08lx\n", 7565042b18Sopenharmony_ci // elfio_section_get_address( psection ), 7665042b18Sopenharmony_ci // elfio_section_get_size( psection ) ); 7765042b18Sopenharmony_ci } 7865042b18Sopenharmony_ci 7965042b18Sopenharmony_ci //----------------------------------------------------------------------------- 8065042b18Sopenharmony_ci // segment 8165042b18Sopenharmony_ci //----------------------------------------------------------------------------- 8265042b18Sopenharmony_ci int segno = elfio_get_segments_num( pelfio ); 8365042b18Sopenharmony_ci printf( "Segments No : %d\n", segno ); 8465042b18Sopenharmony_ci 8565042b18Sopenharmony_ci for ( int i = 0; i < segno; i++ ) { 8665042b18Sopenharmony_ci psegment_t psegment = elfio_get_segment_by_index( pelfio, i ); 8765042b18Sopenharmony_ci elfio_segment_get_file_size( psegment ); 8865042b18Sopenharmony_ci // printf( " [%02d] %08lx : %08lx : %08lx\n", i, 8965042b18Sopenharmony_ci // elfio_segment_get_virtual_address( psegment ), 9065042b18Sopenharmony_ci // elfio_segment_get_memory_size( psegment ), 9165042b18Sopenharmony_ci // elfio_segment_get_file_size( psegment ) ); 9265042b18Sopenharmony_ci } 9365042b18Sopenharmony_ci 9465042b18Sopenharmony_ci //----------------------------------------------------------------------------- 9565042b18Sopenharmony_ci // symbol 9665042b18Sopenharmony_ci //----------------------------------------------------------------------------- 9765042b18Sopenharmony_ci psection_t psection = elfio_get_section_by_name( pelfio, ".symtab" ); 9865042b18Sopenharmony_ci psymbol_t psymbols = elfio_symbol_section_accessor_new( pelfio, psection ); 9965042b18Sopenharmony_ci Elf_Xword symno = elfio_symbol_get_symbols_num( psymbols ); 10065042b18Sopenharmony_ci for ( int i = 0; i < symno; i++ ) { 10165042b18Sopenharmony_ci char name[128]; 10265042b18Sopenharmony_ci Elf64_Addr value; 10365042b18Sopenharmony_ci Elf_Xword size; 10465042b18Sopenharmony_ci unsigned char bind; 10565042b18Sopenharmony_ci unsigned char type; 10665042b18Sopenharmony_ci Elf_Half section_index; 10765042b18Sopenharmony_ci unsigned char other; 10865042b18Sopenharmony_ci elfio_symbol_get_symbol( psymbols, i, name, 128, &value, &size, &bind, 10965042b18Sopenharmony_ci &type, §ion_index, &other ); 11065042b18Sopenharmony_ci // printf( "[%4d] %10lu, %4lu %s\n", i, value, size, name ); 11165042b18Sopenharmony_ci } 11265042b18Sopenharmony_ci elfio_symbol_section_accessor_delete( psymbols ); 11365042b18Sopenharmony_ci 11465042b18Sopenharmony_ci //----------------------------------------------------------------------------- 11565042b18Sopenharmony_ci // relocation 11665042b18Sopenharmony_ci //----------------------------------------------------------------------------- 11765042b18Sopenharmony_ci psection = elfio_get_section_by_name( pelfio, ".rela.dyn" ); 11865042b18Sopenharmony_ci prelocation_t preloc = 11965042b18Sopenharmony_ci elfio_relocation_section_accessor_new( pelfio, psection ); 12065042b18Sopenharmony_ci Elf_Xword relno = elfio_relocation_get_entries_num( preloc ); 12165042b18Sopenharmony_ci for ( int i = 0; i < relno; i++ ) { 12265042b18Sopenharmony_ci Elf64_Addr offset; 12365042b18Sopenharmony_ci Elf_Word symbol; 12465042b18Sopenharmony_ci Elf_Word type; 12565042b18Sopenharmony_ci Elf_Sxword addend; 12665042b18Sopenharmony_ci elfio_relocation_get_entry( preloc, i, &offset, &symbol, &type, 12765042b18Sopenharmony_ci &addend ); 12865042b18Sopenharmony_ci // printf( "[%4d] %16lx, %08x %08x %16lx\n", i, offset, symbol, type, addend ); 12965042b18Sopenharmony_ci } 13065042b18Sopenharmony_ci elfio_relocation_section_accessor_delete( preloc ); 13165042b18Sopenharmony_ci 13265042b18Sopenharmony_ci //----------------------------------------------------------------------------- 13365042b18Sopenharmony_ci // string 13465042b18Sopenharmony_ci //----------------------------------------------------------------------------- 13565042b18Sopenharmony_ci psection = elfio_get_section_by_name( pelfio, ".strtab" ); 13665042b18Sopenharmony_ci pstring_t pstring = elfio_string_section_accessor_new( psection ); 13765042b18Sopenharmony_ci Elf_Word pos = 0; 13865042b18Sopenharmony_ci const char* str = elfio_string_get_string( pstring, pos ); 13965042b18Sopenharmony_ci while ( str ) { 14065042b18Sopenharmony_ci pos += (Elf_Word)strlen( str ) + 1; 14165042b18Sopenharmony_ci str = elfio_string_get_string( pstring, pos ); 14265042b18Sopenharmony_ci // printf( "%s\n", str ); 14365042b18Sopenharmony_ci } 14465042b18Sopenharmony_ci elfio_string_section_accessor_delete( pstring ); 14565042b18Sopenharmony_ci 14665042b18Sopenharmony_ci //----------------------------------------------------------------------------- 14765042b18Sopenharmony_ci // note 14865042b18Sopenharmony_ci //----------------------------------------------------------------------------- 14965042b18Sopenharmony_ci psection = elfio_get_section_by_name( pelfio, ".note.gnu.build-id" ); 15065042b18Sopenharmony_ci pnote_t pnote = elfio_note_section_accessor_new( pelfio, psection ); 15165042b18Sopenharmony_ci int noteno = elfio_note_get_notes_num( pnote ); 15265042b18Sopenharmony_ci for ( int i = 0; i < noteno; i++ ) { 15365042b18Sopenharmony_ci Elf_Word type; 15465042b18Sopenharmony_ci char name[128]; 15565042b18Sopenharmony_ci int name_len = 128; 15665042b18Sopenharmony_ci char* desc; 15765042b18Sopenharmony_ci Elf_Word descSize = 128; 15865042b18Sopenharmony_ci elfio_note_get_note( pnote, i, &type, name, name_len, (void**)&desc, 15965042b18Sopenharmony_ci &descSize ); 16065042b18Sopenharmony_ci // printf( "[%4d] %s %08x\n", i, name, descSize ); 16165042b18Sopenharmony_ci } 16265042b18Sopenharmony_ci elfio_note_section_accessor_delete( pnote ); 16365042b18Sopenharmony_ci 16465042b18Sopenharmony_ci //----------------------------------------------------------------------------- 16565042b18Sopenharmony_ci // dynamic 16665042b18Sopenharmony_ci //----------------------------------------------------------------------------- 16765042b18Sopenharmony_ci psection = elfio_get_section_by_name( pelfio, ".dynamic" ); 16865042b18Sopenharmony_ci pdynamic_t pdynamic = 16965042b18Sopenharmony_ci elfio_dynamic_section_accessor_new( pelfio, psection ); 17065042b18Sopenharmony_ci Elf_Xword dynno = elfio_dynamic_get_entries_num( pdynamic ); 17165042b18Sopenharmony_ci for ( int i = 0; i < dynno; i++ ) { 17265042b18Sopenharmony_ci Elf_Xword tag; 17365042b18Sopenharmony_ci Elf_Xword value; 17465042b18Sopenharmony_ci char str[128]; 17565042b18Sopenharmony_ci elfio_dynamic_get_entry( pdynamic, i, &tag, &value, str, 128 ); 17665042b18Sopenharmony_ci // printf( "[%4d] %16lx %16lx %s\n", i, tag, value, str ); 17765042b18Sopenharmony_ci } 17865042b18Sopenharmony_ci elfio_dynamic_section_accessor_delete( pdynamic ); 17965042b18Sopenharmony_ci 18065042b18Sopenharmony_ci //----------------------------------------------------------------------------- 18165042b18Sopenharmony_ci // array 18265042b18Sopenharmony_ci //----------------------------------------------------------------------------- 18365042b18Sopenharmony_ci psection = elfio_get_section_by_name( pelfio, ".init_array" ); 18465042b18Sopenharmony_ci if ( psection != 0 ) { 18565042b18Sopenharmony_ci parray_t parray = elfio_array_section_accessor_new( pelfio, psection ); 18665042b18Sopenharmony_ci Elf_Xword arrno = elfio_array_get_entries_num( parray ); 18765042b18Sopenharmony_ci for ( int i = 0; i < arrno; i++ ) { 18865042b18Sopenharmony_ci Elf64_Addr addr; 18965042b18Sopenharmony_ci elfio_array_get_entry( parray, i, &addr ); 19065042b18Sopenharmony_ci // printf( "[%4d] %16lx\n", i, addr ); 19165042b18Sopenharmony_ci } 19265042b18Sopenharmony_ci elfio_array_section_accessor_delete( parray ); 19365042b18Sopenharmony_ci } 19465042b18Sopenharmony_ci 19565042b18Sopenharmony_ci elfio_delete( pelfio ); 19665042b18Sopenharmony_ci 19765042b18Sopenharmony_ci return 0; 19865042b18Sopenharmony_ci} 199