diff --git a/lld/ELF/SymbolTable.cpp b/lld/ELF/SymbolTable.cpp index 3efdf12889fb..c99f15829968 100644 --- a/lld/ELF/SymbolTable.cpp +++ b/lld/ELF/SymbolTable.cpp @@ -27,25 +27,29 @@ void SymbolTable::addFile(std::unique_ptr File) { addObject(P); } +template void SymbolTable::init() { + resolve(new (Alloc) + Undefined("_start", Undefined::Synthetic)); +} + void SymbolTable::addObject(ObjectFileBase *File) { if (!ObjectFiles.empty()) { ObjectFileBase &Old = *ObjectFiles[0]; if (!Old.isCompatibleWith(*File)) error(Twine(Old.getName() + " is incompatible with " + File->getName())); } else { - auto *Start = new (Alloc) SyntheticUndefined("_start"); switch (File->kind()) { case InputFile::Object32LEKind: - resolve(Start); + init(); break; case InputFile::Object32BEKind: - resolve(Start); + init(); break; case InputFile::Object64LEKind: - resolve(Start); + init(); break; case InputFile::Object64BEKind: - resolve(Start); + init(); break; } } diff --git a/lld/ELF/SymbolTable.h b/lld/ELF/SymbolTable.h index 4e4780599a26..da417e9832a3 100644 --- a/lld/ELF/SymbolTable.h +++ b/lld/ELF/SymbolTable.h @@ -51,6 +51,7 @@ public: private: void addObject(ObjectFileBase *File); + template void init(); template void resolve(SymbolBody *Body); llvm::DenseMap Symtab; diff --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h index b944301898b6..1b09e0113cf1 100644 --- a/lld/ELF/Symbols.h +++ b/lld/ELF/Symbols.h @@ -42,16 +42,13 @@ public: DefinedAbsoluteKind = 1, DefinedCommonKind = 2, DefinedLast = 2, - UndefinedKind = 3, - UndefinedSyntheticKind = 4 + UndefinedKind = 3 }; Kind kind() const { return static_cast(SymbolKind); } bool isWeak() const { return IsWeak; } - bool isUndefined() const { - return SymbolKind == UndefinedKind || SymbolKind == UndefinedSyntheticKind; - } + bool isUndefined() const { return SymbolKind == UndefinedKind; } bool isDefined() const { return !isUndefined(); } bool isStrongUndefined() const { return !IsWeak && isUndefined(); } bool isCommon() const { return SymbolKind == DefinedCommonKind; } @@ -166,22 +163,14 @@ public: const SectionChunk &Section; }; -// Undefined symbols. -class SyntheticUndefined : public SymbolBody { -public: - explicit SyntheticUndefined(StringRef N) - : SymbolBody(UndefinedKind, N, false) {} - - static bool classof(const SymbolBody *S) { - return S->kind() == UndefinedSyntheticKind; - } -}; - +// Undefined symbol. template class Undefined : public ELFSymbolBody { typedef ELFSymbolBody Base; typedef typename Base::Elf_Sym Elf_Sym; public: + static Elf_Sym Synthetic; + explicit Undefined(StringRef N, const Elf_Sym &Sym) : ELFSymbolBody(Base::UndefinedKind, N, Sym) {} @@ -190,6 +179,9 @@ public: } }; +template +typename Undefined::Elf_Sym Undefined::Synthetic; + } // namespace elf2 } // namespace lld diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 5acb5e08dfa2..31f64083a876 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -302,8 +302,6 @@ template void SymbolTableSection::writeTo(uint8_t *Buf) { const Elf_Sym *InputSym = nullptr; switch (Body->kind()) { - case SymbolBody::UndefinedSyntheticKind: - llvm_unreachable("Should be defined by now"); case SymbolBody::DefinedRegularKind: { auto *Def = cast>(Body); InputSym = &Def->Sym;