Skip to content

Commit 0edc8b5

Browse files
authored
[ELF] Error if a section address is smaller than image base
When using `-no-pie` without a `SECTIONS` command, the linker uses the target's default image base. If `-Ttext=` or `--section-start` specifies an output section address below this base, the result is likely unintended. - With `--no-rosegment`, the PT_LOAD segment covering the ELF header cannot include `.text` if `.text`'s address is too low, causing an `error: output file too large`. - With default `--rosegment`: - If a read-only section (e.g., `.rodata`) exists, a similar `error: output file too large` occurs. - Without read-only sections, the PT_LOAD segment covering the ELF header and program headers includes no sections, which is unusual and likely undesired. This also causes non-ascending PT_LOAD `p_vaddr` values related to the PT_LOAD that overlaps with PT_PHDR (#138584). To prevent these issues, report an error if a section address is below the image base and suggest `--image-base`. This check also applies when `--image-base` is explicitly set but is skipped when a `SECTIONS` command is used. Pull Request: #140187
1 parent 81b8135 commit 0edc8b5

File tree

6 files changed

+65
-19
lines changed

6 files changed

+65
-19
lines changed

lld/ELF/Writer.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,18 +1614,33 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
16141614
for (OutputSection *sec : ctx.outputSections)
16151615
sec->addr = 0;
16161616

1617-
// If addrExpr is set, the address may not be a multiple of the alignment.
1618-
// Warn because this is error-prone.
1619-
for (SectionCommand *cmd : ctx.script->sectionCommands)
1620-
if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
1621-
OutputSection *osec = &osd->osec;
1622-
if (osec->addr % osec->addralign != 0)
1623-
Warn(ctx) << "address (0x" << Twine::utohexstr(osec->addr)
1624-
<< ") of section " << osec->name
1625-
<< " is not a multiple of alignment (" << osec->addralign
1626-
<< ")";
1617+
uint64_t imageBase = ctx.script->hasSectionsCommand || ctx.arg.relocatable
1618+
? 0
1619+
: ctx.target->getImageBase();
1620+
for (SectionCommand *cmd : ctx.script->sectionCommands) {
1621+
auto *osd = dyn_cast<OutputDesc>(cmd);
1622+
if (!osd)
1623+
continue;
1624+
OutputSection *osec = &osd->osec;
1625+
// Error if the address is below the image base when SECTIONS is absent
1626+
// (e.g. when -Ttext is specified and smaller than the default target image
1627+
// base for no-pie).
1628+
if (osec->addr < imageBase && (osec->flags & SHF_ALLOC)) {
1629+
Err(ctx) << "section '" << osec->name << "' address (0x"
1630+
<< Twine::utohexstr(osec->addr)
1631+
<< ") is smaller than image base (0x"
1632+
<< Twine::utohexstr(imageBase) << "); specify --image-base";
16271633
}
16281634

1635+
// If addrExpr is set, the address may not be a multiple of the alignment.
1636+
// Warn because this is error-prone.
1637+
if (osec->addr % osec->addralign != 0)
1638+
Warn(ctx) << "address (0x" << Twine::utohexstr(osec->addr)
1639+
<< ") of section " << osec->name
1640+
<< " is not a multiple of alignment (" << osec->addralign
1641+
<< ")";
1642+
}
1643+
16291644
// Sizes are no longer allowed to grow, so all allowable spills have been
16301645
// taken. Remove any leftover potential spills.
16311646
ctx.script->erasePotentialSpillSections();

lld/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ Breaking changes
6464
in the same segment by default. Pass ``--xosegment`` to lld in order to get
6565
the old behavior back.
6666

67+
* When using ``--no-pie`` without a ``SECTIONS`` command, the linker uses the
68+
target's default image base. If ``-Ttext=`` or ``--section-start`` specifies
69+
an output section address below this base, there will now be an error.
70+
``--image-base`` can be set at a lower address to fix the error.
71+
(`#140187 <https://github.com/llvm/llvm-project/pull/140187>`_)
72+
6773
COFF Improvements
6874
-----------------
6975

lld/test/ELF/linkerscript/out-of-order.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131
# CHECK-NEXT: 5 .hash 00000010 000000000000201c
3232
# CHECK-NEXT: 6 .text 00000008 000000000000202c
3333

34+
# RUN: ld.lld -e 0 -o %t --script %t.script %t.o --fatal-warnings
35+
# RUN: llvm-readelf -Sl %t | FileCheck %s --check-prefix=CHECK1
36+
37+
# CHECK1: Name Type Address Off Size ES Flg Lk Inf Al
38+
# CHECK1-NEXT: NULL 0000000000000000 000000 000000 00 0 0 0
39+
# CHECK1-NEXT: .text PROGBITS 0000000000000000 001000 000008 00 AX 0 0 4
40+
# CHECK1-NEXT: .data PROGBITS 0000000000004000 002000 000008 00 WA 0 0 1
41+
# CHECK1: Program Headers:
42+
# CHECK1-NEXT: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
43+
# CHECK1-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000008 0x000008 R E 0x1000
44+
# CHECK1-NEXT: LOAD 0x002000 0x0000000000004000 0x0000000000004000 0x000008 0x000008 RW 0x1000
45+
3446
.quad 0
3547
.data
3648
.quad 0

lld/test/ELF/linkerscript/section-align2.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# RUN: llvm-readelf -S %t | FileCheck %s
1111

1212
## Check we don't warn in the absence of SECTIONS.
13-
# RUN: ld.lld --fatal-warnings -Ttext=0x10000 %t.o -o /dev/null
13+
# RUN: ld.lld --fatal-warnings -Ttext=0x10000 --image-base=0x10000 %t.o -o /dev/null
1414

1515
# WARN: warning: address (0x10004) of section .data.rel.ro is not a multiple of alignment (16)
1616
# WARN: warning: address (0x20001) of section .data2 is not a multiple of alignment (8)

lld/test/ELF/sectionstart.s

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,42 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
3+
# RUN: not ld.lld %t.o --section-start .text=0x100000 \
4+
# RUN: --section-start=.data=0x110000 --section-start .bss=0x200000
35
# RUN: ld.lld %t.o --section-start .text=0x100000 \
4-
# RUN: --section-start=.data=0x110000 --section-start .bss=0x200000 -o %t
6+
# RUN: --section-start=.data=0x110000 --section-start .bss=0x200000 --noinhibit-exec -o %t 2>&1 | \
7+
# RUN: FileCheck %s --check-prefix=LINK --implicit-check-not=warning:
58
# RUN: llvm-objdump --section-headers %t | FileCheck %s
69

10+
# LINK: warning: section '.text' address (0x100000) is smaller than image base (0x200000); specify --image-base
11+
# LINK-NEXT: warning: section '.data' address (0x110000) is smaller than image base (0x200000); specify --image-base
12+
713
# CHECK: Sections:
814
# CHECK-NEXT: Idx Name Size VMA Type
915
# CHECK-NEXT: 0 00000000 0000000000000000
1016
# CHECK-NEXT: 1 .text 00000001 0000000000100000 TEXT
1117
# CHECK-NEXT: 2 .data 00000004 0000000000110000 DATA
1218
# CHECK-NEXT: 3 .bss 00000004 0000000000200000 BSS
1319

14-
## The same, but dropped "0x" prefix.
15-
# RUN: ld.lld %t.o --section-start .text=100000 \
16-
# RUN: --section-start .data=110000 --section-start .bss=0x200000 -o %t1
20+
## The errors go away when the image base is 0.
21+
# RUN: ld.lld %t.o -pie --section-start .text=0x100000 \
22+
# RUN: --section-start=.data=0x110000 --section-start .bss=0x200000 -o %t 2>&1 | count 0
23+
# RUN: llvm-objdump --section-headers %t | FileCheck %s
24+
25+
## The same, but dropped "0x" prefix. Specify a smaller --image-base to suppress warnings.
26+
# RUN: ld.lld %t.o --image-base=0x90000 --section-start .text=100000 \
27+
# RUN: --section-start .data=110000 --section-start .bss=0x200000 -o %t1 2>&1 | count 0
1728
# RUN: llvm-objdump --section-headers %t1 | FileCheck %s
1829

1930
## Use -Ttext, -Tdata, -Tbss as replacement for --section-start:
20-
# RUN: ld.lld %t.o -Ttext=0x100000 -Tdata=0x110000 -Tbss=0x200000 -o %t4
31+
# RUN: ld.lld %t.o --image-base=0x90000 -Ttext=0x100000 -Tdata=0x110000 -Tbss=0x200000 -o %t4
2132
# RUN: llvm-objdump --section-headers %t4 | FileCheck %s
2233

2334
## The same, but dropped "0x" prefix.
24-
# RUN: ld.lld %t.o -Ttext=100000 -Tdata=110000 -Tbss=200000 -o %t5
35+
# RUN: ld.lld %t.o --image-base=0x90000 -Ttext=100000 -Tdata=110000 -Tbss=200000 -o %t5
2536
# RUN: llvm-objdump --section-headers %t5 | FileCheck %s
2637

2738
## Check form without assignment:
28-
# RUN: ld.lld %t.o -Ttext 0x100000 -Tdata 0x110000 -Tbss 0x200000 -o %t4
39+
# RUN: ld.lld %t.o --image-base=0x90000 -Ttext 0x100000 -Tdata 0x110000 -Tbss 0x200000 -o %t4
2940
# RUN: llvm-objdump --section-headers %t4 | FileCheck %s
3041

3142
## Errors:

lld/test/ELF/ttext-tdata-tbss.s

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
## If -Ttext is smaller than the image base (which defaults to 0x200000 for -no-pie),
1717
## the headers will still be allocated, but mapped at a higher address,
1818
## which may look strange.
19-
# RUN: ld.lld -Ttext 0x0 -Tdata 0x4000 -Tbss 0x8000 %t.o -o %t2
19+
# RUN: ld.lld -Ttext 0x0 -Tdata 0x4000 -Tbss 0x8000 %t.o --noinhibit-exec -o %t2 2>&1 | FileCheck %s --check-prefix=LINK1
2020
# RUN: llvm-readelf -S -l %t2 | FileCheck %s --check-prefix=USER1
21+
# LINK1: warning: section '.text' address (0x0) is smaller than image base (0x200000); specify --image-base
22+
# LINK1-NEXT: warning: section '.data' address (0x4000) is smaller than image base (0x200000); specify --image-base
2123
# USER1: .text PROGBITS 0000000000000000 001000 000001
2224
# USER1-NEXT: .data PROGBITS 0000000000004000 002000 000008
2325
# USER1-NEXT: .bss NOBITS 0000000000008000 002008 000008

0 commit comments

Comments
 (0)