1# Expect script for linker support of STB_GNU_UNIQUE symbols
2#
3#   Copyright (C) 2009-2020 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.
28if { ![is_elf_format] } {
29    return
30}
31
32# Require STB_GNU_UNIQUE support with OSABI set to GNU.
33if { ![supports_gnu_unique] || [istarget tic6x-*-*] } {
34    verbose "UNIQUE tests not run - target does not support UNIQUE"
35    return
36}
37
38set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
39foreach t $test_list {
40    # We need to strip the ".d", but can leave the dirname.
41    verbose [file rootname $t]
42    run_dump_test [file rootname $t]
43}
44
45# We need a working compiler.  (Strictly speaking this is
46# not true, we could use target specific assembler files).
47if { ![check_compiler_available] } {
48    verbose "UNIQUE compiled tests not run - no compiler available"
49    return
50}
51
52# A procedure to check the OS/ABI field in the ELF header of a binary file.
53proc check_osabi { binary_file expected_osabi } {
54    global READELF
55    global READELFFLAGS
56
57    catch "exec $READELF $READELFFLAGS --file-header $binary_file > readelf.out" got
58
59    if ![string match "" $got] then {
60	verbose "proc check_osabi: Readelf produced unexpected out processing $binary_file: $got"
61	return 0
62    }
63
64    if { ![regexp "\n\[ \]*OS/ABI:\[ \]*(.+)\n\[ \]*ABI" \
65	   [file_contents readelf.out] nil osabi] } {
66	verbose "proc check_osabi: Readelf failed to extract an ELF header from $binary_file"
67	return 0
68    }
69
70    if { $osabi == $expected_osabi } {
71	return 1
72    }
73
74    verbose "Expected OSABI: $expected_osabi, Obtained osabi: $osabi"
75
76    return 0
77}
78
79# A procedure to confirm that a file contains the UNIQUE symbol.
80# Returns -1 upon error, 0 if the symbol was not found and 1 if it was found.
81proc contains_unique_symbol { binary_file } {
82    global READELF
83    global READELFFLAGS
84
85    catch "exec $READELF $READELFFLAGS --symbols $binary_file > readelf.out" got
86
87    if ![string match "" $got] then {
88	verbose "proc contains_unique_symbol: Readelf produced unexpected out processing $binary_file: $got"
89	return -1
90    }
91
92    # Look for a line like this:
93    #    54: 0000000000400474     4 OBJECT  UNIQUE DEFAULT   13 a
94
95    if { ![regexp ".*\[ \]*OBJECT\[ \]+UNIQUE\[ \]+DEFAULT\[ \]+\[UND0-9\]+\[ \]+\[ab\]_val\n" [file_contents readelf.out]] } {
96	return 0
97    }
98
99    return 1
100}
101
102set fails 0
103
104# Create object file containing unique symbol.
105if ![ld_compile "$CC -c" "$srcdir/$subdir/unique.s" "tmpdir/unique.o"] {
106    fail "Could not create a unique object"
107    set fails [expr $fails + 1]
108}
109
110# Create object file NOT containing unique symbol.
111if ![ld_compile "$CC -c" "$srcdir/$subdir/unique_empty.s" "tmpdir/unique_empty.o"] {
112    fail "Could not create a non-unique object"
113    set fails [expr $fails + 1]
114}
115
116# When using GCC as the linker driver, we need to specify board cflags when
117# linking because cflags may contain linker options.  For example when linker
118# options are included in GCC spec files then we need the -specs option.
119if [board_info [target_info name] exists cflags] {
120  set board_cflags " [board_info [target_info name] cflags]"
121} else {
122  set board_cflags ""
123}
124
125# Create executable containing unique symbol.
126if ![ld_link "$CC $NOPIE_LDFLAGS $board_cflags" "tmpdir/unique_prog" "tmpdir/unique.o"] {
127    fail "Could not link a unique executable"
128    set fails [expr $fails + 1]
129}
130
131if { $fails != 0 } {
132    return
133}
134
135# Check the object file.
136if {! [check_osabi tmpdir/unique.o {UNIX - GNU}]} {
137    fail "Object containing unique does not have an OS/ABI field of GNU"
138    set fails [expr $fails + 1]
139}
140
141if {[contains_unique_symbol tmpdir/unique.o] != 1} {
142    fail "Object containing unique does not contain an UNIQUE symbol"
143    set fails [expr $fails + 1]
144}
145
146if { $fails == 0 } {
147  pass "Checking unique object"
148}
149
150# Check the executable.
151if {! [check_osabi tmpdir/unique_prog {UNIX - GNU}]} {
152    fail "Executable containing unique does not have an OS/ABI field of GNU"
153    set fails [expr $fails + 1]
154}
155
156if {[contains_unique_symbol tmpdir/unique_prog] != 1} {
157    fail "Executable containing unique does not contain an UNIQUE symbol"
158    set fails [expr $fails + 1]
159}
160
161if { $fails == 0 } {
162  pass "Checking unique executable"
163}
164
165# Check the empty object file.
166switch -glob $target_triplet {
167    hppa*-*-linux* { set expected_none {UNIX - GNU} }
168    default { set expected_none {UNIX - System V} }
169}
170if {! [check_osabi tmpdir/unique_empty.o $expected_none]} {
171    fail "Object NOT containing unique does not have an OS/ABI field of $expected_none"
172    set fails [expr $fails + 1]
173}
174
175if {[contains_unique_symbol tmpdir/unique_empty.o] == 1} {
176    fail "Object NOT containing unique does contain an UNIQUE symbol"
177    set fails [expr $fails + 1]
178}
179
180if { $fails == 0 } {
181  pass "Checking empty unique object"
182}
183
184# ------------------------------------------------------------------------------
185# Only shared library tests below.
186# ------------------------------------------------------------------------------
187
188if { ![check_shared_lib_support] } {
189    return
190}
191
192# Create pic object file containing unique symbol.
193if {![ld_compile "$CC -c -fPIC" "$srcdir/$subdir/unique_shared.s" "tmpdir/unique_shared.o"] } {
194    fail "Could not create a pic unique object"
195    set fails [expr $fails + 1]
196}
197
198# Create shared library containing unique symbol.
199if {![ld_link $ld "tmpdir/libunique_shared.so" "-shared tmpdir/unique_shared.o"] } {
200    fail "Could not create a shared library containing an unique symbol"
201    set fails [expr $fails + 1]
202}
203
204# Create executable NOT containing unique symbol linked against library.
205if {![ld_link "$CC $NOPIE_LDFLAGS $board_cflags" "tmpdir/unique_shared_prog" "-Ltmpdir tmpdir/unique_empty.o -Wl,-Bdynamic,-rpath=./tmpdir -lunique_shared"] } {
206    fail "Could not link a dynamic executable"
207    set fails [expr $fails + 1]
208}
209
210# Create shared library containing unique symbol with reference.
211if {![ld_link $ld "tmpdir/libunique_shared_ref.so" "-shared -z notext tmpdir/unique_shared.o tmpdir/unique_empty.o"] } {
212    fail "Could not create a shared library containing an unique symbol with reference"
213    set fails [expr $fails + 1]
214}
215
216if { $fails != 0 } {
217    return
218}
219
220# Check the unique PIC file.
221if {! [check_osabi tmpdir/unique_shared.o {UNIX - GNU}]} {
222    fail "PIC Object containing unique does not have an OS/ABI field of GNU"
223    set fails [expr $fails + 1]
224}
225
226if {[contains_unique_symbol tmpdir/unique_shared.o] != 1} {
227    fail "PIC Object containing unique does not contain an UNIQUE symbol"
228    set fails [expr $fails + 1]
229}
230
231if { $fails == 0 } {
232  pass "Checking unique PIC object 1"
233}
234
235# Check the unique shared library.
236if {! [check_osabi tmpdir/libunique_shared.so {UNIX - GNU}]} {
237    fail "Shared library containing unique does not have an OS/ABI field of GNU"
238    set fails [expr $fails + 1]
239}
240
241if {[contains_unique_symbol tmpdir/libunique_shared.so] != 1} {
242    fail "Shared library containing unique does not contain an UNIQUE symbol"
243    set fails [expr $fails + 1]
244}
245
246# Check the unique shared library with reference.
247if {! [check_osabi tmpdir/libunique_shared_ref.so {UNIX - GNU}]} {
248    fail "Shared library containing unique with reference does not have an OS/ABI field of GNU"
249    set fails [expr $fails + 1]
250}
251
252if {[contains_unique_symbol tmpdir/libunique_shared_ref.so] != 1} {
253    fail "Shared library containing unique with reference does not contain an UNIQUE symbol"
254    set fails [expr $fails + 1]
255}
256
257if { $fails == 0 } {
258  pass "Checking unique PIC object 2"
259}
260
261# Check the empty executable linked against unique shared library.
262if {! [check_osabi tmpdir/unique_shared_prog $expected_none]} {
263    fail "Executable NOT containing unique does not have an OS/ABI field of $expected_none"
264    set fails [expr $fails + 1]
265}
266
267if {[contains_unique_symbol tmpdir/unique_shared_prog] == 1} {
268    fail "Executable NOT containing unique does contain an UNIQUE symbol"
269    set fails [expr $fails + 1]
270}
271
272if { $fails == 0 } {
273  pass "Checking shared empty executable"
274}
275