diff --git a/elfio/elfio_dump.hpp b/elfio/elfio_dump.hpp index 96c42a1..ef9eb30 100644 --- a/elfio/elfio_dump.hpp +++ b/elfio/elfio_dump.hpp @@ -23,6 +23,7 @@ THE SOFTWARE. #ifndef ELFIO_DUMP_HPP #define ELFIO_DUMP_HPP +#include #include #include #include @@ -424,6 +425,8 @@ class dump std::hex << std::left public: + static const ELFIO::Elf_Xword MAX_DATA_ENTRIES = 64; + //------------------------------------------------------------------------------ static void header( std::ostream& out, const elfio& reader ) @@ -763,6 +766,107 @@ class dump } out << std::endl; } + +//------------------------------------------------------------------------------ + static void + section_data( std::ostream& out, const section* sec ) + { + std::ios_base::fmtflags original_flags = out.flags(); + + out << sec->get_name() << std::endl; + const char* pdata = sec->get_data(); + ELFIO::Elf_Xword i; + for ( i = 0; i < std::min( sec->get_size(), MAX_DATA_ENTRIES ); ++i ) { + if ( i % 16 == 0 ) { + out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]"; + } + + out << " " << DUMP_HEX_FORMAT( 2 ) << ( pdata[i] & 0x000000FF ); + + if ( i % 16 == 15 ) { + out << std::endl; + } + } + if ( i % 16 != 0 ) { + out << std::endl; + } + + out.flags(original_flags); + + return; + } + +//------------------------------------------------------------------------------ + static void + section_datas( std::ostream& out, const elfio& reader ) + { + Elf_Half n = reader.sections.size(); + + if ( n == 0 ) { + return; + } + + out << "Section Data:" << std::endl; + + for ( Elf_Half i = 1; i < n; ++i ) { // For all sections + section* sec = reader.sections[i]; + if ( sec->get_type() == SHT_NOBITS ) { + continue; + } + section_data( out, sec ); + } + + out << std::endl; + } + +//------------------------------------------------------------------------------ + static void + segment_data( std::ostream& out, Elf_Half no, const segment* seg ) + { + std::ios_base::fmtflags original_flags = out.flags(); + + out << "Segment # " << no << std::endl; + const char* pdata = seg->get_data(); + ELFIO::Elf_Xword i; + for ( i = 0; i < std::min( seg->get_file_size(), MAX_DATA_ENTRIES ); ++i ) { + if ( i % 16 == 0 ) { + out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]"; + } + + out << " " << DUMP_HEX_FORMAT( 2 ) << ( pdata[i] & 0x000000FF ); + + if ( i % 16 == 15 ) { + out << std::endl; + } + } + if ( i % 16 != 0 ) { + out << std::endl; + } + + out.flags(original_flags); + + return; + } + +//------------------------------------------------------------------------------ + static void + segment_datas( std::ostream& out, const elfio& reader ) + { + Elf_Half n = reader.segments.size(); + + if ( n == 0 ) { + return; + } + + out << "Segment Data:" << std::endl; + + for ( Elf_Half i = 0; i < n; ++i ) { // For all sections + segment* seg = reader.segments[i]; + segment_data( out, i, seg ); + } + + out << std::endl; + } private: //------------------------------------------------------------------------------ diff --git a/examples/elfdump/elfdump.cpp b/examples/elfdump/elfdump.cpp index 1c6eb50..e74e599 100644 --- a/examples/elfdump/elfdump.cpp +++ b/examples/elfdump/elfdump.cpp @@ -52,6 +52,8 @@ int main( int argc, char** argv ) dump::symbol_tables ( std::cout, reader ); dump::notes ( std::cout, reader ); dump::dynamic_tags ( std::cout, reader ); + dump::section_datas ( std::cout, reader ); + dump::segment_datas ( std::cout, reader ); return 0; }