我刚刚痛苦地发现 gcc 显然默认生成 -fpic 代码,默认情况下与 -fPIE 链接(在 ubuntu 17.04 上)。这完全搞砸了我使用许多不同的 linux 发行版使用的脚本和 makefile 运行的数千个测试。是否有任何全局或每个用户的方法来关闭这些默认值并使编译器向后兼容它几十年来的行为?我对跟踪数千个脚本中的每个编译不感兴趣,这些脚本知道默认值不是 -fpic 等。也许是一两个环境变量?
小智 6
由于Stack Overflow 上的这篇文章,我遇到了同样的问题并且刚刚解决了它。
您应该-no-pie向编译命令行添加选项
没有:
$ gcc main.c -o main
$ file main
main:ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32,
BuildID[sha1]=46ada4e5e25fc120ca052c9beb8bfa5491fc6239, not strippedRun Code Online (Sandbox Code Playgroud)
和:
$ gcc main.c -o main -no-pie
$ file main
main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32,
BuildID[sha1]=17f860c6c84fc1a5771c8744b7aaaf164c219559, not strippedRun Code Online (Sandbox Code Playgroud)