[PATCH] D63132: [ELF][RISCV] Set st_shndx of __global_pointer$ to 1 if .sdata does not exist

James Clarke via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 11 07:53:13 PDT 2019


jrtc27 added a comment.

In D63132#1538147 <https://reviews.llvm.org/D63132#1538147>, @MaskRay wrote:

> > GNU ld treats it as undefined:
>
> Yes. That was why I created the patch to fix the musl -fpie -pie link error: `ld.lld: error: relocation R_RISCV_PCREL_HI20 cannot refer to absolute symbol: __global_pointer$`. This error is lld specific: in -pie/-shared mode, a PC relative relocation to an absolute symbol is not allowed.
>
> In https://groups.google.com/forum/#!searchin/generic-abi/SHN_ABS|sort:date/generic-abi/MC4b4sqnKC0/oQgZxxwMmJAJ, Cary's interpretation of gABI is:
>
> > On the contrary, the only interpretation that makes sense to me is that it will not change because of relocation at link time or at load time.
>
> If lld resolves the PC relative relocation statically, the value will be incorrect when the pie or shared object is loaded at runtime because the SHN_ABS symbol's value does not change. The best lld can do is to issue an error.
>
> An alternative is to create a dummy `.sdata` in musl's `Scrt1.o`. I don't know how reasonable it is to have `lla gp,__global_pointer$` in other code that might not define `.sdata`.


Ah, so GNU ld sets it to 0/UND for -shared, but for -pie it puts it 0x800 past the end of .got:

  [ 7] .got              PROGBITS        0000000000002000 001000 000008 08  WA  0   0  8
    11: 0000000000002808     0 NOTYPE  GLOBAL DEFAULT    7 __global_pointer$

You can see the logic BFD is using in `ld/emulparams/elf32lriscv-defs.sh`.


Repository:
  rLLD LLVM Linker

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D63132/new/

https://reviews.llvm.org/D63132





More information about the llvm-commits mailing list