I'm trying to get the loaded address of an ELF binary, but dlopen
doesn't work as expected:
void *elf = (char *)dlopen (0, RTLD_NOW);
printf ("%p\n", elf);
sleep (100);
It prints 0xb772d918
, but from what /proc/1510/maps
tells, it doesn't point to the loaded address of the dlfn
binary, but the ld-2.15.so
,
08048000-08049000 r-xp 00000000 fc:00 1379 /root/dlfn
08049000-0804a000 r--p 00000000 fc:00 1379 /root/dlfn
0804a000-0804b000 rw-p 00001000 fc:00 1379 /root/dlfn
b7550000-b7552000 rw-p 00000000 00:00 0
b7552000-b76f5000 r-xp 00000000 fc:00 9275 /lib/i386-linux-gnu/libc-2.15.so
b76f5000-b76f7000 r--p 001a3000 fc:00 9275 /lib/i386-linux-gnu/libc-2.15.so
b76f7000-b76f8000 rw-p 001a5000 fc:00 9275 /lib/i386-linux-gnu/libc-2.15.so
b76f8000-b76fb000 rw-p 00000000 00:00 0
b76fb000-b76fe000 r-xp 00000000 fc:00 9305 /lib/i386-linux-gnu/libdl-2.15.so
b76fe000-b76ff000 r--p 00002000 fc:00 9305 /lib/i386-linux-gnu/libdl-2.15.so
b76ff000-b7700000 rw-p 00003000 fc:00 9305 /lib/i386-linux-gnu/libdl-2.15.so
b7708000-b770b000 rw-p 00000000 00:00 0
b770b000-b770c000 r-xp 00000000 00:00 0 [vdso]
b770c000-b772c000 r-xp 00000000 fc:00 9299 /lib/i386-linux-gnu/ld-2.15.so
b772c000-b772d000 r--p 0001f000 fc:00 9299 /lib/i386-linux-gnu/ld-2.15.so
b772d000-b772e000 rw-p 00020000 fc:00 9299 /lib/i386-linux-gnu/ld-2.15.so
bfc34000-bfc55000 rw-p 00000000 00:00 0 [stack]
So, other than parsing /proc/pid/maps
, is there a way to retrieve the loaded address of an ELF binary? (0x0848000 in this case)
On Linux,
dlopen
doesn't return the address where the ELF binary was loaded. It returnsstruct link_map
instead, which has.l_addr
member. So you'll want something like:However, despite what comment in
/usr/include/link.h
says,.l_addr
is actually not a load address either. Instead, it's the difference between where ELF image was linked to load, and where it was actually loaded.For non-PIE main executable, that difference is always 0. For non-prelinked shared library, that difference is always the load address (because non-prelinked ELF shared libraries are linked to load at address 0).
So how do you find the base address of the main executable? The easiest method is to use this code (linked into main executable):
Here is what you should see on 32-bit system:
(The last address:
0xf779a000
will vary from run to run if you have address randomization enabled (as you should)).