1# Expect script for various indirect symbol tests.
2#   Copyright (C) 2012-2021 Free Software Foundation, Inc.
3#
4# This file is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
17#
18
19#
20# Written by H.J. Lu (hongjiu.lu@intel.com)
21#
22
23# Exclude non-ELF targets.
24
25if ![is_elf_format] {
26    return
27}
28
29# Skip target where -shared is not supported
30
31if ![check_shared_lib_support] {
32    return
33}
34
35# Check if compiler works
36if { ![check_compiler_available] } {
37    return
38}
39
40# Some bare-metal targets don't support shared libs or PIC.
41if { ![run_host_cmd_yesno $CC "-shared -fPIC $srcdir/$subdir/dummy.c -o tmpdir/t.so"] } {
42    return
43}
44
45proc check_link_message { cmd string testname } {
46    send_log "$cmd\n"
47    verbose "$cmd"
48    catch "exec $cmd" exec_output
49    send_log "$exec_output\n"
50    verbose "$exec_output"
51
52    foreach str $string {
53	if [string match "*$str*" $exec_output] {
54	    pass "$testname: $str"
55	} else {
56	    fail "$testname: $str"
57	}
58    }
59}
60
61# Disable LTO for these tests.
62set cc_cmd "$CC"
63if {[check_lto_available]} {
64   append cc_cmd " -fno-lto"
65}
66
67# Disable all sanitizers.
68append cc_cmd " $NOSANITIZE_CFLAGS"
69
70if { ![ld_compile $cc_cmd $srcdir/$subdir/indirect1a.c tmpdir/indirect1a.o]
71     || ![ld_compile $cc_cmd $srcdir/$subdir/indirect1b.c tmpdir/indirect1b.o]
72     || ![ld_compile "$cc_cmd -fPIC" $srcdir/$subdir/indirect2.c tmpdir/indirect2.o]
73     || ![ld_compile $cc_cmd $srcdir/$subdir/indirect3a.c tmpdir/indirect3a.o]
74     || ![ld_compile $cc_cmd $srcdir/$subdir/indirect3b.c tmpdir/indirect3b.o]
75     || ![ld_compile $cc_cmd $srcdir/$subdir/indirect4a.c tmpdir/indirect4a.o]
76     || ![ld_compile $cc_cmd $srcdir/$subdir/indirect4b.c tmpdir/indirect4b.o]
77     || ![ld_compile "$cc_cmd -O2 -fPIC -I../bfd" $srcdir/$subdir/pr18720a.c tmpdir/pr18720a.o]
78     || ![ld_compile $cc_cmd $srcdir/$subdir/pr18720b.c tmpdir/pr18720b.o]
79     || ![ld_compile "$cc_cmd -fPIC" $srcdir/$subdir/pr19553d.c tmpdir/pr19553d.o]
80     || ![ld_compile "$cc_cmd -fPIC" $srcdir/$subdir/pr19553c.c tmpdir/pr19553c.o]
81     || ![ld_compile "$cc_cmd -fPIC" $srcdir/$subdir/pr19553b.c tmpdir/pr19553b.o]
82     || ![ld_compile $cc_cmd $srcdir/$subdir/pr19553a.c tmpdir/pr19553a.o] } {
83    unsupported "Indirect symbol tests"
84    return
85}
86
87set build_tests {
88  {"Build libindirect1c.so"
89   "-shared" "-fPIC"
90   {indirect1c.c} {} "libindirect1c.so"}
91  {"Build libindirect3c.so"
92   "-shared" "-fPIC"
93   {indirect3c.c} {} "libindirect3c.so"}
94  {"Build libindirect4c.so"
95   "-shared" "-fPIC"
96   {indirect4c.c} {} "libindirect4c.so"}
97  {"Build libindirect5.so"
98   "-shared -Wl,--version-script=indirect5.map" "-fPIC"
99   {indirect5b.c} {} "libindirect5.so"}
100  {"Build libpr18720c.so"
101   "-shared" "-fPIC"
102   {pr18720c.c} {} "libpr18720c.so"}
103  {"Build pr18720b1.o"
104   "-r -nostdlib tmpdir/pr18720b.o" ""
105   {dummy.c} {} "pr18720b1.o"}
106  {"Build pr18720a"
107   "tmpdir/pr18720a.o tmpdir/pr18720b.o tmpdir/libpr18720c.so" ""
108   {check-ptr-eq.c} {{readelf {--dyn-syms} pr18720.rd}} "pr18720a"}
109  {"Build libpr19553b.so"
110   "-shared -Wl,--version-script=pr19553.map" "-fPIC"
111   {pr19553b.c} {} "libpr19553b.so"}
112  {"Build libpr19553c.so"
113   "-shared -Wl,--version-script=pr19553.map" "-fPIC"
114   {pr19553c.c} {} "libpr19553c.so"}
115  {"Build libpr19553d.so"
116   "-shared tmpdir/libpr19553c.so" "-fPIC"
117   {pr19553d.c} {} "libpr19553d.so"}
118}
119
120run_cc_link_tests $build_tests
121
122set string ": final link failed: bad value"
123set string1 ": local symbol \`foo\' in tmpdir/indirect1b.o is referenced by DSO"
124
125set testname "Indirect symbol 1a"
126set cmd "$ld -e start -o tmpdir/indirect1 tmpdir/indirect1a.o tmpdir/indirect1b.o tmpdir/libindirect1c.so"
127check_link_message "$cmd" [list $string1 $string] "$testname"
128
129set testname "Indirect symbol 1b"
130set cmd "$ld -e start -o tmpdir/indirect1 tmpdir/indirect1a.o tmpdir/libindirect1c.so tmpdir/indirect1b.o"
131check_link_message "$cmd" [list $string1 $string] "$testname"
132
133set string2 ": no symbol version section for versioned symbol \`foo@FOO\'"
134set testname "Indirect symbol 2"
135set cmd "$ld -shared  -o tmpdir/indirect2.so tmpdir/indirect2.o"
136check_link_message "$cmd" [list $string2] "$testname"
137
138set run_tests {
139    {"Run with libindirect3c.so 1"
140     "-Wl,--no-as-needed tmpdir/indirect3a.o tmpdir/indirect3b.o tmpdir/libindirect3c.so" ""
141     {dummy.c} "indirect3a" "indirect3.out"}
142    {"Run with libindirect3c.so 2"
143     "-Wl,--no-as-needed tmpdir/indirect3a.o tmpdir/libindirect3c.so tmpdir/indirect3b.o" ""
144     {dummy.c} "indirect3b" "indirect3.out"}
145    {"Run with libindirect3c.so 3"
146     "-Wl,--no-as-needed tmpdir/indirect3b.o tmpdir/libindirect3c.so tmpdir/indirect3a.o" ""
147     {dummy.c} "indirect3c" "indirect3.out"}
148    {"Run with libindirect3c.so 4"
149     "-Wl,--no-as-needed tmpdir/libindirect3c.so tmpdir/indirect3b.o tmpdir/indirect3a.o" ""
150     {dummy.c} "indirect3d" "indirect3.out"}
151    {"Run with libindirect4c.so 1"
152     "-Wl,--no-as-needed tmpdir/indirect4a.o tmpdir/indirect4b.o tmpdir/libindirect4c.so" ""
153     {dummy.c} "indirect4a" "indirect4.out"}
154    {"Run with libindirect4c.so 2"
155     "-Wl,--no-as-needed tmpdir/indirect4a.o tmpdir/libindirect4c.so tmpdir/indirect4b.o" ""
156     {dummy.c} "indirect4b" "indirect4.out"}
157    {"Run with libindirect4c.so 3"
158     "-Wl,--no-as-needed tmpdir/indirect4b.o tmpdir/libindirect4c.so tmpdir/indirect4a.o" ""
159     {dummy.c} "indirect4c" "indirect4.out"}
160    {"Run with libindirect4c.so 4"
161     "-Wl,--no-as-needed tmpdir/libindirect4c.so tmpdir/indirect4b.o tmpdir/indirect4a.o" ""
162     {dummy.c} "indirect4d" "indirect4.out"}
163    {"Run indirect5 1"
164     "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libindirect5.so" ""
165     {indirect5a.c} "indirect5a" "indirect5.out" "$NOPIE_CFLAGS"}
166    {"Run indirect5 2"
167     "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/indirect5a.o tmpdir/libindirect5.so" ""
168     {dummy.c} "indirect5b" "indirect5.out" "$NOPIE_CFLAGS"}
169    {"Run indirect6 1"
170     "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/libindirect5.so" ""
171     {indirect6a.c} "indirect6a" "indirect5.out" "$NOPIE_CFLAGS"}
172    {"Run indirect6 2"
173     "$NOPIE_LDFLAGS -Wl,--no-as-needed tmpdir/indirect6a.o tmpdir/libindirect5.so" ""
174     {dummy.c} "indirect6b" "indirect5.out" "$NOPIE_CFLAGS"}
175    {"Run with libpr18720c.so 1"
176     "-Wl,--no-as-needed tmpdir/pr18720a.o tmpdir/pr18720b.o tmpdir/libpr18720c.so" ""
177     {check-ptr-eq.c} "pr18720a" "pr18720.out"}
178    {"Run with libpr18720c.so 2"
179     "-Wl,--no-as-needed tmpdir/pr18720a.o tmpdir/libpr18720c.so tmpdir/pr18720b.o" ""
180     {check-ptr-eq.c} "pr18720b" "pr18720.out"}
181    {"Run with libpr18720c.so 3"
182     "-Wl,--no-as-needed tmpdir/pr18720b.o tmpdir/libpr18720c.so tmpdir/pr18720a.o" ""
183     {check-ptr-eq.c} "pr18720c" "pr18720.out"}
184    {"Run with libpr18720c.so 4"
185     "-Wl,--no-as-needed tmpdir/libpr18720c.so tmpdir/pr18720b.o tmpdir/pr18720a.o" ""
186     {check-ptr-eq.c} "pr18720d" "pr18720.out"}
187    {"Run with libpr18720c.so 5"
188     "-Wl,--no-as-needed tmpdir/libpr18720c.so tmpdir/pr18720b1.o tmpdir/pr18720a.o" ""
189     {check-ptr-eq.c} "pr18720d" "pr18720.out"}
190    {"Run with libpr19553b.so"
191     "-Wl,--no-as-needed tmpdir/libpr19553b.so tmpdir/libpr19553d.so -Wl,-rpath-link,." ""
192     {pr19553a.c} "pr19553b" "pr19553b.out"}
193    {"Run with libpr19553c.so"
194     "-Wl,--no-as-needed tmpdir/libpr19553c.so tmpdir/libpr19553b.so tmpdir/libpr19553d.so" ""
195     {pr19553a.c} "pr19553c" "pr19553c.out"}
196    {"Run with libpr19553d.so"
197     "-Wl,--no-as-needed tmpdir/libpr19553d.so tmpdir/libpr19553b.so -Wl,-rpath-link,." ""
198     {pr19553a.c} "pr19553d" "pr19553d.out"}
199}
200
201run_ld_link_exec_tests $run_tests
202
203# Check that "bar" is not dynamic in the executable
204proc check_dynamic_syms { test } {
205    global nm
206    set cmd "$nm -D $test > dump.out"
207    send_log "$cmd\n"
208    catch "exec $cmd" comp_output
209    if ![string match "" $comp_output] then {
210	send_log "$comp_output\n"
211	return 0
212    }
213    if { [string match "* bar\n*" [file_contents "dump.out"]] } then {
214	verbose "output is [file_contents "dump.out"]"
215	return 0
216    }
217    return 1
218}
219
220foreach t [list indirect5a indirect5b indirect6a indirect6b] {
221    set testname [concat $t "dynsym"]
222    if { [check_dynamic_syms tmpdir/$t] } {
223	pass $testname
224    } else {
225	fail $testname
226    }
227}
228
229send_log "$CC -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie"
230catch "exec $CC -fPIE -pie $srcdir/$subdir/main.c -o tmpdir/pie" exec_output
231send_log "$exec_output"
232if { ! [string match "" $exec_output] } {
233    return
234}
235
236set pie_tests {
237    {"Run indirect5 3"
238     "-pie -Wl,--no-as-needed tmpdir/libindirect5.so" ""
239     {indirect5a.c} "indirect5c" "indirect5.out" "-fPIE"}
240    {"Run indirect5 4"
241     "-pie -Wl,--no-as-needed tmpdir/indirect5a.o tmpdir/libindirect5.so" ""
242     {dummy.c} "indirect5d" "indirect5.out" "-fPIE"}
243    {"Run indirect6 3"
244     "-pie -Wl,--no-as-needed tmpdir/libindirect5.so" ""
245     {indirect6a.c} "indirect6c" "indirect5.out" "-fPIE"}
246    {"Run indirect6 4"
247     "-pie -Wl,--no-as-needed tmpdir/indirect6a.o tmpdir/libindirect5.so" "-fPIE"
248     {dummy.c} "indirect6d" "indirect5.out" "-fPIE"}
249}
250
251run_ld_link_exec_tests $pie_tests
252
253foreach t [list indirect5c indirect5d indirect6c indirect6d] {
254    set testname [concat $t "dynsym"]
255    if { [check_dynamic_syms tmpdir/$t] } {
256	pass $testname
257    } else {
258	fail $testname
259    }
260}
261