1 # Expect script for RISC-V ELF linker tests
2 # Copyright (C) 2017-2022 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
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.
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.
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,
22 proc riscv_choose_ilp32_emul {} {
23 if { [istarget "riscvbe-*"] \
24 || [istarget "riscv32be-*"] \
25 || [istarget "riscv64be-*"] } {
31 proc riscv_choose_lp64_emul {} {
32 if { [istarget "riscvbe-*"] \
33 || [istarget "riscv32be-*"] \
34 || [istarget "riscv64be-*"] } {
40 # target: rv32 or rv64.
41 # output: Which output you want? (exe, pie, .so)
42 proc run_dump_test_ifunc { name target output} {
44 set ldflags "-z nocombreloc"
52 set ldflags "$ldflags -pie"
56 set ldflags "$ldflags -shared"
62 set asflags "$asflags -march=rv32i -mabi=ilp32"
63 set ldflags "$ldflags -m[riscv_choose_ilp32_emul]"
66 set asflags "$asflags -march=rv64i -mabi=lp64 -defsym __64_bit__=1"
67 set ldflags "$ldflags -m[riscv_choose_lp64_emul]"
71 run_ld_link_tests [list \
72 [list "$name ($target-$output)" \
76 [concat [list "readelf -rW $name-$output.rd"] \
77 [list "objdump -dw $name.d"]] \
78 "$name-$target.$ext"]]
81 proc run_relax_twice_test {} {
90 set testname "relax-twice"
91 if ![runtest_file_p $runtests $testname] then {
95 # assemble and link the two input files with a version script, then
96 # capture output of nm and compare addresses of the two symbols
97 # 'foobar_new' and 'foobar@@New'. They must be equal.
98 # Bitness doesn't matter so we simply force 64bit.
99 if { ![ld_assemble_flags $as "-march=rv64i" $srcdir/$subdir/relax-twice-1.s tmpdir/relax-twice-1.o ]
100 || ![ld_assemble_flags $as "-march=rv64i" $srcdir/$subdir/relax-twice-2.s tmpdir/relax-twice-2.o]
101 || ![ld_link $ld tmpdir/relax-twice.so "-m[riscv_choose_lp64_emul] -shared --relax --version-script $srcdir/$subdir/relax-twice.ver tmpdir/relax-twice-1.o tmpdir/relax-twice-2.o"] } {
103 } elseif { ![ld_nm $nm "" tmpdir/relax-twice.so] } {
105 } elseif { ![info exists nm_output(foobar_new)]
106 || ![info exists nm_output(foobar@@New)]} {
107 send_log "bad output from nm\n"
108 verbose "bad output from nm"
110 } elseif {$nm_output(foobar_new) != $nm_output(foobar@@New)} {
111 send_log "foobar_new == $nm_output(foobar_new)\n"
112 verbose "foobar_new == $nm_output(foobar_new)"
113 send_log "foobar@@New == $nm_output(foobar@@New)\n"
114 verbose "foobar@@New == $nm_output(foobar@@New)"
121 if [istarget "riscv*-*-*"] {
122 run_dump_test "align-small-region"
123 run_dump_test "call-relax"
124 run_dump_test "pcgp-relax-01"
125 run_dump_test "pcgp-relax-02"
126 run_dump_test "c-lui"
127 run_dump_test "c-lui-2"
128 run_dump_test "disas-jalr"
129 run_dump_test "pcrel-lo-addend"
130 run_dump_test "pcrel-lo-addend-2a"
131 run_dump_test "pcrel-lo-addend-2b"
132 run_dump_test "pcrel-lo-addend-3a"
133 run_dump_test "pcrel-lo-addend-3b"
134 run_dump_test "pcrel-lo-addend-3c"
135 run_dump_test "code-model-medlow-01"
136 run_dump_test "code-model-medlow-02"
137 run_dump_test "code-model-medlow-weakref-01"
138 run_dump_test "code-model-medlow-weakref-02"
139 run_dump_test "code-model-medany-01"
140 run_dump_test "code-model-medany-02"
141 run_dump_test "code-model-medany-weakref-01"
142 run_dump_test "code-model-medany-weakref-02"
143 run_dump_test "code-model-relax-medlow-01"
144 run_dump_test "code-model-relax-medlow-02"
145 run_dump_test "code-model-relax-medlow-weakref-01"
146 run_dump_test "code-model-relax-medlow-weakref-02"
147 run_dump_test "code-model-relax-medany-01"
148 run_dump_test "code-model-relax-medany-02"
149 run_dump_test "code-model-relax-medany-weakref-01"
150 run_dump_test "code-model-relax-medany-weakref-02"
151 run_dump_test "attr-merge-arch-01"
152 run_dump_test "attr-merge-arch-02"
153 run_dump_test "attr-merge-arch-03"
154 run_dump_test "attr-merge-strict-align-01"
155 run_dump_test "attr-merge-strict-align-02"
156 run_dump_test "attr-merge-strict-align-03"
157 run_dump_test "attr-merge-strict-align-04"
158 run_dump_test "attr-merge-strict-align-05"
159 run_dump_test "attr-merge-stack-align"
160 run_dump_test "attr-merge-priv-spec-01"
161 run_dump_test "attr-merge-priv-spec-02"
162 run_dump_test "attr-merge-priv-spec-03"
163 run_dump_test "attr-merge-arch-failed-01"
164 run_dump_test "attr-merge-arch-failed-02"
165 run_dump_test "attr-merge-stack-align-failed"
166 run_dump_test "attr-merge-priv-spec-failed-01"
167 run_dump_test "attr-merge-priv-spec-failed-02"
168 run_dump_test "attr-merge-priv-spec-failed-03"
169 run_dump_test "attr-merge-priv-spec-failed-04"
170 run_dump_test "attr-merge-priv-spec-failed-05"
171 run_dump_test "attr-merge-priv-spec-failed-06"
172 run_dump_test "attr-phdr"
173 run_ld_link_tests [list \
174 [list "Weak reference 32" "-T weakref.ld -m[riscv_choose_ilp32_emul]" "" \
175 "-march=rv32i -mabi=ilp32" {weakref32.s} \
176 {{objdump -d weakref32.d}} "weakref32"] \
177 [list "Weak reference 64" "-T weakref.ld -m[riscv_choose_lp64_emul]" "" \
178 "-march=rv64i -mabi=lp64" {weakref64.s} \
179 {{objdump -d weakref64.d}} "weakref64"]]
181 # The following tests require shared library support.
182 if ![check_shared_lib_support] {
186 run_dump_test "relro-relax-lui"
187 run_dump_test "relro-relax-pcrel"
188 run_dump_test "variant_cc-now"
189 run_dump_test "variant_cc-shared"
190 run_dump_test "variant_cc-r"
193 set abis [list rv32gc ilp32 [riscv_choose_ilp32_emul] rv64gc lp64 [riscv_choose_lp64_emul]]
194 foreach { arch abi emul } $abis {
195 # This checks whether our linker scripts handle __global_pointer$
196 # correctly. It should be defined in executables and PIE, but not
197 # in shared libraries.
198 set suff64 [string map {ilp32 "" lp64 -64} $abi]
199 run_ld_link_tests [list \
200 [list "gp test ($abi shared library)" \
201 "-m$emul -shared" "" \
202 "-march=$arch -mabi=$abi -fpic" \
204 [list "readelf --syms gp-test-lib.sd"] \
205 "gp-test-lib-${abi}.so"] \
206 [list "gp test ($abi executable)" \
208 "-march=$arch -mabi=$abi" \
210 [list "readelf --syms gp-test.sd"] \
215 { "Link non-pic code into a shared library (setup)"
216 "-shared" "" "" {lib-nopic-01a.s}
217 {} "lib-nopic-01a.so" }
219 run_dump_test "lib-nopic-01b"
222 # Check IFUNC by single type relocs.
223 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 exe
224 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 pie
225 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 pic
226 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 exe
227 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 pie
228 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 pic
229 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 exe
230 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 pie
231 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 pic
232 run_dump_test_ifunc "ifunc-reloc-data" rv32 exe
233 run_dump_test_ifunc "ifunc-reloc-data" rv32 pie
234 run_dump_test_ifunc "ifunc-reloc-data" rv32 pic
235 run_dump_test_ifunc "ifunc-reloc-got" rv32 exe
236 run_dump_test_ifunc "ifunc-reloc-got" rv32 pie
237 run_dump_test_ifunc "ifunc-reloc-got" rv32 pic
238 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 exe
239 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 pie
240 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 pic
241 run_dump_test_ifunc "ifunc-reloc-data" rv64 exe
242 run_dump_test_ifunc "ifunc-reloc-data" rv64 pie
243 run_dump_test_ifunc "ifunc-reloc-data" rv64 pic
244 run_dump_test_ifunc "ifunc-reloc-got" rv64 exe
245 run_dump_test_ifunc "ifunc-reloc-got" rv64 pie
246 run_dump_test_ifunc "ifunc-reloc-got" rv64 pic
247 # Check the IFUNC PLT and non-PLT relocs.
248 run_dump_test_ifunc "ifunc-nonplt" rv32 exe
249 run_dump_test_ifunc "ifunc-nonplt" rv32 pie
250 run_dump_test_ifunc "ifunc-nonplt" rv32 pic
251 run_dump_test_ifunc "ifunc-plt-01" rv32 exe
252 run_dump_test_ifunc "ifunc-plt-01" rv32 pie
253 run_dump_test_ifunc "ifunc-plt-01" rv32 pic
254 run_dump_test_ifunc "ifunc-plt-02" rv32 exe
255 run_dump_test_ifunc "ifunc-plt-02" rv32 pie
256 run_dump_test_ifunc "ifunc-plt-02" rv32 pic
257 run_dump_test_ifunc "ifunc-nonplt" rv64 exe
258 run_dump_test_ifunc "ifunc-nonplt" rv64 pie
259 run_dump_test_ifunc "ifunc-nonplt" rv64 pic
260 run_dump_test_ifunc "ifunc-plt-01" rv64 exe
261 run_dump_test_ifunc "ifunc-plt-01" rv64 pie
262 run_dump_test_ifunc "ifunc-plt-01" rv64 pic
263 run_dump_test_ifunc "ifunc-plt-02" rv64 exe
264 run_dump_test_ifunc "ifunc-plt-02" rv64 pie
265 run_dump_test_ifunc "ifunc-plt-02" rv64 pic
266 # Check the .rela.iplt overwrite issue.
267 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 exe
268 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 pie
269 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 pic
270 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 exe
271 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 pie
272 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 pic
274 # Setup shared libraries.
276 { "Build shared library for IFUNC non-PLT caller"
277 "-shared" "" "" {ifunc-seperate-caller-nonplt.s}
278 {} "ifunc-seperate-caller.so" }
279 { "Build shared library for IFUNC PLT caller"
280 "-shared" "" "" {ifunc-seperate-caller-plt.s}
281 {} "ifunc-seperate-caller.so" }
282 { "Build shared library for IFUNC resolver"
283 "-shared" "" "" {ifunc-seperate-resolver.s}
284 {} "ifunc-seperate-resolver.so" }
286 # The IFUNC resolver and caller are in the seperate modules.
287 # If IFUNC resolver and caller are linked to the same module,
288 # then the result are the same as the run_dump_test_ifunc.
289 run_dump_test "ifunc-seperate-nonplt-exe"
290 run_dump_test "ifunc-seperate-nonplt-pie"
291 run_dump_test "ifunc-seperate-nonplt-pic"
292 run_dump_test "ifunc-seperate-plt-exe"
293 run_dump_test "ifunc-seperate-plt-pie"
294 run_dump_test "ifunc-seperate-plt-pic"
295 run_dump_test "ifunc-seperate-pcrel-pie"
296 run_dump_test "ifunc-seperate-pcrel-pic"