2015-05-29 03:09:30 +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 "Config.h"
|
|
|
|
#include "Driver.h"
|
2015-06-01 10:58:15 +08:00
|
|
|
#include "Error.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-06-02 04:10:10 +08:00
|
|
|
#include "llvm/LTO/LTOCodeGenerator.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2015-05-31 11:57:30 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
|
|
|
SymbolTable::SymbolTable() {
|
2015-06-03 13:39:12 +08:00
|
|
|
resolve(new (Alloc) DefinedAbsolute("__ImageBase", Config->ImageBase));
|
2015-05-31 11:34:08 +08:00
|
|
|
if (!Config->EntryName.empty())
|
2015-06-03 13:39:12 +08:00
|
|
|
resolve(new (Alloc) Undefined(Config->EntryName));
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-24 07:56:39 +08:00
|
|
|
void SymbolTable::addFile(std::unique_ptr<InputFile> File) {
|
|
|
|
Files.push_back(std::move(File));
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-06-24 07:56:39 +08:00
|
|
|
std::error_code SymbolTable::run() {
|
|
|
|
while (FileIdx < Files.size()) {
|
|
|
|
InputFile *F = Files[FileIdx++].get();
|
|
|
|
if (Config->Verbose)
|
|
|
|
llvm::outs() << "Reading " << F->getShortName() << "\n";
|
|
|
|
if (auto EC = F->parse())
|
|
|
|
return EC;
|
|
|
|
if (auto *P = dyn_cast<ObjectFile>(F)) {
|
|
|
|
ObjectFiles.push_back(P);
|
|
|
|
} else if (auto *P = dyn_cast<ArchiveFile>(F)) {
|
|
|
|
ArchiveFiles.push_back(P);
|
|
|
|
} else if (auto *P = dyn_cast<BitcodeFile>(F)) {
|
|
|
|
BitcodeFiles.push_back(P);
|
|
|
|
} else {
|
|
|
|
ImportFiles.push_back(cast<ImportFile>(F));
|
2015-06-08 14:00:10 +08:00
|
|
|
}
|
2015-06-24 07:56:39 +08:00
|
|
|
|
|
|
|
for (SymbolBody *B : F->getSymbols())
|
|
|
|
if (B->isExternal())
|
|
|
|
if (auto EC = resolve(B))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
// If a object file contains .drectve section,
|
|
|
|
// read that and add files listed there.
|
|
|
|
StringRef S = F->getDirectives();
|
|
|
|
if (!S.empty())
|
|
|
|
if (auto EC = Driver->parseDirectives(S))
|
|
|
|
return EC;
|
2015-06-08 14:00:10 +08:00
|
|
|
}
|
2015-06-06 10:00:45 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
bool SymbolTable::reportRemainingUndefines() {
|
|
|
|
bool Ret = false;
|
|
|
|
for (auto &I : Symtab) {
|
|
|
|
Symbol *Sym = I.second;
|
|
|
|
auto *Undef = dyn_cast<Undefined>(Sym->Body);
|
|
|
|
if (!Undef)
|
|
|
|
continue;
|
2015-06-25 10:21:44 +08:00
|
|
|
StringRef Name = Undef->getName();
|
2015-05-29 03:09:30 +08:00
|
|
|
if (SymbolBody *Alias = Undef->getWeakAlias()) {
|
|
|
|
Sym->Body = Alias->getReplacement();
|
|
|
|
if (!isa<Defined>(Sym->Body)) {
|
|
|
|
// Aliases are yet another symbols pointed by other symbols
|
|
|
|
// that could also remain undefined.
|
2015-06-25 10:21:44 +08:00
|
|
|
llvm::errs() << "undefined symbol: " << Name << "\n";
|
2015-05-29 03:09:30 +08:00
|
|
|
Ret = true;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-25 10:21:44 +08:00
|
|
|
// If we can resolve a symbol by removing __imp_ prefix, do that.
|
|
|
|
// This odd rule is for compatibility with MSVC linker.
|
|
|
|
if (Name.startswith("__imp_")) {
|
|
|
|
if (Defined *Imp = find(Name.substr(strlen("__imp_")))) {
|
2015-06-25 11:31:47 +08:00
|
|
|
auto *S = new (Alloc) DefinedLocalImport(Name, Imp);
|
|
|
|
LocalImportChunks.push_back(S->getChunk());
|
|
|
|
Sym->Body = S;
|
2015-06-25 10:21:44 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm::errs() << "undefined symbol: " << Name << "\n";
|
2015-05-29 03:09:30 +08:00
|
|
|
Ret = true;
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function resolves conflicts if there's an existing symbol with
|
|
|
|
// the same name. Decisions are made based on symbol type.
|
|
|
|
std::error_code SymbolTable::resolve(SymbolBody *New) {
|
|
|
|
// 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);
|
2015-06-20 05:12:48 +08:00
|
|
|
++Version;
|
2015-05-29 03:09:30 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
int comp = Existing->compare(New);
|
2015-06-20 05:12:48 +08:00
|
|
|
if (comp < 0) {
|
2015-05-29 03:09:30 +08:00
|
|
|
Sym->Body = New;
|
2015-06-20 05:12:48 +08:00
|
|
|
++Version;
|
|
|
|
}
|
2015-06-01 10:58:15 +08:00
|
|
|
if (comp == 0) {
|
|
|
|
llvm::errs() << "duplicate symbol: " << Name << "\n";
|
|
|
|
return make_error_code(LLDError::DuplicateSymbols);
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// If we have an Undefined symbol for a Lazy symbol, we need
|
|
|
|
// to read an archive member to replace the Lazy symbol with
|
|
|
|
// a Defined symbol.
|
|
|
|
if (isa<Undefined>(Existing) || isa<Undefined>(New))
|
|
|
|
if (auto *B = dyn_cast<Lazy>(Sym->Body))
|
|
|
|
return addMemberFile(B);
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads an archive member file pointed by a given symbol.
|
|
|
|
std::error_code SymbolTable::addMemberFile(Lazy *Body) {
|
|
|
|
auto FileOrErr = Body->getMember();
|
|
|
|
if (auto EC = FileOrErr.getError())
|
|
|
|
return EC;
|
|
|
|
std::unique_ptr<InputFile> File = std::move(FileOrErr.get());
|
|
|
|
|
|
|
|
// getMember returns an empty buffer if the member was already
|
|
|
|
// read from the library.
|
|
|
|
if (!File)
|
|
|
|
return std::error_code();
|
|
|
|
if (Config->Verbose)
|
2015-06-08 13:43:50 +08:00
|
|
|
llvm::outs() << "Loaded " << File->getShortName() << " for "
|
2015-05-29 03:09:30 +08:00
|
|
|
<< Body->getName() << "\n";
|
2015-06-24 07:56:39 +08:00
|
|
|
addFile(std::move(File));
|
|
|
|
return std::error_code();
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Chunk *> SymbolTable::getChunks() {
|
|
|
|
std::vector<Chunk *> Res;
|
2015-06-24 07:56:39 +08:00
|
|
|
for (ObjectFile *File : ObjectFiles) {
|
2015-05-29 03:09:30 +08:00
|
|
|
std::vector<Chunk *> &V = File->getChunks();
|
|
|
|
Res.insert(Res.end(), V.begin(), V.end());
|
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2015-05-31 11:34:08 +08:00
|
|
|
Defined *SymbolTable::find(StringRef Name) {
|
2015-05-29 03:09:30 +08:00
|
|
|
auto It = Symtab.find(Name);
|
|
|
|
if (It == Symtab.end())
|
|
|
|
return nullptr;
|
2015-05-31 11:34:08 +08:00
|
|
|
if (auto *Def = dyn_cast<Defined>(It->second->Body))
|
|
|
|
return Def;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-19 01:29:50 +08:00
|
|
|
std::error_code SymbolTable::resolveLazy(StringRef Name) {
|
2015-06-18 08:40:33 +08:00
|
|
|
auto It = Symtab.find(Name);
|
2015-06-24 07:56:39 +08:00
|
|
|
if (It == Symtab.end())
|
|
|
|
return std::error_code();
|
|
|
|
if (auto *B = dyn_cast<Lazy>(It->second->Body)) {
|
|
|
|
if (auto EC = addMemberFile(B))
|
|
|
|
return EC;
|
|
|
|
return run();
|
|
|
|
}
|
2015-06-18 08:40:33 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2015-06-01 00:10:50 +08:00
|
|
|
// Windows specific -- Link default entry point name.
|
2015-05-31 11:34:08 +08:00
|
|
|
ErrorOr<StringRef> SymbolTable::findDefaultEntry() {
|
2015-06-17 08:16:33 +08:00
|
|
|
// If it's DLL, the rule is easy.
|
|
|
|
if (Config->DLL) {
|
|
|
|
StringRef Sym = "_DllMainCRTStartup";
|
|
|
|
if (auto EC = resolve(new (Alloc) Undefined(Sym)))
|
|
|
|
return EC;
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-06-01 00:10:50 +08:00
|
|
|
// User-defined main functions and their corresponding entry points.
|
2015-05-31 11:34:08 +08:00
|
|
|
static const char *Entries[][2] = {
|
|
|
|
{"main", "mainCRTStartup"},
|
|
|
|
{"wmain", "wmainCRTStartup"},
|
|
|
|
{"WinMain", "WinMainCRTStartup"},
|
|
|
|
{"wWinMain", "wWinMainCRTStartup"},
|
|
|
|
};
|
2015-06-01 00:10:50 +08:00
|
|
|
for (auto E : Entries) {
|
2015-06-19 01:29:50 +08:00
|
|
|
resolveLazy(E[1]);
|
2015-06-01 00:10:50 +08:00
|
|
|
if (find(E[1]))
|
|
|
|
return StringRef(E[1]);
|
|
|
|
if (!find(E[0]))
|
2015-05-31 11:34:08 +08:00
|
|
|
continue;
|
2015-06-03 13:39:12 +08:00
|
|
|
if (auto EC = resolve(new (Alloc) Undefined(E[1])))
|
2015-05-31 11:34:08 +08:00
|
|
|
return EC;
|
2015-06-01 00:10:50 +08:00
|
|
|
return StringRef(E[1]);
|
2015-05-31 11:34:08 +08:00
|
|
|
}
|
2015-06-01 10:58:15 +08:00
|
|
|
llvm::errs() << "entry point must be defined\n";
|
|
|
|
return make_error_code(LLDError::InvalidOption);
|
2015-05-31 11:34:08 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 03:55:40 +08:00
|
|
|
std::error_code SymbolTable::addUndefined(StringRef Name) {
|
2015-06-03 13:39:12 +08:00
|
|
|
return resolve(new (Alloc) Undefined(Name));
|
2015-06-01 03:55:40 +08:00
|
|
|
}
|
|
|
|
|
2015-06-01 06:31:31 +08:00
|
|
|
// Resolve To, and make From an alias to To.
|
|
|
|
std::error_code SymbolTable::rename(StringRef From, StringRef To) {
|
2015-06-20 03:23:43 +08:00
|
|
|
// If From is not undefined, do nothing.
|
|
|
|
// Otherwise, rename it to see if To can be resolved instead.
|
|
|
|
auto It = Symtab.find(From);
|
|
|
|
if (It == Symtab.end())
|
|
|
|
return std::error_code();
|
|
|
|
Symbol *Sym = It->second;
|
|
|
|
if (!isa<Undefined>(Sym->Body))
|
|
|
|
return std::error_code();
|
2015-06-01 06:31:31 +08:00
|
|
|
SymbolBody *Body = new (Alloc) Undefined(To);
|
|
|
|
if (auto EC = resolve(Body))
|
|
|
|
return EC;
|
2015-06-20 03:23:43 +08:00
|
|
|
Sym->Body = Body->getReplacement();
|
|
|
|
Body->setBackref(Sym);
|
2015-06-20 05:12:48 +08:00
|
|
|
++Version;
|
2015-06-01 06:31:31 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
void SymbolTable::dump() {
|
|
|
|
for (auto &P : Symtab) {
|
|
|
|
Symbol *Ref = P.second;
|
|
|
|
if (auto *Body = dyn_cast<Defined>(Ref->Body))
|
|
|
|
llvm::dbgs() << Twine::utohexstr(Config->ImageBase + Body->getRVA())
|
|
|
|
<< " " << Body->getName() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-02 04:10:10 +08:00
|
|
|
std::error_code SymbolTable::addCombinedLTOObject() {
|
|
|
|
if (BitcodeFiles.empty())
|
|
|
|
return std::error_code();
|
|
|
|
|
|
|
|
// Create an object file and add it to the symbol table by replacing any
|
|
|
|
// DefinedBitcode symbols with the definitions in the object file.
|
2015-06-10 01:52:17 +08:00
|
|
|
LTOCodeGenerator CG;
|
|
|
|
auto FileOrErr = createLTOObject(&CG);
|
|
|
|
if (auto EC = FileOrErr.getError())
|
2015-06-02 04:10:10 +08:00
|
|
|
return EC;
|
2015-06-10 01:52:17 +08:00
|
|
|
ObjectFile *Obj = FileOrErr.get();
|
|
|
|
|
2015-06-24 07:56:39 +08:00
|
|
|
// Skip the combined object file as the file is processed below
|
|
|
|
// rather than by run().
|
|
|
|
++FileIdx;
|
|
|
|
|
2015-06-02 04:10:10 +08:00
|
|
|
for (SymbolBody *Body : Obj->getSymbols()) {
|
|
|
|
if (!Body->isExternal())
|
|
|
|
continue;
|
2015-06-09 10:53:09 +08:00
|
|
|
// Find an existing Symbol. We should not see any new undefined symbols at
|
|
|
|
// this point.
|
2015-06-02 04:10:10 +08:00
|
|
|
StringRef Name = Body->getName();
|
2015-06-09 10:53:09 +08:00
|
|
|
Symbol *&Sym = Symtab[Name];
|
2015-06-02 04:10:10 +08:00
|
|
|
if (!Sym) {
|
2015-06-09 10:53:09 +08:00
|
|
|
if (!isa<Defined>(Body)) {
|
|
|
|
llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
|
|
|
|
return make_error_code(LLDError::BrokenFile);
|
|
|
|
}
|
|
|
|
Sym = new (Alloc) Symbol(Body);
|
|
|
|
Body->setBackref(Sym);
|
|
|
|
continue;
|
2015-06-02 04:10:10 +08:00
|
|
|
}
|
|
|
|
Body->setBackref(Sym);
|
|
|
|
|
|
|
|
if (isa<DefinedBitcode>(Sym->Body)) {
|
|
|
|
// The symbol should now be defined.
|
|
|
|
if (!isa<Defined>(Body)) {
|
|
|
|
llvm::errs() << "LTO: undefined symbol: " << Name << '\n';
|
|
|
|
return make_error_code(LLDError::BrokenFile);
|
|
|
|
}
|
|
|
|
Sym->Body = Body;
|
2015-06-12 05:49:54 +08:00
|
|
|
} else {
|
|
|
|
int comp = Sym->Body->compare(Body);
|
|
|
|
if (comp < 0)
|
|
|
|
Sym->Body = Body;
|
|
|
|
if (comp == 0) {
|
|
|
|
llvm::errs() << "LTO: unexpected duplicate symbol: " << Name << "\n";
|
|
|
|
return make_error_code(LLDError::BrokenFile);
|
|
|
|
}
|
2015-06-02 04:10:10 +08:00
|
|
|
}
|
2015-06-09 12:29:54 +08:00
|
|
|
|
|
|
|
// We may see new references to runtime library symbols such as __chkstk
|
|
|
|
// here. These symbols must be wholly defined in non-bitcode files.
|
2015-06-24 07:56:39 +08:00
|
|
|
if (auto *B = dyn_cast<Lazy>(Sym->Body))
|
2015-06-24 08:12:34 +08:00
|
|
|
if (auto EC = addMemberFile(B))
|
|
|
|
return EC;
|
2015-06-24 07:56:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t NumBitcodeFiles = BitcodeFiles.size();
|
|
|
|
if (auto EC = run())
|
|
|
|
return EC;
|
|
|
|
if (BitcodeFiles.size() != NumBitcodeFiles) {
|
|
|
|
llvm::errs() << "LTO: late loaded symbol created new bitcode reference\n";
|
|
|
|
return make_error_code(LLDError::BrokenFile);
|
2015-06-02 04:10:10 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 12:29:54 +08:00
|
|
|
// New runtime library symbol references may have created undefined references.
|
|
|
|
if (reportRemainingUndefines())
|
|
|
|
return make_error_code(LLDError::BrokenFile);
|
2015-06-02 04:10:10 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2015-06-10 01:52:17 +08:00
|
|
|
// Combine and compile bitcode files and then return the result
|
|
|
|
// as a regular COFF object file.
|
|
|
|
ErrorOr<ObjectFile *> SymbolTable::createLTOObject(LTOCodeGenerator *CG) {
|
|
|
|
// All symbols referenced by non-bitcode objects must be preserved.
|
2015-06-24 07:56:39 +08:00
|
|
|
for (ObjectFile *File : ObjectFiles)
|
2015-06-10 01:52:17 +08:00
|
|
|
for (SymbolBody *Body : File->getSymbols())
|
|
|
|
if (auto *S = dyn_cast<DefinedBitcode>(Body->getReplacement()))
|
|
|
|
CG->addMustPreserveSymbol(S->getName());
|
|
|
|
|
2015-06-12 05:49:54 +08:00
|
|
|
// Likewise for bitcode symbols which we initially resolved to non-bitcode.
|
2015-06-24 07:56:39 +08:00
|
|
|
for (BitcodeFile *File : BitcodeFiles)
|
2015-06-12 05:49:54 +08:00
|
|
|
for (SymbolBody *Body : File->getSymbols())
|
|
|
|
if (isa<DefinedBitcode>(Body) &&
|
|
|
|
!isa<DefinedBitcode>(Body->getReplacement()))
|
|
|
|
CG->addMustPreserveSymbol(Body->getName());
|
|
|
|
|
2015-06-10 01:52:17 +08:00
|
|
|
// Likewise for other symbols that must be preserved.
|
|
|
|
for (StringRef Name : Config->GCRoots)
|
|
|
|
if (isa<DefinedBitcode>(Symtab[Name]->Body))
|
|
|
|
CG->addMustPreserveSymbol(Name);
|
|
|
|
|
|
|
|
CG->setModule(BitcodeFiles[0]->releaseModule());
|
|
|
|
for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
|
|
|
|
CG->addModule(BitcodeFiles[I]->getModule());
|
|
|
|
|
|
|
|
std::string ErrMsg;
|
|
|
|
LTOMB = CG->compile(false, false, false, ErrMsg); // take MB ownership
|
|
|
|
if (!LTOMB) {
|
|
|
|
llvm::errs() << ErrMsg << '\n';
|
|
|
|
return make_error_code(LLDError::BrokenFile);
|
|
|
|
}
|
2015-06-24 07:56:39 +08:00
|
|
|
auto *Obj = new ObjectFile(LTOMB->getMemBufferRef());
|
|
|
|
Files.emplace_back(Obj);
|
|
|
|
ObjectFiles.push_back(Obj);
|
2015-06-10 01:52:17 +08:00
|
|
|
if (auto EC = Obj->parse())
|
|
|
|
return EC;
|
|
|
|
return Obj;
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|