Just say something else.
In the url mentioned in the question , it mentions that you can pass -mcmodel=large to gcc to tell the compiler to generate a 64-bit operand address for your code.
So gcc -mcmodel=large -shared ac will generate a non-PIC-shared object.
-
Demonstrations:
ac:
#include <stdio.h> void foo(void) { printf("%p\n", main); }
The 32-bit operand of the immediate address blocks the creation of a non-PIC object.
xiami@gentoo ~ $ cc -shared -o a.so ac /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/cck3FWeL.o: relocation R_X86_64_32 against `main' can not be used when making a shared object; recompile with -fPIC /tmp/cck3FWeL.o: error adding symbols: Bad value collect2: error: ld returned 1 exit status
Use -mcmodel=large to solve this problem. (Warnings only appear on my system because a change in .text is prohibited by my PaX kernel.)
xiami@gentoo ~ $ cc -mcmodel=large -shared -o a.so ac /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/../../../../x86_64-pc-linux-gnu/bin/ld: /tmp/ccZ3b9Xk.o: warning: relocation in readonly section `.text'. /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/../../../../x86_64-pc-linux-gnu/bin/ld: warning: creating a DT_TEXTREL in object.
Now you can see that the type of move record is R_X86_64_64 instead of R_X86_64_32, R_X86_64_PLT32, R_X86_64_PLTOFF64.
xiami@gentoo ~ $ objdump -R a.so a.so: file format elf64-x86-64 DYNAMIC RELOCATION RECORDS OFFSET TYPE VALUE ... 0000000000000758 R_X86_64_64 printf ...
And on my system, associate this shared object with regular code and run the program to fix errors, for example: ./a.out: error while loading shared libraries: ./a.so: cannot make segment writable for relocation: Permission denied
This proves that the dynamic loader redirects to .text , which does not have a PIC library.
Xiami
source share