[llvm] [llvm-objcopy] --gap-fill and 0-size sections (PR #75837)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 18 09:58:19 PST 2023
https://github.com/quic-akaryaki created https://github.com/llvm/llvm-project/pull/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 and add a zero-size section to the test, which would trigger the bug.
>From 22f8d2d1eec1973eb1d3b898c3555ae6c7bdec5e Mon Sep 17 00:00:00 2001
From: Alexey Karyakin <akaryaki at quicinc.com>
Date: Mon, 18 Dec 2023 09:51:17 -0800
Subject: [PATCH] [llvm-objcopy] --gap-fill and 0-size sections
In the change that added `--gap-fill`, the condition on choosing the
sections to write in `BinaryWriter::write()` did not exclude zero-size
sections. However, such 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.
---
llvm/lib/ObjCopy/ELF/ELFObject.cpp | 2 +-
llvm/test/tools/llvm-objcopy/ELF/gap-fill.test | 5 +++++
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/ObjCopy/ELF/ELFObject.cpp b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
index 5352736bdcb9b8..c8b66d6fcb5ebf 100644
--- a/llvm/lib/ObjCopy/ELF/ELFObject.cpp
+++ b/llvm/lib/ObjCopy/ELF/ELFObject.cpp
@@ -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);
}
diff --git a/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test b/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test
index c11909746330bb..6bfd27924bf244 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/gap-fill.test
@@ -106,6 +106,11 @@ Sections:
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
Address: 0x0108
Content: 'AABBCCDDFEDCBA'
+ - Name: .zero_size
+ Type: SHT_PROGBITS
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
+ Address: 0x0110
+ Size: 0
- Name: .space2
Type: Fill
Pattern: 'DC'
More information about the llvm-commits
mailing list