[PATCH] RISC-V: Stop referencing __global_pointer$ under PIC

Palmer Dabbelt palmer@rivosinc.com
Mon Jun 26 20:13:55 GMT 2023


On Mon, 26 Jun 2023 13:12:41 PDT (-0700), maskray@google.com wrote:
> On Thu, Jun 8, 2023 at 9:45 AM Palmer Dabbelt <palmer@rivosinc.com> wrote:
>>
>> This has some cascading fallout related to PC-relative references to
>> SHN_ABS that Jim reported in [1].  I have a workaround for that issue in
>> binutils [2], but GP isn't useful in PIC so we might as well just stop
>> referencing it at all.
>>
>> Link: https://sourceware.org/bugzilla/show_bug.cgi?id=24678
>> Link: https://inbox.sourceware.org/binutils/20230608155214.32435-1-palmer@rivosinc.com/T/#u
>> Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
>>
>> ---
>>
>> I haven't tested thiis or the binutils patch.  There's a handful of
>> coupled issues here that might take a bit to untangle, but this came up
>> in the RISC-V LLVM sync this morning so I figured it would be best to
>> send something along.
>> ---
>>  sysdeps/riscv/start.S | 2 ++
>>  1 file changed, 2 insertions(+)
>>
>> diff --git a/sysdeps/riscv/start.S b/sysdeps/riscv/start.S
>> index 6dfe65273f..5eaa8ccf2d 100644
>> --- a/sysdeps/riscv/start.S
>> +++ b/sysdeps/riscv/start.S
>> @@ -71,7 +71,9 @@ END (ENTRY_POINT)
>>  load_gp:
>>  .option push
>>  .option norelax
>> +#if !(defined(__PIC__) || defined(__pic__) || defined(PIC) || defined(pic))
>>         lla   gp, __global_pointer$
>> +#endif
>>  .option pop
>>         ret
>>
>> --
>> 2.40.1
>>
>
> I haven't read all the discussion yet.. If we want to detect
> -fpic/-fPIC (no distinction in LLVM for non-sparc-non-ppc32 targets).
> Checking just __PIC__ is sufficient. __PIC__ seems more popular than
> __pic__ for open-source software.

I found all four used other places in glibc and figured I'd just do the 
same, I'm not really sure it's necessary but it doesn't seem to hurt.

>
> % riscv64-linux-gnu-gcc -fpic -dM -E -xc /dev/null | grep -i pic
> #define __pic__ 1
> #define __PIC__ 1
> #define __riscv_cmodel_pic 1
> % clang --target=riscv64 -fpic -dM -E -xc /dev/null | grep -i pic
> #define __PIC__ 1
> #define __pic__ 1
>
> (-fPIC changes the macro replacements to 2.)
>
> -- 
> 宋方睿


More information about the Libc-alpha mailing list