1 # Expect script for RISC-V ELF linker tests
2 # Copyright (C) 2017-2021 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 "call-relax"
123 run_dump_test "pcgp-relax"
124 run_dump_test "c-lui"
125 run_dump_test "c-lui-2"
126 run_dump_test "disas-jalr"
127 run_dump_test "pcrel-lo-addend"
128 run_dump_test "pcrel-lo-addend-2a"
129 run_dump_test "pcrel-lo-addend-2b"
130 run_dump_test "pcrel-lo-addend-3a"
131 run_dump_test "pcrel-lo-addend-3b"
132 run_dump_test "pcrel-lo-addend-3c"
133 run_dump_test "restart-relax"
134 run_dump_test "attr-merge-arch-01"
135 run_dump_test "attr-merge-arch-02"
136 run_dump_test "attr-merge-arch-03"
137 run_dump_test "attr-merge-strict-align-01"
138 run_dump_test "attr-merge-strict-align-02"
139 run_dump_test "attr-merge-strict-align-03"
140 run_dump_test "attr-merge-strict-align-04"
141 run_dump_test "attr-merge-strict-align-05"
142 run_dump_test "attr-merge-stack-align"
143 run_dump_test "attr-merge-priv-spec-01"
144 run_dump_test "attr-merge-priv-spec-02"
145 run_dump_test "attr-merge-priv-spec-03"
146 run_dump_test "attr-merge-arch-failed-01"
147 run_dump_test "attr-merge-arch-failed-02"
148 run_dump_test "attr-merge-stack-align-failed"
149 run_dump_test "attr-merge-priv-spec-failed-01"
150 run_dump_test "attr-merge-priv-spec-failed-02"
151 run_dump_test "attr-merge-priv-spec-failed-03"
152 run_dump_test "attr-merge-priv-spec-failed-04"
153 run_dump_test "attr-merge-priv-spec-failed-05"
154 run_dump_test "attr-merge-priv-spec-failed-06"
155 run_dump_test "attr-phdr"
156 run_ld_link_tests [list \
157 [list "Weak reference 32" "-T weakref.ld -m[riscv_choose_ilp32_emul]" "" \
158 "-march=rv32i -mabi=ilp32" {weakref32.s} \
159 {{objdump -d weakref32.d}} "weakref32"] \
160 [list "Weak reference 64" "-T weakref.ld -m[riscv_choose_lp64_emul]" "" \
161 "-march=rv64i -mabi=lp64" {weakref64.s} \
162 {{objdump -d weakref64.d}} "weakref64"]]
164 # The following tests require shared library support.
165 if ![check_shared_lib_support] {
169 run_dump_test "relro-relax-lui"
170 run_dump_test "relro-relax-pcrel"
173 set abis [list rv32gc ilp32 [riscv_choose_ilp32_emul] rv64gc lp64 [riscv_choose_lp64_emul]]
174 foreach { arch abi emul } $abis {
175 # This checks whether our linker scripts handle __global_pointer$
176 # correctly. It should be defined in executables and PIE, but not
177 # in shared libraries.
178 set suff64 [string map {ilp32 "" lp64 -64} $abi]
179 run_ld_link_tests [list \
180 [list "gp test ($abi shared library)" \
181 "-m$emul -shared" "" \
182 "-march=$arch -mabi=$abi -fpic" \
184 [list "readelf --syms gp-test-lib.sd"] \
185 "gp-test-lib-${abi}.so"] \
186 [list "gp test ($abi executable)" \
188 "-march=$arch -mabi=$abi" \
190 [list "readelf --syms gp-test.sd"] \
195 { "Link non-pic code into a shared library (setup)"
196 "-shared" "" "" {lib-nopic-01a.s}
197 {} "lib-nopic-01a.so" }
199 run_dump_test "lib-nopic-01b"
202 # Check IFUNC by single type relocs.
203 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 exe
204 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 pie
205 run_dump_test_ifunc "ifunc-reloc-call-01" rv32 pic
206 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 exe
207 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 pie
208 run_dump_test_ifunc "ifunc-reloc-call-02" rv32 pic
209 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 exe
210 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 pie
211 run_dump_test_ifunc "ifunc-reloc-pcrel" rv32 pic
212 run_dump_test_ifunc "ifunc-reloc-data" rv32 exe
213 run_dump_test_ifunc "ifunc-reloc-data" rv32 pie
214 run_dump_test_ifunc "ifunc-reloc-data" rv32 pic
215 run_dump_test_ifunc "ifunc-reloc-got" rv32 exe
216 run_dump_test_ifunc "ifunc-reloc-got" rv32 pie
217 run_dump_test_ifunc "ifunc-reloc-got" rv32 pic
218 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 exe
219 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 pie
220 run_dump_test_ifunc "ifunc-reloc-pcrel" rv64 pic
221 run_dump_test_ifunc "ifunc-reloc-data" rv64 exe
222 run_dump_test_ifunc "ifunc-reloc-data" rv64 pie
223 run_dump_test_ifunc "ifunc-reloc-data" rv64 pic
224 run_dump_test_ifunc "ifunc-reloc-got" rv64 exe
225 run_dump_test_ifunc "ifunc-reloc-got" rv64 pie
226 run_dump_test_ifunc "ifunc-reloc-got" rv64 pic
227 # Check the IFUNC PLT and non-PLT relocs.
228 run_dump_test_ifunc "ifunc-nonplt" rv32 exe
229 run_dump_test_ifunc "ifunc-nonplt" rv32 pie
230 run_dump_test_ifunc "ifunc-nonplt" rv32 pic
231 run_dump_test_ifunc "ifunc-plt-01" rv32 exe
232 run_dump_test_ifunc "ifunc-plt-01" rv32 pie
233 run_dump_test_ifunc "ifunc-plt-01" rv32 pic
234 run_dump_test_ifunc "ifunc-plt-02" rv32 exe
235 run_dump_test_ifunc "ifunc-plt-02" rv32 pie
236 run_dump_test_ifunc "ifunc-plt-02" rv32 pic
237 run_dump_test_ifunc "ifunc-nonplt" rv64 exe
238 run_dump_test_ifunc "ifunc-nonplt" rv64 pie
239 run_dump_test_ifunc "ifunc-nonplt" rv64 pic
240 run_dump_test_ifunc "ifunc-plt-01" rv64 exe
241 run_dump_test_ifunc "ifunc-plt-01" rv64 pie
242 run_dump_test_ifunc "ifunc-plt-01" rv64 pic
243 run_dump_test_ifunc "ifunc-plt-02" rv64 exe
244 run_dump_test_ifunc "ifunc-plt-02" rv64 pie
245 run_dump_test_ifunc "ifunc-plt-02" rv64 pic
246 # Check the .rela.iplt overwrite issue.
247 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 exe
248 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 pie
249 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv32 pic
250 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 exe
251 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 pie
252 run_dump_test_ifunc "ifunc-plt-got-overwrite" rv64 pic
254 # Setup shared libraries.
256 { "Build shared library for IFUNC non-PLT caller"
257 "-shared" "" "" {ifunc-seperate-caller-nonplt.s}
258 {} "ifunc-seperate-caller.so" }
259 { "Build shared library for IFUNC PLT caller"
260 "-shared" "" "" {ifunc-seperate-caller-plt.s}
261 {} "ifunc-seperate-caller.so" }
262 { "Build shared library for IFUNC resolver"
263 "-shared" "" "" {ifunc-seperate-resolver.s}
264 {} "ifunc-seperate-resolver.so" }
266 # The IFUNC resolver and caller are in the seperate modules.
267 # If IFUNC resolver and caller are linked to the same module,
268 # then the result are the same as the run_dump_test_ifunc.
269 run_dump_test "ifunc-seperate-nonplt-exe"
270 run_dump_test "ifunc-seperate-nonplt-pie"
271 run_dump_test "ifunc-seperate-nonplt-pic"
272 run_dump_test "ifunc-seperate-plt-exe"
273 run_dump_test "ifunc-seperate-plt-pie"
274 run_dump_test "ifunc-seperate-plt-pic"
275 run_dump_test "ifunc-seperate-pcrel-pie"
276 run_dump_test "ifunc-seperate-pcrel-pic"