xref: /llvm-project/lld/test/wasm/unresolved-symbols.s (revision 22b7b84860d39da71964c9b329937f2ee1d875ba)
14d888375SSam Clegg# RUN: split-file %s %t
24d888375SSam Clegg# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/main.s -o %t/main.o
34d888375SSam Clegg# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/secondary.s -o %t/secondary.o
4206884bfSSam Clegg
54d888375SSam Clegg## Check that both main.o and secondary.o contain references to the same
64d888375SSam Clegg## undefined function and that both are correctly reported.
74d888375SSam Clegg# RUN: not wasm-ld --no-gc-sections %t/main.o %t/secondary.o -o /dev/null 2>&1 | \
8206884bfSSam Clegg# RUN:   FileCheck -check-prefix=ERRUND %s
94d888375SSam Clegg# ERRUND: error: {{.*}}main.o: undefined symbol: undef_func
104d888375SSam Clegg# ERRUND: error: {{.*}}secondary.o: undefined symbol: undef_func
11206884bfSSam Clegg
12206884bfSSam Clegg## report-all is the default one. Check that we get the same error
134d888375SSam Clegg# RUN: not wasm-ld --no-gc-sections %t/main.o %t/secondary.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
14206884bfSSam Clegg# RUN:   FileCheck -check-prefix=ERRUND %s
15206884bfSSam Clegg
16206884bfSSam Clegg## Error out if unknown option value was set.
174d888375SSam Clegg# RUN: not wasm-ld %t/main.o -o /dev/null --unresolved-symbols=xxx 2>&1 | \
18206884bfSSam Clegg# RUN:   FileCheck -check-prefix=ERR1 %s
19206884bfSSam Clegg# ERR1: unknown --unresolved-symbols value: xxx
20206884bfSSam Clegg## Check alias.
214d888375SSam Clegg# RUN: not wasm-ld %t/main.o -o /dev/null --unresolved-symbols xxx 2>&1 | \
22206884bfSSam Clegg# RUN:   FileCheck -check-prefix=ERR1 %s
23206884bfSSam Clegg
24206884bfSSam Clegg## Ignore all should not produce error and should not produce
255b0e45c8SSam Clegg## any imports.  It should create a stub function in the place of the missing
265b0e45c8SSam Clegg## function symbol.
274d888375SSam Clegg# RUN: wasm-ld %t/main.o -o %t2.wasm --unresolved-symbols=ignore-all
28206884bfSSam Clegg# RUN: obj2yaml %t2.wasm | FileCheck -check-prefix=IGNORE %s
295b0e45c8SSam Clegg
305b0e45c8SSam Clegg## --warn-unresolved-symbols should behave the same
314d888375SSam Clegg# RUN: wasm-ld %t/main.o -o %t2.wasm --warn-unresolved-symbols
325b0e45c8SSam Clegg# RUN: obj2yaml %t2.wasm | FileCheck -check-prefix=IGNORE %s
335b0e45c8SSam Clegg
34206884bfSSam Clegg# IGNORE-NOT: - Type:            IMPORT
35206884bfSSam Clegg# IGNORE-NOT: - Type:            ELEM
36206884bfSSam Clegg#
37206884bfSSam Clegg#      IGNORE:  - Type:            CODE
38206884bfSSam Clegg# IGNORE-NEXT:    Functions:
39206884bfSSam Clegg# IGNORE-NEXT:      - Index:           0
40206884bfSSam Clegg# IGNORE-NEXT:        Locals:          []
41206884bfSSam Clegg# IGNORE-NEXT:        Body:            000B
42206884bfSSam Clegg# IGNORE-NEXT:      - Index:           1
43206884bfSSam Clegg# IGNORE-NEXT:        Locals:          []
449647a6f7SWouter van Oortmerssen# IGNORE-NEXT:        Body:            1080808080001082808080001083808080001A1A0B
45206884bfSSam Clegg# IGNORE-NEXT:      - Index:           2
46206884bfSSam Clegg# IGNORE-NEXT:        Locals:          []
47206884bfSSam Clegg# IGNORE-NEXT:        Body:            4180808080000F0B
48206884bfSSam Clegg# IGNORE-NEXT:      - Index:           3
49206884bfSSam Clegg# IGNORE-NEXT:        Locals:          []
50206884bfSSam Clegg# IGNORE-NEXT:        Body:            4180808080000F0B
51206884bfSSam Clegg#
52206884bfSSam Clegg#      IGNORE:  - Type:            CUSTOM
53206884bfSSam Clegg# IGNORE-NEXT:    Name:            name
54206884bfSSam Clegg# IGNORE-NEXT:    FunctionNames:
55206884bfSSam Clegg# IGNORE-NEXT:      - Index:           0
56206884bfSSam Clegg# IGNORE-NEXT:        Name:            undefined
57206884bfSSam Clegg# IGNORE-NEXT:      - Index:           1
58206884bfSSam Clegg# IGNORE-NEXT:        Name:            _start
59206884bfSSam Clegg# IGNORE-NEXT:      - Index:           2
60206884bfSSam Clegg# IGNORE-NEXT:        Name:            get_data_addr
61206884bfSSam Clegg# IGNORE-NEXT:      - Index:           3
62206884bfSSam Clegg# IGNORE-NEXT:        Name:            get_func_addr
63206884bfSSam Clegg
645b0e45c8SSam Clegg## --import-undefined should handle unresolved functions symbols
655b0e45c8SSam Clegg## by importing them but still report errors/warning for missing data symbols.
665b0e45c8SSam Clegg## `--allow-undefined` should behave like `--import-undefined` +
675b0e45c8SSam Clegg## `--unresolve-symbols=ignore`
684d888375SSam Clegg# RUN: wasm-ld %t/main.o -o %t3.wasm --import-undefined --unresolved-symbols=ignore-all
69206884bfSSam Clegg# RUN: obj2yaml %t3.wasm | FileCheck -check-prefix=IMPORT %s
70206884bfSSam Clegg#      IMPORT:  - Type:            IMPORT
71206884bfSSam Clegg# IMPORT-NEXT:    Imports:
72206884bfSSam Clegg# IMPORT-NEXT:      - Module:          env
73758633f9SSam Clegg# IMPORT-NEXT:        Field:           undef_func
74206884bfSSam Clegg# IMPORT-NEXT:        Kind:            FUNCTION
75206884bfSSam Clegg# IMPORT-NEXT:        SigIndex:        0
76206884bfSSam Clegg# IMPORT-NEXT:  - Type:            FUNCTION
77206884bfSSam Clegg
78758633f9SSam Clegg## Check that --import-undefined reports unresolved data symbols.
794d888375SSam Clegg# RUN: not wasm-ld %t/main.o -o %t3.wasm --import-undefined --unresolved-symbols=report-all 2>&1 | FileCheck -check-prefix=IMPORTUNDEFINED %s
804d888375SSam Clegg# IMPORTUNDEFINED-NOT: error: {{.*}}main.o: undefined symbol: undef_func
814d888375SSam Clegg# IMPORTUNDEFINED: error: {{.*}}main.o: undefined symbol: undef_data
82758633f9SSam Clegg
83206884bfSSam Clegg## Do not report undefines if linking relocatable.
844d888375SSam Clegg# RUN: wasm-ld -r %t/main.o -o %t4.wasm --unresolved-symbols=report-all
85206884bfSSam Clegg# RUN: llvm-readobj %t4.wasm > /dev/null 2>&1
86206884bfSSam Clegg
874d888375SSam Clegg## import-dynamic should fail due to incompatible relocations.
88*22b7b848SSam Clegg# RUN: not wasm-ld %t/main.o -o %t5.wasm --experimental-pic --unresolved-symbols=import-dynamic 2>&1 | FileCheck -check-prefix=ERRNOPIC %s
894d888375SSam Clegg# ERRNOPIC: relocation R_WASM_MEMORY_ADDR_SLEB cannot be used against symbol `undef_data`; recompile with -fPIC
904d888375SSam Clegg# ERRNOPIC: relocation R_WASM_TABLE_INDEX_SLEB cannot be used against symbol `undef_func`; recompile with -fPIC
914d888375SSam Clegg
924d888375SSam Clegg#--- main.s
934d888375SSam Clegg
949647a6f7SWouter van Oortmerssen.functype undef_func () -> ()
959647a6f7SWouter van Oortmerssen.functype get_data_addr () -> (i32)
969647a6f7SWouter van Oortmerssen.functype get_func_addr () -> (i32)
979647a6f7SWouter van Oortmerssen
98206884bfSSam Clegg.globl _start
99206884bfSSam Clegg_start:
100206884bfSSam Clegg    .functype _start () -> ()
101758633f9SSam Clegg    call undef_func
102206884bfSSam Clegg    call get_data_addr
103206884bfSSam Clegg    call get_func_addr
1049647a6f7SWouter van Oortmerssen    drop
1059647a6f7SWouter van Oortmerssen    drop
106206884bfSSam Clegg    end_function
107206884bfSSam Clegg
108206884bfSSam Clegg.globl get_data_addr
109206884bfSSam Cleggget_data_addr:
110206884bfSSam Clegg    .functype get_data_addr () -> (i32)
111206884bfSSam Clegg    i32.const undef_data
112206884bfSSam Clegg    return
113206884bfSSam Clegg    end_function
114206884bfSSam Clegg
115206884bfSSam Clegg.globl get_func_addr
116206884bfSSam Cleggget_func_addr:
117206884bfSSam Clegg    .functype get_func_addr () -> (i32)
118758633f9SSam Clegg    i32.const undef_func
119206884bfSSam Clegg    return
120206884bfSSam Clegg    end_function
1214d888375SSam Clegg
1224d888375SSam Clegg#--- secondary.s
1234d888375SSam Clegg
1244d888375SSam Clegg.functype undef_func () -> ()
1254d888375SSam Clegg.globl foo
1264d888375SSam Cleggfoo:
1274d888375SSam Clegg    .functype foo () -> ()
1284d888375SSam Clegg    call undef_func
1294d888375SSam Clegg    end_function
130