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"
|
2015-06-30 02:50:11 +08:00
|
|
|
#include "Symbols.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#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-06-29 06:16:41 +08:00
|
|
|
#include <utility>
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2015-05-31 11:57:30 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2015-07-01 03:35:21 +08:00
|
|
|
void SymbolTable::addFile(std::unique_ptr<InputFile> FileP) {
|
|
|
|
InputFile *File = FileP.get();
|
|
|
|
Files.push_back(std::move(FileP));
|
|
|
|
if (auto *F = dyn_cast<ArchiveFile>(File)) {
|
|
|
|
ArchiveQueue.push_back(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ObjectQueue.push_back(File);
|
|
|
|
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-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08: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 11:15:15 +08:00
|
|
|
if (queueEmpty())
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
|
|
|
readObjects();
|
|
|
|
readArchives();
|
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 11:15:15 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08: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 11:15:15 +08:00
|
|
|
while (!queueEmpty())
|
2015-08-06 22:58:50 +08:00
|
|
|
step();
|
2015-07-01 03:35:21 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void SymbolTable::readArchives() {
|
2015-07-01 03:35:21 +08:00
|
|
|
if (ArchiveQueue.empty())
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-07-01 03:35:21 +08:00
|
|
|
|
|
|
|
// Add lazy symbols to the symbol table. Lazy symbols that conflict
|
|
|
|
// with existing undefined symbols are accumulated in LazySyms.
|
|
|
|
std::vector<Symbol *> LazySyms;
|
|
|
|
for (ArchiveFile *File : ArchiveQueue) {
|
2015-06-24 07:56:39 +08:00
|
|
|
if (Config->Verbose)
|
2015-07-01 03:35:21 +08:00
|
|
|
llvm::outs() << "Reading " << File->getShortName() << "\n";
|
2015-08-06 22:58:50 +08:00
|
|
|
File->parse();
|
2015-07-01 03:35:21 +08:00
|
|
|
for (Lazy *Sym : File->getLazySymbols())
|
|
|
|
addLazy(Sym, &LazySyms);
|
|
|
|
}
|
|
|
|
ArchiveQueue.clear();
|
2015-06-24 07:56:39 +08:00
|
|
|
|
2015-07-01 03:35:21 +08:00
|
|
|
// Add archive member files to ObjectQueue that should resolve
|
|
|
|
// existing undefined symbols.
|
|
|
|
for (Symbol *Sym : LazySyms)
|
2015-08-06 22:58:50 +08:00
|
|
|
addMemberFile(cast<Lazy>(Sym->Body));
|
2015-07-01 03:35:21 +08:00
|
|
|
}
|
2015-06-24 07:56:39 +08:00
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void SymbolTable::readObjects() {
|
2015-07-01 03:35:21 +08:00
|
|
|
if (ObjectQueue.empty())
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-07-01 03:35:21 +08:00
|
|
|
|
|
|
|
// Add defined and undefined symbols to the symbol table.
|
|
|
|
std::vector<StringRef> Directives;
|
|
|
|
for (size_t I = 0; I < ObjectQueue.size(); ++I) {
|
|
|
|
InputFile *File = ObjectQueue[I];
|
|
|
|
if (Config->Verbose)
|
|
|
|
llvm::outs() << "Reading " << File->getShortName() << "\n";
|
2015-08-06 22:58:50 +08:00
|
|
|
File->parse();
|
2015-07-01 03:35:21 +08:00
|
|
|
// Adding symbols may add more files to ObjectQueue
|
|
|
|
// (but not to ArchiveQueue).
|
|
|
|
for (SymbolBody *Sym : File->getSymbols())
|
|
|
|
if (Sym->isExternal())
|
2015-08-06 22:58:50 +08:00
|
|
|
addSymbol(Sym);
|
2015-07-01 03:35:21 +08:00
|
|
|
StringRef S = File->getDirectives();
|
2015-07-04 09:39:11 +08:00
|
|
|
if (!S.empty()) {
|
2015-07-01 03:35:21 +08:00
|
|
|
Directives.push_back(S);
|
2015-07-04 09:39:11 +08:00
|
|
|
if (Config->Verbose)
|
|
|
|
llvm::outs() << "Directives: " << File->getShortName()
|
|
|
|
<< ": " << S << "\n";
|
|
|
|
}
|
2015-06-08 14:00:10 +08:00
|
|
|
}
|
2015-07-01 03:35:21 +08:00
|
|
|
ObjectQueue.clear();
|
|
|
|
|
|
|
|
// Parse directive sections. This may add files to
|
|
|
|
// ArchiveQueue and ObjectQueue.
|
|
|
|
for (StringRef S : Directives)
|
2015-08-06 22:58:50 +08:00
|
|
|
Driver->parseDirectives(S);
|
2015-06-06 10:00:45 +08:00
|
|
|
}
|
|
|
|
|
2015-07-02 10:38:59 +08:00
|
|
|
bool SymbolTable::queueEmpty() {
|
|
|
|
return ArchiveQueue.empty() && ObjectQueue.empty();
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void SymbolTable::reportRemainingUndefines(bool Resolve) {
|
2015-07-08 02:38:39 +08:00
|
|
|
llvm::SmallPtrSet<SymbolBody *, 8> Undefs;
|
2015-05-29 03:09:30 +08:00
|
|
|
for (auto &I : Symtab) {
|
|
|
|
Symbol *Sym = I.second;
|
2015-07-07 01:45:22 +08:00
|
|
|
auto *Undef = dyn_cast<Undefined>(Sym->Body);
|
2015-05-29 03:09:30 +08:00
|
|
|
if (!Undef)
|
|
|
|
continue;
|
2015-06-25 10:21:44 +08:00
|
|
|
StringRef Name = Undef->getName();
|
2015-07-04 13:28:41 +08:00
|
|
|
// A weak alias may have been resolved, so check for that.
|
|
|
|
if (Defined *D = Undef->getWeakAlias()) {
|
|
|
|
if (Resolve)
|
2015-07-02 08:04:14 +08:00
|
|
|
Sym->Body = D;
|
2015-07-04 13:28:41 +08:00
|
|
|
continue;
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
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_")) {
|
2015-07-02 11:59:04 +08:00
|
|
|
Symbol *Imp = find(Name.substr(strlen("__imp_")));
|
2015-07-07 01:45:22 +08:00
|
|
|
if (Imp && isa<Defined>(Imp->Body)) {
|
2015-07-04 13:28:41 +08:00
|
|
|
if (!Resolve)
|
|
|
|
continue;
|
2015-07-07 01:45:22 +08:00
|
|
|
auto *D = cast<Defined>(Imp->Body);
|
2015-07-02 11:59:04 +08:00
|
|
|
auto *S = new (Alloc) DefinedLocalImport(Name, D);
|
2015-06-25 11:31:47 +08:00
|
|
|
LocalImportChunks.push_back(S->getChunk());
|
|
|
|
Sym->Body = S;
|
2015-06-25 10:21:44 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2015-06-29 03:35:15 +08:00
|
|
|
// Remaining undefined symbols are not fatal if /force is specified.
|
|
|
|
// They are replaced with dummy defined symbols.
|
2015-07-08 02:38:39 +08:00
|
|
|
if (Config->Force && Resolve)
|
|
|
|
Sym->Body = new (Alloc) DefinedAbsolute(Name, 0);
|
|
|
|
Undefs.insert(Sym->Body);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2015-07-08 02:38:39 +08:00
|
|
|
if (Undefs.empty())
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-07-08 02:38:39 +08:00
|
|
|
for (Undefined *U : Config->GCRoot)
|
|
|
|
if (Undefs.count(U->repl()))
|
|
|
|
llvm::errs() << "<root>: undefined symbol: " << U->getName() << "\n";
|
|
|
|
for (std::unique_ptr<InputFile> &File : Files)
|
|
|
|
if (!isa<ArchiveFile>(File.get()))
|
|
|
|
for (SymbolBody *Sym : File->getSymbols())
|
|
|
|
if (Undefs.count(Sym->repl()))
|
|
|
|
llvm::errs() << File->getShortName() << ": undefined symbol: "
|
|
|
|
<< Sym->getName() << "\n";
|
2015-08-06 22:58:50 +08:00
|
|
|
if (!Config->Force)
|
|
|
|
error("Link failed");
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-07-01 03:35:21 +08:00
|
|
|
void SymbolTable::addLazy(Lazy *New, std::vector<Symbol *> *Accum) {
|
2015-07-03 06:52:33 +08:00
|
|
|
Symbol *Sym = insert(New);
|
|
|
|
if (Sym->Body == New)
|
2015-07-01 03:35:21 +08:00
|
|
|
return;
|
2015-07-06 06:05:08 +08:00
|
|
|
for (;;) {
|
|
|
|
SymbolBody *Existing = Sym->Body;
|
|
|
|
if (isa<Defined>(Existing))
|
|
|
|
return;
|
|
|
|
if (Lazy *L = dyn_cast<Lazy>(Existing))
|
|
|
|
if (L->getFileIndex() < New->getFileIndex())
|
|
|
|
return;
|
|
|
|
if (!Sym->Body.compare_exchange_strong(Existing, New))
|
|
|
|
continue;
|
|
|
|
New->setBackref(Sym);
|
2015-07-07 10:15:25 +08:00
|
|
|
if (isa<Undefined>(Existing))
|
|
|
|
Accum->push_back(Sym);
|
2015-07-01 03:35:21 +08:00
|
|
|
return;
|
2015-07-06 06:05:08 +08:00
|
|
|
}
|
2015-07-01 03:35:21 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void SymbolTable::addSymbol(SymbolBody *New) {
|
2015-07-01 03:35:21 +08:00
|
|
|
// Find an existing symbol or create and insert a new one.
|
|
|
|
assert(isa<Defined>(New) || isa<Undefined>(New));
|
2015-07-03 06:52:33 +08:00
|
|
|
Symbol *Sym = insert(New);
|
|
|
|
if (Sym->Body == New)
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-07-01 03:35:21 +08:00
|
|
|
|
2015-07-06 06:05:08 +08:00
|
|
|
for (;;) {
|
|
|
|
SymbolBody *Existing = Sym->Body;
|
|
|
|
|
|
|
|
// If we have an undefined symbol and a lazy symbol,
|
|
|
|
// let the lazy symbol to read a member file.
|
|
|
|
if (auto *L = dyn_cast<Lazy>(Existing)) {
|
|
|
|
// Undefined symbols with weak aliases need not to be resolved,
|
|
|
|
// since they would be replaced with weak aliases if they remain
|
|
|
|
// undefined.
|
|
|
|
if (auto *U = dyn_cast<Undefined>(New))
|
2015-08-06 22:58:50 +08:00
|
|
|
if (!U->WeakAlias) {
|
|
|
|
addMemberFile(L);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-06 06:05:08 +08:00
|
|
|
if (!Sym->Body.compare_exchange_strong(Existing, New))
|
|
|
|
continue;
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-07-06 06:05:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// compare() returns -1, 0, or 1 if the lhs symbol is less preferable,
|
|
|
|
// equivalent (conflicting), or more preferable, respectively.
|
|
|
|
int Comp = Existing->compare(New);
|
2015-08-06 22:58:50 +08:00
|
|
|
if (Comp == 0)
|
|
|
|
error(Twine("duplicate symbol: ") + Existing->getDebugName() + " and " +
|
|
|
|
New->getDebugName());
|
2015-07-06 06:05:08 +08:00
|
|
|
if (Comp < 0)
|
|
|
|
if (!Sym->Body.compare_exchange_strong(Existing, New))
|
|
|
|
continue;
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-06-01 10:58:15 +08:00
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-07-03 06:52:33 +08:00
|
|
|
Symbol *SymbolTable::insert(SymbolBody *New) {
|
|
|
|
Symbol *&Sym = Symtab[New->getName()];
|
|
|
|
if (Sym) {
|
|
|
|
New->setBackref(Sym);
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
Sym = new (Alloc) Symbol(New);
|
|
|
|
New->setBackref(Sym);
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
// Reads an archive member file pointed by a given symbol.
|
2015-08-06 22:58:50 +08:00
|
|
|
void SymbolTable::addMemberFile(Lazy *Body) {
|
|
|
|
std::unique_ptr<InputFile> File = Body->getMember();
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// getMember returns an empty buffer if the member was already
|
|
|
|
// read from the library.
|
|
|
|
if (!File)
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-05-29 03:09:30 +08:00
|
|
|
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));
|
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-07-02 11:59:04 +08:00
|
|
|
Symbol *SymbolTable::find(StringRef Name) {
|
2015-06-29 09:03:53 +08:00
|
|
|
auto It = Symtab.find(Name);
|
|
|
|
if (It == Symtab.end())
|
|
|
|
return nullptr;
|
2015-07-01 07:46:52 +08:00
|
|
|
return It->second;
|
2015-06-29 09:03:53 +08:00
|
|
|
}
|
|
|
|
|
2015-07-29 06:56:02 +08:00
|
|
|
Symbol *SymbolTable::findUnderscore(StringRef Name) {
|
|
|
|
if (Config->Machine == I386)
|
|
|
|
return find(("_" + Name).str());
|
|
|
|
return find(Name);
|
|
|
|
}
|
|
|
|
|
2015-07-14 10:58:13 +08: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))
|
|
|
|
if (!isa<Undefined>(Sym->Body))
|
|
|
|
return Name;
|
2015-07-26 05:54:50 +08:00
|
|
|
if (Config->Machine != I386)
|
2015-07-14 10:58:13 +08: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());
|
|
|
|
}
|
|
|
|
|
2015-07-02 08:04:14 +08:00
|
|
|
void SymbolTable::mangleMaybe(Undefined *U) {
|
|
|
|
if (U->WeakAlias)
|
|
|
|
return;
|
2015-07-02 08:21:11 +08:00
|
|
|
if (!isa<Undefined>(U->repl()))
|
2015-07-02 08:04:14 +08:00
|
|
|
return;
|
2015-07-14 10:58:13 +08:00
|
|
|
StringRef Alias = findMangle(U->getName());
|
|
|
|
if (!Alias.empty())
|
|
|
|
U->WeakAlias = addUndefined(Alias);
|
2015-06-29 06:16:41 +08:00
|
|
|
}
|
|
|
|
|
2015-07-02 08:04:14 +08:00
|
|
|
Undefined *SymbolTable::addUndefined(StringRef Name) {
|
2015-07-02 10:38:59 +08:00
|
|
|
auto *New = new (Alloc) Undefined(Name);
|
|
|
|
addSymbol(New);
|
|
|
|
if (auto *U = dyn_cast<Undefined>(New->repl()))
|
|
|
|
return U;
|
|
|
|
return New;
|
2015-06-01 06:31:31 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 07:51:14 +08:00
|
|
|
DefinedRelative *SymbolTable::addRelative(StringRef Name, uint64_t VA) {
|
|
|
|
auto *New = new (Alloc) DefinedRelative(Name, VA);
|
|
|
|
addSymbol(New);
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefinedAbsolute *SymbolTable::addAbsolute(StringRef Name, uint64_t VA) {
|
|
|
|
auto *New = new (Alloc) DefinedAbsolute(Name, VA);
|
|
|
|
addSymbol(New);
|
|
|
|
return New;
|
2015-07-03 08:02:19 +08:00
|
|
|
}
|
|
|
|
|
2015-06-27 02:58:24 +08:00
|
|
|
void SymbolTable::printMap(llvm::raw_ostream &OS) {
|
|
|
|
for (ObjectFile *File : ObjectFiles) {
|
|
|
|
OS << File->getShortName() << ":\n";
|
|
|
|
for (SymbolBody *Body : File->getSymbols())
|
|
|
|
if (auto *R = dyn_cast<DefinedRegular>(Body))
|
|
|
|
if (R->isLive())
|
|
|
|
OS << Twine::utohexstr(Config->ImageBase + R->getRVA())
|
|
|
|
<< " " << R->getName() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void SymbolTable::addCombinedLTOObject() {
|
2015-06-02 04:10:10 +08:00
|
|
|
if (BitcodeFiles.empty())
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-06-02 04:10:10 +08:00
|
|
|
|
2015-07-04 13:28:41 +08:00
|
|
|
// Diagnose any undefined symbols early, but do not resolve weak externals,
|
|
|
|
// as resolution breaks the invariant that each Symbol points to a unique
|
|
|
|
// SymbolBody, which we rely on to replace DefinedBitcode symbols correctly.
|
2015-08-06 22:58:50 +08:00
|
|
|
reportRemainingUndefines(/*Resolve=*/false);
|
2015-07-04 13:28:41 +08:00
|
|
|
|
2015-06-02 04:10:10 +08:00
|
|
|
// 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;
|
2015-08-14 12:47:07 +08:00
|
|
|
CG.setOptLevel(Config->LTOOptLevel);
|
2015-08-06 22:58:50 +08:00
|
|
|
ObjectFile *Obj = createLTOObject(&CG);
|
2015-06-10 01:52:17 +08:00
|
|
|
|
2015-06-02 04:10:10 +08:00
|
|
|
for (SymbolBody *Body : Obj->getSymbols()) {
|
|
|
|
if (!Body->isExternal())
|
|
|
|
continue;
|
2015-07-04 13:28:41 +08:00
|
|
|
// We should not see any new undefined symbols at this point, but we'll
|
|
|
|
// diagnose them later in reportRemainingUndefines().
|
2015-06-02 04:10:10 +08:00
|
|
|
StringRef Name = Body->getName();
|
2015-07-03 06:52:33 +08:00
|
|
|
Symbol *Sym = insert(Body);
|
2015-06-02 04:10:10 +08:00
|
|
|
|
2015-07-07 01:45:22 +08:00
|
|
|
if (isa<DefinedBitcode>(Sym->Body)) {
|
2015-06-02 04:10:10 +08:00
|
|
|
Sym->Body = Body;
|
2015-07-01 03:35:21 +08:00
|
|
|
continue;
|
2015-06-02 04:10:10 +08:00
|
|
|
}
|
2015-07-07 01:45:22 +08:00
|
|
|
if (auto *L = dyn_cast<Lazy>(Sym->Body)) {
|
2015-07-01 03:35:21 +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-08-06 22:58:50 +08:00
|
|
|
addMemberFile(L);
|
2015-07-01 03:35:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SymbolBody *Existing = Sym->Body;
|
|
|
|
int Comp = Existing->compare(Body);
|
2015-08-06 22:58:50 +08:00
|
|
|
if (Comp == 0)
|
|
|
|
error(Twine("LTO: unexpected duplicate symbol: ") + Name);
|
2015-07-01 03:35:21 +08:00
|
|
|
if (Comp < 0)
|
|
|
|
Sym->Body = Body;
|
2015-06-24 07:56:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t NumBitcodeFiles = BitcodeFiles.size();
|
2015-08-06 22:58:50 +08:00
|
|
|
run();
|
|
|
|
if (BitcodeFiles.size() != NumBitcodeFiles)
|
|
|
|
error("LTO: late loaded symbol created new bitcode reference");
|
2015-06-02 04:10:10 +08:00
|
|
|
}
|
|
|
|
|
2015-06-10 01:52:17 +08:00
|
|
|
// Combine and compile bitcode files and then return the result
|
|
|
|
// as a regular COFF object file.
|
2015-08-06 22:58:50 +08:00
|
|
|
ObjectFile *SymbolTable::createLTOObject(LTOCodeGenerator *CG) {
|
2015-06-10 01:52:17 +08:00
|
|
|
// 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())
|
2015-07-02 08:21:11 +08:00
|
|
|
if (auto *S = dyn_cast<DefinedBitcode>(Body->repl()))
|
2015-06-10 01:52:17 +08:00
|
|
|
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())
|
2015-07-02 08:21:11 +08:00
|
|
|
if (isa<DefinedBitcode>(Body) && !isa<DefinedBitcode>(Body->repl()))
|
2015-06-12 05:49:54 +08:00
|
|
|
CG->addMustPreserveSymbol(Body->getName());
|
|
|
|
|
2015-06-10 01:52:17 +08:00
|
|
|
// Likewise for other symbols that must be preserved.
|
2015-07-04 13:28:41 +08:00
|
|
|
for (Undefined *U : Config->GCRoot) {
|
|
|
|
if (auto *S = dyn_cast<DefinedBitcode>(U->repl()))
|
|
|
|
CG->addMustPreserveSymbol(S->getName());
|
|
|
|
else if (auto *S = dyn_cast_or_null<DefinedBitcode>(U->getWeakAlias()))
|
|
|
|
CG->addMustPreserveSymbol(S->getName());
|
|
|
|
}
|
2015-06-10 01:52:17 +08:00
|
|
|
|
|
|
|
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
|
2015-08-06 22:58:50 +08:00
|
|
|
if (!LTOMB)
|
|
|
|
error(ErrMsg);
|
2015-06-24 07:56:39 +08:00
|
|
|
auto *Obj = new ObjectFile(LTOMB->getMemBufferRef());
|
|
|
|
Files.emplace_back(Obj);
|
|
|
|
ObjectFiles.push_back(Obj);
|
2015-08-06 22:58:50 +08:00
|
|
|
Obj->parse();
|
2015-06-10 01:52:17 +08:00
|
|
|
return Obj;
|
|
|
|
}
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|