EnvironmentLoader/source/elfio/elfio_relocation.hpp

461 lines
17 KiB
C++
Raw Normal View History

2021-12-25 20:14:56 +01:00
/*
2023-01-05 16:26:11 +01:00
Copyright (C) 2001-present by Serge Lamikhov-Center
2021-12-25 20:14:56 +01:00
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_RELOCATION_HPP
#define ELFIO_RELOCATION_HPP
namespace ELFIO {
2023-01-05 16:26:11 +01:00
template <typename T> struct get_sym_and_type;
template <> struct get_sym_and_type<Elf32_Rel>
2021-12-25 20:14:56 +01:00
{
static int get_r_sym( Elf_Xword info )
{
return ELF32_R_SYM( (Elf_Word)info );
}
static int get_r_type( Elf_Xword info )
{
return ELF32_R_TYPE( (Elf_Word)info );
}
};
2023-01-05 16:26:11 +01:00
template <> struct get_sym_and_type<Elf32_Rela>
2021-12-25 20:14:56 +01:00
{
static int get_r_sym( Elf_Xword info )
{
return ELF32_R_SYM( (Elf_Word)info );
}
static int get_r_type( Elf_Xword info )
{
return ELF32_R_TYPE( (Elf_Word)info );
}
};
2023-01-05 16:26:11 +01:00
template <> struct get_sym_and_type<Elf64_Rel>
2021-12-25 20:14:56 +01:00
{
2023-01-05 16:26:11 +01:00
static int get_r_sym( Elf_Xword info ) { return ELF64_R_SYM( info ); }
static int get_r_type( Elf_Xword info ) { return ELF64_R_TYPE( info ); }
2021-12-25 20:14:56 +01:00
};
2023-01-05 16:26:11 +01:00
template <> struct get_sym_and_type<Elf64_Rela>
2021-12-25 20:14:56 +01:00
{
2023-01-05 16:26:11 +01:00
static int get_r_sym( Elf_Xword info ) { return ELF64_R_SYM( info ); }
static int get_r_type( Elf_Xword info ) { return ELF64_R_TYPE( info ); }
2021-12-25 20:14:56 +01:00
};
//------------------------------------------------------------------------------
2023-01-05 16:26:11 +01:00
template <class S> class relocation_section_accessor_template
2021-12-25 20:14:56 +01:00
{
public:
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
explicit relocation_section_accessor_template( const elfio& elf_file,
S* section )
: elf_file( elf_file ), relocation_section( section )
2021-12-25 20:14:56 +01:00
{
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
Elf_Xword get_entries_num() const
2021-12-25 20:14:56 +01:00
{
Elf_Xword nRet = 0;
if ( 0 != relocation_section->get_entry_size() ) {
2023-01-05 16:26:11 +01:00
nRet = relocation_section->get_size() /
relocation_section->get_entry_size();
2021-12-25 20:14:56 +01:00
}
return nRet;
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
bool get_entry( Elf_Xword index,
Elf64_Addr& offset,
Elf_Word& symbol,
unsigned& type,
Elf_Sxword& addend ) const
2021-12-25 20:14:56 +01:00
{
2023-01-05 16:26:11 +01:00
if ( index >= get_entries_num() ) { // Is index valid
2021-12-25 20:14:56 +01:00
return false;
}
if ( elf_file.get_class() == ELFCLASS32 ) {
if ( SHT_REL == relocation_section->get_type() ) {
2023-01-05 16:26:11 +01:00
generic_get_entry_rel<Elf32_Rel>( index, offset, symbol, type,
addend );
2021-12-25 20:14:56 +01:00
}
else if ( SHT_RELA == relocation_section->get_type() ) {
2023-01-05 16:26:11 +01:00
generic_get_entry_rela<Elf32_Rela>( index, offset, symbol, type,
addend );
2021-12-25 20:14:56 +01:00
}
}
else {
if ( SHT_REL == relocation_section->get_type() ) {
2023-01-05 16:26:11 +01:00
generic_get_entry_rel<Elf64_Rel>( index, offset, symbol, type,
addend );
2021-12-25 20:14:56 +01:00
}
else if ( SHT_RELA == relocation_section->get_type() ) {
2023-01-05 16:26:11 +01:00
generic_get_entry_rela<Elf64_Rela>( index, offset, symbol, type,
addend );
2021-12-25 20:14:56 +01:00
}
}
return true;
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
bool get_entry( Elf_Xword index,
Elf64_Addr& offset,
Elf64_Addr& symbolValue,
std::string& symbolName,
unsigned& type,
Elf_Sxword& addend,
Elf_Sxword& calcValue ) const
2021-12-25 20:14:56 +01:00
{
// Do regular job
2023-01-05 16:26:11 +01:00
Elf_Word symbol = 0;
bool ret = get_entry( index, offset, symbol, type, addend );
2021-12-25 20:14:56 +01:00
// Find the symbol
Elf_Xword size;
unsigned char bind;
unsigned char symbolType;
2023-01-05 16:26:11 +01:00
Elf_Half section;
2021-12-25 20:14:56 +01:00
unsigned char other;
2023-01-05 16:26:11 +01:00
symbol_section_accessor symbols(
elf_file, elf_file.sections[get_symbol_table_index()] );
ret = ret && symbols.get_symbol( symbol, symbolName, symbolValue, size,
bind, symbolType, section, other );
if ( ret ) { // Was it successful?
switch ( type ) {
case R_386_NONE: // none
calcValue = 0;
break;
case R_386_32: // S + A
calcValue = symbolValue + addend;
break;
case R_386_PC32: // S + A - P
calcValue = symbolValue + addend - offset;
break;
case R_386_GOT32: // G + A - P
calcValue = 0;
break;
case R_386_PLT32: // L + A - P
calcValue = 0;
break;
case R_386_COPY: // none
calcValue = 0;
break;
case R_386_GLOB_DAT: // S
case R_386_JMP_SLOT: // S
calcValue = symbolValue;
break;
case R_386_RELATIVE: // B + A
calcValue = addend;
break;
case R_386_GOTOFF: // S + A - GOT
calcValue = 0;
break;
case R_386_GOTPC: // GOT + A - P
calcValue = 0;
break;
default: // Not recognized symbol!
calcValue = 0;
break;
}
}
2021-12-25 20:14:56 +01:00
return ret;
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
bool set_entry( Elf_Xword index,
Elf64_Addr offset,
Elf_Word symbol,
unsigned type,
Elf_Sxword addend )
{
if ( index >= get_entries_num() ) { // Is index valid
return false;
}
if ( elf_file.get_class() == ELFCLASS32 ) {
if ( SHT_REL == relocation_section->get_type() ) {
generic_set_entry_rel<Elf32_Rel>( index, offset, symbol, type,
addend );
}
else if ( SHT_RELA == relocation_section->get_type() ) {
generic_set_entry_rela<Elf32_Rela>( index, offset, symbol, type,
addend );
}
}
else {
if ( SHT_REL == relocation_section->get_type() ) {
generic_set_entry_rel<Elf64_Rel>( index, offset, symbol, type,
addend );
}
else if ( SHT_RELA == relocation_section->get_type() ) {
generic_set_entry_rela<Elf64_Rela>( index, offset, symbol, type,
addend );
}
}
return true;
}
//------------------------------------------------------------------------------
void add_entry( Elf64_Addr offset, Elf_Xword info )
2021-12-25 20:14:56 +01:00
{
if ( elf_file.get_class() == ELFCLASS32 ) {
2023-01-05 16:26:11 +01:00
generic_add_entry<Elf32_Rel>( offset, info );
2021-12-25 20:14:56 +01:00
}
else {
2023-01-05 16:26:11 +01:00
generic_add_entry<Elf64_Rel>( offset, info );
2021-12-25 20:14:56 +01:00
}
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
void add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned type )
2021-12-25 20:14:56 +01:00
{
Elf_Xword info;
if ( elf_file.get_class() == ELFCLASS32 ) {
info = ELF32_R_INFO( (Elf_Xword)symbol, type );
}
else {
2023-01-05 16:26:11 +01:00
info = ELF64_R_INFO( (Elf_Xword)symbol, type );
2021-12-25 20:14:56 +01:00
}
add_entry( offset, info );
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
void add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend )
2021-12-25 20:14:56 +01:00
{
if ( elf_file.get_class() == ELFCLASS32 ) {
2023-01-05 16:26:11 +01:00
generic_add_entry<Elf32_Rela>( offset, info, addend );
2021-12-25 20:14:56 +01:00
}
else {
2023-01-05 16:26:11 +01:00
generic_add_entry<Elf64_Rela>( offset, info, addend );
2021-12-25 20:14:56 +01:00
}
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
void add_entry( Elf64_Addr offset,
Elf_Word symbol,
unsigned type,
Elf_Sxword addend )
2021-12-25 20:14:56 +01:00
{
Elf_Xword info;
if ( elf_file.get_class() == ELFCLASS32 ) {
info = ELF32_R_INFO( (Elf_Xword)symbol, type );
}
else {
info = ELF64_R_INFO( (Elf_Xword)symbol, type );
}
add_entry( offset, info, addend );
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
void add_entry( string_section_accessor str_writer,
const char* str,
symbol_section_accessor sym_writer,
Elf64_Addr value,
Elf_Word size,
unsigned char sym_info,
unsigned char other,
Elf_Half shndx,
Elf64_Addr offset,
unsigned type )
2021-12-25 20:14:56 +01:00
{
Elf_Word str_index = str_writer.add_string( str );
Elf_Word sym_index = sym_writer.add_symbol( str_index, value, size,
2023-01-05 16:26:11 +01:00
sym_info, other, shndx );
2021-12-25 20:14:56 +01:00
add_entry( offset, sym_index, type );
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
void swap_symbols( Elf_Xword first, Elf_Xword second )
{
Elf64_Addr offset = 0;
Elf_Word symbol = 0;
unsigned rtype = 0;
Elf_Sxword addend = 0;
for ( Elf_Word i = 0; i < get_entries_num(); i++ ) {
get_entry( i, offset, symbol, rtype, addend );
if ( symbol == first ) {
set_entry( i, offset, (Elf_Word)second, rtype, addend );
}
if ( symbol == second ) {
set_entry( i, offset, (Elf_Word)first, rtype, addend );
}
}
}
//------------------------------------------------------------------------------
2021-12-25 20:14:56 +01:00
private:
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
Elf_Half get_symbol_table_index() const
2021-12-25 20:14:56 +01:00
{
return (Elf_Half)relocation_section->get_link();
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
template <class T>
void generic_get_entry_rel( Elf_Xword index,
Elf64_Addr& offset,
Elf_Word& symbol,
unsigned& type,
Elf_Sxword& addend ) const
2021-12-25 20:14:56 +01:00
{
const endianess_convertor& convertor = elf_file.get_convertor();
const T* pEntry = reinterpret_cast<const T*>(
2023-01-05 16:26:11 +01:00
relocation_section->get_data() +
index * relocation_section->get_entry_size() );
2021-12-25 20:14:56 +01:00
offset = convertor( pEntry->r_offset );
Elf_Xword tmp = convertor( pEntry->r_info );
symbol = get_sym_and_type<T>::get_r_sym( tmp );
type = get_sym_and_type<T>::get_r_type( tmp );
addend = 0;
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
template <class T>
void generic_get_entry_rela( Elf_Xword index,
Elf64_Addr& offset,
Elf_Word& symbol,
unsigned& type,
Elf_Sxword& addend ) const
2021-12-25 20:14:56 +01:00
{
const endianess_convertor& convertor = elf_file.get_convertor();
const T* pEntry = reinterpret_cast<const T*>(
2023-01-05 16:26:11 +01:00
relocation_section->get_data() +
index * relocation_section->get_entry_size() );
2021-12-25 20:14:56 +01:00
offset = convertor( pEntry->r_offset );
Elf_Xword tmp = convertor( pEntry->r_info );
symbol = get_sym_and_type<T>::get_r_sym( tmp );
type = get_sym_and_type<T>::get_r_type( tmp );
addend = convertor( pEntry->r_addend );
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
template <class T>
void generic_set_entry_rel( Elf_Xword index,
Elf64_Addr offset,
Elf_Word symbol,
unsigned type,
Elf_Sxword )
{
const endianess_convertor& convertor = elf_file.get_convertor();
T* pEntry = const_cast<T*>( reinterpret_cast<const T*>(
relocation_section->get_data() +
index * relocation_section->get_entry_size() ) );
if ( elf_file.get_class() == ELFCLASS32 ) {
pEntry->r_info = ELF32_R_INFO( (Elf_Xword)symbol, type );
}
else {
pEntry->r_info = ELF64_R_INFO( (Elf_Xword)symbol, type );
}
pEntry->r_offset = decltype( pEntry->r_offset )( offset );
pEntry->r_offset = convertor( pEntry->r_offset );
pEntry->r_info = convertor( pEntry->r_info );
}
//------------------------------------------------------------------------------
template <class T>
void generic_set_entry_rela( Elf_Xword index,
Elf64_Addr offset,
Elf_Word symbol,
unsigned type,
Elf_Sxword addend )
{
const endianess_convertor& convertor = elf_file.get_convertor();
T* pEntry = const_cast<T*>( reinterpret_cast<const T*>(
relocation_section->get_data() +
index * relocation_section->get_entry_size() ) );
if ( elf_file.get_class() == ELFCLASS32 ) {
pEntry->r_info = ELF32_R_INFO( (Elf_Xword)symbol, type );
}
else {
pEntry->r_info = ELF64_R_INFO( (Elf_Xword)symbol, type );
}
pEntry->r_offset = decltype( pEntry->r_offset )( offset );
pEntry->r_addend = decltype( pEntry->r_addend )( addend );
pEntry->r_offset = convertor( pEntry->r_offset );
pEntry->r_info = convertor( pEntry->r_info );
pEntry->r_addend = convertor( pEntry->r_addend );
}
//------------------------------------------------------------------------------
template <class T>
void generic_add_entry( Elf64_Addr offset, Elf_Xword info )
2021-12-25 20:14:56 +01:00
{
const endianess_convertor& convertor = elf_file.get_convertor();
T entry;
2023-01-05 16:26:11 +01:00
entry.r_offset = decltype( entry.r_offset )( offset );
entry.r_info = decltype( entry.r_info )( info );
2021-12-25 20:14:56 +01:00
entry.r_offset = convertor( entry.r_offset );
entry.r_info = convertor( entry.r_info );
2023-01-05 16:26:11 +01:00
relocation_section->append_data( reinterpret_cast<char*>( &entry ),
sizeof( entry ) );
2021-12-25 20:14:56 +01:00
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
template <class T>
2021-12-25 20:14:56 +01:00
void
generic_add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend )
{
const endianess_convertor& convertor = elf_file.get_convertor();
T entry;
entry.r_offset = offset;
entry.r_info = info;
entry.r_addend = addend;
entry.r_offset = convertor( entry.r_offset );
entry.r_info = convertor( entry.r_info );
entry.r_addend = convertor( entry.r_addend );
2023-01-05 16:26:11 +01:00
relocation_section->append_data( reinterpret_cast<char*>( &entry ),
sizeof( entry ) );
2021-12-25 20:14:56 +01:00
}
2023-01-05 16:26:11 +01:00
//------------------------------------------------------------------------------
2021-12-25 20:14:56 +01:00
private:
const elfio& elf_file;
2023-01-05 16:26:11 +01:00
S* relocation_section = nullptr;
2021-12-25 20:14:56 +01:00
};
2023-01-05 16:26:11 +01:00
using relocation_section_accessor =
relocation_section_accessor_template<section>;
using const_relocation_section_accessor =
relocation_section_accessor_template<const section>;
2021-12-25 20:14:56 +01:00
} // namespace ELFIO
#endif // ELFIO_RELOCATION_HPP