Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -377,6 +377,12 @@ In::EhFrameHdr = make>(); Symtab::X->Sections.push_back(In::EhFrameHdr); } + + if (In::SymTab) + Symtab::X->Sections.push_back(In::SymTab); + Symtab::X->Sections.push_back(In::ShStrTab); + if (In::StrTab) + Symtab::X->Sections.push_back(In::StrTab); } template @@ -1089,10 +1095,6 @@ auto OS = dyn_cast_or_null>(findSection(".ARM.exidx")); if (OS && !OS->Sections.empty() && !Config->Relocatable) OS->addSection(make>()); - - addInputSec(In::SymTab); - addInputSec(In::ShStrTab); - addInputSec(In::StrTab); } // The linker is expected to define SECNAME_start and SECNAME_end @@ -1593,7 +1595,9 @@ EHdr->e_phnum = Phdrs.size(); EHdr->e_shentsize = sizeof(Elf_Shdr); EHdr->e_shnum = OutputSections.size() + 1; - EHdr->e_shstrndx = In::ShStrTab->OutSec->SectionIndex; + EHdr->e_shstrndx = In::ShStrTab->OutSec + ? In::ShStrTab->OutSec->SectionIndex + : SHN_UNDEF; if (Config->EMachine == EM_ARM) // We don't currently use any features incompatible with EF_ARM_EABI_VER5, Index: test/ELF/linkerscript/sections.s =================================================================== --- test/ELF/linkerscript/sections.s +++ test/ELF/linkerscript/sections.s @@ -66,7 +66,7 @@ # SEC-SWAP-NAMES: 7 .shstrtab 0000003b {{[0-9a-f]*}} # SEC-SWAP-NAMES: 8 .strtab 00000008 {{[0-9a-f]*}} -# .shstrtab from the input object file is discarded. +# .shstrtab is discarded. # RUN: echo "SECTIONS { \ # RUN: /DISCARD/ : { *(.shstrtab) } }" > %t.script # RUN: ld.lld -o %t5 --script %t.script %t @@ -74,14 +74,13 @@ # RUN: FileCheck -check-prefix=SEC-DISCARD %s # Idx Name Size -# SEC-DISCARD: 1 .text 0000000e {{[0-9a-f]*}} TEXT DATA -# SEC-DISCARD: 2 .data 00000020 {{[0-9a-f]*}} DATA -# SEC-DISCARD: 3 other 00000003 {{[0-9a-f]*}} DATA -# SEC-DISCARD: 4 .bss 00000002 {{[0-9a-f]*}} BSS -# SEC-DISCARD: 5 .comment 00000008 {{[0-9a-f]*}} -# SEC-DISCARD: 6 .symtab 00000030 {{[0-9a-f]*}} -# SEC-DISCARD: 7 .shstrtab 0000003b {{[0-9a-f]*}} -# SEC-DISCARD: 8 .strtab 00000008 {{[0-9a-f]*}} +# SEC-DISCARD: 1 0000000e {{[0-9a-f]*}} TEXT DATA +# SEC-DISCARD: 2 00000020 {{[0-9a-f]*}} DATA +# SEC-DISCARD: 3 00000003 {{[0-9a-f]*}} DATA +# SEC-DISCARD: 4 00000002 {{[0-9a-f]*}} BSS +# SEC-DISCARD: 5 00000008 {{[0-9a-f]*}} +# SEC-DISCARD: 6 00000030 {{[0-9a-f]*}} +# SEC-DISCARD: 7 00000008 {{[0-9a-f]*}} # Multiple SECTIONS command specifying additional input section descriptions # for the same output section description - input sections are merged into