Skip to content

Commit be01d2e

Browse files
committedMay 8, 2018
New option -z keep-text-section-prefix to keep text sections with prefixes separate.
Separate output sections for selected text section prefixes to enable TLB optimizations and for readablilty. Differential Revision: https://reviews.llvm.org/D45841 llvm-svn: 331823
1 parent 6e76a1b commit be01d2e

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed
 

Diff for: ‎lld/ELF/Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ struct Configuration {
171171
bool ZCopyreloc;
172172
bool ZExecstack;
173173
bool ZHazardplt;
174+
bool ZKeepTextSectionPrefix;
174175
bool ZNodelete;
175176
bool ZNodlopen;
176177
bool ZNow;

Diff for: ‎lld/ELF/Driver.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args) {
764764
Config->ZCopyreloc = getZFlag(Args, "copyreloc", "nocopyreloc", true);
765765
Config->ZExecstack = getZFlag(Args, "execstack", "noexecstack", false);
766766
Config->ZHazardplt = hasZOption(Args, "hazardplt");
767+
Config->ZKeepTextSectionPrefix = getZFlag(
768+
Args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
767769
Config->ZNodelete = hasZOption(Args, "nodelete");
768770
Config->ZNodlopen = hasZOption(Args, "nodlopen");
769771
Config->ZNow = getZFlag(Args, "now", "lazy", false);

Diff for: ‎lld/ELF/Writer.cpp

+19-3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ template <class ELFT> class Writer {
8888
};
8989
} // anonymous namespace
9090

91+
static bool isSectionPrefix(StringRef Prefix, StringRef Name) {
92+
return Name.startswith(Prefix) || Name == Prefix.drop_back();
93+
}
94+
9195
StringRef elf::getOutputSectionName(InputSectionBase *S) {
9296
if (Config->Relocatable)
9397
return S->Name;
@@ -104,13 +108,25 @@ StringRef elf::getOutputSectionName(InputSectionBase *S) {
104108
}
105109
}
106110

111+
// This check is for -z keep-text-section-prefix. This option separates text
112+
// sections with prefix ".text.hot", ".text.unlikely", ".text.startup" or
113+
// ".text.exit".
114+
// When enabled, this allows identifying the hot code region (.text.hot) in
115+
// the final binary which can be selectively mapped to huge pages or mlocked,
116+
// for instance.
117+
if (Config->ZKeepTextSectionPrefix)
118+
for (StringRef V :
119+
{".text.hot.", ".text.unlikely.", ".text.startup.", ".text.exit."}) {
120+
if (isSectionPrefix(V, S->Name))
121+
return V.drop_back();
122+
}
123+
107124
for (StringRef V :
108125
{".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
109126
".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
110127
".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) {
111-
StringRef Prefix = V.drop_back();
112-
if (S->Name.startswith(V) || S->Name == Prefix)
113-
return Prefix;
128+
if (isSectionPrefix(V, S->Name))
129+
return V.drop_back();
114130
}
115131

116132
// CommonSection is identified as "COMMON" in linker scripts.

Diff for: ‎lld/test/ELF/text-section-prefix.s

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# RUN: llvm-mc -filetype=obj %s -o %t
2+
# RUN: ld.lld -z keep-text-section-prefix %t -o %t2
3+
# RUN: llvm-readelf -l %t2 | FileCheck %s
4+
# RUN: ld.lld %t -o %t3
5+
# RUN: llvm-readelf -l %t3 | FileCheck --check-prefix=CHECKNO %s
6+
# RUN: ld.lld -z nokeep-text-section-prefix %t -o %t4
7+
# RUN: llvm-readelf -l %t4 | FileCheck --check-prefix=CHECKNO %s
8+
9+
# CHECK: .text
10+
# CHECK: .text.hot
11+
# CHECK: .text.startup
12+
# CHECK: .text.exit
13+
# CHECK: .text.unlikely
14+
# CHECKNO: .text
15+
# CHECKNO-NOT: .text.hot
16+
17+
_start:
18+
ret
19+
20+
.section .text.f,"ax"
21+
f:
22+
nop
23+
24+
.section .text.hot.f_hot,"ax"
25+
f_hot:
26+
nop
27+
28+
.section .text.startup.f_startup,"ax"
29+
f_startup:
30+
nop
31+
32+
.section .text.exit.f_exit,"ax"
33+
f_exit:
34+
nop
35+
36+
.section .text.unlikely.f_unlikely,"ax"
37+
f_unlikely:
38+
nop

0 commit comments

Comments
 (0)
Please sign in to comment.