2018-01-31 09:45:47 +08:00
|
|
|
//===- MarkLive.cpp -------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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-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;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::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;
|
|
|
|
|
|
|
|
auto 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);
|
2018-01-31 09:45:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add GC root symbols.
|
|
|
|
if (!Config->Entry.empty())
|
|
|
|
Enqueue(Symtab->find(Config->Entry));
|
2018-02-03 06:59:56 +08:00
|
|
|
Enqueue(WasmSym::CallCtors);
|
2018-01-31 09:45:47 +08:00
|
|
|
|
2018-06-29 01:04:58 +08:00
|
|
|
// We need to preserve any exported symbol
|
2018-01-31 09:45:47 +08:00
|
|
|
for (Symbol *Sym : Symtab->getSymbols())
|
2018-06-29 01:04:58 +08:00
|
|
|
if (Sym->isExported())
|
2018-01-31 09:45:47 +08:00
|
|
|
Enqueue(Sym);
|
|
|
|
|
2018-02-17 02:37:32 +08:00
|
|
|
// The ctor functions are all used in the synthetic __wasm_call_ctors
|
|
|
|
// function, but since this function is created in-place it doesn't contain
|
2018-02-20 06:34:47 +08:00
|
|
|
// relocations which mean we have to manually mark the ctors.
|
2018-01-31 09:45:47 +08:00
|
|
|
for (const ObjFile *Obj : Symtab->ObjectFiles) {
|
|
|
|
const WasmLinkingData &L = Obj->getWasmObj()->linkingData();
|
|
|
|
for (const WasmInitFunc &F : L.InitFunctions)
|
2018-02-23 13:08:53 +08:00
|
|
|
Enqueue(Obj->getFunctionSymbol(F.Symbol));
|
2018-01-31 09:45:47 +08:00
|
|
|
}
|
|
|
|
|
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()) {
|
2018-03-10 01:06:38 +08:00
|
|
|
if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB)
|
|
|
|
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).
|
|
|
|
if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB ||
|
|
|
|
Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32) {
|
|
|
|
FunctionSymbol *FuncSym = cast<FunctionSymbol>(Sym);
|
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|