xref: /llvm-project/lld/test/wasm/weak-undefined-pic.s (revision 9647a6f719ee4b1c23eea7aef368c1dcd5317949)
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.functype foo () -> (i32)
16
17.globl get_foo_addr
18get_foo_addr:
19  .functype get_foo_addr () -> (i32)
20  global.get foo@GOT
21  end_function
22
23.globl _start
24_start:
25  .functype _start () -> (i32)
26  call get_foo_addr
27  call foo
28  drop
29  end_function
30
31.weak foo
32
33# Verify that we do not generate dynamic relocations for the GOT entry.
34
35# CHECK-NOT: __wasm_apply_global_relocs
36
37# Verify that we do not generate an import for foo
38
39# CHECK-NOT:  - Type:            IMPORT
40
41#      CHECK:   - Type:            GLOBAL
42# CHECK-NEXT:     Globals:
43# CHECK-NEXT:       - Index:           0
44# CHECK-NEXT:         Type:            I32
45# CHECK-NEXT:         Mutable:         true
46# CHECK-NEXT:         InitExpr:
47# CHECK-NEXT:           Opcode:          I32_CONST
48# CHECK-NEXT:           Value:           66560
49# Global 'undefined_weak:foo' representing the GOT entry for foo
50# Unlike other internal GOT entries that need to be mutable this one
51# is immutable and not updated by `__wasm_apply_global_relocs`
52# CHECK-NEXT:       - Index:           1
53# CHECK-NEXT:         Type:            I32
54# CHECK-NEXT:         Mutable:         false
55# CHECK-NEXT:         InitExpr:
56# CHECK-NEXT:           Opcode:          I32_CONST
57# CHECK-NEXT:           Value:           0
58
59#      CHECK:  - Type:            CUSTOM
60# CHECK-NEXT:    Name:            name
61# CHECK-NEXT:    FunctionNames:
62# CHECK-NEXT:      - Index:           0
63# CHECK-NEXT:        Name:            'undefined_weak:foo'
64# CHECK-NEXT:      - Index:           1
65# CHECK-NEXT:        Name:            get_foo_addr
66# CHECK-NEXT:      - Index:           2
67# CHECK-NEXT:        Name:            _start
68# CHECK-NEXT:    GlobalNames:
69# CHECK-NEXT:      - Index:           0
70# CHECK-NEXT:        Name:            __stack_pointer
71# CHECK-NEXT:      - Index:           1
72# CHECK-NEXT:        Name:            'GOT.func.internal.undefined_weak:foo'
73
74# With `-pie` or `-shared` the resolution should be deferred to the dynamic
75# linker and the function address should be imported as GOT.func.foo.
76#
77# RUN: wasm-ld --experimental-pic -pie %t.o -o %t3.wasm
78# RUN: obj2yaml %t3.wasm | FileCheck %s --check-prefix=IMPORT
79
80#      IMPORT:  - Type:            IMPORT
81#      IMPORT:        Field:           foo
82# IMPORT-NEXT:        Kind:            FUNCTION
83# IMPORT-NEXT:        SigIndex:        0
84# IMPORT-NEXT:      - Module:          GOT.func
85# IMPORT-NEXT:        Field:           foo
86# IMPORT-NEXT:        Kind:            GLOBAL
87# IMPORT-NEXT:        GlobalType:      I32
88# IMPORT-NEXT:        GlobalMutable:   true
89
90#      IMPORT:     GlobalNames:
91# IMPORT-NEXT:       - Index:           0
92# IMPORT-NEXT:         Name:            __memory_base
93# IMPORT-NEXT:       - Index:           1
94# IMPORT-NEXT:         Name:            __table_base
95# IMPORT-NEXT:       - Index:           2
96# IMPORT-NEXT:         Name:            foo
97