如何在 OS X 中禁用 .text 重定位?

How to disable .text relocation in OS X?

我正在尝试通过 __builtin_return_address() 在 OS X:

中获取 return 地址
/* foo.c */
#include <stdio.h>

void foo() {
    printf("return address: %p\n", __builtin_return_address(0));
}

int main() {
    foo();
}

bash-3.2$ clang foo.c
bash-3.2$ nm a.out
0000000100000000 T __mh_execute_header
0000000100000f40 T _foo
0000000100000f70 T _main
                 U _printf
                 U dyld_stub_binder

然而,它并不是return我想要的地址。

bash-3.2$ ./a.out
return address: 0x10c25cf79
bash-3.2$ atos -o a.out 0x10c25cf79
0x10c25cf79                                 

尽管它在 LLDB 中运行良好。

bash-3.2$ lldb a.out
(lldb) target create "a.out"
Current executable set to 'a.out' (x86_64).
(lldb) r
Process 77500 launched: '/private/tmp/a.out' (x86_64)
return address: 0x100000f79
Process 77500 exited with status = 0 (0x00000000) 
(lldb) q
bash-3.2$ atos -o a.out 0x100000f79
main (in a.out) + 9               

这是怎么回事,我该如何解决?

@BrettHale 在评论中回答。这是由 ASLR 引起的。

通过 -no_pie 选项禁用 ASLR 可解决此问题。

$ clang -Wl,-no_pie foo.c
$ ./a.out 
return address: 0x100000f79