Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -345,7 +345,7 @@ for (ObjectFile *F : Symtab::X->getObjectFiles()) for (InputSectionBase *S : F->getSections()) if (!isDiscarded(S) && !S->OutSec) - addSection(Factory, S, getOutputSectionName(S)); + addSection(Factory, S, getOutputSectionName(S->Name)); } // Sets value of a section-defined symbol. Two kinds of Index: lld/trunk/ELF/Writer.h =================================================================== --- lld/trunk/ELF/Writer.h +++ lld/trunk/ELF/Writer.h @@ -41,8 +41,7 @@ bool HasLMA = false; }; -template -llvm::StringRef getOutputSectionName(InputSectionBase *S); +llvm::StringRef getOutputSectionName(llvm::StringRef Name); template void reportDiscarded(InputSectionBase *IS); Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -89,9 +89,10 @@ }; } // anonymous namespace -template -StringRef elf::getOutputSectionName(InputSectionBase *S) { - StringRef Name = S->Name; +StringRef elf::getOutputSectionName(StringRef Name) { + if (Config->Relocatable) + return Name; + for (StringRef V : {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", @@ -712,7 +713,7 @@ } OutputSectionBase *Sec; bool IsNew; - std::tie(Sec, IsNew) = Factory.create(IS, getOutputSectionName(IS)); + std::tie(Sec, IsNew) = Factory.create(IS, getOutputSectionName(IS->Name)); if (IsNew) OutputSections.push_back(Sec); Sec->addSection(IS); @@ -1444,11 +1445,6 @@ template bool elf::isRelroSection(OutputSectionBase *); template bool elf::isRelroSection(OutputSectionBase *); -template StringRef elf::getOutputSectionName(InputSectionBase *); -template StringRef elf::getOutputSectionName(InputSectionBase *); -template StringRef elf::getOutputSectionName(InputSectionBase *); -template StringRef elf::getOutputSectionName(InputSectionBase *); - template void elf::reportDiscarded(InputSectionBase *); template void elf::reportDiscarded(InputSectionBase *); template void elf::reportDiscarded(InputSectionBase *); Index: lld/trunk/test/ELF/relocatable-sections.s =================================================================== --- lld/trunk/test/ELF/relocatable-sections.s +++ lld/trunk/test/ELF/relocatable-sections.s @@ -0,0 +1,30 @@ +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +# RUN: ld.lld -r %t1.o -o %t +# RUN: llvm-objdump -section-headers %t | FileCheck %s + +# CHECK: .text +# CHECK-NEXT: .text._init +# CHECK-NEXT: .text._fini +# CHECK-NEXT: .rela.text +# CHECK-NEXT: .rela.text._init +# CHECK-NEXT: .rela.text._fini + +.globl _start +_start: + call foo + nop + +.section .xxx,"a" + .quad 0 + +.section .text._init,"ax" + .quad .xxx +foo: + call bar + nop + + +.section .text._fini,"ax" + .quad .xxx +bar: + nop