[PATCH] D101773: [MC][ELF] Work around R_MIPS_LO16 relocation handling problem

Dimitry Andric via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 3 11:10:36 PDT 2021


dim created this revision.
dim added reviewers: andrewrk, atanasyan, emaste, MaskRay, nicolas17.
Herald added subscribers: luismarques, s.egerton, PkmX, simoncook, hiraditya, arichardson, sdardis.
dim requested review of this revision.
Herald added a project: LLVM.

This fixes PR49821, and avoids "ld.lld: error: test.o:(.rodata.str1.1):
offset is outside the section" errors when linking certain MIPS objects.

A root of the problem is in the R_MIPS_LO16 relocation handling. Both
R_MIPS_HI16 and R_MIPS_LO16 are considered as absolute relocations (the
type is R_ABS). When we calculate R_MIPS_HI16's addend we find a paired
R_MIPS_LO16, generate combined addend and return correct symbol value
from the InputSectionBase::getRelocTargetVA. For R_MIPS_LO16 we do not
do that and use its addend as is. Probably we should introduce new
expression type for these relocations (like R_RISCV_PC_INDIRECT for
R_RISCV_PCREL_HI20 / R_RISCV_PCREL_LO12) and calculate a correct symbol
value for both relocations at the InputSectionBase::getRelocTargetVA.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101773

Files:
  llvm/lib/MC/ELFObjectWriter.cpp


Index: llvm/lib/MC/ELFObjectWriter.cpp
===================================================================
--- llvm/lib/MC/ELFObjectWriter.cpp
+++ llvm/lib/MC/ELFObjectWriter.cpp
@@ -1381,6 +1381,16 @@
       if (TargetObjectWriter->getEMachine() == ELF::EM_386 &&
           Type == ELF::R_386_GOTOFF)
         return true;
+
+      // Work around an issue in MIPS relocation handling: both R_MIPS_HI16 and
+      // R_MIPS_LO16 are considered as absolute relocations (the type is R_ABS).
+      // When we calculate R_MIPS_HI16's addend we find a paired R_MIPS_LO16,
+      // generate a combined addend and return the correct symbol value from the
+      // InputSectionBase::getRelocTargetVA. For R_MIPS_LO16 we do not do that
+      // and use its addend as is.
+      if (TargetObjectWriter->getEMachine() == ELF::EM_MIPS &&
+          !hasRelocationAddend())
+        return true;
     }
 
     // Most TLS relocations use a got, so they need the symbol. Even those that


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101773.342471.patch
Type: text/x-patch
Size: 978 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210503/d47505ac/attachment.bin>


More information about the llvm-commits mailing list