Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -442,7 +442,7 @@ if (!D) { if (!Sec->Name.startswith(".debug") && !Sec->Name.startswith(".zdebug") && Sec->Name != ".eh_frame" && - Sec->Name != ".gcc_except_table") { + Sec->Name != ".gcc_except_table" && Sec->Name != ".toc") { uint32_t SecIdx = cast(Sym).DiscardedSecIdx; Elf_Shdr_Impl Sec = CHECK(File->getObj().sections(), File)[SecIdx]; Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -718,6 +718,13 @@ if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && CanBeExternal) return false; + // clang/gcc PPC64 may emit a .rela.toc which references a switch table in a + // discarded .rodata/.text section. The .toc and the .rela.toc are incorrectly + // not placed in the comdat. Work around the bug. + if (Config->EMachine == EM_PPC64 && + cast(Sym).DiscardedSecIdx != 0 && Sec.Name == ".toc") + return false; + auto Visibility = [&]() -> std::string { switch (Sym.Visibility) { case STV_INTERNAL: Index: test/ELF/comdat-discarded-ppc64.s =================================================================== --- /dev/null +++ test/ELF/comdat-discarded-ppc64.s @@ -0,0 +1,16 @@ +# REQUIRES: ppc +# RUN: llvm-mc -filetype=obj -triple=powerpc64le %s -o %t.o +# RUN: ld.lld %t.o -o /dev/null + +## clang/gcc PPC64 may emit a .rela.toc which references a switch table in a +## discarded .rodata/.text section. The .toc and the .rela.toc are incorrectly +## not placed in the comdat. +## Don't error "relocation refers to a discarded section". + +.section .text.foo,"axG",@progbits,foo,comdat +.globl foo +foo: +.L0: + +.section .toc,"aw" +.quad .L0