xref: /llvm-project/lld/test/wasm/relocatable.ll (revision f01fdd17b4d202727023da23bdd120eb0bd94f9f)
1; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/hello.s -o %t.hello.o
2; RUN: llc -filetype=obj %s -o %t.o
3; RUN: wasm-ld -r -o %t2.o %t.hello.o %t.o
4; RUN: obj2yaml %t2.o | FileCheck %s
5
6; Verify the resulting object can be used as linker input
7; RUN: wasm-ld --allow-undefined -o %t.wasm %t2.o --export-table
8
9target triple = "wasm32-unknown-unknown"
10
11; Function Attrs: nounwind
12define hidden i32 @my_func() local_unnamed_addr {
13entry:
14  %call = tail call i32 @foo_import()
15  %call2 = tail call i32 @bar_import()
16  ret i32 1
17}
18
19declare i32 @foo_import() local_unnamed_addr
20declare extern_weak i32 @bar_import() local_unnamed_addr
21@data_import = external global i64
22
23@func_addr1 = hidden global ptr @my_func, align 4
24@func_addr2 = hidden global ptr @foo_import, align 4
25@func_addr3 = hidden global ptr @bar_import, align 4
26@data_addr1 = hidden global ptr @data_import, align 8
27
28$func_comdat = comdat any
29@data_comdat = weak_odr constant [3 x i8] c"abc", comdat($func_comdat)
30define linkonce_odr i32 @func_comdat() comdat {
31entry:
32  ret i32 ptrtoint (ptr @data_comdat to i32)
33}
34
35; Test that __attribute__(used) (i.e NO_STRIP) is preserved in the relocated symbol table
36@llvm.used = appending global [1 x ptr] [ptr @my_func], section "llvm.metadata"
37
38define void @_start() {
39  ret void
40}
41
42
43; CHECK:      --- !WASM
44; CHECK-NEXT: FileHeader:
45; CHECK-NEXT:   Version:         0x1
46; CHECK-NEXT: Sections:
47; CHECK-NEXT:   - Type:            TYPE
48; CHECK-NEXT:     Signatures:
49; CHECK-NEXT:       - Index:           0
50; CHECK-NEXT:         ParamTypes:
51; CHECK-NEXT:           - I32
52; CHECK-NEXT:         ReturnTypes:     []
53; CHECK-NEXT:       - Index:           1
54; CHECK-NEXT:         ParamTypes:
55; CHECK-NEXT:         ReturnTypes:
56; CHECK-NEXT:           - I32
57; CHECK-NEXT:       - Index:           2
58; CHECK-NEXT:         ParamTypes:
59; CHECK-NEXT:         ReturnTypes:     []
60; CHECK-NEXT:   - Type:            IMPORT
61; CHECK-NEXT:     Imports:
62; CHECK-NEXT:       - Module:          env
63; CHECK-NEXT:         Field:           __indirect_function_table
64; CHECK-NEXT:         Kind:            TABLE
65; CHECK-NEXT:         Table:
66; CHECK-NEXT:           Index:           0
67; CHECK-NEXT:           ElemType:        FUNCREF
68; CHECK-NEXT:           Limits:
69; CHECK-NEXT:             Minimum:         0x4
70; CHECK-NEXT:       - Module:          env
71; CHECK-NEXT:         Field:           puts
72; CHECK-NEXT:         Kind:            FUNCTION
73; CHECK-NEXT:         SigIndex:        0
74; CHECK-NEXT:       - Module:          env
75; CHECK-NEXT:         Field:           foo_import
76; CHECK-NEXT:         Kind:            FUNCTION
77; CHECK-NEXT:         SigIndex:        1
78; CHECK-NEXT:       - Module:          env
79; CHECK-NEXT:         Field:           bar_import
80; CHECK-NEXT:         Kind:            FUNCTION
81; CHECK-NEXT:         SigIndex:        1
82; CHECK-NEXT:   - Type:            FUNCTION
83; CHECK-NEXT:     FunctionTypes:   [ 2, 1, 1, 2 ]
84; CHECK-NEXT:   - Type:            MEMORY
85; CHECK-NEXT:     Memories:
86; CHECK-NEXT:      - Minimum:         0x1
87; CHECK-NEXT:   - Type:            ELEM
88; CHECK-NEXT:     Segments:
89; CHECK-NEXT:       - Offset:
90; CHECK-NEXT:           Opcode:          I32_CONST
91; CHECK-NEXT:           Value:           1
92; CHECK-NEXT:         Functions:       [ 4, 1, 2 ]
93; SHARED-NEXT:  - Type:            DATACOUNT
94; SHARED-NEXT:    Count:           6
95; CHECK-NEXT:   - Type:            CODE
96; CHECK-NEXT:     Relocations:
97; CHECK-NEXT:       - Type:            R_WASM_MEMORY_ADDR_SLEB
98; CHECK-NEXT:         Index:           1
99; CHECK-NEXT:         Offset:          0x4
100; CHECK-NEXT:       - Type:            R_WASM_FUNCTION_INDEX_LEB
101; CHECK-NEXT:         Index:           2
102; CHECK-NEXT:         Offset:          0xA
103; CHECK-NEXT:       - Type:            R_WASM_FUNCTION_INDEX_LEB
104; CHECK-NEXT:         Index:           4
105; CHECK-NEXT:         Offset:          0x13
106; CHECK-NEXT:       - Type:            R_WASM_FUNCTION_INDEX_LEB
107; CHECK-NEXT:         Index:           5
108; CHECK-NEXT:         Offset:          0x1A
109; CHECK-NEXT:       - Type:            R_WASM_MEMORY_ADDR_SLEB
110; CHECK-NEXT:         Index:           7
111; CHECK-NEXT:         Offset:          0x26
112; CHECK-NEXT:     Functions:
113; CHECK-NEXT:       - Index:         3
114; CHECK-NEXT:         Locals:
115; CHECK-NEXT:         Body:          4180808080001080808080000B
116; CHECK-NEXT:       - Index:         4
117; CHECK-NEXT:         Locals:
118; CHECK-NEXT:         Body:          1081808080001A1082808080001A41010B
119; CHECK-NEXT:       - Index:         5
120; CHECK-NEXT:         Locals:
121; CHECK-NEXT:         Body:          4187808080000B
122; NORMAL-NEXT:  - Type:            DATA
123; NORMAL-NEXT:    Relocations:
124; NORMAL-NEXT:      - Type:            R_WASM_TABLE_INDEX_I32
125; NORMAL-NEXT:        Index:           3
126; NORMAL-NEXT:        Offset:          0x1A
127; NORMAL-NEXT:      - Type:            R_WASM_TABLE_INDEX_I32
128; NORMAL-NEXT:        Index:           4
129; NORMAL-NEXT:        Offset:          0x23
130; NORMAL-NEXT:      - Type:            R_WASM_TABLE_INDEX_I32
131; NORMAL-NEXT:        Index:           5
132; NORMAL-NEXT:        Offset:          0x2C
133; NORMAL-NEXT:      - Type:            R_WASM_MEMORY_ADDR_I32
134; NORMAL-NEXT:        Index:           12
135; NORMAL-NEXT:        Offset:          0x35
136; NORMAL-NEXT:    Segments:
137; NORMAL-NEXT:      - SectionOffset:   6
138; NORMAL-NEXT:        InitFlags:       0
139; NORMAL-NEXT:        Offset:
140; NORMAL-NEXT:          Opcode:          I32_CONST
141; NORMAL-NEXT:          Value:           0
142; NORMAL-NEXT:        Content:         68656C6C6F0A00
143; NORMAL-NEXT:      - SectionOffset:   18
144; NORMAL-NEXT:        InitFlags:       0
145; NORMAL-NEXT:        Offset:
146; NORMAL-NEXT:          Opcode:          I32_CONST
147; NORMAL-NEXT:          Value:           7
148; NORMAL-NEXT:        Content:         '616263'
149; NORMAL-NEXT:      - SectionOffset:   26
150; NORMAL-NEXT:        InitFlags:       0
151; NORMAL-NEXT:        Offset:
152; NORMAL-NEXT:          Opcode:          I32_CONST
153; NORMAL-NEXT:          Value:           12
154; NORMAL-NEXT:        Content:         '01000000'
155; NORMAL-NEXT:      - SectionOffset:   35
156; NORMAL-NEXT:        InitFlags:       0
157; NORMAL-NEXT:        Offset:
158; NORMAL-NEXT:          Opcode:          I32_CONST
159; NORMAL-NEXT:          Value:           16
160; NORMAL-NEXT:        Content:         '02000000'
161; NORMAL-NEXT:      - SectionOffset:   44
162; NORMAL-NEXT:        InitFlags:       0
163; NORMAL-NEXT:        Offset:
164; NORMAL-NEXT:          Opcode:          I32_CONST
165; NORMAL-NEXT:          Value:           20
166; NORMAL-NEXT:        Content:         '03000000'
167; NORMAL-NEXT:      - SectionOffset:   53
168; NORMAL-NEXT:        InitFlags:       0
169; NORMAL-NEXT:        Offset:
170; NORMAL-NEXT:          Opcode:          I32_CONST
171; NORMAL-NEXT:          Value:           24
172; NORMAL-NEXT:        Content:         '00000000'
173; NORMAL-NEXT:  - Type:            CUSTOM
174; NORMAL-NEXT:    Name:            linking
175; NORMAL-NEXT:    Version:         2
176; NORMAL-NEXT:    SymbolTable:
177; NORMAL-NEXT:      - Index:           0
178; NORMAL-NEXT:        Kind:            FUNCTION
179; NORMAL-NEXT:        Name:            hello
180; NORMAL-NEXT:        Flags:           [ VISIBILITY_HIDDEN ]
181; NORMAL-NEXT:        Function:        3
182; NORMAL-NEXT:      - Index:           1
183; NORMAL-NEXT:        Kind:            DATA
184; NORMAL-NEXT:        Name:            hello_str
185; NORMAL-NEXT:        Flags:           [  ]
186; NORMAL-NEXT:        Segment:         0
187; NORMAL-NEXT:        Size:            7
188; NORMAL-NEXT:      - Index:           2
189; NORMAL-NEXT:        Kind:            FUNCTION
190; NORMAL-NEXT:        Name:            puts
191; NORMAL-NEXT:        Flags:           [ UNDEFINED ]
192; NORMAL-NEXT:        Function:        0
193; NORMAL-NEXT:      - Index:           3
194; NORMAL-NEXT:        Kind:            FUNCTION
195; NORMAL-NEXT:        Name:            my_func
196; NORMAL-NEXT:        Flags:           [ VISIBILITY_HIDDEN, NO_STRIP ]
197; NORMAL-NEXT:        Function:        4
198; NORMAL-NEXT:      - Index:           4
199; NORMAL-NEXT:        Kind:            FUNCTION
200; NORMAL-NEXT:        Name:            foo_import
201; NORMAL-NEXT:        Flags:           [ UNDEFINED ]
202; NORMAL-NEXT:        Function:        1
203; NORMAL-NEXT:      - Index:           5
204; NORMAL-NEXT:        Kind:            FUNCTION
205; NORMAL-NEXT:        Name:            bar_import
206; NORMAL-NEXT:        Flags:           [ BINDING_WEAK, UNDEFINED ]
207; NORMAL-NEXT:        Function:        2
208; NORMAL-NEXT:      - Index:           6
209; NORMAL-NEXT:        Kind:            FUNCTION
210; NORMAL-NEXT:        Name:            func_comdat
211; NORMAL-NEXT:        Flags:           [ BINDING_WEAK ]
212; NORMAL-NEXT:        Function:        5
213; NORMAL-NEXT:      - Index:           7
214; NORMAL-NEXT:        Kind:            DATA
215; NORMAL-NEXT:        Name:            data_comdat
216; NORMAL-NEXT:        Flags:           [ BINDING_WEAK ]
217; NORMAL-NEXT:        Segment:         1
218; NORMAL-NEXT:        Size:            3
219; NORMAL-NEXT:      - Index:           8
220; NORMAL-NEXT:        Kind:            DATA
221; NORMAL-NEXT:        Name:            func_addr1
222; NORMAL-NEXT:        Flags:           [ VISIBILITY_HIDDEN ]
223; NORMAL-NEXT:        Segment:         2
224; NORMAL-NEXT:        Size:            4
225; NORMAL-NEXT:      - Index:           9
226; NORMAL-NEXT:        Kind:            DATA
227; NORMAL-NEXT:        Name:            func_addr2
228; NORMAL-NEXT:        Flags:           [ VISIBILITY_HIDDEN ]
229; NORMAL-NEXT:        Segment:         3
230; NORMAL-NEXT:        Size:            4
231; NORMAL-NEXT:      - Index:           10
232; NORMAL-NEXT:        Kind:            DATA
233; NORMAL-NEXT:        Name:            func_addr3
234; NORMAL-NEXT:        Flags:           [ VISIBILITY_HIDDEN ]
235; NORMAL-NEXT:        Segment:         4
236; NORMAL-NEXT:        Size:            4
237; NORMAL-NEXT:      - Index:           11
238; NORMAL-NEXT:        Kind:            DATA
239; NORMAL-NEXT:        Name:            data_addr1
240; NORMAL-NEXT:        Flags:           [ VISIBILITY_HIDDEN ]
241; NORMAL-NEXT:        Segment:         5
242; NORMAL-NEXT:        Size:            4
243; NORMAL-NEXT:      - Index:           12
244; NORMAL-NEXT:        Kind:            DATA
245; NORMAL-NEXT:        Name:            data_import
246; NORMAL-NEXT:        Flags:           [ UNDEFINED ]
247; NORMAL-NEXT:    SegmentInfo:
248; NORMAL-NEXT:      - Index:           0
249; NORMAL-NEXT:        Name:            .rodata.hello_str
250; NORMAL-NEXT:        Alignment:       0
251; NORMAL-NEXT:        Flags:           [  ]
252; NORMAL-NEXT:      - Index:           1
253; NORMAL-NEXT:        Name:            .rodata.data_comdat
254; NORMAL-NEXT:        Alignment:       0
255; NORMAL-NEXT:        Flags:           [  ]
256; NORMAL-NEXT:      - Index:           2
257; NORMAL-NEXT:        Name:            .data.func_addr1
258; NORMAL-NEXT:        Alignment:       2
259; NORMAL-NEXT:        Flags:           [  ]
260; NORMAL-NEXT:      - Index:           3
261; NORMAL-NEXT:        Name:            .data.func_addr2
262; NORMAL-NEXT:        Alignment:       2
263; NORMAL-NEXT:        Flags:           [  ]
264; NORMAL-NEXT:      - Index:           4
265; NORMAL-NEXT:        Name:            .data.func_addr3
266; NORMAL-NEXT:        Alignment:       2
267; NORMAL-NEXT:        Flags:           [  ]
268; NORMAL-NEXT:      - Index:           5
269; NORMAL-NEXT:        Name:            .data.data_addr1
270; NORMAL-NEXT:        Alignment:       3
271; NORMAL-NEXT:        Flags:           [  ]
272; NORMAL-NEXT:    Comdats:
273; NORMAL-NEXT:      - Name:            func_comdat
274; NORMAL-NEXT:        Entries:
275; NORMAL-NEXT:          - Kind:            FUNCTION
276; NORMAL-NEXT:            Index:           5
277; NORMAL-NEXT:          - Kind:            DATA
278; NORMAL-NEXT:            Index:           1
279; NORMAL-NEXT:  - Type:            CUSTOM
280; NORMAL-NEXT:    Name:            name
281; NORMAL-NEXT:    FunctionNames:
282; NORMAL-NEXT:      - Index:           0
283; NORMAL-NEXT:        Name:            puts
284; NORMAL-NEXT:      - Index:           1
285; NORMAL-NEXT:        Name:            foo_import
286; NORMAL-NEXT:      - Index:           2
287; NORMAL-NEXT:        Name:            bar_import
288; NORMAL-NEXT:      - Index:           3
289; NORMAL-NEXT:        Name:            hello
290; NORMAL-NEXT:      - Index:           4
291; NORMAL-NEXT:        Name:            my_func
292; NORMAL-NEXT:      - Index:           5
293; NORMAL-NEXT:        Name:            func_comdat
294; NORMAL-NEXT:...
295