[llvm] 13549fd - MCAssembler: Modify Contents when VarFixups is not empty

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 17 10:29:23 PDT 2025


Author: Fangrui Song
Date: 2025-07-17T10:29:19-07:00
New Revision: 13549fd90af45d2200159cac14a12cf01db56aa1

URL: https://github.com/llvm/llvm-project/commit/13549fd90af45d2200159cac14a12cf01db56aa1
DIFF: https://github.com/llvm/llvm-project/commit/13549fd90af45d2200159cac14a12cf01db56aa1.diff

LOG: MCAssembler: Modify Contents when VarFixups is not empty

When there is no VarFixup, VarContentStart is zero.
`slice(F.VarContentStart - Contents.size(), F.getSize())`
might lead to "runtime error: addition of unsigned offset to" in ubsan builds after #148544

Added: 
    

Modified: 
    llvm/lib/MC/MCAssembler.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index f1a82f6b08d31..3e96bdf5169d8 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -735,13 +735,17 @@ void MCAssembler::layout() {
         // In the variable part, fixup offsets are relative to the fixed part's
         // start. Extend the variable contents to the left to account for the
         // fixed part size.
-        Contents = MutableArrayRef(F.getParent()->ContentStorage)
-                       .slice(F.VarContentStart - Contents.size(), F.getSize());
-        for (MCFixup &Fixup : F.getVarFixups()) {
-          uint64_t FixedValue;
-          MCValue Target;
-          evaluateFixup(F, Fixup, Target, FixedValue,
-                        /*RecordReloc=*/true, Contents);
+        auto VarFixups = F.getVarFixups();
+        if (VarFixups.size()) {
+          Contents =
+              MutableArrayRef(F.getParent()->ContentStorage)
+                  .slice(F.VarContentStart - Contents.size(), F.getSize());
+          for (MCFixup &Fixup : VarFixups) {
+            uint64_t FixedValue;
+            MCValue Target;
+            evaluateFixup(F, Fixup, Target, FixedValue,
+                          /*RecordReloc=*/true, Contents);
+          }
         }
       } else if (auto *AF = dyn_cast<MCAlignFragment>(&F)) {
         // For RISC-V linker relaxation, an alignment relocation might be


        


More information about the llvm-commits mailing list