Skip to content

Commit

Permalink
[lld][WebAssembly] Fix crash with -pie without --allow-undefined
Browse files Browse the repository at this point in the history
`shouldImport` was not returning true in PIC mode even though out
assumption elsewhere (in Relocations.cpp:scanRelocations) is that we
don't report undefined symbols in PIC mode today.  This was resulting
functions that were undefined and but also not imported which hits an
assert later on that all functions have valid indexes.

Differential Revision: https://reviews.llvm.org/D101716
  • Loading branch information
sbc100 committed May 4, 2021
1 parent 24ce194 commit 808fcdd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
27 changes: 16 additions & 11 deletions lld/test/wasm/pie.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; RUN: llc -relocation-model=pic -mattr=+mutable-globals -filetype=obj %s -o %t.o
; RUN: wasm-ld --no-gc-sections --allow-undefined --experimental-pic -pie -o %t.wasm %t.o
; RUN: wasm-ld --no-gc-sections --experimental-pic -pie -o %t.wasm %t.o

target triple = "wasm32-unknown-emscripten"

Expand All @@ -26,9 +26,12 @@ entry:
}

define void @_start() {
call void @external_func()
ret void
}

declare void @external_func()

; CHECK: Sections:
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: dylink
Expand Down Expand Up @@ -88,27 +91,29 @@ define void @_start() {
; RUN: obj2yaml %t.shmem.wasm | FileCheck %s --check-prefix=SHMEM

; SHMEM: - Type: CODE
; SHMEM: - Index: 5
; SHMEM: - Index: 6
; SHMEM-NEXT: Locals: []
; SHMEM-NEXT: Body: 100210040B
; SHMEM-NEXT: Body: 100310050B

; SHMEM: FunctionNames:
; SHMEM-NEXT: - Index: 0
; SHMEM-NEXT: Name: __wasm_call_ctors
; SHMEM-NEXT: Name: external_func
; SHMEM-NEXT: - Index: 1
; SHMEM-NEXT: Name: __wasm_init_tls
; SHMEM-NEXT: Name: __wasm_call_ctors
; SHMEM-NEXT: - Index: 2
; SHMEM-NEXT: Name: __wasm_init_memory
; SHMEM-NEXT: Name: __wasm_init_tls
; SHMEM-NEXT: - Index: 3
; SHMEM-NEXT: Name: __wasm_apply_data_relocs
; SHMEM-NEXT: Name: __wasm_init_memory
; SHMEM-NEXT: - Index: 4
; SHMEM-NEXT: Name: __wasm_apply_global_relocs
; SHMEM-NEXT: Name: __wasm_apply_data_relocs
; SHMEM-NEXT: - Index: 5
; SHMEM-NEXT: Name: __wasm_start
; SHMEM-NEXT: Name: __wasm_apply_global_relocs
; SHMEM-NEXT: - Index: 6
; SHMEM-NEXT: Name: foo
; SHMEM-NEXT: Name: __wasm_start
; SHMEM-NEXT: - Index: 7
; SHMEM-NEXT: Name: get_data_address
; SHMEM-NEXT: Name: foo
; SHMEM-NEXT: - Index: 8
; SHMEM-NEXT: Name: get_data_address
; SHMEM-NEXT: - Index: 9
; SHMEM-NEXT: Name: _start

5 changes: 2 additions & 3 deletions lld/wasm/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ void scanRelocations(InputChunk *chunk) {
addGOTEntry(sym);
break;
}
} else {
} else if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) {
// Report undefined symbols
if (sym->isUndefined() && !config->relocatable && !sym->isWeak())
reportUndefined(sym);
reportUndefined(sym);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lld/wasm/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ static bool shouldImport(Symbol *sym) {
if (isa<DataSymbol>(sym))
return false;

if (config->relocatable ||
if ((config->isPic || config->relocatable) ||
config->unresolvedSymbols == UnresolvedPolicy::ImportFuncs)
return true;
if (config->allowUndefinedSymbols.count(sym->getName()) != 0)
Expand Down

0 comments on commit 808fcdd

Please sign in to comment.