#include "MarkLive.h"
#include "Config.h"
#include "InputChunks.h"
#include "InputElement.h"
#include "SymbolTable.h"
#include "Symbols.h"
#define DEBUG_TYPE "lld"
using namespace llvm;
using namespace llvm::wasm;
namespace lld::wasm {
namespace {
class MarkLive {
public:
void run();
private:
void enqueue(Symbol *sym);
void enqueue(InputChunk *chunk);
void enqueueInitFunctions(const ObjFile *sym);
void enqueueRetainedSegments(const ObjFile *file);
void mark();
bool isCallCtorsLive();
SmallVector<InputChunk *, 256> queue;
};
}
void MarkLive::enqueue(Symbol *sym) {
if (!sym || sym->isLive())
return;
LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n");
InputFile *file = sym->getFile();
bool markImplicitDeps = file && !file->isLive() && sym->isDefined();
sym->markLive();
if (markImplicitDeps) {
if (auto obj = dyn_cast<ObjFile>(file)) {
enqueueInitFunctions(obj);
enqueueRetainedSegments(obj);
}
}
if (InputChunk *chunk = sym->getChunk())
queue.push_back(chunk);
}
void MarkLive::enqueue(InputChunk *chunk) {
LLVM_DEBUG(dbgs() << "markLive: " << toString(chunk) << "\n");
chunk->live = true;
queue.push_back(chunk);
}
void MarkLive::enqueueInitFunctions(const ObjFile *obj) {
const WasmLinkingData &l = obj->getWasmObj()->linkingData();
for (const WasmInitFunc &f : l.InitFunctions) {
auto *initSym = obj->getFunctionSymbol(f.Symbol);
if (!initSym->isDiscarded())
enqueue(initSym);
}
}
void MarkLive::enqueueRetainedSegments(const ObjFile *file) {
for (InputChunk *chunk : file->segments)
if (chunk->isRetained())
enqueue(chunk);
}
void MarkLive::run() {
if (!config->entry.empty())
enqueue(symtab->find(config->entry));
for (Symbol *sym : symtab->symbols())
if (sym->isNoStrip() || sym->isExported())
enqueue(sym);
if (WasmSym::callDtors)
enqueue(WasmSym::callDtors);
for (const ObjFile *obj : ctx.objectFiles)
if (obj->isLive()) {
enqueueInitFunctions(obj);
enqueueRetainedSegments(obj);
}
mark();
if (isCallCtorsLive())
WasmSym::callCtors->markLive();
}
void MarkLive::mark() {
while (!queue.empty()) {
InputChunk *c = queue.pop_back_val();
for (const WasmRelocation reloc : c->getRelocations()) {
if (reloc.Type == R_WASM_TYPE_INDEX_LEB)
continue;
Symbol *sym = c->file->getSymbol(reloc.Index);
if (reloc.Type == R_WASM_TABLE_INDEX_SLEB ||
reloc.Type == R_WASM_TABLE_INDEX_SLEB64 ||
reloc.Type == R_WASM_TABLE_INDEX_I32 ||
reloc.Type == R_WASM_TABLE_INDEX_I64) {
auto *funcSym = cast<FunctionSymbol>(sym);
if (funcSym->isStub)
continue;
}
enqueue(sym);
}
}
}
void markLive() {
if (!config->gcSections)
return;
LLVM_DEBUG(dbgs() << "markLive\n");
MarkLive marker;
marker.run();
if (config->printGcSections) {
for (const ObjFile *obj : ctx.objectFiles) {
for (InputChunk *c : obj->functions)
if (!c->live)
message("removing unused section " + toString(c));
for (InputChunk *c : obj->segments)
if (!c->live)
message("removing unused section " + toString(c));
for (InputGlobal *g : obj->globals)
if (!g->live)
message("removing unused section " + toString(g));
for (InputTag *t : obj->tags)
if (!t->live)
message("removing unused section " + toString(t));
for (InputTable *t : obj->tables)
if (!t->live)
message("removing unused section " + toString(t));
}
for (InputChunk *c : ctx.syntheticFunctions)
if (!c->live)
message("removing unused section " + toString(c));
for (InputGlobal *g : ctx.syntheticGlobals)
if (!g->live)
message("removing unused section " + toString(g));
for (InputTable *t : ctx.syntheticTables)
if (!t->live)
message("removing unused section " + toString(t));
}
}
bool MarkLive::isCallCtorsLive() {
if (config->relocatable)
return false;
if (ctx.isPic)
return true;
for (const ObjFile *file : ctx.objectFiles) {
const WasmLinkingData &l = file->getWasmObj()->linkingData();
for (const WasmInitFunc &f : l.InitFunctions) {
auto *sym = file->getFunctionSymbol(f.Symbol);
if (!sym->isDiscarded() && sym->isLive())
return true;
}
}
return false;
}
}