i386: Allow copy relocs for building PIE
[external/binutils.git] / ld / testsuite / ld-i386 / i386.exp
1 # Expect script for ld-i386 tests
2 #   Copyright (C) 2002-2015 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 i386 linking; all types of relocs.  This tests the assembler and
23 # tools like objdump as well as the linker.
24
25 if {[istarget "i?86-*-vxworks"]} {
26     set i386tests {
27         {"VxWorks shared library test 1" "-shared -Tvxworks1.ld" ""
28          "" {vxworks1-lib.s}
29          {{readelf --relocs vxworks1-lib.rd} {objdump -dr vxworks1-lib.dd}
30           {readelf --symbols vxworks1-lib.nd} {readelf -d vxworks1-lib.td}}
31          "libvxworks1.so"}
32         {"VxWorks executable test 1 (dynamic)" \
33          "tmpdir/libvxworks1.so -Tvxworks1.ld -q --force-dynamic" ""
34          "" {vxworks1.s}
35          {{readelf --relocs vxworks1.rd} {objdump -dr vxworks1.dd}}
36          "vxworks1"}
37         {"VxWorks executable test 2 (dynamic)" \
38          "-Tvxworks1.ld -q --force-dynamic" ""
39          "" {vxworks2.s}
40          {{readelf --segments vxworks2.sd}}
41          "vxworks2"}
42         {"VxWorks executable test 2 (static)"
43          "-Tvxworks1.ld" ""
44          "" {vxworks2.s}
45          {{readelf --segments vxworks2-static.sd}}
46          "vxworks2"}
47     }
48     run_ld_link_tests $i386tests
49     run_dump_test "vxworks1-static"
50 }
51
52 if [istarget "*-*-go32*"] {
53     run_ld_link_tests {{"go32 stub" "" "" "" {zero.s} {} "go32stub"}}
54
55     set src "tmpdir/go32stub"
56     set dest "tmpdir/go32stub-copy"
57
58     set test "go32 stub patch the source"
59     set fi [open $src r+]
60     fconfigure $fi -translation binary
61     if {[read $fi 2] != "MZ"} {
62         fail $test
63     } else {
64         pass $test
65         seek $fi 0x40
66         puts -nonewline $fi "objcopy-test-go32stub"
67     }
68     close $fi
69
70     set test "go32 stub objcopy"
71     set status [remote_exec build $OBJCOPY "$OBJCOPYFLAGS $src $dest"]
72     set exec_output [lindex $status 1]
73     set exec_output [prune_warnings $exec_output]
74     if [string match "" $exec_output] then {
75         pass $test
76     } else {
77         send_log "$exec_output\n"
78         verbose "$exec_output" 1
79         fail $test
80     }
81
82     # cmp would compare the whole files and some data after the initial exe
83     # stub could differ.
84     set test "go32 stub comparison after objcopy"
85     set fi [open $src]
86     fconfigure $fi -translation binary
87     set src_stub [read $fi 2048]
88     close $fi
89     set fi [open $dest]
90     fconfigure $fi -translation binary
91     set dest_stub [read $fi 2048]
92     close $fi
93     if {$src_stub == $dest_stub} {
94         pass $test
95     } else {
96         fail $test
97     }
98 }
99
100 if { !([istarget "i?86-*-elf*"]
101        || (([istarget "i?86-*-linux*"]
102             || [istarget "i?86-*-gnu*"])
103            && ![istarget "*-*-*aout*"]
104            && ![istarget "*-*-*oldld*"])
105        || [istarget "i?86-*-nacl*"]
106        || [istarget "x86_64-*-nacl*"]
107        || [istarget "x86_64-*-linux*"]
108        || [istarget "amd64-*-linux*"]) } {
109     return
110 }
111
112 # List contains test-items with 3 items followed by 2 lists:
113 # 0:name 1:ld early options 2:ld late options 3:assembler options
114 # 4:filenames of assembler files 5: action and options. 6: name of output file
115
116 # Actions:
117 # objdump: Apply objdump options on result.  Compare with regex (last arg).
118 # nm: Apply nm options on result.  Compare with regex (last arg).
119 # readelf: Apply readelf options on result.  Compare with regex (last arg).
120
121 set i386tests {
122     {"Helper shared library (basic PLT test)"
123       "-shared -melf_i386" "" "--32" {pltlib.s} {} "libpltlib.so"}
124     {"basic PLT generation (non-PIC)"
125      "-melf_i386 tmpdir/libpltlib.so" "" "--32" {plt.s}
126      {{objdump -drj.plt plt.pd}} "plt"}
127     {"basic PLT generation (PIC)"
128      "-shared -melf_i386 tmpdir/libpltlib.so" "" "--32" {plt-pic.s}
129      {{objdump -drj.plt plt-pic.pd}} "libplt-pic.so"}
130     {"TLS -fpic -shared transitions"
131      "-shared -melf_i386 --no-ld-generated-unwind-info" ""
132      "--32" {tlspic1.s tlspic2.s}
133      {{readelf -Ssrl tlspic.rd} {objdump -drj.text tlspic.dd}
134       {objdump -sj.got tlspic.sd} {objdump -sj.tdata tlspic.td}}
135       "libtlspic.so"}
136     {"TLS descriptor -fpic -shared transitions"
137      "-shared -melf_i386 --no-ld-generated-unwind-info" ""
138      "--32" {tlsdesc.s tlspic2.s}
139      {{readelf -Ssrl tlsdesc.rd} {objdump -drj.text tlsdesc.dd}
140       {objdump "-s -j.got -j.got.plt" tlsdesc.sd} {objdump -sj.tdata tlsdesc.td}}
141       "libtlsdesc.so"}
142     {"Helper shared library" "-shared -melf_i386" ""
143      "--32" {tlslib.s} {} "libtlslib.so"}
144     {"TLS -fpic and -fno-pic exec transitions"
145      "-melf_i386 tmpdir/libtlslib.so --no-ld-generated-unwind-info" ""
146      "--32" {tlsbinpic.s tlsbin.s}
147      {{readelf -Ssrl tlsbin.rd} {objdump -drj.text tlsbin.dd}
148       {objdump -sj.got tlsbin.sd} {objdump -sj.tdata tlsbin.td}}
149       "tlsbin"}
150     {"TLS descriptor -fpic and -fno-pic exec transitions"
151      "-melf_i386 tmpdir/libtlslib.so --no-ld-generated-unwind-info" ""
152      "--32" {tlsbindesc.s tlsbin.s}
153      {{readelf -Ssrl tlsbindesc.rd} {objdump -drj.text tlsbindesc.dd}
154       {objdump -sj.got tlsbindesc.sd} {objdump -sj.tdata tlsbindesc.td}}
155       "tlsbindesc"}
156     {"TLS -fno-pic -shared"
157      "-shared -melf_i386 --no-ld-generated-unwind-info" ""
158      "--32" {tlsnopic1.s tlsnopic2.s}
159      {{readelf -Ssrl tlsnopic.rd} {objdump -drj.text tlsnopic.dd}
160       {objdump -sj.got tlsnopic.sd}} "libtlsnopic.so"}
161     {"TLS with global dynamic and descriptors"
162      "-shared -melf_i386 --no-ld-generated-unwind-info" ""
163      "--32" {tlsgdesc.s}
164      {{readelf -Ssrl tlsgdesc.rd} {objdump -drj.text tlsgdesc.dd}}
165       "libtlsgdesc.so"}
166     {"TLS in debug sections" "-melf_i386" ""
167      "--32" {tlsg.s}
168      {{objdump -sj.debug_foobar tlsg.sd}} "tlsg"}
169     {"TLS @indntpoff with %eax" "-melf_i386" "" "--32" {tlsindntpoff.s}
170      {{objdump -drj.text tlsindntpoff.dd}} "tlsindntpoff"}
171     {"Reloc section order" "-shared -melf_i386 -z nocombreloc" "" "--32"
172      {reloc.s} {{objdump -hw reloc.d}} "reloc.so"}
173     {"Basic --emit-relocs support" "-shared -melf_i386 --emit-relocs" "" "--32"
174      {emit-relocs.s} {{readelf --relocs emit-relocs.rd}} "emit-relocs.so"}
175     {"-z combreloc relocation sections" "-shared -melf_i386 -z combreloc" ""
176      "--32" {combreloc.s} {{readelf -r combreloc.d}} "combreloc.so"}
177     {"TLS GD->LE transition" "-melf_i386" ""
178      "--32" {tlsgd1.s}
179      {{objdump -dwr tlsgd1.dd}} "tlsgd1"}
180     {"TLS LD->LE transition" "-melf_i386" ""
181      "--32" {tlsld1.s}
182      {{objdump -dwr tlsld1.dd}} "tlsld1"}
183     {"TLS IE->LE transition" "-melf_i386" ""
184      "--32" {tlsie1.s}
185      {{objdump -dwr tlsie1.dd}} "tlsie1"}
186     {"PR ld/17313 (1)" "-melf_i386" ""
187      "--32" {zero.s} {} ""}
188     {"PR ld/17313 (2)" "-melf_i386 -shared --just-symbols=tmpdir/zero.o" ""
189      "--32" {lea1.s} {} "libpr17313.so"}
190     {"PR ld/17306 (1)" "-melf_i386" ""
191      "--32" {pr17306b.s} {} ""}
192     {"PR ld/17306 (2)" "-melf_i386 -shared -Bsymbolic --just-symbols=tmpdir/pr17306b.o" ""
193      "--32" {pr17306a.s} {} "libpr17306.so"}
194     {"PR ld/17709 (1)" "-melf_i386 -shared" ""
195      "--32" {pr17709a.s} {} "libpr17709.so"}
196     {"PR ld/17709 (2)" "-melf_i386 tmpdir/libpr17709.so" ""
197      "--32" {pr17709b.s} {{readelf -r pr17709.rd}} "pr17709"}
198 }
199
200 # So as to avoid rewriting every last test case here in a nacl variant,
201 # we use black magic to massage the generic cases into nacl-variant cases.
202 if [istarget "*-*-nacl*"] {
203     # Change all the -melf_i386 to -melf_i386_nacl so linking can succeed.
204     regsub -all elf_i386 $i386tests elf_i386_nacl i386tests
205
206     # Same, applied to all the run_dump_test cases.
207     set options_regsub(ld) {elf_i386 elf_i386_nacl}
208
209     # The section/segment layout differs too much for the vanilla
210     # readelf output files to match.  So massage the cases so that
211     # they refer to a foo-nacl.rd file instead of a foo.rd file.
212     regsub -all {([a-z0-9]+)\.rd} $i386tests {\1-nacl.rd} i386tests
213
214     # Likewise for PLTs.
215     regsub -all -- {([a-z0-9]+)\.pd} $i386tests {\1-nacl.pd} i386tests
216 }
217
218 run_ld_link_tests $i386tests
219
220 run_dump_test "abs"
221 run_dump_test "pcrel8"
222 run_dump_test "pcrel16"
223 run_dump_test "pcrel16abs"
224 run_dump_test "alloc"
225 run_dump_test "warn1"
226 run_dump_test "tlsgd2"
227 run_dump_test "tlsie2"
228 run_dump_test "tlsie3"
229 run_dump_test "tlsie4"
230 run_dump_test "tlsie5"
231 run_dump_test "hidden1"
232 run_dump_test "hidden2"
233 run_dump_test "hidden3"
234 run_dump_test "protected1"
235 run_dump_test "protected2"
236 run_dump_test "protected3"
237 run_dump_test "protected4"
238 run_dump_test "protected5"
239 run_dump_test "protected6a"
240 run_dump_test "protected6b"
241 run_dump_test "tlspie1"
242 run_dump_test "tlspie2"
243 run_dump_test "nogot1"
244 run_dump_test "nogot2"
245 run_dump_test "discarded1"
246 run_dump_test "pr12718"
247 run_dump_test "pr12921"
248 run_dump_test "pr12570a"
249 run_dump_test "pr12570b"
250 run_dump_test "lea1a"
251 run_dump_test "lea1b"
252 run_dump_test "lea1c"
253 run_dump_test "lea1d"
254 run_dump_test "lea1e"
255 run_dump_test "lea1f"
256 run_dump_test "mov1a"
257 run_dump_test "mov1b"
258
259 if { !([istarget "i?86-*-linux*"]
260        || [istarget "i?86-*-gnu*"]
261        || [istarget "i?86-*-nacl*"]
262        || [istarget "x86_64-*-nacl*"]
263        || [istarget "x86_64-*-linux*"]) } {
264     return
265 }
266
267 run_dump_test "compressed1"
268 run_dump_test "pr12627"
269 run_dump_test "pr13302"
270 run_dump_test "pr14215"
271 run_dump_test "pr17057"
272 run_dump_test "pr17935-1"
273 run_dump_test "pr17935-2"
274
275 # Must be Linux native with the C compiler
276 if { [isnative]
277      && [istarget "i?86-*-linux*"]
278      && [which $CC] != 0 } {
279     run_cc_link_tests [list \
280         [list \
281             "Build plt-lib.so" \
282             "-shared" \
283             "-fPIC" \
284             { plt-lib.c } \
285             {} \
286             "libplt-lib.so" \
287         ] \
288         [list \
289             "Build libplt-main1.a" \
290             "" \
291             "-fPIC" \
292             { plt-main1.c } \
293             {{readelf {-Wr} plt-main1.rd}} \
294             "libplt-main1.a" \
295         ] \
296         [list \
297             "Build libplt-main2.a" \
298             "" \
299             "-fPIC" \
300             { plt-main2.c } \
301             {{readelf {-Wr} plt-main2.rd}} \
302             "libplt-main2.a" \
303         ] \
304         [list \
305             "Build libplt-main3.a" \
306             "" \
307             "-fPIC" \
308             { plt-main3.c } \
309             {{readelf {-Wr} plt-main3.rd}} \
310             "libplt-main3.a" \
311         ] \
312         [list \
313             "Build libplt-main4.a" \
314             "" \
315             "-fPIC" \
316             { plt-main4.c } \
317             {{readelf {-Wr} plt-main4.rd}} \
318             "libplt-main4.a" \
319         ] \
320         [list \
321             "Build plt-main" \
322             "tmpdir/plt-main1.o tmpdir/plt-main2.o tmpdir/plt-main3.o \
323              tmpdir/plt-main4.o tmpdir/libplt-lib.so" \
324             "" \
325             { plt-main5.c } \
326             {{readelf {-Wr} plt-main.rd}} \
327             "plt-main" \
328         ] \
329         [list \
330             "Build plt-main with PIE" \
331             "tmpdir/plt-main1.o tmpdir/plt-main2.o tmpdir/plt-main3.o \
332              tmpdir/plt-main4.o tmpdir/libplt-lib.so -pie" \
333             "-fPIC" \
334             { plt-main5.c } \
335             {{readelf {-Wr} plt-main.rd}} \
336             "plt-main" \
337         ] \
338         [list \
339             "Build copyreloc-lib.so" \
340             "-shared" \
341             "-fPIC" \
342             { copyreloc-lib.c } \
343             {} \
344             "copyreloc-lib.so" \
345         ] \
346         [list \
347             "Build copyreloc-main with PIE and GOTOFF (1)" \
348             "tmpdir/copyreloc-lib.so -pie" \
349             "" \
350             { copyreloc-main.S } \
351             {{readelf {-Wr} copyreloc-main1.rd}} \
352             "copyreloc-main" \
353         ] \
354         [list \
355             "Build copyreloc-main with PIE and GOTOFF (2)" \
356             "tmpdir/copyreloc-lib.so -pie" \
357             "" \
358             { copyreloc-main.S } \
359             {{readelf {-Wr} copyreloc-main2.rd}} \
360             "copyreloc-main" \
361         ] \
362         [list \
363             "Build pr17689.so" \
364             "-shared" \
365             "-fPIC" \
366             { pr17689a.c } \
367             {} \
368             "pr17689.so" \
369         ] \
370         [list \
371             "Build pr17689ver.so" \
372             "-shared -Wl,--version-script,pr17689a.t" \
373             "-fPIC" \
374             { pr17689a.c } \
375             {} \
376             "pr17689ver.so" \
377         ] \
378         [list \
379             "Build pr17689.a" \
380             "" \
381             "" \
382             { pr17689b.S } \
383             {} \
384             "pr17689.a" \
385         ] \
386         [list \
387             "Build pr17689 with PIE and GOTOFF" \
388             "tmpdir/pr17689b.o tmpdir/pr17689.so -pie" \
389             "" \
390             { dummy.c } \
391             {{readelf {-Wr} pr17689.rd}} \
392             "pr17689" \
393         ] \
394         [list \
395             "Build pr17689ver with PIE and GOTOFF" \
396             "tmpdir/pr17689b.o tmpdir/pr17689ver.so -pie" \
397             "" \
398             { dummy.c } \
399             {{readelf {-Wr} pr17689ver.rd}} \
400             "pr17689ver" \
401         ] \
402         [list \
403             "Build pr17827 with PIE and GOTOFF" \
404             "tmpdir/pr17689b.o tmpdir/pr17689.so -pie" \
405             "" \
406             { dummy.c } \
407             {{readelf {-Wr} pr17827.rd}} \
408             "pr17827" \
409         ] \
410     ]
411
412     run_ld_link_exec_tests [] [list \
413         [list \
414             "Run plt-main" \
415             "tmpdir/plt-main1.o tmpdir/plt-main2.o tmpdir/plt-main3.o \
416              tmpdir/plt-main4.o tmpdir/libplt-lib.so" \
417             "" \
418             { plt-main5.c } \
419             "plt-main" \
420             "plt-main.out" \
421         ] \
422         [list \
423             "Run plt-main with PIE" \
424             "tmpdir/plt-main1.o tmpdir/plt-main2.o tmpdir/plt-main3.o \
425              tmpdir/plt-main4.o tmpdir/libplt-lib.so -pie" \
426             "" \
427             { plt-main5.c } \
428             "plt-main-pie" \
429             "plt-main.out" \
430             "-fPIC" \
431         ] \
432         [list \
433             "Run copyreloc-main with PIE and GOTOFF" \
434             "tmpdir/copyreloc-lib.so -pie" \
435             "" \
436             { copyreloc-main.S } \
437             "copyreloc-main" \
438             "copyreloc-main.out" \
439         ] \
440         [list \
441             "Run pr17689 with PIE and GOTOFF" \
442             "tmpdir/pr17689b.o tmpdir/pr17689.so -pie" \
443             "" \
444             { dummy.c } \
445             "pr17689" \
446             "pr17689.out" \
447         ] \
448         [list \
449             "Run pr17689ver with PIE and GOTOFF" \
450             "tmpdir/pr17689b.o tmpdir/pr17689ver.so -pie" \
451             "" \
452             { dummy.c } \
453             "pr17689ver" \
454             "pr17689.out" \
455         ] \
456     ]
457 }