Skip to content

Commit 8e7ae35

Browse files
committed
[llvm-objcopy] Avoid invalid Sec.Offset after D79229
To avoid undefined behavior caught by -fsanitize=undefined on binary-paddr.test void SectionWriter::visit(const Section &Sec) { if (Sec.Type != SHT_NOBITS) // Sec.Contents is empty while Sec.Offset may be out of bound llvm::copy(Sec.Contents, Out.getBufferStart() + Sec.Offset); } (cherry picked from commit 762fb1c)
1 parent d4d4c6b commit 8e7ae35

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

llvm/tools/llvm-objcopy/ELF/Object.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,11 +2271,11 @@ Error BinaryWriter::finalize() {
22712271
// layoutSections, because we want to truncate the last segment to the end of
22722272
// its last non-empty section, to match GNU objcopy's behaviour.
22732273
TotalSize = 0;
2274-
for (SectionBase &Sec : Obj.allocSections()) {
2275-
Sec.Offset = Sec.Addr - MinAddr;
2276-
if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
2274+
for (SectionBase &Sec : Obj.allocSections())
2275+
if (Sec.Type != SHT_NOBITS && Sec.Size > 0) {
2276+
Sec.Offset = Sec.Addr - MinAddr;
22772277
TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size);
2278-
}
2278+
}
22792279

22802280
if (Error E = Buf.allocate(TotalSize))
22812281
return E;

0 commit comments

Comments
 (0)