Skip to content

Commit d9e191c

Browse files
committed
Revert "[ELF] Allow sections after a non-SHF_ALLOC section to be covered by PT_LOAD"
This reverts commit 030ddc0. This breaks http://lab.llvm.org:8011/builders/lldb-arm-ubuntu and http://lab.llvm.org:8011/builders/lldb-aarch64-ubuntu Differential Revision: https://reviews.llvm.org/D85100
1 parent 55ead5b commit d9e191c

File tree

5 files changed

+25
-63
lines changed

5 files changed

+25
-63
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,6 @@ static OutputSection *findByName(ArrayRef<BaseCommand *> vec,
586586
static OutputSection *createSection(InputSectionBase *isec,
587587
StringRef outsecName) {
588588
OutputSection *sec = script->createOutputSection(outsecName, "<internal>");
589-
if (!(isec->flags & SHF_ALLOC))
590-
sec->addrExpr = [] { return 0; };
591589
sec->recordSection(isec);
592590
return sec;
593591
}
@@ -850,14 +848,17 @@ static OutputSection *findFirstSection(PhdrEntry *load) {
850848
// This function assigns offsets to input sections and an output section
851849
// for a single sections command (e.g. ".text { *(.text); }").
852850
void LinkerScript::assignOffsets(OutputSection *sec) {
851+
if (!(sec->flags & SHF_ALLOC))
852+
dot = 0;
853+
853854
const bool sameMemRegion = ctx->memRegion == sec->memRegion;
854855
const bool prevLMARegionIsDefault = ctx->lmaRegion == nullptr;
855856
ctx->memRegion = sec->memRegion;
856857
ctx->lmaRegion = sec->lmaRegion;
857858
if (ctx->memRegion)
858859
dot = ctx->memRegion->curPos;
859860

860-
if (sec->addrExpr)
861+
if ((sec->flags & SHF_ALLOC) && sec->addrExpr)
861862
setDot(sec->addrExpr, sec->location, false);
862863

863864
// If the address of the section has been moved forward by an explicit

lld/ELF/Writer.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,13 +1234,7 @@ static bool shouldSkip(BaseCommand *cmd) {
12341234
static std::vector<BaseCommand *>::iterator
12351235
findOrphanPos(std::vector<BaseCommand *>::iterator b,
12361236
std::vector<BaseCommand *>::iterator e) {
1237-
// OutputSections without the SHF_ALLOC flag are not part of the memory image
1238-
// and their addresses usually don't matter. Place any orphan sections without
1239-
// the SHF_ALLOC flag at the end so that these do not affect the address
1240-
// assignment of OutputSections with the SHF_ALLOC flag.
12411237
OutputSection *sec = cast<OutputSection>(*e);
1242-
if (!(sec->flags & SHF_ALLOC))
1243-
return e;
12441238

12451239
// Find the first element that has as close a rank as possible.
12461240
auto i = std::max_element(b, e, [=](BaseCommand *a, BaseCommand *b) {
@@ -2335,6 +2329,8 @@ std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs(Partition &part) {
23352329
}
23362330

23372331
for (OutputSection *sec : outputSections) {
2332+
if (!(sec->flags & SHF_ALLOC))
2333+
break;
23382334
if (!needsPtLoad(sec))
23392335
continue;
23402336

lld/test/ELF/linkerscript/compress-debug-sections-custom.s

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# REQUIRES: x86, zlib
22

3-
## NOTE GNU ld makes .debug_str and .debug_info SHF_ALLOC due to `. += 10`.
43
# RUN: echo "SECTIONS { \
54
# RUN: .text : { . += 0x10; *(.text) } \
6-
# RUN: .debug_str 0 : { . += 0x10; *(.debug_str) } \
7-
# RUN: .debug_info 0 : { . += 0x10; *(.debug_info) } \
5+
# RUN: .debug_str : { . += 0x10; *(.debug_str) } \
6+
# RUN: .debug_info : { . += 0x10; *(.debug_info) } \
87
# RUN: }" > %t.script
98

109
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o

lld/test/ELF/linkerscript/sections.s

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,29 @@
2525
# SEC-DEFAULT: 7 .shstrtab 0000003b {{[0-9a-f]*}}
2626
# SEC-DEFAULT: 8 .strtab 00000008 {{[0-9a-f]*}}
2727

28-
## Sections are placed in the order specified by the linker script. .data has
29-
## a PT_LOAD segment, even if it is preceded by a non-alloc section. To
30-
## allow this, place non-alloc orphan sections at the end and advance
31-
## location counters for non-alloc non-orphan sections.
28+
# Sections are put in order specified in linker script, other than alloc
29+
# sections going first.
3230
# RUN: echo "SECTIONS { \
3331
# RUN: .bss : { *(.bss) } \
3432
# RUN: other : { *(other) } \
3533
# RUN: .shstrtab : { *(.shstrtab) } \
3634
# RUN: .symtab : { *(.symtab) } \
3735
# RUN: .strtab : { *(.strtab) } \
3836
# RUN: .data : { *(.data) } \
39-
# RUN: .text : { *(.text) } }" > %t3.lds
40-
# RUN: ld.lld -o %t3a -T %t3.lds %t
41-
# RUN: llvm-readelf -S -l %t3a | FileCheck --check-prefix=SEC-ORDER %s
42-
# RUN: ld.lld -o %t3b -T %t3.lds --unique %t
43-
# RUN: llvm-readelf -S -l %t3b | FileCheck --check-prefix=SEC-ORDER %s
37+
# RUN: .text : { *(.text) } }" > %t.script
38+
# RUN: ld.lld -o %t3 --script %t.script %t
39+
# RUN: llvm-objdump --section-headers %t3 | \
40+
# RUN: FileCheck -check-prefix=SEC-ORDER %s
4441

45-
# SEC-ORDER: [Nr] Name Type Address Off Size ES Flg
46-
# SEC-ORDER: [ 0] NULL 0000000000000000 000000 000000 00
47-
# SEC-ORDER-NEXT: [ 1] .bss NOBITS 0000000000000000 001000 000002 00 WA
48-
# SEC-ORDER-NEXT: [ 2] other PROGBITS 0000000000000002 001002 000003 00 WA
49-
# SEC-ORDER-NEXT: [ 3] .shstrtab STRTAB 0000000000000005 001005 00003b 00
50-
# SEC-ORDER-NEXT: [ 4] .symtab SYMTAB 0000000000000040 001040 000030 18
51-
# SEC-ORDER-NEXT: [ 5] .strtab STRTAB 0000000000000070 001070 000008 00
52-
# SEC-ORDER-NEXT: [ 6] .data PROGBITS 0000000000000078 001078 000020 00 WA
53-
# SEC-ORDER-NEXT: [ 7] .text PROGBITS 0000000000000098 001098 00000e 00 AX
54-
# SEC-ORDER-NEXT: [ 8] .comment PROGBITS 0000000000000000 0010a6 000008 01 MS
55-
56-
# SEC-ORDER: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
57-
# SEC-ORDER-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000098 0x000098 RW 0x1000
58-
# SEC-ORDER-NEXT: LOAD 0x001098 0x0000000000000098 0x0000000000000098 0x00000e 0x00000e R E 0x1000
59-
# SEC-ORDER-NEXT: GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0
42+
# Idx Name Size
43+
# SEC-ORDER: 1 .bss 00000002 {{[0-9a-f]*}} BSS
44+
# SEC-ORDER: 2 other 00000003 {{[0-9a-f]*}} DATA
45+
# SEC-ORDER: 3 .shstrtab 0000003b {{[0-9a-f]*}}
46+
# SEC-ORDER: 4 .symtab 00000030 {{[0-9a-f]*}}
47+
# SEC-ORDER: 5 .strtab 00000008 {{[0-9a-f]*}}
48+
# SEC-ORDER: 6 .comment 00000008 {{[0-9a-f]*}}
49+
# SEC-ORDER: 7 .data 00000020 {{[0-9a-f]*}} DATA
50+
# SEC-ORDER: 8 .text 0000000e {{[0-9a-f]*}} TEXT
6051

6152
# .text and .data have swapped names but proper sizes and types.
6253
# RUN: echo "SECTIONS { \
@@ -98,28 +89,6 @@
9889
# SEC-MULTI-NEXT: .shstrtab 00000035 {{[0-9a-f]*}}
9990
# SEC-MULTI-NEXT: .strtab 00000008 {{[0-9a-f]*}}
10091

101-
## other is placed in a PT_LOAD segment even if it is preceded by a non-alloc section.
102-
## The current implementation places .data, .bss, .comment and other in the same PT_LOAD.
103-
# RUN: echo 'SECTIONS { \
104-
# RUN: .text : { *(.text) } \
105-
# RUN: .data : { *(.data) } \
106-
# RUN: .comment : { *(.comment) } \
107-
# RUN: other : { *(other) } }' > %t5.lds
108-
# RUN: ld.lld -o %t5 -T %t5.lds %t
109-
# RUN: llvm-readelf -S -l %t5 | FileCheck --check-prefix=SEP-BY-NONALLOC %s
110-
111-
# SEP-BY-NONALLOC: [Nr] Name Type Address Off Size ES Flg
112-
# SEP-BY-NONALLOC: [ 1] .text PROGBITS 0000000000000000 001000 00000e 00 AX
113-
# SEP-BY-NONALLOC-NEXT: [ 2] .data PROGBITS 000000000000000e 00100e 000020 00 WA
114-
# SEP-BY-NONALLOC-NEXT: [ 3] .bss NOBITS 000000000000002e 00102e 000002 00 WA
115-
# SEP-BY-NONALLOC-NEXT: [ 4] .comment PROGBITS 0000000000000030 00102e 000008 01 MS
116-
# SEP-BY-NONALLOC-NEXT: [ 5] other PROGBITS 0000000000000038 001038 000003 00 WA
117-
118-
# SEP-BY-NONALLOC: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
119-
# SEP-BY-NONALLOC-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x00000e 0x00000e R E 0x1000
120-
# SEP-BY-NONALLOC-NEXT: LOAD 0x00100e 0x000000000000000e 0x000000000000000e 0x00002d 0x00002d RW 0x1000
121-
# SEP-BY-NONALLOC-NEXT: GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0
122-
12392
# Input section pattern contains additional semicolon.
12493
# Case found in linux kernel script. Check we are able to parse it.
12594
# RUN: echo "SECTIONS { .text : { ;;*(.text);;S = 0;; } }" > %t.script

lld/test/ELF/linkerscript/symbols-non-alloc.test

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
# REQUIRES: x86
2-
## The address of a symbol assignment after a non-SHF_ALLOC section equals the
3-
## end address of the section.
4-
52
# RUN: echo '.section .nonalloc,""; .quad 0' \
63
# RUN: | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t
74
# RUN: ld.lld -o %t2 --script %s %t
85
# RUN: llvm-objdump --section-headers -t %t2 | FileCheck %s
96

107
# CHECK: Sections:
11-
# CHECK: .nonalloc 00000008 0000000000000120
8+
# CHECK: .nonalloc 00000008 0000000000000000
129

1310
# CHECK: SYMBOL TABLE:
14-
# CHECK: 0000000000000128 g .nonalloc 0000000000000000 Sym
11+
# CHECK: 0000000000000008 g .nonalloc 0000000000000000 Sym
1512

1613
SECTIONS {
1714
. = SIZEOF_HEADERS;

0 commit comments

Comments
 (0)