Giter Club home page Giter Club logo

elfio's People

Contributors

alan-jowett avatar alexvlx avatar alvarofe avatar belissent avatar bladchan avatar covanam avatar gblues avatar glebx-f avatar klueska avatar martincon avatar matthew-mcraven avatar maxxsoft avatar nioshd avatar orbitcowboy avatar pavelkryukov avatar roboschmied avatar scribam avatar semi avatar serge1 avatar stdrc avatar strazzere avatar tgotic avatar thesamesam avatar tklauser avatar vasekp avatar vincom2 avatar viniciuslrangel avatar xutaxkamay avatar y27988 avatar ytakatsukasa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elfio's Issues

Bombsquad_headless

Can it work on termux Ubuntu
I tried running a programme in Ubuntu but it refused the bombsquad_server (bombsquad_headless)

segmentation fault examples\elfdump

I was able to crash the example/elfdump with an invalid input (generated by afl-fuzz):

The file is available at https://filebin.net/1n9a3p3m6h4qpnp9

$ ./elfdump crash.elf > /dev/null
Segmentation fault
$

gdb backtrace

backtrace:
#0  0x0000000000420983 in ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym> (this=<optimized out>, index=0, name=Python Exception <class 'gdb.error'> There is no member named _M_dataplus.: 
, value=@0x7fffffffdc58: 0, size=@0x7fffffffdc50: 0, bind=@0x7fffffffdc4f: 0 '\000', type=@0x7fffffffdc4e: 0 '\000', section_index=@0x7fffffffdc4c: 0, other=@0x7fffffffdc4b: 0 '\000') at ../../elfio/elfio_strings.hpp:48
        convertor = <optimized out>
        string_section = 0x4549e0
        pStr = <optimized out>
        pSym = <optimized out>
        str_reader = <optimized out>
        ret = false
#1  0x0000000000406b06 in ELFIO::symbol_section_accessor_template<ELFIO::section>::get_symbol (this=<optimized out>, index=<optimized out>, name=..., value=<optimized out>, size=<optimized out>, bind=<optimized out>, type=<optimized out>, section_index=<optimized out>, other=<optimized out>) at ../../elfio/elfio_symbols.hpp:71
No locals.
#2  ELFIO::dump::symbol_tables (out=..., reader=...) at ../../elfio/elfio_dump.hpp:619
        value = 0
        type = 0 '\000'
        section = 0
        size = 0
        bind = 0 '\000'
        other = 0 '\000'
        name = <optimized out>
        i = <optimized out>
        sym_no = <optimized out>
        symbols = <optimized out>
        sec = 0x454940
        i = <optimized out>
        n = <optimized out>
#3  0x00000000004028ec in main (argc=<optimized out>, argv=0x7fffffffde78) at elfdump.cpp:52
        reader = {sections = {parent = 0x7fffffffdce8}, segments = {parent = 0x7fffffffdce8}, header = 0x454280, sections_ = std::vector of length 41, capacity 64 = {0x4542e0, 0x454380, 0x454450, 0x454550, 0x454840, 0x454940, 0x4549e0, 0x455240, 0x455380, 0x455520, 0x4556e0, 0x455d50, 0x455df0, 0x456280, 0x456320, 0x4563c0, 0x458530, 0x458f00, 0x45ae00, 0x45be90, 0x45bf30, 0x45c000, 0x45c0a0, 0x45c360, 0x45c630, 0x45c700, 0x45c9b0, 0x45dcd0, 0x45dd70, 0x45deb0, 0x465ac0, 0x465b60, 0x466510, 0x476c50, 0x48c390, 0x48c430, 0x48c4d0, 0x48e3e0, 0x49c140, 0x49eec0, 0x4a4420}, segments_ = std::vector of length 12, capacity 16 = {0x451fb0, 0x451f00, 0x4a49d0, 0x4a6770, 0x4a6800, 0x4abfa0, 0x4ad950, 0x4adc10, 0x4adcd0, 0x4ade10, 0x4ae6e0, 0x4ae770}, convertor = {need_conversion = false}, current_file_pos = 0}


registers:
rax            0x6                 6
rbx            0x0                 0
rcx            0x4665b0            4613552
rdx            0xadcd              44493
rsi            0x42f5a0            4388256
rdi            0x454940            4540736
rbp            0x7fffffffdd30      0x7fffffffdd30
rsp            0x7fffffffdb80      0x7fffffffdb80
r8             0x7fffffffdc50      140737488346192
r9             0x7fffffffdc4f      140737488346191
r10            0x30                48
r11            0x246               582
r12            0xfffffffffffffffc  -4
r13            0x0                 0
r14            0x4549e0            4540896
r15            0x0                 0
rip            0x420983            0x420983 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+355>
eflags         0x10202             [ IF RF ]
cs             0x33                51
ss             0x2b                43
ds             0x0                 0
es             0x0                 0
fs             0x0                 0
gs             0x0                 0


current instructions:
=> 0x420983 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+355>:	mov    0x0(%r13,%rbx,1),%eax
   0x420988 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+360>:	movslq %fs:(%r12),%rcx
   0x42098d <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+365>:	mov    0xeacc(%rip),%rdx        # 0x42f460 <__afl_area_ptr>
   0x420994 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+372>:	xor    $0x9a54,%rcx
   0x42099b <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+379>:	addb   $0x1,(%rdx,%rcx,1)
   0x42099f <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+383>:	movl   $0x4d2a,%fs:(%r12)
   0x4209a8 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+392>:	cmpb   $0x0,0x0(%rbp)
   0x4209ac <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+396>:	mov    %eax,%r15d
   0x4209af <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+399>:	bswap  %r15d
   0x4209b2 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+402>:	cmove  %eax,%r15d
   0x4209b6 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+406>:	mov    (%r14),%rax
   0x4209b9 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+409>:	mov    %r14,%rdi
   0x4209bc <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+412>:	callq  *0x98(%rax)
   0x4209c2 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+418>:	cmp    %r15,%rax
   0x4209c5 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+421>:	jbe    0x420a79 <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+601>
   0x4209cb <ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const+427>:	movslq %fs:(%r12),%rax


threads backtrace:

Thread 1 (process 12108):
#0  0x0000000000420983 in ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym> (this=<optimized out>, index=0, name=Python Exception <class 'gdb.error'> There is no member named _M_dataplus.: 
, value=@0x7fffffffdc58: 0, size=@0x7fffffffdc50: 0, bind=@0x7fffffffdc4f: 0 '\000', type=@0x7fffffffdc4e: 0 '\000', section_index=@0x7fffffffdc4c: 0, other=@0x7fffffffdc4b: 0 '\000') at ../../elfio/elfio_strings.hpp:48
#1  0x0000000000406b06 in ELFIO::symbol_section_accessor_template<ELFIO::section>::get_symbol (this=<optimized out>, index=<optimized out>, name=..., value=<optimized out>, size=<optimized out>, bind=<optimized out>, type=<optimized out>, section_index=<optimized out>, other=<optimized out>) at ../../elfio/elfio_symbols.hpp:71
#2  ELFIO::dump::symbol_tables (out=..., reader=...) at ../../elfio/elfio_dump.hpp:619
#3  0x00000000004028ec in main (argc=<optimized out>, argv=0x7fffffffde78) at elfdump.cpp:52


NULL pointer dereference in ELFIO::endianess_convertor::operator

Hello.

I found a NULL pointer dereference in ELFIO.

Please confirm.

Thanks.

OS: CentOS 7 64bit
Version: 1fdbb64
PoC Download: null_endianess_convertor.zip

Steps to reproduce:
1.Download the .POC files.
2.Execute the following command
: ./tutorial $POC

=================================================================
==26289==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x5652045535d6 bp 0x000000000000 sp 0x7ffd1642da10 T0)
==26289==The signal is caused by a READ memory access.
==26289==Hint: address points to the zero page.
    #0 0x5652045535d5 in ELFIO::endianess_convertor::operator()(unsigned int) const ../../elfio/elfio_utils.hpp:110
    #1 0x5652045535d5 in bool ELFIO::symbol_section_accessor_template<ELFIO::section>::generic_get_symbol<ELFIO::Elf64_Sym>(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const ../../elfio/elfio_symbols.hpp:225
    #2 0x5652045535d5 in ELFIO::symbol_section_accessor_template<ELFIO::section>::get_symbol(unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned long&, unsigned long&, unsigned char&, unsigned char&, unsigned short&, unsigned char&) const ../../elfio/elfio_symbols.hpp:71
    #3 0x56520452ffb6 in main /home/karas/ELFIO/examples/tutorial/tutorial.cpp:82
    #4 0x7f962fa611c0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x211c0)
    #5 0x565204533f09 in _start (/home/karas/ELFIO/examples/tutorial/tutorial+0xef09)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV ../../elfio/elfio_utils.hpp:110 in ELFIO::endianess_convertor::operator()(unsigned int) const
==26289==ABORTING

==========
[Acknowledgement]
This work was supported by ICT R&D program of MSIP/IITP. [R7518-16-1001, Innovation hub for high Performance Computing]

segmentation fault running example writer output

G++ version 7.4 (tested on 8.4 too)
Ubuntu version 16.04 (tested on 19 too)
After

./configure

and

make

when i run ./writer from examples
then

chmod +x hello_i386_32 

and always get segmentation fault when i run ./hello_i386_32
Any clues ?

