https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70419

--- Comment #2 from Balint Reczey <balint at balintreczey dot hu> ---
(In reply to Balint Reczey from comment #1)
> Also please fix the error message emitted when trying to link a non-PIE
> non-PIC static library to a position independent executable:
> 
> $ cat m.c
> #include <zlib.h>
> 
> int main() {
>   gzopen(0,0);
>   return 1;
> }
> 
> $gcc -pie m.c -lz
> /usr/bin/ld: /tmp/cchMINoQ.o: relocation R_X86_64_PC32 against undefined
> symbol `gzopen' can not be used when making a shared object; recompile with
> -fPIC
> /usr/bin/ld: final link failed: Bad value
> collect2: error: ld returned 1 exit status
> $ gcc --version
> gcc (Debian 6-20160228-1) 6.0.0 20160228 (experimental) [trunk revision
> 233789]
> 
> One better error message could be:
> `gzopen' can not be used when making a position independent executable;
> recompile with -fPIC or -fPIE
> 
> I can open a separate bug for that but this is closely related to the
> clarifications asked here.

The correct myself the error was about not compiling m.c with -fPIE but the
error is still misleading.

The fix is the following, compiling as PIE, linking with shared libz:
$gcc -pie -fPIE  m.c -lz

The other misleading error message about the static library lacking -fPIE can
be triggered this way:
$ gcc -pie -fPIE  m.c  /usr/lib/x86_64-linux-gnu/libz.a
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libz.a(gzlib.o): relocation R_X86_64_32S
against `.rodata' can not be used when making a shared object; recompile with
-fPIC
/usr/lib/x86_64-linux-gnu/libz.a: error adding symbols: Bad value

Reply via email to