Skip to content

Commit 13549fd

Browse files
committed
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
1 parent b8bc3ff commit 13549fd

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

llvm/lib/MC/MCAssembler.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,17 @@ void MCAssembler::layout() {
735735
// In the variable part, fixup offsets are relative to the fixed part's
736736
// start. Extend the variable contents to the left to account for the
737737
// fixed part size.
738-
Contents = MutableArrayRef(F.getParent()->ContentStorage)
739-
.slice(F.VarContentStart - Contents.size(), F.getSize());
740-
for (MCFixup &Fixup : F.getVarFixups()) {
741-
uint64_t FixedValue;
742-
MCValue Target;
743-
evaluateFixup(F, Fixup, Target, FixedValue,
744-
/*RecordReloc=*/true, Contents);
738+
auto VarFixups = F.getVarFixups();
739+
if (VarFixups.size()) {
740+
Contents =
741+
MutableArrayRef(F.getParent()->ContentStorage)
742+
.slice(F.VarContentStart - Contents.size(), F.getSize());
743+
for (MCFixup &Fixup : VarFixups) {
744+
uint64_t FixedValue;
745+
MCValue Target;
746+
evaluateFixup(F, Fixup, Target, FixedValue,
747+
/*RecordReloc=*/true, Contents);
748+
}
745749
}
746750
} else if (auto *AF = dyn_cast<MCAlignFragment>(&F)) {
747751
// For RISC-V linker relaxation, an alignment relocation might be

0 commit comments

Comments
 (0)