[llvm] [AArch64][COFF] Fix HI12 SECREL miscompile in adjustFixupValue (PR #199602)

via llvm-commits llvm-commits at lists.llvm.org
Wed May 27 15:00:44 PDT 2026


https://github.com/hotschmoe updated https://github.com/llvm/llvm-project/pull/199602

>From 33e10ad2327d8696c362e27d8aaf62228210e984 Mon Sep 17 00:00:00 2001
From: hotschmoe <stronggarner66 at gmail.com>
Date: Wed, 27 May 2026 12:11:53 -0700
Subject: [PATCH] [MC][COFF][AArch64] Add helper symbols for large SECREL
 addends

ARM64 COFF SECREL_HIGH12A/LOW12A/LOW12L relocations carry a byte addend in the instruction immediate, which is limited to 12 bits. When lowering a temporary symbol to a section symbol, or when a defined symbol has a larger addend, create a label symbol at the exact section offset and clear the fixed value.

Fixes #199581.

Assisted-by: Claude (Anthropic)

Assisted-by: Codex (OpenAI)
---
 llvm/lib/MC/WinCOFFObjectWriter.cpp     | 71 +++++++++++++++++++++++--
 llvm/test/MC/AArch64/coff-secrel-hi12.s | 52 ++++++++++++++++++
 2 files changed, 119 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/MC/AArch64/coff-secrel-hi12.s

diff --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp
index 8f850c0228f37..17c90ec14607c 100644
--- a/llvm/lib/MC/WinCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp
@@ -129,6 +129,8 @@ class llvm::WinCOFFWriter {
 
   using symbol_map = DenseMap<MCSymbol const *, COFFSymbol *>;
   using section_map = DenseMap<MCSection const *, COFFSection *>;
+  using section_offset_map =
+      DenseMap<std::pair<MCSection const *, uint64_t>, COFFSymbol *>;
 
   using symbol_list = DenseSet<COFFSymbol *>;
 
@@ -141,11 +143,13 @@ class llvm::WinCOFFWriter {
   // Maps used during object file creation.
   section_map SectionMap;
   symbol_map SymbolMap;
+  section_offset_map SecRelSymbolMap;
 
   symbol_list WeakDefaults;
 
   bool UseBigObj;
   bool UseOffsetLabels = false;
+  unsigned SecRelSymbolCount = 0;
 
 public:
   enum DwoMode {
@@ -168,6 +172,7 @@ class llvm::WinCOFFWriter {
 private:
   MCContext &getContext() const { return OWriter.getContext(); }
   COFFSymbol *createSymbol(StringRef Name);
+  COFFSymbol *getOrCreateSecRelSymbol(const MCSymbol &Sym, uint64_t Offset);
   COFFSymbol *getOrCreateCOFFSymbol(const MCSymbol &Sym);
   COFFSection *createSection(StringRef Name);
 
@@ -251,6 +256,33 @@ COFFSymbol *WinCOFFWriter::getOrCreateCOFFSymbol(const MCSymbol &Sym) {
   return Ret;
 }
 
+COFFSymbol *WinCOFFWriter::getOrCreateSecRelSymbol(const MCSymbol &Sym,
+                                                   uint64_t Offset) {
+  MCSection *TargetSection = &Sym.getSection();
+  assert(SectionMap.contains(TargetSection) &&
+         "Section must already have been defined in executePostLayoutBinding!");
+
+  COFFSymbol *&Ret = SecRelSymbolMap[{TargetSection, Offset}];
+  if (Ret)
+    return Ret;
+
+  COFFSection *Section = SectionMap[TargetSection];
+  std::string Name;
+  if (Sym.isTemporary() && !Sym.getName().empty() &&
+      Offset == Asm->getSymbolOffset(Sym))
+    Name = Sym.getName().str();
+  else
+    Name =
+        (Twine("$L") + Section->Name + "_secrel_" + Twine(++SecRelSymbolCount))
+            .str();
+
+  Ret = createSymbol(Name);
+  Ret->Section = Section;
+  Ret->Data.StorageClass = COFF::IMAGE_SYM_CLASS_LABEL;
+  Ret->Data.Value = Offset;
+  return Ret;
+}
+
 COFFSection *WinCOFFWriter::createSection(StringRef Name) {
   Sections.emplace_back(std::make_unique<COFFSection>(Name));
   return Sections.back().get();
@@ -806,7 +838,9 @@ void WinCOFFWriter::reset() {
   Strings.clear();
   SectionMap.clear();
   SymbolMap.clear();
+  SecRelSymbolMap.clear();
   WeakDefaults.clear();
+  SecRelSymbolCount = 0;
 }
 
 void WinCOFFWriter::executePostLayoutBinding() {
@@ -886,9 +920,40 @@ void WinCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,
 
   Reloc.Data.SymbolTableIndex = 0;
   Reloc.Data.VirtualAddress = Asm->getFragmentOffset(F);
+  Reloc.Data.Type = OWriter.TargetObjectWriter->getRelocType(
+      getContext(), Target, Fixup, Target.getSubSym(), Asm->getBackend());
 
-  // Turn relocations for temporary symbols into section relocations.
-  if (A.isTemporary() && !SymbolMap[&A]) {
+  bool IsArm64SecRel12 = false;
+  if (COFF::isAnyArm64(Header.Machine)) {
+    switch (Reloc.Data.Type) {
+    case COFF::IMAGE_REL_ARM64_SECREL_HIGH12A:
+    case COFF::IMAGE_REL_ARM64_SECREL_LOW12A:
+    case COFF::IMAGE_REL_ARM64_SECREL_LOW12L:
+      IsArm64SecRel12 = true;
+      break;
+    }
+  }
+
+  bool NeedsSecRelSymbol = false;
+  uint64_t SecRelSymbolOffset = 0;
+  if (IsArm64SecRel12 && !Target.getSubSym() && A.isInSection()) {
+    SecRelSymbolOffset =
+        Asm->getSymbolOffset(A) + static_cast<uint64_t>(Target.getConstant());
+    if (A.isTemporary() && !SymbolMap.lookup(&A))
+      NeedsSecRelSymbol = !isUInt<12>(SecRelSymbolOffset);
+    else
+      NeedsSecRelSymbol = !isUInt<12>(FixedValue);
+  }
+
+  if (NeedsSecRelSymbol) {
+    if (!isUInt<32>(SecRelSymbolOffset)) {
+      getContext().reportError(Fixup.getLoc(),
+                               "relocation addend out of range");
+      return;
+    }
+    Reloc.Symb = getOrCreateSecRelSymbol(A, SecRelSymbolOffset);
+    FixedValue = 0;
+  } else if (A.isTemporary() && !SymbolMap[&A]) {
     MCSection *TargetSection = &A.getSection();
     assert(
         SectionMap.contains(TargetSection) &&
@@ -920,8 +985,6 @@ void WinCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,
   ++Reloc.Symb->Relocations;
 
   Reloc.Data.VirtualAddress += Fixup.getOffset();
-  Reloc.Data.Type = OWriter.TargetObjectWriter->getRelocType(
-      getContext(), Target, Fixup, Target.getSubSym(), Asm->getBackend());
 
   // The *_REL32 relocations are relative to the end of the relocation,
   // not to the start.
diff --git a/llvm/test/MC/AArch64/coff-secrel-hi12.s b/llvm/test/MC/AArch64/coff-secrel-hi12.s
new file mode 100644
index 0000000000000..63d5a693eefde
--- /dev/null
+++ b/llvm/test/MC/AArch64/coff-secrel-hi12.s
@@ -0,0 +1,52 @@
+// RUN: llvm-mc -triple aarch64-windows -filetype obj -o %t.obj %s
+// RUN: llvm-mc -triple arm64ec-windows -filetype obj -o %t-ec.obj %s
+// RUN: llvm-readobj -r --syms %t.obj | FileCheck %s
+// RUN: llvm-readobj -r --syms %t-ec.obj | FileCheck %s
+// RUN: llvm-objdump --no-print-imm-hex -d %t.obj | FileCheck %s --check-prefix=DISASM
+// RUN: llvm-objdump --no-print-imm-hex -d %t-ec.obj | FileCheck %s --check-prefix=DISASM
+
+  .bss
+  .zero 0xff0
+.Lsmall:
+  .long 0
+  .zero 12
+.Llarge:
+  .long 0
+
+  .text
+f:
+  add x0, x0, :secrel_hi12:.Lsmall
+  add x0, x0, :secrel_lo12:.Lsmall
+  add x1, x1, :secrel_hi12:.Llarge
+  add x1, x1, :secrel_lo12:.Llarge
+  ldr w2, [x2, :secrel_lo12:.Llarge+4]
+  ret
+
+// CHECK:      Relocations [
+// CHECK:        Section ({{[0-9]+}}) .text {
+// CHECK-NEXT:     0x0 IMAGE_REL_ARM64_SECREL_HIGH12A .bss (
+// CHECK-NEXT:     0x4 IMAGE_REL_ARM64_SECREL_LOW12A .bss (
+// CHECK-NEXT:     0x8 IMAGE_REL_ARM64_SECREL_HIGH12A .Llarge (
+// CHECK-NEXT:     0xC IMAGE_REL_ARM64_SECREL_LOW12A .Llarge (
+// CHECK-NEXT:     0x10 IMAGE_REL_ARM64_SECREL_LOW12L $L.bss_secrel_1 (
+// CHECK-NEXT:   }
+
+// CHECK:      Symbol {
+// CHECK:        Name: .Llarge
+// CHECK-NEXT:   Value: 4096
+// CHECK-NEXT:   Section: .bss
+// CHECK:        StorageClass: Label
+
+// CHECK:      Symbol {
+// CHECK:        Name: $L.bss_secrel_1
+// CHECK-NEXT:   Value: 4100
+// CHECK-NEXT:   Section: .bss
+// CHECK:        StorageClass: Label
+
+// DISASM-LABEL: <f>:
+// DISASM-NEXT:    add x0, x0, #4080, lsl #12
+// DISASM-NEXT:    add x0, x0, #4080
+// DISASM-NEXT:    add x1, x1, #0, lsl #12
+// DISASM-NEXT:    add x1, x1, #0
+// DISASM-NEXT:    ldr w2, [x2]
+// DISASM-NEXT:    ret



More information about the llvm-commits mailing list