Skip to content

Commit 24c1c62

Browse files
lld keep data section prefix
1 parent 3b3d1a5 commit 24c1c62

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

lld/ELF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ struct Config {
372372
bool zInitfirst;
373373
bool zInterpose;
374374
bool zKeepTextSectionPrefix;
375+
bool zKeepDataSectionPrefix;
375376
bool zLrodataAfterBss;
376377
bool zNoBtCfi;
377378
bool zNodefaultlib;

lld/ELF/Driver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,10 @@ static void readConfigs(Ctx &ctx, opt::InputArgList &args) {
15661566
ctx.arg.zInterpose = hasZOption(args, "interpose");
15671567
ctx.arg.zKeepTextSectionPrefix = getZFlag(
15681568
args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
1569+
ctx.arg.zKeepDataSectionPrefix = getZFlag(
1570+
args, "keep-data-section-prefix", "nokeep-data-section-prefix", false);
1571+
errs() << "ELF/Driver.cpp:1540\t" << ctx.arg.zKeepDataSectionPrefix << "\n";
1572+
15691573
ctx.arg.zLrodataAfterBss =
15701574
getZFlag(args, "lrodata-after-bss", "nolrodata-after-bss", false);
15711575
ctx.arg.zNoBtCfi = hasZOption(args, "nobtcfi");

lld/ELF/LinkerScript.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ 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");
54+
}
55+
5156
StringRef LinkerScript::getOutputSectionName(const InputSectionBase *s) const {
5257
// This is for --emit-relocs and -r. If .text.foo is emitted as .text.bar, we
5358
// want to emit .rela.text.foo as .rela.text.bar for consistency (this is not
@@ -105,6 +110,70 @@ StringRef LinkerScript::getOutputSectionName(const InputSectionBase *s) const {
105110
return ".text";
106111
}
107112

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+
}
164+
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";
170+
}
171+
172+
return ".bss";
173+
}
174+
return ".bss";
175+
}
176+
108177
for (StringRef v : {".data.rel.ro", ".data", ".rodata",
109178
".bss.rel.ro", ".bss", ".ldata",
110179
".lrodata", ".lbss", ".gcc_except_table",

0 commit comments

Comments
 (0)