If the ELF header which usually can be read using readelf
has been manually manipulated, let's say by increasing the value for the "Size of section headers" the binary still can be executed and works well.
However, this manipulation seems to trip up reverse engineering tools like GDC and GDB gives me the error: not in executable format: File format not recognized
.
Is there a way to fix the ELF header without knowing the original value of "Size of section headers" in order to be able again to analyze the file using standard tools?
Detailed info:
GDB is failing to run the binary because it says the file is not in executable format : File format not recognized
but it works outside the GDB. The same things happen with the libbfd
parser, it can't parse because file format is not recognized. The fact is I only change the number of section headers.
Code
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
Build by invoking make hello
or on a 64-bit system make CFLAGS=-m32 hello
.
ELF header before
$ readelf -h hello
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x8048320
Start of program headers: 52 (bytes into file)
Start of section headers: 4472 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 9
Size of section headers: 40 (bytes)
Number of section headers: 30 <-- notice me!
Section header string table index: 27
ELF header after
$ readelf -h hello
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: EXEC (Executable file)
Machine: Intel 80386
Version: 0x1
Entry point address: 0x8048320
Start of program headers: 52 (bytes into file)
Start of section headers: 4472 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 9
Size of section headers: 40 (bytes)
Number of section headers: 52 <-- already changed!
Section header string table index: 27
GDB will output,
not in executable format: File format not recognized
But if I run it outside of GDB,
$ ./hello output:
Hello World!
So is there either a method to fix the value for e_shnum
without knowing the correct value, or a workaround so I can debug this file in GDB?