我正在尝试通过OS X中的__builtin_return_address()
获取返回地址:
/* 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
但是,它不会返回我想要的地址。
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
发生了什么,我该如何解决?
答案 0 :(得分:1)
@BrettHale在评论中回答。这是由ASLR引起的。
按-no_pie
选项禁用ASLR可解决此问题。
$ clang -Wl,-no_pie foo.c
$ ./a.out
return address: 0x100000f79