C C/C++ App with static libs and -fPIC


I have to add the -fPIC/-pie flags to our apps, but I have a linking problem. I'm looking for the answer, but I did not found it yet. It seems, that the CRT library is not compiled with the -fPIC?

2016-08-03 083003 - /usr/bin/ld: /usr/lib/crtbeginT.o: relocation R_X86_64_32 against `__deregister_frame_info' can not be used when making a shared object; recompile with -fPIC
2016-08-03 083003 - /usr/lib/crtbeginT.o: could not read symbols: Bad value
2016-08-03 083003 - cc: error: linker command failed with exit code 1 (use -v to see invocation)
2016-08-03 083003 - *** Error code 1

Have you ever had problem like this? I am not sure how to fix it.

Best regards,
Can you post details on how you're trying to compile/link? The error message suggests you're trying to create a shared object, and without trying it myself now, I just assume it might be correct and the problem might be a wrong compiler or linker invocation ...

If the project is open source, a link to the repository might help so others could just try it out ;)
Unfortunately I cannot publish the repo, sorry :(

I am using <bsd.lib.mk> makefile for compilation static libraries, and <bsd.prog.mk> to compile an app.

The architecture of the project is as follows:
SDK -> static libraries
SOME_APP uses SDK -> it links the SDK libraries into the APP

All libraries and objects are compiled with the -fPIC flag and -pie is added to linker flags.

Here is our compilation flow:

Static lib mk:
LIB         = $(MY_LIB_NAME)

CFLAGS  = -fPIC -O0 -pipe -fPIE -Wformat -Wformat-security -I./inc
VPATH     = mysrc:${DIRS}
DIRS       = ./src:./src/freebsd/library/
WARNS   = 0

.include <bsd.lib.mk>

App that uses the static lib:
LIBS     = mylib1.a mylib2.a
CFLAGS  += -O0 -pipe -fPIE -fPIC -Wformat -Wformat-security -Wall ${INC}

LDFLAGS = -static -pie
LDADD   += $(LIBS)
SRCS     += $(OBJECTS)


I am not sure, what information would be useful for you, what can I do to provide more details?
What I'm after is a simple testcase to reproduce this problem. I'm pretty sure (although not entirely) that this should be possible.

So am I getting this right, you don't build any shared objects (.so) yourself, just the static archives? And the error occurs only when building an executable linked to those static archives?
I am attaching sample app with the same fault during linking and with the screen of the linker fault. Please take a look :) Thanks in advance!

Edit: No, I don't use any shared library in my project :)


  • freebsdfpic.zip
    36.9 KB · Views: 343
Sorry for the delay, my FreeBSD experimentation VM is busy building ports for 3 days now and my new server isn't installed yet ;) I'll definitely have a look at this (thanks for the sample) because I experienced similar problems before --- so I'll get back here soon :)
Well, I could link it successfully without the global -static flag:
cc -O2 -pipe -O0 -pipe -fPIE -fPIC -Wformat -Wformat-security -Wall -std=gnu99 -fstack-protector-strong -Qunused-arguments -pie -o myapp ./src/myapp.o  libmylib.a

The result is a binary, that has libc linked dynamically, but your mylib statically. I guess that's not exactly what you wanted?

edit: on a side note: what's the PIE/PIC flags good for on your app? Of course it works with no problems without them, even when the static lib is compiled using them ... the problem is indeed that the static version of the C runtime is not position independent.