Don't include shared libraries multiple times.

llvm-svn: 249061
This commit is contained in:
Rafael Espindola 2015-10-01 19:52:48 +00:00
parent 6032d5b332
commit 6a3b5ded8a
5 changed files with 22 additions and 8 deletions

View File

@ -77,13 +77,14 @@ ELFData<ELFT>::getSymbolsHelper(bool Local) {
return make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal);
}
template <class ELFT>
typename ELFData<ELFT>::Elf_Sym_Range ELFData<ELFT>::getNonLocalSymbols() {
if (!Symtab)
return Elf_Sym_Range(nullptr, nullptr);
template <class ELFT> void ELFData<ELFT>::initStringTable() {
ErrorOr<StringRef> StringTableOrErr = ELFObj.getStringTableForSymtab(*Symtab);
error(StringTableOrErr.getError());
StringTable = *StringTableOrErr;
}
template <class ELFT>
typename ELFData<ELFT>::Elf_Sym_Range ELFData<ELFT>::getNonLocalSymbols() {
return getSymbolsHelper(false);
}
@ -141,6 +142,7 @@ template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSections() {
}
template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() {
this->initStringTable();
Elf_Sym_Range Syms = this->getNonLocalSymbols();
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
SymbolBodies.reserve(NumSymbols);
@ -233,7 +235,7 @@ template <class ELFT>
SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
: SharedFileBase(getStaticELFKind<ELFT>(), M), ELFData<ELFT>(M) {}
template <class ELFT> void SharedFile<ELFT>::parse() {
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
const Elf_Shdr *DynamicSec = nullptr;
@ -247,8 +249,7 @@ template <class ELFT> void SharedFile<ELFT>::parse() {
DynamicSec = &Sec;
}
// Also sets StringTable
Elf_Sym_Range Syms = this->getNonLocalSymbols();
this->initStringTable();
SoName = getName();
if (DynamicSec) {
@ -266,7 +267,10 @@ template <class ELFT> void SharedFile<ELFT>::parse() {
}
}
}
}
template <class ELFT> void SharedFile<ELFT>::parse() {
Elf_Sym_Range Syms = this->getNonLocalSymbols();
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
SymbolBodies.reserve(NumSymbols);
for (const Elf_Sym &Sym : Syms) {

View File

@ -116,6 +116,7 @@ protected:
llvm::object::ELFFile<ELFT> ELFObj;
const Elf_Shdr *Symtab = nullptr;
StringRef StringTable;
void initStringTable();
Elf_Sym_Range getNonLocalSymbols();
Elf_Sym_Range getSymbolsHelper(bool);
};
@ -194,6 +195,7 @@ public:
: ELFFileBase(SharedKind, EKind, M) {}
static bool classof(const InputFile *F) { return F->kind() == SharedKind; }
StringRef getSoName() const { return SoName; }
virtual void parseSoName() = 0;
};
template <class ELFT>
@ -217,6 +219,7 @@ public:
explicit SharedFile(MemoryBufferRef M);
void parseSoName() override;
void parse() override;
};

View File

@ -41,6 +41,11 @@ void SymbolTable::addFile(std::unique_ptr<InputFile> File) {
addLazy(&Sym);
return;
}
if (auto *S = dyn_cast<SharedFileBase>(File.get())) {
S->parseSoName();
if (!IncludedSoNames.insert(S->getSoName()).second)
return;
}
File->parse();
addELFFile(cast<ELFFileBase>(File.release()));
}

View File

@ -93,6 +93,7 @@ private:
std::vector<std::unique_ptr<ObjectFileBase>> ObjectFiles;
std::vector<std::unique_ptr<SharedFileBase>> SharedFiles;
llvm::DenseSet<StringRef> IncludedSoNames;
SymbolBody *EntrySym = nullptr;
};

View File

@ -1,9 +1,10 @@
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
// RUN: lld -flavor gnu2 %t.o -shared -soname=bar -o %t.so
// RUN: lld -flavor gnu2 %t.o %t.so -o %t
// RUN: lld -flavor gnu2 %t.o %t.so %t.so -o %t
// RUN: llvm-readobj --dynamic-table %t | FileCheck %s
// CHECK: 0x0000000000000001 NEEDED SharedLibrary (bar)
// CHECK-NOT: NEEDED
.global _start
_start: