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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-12-09 04:20:22 +08:00
|
|
|
#include "SymbolTable.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "Config.h"
|
|
|
|
#include "Driver.h"
|
2015-06-01 10:58:15 +08:00
|
|
|
#include "Error.h"
|
2017-02-03 07:58:14 +08:00
|
|
|
#include "LTO.h"
|
2016-12-18 22:06:06 +08:00
|
|
|
#include "Memory.h"
|
2015-06-30 02:50:11 +08:00
|
|
|
#include "Symbols.h"
|
2015-12-04 10:42:47 +08:00
|
|
|
#include "llvm/IR/LLVMContext.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 {
|
|
|
|
|
2017-02-03 07:58:14 +08:00
|
|
|
enum SymbolPreference {
|
|
|
|
SP_EXISTING = -1,
|
|
|
|
SP_CONFLICT = 0,
|
|
|
|
SP_NEW = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Checks if an existing symbol S should be kept or replaced by a new symbol.
|
|
|
|
/// Returns SP_EXISTING when S should be kept, SP_NEW when the new symbol
|
|
|
|
/// should be kept, and SP_CONFLICT if no valid resolution exists.
|
|
|
|
static SymbolPreference compareDefined(Symbol *S, bool WasInserted,
|
|
|
|
bool NewIsCOMDAT) {
|
|
|
|
// If the symbol wasn't previously known, the new symbol wins by default.
|
|
|
|
if (WasInserted || !isa<Defined>(S->body()))
|
|
|
|
return SP_NEW;
|
|
|
|
|
|
|
|
// If the existing symbol is a DefinedRegular, both it and the new symbol
|
|
|
|
// must be comdats. In that case, we have no reason to prefer one symbol
|
|
|
|
// over the other, and we keep the existing one. If one of the symbols
|
|
|
|
// is not a comdat, we report a conflict.
|
|
|
|
if (auto *R = dyn_cast<DefinedRegular>(S->body())) {
|
|
|
|
if (NewIsCOMDAT && R->isCOMDAT())
|
|
|
|
return SP_EXISTING;
|
|
|
|
else
|
|
|
|
return SP_CONFLICT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Existing symbol is not a DefinedRegular; new symbol wins.
|
|
|
|
return SP_NEW;
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
SymbolTable *Symtab;
|
2015-09-22 03:12:36 +08:00
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
void SymbolTable::addFile(InputFile *File) {
|
2017-02-22 07:22:56 +08:00
|
|
|
log("Reading " + toString(File));
|
2016-12-12 06:15:25 +08:00
|
|
|
File->parse();
|
|
|
|
|
|
|
|
MachineTypes MT = File->getMachineType();
|
|
|
|
if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
|
|
|
|
Config->Machine = MT;
|
|
|
|
} else if (MT != IMAGE_FILE_MACHINE_UNKNOWN && Config->Machine != MT) {
|
|
|
|
fatal(toString(File) + ": machine type " + machineToStr(MT) +
|
|
|
|
" conflicts with " + machineToStr(Config->Machine));
|
2015-07-01 03:35:21 +08:00
|
|
|
}
|
2016-12-12 06:15:25 +08:00
|
|
|
|
2015-07-01 03:35:21 +08:00
|
|
|
if (auto *F = dyn_cast<ObjectFile>(File)) {
|
|
|
|
ObjectFiles.push_back(F);
|
|
|
|
} else if (auto *F = dyn_cast<BitcodeFile>(File)) {
|
|
|
|
BitcodeFiles.push_back(F);
|
2016-12-12 06:15:25 +08:00
|
|
|
} else if (auto *F = dyn_cast<ImportFile>(File)) {
|
|
|
|
ImportFiles.push_back(F);
|
2015-07-01 03:35:21 +08:00
|
|
|
}
|
|
|
|
|
2016-12-12 06:15:25 +08:00
|
|
|
StringRef S = File->getDirectives();
|
|
|
|
if (S.empty())
|
2015-08-06 22:58:50 +08:00
|
|
|
return;
|
2015-07-01 03:35:21 +08:00
|
|
|
|
2017-02-22 07:22:56 +08:00
|
|
|
log("Directives: " + toString(File) + ": " + S);
|
2016-12-12 06:15:25 +08:00
|
|
|
Driver->parseDirectives(S);
|
2015-07-02 10:38:59 +08:00
|
|
|
}
|
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
void SymbolTable::reportRemainingUndefines() {
|
2016-12-09 04:50:47 +08:00
|
|
|
SmallPtrSet<SymbolBody *, 8> Undefs;
|
2015-05-29 03:09:30 +08:00
|
|
|
for (auto &I : Symtab) {
|
|
|
|
Symbol *Sym = I.second;
|
2016-12-10 05:55:24 +08:00
|
|
|
auto *Undef = dyn_cast<Undefined>(Sym->body());
|
2015-05-29 03:09:30 +08:00
|
|
|
if (!Undef)
|
|
|
|
continue;
|
2016-12-10 05:55:24 +08:00
|
|
|
if (!Sym->IsUsedInRegularObj)
|
|
|
|
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()) {
|
2016-12-10 05:55:24 +08: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 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_")));
|
2016-12-10 05:55:24 +08: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 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.
|
2016-12-10 05:55:24 +08:00
|
|
|
if (Config->Force)
|
|
|
|
replaceBody<DefinedAbsolute>(Sym, 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;
|
2016-12-10 05:55:24 +08:00
|
|
|
for (SymbolBody *B : Config->GCRoot)
|
|
|
|
if (Undefs.count(B))
|
2017-02-22 07:22:56 +08:00
|
|
|
warn("<root>: undefined symbol: " + B->getName());
|
2016-12-10 05:55:24 +08:00
|
|
|
for (ObjectFile *File : ObjectFiles)
|
|
|
|
for (SymbolBody *Sym : File->getSymbols())
|
|
|
|
if (Undefs.count(Sym))
|
2017-02-22 07:22:56 +08:00
|
|
|
warn(toString(File) + ": undefined symbol: " + Sym->getName());
|
2015-08-06 22:58:50 +08:00
|
|
|
if (!Config->Force)
|
2016-07-15 09:12:24 +08:00
|
|
|
fatal("link failed");
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
|
2016-12-12 06:15:30 +08:00
|
|
|
Symbol *&Sym = Symtab[CachedHashStringRef(Name)];
|
2016-12-10 05:55:24 +08:00
|
|
|
if (Sym)
|
|
|
|
return {Sym, false};
|
|
|
|
Sym = make<Symbol>();
|
|
|
|
Sym->IsUsedInRegularObj = false;
|
2016-12-15 12:02:23 +08:00
|
|
|
Sym->PendingArchiveLoad = false;
|
2016-12-10 05:55:24 +08:00
|
|
|
return {Sym, true};
|
2015-07-01 03:35:21 +08:00
|
|
|
}
|
|
|
|
|
2016-12-10 05:55:24 +08: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;
|
|
|
|
}
|
2016-12-15 12:02:23 +08:00
|
|
|
if (auto *L = dyn_cast<Lazy>(S->body())) {
|
|
|
|
if (!S->PendingArchiveLoad) {
|
|
|
|
S->PendingArchiveLoad = true;
|
|
|
|
L->File->addMember(&L->Sym);
|
|
|
|
}
|
|
|
|
}
|
2016-12-10 05:55:24 +08:00
|
|
|
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 22:58:50 +08:00
|
|
|
return;
|
2015-06-01 10:58:15 +08:00
|
|
|
}
|
2016-12-10 05:55:24 +08:00
|
|
|
auto *U = dyn_cast<Undefined>(S->body());
|
2016-12-15 12:02:23 +08:00
|
|
|
if (!U || U->WeakAlias || S->PendingArchiveLoad)
|
2016-12-10 05:55:24 +08:00
|
|
|
return;
|
2016-12-15 12:02:23 +08:00
|
|
|
S->PendingArchiveLoad = true;
|
|
|
|
F->addMember(&Sym);
|
2016-12-10 05:55:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) {
|
2017-04-05 08:43:54 +08:00
|
|
|
error("duplicate symbol: " + toString(*Existing->body()) + " in " +
|
2016-12-10 05:55:24 +08:00
|
|
|
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 08:00:05 +08:00
|
|
|
|
2016-12-10 05:55:24 +08: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-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
[COFF] Improve synthetic symbol handling
Summary:
The main change is that we can have SECREL and SECTION relocations
against ___safe_se_handler_table, which is important for handling the
debug info in the MSVCRT.
Previously we were using DefinedRelative for __safe_se_handler_table and
__ImageBase, and after we implement CFGuard, we plan to extend it to
handle __guard_fids_table, __guard_longjmp_table, and more. However,
DefinedRelative is really only suitable for implementing __ImageBase,
because it lacks a Chunk, which you need in order to figure out the
output section index and output section offset when resolving SECREl and
SECTION relocations.
This change renames DefinedRelative to DefinedSynthetic and gives it a
Chunk. One wart is that __ImageBase doesn't have a chunk. It points to
the PE header, effectively. We could split DefinedRelative and
DefinedSynthetic if we think that's cleaner and creates fewer special
cases.
I also added safeseh.s, which checks that we don't emit a safe seh table
entries pointing to garbage collected handlers and that we don't emit a
table at all when there are no handlers.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: inglorion, pcc, llvm-commits, aprantl
Differential Revision: https://reviews.llvm.org/D34577
llvm-svn: 306293
2017-06-26 23:39:52 +08:00
|
|
|
Symbol *SymbolTable::addSynthetic(StringRef N, Chunk *C) {
|
2016-12-10 05:55:24 +08:00
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(N);
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
if (WasInserted || isa<Undefined>(S->body()) || isa<Lazy>(S->body()))
|
[COFF] Improve synthetic symbol handling
Summary:
The main change is that we can have SECREL and SECTION relocations
against ___safe_se_handler_table, which is important for handling the
debug info in the MSVCRT.
Previously we were using DefinedRelative for __safe_se_handler_table and
__ImageBase, and after we implement CFGuard, we plan to extend it to
handle __guard_fids_table, __guard_longjmp_table, and more. However,
DefinedRelative is really only suitable for implementing __ImageBase,
because it lacks a Chunk, which you need in order to figure out the
output section index and output section offset when resolving SECREl and
SECTION relocations.
This change renames DefinedRelative to DefinedSynthetic and gives it a
Chunk. One wart is that __ImageBase doesn't have a chunk. It points to
the PE header, effectively. We could split DefinedRelative and
DefinedSynthetic if we think that's cleaner and creates fewer special
cases.
I also added safeseh.s, which checks that we don't emit a safe seh table
entries pointing to garbage collected handlers and that we don't emit a
table at all when there are no handlers.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: inglorion, pcc, llvm-commits, aprantl
Differential Revision: https://reviews.llvm.org/D34577
llvm-svn: 306293
2017-06-26 23:39:52 +08:00
|
|
|
replaceBody<DefinedSynthetic>(S, N, C);
|
2016-12-10 05:55:24 +08:00
|
|
|
else if (!isa<DefinedCOFF>(S->body()))
|
|
|
|
reportDuplicate(S, nullptr);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2017-02-03 07:58:14 +08:00
|
|
|
Symbol *SymbolTable::addRegular(InputFile *F, StringRef N, bool IsCOMDAT,
|
|
|
|
const coff_symbol_generic *Sym,
|
2016-12-10 05:55:24 +08:00
|
|
|
SectionChunk *C) {
|
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(N);
|
2017-02-03 07:58:14 +08:00
|
|
|
if (!isa<BitcodeFile>(F))
|
|
|
|
S->IsUsedInRegularObj = true;
|
|
|
|
SymbolPreference SP = compareDefined(S, WasInserted, IsCOMDAT);
|
|
|
|
if (SP == SP_CONFLICT) {
|
|
|
|
reportDuplicate(S, F);
|
|
|
|
} else if (SP == SP_NEW) {
|
|
|
|
replaceBody<DefinedRegular>(S, F, N, IsCOMDAT, /*IsExternal*/ true, Sym, C);
|
2017-06-17 04:47:19 +08:00
|
|
|
} else if (SP == SP_EXISTING && IsCOMDAT && C) {
|
|
|
|
C->markDiscarded();
|
|
|
|
// Discard associative chunks that we've parsed so far. No need to recurse
|
|
|
|
// because an associative section cannot have children.
|
|
|
|
for (SectionChunk *Child : C->children())
|
|
|
|
Child->markDiscarded();
|
2015-07-03 06:52:33 +08:00
|
|
|
}
|
2016-12-10 05:55:24 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2017-02-03 07:58:14 +08:00
|
|
|
Symbol *SymbolTable::addCommon(InputFile *F, StringRef N, uint64_t Size,
|
|
|
|
const coff_symbol_generic *Sym, CommonChunk *C) {
|
2016-12-10 05:55:24 +08:00
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
2017-02-03 07:58:14 +08:00
|
|
|
std::tie(S, WasInserted) = insert(N);
|
|
|
|
if (!isa<BitcodeFile>(F))
|
|
|
|
S->IsUsedInRegularObj = true;
|
2016-12-10 05:55:24 +08:00
|
|
|
if (WasInserted || !isa<DefinedCOFF>(S->body()))
|
2017-02-03 07:58:14 +08:00
|
|
|
replaceBody<DefinedCommon>(S, F, N, Size, Sym, C);
|
2016-12-10 05:55:24 +08:00
|
|
|
else if (auto *DC = dyn_cast<DefinedCommon>(S->body()))
|
2017-02-03 07:58:14 +08:00
|
|
|
if (Size > DC->getSize())
|
|
|
|
replaceBody<DefinedCommon>(S, F, N, Size, Sym, C);
|
2016-12-10 05:55:24 +08:00
|
|
|
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-03 06:52:33 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-12-12 06:15:30 +08:00
|
|
|
auto It = Symtab.find(CachedHashStringRef(Name));
|
2015-06-29 09:03:53 +08:00
|
|
|
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) {
|
2016-12-12 06:15:30 +08:00
|
|
|
StringRef Name = Pair.first.val();
|
2015-07-14 10:58:13 +08:00
|
|
|
if (Name.startswith(Prefix))
|
|
|
|
return Name;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef SymbolTable::findMangle(StringRef Name) {
|
|
|
|
if (Symbol *Sym = find(Name))
|
2016-12-10 05:55:24 +08:00
|
|
|
if (!isa<Undefined>(Sym->body()))
|
2015-07-14 10:58:13 +08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
void SymbolTable::mangleMaybe(SymbolBody *B) {
|
|
|
|
auto *U = dyn_cast<Undefined>(B);
|
|
|
|
if (!U || U->WeakAlias)
|
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
|
|
|
}
|
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
SymbolBody *SymbolTable::addUndefined(StringRef Name) {
|
|
|
|
return addUndefined(Name, nullptr, false)->body();
|
2015-07-03 08:02:19 +08:00
|
|
|
}
|
|
|
|
|
2017-02-07 04:47:55 +08:00
|
|
|
std::vector<StringRef> SymbolTable::compileBitcodeFiles() {
|
2017-02-03 07:58:14 +08:00
|
|
|
LTO.reset(new BitcodeCompiler);
|
|
|
|
for (BitcodeFile *F : BitcodeFiles)
|
|
|
|
LTO->add(*F);
|
2017-02-07 04:47:55 +08:00
|
|
|
return LTO->compile();
|
|
|
|
}
|
2015-06-02 04:10:10 +08:00
|
|
|
|
2017-02-07 04:47:55 +08:00
|
|
|
void SymbolTable::addCombinedLTOObjects() {
|
|
|
|
if (BitcodeFiles.empty())
|
|
|
|
return;
|
|
|
|
for (StringRef Object : compileBitcodeFiles()) {
|
|
|
|
auto *Obj = make<ObjectFile>(MemoryBufferRef(Object, "lto.tmp"));
|
2017-02-03 07:58:14 +08:00
|
|
|
Obj->parse();
|
2017-02-07 04:47:55 +08:00
|
|
|
ObjectFiles.push_back(Obj);
|
2015-08-29 06:16:09 +08:00
|
|
|
}
|
2015-06-10 01:52:17 +08:00
|
|
|
}
|
2017-02-07 04:47:55 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|