2015-05-28 19:09:30 +00:00
|
|
|
//===- SymbolTable.cpp ----------------------------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// The LLVM Linker
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2016-12-08 20:20:22 +00:00
|
|
|
#include "SymbolTable.h"
|
2015-05-28 19:09:30 +00:00
|
|
|
#include "Config.h"
|
|
|
|
|
#include "Driver.h"
|
2015-06-01 02:58:15 +00:00
|
|
|
#include "Error.h"
|
2015-06-29 18:50:11 +00:00
|
|
|
#include "Symbols.h"
|
2016-12-08 20:20:22 +00:00
|
|
|
#include "lld/Support/Memory.h"
|
2015-12-04 02:42:47 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-07-14 21:30:37 +00:00
|
|
|
#include "llvm/LTO/legacy/LTOCodeGenerator.h"
|
2015-05-28 19:09:30 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-06-28 22:16:41 +00:00
|
|
|
#include <utility>
|
2015-05-28 19:09:30 +00:00
|
|
|
|
2015-05-31 03:57:30 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
2015-05-28 19:09:30 +00:00
|
|
|
namespace lld {
|
|
|
|
|
namespace coff {
|
|
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
SymbolTable *Symtab;
|
2015-09-21 19:12:36 +00:00
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
void SymbolTable::addFile(InputFile *File) {
|
2016-12-08 20:20:22 +00:00
|
|
|
Files.push_back(File);
|
2015-06-30 19:35:21 +00:00
|
|
|
if (auto *F = dyn_cast<ArchiveFile>(File)) {
|
2016-12-09 21:55:24 +00:00
|
|
|
ArchiveQueue.push_back(F);
|
2015-06-30 19:35:21 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2016-12-09 21:55:24 +00:00
|
|
|
ObjectQueue.push_back(File);
|
2015-06-30 19:35:21 +00:00
|
|
|
if (auto *F = dyn_cast<ObjectFile>(File)) {
|
|
|
|
|
ObjectFiles.push_back(F);
|
|
|
|
|
} else if (auto *F = dyn_cast<BitcodeFile>(File)) {
|
|
|
|
|
BitcodeFiles.push_back(F);
|
|
|
|
|
} else {
|
|
|
|
|
ImportFiles.push_back(cast<ImportFile>(File));
|
|
|
|
|
}
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-06 14:58:50 +00:00
|
|
|
void SymbolTable::step() {
|
COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.
Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.
Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.
The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.
llvm-svn: 241236
2015-07-02 03:15:15 +00:00
|
|
|
if (queueEmpty())
|
2015-08-06 14:58:50 +00:00
|
|
|
return;
|
|
|
|
|
readObjects();
|
2016-12-09 21:55:24 +00:00
|
|
|
readArchive();
|
COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.
Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.
Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.
The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.
llvm-svn: 241236
2015-07-02 03:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-06 14:58:50 +00:00
|
|
|
void SymbolTable::run() {
|
COFF: Infer entry point as early as possible, but not too early.
On Windows, we have four different main functions, {w,}{main,WinMain}.
The linker has to choose a corresponding entry point function among
{w,}{main,WinMain}CRTStartup. These entry point functions are defined
in the standard library. The linker resolves one of them by looking at
which main function is defined and adding a corresponding undefined
symbol to the symbol table.
Object files containing entry point functions conflicts each other.
For example, we cannot resolve both mainCRTStartup and WinMainCRTStartup
because other symbols defined in the files conflict.
Previously, we inferred CRT function name at the very end of name
resolution. I found that that is sometimes too late. If the linker
already linked one of these four archive member objects, it's too late
to change the decision.
The right thing to do here is to infer entry point name after adding
all symbols from command line files and before adding any other files
(which are specified by directive sections). This patch does that.
llvm-svn: 241236
2015-07-02 03:15:15 +00:00
|
|
|
while (!queueEmpty())
|
2015-08-06 14:58:50 +00:00
|
|
|
step();
|
2015-06-30 19:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
void SymbolTable::readArchive() {
|
2015-06-30 19:35:21 +00:00
|
|
|
if (ArchiveQueue.empty())
|
2015-08-06 14:58:50 +00:00
|
|
|
return;
|
2015-06-30 19:35:21 +00:00
|
|
|
|
|
|
|
|
// Add lazy symbols to the symbol table. Lazy symbols that conflict
|
|
|
|
|
// with existing undefined symbols are accumulated in LazySyms.
|
2016-12-09 21:55:24 +00:00
|
|
|
ArchiveFile *File = ArchiveQueue.front();
|
|
|
|
|
ArchiveQueue.pop_front();
|
|
|
|
|
if (Config->Verbose)
|
|
|
|
|
outs() << "Reading " << toString(File) << "\n";
|
|
|
|
|
File->parse();
|
2015-06-30 19:35:21 +00:00
|
|
|
}
|
2015-06-23 23:56:39 +00:00
|
|
|
|
2015-08-06 14:58:50 +00:00
|
|
|
void SymbolTable::readObjects() {
|
2015-06-30 19:35:21 +00:00
|
|
|
if (ObjectQueue.empty())
|
2015-08-06 14:58:50 +00:00
|
|
|
return;
|
2015-06-30 19:35:21 +00:00
|
|
|
|
|
|
|
|
// Add defined and undefined symbols to the symbol table.
|
|
|
|
|
std::vector<StringRef> Directives;
|
2015-09-20 03:11:16 +00:00
|
|
|
for (size_t I = 0; I < ObjectQueue.size(); ++I) {
|
2016-12-09 21:55:24 +00:00
|
|
|
InputFile *File = ObjectQueue[I];
|
2015-09-20 03:11:16 +00:00
|
|
|
if (Config->Verbose)
|
2016-12-08 20:50:47 +00:00
|
|
|
outs() << "Reading " << toString(File) << "\n";
|
2016-12-09 21:55:24 +00:00
|
|
|
File->parse();
|
2015-09-20 03:11:16 +00:00
|
|
|
// Adding symbols may add more files to ObjectQueue
|
|
|
|
|
// (but not to ArchiveQueue).
|
|
|
|
|
StringRef S = File->getDirectives();
|
|
|
|
|
if (!S.empty()) {
|
|
|
|
|
Directives.push_back(S);
|
2015-07-04 01:39:11 +00:00
|
|
|
if (Config->Verbose)
|
2016-12-08 20:50:47 +00:00
|
|
|
outs() << "Directives: " << toString(File) << ": " << S << "\n";
|
2015-07-04 01:39:11 +00:00
|
|
|
}
|
2015-06-08 06:00:10 +00:00
|
|
|
}
|
2015-06-30 19:35:21 +00:00
|
|
|
ObjectQueue.clear();
|
|
|
|
|
|
|
|
|
|
// Parse directive sections. This may add files to
|
|
|
|
|
// ArchiveQueue and ObjectQueue.
|
|
|
|
|
for (StringRef S : Directives)
|
2015-08-06 14:58:50 +00:00
|
|
|
Driver->parseDirectives(S);
|
2015-06-06 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-02 02:38:59 +00:00
|
|
|
bool SymbolTable::queueEmpty() {
|
|
|
|
|
return ArchiveQueue.empty() && ObjectQueue.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
void SymbolTable::reportRemainingUndefines() {
|
2016-12-08 20:50:47 +00:00
|
|
|
SmallPtrSet<SymbolBody *, 8> Undefs;
|
2015-05-28 19:09:30 +00:00
|
|
|
for (auto &I : Symtab) {
|
|
|
|
|
Symbol *Sym = I.second;
|
2016-12-09 21:55:24 +00:00
|
|
|
auto *Undef = dyn_cast<Undefined>(Sym->body());
|
2015-05-28 19:09:30 +00:00
|
|
|
if (!Undef)
|
|
|
|
|
continue;
|
2016-12-09 21:55:24 +00:00
|
|
|
if (!Sym->IsUsedInRegularObj)
|
|
|
|
|
continue;
|
2015-06-25 02:21:44 +00:00
|
|
|
StringRef Name = Undef->getName();
|
2015-07-04 05:28:41 +00:00
|
|
|
// A weak alias may have been resolved, so check for that.
|
|
|
|
|
if (Defined *D = Undef->getWeakAlias()) {
|
2016-12-09 21:55:24 +00:00
|
|
|
// We resolve weak aliases by replacing the alias's SymbolBody with the
|
|
|
|
|
// target's SymbolBody. This causes all SymbolBody pointers referring to
|
|
|
|
|
// the old symbol to instead refer to the new symbol. However, we can't
|
|
|
|
|
// just blindly copy sizeof(Symbol::Body) bytes from D to Sym->Body
|
|
|
|
|
// because D may be an internal symbol, and internal symbols are stored as
|
|
|
|
|
// "unparented" SymbolBodies. For that reason we need to check which type
|
|
|
|
|
// of symbol we are dealing with and copy the correct number of bytes.
|
|
|
|
|
if (isa<DefinedRegular>(D))
|
|
|
|
|
memcpy(Sym->Body.buffer, D, sizeof(DefinedRegular));
|
|
|
|
|
else if (isa<DefinedAbsolute>(D))
|
|
|
|
|
memcpy(Sym->Body.buffer, D, sizeof(DefinedAbsolute));
|
|
|
|
|
else
|
|
|
|
|
// No other internal symbols are possible.
|
|
|
|
|
Sym->Body = D->symbol()->Body;
|
2015-07-04 05:28:41 +00:00
|
|
|
continue;
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
2015-06-25 02:21:44 +00: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_")) {
|
2015-07-02 03:59:04 +00:00
|
|
|
Symbol *Imp = find(Name.substr(strlen("__imp_")));
|
2016-12-09 21:55:24 +00:00
|
|
|
if (Imp && isa<Defined>(Imp->body())) {
|
|
|
|
|
auto *D = cast<Defined>(Imp->body());
|
|
|
|
|
replaceBody<DefinedLocalImport>(Sym, Name, D);
|
|
|
|
|
LocalImportChunks.push_back(
|
|
|
|
|
cast<DefinedLocalImport>(Sym->body())->getChunk());
|
2015-06-25 02:21:44 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-28 19:35:15 +00:00
|
|
|
// Remaining undefined symbols are not fatal if /force is specified.
|
|
|
|
|
// They are replaced with dummy defined symbols.
|
2016-12-09 21:55:24 +00:00
|
|
|
if (Config->Force)
|
|
|
|
|
replaceBody<DefinedAbsolute>(Sym, Name, 0);
|
|
|
|
|
Undefs.insert(Sym->body());
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
2015-07-07 18:38:39 +00:00
|
|
|
if (Undefs.empty())
|
2015-08-06 14:58:50 +00:00
|
|
|
return;
|
2016-12-09 21:55:24 +00:00
|
|
|
for (SymbolBody *B : Config->GCRoot)
|
|
|
|
|
if (Undefs.count(B))
|
|
|
|
|
errs() << "<root>: undefined symbol: " << B->getName() << "\n";
|
|
|
|
|
for (ObjectFile *File : ObjectFiles)
|
|
|
|
|
for (SymbolBody *Sym : File->getSymbols())
|
|
|
|
|
if (Undefs.count(Sym))
|
|
|
|
|
errs() << toString(File) << ": undefined symbol: " << Sym->getName()
|
|
|
|
|
<< "\n";
|
2015-08-06 14:58:50 +00:00
|
|
|
if (!Config->Force)
|
2016-07-15 01:12:24 +00:00
|
|
|
fatal("link failed");
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
|
|
|
|
|
Symbol *&Sym = Symtab[Name];
|
|
|
|
|
if (Sym)
|
|
|
|
|
return {Sym, false};
|
|
|
|
|
Sym = make<Symbol>();
|
|
|
|
|
Sym->IsUsedInRegularObj = false;
|
|
|
|
|
return {Sym, true};
|
2015-06-30 19:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
Symbol *SymbolTable::addUndefined(StringRef Name, InputFile *F,
|
|
|
|
|
bool IsWeakAlias) {
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
|
|
|
|
if (!F || !isa<BitcodeFile>(F))
|
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
|
if (WasInserted || (isa<Lazy>(S->body()) && IsWeakAlias)) {
|
|
|
|
|
replaceBody<Undefined>(S, Name);
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
if (auto *L = dyn_cast<Lazy>(S->body()))
|
|
|
|
|
addMemberFile(L->File, L->Sym);
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol Sym) {
|
|
|
|
|
StringRef Name = Sym.getName();
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
|
|
|
|
if (WasInserted) {
|
|
|
|
|
replaceBody<Lazy>(S, F, Sym);
|
2015-08-06 14:58:50 +00:00
|
|
|
return;
|
2015-06-01 02:58:15 +00:00
|
|
|
}
|
2016-12-09 21:55:24 +00:00
|
|
|
auto *U = dyn_cast<Undefined>(S->body());
|
|
|
|
|
if (!U || U->WeakAlias)
|
|
|
|
|
return;
|
|
|
|
|
addMemberFile(F, Sym);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) {
|
|
|
|
|
fatal("duplicate symbol: " + toString(*Existing->body()) + " in " +
|
|
|
|
|
toString(Existing->body()->getFile()) + " and in " +
|
|
|
|
|
(NewFile ? toString(NewFile) : "(internal)"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addAbsolute(StringRef N, COFFSymbolRef Sym) {
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(N);
|
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
|
if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
|
|
|
|
|
replaceBody<DefinedAbsolute>(S, N, Sym);
|
|
|
|
|
else if (!isa<DefinedCOFF>(S->body()))
|
|
|
|
|
reportDuplicate(S, nullptr);
|
|
|
|
|
return S;
|
|
|
|
|
}
|
2015-09-20 00:00:05 +00:00
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
Symbol *SymbolTable::addAbsolute(StringRef N, uint64_t VA) {
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(N);
|
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
|
if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
|
|
|
|
|
replaceBody<DefinedAbsolute>(S, N, VA);
|
|
|
|
|
else if (!isa<DefinedCOFF>(S->body()))
|
|
|
|
|
reportDuplicate(S, nullptr);
|
|
|
|
|
return S;
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
Symbol *SymbolTable::addRelative(StringRef N, uint64_t VA) {
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(N);
|
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
|
if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
|
|
|
|
|
replaceBody<DefinedRelative>(S, N, VA);
|
|
|
|
|
else if (!isa<DefinedCOFF>(S->body()))
|
|
|
|
|
reportDuplicate(S, nullptr);
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addRegular(ObjectFile *F, COFFSymbolRef Sym,
|
|
|
|
|
SectionChunk *C) {
|
|
|
|
|
StringRef Name;
|
|
|
|
|
F->getCOFFObj()->getSymbolName(Sym, Name);
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
|
if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
|
|
|
|
|
replaceBody<DefinedRegular>(S, F, Sym, C);
|
|
|
|
|
else if (auto *R = dyn_cast<DefinedRegular>(S->body())) {
|
|
|
|
|
if (!C->isCOMDAT() || !R->isCOMDAT())
|
|
|
|
|
reportDuplicate(S, F);
|
|
|
|
|
} else if (auto *B = dyn_cast<DefinedBitcode>(S->body())) {
|
|
|
|
|
if (B->IsReplaceable)
|
|
|
|
|
replaceBody<DefinedRegular>(S, F, Sym, C);
|
|
|
|
|
else if (!C->isCOMDAT())
|
|
|
|
|
reportDuplicate(S, F);
|
|
|
|
|
} else
|
|
|
|
|
replaceBody<DefinedRegular>(S, F, Sym, C);
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addBitcode(BitcodeFile *F, StringRef N, bool IsReplaceable) {
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(N);
|
|
|
|
|
if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body())) {
|
|
|
|
|
replaceBody<DefinedBitcode>(S, F, N, IsReplaceable);
|
|
|
|
|
return S;
|
2015-07-02 22:52:33 +00:00
|
|
|
}
|
2016-12-09 21:55:24 +00:00
|
|
|
if (isa<DefinedCommon>(S->body()))
|
|
|
|
|
return S;
|
|
|
|
|
if (IsReplaceable)
|
|
|
|
|
if (isa<DefinedRegular>(S->body()) || isa<DefinedBitcode>(S->body()))
|
|
|
|
|
return S;
|
|
|
|
|
reportDuplicate(S, F);
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addCommon(ObjectFile *F, COFFSymbolRef Sym,
|
|
|
|
|
CommonChunk *C) {
|
|
|
|
|
StringRef Name;
|
|
|
|
|
F->getCOFFObj()->getSymbolName(Sym, Name);
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
|
if (WasInserted || !isa<DefinedCOFF>(S->body()))
|
|
|
|
|
replaceBody<DefinedCommon>(S, F, Sym, C);
|
|
|
|
|
else if (auto *DC = dyn_cast<DefinedCommon>(S->body()))
|
|
|
|
|
if (Sym.getValue() > DC->getSize())
|
|
|
|
|
replaceBody<DefinedCommon>(S, F, Sym, C);
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addImportData(StringRef N, ImportFile *F) {
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(N);
|
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
|
if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
|
|
|
|
|
replaceBody<DefinedImportData>(S, N, F);
|
|
|
|
|
else if (!isa<DefinedCOFF>(S->body()))
|
|
|
|
|
reportDuplicate(S, nullptr);
|
|
|
|
|
return S;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addImportThunk(StringRef Name, DefinedImportData *ID,
|
|
|
|
|
uint16_t Machine) {
|
|
|
|
|
Symbol *S;
|
|
|
|
|
bool WasInserted;
|
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
|
if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
|
|
|
|
|
replaceBody<DefinedImportThunk>(S, Name, ID, Machine);
|
|
|
|
|
else if (!isa<DefinedCOFF>(S->body()))
|
|
|
|
|
reportDuplicate(S, nullptr);
|
|
|
|
|
return S;
|
2015-07-02 22:52:33 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-28 19:09:30 +00:00
|
|
|
// Reads an archive member file pointed by a given symbol.
|
2016-12-09 21:55:24 +00:00
|
|
|
void SymbolTable::addMemberFile(ArchiveFile *F, const Archive::Symbol Sym) {
|
|
|
|
|
InputFile *File = F->getMember(&Sym);
|
2015-05-28 19:09:30 +00:00
|
|
|
|
|
|
|
|
// getMember returns an empty buffer if the member was already
|
|
|
|
|
// read from the library.
|
|
|
|
|
if (!File)
|
2015-08-06 14:58:50 +00:00
|
|
|
return;
|
2015-05-28 19:09:30 +00:00
|
|
|
if (Config->Verbose)
|
2016-12-09 21:55:24 +00:00
|
|
|
outs() << "Loaded " << toString(File) << " for " << Sym.getName() << "\n";
|
2016-12-08 20:20:22 +00:00
|
|
|
addFile(File);
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<Chunk *> SymbolTable::getChunks() {
|
|
|
|
|
std::vector<Chunk *> Res;
|
2015-06-23 23:56:39 +00:00
|
|
|
for (ObjectFile *File : ObjectFiles) {
|
2015-05-28 19:09:30 +00:00
|
|
|
std::vector<Chunk *> &V = File->getChunks();
|
|
|
|
|
Res.insert(Res.end(), V.begin(), V.end());
|
|
|
|
|
}
|
|
|
|
|
return Res;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-02 03:59:04 +00:00
|
|
|
Symbol *SymbolTable::find(StringRef Name) {
|
2015-06-29 01:03:53 +00:00
|
|
|
auto It = Symtab.find(Name);
|
|
|
|
|
if (It == Symtab.end())
|
|
|
|
|
return nullptr;
|
2015-06-30 23:46:52 +00:00
|
|
|
return It->second;
|
2015-06-29 01:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-28 22:56:02 +00:00
|
|
|
Symbol *SymbolTable::findUnderscore(StringRef Name) {
|
|
|
|
|
if (Config->Machine == I386)
|
|
|
|
|
return find(("_" + Name).str());
|
|
|
|
|
return find(Name);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-14 02:58:13 +00:00
|
|
|
StringRef SymbolTable::findByPrefix(StringRef Prefix) {
|
|
|
|
|
for (auto Pair : Symtab) {
|
|
|
|
|
StringRef Name = Pair.first;
|
|
|
|
|
if (Name.startswith(Prefix))
|
|
|
|
|
return Name;
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef SymbolTable::findMangle(StringRef Name) {
|
|
|
|
|
if (Symbol *Sym = find(Name))
|
2016-12-09 21:55:24 +00:00
|
|
|
if (!isa<Undefined>(Sym->body()))
|
2015-07-14 02:58:13 +00:00
|
|
|
return Name;
|
2015-07-25 21:54:50 +00:00
|
|
|
if (Config->Machine != I386)
|
2015-07-14 02:58:13 +00:00
|
|
|
return findByPrefix(("?" + Name + "@@Y").str());
|
|
|
|
|
if (!Name.startswith("_"))
|
|
|
|
|
return "";
|
|
|
|
|
// Search for x86 C function.
|
|
|
|
|
StringRef S = findByPrefix((Name + "@").str());
|
|
|
|
|
if (!S.empty())
|
|
|
|
|
return S;
|
|
|
|
|
// Search for x86 C++ non-member function.
|
|
|
|
|
return findByPrefix(("?" + Name.substr(1) + "@@Y").str());
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
void SymbolTable::mangleMaybe(SymbolBody *B) {
|
|
|
|
|
auto *U = dyn_cast<Undefined>(B);
|
|
|
|
|
if (!U || U->WeakAlias)
|
2015-07-02 00:04:14 +00:00
|
|
|
return;
|
2015-07-14 02:58:13 +00:00
|
|
|
StringRef Alias = findMangle(U->getName());
|
|
|
|
|
if (!Alias.empty())
|
|
|
|
|
U->WeakAlias = addUndefined(Alias);
|
2015-06-28 22:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
SymbolBody *SymbolTable::addUndefined(StringRef Name) {
|
|
|
|
|
return addUndefined(Name, nullptr, false)->body();
|
2015-07-03 00:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-26 18:58:24 +00:00
|
|
|
void SymbolTable::printMap(llvm::raw_ostream &OS) {
|
|
|
|
|
for (ObjectFile *File : ObjectFiles) {
|
2016-12-07 23:17:02 +00:00
|
|
|
OS << toString(File) << ":\n";
|
2015-06-26 18:58:24 +00:00
|
|
|
for (SymbolBody *Body : File->getSymbols())
|
|
|
|
|
if (auto *R = dyn_cast<DefinedRegular>(Body))
|
2015-09-16 23:55:52 +00:00
|
|
|
if (R->getChunk()->isLive())
|
2015-06-26 18:58:24 +00:00
|
|
|
OS << Twine::utohexstr(Config->ImageBase + R->getRVA())
|
|
|
|
|
<< " " << R->getName() << "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-28 22:16:09 +00:00
|
|
|
void SymbolTable::addCombinedLTOObjects() {
|
|
|
|
|
if (BitcodeFiles.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Create an object file and add it to the symbol table by replacing any
|
|
|
|
|
// DefinedBitcode symbols with the definitions in the object file.
|
2016-04-14 21:41:44 +00:00
|
|
|
LTOCodeGenerator CG(BitcodeFile::Context);
|
2015-08-28 22:16:09 +00:00
|
|
|
CG.setOptLevel(Config->LTOOptLevel);
|
|
|
|
|
std::vector<ObjectFile *> Objs = createLTOObjects(&CG);
|
|
|
|
|
|
2015-06-23 23:56:39 +00:00
|
|
|
size_t NumBitcodeFiles = BitcodeFiles.size();
|
2016-12-09 21:55:24 +00:00
|
|
|
for (ObjectFile *Obj : Objs)
|
|
|
|
|
Obj->parse();
|
2015-08-06 14:58:50 +00:00
|
|
|
run();
|
|
|
|
|
if (BitcodeFiles.size() != NumBitcodeFiles)
|
2016-07-14 23:37:14 +00:00
|
|
|
fatal("LTO: late loaded symbol created new bitcode reference");
|
2015-06-01 20:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-09 17:52:17 +00:00
|
|
|
// Combine and compile bitcode files and then return the result
|
2015-08-28 22:16:09 +00:00
|
|
|
// as a vector of regular COFF object files.
|
|
|
|
|
std::vector<ObjectFile *> SymbolTable::createLTOObjects(LTOCodeGenerator *CG) {
|
2016-12-09 21:55:24 +00:00
|
|
|
// All symbols referenced by non-bitcode objects, including GC roots, must be
|
|
|
|
|
// preserved. We must also replace bitcode symbols with undefined symbols so
|
|
|
|
|
// that they may be replaced with real definitions without conflicting.
|
2015-06-23 23:56:39 +00:00
|
|
|
for (BitcodeFile *File : BitcodeFiles)
|
2016-12-09 21:55:24 +00:00
|
|
|
for (SymbolBody *Body : File->getSymbols()) {
|
|
|
|
|
if (!isa<DefinedBitcode>(Body))
|
|
|
|
|
continue;
|
|
|
|
|
if (Body->symbol()->IsUsedInRegularObj)
|
2015-06-11 21:49:54 +00:00
|
|
|
CG->addMustPreserveSymbol(Body->getName());
|
2016-12-09 21:55:24 +00:00
|
|
|
replaceBody<Undefined>(Body->symbol(), Body->getName());
|
|
|
|
|
}
|
2015-06-09 17:52:17 +00:00
|
|
|
|
2015-08-24 22:22:58 +00:00
|
|
|
CG->setModule(BitcodeFiles[0]->takeModule());
|
2015-06-09 17:52:17 +00:00
|
|
|
for (unsigned I = 1, E = BitcodeFiles.size(); I != E; ++I)
|
2015-11-17 23:30:59 +00:00
|
|
|
CG->addModule(BitcodeFiles[I]->takeModule().get());
|
2015-06-09 17:52:17 +00:00
|
|
|
|
2015-09-17 22:54:08 +00:00
|
|
|
bool DisableVerify = true;
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
|
DisableVerify = false;
|
|
|
|
|
#endif
|
2015-11-17 20:48:12 +00:00
|
|
|
if (!CG->optimize(DisableVerify, false, false, false))
|
2016-07-14 23:37:14 +00:00
|
|
|
fatal(""); // optimize() should have emitted any error message.
|
2015-08-28 22:16:09 +00:00
|
|
|
|
|
|
|
|
Objs.resize(Config->LTOJobs);
|
|
|
|
|
// Use std::list to avoid invalidation of pointers in OSPtrs.
|
|
|
|
|
std::list<raw_svector_ostream> OSs;
|
|
|
|
|
std::vector<raw_pwrite_stream *> OSPtrs;
|
2016-04-09 23:00:31 +00:00
|
|
|
for (SmallString<0> &Obj : Objs) {
|
2015-08-28 22:16:09 +00:00
|
|
|
OSs.emplace_back(Obj);
|
|
|
|
|
OSPtrs.push_back(&OSs.back());
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 20:48:12 +00:00
|
|
|
if (!CG->compileOptimized(OSPtrs))
|
2016-07-14 23:37:14 +00:00
|
|
|
fatal(""); // compileOptimized() should have emitted any error message.
|
2015-08-28 22:16:09 +00:00
|
|
|
|
|
|
|
|
std::vector<ObjectFile *> ObjFiles;
|
2016-04-09 23:00:31 +00:00
|
|
|
for (SmallString<0> &Obj : Objs) {
|
2016-04-12 22:05:38 +00:00
|
|
|
auto *ObjFile = new ObjectFile(MemoryBufferRef(Obj, "<LTO object>"));
|
2015-08-28 22:16:09 +00:00
|
|
|
Files.emplace_back(ObjFile);
|
|
|
|
|
ObjectFiles.push_back(ObjFile);
|
|
|
|
|
ObjFiles.push_back(ObjFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ObjFiles;
|
2015-06-09 17:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
2015-05-28 19:09:30 +00:00
|
|
|
} // namespace coff
|
|
|
|
|
} // namespace lld
|