www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DMD Fails with fPIC error

reply "Reuben" <rdnetto gmail.com> writes:
Hi,
I'm new to D and am trying to compile a simple hello world 
program.
I get the following error when compiling it:

 dmd test.d
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: /opt/dmd-2.065/lib64/libphobos2.a(lifetime_488_4cd.o): relocation R_X86_64_32 against `_D15TypeInfo_Shared7__ClassZ' can not be used when making a shared object; recompile with -fPIC /opt/dmd-2.065/lib64/libphobos2.a: could not read symbols: Bad value collect2: error: ld returned 1 exit status --- errorlevel 1 This error occurs regardless of whether I use the -fPIC option or not. Compiling DMD from source does not change anything. I am using DMD 2.065.0 on Sabayon amd64, compiled from the dlang overlay with gcc (Gentoo Hardened 4.7.3-r1 p1.4, pie-0.5.5) 4.7.3. Thanks in advance.
Jun 13 2014
parent reply Mike Wey <mike-wey example.com> writes:
On 06/14/2014 03:58 AM, Reuben wrote:
 Hi,
 I'm new to D and am trying to compile a simple hello world program.
 I get the following error when compiling it:

 dmd test.d
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: /opt/dmd-2.065/lib64/libphobos2.a(lifetime_488_4cd.o): relocation R_X86_64_32 against `_D15TypeInfo_Shared7__ClassZ' can not be used when making a shared object; recompile with -fPIC /opt/dmd-2.065/lib64/libphobos2.a: could not read symbols: Bad value collect2: error: ld returned 1 exit status --- errorlevel 1 This error occurs regardless of whether I use the -fPIC option or not. Compiling DMD from source does not change anything. I am using DMD 2.065.0 on Sabayon amd64, compiled from the dlang overlay with gcc (Gentoo Hardened 4.7.3-r1 p1.4, pie-0.5.5) 4.7.3. Thanks in advance.
From the error it looks like you are compiling test.d as shared while linking against the static version of the standard library. If "dmd test.d" is the command being run there probably is an error in with the configuration in dmd.conf, the conf file is probably in /etc. Depending on the desired behavior you'll need to remove the -shared flag from the configuration or add -defaultlib=:libphobos2.so -- Mike Wey
Jun 14 2014
parent reply "Reuben" <rdnetto gmail.com> writes:
On Saturday, 14 June 2014 at 10:45:25 UTC, Mike Wey wrote:
 On 06/14/2014 03:58 AM, Reuben wrote:
 Hi,
 I'm new to D and am trying to compile a simple hello world 
 program.
 I get the following error when compiling it:

 dmd test.d
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: /opt/dmd-2.065/lib64/libphobos2.a(lifetime_488_4cd.o): relocation R_X86_64_32 against `_D15TypeInfo_Shared7__ClassZ' can not be used when making a shared object; recompile with -fPIC /opt/dmd-2.065/lib64/libphobos2.a: could not read symbols: Bad value collect2: error: ld returned 1 exit status --- errorlevel 1 This error occurs regardless of whether I use the -fPIC option or not. Compiling DMD from source does not change anything. I am using DMD 2.065.0 on Sabayon amd64, compiled from the dlang overlay with gcc (Gentoo Hardened 4.7.3-r1 p1.4, pie-0.5.5) 4.7.3. Thanks in advance.
From the error it looks like you are compiling test.d as shared while linking against the static version of the standard library. If "dmd test.d" is the command being run there probably is an error in with the configuration in dmd.conf, the conf file is probably in /etc. Depending on the desired behavior you'll need to remove the -shared flag from the configuration or add -defaultlib=:libphobos2.so
dmd.conf contains the default settings. I haven't specified -shared anywhere. The only thing I can think of is that if -fPIC is required, DMD might be implying it somehow. Compiling with -defaultlib seems to have fixed it. I'm getting a warning, but the program compiles and seems to run OK.
dmd -defaultlib=:libphobos2.so -fPIC test.d
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: 
test.o: warning: relocation in readonly section `.rodata'.
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: 
warning: creating a DT_TEXTREL in object.
Is the warning something I should be worried about?
Jun 14 2014
parent reply Mike Wey <mike-wey example.com> writes:
On 06/14/2014 02:01 PM, Reuben wrote:
 On Saturday, 14 June 2014 at 10:45:25 UTC, Mike Wey wrote:
 On 06/14/2014 03:58 AM, Reuben wrote:

 Depending on the desired behavior you'll need to remove the -shared
 flag from the configuration or add -defaultlib=:libphobos2.so
