[WebAssembly] Include weak imports when linking with --relocatable

We need these import since relocations are generated against them.

Patch by Nicholas Wilson!

Differential Revision: https://reviews.llvm.org/D42305

llvm-svn: 322990
This commit is contained in:
Sam Clegg 2018-01-19 20:56:20 +00:00
parent 426f6bef44
commit 729a864dfa
2 changed files with 61 additions and 25 deletions

View File

@ -7,14 +7,17 @@
define hidden i32 @my_func() local_unnamed_addr {
entry:
%call = tail call i32 @foo_import()
%call2 = tail call i32 @bar_import()
ret i32 1
}
declare i32 @foo_import() local_unnamed_addr
declare extern_weak i32 @bar_import() local_unnamed_addr
@data_import = external global i64
@func_addr1 = hidden global i32()* @my_func, align 4
@func_addr2 = hidden global i32()* @foo_import, align 4
@func_addr3 = hidden global i32()* @bar_import, align 4
@data_addr1 = hidden global i64* @data_import, align 8
$func_comdat = comdat any
@ -51,6 +54,10 @@ entry:
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: SigIndex: 2
; CHECK-NEXT: - Module: env
; CHECK-NEXT: Field: bar_import
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: SigIndex: 2
; CHECK-NEXT: - Module: env
; CHECK-NEXT: Field: data_import
; CHECK-NEXT: Kind: GLOBAL
; CHECK-NEXT: GlobalType: I32
@ -62,8 +69,8 @@ entry:
; CHECK-NEXT: - ElemType: ANYFUNC
; CHECK-NEXT: Limits:
; CHECK-NEXT: Flags: [ HAS_MAX ]
; CHECK-NEXT: Initial: 0x00000002
; CHECK-NEXT: Maximum: 0x00000002
; CHECK-NEXT: Initial: 0x00000003
; CHECK-NEXT: Maximum: 0x00000003
; CHECK-NEXT: - Type: MEMORY
; CHECK-NEXT: Memories:
; CHECK-NEXT: - Initial: 0x00000001
@ -80,7 +87,7 @@ entry:
; CHECK-NEXT: Mutable: false
; CHECK-NEXT: InitExpr:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 20
; CHECK-NEXT: Value: 28
; CHECK-NEXT: - Index: 3
; CHECK-NEXT: Type: I32
; CHECK-NEXT: Mutable: false
@ -99,17 +106,23 @@ entry:
; CHECK-NEXT: InitExpr:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 16
; CHECK-NEXT: - Index: 6
; CHECK-NEXT: Type: I32
; CHECK-NEXT: Mutable: false
; CHECK-NEXT: InitExpr:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 24
; CHECK-NEXT: - Type: EXPORT
; CHECK-NEXT: Exports:
; CHECK-NEXT: - Name: hello
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: Index: 2
; CHECK-NEXT: Index: 3
; CHECK-NEXT: - Name: my_func
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: Index: 3
; CHECK-NEXT: Index: 4
; CHECK-NEXT: - Name: func_comdat
; CHECK-NEXT: Kind: FUNCTION
; CHECK-NEXT: Index: 4
; CHECK-NEXT: Index: 5
; CHECK-NEXT: - Name: hello_str
; CHECK-NEXT: Kind: GLOBAL
; CHECK-NEXT: Index: 1
@ -122,15 +135,18 @@ entry:
; CHECK-NEXT: - Name: func_addr2
; CHECK-NEXT: Kind: GLOBAL
; CHECK-NEXT: Index: 4
; CHECK-NEXT: - Name: data_addr1
; CHECK-NEXT: - Name: func_addr3
; CHECK-NEXT: Kind: GLOBAL
; CHECK-NEXT: Index: 5
; CHECK-NEXT: - Name: data_addr1
; CHECK-NEXT: Kind: GLOBAL
; CHECK-NEXT: Index: 6
; CHECK-NEXT: - Type: ELEM
; CHECK-NEXT: Segments:
; CHECK-NEXT: - Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 0
; CHECK-NEXT: Functions: [ 3, 1 ]
; CHECK-NEXT: Functions: [ 4, 1, 2 ]
; CHECK-NEXT: - Type: CODE
; CHECK-NEXT: Relocations:
; CHECK-NEXT: - Type: R_WEBASSEMBLY_MEMORY_ADDR_SLEB
@ -142,19 +158,22 @@ entry:
; CHECK-NEXT: - Type: R_WEBASSEMBLY_FUNCTION_INDEX_LEB
; CHECK-NEXT: Index: 1
; CHECK-NEXT: Offset: 0x00000013
; CHECK-NEXT: - Type: R_WEBASSEMBLY_FUNCTION_INDEX_LEB
; CHECK-NEXT: Index: 2
; CHECK-NEXT: Offset: 0x0000001A
; CHECK-NEXT: - Type: R_WEBASSEMBLY_MEMORY_ADDR_SLEB
; CHECK-NEXT: Index: 2
; CHECK-NEXT: Offset: 0x0000001F
; CHECK-NEXT: Offset: 0x00000026
; CHECK-NEXT: Functions:
; CHECK-NEXT: - Index: 2
; CHECK-NEXT: Locals:
; CHECK-NEXT: Body: 4180808080001080808080000B
; CHECK-NEXT: - Index: 3
; CHECK-NEXT: Locals:
; CHECK-NEXT: Body: 1081808080001A41010B
; CHECK-NEXT: - Index: 4
; CHECK-NEXT: Body: 4180808080001080808080000B
; CHECK-NEXT: - Index: 4
; CHECK-NEXT: Locals:
; CHECK-NEXT: Body: 4194808080000B
; CHECK-NEXT: Body: 1081808080001A1082808080001A41010B
; CHECK-NEXT: - Index: 5
; CHECK-NEXT: Locals:
; CHECK-NEXT: Body: 419C808080000B
; CHECK-NEXT: - Type: DATA
; CHECK-NEXT: Relocations:
; CHECK-NEXT: - Type: R_WEBASSEMBLY_TABLE_INDEX_I32
@ -163,9 +182,12 @@ entry:
; CHECK-NEXT: - Type: R_WEBASSEMBLY_TABLE_INDEX_I32
; CHECK-NEXT: Index: 1
; CHECK-NEXT: Offset: 0x0000001B
; CHECK-NEXT: - Type: R_WEBASSEMBLY_TABLE_INDEX_I32
; CHECK-NEXT: Index: 2
; CHECK-NEXT: Offset: 0x00000024
; CHECK-NEXT: - Type: R_WEBASSEMBLY_MEMORY_ADDR_I32
; CHECK-NEXT: Index: 0
; CHECK-NEXT: Offset: 0x00000024
; CHECK-NEXT: Offset: 0x0000002D
; CHECK-NEXT: Segments:
; CHECK-NEXT: - SectionOffset: 6
; CHECK-NEXT: MemoryIndex: 0
@ -190,16 +212,22 @@ entry:
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 16
; CHECK-NEXT: Content: '00000000'
; CHECK-NEXT: Content: '02000000'
; CHECK-NEXT: - SectionOffset: 45
; CHECK-NEXT: MemoryIndex: 0
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 20
; CHECK-NEXT: Value: 24
; CHECK-NEXT: Content: '00000000'
; CHECK-NEXT: - SectionOffset: 54
; CHECK-NEXT: MemoryIndex: 0
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 28
; CHECK-NEXT: Content: '616263'
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: linking
; CHECK-NEXT: DataSize: 23
; CHECK-NEXT: DataSize: 31
; CHECK-NEXT: SymbolInfo:
; CHECK-NEXT: - Name: hello
; CHECK-NEXT: Flags: [ VISIBILITY_HIDDEN ]
@ -213,6 +241,8 @@ entry:
; CHECK-NEXT: Flags: [ VISIBILITY_HIDDEN ]
; CHECK-NEXT: - Name: func_addr2
; CHECK-NEXT: Flags: [ VISIBILITY_HIDDEN ]
; CHECK-NEXT: - Name: func_addr3
; CHECK-NEXT: Flags: [ VISIBILITY_HIDDEN ]
; CHECK-NEXT: - Name: data_addr1
; CHECK-NEXT: Flags: [ VISIBILITY_HIDDEN ]
; CHECK-NEXT: SegmentInfo:
@ -229,10 +259,14 @@ entry:
; CHECK-NEXT: Alignment: 4
; CHECK-NEXT: Flags: [ ]
; CHECK-NEXT: - Index: 3
; CHECK-NEXT: Name: .data.func_addr3
; CHECK-NEXT: Alignment: 4
; CHECK-NEXT: Flags: [ ]
; CHECK-NEXT: - Index: 4
; CHECK-NEXT: Name: .data.data_addr1
; CHECK-NEXT: Alignment: 8
; CHECK-NEXT: Flags: [ ]
; CHECK-NEXT: - Index: 4
; CHECK-NEXT: - Index: 5
; CHECK-NEXT: Name: .rodata.data_comdat
; CHECK-NEXT: Alignment: 1
; CHECK-NEXT: Flags: [ ]
@ -240,9 +274,9 @@ entry:
; CHECK-NEXT: - Name: func_comdat
; CHECK-NEXT: Entries:
; CHECK-NEXT: - Kind: FUNCTION
; CHECK-NEXT: Index: 4
; CHECK-NEXT: Index: 5
; CHECK-NEXT: - Kind: DATA
; CHECK-NEXT: Index: 4
; CHECK-NEXT: Index: 5
; CHECK-NEXT: - Type: CUSTOM
; CHECK-NEXT: Name: name
; CHECK-NEXT: FunctionNames:
@ -251,9 +285,11 @@ entry:
; CHECK-NEXT: - Index: 1
; CHECK-NEXT: Name: foo_import
; CHECK-NEXT: - Index: 2
; CHECK-NEXT: Name: hello
; CHECK-NEXT: Name: bar_import
; CHECK-NEXT: - Index: 3
; CHECK-NEXT: Name: my_func
; CHECK-NEXT: Name: hello
; CHECK-NEXT: - Index: 4
; CHECK-NEXT: Name: my_func
; CHECK-NEXT: - Index: 5
; CHECK-NEXT: Name: func_comdat
; CHECK-NEXT: ...

View File

@ -596,7 +596,7 @@ void Writer::createSections() {
void Writer::calculateImports() {
for (Symbol *Sym : Symtab->getSymbols()) {
if (!Sym->isUndefined() || Sym->isWeak())
if (!Sym->isUndefined() || (Sym->isWeak() && !Config->EmitRelocs))
continue;
if (Sym->isFunction()) {