elfio_dump.hpp bug

In elfio_dump.hpp,

line611:
for ( Elf_Half i = 0; i < sym_no; ++i ) {
std::string name;
Elf64_Addr value = 0;
Elf_Xword size = 0;
unsigned char bind = 0;
unsigned char type = 0;
Elf_Half section = 0;
unsigned char other = 0;
symbols.get_symbol( i, name, value, size, bind, type, section, other );
symbol_table( out, i, name, value, size, bind, type, section, reader.get_class() );
}
line630:
static void
symbol_table( std::ostream& out,
Elf_Half no,
std::string& name,
Elf64_Addr value,
Elf_Xword size,
unsigned char bind,
unsigned char type,
Elf_Half section,
unsigned int elf_class )

If the sym count is over 65535(Elf_Half),it will never jump out from the loop, and the function "symbol_table" won't work expectly!
So we should change its type to Elf_Xword?

Adding to vcpkg

I have a fork of vcpkg (https://github.com/schultetwin1/vcpkg/tree/add_elfio_port) which will add elfio to vcpkg. If you're ok with it, I'd like open a pull request to merge elfio into vcpkg officially.

In order to do so, it would probably be better to wait for the next official release of ELFIO. So I'll wait for the next release and then put the pull request out.

parent shadows parent on Sections constructor

Hi!

This is easy to fix and should silence compiler warnings:

In the explicit constructor of Sections:

explicit Sections( elfio* parent ) : parent( parent ) {}

Parent is shadowed, so the compiler shouts:

warning: declaration of 'parent' shadows a member of 'ELFIO::elfio::Segments' [-Wshadow]
988 | explicit Segments( elfio* parent ) : parent( parent ) {}

Call to virtual function during construction

Found by Clang-Tidy 7:

/home/travis/build/MIPT-ILab/mipt-mips/simulator/../external/elfio/elfio_header.hpp:97:3: warning: Call to virtual function during construction [clang-analyzer-optin.cplusplus.VirtualCall]
                set_version( EV_CURRENT );
                ^
/home/travis/build/MIPT-ILab/mipt-mips/simulator/infra/memory/elf/elf_loader.cpp:27:18: note: Calling default constructor for 'elfio'
    ELFIO::elfio reader;
                 ^
/home/travis/build/MIPT-ILab/mipt-mips/simulator/../external/elfio/elfio.hpp:81:9: note: Calling 'elfio::create'
        create( ELFCLASS32, ELFDATA2LSB );
        ^
/home/travis/build/MIPT-ILab/mipt-mips/simulator/../external/elfio/elfio.hpp:95:18: note: Calling 'elfio::create_header'
        header = create_header( file_class, encoding );
                 ^
/home/travis/build/MIPT-ILab/mipt-mips/simulator/../external/elfio/elfio.hpp:314:9: note: 7991Taking false branch
        if ( file_class == ELFCLASS64 ) { warning
        ^
/home/travis/build/MIPT-ILab/mipt-mips/simulator/../external/elfio/elfio.hpp:318:14: note: Taking true branch
s        else if ( file_class == ELFCLASS32 ) {
             ^
/home/travis/build/MIPT-ILab/mipt-mips/simulator/../external/elfio/elfio.hpp:319:30: note: Calling constructor for 'elf_header_impl<ELFIO::Elf32_Ehdr>'
            new_header = new elf_header_impl< Elf32_Ehdr >( &convertor,
                             ^
/home/travis/build/MIPT-ILab/mipt-mips/simulator/../external/elfio/elfio_header.hpp:97:3: generated note: This constructor of an object of type 'elf_header_impl' has not returned when the virtual method was called
                set_version( EV_CURRENT );
                ^
/home/travis/build/MIPT-ILab/mipt-mips/simulator/../external/elfio/elfio_header.hpp:97:3: note: Call to virtual function during construction
.

NULL pointer dereference in processStringTable

Hello.

I found a NULL pointer dereference in ELFIO.

Please confirm.

Thanks.

OS: CentOS 7 64bit
Version: 1fdbb64
PoC Download: null_processStringTable.zip

Steps to reproduce:
1.Download the .POC files.
2.Execute the following command
: ./anonymizer $POC

=================================================================
==10845==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000001 (pc 0x7fbadbc20416 bp 0x7ffd892f9e20 sp 0x7ffd892f9598 T0)
==10845==The signal is caused by a READ memory access.
==10845==Hint: address points to the zero page.
    #0 0x7fbadbc20415  (/lib/x86_64-linux-gnu/libc.so.6+0xa8415)
    #1 0x7fbadc54657b  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x5157b)
    #2 0x55c981d5bd25 in processStringTable(ELFIO::section const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) /home/karas/ELFIO/examples/anonymizer/anonymizer.cpp:99
    #3 0x55c981d50b88 in main /home/karas/ELFIO/examples/anonymizer/anonymizer.cpp:128
    #4 0x7fbadbb991c0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x211c0)
    #5 0x55c981d54859 in _start (/home/karas/ELFIO/examples/anonymizer/anonymizer+0xd859)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/lib/x86_64-linux-gnu/libc.so.6+0xa8415) 
==10845==ABORTING

==========
[Acknowledgement]
This work was supported by ICT R&D program of MSIP/IITP. [R7518-16-1001, Innovation hub for high Performance Computing]

MS Visual Studio warning: declaration hides previous local declaration

Hi

This code generates warning in Visual Studio:

Elf_Word offset = sections[i]->get_name_string_offset();

c:\projects\mipt-mips\external\elfio\elfio.hpp(373): warning C4456: declaration of 'offset' hides previous local declaration [C:\projects\mipt-mips\simulator\build\mipt-mips-src.vcxproj]

Should we possible to rename the internal variable to section_offset or is there a better name proposal?

heap-buffer-overflow in ELFIO::note_section_accessor_template

Hello.

I found a heap-buffer-overflow in ELFIO.

Please confirm.

Thanks.

OS: CentOS 7 64bit
Version: 1fdbb64
PoC Download: ov_note_section_accessor_template.zip

Steps to reproduce:
1.Download the .POC files.
2.Execute the following command
: ./elfdump $FILE

=================================================================
==23389==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x604000000031 at pc 0x7f10d43d5733 bp 0x7ffd9f3604c0 sp 0x7ffd9f35fc68
READ of size 4294967295 at 0x604000000031 thread T0
    #0 0x7f10d43d5732  (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x79732)
    #1 0x7f10d40fbab4 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x125ab4)
    #2 0x7f10d40fc7ea in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x1267ea)
    #3 0x55698a59bd12 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(char const*, unsigned long) /usr/include/c++/7/bits/basic_string.h:1403
    #4 0x55698a59bd12 in ELFIO::note_section_accessor_template<ELFIO::section>::get_note(unsigned int, unsigned int&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, void*&, unsigned int&) const ../../elfio/elfio_note.hpp:83
    #5 0x55698a59bd12 in ELFIO::dump::notes(std::ostream&, ELFIO::elfio const&) ../../elfio/elfio_dump.hpp:693
    #6 0x55698a54f1c6 in main /home/karas/ELFIO/examples/elfdump/elfdump.cpp:53
    #7 0x7f10d3a001c0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x211c0)
    #8 0x55698a551d49 in _start (/home/karas/ELFIO/examples/elfdump/elfdump+0xfd49)

0x604000000031 is located 0 bytes to the right of 33-byte region [0x604000000010,0x604000000031)
allocated by thread T0 here:
    #0 0x7f10d443c618 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.4+0xe0618)
    #1 0x55698a55e4f8 in ELFIO::section_impl<ELFIO::Elf32_Shdr>::load(std::istream&, std::fpos<__mbstate_t>) ../../elfio/elfio_section.hpp:248
    #2 0x55698a5a3abf  (/home/karas/ELFIO/examples/elfdump/elfdump+0x61abf)

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x79732) 
Shadow bytes around the buggy address:
  0x0c087fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c087fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c087fff8000: fa fa 00 00 00 00[01]fa fa fa 00 00 00 00 03 fa
  0x0c087fff8010: fa fa 00 00 00 00 01 fa fa fa 00 00 00 00 00 01
  0x0c087fff8020: fa fa 00 00 00 00 01 fa fa fa fa fa fa fa fa fa
  0x0c087fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c087fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
==23389==ABORTING

==========
[Acknowledgement]
This work was supported by ICT R&D program of MSIP/IITP. [R7518-16-1001, Innovation hub for high Performance Computing]

Adding PROGBITS section

Hi,

Is there an API to add an arbitrary PROGBITS section. I see the API can do notes, dynamic and strings types. For example, is it possible to create a section and add raw data buffer to the section without structured layout?

I am working off of the add_note example:

 note_sec->set_type( SHT_PROGBITS);
.. What is the correct accessor writer for this ? 
 note_writer.add_XXXX( &data_buffer, size );

thank you.

Rewriting the symbol table

I want to use this library to rewrite the symbol table of an existing ELF file. In particular I would like to replace every function name with another one. The examples and documentation were very helpful to show me how to parse everything but it doesn't seem to explain how to "flush" changes back. The symbol_section_accessor does not have methods for writing, only get_symbol. Do I need to create a new .symtab and add all symbols I want into it? That seems pretty cumbersome and I'm not sure if the ELF will then still work seamlessly. In fact, getting the file offsets of the function names respectively would be enough as well but that also doesn't seem to be easily doable with ELFIO.

Compilation error if including after <Windows.h>

Hey, wondering if something can be done about that, this piece of code compiles fine:

#include <elfio/elfio.hpp>
#include <Windows.h>

void test()
{
}

But this one doesn't:

#include <Windows.h>
#include <elfio/elfio.hpp>

void test()
{
}
elfio_section.hpp(214,59): warning C4003: not enough arguments for function-like macro invocation 'max'
elfio_section.hpp(214,59): error C2589: '(': illegal token on right side of '::'

image

sections in segments don't get populated when loading new ELF

elfio::load_segments populates the list of segments but it never links them with their sections. Interestingly, there is a comment that suggests this was the original plan:

bool load_segments( std::ifstream& stream )
{
...
    // Add section into the segments' container <- this is never done
    segments_.push_back( seg );
...
}

After adding a new segment the code should iterate through the sections and add the ones that are linked to the segment to segment::sections.

Compile warnings on platforms where size_t is not 64-bits

In elfio_segment.hpp, line 172, a ULLONG is passed to set_stream_size.
On platforms where sizeof(size_t) != sizeof(uint64_t), this will cause a compile time warning about constant conversion.
This also affects elfio_section.hpp

I am raising this issue because (strangely enough), I have a 32-bit use case.
My project consumes this library, and outputs a WASM binary.
WASM, as of right now, always uses 32-bit pointers, which to my understanding means size_t is 32-bits, not 64-bits.
This causes my compilation logs to be flooded with constant conversion warning.

I see two easy ways to fix this:

  1. Change the signature of set_stream_size to always take a uint64_t.
  2. Pass a value of std::numeric_limits<size_t>::max() to set_stream_size (see reference).

My preference would be option 2, as this would avoid API type changes.

If either of these approaches are acceptable, I will open a pull request.

arm so file saving is failed

Hi,

I have one so file which is built for arm Linux. when I tried to use elfio to parse it and save it as the new file. Internal function layout_segments_and_their_sections will return error.

The code is simple and is like:
elf_file.load(inFilename);
elf_file.save(outFilename);

I did not do other changes. Could you please check this issue?
libaes.zip

segmentation fault in example/elfdump

I was able to crash the example/elfdump with an invalid input (generated by afl-fuzz):

The file is available at https://filebin.ca/4rhvxKnzKdGt/test

$ ./elfdump test > /dev/null
Segmentation fault
$

gdb backtrace

backtrace:
#0  __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:440
No locals.
#1  0x00007ffff7f0666c in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) () from /lib/x86_64-linux-gnu/libstdc++.so.6
No symbol table info available.
#2  0x00007ffff7f073f1 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long) () from /lib/x86_64-linux-gnu/libstdc++.so.6
No symbol table info available.
#3  0x0000000000407704 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign (__s=0x45452c "GNU", __n=4294967295, this=<optimized out>) at /bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/basic_string.h:1422
No locals.
#4  ELFIO::note_section_accessor_template<ELFIO::section>::get_note (this=<optimized out>, index=<optimized out>, type=<optimized out>, name=..., desc=<optimized out>, descSize=<optimized out>) at ../../elfio/elfio_note.hpp:83
        align = 4
        convertor = <optimized out>
        namesz = <optimized out>
        max_name_size = <optimized out>
        pData = <optimized out>
#5  ELFIO::dump::notes (out=..., reader=...) at ../../elfio/elfio_dump.hpp:693
        type = 1
        name = <optimized out>
        desc = <optimized out>
        descsz = <optimized out>
        j = 0
        notes = {elf_file = @0x7fffffffdd68, note_section = 0x454450, note_start_positions = std::vector of length 1, capacity 1 = {0}}
        no_notes = <optimized out>
        sec = <optimized out>
        i = 2
        no = <optimized out>
#6  0x0000000000402917 in main (argc=<optimized out>, argv=0x7fffffffdef8) at elfdump.cpp:53
        reader = {sections = {parent = 0x7fffffffdd68}, segments = {parent = 0x7fffffffdd68}, header = 0x454280, sections_ = std::vector of length 41, capacity 64 = {0x4542e0, 0x454380, 0x454450, 0x454550, 0x454840, 0x454940, 0x4550d0, 0x455930, 0x455a70, 0x455c10, 0x455dd0, 0x456440, 0x4564e0, 0x456970, 0x456a10, 0x456ab0, 0x458c20, 0x4595f0, 0x45b510, 0x45c5a0, 0x45c640, 0x45c6e0, 0x45c780, 0x45ca40, 0x45cd10, 0x45cde0, 0x45d090, 0x45e3b0, 0x45e450, 0x45e590, 0x4661a0, 0x466240, 0x466bf0, 0x477320, 0x48c9c0, 0x48ca60, 0x48cb20, 0x48ea30, 0x49c790, 0x49f510, 0x4a4a70}, segments_ = std::vector of length 12, capacity 16 = {0x451f70, 0x451ec0, 0x4a5020, 0x4a6dc0, 0x4a6e50, 0x4ac610, 0x4adfc0, 0x4ae280, 0x4ae340, 0x4ae480, 0x4aed50, 0x4aede0}, convertor = {need_conversion = false}, current_file_pos = 0}


registers:
rax            0x7ffef79bf010      140733052612624
rbx            0x7fffffffdd00      140737488346368
rcx            0x7ffef79bf010      140733052612624
rdx            0xffffffff          4294967295
rsi            0x45452c            4539692
rdi            0x7ffef79bf010      140733052612624
rbp            0x0                 0x0
rsp            0x7fffffffdbe8      0x7fffffffdbe8
r8             0xffffffff          4294967295
r9             0x45452c            4539692
r10            0x22                34
r11            0x246               582
r12            0x7fffffffdd10      140737488346384
r13            0x0                 0
r14            0xffffffff          4294967295
r15            0x7ffef79bf010      140733052612624
rip            0x7ffff7bfe2a4      0x7ffff7bfe2a4 <__memmove_avx_unaligned_erms+548>
eflags         0x10216             [ PF AF IF RF ]
cs             0x33                51
ss             0x2b                43
ds             0x0                 0
es             0x0                 0
fs             0x0                 0
gs             0x0                 0


current instructions:
=> 0x7ffff7bfe2a4 <__memmove_avx_unaligned_erms+548>:	vmovdqu -0x20(%rsi,%rdx,1),%ymm8
   0x7ffff7bfe2aa <__memmove_avx_unaligned_erms+554>:	lea    -0x20(%rdi,%rdx,1),%r11
   0x7ffff7bfe2af <__memmove_avx_unaligned_erms+559>:	lea    -0x20(%rsi,%rdx,1),%rcx
   0x7ffff7bfe2b4 <__memmove_avx_unaligned_erms+564>:	mov    %r11,%r9
   0x7ffff7bfe2b7 <__memmove_avx_unaligned_erms+567>:	mov    %r11,%r8
   0x7ffff7bfe2ba <__memmove_avx_unaligned_erms+570>:	and    $0x1f,%r8
   0x7ffff7bfe2be <__memmove_avx_unaligned_erms+574>:	sub    %r8,%rcx
   0x7ffff7bfe2c1 <__memmove_avx_unaligned_erms+577>:	sub    %r8,%r9
   0x7ffff7bfe2c4 <__memmove_avx_unaligned_erms+580>:	sub    %r8,%rdx
   0x7ffff7bfe2c7 <__memmove_avx_unaligned_erms+583>:	cmp    0x60e8a(%rip),%rdx        # 0x7ffff7c5f158 <__x86_shared_non_temporal_threshold>
   0x7ffff7bfe2ce <__memmove_avx_unaligned_erms+590>:	ja     0x7ffff7bfe3c4 <__memmove_avx_unaligned_erms+836>
   0x7ffff7bfe2d4 <__memmove_avx_unaligned_erms+596>:	vmovdqu (%rcx),%ymm0
   0x7ffff7bfe2d8 <__memmove_avx_unaligned_erms+600>:	vmovdqu -0x20(%rcx),%ymm1
   0x7ffff7bfe2dd <__memmove_avx_unaligned_erms+605>:	vmovdqu -0x40(%rcx),%ymm2
   0x7ffff7bfe2e2 <__memmove_avx_unaligned_erms+610>:	vmovdqu -0x60(%rcx),%ymm3
   0x7ffff7bfe2e7 <__memmove_avx_unaligned_erms+615>:	sub    $0x80,%rcx


threads backtrace:

Thread 1 (process 77605):
#0  __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:440
#1  0x00007ffff7f0666c in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007ffff7f073f1 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x0000000000407704 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign (__s=0x45452c "GNU", __n=4294967295, this=<optimized out>) at /bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/basic_string.h:1422
#4  ELFIO::note_section_accessor_template<ELFIO::section>::get_note (this=<optimized out>, index=<optimized out>, type=<optimized out>, name=..., desc=<optimized out>, descSize=<optimized out>) at ../../elfio/elfio_note.hpp:83
#5  ELFIO::dump::notes (out=..., reader=...) at ../../elfio/elfio_dump.hpp:693
#6  0x0000000000402917 in main (argc=<optimized out>, argv=0x7fffffffdef8) at elfdump.cpp:53

Double free after move constructor

When creating an elfio object and then use it with std::move in the move constructor of a new elfio object, the header, segment and section pointers of the original object aren't reassigned to nullptr, thus causing them to get freed with the destructor of the original object is called.
This causes the destructor of the second elfio object to throw an access violation error when trying to delete these pointers since they are already freed.

Suggested solution:
Implement a custom move constructor and move assignment operator that reassign these pointers to nullptr.

OR

Replace all occurrences of new in the code with std::make_unique and replace all raw pointers with std::unique_ptrs to allow for default move construction and assignment implementations (as present today).

Read only required section information in the elfio class 'load' function

Can only the required sections of the executable be read into the memory instead of reading the complete binary/all the sections and segments of the binary in the 'class elfio' load() function?
With the complete executable being read into the memory, there is a peak in memory usage when binary sizes are in GB.
Can you please consider this enhancement?

Elfio not on conan (yet)

Since you need help with maintainers, @serge1 , and you are happy about #53 , I added elfio to conan package manager,

conan-io/conan-center-index#3390

So I can at least contribute a bit back for using Elfio in a project. Its a wonderful library

Since you mentioned adding examples in #53:
you do not add example folders into build dependencies.
C++ package mangers work in this respect a bit different than packages for a Linux distribution.
In case of vcpkg or conan you are just interested to get what you need to build your app, if more is needed you look to the sources.

As soon as the PR on conan is accepted, what should happen soon, this issue can be closed ;-)

add new section

Hi,

When I use elfio to add the new section in existing elf file, it will have some relocation issues.
Do you have any idea about this?

Saved .elf differs from read .elf by a lot more than the changed section

Hello, I'm don't know a lot of details about the .elf format, so maybe there can be a good reason for it.

I have a bare metal project for ARM Cortex M, using the ARM GCC compiler toolchain for that.
So I use ELFIO load a produced .elf file, search for one section by name, call set_data() on that section to change the content (size should remain the same), and call .save() on that elfio object.
The changed section is only a few uint32's big.

Looking at the resulting file in the hex editor, I find things like big blocks (kilobytes maybe) of zeroes where the original file had data.
Also, the saved file is now 6KB bigger than the original one.

Can there be legitimate reasons for these observations, or is something wrong? (I've not tried using it yet)

Explicit = Operator for elfio Segfault

Correct me if I'm wrong but ELFIO::elfio seems to have an explicit = operator which does a shallow copy of pointers within the object. This causes a segfault on second deconstruction if you happen to use it.

It should probably be explicitly deleted, a deep copy implemented or a solution which can survive a shallow copy (thread safety + shared pointers).

Make STR_FUNC_TABLE public

This define is used to define the str_##name functions inside elfio_dump.hpp that are used to translate field values in headers and structs in the elf file to their corresponding string meanings.
The problem is it is a private function in the class, so the only way to translate field values to strings is to use the dump API (dump::section_header(), dump::header() etc), which dictates what and in what format the fields will be printed.

REQUEST: Make this function public inside the dump class so it can be used wherever needed in the code

Seeing correct endian struct values while debugging

First of, it's great that this library can handle 32-bit, 64-bit and little/big endian ELFs so seemlessly.

However, in terms of endian support, if the host OS is e.g. little endian and the binary being parsed is big endian, the values loaded into the structs will show up with reversed endians in debuggers which can be confusing. This is of course corrected by the library by flipping the byte order as necessary when calling one of its getters. Maybe it's better to flip every struct member's endian immediately after parsing (if necessary) so that during debugging the correct values can be seen inside the structs as well and the specific getter methods which so far handle the byte order swapping can be replaced with struct accessors instead.

Entry of elf executable

This is more of a question than a issue, but how do we determine the entry address of a elf executable. In the tutorial, the entry address was 0x08048000, but I have no idea where that number came from.

Any explanation would be be helpful

PS: Could someone also explain align and what is is, Thanks!

there is a bug in elfio::load_sections

section* sec = create_section();
sec->load(....);
section::header is not credible.
when use symbol_section_accessor_template::get_symbols_num(), may be get a very big number. because section::get_size() get data from section::header. section::get_size() will return a big number, even though it is biger than stream_size. So it can put me in an endless loop when I deal with symtab section. At this point, the program looks like it's dead until it's done processing tens of billions of fake symbol data.

My stupid approach is to add judgment on the return value of get_size() in symbol_section_accessor_template::get_symbols_num().

Elf_Xword get_symbols_num() const
{
    Elf_Xword nRet = 0;
    if ( 0 != symbol_section->get_entry_size() && 
        symbol_section->get_size() < symbol_section->get_stream_size() ) {
        nRet =
            symbol_section->get_size() / symbol_section->get_entry_size();
    }

    return nRet;
}

Add support for memory-mapped ELFs

Currently ELFIO succeeds in parsing the header of a memory-mapped ELF dump file, but fails to parse other elements of the ELF file that are present also in memory such as segment headers and .dynsym symbols.

For some of these structures no changes in parsing code need to be made since fields in the header point to them even in memory (segment headers, for example). For others (symbols, for example), different parsing logic is needed so I suggest adding a flag to elfio's load() function that specifies if the elf passed to it was dumped from memory or not.

Note description contains garbage characters

In get_note() in elfio_note.hpp a mathematical operation is done to calculate the location of the description buffer, but the resulting buffer contains only garbage characters (while readelf's output (-n flag`) provides sane output on the same file. This might indicate a problem with the calculation.

Also, since get_note() receives a void*& desc (instead of void*) to point to the note's description, the use of reinterpret_cast<void*>(some_string.data()) is prevented. A better API might be to just get desc as std::string& and encapsulate the use of void* inside get_note().

Last thing - the note's description is not used in dump::notes, which is a shame since it is already parsed from the note anyway.

I/O processing is not consistent

I noticed that ELFIO produces inconsistent elf binaries in some cases. Here is a small test case:

  1. Tweak elfdump sample to simply read an ELF file and then write a copy to another file:
#include <iostream>
#include <elfio/elfio.hpp>

using namespace ELFIO;

int main( int argc, char** argv )
{
    if ( argc != 3 ) {
        printf( "Usage: elfdump <infile_name> <outfile_name> \n" );
        return 1;
    }

    elfio reader;

    if ( !reader.load( argv[1] ) ) {
        printf( "File %s is not found or it is not an ELF file\n", argv[1] );
        return 1;
    }
    // Create ELF file
    if( !reader.save( argv[2]) ) {
	printf( "File %s cannot be saved\n", argv[2] );
        return 1;	
    }
    return 0;
}
  1. Create a sample ELF file for testing:
$ cat test.c
`int main(void) { return 0; }`
$ avr-gcc test.c -o test.elf
  1. Execute I/O test: $ ./elfdump test.elf test1.elf

  2. Expect that both ELF files are equal, but they are different:

$ cmp -b test.elf test1.elf
test.elf test1.elf differ: byte 153, line 1 is   0 ^@  56 .

Cannot save to in-memory stream buffer

So strange that save() only works correctly with the output file, but not with an in-memory buffer. When I try to save elf into a stringstream, only first 64 bytes are written:

std::stringstream ss(std::ios::out);
e.save(ss);
std::string elf = ss.str(); // only first 64 bytes always!

Any clue?

Documentation seems out of sync

e.g. Segments::get_data() exists (and is in fact used in one of the examples), but is not mentioned in the list of public member functions of the class.

mmap instead of streams

Have you ever considered using mmap under the hood to avoid steaming the entire ELF file into memory at load time?

elfio doesn't seem to be able to read notes from coredump

While readelf -n and objdump -h works, elfio doesn't seem to be able to auto-parse (or, rather, auto-create) note sections from note segments.

Right now i'm using this workaround:

ELFIO::Elf_Half n = reader.segments.size();

        for ( ELFIO::Elf_Half i = 0; i < n; ++i )
        {
            ELFIO::segment* seg = reader.segments[i];
            if (seg->get_type() == PT_NOTE)
            {
                std::ostringstream name;
                name << "note" << (int)i;
                ELFIO::section* note_sec = reader.sections.add(name.str());
                note_sec->set_type( SHT_NOTE );
                note_sec->set_data( seg->get_data(), seg->get_file_size() );
            }
        }

Multi-line comments

Backslashes at the end of comments related to AMDGPU defines break the build and cannot be addressed with pragma's at the user level.

elfio/elfio/elf_types.hpp:398:8: error: multi-line comment [-Werror=comment]
     64 // AMDGPU OS for HSA compatible compute \
        ^
elfio/elfio/elf_types.hpp:401:8: error: multi-line comment [-Werror=comment]
     65 // AMDGPU OS for AMD PAL compatible graphics \
        ^
elfio/elfio/elf_types.hpp:404:8: error: multi-line comment [-Werror=comment]
     66 // AMDGPU OS for Mesa3D compatible graphics \
        ^
elfio/elfio/elf_types.hpp:410:11: error: multi-line comment [-Werror=comment]
     0x100 // Indicates if the XNACK target feature is \

Separate reader and writer

Hi, I've been experimenting with ELFIO and I have more-or-less trying to read in an ELF and the write out again using a separate instance of ELFIO::elfio but there appears to be an issue with the headers.:

// error checking and asserts omitted in the interest of brevity

using Section = ELFIO::section;
using Segment = ELFIO::segment;

void read_write(std::string in, std::string out)
{
    ELFIO::elfio reader{};
    ELFIO::elfio writer{};

    reader.load(in);

    writer.create(reader.get_class(), reader.get_encoding());
    writer.set_os_abi(reader.get_os_abi());
    writer.set_type(reader.get_type());
    writer.set_machine(reader.get_machine());
    writer.set_abi_version(reader.get_abi_version());
    
    std::vector<std::pair<Section*, Section*>> _sections{};
    std::vector<std::pair<Segment*, Segment*>> _segments{};

    for(size_t i = 0; i < reader.sections.size(); ++i)
        _sections.emplace_back(reader.sections[i],
                               writer.sections.add(reader.sections[i]->get_name()));

    for(size_t i = 0; i < reader.segments.size(); ++i)
        _segments.emplace_back(reader.segments[i], writer.segments.add());

    for(auto& itr : _sections)
    {
        auto* _read  = itr.first;
        auto* _write = itr.second;

        _write->set_type(_read->get_type());
        _write->set_flags(_read->get_flags());
        _write->set_addr_align(_read->get_addr_align());
        _write->set_link(_read->get_link());
        _write->set_info(_read->get_info());
        _write->set_entry_size(_read->get_entry_size());
        _write->set_name_string_offset(_read->get_name_string_offset());
        _write->set_data(_read->get_data(), _read->get_size());
    }

    for(auto& itr : _segments)
    {
        auto* _read  = itr.first;
        auto* _write = itr.second;

        _write->set_type(_read->get_type());
        _write->set_flags(_read->get_flags());
        _write->set_align(_read->get_align());
        for(size_t i = 0; i < _read->get_sections_num(); ++i)
        {
            Section* _read_sec = reader.sections[_read->get_section_index_at(i)];
            auto witr =
                std::find_if(writer.sections.begin(), writer.sections.end(),
                             [_read_sec](const ELFIO::section* _wsec) {
                                 return _wsec->get_name() == _read_sec->get_name();
                             });
            Section* _write_sec = *witr;
            _write->add_section(_write_sec, _write_sec->get_addr_align());
        }
    }

    writer.set_entry(reader.get_entry());

    writer.save(_out);
}

However, the ELF is invalid. Here is a diff of readelf -a -W exe vs. readelf -a -W exe.mod:

$ readelf -a -W ./exe &> dump.exe && readelf -a -W ./exe.mod &> dump.exe.mod && diff -y -W 200 ./dump.exe ./dump.exe.mod
ELF Header:                                                                                             ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00                                                Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64                                                                Class:                             ELF64
  Data:                              2's complement, little endian                                        Data:                              2's complement, little endian
  Version:                           1 (current)                                                          Version:                           1 (current)
  OS/ABI:                            UNIX - System V                                                      OS/ABI:                            UNIX - System V
  ABI Version:                       0                                                                    ABI Version:                       0
  Type:                              EXEC (Executable file)                                               Type:                              EXEC (Executable file)
  Machine:                           Advanced Micro Devices X86-64                                        Machine:                           Advanced Micro Devices X86-64
  Version:                           0x1                                                                  Version:                           0x1
  Entry point address:               0x242a10                                                             Entry point address:               0x242a10
  Start of program headers:          64 (bytes into file)                                                 Start of program headers:          64 (bytes into file)
  Start of section headers:          338336 (bytes into file)                                      |      Start of section headers:          350396 (bytes into file)
  Flags:                             0x0                                                                  Flags:                             0x0
  Size of this header:               64 (bytes)                                                           Size of this header:               64 (bytes)
  Size of program headers:           56 (bytes)                                                           Size of program headers:           56 (bytes)
  Number of program headers:         11                                                                   Number of program headers:         11
  Size of section headers:           64 (bytes)                                                           Size of section headers:           64 (bytes)
  Number of section headers:         36                                                            |      Number of section headers:         38
  Section header string table index: 34                                                            |      Section header string table index: 1

Section Headers:                                                                                        Section Headers:
  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al                  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0                  [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
  [ 1] .interp           PROGBITS        00000000002002a8 0002a8 00001c 00   A  0   0  1           |      [ 1] .shstrtab         STRTAB          0000000000000000 045088 00016a 00      0   0  1
  [ 2] .note.ABI-tag     NOTE            00000000002002c4 0002c4 000020 00   A  0   0  4           |      [ 2]                   NULL            0000000000000000 0451f2 000000 00      0   0  0
  [ 3] .dynsym           DYNSYM          00000000002002e8 0002e8 000198 18   A  7   1  8           |      [ 3] .shstrtab         PROGBITS        0000000000000000 001000 00001c 00   A  0   0  1
  [ 4] .gnu.version      VERSYM          0000000000200480 000480 000022 02   A  3   0  2           |      [ 4] b                 NOTE            000000000000001c 00101c 000020 00   A  0   0  4
  [ 5] .gnu.version_r    VERNEED         00000000002004a4 0004a4 000040 00   A  7   2  4           |    readelf: Warning: [ 5]: Link field (7) should index a string section.
  [ 6] .gnu.hash         GNU_HASH        00000000002004e8 0004e8 00001c 00   A  3   0  8           |      [ 5] te.ABI-tag        DYNSYM          0000000000000040 001040 000198 18   A  7   1  8
  [ 7] .dynstr           STRTAB          0000000000200504 000504 0001d5 00   A  0   0  1           |    readelf: Warning: [ 6]: Link field (3) should index a symtab section.
  [ 8] .rela.dyn         RELA            00000000002006e0 0006e0 000030 18   A  3   0  8           |      [ 6] ag                VERSYM          00000000000001d8 0011d8 000022 02   A  3   0  2
  [ 9] .rela.plt         RELA            0000000000200710 000710 000120 18  AI  3  25  8           |    readelf: Warning: [ 7]: Link field (7) should index a string section.
  [10] .rodata           PROGBITS        0000000000200830 000830 000049 00 AMS  0   0  4           |      [ 7] nu.version        VERNEED         00000000000001fc 0011fc 000040 00   A  7   2  4
  [11] .hip_fatbin       PROGBITS        0000000000201000 001000 040779 00   A  0   0 4096         |    readelf: Warning: [ 8]: Link field (3) should index a symtab section.
  [12] .hipFatBinSegment PROGBITS        0000000000241780 041780 000030 00   A  0   0  8           |      [ 8] .version_r        GNU_HASH        0000000000000240 001240 00001c 00   A  3   0  8
  [13] .eh_frame_hdr     PROGBITS        00000000002417b0 0417b0 00007c 00   A  0   0  4           |      [ 9]                   STRTAB          000000000000025c 00125c 0001d5 00   A  0   0  1
  [14] .eh_frame         PROGBITS        0000000000241830 041830 0001d4 00   A  0   0  8           |    readelf: Warning: [10]: Link field (3) should index a symtab section.
  [15] .text             PROGBITS        0000000000242a10 041a10 000522 00  AX  0   0 16           |      [10] sh                RELA            0000000000000438 001438 000030 18   A  3   0  8
  [16] .init             PROGBITS        0000000000242f34 041f34 00001b 00  AX  0   0  4           |    readelf: Warning: [11]: Link field (3) should index a symtab section.
  [17] .fini             PROGBITS        0000000000242f50 041f50 00000d 00  AX  0   0  4           |      [11]                   RELA            0000000000000468 001468 000120 18  AI  3  25  8
  [18] .plt              PROGBITS        0000000000242f60 041f60 0000d0 00  AX  0   0 16           |      [12]                   PROGBITS        0000000000000588 001588 000049 00 AMS  0   0  4
  [19] .fini_array       FINI_ARRAY      0000000000244030 042030 000008 08  WA  0   0  8           |      [13] lt                PROGBITS        0000000000001000 002000 040779 00   A  0   0 4096
  [20] .init_array       INIT_ARRAY      0000000000244038 042038 000018 00  WA  0   0  8           |      [14] hip_fatbin        PROGBITS        0000000000041780 042780 000030 00   A  0   0  8
  [21] .dynamic          DYNAMIC         0000000000244050 042050 0001f0 10  WA  7   0  8           |      [15] BinSegment        PROGBITS        00000000000417b0 0427b0 00007c 00   A  0   0  4
  [22] .got              PROGBITS        0000000000244240 042240 000010 00  WA  0   0  8           |      [16] _frame_hdr        PROGBITS        0000000000041830 042830 0001d4 00   A  0   0  8
  [23] .data             PROGBITS        0000000000245250 042250 000010 00  WA  0   0  8           |      [17]                   PROGBITS        0000000000000000 043000 000522 00  AX  0   0 16
  [24] .tm_clone_table   PROGBITS        0000000000245260 042260 000000 00  WA  0   0  8           |      [18] rame              PROGBITS        0000000000000524 043524 00001b 00  AX  0   0  4
  [25] .got.plt          PROGBITS        0000000000245260 042260 000078 00  WA  0   0  8           |      [19] text              PROGBITS        0000000000000540 043540 00000d 00  AX  0   0  4
  [26] .bss              NOBITS          00000000002452d8 0422d8 000018 00  WA  0   0  8           |      [20] init              PROGBITS        0000000000000550 043550 0000d0 00  AX  0   0 16
  [27] .comment          PROGBITS        0000000000000000 0422d8 00010b 01  MS  0   0  1           |      [21] .fini             FINI_ARRAY      0000000000000000 044000 000008 08  WA  0   0  8
  [28] .debug_loc        PROGBITS        0000000000000000 0423e3 0001a6 00      0   0  1           |      [22] fini_array        INIT_ARRAY      0000000000000008 044008 000018 00  WA  0   0  8
  [29] .debug_abbrev     PROGBITS        0000000000000000 042589 000960 00      0   0  1           |    readelf: Warning: [23]: Link field (7) should index a string section.
  [30] .debug_info       PROGBITS        0000000000000000 042ee9 00be2c 00      0   0  1           |      [23] init_array        DYNAMIC         0000000000000020 044020 0001f0 10  WA  7   0  8
  [31] .debug_str        PROGBITS        0000000000000000 04ed15 002394 01  MS  0   0  1           |      [24] y                 PROGBITS        0000000000000210 044210 000010 00  WA  0   0  8
  [32] .debug_line       PROGBITS        0000000000000000 0510a9 000e2e 00      0   0  1           |      [25] namic             PROGBITS        0000000000000000 045000 000010 00  WA  0   0  8
  [33] .symtab           SYMTAB          0000000000000000 051ed8 0005a0 18     35  33  8           |      [26] .got              PROGBITS        0000000000000010 045010 000000 00  WA  0   0  8
  [34] .shstrtab         STRTAB          0000000000000000 052478 00015f 00      0   0  1           |      [27] lone_table        PROGBITS        0000000000000010 045010 000078 00  WA  0   0  8
  [35] .strtab           STRTAB          0000000000000000 0525d7 0003c5 00      0   0  1           |      [28] e                 NOBITS          0000000000000088 045088 000000 00  WA  0   0  8
                                                                                                   >      [29] t.plt             PROGBITS        0000000000000000 0451f2 00010b 01  MS  0   0  1
                                                                                                   >      [30] s                 PROGBITS        0000000000000000 0452fd 0001a6 00      0   0  1
                                                                                                   >      [31] .debug_loc        PROGBITS        0000000000000000 0454a3 000960 00      0   0  1
                                                                                                   >      [32] bug_abbrev        PROGBITS        0000000000000000 045e03 00be2c 00      0   0  1
                                                                                                   >      [33] debug_info        PROGBITS        0000000000000000 051c2f 002394 01  MS  0   0  1
                                                                                                   >      [34] .debug_str        PROGBITS        0000000000000000 053fc3 000e2e 00      0   0  1
                                                                                                   >    readelf: Warning: [35]: Link field (35) should index a string section.
                                                                                                   >      [35] debug_line        SYMTAB          0000000000000000 054df8 0005a0 18     35  33  8
                                                                                                   >      [36] ne                STRTAB          0000000000000000 055398 00015f 00      0   0  1
                                                                                                   >      [37]                   STRTAB          0000000000000000 0554f7 0003c5 00      0   0  1
Key to Flags:                                                                                           Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),                                    W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),                                   L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),                                              C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)                                                                       l (large), p (processor specific)

There are no section groups in this file.                                                               There are no section groups in this file.

Program Headers:                                                                                        Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align               Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000200040 0x0000000000200040 0x000268 0x000268 R   0x8          |      PHDR           0x000040 0x0000000000000000 0x0000000000000000 0x000268 0x000268 R   0x8
  INTERP         0x0002a8 0x00000000002002a8 0x00000000002002a8 0x00001c 0x00001c R   0x1          |    readelf: Error: the PHDR segment is not covered by a LOAD segment
                                                                                                   >      INTERP         0x001000 0x0000000000000000 0x0000000000000000 0x00001c 0x00001c R   0x1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]                                           [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x000000 0x0000000000200000 0x0000000000200000 0x041a04 0x041a04 R   0x1000       |      LOAD           0x001000 0x0000000000000000 0x0000000000000000 0x041a04 0x041a04 R   0x1000
  LOAD           0x041a10 0x0000000000242a10 0x0000000000242a10 0x000620 0x000620 R E 0x1000       |      LOAD           0x043000 0x0000000000000000 0x0000000000000000 0x000620 0x000620 R E 0x1000
  LOAD           0x042030 0x0000000000244030 0x0000000000244030 0x000220 0x000220 RW  0x1000       |      LOAD           0x044000 0x0000000000000000 0x0000000000000000 0x000220 0x000220 RW  0x1000
  LOAD           0x042250 0x0000000000245250 0x0000000000245250 0x000088 0x0000a0 RW  0x1000       |      LOAD           0x045000 0x0000000000000000 0x0000000000000000 0x000088 0x000088 RW  0x1000
  DYNAMIC        0x042050 0x0000000000244050 0x0000000000244050 0x0001f0 0x0001f0 RW  0x8          |      DYNAMIC        0x044020 0x0000000000000000 0x0000000000000000 0x0001f0 0x0001f0 RW  0x8
  GNU_RELRO      0x042030 0x0000000000244030 0x0000000000244030 0x000220 0x000fd0 R   0x1          |    readelf: Error: no .dynamic section in the dynamic segment
  GNU_EH_FRAME   0x0417b0 0x00000000002417b0 0x00000000002417b0 0x00007c 0x00007c R   0x4          |      GNU_RELRO      0x044000 0x0000000000000000 0x0000000000000000 0x000220 0x000220 R   0x8
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0            |      GNU_EH_FRAME   0x0427b0 0x0000000000000000 0x0000000000000000 0x00007c 0x00007c R   0x4
  NOTE           0x0002c4 0x00000000002002c4 0x00000000002002c4 0x000020 0x000020 R   0x4          |      GNU_STACK      0x045088 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0
                                                                                                   >      NOTE           0x00101c 0x0000000000000000 0x0000000000000000 0x000020 0x000020 R   0x4

 Section to Segment mapping:                                                                             Section to Segment mapping:
  Segment Sections...                                                                                     Segment Sections...
   00                                                                                                      00     
   01     .interp                                                                                  |       01     .shstrtab 
   02     .interp .note.ABI-tag .dynsym .gnu.version .gnu.version_r .gnu.hash .dynstr .rela.dyn    |       02     .shstrtab b te.ABI-tag ag nu.version .version_r  sh   lt hip_fatbin BinSegment _frame_
   03     .text .init .fini .plt                                                                   |       03      rame text init e 
   04     .fini_array .init_array .dynamic .got                                                    |       04     .fini fini_array init_array y e 
   05     .data .tm_clone_table .got.plt .bss                                                      |       05     namic .got lone_table 
   06     .dynamic                                                                                 |       06     e 
   07     .fini_array .init_array .dynamic .got                                                    |       07     .fini fini_array init_array y e 
   08     .eh_frame_hdr                                                                            |       08     
   09                                                                                                      09     
   10     .note.ABI-tag                                                                            |       10     
                                                                                                   >    readelf: Warning: Virtual address 0x200504 not located in any PT_LOAD segment.
                                                                                                   >    readelf: Error: Unable to determine the length of the dynamic string table

Dynamic section at offset 0x42050 contains 31 entries:                                             |    Dynamic section at offset 0x44020 contains 31 entries:
  Tag        Type                         Name/Value                                                      Tag        Type                         Name/Value
 0x000000000000001d (RUNPATH)            Library runpath: [/opt/rocm/lib:/opt/rocm-4.3.0/hip/lib   |     0x000000000000001d (RUNPATH)            0x159
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]                           |     0x0000000000000001 (NEEDED)             0x193
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]                         |     0x0000000000000001 (NEEDED)             0x1a1
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]                               |     0x0000000000000001 (NEEDED)             0x1b1
 0x0000000000000001 (NEEDED)             Shared library: [librt.so.1]                              |     0x0000000000000001 (NEEDED)             0x1bb
 0x0000000000000001 (NEEDED)             Shared library: [libamdhip64.so.4]                        |     0x0000000000000001 (NEEDED)             0x12a
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]                          |     0x0000000000000001 (NEEDED)             0x1c6
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]                               |     0x0000000000000001 (NEEDED)             0x143
 0x0000000000000015 (DEBUG)              0x0                                                             0x0000000000000015 (DEBUG)              0x0
 0x0000000000000007 (RELA)               0x2006e0                                                        0x0000000000000007 (RELA)               0x2006e0
 0x0000000000000008 (RELASZ)             48 (bytes)                                                      0x0000000000000008 (RELASZ)             48 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)                                                      0x0000000000000009 (RELAENT)            24 (bytes)
 0x0000000000000017 (JMPREL)             0x200710                                                        0x0000000000000017 (JMPREL)             0x200710
 0x0000000000000002 (PLTRELSZ)           288 (bytes)                                                     0x0000000000000002 (PLTRELSZ)           288 (bytes)
 0x0000000000000003 (PLTGOT)             0x245260                                                        0x0000000000000003 (PLTGOT)             0x245260
 0x0000000000000014 (PLTREL)             RELA                                                            0x0000000000000014 (PLTREL)             RELA
 0x0000000000000006 (SYMTAB)             0x2002e8                                                        0x0000000000000006 (SYMTAB)             0x2002e8
 0x000000000000000b (SYMENT)             24 (bytes)                                                      0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000005 (STRTAB)             0x200504                                                        0x0000000000000005 (STRTAB)             0x200504
 0x000000000000000a (STRSZ)              469 (bytes)                                                     0x000000000000000a (STRSZ)              469 (bytes)
 0x000000006ffffef5 (GNU_HASH)           0x2004e8                                                        0x000000006ffffef5 (GNU_HASH)           0x2004e8
 0x0000000000000019 (INIT_ARRAY)         0x244038                                                        0x0000000000000019 (INIT_ARRAY)         0x244038
 0x000000000000001b (INIT_ARRAYSZ)       24 (bytes)                                                      0x000000000000001b (INIT_ARRAYSZ)       24 (bytes)
 0x000000000000001a (FINI_ARRAY)         0x244030                                                        0x000000000000001a (FINI_ARRAY)         0x244030
 0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)                                                       0x000000000000001c (FINI_ARRAYSZ)       8 (bytes)
 0x000000000000000c (INIT)               0x242f34                                                        0x000000000000000c (INIT)               0x242f34
 0x000000000000000d (FINI)               0x242f50                                                        0x000000000000000d (FINI)               0x242f50
 0x000000006ffffff0 (VERSYM)             0x200480                                                        0x000000006ffffff0 (VERSYM)             0x200480
 0x000000006ffffffe (VERNEED)            0x2004a4                                                        0x000000006ffffffe (VERNEED)            0x2004a4
 0x000000006fffffff (VERNEEDNUM)         2                                                               0x000000006fffffff (VERNEEDNUM)         2
 0x0000000000000000 (NULL)               0x0                                                             0x0000000000000000 (NULL)               0x0

