gcc: confused about -static -shared -fPIE -fPIC -Wl,-pie

痴心易碎 提交于 2019-12-22 18:38:13

问题


I'm trying to build clang, with all library static linked in. So that I can run it on CentOS 6 with ancient GCC 4.4 version.

At first, I think adding the option -static by turning on LLVM_BUILD_STATIC is enough. But in the link stage, it errors out.

dynamic STT_GNU_IFUNC symbol `strcmp' with pointer equality in `/usr/lib/../lib64/libc.a(strcmp.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie

So, I add -fPIE -Wl,-pie to CMAKE_CXX_FLAGS, and it says

-- Performing Test HAVE_CXX_ATOMICS_WITH_LIB
-- Performing Test HAVE_CXX_ATOMICS_WITH_LIB - Failed
CMake Error at cmake/modules/CheckAtomic.cmake:49 (message):
  Host compiler must support std::atomic!
Call Stack (most recent call first):
  cmake/config-ix.cmake:307 (include)
  CMakeLists.txt:590 (include)

I checked the cmake/modules/CheckAtomic.cmake file, It compiles the following code

#include <atomic>
std::atomic<float> x(0.0f);
int main() { return (float)x; }

with command

/home/hailin/gcc-4.8.3-boost-1.55/rtf/bin/g++ -fPIE -Wl,-pie -DHAVE_CXX_ATOMICS_WITHOUT_LIB -std=c++11 -static -lm

/home/hailin/gcc-4.8.3-boost-1.55/rtf/bin/g++ -fPIE -Wl,-pie -DHAVE_CXX_ATOMICS_WITH_LIB -std=c++11 -static -lm -latomic

The command with option -Wl,-pie reproduce the same error.

It seems like a dead end. Is there any conflict between -shared and -fPIE -Wl,-pie ?


回答1:


Old question, but in case someone else hits it: apparently you need to pass -pie to the compiler driver (gcc/g++), not just the linker (-Wl,-pie). Some startup object files differ for PIE (e.g. Scrt1.o instead of crt1.o) and these are passed by the driver to the linker, so the driver needs to know that you're making a PIE.



来源:https://stackoverflow.com/questions/43930447/gcc-confused-about-static-shared-fpie-fpic-wl-pie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!