An attempt to implement memory translation for sections

This commit is contained in:
Serge Lamikhov-Center 2021-09-19 08:02:58 +03:00
parent 0a15ec0aea
commit b527ea992a
4 changed files with 17 additions and 10 deletions

3
.vscode/launch.json vendored
View File

@ -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"
}
]
}

View File

@ -421,10 +421,12 @@ class elfio
unsigned char file_class = get_class();
if ( file_class == ELFCLASS64 ) {
new_section = new section_impl<Elf64_Shdr>( &convertor );
new_section =
new section_impl<Elf64_Shdr>( &convertor, &addr_translator );
}
else if ( file_class == ELFCLASS32 ) {
new_section = new section_impl<Elf32_Shdr>( &convertor );
new_section =
new section_impl<Elf32_Shdr>( &convertor, &addr_translator );
}
else {
return nullptr;

View File

@ -72,8 +72,9 @@ template <class T> 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<char*>( &header ), sizeof( header ),
'\0' );
@ -192,10 +193,10 @@ template <class T> class section_impl : public section
std::fill_n( reinterpret_cast<char*>( &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<char*>( &header ), sizeof( header ) );
Elf_Xword size = get_size();
@ -204,7 +205,8 @@ template <class T> 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 T> 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;
};

View File

@ -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;
}
}