Skip to content

Commit c13d858

Browse files
committedMar 8, 2018
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 llvm-svn: 326972
1 parent 267589a commit c13d858

File tree

2 files changed

+35
-48
lines changed

2 files changed

+35
-48
lines changed
 

‎lld/ELF/InputFiles.cpp

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,61 +1119,51 @@ InputFile *LazyObjFile::fetch() {
11191119
}
11201120

11211121
template <class ELFT> void LazyObjFile::parse() {
1122-
for (StringRef Sym : getSymbolNames())
1123-
Symtab->addLazyObject<ELFT>(Sym, *this);
1122+
// A lazy object file wraps either a bitcode file or an ELF file.
1123+
if (isBitcode(this->MB)) {
1124+
std::unique_ptr<lto::InputFile> Obj =
1125+
CHECK(lto::InputFile::create(this->MB), this);
1126+
for (const lto::InputFile::Symbol &Sym : Obj->symbols())
1127+
if (!Sym.isUndefined())
1128+
Symtab->addLazyObject<ELFT>(Saver.save(Sym.getName()), *this);
1129+
return;
1130+
}
1131+
1132+
switch (getELFKind(this->MB)) {
1133+
case ELF32LEKind:
1134+
addElfSymbols<ELF32LE>();
1135+
return;
1136+
case ELF32BEKind:
1137+
addElfSymbols<ELF32BE>();
1138+
return;
1139+
case ELF64LEKind:
1140+
addElfSymbols<ELF64LE>();
1141+
return;
1142+
case ELF64BEKind:
1143+
addElfSymbols<ELF64BE>();
1144+
return;
1145+
default:
1146+
llvm_unreachable("getELFKind");
1147+
}
11241148
}
11251149

1126-
template <class ELFT> std::vector<StringRef> LazyObjFile::getElfSymbols() {
1127-
typedef typename ELFT::Shdr Elf_Shdr;
1128-
typedef typename ELFT::Sym Elf_Sym;
1129-
typedef typename ELFT::SymRange Elf_Sym_Range;
1150+
template <class ELFT> void LazyObjFile::addElfSymbols() {
1151+
ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer()));
1152+
ArrayRef<typename ELFT::Shdr> Sections = CHECK(Obj.sections(), this);
11301153

1131-
ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(this->MB.getBuffer()));
1132-
ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this);
1133-
for (const Elf_Shdr &Sec : Sections) {
1154+
for (const typename ELFT::Shdr &Sec : Sections) {
11341155
if (Sec.sh_type != SHT_SYMTAB)
11351156
continue;
11361157

1137-
Elf_Sym_Range Syms = CHECK(Obj.symbols(&Sec), this);
1158+
typename ELFT::SymRange Syms = CHECK(Obj.symbols(&Sec), this);
11381159
uint32_t FirstNonLocal = Sec.sh_info;
11391160
StringRef StringTable =
11401161
CHECK(Obj.getStringTableForSymtab(Sec, Sections), this);
1141-
std::vector<StringRef> V;
11421162

1143-
for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
1163+
for (const typename ELFT::Sym &Sym : Syms.slice(FirstNonLocal))
11441164
if (Sym.st_shndx != SHN_UNDEF)
1145-
V.push_back(CHECK(Sym.getName(StringTable), this));
1146-
return V;
1147-
}
1148-
return {};
1149-
}
1150-
1151-
std::vector<StringRef> LazyObjFile::getBitcodeSymbols() {
1152-
std::unique_ptr<lto::InputFile> Obj =
1153-
CHECK(lto::InputFile::create(this->MB), this);
1154-
std::vector<StringRef> V;
1155-
for (const lto::InputFile::Symbol &Sym : Obj->symbols())
1156-
if (!Sym.isUndefined())
1157-
V.push_back(Saver.save(Sym.getName()));
1158-
return V;
1159-
}
1160-
1161-
// Returns a vector of globally-visible defined symbol names.
1162-
std::vector<StringRef> LazyObjFile::getSymbolNames() {
1163-
if (isBitcode(this->MB))
1164-
return getBitcodeSymbols();
1165-
1166-
switch (getELFKind(this->MB)) {
1167-
case ELF32LEKind:
1168-
return getElfSymbols<ELF32LE>();
1169-
case ELF32BEKind:
1170-
return getElfSymbols<ELF32BE>();
1171-
case ELF64LEKind:
1172-
return getElfSymbols<ELF64LE>();
1173-
case ELF64BEKind:
1174-
return getElfSymbols<ELF64BE>();
1175-
default:
1176-
llvm_unreachable("getELFKind");
1165+
Symtab->addLazyObject<ELFT>(CHECK(Sym.getName(StringTable), this),
1166+
*this);
11771167
}
11781168
}
11791169

@@ -1203,7 +1193,6 @@ template <class ELFT> void elf::readJustSymbolsFile(MemoryBufferRef MB) {
12031193
StringRef StringTable =
12041194
CHECK(Obj.getStringTableForSymtab(Sec, Sections), ObjName);
12051195

1206-
std::vector<std::pair<StringRef, uint64_t>> Ret;
12071196
for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
12081197
if (Sym.st_shndx != SHN_UNDEF)
12091198
Symtab->addRegular(CHECK(Sym.getName(StringTable), ObjName),

‎lld/ELF/InputFiles.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,7 @@ class LazyObjFile : public InputFile {
242242
InputFile *fetch();
243243

244244
private:
245-
std::vector<StringRef> getSymbolNames();
246-
template <class ELFT> std::vector<StringRef> getElfSymbols();
247-
std::vector<StringRef> getBitcodeSymbols();
245+
template <class ELFT> void addElfSymbols();
248246

249247
bool Seen = false;
250248
uint64_t OffsetInArchive;

0 commit comments

Comments
 (0)
Please sign in to comment.