Skip to content

Commit 9ad0175

Browse files
committed
[ELF] Keep non-alloc orphan sections at the end
https://reviews.llvm.org/D85867 changed the way we assign file offsets (alloc sections first, then non-alloc sections). It also removed a non-alloc special case from `findOrphanPos`. Looking at the memory-nonalloc-no-warn.test change, which would be needed by #93761, it makes sense to restore the previous behavior: when placing non-alloc orphan sections, keep these sections at the end so that the section index order matches the file offset order. This change is cosmetic. In sections-nonalloc.s, GNU ld places the orphan `other3` in the middle and the orphan .symtab/.shstrtab/.strtab at the end. Pull Request: #94519
1 parent c771b67 commit 9ad0175

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

lld/ELF/Writer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,11 @@ static bool shouldSkip(SectionCommand *cmd) {
921921
static SmallVectorImpl<SectionCommand *>::iterator
922922
findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
923923
SmallVectorImpl<SectionCommand *>::iterator e) {
924+
// Place non-alloc orphan sections at the end. This matches how we assign file
925+
// offsets to non-alloc sections.
924926
OutputSection *sec = &cast<OutputDesc>(*e)->osec;
927+
if (!(sec->flags & SHF_ALLOC))
928+
return e;
925929

926930
// As a special case, place .relro_padding before the SymbolAssignment using
927931
// DATA_SEGMENT_RELRO_END, if present.

lld/test/ELF/linkerscript/memory-nonalloc-no-warn.test

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616
## The output file must include all sections.
1717
# RUN: llvm-readelf -S %t/a.elf | FileCheck %s
1818

19-
# CHECK: There are 12 section headers, starting at offset 0x2140:
19+
# CHECK: There are 12 section headers, starting at offset 0x2138:
2020
# CHECK: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
2121
# CHECK-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 0
2222
# CHECK-NEXT: [ 1] .nonalloc PROGBITS 0000000000000000 001064 001000 00 W 0 0 1
23-
# CHECK-NEXT: [ 2] .comment PROGBITS 0000000000000000 {{.*}} {{.*}} 01 MS 0 0 1
24-
# CHECK-NEXT: [ 3] .symtab SYMTAB 0000000000000000 {{.*}} {{.*}} 18 5 1 8
25-
# CHECK-NEXT: [ 4] .shstrtab STRTAB 0000000000000000 {{.*}} {{.*}} 00 0 0 1
26-
# CHECK-NEXT: [ 5] .strtab STRTAB 0000000000000000 {{.*}} {{.*}} 00 0 0 1
27-
# CHECK-NEXT: [ 6] .dat PROGBITS 0000000000000000 002137 000004 00 W 0 0 1
28-
# CHECK-NEXT: [ 7] .intvec0_out PROGBITS 0000000000000000 00213b 000000 00 W 0 0 1
29-
# CHECK-NEXT: [ 8] .intvec1_out PROGBITS 0000000000000000 00213b 000000 00 W 0 0 1
30-
# CHECK-NEXT: [ 9] .intvec2_out PROGBITS 0000000000000000 00213b 000000 00 W 0 0 1
31-
# CHECK-NEXT: [10] .intvec3_out PROGBITS 00000000803fe060 001060 000004 00 AX 0 0 1
32-
# CHECK-NEXT: [11] .text PROGBITS 00000000803fe064 001064 000000 00 AX 0 0 4
23+
# CHECK-NEXT: [ 2] .dat PROGBITS 0000000000000000 002064 000004 00 W 0 0 1
24+
# CHECK-NEXT: [ 3] .intvec0_out PROGBITS 0000000000000000 002068 000000 00 W 0 0 1
25+
# CHECK-NEXT: [ 4] .intvec1_out PROGBITS 0000000000000000 002068 000000 00 W 0 0 1
26+
# CHECK-NEXT: [ 5] .intvec2_out PROGBITS 0000000000000000 002068 000000 00 W 0 0 1
27+
# CHECK-NEXT: [ 6] .intvec3_out PROGBITS 00000000803fe060 001060 000004 00 AX 0 0 1
28+
# CHECK-NEXT: [ 7] .text PROGBITS 00000000803fe064 001064 000000 00 AX 0 0 4
29+
# CHECK-NEXT: [ 8] .comment PROGBITS 0000000000000000 {{.*}} {{.*}} 01 MS 0 0 1
30+
# CHECK-NEXT: [ 9] .symtab SYMTAB 0000000000000000 {{.*}} {{.*}} 18 11 1 8
31+
# CHECK-NEXT: [10] .shstrtab STRTAB 0000000000000000 {{.*}} {{.*}} 00 0 0 1
32+
# CHECK-NEXT: [11] .strtab STRTAB 0000000000000000 {{.*}} {{.*}} 00 0 0 1
3333

3434

3535
#--- a.s

lld/test/ELF/linkerscript/sections-nonalloc.s

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
# CHECK-NEXT: [ 2] data1 PROGBITS 0000000000000001 001001 000001 00 WA 0
1717
# CHECK-NEXT: [ 3] other1 PROGBITS 0000000000000000 001008 000001 00 0
1818
# CHECK-NEXT: [ 4] other2 PROGBITS 0000000000000000 001010 000001 00 0
19-
## Orphan placement places other3, .symtab, .shstrtab and .strtab after other2.
20-
# CHECK-NEXT: [ 5] other3 PROGBITS 0000000000000000 001020 000001 00 0
21-
# CHECK-NEXT: [ 6] .symtab SYMTAB 0000000000000000 001028 000030 18 8
22-
# CHECK-NEXT: [ 7] .shstrtab STRTAB 0000000000000000 001058 00004d 00 0
23-
# CHECK-NEXT: [ 8] .strtab STRTAB 0000000000000000 0010a5 000008 00 0
24-
# CHECK-NEXT: [ 9] data2 PROGBITS 0000000000000002 001002 000001 00 WA 0
19+
# CHECK-NEXT: [ 5] data2 PROGBITS 0000000000000002 001002 000001 00 WA 0
2520
## max{sortRank(data1),sortRank(data2)} <= sortRank(data3). data3 is placed after the latter.
26-
# CHECK-NEXT: [10] data3 PROGBITS 0000000000000003 001003 000001 00 WA 0
27-
# CHECK-NEXT: [11] .text PROGBITS 0000000000000004 001004 000001 00 AX 0
21+
# CHECK-NEXT: [ 6] data3 PROGBITS 0000000000000003 001003 000001 00 WA 0
22+
# CHECK-NEXT: [ 7] .text PROGBITS 0000000000000004 001004 000001 00 AX 0
23+
## Non-alloc orphan sections other3, .symtab, .shstrtab and .strtab are placed at the end.
24+
# CHECK-NEXT: [ 8] other3 PROGBITS 0000000000000000 001020 000001 00 0
25+
# CHECK-NEXT: [ 9] .symtab SYMTAB 0000000000000000 001028 000030 18 11
26+
# CHECK-NEXT: [10] .shstrtab STRTAB 0000000000000000 001058 00004d 00 0
27+
# CHECK-NEXT: [11] .strtab STRTAB 0000000000000000 0010a5 000008 00 0
2828

2929
# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
3030
# CHECK-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000004 0x000004 RW 0x1000
@@ -41,12 +41,12 @@
4141
# CHECK1-NEXT: [ 3] data1 PROGBITS 00000000000000b2 0000b2 000001 00 WA 0
4242
# CHECK1-NEXT: [ 4] other1 PROGBITS 0000000000000000 0000b8 000001 00 0
4343
# CHECK1-NEXT: [ 5] other2 PROGBITS 0000000000000000 0000c0 000001 00 0
44-
# CHECK1-NEXT: [ 6] other3 PROGBITS 0000000000000000 0000d0 000001 00 0
45-
# CHECK1-NEXT: [ 7] .symtab SYMTAB 0000000000000000 0000d8 000030 18 9
46-
# CHECK1-NEXT: [ 8] .shstrtab STRTAB 0000000000000000 000108 00004d 00 0
47-
# CHECK1-NEXT: [ 9] .strtab STRTAB 0000000000000000 000155 000008 00 0
48-
# CHECK1-NEXT: [10] data2 PROGBITS 00000000000000b3 0000b3 000001 00 WA 0
49-
# CHECK1-NEXT: [11] data3 PROGBITS 00000000000000b4 0000b4 000001 00 WA 0
44+
# CHECK1-NEXT: [ 6] data2 PROGBITS 00000000000000b3 0000b3 000001 00 WA 0
45+
# CHECK1-NEXT: [ 7] data3 PROGBITS 00000000000000b4 0000b4 000001 00 WA 0
46+
# CHECK1-NEXT: [ 8] other3 PROGBITS 0000000000000000 0000d0 000001 00 0
47+
# CHECK1-NEXT: [ 9] .symtab SYMTAB 0000000000000000 0000d8 000030 18 11
48+
# CHECK1-NEXT: [10] .shstrtab STRTAB 0000000000000000 000108 00004d 00 0
49+
# CHECK1-NEXT: [11] .strtab STRTAB 0000000000000000 000155 000008 00 0
5050
# CHECK1: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
5151
# CHECK1-NEXT: LOAD 0x000000 0x0000000000000000 0x0000000000000000 0x0000b5 0x0000b5 RWE 0x1000
5252
# CHECK1-NEXT: 0x60000000 0x0000b8 0x0000000000000000 0x0000000000000000 0x000009 0x000001 0x8

lld/test/ELF/linkerscript/sections.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
# SEP-BY-NONALLOC: [ 1] .text PROGBITS 0000000000000000 001000 00000e 00 AX
8080
# SEP-BY-NONALLOC-NEXT: [ 2] .data PROGBITS 000000000000000e 00100e 000020 00 WA
8181
# SEP-BY-NONALLOC-NEXT: [ 3] .comment PROGBITS 0000000000000000 001031 000008 01 MS
82-
# SEP-BY-NONALLOC: [ 7] other PROGBITS 000000000000002e 00102e 000003 00 WA
83-
# SEP-BY-NONALLOC-NEXT: [ 8] .bss NOBITS 0000000000000031 001031 000002 00 WA
82+
# SEP-BY-NONALLOC: [ 4] other PROGBITS 000000000000002e 00102e 000003 00 WA
83+
# SEP-BY-NONALLOC-NEXT: [ 5] .bss NOBITS 0000000000000031 001031 000002 00 WA
8484

8585
# SEP-BY-NONALLOC: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
8686
# SEP-BY-NONALLOC-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x00000e 0x00000e R E 0x1000

0 commit comments

Comments
 (0)