[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:58 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-mc
Author: felicia (meowette)
<details>
<summary>Changes</summary>
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! ^-^
---
Full diff: https://github.com/llvm/llvm-project/pull/181459.diff
1 Files Affected:
- (modified) llvm/lib/MC/WinCOFFObjectWriter.cpp (+5-2)
``````````diff
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");
``````````
</details>
https://github.com/llvm/llvm-project/pull/181459
More information about the llvm-commits
mailing list