Run more ld tests when not native
[deliverable/binutils-gdb.git] / ld / testsuite / ld-unique / unique.exp
1 # Expect script for linker support of STB_GNU_UNIQUE symbols
2 #
3 # Copyright (C) 2009-2017 Free Software Foundation, Inc.
4 # Contributed by Red Hat.
5 #
6 # This file is part of the GNU Binutils.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 # MA 02110-1301, USA.
22 #
23 # Written by Nick Clifton <nickc@redhat.com>
24 # Adapted for unique checking by Mark J. Wielaard <mjw@redhat.com>
25
26
27 # Exclude non-ELF targets.
28 if { ![is_elf_format] } {
29 return
30 }
31
32 # Require STB_GNU_UNIQUE support with OSABI set to GNU.
33 if { ![supports_gnu_unique] } {
34 verbose "UNIQUE tests not run - target does not support UNIQUE"
35 return
36 }
37
38 run_dump_test "unique"
39
40 # We need a working compiler. (Strictly speaking this is
41 # not true, we could use target specific assembler files).
42 if { [which $CC] == 0 } {
43 verbose "UNIQUE compiled tests not run - no compiler available"
44 return
45 }
46
47 # A procedure to check the OS/ABI field in the ELF header of a binary file.
48 proc check_osabi { binary_file expected_osabi } {
49 global READELF
50 global READELFFLAGS
51
52 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
53
54 if ![string match "" $got] then {
55 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
56 return 0
57 }
58
59 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
60 [file_contents readelf.out] nil osabi] } {
61 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
62 return 0
63 }
64
65 if { $osabi == $expected_osabi } {
66 return 1
67 }
68
69 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
70
71 return 0
72 }
73
74 # A procedure to confirm that a file contains the UNIQUE symbol.
75 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
76 proc contains_unique_symbol { binary_file } {
77 global READELF
78 global READELFFLAGS
79
80 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
81
82 if ![string match "" $got] then {
83 verbose "proc contains_unique_symbol: Readelf produced unexpected out processing $binary_file: $got"
84 return -1
85 }
86
87 # Look for a line like this:
88 # 54: 0000000000400474 4 OBJECT UNIQUE DEFAULT 13 a
89
90 if { ![regexp ".*\[ \]*OBJECT\[ \]+UNIQUE\[ \]+DEFAULT\[ \]+\[UND0-9\]+\[ \]+\[ab\]\n" [file_contents readelf.out]] } {
91 return 0
92 }
93
94 return 1
95 }
96
97 set fails 0
98
99 # Create object file containing unique symbol.
100 if ![ld_compile "$CC -c" "$srcdir/$subdir/unique.s" "tmpdir/unique.o"] {
101 fail "Could not create a unique object"
102 set fails [expr $fails + 1]
103 }
104
105 # Create object file NOT containing unique symbol.
106 if ![ld_compile "$CC -c" "$srcdir/$subdir/unique_empty.s" "tmpdir/unique_empty.o"] {
107 fail "Could not create a non-unique object"
108 set fails [expr $fails + 1]
109 }
110
111 # Create pic object file containing unique symbol.
112 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/unique_shared.s" "tmpdir/unique_shared.o"] {
113 fail "Could not create a pic unique object"
114 set fails [expr $fails + 1]
115 }
116
117 # Create executable containing unique symbol.
118 if ![ld_link $CC "tmpdir/unique_prog" "tmpdir/unique.o"] {
119 fail "Could not link a unique executable"
120 set fails [expr $fails + 1]
121 }
122
123 # Create shared library containing unique symbol.
124 if ![ld_link $ld "tmpdir/libunique_shared.so" "-shared tmpdir/unique_shared.o"] {
125 fail "Could not create a shared library containing an unique symbol"
126 set fails [expr $fails + 1]
127 }
128
129 # Create executable NOT containing unique symbol linked against library.
130 if ![ld_link $CC "tmpdir/unique_shared_prog" "-Ltmpdir tmpdir/unique_empty.o -Wl,-Bdynamic,-rpath=./tmpdir -lunique_shared"] {
131 fail "Could not link a dynamic executable"
132 set fails [expr $fails + 1]
133 }
134
135 # Create shared library containing unique symbol with reference.
136 if ![ld_link $ld "tmpdir/libunique_shared_ref.so" "-shared tmpdir/unique_shared.o tmpdir/unique_empty.o"] {
137 fail "Could not create a shared library containing an unique symbol with reference"
138 set fails [expr $fails + 1]
139 }
140
141 if { $fails != 0 } {
142 return
143 }
144
145 # Check the object file.
146 if {! [check_osabi tmpdir/unique.o {UNIX - GNU}]} {
147 fail "Object containing unique does not have an OS/ABI field of GNU"
148 set fails [expr $fails + 1]
149 }
150
151 if {[contains_unique_symbol tmpdir/unique.o] != 1} {
152 fail "Object containing unique does not contain an UNIQUE symbol"
153 set fails [expr $fails + 1]
154 }
155
156 if { $fails == 0 } {
157 pass "Checking unique object"
158 }
159
160 # Check the executable.
161 if {! [check_osabi tmpdir/unique_prog {UNIX - GNU}]} {
162 fail "Executable containing unique does not have an OS/ABI field of GNU"
163 set fails [expr $fails + 1]
164 }
165
166 if {[contains_unique_symbol tmpdir/unique_prog] != 1} {
167 fail "Executable containing unique does not contain an UNIQUE symbol"
168 set fails [expr $fails + 1]
169 }
170
171 if { $fails == 0 } {
172 pass "Checking unique executable"
173 }
174
175 # Check the empty object file.
176 case $target_triplet in {
177 { hppa*-*-linux* } { set expected_none {UNIX - GNU} }
178 default { set expected_none {UNIX - System V} }
179 }
180 if {! [check_osabi tmpdir/unique_empty.o $expected_none]} {
181 fail "Object NOT containing unique does not have an OS/ABI field of $expected_none"
182 set fails [expr $fails + 1]
183 }
184
185 if {[contains_unique_symbol tmpdir/unique_empty.o] == 1} {
186 fail "Object NOT containing unique does contain an UNIQUE symbol"
187 set fails [expr $fails + 1]
188 }
189
190 if { $fails == 0 } {
191 pass "Checking empty unique object"
192 }
193
194 # Check the unique PIC file.
195 if {! [check_osabi tmpdir/unique_shared.o {UNIX - GNU}]} {
196 fail "PIC Object containing unique does not have an OS/ABI field of GNU"
197 set fails [expr $fails + 1]
198 }
199
200 if {[contains_unique_symbol tmpdir/unique_shared.o] != 1} {
201 fail "PIC Object containing unique does not contain an UNIQUE symbol"
202 set fails [expr $fails + 1]
203 }
204
205 if { $fails == 0 } {
206 pass "Checking unique PIC object"
207 }
208
209 # Check the unique shared library.
210 if {! [check_osabi tmpdir/libunique_shared.so {UNIX - GNU}]} {
211 fail "Shared library containing unique does not have an OS/ABI field of GNU"
212 set fails [expr $fails + 1]
213 }
214
215 if {[contains_unique_symbol tmpdir/libunique_shared.so] != 1} {
216 fail "Shared library containing unique does not contain an UNIQUE symbol"
217 set fails [expr $fails + 1]
218 }
219
220 # Check the unique shared library with reference.
221 if {! [check_osabi tmpdir/libunique_shared_ref.so {UNIX - GNU}]} {
222 fail "Shared library containing unique with reference does not have an OS/ABI field of GNU"
223 set fails [expr $fails + 1]
224 }
225
226 if {[contains_unique_symbol tmpdir/libunique_shared_ref.so] != 1} {
227 fail "Shared library containing unique with reference does not contain an UNIQUE symbol"
228 set fails [expr $fails + 1]
229 }
230
231 if { $fails == 0 } {
232 pass "Checking unique PIC object"
233 }
234
235 # Check the empty executable linked against unique shared library.
236 if {! [check_osabi tmpdir/unique_shared_prog $expected_none]} {
237 fail "Executable NOT containing unique does not have an OS/ABI field of $expected_none"
238 set fails [expr $fails + 1]
239 }
240
241 if {[contains_unique_symbol tmpdir/unique_shared_prog] == 1} {
242 fail "Executable NOT containing unique does contain an UNIQUE symbol"
243 set fails [expr $fails + 1]
244 }
245
246 if { $fails == 0 } {
247 pass "Checking shared empty executable"
248 }
This page took 0.059522 seconds and 4 git commands to generate.