Skip to content

Commit 535520c

Browse files
[llvm-objcopy] --gap-fill and 0-size sections (llvm#75837)
In the change that added `--gap-fill`, the condition to choose the sections to write in `BinaryWriter::write()` did not exclude zero-size sections. However, zero-size sections did not have correct offsets assigned in `BinaryWriter::finalize()`. The result is either a failed assertion, or memory corruption due to writing to the buffer beyond its size. To fix this, exclude zero-size sections from writing. Also, add a zero-size section to the test, which would trigger the problem.
1 parent 52e7b6f commit 535520c

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

llvm/lib/ObjCopy/ELF/ELFObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,7 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
26382638
Error BinaryWriter::write() {
26392639
SmallVector<const SectionBase *, 30> SectionsToWrite;
26402640
for (const SectionBase &Sec : Obj.allocSections()) {
2641-
if (Sec.Type != SHT_NOBITS)
2641+
if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
26422642
SectionsToWrite.push_back(&Sec);
26432643
}
26442644

llvm/test/tools/llvm-objcopy/ELF/gap-fill.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ Sections:
106106
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
107107
Address: 0x0108
108108
Content: 'AABBCCDDFEDCBA'
109+
- Name: .zero_size
110+
Type: SHT_PROGBITS
111+
Flags: [ SHF_ALLOC ]
112+
Address: 0x0110
113+
Size: 0
109114
- Name: .space2
110115
Type: Fill
111116
Pattern: 'DC'

0 commit comments

Comments
 (0)