Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -167,6 +167,7 @@ bool WriteAddends; bool ZCombreloc; bool ZCopyreloc; + bool ZKeepTextSectionPrefix; bool ZExecstack; bool ZHazardplt; bool ZNodelete; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -761,6 +761,8 @@ Args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true); Config->ZCombreloc = getZFlag(Args, "combreloc", "nocombreloc", true); Config->ZCopyreloc = getZFlag(Args, "copyreloc", "nocopyreloc", true); + Config->ZKeepTextSectionPrefix = getZFlag( + Args, "keep-text-section-prefix", "nokeep-text-section-prefix", false); Config->ZExecstack = getZFlag(Args, "execstack", "noexecstack", false); Config->ZHazardplt = hasZOption(Args, "hazardplt"); Config->ZNodelete = hasZOption(Args, "nodelete"); Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -88,6 +88,10 @@ }; } // anonymous namespace +static bool isSectionPrefix(StringRef Prefix, StringRef Name) { + return Name.startswith(Prefix) || Name == Prefix.drop_back(); +} + StringRef elf::getOutputSectionName(InputSectionBase *S) { if (Config->Relocatable) return S->Name; @@ -104,13 +108,23 @@ } } + // This check is for -z keep-text-section-prefix. This option separates text + // sections with prefix ".text.hot", ".text.unlikely", ".text.startup" or + // ".text.exit". + if (Config->ZKeepTextSectionPrefix) { + for (StringRef V : + {".text.hot.", ".text.unlikely.", ".text.startup.", ".text.exit."}) { + if (isSectionPrefix(V, S->Name)) + return V.drop_back(); + } + } + for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.", ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) { - StringRef Prefix = V.drop_back(); - if (S->Name.startswith(V) || S->Name == Prefix) - return Prefix; + if (isSectionPrefix(V, S->Name)) + return V.drop_back(); } // CommonSection is identified as "COMMON" in linker scripts. Index: test/ELF/text-section-prefix.s =================================================================== --- test/ELF/text-section-prefix.s +++ test/ELF/text-section-prefix.s @@ -0,0 +1,32 @@ +# RUN: llvm-mc -filetype=obj %s -o %t +# RUN: ld.lld -z keep-text-section-prefix %t -o %t2 +# RUN: llvm-readelf -l %t2 | FileCheck %s + +# CHECK: .text +# CHECK: .text.hot +# CHECK: .text.startup +# CHECK: .text.exit +# CHECK: .text.unlikely + +_start: + ret + +.section .text.f,"ax" +f: + nop + +.section .text.hot.f_hot,"ax" +f_hot: + nop + +.section .text.startup.f_startup,"ax" +f_startup: + nop + +.section .text.exit.f_exit,"ax" +f_exit: + nop + +.section .text.unlikely.f_unlikely,"ax" +f_unlikely: + nop