Index: ELF/InputFiles.h =================================================================== --- ELF/InputFiles.h +++ ELF/InputFiles.h @@ -184,17 +184,18 @@ typedef typename ELFT::Word Elf_Word; typedef typename ELFT::CGProfile Elf_CGProfile; +public: + static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; } + StringRef getShtGroupSignature(ArrayRef Sections, const Elf_Shdr &Sec); -public: - static bool classof(const InputFile *F) { return F->kind() == Base::ObjKind; } - ArrayRef getLocalSymbols(); ArrayRef getGlobalSymbols(); ObjFile(MemoryBufferRef M, StringRef ArchiveName); - void parse(llvm::DenseSet &ComdatGroups); + void parse(llvm::DenseMap + &ComdatGroups); Symbol &getSymbol(uint32_t SymbolIndex) const { if (SymbolIndex >= this->Symbols.size()) @@ -235,8 +236,8 @@ ArrayRef CGProfile; private: - void - initializeSections(llvm::DenseSet &ComdatGroups); + void initializeSections(llvm::DenseMap &ComdatGroups); void initializeSymbols(); void initializeJustSymbols(); void initializeDwarf(); @@ -315,7 +316,8 @@ uint64_t OffsetInArchive); static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; } template - void parse(llvm::DenseSet &ComdatGroups); + void parse(llvm::DenseMap + &ComdatGroups); std::unique_ptr Obj; }; Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -292,7 +292,8 @@ } template -void ObjFile::parse(DenseSet &ComdatGroups) { +void ObjFile::parse( + DenseMap &ComdatGroups) { // Read a section table. JustSymbols is usually false. if (this->JustSymbols) initializeJustSymbols(); @@ -398,7 +399,7 @@ template void ObjFile::initializeSections( - DenseSet &ComdatGroups) { + DenseMap &ComdatGroups) { const ELFFile &Obj = this->getObj(); ArrayRef ObjSections = CHECK(Obj.sections(), this); @@ -457,7 +458,8 @@ if (Entries[0] != GRP_COMDAT) fatal(toString(this) + ": unsupported SHT_GROUP format"); - bool IsNew = ComdatGroups.insert(CachedHashStringRef(Signature)).second; + bool IsNew = + ComdatGroups.try_emplace(CachedHashStringRef(Signature), this).second; if (IsNew) { if (Config->Relocatable) this->Sections[I] = createInputSection(Sec); @@ -1179,10 +1181,12 @@ } template -void BitcodeFile::parse(DenseSet &ComdatGroups) { +void BitcodeFile::parse( + DenseMap &ComdatGroups) { std::vector KeptComdats; for (StringRef S : Obj->getComdatTable()) - KeptComdats.push_back(ComdatGroups.insert(CachedHashStringRef(S)).second); + KeptComdats.push_back( + ComdatGroups.try_emplace(CachedHashStringRef(S), this).second); for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) Symbols.push_back(createBitcodeSymbol(KeptComdats, ObjSym, *this)); @@ -1342,10 +1346,14 @@ template void ArchiveFile::parse(); template void ArchiveFile::parse(); -template void BitcodeFile::parse(DenseSet &); -template void BitcodeFile::parse(DenseSet &); -template void BitcodeFile::parse(DenseSet &); -template void BitcodeFile::parse(DenseSet &); +template void +BitcodeFile::parse(DenseMap &); +template void +BitcodeFile::parse(DenseMap &); +template void +BitcodeFile::parse(DenseMap &); +template void +BitcodeFile::parse(DenseMap &); template void LazyObjFile::parse(); template void LazyObjFile::parse(); Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -663,10 +663,43 @@ return Addend; } +// If Rel is in the same file where Sym was created, we know its st_shndx and +// can thus check if sh_shndx refers to a discarded COMDAT section. For that +// case, Sym is defined but created as Undefined to catch errors. +template +static std::string maybeReportDiscardedComdat(Symbol &Sym, + InputSectionBase &Sec, + const RelTy &Rel) { + ObjFile *File = Sec.getFile(); + if (File != Sym.File) + return ""; + uint32_t SecIdx = File->getSectionIndex( + File->getELFSyms()[Rel.getSymbol(Config->IsMips64EL)]); + if (File->getSections()[SecIdx] != &InputSection::Discarded) + return ""; + ArrayRef> ObjSections = + CHECK(File->getObj().sections(), File); + Elf_Shdr_Impl ELFSec = ObjSections[SecIdx - 1]; + if (ELFSec.sh_type != SHT_GROUP) + return ""; + + StringRef Signature = File->getShtGroupSignature(ObjSections, ELFSec); + auto It = Symtab->ComdatGroups.find(CachedHashStringRef(Signature)); + if (It == Symtab->ComdatGroups.end()) + return ""; + return ("relocation refers to a symbol in a discarded section: " + + toString(Sym) + "\n>>> section group signature: " + Signature + + "\n>>> defined in " + toString(File) + + "\n>>> prevailing definition is in " + toString(It->second) + + "\n>>> referenced by ") + .str(); +} + // Report an undefined symbol if necessary. // Returns true if this function printed out an error message. +template static bool maybeReportUndefined(Symbol &Sym, InputSectionBase &Sec, - uint64_t Offset) { + const RelTy &Rel) { if (Sym.isLocal() || !Sym.isUndefined() || Sym.isWeak()) return false; @@ -675,19 +708,29 @@ if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore && CanBeExternal) return false; - std::string Msg = "undefined "; - if (Sym.Visibility == STV_INTERNAL) - Msg += "internal "; - else if (Sym.Visibility == STV_HIDDEN) - Msg += "hidden "; - else if (Sym.Visibility == STV_PROTECTED) - Msg += "protected "; - Msg += "symbol: " + toString(Sym) + "\n>>> referenced by "; + auto Visibility = [&]() -> StringRef { + switch (Sym.Visibility) { + case STV_INTERNAL: + return "internal "; + case STV_HIDDEN: + return "hidden "; + case STV_PROTECTED: + return "protected "; + default: + return ""; + } + }; - std::string Src = Sec.getSrcMsg(Sym, Offset); + std::string Msg = maybeReportDiscardedComdat(Sym, Sec, Rel); + if (Msg.empty()) + Msg = ("undefined " + Visibility() + "symbol: " + toString(Sym) + + "\n>>> referenced by ") + .str(); + + std::string Src = Sec.getSrcMsg(Sym, Rel.r_offset); if (!Src.empty()) Msg += Src + "\n>>> "; - Msg += Sec.getObjMsg(Offset); + Msg += Sec.getObjMsg(Rel.r_offset); if (Sym.getName().startswith("_ZTV")) Msg += "\nthe vtable symbol may be undefined because the class is missing " @@ -1019,7 +1062,7 @@ return; // Skip if the target symbol is an erroneous undefined symbol. - if (maybeReportUndefined(Sym, Sec, Rel.r_offset)) + if (maybeReportUndefined(Sym, Sec, Rel)) return; const uint8_t *RelocatedAddr = Sec.data().begin() + Rel.r_offset; Index: ELF/SymbolTable.h =================================================================== --- ELF/SymbolTable.h +++ ELF/SymbolTable.h @@ -82,6 +82,11 @@ // Set of .so files to not link the same shared object file more than once. llvm::DenseMap SoNames; + // Comdat groups define "link once" sections. If two comdat groups have the + // same name, only one of them is linked, and the other is ignored. This map + // is used to uniquify them. + llvm::DenseMap ComdatGroups; + private: std::pair insertName(StringRef Name); @@ -104,11 +109,6 @@ llvm::DenseMap SymMap; std::vector SymVector; - // Comdat groups define "link once" sections. If two comdat groups have the - // same name, only one of them is linked, and the other is ignored. This set - // is used to uniquify them. - llvm::DenseSet ComdatGroups; - // A map from demangled symbol names to their symbol objects. // This mapping is 1:N because two symbols with different versions // can have the same name. We use this map to handle "extern C++ {}" Index: ELF/SymbolTable.cpp =================================================================== --- ELF/SymbolTable.cpp +++ ELF/SymbolTable.cpp @@ -141,7 +141,7 @@ LTO->add(*F); for (InputFile *File : LTO->compile()) { - DenseSet DummyGroups; + DenseMap DummyGroups; auto *Obj = cast>(File); Obj->parse(DummyGroups); for (Symbol *Sym : Obj->getGlobalSymbols()) Index: test/ELF/Inputs/comdat-discarded-error.s =================================================================== --- /dev/null +++ test/ELF/Inputs/comdat-discarded-error.s @@ -0,0 +1,3 @@ +.section .text.foo,"axG",@progbits,foo,comdat +.globl foo +foo: Index: test/ELF/comdat-discarded-error.s =================================================================== --- /dev/null +++ test/ELF/comdat-discarded-error.s @@ -0,0 +1,18 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/comdat-discarded-error.s -o %t1.o +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t2.o +# RUN: not ld.lld %t1.o %t2.o -o /dev/null 2>&1 | FileCheck %s + +# CHECK: error: relocation refers to a symbol in a discarded section: bar +# CHECK-NEXT: >>> section group signature: foo +# CHECK-NEXT: >>> defined in {{.*}}2.o +# CHECK-NEXT: >>> prevailing definition is in {{.*}}1.o +# CHECK-NEXT: >>> referenced by {{.*}}2.o:(.text+0x1) + +.globl _start +_start: + jmp bar + +.section .text.foo,"axG",@progbits,foo,comdat +.globl bar +bar: