Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -461,12 +461,15 @@ const IRObjectFile &Obj, const BasicSymbolRef &Sym) { const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl()); - if (GV) - if (const Comdat *C = GV->getComdat()) - if (!KeptComdats.count(C)) - return nullptr; + SmallString<64> Name; + raw_svector_ostream OS(Name); + Sym.printName(OS); + StringRef NameRef = Saver.save(StringRef(Name)); + SymbolBody *Body; uint32_t Flags = Sym.getFlags(); + bool IsWeak = Flags & BasicSymbolRef::SF_Weak; + uint8_t Visibility; if (GV) Visibility = getGvVisibility(GV); @@ -475,14 +478,15 @@ // protected visibility. Visibility = STV_DEFAULT; - SmallString<64> Name; - raw_svector_ostream OS(Name); - Sym.printName(OS); - StringRef NameRef = Saver.save(StringRef(Name)); + if (GV) + if (const Comdat *C = GV->getComdat()) + if (!KeptComdats.count(C)) { + Body = new (Alloc) + UndefinedBitcode(NameRef, IsWeak, Visibility); + return Body; + } const Module &M = Obj.getModule(); - SymbolBody *Body; - bool IsWeak = Flags & BasicSymbolRef::SF_Weak; if (Flags & BasicSymbolRef::SF_Undefined) { Body = new (Alloc) UndefinedBitcode(NameRef, IsWeak, Visibility); } else if (Flags & BasicSymbolRef::SF_Common) { Index: test/ELF/lto/Inputs/comdat.s =================================================================== --- /dev/null +++ test/ELF/lto/Inputs/comdat.s @@ -0,0 +1,5 @@ + .section .text.f,"axG",@progbits,c,comdat + .globl foo + +foo: + retq Index: test/ELF/lto/comdat2.ll =================================================================== --- /dev/null +++ test/ELF/lto/comdat2.ll @@ -0,0 +1,25 @@ +; RUN: llvm-as %s -o %t.o +; RUN: llvm-mc -triple=x86_64-pc-linux %p/Inputs/comdat.s -o %t2.o -filetype=obj +; RUN: ld.lld -m elf_x86_64 %t.o %t2.o -o %t.so -shared +; RUN: llvm-readobj -t %t.so | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +$c = comdat any + +define protected void @foo() comdat($c) { + ret void +} + +; CHECK: Symbol { +; CHECK: Name: foo +; CHECK-NEXT: Value: 0x1000 +; CHECK-NEXT: Size: 1 +; CHECK-NEXT: Binding: Global +; CHECK-NEXT: Type: Function +; CHECK-NEXT: Other [ +; CHECK-NEXT: STV_PROTECTED +; CHECK-NEXT: ] +; CHECK-NEXT: Section: .text +; CHECK-NEXT: }