Skip to content

Commit d4d4c6b

Browse files
committed
[llvm-objcopy] -O binary: skip empty sections
After SHF_ALLOC sections are ordered by LMA: * If initial sections are empty, GNU objcopy skips their contents while we emit leading zeros. (binary-paddr.test %t4) * If trailing sections are empty, GNU objcopy skips their contents while we emit trailing zeros. (binary-paddr.test %t5) This patch matches GNU objcopy's behavior. Linkers don't keep p_memsz PT_LOAD segments. Such empty sections would not have a containing PT_LOAD and `Section::ParentSegment` might be null if linkers fail to optimize the file offsets (lld D79254). In particular, without D79254, the arm Linux kernel's multi_v5_defconfig depends on this behavior: in `vmlinux`, an empty .text_itcm is mapped at a very high address (0xfffe0000) but the kernel does not expect `objcopy -O binary` to create a very large `arch/arm/boot/Image` (0xfffe0000-0xc0000000 ~= 1GiB). See https://bugs.llvm.org/show_bug.cgi?id=45632 Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D79229 (cherry picked from commit ec78690)
1 parent 060f2f8 commit d4d4c6b

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

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

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ProgramHeaders:
4848
# RUN: od -A x -t x2 %t2.out | FileCheck %s --check-prefix=CHECK2 --ignore-case
4949
# RUN: wc -c %t2.out | FileCheck %s --check-prefix=SIZE2
5050

51-
## The computed LMA of .data is 0x4000. The minimum LMA of all sections is 0x1000.
51+
## The computed LMA of .data is 0x4000. The minimum LMA of all non-empty sections is 0x1000.
5252
## The content of .data will be written at 0x4000-0x1000 = 0x3000.
5353
# CHECK2: 000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000
5454
# CHECK2-NEXT: 000010 0000 0000 0000 0000 0000 0000 0000 0000
@@ -93,7 +93,7 @@ ProgramHeaders:
9393
# RUN: od -A x -t x2 %t3.out | FileCheck %s --check-prefix=CHECK3 --ignore-case
9494
# RUN: wc -c %t3.out | FileCheck %s --check-prefix=SIZE3
9595

96-
## The minimum LMA of all sections is 0x1000.
96+
## The minimum LMA of all non-empty sections is 0x1000.
9797
## The content of .data will be written at 0x3000-0x1000 = 0x2000.
9898
# CHECK3: 000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000
9999
# CHECK3-NEXT: 000010 0000 0000 0000 0000 0000 0000 0000 0000
@@ -130,3 +130,58 @@ ProgramHeaders:
130130
PAddr: 0x3000
131131
Sections:
132132
- Section: .data
133+
134+
## The first section (.text) is empty. Test that we skip its LMA until the first
135+
## non-empty section, otherwise we would leave a large number of leading zeroes.
136+
# RUN: yaml2obj --docnum=4 %s -o %t4
137+
# RUN: llvm-objcopy -O binary %t4 %t4.out
138+
# RUN: od -A x -t x2 %t4.out | FileCheck %s --check-prefix=SKIPEMPTY
139+
140+
# SKIPEMPTY: 000000 3232
141+
# SKIPEMPTY-NEXT: 000002
142+
143+
--- !ELF
144+
FileHeader:
145+
Class: ELFCLASS64
146+
Data: ELFDATA2LSB
147+
Type: ET_EXEC
148+
Machine: EM_X86_64
149+
Sections:
150+
- Name: .text
151+
Type: SHT_PROGBITS
152+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
153+
Address: 0x1000
154+
AddressAlign: 0x1000
155+
- Name: gap
156+
Type: Fill
157+
Size: 0x1000
158+
- Name: .data
159+
Type: SHT_PROGBITS
160+
Flags: [ SHF_ALLOC, SHF_WRITE ]
161+
Content: "3232"
162+
163+
## The last section (.data) is empty. Test that we stop dumping after the last
164+
## non-empty section, otherwise we would leave a large number of trailing zeroes.
165+
# RUN: yaml2obj --docnum=5 %s -o %t5
166+
# RUN: llvm-objcopy -O binary %t5 %t5.out
167+
# RUN: od -A x -t x2 %t5.out | FileCheck %s --check-prefix=SKIPEMPTY
168+
169+
--- !ELF
170+
FileHeader:
171+
Class: ELFCLASS64
172+
Data: ELFDATA2LSB
173+
Type: ET_EXEC
174+
Machine: EM_X86_64
175+
Sections:
176+
- Name: .text
177+
Type: SHT_PROGBITS
178+
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
179+
Address: 0x1000
180+
AddressAlign: 0x1000
181+
Content: "3232"
182+
- Name: gap
183+
Type: Fill
184+
Size: 0xffd
185+
- Name: .data
186+
Type: SHT_PROGBITS
187+
Flags: [ SHF_ALLOC, SHF_WRITE ]

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,25 +2254,26 @@ Error BinaryWriter::finalize() {
22542254
OrderedSegments.erase(End, std::end(OrderedSegments));
22552255

22562256
// Compute the section LMA based on its sh_offset and the containing segment's
2257-
// p_offset and p_paddr. Also compute the minimum LMA of all sections as
2258-
// MinAddr. In the output, the contents between address 0 and MinAddr will be
2259-
// skipped.
2257+
// p_offset and p_paddr. Also compute the minimum LMA of all non-empty
2258+
// sections as MinAddr. In the output, the contents between address 0 and
2259+
// MinAddr will be skipped.
22602260
uint64_t MinAddr = UINT64_MAX;
22612261
for (SectionBase &Sec : Obj.allocSections()) {
22622262
if (Sec.ParentSegment != nullptr)
22632263
Sec.Addr =
22642264
Sec.Offset - Sec.ParentSegment->Offset + Sec.ParentSegment->PAddr;
2265-
MinAddr = std::min(MinAddr, Sec.Addr);
2265+
if (Sec.Size > 0)
2266+
MinAddr = std::min(MinAddr, Sec.Addr);
22662267
}
22672268

22682269
// Now that every section has been laid out we just need to compute the total
22692270
// file size. This might not be the same as the offset returned by
22702271
// layoutSections, because we want to truncate the last segment to the end of
2271-
// its last section, to match GNU objcopy's behaviour.
2272+
// its last non-empty section, to match GNU objcopy's behaviour.
22722273
TotalSize = 0;
22732274
for (SectionBase &Sec : Obj.allocSections()) {
22742275
Sec.Offset = Sec.Addr - MinAddr;
2275-
if (Sec.Type != SHT_NOBITS)
2276+
if (Sec.Type != SHT_NOBITS && Sec.Size > 0)
22762277
TotalSize = std::max(TotalSize, Sec.Offset + Sec.Size);
22772278
}
22782279

0 commit comments

Comments
 (0)