Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2024

Conversation

aeubanks
Copy link
Contributor

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.

…_LARGE section

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 llvm#71248 by only hinting on an overflow.
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2023

@llvm/pr-subscribers-lld-elf

@llvm/pr-subscribers-lld

Author: Arthur Eubanks (aeubanks)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/73045.diff

2 Files Affected:

  • (modified) lld/ELF/Relocations.cpp (+6)
  • (added) lld/test/ELF/x86-64-pc32-overflow-large.s (+25)
diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index fe3d7f419e84aa6..37a2363094020d0 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -105,6 +105,12 @@ 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 (rel.type == R_X86_64_PC32 && rel.sym->getOutputSection() &&
+        (rel.sym->getOutputSection()->flags & SHF_X86_64_LARGE)) {
+      hint += "; a R_X86_64_PC32 relocation 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 000000000000000..54c20eddfd04c33
--- /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: {{.*}}; a R_X86_64_PC32 relocation 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",@progbits
+.type   hello, @object
+.globl  hello
+hello:
+.long   1
+
+#--- lds
+SECTIONS {
+  .text 0x100000 : { *(.text) }
+  ldata 0x80200000 : { *(ldata) }
+}

@aeubanks aeubanks requested a review from MaskRay January 16, 2024 23:35
if (config->emachine == EM_X86_64 && rel.type == R_X86_64_PC32 &&
rel.sym->getOutputSection() &&
(rel.sym->getOutputSection()->flags & SHF_X86_64_LARGE)) {
hint += "; a R_X86_64_PC32 relocation should not reference a section "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the diagnostic the sentence can be more precise. Perhaps ; R_X86_64_PC32 should not reference a section marked . In other messages we do use R_* to refer to a relocation of the type.

@aeubanks aeubanks merged commit 1d4c009 into llvm:main Jan 17, 2024
@aeubanks aeubanks deleted the errorlarge branch January 17, 2024 23:39
ampandey-1995 pushed a commit to ampandey-1995/llvm-project that referenced this pull request Jan 19, 2024
…_LARGE section (llvm#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 llvm#71248 by only hinting on an overflow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants