Handle weak alias for PIE with copy reloc
[platform/upstream/binutils.git] / ld / testsuite / ld-x86-64 / x86-64.exp
1 # Expect script for ld-x86_64 tests
2 #   Copyright (C) 2002-2014 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 # Test x86_64 linking; all types of relocs.  This tests the assembler and
23 # tools like objdump as well as the linker.
24
25 if { !([istarget "x86_64-*-elf*"]
26        || [istarget "x86_64-*-nacl*"]
27        || [istarget "x86_64-*-linux*"]) } {
28     return
29 }
30
31 if [istarget "*-*-nacl*"] {
32     set emul "elf_x86_64_nacl"
33 } else {
34     set emul "elf_x86_64"
35 }
36
37 # List contains test-items with 3 items followed by 2 lists:
38 # 0:name 1:ld early options 2:ld late options 3:assembler options
39 # 4:filenames of assembler files 5: action and options. 6: name of output file
40
41 # Actions:
42 # objdump: Apply objdump options on result.  Compare with regex (last arg).
43 # nm: Apply nm options on result.  Compare with regex (last arg).
44 # readelf: Apply readelf options on result.  Compare with regex (last arg).
45
46 set x86_64tests {
47     {"Helper shared library (basic PLT test)"
48       "-shared -melf_x86_64" "" "--64" {pltlib.s} {} "libpltlib.so"}
49     {"basic PLT generation"
50      "-melf_x86_64 tmpdir/libpltlib.so" "" "--64" {plt.s}
51      {{objdump -drj.plt plt.pd}} "plt"}
52     {"TLS -fpic -shared transitions"
53      "-shared -melf_x86_64 --no-ld-generated-unwind-info" ""
54      "--64" {tlspic1.s tlspic2.s}
55      {{readelf -WSsrl tlspic.rd} {objdump -drj.text tlspic.dd}
56       {objdump -sj.got tlspic.sd} {objdump -sj.tdata tlspic.td}}
57       "libtlspic.so"}
58     {"TLS descriptor -fpic -shared transitions"
59      "-shared -melf_x86_64 --no-ld-generated-unwind-info" ""
60      "--64" {tlsdesc.s tlspic2.s}
61      {{readelf -WSsrld tlsdesc.rd} {objdump -drj.text tlsdesc.dd}
62       {objdump "-s -j.got -j.got.plt" tlsdesc.sd} {objdump -sj.tdata tlsdesc.td}
63       {objdump -drj.plt tlsdesc.pd}} "libtlsdesc.so"}
64     {"Helper shared library" "-shared -melf_x86_64" ""
65      "--64" {tlslib.s} {} "libtlslib.so"}
66     {"TLS -fpic and -fno-pic exec transitions"
67      "-melf_x86_64 tmpdir/libtlslib.so --no-ld-generated-unwind-info" ""
68      "--64" {tlsbinpic.s tlsbin.s}
69      {{readelf -WSsrl tlsbin.rd} {objdump -drj.text tlsbin.dd}
70       {objdump -sj.got tlsbin.sd} {objdump -sj.tdata tlsbin.td}}
71       "tlsbin"}
72     {"TLS descriptor -fpic and -fno-pic exec transitions"
73      "-melf_x86_64 tmpdir/libtlslib.so --no-ld-generated-unwind-info" ""
74      "--64" {tlsbindesc.s tlsbin.s}
75      {{readelf -WSsrl tlsbindesc.rd} {objdump -drj.text tlsbindesc.dd}
76       {objdump -sj.got tlsbindesc.sd} {objdump -sj.tdata tlsbindesc.td}}
77       "tlsbindesc"}
78     {"TLS with global dynamic and descriptors"
79      "-shared -melf_x86_64 --no-ld-generated-unwind-info" ""
80      "--64" {tlsgdesc.s}
81      {{readelf -WSsrl tlsgdesc.rd} {objdump -drj.text tlsgdesc.dd}}
82       "libtlsgdesc.so"}
83     {"TLS in debug sections" "-melf_x86_64" ""
84      "--64" {tlsg.s}
85      {{objdump -sj.debug_foobar tlsg.sd}} "tlsg"}
86     {"TLS GD->LE transition" "-melf_x86_64" ""
87      "--64" {tlsgd1.s}
88      {{objdump -dwr tlsgd1.dd}} "tlsgd1"}
89     {"TLS LD->LE transition" "-melf_x86_64" ""
90      "--64" {tlsld1.s}
91      {{objdump -dwr tlsld1.dd}} "tlsld1"}
92     {"TLS IE->LE transition" "-melf_x86_64" ""
93      "--64" {tlsie1.s}
94      {{objdump -dwr tlsie1.dd}} "tlsie1"}
95     {"Helper 64bit object 1" "-r -melf_x86_64" ""
96      "--64" {mixed1a.s} {} "libmixe1a.o"}
97     {"Helper 32bit object 1" "-r -melf_i386" ""
98      "--32" {mixed1b.s} {} "libmixe1b.o"}
99     {"Helper 64bit object 2" "-r -melf_x86_64" ""
100      "--64" {mixed2a.s} {} "libmixe2a.o"}
101     {"Helper 32bit object 2" "-r -melf_i386" ""
102      "--32" {mixed2b.s} {} "libmixe2b.o"}
103     {"Split by file with 'l' flag on section."
104      "-split-by-file -r -melf_x86_64" ""
105      "--64" {split-by-file1.s split-by-file2.s}
106      {{readelf -SW split-by-file.rd}} "split-by-file.o"}
107     {"TLS X32 IE->LE transition" "-melf32_x86_64" ""
108      "--x32" {tlsie4.s}
109      {{objdump -dwr tlsie4.dd}} "tlsie4"}
110     {"TLS X32 GD->LE transition" "-melf32_x86_64" ""
111      "--x32" {tlsgd4.s}
112      {{objdump -dwr tlsgd4.dd}} "tlsgd4"}
113     {"Helper TLS GD->IE transition DSO" "-shared -melf_x86_64" ""
114      "--64" {tlsgd5b.s} {} "libtlsgd5.so"}
115     {"TLS GD->IE transition" "-melf_x86_64 tmpdir/libtlsgd5.so" ""
116      "--64" {tlsgd5a.s}
117      {{objdump -dwr tlsgd5.dd}} "tlsgd5"}
118     {"Helper TLS X32 GD->IE transition DSO" "-shared -melf32_x86_64" ""
119      "--x32" {tlsgd6b.s} {} "libtlsgd6.so"}
120     {"TLS X32 GD->IE transition" "-melf32_x86_64 tmpdir/libtlsgd6.so" ""
121      "--x32" {tlsgd6a.s}
122      {{objdump -dwr tlsgd6.dd}} "tlsgd6"}
123     {"TLS X32 LD->LE transition" "-melf32_x86_64" ""
124      "--x32" {tlsld2.s}
125      {{objdump -dwr tlsld2.dd}} "tlsld2"}
126     {"TLS -mcmodel=large GD->LE transition" "-melf_x86_64" ""
127      "--64" {tlsgd7.s}
128      {{objdump -dwr tlsgd7.dd}} "tlsgd7"}
129     {"TLS -mcmodel=large LD->LE transition" "-melf_x86_64" ""
130      "--64" {tlsld3.s}
131      {{objdump -dwr tlsld3.dd}} "tlsld3"}
132     {"TLS -mcmodel=large GD->IE transition" "-melf_x86_64 tmpdir/libtlsgd5.so" ""
133      "--64" {tlsgd8.s}
134      {{objdump -dwrj.text tlsgd8.dd}} "tlsgd8"}
135
136      {"build 32-bit object with 33 locals" "-melf_x86_64 -e 0" "" "--32" {32bit.s} {{ ld incompatible.l }} "dummy" }
137      {"build 64-bit object" "-melf_x86_64 -e 0 --defsym foo=1" "" "--64" {64bit.s} {} "dummy" }
138      {"link mixed objects"  "-melf_x86_64 -e 0 tmpdir/32bit.o tmpdir/64bit.o" "" "" {} { { ld incompatible.l } } "mixed"}
139     {"PR ld/17313 (1)" "-melf_x86_64" ""
140      "--64" {dummy.s} {} ""}
141     {"PR ld/17313 (2)" "-melf_x86_64 -shared --just-symbols=tmpdir/dummy.o" ""
142      "--64" {lea1.s} {} "libpr17313.so"}
143     {"PR ld/17306 (1)" "-melf_x86_64" ""
144      "--64" {pr17306b.s} {} ""}
145     {"PR ld/17306 (2)" "-melf_x86_64 -shared -Bsymbolic --just-symbols=tmpdir/pr17306b.o" ""
146      "--64" {pr17306a.s} {} "libpr17306.so"}
147 }
148
149 # So as to avoid rewriting every last test case here in a nacl variant,
150 # we use black magic to massage the generic cases into nacl-variant cases.
151 if [istarget "*-*-nacl*"] {
152     set emul elf_x86_64_nacl
153
154     set lhs {elf(32)?_(i386|x86_64)[[:>:]]}
155     set rhs {&_nacl}
156
157     # Change all the -melf_x86_64 to -melf_x86_64_nacl so linking can succeed.
158     regsub -all -- $lhs $x86_64tests $rhs x86_64tests
159
160     # Same, applied to all the run_dump_test cases.
161     set options_regsub(ld) [list $lhs $rhs]
162
163     # The section/segment layout differs too much for the vanilla
164     # readelf output files to match.  So massage the cases so that
165     # they refer to a foo-nacl.rd file instead of a foo.rd file.
166     regsub -all -- {([a-z0-9]+)\.rd} $x86_64tests {\1-nacl.rd} x86_64tests
167
168     # Likewise for PLTs.
169     regsub -all -- {([a-z0-9]+)\.pd} $x86_64tests {\1-nacl.pd} x86_64tests
170 } else {
171     set emul elf_x86_64
172 }
173
174 run_ld_link_tests $x86_64tests
175
176 global link_output
177 global ld
178
179 set test_name "Mixed x86_64 and i386 input test 1"
180 set test mixed1
181 if { ![ld_simple_link $ld tmpdir/$test "-m$emul tmpdir/${test}a.o tmpdir/${test}b.o"] } {
182     if [string match "*i386* architecture of input file `tmpdir/${test}b.o' is incompatible with i386:x86-64* output*" $link_output] {
183         pass "$test_name"
184     } {
185         fail "$test_name"
186     }
187 }
188
189 set test_name "Mixed x86_64 and i386 input test 2"
190 set test mixed2
191 if { ![ld_simple_link $ld tmpdir/$test "-m$emul tmpdir/${test}a.o tmpdir/${test}b.o"] } {
192     if [string match "*i386* architecture of input file `tmpdir/${test}b.o' is incompatible with i386:x86-64* output*" $link_output] {
193         pass "$test_name"
194     } {
195         fail "$test_name"
196     }
197 }
198
199 run_dump_test "abs"
200 run_dump_test "abs-l1om"
201 run_dump_test "pcrel8"
202 run_dump_test "pcrel16"
203 run_dump_test "tlsgd2"
204 run_dump_test "tlsgd3"
205 run_dump_test "tlsie2"
206 run_dump_test "tlsie3"
207 run_dump_test "hidden1"
208 run_dump_test "hidden2"
209 run_dump_test "hidden3"
210 run_dump_test "protected1"
211 run_dump_test "protected2"
212 run_dump_test "protected2-l1om"
213 run_dump_test "protected3"
214 run_dump_test "protected3-l1om"
215 run_dump_test "protected4"
216 run_dump_test "protected5"
217 run_dump_test "tlsle1"
218 run_dump_test "tlspie1"
219 run_dump_test "unique1"
220 run_dump_test "nogot1"
221 run_dump_test "nogot2"
222 run_dump_test "discarded1"
223 run_dump_test "pr12718"
224 run_dump_test "pr12921"
225 run_dump_test "pr13947"
226 run_dump_test "pr12570a"
227 run_dump_test "pr12570b"
228 run_dump_test "pr14215"
229 run_dump_test "pr14207"
230
231 if { ![istarget "x86_64-*-linux*"] && ![istarget "x86_64-*-nacl*"]} {
232     return
233 }
234
235 if ![ld_assemble $as "--x32 $srcdir/$subdir/start.s" tmpdir/startx32.o] {
236     unresolved "Build ILP32 start.o"
237     return
238 }
239
240 if ![ld_assemble $as "--32 $srcdir/$subdir/start.s" tmpdir/start32.o] {
241     unresolved "Build ia32 start.o"
242     return
243 }
244
245 if ![ld_assemble $as "--64 $srcdir/$subdir/start.s" tmpdir/start64.o] {
246     unresolved "Build LP64 start.o"
247     return
248 }
249
250 if ![ld_assemble $as "--x32 $srcdir/$subdir/foo.s" tmpdir/foox32.o] {
251     unresolved "Build ILP32 foo.o"
252     return
253 }
254
255 if ![ld_assemble $as "--32 $srcdir/$subdir/foo.s" tmpdir/foo32.o] {
256     unresolved "Build ia32 foo.o"
257     return
258 }
259
260 if ![ld_assemble $as "--64 $srcdir/$subdir/foo.s" tmpdir/foo64.o] {
261     unresolved "Build LP64 foo.o"
262     return
263 }
264
265 run_dump_test "compressed1"
266 run_dump_test "ilp32-1"
267 run_dump_test "ilp32-2"
268 run_dump_test "ilp32-3"
269 run_dump_test "ilp32-4"
270 run_dump_test "ilp32-4-nacl"
271 run_dump_test "ilp32-5"
272 run_dump_test "ilp32-6"
273 run_dump_test "ilp32-7"
274 run_dump_test "ilp32-8"
275 run_dump_test "ilp32-9"
276 run_dump_test "ilp32-10"
277 run_dump_test "ilp32-11"
278 run_dump_test "ia32-1"
279 run_dump_test "ia32-2"
280 run_dump_test "ia32-3"
281 run_dump_test "lp64-1"
282 run_dump_test "lp64-2"
283 run_dump_test "lp64-3"
284 run_dump_test "pr13082-1a"
285 run_dump_test "pr13082-1b"
286 run_dump_test "pr13082-2a"
287 run_dump_test "pr13082-2b"
288 run_dump_test "pr13082-3a"
289 run_dump_test "pr13082-3b"
290 run_dump_test "pr13082-4a"
291 run_dump_test "pr13082-4b"
292 run_dump_test "pr13082-5a"
293 run_dump_test "pr13082-5b"
294 run_dump_test "pr13082-6a"
295 run_dump_test "pr13082-6b"
296 run_dump_test "lea1a"
297 run_dump_test "lea1b"
298 run_dump_test "lea1c"
299 run_dump_test "lea1d"
300 run_dump_test "lea1e"
301 run_dump_test "lea1f"
302 run_dump_test "lea1g"
303 run_dump_test "lea1h"
304 run_dump_test "lea1i"
305 run_dump_test "lea1j"
306 run_dump_test "lea1k"
307 run_dump_test "lea1l"
308 run_dump_test "mov1a"
309 run_dump_test "mov1b"
310 run_dump_test "mov1c"
311 run_dump_test "mov1d"
312
313 # Must be native with the C compiler
314 if { [isnative] && [which $CC] != 0 } {
315     run_cc_link_tests {
316         {"Helper X32 DSO from x86-64 object" "" "-m64 -fPIC -g"
317          {simple.c} {} "libsimple.a"}
318     }
319
320     set convertx32 "$objcopy -O elf32-x86-64 tmpdir/simple.o tmpdir/simple-x32.o"
321     send_log "$convertx32\n"
322     set got [remote_exec host "$convertx32"]
323     if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
324         send_log "$got\n"
325         fail "Convert x86-64 object to x32"
326         return
327     }
328
329     run_ld_link_tests {
330         {"X32 DSO from x86-64 object"
331          "-shared -melf32_x86_64 tmpdir/simple-x32.o" "" "--x32"
332         {dummy.s} {{readelf {-s --wide} x86-64-x32.rd}} "x86-64-x32"}
333     }
334
335     run_cc_link_tests [list \
336         [list \
337             "Build copyreloc-lib.so" \
338             "-shared" \
339             "-fPIC" \
340             { copyreloc-lib.c } \
341             {} \
342             "copyreloc-lib.so" \
343         ] \
344         [list \
345             "Build copyreloc-main with PIE without -fPIE (1)" \
346             "tmpdir/copyreloc-lib.so -pie" \
347             "" \
348             { copyreloc-main.S } \
349             {{readelf {-Wr} copyreloc-main1.rd}} \
350             "copyreloc-main" \
351         ] \
352         [list \
353             "Build copyreloc-main with PIE without -fPIE (2)" \
354             "tmpdir/copyreloc-lib.so -pie" \
355             "" \
356             { copyreloc-main.S } \
357             {{readelf {-Wr} copyreloc-main2.rd}} \
358             "copyreloc-main" \
359         ] \
360         [list \
361             "Build pr17689.so" \
362             "-shared" \
363             "-fPIC" \
364             { pr17689a.c } \
365             {} \
366             "pr17689.so" \
367         ] \
368         [list \
369             "Build pr17689 with PIE without -fPIE" \
370             "tmpdir/pr17689.so -pie" \
371             "" \
372             { pr17689b.S } \
373             {{readelf {-Wr} pr17689.rd}} \
374             "pr17689" \
375         ] \
376     ]
377
378     run_ld_link_exec_tests [] [list \
379         [list \
380             "Run copyreloc-main with PIE without -fPIE" \
381             "tmpdir/copyreloc-lib.so -pie" \
382             "" \
383             { copyreloc-main.S } \
384             "copyreloc-main" \
385             "copyreloc-main.out" \
386         ] \
387         [list \
388             "Run pr17689 with PIE without -fPIE" \
389             "tmpdir/pr17689.so -pie" \
390             "" \
391             { pr17689b.S } \
392             "pr17689" \
393             "pr17689.out" \
394         ] \
395     ]
396 }