Skip to content

Commit 6328b71

Browse files
simplify implementation
1 parent 24c1c62 commit 6328b71

File tree

3 files changed

+33
-73
lines changed

3 files changed

+33
-73
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,8 @@ struct Config {
371371
bool zIfuncNoplt;
372372
bool zInitfirst;
373373
bool zInterpose;
374-
bool zKeepTextSectionPrefix;
375374
bool zKeepDataSectionPrefix;
375+
bool zKeepTextSectionPrefix;
376376
bool zLrodataAfterBss;
377377
bool zNoBtCfi;
378378
bool zNodefaultlib;

lld/ELF/Driver.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,11 +1564,10 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
15641564
ctx.arg.zIfuncNoplt = hasZOption(args, "ifunc-noplt");
15651565
ctx.arg.zInitfirst = hasZOption(args, "initfirst");
15661566
ctx.arg.zInterpose = hasZOption(args, "interpose");
1567-
ctx.arg.zKeepTextSectionPrefix = getZFlag(
1568-
args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
15691567
ctx.arg.zKeepDataSectionPrefix = getZFlag(
15701568
args, "keep-data-section-prefix", "nokeep-data-section-prefix", false);
1571-
errs() << "ELF/Driver.cpp:1540\t" << ctx.arg.zKeepDataSectionPrefix << "\n";
1569+
ctx.arg.zKeepTextSectionPrefix = getZFlag(
1570+
args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
15721571

15731572
ctx.arg.zLrodataAfterBss =
15741573
getZFlag(args, "lrodata-after-bss", "nolrodata-after-bss", false);

lld/ELF/LinkerScript.cpp

Lines changed: 30 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ static bool isSectionPrefix(StringRef prefix, StringRef name) {
4848
return name.consume_front(prefix) && (name.empty() || name[0] == '.');
4949
}
5050

51-
bool hasUnlikelySuffix(StringRef name) {
52-
return name.ends_with(".unlikely") || name.ends_with(".unlikely.") ||
53-
name.ends_with("unlikely");
51+
static bool isSectionSuffix(StringRef suffix, StringRef name) {
52+
name.consume_back(".");
53+
return name.ends_with(suffix);
5454
}
5555

5656
StringRef LinkerScript::getOutputSectionName(const InputSectionBase *s) const {
@@ -110,77 +110,38 @@ StringRef LinkerScript::getOutputSectionName(const InputSectionBase *s) const {
110110
return ".text";
111111
}
112112

113-
if (isSectionPrefix(".data.rel.ro", s->name)) {
114-
// Map input sections' .rodata and rodata.hot into .rodata.hot in the
115-
// output. Map input sections' .rodata.unlikely into .rodata in the output.
116-
if (ctx.arg.zKeepDataSectionPrefix) {
117-
if (isSectionPrefix(".data.rel.ro.unlikely", s->name) ||
118-
hasUnlikelySuffix(s->name)) {
119-
return ".data.rel.ro.unlikely";
120-
}
121-
122-
return ".data.rel.ro";
123-
}
124-
return ".data.rel.ro";
125-
}
126-
127-
if (isSectionPrefix(".data", s->name)) {
128-
// Map input sections' .rodata and rodata.hot into .rodata.hot in the
129-
// output. Map input sections' .rodata.unlikely into .rodata in the output.
130-
if (ctx.arg.zKeepDataSectionPrefix) {
131-
if (isSectionPrefix(".data.unlikely", s->name) ||
132-
hasUnlikelySuffix(s->name)) {
133-
// errs() << "LinkerScript.cpp:100\t" << s->name << "\n";
134-
return ".data.unlikely";
135-
}
136-
137-
return ".data";
138-
}
139-
return ".data";
140-
}
141-
142-
if (isSectionPrefix(".rodata", s->name)) {
143-
// Map input sections' .rodata and rodata.hot into .rodata.hot in the
144-
// output. Map input sections' .rodata.unlikely into .rodata in the output.
145-
if (ctx.arg.zKeepDataSectionPrefix) {
146-
// if (isSectionPrefix(".rodata.cst", s->name)) {
147-
// errs() << "LinkerScript.cpp:100\t" << s->name << "\n";
148-
//}
149-
// .rodata.cst<N>.unlikely
150-
// .rodata.str4.16.unlikely
151-
if (isSectionPrefix(".rodata.unlikely", s->name) ||
152-
hasUnlikelySuffix(s->name)) {
153-
// return ".rodata.unlikely";
154-
return ".rodata.unlikely";
155-
}
156-
return ".rodata";
157-
}
158-
return ".rodata";
159-
}
160-
161-
if (isSectionPrefix(".bss.rel.ro", s->name)) {
162-
return ".bss.rel.ro";
163-
}
113+
// When zKeepDataSectionPrefix is true, keep .hot and .unlikely suffixes
114+
// in data sections.
115+
static constexpr StringRef dataSectionPrefixes[] = {
116+
".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
117+
};
164118

165-
if (isSectionPrefix(".bss", s->name)) {
166-
if (ctx.arg.zKeepDataSectionPrefix) {
167-
if (isSectionPrefix(".bss.unlikely", s->name)) {
168-
// errs() << "LinkerScript.cpp:100\t" << s->name << "\n";
169-
return ".bss.unlikely";
119+
for (auto [index, v] : llvm::enumerate(dataSectionPrefixes)) {
120+
if (isSectionPrefix(v, s->name)) {
121+
if (ctx.arg.zKeepDataSectionPrefix) {
122+
if (isSectionPrefix(".hot", s->name.substr(v.size())))
123+
return s->name.substr(0, v.size() + 4);
124+
if (isSectionPrefix(".unlikely", s->name.substr(v.size())))
125+
return s->name.substr(0, v.size() + 9);
126+
// for .rodata, a section could be`.rodata.cst<N>.hot.` for constant
127+
// pool or `rodata.str<N>.hot.` for string literals.
128+
if (index == 2) {
129+
if (isSectionSuffix(".hot", s->name)) {
130+
return ".rodata.hot";
131+
}
132+
if (isSectionSuffix(".unlikely", s->name)) {
133+
return ".rodata.unlikely";
134+
}
135+
}
170136
}
171-
172-
return ".bss";
137+
return v;
173138
}
174-
return ".bss";
175139
}
176140

177-
for (StringRef v : {".data.rel.ro", ".data", ".rodata",
178-
".bss.rel.ro", ".bss", ".ldata",
179-
".lrodata", ".lbss", ".gcc_except_table",
180-
".init_array", ".fini_array", ".tbss",
181-
".tdata", ".ARM.exidx", ".ARM.extab",
182-
".ctors", ".dtors", ".sbss",
183-
".sdata", ".srodata"})
141+
for (StringRef v :
142+
{".ldata", ".lrodata", ".lbss", ".gcc_except_table", ".init_array",
143+
".fini_array", ".tbss", ".tdata", ".ARM.exidx", ".ARM.extab", ".ctors",
144+
".dtors", ".sbss", ".sdata", ".srodata"})
184145
if (isSectionPrefix(v, s->name))
185146
return v;
186147

0 commit comments

Comments
 (0)