movecx
movecx

Reputation: 55

Can the ELF entry point be different from the usual 0x80****** ? Why would that be done?

I'm playing around with a binary and when I load it into my debugger, or even run readelf, I noticed the entry point is 0x530 instead of the usual 0x80****** that'd learned ELF's were loaded at.

Why is this? Is there anything else going on? The binary is linked and not stripped.

Upvotes: 2

Views: 1275

Answers (2)

As mentioned by Employed, the entry address is not fixed.

Just to verify, I've tried on x86_64:

gcc -Wl,-Ttext-segment=0x800000 hello_world.c 

which sets the entry point to 0x800000 (+ the ELF header size, which gets loaded at 0x800000 in memory) instead of the default 0x400000.

Then both:

readelf -h a.out

and gdb -ex 'b _start' tell me the entry is at 0x800440 as expected (the header is 0x440 bytes).

This is because that value is an input that tells the Linux kernel where to set the PC when forking a new process.

The default 0x400000 comes from the default linker script used. You can also modify the linker script as mentioned in https://stackoverflow.com/a/31380105/895245 , change 0x400000 there, and use the new script with -T script

If I put it at anything below 0x200000 (2Mb) exactly or other low addresses, the program gets killed. I think this is because ld always loads the sections at multiples of 2Mb, which is the largest page size supported (in huge page), so anything lower starts at 0, which is bad: Why is the ELF execution entry point virtual address of the form 0x80xxxxx and not zero 0x0?

Upvotes: 1

Employed Russian
Employed Russian

Reputation: 213386

instead of the usual 0x80****** that'd learned ELF's were loaded at.

You learned wrong.

While 0x804800 is the usual address that 32-bit x86 Linux binaries are linked at, that address is by no means universal or special.

64-bit x86_64 and aarch64 binaries are linked at default address of 0x40000, and powerpc64le binaries at default address of 0x10000000.

There is no reason a binary could not be linked at any other (page-aligned) address (so long as it is not 0, and allows for sufficient stack at the high end of the address space.

Why is this?

The binary was likely linked with a custom linker script. Nothing wrong with that.

Upvotes: 2

Related Questions