Skip to content

Commit 08e16d6

Browse files
committed
[lld][WebAssembly] Allow --trace-symbol to work with symbols with custom import names
1 parent 486d00e commit 08e16d6

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-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
@@ -605,6 +605,15 @@ static void setImportAttributes(T *existing,
605605
}
606606
}
607607

608+
static void traceImport(std::optional<StringRef> importName, InputFile *file) {
609+
if (importName.has_value()) {
610+
auto name = importName.value();
611+
if (symtab->isTraced(name)) {
612+
printTraceSymbolUndefined(name, file);
613+
}
614+
}
615+
}
616+
608617
Symbol *SymbolTable::addUndefinedFunction(StringRef name,
609618
std::optional<StringRef> importName,
610619
std::optional<StringRef> importModule,
@@ -624,6 +633,7 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef name,
624633
printTraceSymbolUndefined(name, file);
625634

626635
auto replaceSym = [&]() {
636+
traceImport(importName, file);
627637
replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,
628638
file, sig, isCalledDirectly);
629639
};
@@ -666,6 +676,7 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef name,
666676
replaceSym();
667677
}
668678
if (existingUndefined) {
679+
traceImport(importName, file);
669680
setImportAttributes(existingUndefined, importName, importModule, flags,
670681
file);
671682
if (isCalledDirectly)
@@ -718,10 +729,11 @@ Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
718729
if (s->traced)
719730
printTraceSymbolUndefined(name, file);
720731

721-
if (wasInserted)
732+
if (wasInserted) {
733+
traceImport(importName, file);
722734
replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags,
723735
file, type);
724-
else if (auto *lazy = dyn_cast<LazySymbol>(s))
736+
} else if (auto *lazy = dyn_cast<LazySymbol>(s))
725737
lazy->extract();
726738
else if (s->isDefined())
727739
checkGlobalType(s, file, type);
@@ -744,10 +756,11 @@ Symbol *SymbolTable::addUndefinedTable(StringRef name,
744756
if (s->traced)
745757
printTraceSymbolUndefined(name, file);
746758

747-
if (wasInserted)
759+
if (wasInserted) {
760+
traceImport(importName, file);
748761
replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags,
749762
file, type);
750-
else if (auto *lazy = dyn_cast<LazySymbol>(s))
763+
} else if (auto *lazy = dyn_cast<LazySymbol>(s))
751764
lazy->extract();
752765
else if (s->isDefined())
753766
checkTableType(s, file, type);
@@ -770,10 +783,11 @@ Symbol *SymbolTable::addUndefinedTag(StringRef name,
770783
if (s->traced)
771784
printTraceSymbolUndefined(name, file);
772785

773-
if (wasInserted)
786+
if (wasInserted) {
787+
traceImport(importName, file);
774788
replaceSymbol<UndefinedTag>(s, name, importName, importModule, flags, file,
775789
sig);
776-
else if (auto *lazy = dyn_cast<LazySymbol>(s))
790+
} else if (auto *lazy = dyn_cast<LazySymbol>(s))
777791
lazy->extract();
778792
else if (s->isDefined())
779793
checkTagType(s, file, sig);
@@ -937,9 +951,20 @@ bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig,
937951
// Set a flag for --trace-symbol so that we can print out a log message
938952
// if a new symbol with the same name is inserted into the symbol table.
939953
void SymbolTable::trace(StringRef name) {
954+
tracingEnabled = true;
940955
symMap.insert({CachedHashStringRef(name), -1});
941956
}
942957

958+
bool SymbolTable::isTraced(StringRef name) {
959+
// Early exit in the default case to avoid symbol hashmap lookup.
960+
if (!tracingEnabled)
961+
return false;
962+
auto it = symMap.find(CachedHashStringRef(name));
963+
if (it == symMap.end())
964+
return false;
965+
return it->second == -1 || symVector[it->second]->traced;
966+
}
967+
943968
void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
944969
// Swap symbols as instructed by -wrap.
945970
int &origIdx = symMap[CachedHashStringRef(sym->getName())];

lld/wasm/SymbolTable.h

+5
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ class SymbolTable {
5050

5151
void trace(StringRef name);
5252

53+
bool isTraced(StringRef name);
54+
5355
Symbol *addSharedFunction(StringRef name, uint32_t flags, InputFile *file,
5456
const WasmSignature *sig);
5557
Symbol *addSharedData(StringRef name, uint32_t flags, InputFile *file);
58+
5659
Symbol *addDefinedFunction(StringRef name, uint32_t flags, InputFile *file,
5760
InputFunction *function);
5861
Symbol *addDefinedData(StringRef name, uint32_t flags, InputFile *file,
@@ -135,6 +138,8 @@ class SymbolTable {
135138

136139
// For LTO.
137140
std::unique_ptr<BitcodeCompiler> lto;
141+
142+
bool tracingEnabled = false;
138143
};
139144

140145
extern SymbolTable *symtab;

0 commit comments

Comments
 (0)