From 4e225deab4de4c0c9ca353e611b1a5b1c7d89ec2 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Wed, 3 Jul 2019 15:38:59 +0000 Subject: [PATCH] [ELF][RISCV] Error on R_RISCV_PCREL_LO12_[IS] that point to absolute symbols 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 Differential Revision: https://reviews.llvm.org/D63273 llvm-svn: 365049 --- lld/ELF/InputSection.cpp | 5 +++++ lld/test/ELF/riscv-pcrel-hilo-error.s | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 lld/test/ELF/riscv-pcrel-hilo-error.s diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 08fbbe8e3c6d..e11cbbb9a22b 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -570,6 +570,11 @@ static uint64_t getARMStaticBase(const Symbol &Sym) { // R_RISCV_PCREL_LO12's symbol and addend. static Relocation *getRISCVPCRelHi20(const Symbol *Sym, uint64_t Addend) { const Defined *D = cast(Sym); + if (!D->Section) { + error("R_RISCV_PCREL_LO12 relocation points to an absolute symbol: " + + Sym->getName()); + return nullptr; + } InputSection *IS = cast(D->Section); if (Addend != 0) diff --git a/lld/test/ELF/riscv-pcrel-hilo-error.s b/lld/test/ELF/riscv-pcrel-hilo-error.s new file mode 100644 index 000000000000..bfc205520bd8 --- /dev/null +++ b/lld/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: external + +addi sp,sp,%pcrel_lo(external)