a relocatable non-pic executable

Andrew Haley aph@redhat.com
Tue Jul 27 06:03:00 GMT 2010


On 07/26/2010 07:19 PM, Mathieu Lacage wrote:
> On Mon, Jul 26, 2010 at 6:59 PM, Mathieu Lacage
> <mathieu.lacage@gmail.com> wrote:
>> On Mon, Jul 26, 2010 at 6:38 PM, Mathieu Lacage
>> <mathieu.lacage@gmail.com> wrote:
>>> I am trying to figure out if it is possible to generate a relocatable
>>> non-pic executable: it looks like generating an executable does not
>>
>> I forgot to specify that I am trying to do this on ia32 and x86_64.
> 
> -bash-3.2$ uname -p
> x86_64
> -bash-3.2$ cat /etc/fedora-release
> Fedora release 10 (Cambridge)
> -bash-3.2$ gcc --version
> gcc (GCC) 4.3.2 20081105 (Red Hat 4.3.2-7)
> -bash-3.2$ cat test.c
> void bar (void)
> {}
> int main (int argc, char *argv[])
> {
>   bar ();
>   return 0;
> }
> -bash-3.2$ gcc -c test.c -o test.o
> -bash-3.2$ readelf -r ./test.o
> Relocation section '.rela.text' at offset 0x550 contains 1 entries:
>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
> 000000000016  000800000002 R_X86_64_PC32     0000000000000000 bar +
> fffffffffffffffc
> ...
> -bash-3.2$ gcc test.o -o test
> -bash-3.2$ readelf -r ./test
> Relocation section '.rela.dyn' at offset 0x328 contains 1 entries:
>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
> 000000600838  000100000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
> ...
> -bash-3.2$ gcc  -Wl,--verbose -o test test.o|grep -B3 .rela.text
>   .rela.dyn       :
>     {
>       *(.rela.init)
>       *(.rela.text .rela.text.* .rela.gnu.linkonce.t.*)
> 
> To summarize: gcc generates the right relocation, the linker script
> says that it should preserve the section it is located in (.rela.text
> should go in .rela.dyn), the final output binary contains .rela.dyn
> but it does not contain the relocation anymore.
> 
> I tried -Wl,-r but this complains about a missing libgcc_s.a which I
> replaced with libgcc.a and the result is an ELF file without even a
> program header :/
> 
> I am _very_ stubborn, so, I tried to be smarter (which is obviously
> not sufficient):
> 
> -bash-3.2$ gcc -mcmodel=large -c test.c -o test.o
> -bash-3.2$ ld -shared ./test.o -o test
> -bash-3.2$ readelf -r ./test
> Relocation section '.rela.dyn' at offset 0x220 contains 3 entries:
>   Offset          Info           Type           Sym. Value    Sym. Name + Addend
> ...
> 00000000027f  000600000001 R_X86_64_64       0000000000000268 bar + 0
> -bash-3.2$ ./test
> Segmentation fault
> 
> Ok, well, I can imagine why this fails: I tried linking statically
> against all the right gcc object files and libs but to no avail.
> 
> At this point, I am interested in the answer to my original question
> mostly because I am curious.

Sorry to state the obvious, but you don't seem to be using the -pie option.

Andrew.



More information about the Gcc-help mailing list