From 6d60be2dfd865b992a347bf51aa75b3dbd6236ca Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Fri, 27 Jul 2012 16:54:26 +0300 Subject: [PATCH] A test program added; str_elf_class output implemented --- elfio/elfio_dump.hpp | 97 +++++++++++++++++++++++++++++++++++++++ elfio/elfio_dump_test.cpp | 27 +++++++++++ 2 files changed, 124 insertions(+) create mode 100644 elfio/elfio_dump_test.cpp diff --git a/elfio/elfio_dump.hpp b/elfio/elfio_dump.hpp index e69de29..ecd1ce4 100644 --- a/elfio/elfio_dump.hpp +++ b/elfio/elfio_dump.hpp @@ -0,0 +1,97 @@ +/* +Copyright (C) 2001-2011 by Serge Lamikhov-Center + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef ELFIO_DUMP_HPP +#define ELFIO_DUMP_HPP + +#include +#include +#include + +namespace ELFIO { +//------------------------------------------------------------------------------ +class dump +{ + public: +//------------------------------------------------------------------------------ + static void + header( std::ostream& out, elfio& reader ) + { + out << "ELF Header\n" << std::endl; + out << " Class: " + << str_elf_class( reader.get_class() ) + << "(" << (int)reader.get_class() << ")" + << std::endl; + out << " Encoding: " + << ( ( ELFDATA2LSB == reader.get_encoding() ) ? "Little endian" : "" ) + << ( ( ELFDATA2MSB == reader.get_encoding() ) ? "Big endian" : "" ) + << ( ( ( ELFDATA2LSB != reader.get_encoding() ) && + ( ELFDATA2MSB != reader.get_encoding() ) ) ? "Unknown" : "" ) + << std::endl; + out << " ELFVersion: " + << ( ( EV_CURRENT == reader.get_elf_version() ) ? "Current" : "Unknown" ) + << "(" << (int)reader.get_elf_version() << ")" + << std::endl; + out << " Type: " << std::hex << reader.get_type() << std::endl; + out << " Machine: " << std::hex << reader.get_machine() << std::endl; + out << " Version: " << std::hex << reader.get_version() << std::endl; + out << " Entry: " << std::hex << reader.get_entry() << std::endl; + out << " Flags: " << std::hex << reader.get_flags() << std::endl; + } + + +//------------------------------------------------------------------------------ + static std::string + str_section_type( Elf_Word type ) + { + } + + static std::string + str_elf_class( Elf_Word type ) + { + struct convert + { + Elf_Word type; + const char* str; + }; + convert converts[] = + { + { ELFCLASS32, "ELF32" }, + { ELFCLASS64, "ELF64" }, + }; + + std::string res = "UNKNOWN"; + for ( unsigned int i = 0; i < sizeof( converts )/sizeof( convert ); ++i ) { + if ( converts[i].type == type ) { + res = converts[i].str; + break; + } + } + + return res; + } + +}; + +} // namespace ELFIO + +#endif // ELFIO_DUMP_HPP diff --git a/elfio/elfio_dump_test.cpp b/elfio/elfio_dump_test.cpp new file mode 100644 index 0000000..8395ce8 --- /dev/null +++ b/elfio/elfio_dump_test.cpp @@ -0,0 +1,27 @@ +#include +#include + +using namespace ELFIO; + +int main( int argc, char** argv ) +{ + if ( argc != 2 ) { + printf( "Usage: ELFDump \n" ); + return 1; + } + + + // Open ELF reader + elfio reader; + + if ( !reader.load( argv[1] ) ) { + printf( "File %s is not found or it is not an ELF file\n", argv[1] ); + return 1; + } + + // Print ELF file header + dump::header( std::cout, reader ); + + + return 0; +}