dmd.conf contains the default settings. I haven't specified -shared anywhere. The only thing I can think of is that if -fPIC is required, DMD might be implying it somehow.
Try running "dmd test.d -v" the last line in the outpus from dmd should show hows it's invoking gcc for the linking step. Posting that here might give us some clue of what dmd is doing.
 Compiling with -defaultlib seems to have fixed it. I'm getting a
 warning, but the program compiles and seems to run OK.

 dmd -defaultlib=:libphobos2.so -fPIC test.d
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld:
 test.o: warning: relocation in readonly section `.rodata'.
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld:
 warning: creating a DT_TEXTREL in object.
Is the warning something I should be worried about?
I don't know about this one. -- Mike Wey
Jun 14 2014
parent reply "Reuben" <rdnetto gmail.com> writes:
On Saturday, 14 June 2014 at 13:05:52 UTC, Mike Wey wrote:
 On 06/14/2014 02:01 PM, Reuben wrote:
 On Saturday, 14 June 2014 at 10:45:25 UTC, Mike Wey wrote:
 On 06/14/2014 03:58 AM, Reuben wrote:

 Depending on the desired behavior you'll need to remove the 
 -shared
 flag from the configuration or add -defaultlib=:libphobos2.so
dmd.conf contains the default settings. I haven't specified -shared anywhere. The only thing I can think of is that if -fPIC is required, DMD might be implying it somehow.
Try running "dmd test.d -v" the last line in the outpus from dmd should show hows it's invoking gcc for the linking step. Posting that here might give us some clue of what dmd is doing.
 dmd test.d -v
gcc test.o -o test -m64 -L/opt/dmd-2.065/lib64 -Xlinker -rpath -Xlinker /opt/dmd-2.065/lib64 -Xlinker --export-dynamic -l:libphobos2.a -lpthread -lm -lrt
 dmd -defaultlib=:libphobos2.so -fPIC test.d -v
gcc test.o -o test -m64 -L/opt/dmd-2.065/lib64 -Xlinker -rpath -Xlinker /opt/dmd-2.065/lib64 -Xlinker --export-dynamic -l:libphobos2.so -lpthread -lm -lrt It looks like the only difference is which version of Phobos we link. I think the reason for this might be that since my version of gcc is hardened, it uses -fPIE by default for linking. (http://wiki.gentoo.org/wiki/Hardened/Toolchain#Automatic_generation_of_Position_Independent_Executables_.28PIEs.29)
Jun 14 2014
parent reply Mike Wey <mike-wey example.com> writes:
On 06/14/2014 06:37 PM, Reuben wrote:
 It looks like the only difference is which version of Phobos we link. I
 think the reason for this might be that since my version of gcc is
 hardened, it uses -fPIE by default for linking.
 (http://wiki.gentoo.org/wiki/Hardened/Toolchain#Automatic_generation_of_Position_Independent_Executables_.28PIEs.29)
In that case the static Phobos needs to be build with -fPIC, which currently isn't the case looking at the Ebuild. -- Mike Wey
Jun 15 2014
parent reply "Reuben" <rdnetto gmail.com> writes:
On Sunday, 15 June 2014 at 09:08:10 UTC, Mike Wey wrote:
 On 06/14/2014 06:37 PM, Reuben wrote:
 It looks like the only difference is which version of Phobos 
 we link. I
 think the reason for this might be that since my version of 
 gcc is
 hardened, it uses -fPIE by default for linking.
 (http://wiki.gentoo.org/wiki/Hardened/Toolchain#Automatic_generation_of_Position_Independent_Executables_.28PIEs.29)
In that case the static Phobos needs to be build with -fPIC, which currently isn't the case looking at the Ebuild.
Compiling DMD with PIC=1 doesn't seem to do the trick. -fPIC is used for the C files, but when linking Phobos the following appears:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: 
generated/linux/release/64/libphobos2.so.0..o: warning: 
relocation in readonly section `.deh_eh'.
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: 
warning: creating a DT_TEXTREL in object.
Compiling with it then gives the following:
 $ ./dmd test.d
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: 
test.o: warning: relocation in readonly section 
`.text._D3std5stdio16__T7writelnTAyaZ7writelnFAyaZv'.
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: 
test.o: relocation R_X86_64_PC32 against undefined symbol 
`fprintf  GLIBC_2.2.5' can not be used when making a shared 
object; recompile with -fPIC
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld: 
final link failed: Bad value
collect2: error: ld returned 1 exit status --- errorlevel 1
readelf doesn't show any TEXTRELs in libc, so I assume it is also hardened.
Jun 17 2014
parent reply Mike Wey <mike-wey example.com> writes:
On 06/18/2014 08:51 AM, Reuben wrote:
 On Sunday, 15 June 2014 at 09:08:10 UTC, Mike Wey wrote:
 In that case the static Phobos needs to be build with -fPIC, which
 currently isn't the case looking at the Ebuild.
Compiling DMD with PIC=1 doesn't seem to do the trick. -fPIC is used for the C files, but when linking Phobos the following appears:
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld:
 generated/linux/release/64/libphobos2.so.0..o: warning: relocation in
 readonly section `.deh_eh'.
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/bin/ld:
 warning: creating a DT_TEXTREL in object.
Compiling dmd with -fPIC shoudn't matter, building the static version of Phobos works for me (make -f posix.mak DFLAGS="-fPIC"), but i don't have an hardened gcc, so that may be why. Google tells me the DT_TEXTREL error is because of non -fPIC objects in a shared library.
 Compiling with it then gives the following:
 $ ./dmd test.d
 ... error ...
readelf doesn't show any TEXTRELs in libc, so I assume it is also hardened.
test.d still needs to be build with -fPIC. -- Mike Wey
Jun 19 2014
parent "Reuben" <rdnetto gmail.com> writes:
Thanks, that did the trick.

Here's a summary post for anyone else with the same problem:

Problem:
DMD uses GCC to perform linking. On Hardened Gentoo (and 
derivatives like Sabayon), GCC implies -fPIE, which causes 
linking to fail if phobos and druntime were not compiled with 
-fPIC. You can check if this is the case by looking at the output 
of `gcc --version` - it will include the phrase Gentoo Hardened.

Workaround A:
Dynamically link Phobos instead.
$ dmd -defaultlib=:libphobos2.so -fPIC test.d

Workaround B:
Use a non-hardened compiler, such as Clang or a vanilla build of 
GCC. (Compiling GCC takes about 7 GB and 40 min, which makes it 
more expensive than actually fixing the problem).
$ env CC=/usr/bin/clang dmd test.d

Actual Fix:
Compile Phobos and Druntime with PIC=1. Note that for ebuilds, 
this includes the install target (which presumably does some 
linking).
You can then compile programs with:
$ dmd -fPIC test.d

Additional Notes:
-I've submitted a pull request to the overlay so that PIC is set 
automatically if hardened GCC is detected.
-LDC currently has the same problem. I'll probably look at fixing 
it in the near future.
Jun 22 2014