Skip to content

Commit 9c865c2

Browse files
sbc100tstellar
authored andcommitted
[lld][WebAssembly] Implement --why-extract flag from the ELF backend
See https://reviews.llvm.org/D109572 for the original ELF version. Differential Revision: https://reviews.llvm.org/D145431 (cherry picked from commit 8aef04f)
1 parent ad815b6 commit 9c865c2

File tree

5 files changed

+143
-8
lines changed

5 files changed

+143
-8
lines changed

lld/test/wasm/why-extract.s

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# RUN: rm -rf %t && split-file %s %t
2+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/main.s -o %t/main.o
3+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/a.s -o %t/a.o
4+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/a_b.s -o %t/a_b.o
5+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/b.s -o %t/b.o
6+
# RUN: llvm-ar rc %t/a.a %t/a.o
7+
# RUN: llvm-ar rc %t/a_b.a %t/a_b.o
8+
# RUN: llvm-ar rc %t/b.a %t/b.o
9+
# RUN: cd %t
10+
11+
## Nothing is extracted from an archive. The file is created with just a header.
12+
# RUN: wasm-ld main.o a.o b.a -o /dev/null --why-extract=why1.txt
13+
# RUN: FileCheck %s --input-file=why1.txt --check-prefix=CHECK1 --match-full-lines --strict-whitespace
14+
15+
# CHECK1:reference extracted symbol
16+
# CHECK1-NOT:{{.}}
17+
18+
## Some archive members are extracted.
19+
# RUN: wasm-ld main.o a_b.a b.a -o /dev/null --why-extract=why2.txt
20+
# RUN: FileCheck %s --input-file=why2.txt --check-prefix=CHECK2 --match-full-lines --strict-whitespace
21+
22+
# CHECK2:reference extracted symbol
23+
# CHECK2-NEXT:main.o a_b.a(a_b.o) a
24+
# CHECK2-NEXT:a_b.a(a_b.o) b.a(b.o) b()
25+
26+
## An undefined symbol error does not suppress the output.
27+
# RUN: not wasm-ld main.o a_b.a -o /dev/null --why-extract=why3.txt
28+
# RUN: FileCheck %s --input-file=why3.txt --check-prefix=CHECK3 --match-full-lines --strict-whitespace
29+
30+
## Check that backward references are supported.
31+
## - means stdout.
32+
# RUN: wasm-ld b.a a_b.a main.o -o /dev/null --why-extract=- | FileCheck %s --check-prefix=CHECK4
33+
34+
# CHECK3:reference extracted symbol
35+
# CHECK3-NEXT:main.o a_b.a(a_b.o) a
36+
37+
# CHECK4:reference extracted symbol
38+
# CHECK4-NEXT:a_b.a(a_b.o) b.a(b.o) b()
39+
# CHECK4-NEXT:main.o a_b.a(a_b.o) a
40+
41+
# RUN: wasm-ld main.o a_b.a b.a -o /dev/null --no-demangle --why-extract=- | FileCheck %s --check-prefix=MANGLED
42+
43+
# MANGLED: a_b.a(a_b.o) b.a(b.o) _Z1bv
44+
45+
# RUN: wasm-ld main.o a.a b.a -o /dev/null -u _Z1bv --why-extract=- | FileCheck %s --check-prefix=UNDEFINED
46+
47+
## We insert -u symbol before processing other files, so its name is <internal>.
48+
## This is not ideal.
49+
# UNDEFINED: <internal> b.a(b.o) b()
50+
51+
# RUN: wasm-ld main.o a.a b.a -o /dev/null -e _Z1bv --why-extract=- | FileCheck %s --check-prefix=ENTRY
52+
53+
# ENTRY: --entry b.a(b.o) b()
54+
55+
# SCRIPT: <internal> b.a(b.o) b()
56+
57+
# RUN: not wasm-ld -shared main.o -o /dev/null --why-extract=/ 2>&1 | FileCheck %s --check-prefix=ERR
58+
59+
# ERR: error: cannot open --why-extract= file /: {{.*}}
60+
61+
#--- main.s
62+
.globl _start
63+
.functype a () -> ()
64+
_start:
65+
.functype _start () -> ()
66+
call a
67+
end_function
68+
69+
#--- a.s
70+
.globl a
71+
a:
72+
.functype a () -> ()
73+
end_function
74+
75+
#--- a_b.s
76+
.functype _Z1bv () -> ()
77+
.globl a
78+
a:
79+
.functype a () -> ()
80+
call _Z1bv
81+
end_function
82+
83+
#--- b.s
84+
.globl _Z1bv
85+
_Z1bv:
86+
.functype _Z1bv () -> ()
87+
end_function

