Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在OSX中禁用.text重定位?_C_Macos_Clang_Relocation - Fatal编程技术网

C 如何在OSX中禁用.text重定位?

C 如何在OSX中禁用.text重定位?,c,macos,clang,relocation,C,Macos,Clang,Relocation,我正在尝试通过OS X中的\uuuu内置\uu return\u 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 0000000100000

我正在尝试通过OS X中的
\uuuu内置\uu return\u 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
但它在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\u pie
选项禁用ASLR可解决此问题

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

我相信这与许多现代操作系统实现中的安全特性有关。OS X(Darwin)上的内存页大小为4KB(4096字节),这会将寻址偏移
49756
页,即0xC25C000。我相信这与Linux上x86[-64]的页面大小相同。不确定其他BSD等。我的理解是LLDB禁用ASLR——可能是因为它通过重复执行使调试更容易。也许你可以禁用ASLR以正常执行…@BrettHale当我禁用ASLR时它起作用了。非常感谢。我不确定如何禁用它,或者这是否是唯一的机制。你应该接受你自己的答案。再次感谢,@BrettHale!
$ clang -Wl,-no_pie foo.c
$ ./a.out 
return address: 0x100000f79