Skip to content

Commit 5cbb61e

Browse files
committed
[lld][WebAssembly] Allow --trace-symbol to work with symbols with custom import names
1 parent 61e62ce commit 5cbb61e

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

lld/test/wasm/trace-symbol.s

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1+
# Test -y symbol and -trace-symbol=symbol
2+
13
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/ret32.s -o %t.ret32.o
24
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.start.o %s
35
# RUN: wasm-ld -o %t.wasm %t.start.o %t.ret32.o -y ret32 -y _start | FileCheck %s -check-prefix=BOTH
46
# RUN: wasm-ld -o %t.wasm %t.ret32.o %t.start.o -y ret32 -y _start | FileCheck %s -check-prefix=REVERSED
57

6-
# check alias
8+
# check long argument form
79
# RUN: wasm-ld -o %t.wasm %t.start.o %t.ret32.o -trace-symbol=_start | FileCheck %s -check-prefixes=JUST-START
810

11+
# check import names can be traced
12+
# RUN: wasm-ld -o %t.wasm %t.start.o %t.ret32.o -trace-symbol=foo_external | FileCheck %s -check-prefixes=IMPORT-NAME
13+
914
.functype ret32 (f32) -> (i32)
1015

16+
.functype foo_import () -> ()
17+
.import_name foo_import, foo_external
18+
1119
.globl _start
1220
_start:
1321
.functype _start () -> ()
1422
f32.const 0.0
1523
call ret32
1624
drop
25+
call foo_import
1726
end_function
1827

1928
# BOTH: start.o: definition of _start
@@ -26,3 +35,6 @@ _start:
2635

2736
# JUST-START: start.o: definition of _start
2837
# JUST-START-NOT: ret32
38+
39+
# IMPORT-NAME: start.o: reference to foo_external
40+
# IMPORT-NAME-NOT: ret32

lld/wasm/SymbolTable.cpp

+31-6
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,15 @@ static void setImportAttributes(T *existing,
507507
}
508508
}
509509

510+
static void traceImport(std::optional<StringRef> importName, InputFile *file) {
511+
if (importName.has_value()) {
512+
auto name = importName.value();
513+
if (symtab->isTraced(name)) {
514+
printTraceSymbolUndefined(name, file);
515+
}
516+
}
517+
}
518+
510519
Symbol *SymbolTable::addUndefinedFunction(StringRef name,
511520
std::optional<StringRef> importName,
512521
std::optional<StringRef> importModule,
@@ -526,6 +535,7 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef name,
526535
printTraceSymbolUndefined(name, file);
527536

528537
auto replaceSym = [&]() {
538+
traceImport(importName, file);
529539
replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,
530540
file, sig, isCalledDirectly);
531541
};
@@ -560,6 +570,7 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef name,
560570
replaceSym();
561571
}
562572
if (existingUndefined) {
573+
traceImport(importName, file);
563574
setImportAttributes(existingUndefined, importName, importModule, flags,
564575
file);
565576
if (isCalledDirectly)
@@ -612,10 +623,11 @@ Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
612623
if (s->traced)
613624
printTraceSymbolUndefined(name, file);
614625

615-
if (wasInserted)
626+
if (wasInserted) {
627+
traceImport(importName, file);
616628
replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags,
617629
file, type);
618-
else if (auto *lazy = dyn_cast<LazySymbol>(s))
630+
} else if (auto *lazy = dyn_cast<LazySymbol>(s))
619631
lazy->extract();
620632
else if (s->isDefined())
621633
checkGlobalType(s, file, type);
@@ -638,10 +650,11 @@ Symbol *SymbolTable::addUndefinedTable(StringRef name,
638650
if (s->traced)
639651
printTraceSymbolUndefined(name, file);
640652

641-
if (wasInserted)
653+
if (wasInserted) {
654+
traceImport(importName, file);
642655
replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags,
643656
file, type);
644-
else if (auto *lazy = dyn_cast<LazySymbol>(s))
657+
} else if (auto *lazy = dyn_cast<LazySymbol>(s))
645658
lazy->extract();
646659
else if (s->isDefined())
647660
checkTableType(s, file, type);
@@ -664,10 +677,11 @@ Symbol *SymbolTable::addUndefinedTag(StringRef name,
664677
if (s->traced)
665678
printTraceSymbolUndefined(name, file);
666679

667-
if (wasInserted)
680+
if (wasInserted) {
681+
traceImport(importName, file);
668682
replaceSymbol<UndefinedTag>(s, name, importName, importModule, flags, file,
669683
sig);
670-
else if (auto *lazy = dyn_cast<LazySymbol>(s))
684+
} else if (auto *lazy = dyn_cast<LazySymbol>(s))
671685
lazy->extract();
672686
else if (s->isDefined())
673687
checkTagType(s, file, sig);
@@ -828,9 +842,20 @@ bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig,
828842
// Set a flag for --trace-symbol so that we can print out a log message
829843
// if a new symbol with the same name is inserted into the symbol table.
830844
void SymbolTable::trace(StringRef name) {
845+
tracingEnabled = true;
831846
symMap.insert({CachedHashStringRef(name), -1});
832847
}
833848

849+
bool SymbolTable::isTraced(StringRef name) {
850+
// Early exit in the default case to avoid symbol hashmap lookup.
851+
if (!tracingEnabled)
852+
return false;
853+
auto it = symMap.find(CachedHashStringRef(name));
854+
if (it == symMap.end())
855+
return false;
856+
return it->second == -1 || symVector[it->second]->traced;
857+
}
858+
834859
void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
835860
// Swap symbols as instructed by -wrap.
836861
int &origIdx = symMap[CachedHashStringRef(sym->getName())];

lld/wasm/SymbolTable.h

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class SymbolTable {
5050

5151
void trace(StringRef name);
5252

53+
bool isTraced(StringRef name);
54+
5355
Symbol *addDefinedFunction(StringRef name, uint32_t flags, InputFile *file,
5456
InputFunction *function);
5557
Symbol *addDefinedData(StringRef name, uint32_t flags, InputFile *file,
@@ -132,6 +134,8 @@ class SymbolTable {
132134

133135
// For LTO.
134136
std::unique_ptr<BitcodeCompiler> lto;
137+
138+
bool tracingEnabled = false;
135139
};
136140

137141
extern SymbolTable *symtab;

0 commit comments

Comments
 (0)