Relocation section '.rela.dyn' at offset 0x6e0 contains 2 entries:                                 |    Relocation section 'sh' at offset 0x1438 contains 2 entries:
    Offset             Info             Type               Symbol's Value  Symbol's Name + Adden   |
0000000000244240  0000000100000006 R_X86_64_GLOB_DAT      0000000000000000 __libc_start_main@GLI   |    Relocation section '' at offset 0x1468 contains 12 entries:
0000000000244248  0000000200000006 R_X86_64_GLOB_DAT      0000000000000000 __gmon_start__ + 0      |
                                                                                                   |    There are no static relocations in this file.
Relocation section '.rela.plt' at offset 0x710 contains 12 entries:                                |    To see the dynamic relocations add --use-dynamic to the command line.
    Offset             Info             Type               Symbol's Value  Symbol's Name + Adden   <
0000000000245278  0000000500000007 R_X86_64_JUMP_SLOT     0000000000000000 __hipPopCallConfigura   <
0000000000245280  0000000600000007 R_X86_64_JUMP_SLOT     0000000000000000 hipLaunchKernel@hip_4   <
0000000000245288  0000000700000007 R_X86_64_JUMP_SLOT     0000000000000000 printf@GLIBC_2.2.5 +    <
0000000000245290  0000000800000007 R_X86_64_JUMP_SLOT     0000000000000000 __hipPushCallConfigur   <
0000000000245298  0000000900000007 R_X86_64_JUMP_SLOT     0000000000000000 hipMalloc@hip_4.2 + 0   <
00000000002452a0  0000000a00000007 R_X86_64_JUMP_SLOT     0000000000000000 hipMemset@hip_4.2 + 0   <
00000000002452a8  0000000b00000007 R_X86_64_JUMP_SLOT     0000000000000000 hipMemcpy@hip_4.2 + 0   <
00000000002452b0  0000000c00000007 R_X86_64_JUMP_SLOT     0000000000000000 hipDeviceSynchronize@   <
00000000002452b8  0000000d00000007 R_X86_64_JUMP_SLOT     0000000000000000 __hipRegisterFatBinar   <
00000000002452c0  0000000e00000007 R_X86_64_JUMP_SLOT     0000000000000000 __hipRegisterFunction   <
00000000002452c8  0000000f00000007 R_X86_64_JUMP_SLOT     0000000000000000 __hipUnregisterFatBin   <
00000000002452d0  0000001000000007 R_X86_64_JUMP_SLOT     0000000000000000 __cxa_atexit@GLIBC_2.   <

