在linux中,在文件dl-machine.h中有以下函数来获取共享对象的运行时加载地址。这也适用于FreeBSD,或者有不同的方法吗?
/* Return the run-time load address of the shared object. */
static inline ElfW(Addr) __attribute__ ((unused))
elf_machine_load_address (void)
{
ElfW(Addr) addr;
/* The easy way is just the same as on x86:
leaq _dl_start, %0
leaq _dl_start(%%rip), %1
subq %0, %1
but this does not work with binutils since we then have
a R_X86_64_32S relocation in a shared lib.
Instead we store the address of _dl_start in the data section
and compare it with the current value that we can get via
an RIP relative addressing mode. Note that this is the address
of _dl_start before any relocation performed at runtime. In case
the binary is prelinked the resulting "address" is actually a
load offset which is zero if the binary was loaded at the address
it is prelinked for. */
asm ("lea _dl_start(%%rip), %0\n\t"
"sub 1f(%%rip), %0\n\t"
".section\t.data.rel.ro\n"
"1:\t" ASM_ADDR " _dl_start\n\t"
".previous\n\t"
: "=r" (addr) : : "cc");
return addr;
}
答案 0 :(得分:0)
FreeBSD上不存在文件dl-machine.h
(至少在10.1 amd64上不存在)。
在FreeBSD上就像在Linux上一样,您可以使用dlsym
获取共享对象中的符号地址。
主要区别在于FreeBSD还提供了dlfunc
来获取函数的地址而没有编译器警告。
答案 1 :(得分:0)
您可以使用dladdr()
获取共享对象的基址,并在对象的其他位置给定地址。如果您从共享对象的代码中调用该函数非常简单。