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