Skip to content

Commit 93345e8

Browse files
pattopMaskRay
authored andcommitted
[llvm-objcopy] -O binary: consider SHT_NOBITS sections to be empty
This is consistent with BFD objcopy. Previously llvm objcopy would allocate space for SHT_NOBITS sections often resulting in enormous binary files. New test case (binary-paddr.test %t6). Reviewed By: jhenderson, MaskRay Differential Revision: https://reviews.llvm.org/D95569
1 parent 8e661d3 commit 93345e8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,34 @@ Sections:
183183
- Name: .data
184184
Type: SHT_PROGBITS
185185
Flags: [ SHF_ALLOC, SHF_WRITE ]
186+
187+
## NOBITS sections should not appear in output.
188+
# RUN: yaml2obj --docnum=6 %s -o %t6
189+
# RUN: llvm-objcopy -O binary %t6 %t6.out
190+
# RUN: od -A x -t x2 %t6.out | FileCheck %s --check-prefix=SKIPNOBITS --ignore-case
191+
192+
# SKIPNOBITS: 000000 c3c3 c3c3
193+
# SKIPNOBITS-NEXT: 000004
194+
195+
--- !ELF
196+
FileHeader:
197+
Class: ELFCLASS64
198+
Data: ELFDATA2LSB
199+
Type: ET_EXEC
200+
Machine: EM_X86_64
201+
Sections:
202+
- Name: .bss
203+
Type: SHT_NOBITS
204+
Flags: [ SHF_ALLOC ]
205+
Address: 0x1000
206+
AddressAlign: 0x1000
207+
Size: 0x123
208+
- Name: gap
209+
Type: Fill
210+
Size: 0xffd
211+
- Name: .text
212+
Type: SHT_PROGBITS
213+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
214+
Address: 0x4000
215+
AddressAlign: 0x1000
216+
Content: "c3c3c3c3"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2553,7 +2553,7 @@ Error BinaryWriter::finalize() {
25532553
if (Sec.ParentSegment != nullptr)
25542554
Sec.Addr =
25552555
Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr;
2556-
if (Sec.Size > 0)
2556+
if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
25572557
MinAddr = std::min(MinAddr, Sec.Addr);
25582558
}
25592559

0 commit comments

Comments
 (0)