lld/wasm/Config.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
namespace lld {
2020
namespace wasm {
2121

22+
class InputFile;
23+
class Symbol;
24+
2225
// For --unresolved-symbols.
2326
enum class UnresolvedPolicy { ReportError, Warn, Ignore, ImportDynamic };
2427

@@ -72,6 +75,7 @@ struct Configuration {
7275
llvm::StringRef mapFile;
7376
llvm::StringRef outputFile;
7477
llvm::StringRef thinLTOCacheDir;
78+
llvm::StringRef whyExtract;
7579

7680
llvm::StringSet<> allowUndefinedSymbols;
7781
llvm::StringSet<> exportedSymbols;
@@ -82,7 +86,8 @@ struct Configuration {
8286
std::optional<std::vector<std::string>> extraFeatures;
8387

8488
// The following config options do not directly correspond to any
85-
// particular command line options.
89+
// particular command line options, and should probably be moved to seperate
90+
// Ctx struct as in ELF/Config.h
8691

8792
// True if we are creating position-independent code.
8893
bool isPic;
@@ -100,6 +105,11 @@ struct Configuration {
100105
// Will be set to true if bss data segments should be emitted. In most cases
101106
// this is not necessary.
102107
bool emitBssSegments = false;
108+
109+
// A tuple of (reference, extractedFile, sym). Used by --why-extract=.
110+
llvm::SmallVector<std::tuple<std::string, const InputFile *, const Symbol &>,
111+
0>
112+
whyExtractRecords;
103113
};
104114

105115
// The only instance of Configuration struct.

lld/wasm/Driver.cpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ static void readConfigs(opt::InputArgList &args) {
450450
parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
451451
"--thinlto-cache-policy: invalid cache policy");
452452
config->unresolvedSymbols = getUnresolvedSymbolPolicy(args);
453+
config->whyExtract = args.getLastArgValue(OPT_why_extract);
453454
errorHandler().verbose = args.hasArg(OPT_verbose);
454455
LLVM_DEBUG(errorHandler().verbose = true);
455456

@@ -631,7 +632,7 @@ static const char *getReproduceOption(opt::InputArgList &args) {
631632
}
632633

633634
// Force Sym to be entered in the output. Used for -u or equivalent.
634-
static Symbol *handleUndefined(StringRef name) {
635+
static Symbol *handleUndefined(StringRef name, const char *option) {
635636
Symbol *sym = symtab->find(name);
636637
if (!sym)
637638
return nullptr;
@@ -640,8 +641,11 @@ static Symbol *handleUndefined(StringRef name) {
640641
// eliminate it. Mark the symbol as "used" to prevent it.
641642
sym->isUsedInRegularObj = true;
642643

643-
if (auto *lazySym = dyn_cast<LazySymbol>(sym))
644+
if (auto *lazySym = dyn_cast<LazySymbol>(sym)) {
644645
lazySym->fetch();
646+
if (!config->whyExtract.empty())
647+
config->whyExtractRecords.emplace_back(option, sym->getFile(), *sym);
648+
}
645649

646650
return sym;
647651
}
@@ -653,8 +657,31 @@ static void handleLibcall(StringRef name) {
653657

654658
if (auto *lazySym = dyn_cast<LazySymbol>(sym)) {
655659
MemoryBufferRef mb = lazySym->getMemberBuffer();
656-
if (isBitcode(mb))
660+
if (isBitcode(mb)) {
661+
if (!config->whyExtract.empty())
662+
config->whyExtractRecords.emplace_back("<libcall>", sym->getFile(),
663+
*sym);
657664
lazySym->fetch();
665+
}
666+
}
667+
}
668+
669+
static void writeWhyExtract() {
670+
if (config->whyExtract.empty())
671+
return;
672+
673+
std::error_code ec;
674+
raw_fd_ostream os(config->whyExtract, ec, sys::fs::OF_None);
675+
if (ec) {
676+
error("cannot open --why-extract= file " + config->whyExtract + ": " +
677+
ec.message());
678+
return;
679+
}
680+
681+
os << "reference\textracted\tsymbol\n";
682+
for (auto &entry : config->whyExtractRecords) {
683+
os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t'
684+
<< toString(std::get<2>(entry)) << '\n';
658685
}
659686
}
660687

@@ -1035,16 +1062,16 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
10351062

10361063
// Handle the `--undefined <sym>` options.
10371064
for (auto *arg : args.filtered(OPT_undefined))
1038-
handleUndefined(arg->getValue());
1065+
handleUndefined(arg->getValue(), "<internal>");
10391066

10401067
// Handle the `--export <sym>` options
10411068
// This works like --undefined but also exports the symbol if its found
10421069
for (auto &iter : config->exportedSymbols)
1043-
handleUndefined(iter.first());
1070+
handleUndefined(iter.first(), "--export");
10441071

10451072
Symbol *entrySym = nullptr;
10461073
if (!config->relocatable && !config->entry.empty()) {
1047-
entrySym = handleUndefined(config->entry);
1074+
entrySym = handleUndefined(config->entry, "--entry");
10481075
if (entrySym && entrySym->isDefined())
10491076
entrySym->forceExport = true;
10501077
else
@@ -1061,7 +1088,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
10611088
!WasmSym::callCtors->isUsedInRegularObj &&
10621089
WasmSym::callCtors->getName() != config->entry &&
10631090
!config->exportedSymbols.count(WasmSym::callCtors->getName())) {
1064-
if (Symbol *callDtors = handleUndefined("__wasm_call_dtors")) {
1091+
if (Symbol *callDtors =
1092+
handleUndefined("__wasm_call_dtors", "<internal>")) {
10651093
if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
10661094
if (callDtorsFunc->signature &&
10671095
(!callDtorsFunc->signature->Params.empty() ||
@@ -1096,6 +1124,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
10961124
if (errorCount())
10971125
return;
10981126

1127+
writeWhyExtract();
1128+
10991129
// Do link-time optimization if given files are LLVM bitcode files.
11001130
// This compiles bitcode files into real object files.
11011131
symtab->compileBitcodeFiles();

lld/wasm/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ defm whole_archive: B<"whole-archive",
221221
"Force load of all members in a static library",
222222
"Do not force load of all members in a static library (default)">;
223223

224+
def why_extract: JJ<"why-extract=">, HelpText<"Print to a file about why archive members are extracted">;
225+
224226
defm check_features: BB<"check-features",
225227
"Check feature compatibility of linked objects (default)",
226228
"Ignore feature compatibility of linked objects">;

lld/wasm/SymbolTable.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef name,
524524
lazy->signature = sig;
525525
} else {
526526
lazy->fetch();
527+
if (!config->whyExtract.empty())
528+
config->whyExtractRecords.emplace_back(toString(file), s->getFile(),
529+
*s);
527530
}
528531
} else {
529532
auto existingFunction = dyn_cast<FunctionSymbol>(s);
@@ -748,7 +751,10 @@ void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) {
748751
}
749752

750753
LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
754+
const InputFile *oldFile = s->getFile();
751755
file->addMember(sym);
756+
if (!config->whyExtract.empty())
757+
config->whyExtractRecords.emplace_back(toString(oldFile), s->getFile(), *s);
752758
}
753759

754760
bool SymbolTable::addComdat(StringRef name) {

0 commit comments

Comments
 (0)