Skip to content

Commit

Permalink
[llvm-objcopy] --gap-fill and 0-size sections (llvm#75837)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
quic-akaryaki authored Dec 19, 2023
1 parent 52e7b6f commit 535520c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/lib/ObjCopy/ELF/ELFObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2638,7 +2638,7 @@ template <class ELFT> Error ELFWriter<ELFT>::finalize() {
Error BinaryWriter::write() {
SmallVector<const SectionBase *, 30> SectionsToWrite;
for (const SectionBase &Sec : Obj.allocSections()) {
if (Sec.Type != SHT_NOBITS)
if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
SectionsToWrite.push_back(&Sec);
}

Expand Down
5 changes: 5 additions & 0 deletions llvm/test/tools/llvm-objcopy/ELF/gap-fill.test
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ Sections:
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x0108
Content: 'AABBCCDDFEDCBA'
- Name: .zero_size
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC ]
Address: 0x0110
Size: 0
- Name: .space2
Type: Fill
Pattern: 'DC'
Expand Down

0 comments on commit 535520c

Please sign in to comment.