#include "MarkLive.h"
#include "Config.h"
#include "OutputSegment.h"
#include "SymbolTable.h"
#include "Symbols.h"
#include "UnwindInfoSection.h"
#include "lld/Common/ErrorHandler.h"
#include "llvm/Support/TimeProfiler.h"
#include "mach-o/compact_unwind_encoding.h"
namespace lld::macho {
using namespace llvm;
using namespace llvm::MachO;
struct WhyLiveEntry {
InputSection *isec;
const WhyLiveEntry *prev;
WhyLiveEntry(InputSection *isec, const WhyLiveEntry *prev)
: isec(isec), prev(prev) {}
};
class MarkLive {
public:
virtual void enqueue(InputSection *isec, uint64_t off) = 0;
virtual void addSym(Symbol *s) = 0;
virtual void markTransitively() = 0;
virtual ~MarkLive() = default;
};
template <bool RecordWhyLive> class MarkLiveImpl : public MarkLive {
public:
using WorklistEntry =
std::conditional_t<RecordWhyLive, WhyLiveEntry, InputSection>;
void enqueue(InputSection *isec, uint64_t off) override {
enqueue(isec, off, nullptr);
}
void addSym(Symbol *s) override { addSym(s, nullptr); }
void markTransitively() override;
private:
void enqueue(InputSection *isec, uint64_t off, const WorklistEntry *prev);
void addSym(Symbol *s, const WorklistEntry *prev);
const InputSection *getInputSection(const WorklistEntry *) const;
WorklistEntry *makeEntry(InputSection *, const WorklistEntry *prev) const;
SmallVector<WorklistEntry *, 256> worklist;
};
template <bool RecordWhyLive>
void MarkLiveImpl<RecordWhyLive>::enqueue(
InputSection *isec, uint64_t off,
const typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) {
if (isec->isLive(off))
return;
isec->markLive(off);
if (auto s = dyn_cast<ConcatInputSection>(isec)) {
assert(!s->isCoalescedWeak());
worklist.push_back(makeEntry(s, prev));
}
}
static void printWhyLive(const Symbol *s, const WhyLiveEntry *prev) {
std::string out = toString(*s) + " from " + toString(s->getFile());
int indent = 2;
for (const WhyLiveEntry *entry = prev; entry;
entry = entry->prev, indent += 2) {
const TinyPtrVector<Defined *> &symbols = entry->isec->symbols;
if (!symbols.empty())
out += "\n" + std::string(indent, ' ') + toString(*symbols.front()) +
" from " + toString(symbols.front()->getFile());
}
message(out);
}
template <bool RecordWhyLive>
void MarkLiveImpl<RecordWhyLive>::addSym(
Symbol *s,
const typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) {
if (s->used)
return;
s->used = true;
if constexpr (RecordWhyLive)
if (!config->whyLive.empty() && config->whyLive.match(s->getName()))
printWhyLive(s, prev);
if (auto *d = dyn_cast<Defined>(s)) {
if (d->isec())
enqueue(d->isec(), d->value, prev);
if (d->unwindEntry())
enqueue(d->unwindEntry(), 0, prev);
}
}
template <bool RecordWhyLive>
const InputSection *MarkLiveImpl<RecordWhyLive>::getInputSection(
const MarkLiveImpl<RecordWhyLive>::WorklistEntry *entry) const {
if constexpr (RecordWhyLive)
return entry->isec;
else
return entry;
}
template <bool RecordWhyLive>
typename MarkLiveImpl<RecordWhyLive>::WorklistEntry *
MarkLiveImpl<RecordWhyLive>::makeEntry(
InputSection *isec,
const MarkLiveImpl<RecordWhyLive>::WorklistEntry *prev) const {
if constexpr (RecordWhyLive) {
if (!isec) {
assert(!prev);
return nullptr;
}
return make<WhyLiveEntry>(isec, prev);
} else {
return isec;
}
}
template <bool RecordWhyLive>
void MarkLiveImpl<RecordWhyLive>::markTransitively() {
do {
while (!worklist.empty()) {
WorklistEntry *entry = worklist.pop_back_val();
auto *isec = cast<ConcatInputSection>(getInputSection(entry));
assert(isec->live && "We mark as live when pushing onto the worklist!");
for (const Reloc &r : isec->relocs) {
if (auto *s = r.referent.dyn_cast<Symbol *>())
addSym(s, entry);
else
enqueue(r.referent.get<InputSection *>(), r.addend, entry);
}
for (Defined *d : getInputSection(entry)->symbols)
addSym(d, entry);
}
for (ConcatInputSection *isec : inputSections) {
if (!(isec->getFlags() & S_ATTR_LIVE_SUPPORT) || isec->live)
continue;
for (const Reloc &r : isec->relocs) {
if (auto *s = r.referent.dyn_cast<Symbol *>()) {
if (s->isLive()) {
InputSection *referentIsec = nullptr;
if (auto *d = dyn_cast<Defined>(s))
referentIsec = d->isec();
enqueue(isec, 0, makeEntry(referentIsec, nullptr));
}
} else {
auto *referentIsec = r.referent.get<InputSection *>();
if (referentIsec->isLive(r.addend))
enqueue(isec, 0, makeEntry(referentIsec, nullptr));
}
}
}
} while (!worklist.empty());
}
void markLive() {
TimeTraceScope timeScope("markLive");
MarkLive *marker;
if (config->whyLive.empty())
marker = make<MarkLiveImpl<false>>();
else
marker = make<MarkLiveImpl<true>>();
if (config->entry)
marker->addSym(config->entry);
for (Symbol *sym : symtab->getSymbols()) {
if (auto *defined = dyn_cast<Defined>(sym)) {
if (!config->exportedSymbols.empty() &&
config->exportedSymbols.match(defined->getName())) {
marker->addSym(defined);
continue;
}
if (defined->referencedDynamically || defined->noDeadStrip) {
marker->addSym(defined);
continue;
}
bool externsAreRoots =
config->outputType != MH_EXECUTE || config->exportDynamic;
if (externsAreRoots && !defined->privateExtern) {
marker->addSym(defined);
continue;
}
}
}
for (Symbol *sym : config->explicitUndefineds)
marker->addSym(sym);
for (const InputFile *file : inputFiles)
if (auto *objFile = dyn_cast<ObjFile>(file))
for (Symbol *sym : objFile->symbols)
if (auto *defined = dyn_cast_or_null<Defined>(sym))
if (!defined->isExternal() && defined->noDeadStrip)
marker->addSym(defined);
if (auto *stubBinder =
dyn_cast_or_null<DylibSymbol>(symtab->find("dyld_stub_binder")))
marker->addSym(stubBinder);
for (ConcatInputSection *isec : inputSections) {
if (isec->getFlags() & S_ATTR_NO_DEAD_STRIP) {
marker->enqueue(isec, 0);
continue;
}
if (sectionType(isec->getFlags()) == S_MOD_INIT_FUNC_POINTERS ||
sectionType(isec->getFlags()) == S_MOD_TERM_FUNC_POINTERS) {
assert(!config->emitInitOffsets ||
sectionType(isec->getFlags()) != S_MOD_INIT_FUNC_POINTERS);
marker->enqueue(isec, 0);
continue;
}
}
for (ConcatInputSection *isec : in.initOffsets->inputs())
marker->enqueue(isec, 0);
marker->markTransitively();
}
}