2018-01-31 09:45:47 +08:00
|
|
|
//===- MarkLive.cpp -------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2018-01-31 09:45:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements --gc-sections, which is a feature to remove unused
|
|
|
|
// chunks from the output. Unused chunks are those that are not reachable from
|
|
|
|
// known root symbols or chunks. This feature is implemented as a mark-sweep
|
|
|
|
// garbage collector.
|
|
|
|
//
|
|
|
|
// Here's how it works. Each InputChunk has a "Live" bit. The bit is off by
|
|
|
|
// default. Starting with the GC-roots, visit all reachable chunks and set their
|
|
|
|
// Live bits. The Writer will then ignore chunks whose Live bits are off, so
|
|
|
|
// that such chunk are not appear in the output.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MarkLive.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "InputChunks.h"
|
2018-12-08 14:17:43 +08:00
|
|
|
#include "InputEvent.h"
|
2018-04-21 01:28:12 +08:00
|
|
|
#include "InputGlobal.h"
|
2018-01-31 09:45:47 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Symbols.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
|
|
|
void lld::wasm::markLive() {
|
2018-06-22 23:13:10 +08:00
|
|
|
if (!config->gcSections)
|
|
|
|
return;
|
|
|
|
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "markLive\n");
|
2018-01-31 09:45:47 +08:00
|
|
|
SmallVector<InputChunk *, 256> q;
|
|
|
|
|
2019-03-02 06:35:47 +08:00
|
|
|
std::function<void(Symbol*)> enqueue = [&](Symbol *sym) {
|
2018-04-21 01:18:06 +08:00
|
|
|
if (!sym || sym->isLive())
|
2018-01-31 09:45:47 +08:00
|
|
|
return;
|
2018-06-21 23:00:00 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n");
|
2018-04-21 01:18:06 +08:00
|
|
|
sym->markLive();
|
|
|
|
if (InputChunk *chunk = sym->getChunk())
|
|
|
|
q.push_back(chunk);
|
2019-03-02 06:35:47 +08:00
|
|
|
|
2019-07-16 16:08:17 +08:00
|
|
|
// The ctor functions are all referenced by the synthetic callCtors
|
2019-03-02 06:35:47 +08:00
|
|
|
// function. However, this function does not contain relocations so we
|
2019-07-16 16:08:17 +08:00
|
|
|
// have to manually mark the ctors as live if callCtors itself is live.
|
2019-03-02 06:35:47 +08:00
|
|
|
if (sym == WasmSym::callCtors) {
|
2019-07-04 06:04:54 +08:00
|
|
|
if (config->isPic)
|
|
|
|
enqueue(WasmSym::applyRelocs);
|
2019-03-02 06:35:47 +08:00
|
|
|
for (const ObjFile *obj : symtab->objectFiles) {
|
|
|
|
const WasmLinkingData &l = obj->getWasmObj()->linkingData();
|
2019-06-07 14:00:46 +08:00
|
|
|
for (const WasmInitFunc &f : l.InitFunctions) {
|
|
|
|
auto* initSym = obj->getFunctionSymbol(f.Symbol);
|
|
|
|
if (!initSym->isDiscarded())
|
|
|
|
enqueue(initSym);
|
|
|
|
}
|
2019-03-02 06:35:47 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-31 09:45:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add GC root symbols.
|
|
|
|
if (!config->entry.empty())
|
|
|
|
enqueue(symtab->find(config->entry));
|
|
|
|
|
2019-08-30 06:41:05 +08:00
|
|
|
// We need to preserve any no-strip or exported symbol
|
2018-01-31 09:45:47 +08:00
|
|
|
for (Symbol *sym : symtab->getSymbols())
|
2019-08-30 06:41:05 +08:00
|
|
|
if (sym->isNoStrip() || sym->isExported())
|
2018-01-31 09:45:47 +08:00
|
|
|
enqueue(sym);
|
|
|
|
|
2019-03-02 06:35:47 +08:00
|
|
|
// For relocatable output, we need to preserve all the ctor functions
|
|
|
|
if (config->relocatable) {
|
|
|
|
for (const ObjFile *obj : symtab->objectFiles) {
|
|
|
|
const WasmLinkingData &l = obj->getWasmObj()->linkingData();
|
|
|
|
for (const WasmInitFunc &f : l.InitFunctions)
|
|
|
|
enqueue(obj->getFunctionSymbol(f.Symbol));
|
|
|
|
}
|
2018-01-31 09:45:47 +08:00
|
|
|
}
|
|
|
|
|
2019-07-04 06:04:54 +08:00
|
|
|
if (config->isPic)
|
2019-04-05 02:40:51 +08:00
|
|
|
enqueue(WasmSym::callCtors);
|
|
|
|
|
2019-09-05 03:50:39 +08:00
|
|
|
if (config->sharedMemory && !config->shared)
|
|
|
|
enqueue(WasmSym::initMemory);
|
|
|
|
|
2018-02-20 06:34:47 +08:00
|
|
|
// Follow relocations to mark all reachable chunks.
|
|
|
|
while (!q.empty()) {
|
|
|
|
InputChunk *c = q.pop_back_val();
|
|
|
|
|
|
|
|
for (const WasmRelocation reloc : c->getRelocations()) {
|
2019-02-05 01:49:33 +08:00
|
|
|
if (reloc.Type == R_WASM_TYPE_INDEX_LEB)
|
2018-03-10 01:06:38 +08:00
|
|
|
continue;
|
|
|
|
Symbol *sym = c->file->getSymbol(reloc.Index);
|
|
|
|
|
|
|
|
// If the function has been assigned the special index zero in the table,
|
|
|
|
// the relocation doesn't pull in the function body, since the function
|
|
|
|
// won't actually go in the table (the runtime will trap attempts to call
|
|
|
|
// that index, since we don't use it). A function with a table index of
|
|
|
|
// zero is only reachable via "call", not via "call_indirect". The stub
|
|
|
|
// functions used for weak-undefined symbols have this behaviour (compare
|
|
|
|
// equal to null pointer, only reachable via direct call).
|
2019-02-05 01:49:33 +08:00
|
|
|
if (reloc.Type == R_WASM_TABLE_INDEX_SLEB ||
|
|
|
|
reloc.Type == R_WASM_TABLE_INDEX_I32) {
|
2019-02-05 03:13:46 +08:00
|
|
|
auto *funcSym = cast<FunctionSymbol>(sym);
|
2018-03-10 01:06:38 +08:00
|
|
|
if (funcSym->hasTableIndex() && funcSym->getTableIndex() == 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
enqueue(sym);
|
2018-01-31 09:45:47 +08:00
|
|
|
}
|
2018-02-20 06:34:47 +08:00
|
|
|
}
|
2018-01-31 09:45:47 +08:00
|
|
|
|
|
|
|
// Report garbage-collected sections.
|
|
|
|
if (config->printGcSections) {
|
|
|
|
for (const ObjFile *obj : symtab->objectFiles) {
|
|
|
|
for (InputChunk *c : obj->functions)
|
2018-02-20 06:29:48 +08:00
|
|
|
if (!c->live)
|
|
|
|
message("removing unused section " + toString(c));
|
2018-01-31 09:45:47 +08:00
|
|
|
for (InputChunk *c : obj->segments)
|
2018-02-20 06:29:48 +08:00
|
|
|
if (!c->live)
|
|
|
|
message("removing unused section " + toString(c));
|
2018-04-21 01:28:12 +08:00
|
|
|
for (InputGlobal *g : obj->globals)
|
|
|
|
if (!g->live)
|
|
|
|
message("removing unused section " + toString(g));
|
2018-12-08 14:17:43 +08:00
|
|
|
for (InputEvent *e : obj->events)
|
|
|
|
if (!e->live)
|
|
|
|
message("removing unused section " + toString(e));
|
2018-01-31 09:45:47 +08:00
|
|
|
}
|
2018-04-21 01:09:18 +08:00
|
|
|
for (InputChunk *c : symtab->syntheticFunctions)
|
|
|
|
if (!c->live)
|
|
|
|
message("removing unused section " + toString(c));
|
2018-04-21 01:28:12 +08:00
|
|
|
for (InputGlobal *g : symtab->syntheticGlobals)
|
|
|
|
if (!g->live)
|
|
|
|
message("removing unused section " + toString(g));
|
2018-01-31 09:45:47 +08:00
|
|
|
}
|
|
|
|
}
|