aboutsummaryrefslogtreecommitdiff
path: root/lld/MachO/SymbolTable.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lld/MachO/SymbolTable.cpp')
-rw-r--r--lld/MachO/SymbolTable.cpp235
1 files changed, 178 insertions, 57 deletions
diff --git a/lld/MachO/SymbolTable.cpp b/lld/MachO/SymbolTable.cpp
index 2f0844fadaaa..c5808a89bf57 100644
--- a/lld/MachO/SymbolTable.cpp
+++ b/lld/MachO/SymbolTable.cpp
@@ -7,9 +7,11 @@
//===----------------------------------------------------------------------===//
#include "SymbolTable.h"
+#include "ConcatOutputSection.h"
#include "Config.h"
#include "InputFiles.h"
#include "Symbols.h"
+#include "SyntheticSections.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Memory.h"
@@ -17,72 +19,99 @@ using namespace llvm;
using namespace lld;
using namespace lld::macho;
-Symbol *SymbolTable::find(StringRef name) {
- auto it = symMap.find(CachedHashStringRef(name));
+Symbol *SymbolTable::find(CachedHashStringRef cachedName) {
+ auto it = symMap.find(cachedName);
if (it == symMap.end())
return nullptr;
return symVector[it->second];
}
-std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
+std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
+ const InputFile *file) {
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};
+ Symbol *sym;
+ if (!p.second) {
+ // Name already present in the symbol table.
+ sym = symVector[p.first->second];
+ } else {
+ // Name is a new symbol.
+ sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
+ symVector.push_back(sym);
+ }
- // Name is a new symbol.
- Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
- symVector.push_back(sym);
- return {sym, true};
+ sym->isUsedInRegularObj |= !file || isa<ObjFile>(file);
+ return {sym, p.second};
}
-Symbol *SymbolTable::addDefined(StringRef name, InputSection *isec,
- uint32_t value, bool isWeakDef,
- bool isPrivateExtern) {
+Defined *SymbolTable::addDefined(StringRef name, InputFile *file,
+ InputSection *isec, uint64_t value,
+ uint64_t size, bool isWeakDef,
+ bool isPrivateExtern, bool isThumb,
+ bool isReferencedDynamically,
+ bool noDeadStrip) {
Symbol *s;
bool wasInserted;
bool overridesWeakDef = false;
- std::tie(s, wasInserted) = insert(name);
+ std::tie(s, wasInserted) = insert(name, file);
+
+ assert(!isWeakDef || (isa<BitcodeFile>(file) && !isec) ||
+ (isa<ObjFile>(file) && file == isec->getFile()));
if (!wasInserted) {
if (auto *defined = dyn_cast<Defined>(s)) {
if (isWeakDef) {
- // Both old and new symbol weak (e.g. inline function in two TUs):
- // If one of them isn't private extern, the merged symbol isn't.
- if (defined->isWeakDef())
+ if (defined->isWeakDef()) {
+ // Both old and new symbol weak (e.g. inline function in two TUs):
+ // If one of them isn't private extern, the merged symbol isn't.
defined->privateExtern &= isPrivateExtern;
- return s;
+ defined->referencedDynamically |= isReferencedDynamically;
+ defined->noDeadStrip |= noDeadStrip;
+
+ // FIXME: Handle this for bitcode files.
+ // FIXME: We currently only do this if both symbols are weak.
+ // We could do this if either is weak (but getting the
+ // case where !isWeakDef && defined->isWeakDef() right
+ // requires some care and testing).
+ if (auto concatIsec = dyn_cast_or_null<ConcatInputSection>(isec))
+ concatIsec->wasCoalesced = true;
+ }
+
+ return defined;
}
if (!defined->isWeakDef())
- error("duplicate symbol: " + name);
+ error("duplicate symbol: " + name + "\n>>> defined in " +
+ toString(defined->getFile()) + "\n>>> defined in " +
+ toString(file));
} else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
overridesWeakDef = !isWeakDef && dysym->isWeakDef();
+ dysym->unreference();
}
// Defined symbols take priority over other types of symbols, so in case
// of a name conflict, we fall through to the replaceSymbol() call below.
}
- Defined *defined =
- replaceSymbol<Defined>(s, name, isec, value, isWeakDef,
- /*isExternal=*/true, isPrivateExtern);
+ Defined *defined = replaceSymbol<Defined>(
+ s, name, file, isec, value, size, isWeakDef, /*isExternal=*/true,
+ isPrivateExtern, isThumb, isReferencedDynamically, noDeadStrip);
defined->overridesWeakDef = overridesWeakDef;
- return s;
+ return defined;
}
-Symbol *SymbolTable::addUndefined(StringRef name, bool isWeakRef) {
+Symbol *SymbolTable::addUndefined(StringRef name, InputFile *file,
+ bool isWeakRef) {
Symbol *s;
bool wasInserted;
- std::tie(s, wasInserted) = insert(name);
+ std::tie(s, wasInserted) = insert(name, file);
- auto refState = isWeakRef ? RefState::Weak : RefState::Strong;
+ RefState refState = isWeakRef ? RefState::Weak : RefState::Strong;
if (wasInserted)
- replaceSymbol<Undefined>(s, name, refState);
+ replaceSymbol<Undefined>(s, name, file, refState);
else if (auto *lazy = dyn_cast<LazySymbol>(s))
lazy->fetchArchiveMember();
else if (auto *dynsym = dyn_cast<DylibSymbol>(s))
- dynsym->refState = std::max(dynsym->refState, refState);
+ dynsym->reference(refState);
else if (auto *undefined = dyn_cast<Undefined>(s))
undefined->refState = std::max(undefined->refState, refState);
return s;
@@ -92,7 +121,7 @@ Symbol *SymbolTable::addCommon(StringRef name, InputFile *file, uint64_t size,
uint32_t align, bool isPrivateExtern) {
Symbol *s;
bool wasInserted;
- std::tie(s, wasInserted) = insert(name);
+ std::tie(s, wasInserted) = insert(name, file);
if (!wasInserted) {
if (auto *common = dyn_cast<CommonSymbol>(s)) {
@@ -113,9 +142,9 @@ Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef,
bool isTlv) {
Symbol *s;
bool wasInserted;
- std::tie(s, wasInserted) = insert(name);
+ std::tie(s, wasInserted) = insert(name, file);
- auto refState = RefState::Unreferenced;
+ RefState refState = RefState::Unreferenced;
if (!wasInserted) {
if (auto *defined = dyn_cast<Defined>(s)) {
if (isWeakDef && !defined->isWeakDef())
@@ -123,22 +152,32 @@ Symbol *SymbolTable::addDylib(StringRef name, DylibFile *file, bool isWeakDef,
} else if (auto *undefined = dyn_cast<Undefined>(s)) {
refState = undefined->refState;
} else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
- refState = dysym->refState;
+ refState = dysym->getRefState();
}
}
+ bool isDynamicLookup = file == nullptr;
if (wasInserted || isa<Undefined>(s) ||
- (isa<DylibSymbol>(s) && !isWeakDef && s->isWeakDef()))
+ (isa<DylibSymbol>(s) &&
+ ((!isWeakDef && s->isWeakDef()) ||
+ (!isDynamicLookup && cast<DylibSymbol>(s)->isDynamicLookup())))) {
+ if (auto *dynsym = dyn_cast<DylibSymbol>(s))
+ dynsym->unreference();
replaceSymbol<DylibSymbol>(s, file, name, isWeakDef, refState, isTlv);
+ }
return s;
}
+Symbol *SymbolTable::addDynamicLookup(StringRef name) {
+ return addDylib(name, /*file=*/nullptr, /*isWeakDef=*/false, /*isTlv=*/false);
+}
+
Symbol *SymbolTable::addLazy(StringRef name, ArchiveFile *file,
const object::Archive::Symbol &sym) {
Symbol *s;
bool wasInserted;
- std::tie(s, wasInserted) = insert(name);
+ std::tie(s, wasInserted) = insert(name, file);
if (wasInserted)
replaceSymbol<LazySymbol>(s, file, sym);
@@ -147,37 +186,119 @@ Symbol *SymbolTable::addLazy(StringRef name, ArchiveFile *file,
return s;
}
-Symbol *SymbolTable::addDSOHandle(const MachHeaderSection *header) {
- Symbol *s;
- bool wasInserted;
- std::tie(s, wasInserted) = insert(DSOHandle::name);
- if (!wasInserted) {
- // FIXME: Make every symbol (including absolute symbols) contain a
- // reference to their originating file, then add that file name to this
- // error message.
- if (isa<Defined>(s))
- error("found defined symbol with illegal name " + DSOHandle::name);
- }
- replaceSymbol<DSOHandle>(s, header);
+Defined *SymbolTable::addSynthetic(StringRef name, InputSection *isec,
+ uint64_t value, bool isPrivateExtern,
+ bool includeInSymtab,
+ bool referencedDynamically) {
+ Defined *s = addDefined(name, nullptr, isec, value, /*size=*/0,
+ /*isWeakDef=*/false, isPrivateExtern,
+ /*isThumb=*/false, referencedDynamically,
+ /*noDeadStrip=*/false);
+ s->includeInSymtab = includeInSymtab;
return s;
}
-void lld::macho::treatUndefinedSymbol(StringRef symbolName,
- StringRef fileName) {
- std::string message = ("undefined symbol: " + symbolName).str();
- if (!fileName.empty())
- message += ("\n>>> referenced by " + fileName).str();
+enum class Boundary {
+ Start,
+ End,
+};
+
+static Defined *createBoundarySymbol(const Undefined &sym) {
+ return symtab->addSynthetic(
+ sym.getName(), /*isec=*/nullptr, /*value=*/-1, /*isPrivateExtern=*/true,
+ /*includeInSymtab=*/false, /*referencedDynamically=*/false);
+}
+
+static void handleSectionBoundarySymbol(const Undefined &sym, StringRef segSect,
+ Boundary which) {
+ StringRef segName, sectName;
+ std::tie(segName, sectName) = segSect.split('$');
+
+ // Attach the symbol to any InputSection that will end up in the right
+ // OutputSection -- it doesn't matter which one we pick.
+ // Don't bother looking through inputSections for a matching
+ // ConcatInputSection -- we need to create ConcatInputSection for
+ // non-existing sections anyways, and that codepath works even if we should
+ // already have a ConcatInputSection with the right name.
+
+ OutputSection *osec = nullptr;
+ // This looks for __TEXT,__cstring etc.
+ for (SyntheticSection *ssec : syntheticSections)
+ if (ssec->segname == segName && ssec->name == sectName) {
+ osec = ssec->isec->parent;
+ break;
+ }
+
+ if (!osec) {
+ ConcatInputSection *isec = make<ConcatInputSection>(segName, sectName);
+
+ // This runs after markLive() and is only called for Undefineds that are
+ // live. Marking the isec live ensures an OutputSection is created that the
+ // start/end symbol can refer to.
+ assert(sym.isLive());
+ isec->live = true;
+
+ // This runs after gatherInputSections(), so need to explicitly set parent
+ // and add to inputSections.
+ osec = isec->parent = ConcatOutputSection::getOrCreateForInput(isec);
+ inputSections.push_back(isec);
+ }
+
+ if (which == Boundary::Start)
+ osec->sectionStartSymbols.push_back(createBoundarySymbol(sym));
+ else
+ osec->sectionEndSymbols.push_back(createBoundarySymbol(sym));
+}
+
+static void handleSegmentBoundarySymbol(const Undefined &sym, StringRef segName,
+ Boundary which) {
+ OutputSegment *seg = getOrCreateOutputSegment(segName);
+ if (which == Boundary::Start)
+ seg->segmentStartSymbols.push_back(createBoundarySymbol(sym));
+ else
+ seg->segmentEndSymbols.push_back(createBoundarySymbol(sym));
+}
+
+void lld::macho::treatUndefinedSymbol(const Undefined &sym, StringRef source) {
+ // Handle start/end symbols.
+ StringRef name = sym.getName();
+ if (name.consume_front("section$start$"))
+ return handleSectionBoundarySymbol(sym, name, Boundary::Start);
+ if (name.consume_front("section$end$"))
+ return handleSectionBoundarySymbol(sym, name, Boundary::End);
+ if (name.consume_front("segment$start$"))
+ return handleSegmentBoundarySymbol(sym, name, Boundary::Start);
+ if (name.consume_front("segment$end$"))
+ return handleSegmentBoundarySymbol(sym, name, Boundary::End);
+
+ // Handle -U.
+ if (config->explicitDynamicLookups.count(sym.getName())) {
+ symtab->addDynamicLookup(sym.getName());
+ return;
+ }
+
+ // Handle -undefined.
+ auto message = [source, &sym]() {
+ std::string message = "undefined symbol";
+ if (config->archMultiple)
+ message += (" for arch " + getArchitectureName(config->arch())).str();
+ message += ": " + toString(sym);
+ if (!source.empty())
+ message += "\n>>> referenced by " + source.str();
+ else
+ message += "\n>>> referenced by " + toString(sym.getFile());
+ return message;
+ };
switch (config->undefinedSymbolTreatment) {
- case UndefinedSymbolTreatment::suppress:
- break;
case UndefinedSymbolTreatment::error:
- error(message);
+ error(message());
break;
case UndefinedSymbolTreatment::warning:
- warn(message);
- break;
+ warn(message());
+ LLVM_FALLTHROUGH;
case UndefinedSymbolTreatment::dynamic_lookup:
- error("dynamic_lookup unimplemented for " + message);
+ case UndefinedSymbolTreatment::suppress:
+ symtab->addDynamicLookup(sym.getName());
break;
case UndefinedSymbolTreatment::unknown:
llvm_unreachable("unknown -undefined TREATMENT");