@@ -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
5656StringRef 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