[lld] r363128 - [ELF][RISCV] Treat R_RISCV_{ADD, SET, SUB}* as link-time constants

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 12 00:53:06 PDT 2019


Author: maskray
Date: Wed Jun 12 00:53:06 2019
New Revision: 363128

URL: http://llvm.org/viewvc/llvm-project?rev=363128&view=rev
Log:
[ELF][RISCV] Treat R_RISCV_{ADD,SET,SUB}* as link-time constants

R_RISCV_{ADD,SET,SUB}* are used for local label computation.
Add a new RelExpr member R_RISCV_ADD to represent them.

R_RISCV_ADD is treated as a link-time constant because otherwise
R_RISCV_{ADD,SET,SUB}* are not allowed in -pie/-shared mode.
In glibc Scrt1.o, .rela.eh_frame contains such relocations.
Because .eh_frame is not writable, we get this error:

    ld.lld: error: can't create dynamic relocation R_RISCV_ADD32 against symbol: .L0 in readonly segment; recompil object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
    >>> defined in ..../riscv64-linux-gnu/lib/Scrt1.o

With D63076 and this patch, I can run -pie/-shared programs linked against glibc.

Note llvm-mc cannot currently produce R_RISCV_SET* so they are not tested.

Reviewed By: ruiu

Differential Revision: https://reviews.llvm.org/D63183

Added:
    lld/trunk/test/ELF/riscv-reloc-add.s
Modified:
    lld/trunk/ELF/Arch/RISCV.cpp
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/Relocations.cpp
    lld/trunk/ELF/Relocations.h

Modified: lld/trunk/ELF/Arch/RISCV.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Arch/RISCV.cpp?rev=363128&r1=363127&r2=363128&view=diff
==============================================================================
--- lld/trunk/ELF/Arch/RISCV.cpp (original)
+++ lld/trunk/ELF/Arch/RISCV.cpp Wed Jun 12 00:53:06 2019
@@ -62,6 +62,20 @@ uint32_t RISCV::calcEFlags() const {
 RelExpr RISCV::getRelExpr(const RelType Type, const Symbol &S,
                           const uint8_t *Loc) const {
   switch (Type) {
+  case R_RISCV_ADD8:
+  case R_RISCV_ADD16:
+  case R_RISCV_ADD32:
+  case R_RISCV_ADD64:
+  case R_RISCV_SET6:
+  case R_RISCV_SET8:
+  case R_RISCV_SET16:
+  case R_RISCV_SET32:
+  case R_RISCV_SUB6:
+  case R_RISCV_SUB8:
+  case R_RISCV_SUB16:
+  case R_RISCV_SUB32:
+  case R_RISCV_SUB64:
+    return R_RISCV_ADD;
   case R_RISCV_JAL:
   case R_RISCV_BRANCH:
   case R_RISCV_CALL:

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=363128&r1=363127&r2=363128&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Wed Jun 12 00:53:06 2019
@@ -632,6 +632,7 @@ static uint64_t getRelocTargetVA(const I
   case R_DTPREL:
   case R_RELAX_TLS_LD_TO_LE_ABS:
   case R_RELAX_GOT_PC_NOPIC:
+  case R_RISCV_ADD:
     return Sym.getVA(A);
   case R_ADDEND:
     return A;

Modified: lld/trunk/ELF/Relocations.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.cpp?rev=363128&r1=363127&r2=363128&view=diff
==============================================================================
--- lld/trunk/ELF/Relocations.cpp (original)
+++ lld/trunk/ELF/Relocations.cpp Wed Jun 12 00:53:06 2019
@@ -401,8 +401,9 @@ static bool isStaticLinkTimeConstant(Rel
             R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC, R_MIPS_TLSGD,
             R_AARCH64_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC, R_GOTPLTONLY_PC,
             R_PLT_PC, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC, R_PPC32_PLTREL,
-            R_PPC64_CALL_PLT, R_PPC64_RELAX_TOC, R_TLSDESC_CALL, R_TLSDESC_PC,
-            R_AARCH64_TLSDESC_PAGE, R_HINT, R_TLSLD_HINT, R_TLSIE_HINT>(E))
+            R_PPC64_CALL_PLT, R_PPC64_RELAX_TOC, R_RISCV_ADD, R_TLSDESC_CALL,
+            R_TLSDESC_PC, R_AARCH64_TLSDESC_PAGE, R_HINT, R_TLSLD_HINT,
+            R_TLSIE_HINT>(E))
     return true;
 
   // These never do, except if the entire file is position dependent or if

Modified: lld/trunk/ELF/Relocations.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Relocations.h?rev=363128&r1=363127&r2=363128&view=diff
==============================================================================
--- lld/trunk/ELF/Relocations.h (original)
+++ lld/trunk/ELF/Relocations.h Wed Jun 12 00:53:06 2019
@@ -96,6 +96,7 @@ enum RelExpr {
   R_PPC64_CALL_PLT,
   R_PPC64_RELAX_TOC,
   R_PPC64_TOCBASE,
+  R_RISCV_ADD,
   R_RISCV_PC_INDIRECT,
 };
 

Added: lld/trunk/test/ELF/riscv-reloc-add.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/riscv-reloc-add.s?rev=363128&view=auto
==============================================================================
--- lld/trunk/test/ELF/riscv-reloc-add.s (added)
+++ lld/trunk/test/ELF/riscv-reloc-add.s Wed Jun 12 00:53:06 2019
@@ -0,0 +1,26 @@
+# REQUIRES: riscv
+# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+relax %s -o %t.32.o
+# RUN: ld.lld -pie %t.32.o -o %t.32
+# RUN: llvm-readelf -x .rodata %t.32 | FileCheck --check-prefix=HEX %s
+
+# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+relax %s -o %t.64.o
+# RUN: ld.lld -shared %t.64.o -o %t.64
+# RUN: llvm-readelf -x .rodata %t.64 | FileCheck --check-prefix=HEX %s
+
+# HEX:      section '.rodata':
+# HEX-NEXT: 0x{{[0-9a-f]+}} 04000000 00000000 04000000 040004
+
+## R_RISCV_ADD* and R_RISCV_SUB* are link-time constants, otherwise they are
+## not allowed in -pie/-shared mode.
+
+.global _start
+_start:
+.L0:
+  ret
+.L1:
+
+.rodata
+.dword .L1 - .L0
+.word .L1 - .L0
+.half .L1 - .L0
+.byte .L1 - .L0




More information about the llvm-commits mailing list