Skip to content

Commit cf23159

Browse files
sbc100tstellar
authored andcommitted
Merging r368310:
------------------------------------------------------------------------ r368310 | sbc | 2019-08-08 09:58:36 -0700 (Thu, 08 Aug 2019) | 11 lines [lld][WebAssembly] Add optional symbols after input file handling This allows undefined references in input files be resolved by the optional symbols. Previously we were doing this before input file reading which means it was working only for command line symbols references (i.e. -u or --export). Also use addOptionalDataSymbol for __dso_handle and make all optional symbols hidden by default. Differential Revision: https://reviews.llvm.org/D65920 ------------------------------------------------------------------------
1 parent 9a9b649 commit cf23159

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

lld/test/wasm/global-base.test

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@ CHECK-1024-NEXT: Opcode: I32_CONST
2424
CHECK-1024-NEXT: Value: 1024
2525

2626
CHECK-1024: - Type: EXPORT
27-
CHECK-1024-NEXT: Exports:
28-
CHECK-1024-NEXT: - Name: memory
29-
CHECK-1024-NEXT: Kind: MEMORY
30-
CHECK-1024-NEXT: Index: 0
31-
CHECK-1024-NEXT: - Name: __data_end
27+
CHECK-1024: - Name: __data_end
3228
CHECK-1024-NEXT: Kind: GLOBAL
3329
CHECK-1024-NEXT: Index: 1
3430
CHECK-1024-NEXT: - Name: __global_base
@@ -59,11 +55,7 @@ CHECK-16777216-NEXT: Opcode: I32_CONST
5955
CHECK-16777216-NEXT: Value: 16777216
6056

6157
CHECK-16777216: - Type: EXPORT
62-
CHECK-16777216-NEXT: Exports:
63-
CHECK-16777216-NEXT: - Name: memory
64-
CHECK-16777216-NEXT: Kind: MEMORY
65-
CHECK-16777216-NEXT: Index: 0
66-
CHECK-16777216-NEXT: - Name: __data_end
58+
CHECK-16777216: - Name: __data_end
6759
CHECK-16777216-NEXT: Kind: GLOBAL
6860
CHECK-16777216-NEXT: Index: 1
6961
CHECK-16777216-NEXT: - Name: __global_base

lld/test/wasm/optional-symbol.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: llc -filetype=obj -o %t.o %s
2+
; RUN: wasm-ld --export=get_handle %t.o -o %t.wasm
3+
4+
target triple = "wasm32-unknown-unknown"
5+
6+
@__dso_handle = external global i8*
7+
8+
define i8** @get_handle() {
9+
ret i8** @__dso_handle
10+
}
11+
12+
define void @_start() {
13+
ret void
14+
}

lld/test/wasm/stack-first.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ CHECK-NEXT: Exports:
3232
CHECK-NEXT: - Name: memory
3333
CHECK-NEXT: Kind: MEMORY
3434
CHECK-NEXT: Index: 0
35+
CHECK-NEXT: - Name: _start
36+
CHECK-NEXT: Kind: FUNCTION
37+
CHECK-NEXT: Index: 0
3538
CHECK-NEXT: - Name: __data_end
3639
CHECK-NEXT: Kind: GLOBAL
3740
CHECK-NEXT: Index: 1
3841
CHECK-NEXT: - Name: __heap_base
3942
CHECK-NEXT: Kind: GLOBAL
4043
CHECK-NEXT: Index: 2
41-
CHECK-NEXT: - Name: _start
42-
CHECK-NEXT: Kind: FUNCTION
43-
CHECK-NEXT: Index: 0

lld/wasm/Driver.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,6 @@ static void createSyntheticSymbols() {
482482
}
483483
}
484484

485-
if (!config->shared)
486-
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
487485

488486
if (config->isPic) {
489487
WasmSym::stackPointer =
@@ -512,8 +510,6 @@ static void createSyntheticSymbols() {
512510
// See: https://github.com/WebAssembly/mutable-global
513511
WasmSym::stackPointer = symtab->addSyntheticGlobal(
514512
"__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, stackPointer);
515-
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
516-
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
517513
}
518514

519515
if (config->sharedMemory && !config->shared) {
@@ -539,9 +535,17 @@ static void createSyntheticSymbols() {
539535
"__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
540536
make<SyntheticFunction>(i32ArgSignature, "__wasm_init_tls"));
541537
}
538+
}
539+
540+
static void createOptionalSymbols() {
541+
if (!config->relocatable)
542+
WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
542543

543-
WasmSym::dsoHandle = symtab->addSyntheticDataSymbol(
544-
"__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN);
544+
if (!config->isPic) {
545+
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
546+
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
547+
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
548+
}
545549
}
546550

547551
// Reconstructs command line arguments so that so that you can re-run
@@ -724,6 +728,8 @@ void LinkerDriver::link(ArrayRef<const char *> argsArr) {
724728
if (errorCount())
725729
return;
726730

731+
createOptionalSymbols();
732+
727733
// Handle the `--undefined <sym>` options.
728734
for (auto *arg : args.filtered(OPT_undefined))
729735
handleUndefined(arg->getValue());

lld/wasm/SymbolTable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,15 @@ DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,
205205
// Adds an optional, linker generated, data symbols. The symbol will only be
206206
// added if there is an undefine reference to it, or if it is explictly exported
207207
// via the --export flag. Otherwise we don't add the symbol and return nullptr.
208-
DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name, uint32_t value,
209-
uint32_t flags) {
208+
DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name,
209+
uint32_t value) {
210210
Symbol *s = find(name);
211211
if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0))
212212
s = insertName(name).first;
213213
else if (!s || s->isDefined())
214214
return nullptr;
215215
LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");
216-
auto *rtn = replaceSymbol<DefinedData>(s, name, flags);
216+
auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN);
217217
rtn->setVirtualAddress(value);
218218
rtn->referenced = true;
219219
return rtn;

lld/wasm/SymbolTable.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ class SymbolTable {
7777
InputGlobal *global);
7878
DefinedFunction *addSyntheticFunction(StringRef name, uint32_t flags,
7979
InputFunction *function);
80-
DefinedData *addOptionalDataSymbol(StringRef name, uint32_t value = 0,
81-
uint32_t flags = 0);
80+
DefinedData *addOptionalDataSymbol(StringRef name, uint32_t value = 0);
8281

8382
void handleSymbolVariants();
8483
void handleWeakUndefines();

0 commit comments

Comments
 (0)