Skip to content

Commit 27778e5

Browse files
resolve comments
1 parent 9cdf86d commit 27778e5

File tree

5 files changed

+99
-59
lines changed

5 files changed

+99
-59
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ struct Config {
404404
bool zIfuncNoplt;
405405
bool zInitfirst;
406406
bool zInterpose;
407-
bool zKeepTextSectionPrefix;
408407
bool zKeepDataSectionPrefix;
408+
bool zKeepTextSectionPrefix;
409409
bool zLrodataAfterBss;
410410
bool zNoBtCfi;
411411
bool zNodefaultlib;

lld/ELF/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,10 +1625,10 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
16251625
ctx.arg.zIfuncNoplt = hasZOption(args, "ifunc-noplt");
16261626
ctx.arg.zInitfirst = hasZOption(args, "initfirst");
16271627
ctx.arg.zInterpose = hasZOption(args, "interpose");
1628-
ctx.arg.zKeepTextSectionPrefix = getZFlag(
1629-
args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
16301628
ctx.arg.zKeepDataSectionPrefix = getZFlag(
16311629
args, "keep-data-section-prefix", "nokeep-data-section-prefix", false);
1630+
ctx.arg.zKeepTextSectionPrefix = getZFlag(
1631+
args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
16321632
ctx.arg.zLrodataAfterBss =
16331633
getZFlag(args, "lrodata-after-bss", "nolrodata-after-bss", false);
16341634
ctx.arg.zNoBtCfi = hasZOption(args, "nobtcfi");

lld/ELF/Writer.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,12 @@ template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
554554
}
555555

556556
// Returns true if the section is a data section that's read only and
557-
// relocatable.
558-
static bool isRelRoDataSection(Ctx &ctx, StringRef SectionName) {
559-
// If -z keep-data-section-prefix is given, '<section>.hot' and
560-
// '<section>.unlikely' is considered a split of '<section>' based on hotness.
561-
// They should share the same section attributes.
557+
// relocatable per its section name. Note this function doesn't look at section
558+
// sh_type or sh_flags.
559+
static bool isRelRoDataSectionByName(Ctx &ctx, StringRef SectionName) {
560+
// If -z keep-data-section-prefix is given, '<section>.hot' and
561+
// '<section>.unlikely' is considered a split of '<section>' based on
562+
// hotness. They should share the same section attributes.
562563
if (ctx.arg.zKeepDataSectionPrefix) {
563564
if (SectionName.ends_with(".hot"))
564565
SectionName = SectionName.drop_back(4);
@@ -645,7 +646,7 @@ static bool isRelroSection(Ctx &ctx, const OutputSection *sec) {
645646
// magic section names.
646647
StringRef s = sec->name;
647648

648-
bool abiAgnostic = isRelRoDataSection(ctx, s) || s == ".bss.rel.ro" ||
649+
bool abiAgnostic = isRelRoDataSectionByName(ctx, s) || s == ".bss.rel.ro" ||
649650
s == ".ctors" || s == ".dtors" || s == ".jcr" ||
650651
s == ".eh_frame" || s == ".fini_array" ||
651652
s == ".init_array" || s == ".preinit_array";
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# REQUIRES: x86
2+
3+
# RUN: rm -rf %t && split-file %s %t && cd %t
4+
5+
# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux input.s -o a.o
6+
7+
# RUN: ld.lld -z keep-data-section-prefix -T linker-script.lds a.o -o a.out
8+
# RUN: llvm-readelf -l a.out | FileCheck --check-prefixes=SEG,LS %s
9+
# RUN: llvm-readelf -S a.out | FileCheck %s --check-prefix=CHECK-LS
10+
11+
# RUN: ld.lld -z keep-data-section-prefix a.o -o b.out
12+
# RUN: llvm-readelf -l b.out | FileCheck --check-prefixes=SEG,PRE %s
13+
# RUN: llvm-readelf -S b.out | FileCheck %s --check-prefix=CHECK-PRE
14+
15+
# RUN: ld.lld a.o -o c.out
16+
# RUN: llvm-readelf -l c.out | FileCheck --check-prefixes=SEG,PRE %s
17+
# RUN: llvm-readelf -S c.out | FileCheck %s --check-prefix=CHECK-PRE
18+
19+
# RUN: not ld.lld -T linker-script.lds a.o -o d.out 2>&1 | FileCheck %s
20+
# CHECK: error: section: .relro_padding is not contiguous with other relro sections
21+
22+
## The first PW PT_LOAD segment has FileSiz 0x126f (0x1000 + 0x200 + 0x60 + 0xf),
23+
## and its p_offset p_vaddr p_paddr p_filesz should match PT_GNU_RELRO.
24+
# Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
25+
# SEG: LOAD 0x0001c8 0x00000000002011c8 0x00000000002011c8 0x000001 0x000001 R E 0x1000
26+
# SEG-NEXT: LOAD 0x0001c9 0x00000000002021c9 0x00000000002021c9 0x00126f 0x001e37 RW 0x1000
27+
# SEG-NEXT: LOAD 0x001438 0x0000000000204438 0x0000000000204438 0x000001 0x000002 RW 0x1000
28+
# SEG-NEXT: GNU_RELRO 0x0001c9 0x00000000002021c9 0x00000000002021c9 0x00126f 0x001e37 R 0x1
29+
# SEG-NEXT: GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0x0
30+
31+
## Input to output mapping per linker script
32+
## .data.rel.ro.split -> .data.rel.ro
33+
## .data.rel.ro -> .data.rel.ro
34+
## .data.rel.ro.hot -> .data.rel.ro.hot
35+
## .data.rel.ro.unlikely -> .data.rel.ro.unlikely
36+
# LS: .text
37+
# LS-NEXT: .data.rel.ro.hot .data.rel.ro .data.rel.ro.unlikely .relro_padding
38+
# LS-NEXT: .data .bss
39+
40+
# [Nr] Name Type Address Off Size
41+
# CHECK-LS: .data.rel.ro.hot PROGBITS 00000000002021c9 0001c9 00000f
42+
# CHECK-LS-NEXT: .data.rel.ro PROGBITS 00000000002021d8 0001d8 000260
43+
# CHECK-LS-NEXT: .data.rel.ro.unlikely PROGBITS 0000000000202438 000438 001000
44+
# CHECK-LS-NEXT: .relro_padding NOBITS 0000000000203438 001438 000bc8
45+
# CHECK-LS-NEXT: .data PROGBITS 0000000000204438 001438 000001
46+
# CHECK-LS-NEXT: .bss NOBITS 0000000000204439 001439 000001
47+
48+
## Linker script is not provided to map data sections.
49+
## So all input sections with prefix .data.rel.ro will map to .data.rel.ro in the output.
50+
# PRE: .text
51+
# PRE-NEXT: .data.rel.ro .relro_padding
52+
# PRE-NEXT: .data .bss
53+
54+
# [Nr] Name Type Address Off Size
55+
# CHECK-PRE: .data.rel.ro PROGBITS 00000000002021c9 0001c9 00126f
56+
# CHECK-PRE-NEXT: .relro_padding NOBITS 0000000000203438 001438 000bc8
57+
# CHECK-PRE-NEXT: .data PROGBITS 0000000000204438 001438 000001
58+
# CHECK-PRE-NEXT: .bss NOBITS 0000000000204439 001439 000001
59+
60+
#--- linker-script.lds
61+
SECTIONS {
62+
.data.rel.ro.hot : { *(.data.rel.ro.hot) }
63+
.data.rel.ro : { .data.rel.ro }
64+
.data.rel.ro.unlikely : { *(.data.rel.ro.unlikely) }
65+
} INSERT AFTER .text
66+
67+
68+
#--- input.s
69+
.globl _start
70+
_start:
71+
ret
72+
73+
.section .data.rel.ro.hot, "aw"
74+
.space 15
75+
76+
.section .data.rel.ro, "aw"
77+
.space 96
78+
79+
.section .data.rel.ro.split,"aw"
80+
.space 512
81+
82+
.section .data.rel.ro.unlikely, "aw"
83+
.space 4096
84+
85+
.section .data, "aw"
86+
.space 1
87+
88+
.section .bss, "aw"
89+
.space 1

lld/test/ELF/relro-data-unlikely.s

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)