[lld] 1d4c009 - [lld/ELF] Hint if R_X86_64_PC32 overflows and references a SHF_X86_64_LARGE section (#73045)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 17 15:39:02 PST 2024


Author: Arthur Eubanks
Date: 2024-01-17T15:38:59-08:00
New Revision: 1d4c0092a82aa351c5a6e9e501cc5edbc6c0477f

URL: https://github.com/llvm/llvm-project/commit/1d4c0092a82aa351c5a6e9e501cc5edbc6c0477f
DIFF: https://github.com/llvm/llvm-project/commit/1d4c0092a82aa351c5a6e9e501cc5edbc6c0477f.diff

LOG: [lld/ELF] Hint if R_X86_64_PC32 overflows and references a SHF_X86_64_LARGE section (#73045)

Makes it clearer what the issue is when hand-written assembly doesn't
follow medium code model assumptions in a medium code model build.

Alternative to #71248 by only hinting on an overflow.

Added: 
    lld/test/ELF/x86-64-pc32-overflow-large.s

Modified: 
    lld/ELF/Relocations.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index af974d0f90e1d2..59b02207958717 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -105,6 +105,13 @@ void elf::reportRangeError(uint8_t *loc, const Relocation &rel, const Twine &v,
       hint = "; references '" + lld::toString(*rel.sym) + '\'';
     else if (auto *d = dyn_cast<Defined>(rel.sym))
       hint = ("; references section '" + d->section->name + "'").str();
+
+    if (config->emachine == EM_X86_64 && rel.type == R_X86_64_PC32 &&
+        rel.sym->getOutputSection() &&
+        (rel.sym->getOutputSection()->flags & SHF_X86_64_LARGE)) {
+      hint += "; R_X86_64_PC32 should not reference a section marked "
+              "SHF_X86_64_LARGE";
+    }
   }
   if (!errPlace.srcLoc.empty())
     hint += "\n>>> referenced by " + errPlace.srcLoc;

diff  --git a/lld/test/ELF/x86-64-pc32-overflow-large.s b/lld/test/ELF/x86-64-pc32-overflow-large.s
new file mode 100644
index 00000000000000..fb8f3e4480c40f
--- /dev/null
+++ b/lld/test/ELF/x86-64-pc32-overflow-large.s
@@ -0,0 +1,25 @@
+# REQUIRES: x86
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a.s -o %t/a.o
+# RUN: not ld.lld %t/a.o -T %t/lds -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error: {{.*}}a.o:(.text+{{.*}}): relocation R_X86_64_PC32 out of range: {{.*}}; R_X86_64_PC32 should not reference a section marked SHF_X86_64_LARGE
+
+#--- a.s
+.text
+.globl _start
+.type _start, @function
+_start:
+  movq hello(%rip), %rax
+
+.section ldata,"awl", at progbits
+.type   hello, @object
+.globl  hello
+hello:
+.long   1
+
+#--- lds
+SECTIONS {
+  .text 0x100000 : { *(.text) }
+  ldata 0x80200000 : { *(ldata) }
+}


        


More information about the llvm-commits mailing list