[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

llvm-svn: 368310
This commit is contained in:
Sam Clegg 2019-08-08 16:58:36 +00:00
parent 0e5eef5c8f
commit caa0db1318
6 changed files with 35 additions and 24 deletions

View File

@ -24,11 +24,7 @@ CHECK-1024-NEXT: Opcode: I32_CONST
CHECK-1024-NEXT: Value: 1024
CHECK-1024: - Type: EXPORT
CHECK-1024-NEXT: Exports:
CHECK-1024-NEXT: - Name: memory
CHECK-1024-NEXT: Kind: MEMORY
CHECK-1024-NEXT: Index: 0
CHECK-1024-NEXT: - Name: __data_end
CHECK-1024: - Name: __data_end
CHECK-1024-NEXT: Kind: GLOBAL
CHECK-1024-NEXT: Index: 1
CHECK-1024-NEXT: - Name: __global_base
@ -59,11 +55,7 @@ CHECK-16777216-NEXT: Opcode: I32_CONST
CHECK-16777216-NEXT: Value: 16777216
CHECK-16777216: - Type: EXPORT
CHECK-16777216-NEXT: Exports:
CHECK-16777216-NEXT: - Name: memory
CHECK-16777216-NEXT: Kind: MEMORY
CHECK-16777216-NEXT: Index: 0
CHECK-16777216-NEXT: - Name: __data_end
CHECK-16777216: - Name: __data_end
CHECK-16777216-NEXT: Kind: GLOBAL
CHECK-16777216-NEXT: Index: 1
CHECK-16777216-NEXT: - Name: __global_base

View File

@ -0,0 +1,14 @@
; RUN: llc -filetype=obj -o %t.o %s
; RUN: wasm-ld --export=get_handle %t.o -o %t.wasm
target triple = "wasm32-unknown-unknown"
@__dso_handle = external global i8*
define i8** @get_handle() {
ret i8** @__dso_handle
}
define void @_start() {
ret void
}

View File

@ -32,12 +32,12 @@ CHECK-NEXT: Exports:
CHECK-NEXT: - Name: memory
CHECK-NEXT: Kind: MEMORY
CHECK-NEXT: Index: 0
CHECK-NEXT: - Name: _start
CHECK-NEXT: Kind: FUNCTION
CHECK-NEXT: Index: 0
CHECK-NEXT: - Name: __data_end
CHECK-NEXT: Kind: GLOBAL
CHECK-NEXT: Index: 1
CHECK-NEXT: - Name: __heap_base
CHECK-NEXT: Kind: GLOBAL
CHECK-NEXT: Index: 2
CHECK-NEXT: - Name: _start
CHECK-NEXT: Kind: FUNCTION
CHECK-NEXT: Index: 0

View File

@ -493,8 +493,6 @@ static void createSyntheticSymbols() {
}
}
if (!config->shared)
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
if (config->isPic) {
WasmSym::stackPointer =
@ -523,8 +521,6 @@ static void createSyntheticSymbols() {
// See: https://github.com/WebAssembly/mutable-global
WasmSym::stackPointer = symtab->addSyntheticGlobal(
"__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, stackPointer);
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
}
if (config->sharedMemory && !config->shared) {
@ -535,9 +531,17 @@ static void createSyntheticSymbols() {
"__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
make<SyntheticFunction>(i32ArgSignature, "__wasm_init_tls"));
}
}
WasmSym::dsoHandle = symtab->addSyntheticDataSymbol(
"__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN);
static void createOptionalSymbols() {
if (!config->relocatable)
WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
if (!config->isPic) {
WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
}
}
// Reconstructs command line arguments so that so that you can re-run
@ -720,6 +724,8 @@ void LinkerDriver::link(ArrayRef<const char *> argsArr) {
if (errorCount())
return;
createOptionalSymbols();
// Handle the `--undefined <sym>` options.
for (auto *arg : args.filtered(OPT_undefined))
handleUndefined(arg->getValue());

View File

@ -205,15 +205,15 @@ DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,
// Adds an optional, linker generated, data symbols. The symbol will only be
// added if there is an undefine reference to it, or if it is explictly exported
// via the --export flag. Otherwise we don't add the symbol and return nullptr.
DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name, uint32_t value,
uint32_t flags) {
DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name,
uint32_t value) {
Symbol *s = find(name);
if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0))
s = insertName(name).first;
else if (!s || s->isDefined())
return nullptr;
LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");
auto *rtn = replaceSymbol<DefinedData>(s, name, flags);
auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN);
rtn->setVirtualAddress(value);
rtn->referenced = true;
return rtn;

View File

@ -77,8 +77,7 @@ public:
InputGlobal *global);
DefinedFunction *addSyntheticFunction(StringRef name, uint32_t flags,
InputFunction *function);
DefinedData *addOptionalDataSymbol(StringRef name, uint32_t value = 0,
uint32_t flags = 0);
DefinedData *addOptionalDataSymbol(StringRef name, uint32_t value = 0);
void handleSymbolVariants();
void handleWeakUndefines();