Skip to content

Commit 3ac426f

Browse files
committed
lld: relocate PHDR if sectionStartMap is not empty
1 parent c50cba6 commit 3ac426f

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lld/ELF/Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ template <class ELFT> void Writer<ELFT>::run() {
331331
for (OutputSection *sec : ctx.outputSections)
332332
sec->maybeCompress<ELFT>(ctx);
333333

334-
if (ctx.script->hasSectionsCommand)
334+
if (ctx.script->hasSectionsCommand || !ctx.arg.sectionStartMap.empty())
335335
ctx.script->allocateHeaders(ctx.mainPart->phdrs);
336336

337337
// Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a

lld/test/ELF/sectionstart.s

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@
1111
# CHECK-NEXT: 2 .data 00000004 0000000000110000 DATA
1212
# CHECK-NEXT: 3 .bss 00000004 0000000000200000 BSS
1313

14+
## Check that PHDRS are allocated below .text if .text is below default
15+
## base for non-pie case
16+
17+
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
18+
# RUN: ld.lld %t.o -Ttext=0x3000 -o %t
19+
# RUN: llvm-readelf -l %t | FileCheck --check-prefix=CHECK-TEXT %s
20+
21+
# CHECK-TEXT: Program Headers:
22+
# CHECK-TEXT-NEXT: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
23+
# CHECK-TEXT-NEXT: PHDR 0x000040 0x0000000000002040 0x0000000000002040 0x000118 0x000118 R 0x8
24+
# CHECK-TEXT-NEXT: LOAD 0x000000 0x0000000000002000 0x0000000000002000 0x000158 0x000158 R 0x1000
25+
# CHECK-TEXT-NEXT: LOAD 0x001000 0x0000000000003000 0x0000000000003000 0x000001 0x000001 R E 0x1000
26+
27+
## Check that PHDRS are deleted if they don't fit
28+
29+
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
30+
# RUN: ld.lld %t.o -Ttext=0x0 -o %t
31+
# RUN: llvm-readelf -l %t | FileCheck --check-prefix=CHECK-TEXT-ZERO %s
32+
33+
# CHECK-TEXT-ZERO: Program Headers:
34+
# CHECK-TEXT-ZERO-NEXT: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
35+
# CHECK-TEXT-ZERO-NEXT: LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000001 0x000001 R E 0x1000
36+
# CHECK-TEXT-ZERO-NEXT: LOAD 0x001001 0x0000000000001001 0x0000000000001001 0x000004 0x000008 RW 0x1000
37+
1438
## The same, but dropped "0x" prefix.
1539
# RUN: ld.lld %t.o --section-start .text=100000 \
1640
# RUN: --section-start .data=110000 --section-start .bss=0x200000 -o %t1

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
# USER1-NEXT: .rodata PROGBITS 0000000000009008 002008 000008
2525
# USER1-NEXT: .aw PROGBITS 000000000000a010 002010 000008
2626
# USER1: Type
27-
# USER1-NEXT: PHDR 0x000040 0x0000000000200040
28-
# USER1-NEXT: LOAD 0x000000 0x0000000000200000
2927
# USER1-NEXT: LOAD 0x001000 0x0000000000000000
3028

3129
## Specify --image-base to make program headers look normal.

0 commit comments

Comments
 (0)