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