[lld][WebAssembly] Perform data relocations during start function
[llvm-project.git] / lld / test / wasm / data-segments.ll
blobbe526de9d2131251934e22c11019e7d86d5c3003
1 ; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.o -mattr=+atomics
2 ; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.bulk-mem.o -mattr=+bulk-memory
3 ; RUN: llc --mtriple=wasm64-unknown-unknown -filetype=obj %s -o %t.bulk-mem64.o -mattr=+bulk-memory
4 ; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.bulk-mem.o -mattr=+atomics,+bulk-memory
5 ; RUN: llc --mtriple=wasm64-unknown-unknown -filetype=obj %s -o %t.atomics.bulk-mem64.o -mattr=+atomics,+bulk-memory
6 ; RUN: llc --mtriple=wasm32-unknown-unknown -filetype=obj %s -o %t.atomics.bulk-mem.pic.o -relocation-model=pic -mattr=+atomics,+bulk-memory,+mutable-globals
7 ; RUN: llc --mtriple=wasm64-unknown-unknown -filetype=obj %s -o %t.atomics.bulk-mem.pic-mem64.o -relocation-model=pic -mattr=+atomics,+bulk-memory,+mutable-globals
9 ; atomics, shared memory => error
10 ; RUN: not wasm-ld -no-gc-sections --no-entry --shared-memory --max-memory=131072 %t.atomics.o -o %t.atomics.wasm 2>&1 | FileCheck %s --check-prefix ERROR
12 ; bulk memory, unshared memory => active segments
13 ; RUN: wasm-ld -no-gc-sections --no-entry %t.bulk-mem.o -o %t.bulk-mem.wasm
14 ; RUN: obj2yaml %t.bulk-mem.wasm | FileCheck %s --check-prefixes ACTIVE,ACTIVE32
16 ; bulk memory, unshared memory, wasm64 => active segments
17 ; RUN: wasm-ld -mwasm64 -no-gc-sections --no-entry %t.bulk-mem64.o -o %t.bulk-mem64.wasm
18 ; RUN: obj2yaml %t.bulk-mem64.wasm | FileCheck %s --check-prefixes ACTIVE,ACTIVE64
20 ; atomics, bulk memory, shared memory => passive segments
21 ; RUN: wasm-ld -no-gc-sections --no-entry --shared-memory --max-memory=131072 %t.atomics.bulk-mem.o -o %t.atomics.bulk-mem.wasm
22 ; RUN: obj2yaml %t.atomics.bulk-mem.wasm | FileCheck %s --check-prefix PASSIVE
23 ; RUN: llvm-objdump --disassemble-symbols=__wasm_init_memory --no-show-raw-insn --no-leading-addr %t.atomics.bulk-mem.wasm | FileCheck %s --check-prefixes DIS,NOPIC-DIS -DPTR=i32
25 ; atomics, bulk memory, shared memory, wasm64 => passive segments
26 ; RUN: wasm-ld -mwasm64 -no-gc-sections --no-entry --shared-memory --max-memory=131072 %t.atomics.bulk-mem64.o -o %t.atomics.bulk-mem64.wasm
27 ; RUN: obj2yaml %t.atomics.bulk-mem64.wasm | FileCheck %s --check-prefix PASSIVE
28 ; RUN: llvm-objdump --disassemble-symbols=__wasm_init_memory --no-show-raw-insn --no-leading-addr %t.atomics.bulk-mem64.wasm | FileCheck %s --check-prefixes DIS,NOPIC-DIS -DPTR=i64
30 ; Also test in combination with PIC/pie
31 ; RUN: wasm-ld --experimental-pic -pie -no-gc-sections --no-entry --shared-memory --max-memory=131072 %t.atomics.bulk-mem.pic.o -o %t.pic.wasm
32 ; RUN: obj2yaml %t.pic.wasm | FileCheck %s --check-prefixes PASSIVE-PIC,PASSIVE32-PIC
33 ; RUN: llvm-objdump --disassemble-symbols=__wasm_init_memory --no-show-raw-insn --no-leading-addr %t.pic.wasm | FileCheck %s --check-prefixes DIS,PIC-DIS -DPTR=i32
35 ; Also test in combination with PIC/pie + wasm64
36 ; RUN: wasm-ld -mwasm64 --experimental-pic -pie -no-gc-sections --no-entry --shared-memory --max-memory=131072 %t.atomics.bulk-mem.pic-mem64.o -o %t.pic-mem64.wasm
37 ; RUN: obj2yaml %t.pic-mem64.wasm | FileCheck %s --check-prefixes PASSIVE-PIC,PASSIVE64-PIC
38 ; RUN: llvm-objdump --disassemble-symbols=__wasm_init_memory --no-show-raw-insn --no-leading-addr %t.pic-mem64.wasm | FileCheck %s --check-prefixes DIS,PIC-DIS -DPTR=i64
40 @a = hidden global [6 x i8] c"hello\00", align 1
41 @b = hidden global [8 x i8] c"goodbye\00", align 1
42 @c = hidden global [10000 x i8] zeroinitializer, align 1
43 @d = hidden global i32 42, align 4
45 @e = private constant [9 x i8] c"constant\00", align 1
46 @f = private constant i8 43, align 4
48 ; ERROR: 'bulk-memory' feature must be used in order to use shared memory
50 ; ACTIVE-LABEL: - Type:            CODE
51 ; ACTIVE-NEXT:    Functions:
52 ; ACTIVE-NEXT:      - Index:           0
53 ; ACTIVE-NEXT:        Locals:          []
54 ; ACTIVE-NEXT:        Body:            0B
55 ; ACTIVE-NEXT:  - Type:            DATA
56 ; ACTIVE-NEXT:    Segments:
57 ; ACTIVE-NEXT:      - SectionOffset:   7
58 ; ACTIVE-NEXT:        InitFlags:       0
59 ; ACTIVE-NEXT:        Offset:
60 ; ACTIVE32-NEXT:        Opcode:          I32_CONST
61 ; ACTIVE64-NEXT:        Opcode:          I64_CONST
62 ; ACTIVE-NEXT:          Value:           1024
63 ; ACTIVE-NEXT:        Content:         636F6E7374616E74000000002B
64 ; ACTIVE-NEXT:      - SectionOffset:   26
65 ; ACTIVE-NEXT:        InitFlags:       0
66 ; ACTIVE-NEXT:        Offset:
67 ; ACTIVE32-NEXT:        Opcode:          I32_CONST
68 ; ACTIVE64-NEXT:        Opcode:          I64_CONST
69 ; ACTIVE-NEXT:          Value:           1040
70 ; ACTIVE-NEXT:        Content:         68656C6C6F00676F6F646279650000002A000000
71 ; ACTIVE-NEXT:  - Type:            CUSTOM
72 ; ACTIVE-NEXT:    Name:            name
73 ; ACTIVE-NEXT:    FunctionNames:
74 ; ACTIVE-NEXT:      - Index:           0
75 ; ACTIVE-NEXT:        Name:            __wasm_call_ctors
77 ; PASSIVE-LABEL: - Type:            START
78 ; PASSIVE-NEXT:    StartFunction:   2
79 ; PASSIVE-LABEL: - Type:            DATACOUNT
80 ; PASSIVE-NEXT:    Count:           2
81 ; PASSIVE-LABEL: - Type:            CODE
82 ; PASSIVE-NEXT:    Functions:
83 ; PASSIVE-NEXT:      - Index:           0
84 ; PASSIVE-NEXT:        Locals:          []
85 ; PASSIVE-NEXT:        Body:            0B
86 ; PASSIVE-NEXT:      - Index:           1
87 ; PASSIVE-NEXT:        Locals:          []
88 ; PASSIVE-NEXT:        Body:            0B
89 ; PASSIVE-NEXT:      - Index:           2
90 ; PASSIVE-NEXT:        Locals:          []
91 ; PASSIVE-NEXT:        Body:            {{.*}}
92 ; PASSIVE-NEXT:  - Type:            DATA
93 ; PASSIVE-NEXT:    Segments:
94 ; PASSIVE-NEXT:      - SectionOffset:   3
95 ; PASSIVE-NEXT:        InitFlags:       1
96 ; PASSIVE-NEXT:        Content:         636F6E7374616E74000000002B
97 ; PASSIVE-NEXT:      - SectionOffset:   18
98 ; PASSIVE-NEXT:        InitFlags:       1
99 ; PASSIVE-NEXT:        Content:         68656C6C6F00676F6F646279650000002A000000
100 ; PASSIVE-NEXT:  - Type:            CUSTOM
101 ; PASSIVE-NEXT:    Name:            name
102 ; PASSIVE-NEXT:    FunctionNames:
103 ; PASSIVE-NEXT:      - Index:           0
104 ; PASSIVE-NEXT:        Name:            __wasm_call_ctors
105 ; PASSIVE-NEXT:      - Index:           1
106 ; PASSIVE-NEXT:        Name:            __wasm_init_tls
107 ; PASSIVE-NEXT:      - Index:           2
108 ; PASSIVE-NEXT:        Name:            __wasm_init_memory
110 ;      PASSIVE-PIC:  - Type:            START
111 ; PASSIVE-PIC-NEXT:    StartFunction:   2
112 ; PASSIVE-PIC-NEXT:  - Type:            DATACOUNT
113 ; PASSIVE-PIC-NEXT:    Count:           2
114 ; PASSIVE-PIC-NEXT:  - Type:            CODE
115 ; PASSIVE-PIC-NEXT:    Functions:
116 ; PASSIVE-PIC-NEXT:      - Index:           0
117 ; PASSIVE-PIC-NEXT:        Locals:          []
118 ; PASSIVE-PIC-NEXT:        Body:            0B
119 ; PASSIVE-PIC-NEXT:      - Index:           1
120 ; PASSIVE-PIC-NEXT:        Locals:          []
121 ; PASSIVE-PIC-NEXT:        Body:            0B
122 ; PASSIVE-PIC-NEXT:      - Index:           2
123 ; PASSIVE-PIC-NEXT:        Locals:
124 ; PASSIVE32-PIC-NEXT:          - Type:            I32
125 ; PASSIVE64-PIC-NEXT:          - Type:            I64
126 ; PASSIVE-PIC-NEXT:              Count:           1
127 ; PASSIVE-PIC-NEXT:        Body:            {{.*}}
128 ; PASSIVE-PIC-NEXT:      - Index:           3
129 ; PASSIVE-PIC-NEXT:        Locals:          []
130 ; PASSIVE-PIC-NEXT:        Body:            0B
131 ; PASSIVE-PIC-NEXT:  - Type:            DATA
132 ; PASSIVE-PIC-NEXT:    Segments:
133 ; PASSIVE-PIC-NEXT:      - SectionOffset:   3
134 ; PASSIVE-PIC-NEXT:        InitFlags:       1
135 ; PASSIVE-PIC-NEXT:        Content:         636F6E7374616E74000000002B
136 ; PASSIVE-PIC-NEXT:      - SectionOffset:   18
137 ; PASSIVE-PIC-NEXT:        InitFlags:       1
138 ; PASSIVE-PIC-NEXT:        Content:         68656C6C6F00676F6F646279650000002A000000
139 ; PASSIVE-PIC-NEXT:  - Type:            CUSTOM
140 ; PASSIVE-PIC-NEXT:    Name:            name
141 ; PASSIVE-PIC-NEXT:    FunctionNames:
142 ; PASSIVE-PIC-NEXT:      - Index:           0
143 ; PASSIVE-PIC-NEXT:        Name:            __wasm_call_ctors
144 ; PASSIVE-PIC-NEXT:      - Index:           1
145 ; PASSIVE-PIC-NEXT:        Name:            __wasm_init_tls
146 ; PASSIVE-PIC-NEXT:      - Index:           2
147 ; PASSIVE-PIC-NEXT:        Name:            __wasm_init_memory
148 ; PASSIVE-PIC-NEXT:      - Index:           3
149 ; PASSIVE-PIC-NEXT:        Name:            __wasm_apply_data_relocs
151 ; DIS-LABEL:       <__wasm_init_memory>:
153 ; PIC-DIS:           .local [[PTR]]
154 ; PIC-DIS-NEXT:      global.get      1
155 ; PIC-DIS-NEXT:      [[PTR]].const   10036
156 ; PIC-DIS-NEXT:      [[PTR]].add
157 ; PIC-DIS-NEXT:      local.set       0
159 ; DIS:               block
160 ; DIS-NEXT:           block
161 ; DIS-NEXT:            block
163 ; NOPIC-DIS-NEXT:       [[PTR]].const   11060
164 ; PIC-DIS-NEXT:         local.get       0
166 ; DIS-NEXT:             i32.const       0
167 ; DIS-NEXT:             i32.const       1
168 ; DIS-NEXT:             i32.atomic.rmw.cmpxchg  0
169 ; DIS-NEXT:             br_table        {0, 1, 2}      # 1:     down to label1
170 ; DIS-NEXT:                                            # 2:     down to label0
171 ; DIS-NEXT:            end
173 ; NOPIC-DIS-NEXT:      [[PTR]].const   1024
174 ; PIC-DIS-NEXT:        [[PTR]].const   0
175 ; PIC-DIS-NEXT:        global.get      1
176 ; PIC-DIS-NEXT:        [[PTR]].add
178 ; DIS-NEXT:            i32.const       0
179 ; DIS-NEXT:            i32.const       13
180 ; DIS-NEXT:            memory.init     0, 0
182 ; NOPIC-DIS-NEXT:      [[PTR]].const   1040
183 ; PIC-DIS-NEXT:        [[PTR]].const   16
184 ; PIC-DIS-NEXT:        global.get      1
185 ; PIC-DIS-NEXT:        [[PTR]].add
187 ; DIS-NEXT:            i32.const       0
188 ; DIS-NEXT:            i32.const       20
189 ; DIS-NEXT:            memory.init     1, 0
190 ; NOPIC-DIS-NEXT:      [[PTR]].const   1060
191 ; PIC-DIS-NEXT:        [[PTR]].const   36
192 ; PIC-DIS-NEXT:        global.get      1
193 ; PIC-DIS-NEXT:        [[PTR]].add
194 ; DIS-NEXT:            i32.const       0
195 ; DIS-NEXT:            i32.const       10000
196 ; DIS-NEXT:            memory.fill     0
198 ; PIC-DIS-NEXT:        call 3
200 ; NOPIC-DIS-NEXT:      [[PTR]].const   11060
201 ; PIC-DIS-NEXT:        local.get       0
203 ; DIS-NEXT:            i32.const       2
204 ; DIS-NEXT:            i32.atomic.store        0
206 ; NOPIC-DIS-NEXT:      [[PTR]].const   11060
207 ; PIC-DIS-NEXT:        local.get       0
209 ; DIS-NEXT:            i32.const       -1
210 ; DIS-NEXT:            memory.atomic.notify    0
211 ; DIS-NEXT:            drop
212 ; DIS-NEXT:            br              1               # 1:     down to label1
213 ; DIS-NEXT:           end
215 ; NOPIC-DIS-NEXT:     [[PTR]].const   11060
216 ; PIC-DIS-NEXT:       local.get       0
218 ; DIS-NEXT:           i32.const       1
219 ; DIS-NEXT:           i64.const       -1
220 ; DIS-NEXT:           memory.atomic.wait32    0
221 ; DIS-NEXT:           drop
222 ; DIS-NEXT:          end
223 ; DIS-NEXT:          data.drop       0
224 ; DIS-NEXT:          data.drop       1
225 ; DIS-NEXT:         end