1/* 2Copyright (C) 2001-present by Serge Lamikhov-Center 3 4Permission is hereby granted, free of charge, to any person obtaining a copy 5of this software and associated documentation files (the "Software"), to deal 6in the Software without restriction, including without limitation the rights 7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8copies of the Software, and to permit persons to whom the Software is 9furnished to do so, subject to the following conditions: 10 11The above copyright notice and this permission notice shall be included in 12all copies or substantial portions of the Software. 13 14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20THE SOFTWARE. 21*/ 22 23#include <iostream> 24#include <elfio/elfio.hpp> 25 26using namespace ELFIO; 27 28int main( int argc, char** argv ) 29{ 30 if ( argc != 2 ) { 31 std::cout << "Usage: tutorial <elf_file>" << std::endl; 32 return 1; 33 } 34 35 // Create an elfio reader 36 elfio reader; 37 38 // Load ELF data 39 if ( !reader.load( argv[1] ) ) { 40 std::cout << "Can't find or process ELF file " << argv[1] << std::endl; 41 return 2; 42 } 43 44 // Print ELF file properties 45 std::cout << "ELF file class : "; 46 if ( reader.get_class() == ELFCLASS32 ) 47 std::cout << "ELF32" << std::endl; 48 else 49 std::cout << "ELF64" << std::endl; 50 51 std::cout << "ELF file encoding : "; 52 if ( reader.get_encoding() == ELFDATA2LSB ) 53 std::cout << "Little endian" << std::endl; 54 else 55 std::cout << "Big endian" << std::endl; 56 57 // Print ELF file sections info 58 Elf_Half sec_num = reader.sections.size(); 59 std::cout << "Number of sections: " << sec_num << std::endl; 60 for ( int i = 0; i < sec_num; ++i ) { 61 section* psec = reader.sections[i]; 62 std::cout << " [" << i << "] " << psec->get_name() << "\t" 63 << psec->get_size() << std::endl; 64 // Access to section's data 65 // const char* p = reader.sections[i]->get_data() 66 } 67 68 // Print ELF file segments info 69 Elf_Half seg_num = reader.segments.size(); 70 std::cout << "Number of segments: " << seg_num << std::endl; 71 for ( int i = 0; i < seg_num; ++i ) { 72 const segment* pseg = reader.segments[i]; 73 std::cout << " [" << i << "] 0x" << std::hex << pseg->get_flags() 74 << "\t0x" << pseg->get_virtual_address() << "\t0x" 75 << pseg->get_file_size() << "\t0x" << pseg->get_memory_size() 76 << std::endl; 77 // Access to segments's data 78 // const char* p = reader.segments[i]->get_data() 79 } 80 81 for ( int i = 0; i < sec_num; ++i ) { 82 section* psec = reader.sections[i]; 83 // Check section type 84 if ( psec->get_type() == SHT_SYMTAB ) { 85 const symbol_section_accessor symbols( reader, psec ); 86 for ( unsigned int j = 0; j < symbols.get_symbols_num(); ++j ) { 87 std::string name; 88 Elf64_Addr value; 89 Elf_Xword size; 90 unsigned char bind; 91 unsigned char type; 92 Elf_Half section_index; 93 unsigned char other; 94 95 // Read symbol properties 96 symbols.get_symbol( j, name, value, size, bind, type, 97 section_index, other ); 98 std::cout << j << " " << name << " " << value << std::endl; 99 } 100 } 101 } 102 103 return 0; 104} 105