Skip to content

Commit acb66b9

Browse files
committed
[ELF] --oformat=binary: use LMA to compute file offsets
--oformat=binary is rare (used in a few places in FreeBSD, see `stand/i386/mbr/Makefile` `LDFLAGS_BIN`) The result should be identical to a normal output transformed by `objcopy -O binary`. The current implementation ignores addresses and lays out sections by respecting output section alignments. It can fail when an output section address is specified, e.g. `.rodata ALIGN(16) :` (PR33651). Fix PR33651 by respecting LMA. The code is similar to `tools/llvm-objcop/ELF/Object.cpp` BinaryWriter::finalize after D71035 and D79229. Unforunately for an output section without PT_LOAD, we assume its LMA is equal to its VMA. So the result is still incorrect when an output section LMA (`AT(...)`) is specified Also drop `alignTo(off, config->wordsize)`. GNU ld does not round up the file size. Differential Revision: https://reviews.llvm.org/D85086
1 parent 98d91ae commit acb66b9

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

lld/ELF/Writer.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,11 +2547,24 @@ static uint64_t setFileOffset(OutputSection *os, uint64_t off) {
25472547
}
25482548

25492549
template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
2550-
uint64_t off = 0;
2550+
// Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr.
2551+
auto needsOffset = [](OutputSection &sec) {
2552+
return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;
2553+
};
2554+
uint64_t minAddr = UINT64_MAX;
25512555
for (OutputSection *sec : outputSections)
2552-
if (sec->flags & SHF_ALLOC)
2553-
off = setFileOffset(sec, off);
2554-
fileSize = alignTo(off, config->wordsize);
2556+
if (needsOffset(*sec)) {
2557+
sec->offset = sec->getLMA();
2558+
minAddr = std::min(minAddr, sec->offset);
2559+
}
2560+
2561+
// Sections are laid out at LMA minus minAddr.
2562+
fileSize = 0;
2563+
for (OutputSection *sec : outputSections)
2564+
if (needsOffset(*sec)) {
2565+
sec->offset -= minAddr;
2566+
fileSize = std::max(fileSize, sec->offset + sec->size);
2567+
}
25552568
}
25562569

25572570
static std::string rangeToString(uint64_t addr, uint64_t len) {

lld/test/ELF/oformat-binary-ttext.s

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
# RUN: ld.lld -N -Ttext 0x100 -o %t.out %t --oformat binary
55
# RUN: od -t x1 -v %t.out | FileCheck %s --check-prefix=BIN
66

7-
# BIN: 0000000 90 00 00 00 00 00 00 00
8-
# BIN-NEXT: 0000010
9-
# BIN-NOT: 0000020
7+
# BIN: 0000000 90
8+
# BIN-NEXT: 0000001
109

1110
## The same but without OMAGIC.
1211
# RUN: ld.lld -Ttext 0x100 -o %t.out %t --oformat binary

lld/test/ELF/oformat-binary.s

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
# RUN: ld.lld -o %t.out %t --oformat binary
55
# RUN: od -t x1 -v %t.out | FileCheck %s
6-
# CHECK: 000000 90 11 22 00 00 00 00 00
7-
# CHECK-NOT: 00000010
6+
# CHECK: 0000000 90 11 22
7+
# CHECK-NEXT: 0000003
88

99
## Check case when linkerscript is used.
1010
# RUN: echo "SECTIONS { . = 0x1000; }" > %t.script
@@ -15,6 +15,31 @@
1515
# RUN: ld.lld -o %t2.out --script %t.script %t --oformat binary
1616
# RUN: od -t x1 -v %t2.out | FileCheck %s
1717

18+
## LMA(.text)=0x100, LMA(.mysec)=0x108. The minimum LMA of all non-empty sections is 0x100.
19+
## We place an output section at its LMA minus 0x100.
20+
# RUN: echo 'SECTIONS { .text 0x100 : {*(.text)} .mysec ALIGN(8) : {*(.mysec*)} }' > %talign.lds
21+
# RUN: ld.lld -T %talign.lds %t --oformat binary -o %talign
22+
# RUN: od -Ax -t x1 %talign | FileCheck %s --check-prefix=ALIGN --ignore-case
23+
24+
# ALIGN: 000000 90 00 00 00 00 00 00 00 11 22
25+
# ALIGN-NEXT: 00000a
26+
27+
## The empty section .data is ignored when computing the file size.
28+
# RUN: echo 'SECTIONS { .text : {*(.text .mysec*)} .data 0x100 : {keep = .;}}' > %tempty.lds
29+
# RUN: ld.lld -T %tempty.lds %t --oformat binary -o %tempty
30+
# RUN: od -t x1 %tempty | FileCheck %s
31+
32+
## NOBITS sections are ignored as well.
33+
# RUN: echo 'SECTIONS { .text : {*(.text .mysec*)} .data 0x100 (NOLOAD) : {BYTE(0)}}' > %tnobits.lds
34+
# RUN: ld.lld -T %tnobits.lds %t --oformat binary -o %tnobits
35+
# RUN: od -t x1 %tnobits | FileCheck %s
36+
37+
## FIXME .mysec should be placed at file offset 1.
38+
## This does not work because for a section without PT_LOAD, we consider LMA = VMA.
39+
# RUN: echo 'SECTIONS { .text : {*(.text)} .mysec 0x8 : AT(1) {*(.mysec*)} }' > %tlma.lds
40+
# RUN: ld.lld -T %tlma.lds %t --oformat binary -o %tlma
41+
# RUN: od -Ax -t x1 %tlma | FileCheck %s --check-prefix=ALIGN --ignore-case
42+
1843
# RUN: not ld.lld -o /dev/null %t --oformat foo 2>&1 \
1944
# RUN: | FileCheck %s --check-prefix ERR
2045
# ERR: unknown --oformat value: foo

0 commit comments

Comments
 (0)