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-08-04 22:29:01 +08:00
|
|
|
auto *P = cast<ObjectFileBase>(FileP);
|
2015-07-25 05:03:07 +08:00
|
|
|
addObject(P);
|
|
|
|
}
|
|
|
|
|
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-08-04 22:29:01 +08:00
|
|
|
void SymbolTable::addObject(ObjectFileBase *File) {
|
2015-09-04 03:13:13 +08:00
|
|
|
if (const ObjectFileBase *Old = getFirstObject()) {
|
|
|
|
if (!Old->isCompatibleWith(*File))
|
|
|
|
error(Twine(Old->getName() + " is incompatible with " + File->getName()));
|
2015-08-31 09:16:19 +08:00
|
|
|
} else {
|
2015-09-03 04:43:43 +08:00
|
|
|
switch (File->getELFKind()) {
|
|
|
|
case ELF32LEKind:
|
2015-08-31 09:46:20 +08:00
|
|
|
init<ELF32LE>();
|
2015-08-31 09:16:19 +08:00
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF32BEKind:
|
2015-08-31 09:46:20 +08:00
|
|
|
init<ELF32BE>();
|
2015-08-31 09:16:19 +08:00
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF64LEKind:
|
2015-08-31 09:46:20 +08:00
|
|
|
init<ELF64LE>();
|
2015-08-31 09:16:19 +08:00
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF64BEKind:
|
2015-08-31 09:46:20 +08:00
|
|
|
init<ELF64BE>();
|
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
|
|
|
ObjectFiles.emplace_back(File);
|
2015-08-31 09:16:19 +08:00
|
|
|
for (SymbolBody *Body : File->getSymbols()) {
|
2015-09-03 04:43:43 +08:00
|
|
|
switch (File->getELFKind()) {
|
|
|
|
case ELF32LEKind:
|
2015-08-31 09:16:19 +08:00
|
|
|
resolve<ELF32LE>(Body);
|
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF32BEKind:
|
2015-08-31 09:16:19 +08:00
|
|
|
resolve<ELF32BE>(Body);
|
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF64LEKind:
|
2015-08-31 09:16:19 +08:00
|
|
|
resolve<ELF64LE>(Body);
|
|
|
|
break;
|
2015-09-03 04:43:43 +08:00
|
|
|
case ELF64BEKind:
|
2015-08-31 09:16:19 +08:00
|
|
|
resolve<ELF64BE>(Body);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|