The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently         The decoding of unwind sections for machine type Advanced Micro Devices X86-64 is not currently 
                                                                                                   >    readelf: Warning: Virtual address 0x2004e8 not located in any PT_LOAD segment.
                                                                                                   >    readelf: Error: Failed to read in number of buckets

Symbol table '.dynsym' contains 17 entries:                                                        |    Symbol table 'te.ABI-tag' contains 17 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name                                            Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND                                         |         0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND ^Areadelf: Warning: Virtual address 0
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)       |    readelf: Error: Reading 2 bytes extends past end of file for version data
     2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__                          |
     3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTable             |         1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND readelf: Warning: Virtual address 0x2
     4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable               |    readelf: Error: Reading 2 bytes extends past end of file for version data
     5: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipPopCallConfiguration@hip_4.2 (3)   |
     6: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipLaunchKernel@hip_4.2 (3)             |         2: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND readelf: Warning: Virtual address 0x2
     7: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf@GLIBC_2.2.5 (2)                  |    readelf: Error: Reading 2 bytes extends past end of file for version data
     8: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipPushCallConfiguration@hip_4.2 (3   |
     9: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipMalloc@hip_4.2 (3)                   |         3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND ^F^Oreadelf: Warning: Virtual address
    10: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipMemset@hip_4.2 (3)                   |    readelf: Error: Reading 2 bytes extends past end of file for version data
    11: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipMemcpy@hip_4.2 (3)                   |
    12: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipDeviceSynchronize@hip_4.2 (3)        |         4: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND readelf: Warning: Virtual address 0x2
    13: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipRegisterFatBinary@hip_4.2 (3)      |    readelf: Error: Reading 2 bytes extends past end of file for version data
    14: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipRegisterFunction@hip_4.2 (3)       |
    15: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipUnregisterFatBinary@hip_4.2 (3)    |         5: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
    16: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_atexit@GLIBC_2.2.5 (2)            |    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >         6: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >         7: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >         8: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >         9: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >        10: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >        11: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >        12: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >        13: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >        14: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >        15: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >
                                                                                                   >        16: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND <corrupt>readelf: Warning: Virtual ad
                                                                                                   >    readelf: Error: Reading 2 bytes extends past end of file for version data
                                                                                                   >

Symbol table '.symtab' contains 60 entries:                                                        |    Symbol table 'debug_line' contains 60 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name                                            Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND                                                   0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c                              |         1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
     2: 0000000000245260     0 OBJECT  LOCAL  DEFAULT   24 __TMC_LIST__                            |         2: 0000000000245260     0 OBJECT  LOCAL  DEFAULT   24 
     3: 0000000000242a50     0 FUNC    LOCAL  DEFAULT   15 deregister_tm_clones                    |         3: 0000000000242a50     0 FUNC    LOCAL  DEFAULT   15 
     4: 0000000000242a80     0 FUNC    LOCAL  DEFAULT   15 register_tm_clones                      |         4: 0000000000242a80     0 FUNC    LOCAL  DEFAULT   15 
     5: 0000000000242ac0     0 FUNC    LOCAL  DEFAULT   15 __do_global_dtors_aux                   |         5: 0000000000242ac0     0 FUNC    LOCAL  DEFAULT   15 
     6: 00000000002452d8     1 OBJECT  LOCAL  DEFAULT   26 completed.0                             |         6: 00000000002452d8     1 OBJECT  LOCAL  DEFAULT   26 
     7: 0000000000244030     0 OBJECT  LOCAL  DEFAULT   19 __do_global_dtors_aux_fini_array_entr   |         7: 0000000000244030     0 OBJECT  LOCAL  DEFAULT   19 
     8: 0000000000242af0     0 FUNC    LOCAL  DEFAULT   15 frame_dummy                             |         8: 0000000000242af0     0 FUNC    LOCAL  DEFAULT   15 
     9: 0000000000244038     0 OBJECT  LOCAL  DEFAULT   20 __frame_dummy_init_array_entry          |         9: 0000000000244038     0 OBJECT  LOCAL  DEFAULT   20 ^Z
    10: 0000000000245258     0 OBJECT  LOCAL  HIDDEN    23 __dso_handle                            |        10: 0000000000245258     0 OBJECT  LOCAL  HIDDEN    23 
    11: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS source2.hip                             |        11: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
    12: 0000000000242d20    91 FUNC    LOCAL  DEFAULT   15 __hip_module_ctor                       |        12: 0000000000242d20    91 FUNC    LOCAL  DEFAULT   15 
    13: 00000000002452e0     8 OBJECT  LOCAL  DEFAULT   26 __hip_gpubin_handle                     |        13: 00000000002452e0     8 OBJECT  LOCAL  DEFAULT   26 
    14: 0000000000241780    24 OBJECT  LOCAL  DEFAULT   12 __hip_fatbin_wrapper                    |        14: 0000000000241780    24 OBJECT  LOCAL  DEFAULT   12 
    15: 0000000000242d80    31 FUNC    LOCAL  DEFAULT   15 __hip_module_dtor                       |        15: 0000000000242d80    31 FUNC    LOCAL  DEFAULT   15 
    16: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS source1.hip                             |        16: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS ��
    17: 0000000000242e00    91 FUNC    LOCAL  DEFAULT   15 __hip_module_ctor                       |        17: 0000000000242e00    91 FUNC    LOCAL  DEFAULT   15 
    18: 00000000002452e8     8 OBJECT  LOCAL  DEFAULT   26 __hip_gpubin_handle                     |        18: 00000000002452e8     8 OBJECT  LOCAL  DEFAULT   26 
    19: 0000000000241798    24 OBJECT  LOCAL  DEFAULT   12 __hip_fatbin_wrapper                    |        19: 0000000000241798    24 OBJECT  LOCAL  DEFAULT   12 
    20: 0000000000242e60    31 FUNC    LOCAL  DEFAULT   15 __hip_module_dtor                       |        20: 0000000000242e60    31 FUNC    LOCAL  DEFAULT   15 
    21: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS inst.hip                                |        21: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
    22: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c                              |        22: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 
    23: 0000000000241830     0 OBJECT  LOCAL  DEFAULT   14 __FRAME_END__                           |        23: 0000000000241830     0 OBJECT  LOCAL  DEFAULT   14 
    24: 0000000000245260     0 OBJECT  LOCAL  HIDDEN    24 __TMC_END__                             |        24: 0000000000245260     0 OBJECT  LOCAL  HIDDEN    24 
    25: 0000000000242a40     5 FUNC    LOCAL  HIDDEN    15 _dl_relocate_static_pie                 |        25: 0000000000242a40     5 FUNC    LOCAL  HIDDEN    15 R$
    26: 0000000000245260     0 NOTYPE  LOCAL  HIDDEN    25 _GLOBAL_OFFSET_TABLE_                   |        26: 0000000000245260     0 NOTYPE  LOCAL  HIDDEN    25 ^N^A
    27: 0000000000244038     0 NOTYPE  LOCAL  HIDDEN    20 __init_array_start                      |        27: 0000000000244038     0 NOTYPE  LOCAL  HIDDEN    20 0^H 
    28: 0000000000244050     0 NOTYPE  LOCAL  HIDDEN    20 __init_array_end                        |        28: 0000000000244050     0 NOTYPE  LOCAL  HIDDEN    20 
    29: 0000000000244050     0 NOTYPE  LOCAL  HIDDEN    21 _DYNAMIC                                |        29: 0000000000244050     0 NOTYPE  LOCAL  HIDDEN    21 
    30: 0000000000242f34     0 FUNC    LOCAL  HIDDEN    16 _init                                   |        30: 0000000000242f34     0 FUNC    LOCAL  HIDDEN    16 
    31: 0000000000242f50     0 FUNC    LOCAL  HIDDEN    17 _fini                                   |        31: 0000000000242f50     0 FUNC    LOCAL  HIDDEN    17 
    32: 0000000000242f20    18 FUNC    LOCAL  HIDDEN    15 atexit                                  |        32: 0000000000242f20    18 FUNC    LOCAL  HIDDEN    15 
    33: 0000000000242f10     5 FUNC    GLOBAL DEFAULT   15 __libc_csu_fini                         |        33: 0000000000242f10     5 FUNC    GLOBAL DEFAULT   15 
    34: 0000000000242a10    47 FUNC    GLOBAL DEFAULT   15 _start                                  |        34: 0000000000242a10    47 FUNC    GLOBAL DEFAULT   15 ^W$
    35: 0000000000242ea0   101 FUNC    GLOBAL DEFAULT   15 __libc_csu_init                         |        35: 0000000000242ea0   101 FUNC    GLOBAL DEFAULT   15 ^X
    36: 0000000000242b60   434 FUNC    GLOBAL DEFAULT   15 main                                    |        36: 0000000000242b60   434 FUNC    GLOBAL DEFAULT   15 �-$
    37: 0000000000245250     0 NOTYPE  WEAK   DEFAULT   23 data_start                              |        37: 0000000000245250     0 NOTYPE  WEAK   DEFAULT   23 
    38: 0000000000200830     4 OBJECT  GLOBAL DEFAULT   10 _IO_stdin_used                          |        38: 0000000000200830     4 OBJECT  GLOBAL DEFAULT   10 
    39: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main                       |        39: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    40: 0000000000245250     0 NOTYPE  GLOBAL DEFAULT   23 __data_start                            |        40: 0000000000245250     0 NOTYPE  GLOBAL DEFAULT   23 
    41: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__                          |        41: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND 
    42: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_deregisterTMCloneTable             |        42: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND 
    43: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND _ITM_registerTMCloneTable               |        43: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND 
    44: 0000000000242b00    90 FUNC    GLOBAL DEFAULT   15 _Z22__device_stub__source2i             |        44: 0000000000242b00    90 FUNC    GLOBAL DEFAULT   15 ^N
    45: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipPopCallConfiguration               |        45: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND $
    46: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipLaunchKernel                         |        46: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    47: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND printf                                  |        47: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    48: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipPushCallConfiguration              |        48: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    49: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipMalloc                               |        49: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    50: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipMemset                               |        50: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND �^C
    51: 0000000000242da0    88 FUNC    GLOBAL DEFAULT   15 _Z22__device_stub__source1Pi            |        51: 0000000000242da0    88 FUNC    GLOBAL DEFAULT   15 $
    52: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipMemcpy                               |        52: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    53: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND hipDeviceSynchronize                    |        53: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND ^A
    54: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipRegisterFatBinary                  |        54: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    55: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipRegisterFunction                   |        55: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    56: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __hipUnregisterFatBinary                |        56: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
    57: 0000000000242e80     1 FUNC    GLOBAL DEFAULT   15 _ZN11device_data8allocateEmP12ihipStr   |        57: 0000000000242e80     1 FUNC    GLOBAL DEFAULT   15 
    58: 0000000000242e90     1 FUNC    GLOBAL DEFAULT   15 _ZN11device_data10deallocateEP12ihipS   |        58: 0000000000242e90     1 FUNC    GLOBAL DEFAULT   15 
    59: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __cxa_atexit                            |        59: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND 
                                                                                                   |    readelf: Error: Section .shstrtab has an invalid sh_entsize of 0x0
Version symbols section '.gnu.version' contains 17 entries:                                        |
 Addr: 0x0000000000200480  Offset: 0x000480  Link: 3 (.dynsym)                                     |    Version needs section 'nu.version' contains 2 entries:
  000:   0 (*local*)       2 (GLIBC_2.2.5)   1 (*global*)      1 (*global*)                        |     Addr: 0x00000000000001fc  Offset: 0x0011fc  Link: 7 (nu.version)
  004:   1 (*global*)      3 (hip_4.2)       3 (hip_4.2)       2 (GLIBC_2.2.5)                     |      000000: Version: 1  File: 12a  Cnt: 1
  008:   3 (hip_4.2)       3 (hip_4.2)       3 (hip_4.2)       3 (hip_4.2)                         |      0x0020:   Name index: 13b  Flags: none  Version: 3
  00c:   3 (hip_4.2)       3 (hip_4.2)       3 (hip_4.2)       3 (hip_4.2)                         |      0x0010: Version: 1  File: 143  Cnt: 1
  010:   2 (GLIBC_2.2.5)                                                                           |      0x0030:   Name index: 14d  Flags: none  Version: 2
                                                                                                   <
Version needs section '.gnu.version_r' contains 2 entries:                                         <
 Addr: 0x00000000002004a4  Offset: 0x0004a4  Link: 7 (.dynstr)                                     <
  000000: Version: 1  File: libamdhip64.so.4  Cnt: 1                                               <
  0x0020:   Name: hip_4.2  Flags: none  Version: 3                                                 <
  0x0010: Version: 1  File: libc.so.6  Cnt: 1                                                      <
  0x0030:   Name: GLIBC_2.2.5  Flags: none  Version: 2                                             <

Displaying notes found in: .note.ABI-tag                                                           |    Displaying notes found in: b
  Owner                Data size        Description                                                       Owner                Data size        Description
  GNU                  0x00000010       NT_GNU_ABI_TAG (ABI version tag)            OS: Linux, A          GNU                  0x00000010       NT_GNU_ABI_TAG (ABI version tag)            OS: Linux, A

64 bit writer example: ELF load command alignment not page-aligned

Hi,

I tried https://github.com/serge1/ELFIO/blob/master/examples/writer/writer.cpp with changes to make it 64 bit, save to .so and dlopen it. Get the error: ELF load command alignment not page-aligned

https://github.com/serge1/ELFIO/blob/master/examples/writer/writer.cpp#L10 to:

    writer.create( ELFCLASS64, ELFDATA2LSB );

    writer.set_os_abi( ELFOSABI_LINUX );
    writer.set_type( ET_DYN );
    writer.set_machine( EM_X86_64 );

...
    // Create ELF file
    writer.save( "./tests/examples/libxml.so" );
    int* handle=(int*)dlopen("./tests/examples/libxml.so", RTLD_LAZY);
    if (!handle) {
        std::cerr << "Cannot open library: " << dlerror() << '\n';
    }
    else dlclose(handle);

Searching the interwebs doesn't turn up what it means. Is there something else I need to change?

Loaded, modified, saved elf file is more different than expected from original

Hello, I'm don't know a lot of details about the .elf format, so maybe there can be a good reason for it.

I have a bare metal project for ARM Cortex M, using the ARM GCC compiler toolchain for that.
So I use ELFIO load a produced .elf file, search for one section by name, call set_data() on that section to change the content (size should remain the same), and call .save() on that elfio object.
The changed section is only a few uint32's big.

Looking at the resulting file in the hex editor, I find things like big blocks (kilobytes maybe) of zeroes where the original file had data.
Also, the saved file is now 6KB bigger than the original one.

Can there be legitimate reasons for these observations, or is something wrong? (I've not tried using it yet)

Edit/Remove Sections

Hi,

Really appreciate your ELFIO and the great documentation! As far as I can see there is no API to remove a section or edit a section? Is this possible or am I overlooking something?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.