Avoiding position-independent executables due to linker error 'Relocation R_X86_64_32S against symbol `DeepState_UsingSymExec` can not be used when making a PIE object'
Closed this issue · 0 comments
Earlier today, I tried to build a DeepState test harness with other source files (including code from a rule-based testing framework) using make and ran into this:
$ make
g++ -Wall -g -std=c++11 -Iinclude -ldeepstate -o fuzztest_deepstateharness.bin bsd_random.o Random.o Pick.o Pick_default.o fuzztest_deepstateharness.o
/usr/bin/ld: fuzztest_deepstateharness.o: relocation R_X86_64_32S against symbol `DeepState_UsingSymExec' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
makefile: recipe for target 'fuzztest_deepstateharness.bin' failed
make: *** [fuzztest_deepstateharness.bin] Error 1
while using this makefile:
C = g++
STEST_DIR = ../../STest/
CFLAGS = -Wall -g -std=c++11 -Iinclude
fuzztest_deepstateharness.bin: bsd_random.o Random.o Pick.o Pick_default.o fuzztest_deepstateharness.o
$(C) $(CFLAGS) -ldeepstate -o fuzztest_deepstateharness.bin bsd_random.o Random.o Pick.o Pick_default.o fuzztest_deepstateharness.o
bsd_random.o: $(STEST_DIR)Random/bsd_random.c
gcc -Wall -g -c -Iinclude $(STEST_DIR)Random/bsd_random.c
Random.o: $(STEST_DIR)Random/Random.cpp
$(C) $(CFLAGS) -c $(STEST_DIR)Random/Random.cpp
Pick.o: $(STEST_DIR)Pick/Pick.cpp
$(C) $(CFLAGS) -c $(STEST_DIR)Pick/Pick.cpp
Pick_default.o: $(STEST_DIR)Pick/Pick_default.cpp
$(C) $(CFLAGS) -c $(STEST_DIR)Pick/Pick_default.cpp
fuzztest_deepstateharness.o: ./fuzztest_deepstateharness.cpp
$(C) $(CFLAGS) -c ./fuzztest_deepstateharness.cpp
clean:
rm fuzztest_deepstateharness.bin bsd_random.o Random.o Pick.o Pick_default.o fuzztest_deepstateharness.o
Looks like the linking error comes from DeepState's static library which probably creates some assembly code object that is not inherently position-independent, or it requires to be emplaced in a fixed address space instead of being readily available for shared libraries from a random memory location. I think my compiler and arch (GCC 7.5, x86_64) may be prone to it as well (for instance, I read somewhere that AArch64 does not use PIE with static libraries by default).
Solution
Adding -fPIC
clearly didn't work for me, but including -no-pie
in my C++ compiler flags resolved it (one only requires it while preparing the final executable binary as that goes for the linking stage, so emplacing the flag/option in this line for me works as well).