[llvm] [windows][x86][asm] fix nullptr deref in WinCOFFWriter::recordRelocation (PR #181459)

via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 20:41:07 PST 2026


https://github.com/meowette created https://github.com/llvm/llvm-project/pull/181459

This fixes llvm segfaulting when trying to compile this LLVM IR for MSVC with Intel syntax:

https://godbolt.org/z/a3bqj85rK

```llvm
target triple = "x86_64-pc-windows-msvc"

module asm ".intel_syntax"
module asm "jz [0x6]"
module asm "ret"
module asm ".att_syntax"
```

I encountered this issue going through rust issues regarding MSVC assembly (https://github.com/rust-lang/rust/issues/135362) and wanted to look into this as my first contribution to llvm. With this patch the behavior matches what happens on linux when `getAddSym` returns null and will simply return early from `recordRelocation`. Though I am unsure what the purpose of the assert was, since this technically can output valid assembly, and is also not present in the linux/ELF counterpart: https://github.com/llvm/llvm-project/blob/742af32b67c0d70ced4837fbde778ee5ea6529b4/llvm/lib/MC/ELFObjectWriter.cpp#L1319-L1325

I am still not exactly sure if this relative addressing should be allowed in general due to it *very* easily triggering UB, preferably labels should be used. Let me know what you think! ^-^

>From 0780afcebca808492f1bdb66e7328bdce2c1fabf Mon Sep 17 00:00:00 2001
From: meowette <41162644+meowette at users.noreply.github.com>
Date: Sat, 14 Feb 2026 05:15:14 +0100
Subject: [PATCH] [windows][x86][asm] fix nullptr deref in
 WinCOFFWriter::recordRelocation This fixes llvm segfaulting when trying to
 compile this LLVM IR:

https://godbolt.org/z/a3bqj85rK

```llvm
target triple = "x86_64-pc-windows-msvc"

module asm ".intel_syntax"
module asm "jz [0x6]"
module asm "ret"
module asm ".att_syntax"
```
---
 llvm/lib/MC/WinCOFFObjectWriter.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp
index 8f850c0228f37..b610f2dc2a42c 100644
--- a/llvm/lib/MC/WinCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp
@@ -840,9 +840,12 @@ void WinCOFFWriter::executePostLayoutBinding() {
 
 void WinCOFFWriter::recordRelocation(const MCFragment &F, const MCFixup &Fixup,
                                      MCValue Target, uint64_t &FixedValue) {
-  assert(Target.getAddSym() && "Relocation must reference a symbol!");
+  auto *SymA = Target.getAddSym();
 
-  const MCSymbol &A = *Target.getAddSym();
+  if (!SymA)
+    return;
+
+  const MCSymbol &A = *SymA;
   if (!A.isRegistered()) {
     getContext().reportError(Fixup.getLoc(), Twine("symbol '") + A.getName() +
                                                  "' can not be undefined");



More information about the llvm-commits mailing list