mirror of
https://github.com/serge1/ELFIO.git
synced 2025-01-30 06:32:43 +00:00
Reformat sources by using clang-format
It is not perfect (and, probably, it is worser than it was before), but, it is automatic
This commit is contained in:
parent
46acd5c16d
commit
9c739b49a0
@ -1,27 +1,34 @@
|
||||
{
|
||||
BasedOnStyle : LLVM,
|
||||
BasedOnStyle : LLVM
|
||||
|
||||
AlignConsecutiveAssignments : true,
|
||||
AlignConsecutiveDeclarations : true,
|
||||
AlignConsecutiveMacros : true,
|
||||
AlignEscapedNewlines : true,
|
||||
AlignOperands : true,
|
||||
AlignTrailingComments : true,
|
||||
|
||||
UseTab : Never,
|
||||
IndentWidth : 4,
|
||||
TabWidth : 4,
|
||||
BreakBeforeBraces : Stroustrup,
|
||||
AllowShortIfStatementsOnASingleLine : false,
|
||||
IndentCaseLabels : false,
|
||||
ColumnLimit : 90,
|
||||
AccessModifierOffset : -4,
|
||||
NamespaceIndentation : Inner,
|
||||
PointerAlignment : Left,
|
||||
FixNamespaceComments : true,
|
||||
SortIncludes : false,
|
||||
|
||||
ReflowComments : false,
|
||||
SpacesInConditionalStatement : true,
|
||||
SpacesInParentheses : true,
|
||||
}
|
||||
AccessModifierOffset : -2
|
||||
AlignAfterOpenBracket : true
|
||||
AlignConsecutiveAssignments : true
|
||||
AlignConsecutiveDeclarations : true
|
||||
AlignConsecutiveMacros : true
|
||||
AlignEscapedNewlines : true
|
||||
AlignOperands : true
|
||||
AlignTrailingComments : true
|
||||
BinPackParameters : false
|
||||
BraceWrapping:
|
||||
AfterCaseLabel : true
|
||||
AfterClass : true
|
||||
AfterEnum : true
|
||||
AfterFunction : true
|
||||
AfterNamespace : false
|
||||
AfterStruct : true
|
||||
AfterExternBlock : true
|
||||
BeforeCatch : true
|
||||
BeforeElse : true
|
||||
BreakBeforeBraces : Custom
|
||||
ColumnLimit : 80
|
||||
FixNamespaceComments : true
|
||||
IndentCaseLabels : false
|
||||
IndentWidth : 4
|
||||
NamespaceIndentation : Inner
|
||||
PointerAlignment : Left
|
||||
ReflowComments : false
|
||||
SortIncludes : false
|
||||
SpacesInConditionalStatement : true
|
||||
SpacesInParentheses : true
|
||||
TabWidth : 4
|
||||
UseTab : Never
|
||||
|
@ -80,7 +80,6 @@ typedef uint64_t Elf64_Off;
|
||||
#define ET_LOPROC 0xFF00
|
||||
#define ET_HIPROC 0xFFFF
|
||||
|
||||
|
||||
#define EM_NONE 0 // No machine
|
||||
#define EM_M32 1 // AT&T WE 32100
|
||||
#define EM_SPARC 2 // SUN SPARC
|
||||
@ -91,7 +90,8 @@ typedef uint64_t Elf64_Off;
|
||||
#define EM_860 7 // Intel 80860
|
||||
#define EM_MIPS 8 // MIPS R3000 (officially, big-endian only)
|
||||
#define EM_S370 9 // IBM System/370
|
||||
#define EM_MIPS_RS3_LE 10 // MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated
|
||||
#define EM_MIPS_RS3_LE \
|
||||
10 // MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated
|
||||
#define EM_res011 11 // Reserved
|
||||
#define EM_res012 12 // Reserved
|
||||
#define EM_res013 13 // Reserved
|
||||
@ -192,7 +192,8 @@ typedef uint64_t Elf64_Off;
|
||||
#define EM_SE_C33 107 // S1C33 Family of Seiko Epson processors
|
||||
#define EM_SEP 108 // Sharp embedded microprocessor
|
||||
#define EM_ARCA 109 // Arca RISC Microprocessor
|
||||
#define EM_UNICORE 110 // Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University
|
||||
#define EM_UNICORE \
|
||||
110 // Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University
|
||||
#define EM_EXCESS 111 // eXcess: 16/32/64-bit configurable embedded CPU
|
||||
#define EM_DXP 112 // Icera Semiconductor Inc. Deep Execution Processor
|
||||
#define EM_ALTERA_NIOS2 113 // Altera Nios II soft-core processor
|
||||
@ -200,7 +201,8 @@ typedef uint64_t Elf64_Off;
|
||||
#define EM_XGATE 115 // Motorola XGATE embedded processor
|
||||
#define EM_C166 116 // Infineon C16x/XC16x processor
|
||||
#define EM_M16C 117 // Renesas M16C series microprocessors
|
||||
#define EM_DSPIC30F 118 // Microchip Technology dsPIC30F Digital Signal Controller
|
||||
#define EM_DSPIC30F \
|
||||
118 // Microchip Technology dsPIC30F Digital Signal Controller
|
||||
#define EM_CE 119 // Freescale Communication Engine RISC core
|
||||
#define EM_M32C 120 // Renesas M32C series microprocessors
|
||||
#define EM_res121 121 // Reserved
|
||||
@ -250,7 +252,8 @@ typedef uint64_t Elf64_Off;
|
||||
#define EM_QDSP6 164 // QUALCOMM DSP6 Processor
|
||||
#define EM_8051 165 // Intel 8051 and variants
|
||||
#define EM_STXP7X 166 // STMicroelectronics STxP7x family
|
||||
#define EM_NDS32 167 // Andes Technology compact code size embedded RISC processor family
|
||||
#define EM_NDS32 \
|
||||
167 // Andes Technology compact code size embedded RISC processor family
|
||||
#define EM_ECOG1 168 // Cyan Technology eCOG1X family
|
||||
#define EM_ECOG1X 168 // Cyan Technology eCOG1X family
|
||||
#define EM_MAXQ30 169 // Dallas Semiconductor MAXQ30 Core Micro-controllers
|
||||
@ -369,17 +372,20 @@ typedef uint64_t Elf64_Off;
|
||||
#define ELFOSABI_AROS 15 // Amiga Research OS
|
||||
#define ELFOSABI_FENIXOS 16 // The FenixOS highly scalable multi-core OS
|
||||
// 64-255 Architecture-specific value range
|
||||
#define ELFOSABI_AMDGPU_HSA 64 // AMDGPU OS for HSA compatible compute
|
||||
#define ELFOSABI_AMDGPU_HSA \
|
||||
64 // AMDGPU OS for HSA compatible compute \
|
||||
// kernels.
|
||||
#define ELFOSABI_AMDGPU_PAL 65 // AMDGPU OS for AMD PAL compatible graphics
|
||||
#define ELFOSABI_AMDGPU_PAL \
|
||||
65 // AMDGPU OS for AMD PAL compatible graphics \
|
||||
// shaders and compute kernels.
|
||||
#define ELFOSABI_AMDGPU_MESA3D 66 // AMDGPU OS for Mesa3D compatible graphics
|
||||
#define ELFOSABI_AMDGPU_MESA3D \
|
||||
66 // AMDGPU OS for Mesa3D compatible graphics \
|
||||
// shaders and compute kernels.
|
||||
|
||||
|
||||
// AMDGPU specific e_flags
|
||||
#define EF_AMDGPU_MACH 0x0ff // AMDGPU processor selection mask.
|
||||
#define EF_AMDGPU_XNACK 0x100 // Indicates if the XNACK target feature is
|
||||
#define EF_AMDGPU_XNACK \
|
||||
0x100 // Indicates if the XNACK target feature is \
|
||||
// enabled for all code contained in the ELF.
|
||||
// AMDGPU processors
|
||||
#define EF_AMDGPU_MACH_NONE 0x000 // Unspecified processor.
|
||||
@ -685,9 +691,9 @@ typedef uint64_t Elf64_Off;
|
||||
#define DF_BIND_NOW 0x8
|
||||
#define DF_STATIC_TLS 0x10
|
||||
|
||||
|
||||
// ELF file header
|
||||
struct Elf32_Ehdr {
|
||||
struct Elf32_Ehdr
|
||||
{
|
||||
unsigned char e_ident[EI_NIDENT];
|
||||
Elf_Half e_type;
|
||||
Elf_Half e_machine;
|
||||
@ -704,7 +710,8 @@ struct Elf32_Ehdr {
|
||||
Elf_Half e_shstrndx;
|
||||
};
|
||||
|
||||
struct Elf64_Ehdr {
|
||||
struct Elf64_Ehdr
|
||||
{
|
||||
unsigned char e_ident[EI_NIDENT];
|
||||
Elf_Half e_type;
|
||||
Elf_Half e_machine;
|
||||
@ -721,9 +728,9 @@ struct Elf64_Ehdr {
|
||||
Elf_Half e_shstrndx;
|
||||
};
|
||||
|
||||
|
||||
// Section header
|
||||
struct Elf32_Shdr {
|
||||
struct Elf32_Shdr
|
||||
{
|
||||
Elf_Word sh_name;
|
||||
Elf_Word sh_type;
|
||||
Elf_Word sh_flags;
|
||||
@ -736,7 +743,8 @@ struct Elf32_Shdr {
|
||||
Elf_Word sh_entsize;
|
||||
};
|
||||
|
||||
struct Elf64_Shdr {
|
||||
struct Elf64_Shdr
|
||||
{
|
||||
Elf_Word sh_name;
|
||||
Elf_Word sh_type;
|
||||
Elf_Xword sh_flags;
|
||||
@ -749,9 +757,9 @@ struct Elf64_Shdr {
|
||||
Elf_Xword sh_entsize;
|
||||
};
|
||||
|
||||
|
||||
// Segment header
|
||||
struct Elf32_Phdr {
|
||||
struct Elf32_Phdr
|
||||
{
|
||||
Elf_Word p_type;
|
||||
Elf32_Off p_offset;
|
||||
Elf32_Addr p_vaddr;
|
||||
@ -762,7 +770,8 @@ struct Elf32_Phdr {
|
||||
Elf_Word p_align;
|
||||
};
|
||||
|
||||
struct Elf64_Phdr {
|
||||
struct Elf64_Phdr
|
||||
{
|
||||
Elf_Word p_type;
|
||||
Elf_Word p_flags;
|
||||
Elf64_Off p_offset;
|
||||
@ -773,9 +782,9 @@ struct Elf64_Phdr {
|
||||
Elf_Xword p_align;
|
||||
};
|
||||
|
||||
|
||||
// Symbol table entry
|
||||
struct Elf32_Sym {
|
||||
struct Elf32_Sym
|
||||
{
|
||||
Elf_Word st_name;
|
||||
Elf32_Addr st_value;
|
||||
Elf_Word st_size;
|
||||
@ -784,7 +793,8 @@ struct Elf32_Sym {
|
||||
Elf_Half st_shndx;
|
||||
};
|
||||
|
||||
struct Elf64_Sym {
|
||||
struct Elf64_Sym
|
||||
{
|
||||
Elf_Word st_name;
|
||||
unsigned char st_info;
|
||||
unsigned char st_other;
|
||||
@ -793,48 +803,51 @@ struct Elf64_Sym {
|
||||
Elf_Xword st_size;
|
||||
};
|
||||
|
||||
|
||||
#define ELF_ST_BIND( i ) ( ( i ) >> 4 )
|
||||
#define ELF_ST_TYPE( i ) ( (i)&0xf )
|
||||
#define ELF_ST_INFO( b, t ) ( ( ( b ) << 4 ) + ( (t)&0xf ) )
|
||||
|
||||
#define ELF_ST_VISIBILITY( o ) ( (o)&0x3 )
|
||||
|
||||
|
||||
// Relocation entries
|
||||
struct Elf32_Rel {
|
||||
struct Elf32_Rel
|
||||
{
|
||||
Elf32_Addr r_offset;
|
||||
Elf_Word r_info;
|
||||
};
|
||||
|
||||
struct Elf32_Rela {
|
||||
struct Elf32_Rela
|
||||
{
|
||||
Elf32_Addr r_offset;
|
||||
Elf_Word r_info;
|
||||
Elf_Sword r_addend;
|
||||
};
|
||||
|
||||
struct Elf64_Rel {
|
||||
struct Elf64_Rel
|
||||
{
|
||||
Elf64_Addr r_offset;
|
||||
Elf_Xword r_info;
|
||||
};
|
||||
|
||||
struct Elf64_Rela {
|
||||
struct Elf64_Rela
|
||||
{
|
||||
Elf64_Addr r_offset;
|
||||
Elf_Xword r_info;
|
||||
Elf_Sxword r_addend;
|
||||
};
|
||||
|
||||
|
||||
#define ELF32_R_SYM( i ) ( ( i ) >> 8 )
|
||||
#define ELF32_R_TYPE( i ) ( (unsigned char)( i ) )
|
||||
#define ELF32_R_INFO( s, t ) ( ( ( s ) << 8 ) + (unsigned char)( t ) )
|
||||
|
||||
#define ELF64_R_SYM( i ) ( ( i ) >> 32 )
|
||||
#define ELF64_R_TYPE( i ) ( (i)&0xffffffffL )
|
||||
#define ELF64_R_INFO(s,t) ((((int64_t)(s))<<32)+((t)&0xffffffffL))
|
||||
#define ELF64_R_INFO( s, t ) \
|
||||
( ( ( ( int64_t )( s ) ) << 32 ) + ( (t)&0xffffffffL ) )
|
||||
|
||||
// Dynamic structure
|
||||
struct Elf32_Dyn {
|
||||
struct Elf32_Dyn
|
||||
{
|
||||
Elf_Sword d_tag;
|
||||
union {
|
||||
Elf_Word d_val;
|
||||
@ -842,7 +855,8 @@ struct Elf32_Dyn {
|
||||
} d_un;
|
||||
};
|
||||
|
||||
struct Elf64_Dyn {
|
||||
struct Elf64_Dyn
|
||||
{
|
||||
Elf_Sxword d_tag;
|
||||
union {
|
||||
Elf_Xword d_val;
|
||||
|
243
elfio/elfio.hpp
243
elfio/elfio.hpp
@ -47,25 +47,19 @@ THE SOFTWARE.
|
||||
#include <elfio/elfio_strings.hpp>
|
||||
|
||||
#define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \
|
||||
TYPE \
|
||||
get_##FNAME() const \
|
||||
{ \
|
||||
return header ? (header->get_##FNAME()) : 0; \
|
||||
}
|
||||
TYPE get_##FNAME() const { return header ? ( header->get_##FNAME() ) : 0; }
|
||||
|
||||
#define ELFIO_HEADER_ACCESS_GET_SET( TYPE, FNAME ) \
|
||||
TYPE \
|
||||
get_##FNAME() const \
|
||||
TYPE get_##FNAME() const \
|
||||
{ \
|
||||
return header ? ( header->get_##FNAME() ) : 0; \
|
||||
} \
|
||||
void \
|
||||
set_##FNAME( TYPE val ) \
|
||||
void set_##FNAME( TYPE val ) \
|
||||
{ \
|
||||
if ( header ) { \
|
||||
header->set_##FNAME( val ); \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
namespace ELFIO {
|
||||
|
||||
@ -82,10 +76,7 @@ class elfio
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
~elfio()
|
||||
{
|
||||
clean();
|
||||
}
|
||||
~elfio() { clean(); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void create( unsigned char file_class, unsigned char encoding )
|
||||
@ -119,10 +110,8 @@ class elfio
|
||||
|
||||
// Is it ELF file?
|
||||
if ( stream.gcount() != sizeof( e_ident ) ||
|
||||
e_ident[EI_MAG0] != ELFMAG0 ||
|
||||
e_ident[EI_MAG1] != ELFMAG1 ||
|
||||
e_ident[EI_MAG2] != ELFMAG2 ||
|
||||
e_ident[EI_MAG3] != ELFMAG3 ) {
|
||||
e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 ||
|
||||
e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -170,13 +159,15 @@ class elfio
|
||||
// The position of the section table is variable and needs to be fixed
|
||||
// before saving.
|
||||
header->set_segments_num( segments.size() );
|
||||
header->set_segments_offset( segments.size() ? header->get_header_size() : 0 );
|
||||
header->set_segments_offset( segments.size() ? header->get_header_size()
|
||||
: 0 );
|
||||
header->set_sections_num( sections.size() );
|
||||
header->set_sections_offset( 0 );
|
||||
|
||||
// Layout the first section right after the segment table
|
||||
current_file_pos = header->get_header_size() +
|
||||
header->get_segment_entry_size() * (Elf_Xword)header->get_segments_num();
|
||||
header->get_segment_entry_size() *
|
||||
(Elf_Xword)header->get_segments_num();
|
||||
|
||||
calc_segment_alignment();
|
||||
|
||||
@ -212,10 +203,7 @@ class elfio
|
||||
ELFIO_HEADER_ACCESS_GET_SET( Elf_Half, section_name_str_index );
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
const endianess_convertor& get_convertor() const
|
||||
{
|
||||
return convertor;
|
||||
}
|
||||
const endianess_convertor& get_convertor() const { return convertor; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Xword get_default_entry_size( Elf_Word section_type ) const
|
||||
@ -256,33 +244,36 @@ class elfio
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
bool is_offset_in_section( Elf64_Off offset, const section* sec ) const {
|
||||
return offset >= sec->get_offset() && offset < sec->get_offset()+sec->get_size();
|
||||
bool is_offset_in_section( Elf64_Off offset, const section* sec ) const
|
||||
{
|
||||
return ( offset >= sec->get_offset() ) &&
|
||||
( offset < ( sec->get_offset() + sec->get_size() ) );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
public:
|
||||
|
||||
//! returns an empty string if no problems are detected,
|
||||
//! or a string containing an error message if problems are found
|
||||
std::string validate() const {
|
||||
std::string validate() const
|
||||
{
|
||||
|
||||
// check for overlapping sections in the file
|
||||
for ( int i = 0; i < sections.size(); ++i ) {
|
||||
for ( int j = i + 1; j < sections.size(); ++j ) {
|
||||
const section* a = sections[i];
|
||||
const section* b = sections[j];
|
||||
if ( !(a->get_type() & SHT_NOBITS)
|
||||
&& !(b->get_type() & SHT_NOBITS)
|
||||
&& (a->get_size() > 0)
|
||||
&& (b->get_size() > 0)
|
||||
&& (a->get_offset() > 0)
|
||||
&& (b->get_offset() > 0)) {
|
||||
if ( is_offset_in_section( a->get_offset(), b )
|
||||
|| is_offset_in_section( a->get_offset()+a->get_size()-1, b )
|
||||
|| is_offset_in_section( b->get_offset(), a )
|
||||
|| is_offset_in_section( b->get_offset()+b->get_size()-1, a )) {
|
||||
return "Sections " + a->get_name() + " and " + b->get_name() + " overlap in file";
|
||||
if ( !( a->get_type() & SHT_NOBITS ) &&
|
||||
!( b->get_type() & SHT_NOBITS ) && ( a->get_size() > 0 ) &&
|
||||
( b->get_size() > 0 ) && ( a->get_offset() > 0 ) &&
|
||||
( b->get_offset() > 0 ) ) {
|
||||
if ( is_offset_in_section( a->get_offset(), b ) ||
|
||||
is_offset_in_section(
|
||||
a->get_offset() + a->get_size() - 1, b ) ||
|
||||
is_offset_in_section( b->get_offset(), a ) ||
|
||||
is_offset_in_section(
|
||||
b->get_offset() + b->get_size() - 1, a ) ) {
|
||||
return "Sections " + a->get_name() + " and " +
|
||||
b->get_name() + " overlap in file";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -315,17 +306,18 @@ class elfio
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
elf_header* create_header( unsigned char file_class, unsigned char encoding )
|
||||
elf_header* create_header( unsigned char file_class,
|
||||
unsigned char encoding )
|
||||
{
|
||||
elf_header* new_header = 0;
|
||||
|
||||
if ( file_class == ELFCLASS64 ) {
|
||||
new_header = new elf_header_impl< Elf64_Ehdr >( &convertor,
|
||||
encoding );
|
||||
new_header =
|
||||
new elf_header_impl<Elf64_Ehdr>( &convertor, encoding );
|
||||
}
|
||||
else if ( file_class == ELFCLASS32 ) {
|
||||
new_header = new elf_header_impl< Elf32_Ehdr >( &convertor,
|
||||
encoding );
|
||||
new_header =
|
||||
new elf_header_impl<Elf32_Ehdr>( &convertor, encoding );
|
||||
}
|
||||
else {
|
||||
return 0;
|
||||
@ -356,7 +348,6 @@ class elfio
|
||||
return new_section;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
segment* create_segment()
|
||||
{
|
||||
@ -404,7 +395,8 @@ class elfio
|
||||
|
||||
for ( Elf_Half i = 0; i < num; ++i ) {
|
||||
section* sec = create_section();
|
||||
sec->load( stream, (std::streamoff)offset + (std::streampos)i * entry_size );
|
||||
sec->load( stream, (std::streamoff)offset +
|
||||
(std::streampos)i * entry_size );
|
||||
sec->set_index( i );
|
||||
// To mark that the section is not permitted to reassign address
|
||||
// during layout calculation
|
||||
@ -431,10 +423,15 @@ class elfio
|
||||
//! Checks whether the addresses of the section entirely fall within the given segment.
|
||||
//! It doesn't matter if the addresses are memory addresses, or file offsets,
|
||||
//! they just need to be in the same address space
|
||||
bool is_sect_in_seg ( Elf64_Off sect_begin, Elf_Xword sect_size, Elf64_Off seg_begin, Elf64_Off seg_end ) {
|
||||
return seg_begin <= sect_begin
|
||||
&& sect_begin + sect_size <= seg_end
|
||||
&& sect_begin < seg_end; // this is important criteria when sect_size == 0
|
||||
bool is_sect_in_seg( Elf64_Off sect_begin,
|
||||
Elf_Xword sect_size,
|
||||
Elf64_Off seg_begin,
|
||||
Elf64_Off seg_end )
|
||||
{
|
||||
return ( seg_begin <= sect_begin ) &&
|
||||
( sect_begin + sect_size <= seg_end ) &&
|
||||
( sect_begin <
|
||||
seg_end ); // this is important criteria when sect_size == 0
|
||||
// Example: seg_begin=10, seg_end=12 (-> covering the bytes 10 and 11)
|
||||
// sect_begin=12, sect_size=0 -> shall return false!
|
||||
}
|
||||
@ -460,7 +457,8 @@ class elfio
|
||||
return false;
|
||||
}
|
||||
|
||||
seg->load( stream, (std::streamoff)offset + (std::streampos)i * entry_size );
|
||||
seg->load( stream, (std::streamoff)offset +
|
||||
(std::streampos)i * entry_size );
|
||||
seg->set_index( i );
|
||||
|
||||
// Add sections to the segments (similar to readelfs algorithm)
|
||||
@ -473,9 +471,12 @@ class elfio
|
||||
|
||||
// SHF_ALLOC sections are matched based on the virtual address
|
||||
// otherwise the file offset is matched
|
||||
if( psec->get_flags() & SHF_ALLOC
|
||||
? is_sect_in_seg( psec->get_address(), psec->get_size(), segVBaseAddr, segVEndAddr )
|
||||
: is_sect_in_seg( psec->get_offset(), psec->get_size(), segBaseOffset, segEndOffset )) {
|
||||
if ( ( psec->get_flags() & SHF_ALLOC )
|
||||
? is_sect_in_seg( psec->get_address(),
|
||||
psec->get_size(), segVBaseAddr,
|
||||
segVEndAddr )
|
||||
: is_sect_in_seg( psec->get_offset(), psec->get_size(),
|
||||
segBaseOffset, segEndOffset ) ) {
|
||||
// Alignment of segment shall not be updated, to preserve original value
|
||||
// It will be re-calculated on saving.
|
||||
seg->add_section_index( psec->get_index(), 0 );
|
||||
@ -490,10 +491,7 @@ class elfio
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool save_header( std::ostream& stream )
|
||||
{
|
||||
return header->save( stream );
|
||||
}
|
||||
bool save_header( std::ostream& stream ) { return header->save( stream ); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool save_sections( std::ostream& stream )
|
||||
@ -503,7 +501,8 @@ class elfio
|
||||
|
||||
std::streampos headerPosition =
|
||||
(std::streamoff)header->get_sections_offset() +
|
||||
(std::streampos)header->get_section_entry_size() * sec->get_index();
|
||||
(std::streampos)header->get_section_entry_size() *
|
||||
sec->get_index();
|
||||
|
||||
sec->save( stream, headerPosition, sec->get_offset() );
|
||||
}
|
||||
@ -516,8 +515,10 @@ class elfio
|
||||
for ( unsigned int i = 0; i < segments_.size(); ++i ) {
|
||||
segment* seg = segments_.at( i );
|
||||
|
||||
std::streampos headerPosition = header->get_segments_offset() +
|
||||
(std::streampos)header->get_segment_entry_size()*seg->get_index();
|
||||
std::streampos headerPosition =
|
||||
header->get_segments_offset() +
|
||||
(std::streampos)header->get_segment_entry_size() *
|
||||
seg->get_index();
|
||||
|
||||
seg->save( stream, headerPosition, seg->get_offset() );
|
||||
}
|
||||
@ -531,8 +532,7 @@ class elfio
|
||||
|
||||
for ( unsigned int j = 0; !found && ( j < segments.size() ); ++j ) {
|
||||
for ( unsigned int k = 0;
|
||||
!found && ( k < segments[j]->get_sections_num() );
|
||||
++k ) {
|
||||
!found && ( k < segments[j]->get_sections_num() ); ++k ) {
|
||||
found = segments[j]->get_section_index_at( k ) == section_index;
|
||||
}
|
||||
}
|
||||
@ -569,8 +569,8 @@ class elfio
|
||||
// Bring the segments which start at address 0 to the front
|
||||
size_t nextSlot = 0;
|
||||
for ( size_t i = 0; i < worklist.size(); ++i ) {
|
||||
if( i != nextSlot && worklist[i]->is_offset_initialized()
|
||||
&& worklist[i]->get_offset() == 0 ) {
|
||||
if ( i != nextSlot && worklist[i]->is_offset_initialized() &&
|
||||
worklist[i]->get_offset() == 0 ) {
|
||||
if ( worklist[nextSlot]->get_offset() == 0 ) {
|
||||
++nextSlot;
|
||||
}
|
||||
@ -599,7 +599,6 @@ class elfio
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool layout_sections_without_segments()
|
||||
{
|
||||
@ -608,9 +607,10 @@ class elfio
|
||||
section* sec = sections_[i];
|
||||
|
||||
Elf_Xword section_align = sec->get_addr_align();
|
||||
if ( section_align > 1 && current_file_pos % section_align != 0 ) {
|
||||
current_file_pos += section_align -
|
||||
current_file_pos % section_align;
|
||||
if ( section_align > 1 &&
|
||||
current_file_pos % section_align != 0 ) {
|
||||
current_file_pos +=
|
||||
section_align - current_file_pos % section_align;
|
||||
}
|
||||
|
||||
if ( 0 != sec->get_index() )
|
||||
@ -626,11 +626,11 @@ class elfio
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void calc_segment_alignment()
|
||||
{
|
||||
for( std::vector<segment*>::iterator s = segments_.begin(); s != segments_.end(); ++s ) {
|
||||
for ( std::vector<segment*>::iterator s = segments_.begin();
|
||||
s != segments_.end(); ++s ) {
|
||||
segment* seg = *s;
|
||||
for ( int i = 0; i < seg->get_sections_num(); ++i ) {
|
||||
section* sect = sections_[seg->get_section_index_at( i )];
|
||||
@ -662,7 +662,8 @@ class elfio
|
||||
if ( seg->get_type() == PT_PHDR && seg->get_sections_num() == 0 ) {
|
||||
seg_start_pos = header->get_segments_offset();
|
||||
segment_memory = segment_filesize =
|
||||
header->get_segment_entry_size() * (Elf_Xword)header->get_segments_num();
|
||||
header->get_segment_entry_size() *
|
||||
(Elf_Xword)header->get_segments_num();
|
||||
}
|
||||
// Special case:
|
||||
else if ( seg->is_offset_initialized() && seg->get_offset() == 0 ) {
|
||||
@ -673,18 +674,20 @@ class elfio
|
||||
}
|
||||
// New segments with not generated sections
|
||||
// have to be aligned
|
||||
else if ( seg->get_sections_num()
|
||||
&& !section_generated[seg->get_section_index_at( 0 )] ) {
|
||||
else if ( seg->get_sections_num() &&
|
||||
!section_generated[seg->get_section_index_at( 0 )] ) {
|
||||
Elf_Xword align = seg->get_align() > 0 ? seg->get_align() : 1;
|
||||
Elf64_Off cur_page_alignment = current_file_pos % align;
|
||||
Elf64_Off req_page_alignment = seg->get_virtual_address() % align;
|
||||
Elf64_Off req_page_alignment =
|
||||
seg->get_virtual_address() % align;
|
||||
Elf64_Off error = req_page_alignment - cur_page_alignment;
|
||||
|
||||
current_file_pos += ( seg->get_align() + error ) % align;
|
||||
seg_start_pos = current_file_pos;
|
||||
}
|
||||
else if ( seg->get_sections_num() ) {
|
||||
seg_start_pos = sections[seg->get_section_index_at( 0 )]->get_offset();
|
||||
seg_start_pos =
|
||||
sections[seg->get_section_index_at( 0 )]->get_offset();
|
||||
}
|
||||
|
||||
// Write segment's data
|
||||
@ -701,13 +704,14 @@ class elfio
|
||||
|
||||
Elf_Xword secAlign = 0;
|
||||
// Fix up the alignment
|
||||
if ( !section_generated[index] && sec->is_address_initialized()
|
||||
&& SHT_NOBITS != sec->get_type()
|
||||
&& SHT_NULL != sec->get_type()
|
||||
&& 0 != sec->get_size() ) {
|
||||
if ( !section_generated[index] &&
|
||||
sec->is_address_initialized() &&
|
||||
SHT_NOBITS != sec->get_type() &&
|
||||
SHT_NULL != sec->get_type() && 0 != sec->get_size() ) {
|
||||
// Align the sections based on the virtual addresses
|
||||
// when possible (this is what matters for execution)
|
||||
Elf64_Off req_offset = sec->get_address() - seg->get_virtual_address();
|
||||
Elf64_Off req_offset =
|
||||
sec->get_address() - seg->get_virtual_address();
|
||||
Elf64_Off cur_offset = current_file_pos - seg_start_pos;
|
||||
if ( req_offset < cur_offset ) {
|
||||
// something has gone awfully wrong, abort!
|
||||
@ -716,7 +720,8 @@ class elfio
|
||||
}
|
||||
secAlign = req_offset - cur_offset;
|
||||
}
|
||||
else if (!section_generated[index] && !sec->is_address_initialized() ) {
|
||||
else if ( !section_generated[index] &&
|
||||
!sec->is_address_initialized() ) {
|
||||
// If no address has been specified then only the section
|
||||
// alignment constraint has to be matched
|
||||
Elf_Xword align = sec->get_addr_align();
|
||||
@ -728,14 +733,16 @@ class elfio
|
||||
}
|
||||
else if ( section_generated[index] ) {
|
||||
// Alignment for already generated sections
|
||||
secAlign = sec->get_offset() - seg_start_pos - segment_filesize;
|
||||
secAlign =
|
||||
sec->get_offset() - seg_start_pos - segment_filesize;
|
||||
}
|
||||
|
||||
// Determine the segment file and memory sizes
|
||||
// Special case .tbss section (NOBITS) in non TLS segment
|
||||
if ( (sec->get_flags() & SHF_ALLOC)
|
||||
&& !( (sec->get_flags() & SHF_TLS) && (seg->get_type() != PT_TLS)
|
||||
&& ( SHT_NOBITS == sec->get_type())) )
|
||||
if ( ( sec->get_flags() & SHF_ALLOC ) &&
|
||||
!( ( sec->get_flags() & SHF_TLS ) &&
|
||||
( seg->get_type() != PT_TLS ) &&
|
||||
( SHT_NOBITS == sec->get_type() ) ) )
|
||||
segment_memory += sec->get_size() + secAlign;
|
||||
|
||||
if ( SHT_NOBITS != sec->get_type() )
|
||||
@ -750,8 +757,8 @@ class elfio
|
||||
|
||||
// Set the section addresses when missing
|
||||
if ( !sec->is_address_initialized() )
|
||||
sec->set_address( seg->get_virtual_address()
|
||||
+ current_file_pos - seg_start_pos);
|
||||
sec->set_address( seg->get_virtual_address() +
|
||||
current_file_pos - seg_start_pos );
|
||||
|
||||
if ( 0 != sec->get_index() )
|
||||
sec->set_offset( current_file_pos );
|
||||
@ -788,23 +795,17 @@ class elfio
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
public:
|
||||
friend class Sections;
|
||||
class Sections {
|
||||
class Sections
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
Sections( elfio* parent_ ) :
|
||||
parent( parent_ )
|
||||
{
|
||||
}
|
||||
Sections( elfio* parent_ ) : parent( parent_ ) {}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half size() const
|
||||
{
|
||||
return (Elf_Half)parent->sections_.size();
|
||||
}
|
||||
Elf_Half size() const { return (Elf_Half)parent->sections_.size(); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
section* operator[]( unsigned int index ) const
|
||||
@ -824,8 +825,7 @@ class elfio
|
||||
section* sec = 0;
|
||||
|
||||
std::vector<section*>::const_iterator it;
|
||||
for ( it = parent->sections_.begin();
|
||||
it != parent->sections_.end();
|
||||
for ( it = parent->sections_.begin(); it != parent->sections_.end();
|
||||
++it ) {
|
||||
if ( ( *it )->get_name() == name ) {
|
||||
sec = *it;
|
||||
@ -852,22 +852,26 @@ class elfio
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::vector<section*>::iterator begin() {
|
||||
std::vector<section*>::iterator begin()
|
||||
{
|
||||
return parent->sections_.begin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::vector<section*>::iterator end() {
|
||||
std::vector<section*>::iterator end()
|
||||
{
|
||||
return parent->sections_.end();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::vector<section*>::const_iterator begin() const {
|
||||
std::vector<section*>::const_iterator begin() const
|
||||
{
|
||||
return parent->sections_.cbegin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::vector<section*>::const_iterator end() const {
|
||||
std::vector<section*>::const_iterator end() const
|
||||
{
|
||||
return parent->sections_.cend();
|
||||
}
|
||||
|
||||
@ -879,19 +883,14 @@ class elfio
|
||||
//------------------------------------------------------------------------------
|
||||
public:
|
||||
friend class Segments;
|
||||
class Segments {
|
||||
class Segments
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
Segments( elfio* parent_ ) :
|
||||
parent( parent_ )
|
||||
{
|
||||
}
|
||||
Segments( elfio* parent_ ) : parent( parent_ ) {}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half size() const
|
||||
{
|
||||
return (Elf_Half)parent->segments_.size();
|
||||
}
|
||||
Elf_Half size() const { return (Elf_Half)parent->segments_.size(); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
segment* operator[]( unsigned int index ) const
|
||||
@ -899,30 +898,30 @@ class elfio
|
||||
return parent->segments_[index];
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
segment* add() { return parent->create_segment(); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
segment* add()
|
||||
std::vector<segment*>::iterator begin()
|
||||
{
|
||||
return parent->create_segment();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::vector<segment*>::iterator begin() {
|
||||
return parent->segments_.begin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::vector<segment*>::iterator end() {
|
||||
std::vector<segment*>::iterator end()
|
||||
{
|
||||
return parent->segments_.end();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::vector<segment*>::const_iterator begin() const {
|
||||
std::vector<segment*>::const_iterator begin() const
|
||||
{
|
||||
return parent->segments_.cbegin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::vector<segment*>::const_iterator end() const {
|
||||
std::vector<segment*>::const_iterator end() const
|
||||
{
|
||||
return parent->segments_.cend();
|
||||
}
|
||||
|
||||
|
@ -32,56 +32,49 @@ THE SOFTWARE.
|
||||
|
||||
namespace ELFIO {
|
||||
|
||||
|
||||
static struct class_table_t {
|
||||
static struct class_table_t
|
||||
{
|
||||
const char key;
|
||||
const char* str;
|
||||
} class_table [] =
|
||||
{
|
||||
} class_table[] = {
|
||||
{ ELFCLASS32, "ELF32" },
|
||||
{ ELFCLASS64, "ELF64" },
|
||||
};
|
||||
|
||||
|
||||
static struct endian_table_t {
|
||||
static struct endian_table_t
|
||||
{
|
||||
const char key;
|
||||
const char* str;
|
||||
} endian_table [] =
|
||||
{
|
||||
} endian_table[] = {
|
||||
{ ELFDATANONE, "None" },
|
||||
{ ELFDATA2LSB, "Little endian" },
|
||||
{ ELFDATA2MSB, "Big endian" },
|
||||
};
|
||||
|
||||
|
||||
static struct version_table_t {
|
||||
static struct version_table_t
|
||||
{
|
||||
const Elf64_Word key;
|
||||
const char* str;
|
||||
} version_table [] =
|
||||
{
|
||||
} version_table[] = {
|
||||
{ EV_NONE, "None" },
|
||||
{ EV_CURRENT, "Current" },
|
||||
};
|
||||
|
||||
|
||||
static struct type_table_t {
|
||||
static struct type_table_t
|
||||
{
|
||||
const Elf32_Half key;
|
||||
const char* str;
|
||||
} type_table [] =
|
||||
{
|
||||
{ ET_NONE, "No file type" },
|
||||
{ ET_REL , "Relocatable file" },
|
||||
{ ET_EXEC, "Executable file" },
|
||||
{ ET_DYN , "Shared object file" },
|
||||
} type_table[] = {
|
||||
{ ET_NONE, "No file type" }, { ET_REL, "Relocatable file" },
|
||||
{ ET_EXEC, "Executable file" }, { ET_DYN, "Shared object file" },
|
||||
{ ET_CORE, "Core file" },
|
||||
};
|
||||
|
||||
|
||||
static struct machine_table_t {
|
||||
static struct machine_table_t
|
||||
{
|
||||
const Elf64_Half key;
|
||||
const char* str;
|
||||
} machine_table [] =
|
||||
{
|
||||
} machine_table[] = {
|
||||
{ EM_NONE, "No machine" },
|
||||
{ EM_M32, "AT&T WE 32100" },
|
||||
{ EM_SPARC, "SUN SPARC" },
|
||||
@ -92,7 +85,8 @@ static struct machine_table_t {
|
||||
{ EM_860, "Intel 80860" },
|
||||
{ EM_MIPS, "MIPS R3000 (officially, big-endian only)" },
|
||||
{ EM_S370, "IBM System/370" },
|
||||
{ EM_MIPS_RS3_LE , "MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated" },
|
||||
{ EM_MIPS_RS3_LE,
|
||||
"MIPS R3000 little-endian (Oct 4 1999 Draft) Deprecated" },
|
||||
{ EM_res011, "Reserved" },
|
||||
{ EM_res012, "Reserved" },
|
||||
{ EM_res013, "Reserved" },
|
||||
@ -193,7 +187,8 @@ static struct machine_table_t {
|
||||
{ EM_SE_C33, "S1C33 Family of Seiko Epson processors" },
|
||||
{ EM_SEP, "Sharp embedded microprocessor" },
|
||||
{ EM_ARCA, "Arca RISC Microprocessor" },
|
||||
{ EM_UNICORE , "Microprocessor series from PKU-Unity Ltd. and MPRC of Peking University" },
|
||||
{ EM_UNICORE, "Microprocessor series from PKU-Unity Ltd. and MPRC of "
|
||||
"Peking University" },
|
||||
{ EM_EXCESS, "eXcess: 16/32/64-bit configurable embedded CPU" },
|
||||
{ EM_DXP, "Icera Semiconductor Inc. Deep Execution Processor" },
|
||||
{ EM_ALTERA_NIOS2, "Altera Nios II soft-core processor" },
|
||||
@ -251,7 +246,8 @@ static struct machine_table_t {
|
||||
{ EM_QDSP6, "QUALCOMM DSP6 Processor" },
|
||||
{ EM_8051, "Intel 8051 and variants" },
|
||||
{ EM_STXP7X, "STMicroelectronics STxP7x family" },
|
||||
{ EM_NDS32 , "Andes Technology compact code size embedded RISC processor family" },
|
||||
{ EM_NDS32,
|
||||
"Andes Technology compact code size embedded RISC processor family" },
|
||||
{ EM_ECOG1, "Cyan Technology eCOG1X family" },
|
||||
{ EM_ECOG1X, "Cyan Technology eCOG1X family" },
|
||||
{ EM_MAXQ30, "Dallas Semiconductor MAXQ30 Core Micro-controllers" },
|
||||
@ -278,12 +274,11 @@ static struct machine_table_t {
|
||||
{ EM_CUDA, "NVIDIA CUDA architecture " },
|
||||
};
|
||||
|
||||
|
||||
static struct section_type_table_t {
|
||||
static struct section_type_table_t
|
||||
{
|
||||
const Elf64_Half key;
|
||||
const char* str;
|
||||
} section_type_table [] =
|
||||
{
|
||||
} section_type_table[] = {
|
||||
{ SHT_NULL, "NULL" },
|
||||
{ SHT_PROGBITS, "PROGBITS" },
|
||||
{ SHT_SYMTAB, "SYMTAB" },
|
||||
@ -303,79 +298,54 @@ static struct section_type_table_t {
|
||||
{ SHT_SYMTAB_SHNDX, "SYMTAB_SHNDX " },
|
||||
};
|
||||
|
||||
|
||||
static struct segment_type_table_t {
|
||||
static struct segment_type_table_t
|
||||
{
|
||||
const Elf_Word key;
|
||||
const char* str;
|
||||
} segment_type_table [] =
|
||||
{
|
||||
{ PT_NULL , "NULL" },
|
||||
{ PT_LOAD , "LOAD" },
|
||||
{ PT_DYNAMIC, "DYNAMIC" },
|
||||
{ PT_INTERP , "INTERP" },
|
||||
{ PT_NOTE , "NOTE" },
|
||||
{ PT_SHLIB , "SHLIB" },
|
||||
{ PT_PHDR , "PHDR" },
|
||||
{ PT_TLS , "TLS" },
|
||||
} segment_type_table[] = {
|
||||
{ PT_NULL, "NULL" }, { PT_LOAD, "LOAD" }, { PT_DYNAMIC, "DYNAMIC" },
|
||||
{ PT_INTERP, "INTERP" }, { PT_NOTE, "NOTE" }, { PT_SHLIB, "SHLIB" },
|
||||
{ PT_PHDR, "PHDR" }, { PT_TLS, "TLS" },
|
||||
};
|
||||
|
||||
|
||||
static struct segment_flag_table_t {
|
||||
static struct segment_flag_table_t
|
||||
{
|
||||
const Elf_Word key;
|
||||
const char* str;
|
||||
} segment_flag_table [] =
|
||||
{
|
||||
{ 0, "" },
|
||||
{ 1, "X" },
|
||||
{ 2, "W" },
|
||||
{ 3, "WX" },
|
||||
{ 4, "R" },
|
||||
{ 5, "RX" },
|
||||
{ 6, "RW" },
|
||||
{ 7, "RWX" },
|
||||
} segment_flag_table[] = {
|
||||
{ 0, "" }, { 1, "X" }, { 2, "W" }, { 3, "WX" },
|
||||
{ 4, "R" }, { 5, "RX" }, { 6, "RW" }, { 7, "RWX" },
|
||||
};
|
||||
|
||||
|
||||
static struct symbol_bind_t {
|
||||
static struct symbol_bind_t
|
||||
{
|
||||
const Elf_Word key;
|
||||
const char* str;
|
||||
} symbol_bind_table [] =
|
||||
{
|
||||
{ STB_LOCAL , "LOCAL" },
|
||||
{ STB_GLOBAL , "GLOBAL" },
|
||||
{ STB_WEAK , "WEAK" },
|
||||
{ STB_LOOS , "LOOS" },
|
||||
{ STB_HIOS , "HIOS" },
|
||||
{ STB_MULTIDEF, "MULTIDEF" },
|
||||
{ STB_LOPROC , "LOPROC" },
|
||||
{ STB_HIPROC , "HIPROC" },
|
||||
} symbol_bind_table[] = {
|
||||
{ STB_LOCAL, "LOCAL" }, { STB_GLOBAL, "GLOBAL" },
|
||||
{ STB_WEAK, "WEAK" }, { STB_LOOS, "LOOS" },
|
||||
{ STB_HIOS, "HIOS" }, { STB_MULTIDEF, "MULTIDEF" },
|
||||
{ STB_LOPROC, "LOPROC" }, { STB_HIPROC, "HIPROC" },
|
||||
};
|
||||
|
||||
|
||||
static struct symbol_type_t {
|
||||
static struct symbol_type_t
|
||||
{
|
||||
const Elf_Word key;
|
||||
const char* str;
|
||||
} symbol_type_table [] =
|
||||
{
|
||||
{ STT_NOTYPE , "NOTYPE" },
|
||||
{ STT_OBJECT , "OBJECT" },
|
||||
{ STT_FUNC , "FUNC" },
|
||||
{ STT_SECTION, "SECTION" },
|
||||
{ STT_FILE , "FILE" },
|
||||
{ STT_COMMON , "COMMON" },
|
||||
{ STT_TLS , "TLS" },
|
||||
{ STT_LOOS , "LOOS" },
|
||||
{ STT_HIOS , "HIOS" },
|
||||
{ STT_LOPROC , "LOPROC" },
|
||||
} symbol_type_table[] = {
|
||||
{ STT_NOTYPE, "NOTYPE" }, { STT_OBJECT, "OBJECT" },
|
||||
{ STT_FUNC, "FUNC" }, { STT_SECTION, "SECTION" },
|
||||
{ STT_FILE, "FILE" }, { STT_COMMON, "COMMON" },
|
||||
{ STT_TLS, "TLS" }, { STT_LOOS, "LOOS" },
|
||||
{ STT_HIOS, "HIOS" }, { STT_LOPROC, "LOPROC" },
|
||||
{ STT_HIPROC, "HIPROC" },
|
||||
};
|
||||
|
||||
|
||||
static struct dynamic_tag_t {
|
||||
static struct dynamic_tag_t
|
||||
{
|
||||
const Elf_Word key;
|
||||
const char* str;
|
||||
} dynamic_tag_table [] =
|
||||
{
|
||||
} dynamic_tag_table[] = {
|
||||
{ DT_NULL, "NULL" },
|
||||
{ DT_NEEDED, "NEEDED" },
|
||||
{ DT_PLTRELSZ, "PLTRELSZ" },
|
||||
@ -418,37 +388,41 @@ static const ELFIO::Elf_Xword MAX_DATA_ENTRIES = 64;
|
||||
//------------------------------------------------------------------------------
|
||||
class dump
|
||||
{
|
||||
#define DUMP_DEC_FORMAT( width ) std::setw(width) << std::setfill( ' ' ) << \
|
||||
std::dec << std::right
|
||||
#define DUMP_HEX_FORMAT( width ) std::setw(width) << std::setfill( '0' ) << \
|
||||
std::hex << std::right
|
||||
#define DUMP_STR_FORMAT( width ) std::setw(width) << std::setfill( ' ' ) << \
|
||||
std::hex << std::left
|
||||
#define DUMP_DEC_FORMAT( width ) \
|
||||
std::setw( width ) << std::setfill( ' ' ) << std::dec << std::right
|
||||
#define DUMP_HEX_FORMAT( width ) \
|
||||
std::setw( width ) << std::setfill( '0' ) << std::hex << std::right
|
||||
#define DUMP_STR_FORMAT( width ) \
|
||||
std::setw( width ) << std::setfill( ' ' ) << std::hex << std::left
|
||||
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
header( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
if (!reader.get_header_size())
|
||||
static void header( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
if ( !reader.get_header_size() ) {
|
||||
return;
|
||||
}
|
||||
out << "ELF Header" << std::endl << std::endl
|
||||
out << "ELF Header" << std::endl
|
||||
<< std::endl
|
||||
<< " Class: " << str_class( reader.get_class() ) << std::endl
|
||||
<< " Encoding: " << str_endian( reader.get_encoding() ) << std::endl
|
||||
<< " ELFVersion: " << str_version( reader.get_elf_version() ) << std::endl
|
||||
<< " Encoding: " << str_endian( reader.get_encoding() )
|
||||
<< std::endl
|
||||
<< " ELFVersion: " << str_version( reader.get_elf_version() )
|
||||
<< std::endl
|
||||
<< " Type: " << str_type( reader.get_type() ) << std::endl
|
||||
<< " Machine: " << str_machine( reader.get_machine() ) << std::endl
|
||||
<< " Version: " << str_version( reader.get_version() ) << std::endl
|
||||
<< " Entry: " << "0x" << std::hex << reader.get_entry() << std::endl
|
||||
<< " Flags: " << "0x" << std::hex << reader.get_flags() << std::endl
|
||||
<< " Machine: " << str_machine( reader.get_machine() )
|
||||
<< std::endl
|
||||
<< " Version: " << str_version( reader.get_version() )
|
||||
<< std::endl
|
||||
<< " Entry: "
|
||||
<< "0x" << std::hex << reader.get_entry() << std::endl
|
||||
<< " Flags: "
|
||||
<< "0x" << std::hex << reader.get_flags() << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
section_headers( std::ostream& out, const elfio& reader )
|
||||
static void section_headers( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
Elf_Half n = reader.sections.size();
|
||||
|
||||
@ -458,10 +432,14 @@ class dump
|
||||
|
||||
out << "Section Headers:" << std::endl;
|
||||
if ( reader.get_class() == ELFCLASS32 ) { // Output for 32-bit
|
||||
out << "[ Nr ] Type Addr Size ES Flg Lk Inf Al Name" << std::endl;
|
||||
out << "[ Nr ] Type Addr Size ES Flg Lk Inf "
|
||||
"Al Name"
|
||||
<< std::endl;
|
||||
}
|
||||
else { // Output for 64-bit
|
||||
out << "[ Nr ] Type Addr Size ES Flg" << std::endl
|
||||
out << "[ Nr ] Type Addr Size "
|
||||
" ES Flg"
|
||||
<< std::endl
|
||||
<< " Lk Inf Al Name" << std::endl;
|
||||
}
|
||||
|
||||
@ -475,43 +453,37 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
section_header( std::ostream& out, Elf_Half no, const section* sec,
|
||||
static void section_header( std::ostream& out,
|
||||
Elf_Half no,
|
||||
const section* sec,
|
||||
unsigned char elf_class )
|
||||
{
|
||||
std::ios_base::fmtflags original_flags = out.flags();
|
||||
|
||||
if ( elf_class == ELFCLASS32 ) { // Output for 32-bit
|
||||
out << "["
|
||||
<< DUMP_DEC_FORMAT( 5 ) << no
|
||||
<< "] "
|
||||
<< DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() ) << " "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << sec->get_address() << " "
|
||||
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
|
||||
<< DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() )
|
||||
<< " " << DUMP_HEX_FORMAT( 8 ) << sec->get_address() << " "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << sec->get_size() << " "
|
||||
<< DUMP_HEX_FORMAT( 2 ) << sec->get_entry_size() << " "
|
||||
<< DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() ) << " "
|
||||
<< DUMP_HEX_FORMAT( 2 ) << sec->get_link() << " "
|
||||
<< DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() )
|
||||
<< " " << DUMP_HEX_FORMAT( 2 ) << sec->get_link() << " "
|
||||
<< DUMP_HEX_FORMAT( 3 ) << sec->get_info() << " "
|
||||
<< DUMP_HEX_FORMAT( 2 ) << sec->get_addr_align() << " "
|
||||
<< DUMP_STR_FORMAT( 17 ) << sec->get_name() << " "
|
||||
<< std::endl;
|
||||
<< DUMP_STR_FORMAT( 17 ) << sec->get_name() << " " << std::endl;
|
||||
}
|
||||
else { // Output for 64-bit
|
||||
out << "["
|
||||
<< DUMP_DEC_FORMAT( 5 ) << no
|
||||
<< "] "
|
||||
<< DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() ) << " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << sec->get_address() << " "
|
||||
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
|
||||
<< DUMP_STR_FORMAT( 17 ) << str_section_type( sec->get_type() )
|
||||
<< " " << DUMP_HEX_FORMAT( 16 ) << sec->get_address() << " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << sec->get_size() << " "
|
||||
<< DUMP_HEX_FORMAT( 4 ) << sec->get_entry_size() << " "
|
||||
<< DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() ) << " "
|
||||
<< std::endl
|
||||
<< " "
|
||||
<< DUMP_HEX_FORMAT( 4 ) << sec->get_link() << " "
|
||||
<< DUMP_STR_FORMAT( 3 ) << section_flags( sec->get_flags() )
|
||||
<< " " << std::endl
|
||||
<< " " << DUMP_HEX_FORMAT( 4 ) << sec->get_link() << " "
|
||||
<< DUMP_HEX_FORMAT( 4 ) << sec->get_info() << " "
|
||||
<< DUMP_HEX_FORMAT( 4 ) << sec->get_addr_align() << " "
|
||||
<< DUMP_STR_FORMAT( 17 ) << sec->get_name() << " "
|
||||
<< std::endl;
|
||||
<< DUMP_STR_FORMAT( 17 ) << sec->get_name() << " " << std::endl;
|
||||
}
|
||||
|
||||
out.flags( original_flags );
|
||||
@ -520,8 +492,7 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
segment_headers( std::ostream& out, const elfio& reader )
|
||||
static void segment_headers( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
Elf_Half n = reader.segments.size();
|
||||
if ( n == 0 ) {
|
||||
@ -530,12 +501,16 @@ class dump
|
||||
|
||||
out << "Segment headers:" << std::endl;
|
||||
if ( reader.get_class() == ELFCLASS32 ) { // Output for 32-bit
|
||||
out << "[ Nr ] Type VirtAddr PhysAddr FileSize Mem.Size Flags Align"
|
||||
out << "[ Nr ] Type VirtAddr PhysAddr FileSize Mem.Size "
|
||||
"Flags Align"
|
||||
<< std::endl;
|
||||
}
|
||||
else { // Output for 64-bit
|
||||
out << "[ Nr ] Type VirtAddr PhysAddr Flags" << std::endl
|
||||
<< " FileSize Mem.Size Align"
|
||||
out << "[ Nr ] Type VirtAddr PhysAddr "
|
||||
"Flags"
|
||||
<< std::endl
|
||||
<< " FileSize Mem.Size "
|
||||
"Align"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
@ -548,65 +523,65 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
segment_header( std::ostream& out, Elf_Half no, const segment* seg,
|
||||
static void segment_header( std::ostream& out,
|
||||
Elf_Half no,
|
||||
const segment* seg,
|
||||
unsigned int elf_class )
|
||||
{
|
||||
std::ios_base::fmtflags original_flags = out.flags();
|
||||
|
||||
if ( elf_class == ELFCLASS32 ) { // Output for 32-bit
|
||||
out << "["
|
||||
<< DUMP_DEC_FORMAT( 5 ) << no
|
||||
<< "] "
|
||||
<< DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) << " "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << seg->get_virtual_address() << " "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << seg->get_physical_address() << " "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << seg->get_file_size() << " "
|
||||
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
|
||||
<< DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() )
|
||||
<< " " << DUMP_HEX_FORMAT( 8 ) << seg->get_virtual_address()
|
||||
<< " " << DUMP_HEX_FORMAT( 8 ) << seg->get_physical_address()
|
||||
<< " " << DUMP_HEX_FORMAT( 8 ) << seg->get_file_size() << " "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << seg->get_memory_size() << " "
|
||||
<< DUMP_STR_FORMAT( 8 ) << str_segment_flag( seg->get_flags() ) << " "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << seg->get_align() << " "
|
||||
<< DUMP_STR_FORMAT( 8 ) << str_segment_flag( seg->get_flags() )
|
||||
<< " " << DUMP_HEX_FORMAT( 8 ) << seg->get_align() << " "
|
||||
<< std::endl;
|
||||
}
|
||||
else { // Output for 64-bit
|
||||
out << "["
|
||||
<< DUMP_DEC_FORMAT( 5 ) << no
|
||||
<< "] "
|
||||
<< DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) << " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << seg->get_virtual_address() << " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << seg->get_physical_address() << " "
|
||||
<< DUMP_STR_FORMAT( 16 ) << str_segment_flag( seg->get_flags() ) << " "
|
||||
<< std::endl
|
||||
<< " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << seg->get_file_size() << " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << seg->get_memory_size() << " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << seg->get_align() << " "
|
||||
<< std::endl;
|
||||
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
|
||||
<< DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() )
|
||||
<< " " << DUMP_HEX_FORMAT( 16 ) << seg->get_virtual_address()
|
||||
<< " " << DUMP_HEX_FORMAT( 16 ) << seg->get_physical_address()
|
||||
<< " " << DUMP_STR_FORMAT( 16 )
|
||||
<< str_segment_flag( seg->get_flags() ) << " " << std::endl
|
||||
<< " " << DUMP_HEX_FORMAT( 16 )
|
||||
<< seg->get_file_size() << " " << DUMP_HEX_FORMAT( 16 )
|
||||
<< seg->get_memory_size() << " " << DUMP_HEX_FORMAT( 16 )
|
||||
<< seg->get_align() << " " << std::endl;
|
||||
}
|
||||
|
||||
out.flags( original_flags );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
symbol_tables( std::ostream& out, const elfio& reader )
|
||||
static void symbol_tables( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
Elf_Half n = reader.sections.size();
|
||||
for ( Elf_Half i = 0; i < n; ++i ) { // For all sections
|
||||
section* sec = reader.sections[i];
|
||||
if ( SHT_SYMTAB == sec->get_type() || SHT_DYNSYM == sec->get_type() ) {
|
||||
if ( SHT_SYMTAB == sec->get_type() ||
|
||||
SHT_DYNSYM == sec->get_type() ) {
|
||||
symbol_section_accessor symbols( reader, sec );
|
||||
|
||||
Elf_Xword sym_no = symbols.get_symbols_num();
|
||||
if ( sym_no > 0 ) {
|
||||
out << "Symbol table (" << sec->get_name() << ")" << std::endl;
|
||||
if ( reader.get_class() == ELFCLASS32 ) { // Output for 32-bit
|
||||
out << "[ Nr ] Value Size Type Bind Sect Name"
|
||||
out << "Symbol table (" << sec->get_name() << ")"
|
||||
<< std::endl;
|
||||
if ( reader.get_class() ==
|
||||
ELFCLASS32 ) { // Output for 32-bit
|
||||
out << "[ Nr ] Value Size Type Bind "
|
||||
"Sect Name"
|
||||
<< std::endl;
|
||||
}
|
||||
else { // Output for 64-bit
|
||||
out << "[ Nr ] Value Size Type Bind Sect" << std::endl
|
||||
<< " Name"
|
||||
<< std::endl;
|
||||
out << "[ Nr ] Value Size Type "
|
||||
" Bind Sect"
|
||||
<< std::endl
|
||||
<< " Name" << std::endl;
|
||||
}
|
||||
for ( Elf_Xword i = 0; i < sym_no; ++i ) {
|
||||
std::string name;
|
||||
@ -616,8 +591,10 @@ class dump
|
||||
unsigned char type = 0;
|
||||
Elf_Half section = 0;
|
||||
unsigned char other = 0;
|
||||
symbols.get_symbol( i, name, value, size, bind, type, section, other );
|
||||
symbol_table( out, i, name, value, size, bind, type, section, reader.get_class() );
|
||||
symbols.get_symbol( i, name, value, size, bind, type,
|
||||
section, other );
|
||||
symbol_table( out, i, name, value, size, bind, type,
|
||||
section, reader.get_class() );
|
||||
}
|
||||
|
||||
out << std::endl;
|
||||
@ -627,8 +604,7 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
symbol_table( std::ostream& out,
|
||||
static void symbol_table( std::ostream& out,
|
||||
Elf_Xword no,
|
||||
std::string& name,
|
||||
Elf64_Addr value,
|
||||
@ -641,29 +617,22 @@ class dump
|
||||
std::ios_base::fmtflags original_flags = out.flags();
|
||||
|
||||
if ( elf_class == ELFCLASS32 ) { // Output for 32-bit
|
||||
out << "["
|
||||
<< DUMP_DEC_FORMAT( 5 ) << no
|
||||
<< "] "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << value << " "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << size << " "
|
||||
<< DUMP_STR_FORMAT( 7 ) << str_symbol_type( type ) << " "
|
||||
<< DUMP_STR_FORMAT( 8 ) << str_symbol_bind( bind ) << " "
|
||||
<< DUMP_DEC_FORMAT( 5 ) << section << " "
|
||||
<< DUMP_STR_FORMAT( 1 ) << name << " "
|
||||
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << value << " " << DUMP_HEX_FORMAT( 8 )
|
||||
<< size << " " << DUMP_STR_FORMAT( 7 )
|
||||
<< str_symbol_type( type ) << " " << DUMP_STR_FORMAT( 8 )
|
||||
<< str_symbol_bind( bind ) << " " << DUMP_DEC_FORMAT( 5 )
|
||||
<< section << " " << DUMP_STR_FORMAT( 1 ) << name << " "
|
||||
<< std::endl;
|
||||
}
|
||||
else { // Output for 64-bit
|
||||
out << "["
|
||||
<< DUMP_DEC_FORMAT( 5 ) << no
|
||||
<< "] "
|
||||
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << value << " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << size << " "
|
||||
<< DUMP_STR_FORMAT( 7 ) << str_symbol_type( type ) << " "
|
||||
<< DUMP_STR_FORMAT( 8 ) << str_symbol_bind( bind ) << " "
|
||||
<< DUMP_DEC_FORMAT( 5 ) << section << " "
|
||||
<< std::endl
|
||||
<< " "
|
||||
<< DUMP_STR_FORMAT( 1 ) << name << " "
|
||||
<< DUMP_HEX_FORMAT( 16 ) << size << " " << DUMP_STR_FORMAT( 7 )
|
||||
<< str_symbol_type( type ) << " " << DUMP_STR_FORMAT( 8 )
|
||||
<< str_symbol_bind( bind ) << " " << DUMP_DEC_FORMAT( 5 )
|
||||
<< section << " " << std::endl
|
||||
<< " " << DUMP_STR_FORMAT( 1 ) << name << " "
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
@ -671,8 +640,7 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
notes( std::ostream& out, const elfio& reader )
|
||||
static void notes( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
Elf_Half no = reader.sections.size();
|
||||
for ( Elf_Half i = 0; i < no; ++i ) { // For all sections
|
||||
@ -681,9 +649,9 @@ class dump
|
||||
note_section_accessor notes( reader, sec );
|
||||
Elf_Word no_notes = notes.get_notes_num();
|
||||
if ( no > 0 ) {
|
||||
out << "Note section (" << sec->get_name() << ")" << std::endl
|
||||
<< " No Type Name"
|
||||
<< std::endl;
|
||||
out << "Note section (" << sec->get_name() << ")"
|
||||
<< std::endl
|
||||
<< " No Type Name" << std::endl;
|
||||
for ( Elf_Word j = 0; j < no_notes; ++j ) { // For all notes
|
||||
Elf_Word type;
|
||||
std::string name;
|
||||
@ -705,22 +673,15 @@ class dump
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
note( std::ostream& out,
|
||||
int no,
|
||||
Elf_Word type,
|
||||
const std::string& name )
|
||||
note( std::ostream& out, int no, Elf_Word type, const std::string& name )
|
||||
{
|
||||
out << " ["
|
||||
<< DUMP_DEC_FORMAT( 2 ) << no
|
||||
<< "] "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << type << " "
|
||||
<< DUMP_STR_FORMAT( 1 ) << name
|
||||
<< std::endl;
|
||||
out << " [" << DUMP_DEC_FORMAT( 2 ) << no << "] "
|
||||
<< DUMP_HEX_FORMAT( 8 ) << type << " " << DUMP_STR_FORMAT( 1 )
|
||||
<< name << std::endl;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
dynamic_tags( std::ostream& out, const elfio& reader )
|
||||
static void dynamic_tags( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
Elf_Half n = reader.sections.size();
|
||||
for ( Elf_Half i = 0; i < n; ++i ) { // For all sections
|
||||
@ -730,14 +691,16 @@ class dump
|
||||
|
||||
Elf_Xword dyn_no = dynamic.get_entries_num();
|
||||
if ( dyn_no > 0 ) {
|
||||
out << "Dynamic section (" << sec->get_name() << ")" << std::endl;
|
||||
out << "Dynamic section (" << sec->get_name() << ")"
|
||||
<< std::endl;
|
||||
out << "[ Nr ] Tag Name/Value" << std::endl;
|
||||
for ( Elf_Xword i = 0; i < dyn_no; ++i ) {
|
||||
Elf_Xword tag = 0;
|
||||
Elf_Xword value = 0;
|
||||
std::string str;
|
||||
dynamic.get_entry( i, tag, value, str );
|
||||
dynamic_tag( out, i, tag, value, str, reader.get_class() );
|
||||
dynamic_tag( out, i, tag, value, str,
|
||||
reader.get_class() );
|
||||
if ( DT_NULL == tag ) {
|
||||
break;
|
||||
}
|
||||
@ -750,17 +713,14 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
dynamic_tag( std::ostream& out,
|
||||
static void dynamic_tag( std::ostream& out,
|
||||
Elf_Xword no,
|
||||
Elf_Xword tag,
|
||||
Elf_Xword value,
|
||||
std::string str,
|
||||
unsigned int /*elf_class*/ )
|
||||
{
|
||||
out << "["
|
||||
<< DUMP_DEC_FORMAT( 5 ) << no
|
||||
<< "] "
|
||||
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
|
||||
<< DUMP_STR_FORMAT( 16 ) << str_dynamic_tag( tag ) << " ";
|
||||
if ( str.empty() ) {
|
||||
out << DUMP_HEX_FORMAT( 16 ) << value << " ";
|
||||
@ -772,8 +732,7 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
section_data( std::ostream& out, const section* sec )
|
||||
static void section_data( std::ostream& out, const section* sec )
|
||||
{
|
||||
std::ios_base::fmtflags original_flags = out.flags();
|
||||
|
||||
@ -781,7 +740,8 @@ class dump
|
||||
const char* pdata = sec->get_data();
|
||||
if ( pdata ) {
|
||||
ELFIO::Elf_Xword i;
|
||||
for ( i = 0; i < std::min( sec->get_size(), MAX_DATA_ENTRIES ); ++i ) {
|
||||
for ( i = 0; i < std::min( sec->get_size(), MAX_DATA_ENTRIES );
|
||||
++i ) {
|
||||
if ( i % 16 == 0 ) {
|
||||
out << "[" << DUMP_HEX_FORMAT( 8 ) << i << "]";
|
||||
}
|
||||
@ -803,8 +763,7 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
section_datas( std::ostream& out, const elfio& reader )
|
||||
static void section_datas( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
Elf_Half n = reader.sections.size();
|
||||
|
||||
@ -835,7 +794,8 @@ class dump
|
||||
const char* pdata = seg->get_data();
|
||||
if ( pdata ) {
|
||||
ELFIO::Elf_Xword i;
|
||||
for ( i = 0; i < std::min( seg->get_file_size(), MAX_DATA_ENTRIES ); ++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 << "]";
|
||||
}
|
||||
@ -857,8 +817,7 @@ class dump
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static void
|
||||
segment_datas( std::ostream& out, const elfio& reader )
|
||||
static void segment_datas( std::ostream& out, const elfio& reader )
|
||||
{
|
||||
Elf_Half n = reader.segments.size();
|
||||
|
||||
@ -879,12 +838,11 @@ class dump
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
template <typename T, typename K>
|
||||
std::string
|
||||
static
|
||||
find_value_in_table( const T& table, const K& key )
|
||||
std::string static find_value_in_table( const T& table, const K& key )
|
||||
{
|
||||
std::string res = "?";
|
||||
for ( unsigned int i = 0; i < sizeof( table )/sizeof( table[0] ); ++i ) {
|
||||
for ( unsigned int i = 0; i < sizeof( table ) / sizeof( table[0] );
|
||||
++i ) {
|
||||
if ( table[i].key == key ) {
|
||||
res = table[i].str;
|
||||
break;
|
||||
@ -894,12 +852,9 @@ class dump
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <typename T, typename K>
|
||||
static
|
||||
std::string
|
||||
format_assoc( const T& table, const K& key )
|
||||
static std::string format_assoc( const T& table, const K& key )
|
||||
{
|
||||
std::string str = find_value_in_table( table, key );
|
||||
if ( str == "?" ) {
|
||||
@ -911,21 +866,15 @@ class dump
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
static
|
||||
std::string
|
||||
format_assoc( const T& table, const char key )
|
||||
static std::string format_assoc( const T& table, const char key )
|
||||
{
|
||||
return format_assoc( table, (const int)key );
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
static
|
||||
std::string
|
||||
section_flags( Elf_Xword flags )
|
||||
static std::string section_flags( Elf_Xword flags )
|
||||
{
|
||||
std::string ret = "";
|
||||
if ( flags & SHF_WRITE ) {
|
||||
@ -941,13 +890,9 @@ class dump
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
#define STR_FUNC_TABLE( name ) \
|
||||
template< typename T > \
|
||||
static \
|
||||
std::string \
|
||||
str_##name( const T key ) \
|
||||
template <typename T> static std::string str_##name( const T key ) \
|
||||
{ \
|
||||
return format_assoc( name##_table, key ); \
|
||||
}
|
||||
@ -970,7 +915,6 @@ class dump
|
||||
#undef DUMP_STR_FORMAT
|
||||
}; // class dump
|
||||
|
||||
|
||||
}; // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_DUMP_HPP
|
||||
|
@ -26,33 +26,30 @@ THE SOFTWARE.
|
||||
namespace ELFIO {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class S >
|
||||
class dynamic_section_accessor_template
|
||||
template <class S> class dynamic_section_accessor_template
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
dynamic_section_accessor_template( const elfio& elf_file_, S* section_ ) :
|
||||
elf_file( elf_file_ ),
|
||||
dynamic_section( section_ )
|
||||
dynamic_section_accessor_template( const elfio& elf_file_, S* section_ )
|
||||
: elf_file( elf_file_ ), dynamic_section( section_ )
|
||||
{
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Xword
|
||||
get_entries_num() const
|
||||
Elf_Xword get_entries_num() const
|
||||
{
|
||||
Elf_Xword nRet = 0;
|
||||
|
||||
if ( 0 != dynamic_section->get_entry_size() ) {
|
||||
nRet = dynamic_section->get_size() / dynamic_section->get_entry_size();
|
||||
nRet =
|
||||
dynamic_section->get_size() / dynamic_section->get_entry_size();
|
||||
}
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_entry( Elf_Xword index,
|
||||
bool get_entry( Elf_Xword index,
|
||||
Elf_Xword& tag,
|
||||
Elf_Xword& value,
|
||||
std::string& str ) const
|
||||
@ -69,9 +66,7 @@ class dynamic_section_accessor_template
|
||||
}
|
||||
|
||||
// If the tag may have a string table reference, prepare the string
|
||||
if ( tag == DT_NEEDED ||
|
||||
tag == DT_SONAME ||
|
||||
tag == DT_RPATH ||
|
||||
if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH ||
|
||||
tag == DT_RUNPATH ) {
|
||||
string_section_accessor strsec =
|
||||
elf_file.sections[get_string_table_index()];
|
||||
@ -90,9 +85,7 @@ class dynamic_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf_Xword tag,
|
||||
Elf_Xword value )
|
||||
void add_entry( Elf_Xword tag, Elf_Xword value )
|
||||
{
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
generic_add_entry<Elf32_Dyn>( tag, value );
|
||||
@ -103,9 +96,7 @@ class dynamic_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf_Xword tag,
|
||||
const std::string& str )
|
||||
void add_entry( Elf_Xword tag, const std::string& str )
|
||||
{
|
||||
string_section_accessor strsec =
|
||||
elf_file.sections[get_string_table_index()];
|
||||
@ -116,16 +107,14 @@ class dynamic_section_accessor_template
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_string_table_index() const
|
||||
Elf_Half get_string_table_index() const
|
||||
{
|
||||
return (Elf_Half)dynamic_section->get_link();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void
|
||||
generic_get_entry_dyn( Elf_Xword index,
|
||||
void generic_get_entry_dyn( Elf_Xword index,
|
||||
Elf_Xword& tag,
|
||||
Elf_Xword& value ) const
|
||||
{
|
||||
@ -133,7 +122,8 @@ class dynamic_section_accessor_template
|
||||
|
||||
// Check unusual case when dynamic section has no data
|
||||
if ( dynamic_section->get_data() == 0 ||
|
||||
( index + 1 ) * dynamic_section->get_entry_size() > dynamic_section->get_size() ) {
|
||||
( index + 1 ) * dynamic_section->get_entry_size() >
|
||||
dynamic_section->get_size() ) {
|
||||
tag = DT_NULL;
|
||||
value = 0;
|
||||
return;
|
||||
@ -188,9 +178,7 @@ class dynamic_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class T >
|
||||
void
|
||||
generic_add_entry( Elf_Xword tag, Elf_Xword value )
|
||||
template <class T> void generic_add_entry( Elf_Xword tag, Elf_Xword value )
|
||||
{
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
@ -240,7 +228,8 @@ class dynamic_section_accessor_template
|
||||
|
||||
entry.d_tag = convertor( tag );
|
||||
|
||||
dynamic_section->append_data( reinterpret_cast<char*>( &entry ), sizeof( entry ) );
|
||||
dynamic_section->append_data( reinterpret_cast<char*>( &entry ),
|
||||
sizeof( entry ) );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@ -250,7 +239,8 @@ class dynamic_section_accessor_template
|
||||
};
|
||||
|
||||
using dynamic_section_accessor = dynamic_section_accessor_template<section>;
|
||||
using const_dynamic_section_accessor = dynamic_section_accessor_template<const section>;
|
||||
using const_dynamic_section_accessor =
|
||||
dynamic_section_accessor_template<const section>;
|
||||
|
||||
} // namespace ELFIO
|
||||
|
||||
|
@ -56,14 +56,15 @@ class elf_header
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Half, section_name_str_index );
|
||||
};
|
||||
|
||||
|
||||
template <class T> struct elf_header_impl_types;
|
||||
template<> struct elf_header_impl_types<Elf32_Ehdr> {
|
||||
template <> struct elf_header_impl_types<Elf32_Ehdr>
|
||||
{
|
||||
typedef Elf32_Phdr Phdr_type;
|
||||
typedef Elf32_Shdr Shdr_type;
|
||||
static const unsigned char file_class = ELFCLASS32;
|
||||
};
|
||||
template<> struct elf_header_impl_types<Elf64_Ehdr> {
|
||||
template <> struct elf_header_impl_types<Elf64_Ehdr>
|
||||
{
|
||||
typedef Elf64_Phdr Phdr_type;
|
||||
typedef Elf64_Shdr Shdr_type;
|
||||
static const unsigned char file_class = ELFCLASS64;
|
||||
@ -73,12 +74,12 @@ template< class T > class elf_header_impl : public elf_header
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
elf_header_impl( endianess_convertor* convertor_,
|
||||
unsigned char encoding )
|
||||
elf_header_impl( endianess_convertor* convertor_, unsigned char encoding )
|
||||
{
|
||||
convertor = convertor_;
|
||||
|
||||
std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), '\0' );
|
||||
std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ),
|
||||
'\0' );
|
||||
|
||||
header.e_ident[EI_MAG0] = ELFMAG0;
|
||||
header.e_ident[EI_MAG1] = ELFMAG1;
|
||||
@ -91,15 +92,16 @@ template< class T > class elf_header_impl : public elf_header
|
||||
header.e_ehsize = ( sizeof( header ) );
|
||||
header.e_ehsize = ( *convertor )( header.e_ehsize );
|
||||
header.e_shstrndx = ( *convertor )( (Elf_Half)1 );
|
||||
header.e_phentsize = sizeof( typename elf_header_impl_types<T>::Phdr_type );
|
||||
header.e_shentsize = sizeof( typename elf_header_impl_types<T>::Shdr_type );
|
||||
header.e_phentsize =
|
||||
sizeof( typename elf_header_impl_types<T>::Phdr_type );
|
||||
header.e_shentsize =
|
||||
sizeof( typename elf_header_impl_types<T>::Shdr_type );
|
||||
header.e_phentsize = ( *convertor )( header.e_phentsize );
|
||||
header.e_shentsize = ( *convertor )( header.e_shentsize );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
load( std::istream& stream )
|
||||
bool load( std::istream& stream )
|
||||
{
|
||||
stream.seekg( 0 );
|
||||
stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) );
|
||||
@ -108,11 +110,11 @@ template< class T > class elf_header_impl : public elf_header
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
save( std::ostream& stream ) const
|
||||
bool save( std::ostream& stream ) const
|
||||
{
|
||||
stream.seekp( 0 );
|
||||
stream.write( reinterpret_cast<const char*>( &header ), sizeof( header ) );
|
||||
stream.write( reinterpret_cast<const char*>( &header ),
|
||||
sizeof( header ) );
|
||||
|
||||
return stream.good();
|
||||
}
|
||||
@ -128,7 +130,9 @@ template< class T > class elf_header_impl : public elf_header
|
||||
|
||||
ELFIO_GET_SET_ACCESS( Elf_Word, version, header.e_version );
|
||||
ELFIO_GET_SET_ACCESS( unsigned char, os_abi, header.e_ident[EI_OSABI] );
|
||||
ELFIO_GET_SET_ACCESS( unsigned char, abi_version, header.e_ident[EI_ABIVERSION] );
|
||||
ELFIO_GET_SET_ACCESS( unsigned char,
|
||||
abi_version,
|
||||
header.e_ident[EI_ABIVERSION] );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Half, type, header.e_type );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Half, machine, header.e_machine );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Word, flags, header.e_flags );
|
||||
|
@ -38,27 +38,24 @@ namespace ELFIO {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class S >
|
||||
class note_section_accessor_template
|
||||
template <class S> class note_section_accessor_template
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
note_section_accessor_template( const elfio& elf_file_, S* section_ ) :
|
||||
elf_file( elf_file_ ), note_section( section_ )
|
||||
note_section_accessor_template( const elfio& elf_file_, S* section_ )
|
||||
: elf_file( elf_file_ ), note_section( section_ )
|
||||
{
|
||||
process_section();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
get_notes_num() const
|
||||
Elf_Word get_notes_num() const
|
||||
{
|
||||
return (Elf_Word)note_start_positions.size();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_note( Elf_Word index,
|
||||
bool get_note( Elf_Word index,
|
||||
Elf_Word& type,
|
||||
std::string& name,
|
||||
void*& desc,
|
||||
@ -68,7 +65,8 @@ class note_section_accessor_template
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* pData = note_section->get_data() + note_start_positions[index];
|
||||
const char* pData =
|
||||
note_section->get_data() + note_start_positions[index];
|
||||
int align = sizeof( Elf_Word );
|
||||
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
@ -76,9 +74,9 @@ class note_section_accessor_template
|
||||
Elf_Word namesz = convertor( *(const Elf_Word*)( pData ) );
|
||||
descSize = convertor( *(const Elf_Word*)( pData + sizeof( namesz ) ) );
|
||||
|
||||
Elf_Xword max_name_size = note_section->get_size() - note_start_positions[index];
|
||||
if ( namesz < 1 ||
|
||||
namesz > max_name_size ||
|
||||
Elf_Xword max_name_size =
|
||||
note_section->get_size() - note_start_positions[index];
|
||||
if ( namesz < 1 || namesz > max_name_size ||
|
||||
(Elf_Xword)namesz + descSize > max_name_size ) {
|
||||
return false;
|
||||
}
|
||||
@ -87,7 +85,8 @@ class note_section_accessor_template
|
||||
desc = 0;
|
||||
}
|
||||
else {
|
||||
desc = const_cast<char*> ( pData + 3*align +
|
||||
desc =
|
||||
const_cast<char*>( pData + 3 * align +
|
||||
( ( namesz + align - 1 ) / align ) * align );
|
||||
}
|
||||
|
||||
@ -107,6 +106,7 @@ class note_section_accessor_template
|
||||
Elf_Word nameLenConv = convertor( nameLen );
|
||||
std::string buffer( reinterpret_cast<char*>( &nameLenConv ), align );
|
||||
Elf_Word descSizeConv = convertor( descSize );
|
||||
|
||||
buffer.append( reinterpret_cast<char*>( &descSizeConv ), align );
|
||||
type = convertor( type );
|
||||
buffer.append( reinterpret_cast<char*>( &type ), align );
|
||||
@ -146,8 +146,7 @@ class note_section_accessor_template
|
||||
Elf_Word align = sizeof( Elf_Word );
|
||||
while ( current + (Elf_Xword)3 * align <= size ) {
|
||||
note_start_positions.push_back( current );
|
||||
Elf_Word namesz = convertor(
|
||||
*(const Elf_Word*)( data + current ) );
|
||||
Elf_Word namesz = convertor( *(const Elf_Word*)( data + current ) );
|
||||
Elf_Word descsz = convertor(
|
||||
*(const Elf_Word*)( data + current + sizeof( namesz ) ) );
|
||||
|
||||
@ -165,7 +164,8 @@ class note_section_accessor_template
|
||||
};
|
||||
|
||||
using note_section_accessor = note_section_accessor_template<section>;
|
||||
using const_note_section_accessor = note_section_accessor_template<const section>;
|
||||
using const_note_section_accessor =
|
||||
note_section_accessor_template<const section>;
|
||||
|
||||
} // namespace ELFIO
|
||||
|
||||
|
@ -50,56 +50,40 @@ template<> struct get_sym_and_type< Elf32_Rela >
|
||||
};
|
||||
template <> struct get_sym_and_type<Elf64_Rel>
|
||||
{
|
||||
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 );
|
||||
}
|
||||
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 ); }
|
||||
};
|
||||
template <> struct get_sym_and_type<Elf64_Rela>
|
||||
{
|
||||
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 );
|
||||
}
|
||||
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 ); }
|
||||
};
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class S >
|
||||
class relocation_section_accessor_template
|
||||
template <class S> class relocation_section_accessor_template
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
relocation_section_accessor_template( const elfio& elf_file_, S* section_ ) :
|
||||
elf_file( elf_file_ ),
|
||||
relocation_section( section_ )
|
||||
relocation_section_accessor_template( const elfio& elf_file_, S* section_ )
|
||||
: elf_file( elf_file_ ), relocation_section( section_ )
|
||||
{
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Xword
|
||||
get_entries_num() const
|
||||
Elf_Xword get_entries_num() const
|
||||
{
|
||||
Elf_Xword nRet = 0;
|
||||
|
||||
if ( 0 != relocation_section->get_entry_size() ) {
|
||||
nRet = relocation_section->get_size() / relocation_section->get_entry_size();
|
||||
nRet = relocation_section->get_size() /
|
||||
relocation_section->get_entry_size();
|
||||
}
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_entry( Elf_Xword index,
|
||||
bool get_entry( Elf_Xword index,
|
||||
Elf64_Addr& offset,
|
||||
Elf_Word& symbol,
|
||||
Elf_Word& type,
|
||||
@ -111,22 +95,22 @@ class relocation_section_accessor_template
|
||||
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
if ( SHT_REL == relocation_section->get_type() ) {
|
||||
generic_get_entry_rel< Elf32_Rel >( index, offset, symbol,
|
||||
type, addend );
|
||||
generic_get_entry_rel<Elf32_Rel>( index, offset, symbol, type,
|
||||
addend );
|
||||
}
|
||||
else if ( SHT_RELA == relocation_section->get_type() ) {
|
||||
generic_get_entry_rela< Elf32_Rela >( index, offset, symbol,
|
||||
type, addend );
|
||||
generic_get_entry_rela<Elf32_Rela>( index, offset, symbol, type,
|
||||
addend );
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ( SHT_REL == relocation_section->get_type() ) {
|
||||
generic_get_entry_rel< Elf64_Rel >( index, offset, symbol,
|
||||
type, addend );
|
||||
generic_get_entry_rel<Elf64_Rel>( index, offset, symbol, type,
|
||||
addend );
|
||||
}
|
||||
else if ( SHT_RELA == relocation_section->get_type() ) {
|
||||
generic_get_entry_rela< Elf64_Rela >( index, offset, symbol,
|
||||
type, addend );
|
||||
generic_get_entry_rela<Elf64_Rela>( index, offset, symbol, type,
|
||||
addend );
|
||||
}
|
||||
}
|
||||
|
||||
@ -134,8 +118,7 @@ class relocation_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_entry( Elf_Xword index,
|
||||
bool get_entry( Elf_Xword index,
|
||||
Elf64_Addr& offset,
|
||||
Elf64_Addr& symbolValue,
|
||||
std::string& symbolName,
|
||||
@ -154,9 +137,10 @@ class relocation_section_accessor_template
|
||||
Elf_Half section;
|
||||
unsigned char other;
|
||||
|
||||
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 );
|
||||
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 ) {
|
||||
@ -201,8 +185,7 @@ class relocation_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
set_entry(Elf_Xword index,
|
||||
bool set_entry( Elf_Xword index,
|
||||
Elf64_Addr offset,
|
||||
Elf_Word symbol,
|
||||
Elf_Word type,
|
||||
@ -214,18 +197,22 @@ class relocation_section_accessor_template
|
||||
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
if ( SHT_REL == relocation_section->get_type() ) {
|
||||
generic_set_entry_rel<Elf32_Rel>(index, offset, symbol, type, addend);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
generic_set_entry_rela<Elf64_Rela>( index, offset, symbol, type,
|
||||
addend );
|
||||
}
|
||||
}
|
||||
|
||||
@ -233,8 +220,7 @@ class relocation_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf64_Addr offset, Elf_Xword info )
|
||||
void add_entry( Elf64_Addr offset, Elf_Xword info )
|
||||
{
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
generic_add_entry<Elf32_Rel>( offset, info );
|
||||
@ -245,8 +231,7 @@ class relocation_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned char type )
|
||||
void add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned char type )
|
||||
{
|
||||
Elf_Xword info;
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
@ -260,8 +245,7 @@ class relocation_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend )
|
||||
void add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend )
|
||||
{
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
generic_add_entry<Elf32_Rela>( offset, info, addend );
|
||||
@ -272,8 +256,9 @@ class relocation_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned char type,
|
||||
void add_entry( Elf64_Addr offset,
|
||||
Elf_Word symbol,
|
||||
unsigned char type,
|
||||
Elf_Sxword addend )
|
||||
{
|
||||
Elf_Xword info;
|
||||
@ -288,8 +273,7 @@ class relocation_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( string_section_accessor str_writer,
|
||||
void add_entry( string_section_accessor str_writer,
|
||||
const char* str,
|
||||
symbol_section_accessor sym_writer,
|
||||
Elf64_Addr value,
|
||||
@ -307,22 +291,18 @@ class relocation_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
swap_symbols(Elf_Xword first, Elf_Xword second)
|
||||
void swap_symbols( Elf_Xword first, Elf_Xword second )
|
||||
{
|
||||
Elf64_Addr offset;
|
||||
Elf_Word symbol;
|
||||
Elf_Word rtype;
|
||||
Elf_Sxword addend;
|
||||
for (Elf_Word i = 0; i < get_entries_num(); i++)
|
||||
{
|
||||
for ( Elf_Word i = 0; i < get_entries_num(); i++ ) {
|
||||
get_entry( i, offset, symbol, rtype, addend );
|
||||
if (symbol == first)
|
||||
{
|
||||
if ( symbol == first ) {
|
||||
set_entry( i, offset, (Elf_Word)second, rtype, addend );
|
||||
}
|
||||
if (symbol == second)
|
||||
{
|
||||
if ( symbol == second ) {
|
||||
set_entry( i, offset, (Elf_Word)first, rtype, addend );
|
||||
}
|
||||
}
|
||||
@ -331,16 +311,14 @@ class relocation_section_accessor_template
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_symbol_table_index() const
|
||||
Elf_Half get_symbol_table_index() const
|
||||
{
|
||||
return (Elf_Half)relocation_section->get_link();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void
|
||||
generic_get_entry_rel( Elf_Xword index,
|
||||
void generic_get_entry_rel( Elf_Xword index,
|
||||
Elf64_Addr& offset,
|
||||
Elf_Word& symbol,
|
||||
Elf_Word& type,
|
||||
@ -360,8 +338,7 @@ class relocation_section_accessor_template
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void
|
||||
generic_get_entry_rela( Elf_Xword index,
|
||||
void generic_get_entry_rela( Elf_Xword index,
|
||||
Elf64_Addr& offset,
|
||||
Elf_Word& symbol,
|
||||
Elf_Word& type,
|
||||
@ -381,8 +358,7 @@ class relocation_section_accessor_template
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void
|
||||
generic_set_entry_rel(Elf_Xword index,
|
||||
void generic_set_entry_rel( Elf_Xword index,
|
||||
Elf64_Addr offset,
|
||||
Elf_Word symbol,
|
||||
Elf_Word type,
|
||||
@ -390,7 +366,8 @@ class relocation_section_accessor_template
|
||||
{
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
T *pEntry = const_cast<T *>(reinterpret_cast<const T *>(relocation_section->get_data() +
|
||||
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 ) {
|
||||
@ -406,8 +383,7 @@ class relocation_section_accessor_template
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void
|
||||
generic_set_entry_rela(Elf_Xword index,
|
||||
void generic_set_entry_rela( Elf_Xword index,
|
||||
Elf64_Addr offset,
|
||||
Elf_Word symbol,
|
||||
Elf_Word type,
|
||||
@ -415,7 +391,8 @@ class relocation_section_accessor_template
|
||||
{
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
T *pEntry = const_cast<T *>(reinterpret_cast<const T *>(relocation_section->get_data() +
|
||||
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 ) {
|
||||
@ -433,8 +410,7 @@ class relocation_section_accessor_template
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void
|
||||
generic_add_entry( Elf64_Addr offset, Elf_Xword info )
|
||||
void generic_add_entry( Elf64_Addr offset, Elf_Xword info )
|
||||
{
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
@ -444,7 +420,8 @@ class relocation_section_accessor_template
|
||||
entry.r_offset = convertor( entry.r_offset );
|
||||
entry.r_info = convertor( entry.r_info );
|
||||
|
||||
relocation_section->append_data( reinterpret_cast<char*>( &entry ), sizeof( entry ) );
|
||||
relocation_section->append_data( reinterpret_cast<char*>( &entry ),
|
||||
sizeof( entry ) );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@ -462,7 +439,8 @@ class relocation_section_accessor_template
|
||||
entry.r_info = convertor( entry.r_info );
|
||||
entry.r_addend = convertor( entry.r_addend );
|
||||
|
||||
relocation_section->append_data( reinterpret_cast<char*>( &entry ), sizeof( entry ) );
|
||||
relocation_section->append_data( reinterpret_cast<char*>( &entry ),
|
||||
sizeof( entry ) );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
@ -471,8 +449,10 @@ class relocation_section_accessor_template
|
||||
S* relocation_section;
|
||||
};
|
||||
|
||||
using relocation_section_accessor = relocation_section_accessor_template<section>;
|
||||
using const_relocation_section_accessor = relocation_section_accessor_template<const section>;
|
||||
using relocation_section_accessor =
|
||||
relocation_section_accessor_template<section>;
|
||||
using const_relocation_section_accessor =
|
||||
relocation_section_accessor_template<const section>;
|
||||
|
||||
} // namespace ELFIO
|
||||
|
||||
|
@ -31,6 +31,7 @@ namespace ELFIO {
|
||||
class section
|
||||
{
|
||||
friend class elfio;
|
||||
|
||||
public:
|
||||
virtual ~section(){};
|
||||
|
||||
@ -59,23 +60,22 @@ protected:
|
||||
ELFIO_SET_ACCESS_DECL( Elf64_Off, offset );
|
||||
ELFIO_SET_ACCESS_DECL( Elf_Half, index );
|
||||
|
||||
virtual void load( std::istream& stream,
|
||||
std::streampos header_offset ) = 0;
|
||||
virtual void load( std::istream& stream, std::streampos header_offset ) = 0;
|
||||
virtual void save( std::ostream& stream,
|
||||
std::streampos header_offset,
|
||||
std::streampos data_offset ) = 0;
|
||||
virtual bool is_address_initialized() const = 0;
|
||||
};
|
||||
|
||||
|
||||
template< class T >
|
||||
class section_impl : public section
|
||||
template <class T> class section_impl : public section
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
section_impl( const endianess_convertor* convertor_ ) : convertor( convertor_ )
|
||||
section_impl( const endianess_convertor* convertor_ )
|
||||
: convertor( convertor_ )
|
||||
{
|
||||
std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), '\0' );
|
||||
std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ),
|
||||
'\0' );
|
||||
is_address_set = false;
|
||||
data = 0;
|
||||
data_size = 0;
|
||||
@ -84,10 +84,7 @@ class section_impl : public section
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
~section_impl()
|
||||
{
|
||||
delete [] data;
|
||||
}
|
||||
~section_impl() { delete[] data; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Section info functions
|
||||
@ -102,30 +99,16 @@ class section_impl : public section
|
||||
ELFIO_GET_ACCESS( Elf64_Addr, address, header.sh_addr );
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_index() const
|
||||
{
|
||||
return index;
|
||||
}
|
||||
|
||||
Elf_Half get_index() const { return index; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
std::string
|
||||
get_name() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
std::string get_name() const { return name; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_name( std::string name_ )
|
||||
{
|
||||
name = name_;
|
||||
}
|
||||
void set_name( std::string name_ ) { name = name_; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_address( Elf64_Addr value )
|
||||
void set_address( Elf64_Addr value )
|
||||
{
|
||||
header.sh_addr = value;
|
||||
header.sh_addr = ( *convertor )( header.sh_addr );
|
||||
@ -133,28 +116,20 @@ class section_impl : public section
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
is_address_initialized() const
|
||||
{
|
||||
return is_address_set;
|
||||
}
|
||||
bool is_address_initialized() const { return is_address_set; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
const char*
|
||||
get_data() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
const char* get_data() const { return data; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_data( const char* raw_data, Elf_Word size )
|
||||
void set_data( const char* raw_data, Elf_Word size )
|
||||
{
|
||||
if ( get_type() != SHT_NOBITS ) {
|
||||
delete[] data;
|
||||
try {
|
||||
data = new char[size];
|
||||
} catch (const std::bad_alloc&) {
|
||||
}
|
||||
catch ( const std::bad_alloc& ) {
|
||||
data = 0;
|
||||
data_size = 0;
|
||||
size = 0;
|
||||
@ -169,15 +144,13 @@ class section_impl : public section
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_data( const std::string& str_data )
|
||||
void set_data( const std::string& str_data )
|
||||
{
|
||||
return set_data( str_data.c_str(), (Elf_Word)str_data.size() );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
append_data( const char* raw_data, Elf_Word size )
|
||||
void append_data( const char* raw_data, Elf_Word size )
|
||||
{
|
||||
if ( get_type() != SHT_NOBITS ) {
|
||||
if ( get_size() + size < data_size ) {
|
||||
@ -188,13 +161,15 @@ class section_impl : public section
|
||||
char* new_data;
|
||||
try {
|
||||
new_data = new char[data_size];
|
||||
} catch (const std::bad_alloc&) {
|
||||
}
|
||||
catch ( const std::bad_alloc& ) {
|
||||
new_data = 0;
|
||||
size = 0;
|
||||
}
|
||||
if ( 0 != new_data ) {
|
||||
std::copy( data, data + get_size(), new_data );
|
||||
std::copy( raw_data, raw_data + size, new_data + get_size() );
|
||||
std::copy( raw_data, raw_data + size,
|
||||
new_data + get_size() );
|
||||
delete[] data;
|
||||
data = new_data;
|
||||
}
|
||||
@ -204,8 +179,7 @@ class section_impl : public section
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
append_data( const std::string& str_data )
|
||||
void append_data( const std::string& str_data )
|
||||
{
|
||||
return append_data( str_data.c_str(), (Elf_Word)str_data.size() );
|
||||
}
|
||||
@ -216,18 +190,13 @@ class section_impl : public section
|
||||
ELFIO_GET_SET_ACCESS( Elf64_Off, offset, header.sh_offset );
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_index( Elf_Half value )
|
||||
{
|
||||
index = value;
|
||||
}
|
||||
void set_index( Elf_Half value ) { index = value; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
load( std::istream& stream,
|
||||
std::streampos header_offset )
|
||||
void load( std::istream& stream, std::streampos header_offset )
|
||||
{
|
||||
std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), '\0' );
|
||||
std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ),
|
||||
'\0' );
|
||||
|
||||
stream.seekg( 0, stream.end );
|
||||
set_stream_size( stream.tellg() );
|
||||
@ -235,12 +204,13 @@ class section_impl : public section
|
||||
stream.seekg( header_offset );
|
||||
stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) );
|
||||
|
||||
|
||||
Elf_Xword size = get_size();
|
||||
if ( 0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type() && size < get_stream_size()) {
|
||||
if ( 0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type() &&
|
||||
size < get_stream_size() ) {
|
||||
try {
|
||||
data = new char[size + 1];
|
||||
} catch (const std::bad_alloc&) {
|
||||
}
|
||||
catch ( const std::bad_alloc& ) {
|
||||
data = 0;
|
||||
data_size = 0;
|
||||
}
|
||||
@ -255,8 +225,7 @@ class section_impl : public section
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
save( std::ostream& stream,
|
||||
void save( std::ostream& stream,
|
||||
std::streampos header_offset,
|
||||
std::streampos data_offset )
|
||||
{
|
||||
@ -275,34 +244,25 @@ class section_impl : public section
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
save_header( std::ostream& stream,
|
||||
std::streampos header_offset ) const
|
||||
void save_header( std::ostream& stream, std::streampos header_offset ) const
|
||||
{
|
||||
stream.seekp( header_offset );
|
||||
stream.write( reinterpret_cast<const char*>( &header ), sizeof( header ) );
|
||||
stream.write( reinterpret_cast<const char*>( &header ),
|
||||
sizeof( header ) );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
save_data( std::ostream& stream,
|
||||
std::streampos data_offset ) const
|
||||
void save_data( std::ostream& stream, std::streampos data_offset ) const
|
||||
{
|
||||
stream.seekp( data_offset );
|
||||
stream.write( get_data(), get_size() );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
size_t get_stream_size() const
|
||||
{
|
||||
return stream_size;
|
||||
}
|
||||
size_t get_stream_size() const { return stream_size; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void set_stream_size(size_t value)
|
||||
{
|
||||
stream_size = value;
|
||||
}
|
||||
void set_stream_size( size_t value ) { stream_size = value; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
|
@ -31,6 +31,7 @@ namespace ELFIO {
|
||||
class segment
|
||||
{
|
||||
friend class elfio;
|
||||
|
||||
public:
|
||||
virtual ~segment(){};
|
||||
|
||||
@ -46,7 +47,8 @@ class segment
|
||||
|
||||
virtual const char* get_data() const = 0;
|
||||
|
||||
virtual Elf_Half add_section_index( Elf_Half index, Elf_Xword addr_align ) = 0;
|
||||
virtual Elf_Half add_section_index( Elf_Half index,
|
||||
Elf_Xword addr_align ) = 0;
|
||||
virtual Elf_Half get_sections_num() const = 0;
|
||||
virtual Elf_Half get_section_index_at( Elf_Half num ) const = 0;
|
||||
virtual bool is_offset_initialized() const = 0;
|
||||
@ -57,29 +59,25 @@ class segment
|
||||
|
||||
virtual const std::vector<Elf_Half>& get_sections() const = 0;
|
||||
virtual void load( std::istream& stream, std::streampos header_offset ) = 0;
|
||||
virtual void save( std::ostream& stream, std::streampos header_offset,
|
||||
virtual void save( std::ostream& stream,
|
||||
std::streampos header_offset,
|
||||
std::streampos data_offset ) = 0;
|
||||
};
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class T >
|
||||
class segment_impl : public segment
|
||||
template <class T> class segment_impl : public segment
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
segment_impl( endianess_convertor* convertor_ ) :
|
||||
stream_size( 0 ), index( 0 ), data( 0 ), convertor( convertor_ )
|
||||
segment_impl( endianess_convertor* convertor_ )
|
||||
: stream_size( 0 ), index( 0 ), data( 0 ), convertor( convertor_ )
|
||||
{
|
||||
is_offset_set = false;
|
||||
std::fill_n( reinterpret_cast<char*>( &ph ), sizeof( ph ), '\0' );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
virtual ~segment_impl()
|
||||
{
|
||||
delete [] data;
|
||||
}
|
||||
virtual ~segment_impl() { delete[] data; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Section info functions
|
||||
@ -94,36 +92,19 @@ class segment_impl : public segment
|
||||
size_t stream_size;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
size_t
|
||||
get_stream_size() const
|
||||
{
|
||||
return stream_size;
|
||||
}
|
||||
size_t get_stream_size() const { return stream_size; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_stream_size(size_t value)
|
||||
{
|
||||
stream_size = value;
|
||||
}
|
||||
void set_stream_size( size_t value ) { stream_size = value; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_index() const
|
||||
{
|
||||
return index;
|
||||
}
|
||||
Elf_Half get_index() const { return index; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
const char*
|
||||
get_data() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
const char* get_data() const { return data; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
add_section_index( Elf_Half sec_index, Elf_Xword addr_align )
|
||||
Elf_Half add_section_index( Elf_Half sec_index, Elf_Xword addr_align )
|
||||
{
|
||||
sections.push_back( sec_index );
|
||||
if ( addr_align > get_align() ) {
|
||||
@ -134,15 +115,10 @@ class segment_impl : public segment
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_sections_num() const
|
||||
{
|
||||
return (Elf_Half)sections.size();
|
||||
}
|
||||
Elf_Half get_sections_num() const { return (Elf_Half)sections.size(); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_section_index_at( Elf_Half num ) const
|
||||
Elf_Half get_section_index_at( Elf_Half num ) const
|
||||
{
|
||||
if ( num < sections.size() ) {
|
||||
return sections[num];
|
||||
@ -156,8 +132,7 @@ class segment_impl : public segment
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_offset( Elf64_Off value )
|
||||
void set_offset( Elf64_Off value )
|
||||
{
|
||||
ph.p_offset = value;
|
||||
ph.p_offset = ( *convertor )( ph.p_offset );
|
||||
@ -165,30 +140,16 @@ class segment_impl : public segment
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
is_offset_initialized() const
|
||||
{
|
||||
return is_offset_set;
|
||||
}
|
||||
bool is_offset_initialized() const { return is_offset_set; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
const std::vector<Elf_Half>&
|
||||
get_sections() const
|
||||
{
|
||||
return sections;
|
||||
}
|
||||
const std::vector<Elf_Half>& get_sections() const { return sections; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_index( Elf_Half value )
|
||||
{
|
||||
index = value;
|
||||
}
|
||||
void set_index( Elf_Half value ) { index = value; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
load( std::istream& stream,
|
||||
std::streampos header_offset )
|
||||
void load( std::istream& stream, std::streampos header_offset )
|
||||
{
|
||||
|
||||
stream.seekg( 0, stream.end );
|
||||
@ -208,7 +169,8 @@ class segment_impl : public segment
|
||||
else {
|
||||
try {
|
||||
data = new char[size + 1];
|
||||
} catch (const std::bad_alloc&) {
|
||||
}
|
||||
catch ( const std::bad_alloc& ) {
|
||||
data = 0;
|
||||
}
|
||||
|
||||
|
@ -30,20 +30,16 @@ THE SOFTWARE.
|
||||
namespace ELFIO {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class S >
|
||||
class string_section_accessor_template
|
||||
template <class S> class string_section_accessor_template
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
string_section_accessor_template( S* section_ ) :
|
||||
string_section( section_ )
|
||||
string_section_accessor_template( S* section_ ) : string_section( section_ )
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
const char*
|
||||
get_string( Elf_Word index ) const
|
||||
const char* get_string( Elf_Word index ) const
|
||||
{
|
||||
if ( string_section ) {
|
||||
if ( index < string_section->get_size() ) {
|
||||
@ -57,10 +53,8 @@ class string_section_accessor_template
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
add_string( const char* str )
|
||||
Elf_Word add_string( const char* str )
|
||||
{
|
||||
Elf_Word current_position = 0;
|
||||
|
||||
@ -73,16 +67,15 @@ class string_section_accessor_template
|
||||
string_section->append_data( &empty_string, 1 );
|
||||
current_position++;
|
||||
}
|
||||
string_section->append_data( str, (Elf_Word)std::strlen( str ) + 1 );
|
||||
string_section->append_data( str,
|
||||
(Elf_Word)std::strlen( str ) + 1 );
|
||||
}
|
||||
|
||||
return current_position;
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
add_string( const std::string& str )
|
||||
Elf_Word add_string( const std::string& str )
|
||||
{
|
||||
return add_string( str.c_str() );
|
||||
}
|
||||
@ -93,7 +86,8 @@ class string_section_accessor_template
|
||||
};
|
||||
|
||||
using string_section_accessor = string_section_accessor_template<section>;
|
||||
using const_string_section_accessor = string_section_accessor_template<const section>;
|
||||
using const_string_section_accessor =
|
||||
string_section_accessor_template<const section>;
|
||||
|
||||
} // namespace ELFIO
|
||||
|
||||
|
@ -26,33 +26,31 @@ THE SOFTWARE.
|
||||
namespace ELFIO {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class S >
|
||||
class symbol_section_accessor_template
|
||||
template <class S> class symbol_section_accessor_template
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
symbol_section_accessor_template( const elfio& elf_file_, S* symbol_section_ ) :
|
||||
elf_file( elf_file_ ),
|
||||
symbol_section( symbol_section_ )
|
||||
symbol_section_accessor_template( const elfio& elf_file_,
|
||||
S* symbol_section_ )
|
||||
: elf_file( elf_file_ ), symbol_section( symbol_section_ )
|
||||
{
|
||||
find_hash_section();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Xword
|
||||
get_symbols_num() const
|
||||
Elf_Xword get_symbols_num() const
|
||||
{
|
||||
Elf_Xword nRet = 0;
|
||||
if ( 0 != symbol_section->get_entry_size() ) {
|
||||
nRet = symbol_section->get_size() / symbol_section->get_entry_size();
|
||||
nRet =
|
||||
symbol_section->get_size() / symbol_section->get_entry_size();
|
||||
}
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_symbol( Elf_Xword index,
|
||||
bool get_symbol( Elf_Xword index,
|
||||
std::string& name,
|
||||
Elf64_Addr& value,
|
||||
Elf_Xword& size,
|
||||
@ -76,8 +74,7 @@ class symbol_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_symbol( const std::string& name,
|
||||
bool get_symbol( const std::string& name,
|
||||
Elf64_Addr& value,
|
||||
Elf_Xword& size,
|
||||
unsigned char& bind,
|
||||
@ -93,13 +90,16 @@ class symbol_section_accessor_template
|
||||
sizeof( Elf_Word ) );
|
||||
Elf_Word val = elf_hash( (const unsigned char*)name.c_str() );
|
||||
Elf_Word y = *(const Elf_Word*)( hash_section->get_data() +
|
||||
( 2 + val % nbucket ) * sizeof( Elf_Word ) );
|
||||
( 2 + val % nbucket ) *
|
||||
sizeof( Elf_Word ) );
|
||||
std::string str;
|
||||
get_symbol( y, str, value, size, bind, type, section_index, other );
|
||||
while ( str != name && STN_UNDEF != y && y < nchain ) {
|
||||
y = *(const Elf_Word*)( hash_section->get_data() +
|
||||
( 2 + nbucket + y ) * sizeof( Elf_Word ) );
|
||||
get_symbol( y, str, value, size, bind, type, section_index, other );
|
||||
( 2 + nbucket + y ) *
|
||||
sizeof( Elf_Word ) );
|
||||
get_symbol( y, str, value, size, bind, type, section_index,
|
||||
other );
|
||||
}
|
||||
if ( str == name ) {
|
||||
ret = true;
|
||||
@ -121,8 +121,7 @@ class symbol_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_symbol( const Elf64_Addr& value,
|
||||
bool get_symbol( const Elf64_Addr& value,
|
||||
std::string& name,
|
||||
Elf_Xword& size,
|
||||
unsigned char& bind,
|
||||
@ -131,7 +130,6 @@ class symbol_section_accessor_template
|
||||
unsigned char& other ) const
|
||||
{
|
||||
|
||||
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
Elf_Xword idx = 0;
|
||||
@ -139,26 +137,34 @@ class symbol_section_accessor_template
|
||||
Elf64_Addr v = 0;
|
||||
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
match = generic_search_symbols<Elf32_Sym>([&](const Elf32_Sym* sym) {
|
||||
match = generic_search_symbols<Elf32_Sym>(
|
||||
[&]( const Elf32_Sym* sym ) {
|
||||
return convertor( sym->st_value ) == value;
|
||||
}, idx);
|
||||
} else {
|
||||
match = generic_search_symbols<Elf64_Sym>([&](const Elf64_Sym* sym) {
|
||||
},
|
||||
idx );
|
||||
}
|
||||
else {
|
||||
match = generic_search_symbols<Elf64_Sym>(
|
||||
[&]( const Elf64_Sym* sym ) {
|
||||
return convertor( sym->st_value ) == value;
|
||||
}, idx);
|
||||
},
|
||||
idx );
|
||||
}
|
||||
|
||||
if ( match ) {
|
||||
return get_symbol( idx, name, v, size, bind, type, section_index, other );
|
||||
return get_symbol( idx, name, v, size, bind, type, section_index,
|
||||
other );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size,
|
||||
unsigned char info, unsigned char other,
|
||||
Elf_Word add_symbol( Elf_Word name,
|
||||
Elf64_Addr value,
|
||||
Elf_Xword size,
|
||||
unsigned char info,
|
||||
unsigned char other,
|
||||
Elf_Half shndx )
|
||||
{
|
||||
Elf_Word nRet;
|
||||
@ -173,31 +179,37 @@ class symbol_section_accessor_template
|
||||
}
|
||||
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
nRet = generic_add_symbol<Elf32_Sym>( name, value, size, info, other,
|
||||
shndx );
|
||||
nRet = generic_add_symbol<Elf32_Sym>( name, value, size, info,
|
||||
other, shndx );
|
||||
}
|
||||
else {
|
||||
nRet = generic_add_symbol<Elf64_Sym>( name, value, size, info, other,
|
||||
shndx );
|
||||
nRet = generic_add_symbol<Elf64_Sym>( name, value, size, info,
|
||||
other, shndx );
|
||||
}
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size,
|
||||
unsigned char bind, unsigned char type, unsigned char other,
|
||||
Elf_Word add_symbol( Elf_Word name,
|
||||
Elf64_Addr value,
|
||||
Elf_Xword size,
|
||||
unsigned char bind,
|
||||
unsigned char type,
|
||||
unsigned char other,
|
||||
Elf_Half shndx )
|
||||
{
|
||||
return add_symbol( name, value, size, ELF_ST_INFO( bind, type ), other, shndx );
|
||||
return add_symbol( name, value, size, ELF_ST_INFO( bind, type ), other,
|
||||
shndx );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
add_symbol( string_section_accessor& pStrWriter, const char* str,
|
||||
Elf64_Addr value, Elf_Xword size,
|
||||
unsigned char info, unsigned char other,
|
||||
Elf_Word add_symbol( string_section_accessor& pStrWriter,
|
||||
const char* str,
|
||||
Elf64_Addr value,
|
||||
Elf_Xword size,
|
||||
unsigned char info,
|
||||
unsigned char other,
|
||||
Elf_Half shndx )
|
||||
{
|
||||
Elf_Word index = pStrWriter.add_string( str );
|
||||
@ -205,18 +217,23 @@ class symbol_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
add_symbol( string_section_accessor& pStrWriter, const char* str,
|
||||
Elf64_Addr value, Elf_Xword size,
|
||||
unsigned char bind, unsigned char type, unsigned char other,
|
||||
Elf_Word add_symbol( string_section_accessor& pStrWriter,
|
||||
const char* str,
|
||||
Elf64_Addr value,
|
||||
Elf_Xword size,
|
||||
unsigned char bind,
|
||||
unsigned char type,
|
||||
unsigned char other,
|
||||
Elf_Half shndx )
|
||||
{
|
||||
return add_symbol( pStrWriter, str, value, size, ELF_ST_INFO( bind, type ), other, shndx );
|
||||
return add_symbol( pStrWriter, str, value, size,
|
||||
ELF_ST_INFO( bind, type ), other, shndx );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Xword
|
||||
arrange_local_symbols(std::function<void(Elf_Xword first, Elf_Xword second)> func = nullptr)
|
||||
Elf_Xword arrange_local_symbols(
|
||||
std::function<void( Elf_Xword first, Elf_Xword second )> func =
|
||||
nullptr )
|
||||
{
|
||||
int nRet = 0;
|
||||
|
||||
@ -233,8 +250,7 @@ class symbol_section_accessor_template
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
find_hash_section()
|
||||
void find_hash_section()
|
||||
{
|
||||
hash_section = 0;
|
||||
hash_section_index = 0;
|
||||
@ -249,23 +265,17 @@ class symbol_section_accessor_template
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_string_table_index() const
|
||||
Elf_Half get_string_table_index() const
|
||||
{
|
||||
return (Elf_Half)symbol_section->get_link();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_hash_table_index() const
|
||||
{
|
||||
return hash_section_index;
|
||||
}
|
||||
Elf_Half get_hash_table_index() const { return hash_section_index; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class T >
|
||||
const T*
|
||||
generic_get_symbol_ptr(Elf_Xword index) const {
|
||||
template <class T> const T* generic_get_symbol_ptr( Elf_Xword index ) const
|
||||
{
|
||||
if ( 0 != symbol_section->get_data() && index < get_symbols_num() ) {
|
||||
const T* pSym = reinterpret_cast<const T*>(
|
||||
symbol_section->get_data() +
|
||||
@ -279,8 +289,9 @@ class symbol_section_accessor_template
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
bool
|
||||
generic_search_symbols(std::function<bool(const T*)> match, Elf_Xword& idx) const {
|
||||
bool generic_search_symbols( std::function<bool( const T* )> match,
|
||||
Elf_Xword& idx ) const
|
||||
{
|
||||
for ( Elf_Xword i = 0; i < get_symbols_num(); i++ ) {
|
||||
const T* symPtr = generic_get_symbol_ptr<T>( i );
|
||||
|
||||
@ -298,11 +309,12 @@ class symbol_section_accessor_template
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
bool
|
||||
generic_get_symbol( Elf_Xword index,
|
||||
std::string& name, Elf64_Addr& value,
|
||||
bool generic_get_symbol( Elf_Xword index,
|
||||
std::string& name,
|
||||
Elf64_Addr& value,
|
||||
Elf_Xword& size,
|
||||
unsigned char& bind, unsigned char& type,
|
||||
unsigned char& bind,
|
||||
unsigned char& type,
|
||||
Elf_Half& section_index,
|
||||
unsigned char& other ) const
|
||||
{
|
||||
@ -315,9 +327,11 @@ class symbol_section_accessor_template
|
||||
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
section* string_section = elf_file.sections[get_string_table_index()];
|
||||
section* string_section =
|
||||
elf_file.sections[get_string_table_index()];
|
||||
string_section_accessor str_reader( string_section );
|
||||
const char* pStr = str_reader.get_string( convertor( pSym->st_name ) );
|
||||
const char* pStr =
|
||||
str_reader.get_string( convertor( pSym->st_name ) );
|
||||
if ( 0 != pStr ) {
|
||||
name = pStr;
|
||||
}
|
||||
@ -336,9 +350,11 @@ class symbol_section_accessor_template
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
Elf_Word
|
||||
generic_add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size,
|
||||
unsigned char info, unsigned char other,
|
||||
Elf_Word generic_add_symbol( Elf_Word name,
|
||||
Elf64_Addr value,
|
||||
Elf_Xword size,
|
||||
unsigned char info,
|
||||
unsigned char other,
|
||||
Elf_Half shndx )
|
||||
{
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
@ -363,40 +379,38 @@ class symbol_section_accessor_template
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
Elf_Xword
|
||||
generic_arrange_local_symbols(std::function<void (Elf_Xword first, Elf_Xword second)> func)
|
||||
Elf_Xword generic_arrange_local_symbols(
|
||||
std::function<void( Elf_Xword first, Elf_Xword second )> func )
|
||||
{
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
const Elf_Xword size = symbol_section->get_entry_size();
|
||||
|
||||
Elf_Xword first_not_local = 1; // Skip the first entry. It is always NOTYPE
|
||||
Elf_Xword first_not_local =
|
||||
1; // Skip the first entry. It is always NOTYPE
|
||||
Elf_Xword current = 0;
|
||||
Elf_Xword count = get_symbols_num();
|
||||
|
||||
while (true)
|
||||
{
|
||||
while ( true ) {
|
||||
T* p1 = nullptr;
|
||||
T* p2 = nullptr;
|
||||
|
||||
while (first_not_local < count)
|
||||
{
|
||||
p1 = const_cast<T *>(generic_get_symbol_ptr<T>(first_not_local));
|
||||
while ( first_not_local < count ) {
|
||||
p1 = const_cast<T*>(
|
||||
generic_get_symbol_ptr<T>( first_not_local ) );
|
||||
if ( ELF_ST_BIND( convertor( p1->st_info ) ) != STB_LOCAL )
|
||||
break;
|
||||
++first_not_local;
|
||||
}
|
||||
|
||||
current = first_not_local + 1;
|
||||
while (current < count)
|
||||
{
|
||||
while ( current < count ) {
|
||||
p2 = const_cast<T*>( generic_get_symbol_ptr<T>( current ) );
|
||||
if ( ELF_ST_BIND( convertor( p2->st_info ) ) == STB_LOCAL )
|
||||
break;
|
||||
++current;
|
||||
}
|
||||
|
||||
if (first_not_local < count && current < count)
|
||||
{
|
||||
if ( first_not_local < count && current < count ) {
|
||||
if ( func )
|
||||
func( first_not_local, current );
|
||||
|
||||
@ -406,8 +420,7 @@ class symbol_section_accessor_template
|
||||
std::copy( p2, p2 + 1, p1 );
|
||||
std::copy( &tmp, &tmp + 1, p2 );
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// Update 'info' field of the section
|
||||
symbol_section->set_info( first_not_local );
|
||||
break;
|
||||
@ -428,7 +441,8 @@ class symbol_section_accessor_template
|
||||
};
|
||||
|
||||
using symbol_section_accessor = symbol_section_accessor_template<section>;
|
||||
using const_symbol_section_accessor = symbol_section_accessor_template<const section>;
|
||||
using const_symbol_section_accessor =
|
||||
symbol_section_accessor_template<const section>;
|
||||
|
||||
} // namespace ELFIO
|
||||
|
||||
|
@ -24,10 +24,7 @@ THE SOFTWARE.
|
||||
#define ELFIO_UTILS_HPP
|
||||
|
||||
#define ELFIO_GET_ACCESS( TYPE, NAME, FIELD ) \
|
||||
TYPE get_##NAME() const \
|
||||
{ \
|
||||
return (*convertor)( FIELD ); \
|
||||
}
|
||||
TYPE get_##NAME() const { return ( *convertor )( FIELD ); }
|
||||
#define ELFIO_SET_ACCESS( TYPE, NAME, FIELD ) \
|
||||
void set_##NAME( TYPE value ) \
|
||||
{ \
|
||||
@ -35,18 +32,14 @@ THE SOFTWARE.
|
||||
FIELD = ( *convertor )( FIELD ); \
|
||||
}
|
||||
#define ELFIO_GET_SET_ACCESS( TYPE, NAME, FIELD ) \
|
||||
TYPE get_##NAME() const \
|
||||
{ \
|
||||
return (*convertor)( FIELD ); \
|
||||
} \
|
||||
TYPE get_##NAME() const { return ( *convertor )( FIELD ); } \
|
||||
void set_##NAME( TYPE value ) \
|
||||
{ \
|
||||
FIELD = value; \
|
||||
FIELD = ( *convertor )( FIELD ); \
|
||||
}
|
||||
|
||||
#define ELFIO_GET_ACCESS_DECL( TYPE, NAME ) \
|
||||
virtual TYPE get_##NAME() const = 0
|
||||
#define ELFIO_GET_ACCESS_DECL( TYPE, NAME ) virtual TYPE get_##NAME() const = 0
|
||||
|
||||
#define ELFIO_SET_ACCESS_DECL( TYPE, NAME ) \
|
||||
virtual void set_##NAME( TYPE value ) = 0
|
||||
@ -58,30 +51,25 @@ THE SOFTWARE.
|
||||
namespace ELFIO {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class endianess_convertor {
|
||||
class endianess_convertor
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
endianess_convertor()
|
||||
{
|
||||
need_conversion = false;
|
||||
}
|
||||
endianess_convertor() { need_conversion = false; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
setup( unsigned char elf_file_encoding )
|
||||
void setup( unsigned char elf_file_encoding )
|
||||
{
|
||||
need_conversion = ( elf_file_encoding != get_host_encoding() );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
uint64_t
|
||||
operator()( uint64_t value ) const
|
||||
uint64_t operator()( uint64_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
return value;
|
||||
}
|
||||
value =
|
||||
( ( value & 0x00000000000000FFull ) << 56 ) |
|
||||
value = ( ( value & 0x00000000000000FFull ) << 56 ) |
|
||||
( ( value & 0x000000000000FF00ull ) << 40 ) |
|
||||
( ( value & 0x0000000000FF0000ull ) << 24 ) |
|
||||
( ( value & 0x00000000FF000000ull ) << 8 ) |
|
||||
@ -94,8 +82,7 @@ class endianess_convertor {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int64_t
|
||||
operator()( int64_t value ) const
|
||||
int64_t operator()( int64_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
return value;
|
||||
@ -104,24 +91,20 @@ class endianess_convertor {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
uint32_t
|
||||
operator()( uint32_t value ) const
|
||||
uint32_t operator()( uint32_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
return value;
|
||||
}
|
||||
value =
|
||||
( ( value & 0x000000FF ) << 24 ) |
|
||||
( ( value & 0x0000FF00 ) << 8 ) |
|
||||
( ( value & 0x00FF0000 ) >> 8 ) |
|
||||
( ( value & 0xFF000000 ) >> 24 );
|
||||
( ( value & 0x000000FF ) << 24 ) | ( ( value & 0x0000FF00 ) << 8 ) |
|
||||
( ( value & 0x00FF0000 ) >> 8 ) | ( ( value & 0xFF000000 ) >> 24 );
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int32_t
|
||||
operator()( int32_t value ) const
|
||||
int32_t operator()( int32_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
return value;
|
||||
@ -130,22 +113,18 @@ class endianess_convertor {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
uint16_t
|
||||
operator()( uint16_t value ) const
|
||||
uint16_t operator()( uint16_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
return value;
|
||||
}
|
||||
value =
|
||||
( ( value & 0x00FF ) << 8 ) |
|
||||
( ( value & 0xFF00 ) >> 8 );
|
||||
value = ( ( value & 0x00FF ) << 8 ) | ( ( value & 0xFF00 ) >> 8 );
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int16_t
|
||||
operator()( int16_t value ) const
|
||||
int16_t operator()( int16_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
return value;
|
||||
@ -154,24 +133,15 @@ class endianess_convertor {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int8_t
|
||||
operator()( int8_t value ) const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
int8_t operator()( int8_t value ) const { return value; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
uint8_t
|
||||
operator()( uint8_t value ) const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
uint8_t operator()( uint8_t value ) const { return value; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
unsigned char
|
||||
get_host_encoding() const
|
||||
unsigned char get_host_encoding() const
|
||||
{
|
||||
static const int tmp = 1;
|
||||
if ( 1 == *(const char*)&tmp ) {
|
||||
@ -187,11 +157,8 @@ class endianess_convertor {
|
||||
bool need_conversion;
|
||||
};
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
inline
|
||||
uint32_t
|
||||
elf_hash( const unsigned char *name )
|
||||
inline uint32_t elf_hash( const unsigned char* name )
|
||||
{
|
||||
uint32_t h = 0, g;
|
||||
while ( *name ) {
|
||||
|
@ -49,9 +49,12 @@ diff before.txt after.txt
|
||||
|
||||
using namespace ELFIO;
|
||||
|
||||
void overwrite_data(const std::string &filename, long offset, std::string &str)
|
||||
void overwrite_data( const std::string& filename,
|
||||
long offset,
|
||||
std::string& str )
|
||||
{
|
||||
std::ofstream file(filename, std::ios::in | std::ios::out | std::ios::binary);
|
||||
std::ofstream file( filename,
|
||||
std::ios::in | std::ios::out | std::ios::binary );
|
||||
if ( !file )
|
||||
throw "Error opening file" + filename;
|
||||
std::string data( str.length(), '-' );
|
||||
@ -63,8 +66,7 @@ void process_string_table(const section *s, const std::string &filename)
|
||||
{
|
||||
std::cout << "Info: processing string table section" << std::endl;
|
||||
int index = 1;
|
||||
while (index < s->get_size())
|
||||
{
|
||||
while ( index < s->get_size() ) {
|
||||
auto str = std::string( s->get_data() + index );
|
||||
// For the example purpose, we rename main function name only
|
||||
if ( str == "main" )
|
||||
@ -75,10 +77,8 @@ void process_string_table(const section *s, const std::string &filename)
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
try {
|
||||
if ( argc != 2 ) {
|
||||
std::cout << "Usage: anonymizer <file_name>\n";
|
||||
return 1;
|
||||
}
|
||||
@ -87,28 +87,26 @@ int main(int argc, char **argv)
|
||||
|
||||
elfio reader;
|
||||
|
||||
if (!reader.load(filename))
|
||||
{
|
||||
std::cerr << "File " << filename << " is not found or it is not an ELF file\n";
|
||||
if ( !reader.load( filename ) ) {
|
||||
std::cerr << "File " << filename
|
||||
<< " is not found or it is not an ELF file\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (auto section = reader.sections.begin(); section != reader.sections.end(); ++section)
|
||||
{
|
||||
for ( auto section = reader.sections.begin();
|
||||
section != reader.sections.end(); ++section ) {
|
||||
if ( ( *section )->get_type() == SHT_STRTAB &&
|
||||
std::string((*section)->get_name()) == std::string(".strtab"))
|
||||
{
|
||||
std::string( ( *section )->get_name() ) ==
|
||||
std::string( ".strtab" ) ) {
|
||||
process_string_table( *section, filename );
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
catch (const std::string &s)
|
||||
{
|
||||
catch ( const std::string& s ) {
|
||||
std::cerr << s << std::endl;
|
||||
}
|
||||
catch (const char *s)
|
||||
{
|
||||
catch ( const char* s ) {
|
||||
std::cerr << s << std::endl;
|
||||
}
|
||||
return 1;
|
||||
|
@ -37,11 +37,8 @@ int main( int argc, char** argv )
|
||||
std::cout << "Number of sections: " << sec_num << std::endl;
|
||||
for ( int i = 0; i < sec_num; ++i ) {
|
||||
section* psec = reader.sections[i];
|
||||
std::cout << " [" << i << "] "
|
||||
<< psec->get_name()
|
||||
<< "\t"
|
||||
<< psec->get_size()
|
||||
<< std::endl;
|
||||
std::cout << " [" << i << "] " << psec->get_name() << "\t"
|
||||
<< psec->get_size() << std::endl;
|
||||
// Access to section's data
|
||||
// const char* p = reader.sections[i]->get_data()
|
||||
}
|
||||
@ -51,14 +48,9 @@ int main( int argc, char** argv )
|
||||
std::cout << "Number of segments: " << seg_num << std::endl;
|
||||
for ( int i = 0; i < seg_num; ++i ) {
|
||||
const segment* pseg = reader.segments[i];
|
||||
std::cout << " [" << i << "] 0x" << std::hex
|
||||
<< pseg->get_flags()
|
||||
<< "\t0x"
|
||||
<< pseg->get_virtual_address()
|
||||
<< "\t0x"
|
||||
<< pseg->get_file_size()
|
||||
<< "\t0x"
|
||||
<< pseg->get_memory_size()
|
||||
std::cout << " [" << i << "] 0x" << std::hex << pseg->get_flags()
|
||||
<< "\t0x" << pseg->get_virtual_address() << "\t0x"
|
||||
<< pseg->get_file_size() << "\t0x" << pseg->get_memory_size()
|
||||
<< std::endl;
|
||||
// Access to segments's data
|
||||
// const char* p = reader.segments[i]->get_data()
|
||||
@ -79,8 +71,8 @@ int main( int argc, char** argv )
|
||||
unsigned char other;
|
||||
|
||||
// Read symbol properties
|
||||
symbols.get_symbol( j, name, value, size, bind,
|
||||
type, section_index, other );
|
||||
symbols.get_symbol( j, name, value, size, bind, type,
|
||||
section_index, other );
|
||||
std::cout << j << " " << name << " " << value << std::endl;
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,8 @@ int main( void )
|
||||
writer.set_machine( EM_X86_64 );
|
||||
|
||||
// This is our code
|
||||
char text[] = { '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4
|
||||
char text[] = {
|
||||
'\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4
|
||||
'\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1
|
||||
'\xB9', '\x00', '\x00', '\x00', '\x00', // mov ecx, msg
|
||||
'\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14
|
||||
@ -37,8 +38,7 @@ int main( void )
|
||||
'\xCD', '\x80', // int 0x80
|
||||
'\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10
|
||||
'\x2C', '\x20', '\x57', '\x6F', '\x72',
|
||||
'\x6C', '\x64', '\x21', '\x0A'
|
||||
};
|
||||
'\x6C', '\x64', '\x21', '\x0A' };
|
||||
Elf64_Addr place_to_adjust = 11;
|
||||
|
||||
// Create code section
|
||||
@ -68,9 +68,8 @@ int main( void )
|
||||
// Create symbol table writer
|
||||
symbol_section_accessor syma( writer, sym_sec );
|
||||
// Add symbol entry (msg has offset == 29)
|
||||
Elf_Word sym_to_adjust = syma.add_symbol( str_index, 29, 0, STB_GLOBAL,
|
||||
STT_OBJECT, 0,
|
||||
text_sec->get_index() );
|
||||
Elf_Word sym_to_adjust = syma.add_symbol(
|
||||
str_index, 29, 0, STB_GLOBAL, STT_OBJECT, 0, text_sec->get_index() );
|
||||
// Another way to add symbol
|
||||
syma.add_symbol( stra, "_start", 0x00000000, 0, STB_WEAK, STT_FUNC, 0,
|
||||
text_sec->get_index() );
|
||||
|
@ -20,7 +20,8 @@ int main( void )
|
||||
text_sec->set_addr_align( 0x10 );
|
||||
|
||||
// Add data into it
|
||||
char text[] = { '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4
|
||||
char text[] = {
|
||||
'\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4
|
||||
'\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1
|
||||
'\xB9', '\x20', '\x80', '\x04', '\x08', // mov ecx, msg
|
||||
'\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14
|
||||
@ -39,7 +40,8 @@ int main( void )
|
||||
text_seg->set_align( 0x1000 );
|
||||
|
||||
// Add code section into program segment
|
||||
text_seg->add_section_index( text_sec->get_index(), text_sec->get_addr_align() );
|
||||
text_seg->add_section_index( text_sec->get_index(),
|
||||
text_sec->get_addr_align() );
|
||||
|
||||
// Create data section
|
||||
section* data_sec = writer.sections.add( ".data" );
|
||||
@ -47,10 +49,10 @@ int main( void )
|
||||
data_sec->set_flags( SHF_ALLOC | SHF_WRITE );
|
||||
data_sec->set_addr_align( 0x4 );
|
||||
|
||||
char data[] = { '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10
|
||||
char data[] = {
|
||||
'\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10
|
||||
'\x2C', '\x20', '\x57', '\x6F', '\x72',
|
||||
'\x6C', '\x64', '\x21', '\x0A'
|
||||
};
|
||||
'\x6C', '\x64', '\x21', '\x0A' };
|
||||
data_sec->set_data( data, sizeof( data ) );
|
||||
|
||||
// Create a read/write segment
|
||||
@ -62,7 +64,8 @@ int main( void )
|
||||
data_seg->set_align( 0x10 );
|
||||
|
||||
// Add code section into program segment
|
||||
data_seg->add_section_index( data_sec->get_index(), data_sec->get_addr_align() );
|
||||
data_seg->add_section_index( data_sec->get_index(),
|
||||
data_sec->get_addr_align() );
|
||||
|
||||
// Add optional signature for the file producer
|
||||
section* note_sec = writer.sections.add( ".note" );
|
||||
|
@ -10,8 +10,7 @@
|
||||
using namespace ELFIO;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
checkHeader( elfio& reader,
|
||||
void checkHeader( elfio& reader,
|
||||
unsigned char nClass,
|
||||
unsigned char encoding,
|
||||
unsigned char elfVersion,
|
||||
@ -39,10 +38,8 @@ checkHeader( elfio& reader,
|
||||
BOOST_CHECK_EQUAL( reader.segments.size(), segNum );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
checkSection( const section* sec,
|
||||
void checkSection( const section* sec,
|
||||
Elf_Half index,
|
||||
std::string name,
|
||||
Elf_Word type,
|
||||
@ -66,10 +63,8 @@ checkSection( const section* sec,
|
||||
BOOST_CHECK_EQUAL( sec->get_entry_size(), entrySize );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
checkSection( const section* sec,
|
||||
void checkSection( const section* sec,
|
||||
const std::string& name,
|
||||
Elf_Word type,
|
||||
Elf_Xword flags,
|
||||
@ -84,10 +79,8 @@ checkSection( const section* sec,
|
||||
info, addrAlign, entrySize );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
checkSegment( const segment* seg,
|
||||
void checkSegment( const segment* seg,
|
||||
Elf_Word type,
|
||||
Elf64_Addr vaddr,
|
||||
Elf64_Addr paddr,
|
||||
@ -105,14 +98,16 @@ checkSegment( const segment* seg,
|
||||
BOOST_CHECK_EQUAL( seg->get_align(), align );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
checkSymbol( const symbol_section_accessor& sr, Elf_Xword index,
|
||||
std::string name_, Elf64_Addr value_,
|
||||
void checkSymbol( const symbol_section_accessor& sr,
|
||||
Elf_Xword index,
|
||||
std::string name_,
|
||||
Elf64_Addr value_,
|
||||
Elf_Xword size_,
|
||||
unsigned char bind_, unsigned char type_,
|
||||
Elf_Half section_, unsigned char other_ )
|
||||
unsigned char bind_,
|
||||
unsigned char type_,
|
||||
Elf_Half section_,
|
||||
unsigned char other_ )
|
||||
{
|
||||
std::string name;
|
||||
Elf64_Addr value;
|
||||
@ -122,7 +117,8 @@ checkSymbol( const symbol_section_accessor& sr, Elf_Xword index,
|
||||
Elf_Half section;
|
||||
unsigned char other;
|
||||
|
||||
BOOST_REQUIRE_EQUAL( sr.get_symbol( index, name, value, size, bind, type, section, other ),
|
||||
BOOST_REQUIRE_EQUAL(
|
||||
sr.get_symbol( index, name, value, size, bind, type, section, other ),
|
||||
true );
|
||||
BOOST_CHECK_EQUAL( name, name_ );
|
||||
BOOST_CHECK_EQUAL( value, value_ );
|
||||
@ -133,13 +129,14 @@ checkSymbol( const symbol_section_accessor& sr, Elf_Xword index,
|
||||
BOOST_CHECK_EQUAL( other, other_ );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
checkRelocation( const relocation_section_accessor* pRT, Elf_Xword index,
|
||||
Elf64_Addr offset_, Elf64_Addr symbolValue_,
|
||||
void checkRelocation( const relocation_section_accessor* pRT,
|
||||
Elf_Xword index,
|
||||
Elf64_Addr offset_,
|
||||
Elf64_Addr symbolValue_,
|
||||
std::string symbolName_,
|
||||
unsigned char type_, Elf_Sxword addend_,
|
||||
unsigned char type_,
|
||||
Elf_Sxword addend_,
|
||||
Elf_Sxword calcValue_ )
|
||||
{
|
||||
Elf64_Addr offset;
|
||||
@ -160,11 +157,11 @@ checkRelocation( const relocation_section_accessor* pRT, Elf_Xword index,
|
||||
BOOST_CHECK_EQUAL( calcValue, calcValue_ );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void
|
||||
checkNote( const note_section_accessor& notes, Elf_Word index,
|
||||
Elf_Word type_, std::string name_,
|
||||
void checkNote( const note_section_accessor& notes,
|
||||
Elf_Word index,
|
||||
Elf_Word type_,
|
||||
std::string name_,
|
||||
Elf_Word descSize_ )
|
||||
{
|
||||
Elf_Word type;
|
||||
@ -179,14 +176,13 @@ void
|
||||
BOOST_CHECK_EQUAL( descSize, descSize_ );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( load32 )
|
||||
{
|
||||
elfio reader;
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/hello_32" ), true );
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_EXEC,
|
||||
EM_386, 1, 0x80482b0, 0, 28, 7, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_EXEC, EM_386,
|
||||
1, 0x80482b0, 0, 28, 7, 0, 0 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
@ -194,20 +190,19 @@ BOOST_AUTO_TEST_CASE( load32 )
|
||||
checkSection( sec, 0, "", SHT_NULL, 0, 0, 0, 0, 0, 0, 0 );
|
||||
|
||||
sec = reader.sections[1];
|
||||
checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC,
|
||||
0x08048114, 0x13, 0, 0, 1, 0 );
|
||||
checkSection( sec, 1, ".interp", SHT_PROGBITS, SHF_ALLOC, 0x08048114, 0x13,
|
||||
0, 0, 1, 0 );
|
||||
|
||||
sec = reader.sections[9];
|
||||
checkSection( sec, 9, ".rel.plt", SHT_REL, SHF_ALLOC,
|
||||
0x08048234, 0x18, 4, 11, 4, 8 );
|
||||
checkSection( sec, 9, ".rel.plt", SHT_REL, SHF_ALLOC, 0x08048234, 0x18, 4,
|
||||
11, 4, 8 );
|
||||
|
||||
sec = reader.sections[19];
|
||||
checkSection( sec, 19, ".dynamic", SHT_DYNAMIC, SHF_WRITE | SHF_ALLOC,
|
||||
0x080494a0, 0xc8, 5, 0, 4, 8 );
|
||||
|
||||
sec = reader.sections[27];
|
||||
checkSection( sec, 27, ".strtab", SHT_STRTAB, 0,
|
||||
0x0, 0x259, 0, 0, 1, 0 );
|
||||
checkSection( sec, 27, ".strtab", SHT_STRTAB, 0, 0x0, 0x259, 0, 0, 1, 0 );
|
||||
|
||||
const section* sec1 = reader.sections[".strtab"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
@ -215,22 +210,20 @@ BOOST_AUTO_TEST_CASE( load32 )
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check segments
|
||||
segment* seg = reader.segments[0];
|
||||
checkSegment( seg, PT_PHDR, 0x08048034, 0x08048034,
|
||||
0x000e0, 0x000e0, PF_R + PF_X, 4 );
|
||||
checkSegment( seg, PT_PHDR, 0x08048034, 0x08048034, 0x000e0, 0x000e0,
|
||||
PF_R + PF_X, 4 );
|
||||
|
||||
seg = reader.segments[4];
|
||||
checkSegment( seg, PT_DYNAMIC, 0x080494a0, 0x080494a0,
|
||||
0x000c8, 0x000c8, PF_R + PF_W, 4 );
|
||||
checkSegment( seg, PT_DYNAMIC, 0x080494a0, 0x080494a0, 0x000c8, 0x000c8,
|
||||
PF_R + PF_W, 4 );
|
||||
|
||||
seg = reader.segments[6];
|
||||
checkSegment( seg, 0x6474E551, 0x0, 0x0,
|
||||
0x0, 0x0, PF_R + PF_W, 4 );
|
||||
checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 4 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check symbol table
|
||||
sec = reader.sections[".symtab"];
|
||||
|
||||
|
||||
symbol_section_accessor sr( reader, sec );
|
||||
|
||||
BOOST_CHECK_EQUAL( sr.get_symbols_num(), 68 );
|
||||
@ -240,8 +233,8 @@ BOOST_AUTO_TEST_CASE( load32 )
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 39, "hello.c", 0x00000000, 0, STB_LOCAL, STT_FILE, SHN_ABS,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 65, "__i686.get_pc_thunk.bx", 0x08048429, 0, STB_GLOBAL, STT_FUNC, 12,
|
||||
ELF_ST_VISIBILITY( STV_HIDDEN ) );
|
||||
checkSymbol( sr, 65, "__i686.get_pc_thunk.bx", 0x08048429, 0, STB_GLOBAL,
|
||||
STT_FUNC, 12, ELF_ST_VISIBILITY( STV_HIDDEN ) );
|
||||
checkSymbol( sr, 66, "main", 0x08048384, 43, STB_GLOBAL, STT_FUNC, 12,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 67, "_init", 0x0804824c, 0, STB_GLOBAL, STT_FUNC, 10,
|
||||
@ -251,34 +244,34 @@ BOOST_AUTO_TEST_CASE( load32 )
|
||||
// Check relocation table
|
||||
sec = reader.sections[".rel.dyn"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc.get_entries_num(), 1 );
|
||||
|
||||
checkRelocation( &reloc, 0, 0x08049568, 0x0, "__gmon_start__", R_386_GLOB_DAT, 0, 0 );
|
||||
checkRelocation( &reloc, 0, 0x08049568, 0x0, "__gmon_start__",
|
||||
R_386_GLOB_DAT, 0, 0 );
|
||||
|
||||
sec = reader.sections[".rel.plt"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc1( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 3 );
|
||||
|
||||
checkRelocation( &reloc1, 0, 0x08049578, 0x0, "__gmon_start__", R_X86_64_JUMP_SLOT, 0, 0 );
|
||||
checkRelocation( &reloc1, 1, 0x0804957c, 0x0, "__libc_start_main", R_X86_64_JUMP_SLOT, 0, 0 );
|
||||
checkRelocation( &reloc1, 2, 0x08049580, 0x0, "puts", R_X86_64_JUMP_SLOT, 0, 0 );
|
||||
checkRelocation( &reloc1, 0, 0x08049578, 0x0, "__gmon_start__",
|
||||
R_X86_64_JUMP_SLOT, 0, 0 );
|
||||
checkRelocation( &reloc1, 1, 0x0804957c, 0x0, "__libc_start_main",
|
||||
R_X86_64_JUMP_SLOT, 0, 0 );
|
||||
checkRelocation( &reloc1, 2, 0x08049580, 0x0, "puts", R_X86_64_JUMP_SLOT, 0,
|
||||
0 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check note reader
|
||||
sec = reader.sections[".note.ABI-tag"];
|
||||
|
||||
|
||||
note_section_accessor notes( reader, sec );
|
||||
BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u );
|
||||
|
||||
checkNote( notes, 0, 1, std::string( "GNU" ), 16 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( load64 )
|
||||
{
|
||||
@ -304,8 +297,8 @@ BOOST_AUTO_TEST_CASE( load64 )
|
||||
|
||||
sec = reader.sections[9];
|
||||
|
||||
checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC,
|
||||
0x0000000000400340, 0x30, 4, 11, 8, 0x18 );
|
||||
checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, 0x0000000000400340,
|
||||
0x30, 4, 11, 8, 0x18 );
|
||||
|
||||
sec = reader.sections[20];
|
||||
|
||||
@ -314,8 +307,7 @@ BOOST_AUTO_TEST_CASE( load64 )
|
||||
|
||||
sec = reader.sections[28];
|
||||
|
||||
checkSection( sec, 28, ".strtab", SHT_STRTAB, 0,
|
||||
0x0, 0x23f, 0, 0, 1, 0 );
|
||||
checkSection( sec, 28, ".strtab", SHT_STRTAB, 0, 0x0, 0x23f, 0, 0, 1, 0 );
|
||||
|
||||
const section* sec1 = reader.sections[".strtab"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
@ -328,17 +320,16 @@ BOOST_AUTO_TEST_CASE( load64 )
|
||||
|
||||
seg = reader.segments[2];
|
||||
checkSegment( seg, PT_LOAD, 0x0000000000400000, 0x0000000000400000,
|
||||
0x000000000000066c, 0x000000000000066c, PF_R + PF_X, 0x200000 );
|
||||
0x000000000000066c, 0x000000000000066c, PF_R + PF_X,
|
||||
0x200000 );
|
||||
|
||||
seg = reader.segments[7];
|
||||
checkSegment( seg, 0x6474E551, 0x0, 0x0,
|
||||
0x0, 0x0, PF_R + PF_W, 8 );
|
||||
checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 8 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check symbol table
|
||||
sec = reader.sections[".symtab"];
|
||||
|
||||
|
||||
symbol_section_accessor sr( reader, sec );
|
||||
|
||||
BOOST_CHECK_EQUAL( sr.get_symbols_num(), 67 );
|
||||
@ -348,10 +339,10 @@ BOOST_AUTO_TEST_CASE( load64 )
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 40, "hello.c", 0x00000000, 0, STB_LOCAL, STT_FILE, SHN_ABS,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 52, "__gmon_start__", 0x00000000, 0, STB_WEAK, STT_NOTYPE, STN_UNDEF,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 64, "_edata", 0x0060085c, 0, STB_GLOBAL, STT_NOTYPE, SHN_ABS,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 52, "__gmon_start__", 0x00000000, 0, STB_WEAK, STT_NOTYPE,
|
||||
STN_UNDEF, ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 64, "_edata", 0x0060085c, 0, STB_GLOBAL, STT_NOTYPE,
|
||||
SHN_ABS, ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 65, "main", 0x00400498, 21, STB_GLOBAL, STT_FUNC, 12,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 66, "_init", 0x00400370, 0, STB_GLOBAL, STT_FUNC, 10,
|
||||
@ -361,33 +352,32 @@ BOOST_AUTO_TEST_CASE( load64 )
|
||||
// Check relocation table
|
||||
sec = reader.sections[".rela.dyn"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc.get_entries_num(), 1 );
|
||||
|
||||
checkRelocation( &reloc, 0, 0x00600828, 0x0, "__gmon_start__", R_X86_64_GLOB_DAT, 0, 0 );
|
||||
checkRelocation( &reloc, 0, 0x00600828, 0x0, "__gmon_start__",
|
||||
R_X86_64_GLOB_DAT, 0, 0 );
|
||||
|
||||
sec = reader.sections[".rela.plt"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc1( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 2 );
|
||||
|
||||
checkRelocation( &reloc1, 0, 0x00600848, 0x0, "puts", R_X86_64_JUMP_SLOT, 0, 0 );
|
||||
checkRelocation( &reloc1, 1, 0x00600850, 0x0, "__libc_start_main", R_X86_64_JUMP_SLOT, 0, 0 );
|
||||
checkRelocation( &reloc1, 0, 0x00600848, 0x0, "puts", R_X86_64_JUMP_SLOT, 0,
|
||||
0 );
|
||||
checkRelocation( &reloc1, 1, 0x00600850, 0x0, "__libc_start_main",
|
||||
R_X86_64_JUMP_SLOT, 0, 0 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check note reader
|
||||
sec = reader.sections[".note.ABI-tag"];
|
||||
|
||||
|
||||
note_section_accessor notes( reader, sec );
|
||||
BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u );
|
||||
|
||||
checkNote( notes, 0, 1, std::string( "GNU" ), 16 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( hello_64_o )
|
||||
{
|
||||
@ -397,8 +387,8 @@ BOOST_AUTO_TEST_CASE( hello_64_o )
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check ELF header
|
||||
checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL,
|
||||
EM_X86_64, 1, 0, 0, 13, 0, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_X86_64,
|
||||
1, 0, 0, 13, 0, 0, 0 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
@ -408,15 +398,14 @@ BOOST_AUTO_TEST_CASE( hello_64_o )
|
||||
|
||||
sec = reader.sections[1];
|
||||
|
||||
checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
|
||||
0x0, 0x15, 0, 0, 4, 0 );
|
||||
checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0,
|
||||
0x15, 0, 0, 4, 0 );
|
||||
|
||||
section* sec1 = reader.sections[".text"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
|
||||
sec = reader.sections[12];
|
||||
checkSection( sec, 12, ".strtab", SHT_STRTAB, 0,
|
||||
0x0, 0x13, 0, 0, 1, 0 );
|
||||
checkSection( sec, 12, ".strtab", SHT_STRTAB, 0, 0x0, 0x13, 0, 0, 1, 0 );
|
||||
|
||||
sec1 = reader.sections[".strtab"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
@ -425,7 +414,6 @@ BOOST_AUTO_TEST_CASE( hello_64_o )
|
||||
// Check symbol table
|
||||
sec = reader.sections[".symtab"];
|
||||
|
||||
|
||||
symbol_section_accessor sr( reader, sec );
|
||||
|
||||
BOOST_CHECK_EQUAL( sr.get_symbols_num(), 11 );
|
||||
@ -436,23 +424,21 @@ BOOST_AUTO_TEST_CASE( hello_64_o )
|
||||
// Check relocation table
|
||||
sec = reader.sections[".rela.text"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 );
|
||||
|
||||
checkRelocation( &reloc, 0, 0x00000005, 0x0, "", R_X86_64_32, 0, 0 );
|
||||
checkRelocation( &reloc, 1, 0x0000000A, 0x0, "puts", R_X86_64_PC32, 0xfffffffffffffffcULL, -14 );
|
||||
checkRelocation( &reloc, 1, 0x0000000A, 0x0, "puts", R_X86_64_PC32,
|
||||
0xfffffffffffffffcULL, -14 );
|
||||
|
||||
sec = reader.sections[".rela.eh_frame"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc1( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 1 );
|
||||
|
||||
checkRelocation( &reloc1, 0, 0x00000020, 0x0, "", R_X86_64_32, 0, 0 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( hello_32_o )
|
||||
{
|
||||
@ -462,8 +448,8 @@ BOOST_AUTO_TEST_CASE( hello_32_o )
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check ELF header
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL,
|
||||
EM_386, 1, 0, 0, 11, 0, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_386, 1,
|
||||
0, 0, 11, 0, 0, 0 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
@ -473,17 +459,15 @@ BOOST_AUTO_TEST_CASE( hello_32_o )
|
||||
|
||||
sec = reader.sections[1];
|
||||
|
||||
checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
|
||||
0x0, 0x2b, 0, 0, 4, 0 );
|
||||
checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0,
|
||||
0x2b, 0, 0, 4, 0 );
|
||||
|
||||
section* sec1 = reader.sections[".text"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
|
||||
|
||||
sec = reader.sections[10];
|
||||
|
||||
checkSection( sec, 10, ".strtab", SHT_STRTAB, 0,
|
||||
0x0, 0x13, 0, 0, 1, 0 );
|
||||
checkSection( sec, 10, ".strtab", SHT_STRTAB, 0, 0x0, 0x13, 0, 0, 1, 0 );
|
||||
|
||||
sec1 = reader.sections[".strtab"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
@ -492,7 +476,6 @@ BOOST_AUTO_TEST_CASE( hello_32_o )
|
||||
// Check symbol table
|
||||
sec = reader.sections[".symtab"];
|
||||
|
||||
|
||||
symbol_section_accessor sr( reader, sec );
|
||||
|
||||
BOOST_CHECK_EQUAL( sr.get_symbols_num(), 10 );
|
||||
@ -503,7 +486,6 @@ BOOST_AUTO_TEST_CASE( hello_32_o )
|
||||
// Check relocation table
|
||||
sec = reader.sections[".rel.text"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 );
|
||||
|
||||
@ -511,7 +493,6 @@ BOOST_AUTO_TEST_CASE( hello_32_o )
|
||||
checkRelocation( &reloc, 1, 0x00000019, 0x0, "puts", R_386_PC32, 0x0, -25 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_ppc_o )
|
||||
{
|
||||
@ -521,8 +502,8 @@ BOOST_AUTO_TEST_CASE( test_ppc_o )
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check ELF header
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL,
|
||||
EM_PPC, 1, 0, 0, 16, 0, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC, 1,
|
||||
0, 0, 16, 0, 0, 0 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
@ -532,16 +513,15 @@ BOOST_AUTO_TEST_CASE( test_ppc_o )
|
||||
|
||||
sec = reader.sections[1];
|
||||
|
||||
checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
|
||||
0x0, 0x118, 0, 0, 4, 0 );
|
||||
checkSection( sec, 1, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, 0x0,
|
||||
0x118, 0, 0, 4, 0 );
|
||||
|
||||
section* sec1 = reader.sections[".text"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
|
||||
sec = reader.sections[15];
|
||||
|
||||
checkSection( sec, 15, ".strtab", SHT_STRTAB, 0,
|
||||
0x0, 0x14f, 0, 0, 1, 0 );
|
||||
checkSection( sec, 15, ".strtab", SHT_STRTAB, 0, 0x0, 0x14f, 0, 0, 1, 0 );
|
||||
|
||||
sec1 = reader.sections[".strtab"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
@ -550,20 +530,18 @@ BOOST_AUTO_TEST_CASE( test_ppc_o )
|
||||
// Check symbol table
|
||||
sec = reader.sections[".symtab"];
|
||||
|
||||
|
||||
symbol_section_accessor sr( reader, sec );
|
||||
|
||||
BOOST_CHECK_EQUAL( sr.get_symbols_num(), 24 );
|
||||
checkSymbol( sr, 14, "main", 0x00000000, 92, STB_GLOBAL, STT_FUNC, 1,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 8, "_GLOBAL__I_main", 0x000000DC, 60, STB_LOCAL, STT_FUNC, 1,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 8, "_GLOBAL__I_main", 0x000000DC, 60, STB_LOCAL, STT_FUNC,
|
||||
1, ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check relocation table
|
||||
sec = reader.sections[".rela.text"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc.get_entries_num(), 18 );
|
||||
|
||||
@ -573,7 +551,6 @@ BOOST_AUTO_TEST_CASE( test_ppc_o )
|
||||
|
||||
sec = reader.sections[".rela.ctors"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc1( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 1 );
|
||||
|
||||
@ -581,14 +558,12 @@ BOOST_AUTO_TEST_CASE( test_ppc_o )
|
||||
|
||||
sec = reader.sections[".rela.eh_frame"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc2( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc2.get_entries_num(), 3 );
|
||||
|
||||
checkRelocation( &reloc2, 1, 0x00000020, 0x0, "", 1, 0x0, 0x0 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_ppc )
|
||||
{
|
||||
@ -598,8 +573,8 @@ BOOST_AUTO_TEST_CASE( test_ppc )
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check ELF header
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_EXEC,
|
||||
EM_PPC, 1, 0x10000550, 0, 31, 8, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_EXEC, EM_PPC,
|
||||
1, 0x10000550, 0, 31, 8, 0, 0 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
@ -614,8 +589,8 @@ BOOST_AUTO_TEST_CASE( test_ppc )
|
||||
|
||||
sec = reader.sections[9];
|
||||
|
||||
checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC,
|
||||
0x00000000010000494, 0x6c, 4, 22, 4, 0xc );
|
||||
checkSection( sec, 9, ".rela.plt", SHT_RELA, SHF_ALLOC, 0x00000000010000494,
|
||||
0x6c, 4, 22, 4, 0xc );
|
||||
|
||||
sec = reader.sections[20];
|
||||
|
||||
@ -624,32 +599,28 @@ BOOST_AUTO_TEST_CASE( test_ppc )
|
||||
|
||||
sec = reader.sections[28];
|
||||
|
||||
checkSection( sec, 28, ".shstrtab", SHT_STRTAB, 0,
|
||||
0x0, 0x101, 0, 0, 1, 0 );
|
||||
checkSection( sec, 28, ".shstrtab", SHT_STRTAB, 0, 0x0, 0x101, 0, 0, 1, 0 );
|
||||
|
||||
const section* sec1 = reader.sections[".shstrtab"];
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), sec1->get_index() );
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check segments
|
||||
segment* seg = reader.segments[0];
|
||||
checkSegment( seg, PT_PHDR, 0x10000034, 0x10000034,
|
||||
0x00100, 0x00100, PF_R + PF_X, 4 );
|
||||
checkSegment( seg, PT_PHDR, 0x10000034, 0x10000034, 0x00100, 0x00100,
|
||||
PF_R + PF_X, 4 );
|
||||
|
||||
seg = reader.segments[2];
|
||||
checkSegment( seg, PT_LOAD, 0x10000000, 0x10000000,
|
||||
0x00acc, 0x00acc, PF_R + PF_X, 0x10000 );
|
||||
checkSegment( seg, PT_LOAD, 0x10000000, 0x10000000, 0x00acc, 0x00acc,
|
||||
PF_R + PF_X, 0x10000 );
|
||||
|
||||
seg = reader.segments[7];
|
||||
checkSegment( seg, 0x6474E551, 0x0, 0x0,
|
||||
0x0, 0x0, PF_R + PF_W, 0x4 );
|
||||
checkSegment( seg, 0x6474E551, 0x0, 0x0, 0x0, 0x0, PF_R + PF_W, 0x4 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check symbol table
|
||||
sec = reader.sections[".symtab"];
|
||||
|
||||
|
||||
symbol_section_accessor sr( reader, sec );
|
||||
|
||||
BOOST_CHECK_EQUAL( sr.get_symbols_num(), 80 );
|
||||
@ -657,11 +628,12 @@ BOOST_AUTO_TEST_CASE( test_ppc )
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 1, "", 0x10000134, 0, STB_LOCAL, STT_SECTION, 1,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 40, "__CTOR_END__", 0x10010AD4, 0, STB_LOCAL, STT_OBJECT, 16,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 52, "__init_array_start", 0x10010acc, 0, STB_LOCAL, STT_NOTYPE, 16,
|
||||
ELF_ST_VISIBILITY( STV_HIDDEN ) );
|
||||
checkSymbol( sr, 64, "_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4", 0x10000920, 204, STB_GLOBAL, STT_FUNC, SHN_UNDEF,
|
||||
checkSymbol( sr, 40, "__CTOR_END__", 0x10010AD4, 0, STB_LOCAL, STT_OBJECT,
|
||||
16, ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 52, "__init_array_start", 0x10010acc, 0, STB_LOCAL,
|
||||
STT_NOTYPE, 16, ELF_ST_VISIBILITY( STV_HIDDEN ) );
|
||||
checkSymbol( sr, 64, "_ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4", 0x10000920,
|
||||
204, STB_GLOBAL, STT_FUNC, SHN_UNDEF,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
checkSymbol( sr, 78, "main", 0x1000069c, 92, STB_GLOBAL, STT_FUNC, 11,
|
||||
ELF_ST_VISIBILITY( STV_DEFAULT ) );
|
||||
@ -672,7 +644,6 @@ BOOST_AUTO_TEST_CASE( test_ppc )
|
||||
// Check relocation table
|
||||
sec = reader.sections[".rela.dyn"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc.get_entries_num(), 2 );
|
||||
|
||||
@ -680,25 +651,23 @@ BOOST_AUTO_TEST_CASE( test_ppc )
|
||||
|
||||
sec = reader.sections[".rela.plt"];
|
||||
|
||||
|
||||
relocation_section_accessor reloc1( reader, sec );
|
||||
BOOST_CHECK_EQUAL( reloc1.get_entries_num(), 9 );
|
||||
|
||||
checkRelocation( &reloc1, 0, 0x10010be4, 0x100008e0, "__cxa_atexit", 21, 0, 0 );
|
||||
checkRelocation( &reloc1, 0, 0x10010be4, 0x100008e0, "__cxa_atexit", 21, 0,
|
||||
0 );
|
||||
checkRelocation( &reloc1, 1, 0x10010be8, 0x0, "__gmon_start__", 21, 0, 0 );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check note reader
|
||||
sec = reader.sections[".note.ABI-tag"];
|
||||
|
||||
|
||||
note_section_accessor notes( reader, sec );
|
||||
BOOST_CHECK_EQUAL( notes.get_notes_num(), 1u );
|
||||
|
||||
checkNote( notes, 0, 1, std::string( "GNU" ), 16 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_dummy_out_i386_32 )
|
||||
{
|
||||
@ -729,13 +698,13 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_i386_32 )
|
||||
writer.save( "elf_examples/elf_dummy_header_i386_32.elf" );
|
||||
|
||||
elfio reader;
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/elf_dummy_header_i386_32.elf" ),
|
||||
true );
|
||||
BOOST_REQUIRE_EQUAL(
|
||||
reader.load( "elf_examples/elf_dummy_header_i386_32.elf" ), true );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check ELF header
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL,
|
||||
EM_386, EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_386,
|
||||
EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 );
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
section* sec = reader.sections[""];
|
||||
@ -744,17 +713,14 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_i386_32 )
|
||||
|
||||
sec = reader.sections[".shstrtab"];
|
||||
|
||||
checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0,
|
||||
0, 17, 0, 0, 1, 0 );
|
||||
checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 );
|
||||
|
||||
sec = reader.sections[".note"];
|
||||
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), 2 );
|
||||
checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC,
|
||||
0, 28, 0, 0, 4, 0 );
|
||||
checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_32 )
|
||||
{
|
||||
@ -785,13 +751,13 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_32 )
|
||||
writer.save( "elf_examples/elf_dummy_header_ppc_32.elf" );
|
||||
|
||||
elfio reader;
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/elf_dummy_header_ppc_32.elf" ),
|
||||
true );
|
||||
BOOST_REQUIRE_EQUAL(
|
||||
reader.load( "elf_examples/elf_dummy_header_ppc_32.elf" ), true );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check ELF header
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL,
|
||||
EM_PPC, EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS32, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC,
|
||||
EV_CURRENT, 0x80482b0, 0, 3, 0, 0, 0 );
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
section* sec = reader.sections[""];
|
||||
@ -801,16 +767,13 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_32 )
|
||||
sec = reader.sections[".note"];
|
||||
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), 2 );
|
||||
checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC,
|
||||
0, 28, 0, 0, 4, 0 );
|
||||
checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 );
|
||||
|
||||
sec = reader.sections[".shstrtab"];
|
||||
|
||||
checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0,
|
||||
0, 17, 0, 0, 1, 0 );
|
||||
checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_dummy_out_i386_64 )
|
||||
{
|
||||
@ -841,13 +804,13 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_i386_64 )
|
||||
writer.save( "elf_examples/elf_dummy_header_i386_64.elf" );
|
||||
|
||||
elfio reader;
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/elf_dummy_header_i386_64.elf" ),
|
||||
true );
|
||||
BOOST_REQUIRE_EQUAL(
|
||||
reader.load( "elf_examples/elf_dummy_header_i386_64.elf" ), true );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check ELF header
|
||||
checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL,
|
||||
EM_X86_64, EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS64, ELFDATA2LSB, EV_CURRENT, ET_REL, EM_X86_64,
|
||||
EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 );
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
section* sec = reader.sections[""];
|
||||
@ -857,16 +820,13 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_i386_64 )
|
||||
sec = reader.sections[".note"];
|
||||
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), 2 );
|
||||
checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC,
|
||||
0, 28, 0, 0, 4, 0 );
|
||||
checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 );
|
||||
|
||||
sec = reader.sections[".shstrtab"];
|
||||
|
||||
checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0,
|
||||
0, 17, 0, 0, 1, 0 );
|
||||
checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_64 )
|
||||
{
|
||||
@ -897,13 +857,13 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_64 )
|
||||
writer.save( "elf_examples/elf_dummy_header_ppc_64.elf" );
|
||||
|
||||
elfio reader;
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/elf_dummy_header_ppc_64.elf" ),
|
||||
true );
|
||||
BOOST_REQUIRE_EQUAL(
|
||||
reader.load( "elf_examples/elf_dummy_header_ppc_64.elf" ), true );
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check ELF header
|
||||
checkHeader( reader, ELFCLASS64, ELFDATA2MSB, EV_CURRENT, ET_REL,
|
||||
EM_PPC64, EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 );
|
||||
checkHeader( reader, ELFCLASS64, ELFDATA2MSB, EV_CURRENT, ET_REL, EM_PPC64,
|
||||
EV_CURRENT, 0x120380482b0ull, 0, 3, 0, 0, 0 );
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Check sections
|
||||
section* sec = reader.sections[""];
|
||||
@ -912,24 +872,20 @@ BOOST_AUTO_TEST_CASE( test_dummy_out_ppc_64 )
|
||||
|
||||
sec = reader.sections[".shstrtab"];
|
||||
|
||||
checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0,
|
||||
0, 17, 0, 0, 1, 0 );
|
||||
checkSection( sec, 1, ".shstrtab", SHT_STRTAB, 0, 0, 17, 0, 0, 1, 0 );
|
||||
|
||||
sec = reader.sections[".note"];
|
||||
|
||||
BOOST_CHECK_EQUAL( sec->get_index(), 2 );
|
||||
checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC,
|
||||
0, 28, 0, 0, 4, 0 );
|
||||
checkSection( sec, 2, ".note", SHT_NOTE, SHF_ALLOC, 0, 28, 0, 0, 4, 0 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_dynamic_64_1 )
|
||||
{
|
||||
elfio reader;
|
||||
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ),
|
||||
true );
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ), true );
|
||||
|
||||
section* dynsec = reader.sections[".dynamic"];
|
||||
BOOST_REQUIRE( dynsec != NULL );
|
||||
@ -958,14 +914,12 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_1 )
|
||||
BOOST_CHECK_EQUAL( value, 0 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_dynamic_64_2 )
|
||||
{
|
||||
elfio reader;
|
||||
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/libfunc.so" ),
|
||||
true );
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/libfunc.so" ), true );
|
||||
|
||||
section* dynsec = reader.sections[".dynamic"];
|
||||
BOOST_REQUIRE( dynsec != NULL );
|
||||
@ -991,14 +945,12 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_2 )
|
||||
BOOST_CHECK_EQUAL( value, 0 );
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( test_dynamic_64_3 )
|
||||
{
|
||||
elfio reader;
|
||||
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ),
|
||||
true );
|
||||
BOOST_REQUIRE_EQUAL( reader.load( "elf_examples/main" ), true );
|
||||
|
||||
section* dynsec = reader.sections[".dynamic"];
|
||||
BOOST_REQUIRE( dynsec != NULL );
|
||||
@ -1022,9 +974,7 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_3 )
|
||||
|
||||
for ( unsigned int i = 0; i < da.get_entries_num(); ++i ) {
|
||||
da.get_entry( i, tag, value, str );
|
||||
if ( tag == DT_NEEDED ||
|
||||
tag == DT_SONAME ||
|
||||
tag == DT_RPATH ||
|
||||
if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH ||
|
||||
tag == DT_RUNPATH ) {
|
||||
da1.add_entry( tag, str );
|
||||
}
|
||||
@ -1038,9 +988,7 @@ BOOST_AUTO_TEST_CASE( test_dynamic_64_3 )
|
||||
da1.get_entry( i, tag1, value1, str1 );
|
||||
|
||||
BOOST_CHECK_EQUAL( tag, tag1 );
|
||||
if ( tag == DT_NEEDED ||
|
||||
tag == DT_SONAME ||
|
||||
tag == DT_RPATH ||
|
||||
if ( tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH ||
|
||||
tag == DT_RUNPATH ) {
|
||||
BOOST_CHECK_EQUAL( str, str1 );
|
||||
}
|
||||
|
@ -50,7 +50,8 @@ bool write_obj_i386(bool is64bit)
|
||||
data_sec->set_flags( SHF_ALLOC | SHF_WRITE );
|
||||
data_sec->set_addr_align( 4 );
|
||||
|
||||
char data[] = {'\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10
|
||||
char data[] = {
|
||||
'\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10
|
||||
'\x2C', '\x20', '\x57', '\x6F', '\x72',
|
||||
'\x6C', '\x64', '\x21', '\x0A' };
|
||||
data_sec->set_data( data, sizeof( data ) );
|
||||
@ -70,14 +71,12 @@ bool write_obj_i386(bool is64bit)
|
||||
sym_sec->set_entry_size( writer.get_default_entry_size( SHT_SYMTAB ) );
|
||||
|
||||
symbol_section_accessor symbol_writer( writer, sym_sec );
|
||||
Elf_Word nSymIndex = symbol_writer.add_symbol(nStrIndex, 0, 0,
|
||||
STB_LOCAL, STT_NOTYPE, 0,
|
||||
data_sec->get_index());
|
||||
Elf_Word nSymIndex = symbol_writer.add_symbol(
|
||||
nStrIndex, 0, 0, STB_LOCAL, STT_NOTYPE, 0, data_sec->get_index() );
|
||||
|
||||
// Another way to add symbol
|
||||
symbol_writer.add_symbol(str_writer, "_start", 0x00000000, 0,
|
||||
STB_WEAK, STT_FUNC, 0,
|
||||
text_sec->get_index());
|
||||
symbol_writer.add_symbol( str_writer, "_start", 0x00000000, 0, STB_WEAK,
|
||||
STT_FUNC, 0, text_sec->get_index() );
|
||||
|
||||
// Create relocation table section*
|
||||
section* rel_sec = writer.sections.add( ".rel.text" );
|
||||
@ -107,14 +106,17 @@ bool write_obj_i386(bool is64bit)
|
||||
note_writer.add_note( 0x77, "Created by ELFIO", 0, 0 );
|
||||
|
||||
// Create ELF file
|
||||
writer.save(
|
||||
is64bit ? "elf_examples/write_obj_i386_64.o" : "elf_examples/write_obj_i386_32.o");
|
||||
writer.save( is64bit ? "elf_examples/write_obj_i386_64.o"
|
||||
: "elf_examples/write_obj_i386_32.o" );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
bool write_exe_i386(const std::string &filename, bool is64bit, bool set_addr = false, Elf64_Addr addr = 0)
|
||||
bool write_exe_i386( const std::string& filename,
|
||||
bool is64bit,
|
||||
bool set_addr = false,
|
||||
Elf64_Addr addr = 0 )
|
||||
{
|
||||
elfio writer;
|
||||
|
||||
@ -128,8 +130,7 @@ bool write_exe_i386(const std::string &filename, bool is64bit, bool set_addr = f
|
||||
text_sec->set_type( SHT_PROGBITS );
|
||||
text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR );
|
||||
text_sec->set_addr_align( 0x10 );
|
||||
if (set_addr)
|
||||
{
|
||||
if ( set_addr ) {
|
||||
text_sec->set_address( addr );
|
||||
}
|
||||
|
||||
@ -151,7 +152,8 @@ bool write_exe_i386(const std::string &filename, bool is64bit, bool set_addr = f
|
||||
text_seg->set_physical_address( 0x08048000 );
|
||||
text_seg->set_flags( PF_X | PF_R );
|
||||
text_seg->set_align( 0x1000 );
|
||||
text_seg->add_section_index(text_sec->get_index(), text_sec->get_addr_align());
|
||||
text_seg->add_section_index( text_sec->get_index(),
|
||||
text_sec->get_addr_align() );
|
||||
|
||||
// Create data section*
|
||||
section* data_sec = writer.sections.add( ".data" );
|
||||
@ -159,7 +161,8 @@ bool write_exe_i386(const std::string &filename, bool is64bit, bool set_addr = f
|
||||
data_sec->set_flags( SHF_ALLOC | SHF_WRITE );
|
||||
data_sec->set_addr_align( 0x4 );
|
||||
|
||||
char data[] = {'\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10
|
||||
char data[] = {
|
||||
'\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10
|
||||
'\x2C', '\x20', '\x57', '\x6F', '\x72',
|
||||
'\x6C', '\x64', '\x21', '\x0A' };
|
||||
data_sec->set_data( data, sizeof( data ) );
|
||||
@ -170,7 +173,8 @@ bool write_exe_i386(const std::string &filename, bool is64bit, bool set_addr = f
|
||||
data_seg->set_physical_address( 0x08048020 );
|
||||
data_seg->set_flags( PF_W | PF_R );
|
||||
data_seg->set_align( 0x10 );
|
||||
data_seg->add_section_index(data_sec->get_index(), data_sec->get_addr_align());
|
||||
data_seg->add_section_index( data_sec->get_index(),
|
||||
data_sec->get_addr_align() );
|
||||
|
||||
section* note_sec = writer.sections.add( ".note" );
|
||||
note_sec->set_type( SHT_NOTE );
|
||||
@ -198,8 +202,7 @@ void checkObjestsAreEqual(std::string file_name1, std::string file_name2)
|
||||
BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true );
|
||||
BOOST_REQUIRE_EQUAL( file2.load( file_name2 ), true );
|
||||
|
||||
for (int i = 0; i < file1.sections.size(); ++i)
|
||||
{
|
||||
for ( int i = 0; i < file1.sections.size(); ++i ) {
|
||||
BOOST_CHECK_EQUAL( file1.sections[i]->get_address(),
|
||||
file2.sections[i]->get_address() );
|
||||
BOOST_CHECK_EQUAL( file1.sections[i]->get_addr_align(),
|
||||
@ -224,8 +227,7 @@ void checkObjestsAreEqual(std::string file_name1, std::string file_name2)
|
||||
file2.sections[i]->get_type() );
|
||||
|
||||
if ( file1.sections[i]->get_type() == SHT_NULL ||
|
||||
file1.sections[i]->get_type() == SHT_NOBITS)
|
||||
{
|
||||
file1.sections[i]->get_type() == SHT_NOBITS ) {
|
||||
continue;
|
||||
}
|
||||
BOOST_REQUIRE_NE( file1.sections[i]->get_data(), (const char*)0 );
|
||||
@ -240,8 +242,7 @@ void checkObjestsAreEqual(std::string file_name1, std::string file_name2)
|
||||
BOOST_CHECK_EQUAL( file1.sections[i]->get_size(),
|
||||
file2.sections[i]->get_size() );
|
||||
if ( ( file2.sections[i]->get_type() != SHT_NULL ) &&
|
||||
(file2.sections[i]->get_type() != SHT_NOBITS))
|
||||
{
|
||||
( file2.sections[i]->get_type() != SHT_NOBITS ) ) {
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS( pdata1.begin(), pdata1.end(),
|
||||
pdata2.begin(), pdata2.end() );
|
||||
}
|
||||
@ -249,7 +250,9 @@ void checkObjestsAreEqual(std::string file_name1, std::string file_name2)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void checkExeAreEqual(std::string file_name1, std::string file_name2, int skipTests = 0)
|
||||
void checkExeAreEqual( std::string file_name1,
|
||||
std::string file_name2,
|
||||
int skipTests = 0 )
|
||||
{
|
||||
checkObjestsAreEqual( file_name1, file_name2 );
|
||||
|
||||
@ -259,8 +262,7 @@ void checkExeAreEqual(std::string file_name1, std::string file_name2, int skipTe
|
||||
BOOST_REQUIRE_EQUAL( file1.load( file_name1 ), true );
|
||||
BOOST_REQUIRE_EQUAL( file2.load( file_name2 ), true );
|
||||
|
||||
for (int i = 0; i < file1.segments.size(); ++i)
|
||||
{
|
||||
for ( int i = 0; i < file1.segments.size(); ++i ) {
|
||||
if ( !( skipTests & SEG_ALIGN ) )
|
||||
BOOST_CHECK_EQUAL( file1.segments[i]->get_align(),
|
||||
file2.segments[i]->get_align() );
|
||||
@ -272,7 +274,8 @@ void checkExeAreEqual(std::string file_name1, std::string file_name2, int skipTe
|
||||
file2.segments[i]->get_type() );
|
||||
|
||||
// skip data comparisons of the program header and of empty segments
|
||||
if (file1.segments[i]->get_type() == PT_PHDR || !file1.segments[i]->get_file_size())
|
||||
if ( file1.segments[i]->get_type() == PT_PHDR ||
|
||||
!file1.segments[i]->get_file_size() )
|
||||
continue;
|
||||
|
||||
BOOST_REQUIRE_NE( file1.segments[i]->get_data(), (const char*)0 );
|
||||
@ -287,10 +290,10 @@ void checkExeAreEqual(std::string file_name1, std::string file_name2, int skipTe
|
||||
|
||||
// truncate the data if the header and the segment table is
|
||||
// part of the segment
|
||||
Elf64_Off afterPHDR = file1.get_segments_offset() +
|
||||
Elf64_Off afterPHDR =
|
||||
file1.get_segments_offset() +
|
||||
file1.get_segment_entry_size() * (Elf64_Off)file1.segments.size();
|
||||
if (file1.segments[i]->get_offset() < afterPHDR)
|
||||
{
|
||||
if ( file1.segments[i]->get_offset() < afterPHDR ) {
|
||||
pdata1 = pdata1.substr( (unsigned int)afterPHDR );
|
||||
pdata2 = pdata2.substr( (unsigned int)afterPHDR );
|
||||
}
|
||||
@ -304,7 +307,8 @@ void checkExeAreEqual(std::string file_name1, std::string file_name2, int skipTe
|
||||
BOOST_AUTO_TEST_CASE( write_obj_i386_32 )
|
||||
{
|
||||
BOOST_CHECK_EQUAL( true, write_obj_i386( false ) );
|
||||
output_test_stream output("elf_examples/write_obj_i386_32_match.o", true, false);
|
||||
output_test_stream output( "elf_examples/write_obj_i386_32_match.o", true,
|
||||
false );
|
||||
std::ifstream input( "elf_examples/write_obj_i386_32.o", std::ios::binary );
|
||||
output << input.rdbuf();
|
||||
BOOST_CHECK( output.match_pattern() );
|
||||
@ -314,7 +318,8 @@ BOOST_AUTO_TEST_CASE(write_obj_i386_32)
|
||||
BOOST_AUTO_TEST_CASE( write_obj_i386_64 )
|
||||
{
|
||||
BOOST_CHECK_EQUAL( true, write_obj_i386( true ) );
|
||||
output_test_stream output("elf_examples/write_obj_i386_64_match.o", true, false);
|
||||
output_test_stream output( "elf_examples/write_obj_i386_64_match.o", true,
|
||||
false );
|
||||
std::ifstream input( "elf_examples/write_obj_i386_64.o", std::ios::binary );
|
||||
output << input.rdbuf();
|
||||
BOOST_CHECK( output.match_pattern() );
|
||||
@ -329,7 +334,8 @@ BOOST_AUTO_TEST_CASE(write_exe_i386_32)
|
||||
output_test_stream output( reference_file, true, false );
|
||||
std::ifstream input( generated_file, std::ios::binary );
|
||||
output << input.rdbuf();
|
||||
BOOST_CHECK_MESSAGE(output.match_pattern(), "Comparing " + generated_file + " and " + reference_file);
|
||||
BOOST_CHECK_MESSAGE( output.match_pattern(), "Comparing " + generated_file +
|
||||
" and " + reference_file );
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@ -372,7 +378,8 @@ BOOST_AUTO_TEST_CASE(elfio_copy)
|
||||
{
|
||||
elfio e;
|
||||
|
||||
const std::string filename("elf_examples/write_exe_i386_32_section_added");
|
||||
const std::string filename(
|
||||
"elf_examples/write_exe_i386_32_section_added" );
|
||||
write_exe_i386( filename, false, true, 0x08048100 );
|
||||
|
||||
e.load( filename );
|
||||
@ -388,10 +395,8 @@ BOOST_AUTO_TEST_CASE(elf_exe_copy_64)
|
||||
{
|
||||
checkExeAreEqual( "elf_examples/64bitLOAD.elf",
|
||||
"elf_examples/64bitLOAD_copy.elf" );
|
||||
checkExeAreEqual("elf_examples/asm64",
|
||||
"elf_examples/asm64_copy");
|
||||
checkExeAreEqual("elf_examples/hello_64",
|
||||
"elf_examples/hello_64_copy");
|
||||
checkExeAreEqual( "elf_examples/asm64", "elf_examples/asm64_copy" );
|
||||
checkExeAreEqual( "elf_examples/hello_64", "elf_examples/hello_64_copy" );
|
||||
|
||||
// The last segment (GNU_RELRO) is bigger than necessary.
|
||||
// I don't see why but it contains a few bits of the .got.plt section.
|
||||
@ -405,22 +410,18 @@ BOOST_AUTO_TEST_CASE(elf_exe_copy_64)
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
BOOST_AUTO_TEST_CASE( elf_exe_copy_32 )
|
||||
{
|
||||
checkExeAreEqual("elf_examples/asm",
|
||||
"elf_examples/asm_copy");
|
||||
checkExeAreEqual( "elf_examples/asm", "elf_examples/asm_copy" );
|
||||
checkExeAreEqual( "elf_examples/arm_v7m_test_debug.elf",
|
||||
"elf_examples/arm_v7m_test_debug_copy.elf" );
|
||||
checkExeAreEqual( "elf_examples/arm_v7m_test_release.elf",
|
||||
"elf_examples/arm_v7m_test_release_copy.elf" );
|
||||
checkExeAreEqual("elf_examples/hello_32",
|
||||
"elf_examples/hello_32_copy");
|
||||
checkExeAreEqual("elf_examples/hello_arm",
|
||||
"elf_examples/hello_arm_copy");
|
||||
checkExeAreEqual( "elf_examples/hello_32", "elf_examples/hello_32_copy" );
|
||||
checkExeAreEqual( "elf_examples/hello_arm", "elf_examples/hello_arm_copy" );
|
||||
checkExeAreEqual( "elf_examples/hello_arm_stripped",
|
||||
"elf_examples/hello_arm_stripped_copy" );
|
||||
checkExeAreEqual( "elf_examples/read_write_arm_elf32_input",
|
||||
"elf_examples/read_write_arm_elf32_input_copy" );
|
||||
checkExeAreEqual("elf_examples/test_ppc",
|
||||
"elf_examples/test_ppc_copy");
|
||||
checkExeAreEqual( "elf_examples/test_ppc", "elf_examples/test_ppc_copy" );
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@ -452,8 +453,7 @@ BOOST_AUTO_TEST_CASE(get_symbol_32)
|
||||
section* psymsec = elf.sections[".symtab"];
|
||||
const symbol_section_accessor symbols( elf, psymsec );
|
||||
|
||||
BOOST_CHECK_EQUAL(true,
|
||||
symbols.get_symbol(0x08048478, name, size, bind,
|
||||
BOOST_CHECK_EQUAL( true, symbols.get_symbol( 0x08048478, name, size, bind,
|
||||
type, section_index, other ) );
|
||||
BOOST_CHECK_EQUAL( "_IO_stdin_used", name );
|
||||
BOOST_CHECK_EQUAL( 14, section_index );
|
||||
@ -476,8 +476,7 @@ BOOST_AUTO_TEST_CASE(get_symbol_64)
|
||||
section* psymsec = elf.sections[".symtab"];
|
||||
const symbol_section_accessor symbols( elf, psymsec );
|
||||
|
||||
BOOST_CHECK_EQUAL(true,
|
||||
symbols.get_symbol(0x00400498, name, size, bind,
|
||||
BOOST_CHECK_EQUAL( true, symbols.get_symbol( 0x00400498, name, size, bind,
|
||||
type, section_index, other ) );
|
||||
BOOST_CHECK_EQUAL( "main", name );
|
||||
BOOST_CHECK_EQUAL( 12, section_index );
|
||||
@ -535,11 +534,16 @@ BOOST_AUTO_TEST_CASE(null_section_inside_segment)
|
||||
text_seg->set_flags( PF_X | PF_R );
|
||||
text_seg->set_align( 0x1000 );
|
||||
// Add sections into the loadable segment
|
||||
text_seg->add_section_index(null_sec1->get_index(), null_sec1->get_addr_align());
|
||||
text_seg->add_section_index(text_sec1->get_index(), text_sec1->get_addr_align());
|
||||
text_seg->add_section_index(null_sec2->get_index(), null_sec2->get_addr_align());
|
||||
text_seg->add_section_index(text_sec2->get_index(), text_sec2->get_addr_align());
|
||||
text_seg->add_section_index(null_sec3->get_index(), null_sec3->get_addr_align());
|
||||
text_seg->add_section_index( null_sec1->get_index(),
|
||||
null_sec1->get_addr_align() );
|
||||
text_seg->add_section_index( text_sec1->get_index(),
|
||||
text_sec1->get_addr_align() );
|
||||
text_seg->add_section_index( null_sec2->get_index(),
|
||||
null_sec2->get_addr_align() );
|
||||
text_seg->add_section_index( text_sec2->get_index(),
|
||||
text_sec2->get_addr_align() );
|
||||
text_seg->add_section_index( null_sec3->get_index(),
|
||||
null_sec3->get_addr_align() );
|
||||
// Setup entry point
|
||||
writer.set_entry( 0x08048000 );
|
||||
// Create ELF file
|
||||
@ -571,19 +575,18 @@ BOOST_AUTO_TEST_CASE(invalid_file)
|
||||
BOOST_REQUIRE_NE( psymsec, (void*)0 );
|
||||
const symbol_section_accessor symbols( elf, psymsec );
|
||||
|
||||
BOOST_CHECK_EQUAL(true,
|
||||
symbols.get_symbol("main", value, size, bind,
|
||||
BOOST_CHECK_EQUAL( true, symbols.get_symbol( "main", value, size, bind,
|
||||
type, section_index, other ) );
|
||||
BOOST_CHECK_EQUAL( 0x402560, value );
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
true, symbols.get_symbol("frame_dummy", value, size, bind,
|
||||
BOOST_CHECK_EQUAL( true,
|
||||
symbols.get_symbol( "frame_dummy", value, size, bind,
|
||||
type, section_index, other ) );
|
||||
BOOST_CHECK_EQUAL( 0x402550, value );
|
||||
|
||||
BOOST_CHECK_EQUAL( false,
|
||||
symbols.get_symbol(0x00400498, name, size, bind,
|
||||
type, section_index, other));
|
||||
symbols.get_symbol( 0x00400498, name, size, bind, type,
|
||||
section_index, other ) );
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@ -620,35 +623,43 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols)
|
||||
name = "Str1";
|
||||
bind = STB_GLOBAL;
|
||||
value = 1;
|
||||
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
|
||||
other, section_index );
|
||||
name = "Str2";
|
||||
bind = STB_LOCAL;
|
||||
value = 2;
|
||||
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
|
||||
other, section_index );
|
||||
name = "Str3";
|
||||
bind = STB_WEAK;
|
||||
value = 3;
|
||||
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
|
||||
other, section_index );
|
||||
name = "Str4";
|
||||
bind = STB_LOCAL;
|
||||
value = 4;
|
||||
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
|
||||
other, section_index );
|
||||
name = "Str5";
|
||||
bind = STB_LOCAL;
|
||||
value = 5;
|
||||
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
|
||||
other, section_index );
|
||||
name = "Str6";
|
||||
bind = STB_GLOBAL;
|
||||
value = 6;
|
||||
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
|
||||
other, section_index );
|
||||
name = "Str7";
|
||||
bind = STB_LOCAL;
|
||||
value = 7;
|
||||
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
|
||||
other, section_index );
|
||||
name = "Str8";
|
||||
bind = STB_WEAK;
|
||||
value = 8;
|
||||
symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
symbols.add_symbol( str_writer, name.c_str(), value, size, bind, type,
|
||||
other, section_index );
|
||||
|
||||
symbols.arrange_local_symbols( [&]( Elf_Xword first, Elf_Xword ) -> void {
|
||||
static int counter = 0;
|
||||
@ -686,17 +697,17 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols)
|
||||
BOOST_CHECK_LE( (Elf_Xword)bound, num );
|
||||
|
||||
// Check that all symbols are LOCAL until the bound value
|
||||
for (Elf_Word i = 0; i < bound; i++)
|
||||
{
|
||||
rsymbols.get_symbol(i, name, value, size, bind, type, section_index, other);
|
||||
for ( Elf_Word i = 0; i < bound; i++ ) {
|
||||
rsymbols.get_symbol( i, name, value, size, bind, type, section_index,
|
||||
other );
|
||||
BOOST_CHECK_EQUAL( bind, (unsigned char)STB_LOCAL );
|
||||
}
|
||||
BOOST_CHECK_EQUAL( name, "Str7" );
|
||||
|
||||
// Check that all symbols are not LOCAL after the bound value
|
||||
for (Elf_Word i = bound; i < num; i++)
|
||||
{
|
||||
rsymbols.get_symbol(i, name, value, size, bind, type, section_index, other);
|
||||
for ( Elf_Word i = bound; i < num; i++ ) {
|
||||
rsymbols.get_symbol( i, name, value, size, bind, type, section_index,
|
||||
other );
|
||||
|
||||
BOOST_CHECK_NE( bind, (unsigned char)STB_LOCAL );
|
||||
}
|
||||
@ -742,35 +753,43 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols_with_reallocation)
|
||||
name = "Str1";
|
||||
bind = STB_GLOBAL;
|
||||
value = 1;
|
||||
Elf_Word sym1 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
Elf_Word sym1 = symbols.add_symbol( str_writer, name.c_str(), value, size,
|
||||
bind, type, other, section_index );
|
||||
name = "Str2";
|
||||
bind = STB_LOCAL;
|
||||
value = 2;
|
||||
Elf_Word sym2 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
Elf_Word sym2 = symbols.add_symbol( str_writer, name.c_str(), value, size,
|
||||
bind, type, other, section_index );
|
||||
name = "Str3";
|
||||
bind = STB_WEAK;
|
||||
value = 3;
|
||||
Elf_Word sym3 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
Elf_Word sym3 = symbols.add_symbol( str_writer, name.c_str(), value, size,
|
||||
bind, type, other, section_index );
|
||||
name = "Str4";
|
||||
bind = STB_LOCAL;
|
||||
value = 4;
|
||||
Elf_Word sym4 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
Elf_Word sym4 = symbols.add_symbol( str_writer, name.c_str(), value, size,
|
||||
bind, type, other, section_index );
|
||||
name = "Str5";
|
||||
bind = STB_LOCAL;
|
||||
value = 5;
|
||||
Elf_Word sym5 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
Elf_Word sym5 = symbols.add_symbol( str_writer, name.c_str(), value, size,
|
||||
bind, type, other, section_index );
|
||||
name = "Str6";
|
||||
bind = STB_GLOBAL;
|
||||
value = 6;
|
||||
Elf_Word sym6 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
Elf_Word sym6 = symbols.add_symbol( str_writer, name.c_str(), value, size,
|
||||
bind, type, other, section_index );
|
||||
name = "Str7";
|
||||
bind = STB_LOCAL;
|
||||
value = 7;
|
||||
Elf_Word sym7 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
Elf_Word sym7 = symbols.add_symbol( str_writer, name.c_str(), value, size,
|
||||
bind, type, other, section_index );
|
||||
name = "Str8";
|
||||
bind = STB_WEAK;
|
||||
value = 8;
|
||||
Elf_Word sym8 = symbols.add_symbol(str_writer, name.c_str(), value, size, bind, type, other, section_index);
|
||||
Elf_Word sym8 = symbols.add_symbol( str_writer, name.c_str(), value, size,
|
||||
bind, type, other, section_index );
|
||||
|
||||
section* rel_sec = writer.sections.add( ".rel.text" );
|
||||
rel_sec->set_type( SHT_REL );
|
||||
@ -800,15 +819,15 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols_with_reallocation)
|
||||
|
||||
std::vector<std::string> before;
|
||||
|
||||
for (Elf_Word i = 0; i < rela.get_entries_num(); i++)
|
||||
{
|
||||
for ( Elf_Word i = 0; i < rela.get_entries_num(); i++ ) {
|
||||
Elf64_Addr offset;
|
||||
Elf_Word symbol;
|
||||
Elf_Word rtype;
|
||||
Elf_Sxword addend;
|
||||
|
||||
rela.get_entry( i, offset, symbol, rtype, addend );
|
||||
symbols.get_symbol(symbol, name, value, size, bind, type, section_index, other);
|
||||
symbols.get_symbol( symbol, name, value, size, bind, type,
|
||||
section_index, other );
|
||||
before.push_back( name );
|
||||
}
|
||||
|
||||
@ -834,17 +853,18 @@ BOOST_AUTO_TEST_CASE(rearrange_local_symbols_with_reallocation)
|
||||
|
||||
std::vector<std::string> after;
|
||||
|
||||
for (Elf_Word i = 0; i < rel.get_entries_num(); i++)
|
||||
{
|
||||
for ( Elf_Word i = 0; i < rel.get_entries_num(); i++ ) {
|
||||
Elf64_Addr offset;
|
||||
Elf_Word symbol;
|
||||
Elf_Word rtype;
|
||||
Elf_Sxword addend;
|
||||
|
||||
rel.get_entry( i, offset, symbol, rtype, addend );
|
||||
syms.get_symbol(symbol, name, value, size, bind, type, section_index, other);
|
||||
syms.get_symbol( symbol, name, value, size, bind, type, section_index,
|
||||
other );
|
||||
after.push_back( name );
|
||||
}
|
||||
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(before.begin(), before.end(), after.begin(), after.end());
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS( before.begin(), before.end(), after.begin(),
|
||||
after.end() );
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user