Skip to content

Commit

Permalink
An attempt to implement memory translation for sections
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1 committed Sep 19, 2021
1 parent 0a15ec0 commit b527ea9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"2919",
"/usr/bin/bash"
],
"sudo" : true,
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
Expand All @@ -70,7 +71,7 @@
"ignoreFailures": true
}
],
"miDebuggerPath": "/usr/bin/gdb"
"miDebuggerPath": "/home/user/ELFIO/mygdb.sh"
}
]
}
6 changes: 4 additions & 2 deletions elfio/elfio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions elfio/elfio_section.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
};
Expand Down
3 changes: 2 additions & 1 deletion elfio/elfio_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit b527ea9

Please sign in to comment.