serge1 / ELFIO

ELFIO - ELF (Executable and Linkable Format) reader and producer implemented as a header only C++ library
http://serge1.github.io/ELFIO
MIT License
720 stars 155 forks source link

I/O processing is not consistent #66

Closed orbitcowboy closed 3 years ago

orbitcowboy commented 3 years ago

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;
}

2) Create a sample ELF file for testing:

$ cat test.c
`int main(void) { return 0; }`
$ avr-gcc test.c -o test.elf

3) Execute I/O test: $ ./elfdump test.elf test1.elf

4) 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 .
serge1 commented 3 years ago

Hi,

A linker output and a file created by ELFIO library may differ in their content layout. Therefore, binary comparison between the files is not valid.

A better way to compare the files is usage of function checkExeAreEqual() located in ELFIOTest1.cpp.

If you submit your binary file, I can add it to the test directory for regression testing.

Did you try to execute the files?

serge1 commented 3 years ago

Also, please see #62

serge1 commented 3 years ago

Hi @orbitcowboy,

May I mark you question as answered/closed already?

orbitcowboy commented 3 years ago

Also, please see #62 Thank you! This fully answered my question. I did also some verification locally with some µController elf files. Everything seems to work as it should. Many thanks for this very helpful library