@@ -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+
5156StringRef 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