[lld] r326972 - Simplify LazyobjFile and readElfSymbols.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 7 17:05:58 PST 2018
Author: ruiu
Date: Wed Mar 7 17:05:58 2018
New Revision: 326972
URL: http://llvm.org/viewvc/llvm-project?rev=326972&view=rev
Log:
Simplify LazyobjFile and readElfSymbols.
addElfSymbols and readJustSymbolsFile still has duplicate code, but
I didn't come up with a good idea to eliminate them. Since this patch
is an improvement, I'm sending this for review.
Differential Revision: https://reviews.llvm.org/D44187
Modified:
lld/trunk/ELF/InputFiles.cpp
lld/trunk/ELF/InputFiles.h
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=326972&r1=326971&r2=326972&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Wed Mar 7 17:05:58 2018
@@ -1119,61 +1119,51 @@ InputFile *LazyObjFile::fetch() {
}
template <class ELFT> void LazyObjFile::parse() {
- for (StringRef Sym : getSymbolNames())
- Symtab->addLazyObject<ELFT>(Sym, *this);
+ // A lazy object file wraps either a bitcode file or an ELF file.
+ if (isBitcode(this->MB)) {
+ std::unique_ptr<lto::InputFile> Obj =
+ CHECK(lto::InputFile::create(this->MB), this);
+ for (const lto::InputFile::Symbol &Sym : Obj->symbols())
+ if (!Sym.isUndefined())
+ Symtab->addLazyObject<ELFT>(Saver.save(Sym.getName()), *this);
+ return;
+ }
+
+ switch (getELFKind(this->MB)) {
+ case ELF32LEKind:
+ addElfSymbols<ELF32LE>();
+ return;
+ case ELF32BEKind:
+ addElfSymbols<ELF32BE>();
+ return;
+ case ELF64LEKind:
+ addElfSymbols<ELF64LE>();
+ return;
+ case ELF64BEKind:
+ addElfSymbols<ELF64BE>();
+ return;
+ default:
+ llvm_unreachable("getELFKind");
+ }
}
-template <class ELFT> std::vector<StringRef> LazyObjFile::getElfSymbols() {
- typedef typename ELFT::Shdr Elf_Shdr;
- typedef typename ELFT::Sym Elf_Sym;
- typedef typename ELFT::SymRange Elf_Sym_Range;
+template <class ELFT> void LazyObjFile::addElfSymbols() {
+ ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer()));
+ ArrayRef<typename ELFT::Shdr> Sections = CHECK(Obj.sections(), this);
- ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(this->MB.getBuffer()));
- ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
- for (const Elf_Shdr &Sec : Sections) {
+ for (const typename ELFT::Shdr &Sec : Sections) {
if (Sec.sh_type != SHT_SYMTAB)
continue;
- Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), this);
+ typename ELFT::SymRange Syms = CHECK(Obj.symbols(&Sec), this);
uint32_t FirstNonLocal = Sec.sh_info;
StringRef StringTable =
CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
- std::vector<StringRef> V;
- for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
+ for (const typename ELFT::Sym &Sym : Syms.slice(FirstNonLocal))
if (Sym.st_shndx != SHN_UNDEF)
- V.push_back(CHECK(Sym.getName(StringTable), this));
- return V;
- }
- return {};
-}
-
-std::vector<StringRef> LazyObjFile::getBitcodeSymbols() {
- std::unique_ptr<lto::InputFile> Obj =
- CHECK(lto::InputFile::create(this->MB), this);
- std::vector<StringRef> V;
- for (const lto::InputFile::Symbol &Sym : Obj->symbols())
- if (!Sym.isUndefined())
- V.push_back(Saver.save(Sym.getName()));
- return V;
-}
-
-// Returns a vector of globally-visible defined symbol names.
-std::vector<StringRef> LazyObjFile::getSymbolNames() {
- if (isBitcode(this->MB))
- return getBitcodeSymbols();
-
- switch (getELFKind(this->MB)) {
- case ELF32LEKind:
- return getElfSymbols<ELF32LE>();
- case ELF32BEKind:
- return getElfSymbols<ELF32BE>();
- case ELF64LEKind:
- return getElfSymbols<ELF64LE>();
- case ELF64BEKind:
- return getElfSymbols<ELF64BE>();
- default:
- llvm_unreachable("getELFKind");
+ Symtab->addLazyObject<ELFT>(CHECK(Sym.getName(StringTable), this),
+ *this);
}
}
@@ -1203,7 +1193,6 @@ template <class ELFT> void elf::readJust
StringRef StringTable =
CHECK(Obj.getStringTableForSymtab(Sec, Sections), ObjName);
- std::vector<std::pair<StringRef, uint64_t>> Ret;
for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
if (Sym.st_shndx != SHN_UNDEF)
Symtab->addRegular(CHECK(Sym.getName(StringTable), ObjName),
Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=326972&r1=326971&r2=326972&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Wed Mar 7 17:05:58 2018
@@ -242,9 +242,7 @@ public:
InputFile *fetch();
private:
- std::vector<StringRef> getSymbolNames();
- template <class ELFT> std::vector<StringRef> getElfSymbols();
- std::vector<StringRef> getBitcodeSymbols();
+ template <class ELFT> void addElfSymbols();
bool Seen = false;
uint64_t OffsetInArchive;
More information about the llvm-commits
mailing list