Apply .clangformat to all repo source files
Change-Id: I7e79c6058f0303f9a98911e3b7dd2e8596079344
此提交包含在:
1 父節點
18e70b1e6b
當前提交
1ba06f63c4
293 file changed
+43974
-45824
No files matched your search
+542
-543
File diff suppressed because it is too large.
Load diff
+328
-436
File diff suppressed because it is too large.
Load diff
+529
-676
File diff suppressed because it is too large.
Load diff
+135
-171
@@ -26,228 +26,192 @@ THE SOFTWARE.
|
||||
namespace ELFIO {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class dynamic_section_accessor
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
dynamic_section_accessor( const elfio& elf_file_, section* section_ ) :
|
||||
elf_file( elf_file_ ),
|
||||
dynamic_section( section_ )
|
||||
{
|
||||
}
|
||||
class dynamic_section_accessor {
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
dynamic_section_accessor(const elfio& elf_file_, section* 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() ) {
|
||||
if (0 != dynamic_section->get_entry_size()) {
|
||||
nRet = dynamic_section->get_size() / dynamic_section->get_entry_size();
|
||||
}
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_entry( Elf_Xword index,
|
||||
Elf_Xword& tag,
|
||||
Elf_Xword& value,
|
||||
std::string& str ) const
|
||||
{
|
||||
if ( index >= get_entries_num() ) { // Is index valid
|
||||
//------------------------------------------------------------------------------
|
||||
bool get_entry(Elf_Xword index, Elf_Xword& tag, Elf_Xword& value, std::string& str) const {
|
||||
if (index >= get_entries_num()) { // Is index valid
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
generic_get_entry_dyn< Elf32_Dyn >( index, tag, value );
|
||||
}
|
||||
else {
|
||||
generic_get_entry_dyn< Elf64_Dyn >( index, tag, value );
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
generic_get_entry_dyn<Elf32_Dyn>(index, tag, value);
|
||||
} else {
|
||||
generic_get_entry_dyn<Elf64_Dyn>(index, tag, value);
|
||||
}
|
||||
|
||||
// If the tag may have a string table reference, prepare the string
|
||||
if ( tag == DT_NEEDED ||
|
||||
tag == DT_SONAME ||
|
||||
tag == DT_RPATH ||
|
||||
tag == DT_RUNPATH ) {
|
||||
string_section_accessor strsec =
|
||||
elf_file.sections[ get_string_table_index() ];
|
||||
const char* result = strsec.get_string( value );
|
||||
if ( 0 == result ) {
|
||||
if (tag == DT_NEEDED || tag == DT_SONAME || tag == DT_RPATH || tag == DT_RUNPATH) {
|
||||
string_section_accessor strsec = elf_file.sections[get_string_table_index()];
|
||||
const char* result = strsec.get_string(value);
|
||||
if (0 == result) {
|
||||
str.clear();
|
||||
return false;
|
||||
}
|
||||
str = result;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
str.clear();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf_Xword& tag,
|
||||
Elf_Xword& value )
|
||||
{
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
generic_add_entry< Elf32_Dyn >( tag, value );
|
||||
}
|
||||
else {
|
||||
generic_add_entry< Elf64_Dyn >( tag, value );
|
||||
//------------------------------------------------------------------------------
|
||||
void add_entry(Elf_Xword& tag, Elf_Xword& value) {
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
generic_add_entry<Elf32_Dyn>(tag, value);
|
||||
} else {
|
||||
generic_add_entry<Elf64_Dyn>(tag, value);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf_Xword& tag,
|
||||
std::string& str )
|
||||
{
|
||||
string_section_accessor strsec =
|
||||
elf_file.sections[ get_string_table_index() ];
|
||||
Elf_Xword value = strsec.add_string( str );
|
||||
add_entry( tag, value );
|
||||
//------------------------------------------------------------------------------
|
||||
void add_entry(Elf_Xword& tag, std::string& str) {
|
||||
string_section_accessor strsec = elf_file.sections[get_string_table_index()];
|
||||
Elf_Xword value = strsec.add_string(str);
|
||||
add_entry(tag, value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_string_table_index() const
|
||||
{
|
||||
return (Elf_Half)dynamic_section->get_link();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
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,
|
||||
Elf_Xword& tag,
|
||||
Elf_Xword& value ) const
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void generic_get_entry_dyn(Elf_Xword index, Elf_Xword& tag, Elf_Xword& value) const {
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
// 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() ) {
|
||||
tag = DT_NULL;
|
||||
if (dynamic_section->get_data() == 0 ||
|
||||
(index + 1) * dynamic_section->get_entry_size() > dynamic_section->get_size()) {
|
||||
tag = DT_NULL;
|
||||
value = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
const T* pEntry = reinterpret_cast<const T*>(
|
||||
dynamic_section->get_data() +
|
||||
index * dynamic_section->get_entry_size() );
|
||||
tag = convertor( pEntry->d_tag );
|
||||
switch ( tag ) {
|
||||
case DT_NULL:
|
||||
case DT_SYMBOLIC:
|
||||
case DT_TEXTREL:
|
||||
case DT_BIND_NOW:
|
||||
value = 0;
|
||||
break;
|
||||
case DT_NEEDED:
|
||||
case DT_PLTRELSZ:
|
||||
case DT_RELASZ:
|
||||
case DT_RELAENT:
|
||||
case DT_STRSZ:
|
||||
case DT_SYMENT:
|
||||
case DT_SONAME:
|
||||
case DT_RPATH:
|
||||
case DT_RELSZ:
|
||||
case DT_RELENT:
|
||||
case DT_PLTREL:
|
||||
case DT_INIT_ARRAYSZ:
|
||||
case DT_FINI_ARRAYSZ:
|
||||
case DT_RUNPATH:
|
||||
case DT_FLAGS:
|
||||
case DT_PREINIT_ARRAYSZ:
|
||||
value = convertor( pEntry->d_un.d_val );
|
||||
break;
|
||||
case DT_PLTGOT:
|
||||
case DT_HASH:
|
||||
case DT_STRTAB:
|
||||
case DT_SYMTAB:
|
||||
case DT_RELA:
|
||||
case DT_INIT:
|
||||
case DT_FINI:
|
||||
case DT_REL:
|
||||
case DT_DEBUG:
|
||||
case DT_JMPREL:
|
||||
case DT_INIT_ARRAY:
|
||||
case DT_FINI_ARRAY:
|
||||
case DT_PREINIT_ARRAY:
|
||||
default:
|
||||
value = convertor( pEntry->d_un.d_ptr );
|
||||
break;
|
||||
const T* pEntry = reinterpret_cast<const T*>(dynamic_section->get_data() +
|
||||
index * dynamic_section->get_entry_size());
|
||||
tag = convertor(pEntry->d_tag);
|
||||
switch (tag) {
|
||||
case DT_NULL:
|
||||
case DT_SYMBOLIC:
|
||||
case DT_TEXTREL:
|
||||
case DT_BIND_NOW:
|
||||
value = 0;
|
||||
break;
|
||||
case DT_NEEDED:
|
||||
case DT_PLTRELSZ:
|
||||
case DT_RELASZ:
|
||||
case DT_RELAENT:
|
||||
case DT_STRSZ:
|
||||
case DT_SYMENT:
|
||||
case DT_SONAME:
|
||||
case DT_RPATH:
|
||||
case DT_RELSZ:
|
||||
case DT_RELENT:
|
||||
case DT_PLTREL:
|
||||
case DT_INIT_ARRAYSZ:
|
||||
case DT_FINI_ARRAYSZ:
|
||||
case DT_RUNPATH:
|
||||
case DT_FLAGS:
|
||||
case DT_PREINIT_ARRAYSZ:
|
||||
value = convertor(pEntry->d_un.d_val);
|
||||
break;
|
||||
case DT_PLTGOT:
|
||||
case DT_HASH:
|
||||
case DT_STRTAB:
|
||||
case DT_SYMTAB:
|
||||
case DT_RELA:
|
||||
case DT_INIT:
|
||||
case DT_FINI:
|
||||
case DT_REL:
|
||||
case DT_DEBUG:
|
||||
case DT_JMPREL:
|
||||
case DT_INIT_ARRAY:
|
||||
case DT_FINI_ARRAY:
|
||||
case DT_PREINIT_ARRAY:
|
||||
default:
|
||||
value = convertor(pEntry->d_un.d_ptr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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();
|
||||
|
||||
T entry;
|
||||
|
||||
switch ( tag ) {
|
||||
case DT_NULL:
|
||||
case DT_SYMBOLIC:
|
||||
case DT_TEXTREL:
|
||||
case DT_BIND_NOW:
|
||||
value = 0;
|
||||
case DT_NEEDED:
|
||||
case DT_PLTRELSZ:
|
||||
case DT_RELASZ:
|
||||
case DT_RELAENT:
|
||||
case DT_STRSZ:
|
||||
case DT_SYMENT:
|
||||
case DT_SONAME:
|
||||
case DT_RPATH:
|
||||
case DT_RELSZ:
|
||||
case DT_RELENT:
|
||||
case DT_PLTREL:
|
||||
case DT_INIT_ARRAYSZ:
|
||||
case DT_FINI_ARRAYSZ:
|
||||
case DT_RUNPATH:
|
||||
case DT_FLAGS:
|
||||
case DT_PREINIT_ARRAYSZ:
|
||||
entry.d_un.d_val = convertor( value );
|
||||
break;
|
||||
case DT_PLTGOT:
|
||||
case DT_HASH:
|
||||
case DT_STRTAB:
|
||||
case DT_SYMTAB:
|
||||
case DT_RELA:
|
||||
case DT_INIT:
|
||||
case DT_FINI:
|
||||
case DT_REL:
|
||||
case DT_DEBUG:
|
||||
case DT_JMPREL:
|
||||
case DT_INIT_ARRAY:
|
||||
case DT_FINI_ARRAY:
|
||||
case DT_PREINIT_ARRAY:
|
||||
default:
|
||||
entry.d_un.d_ptr = convertor( value );
|
||||
break;
|
||||
switch (tag) {
|
||||
case DT_NULL:
|
||||
case DT_SYMBOLIC:
|
||||
case DT_TEXTREL:
|
||||
case DT_BIND_NOW:
|
||||
value = 0;
|
||||
case DT_NEEDED:
|
||||
case DT_PLTRELSZ:
|
||||
case DT_RELASZ:
|
||||
case DT_RELAENT:
|
||||
case DT_STRSZ:
|
||||
case DT_SYMENT:
|
||||
case DT_SONAME:
|
||||
case DT_RPATH:
|
||||
case DT_RELSZ:
|
||||
case DT_RELENT:
|
||||
case DT_PLTREL:
|
||||
case DT_INIT_ARRAYSZ:
|
||||
case DT_FINI_ARRAYSZ:
|
||||
case DT_RUNPATH:
|
||||
case DT_FLAGS:
|
||||
case DT_PREINIT_ARRAYSZ:
|
||||
entry.d_un.d_val = convertor(value);
|
||||
break;
|
||||
case DT_PLTGOT:
|
||||
case DT_HASH:
|
||||
case DT_STRTAB:
|
||||
case DT_SYMTAB:
|
||||
case DT_RELA:
|
||||
case DT_INIT:
|
||||
case DT_FINI:
|
||||
case DT_REL:
|
||||
case DT_DEBUG:
|
||||
case DT_JMPREL:
|
||||
case DT_INIT_ARRAY:
|
||||
case DT_FINI_ARRAY:
|
||||
case DT_PREINIT_ARRAY:
|
||||
default:
|
||||
entry.d_un.d_ptr = convertor(value);
|
||||
break;
|
||||
}
|
||||
|
||||
entry.d_tag = convertor( tag );
|
||||
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));
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
const elfio& elf_file;
|
||||
section* dynamic_section;
|
||||
section* dynamic_section;
|
||||
};
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_DYNAMIC_HPP
|
||||
#endif // ELFIO_DYNAMIC_HPP
|
||||
+77
-81
@@ -27,120 +27,116 @@ THE SOFTWARE.
|
||||
|
||||
namespace ELFIO {
|
||||
|
||||
class elf_header
|
||||
{
|
||||
public:
|
||||
virtual ~elf_header() {};
|
||||
virtual bool load( std::istream& stream ) = 0;
|
||||
virtual bool save( std::ostream& stream ) const = 0;
|
||||
class elf_header {
|
||||
public:
|
||||
virtual ~elf_header(){};
|
||||
virtual bool load(std::istream& stream) = 0;
|
||||
virtual bool save(std::ostream& stream) const = 0;
|
||||
|
||||
// ELF header functions
|
||||
ELFIO_GET_ACCESS_DECL( unsigned char, class );
|
||||
ELFIO_GET_ACCESS_DECL( unsigned char, elf_version );
|
||||
ELFIO_GET_ACCESS_DECL( unsigned char, encoding );
|
||||
ELFIO_GET_ACCESS_DECL( Elf_Word, version );
|
||||
ELFIO_GET_ACCESS_DECL( Elf_Half, header_size );
|
||||
ELFIO_GET_ACCESS_DECL( Elf_Half, section_entry_size );
|
||||
ELFIO_GET_ACCESS_DECL( Elf_Half, segment_entry_size );
|
||||
ELFIO_GET_ACCESS_DECL(unsigned char, class);
|
||||
ELFIO_GET_ACCESS_DECL(unsigned char, elf_version);
|
||||
ELFIO_GET_ACCESS_DECL(unsigned char, encoding);
|
||||
ELFIO_GET_ACCESS_DECL(Elf_Word, version);
|
||||
ELFIO_GET_ACCESS_DECL(Elf_Half, header_size);
|
||||
ELFIO_GET_ACCESS_DECL(Elf_Half, section_entry_size);
|
||||
ELFIO_GET_ACCESS_DECL(Elf_Half, segment_entry_size);
|
||||
|
||||
ELFIO_GET_SET_ACCESS_DECL( unsigned char, os_abi );
|
||||
ELFIO_GET_SET_ACCESS_DECL( unsigned char, abi_version );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Half, type );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Half, machine );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, entry );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Half, sections_num );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, sections_offset );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Half, segments_num );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, segments_offset );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Half, section_name_str_index );
|
||||
ELFIO_GET_SET_ACCESS_DECL(unsigned char, os_abi);
|
||||
ELFIO_GET_SET_ACCESS_DECL(unsigned char, abi_version);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Half, type);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Half, machine);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Word, flags);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf64_Addr, entry);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Half, sections_num);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf64_Off, sections_offset);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Half, segments_num);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf64_Off, segments_offset);
|
||||
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 <class T>
|
||||
struct elf_header_impl_types;
|
||||
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;
|
||||
};
|
||||
|
||||
template< class T > class elf_header_impl : public elf_header
|
||||
{
|
||||
public:
|
||||
elf_header_impl( endianess_convertor* convertor_,
|
||||
unsigned char encoding )
|
||||
{
|
||||
template <class T>
|
||||
class elf_header_impl : public elf_header {
|
||||
public:
|
||||
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;
|
||||
header.e_ident[EI_MAG2] = ELFMAG2;
|
||||
header.e_ident[EI_MAG3] = ELFMAG3;
|
||||
header.e_ident[EI_CLASS] = elf_header_impl_types<T>::file_class;
|
||||
header.e_ident[EI_DATA] = encoding;
|
||||
header.e_ident[EI_MAG0] = ELFMAG0;
|
||||
header.e_ident[EI_MAG1] = ELFMAG1;
|
||||
header.e_ident[EI_MAG2] = ELFMAG2;
|
||||
header.e_ident[EI_MAG3] = ELFMAG3;
|
||||
header.e_ident[EI_CLASS] = elf_header_impl_types<T>::file_class;
|
||||
header.e_ident[EI_DATA] = encoding;
|
||||
header.e_ident[EI_VERSION] = EV_CURRENT;
|
||||
header.e_version = EV_CURRENT;
|
||||
header.e_version = (*convertor)( header.e_version );
|
||||
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 = (*convertor)( header.e_phentsize );
|
||||
header.e_shentsize = (*convertor)( header.e_shentsize );
|
||||
header.e_version = EV_CURRENT;
|
||||
header.e_version = (*convertor)(header.e_version);
|
||||
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 = (*convertor)(header.e_phentsize);
|
||||
header.e_shentsize = (*convertor)(header.e_shentsize);
|
||||
}
|
||||
|
||||
bool
|
||||
load( std::istream& stream )
|
||||
{
|
||||
stream.seekg( 0 );
|
||||
stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) );
|
||||
bool load(std::istream& stream) {
|
||||
stream.seekg(0);
|
||||
stream.read(reinterpret_cast<char*>(&header), sizeof(header));
|
||||
|
||||
return (stream.gcount() == sizeof( header ) );
|
||||
return (stream.gcount() == sizeof(header));
|
||||
}
|
||||
|
||||
bool
|
||||
save( std::ostream& stream ) const
|
||||
{
|
||||
stream.seekp( 0 );
|
||||
stream.write( reinterpret_cast<const char*>( &header ), sizeof( header ) );
|
||||
bool save(std::ostream& stream) const {
|
||||
stream.seekp(0);
|
||||
stream.write(reinterpret_cast<const char*>(&header), sizeof(header));
|
||||
|
||||
return stream.good();
|
||||
}
|
||||
|
||||
// ELF header functions
|
||||
ELFIO_GET_ACCESS( unsigned char, class, header.e_ident[EI_CLASS] );
|
||||
ELFIO_GET_ACCESS( unsigned char, elf_version, header.e_ident[EI_VERSION] );
|
||||
ELFIO_GET_ACCESS( unsigned char, encoding, header.e_ident[EI_DATA] );
|
||||
ELFIO_GET_ACCESS( Elf_Word, version, header.e_version );
|
||||
ELFIO_GET_ACCESS( Elf_Half, header_size, header.e_ehsize );
|
||||
ELFIO_GET_ACCESS( Elf_Half, section_entry_size, header.e_shentsize );
|
||||
ELFIO_GET_ACCESS( Elf_Half, segment_entry_size, header.e_phentsize );
|
||||
ELFIO_GET_ACCESS(unsigned char, class, header.e_ident[EI_CLASS]);
|
||||
ELFIO_GET_ACCESS(unsigned char, elf_version, header.e_ident[EI_VERSION]);
|
||||
ELFIO_GET_ACCESS(unsigned char, encoding, header.e_ident[EI_DATA]);
|
||||
ELFIO_GET_ACCESS(Elf_Word, version, header.e_version);
|
||||
ELFIO_GET_ACCESS(Elf_Half, header_size, header.e_ehsize);
|
||||
ELFIO_GET_ACCESS(Elf_Half, section_entry_size, header.e_shentsize);
|
||||
ELFIO_GET_ACCESS(Elf_Half, segment_entry_size, header.e_phentsize);
|
||||
|
||||
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( 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 );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Half, section_name_str_index, header.e_shstrndx );
|
||||
ELFIO_GET_SET_ACCESS( Elf64_Addr, entry, header.e_entry );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Half, sections_num, header.e_shnum );
|
||||
ELFIO_GET_SET_ACCESS( Elf64_Off, sections_offset, header.e_shoff );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Half, segments_num, header.e_phnum );
|
||||
ELFIO_GET_SET_ACCESS( Elf64_Off, segments_offset, header.e_phoff );
|
||||
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(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);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Half, section_name_str_index, header.e_shstrndx);
|
||||
ELFIO_GET_SET_ACCESS(Elf64_Addr, entry, header.e_entry);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Half, sections_num, header.e_shnum);
|
||||
ELFIO_GET_SET_ACCESS(Elf64_Off, sections_offset, header.e_shoff);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Half, segments_num, header.e_phnum);
|
||||
ELFIO_GET_SET_ACCESS(Elf64_Off, segments_offset, header.e_phoff);
|
||||
|
||||
private:
|
||||
private:
|
||||
T header;
|
||||
endianess_convertor* convertor;
|
||||
};
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELF_HEADER_HPP
|
||||
#endif // ELF_HEADER_HPP
|
||||
+61
-83
@@ -38,129 +38,107 @@ namespace ELFIO {
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class note_section_accessor
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
note_section_accessor( const elfio& elf_file_, section* section_ ) :
|
||||
elf_file( elf_file_ ), note_section( section_ )
|
||||
{
|
||||
class note_section_accessor {
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
note_section_accessor(const elfio& elf_file_, section* section_)
|
||||
: elf_file(elf_file_), note_section(section_) {
|
||||
process_section();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
get_notes_num() const
|
||||
{
|
||||
return (Elf_Word)note_start_positions.size();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word get_notes_num() const { return (Elf_Word)note_start_positions.size(); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_note( Elf_Word index,
|
||||
Elf_Word& type,
|
||||
std::string& name,
|
||||
void*& desc,
|
||||
Elf_Word& descSize ) const
|
||||
{
|
||||
if ( index >= note_section->get_size() ) {
|
||||
//------------------------------------------------------------------------------
|
||||
bool get_note(Elf_Word index, Elf_Word& type, std::string& name, void*& desc,
|
||||
Elf_Word& descSize) const {
|
||||
if (index >= note_section->get_size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* pData = note_section->get_data() + note_start_positions[index];
|
||||
int align = sizeof( Elf_Word );
|
||||
int align = sizeof(Elf_Word);
|
||||
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
type = convertor( *(Elf_Word*)( pData + 2*align ) );
|
||||
Elf_Word namesz = convertor( *(Elf_Word*)( pData ) );
|
||||
descSize = convertor( *(Elf_Word*)( pData + sizeof( namesz ) ) );
|
||||
type = convertor(*(Elf_Word*)(pData + 2 * align));
|
||||
Elf_Word namesz = convertor(*(Elf_Word*)(pData));
|
||||
descSize = convertor(*(Elf_Word*)(pData + sizeof(namesz)));
|
||||
Elf_Word max_name_size = note_section->get_size() - note_start_positions[index];
|
||||
if ( namesz > max_name_size ||
|
||||
namesz + descSize > max_name_size ) {
|
||||
if (namesz > max_name_size || namesz + descSize > max_name_size) {
|
||||
return false;
|
||||
}
|
||||
name.assign( pData + 3*align, namesz - 1);
|
||||
if ( 0 == descSize ) {
|
||||
name.assign(pData + 3 * align, namesz - 1);
|
||||
if (0 == descSize) {
|
||||
desc = 0;
|
||||
}
|
||||
else {
|
||||
desc = const_cast<char*> ( pData + 3*align +
|
||||
( ( namesz + align - 1 )/align )*align );
|
||||
} else {
|
||||
desc = const_cast<char*>(pData + 3 * align + ((namesz + align - 1) / align) * align);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void add_note( Elf_Word type,
|
||||
const std::string& name,
|
||||
const void* desc,
|
||||
Elf_Word descSize )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
void add_note(Elf_Word type, const std::string& name, const void* desc, Elf_Word descSize) {
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
int align = sizeof( Elf_Word );
|
||||
Elf_Word nameLen = (Elf_Word)name.size() + 1;
|
||||
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 );
|
||||
buffer.append( name );
|
||||
buffer.append( 1, '\x00' );
|
||||
const char pad[] = { '\0', '\0', '\0', '\0' };
|
||||
if ( nameLen % align != 0 ) {
|
||||
buffer.append( pad, align - nameLen % align );
|
||||
int align = sizeof(Elf_Word);
|
||||
Elf_Word nameLen = (Elf_Word)name.size() + 1;
|
||||
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);
|
||||
buffer.append(name);
|
||||
buffer.append(1, '\x00');
|
||||
const char pad[] = {'\0', '\0', '\0', '\0'};
|
||||
if (nameLen % align != 0) {
|
||||
buffer.append(pad, align - nameLen % align);
|
||||
}
|
||||
if ( desc != 0 && descSize != 0 ) {
|
||||
buffer.append( reinterpret_cast<const char*>( desc ), descSize );
|
||||
if ( descSize % align != 0 ) {
|
||||
buffer.append( pad, align - descSize % align );
|
||||
if (desc != 0 && descSize != 0) {
|
||||
buffer.append(reinterpret_cast<const char*>(desc), descSize);
|
||||
if (descSize % align != 0) {
|
||||
buffer.append(pad, align - descSize % align);
|
||||
}
|
||||
}
|
||||
|
||||
note_start_positions.push_back( note_section->get_size() );
|
||||
note_section->append_data( buffer );
|
||||
note_start_positions.push_back(note_section->get_size());
|
||||
note_section->append_data(buffer);
|
||||
}
|
||||
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
void process_section()
|
||||
{
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
void process_section() {
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
const char* data = note_section->get_data();
|
||||
Elf_Xword size = note_section->get_size();
|
||||
Elf_Xword current = 0;
|
||||
const char* data = note_section->get_data();
|
||||
Elf_Xword size = note_section->get_size();
|
||||
Elf_Xword current = 0;
|
||||
|
||||
note_start_positions.clear();
|
||||
|
||||
// Is it empty?
|
||||
if ( 0 == data || 0 == size ) {
|
||||
if (0 == data || 0 == size) {
|
||||
return;
|
||||
}
|
||||
|
||||
int align = sizeof( Elf_Word );
|
||||
while ( current + 3*align <= size ) {
|
||||
note_start_positions.push_back( current );
|
||||
Elf_Word namesz = convertor(
|
||||
*(Elf_Word*)( data + current ) );
|
||||
Elf_Word descsz = convertor(
|
||||
*(Elf_Word*)( data + current + sizeof( namesz ) ) );
|
||||
int align = sizeof(Elf_Word);
|
||||
while (current + 3 * align <= size) {
|
||||
note_start_positions.push_back(current);
|
||||
Elf_Word namesz = convertor(*(Elf_Word*)(data + current));
|
||||
Elf_Word descsz = convertor(*(Elf_Word*)(data + current + sizeof(namesz)));
|
||||
|
||||
current += 3*sizeof( Elf_Word ) +
|
||||
( ( namesz + align - 1 ) / align ) * align +
|
||||
( ( descsz + align - 1 ) / align ) * align;
|
||||
current += 3 * sizeof(Elf_Word) + ((namesz + align - 1) / align) * align +
|
||||
((descsz + align - 1) / align) * align;
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
const elfio& elf_file;
|
||||
section* note_section;
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
const elfio& elf_file;
|
||||
section* note_section;
|
||||
std::vector<Elf_Xword> note_start_positions;
|
||||
};
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_NOTE_HPP
|
||||
#endif // ELFIO_NOTE_HPP
|
||||
+166
-255
@@ -25,345 +25,256 @@ THE SOFTWARE.
|
||||
|
||||
namespace ELFIO {
|
||||
|
||||
template<typename T> struct get_sym_and_type;
|
||||
template<> struct get_sym_and_type< Elf32_Rel >
|
||||
{
|
||||
static int get_r_sym( Elf_Xword info )
|
||||
{
|
||||
return ELF32_R_SYM( (Elf_Word)info );
|
||||
}
|
||||
static int get_r_type( Elf_Xword info )
|
||||
{
|
||||
return ELF32_R_TYPE( (Elf_Word)info );
|
||||
}
|
||||
template <typename T>
|
||||
struct get_sym_and_type;
|
||||
template <>
|
||||
struct get_sym_and_type<Elf32_Rel> {
|
||||
static int get_r_sym(Elf_Xword info) { return ELF32_R_SYM((Elf_Word)info); }
|
||||
static int get_r_type(Elf_Xword info) { return ELF32_R_TYPE((Elf_Word)info); }
|
||||
};
|
||||
template<> struct get_sym_and_type< Elf32_Rela >
|
||||
{
|
||||
static int get_r_sym( Elf_Xword info )
|
||||
{
|
||||
return ELF32_R_SYM( (Elf_Word)info );
|
||||
}
|
||||
static int get_r_type( Elf_Xword info )
|
||||
{
|
||||
return ELF32_R_TYPE( (Elf_Word)info );
|
||||
}
|
||||
template <>
|
||||
struct get_sym_and_type<Elf32_Rela> {
|
||||
static int get_r_sym(Elf_Xword info) { return ELF32_R_SYM((Elf_Word)info); }
|
||||
static int get_r_type(Elf_Xword info) { return ELF32_R_TYPE((Elf_Word)info); }
|
||||
};
|
||||
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 );
|
||||
}
|
||||
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); }
|
||||
};
|
||||
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 );
|
||||
}
|
||||
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); }
|
||||
};
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class relocation_section_accessor
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
relocation_section_accessor( const elfio& elf_file_, section* section_ ) :
|
||||
elf_file( elf_file_ ),
|
||||
relocation_section( section_ )
|
||||
{
|
||||
}
|
||||
class relocation_section_accessor {
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
relocation_section_accessor(const elfio& elf_file_, section* 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() ) {
|
||||
if (0 != relocation_section->get_entry_size()) {
|
||||
nRet = relocation_section->get_size() / relocation_section->get_entry_size();
|
||||
}
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_entry( Elf_Xword index,
|
||||
Elf64_Addr& offset,
|
||||
Elf_Word& symbol,
|
||||
Elf_Word& type,
|
||||
Elf_Sxword& addend ) const
|
||||
{
|
||||
if ( index >= get_entries_num() ) { // Is index valid
|
||||
//------------------------------------------------------------------------------
|
||||
bool get_entry(Elf_Xword index, Elf64_Addr& offset, Elf_Word& symbol, Elf_Word& type,
|
||||
Elf_Sxword& addend) const {
|
||||
if (index >= get_entries_num()) { // Is index valid
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
if ( SHT_REL == relocation_section->get_type() ) {
|
||||
generic_get_entry_rel< Elf32_Rel >( index, offset, symbol,
|
||||
type, addend );
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
if (SHT_REL == relocation_section->get_type()) {
|
||||
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);
|
||||
}
|
||||
else if ( SHT_RELA == relocation_section->get_type() ) {
|
||||
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 );
|
||||
}
|
||||
else if ( SHT_RELA == relocation_section->get_type() ) {
|
||||
generic_get_entry_rela< Elf64_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);
|
||||
} else if (SHT_RELA == relocation_section->get_type()) {
|
||||
generic_get_entry_rela<Elf64_Rela>(index, offset, symbol, type, addend);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_entry( Elf_Xword index,
|
||||
Elf64_Addr& offset,
|
||||
Elf64_Addr& symbolValue,
|
||||
std::string& symbolName,
|
||||
Elf_Word& type,
|
||||
Elf_Sxword& addend,
|
||||
Elf_Sxword& calcValue ) const
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
bool get_entry(Elf_Xword index, Elf64_Addr& offset, Elf64_Addr& symbolValue,
|
||||
std::string& symbolName, Elf_Word& type, Elf_Sxword& addend,
|
||||
Elf_Sxword& calcValue) const {
|
||||
// Do regular job
|
||||
Elf_Word symbol;
|
||||
bool ret = get_entry( index, offset, symbol, type, addend );
|
||||
bool ret = get_entry(index, offset, symbol, type, addend);
|
||||
|
||||
// Find the symbol
|
||||
Elf_Xword size;
|
||||
Elf_Xword size;
|
||||
unsigned char bind;
|
||||
unsigned char symbolType;
|
||||
Elf_Half section;
|
||||
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 ) {
|
||||
case R_386_NONE: // none
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_32: // S + A
|
||||
calcValue = symbolValue + addend;
|
||||
break;
|
||||
case R_386_PC32: // S + A - P
|
||||
calcValue = symbolValue + addend - offset;
|
||||
break;
|
||||
case R_386_GOT32: // G + A - P
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_PLT32: // L + A - P
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_COPY: // none
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_GLOB_DAT: // S
|
||||
case R_386_JMP_SLOT: // S
|
||||
calcValue = symbolValue;
|
||||
break;
|
||||
case R_386_RELATIVE: // B + A
|
||||
calcValue = addend;
|
||||
break;
|
||||
case R_386_GOTOFF: // S + A - GOT
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_GOTPC: // GOT + A - P
|
||||
calcValue = 0;
|
||||
break;
|
||||
default: // Not recognized symbol!
|
||||
calcValue = 0;
|
||||
break;
|
||||
if (ret) { // Was it successful?
|
||||
switch (type) {
|
||||
case R_386_NONE: // none
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_32: // S + A
|
||||
calcValue = symbolValue + addend;
|
||||
break;
|
||||
case R_386_PC32: // S + A - P
|
||||
calcValue = symbolValue + addend - offset;
|
||||
break;
|
||||
case R_386_GOT32: // G + A - P
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_PLT32: // L + A - P
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_COPY: // none
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_GLOB_DAT: // S
|
||||
case R_386_JMP_SLOT: // S
|
||||
calcValue = symbolValue;
|
||||
break;
|
||||
case R_386_RELATIVE: // B + A
|
||||
calcValue = addend;
|
||||
break;
|
||||
case R_386_GOTOFF: // S + A - GOT
|
||||
calcValue = 0;
|
||||
break;
|
||||
case R_386_GOTPC: // GOT + A - P
|
||||
calcValue = 0;
|
||||
break;
|
||||
default: // Not recognized symbol!
|
||||
calcValue = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf64_Addr offset, Elf_Xword info )
|
||||
{
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
generic_add_entry< Elf32_Rel >( offset, info );
|
||||
}
|
||||
else {
|
||||
generic_add_entry< Elf64_Rel >( offset, info );
|
||||
//------------------------------------------------------------------------------
|
||||
void add_entry(Elf64_Addr offset, Elf_Xword info) {
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
generic_add_entry<Elf32_Rel>(offset, info);
|
||||
} else {
|
||||
generic_add_entry<Elf64_Rel>(offset, info);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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 ) {
|
||||
info = ELF32_R_INFO( (Elf_Xword)symbol, type );
|
||||
}
|
||||
else {
|
||||
info = ELF64_R_INFO((Elf_Xword)symbol, type );
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
info = ELF32_R_INFO((Elf_Xword)symbol, type);
|
||||
} else {
|
||||
info = ELF64_R_INFO((Elf_Xword)symbol, type);
|
||||
}
|
||||
|
||||
add_entry( offset, info );
|
||||
add_entry(offset, info);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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 );
|
||||
}
|
||||
else {
|
||||
generic_add_entry< Elf64_Rela >( offset, info, 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);
|
||||
} else {
|
||||
generic_add_entry<Elf64_Rela>(offset, info, addend);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( Elf64_Addr offset, Elf_Word symbol, unsigned char type,
|
||||
Elf_Sxword addend )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
void add_entry(Elf64_Addr offset, Elf_Word symbol, unsigned char type, Elf_Sxword addend) {
|
||||
Elf_Xword info;
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
info = ELF32_R_INFO( (Elf_Xword)symbol, type );
|
||||
}
|
||||
else {
|
||||
info = ELF64_R_INFO( (Elf_Xword)symbol, type );
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
info = ELF32_R_INFO((Elf_Xword)symbol, type);
|
||||
} else {
|
||||
info = ELF64_R_INFO((Elf_Xword)symbol, type);
|
||||
}
|
||||
|
||||
add_entry( offset, info, addend );
|
||||
add_entry(offset, info, addend);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
add_entry( string_section_accessor str_writer,
|
||||
const char* str,
|
||||
symbol_section_accessor sym_writer,
|
||||
Elf64_Addr value,
|
||||
Elf_Word size,
|
||||
unsigned char sym_info,
|
||||
unsigned char other,
|
||||
Elf_Half shndx,
|
||||
Elf64_Addr offset,
|
||||
unsigned char type )
|
||||
{
|
||||
Elf_Word str_index = str_writer.add_string( str );
|
||||
Elf_Word sym_index = sym_writer.add_symbol( str_index, value, size,
|
||||
sym_info, other, shndx );
|
||||
add_entry( offset, sym_index, type );
|
||||
//------------------------------------------------------------------------------
|
||||
void add_entry(string_section_accessor str_writer, const char* str,
|
||||
symbol_section_accessor sym_writer, Elf64_Addr value, Elf_Word size,
|
||||
unsigned char sym_info, unsigned char other, Elf_Half shndx, Elf64_Addr offset,
|
||||
unsigned char type) {
|
||||
Elf_Word str_index = str_writer.add_string(str);
|
||||
Elf_Word sym_index = sym_writer.add_symbol(str_index, value, size, sym_info, other, shndx);
|
||||
add_entry(offset, sym_index, type);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_symbol_table_index() const
|
||||
{
|
||||
return (Elf_Half)relocation_section->get_link();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
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,
|
||||
Elf64_Addr& offset,
|
||||
Elf_Word& symbol,
|
||||
Elf_Word& type,
|
||||
Elf_Sxword& addend ) const
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void generic_get_entry_rel(Elf_Xword index, Elf64_Addr& offset, Elf_Word& symbol,
|
||||
Elf_Word& type, Elf_Sxword& addend) const {
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
const T* pEntry = reinterpret_cast<const T*>(
|
||||
relocation_section->get_data() +
|
||||
index * relocation_section->get_entry_size() );
|
||||
offset = convertor( pEntry->r_offset );
|
||||
Elf_Xword tmp = convertor( pEntry->r_info );
|
||||
symbol = get_sym_and_type<T>::get_r_sym( tmp );
|
||||
type = get_sym_and_type<T>::get_r_type( tmp );
|
||||
addend = 0;
|
||||
const T* pEntry = reinterpret_cast<const T*>(relocation_section->get_data() +
|
||||
index * relocation_section->get_entry_size());
|
||||
offset = convertor(pEntry->r_offset);
|
||||
Elf_Xword tmp = convertor(pEntry->r_info);
|
||||
symbol = get_sym_and_type<T>::get_r_sym(tmp);
|
||||
type = get_sym_and_type<T>::get_r_type(tmp);
|
||||
addend = 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class T >
|
||||
void
|
||||
generic_get_entry_rela( Elf_Xword index,
|
||||
Elf64_Addr& offset,
|
||||
Elf_Word& symbol,
|
||||
Elf_Word& type,
|
||||
Elf_Sxword& addend ) const
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void generic_get_entry_rela(Elf_Xword index, Elf64_Addr& offset, Elf_Word& symbol,
|
||||
Elf_Word& type, Elf_Sxword& addend) const {
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
const T* pEntry = reinterpret_cast<const T*>(
|
||||
relocation_section->get_data() +
|
||||
index * relocation_section->get_entry_size() );
|
||||
offset = convertor( pEntry->r_offset );
|
||||
Elf_Xword tmp = convertor( pEntry->r_info );
|
||||
symbol = get_sym_and_type<T>::get_r_sym( tmp );
|
||||
type = get_sym_and_type<T>::get_r_type( tmp );
|
||||
addend = convertor( pEntry->r_addend );
|
||||
const T* pEntry = reinterpret_cast<const T*>(relocation_section->get_data() +
|
||||
index * relocation_section->get_entry_size());
|
||||
offset = convertor(pEntry->r_offset);
|
||||
Elf_Xword tmp = convertor(pEntry->r_info);
|
||||
symbol = get_sym_and_type<T>::get_r_sym(tmp);
|
||||
type = get_sym_and_type<T>::get_r_type(tmp);
|
||||
addend = convertor(pEntry->r_addend);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class T >
|
||||
void
|
||||
generic_add_entry( Elf64_Addr offset, Elf_Xword info )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void generic_add_entry(Elf64_Addr offset, Elf_Xword info) {
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
T entry;
|
||||
entry.r_offset = offset;
|
||||
entry.r_info = info;
|
||||
entry.r_offset = convertor( entry.r_offset );
|
||||
entry.r_info = convertor( entry.r_info );
|
||||
entry.r_info = info;
|
||||
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));
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class T >
|
||||
void
|
||||
generic_add_entry( Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
void generic_add_entry(Elf64_Addr offset, Elf_Xword info, Elf_Sxword addend) {
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
T entry;
|
||||
entry.r_offset = offset;
|
||||
entry.r_info = info;
|
||||
entry.r_info = info;
|
||||
entry.r_addend = addend;
|
||||
entry.r_offset = convertor( entry.r_offset );
|
||||
entry.r_info = convertor( entry.r_info );
|
||||
entry.r_addend = convertor( entry.r_addend );
|
||||
entry.r_offset = convertor(entry.r_offset);
|
||||
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));
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
const elfio& elf_file;
|
||||
section* relocation_section;
|
||||
section* relocation_section;
|
||||
};
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_RELOCATION_HPP
|
||||
#endif // ELFIO_RELOCATION_HPP
|
||||
+132
-188
@@ -28,269 +28,213 @@ THE SOFTWARE.
|
||||
|
||||
namespace ELFIO {
|
||||
|
||||
class section
|
||||
{
|
||||
class section {
|
||||
friend class elfio;
|
||||
public:
|
||||
virtual ~section() {};
|
||||
|
||||
ELFIO_GET_ACCESS_DECL ( Elf_Half, index );
|
||||
ELFIO_GET_SET_ACCESS_DECL( std::string, name );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, flags );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, info );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, link );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, addr_align );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, entry_size );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, address );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, size );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, name_string_offset );
|
||||
public:
|
||||
virtual ~section(){};
|
||||
|
||||
virtual const char* get_data() const = 0;
|
||||
virtual void set_data( const char* pData, Elf_Word size ) = 0;
|
||||
virtual void set_data( const std::string& data ) = 0;
|
||||
virtual void append_data( const char* pData, Elf_Word size ) = 0;
|
||||
virtual void append_data( const std::string& data ) = 0;
|
||||
ELFIO_GET_ACCESS_DECL(Elf_Half, index);
|
||||
ELFIO_GET_SET_ACCESS_DECL(std::string, name);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Word, type);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, flags);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Word, info);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Word, link);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, addr_align);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, entry_size);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf64_Addr, address);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, size);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Word, name_string_offset);
|
||||
|
||||
protected:
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf64_Off, offset );
|
||||
ELFIO_SET_ACCESS_DECL( Elf_Half, index );
|
||||
|
||||
virtual void load( std::istream& f,
|
||||
std::streampos header_offset ) = 0;
|
||||
virtual void save( std::ostream& f,
|
||||
std::streampos header_offset,
|
||||
std::streampos data_offset ) = 0;
|
||||
virtual bool is_address_initialized() const = 0;
|
||||
virtual const char* get_data() const = 0;
|
||||
virtual void set_data(const char* pData, Elf_Word size) = 0;
|
||||
virtual void set_data(const std::string& data) = 0;
|
||||
virtual void append_data(const char* pData, Elf_Word size) = 0;
|
||||
virtual void append_data(const std::string& data) = 0;
|
||||
|
||||
protected:
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf64_Off, offset);
|
||||
ELFIO_SET_ACCESS_DECL(Elf_Half, index);
|
||||
|
||||
virtual void load(std::istream& f, std::streampos header_offset) = 0;
|
||||
virtual void save(std::ostream& f, std::streampos header_offset,
|
||||
std::streampos data_offset) = 0;
|
||||
virtual bool is_address_initialized() const = 0;
|
||||
};
|
||||
|
||||
|
||||
template< class T >
|
||||
class section_impl : public section
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
section_impl( const endianess_convertor* convertor_ ) : convertor( convertor_ )
|
||||
{
|
||||
std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), '\0' );
|
||||
template <class T>
|
||||
class section_impl : public section {
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
section_impl(const endianess_convertor* convertor_) : convertor(convertor_) {
|
||||
std::fill_n(reinterpret_cast<char*>(&header), sizeof(header), '\0');
|
||||
is_address_set = false;
|
||||
data = 0;
|
||||
data_size = 0;
|
||||
data = 0;
|
||||
data_size = 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
~section_impl()
|
||||
{
|
||||
delete [] data;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
~section_impl() { delete[] data; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
// Section info functions
|
||||
ELFIO_GET_SET_ACCESS( Elf_Word, type, header.sh_type );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Xword, flags, header.sh_flags );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Xword, size, header.sh_size );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Word, link, header.sh_link );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Word, info, header.sh_info );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Xword, addr_align, header.sh_addralign );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Xword, entry_size, header.sh_entsize );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Word, name_string_offset, header.sh_name );
|
||||
ELFIO_GET_ACCESS ( Elf64_Addr, address, header.sh_addr );
|
||||
ELFIO_GET_SET_ACCESS(Elf_Word, type, header.sh_type);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Xword, flags, header.sh_flags);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Xword, size, header.sh_size);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Word, link, header.sh_link);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Word, info, header.sh_info);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Xword, addr_align, header.sh_addralign);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Xword, entry_size, header.sh_entsize);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Word, name_string_offset, header.sh_name);
|
||||
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 );
|
||||
header.sh_addr = (*convertor)(header.sh_addr);
|
||||
is_address_set = true;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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 )
|
||||
{
|
||||
if ( get_type() != SHT_NOBITS ) {
|
||||
delete [] data;
|
||||
//------------------------------------------------------------------------------
|
||||
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&) {
|
||||
data = 0;
|
||||
data = 0;
|
||||
data_size = 0;
|
||||
size = 0;
|
||||
size = 0;
|
||||
}
|
||||
if ( 0 != data && 0 != raw_data ) {
|
||||
if (0 != data && 0 != raw_data) {
|
||||
data_size = size;
|
||||
std::copy( raw_data, raw_data + size, data );
|
||||
std::copy(raw_data, raw_data + size, data);
|
||||
}
|
||||
}
|
||||
|
||||
set_size( size );
|
||||
set_size(size);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_data( const std::string& str_data )
|
||||
{
|
||||
return set_data( str_data.c_str(), (Elf_Word)str_data.size() );
|
||||
//------------------------------------------------------------------------------
|
||||
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 )
|
||||
{
|
||||
if ( get_type() != SHT_NOBITS ) {
|
||||
if ( get_size() + size < data_size ) {
|
||||
std::copy( raw_data, raw_data + size, data + get_size() );
|
||||
}
|
||||
else {
|
||||
data_size = 2*( data_size + size);
|
||||
//------------------------------------------------------------------------------
|
||||
void append_data(const char* raw_data, Elf_Word size) {
|
||||
if (get_type() != SHT_NOBITS) {
|
||||
if (get_size() + size < data_size) {
|
||||
std::copy(raw_data, raw_data + size, data + get_size());
|
||||
} else {
|
||||
data_size = 2 * (data_size + size);
|
||||
char* new_data;
|
||||
try {
|
||||
new_data = new char[data_size];
|
||||
} catch (const std::bad_alloc&) {
|
||||
new_data = 0;
|
||||
size = 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() );
|
||||
delete [] data;
|
||||
if (0 != new_data) {
|
||||
std::copy(data, data + get_size(), new_data);
|
||||
std::copy(raw_data, raw_data + size, new_data + get_size());
|
||||
delete[] data;
|
||||
data = new_data;
|
||||
}
|
||||
}
|
||||
set_size( get_size() + size );
|
||||
set_size(get_size() + size);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
append_data( const std::string& str_data )
|
||||
{
|
||||
return append_data( str_data.c_str(), (Elf_Word)str_data.size() );
|
||||
//------------------------------------------------------------------------------
|
||||
void append_data(const std::string& str_data) {
|
||||
return append_data(str_data.c_str(), (Elf_Word)str_data.size());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
protected:
|
||||
//------------------------------------------------------------------------------
|
||||
ELFIO_GET_SET_ACCESS( Elf64_Off, offset, header.sh_offset );
|
||||
//------------------------------------------------------------------------------
|
||||
protected:
|
||||
//------------------------------------------------------------------------------
|
||||
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 )
|
||||
{
|
||||
std::fill_n( reinterpret_cast<char*>( &header ), sizeof( header ), '\0' );
|
||||
stream.seekg( header_offset );
|
||||
stream.read( reinterpret_cast<char*>( &header ), sizeof( header ) );
|
||||
//------------------------------------------------------------------------------
|
||||
void load(std::istream& stream, std::streampos header_offset) {
|
||||
std::fill_n(reinterpret_cast<char*>(&header), sizeof(header), '\0');
|
||||
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() ) {
|
||||
if (0 == data && SHT_NULL != get_type() && SHT_NOBITS != get_type()) {
|
||||
try {
|
||||
data = new char[size];
|
||||
} catch (const std::bad_alloc&) {
|
||||
data = 0;
|
||||
data = 0;
|
||||
data_size = 0;
|
||||
}
|
||||
if ( 0 != size ) {
|
||||
stream.seekg( (*convertor)( header.sh_offset ) );
|
||||
stream.read( data, size );
|
||||
if (0 != size) {
|
||||
stream.seekg((*convertor)(header.sh_offset));
|
||||
stream.read(data, size);
|
||||
data_size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
save( std::ostream& f,
|
||||
std::streampos header_offset,
|
||||
std::streampos data_offset )
|
||||
{
|
||||
if ( 0 != get_index() ) {
|
||||
//------------------------------------------------------------------------------
|
||||
void save(std::ostream& f, std::streampos header_offset, std::streampos data_offset) {
|
||||
if (0 != get_index()) {
|
||||
header.sh_offset = data_offset;
|
||||
header.sh_offset = (*convertor)( header.sh_offset );
|
||||
header.sh_offset = (*convertor)(header.sh_offset);
|
||||
}
|
||||
|
||||
save_header( f, header_offset );
|
||||
if ( get_type() != SHT_NOBITS && get_type() != SHT_NULL &&
|
||||
get_size() != 0 && data != 0 ) {
|
||||
save_data( f, data_offset );
|
||||
save_header(f, header_offset);
|
||||
if (get_type() != SHT_NOBITS && get_type() != SHT_NULL && get_size() != 0 && data != 0) {
|
||||
save_data(f, data_offset);
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
save_header( std::ostream& f,
|
||||
std::streampos header_offset ) const
|
||||
{
|
||||
f.seekp( header_offset );
|
||||
f.write( reinterpret_cast<const char*>( &header ), sizeof( header ) );
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
void save_header(std::ostream& f, std::streampos header_offset) const {
|
||||
f.seekp(header_offset);
|
||||
f.write(reinterpret_cast<const char*>(&header), sizeof(header));
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
save_data( std::ostream& f,
|
||||
std::streampos data_offset ) const
|
||||
{
|
||||
f.seekp( data_offset );
|
||||
f.write( get_data(), get_size() );
|
||||
//------------------------------------------------------------------------------
|
||||
void save_data(std::ostream& f, std::streampos data_offset) const {
|
||||
f.seekp(data_offset);
|
||||
f.write(get_data(), get_size());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
T header;
|
||||
Elf_Half index;
|
||||
std::string name;
|
||||
char* data;
|
||||
Elf_Word data_size;
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
T header;
|
||||
Elf_Half index;
|
||||
std::string name;
|
||||
char* data;
|
||||
Elf_Word data_size;
|
||||
const endianess_convertor* convertor;
|
||||
bool is_address_set;
|
||||
bool is_address_set;
|
||||
};
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_SECTION_HPP
|
||||
#endif // ELFIO_SECTION_HPP
|
||||
+90
-132
@@ -28,193 +28,151 @@ THE SOFTWARE.
|
||||
|
||||
namespace ELFIO {
|
||||
|
||||
class segment
|
||||
{
|
||||
class segment {
|
||||
friend class elfio;
|
||||
public:
|
||||
virtual ~segment() {};
|
||||
|
||||
ELFIO_GET_ACCESS_DECL ( Elf_Half, index );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, type );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Word, flags );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, align );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, virtual_address );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf64_Addr, physical_address );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, file_size );
|
||||
ELFIO_GET_SET_ACCESS_DECL( Elf_Xword, memory_size );
|
||||
ELFIO_GET_ACCESS_DECL( Elf64_Off, offset );
|
||||
public:
|
||||
virtual ~segment(){};
|
||||
|
||||
ELFIO_GET_ACCESS_DECL(Elf_Half, index);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Word, type);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Word, flags);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, align);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf64_Addr, virtual_address);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf64_Addr, physical_address);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, file_size);
|
||||
ELFIO_GET_SET_ACCESS_DECL(Elf_Xword, memory_size);
|
||||
ELFIO_GET_ACCESS_DECL(Elf64_Off, offset);
|
||||
|
||||
virtual const char* get_data() const = 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;
|
||||
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;
|
||||
|
||||
protected:
|
||||
ELFIO_SET_ACCESS_DECL( Elf64_Off, offset );
|
||||
ELFIO_SET_ACCESS_DECL( Elf_Half, index );
|
||||
|
||||
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& f, std::streampos header_offset,
|
||||
std::streampos data_offset ) = 0;
|
||||
protected:
|
||||
ELFIO_SET_ACCESS_DECL(Elf64_Off, offset);
|
||||
ELFIO_SET_ACCESS_DECL(Elf_Half, index);
|
||||
|
||||
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& f, std::streampos header_offset,
|
||||
std::streampos data_offset) = 0;
|
||||
};
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class T >
|
||||
class segment_impl : public segment
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
segment_impl( endianess_convertor* convertor_ ) :
|
||||
convertor( convertor_ )
|
||||
{
|
||||
template <class T>
|
||||
class segment_impl : public segment {
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
segment_impl(endianess_convertor* convertor_) : convertor(convertor_) {
|
||||
is_offset_set = false;
|
||||
std::fill_n( reinterpret_cast<char*>( &ph ), sizeof( ph ), '\0' );
|
||||
std::fill_n(reinterpret_cast<char*>(&ph), sizeof(ph), '\0');
|
||||
data = 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
virtual ~segment_impl()
|
||||
{
|
||||
delete [] data;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
virtual ~segment_impl() { delete[] data; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
// Section info functions
|
||||
ELFIO_GET_SET_ACCESS( Elf_Word, type, ph.p_type );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Word, flags, ph.p_flags );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Xword, align, ph.p_align );
|
||||
ELFIO_GET_SET_ACCESS( Elf64_Addr, virtual_address, ph.p_vaddr );
|
||||
ELFIO_GET_SET_ACCESS( Elf64_Addr, physical_address, ph.p_paddr );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Xword, file_size, ph.p_filesz );
|
||||
ELFIO_GET_SET_ACCESS( Elf_Xword, memory_size, ph.p_memsz );
|
||||
ELFIO_GET_ACCESS( Elf64_Off, offset, ph.p_offset );
|
||||
ELFIO_GET_SET_ACCESS(Elf_Word, type, ph.p_type);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Word, flags, ph.p_flags);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Xword, align, ph.p_align);
|
||||
ELFIO_GET_SET_ACCESS(Elf64_Addr, virtual_address, ph.p_vaddr);
|
||||
ELFIO_GET_SET_ACCESS(Elf64_Addr, physical_address, ph.p_paddr);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Xword, file_size, ph.p_filesz);
|
||||
ELFIO_GET_SET_ACCESS(Elf_Xword, memory_size, ph.p_memsz);
|
||||
ELFIO_GET_ACCESS(Elf64_Off, offset, ph.p_offset);
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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 )
|
||||
{
|
||||
sections.push_back( sec_index );
|
||||
if ( addr_align > get_align() ) {
|
||||
set_align( 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()) {
|
||||
set_align(addr_align);
|
||||
}
|
||||
|
||||
return (Elf_Half)sections.size();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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
|
||||
{
|
||||
if ( num < sections.size() ) {
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half get_section_index_at(Elf_Half num) const {
|
||||
if (num < sections.size()) {
|
||||
return sections[num];
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
protected:
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
protected:
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_offset( Elf64_Off value )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
void set_offset(Elf64_Off value) {
|
||||
ph.p_offset = value;
|
||||
ph.p_offset = (*convertor)( ph.p_offset );
|
||||
ph.p_offset = (*convertor)(ph.p_offset);
|
||||
is_offset_set = true;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
set_index( Elf_Half value )
|
||||
{
|
||||
index = value;
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
const std::vector<Elf_Half>& get_sections() const { return sections; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
load( std::istream& stream,
|
||||
std::streampos header_offset )
|
||||
{
|
||||
stream.seekg( header_offset );
|
||||
stream.read( reinterpret_cast<char*>( &ph ), sizeof( ph ) );
|
||||
//------------------------------------------------------------------------------
|
||||
void set_index(Elf_Half value) { index = value; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void load(std::istream& stream, std::streampos header_offset) {
|
||||
stream.seekg(header_offset);
|
||||
stream.read(reinterpret_cast<char*>(&ph), sizeof(ph));
|
||||
is_offset_set = true;
|
||||
|
||||
if ( PT_NULL != get_type() && 0 != get_file_size() ) {
|
||||
stream.seekg( (*convertor)( ph.p_offset ) );
|
||||
if (PT_NULL != get_type() && 0 != get_file_size()) {
|
||||
stream.seekg((*convertor)(ph.p_offset));
|
||||
Elf_Xword size = get_file_size();
|
||||
try {
|
||||
data = new char[size];
|
||||
} catch (const std::bad_alloc&) {
|
||||
data = 0;
|
||||
}
|
||||
if ( 0 != data ) {
|
||||
stream.read( data, size );
|
||||
if (0 != data) {
|
||||
stream.read(data, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void save( std::ostream& f,
|
||||
std::streampos header_offset,
|
||||
std::streampos data_offset )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
void save(std::ostream& f, std::streampos header_offset, std::streampos data_offset) {
|
||||
ph.p_offset = data_offset;
|
||||
ph.p_offset = (*convertor)(ph.p_offset);
|
||||
f.seekp( header_offset );
|
||||
f.write( reinterpret_cast<const char*>( &ph ), sizeof( ph ) );
|
||||
f.seekp(header_offset);
|
||||
f.write(reinterpret_cast<const char*>(&ph), sizeof(ph));
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
T ph;
|
||||
Elf_Half index;
|
||||
char* data;
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
T ph;
|
||||
Elf_Half index;
|
||||
char* data;
|
||||
std::vector<Elf_Half> sections;
|
||||
endianess_convertor* convertor;
|
||||
bool is_offset_set;
|
||||
endianess_convertor* convertor;
|
||||
bool is_offset_set;
|
||||
};
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_SEGMENT_HPP
|
||||
#endif // ELFIO_SEGMENT_HPP
|
||||
+21
-33
@@ -30,24 +30,18 @@ THE SOFTWARE.
|
||||
namespace ELFIO {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class string_section_accessor
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
string_section_accessor( section* section_ ) :
|
||||
string_section( section_ )
|
||||
{
|
||||
}
|
||||
class string_section_accessor {
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
string_section_accessor(section* section_) : string_section(section_) {}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
const char*
|
||||
get_string( Elf_Word index ) const
|
||||
{
|
||||
if ( string_section ) {
|
||||
if ( index < string_section->get_size() ) {
|
||||
//------------------------------------------------------------------------------
|
||||
const char* get_string(Elf_Word index) const {
|
||||
if (string_section) {
|
||||
if (index < string_section->get_size()) {
|
||||
const char* data = string_section->get_data();
|
||||
if ( 0 != data ) {
|
||||
if (0 != data) {
|
||||
return data + index;
|
||||
}
|
||||
}
|
||||
@@ -57,40 +51,34 @@ class string_section_accessor
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
add_string( const char* str )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word add_string(const char* str) {
|
||||
Elf_Word current_position = 0;
|
||||
|
||||
|
||||
if (string_section) {
|
||||
// Strings are addeded to the end of the current section data
|
||||
current_position = (Elf_Word)string_section->get_size();
|
||||
|
||||
if ( current_position == 0 ) {
|
||||
if (current_position == 0) {
|
||||
char empty_string = '\0';
|
||||
string_section->append_data( &empty_string, 1 );
|
||||
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 )
|
||||
{
|
||||
return add_string( str.c_str() );
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word add_string(const std::string& str) { return add_string(str.c_str()); }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
section* string_section;
|
||||
};
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_STRINGS_HPP
|
||||
#endif // ELFIO_STRINGS_HPP
|
||||
+108
-166
@@ -26,82 +26,61 @@ THE SOFTWARE.
|
||||
namespace ELFIO {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class symbol_section_accessor
|
||||
{
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
symbol_section_accessor( const elfio& elf_file_, section* symbol_section_ ) :
|
||||
elf_file( elf_file_ ),
|
||||
symbol_section( symbol_section_ )
|
||||
{
|
||||
class symbol_section_accessor {
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
symbol_section_accessor(const elfio& elf_file_, section* 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() ) {
|
||||
if (0 != symbol_section->get_entry_size()) {
|
||||
nRet = symbol_section->get_size() / symbol_section->get_entry_size();
|
||||
}
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_symbol( Elf_Xword index,
|
||||
std::string& name,
|
||||
Elf64_Addr& value,
|
||||
Elf_Xword& size,
|
||||
unsigned char& bind,
|
||||
unsigned char& type,
|
||||
Elf_Half& section_index,
|
||||
unsigned char& other ) const
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
bool get_symbol(Elf_Xword index, std::string& name, Elf64_Addr& value, Elf_Xword& size,
|
||||
unsigned char& bind, unsigned char& type, Elf_Half& section_index,
|
||||
unsigned char& other) const {
|
||||
bool ret = false;
|
||||
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
ret = generic_get_symbol<Elf32_Sym>( index, name, value, size, bind,
|
||||
type, section_index, other );
|
||||
}
|
||||
else {
|
||||
ret = generic_get_symbol<Elf64_Sym>( index, name, value, size, bind,
|
||||
type, section_index, other );
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
ret = generic_get_symbol<Elf32_Sym>(index, name, value, size, bind, type, section_index,
|
||||
other);
|
||||
} else {
|
||||
ret = generic_get_symbol<Elf64_Sym>(index, name, value, size, bind, type, section_index,
|
||||
other);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
bool
|
||||
get_symbol( const std::string& name,
|
||||
Elf64_Addr& value,
|
||||
Elf_Xword& size,
|
||||
unsigned char& bind,
|
||||
unsigned char& type,
|
||||
Elf_Half& section_index,
|
||||
unsigned char& other ) const
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
bool get_symbol(const std::string& name, Elf64_Addr& value, Elf_Xword& size,
|
||||
unsigned char& bind, unsigned char& type, Elf_Half& section_index,
|
||||
unsigned char& other) const {
|
||||
bool ret = false;
|
||||
|
||||
if ( 0 != get_hash_table_index() ) {
|
||||
if (0 != get_hash_table_index()) {
|
||||
Elf_Word nbucket = *(Elf_Word*)hash_section->get_data();
|
||||
Elf_Word nchain = *(Elf_Word*)( hash_section->get_data() +
|
||||
sizeof( Elf_Word ) );
|
||||
Elf_Word val = elf_hash( (const unsigned char*)name.c_str() );
|
||||
Elf_Word nchain = *(Elf_Word*)(hash_section->get_data() + sizeof(Elf_Word));
|
||||
Elf_Word val = elf_hash((const unsigned char*)name.c_str());
|
||||
|
||||
Elf_Word y = *(Elf_Word*)( hash_section->get_data() +
|
||||
( 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 = *(Elf_Word*)( hash_section->get_data() +
|
||||
( 2 + nbucket + y ) * sizeof( Elf_Word ) );
|
||||
get_symbol( y, str, value, size, bind, type, section_index, other );
|
||||
Elf_Word y =
|
||||
*(Elf_Word*)(hash_section->get_data() + (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 = *(Elf_Word*)(hash_section->get_data() + (2 + nbucket + y) * sizeof(Elf_Word));
|
||||
get_symbol(y, str, value, size, bind, type, section_index, other);
|
||||
}
|
||||
if ( str == name ) {
|
||||
if (str == name) {
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
@@ -109,128 +88,95 @@ class symbol_section_accessor
|
||||
return ret;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Word
|
||||
add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size,
|
||||
unsigned char info, unsigned char other,
|
||||
Elf_Half shndx )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
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;
|
||||
|
||||
if ( symbol_section->get_size() == 0 ) {
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
nRet = generic_add_symbol<Elf32_Sym>( 0, 0, 0, 0, 0, 0 );
|
||||
}
|
||||
else {
|
||||
nRet = generic_add_symbol<Elf64_Sym>( 0, 0, 0, 0, 0, 0 );
|
||||
if (symbol_section->get_size() == 0) {
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
nRet = generic_add_symbol<Elf32_Sym>(0, 0, 0, 0, 0, 0);
|
||||
} else {
|
||||
nRet = generic_add_symbol<Elf64_Sym>(0, 0, 0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if ( elf_file.get_class() == ELFCLASS32 ) {
|
||||
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 );
|
||||
if (elf_file.get_class() == ELFCLASS32) {
|
||||
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);
|
||||
}
|
||||
|
||||
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_Half shndx )
|
||||
{
|
||||
return add_symbol( name, value, size, ELF_ST_INFO( bind, type ), other, shndx );
|
||||
//------------------------------------------------------------------------------
|
||||
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);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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 );
|
||||
return add_symbol( index, value, size, info, 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_Half shndx) {
|
||||
Elf_Word index = pStrWriter.add_string(str);
|
||||
return add_symbol(index, value, size, info, other, shndx);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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 );
|
||||
//------------------------------------------------------------------------------
|
||||
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);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
find_hash_section()
|
||||
{
|
||||
hash_section = 0;
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
void find_hash_section() {
|
||||
hash_section = 0;
|
||||
hash_section_index = 0;
|
||||
Elf_Half nSecNo = elf_file.sections.size();
|
||||
for ( Elf_Half i = 0; i < nSecNo && 0 == hash_section_index; ++i ) {
|
||||
for (Elf_Half i = 0; i < nSecNo && 0 == hash_section_index; ++i) {
|
||||
const section* sec = elf_file.sections[i];
|
||||
if ( sec->get_link() == symbol_section->get_index() ) {
|
||||
hash_section = sec;
|
||||
if (sec->get_link() == symbol_section->get_index()) {
|
||||
hash_section = sec;
|
||||
hash_section_index = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
Elf_Half
|
||||
get_string_table_index() const
|
||||
{
|
||||
return (Elf_Half)symbol_section->get_link();
|
||||
}
|
||||
//------------------------------------------------------------------------------
|
||||
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 >
|
||||
bool
|
||||
generic_get_symbol( Elf_Xword index,
|
||||
std::string& name, Elf64_Addr& value,
|
||||
Elf_Xword& size,
|
||||
unsigned char& bind, unsigned char& type,
|
||||
Elf_Half& section_index,
|
||||
unsigned char& other ) const
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
bool generic_get_symbol(Elf_Xword index, std::string& name, Elf64_Addr& value, Elf_Xword& size,
|
||||
unsigned char& bind, unsigned char& type, Elf_Half& section_index,
|
||||
unsigned char& other) const {
|
||||
bool ret = false;
|
||||
|
||||
if ( index < get_symbols_num() ) {
|
||||
const T* pSym = reinterpret_cast<const T*>(
|
||||
symbol_section->get_data() +
|
||||
index * symbol_section->get_entry_size() );
|
||||
if (index < get_symbols_num()) {
|
||||
const T* pSym = reinterpret_cast<const T*>(symbol_section->get_data() +
|
||||
index * symbol_section->get_entry_size());
|
||||
|
||||
const endianess_convertor& convertor = elf_file.get_convertor();
|
||||
|
||||
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 ) );
|
||||
if ( 0 != pStr ) {
|
||||
string_section_accessor str_reader(string_section);
|
||||
const char* pStr = str_reader.get_string(convertor(pSym->st_name));
|
||||
if (0 != pStr) {
|
||||
name = pStr;
|
||||
}
|
||||
value = convertor( pSym->st_value );
|
||||
size = convertor( pSym->st_size );
|
||||
bind = ELF_ST_BIND( pSym->st_info );
|
||||
type = ELF_ST_TYPE( pSym->st_info );
|
||||
section_index = convertor( pSym->st_shndx );
|
||||
other = pSym->st_other;
|
||||
value = convertor(pSym->st_value);
|
||||
size = convertor(pSym->st_size);
|
||||
bind = ELF_ST_BIND(pSym->st_info);
|
||||
type = ELF_ST_TYPE(pSym->st_info);
|
||||
section_index = convertor(pSym->st_shndx);
|
||||
other = pSym->st_other;
|
||||
|
||||
ret = true;
|
||||
}
|
||||
@@ -238,41 +184,37 @@ class symbol_section_accessor
|
||||
return ret;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
template< class T >
|
||||
Elf_Word
|
||||
generic_add_symbol( Elf_Word name, Elf64_Addr value, Elf_Xword size,
|
||||
unsigned char info, unsigned char other,
|
||||
Elf_Half shndx )
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
template <class T>
|
||||
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();
|
||||
|
||||
T entry;
|
||||
entry.st_name = convertor( name );
|
||||
entry.st_name = convertor(name);
|
||||
entry.st_value = value;
|
||||
entry.st_value = convertor( entry.st_value );
|
||||
entry.st_size = size;
|
||||
entry.st_size = convertor( entry.st_size );
|
||||
entry.st_info = convertor( info );
|
||||
entry.st_other = convertor( other );
|
||||
entry.st_shndx = convertor( shndx );
|
||||
entry.st_value = convertor(entry.st_value);
|
||||
entry.st_size = size;
|
||||
entry.st_size = convertor(entry.st_size);
|
||||
entry.st_info = convertor(info);
|
||||
entry.st_other = convertor(other);
|
||||
entry.st_shndx = convertor(shndx);
|
||||
|
||||
symbol_section->append_data( reinterpret_cast<char*>( &entry ),
|
||||
sizeof( entry ) );
|
||||
symbol_section->append_data(reinterpret_cast<char*>(&entry), sizeof(entry));
|
||||
|
||||
Elf_Word nRet = symbol_section->get_size() / sizeof( entry ) - 1;
|
||||
Elf_Word nRet = symbol_section->get_size() / sizeof(entry) - 1;
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
const elfio& elf_file;
|
||||
section* symbol_section;
|
||||
Elf_Half hash_section_index;
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
const elfio& elf_file;
|
||||
section* symbol_section;
|
||||
Elf_Half hash_section_index;
|
||||
const section* hash_section;
|
||||
};
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_SYMBOLS_HPP
|
||||
#endif // ELFIO_SYMBOLS_HPP
|
||||
+68
-120
@@ -23,187 +23,135 @@ THE SOFTWARE.
|
||||
#ifndef ELFIO_UTILS_HPP
|
||||
#define ELFIO_UTILS_HPP
|
||||
|
||||
#define ELFIO_GET_ACCESS( TYPE, NAME, FIELD ) \
|
||||
TYPE get_##NAME() const \
|
||||
{ \
|
||||
return (*convertor)( FIELD ); \
|
||||
#define ELFIO_GET_ACCESS(TYPE, NAME, FIELD) \
|
||||
TYPE get_##NAME() const { return (*convertor)(FIELD); }
|
||||
#define ELFIO_SET_ACCESS(TYPE, NAME, FIELD) \
|
||||
void set_##NAME(TYPE value) { \
|
||||
FIELD = value; \
|
||||
FIELD = (*convertor)(FIELD); \
|
||||
}
|
||||
#define ELFIO_SET_ACCESS( TYPE, NAME, FIELD ) \
|
||||
void set_##NAME( TYPE value ) \
|
||||
{ \
|
||||
FIELD = value; \
|
||||
FIELD = (*convertor)( FIELD ); \
|
||||
}
|
||||
#define ELFIO_GET_SET_ACCESS( TYPE, NAME, FIELD ) \
|
||||
TYPE get_##NAME() const \
|
||||
{ \
|
||||
return (*convertor)( FIELD ); \
|
||||
} \
|
||||
void set_##NAME( TYPE value ) \
|
||||
{ \
|
||||
FIELD = value; \
|
||||
FIELD = (*convertor)( FIELD ); \
|
||||
#define ELFIO_GET_SET_ACCESS(TYPE, NAME, 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
|
||||
#define ELFIO_SET_ACCESS_DECL(TYPE, NAME) virtual void set_##NAME(TYPE value) = 0
|
||||
|
||||
#define ELFIO_GET_SET_ACCESS_DECL( TYPE, NAME ) \
|
||||
virtual TYPE get_##NAME() const = 0; \
|
||||
virtual void set_##NAME( TYPE value ) = 0
|
||||
#define ELFIO_GET_SET_ACCESS_DECL(TYPE, NAME) \
|
||||
virtual TYPE get_##NAME() const = 0; \
|
||||
virtual void set_##NAME(TYPE value) = 0
|
||||
|
||||
namespace ELFIO {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
class endianess_convertor {
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
endianess_convertor()
|
||||
{
|
||||
need_conversion = false;
|
||||
public:
|
||||
//------------------------------------------------------------------------------
|
||||
endianess_convertor() { need_conversion = false; }
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void setup(unsigned char elf_file_encoding) {
|
||||
need_conversion = (elf_file_encoding != get_host_encoding());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
setup( unsigned char elf_file_encoding )
|
||||
{
|
||||
need_conversion = ( elf_file_encoding != get_host_encoding() );
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
uint64_t
|
||||
operator()( uint64_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
//------------------------------------------------------------------------------
|
||||
uint64_t operator()(uint64_t value) const {
|
||||
if (!need_conversion) {
|
||||
return value;
|
||||
}
|
||||
value =
|
||||
( ( value & 0x00000000000000FFull ) << 56 ) |
|
||||
( ( value & 0x000000000000FF00ull ) << 40 ) |
|
||||
( ( value & 0x0000000000FF0000ull ) << 24 ) |
|
||||
( ( value & 0x00000000FF000000ull ) << 8 ) |
|
||||
( ( value & 0x000000FF00000000ull ) >> 8 ) |
|
||||
( ( value & 0x0000FF0000000000ull ) >> 24 ) |
|
||||
( ( value & 0x00FF000000000000ull ) >> 40 ) |
|
||||
( ( value & 0xFF00000000000000ull ) >> 56 );
|
||||
value = ((value & 0x00000000000000FFull) << 56) | ((value & 0x000000000000FF00ull) << 40) |
|
||||
((value & 0x0000000000FF0000ull) << 24) | ((value & 0x00000000FF000000ull) << 8) |
|
||||
((value & 0x000000FF00000000ull) >> 8) | ((value & 0x0000FF0000000000ull) >> 24) |
|
||||
((value & 0x00FF000000000000ull) >> 40) | ((value & 0xFF00000000000000ull) >> 56);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int64_t
|
||||
operator()( int64_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
//------------------------------------------------------------------------------
|
||||
int64_t operator()(int64_t value) const {
|
||||
if (!need_conversion) {
|
||||
return value;
|
||||
}
|
||||
return (int64_t)(*this)( (uint64_t)value );
|
||||
return (int64_t)(*this)((uint64_t)value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
uint32_t
|
||||
operator()( uint32_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
//------------------------------------------------------------------------------
|
||||
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 = ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8) |
|
||||
((value & 0x00FF0000) >> 8) | ((value & 0xFF000000) >> 24);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
int32_t
|
||||
operator()( int32_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
//------------------------------------------------------------------------------
|
||||
int32_t operator()(int32_t value) const {
|
||||
if (!need_conversion) {
|
||||
return value;
|
||||
}
|
||||
return (int32_t)(*this)( (uint32_t)value );
|
||||
return (int32_t)(*this)((uint32_t)value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
uint16_t
|
||||
operator()( uint16_t value ) const
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
//------------------------------------------------------------------------------
|
||||
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
|
||||
{
|
||||
if ( !need_conversion ) {
|
||||
//------------------------------------------------------------------------------
|
||||
int16_t operator()(int16_t value) const {
|
||||
if (!need_conversion) {
|
||||
return value;
|
||||
}
|
||||
return (int16_t)(*this)( (uint16_t)value );
|
||||
return (int16_t)(*this)((uint16_t)value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
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
|
||||
{
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
unsigned char get_host_encoding() const {
|
||||
static const int tmp = 1;
|
||||
if ( 1 == *(char*)&tmp ) {
|
||||
if (1 == *(char*)&tmp) {
|
||||
return ELFDATA2LSB;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return ELFDATA2MSB;
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
//------------------------------------------------------------------------------
|
||||
private:
|
||||
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 ) {
|
||||
while (*name) {
|
||||
h = (h << 4) + *name++;
|
||||
g = h & 0xf0000000;
|
||||
if ( g != 0 )
|
||||
h ^= g >> 24;
|
||||
if (g != 0) h ^= g >> 24;
|
||||
h &= ~g;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
} // namespace ELFIO
|
||||
} // namespace ELFIO
|
||||
|
||||
#endif // ELFIO_UTILS_HPP
|
||||
#endif // ELFIO_UTILS_HPP
|
||||
Reference in new issue
封鎖使用者