2020-04-03 02:54:05 +08:00
|
|
|
//===- SymbolTable.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
|
|
|
#include "lld/Common/Memory.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::macho;
|
|
|
|
|
|
|
|
Symbol *SymbolTable::find(StringRef name) {
|
|
|
|
auto it = symMap.find(llvm::CachedHashStringRef(name));
|
|
|
|
if (it == symMap.end())
|
|
|
|
return nullptr;
|
|
|
|
return symVector[it->second];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
|
|
|
|
auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
|
|
|
|
|
|
|
|
// Name already present in the symbol table.
|
|
|
|
if (!p.second)
|
|
|
|
return {symVector[p.first->second], false};
|
|
|
|
|
|
|
|
// Name is a new symbol.
|
|
|
|
Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
|
|
|
|
symVector.push_back(sym);
|
|
|
|
return {sym, true};
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addDefined(StringRef name, InputSection *isec,
|
2020-07-25 06:55:25 +08:00
|
|
|
uint32_t value, bool isWeakDef) {
|
2020-04-03 02:54:05 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2020-08-28 06:59:30 +08:00
|
|
|
bool overridesWeakDef = false;
|
2020-04-03 02:54:05 +08:00
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
2020-07-25 06:55:25 +08:00
|
|
|
if (!wasInserted) {
|
|
|
|
if (auto *defined = dyn_cast<Defined>(s)) {
|
|
|
|
if (isWeakDef)
|
|
|
|
return s;
|
|
|
|
if (!defined->isWeakDef())
|
|
|
|
error("duplicate symbol: " + name);
|
2020-08-28 06:59:30 +08:00
|
|
|
} else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
|
|
|
|
overridesWeakDef = !isWeakDef && dysym->isWeakDef();
|
2020-07-25 06:55:25 +08:00
|
|
|
}
|
|
|
|
// Defined symbols take priority over other types of symbols, so in case
|
|
|
|
// of a name conflict, we fall through to the replaceSymbol() call below.
|
|
|
|
}
|
|
|
|
|
2020-08-28 06:59:30 +08:00
|
|
|
Defined *defined = replaceSymbol<Defined>(s, name, isec, value, isWeakDef,
|
|
|
|
/*isExternal=*/true);
|
|
|
|
defined->overridesWeakDef = overridesWeakDef;
|
2020-04-03 02:54:05 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addUndefined(StringRef name) {
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
|
|
|
if (wasInserted)
|
|
|
|
replaceSymbol<Undefined>(s, name);
|
2020-05-15 03:43:51 +08:00
|
|
|
else if (LazySymbol *lazy = dyn_cast<LazySymbol>(s))
|
|
|
|
lazy->fetchArchiveMember();
|
2020-04-03 02:54:05 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:44:14 +08:00
|
|
|
Symbol *SymbolTable::addCommon(StringRef name, InputFile *file, uint64_t size,
|
|
|
|
uint32_t align) {
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
|
|
|
if (!wasInserted) {
|
|
|
|
if (auto *common = dyn_cast<CommonSymbol>(s)) {
|
|
|
|
if (size < common->size)
|
|
|
|
return s;
|
2020-09-25 06:00:56 +08:00
|
|
|
} else if (isa<Defined>(s)) {
|
2020-09-25 05:44:14 +08:00
|
|
|
return s;
|
|
|
|
}
|
2020-09-25 06:00:56 +08:00
|
|
|
// Common symbols take priority over all non-Defined symbols, so in case of
|
|
|
|
// a name conflict, we fall through to the replaceSymbol() call below.
|
2020-09-25 05:44:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
replaceSymbol<CommonSymbol>(s, name, file, size, align);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:50:09 +08:00
|
|
|
Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef,
|
|
|
|
bool isTlv) {
|
2020-04-22 04:37:57 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
2020-08-28 06:59:30 +08:00
|
|
|
if (!wasInserted && isWeakDef)
|
|
|
|
if (auto *defined = dyn_cast<Defined>(s))
|
|
|
|
if (!defined->isWeakDef())
|
|
|
|
defined->overridesWeakDef = true;
|
|
|
|
|
2020-07-25 06:55:25 +08:00
|
|
|
if (wasInserted || isa<Undefined>(s) ||
|
|
|
|
(isa<DylibSymbol>(s) && !isWeakDef && s->isWeakDef()))
|
2020-08-13 10:50:09 +08:00
|
|
|
replaceSymbol<DylibSymbol>(s, file, name, isWeakDef, isTlv);
|
2020-07-25 06:55:25 +08:00
|
|
|
|
2020-04-22 04:37:57 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-05-15 03:43:51 +08:00
|
|
|
Symbol *SymbolTable::addLazy(StringRef name, ArchiveFile *file,
|
|
|
|
const llvm::object::Archive::Symbol &sym) {
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name);
|
|
|
|
|
|
|
|
if (wasInserted)
|
|
|
|
replaceSymbol<LazySymbol>(s, file, sym);
|
2020-07-25 06:55:25 +08:00
|
|
|
else if (isa<Undefined>(s) || (isa<DylibSymbol>(s) && s->isWeakDef()))
|
2020-05-15 03:43:51 +08:00
|
|
|
file->fetch(sym);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-07-31 05:28:41 +08:00
|
|
|
Symbol *SymbolTable::addDSOHandle(const MachHeaderSection *header) {
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(DSOHandle::name);
|
|
|
|
if (!wasInserted) {
|
2020-09-18 23:40:46 +08:00
|
|
|
// FIXME: Make every symbol (including absolute symbols) contain a
|
|
|
|
// reference to their originating file, then add that file name to this
|
|
|
|
// error message.
|
2020-07-31 05:28:41 +08:00
|
|
|
if (auto *defined = dyn_cast<Defined>(s))
|
2020-09-18 23:40:46 +08:00
|
|
|
error("found defined symbol with illegal name " + DSOHandle::name);
|
2020-07-31 05:28:41 +08:00
|
|
|
}
|
|
|
|
replaceSymbol<DSOHandle>(s, header);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
SymbolTable *macho::symtab;
|