f3ff95cf96ec79c24fedee5823805e32cc6f62ec
[binutils-gdb.git] / ld / testsuite / ld-riscv-elf / ld-riscv-elf.exp
1 # Expect script for RISC-V ELF linker tests
2 # Copyright (C) 2017-2021 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20 #
21
22 proc riscv_choose_ilp32_emul {} {
23 if { [istarget "riscvbe-*"] \
24 || [istarget "riscv32be-*"] \
25 || [istarget "riscv64be-*"] } {
26 return "elf32briscv"
27 }
28 return "elf32lriscv"
29 }
30
31 proc riscv_choose_lp64_emul {} {
32 if { [istarget "riscvbe-*"] \
33 || [istarget "riscv32be-*"] \
34 || [istarget "riscv64be-*"] } {
35 return "elf64briscv"
36 }
37 return "elf64lriscv"
38 }
39
40 # target: rv32 or rv64.
41 # output: Which output you want? (exe, pie, .so)
42 proc run_dump_test_ifunc { name target output} {
43 set asflags ""
44 set ldflags "-z nocombreloc"
45
46 switch -- $output {
47 exe {
48 set ext "exe"
49 }
50 pie {
51 set ext "pie"
52 set ldflags "$ldflags -pie"
53 }
54 pic {
55 set ext "so"
56 set ldflags "$ldflags -shared"
57 }
58 }
59
60 switch -- $target {
61 rv32 {
62 set asflags "$asflags -march=rv32i -mabi=ilp32"
63 set ldflags "$ldflags -m[riscv_choose_ilp32_emul]"
64 }
65 rv64 {
66 set asflags "$asflags -march=rv64i -mabi=lp64 -defsym __64_bit__=1"
67 set ldflags "$ldflags -m[riscv_choose_lp64_emul]"
68 }
69 }
70
71 run_ld_link_tests [list \
72 [list "$name ($target-$output)" \
73 "$ldflags" "" \
74 "$asflags" \
75 [list "$name.s"] \
76 [concat [list "readelf -rW $name-$output.rd"] \
77 [list "objdump -dw $name.d"]] \
78 "$name-$target.$ext"]]
79 }
80
81 if [istarget "riscv*-*-*"] {
82 run_dump_test "call-relax"
83 run_dump_test "pcgp-relax"
84 run_dump_test "c-lui"
85 run_dump_test "c-lui-2"
86 run_dump_test "disas-jalr"
87 run_dump_test "pcrel-lo-addend"
88 run_dump_test "pcrel-lo-addend-2"
89 run_dump_test "restart-relax"
90 run_dump_test "attr-merge-arch-01"
91 run_dump_test "attr-merge-arch-02"
92 run_dump_test "attr-merge-arch-03"
93 run_dump_test "attr-merge-strict-align-01"
94 run_dump_test "attr-merge-strict-align-02"
95 run_dump_test "attr-merge-strict-align-03"
96 run_dump_test "attr-merge-strict-align-04"
97 run_dump_test "attr-merge-strict-align-05"
98 run_dump_test "attr-merge-stack-align"
99 run_dump_test "attr-merge-priv-spec-01"
100 run_dump_test "attr-merge-priv-spec-02"
101 run_dump_test "attr-merge-priv-spec-03"
102 run_dump_test "attr-merge-arch-failed-01"
103 run_dump_test "attr-merge-arch-failed-02"
104 run_dump_test "attr-merge-stack-align-failed"
105 run_dump_test "attr-merge-priv-spec-failed-01"
106 run_dump_test "attr-merge-priv-spec-failed-02"
107 run_dump_test "attr-merge-priv-spec-failed-03"
108 run_dump_test "attr-merge-priv-spec-failed-04"
109 run_dump_test "attr-merge-priv-spec-failed-05"
110 run_dump_test "attr-merge-priv-spec-failed-06"
111 run_ld_link_tests [list \
112 [list "Weak reference 32" "-T weakref.ld -m[riscv_choose_ilp32_emul]" "" \
113 "-march=rv32i -mabi=ilp32" {weakref32.s} \
114 {{objdump -d weakref32.d}} "weakref32"] \
115 [list "Weak reference 64" "-T weakref.ld -m[riscv_choose_lp64_emul]" "" \
116 "-march=rv64i -mabi=lp64" {weakref64.s} \
117 {{objdump -d weakref64.d}} "weakref64"]]
118
119 # The following tests require shared library support.
120 if ![check_shared_lib_support] {
121 return
122 }
123
124 set abis [list rv32gc ilp32 [riscv_choose_ilp32_emul] rv64gc lp64 [riscv_choose_lp64_emul]]
125 foreach { arch abi emul } $abis {
126 # This checks whether our linker scripts handle __global_pointer$
127 # correctly. It should be defined in executables and PIE, but not
128 # in shared libraries.
129 set suff64 [string map {ilp32 "" lp64 -64} $abi]
130 run_ld_link_tests [list \
131 [list "gp test ($abi shared library)" \
132 "-m$emul -shared" "" \
133 "-march=$arch -mabi=$abi -fpic" \
134 { gp-test.s } \
135 [list "readelf --syms gp-test-lib.sd"] \
136 "gp-test-lib-${abi}.so"] \
137 [list "gp test ($abi executable)" \
138 "-m$emul" "" \
139 "-march=$arch -mabi=$abi" \
140 { gp-test.s } \
141 [list "readelf --syms gp-test.sd"] \
142 "gp-test-${abi}"]]
143 }
144
145 run_ld_link_tests {
146 { "Link non-pic code into a shared library (setup)"
147 "-shared" "" "" {lib-nopic-01a.s}
148 {} "lib-nopic-01a.so" }
149 }
150 run_dump_test "lib-nopic-01b"
151
152 # IFUNC testcases.
153 # Check IFUNC by single type relocs.
154 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 exe
155 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 pie
156 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 pic
157 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 exe
158 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 pie
159 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 pic
160 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 exe
161 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 pie
162 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 pic
163 run_dump_test_ifunc "ifunc-reloc-data" rv32 exe
164 run_dump_test_ifunc "ifunc-reloc-data" rv32 pie
165 run_dump_test_ifunc "ifunc-reloc-data" rv32 pic
166 run_dump_test_ifunc "ifunc-reloc-got" rv32 exe
167 run_dump_test_ifunc "ifunc-reloc-got" rv32 pie
168 run_dump_test_ifunc "ifunc-reloc-got" rv32 pic
169 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 exe
170 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 pie
171 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 pic
172 run_dump_test_ifunc "ifunc-reloc-data" rv64 exe
173 run_dump_test_ifunc "ifunc-reloc-data" rv64 pie
174 run_dump_test_ifunc "ifunc-reloc-data" rv64 pic
175 run_dump_test_ifunc "ifunc-reloc-got" rv64 exe
176 run_dump_test_ifunc "ifunc-reloc-got" rv64 pie
177 run_dump_test_ifunc "ifunc-reloc-got" rv64 pic
178 # Check the IFUNC PLT and non-PLT relocs.
179 run_dump_test_ifunc "ifunc-nonplt" rv32 exe
180 run_dump_test_ifunc "ifunc-nonplt" rv32 pie
181 run_dump_test_ifunc "ifunc-nonplt" rv32 pic
182 run_dump_test_ifunc "ifunc-plt-01" rv32 exe
183 run_dump_test_ifunc "ifunc-plt-01" rv32 pie
184 run_dump_test_ifunc "ifunc-plt-01" rv32 pic
185 run_dump_test_ifunc "ifunc-plt-02" rv32 exe
186 run_dump_test_ifunc "ifunc-plt-02" rv32 pie
187 run_dump_test_ifunc "ifunc-plt-02" rv32 pic
188 run_dump_test_ifunc "ifunc-nonplt" rv64 exe
189 run_dump_test_ifunc "ifunc-nonplt" rv64 pie
190 run_dump_test_ifunc "ifunc-nonplt" rv64 pic
191 run_dump_test_ifunc "ifunc-plt-01" rv64 exe
192 run_dump_test_ifunc "ifunc-plt-01" rv64 pie
193 run_dump_test_ifunc "ifunc-plt-01" rv64 pic
194 run_dump_test_ifunc "ifunc-plt-02" rv64 exe
195 run_dump_test_ifunc "ifunc-plt-02" rv64 pie
196 run_dump_test_ifunc "ifunc-plt-02" rv64 pic
197 # Check the .rela.iplt overwrite issue.
198 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 exe
199 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 pie
200 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 pic
201 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 exe
202 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 pie
203 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 pic
204
205 # Setup shared libraries.
206 run_ld_link_tests {
207 { "Build shared library for IFUNC non-PLT caller"
208 "-shared" "" "" {ifunc-seperate-caller-nonplt.s}
209 {} "ifunc-seperate-caller.so" }
210 { "Build shared library for IFUNC PLT caller"
211 "-shared" "" "" {ifunc-seperate-caller-plt.s}
212 {} "ifunc-seperate-caller.so" }
213 { "Build shared library for IFUNC resolver"
214 "-shared" "" "" {ifunc-seperate-resolver.s}
215 {} "ifunc-seperate-resolver.so" }
216 }
217 # The IFUNC resolver and caller are in the seperate modules.
218 # If IFUNC resolver and caller are linked to the same module,
219 # then the result are the same as the run_dump_test_ifunc.
220 run_dump_test "ifunc-seperate-nonplt-exe"
221 run_dump_test "ifunc-seperate-nonplt-pie"
222 run_dump_test "ifunc-seperate-nonplt-pic"
223 run_dump_test "ifunc-seperate-plt-exe"
224 run_dump_test "ifunc-seperate-plt-pie"
225 run_dump_test "ifunc-seperate-plt-pic"
226 run_dump_test "ifunc-seperate-pcrel-pie"
227 run_dump_test "ifunc-seperate-pcrel-pic"
228 }