Skip to content

Commit 8c7c536

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.5-bogner
1 parent 9f438e0 commit 8c7c536

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

lld/ELF/Writer.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,17 +1615,31 @@ template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
16151615
sec->addr = 0;
16161616

16171617
// 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-
<< ")";
1618+
// Warn because this is error-prone. In addition, warn if the address
1619+
// is smaller than the image base when SECTIONS is absent (e.g. when -Ttext
1620+
// is specified and smaller than the default target image base for no-pie).
1621+
uint64_t imageBase = ctx.script->hasSectionsCommand || ctx.arg.relocatable
1622+
? 0
1623+
: ctx.target->getImageBase();
1624+
for (SectionCommand *cmd : ctx.script->sectionCommands) {
1625+
auto *osd = dyn_cast<OutputDesc>(cmd);
1626+
if (!osd)
1627+
continue;
1628+
OutputSection *osec = &osd->osec;
1629+
if (osec->addr < imageBase && (osec->flags & SHF_ALLOC)) {
1630+
Warn(ctx) << "section '" << osec->name << "' address (0x"
1631+
<< Twine::utohexstr(osec->addr)
1632+
<< ") is smaller than image base (0x"
1633+
<< Twine::utohexstr(imageBase) << "); specify --image-base";
16271634
}
16281635

1636+
if (osec->addr % osec->addralign != 0)
1637+
Warn(ctx) << "address (0x" << Twine::utohexstr(osec->addr)
1638+
<< ") of section " << osec->name
1639+
<< " is not a multiple of alignment (" << osec->addralign
1640+
<< ")";
1641+
}
1642+
16291643
// Sizes are no longer allowed to grow, so all allowable spills have been
16301644
// taken. Remove any leftover potential spills.
16311645
ctx.script->erasePotentialSpillSections();

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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
33
# RUN: ld.lld %t.o --section-start .text=0x100000 \
4-
# RUN: --section-start=.data=0x110000 --section-start .bss=0x200000 -o %t
4+
# RUN: --section-start=.data=0x110000 --section-start .bss=0x200000 -o %t 2>&1 | \
5+
# RUN: FileCheck %s --check-prefix=LINK --implicit-check-not=warning:
56
# RUN: llvm-objdump --section-headers %t | FileCheck %s
67

8+
# LINK: warning: section '.text' address (0x100000) is smaller than image base (0x200000); specify --image-base
9+
# LINK-NEXT: warning: section '.data' address (0x110000) is smaller than image base (0x200000); specify --image-base
10+
711
# CHECK: Sections:
812
# CHECK-NEXT: Idx Name Size VMA Type
913
# CHECK-NEXT: 0 00000000 0000000000000000
1014
# CHECK-NEXT: 1 .text 00000001 0000000000100000 TEXT
1115
# CHECK-NEXT: 2 .data 00000004 0000000000110000 DATA
1216
# CHECK-NEXT: 3 .bss 00000004 0000000000200000 BSS
1317

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
18+
## The warnings go away when the image base is 0.
19+
# RUN: ld.lld %t.o -pie --section-start .text=0x100000 \
20+
# RUN: --section-start=.data=0x110000 --section-start .bss=0x200000 -o %t --noinhibit-exec
21+
# RUN: llvm-objdump --section-headers %t | FileCheck %s
22+
23+
## The same, but dropped "0x" prefix. Specify a smaller --image-base to suppress warnings.
24+
# RUN: ld.lld %t.o --image-base=0x90000 --section-start .text=100000 \
25+
# RUN: --section-start .data=110000 --section-start .bss=0x200000 -o %t1 --noinhibit-exec
1726
# RUN: llvm-objdump --section-headers %t1 | FileCheck %s
1827

1928
## Use -Ttext, -Tdata, -Tbss as replacement for --section-start:

0 commit comments

Comments
 (0)