Support -z relro on metag
[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 native system. FIXME: Strictly speaking this
41 # is not true, we just need to know how to create a fully
42 # linked executable, including the C and Z libraries, using
43 # the linker that is under test.
44 if ![isnative] {
45 verbose "UNIQUE compiled tests not run - not a native toolchain"
46 return
47 }
48
49 # We need a working compiler. (Strictly speaking this is
50 # not true, we could use target specific assembler files).
51 if { [which $CC] == 0 } {
52 verbose "UNIQUE compiled tests not run - no compiler available"
53 return
54 }
55
56 # A procedure to check the OS/ABI field in the ELF header of a binary file.
57 proc check_osabi { binary_file expected_osabi } {
58 global READELF
59 global READELFFLAGS
60
61 catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
62
63 if ![string match "" $got] then {
64 verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
65 return 0
66 }
67
68 if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
69 [file_contents readelf.out] nil osabi] } {
70 verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
71 return 0
72 }
73
74 if { $osabi == $expected_osabi } {
75 return 1
76 }
77
78 verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
79
80 return 0
81 }
82
83 # A procedure to confirm that a file contains the UNIQUE symbol.
84 # Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
85 proc contains_unique_symbol { binary_file } {
86 global READELF
87 global READELFFLAGS
88
89 catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
90
91 if ![string match "" $got] then {
92 verbose "proc contains_unique_symbol: Readelf produced unexpected out processing $binary_file: $got"
93 return -1
94 }
95
96 # Look for a line like this:
97 # 54: 0000000000400474 4 OBJECT UNIQUE DEFAULT 13 a
98
99 if { ![regexp ".*\[ \]*OBJECT\[ \]+UNIQUE\[ \]+DEFAULT\[ \]+\[UND0-9\]+\[ \]+\[ab\]\n" [file_contents readelf.out]] } {
100 return 0
101 }
102
103 return 1
104 }
105
106 set fails 0
107
108 # Create object file containing unique symbol.
109 if ![ld_compile "$CC -c" "$srcdir/$subdir/unique.s" "tmpdir/unique.o"] {
110 fail "Could not create a unique object"
111 set fails [expr $fails + 1]
112 }
113
114 # Create object file NOT containing unique symbol.
115 if ![ld_compile "$CC -c" "$srcdir/$subdir/unique_empty.s" "tmpdir/unique_empty.o"] {
116 fail "Could not create a non-unique object"
117 set fails [expr $fails + 1]
118 }
119
120 # Create pic object file containing unique symbol.
121 if ![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/unique_shared.s" "tmpdir/unique_shared.o"] {
122 fail "Could not create a pic unique object"
123 set fails [expr $fails + 1]
124 }
125
126 # Create executable containing unique symbol.
127 if ![default_ld_link $ld "tmpdir/unique_prog" "tmpdir/unique.o"] {
128 fail "Could not link a unique executable"
129 set fails [expr $fails + 1]
130 }
131
132 # Create shared library containing unique symbol.
133 if ![ld_simple_link $ld "tmpdir/libunique_shared.so" "-shared tmpdir/unique_shared.o"] {
134 fail "Could not create a shared library containing an unique symbol"
135 set fails [expr $fails + 1]
136 }
137
138 # Create executable NOT containing unique symbol linked against library.
139 if ![default_ld_link $ld "tmpdir/unique_shared_prog" "-Ltmpdir tmpdir/unique_empty.o -Bdynamic -lunique_shared -rpath ./tmpdir"] {
140 fail "Could not link a dynamic executable"
141 set fails [expr $fails + 1]
142 }
143
144 # Create shared library containing unique symbol with reference.
145 if ![ld_simple_link $ld "tmpdir/libunique_shared_ref.so" "-shared tmpdir/unique_shared.o tmpdir/unique_empty.o"] {
146 fail "Could not create a shared library containing an unique symbol with reference"
147 set fails [expr $fails + 1]
148 }
149
150 if { $fails != 0 } {
151 return
152 }
153
154 # Check the object file.
155 if {! [check_osabi tmpdir/unique.o {UNIX - GNU}]} {
156 fail "Object containing unique does not have an OS/ABI field of GNU"
157 set fails [expr $fails + 1]
158 }
159
160 if {[contains_unique_symbol tmpdir/unique.o] != 1} {
161 fail "Object containing unique does not contain an UNIQUE symbol"
162 set fails [expr $fails + 1]
163 }
164
165 if { $fails == 0 } {
166 pass "Checking unique object"
167 }
168
169 # Check the executable.
170 if {! [check_osabi tmpdir/unique_prog {UNIX - GNU}]} {
171 fail "Executable containing unique does not have an OS/ABI field of GNU"
172 set fails [expr $fails + 1]
173 }
174
175 if {[contains_unique_symbol tmpdir/unique_prog] != 1} {
176 fail "Executable containing unique does not contain an UNIQUE symbol"
177 set fails [expr $fails + 1]
178 }
179
180 if { $fails == 0 } {
181 pass "Checking unique executable"
182 }
183
184 # Check the empty object file.
185 if {! [check_osabi tmpdir/unique_empty.o {UNIX - System V}]} {
186 fail "Object NOT containing unique does not have an OS/ABI field of System V"
187 set fails [expr $fails + 1]
188 }
189
190 if {[contains_unique_symbol tmpdir/unique_empty.o] == 1} {
191 fail "Object NOT containing unique does contain an UNIQUE symbol"
192 set fails [expr $fails + 1]
193 }
194
195 if { $fails == 0 } {
196 pass "Checking empty unique object"
197 }
198
199 # Check the unique PIC file.
200 if {! [check_osabi tmpdir/unique_shared.o {UNIX - GNU}]} {
201 fail "PIC Object containing unique does not have an OS/ABI field of GNU"
202 set fails [expr $fails + 1]
203 }
204
205 if {[contains_unique_symbol tmpdir/unique_shared.o] != 1} {
206 fail "PIC Object containing unique does not contain an UNIQUE symbol"
207 set fails [expr $fails + 1]
208 }
209
210 if { $fails == 0 } {
211 pass "Checking unique PIC object"
212 }
213
214 # Check the unique shared library.
215 if {! [check_osabi tmpdir/libunique_shared.so {UNIX - GNU}]} {
216 fail "Shared library containing unique does not have an OS/ABI field of GNU"
217 set fails [expr $fails + 1]
218 }
219
220 if {[contains_unique_symbol tmpdir/libunique_shared.so] != 1} {
221 fail "Shared library containing unique does not contain an UNIQUE symbol"
222 set fails [expr $fails + 1]
223 }
224
225 # Check the unique shared library with reference.
226 if {! [check_osabi tmpdir/libunique_shared_ref.so {UNIX - GNU}]} {
227 fail "Shared library containing unique with reference does not have an OS/ABI field of GNU"
228 set fails [expr $fails + 1]
229 }
230
231 if {[contains_unique_symbol tmpdir/libunique_shared_ref.so] != 1} {
232 fail "Shared library containing unique with reference does not contain an UNIQUE symbol"
233 set fails [expr $fails + 1]
234 }
235
236 if { $fails == 0 } {
237 pass "Checking unique PIC object"
238 }
239
240 # Check the empty executable linked against unique shared library.
241 if {! [check_osabi tmpdir/unique_shared_prog {UNIX - System V}]} {
242 fail "Executable NOT containing unique does not have an OS/ABI field of System V"
243 set fails [expr $fails + 1]
244 }
245
246 if {[contains_unique_symbol tmpdir/unique_shared_prog] == 1} {
247 fail "Executable NOT containing unique does contain an UNIQUE symbol"
248 set fails [expr $fails + 1]
249 }
250
251 if { $fails == 0 } {
252 pass "Checking shared empty executable"
253 }
254
255 # Clean up, unless we are being verbose, in which case we leave the files available.
256 if { $verbose < 1 } {
257 remote_file host delete "tmpdir/unique_empty.o"
258 remote_file host delete "tmpdir/unique.o"
259 remote_file host delete "tmpdir/unique_shared.o"
260 remote_file host delete "tmpdir/libunique_shared.so"
261 remote_file host delete "tmpdir/libunique_shared_ref.so"
262 remote_file host delete "tmpdir/unique_prog"
263 remote_file host delete "tmpdir/unique_shared_prog"
264 }
This page took 0.037034 seconds and 5 git commands to generate.