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 #include <iostream>
24 #include <elfio/elfio.hpp>
25
26 using namespace ELFIO;
27
main(int argc,char ** argv)28 int 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