[PATCH v5 1/1] elf: Handle static PIE loaded at the specific address [BZ #31799]

Florian Weimer fweimer@redhat.com
Fri Jun 14 10:36:30 GMT 2024


* H. J. Lu:

> On Mon, May 27, 2024 at 11:05 PM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * H. J. Lu:
>>
>> > When a static PIE is loaded at the specific address, its PT_DYNAMIC
>> > segment entries contain the relocated values for the load address.
>> > Compute the load address from the p_vaddr of the PT_LOAD segement
>> > which covers the file start.  This fixes BZ #31799.
>>
>> A PIE loaded at a specific address is no longer PIE.
>>
>> I think this should work:
>>
>> cat > t.c <<EOF
>> #include <stdio.h>
>>
>> int
>> main (void)
>> {
>>   puts ("Hello, world!");
>> }
>> EOF
>> ld --verbose  | sed -n -e '/^=/,/^=/{//!{s/0x400000/0x100000000/g;p}}' \
>>   > ld.script
>> gcc -fpie -static -Wl,-T,ld.script t.c
>>
>> But it fails with:
>>
>> /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crt1.o: in function `_start':
>> (.text+0x1b): failed to convert GOTPCREL relocation against 'main'; relink with --no-relax
>> collect2: error: ld returned 1 exit status
>
> Did you mean?
>
> [hjl@gnu-cfl-3 tmp]$ gcc -static -Wl,-Ttext-segment=0x100000000  x.c
> /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crt1.o: in
> function `_start':
> (.text+0x1b): failed to convert GOTPCREL relocation against 'main';
> relink with --no-relax
> /usr/local/bin/ld: final link failed
> collect2: error: ld returned 1 exit status
> [hjl@gnu-cfl-3 tmp]$ gcc -static -Wl,-Ttext-segment=0x100000000  x.c
> /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crt1.o: in
> function `_start':
> (.text+0x1b): failed to convert GOTPCREL relocation against 'main';
> relink with --no-relax
> /usr/local/bin/ld: final link failed
> collect2: error: ld returned 1 exit status
> [hjl@gnu-cfl-3 tmp]$

Sort of, but I compiled with PIE as well.

> This no longer fits in the small model.

You need to *compile* as PIE.  The link editor should be able to produce
a position-dependent executable that is loaded at an arbitrary address
from such relocatable input files.  The small model and GOTPCREL still
work because the GOT is still less than 2 GiB away from the code.  This
is just a linker limitation.

Thanks,
Florian



More information about the Libc-alpha mailing list