1# Checks handling of undefined weak external functions.  When the
2# static linker decides they are undefined, check GOT relocations
3# resolve to zero (i.e. a global that contains zero.).
4#
5# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
6# RUN: wasm-ld %t.o -o %t1.wasm
7# RUN: obj2yaml %t1.wasm | FileCheck %s
8#
9# With `--unresolved-symbols=ignore-all` the behaviour should be the same
10# as the default.>
11#
12# RUN: wasm-ld --unresolved-symbols=ignore-all %t.o -o %t2.wasm
13# RUN: obj2yaml %t2.wasm | FileCheck %s
14
15.globl get_foo_addr
16get_foo_addr:
17  .functype get_foo_addr () -> (i32)
18  global.get foo@GOT
19  end_function
20
21.globl _start
22_start:
23  .functype _start () -> ()
24  call get_foo_addr
25  end_function
26
27.weak foo
28.functype foo () -> (i32)
29
30# Verify that we do not generate dynamic relocations for the GOT entry.
31
32# CHECK-NOT: __wasm_apply_relocs
33
34# Verify that we do not generate an import for foo
35
36# CHECK-NOT:  - Type:            IMPORT
37
38#      CHECK:   - Type:            GLOBAL
39# CHECK-NEXT:     Globals:
40# CHECK-NEXT:       - Index:           0
41# CHECK-NEXT:         Type:            I32
42# CHECK-NEXT:         Mutable:         true
43# CHECK-NEXT:         InitExpr:
44# CHECK-NEXT:           Opcode:          I32_CONST
45# CHECK-NEXT:           Value:           66560
46# Global 'undefined_weak:foo' representing the GOT entry for foo
47# Unlike other internal GOT entries that need to be mutable this one
48# is immutable and not updated by `__wasm_apply_relocs`
49# CHECK-NEXT:       - Index:           1
50# CHECK-NEXT:         Type:            I32
51# CHECK-NEXT:         Mutable:         false
52# CHECK-NEXT:         InitExpr:
53# CHECK-NEXT:           Opcode:          I32_CONST
54# CHECK-NEXT:           Value:           0
55
56#      CHECK:  - Type:            CUSTOM
57# CHECK-NEXT:    Name:            name
58# CHECK-NEXT:    FunctionNames:
59# CHECK-NEXT:      - Index:           0
60# CHECK-NEXT:        Name:            'undefined_weak:foo'
61# CHECK-NEXT:      - Index:           1
62# CHECK-NEXT:        Name:            get_foo_addr
63# CHECK-NEXT:      - Index:           2
64# CHECK-NEXT:        Name:            _start
65# CHECK-NEXT:    GlobalNames:
66# CHECK-NEXT:      - Index:           0
67# CHECK-NEXT:        Name:            __stack_pointer
68# CHECK-NEXT:      - Index:           1
69# CHECK-NEXT:        Name:            'undefined_weak:foo'
70
71# With `-pie` or `-shared` the resolution should be deferred to the dynamic
72# linker and the function address should be imported as GOT.func.foo.
73#
74# RUN: wasm-ld --experimental-pic -pie %t.o -o %t3.wasm
75# RUN: obj2yaml %t3.wasm | FileCheck %s --check-prefix=IMPORT
76
77#      IMPORT:  - Type:            IMPORT
78#      IMPORT:      - Module:          GOT.func
79# IMPORT-NEXT:        Field:           foo
80# IMPORT-NEXT:        Kind:            GLOBAL
81# IMPORT-NEXT:        GlobalType:      I32
82# IMPORT-NEXT:        GlobalMutable:   true
83
84#      IMPORT:     GlobalNames:
85# IMPORT-NEXT:       - Index:           0
86# IMPORT-NEXT:         Name:            __memory_base
87# IMPORT-NEXT:       - Index:           1
88# IMPORT-NEXT:         Name:            __table_base
89# IMPORT-NEXT:       - Index:           2
90# IMPORT-NEXT:         Name:            foo
91