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"
|
2021-02-11 19:15:24 +08:00
|
|
|
#include "InputElement.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;
|
|
|
|
|
2019-10-10 11:23:06 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
2018-06-22 23:13:10 +08:00
|
|
|
|
2019-10-10 11:23:06 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MarkLive {
|
|
|
|
public:
|
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void enqueue(Symbol *sym);
|
2020-10-01 11:00:04 +08:00
|
|
|
void enqueueInitFunctions(const ObjFile *sym);
|
2019-10-10 11:23:06 +08:00
|
|
|
void mark();
|
2020-10-01 08:21:57 +08:00
|
|
|
bool isCallCtorsLive();
|
2019-10-10 11:23:06 +08:00
|
|
|
|
|
|
|
// A list of chunks to visit.
|
|
|
|
SmallVector<InputChunk *, 256> queue;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void MarkLive::enqueue(Symbol *sym) {
|
|
|
|
if (!sym || sym->isLive())
|
|
|
|
return;
|
|
|
|
LLVM_DEBUG(dbgs() << "markLive: " << sym->getName() << "\n");
|
2020-10-01 11:00:04 +08:00
|
|
|
|
|
|
|
InputFile *file = sym->getFile();
|
|
|
|
bool needInitFunctions = file && !file->isLive() && sym->isDefined();
|
|
|
|
|
2019-10-10 11:23:06 +08:00
|
|
|
sym->markLive();
|
2020-10-01 11:00:04 +08:00
|
|
|
|
|
|
|
// Mark ctor functions in the object that defines this symbol live.
|
|
|
|
// The ctor functions are all referenced by the synthetic callCtors
|
|
|
|
// function. However, this function does not contain relocations so we
|
|
|
|
// have to manually mark the ctors as live.
|
|
|
|
if (needInitFunctions)
|
|
|
|
enqueueInitFunctions(cast<ObjFile>(file));
|
|
|
|
|
2019-10-10 11:23:06 +08:00
|
|
|
if (InputChunk *chunk = sym->getChunk())
|
|
|
|
queue.push_back(chunk);
|
|
|
|
}
|
2018-01-31 09:45:47 +08:00
|
|
|
|
2020-10-01 11:00:04 +08:00
|
|
|
// The ctor functions are all referenced by the synthetic callCtors
|
|
|
|
// function. However, this function does not contain relocations so we
|
|
|
|
// have to manually mark the ctors as live.
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 11:23:06 +08:00
|
|
|
void MarkLive::run() {
|
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);
|
|
|
|
|
2020-12-03 07:55:25 +08:00
|
|
|
if (WasmSym::callDtors)
|
|
|
|
enqueue(WasmSym::callDtors);
|
2020-10-01 08:21:57 +08:00
|
|
|
|
2020-10-01 11:00:04 +08:00
|
|
|
// Enqueue constructors in objects explicitly live from the command-line.
|
|
|
|
for (const ObjFile *obj : symtab->objectFiles)
|
|
|
|
if (obj->isLive())
|
|
|
|
enqueueInitFunctions(obj);
|
|
|
|
|
2019-10-10 11:23:06 +08:00
|
|
|
mark();
|
2020-10-01 11:00:04 +08:00
|
|
|
|
|
|
|
// If we have any non-discarded init functions, mark `__wasm_call_ctors` as
|
|
|
|
// live so that we assign it an index and call it.
|
|
|
|
if (isCallCtorsLive())
|
|
|
|
WasmSym::callCtors->markLive();
|
2019-10-10 11:23:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MarkLive::mark() {
|
2018-02-20 06:34:47 +08:00
|
|
|
// Follow relocations to mark all reachable chunks.
|
2019-10-10 11:23:06 +08:00
|
|
|
while (!queue.empty()) {
|
|
|
|
InputChunk *c = queue.pop_back_val();
|
2018-02-20 06:34:47 +08:00
|
|
|
|
|
|
|
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 ||
|
2020-07-11 07:51:01 +08:00
|
|
|
reloc.Type == R_WASM_TABLE_INDEX_SLEB64 ||
|
|
|
|
reloc.Type == R_WASM_TABLE_INDEX_I32 ||
|
|
|
|
reloc.Type == R_WASM_TABLE_INDEX_I64) {
|
2019-02-05 03:13:46 +08:00
|
|
|
auto *funcSym = cast<FunctionSymbol>(sym);
|
2020-11-24 07:41:07 +08:00
|
|
|
if (funcSym->isStub)
|
2018-03-10 01:06:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
enqueue(sym);
|
2018-01-31 09:45:47 +08:00
|
|
|
}
|
2018-02-20 06:34:47 +08:00
|
|
|
}
|
2019-10-10 11:23:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void markLive() {
|
|
|
|
if (!config->gcSections)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "markLive\n");
|
|
|
|
|
|
|
|
MarkLive marker;
|
|
|
|
marker.run();
|
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));
|
2021-06-15 16:49:43 +08:00
|
|
|
for (InputTag *t : obj->tags)
|
|
|
|
if (!t->live)
|
|
|
|
message("removing unused section " + toString(t));
|
2021-01-05 19:08:58 +08:00
|
|
|
for (InputTable *t : obj->tables)
|
|
|
|
if (!t->live)
|
|
|
|
message("removing unused section " + toString(t));
|
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));
|
2021-01-14 17:15:56 +08:00
|
|
|
for (InputTable *t : symtab->syntheticTables)
|
|
|
|
if (!t->live)
|
|
|
|
message("removing unused section " + toString(t));
|
2018-01-31 09:45:47 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-10 11:23:06 +08:00
|
|
|
|
2020-10-01 08:21:57 +08:00
|
|
|
bool MarkLive::isCallCtorsLive() {
|
|
|
|
// In a reloctable link, we don't call `__wasm_call_ctors`.
|
|
|
|
if (config->relocatable)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// In Emscripten-style PIC, we call `__wasm_call_ctors` which calls
|
2021-01-29 00:20:42 +08:00
|
|
|
// `__wasm_apply_data_relocs`.
|
2020-10-01 08:21:57 +08:00
|
|
|
if (config->isPic)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If there are any init functions, mark `__wasm_call_ctors` live so that
|
|
|
|
// it can call them.
|
|
|
|
for (const ObjFile *file : symtab->objectFiles) {
|
|
|
|
const WasmLinkingData &l = file->getWasmObj()->linkingData();
|
2020-10-01 11:00:04 +08:00
|
|
|
for (const WasmInitFunc &f : l.InitFunctions) {
|
|
|
|
auto *sym = file->getFunctionSymbol(f.Symbol);
|
|
|
|
if (!sym->isDiscarded() && sym->isLive())
|
2020-10-01 08:21:57 +08:00
|
|
|
return true;
|
2020-10-01 11:00:04 +08:00
|
|
|
}
|
2020-10-01 08:21:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-10 11:23:06 +08:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|