diff --git a/lld/wasm/MarkLive.cpp b/lld/wasm/MarkLive.cpp --- a/lld/wasm/MarkLive.cpp +++ b/lld/wasm/MarkLive.cpp @@ -25,6 +25,9 @@ #include "SymbolTable.h" #include "Symbols.h" +#include "lld/Common/CommonLinkerContext.h" +#include "lld/Common/Strings.h" + #define DEBUG_TYPE "lld" using namespace llvm; @@ -98,10 +101,23 @@ enqueue(WasmSym::callDtors); // Enqueue constructors in objects explicitly live from the command-line. - for (const ObjFile *obj : symtab->objectFiles) - if (obj->isLive()) + for (const ObjFile *obj : symtab->objectFiles) { + if (obj->isLive()) { enqueueInitFunctions(obj); + // If the `__start_` symbol for a given segment is referenced then keep + // all of its contents alive. + for (InputChunk *seg : obj->segments) { + if (isValidCIdentifier(seg->name)) { + StringRef name = saver().save("__start_" + seg->name); + if (symtab->find(name) || config->exportedSymbols.count(name) != 0) { + seg->live = true; + } + } + } + } + } + mark(); // If we have any non-discarded init functions, mark `__wasm_call_ctors` as