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 <iostream>
2465042b18Sopenharmony_ci#include <elfio/elfio.hpp>
2565042b18Sopenharmony_ci
2665042b18Sopenharmony_ciusing namespace ELFIO;
2765042b18Sopenharmony_ci
2865042b18Sopenharmony_ciint main( int argc, char** argv )
2965042b18Sopenharmony_ci{
3065042b18Sopenharmony_ci    if ( argc != 2 ) {
3165042b18Sopenharmony_ci        std::cout << "Usage: tutorial <elf_file>" << std::endl;
3265042b18Sopenharmony_ci        return 1;
3365042b18Sopenharmony_ci    }
3465042b18Sopenharmony_ci
3565042b18Sopenharmony_ci    // Create an elfio reader
3665042b18Sopenharmony_ci    elfio reader;
3765042b18Sopenharmony_ci
3865042b18Sopenharmony_ci    // Load ELF data
3965042b18Sopenharmony_ci    if ( !reader.load( argv[1] ) ) {
4065042b18Sopenharmony_ci        std::cout << "Can't find or process ELF file " << argv[1] << std::endl;
4165042b18Sopenharmony_ci        return 2;
4265042b18Sopenharmony_ci    }
4365042b18Sopenharmony_ci
4465042b18Sopenharmony_ci    // Print ELF file properties
4565042b18Sopenharmony_ci    std::cout << "ELF file class    : ";
4665042b18Sopenharmony_ci    if ( reader.get_class() == ELFCLASS32 )
4765042b18Sopenharmony_ci        std::cout << "ELF32" << std::endl;
4865042b18Sopenharmony_ci    else
4965042b18Sopenharmony_ci        std::cout << "ELF64" << std::endl;
5065042b18Sopenharmony_ci
5165042b18Sopenharmony_ci    std::cout << "ELF file encoding : ";
5265042b18Sopenharmony_ci    if ( reader.get_encoding() == ELFDATA2LSB )
5365042b18Sopenharmony_ci        std::cout << "Little endian" << std::endl;
5465042b18Sopenharmony_ci    else
5565042b18Sopenharmony_ci        std::cout << "Big endian" << std::endl;
5665042b18Sopenharmony_ci
5765042b18Sopenharmony_ci    // Print ELF file sections info
5865042b18Sopenharmony_ci    Elf_Half sec_num = reader.sections.size();
5965042b18Sopenharmony_ci    std::cout << "Number of sections: " << sec_num << std::endl;
6065042b18Sopenharmony_ci    for ( int i = 0; i < sec_num; ++i ) {
6165042b18Sopenharmony_ci        section* psec = reader.sections[i];
6265042b18Sopenharmony_ci        std::cout << "  [" << i << "] " << psec->get_name() << "\t"
6365042b18Sopenharmony_ci                  << psec->get_size() << std::endl;
6465042b18Sopenharmony_ci        // Access to section's data
6565042b18Sopenharmony_ci        // const char* p = reader.sections[i]->get_data()
6665042b18Sopenharmony_ci    }
6765042b18Sopenharmony_ci
6865042b18Sopenharmony_ci    // Print ELF file segments info
6965042b18Sopenharmony_ci    Elf_Half seg_num = reader.segments.size();
7065042b18Sopenharmony_ci    std::cout << "Number of segments: " << seg_num << std::endl;
7165042b18Sopenharmony_ci    for ( int i = 0; i < seg_num; ++i ) {
7265042b18Sopenharmony_ci        const segment* pseg = reader.segments[i];
7365042b18Sopenharmony_ci        std::cout << "  [" << i << "] 0x" << std::hex << pseg->get_flags()
7465042b18Sopenharmony_ci                  << "\t0x" << pseg->get_virtual_address() << "\t0x"
7565042b18Sopenharmony_ci                  << pseg->get_file_size() << "\t0x" << pseg->get_memory_size()
7665042b18Sopenharmony_ci                  << std::endl;
7765042b18Sopenharmony_ci        // Access to segments's data
7865042b18Sopenharmony_ci        // const char* p = reader.segments[i]->get_data()
7965042b18Sopenharmony_ci    }
8065042b18Sopenharmony_ci
8165042b18Sopenharmony_ci    for ( int i = 0; i < sec_num; ++i ) {
8265042b18Sopenharmony_ci        section* psec = reader.sections[i];
8365042b18Sopenharmony_ci        // Check section type
8465042b18Sopenharmony_ci        if ( psec->get_type() == SHT_SYMTAB ) {
8565042b18Sopenharmony_ci            const symbol_section_accessor symbols( reader, psec );
8665042b18Sopenharmony_ci            for ( unsigned int j = 0; j < symbols.get_symbols_num(); ++j ) {
8765042b18Sopenharmony_ci                std::string   name;
8865042b18Sopenharmony_ci                Elf64_Addr    value;
8965042b18Sopenharmony_ci                Elf_Xword     size;
9065042b18Sopenharmony_ci                unsigned char bind;
9165042b18Sopenharmony_ci                unsigned char type;
9265042b18Sopenharmony_ci                Elf_Half      section_index;
9365042b18Sopenharmony_ci                unsigned char other;
9465042b18Sopenharmony_ci
9565042b18Sopenharmony_ci                // Read symbol properties
9665042b18Sopenharmony_ci                symbols.get_symbol( j, name, value, size, bind, type,
9765042b18Sopenharmony_ci                                    section_index, other );
9865042b18Sopenharmony_ci                std::cout << j << " " << name << " " << value << std::endl;
9965042b18Sopenharmony_ci            }
10065042b18Sopenharmony_ci        }
10165042b18Sopenharmony_ci    }
10265042b18Sopenharmony_ci
10365042b18Sopenharmony_ci    return 0;
10465042b18Sopenharmony_ci}
105