2015-07-25 05:03:07 +08:00
|
|
|
//===- SymbolTable.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SymbolTable.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Error.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2015-08-31 09:16:19 +08:00
|
|
|
using namespace llvm::object;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2015-08-15 00:46:28 +08:00
|
|
|
SymbolTable::SymbolTable() {
|
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2015-08-04 22:29:01 +08:00
|
|
|
void SymbolTable::addFile(std::unique_ptr<InputFile> File) {
|
2015-07-25 05:03:07 +08:00
|
|
|
File->parse();
|
|
|
|
InputFile *FileP = File.release();
|
2015-09-04 04:03:54 +08:00
|
|
|
auto *P = cast<ELFFileBase>(FileP);
|
|
|
|
addELFFile(P);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 09:46:20 +08:00
|
|
|
template <class ELFT> void SymbolTable::init() {
|
|
|
|
resolve<ELFT>(new (Alloc)
|
|
|
|
Undefined<ELFT>("_start", Undefined<ELFT>::Synthetic));
|
|
|
|
}
|
|
|
|
|
2015-09-04 08:09:43 +08:00
|
|
|
template <class ELFT> void SymbolTable::addELFFile(ELFFileBase *File) {
|
2015-09-04 04:03:54 +08:00
|
|
|
if (const ELFFileBase *Old = getFirstELF()) {
|
2015-09-04 03:13:13 +08:00
|
|
|
if (!Old->isCompatibleWith(*File))
|
|
|
|
error(Twine(Old->getName() + " is incompatible with " + File->getName()));
|
2015-08-31 09:16:19 +08:00
|
|
|
} else {
|
2015-09-04 08:09:43 +08:00
|
|
|
init<ELFT>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *O = dyn_cast<ObjectFileBase>(File)) {
|
|
|
|
ObjectFiles.emplace_back(O);
|
|
|
|
for (SymbolBody *Body : O->getSymbols())
|
|
|
|
resolve<ELFT>(Body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *S = dyn_cast<SharedFileBase>(File))
|
|
|
|
SharedFiles.emplace_back(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolTable::addELFFile(ELFFileBase *File) {
|
|
|
|
switch (File->getELFKind()) {
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF32LEKind:
|
2015-09-04 08:09:43 +08:00
|
|
|
addELFFile<ELF32LE>(File);
|
2015-08-31 09:16:19 +08:00
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF32BEKind:
|
2015-09-04 08:09:43 +08:00
|
|
|
addELFFile<ELF32BE>(File);
|
2015-08-31 09:16:19 +08:00
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF64LEKind:
|
2015-09-04 08:09:43 +08:00
|
|
|
addELFFile<ELF64LE>(File);
|
2015-08-31 09:16:19 +08:00
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF64BEKind:
|
2015-09-04 08:09:43 +08:00
|
|
|
addELFFile<ELF64BE>(File);
|
2015-08-31 09:16:19 +08:00
|
|
|
break;
|
2015-08-05 20:03:34 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 22:29:01 +08:00
|
|
|
void SymbolTable::reportRemainingUndefines() {
|
2015-07-25 05:03:07 +08:00
|
|
|
for (auto &I : Symtab) {
|
2015-08-15 00:46:28 +08:00
|
|
|
SymbolBody *Body = I.second->Body;
|
|
|
|
if (Body->isStrongUndefined())
|
|
|
|
error(Twine("undefined symbol: ") + Body->getName());
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function resolves conflicts if there's an existing symbol with
|
|
|
|
// the same name. Decisions are made based on symbol type.
|
2015-08-31 09:16:19 +08:00
|
|
|
template <class ELFT> void SymbolTable::resolve(SymbolBody *New) {
|
2015-07-25 05:03:07 +08:00
|
|
|
// Find an existing Symbol or create and insert a new one.
|
|
|
|
StringRef Name = New->getName();
|
|
|
|
Symbol *&Sym = Symtab[Name];
|
|
|
|
if (!Sym) {
|
|
|
|
Sym = new (Alloc) Symbol(New);
|
|
|
|
New->setBackref(Sym);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
New->setBackref(Sym);
|
|
|
|
|
|
|
|
// compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
|
|
|
|
// equivalent (conflicting), or more preferable, respectively.
|
|
|
|
SymbolBody *Existing = Sym->Body;
|
2015-08-31 09:16:19 +08:00
|
|
|
int comp = Existing->compare<ELFT>(New);
|
2015-07-25 05:03:07 +08:00
|
|
|
if (comp < 0)
|
|
|
|
Sym->Body = New;
|
|
|
|
if (comp == 0)
|
|
|
|
error(Twine("duplicate symbol: ") + Name);
|
|
|
|
}
|