From b527ea992a3c79e530086bb48bed7c94d3d008a8 Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Sun, 19 Sep 2021 08:02:58 +0300 Subject: [PATCH] An attempt to implement memory translation for sections --- .vscode/launch.json | 3 ++- elfio/elfio.hpp | 6 ++++-- elfio/elfio_section.hpp | 15 +++++++++------ elfio/elfio_utils.hpp | 3 ++- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index d870f507..abb9789b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -58,6 +58,7 @@ "2919", "/usr/bin/bash" ], + "sudo" : true, "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], @@ -70,7 +71,7 @@ "ignoreFailures": true } ], - "miDebuggerPath": "/usr/bin/gdb" + "miDebuggerPath": "/home/user/ELFIO/mygdb.sh" } ] } \ No newline at end of file diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index d983ea5d..5d380654 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -421,10 +421,12 @@ class elfio unsigned char file_class = get_class(); if ( file_class == ELFCLASS64 ) { - new_section = new section_impl( &convertor ); + new_section = + new section_impl( &convertor, &addr_translator ); } else if ( file_class == ELFCLASS32 ) { - new_section = new section_impl( &convertor ); + new_section = + new section_impl( &convertor, &addr_translator ); } else { return nullptr; diff --git a/elfio/elfio_section.hpp b/elfio/elfio_section.hpp index ced712f1..2097e0bd 100644 --- a/elfio/elfio_section.hpp +++ b/elfio/elfio_section.hpp @@ -72,8 +72,9 @@ template class section_impl : public section { public: //------------------------------------------------------------------------------ - section_impl( const endianess_convertor* convertor ) - : convertor( convertor ) + section_impl( const endianess_convertor* convertor, + const address_translator* translator ) + : convertor( convertor ), translator( translator ) { std::fill_n( reinterpret_cast( &header ), sizeof( header ), '\0' ); @@ -192,10 +193,10 @@ template class section_impl : public section std::fill_n( reinterpret_cast( &header ), sizeof( header ), '\0' ); - stream.seekg( 0, stream.end ); - set_stream_size( stream.tellg() ); + // stream.seekg( 0, stream.end ); + set_stream_size( 0xFFFFFFFF /*stream.tellg()*/ ); - stream.seekg( header_offset ); + stream.seekg( ( *translator )( header_offset ) ); stream.read( reinterpret_cast( &header ), sizeof( header ) ); Elf_Xword size = get_size(); @@ -204,7 +205,8 @@ template class section_impl : public section data = new ( std::nothrow ) char[size + 1]; if ( ( 0 != size ) && ( nullptr != data ) ) { - stream.seekg( ( *convertor )( header.sh_offset ) ); + stream.seekg( + ( *translator )( ( *convertor )( header.sh_offset ) ) ); stream.read( data, size ); data[size] = 0; // Ensure data is ended with 0 to avoid oob read data_size = size; @@ -263,6 +265,7 @@ template class section_impl : public section char* data; Elf_Word data_size; const endianess_convertor* convertor; + const address_translator* translator; bool is_address_set; size_t stream_size; }; diff --git a/elfio/elfio_utils.hpp b/elfio/elfio_utils.hpp index bb788587..287a058b 100644 --- a/elfio/elfio_utils.hpp +++ b/elfio/elfio_utils.hpp @@ -190,7 +190,8 @@ class address_translator for ( auto& t : translation ) { if ( t.map_to <= value && ( ( value - t.map_to ) < ( t.end - t.start ) ) ) { - std::cout << std::hex << t.start - t.map_to + value << std::endl; + std::cout << std::hex << t.start - t.map_to + value << " " + << value << std::endl; return t.start - t.map_to + value; } }