[PATCH] D63273: [ELF][RISCV] Error on R_RISCV_PCREL_LO12_[IS] that point to absolute symbols

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 13 07:57:55 PDT 2019


MaskRay created this revision.
MaskRay added reviewers: jrtc27, PkmX, ruiu.
Herald added subscribers: llvm-commits, Jim, benna, psnobl, jocewei, rkruppe, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, shiva0217, kito-cheng, niosHD, sabuasal, apazos, simoncook, johnrusso, rbar, asb, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

The referenced symbol is expected to point to an R_RISCV_*_HI20
relocation. An absolute symbol has no associated section, therefore
there cannot be a matching R_RISCV_*_HI20.

This fixes the crash reported by PR42038. For reference, ld.bfd errors:

  (.init+0x4): dangerous relocation: %pcrel_lo missing matching %pcrel_hi


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D63273

Files:
  ELF/InputSection.cpp
  test/ELF/riscv-pcrel-hilo-error.s


Index: test/ELF/riscv-pcrel-hilo-error.s
===================================================================
--- /dev/null
+++ test/ELF/riscv-pcrel-hilo-error.s
@@ -0,0 +1,7 @@
+# REQUIRES: riscv
+# RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.o
+# RUN: not ld.lld %t.o --defsym external=0 2>&1 | FileCheck %s
+
+# CHECK: error: R_RISCV_PCREL_LO12 relocation points to an absolute symbol
+
+addi sp,sp,%pcrel_lo(external)
Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -570,6 +570,10 @@
 // R_RISCV_PCREL_LO12's symbol and addend.
 static Relocation *getRISCVPCRelHi20(const Symbol *Sym, uint64_t Addend) {
   const Defined *D = cast<Defined>(Sym);
+  if (!D->Section) {
+    error("R_RISCV_PCREL_LO12 relocation points to an absolute symbol");
+    return nullptr;
+  }
   InputSection *IS = cast<InputSection>(D->Section);
 
   if (Addend != 0)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63273.204542.patch
Type: text/x-patch
Size: 956 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190613/272eb1aa/attachment.bin>


More information about the llvm-commits mailing list