Update year range in copyright notice of binutils files
[binutils-gdb.git] / ld / testsuite / ld-elf / no-section-header.exp
blob02b9548e32231f547469feb12eca70a2b5694b6e
1 # Expect script for -z nosectionheader and --strip-section-headers tests
2 # Copyright (C) 2023-2024 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
21 # Written by H.J. Lu (hongjiu.lu@intel.com)
24 if { ![is_elf_format] } {
25 return
28 proc binutils_test { prog_name ld_options test readelf_expected nm_expected} {
29 global as
30 global ld
31 global READELF
32 global NM
33 global objcopy
34 global strip
35 global srcdir
36 global subdir
37 global link_output
39 eval set prog \$$prog_name
41 set test_name "$prog_name --strip-section-headers $ld_options ($test)"
43 if { ![ld_assemble $as $srcdir/$subdir/$test.s tmpdir/$test.o ] } {
44 unresolved "$test_name"
45 return
48 append ld_options " -z separate-code -z stack-size=0"
49 if { ![ld_link $ld tmpdir/$test "$ld_options tmpdir/$test.o"] } {
50 if { [string match "*not supported*" $link_output]
51 || [string match "*unrecognized option*" $link_output]
52 || [string match "*-z .* ignored*" $link_output] } {
53 unsupported "$ld_options is not supported by this target"
54 } else {
55 unresolved "$test_name"
57 return
60 send_log "$prog --strip-section-headers tmpdir/$test\n"
61 set got [remote_exec host "$prog --strip-section-headers tmpdir/$test"]
62 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
63 send_log "$got\n"
64 fail "$test_name"
65 return
68 send_log "$READELF -lSDs --wide tmpdir/$test > tmpdir/$test.out\n"
69 set got [remote_exec host "$READELF -lSDs --wide tmpdir/$test" "" "/dev/null" "tmpdir/$test.out"]
70 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
71 send_log "$got\n"
72 unresolved "$test_name"
73 return
76 if { [regexp_diff "tmpdir/$test.out" "$srcdir/$subdir/$readelf_expected"] } then {
77 fail "$test_name"
78 return
81 if { [string match "*-shared *" $ld_options] } {
82 send_log "$NM -D tmpdir/$test > tmpdir/$test.out\n"
83 set got [remote_exec host "$NM -D tmpdir/$test" "" "/dev/null" "tmpdir/$test.out"]
84 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
85 send_log "$got\n"
86 unresolved "$test_name"
87 return
90 if { [regexp_diff "tmpdir/$test.out" "$srcdir/$subdir/$nm_expected"] } then {
91 fail "$test_name"
92 return
96 pass "$test_name"
99 if { [istarget "mips*-*-*"] } {
100 set gnu_hash_style "sysv"
101 } else {
102 set gnu_hash_style "gnu"
105 binutils_test objcopy "--hash-style=both" start start-noheader.rd \
106 start-noheader.nd
107 binutils_test objcopy "--hash-style=gnu" start start-noheader.rd \
108 start-noheader.nd
109 binutils_test objcopy "--hash-style=sysv" start start-noheader.rd \
110 start-noheader.nd
111 binutils_test objcopy "--hash-style=both -shared" start \
112 start-shared-noheader-$gnu_hash_style.rd start-shared-noheader.nd
113 binutils_test objcopy "--hash-style=gnu -shared" start \
114 start-shared-noheader-$gnu_hash_style.rd start-shared-noheader.nd
115 binutils_test objcopy "--hash-style=sysv -shared" start \
116 start-shared-noheader-sysv.rd start-shared-noheader.nd
117 binutils_test strip "--hash-style=both" start start-noheader.rd \
118 start-noheader.nd
119 binutils_test strip "--hash-style=gnu" start start-noheader.rd \
120 start-noheader.nd
121 binutils_test strip "--hash-style=sysv" start start-noheader.rd \
122 start-noheader.nd
123 binutils_test strip "--hash-style=both -shared" start \
124 start-shared-noheader-$gnu_hash_style.rd start-shared-noheader.nd
125 binutils_test strip "--hash-style=gnu -shared" start \
126 start-shared-noheader-$gnu_hash_style.rd start-shared-noheader.nd
127 binutils_test strip "--hash-style=sysv -shared" start \
128 start-shared-noheader-sysv.rd start-shared-noheader.nd
131 if { ![check_compiler_available] || ![check_shared_lib_support] } {
132 return
135 proc binutils_run_test { prog } {
136 global CC_FOR_TARGET
137 global READELF
138 global NM
139 global objcopy
140 global strip
141 global srcdir
142 global subdir
143 # Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if non-PIE is required.
144 global NOPIE_CFLAGS NOPIE_LDFLAGS
146 set sec_hdr "sec-hdr"
147 if { "$prog" == "" } {
148 set prog_name none
149 } else {
150 set prog_name $prog
151 set ld_options ""
152 switch -- $prog {
153 objcopy
154 { set prog $objcopy }
155 strip
156 { set prog $strip }
157 default
159 fail "Build pr25617-1a-no-sec-hdr.so ($prog_name)"
160 break
165 run_cc_link_tests [list \
166 [list \
167 "Build pr25617-1a-no-sec-hdr.so ($prog_name)" \
168 "-shared -Wl,-z,separate-code,--hash-style=sysv" \
169 "-fPIC" \
170 {pr25617-1a.c} \
171 [list \
172 [list "readelf" "-lWSDs" "pr25617-1a-$sec_hdr.rd"] \
173 [list "nm" "-D" "pr25617-1a-no-sec-hdr.nd"] \
175 "pr25617-1a-no-sec-hdr.so" \
177 [list \
178 "Build pr25617-1a-now-no-sec-hdr.so ($prog_name)" \
179 "-shared -Wl,-z,separate-code,-z,now,--hash-style=gnu" \
180 "-fPIC" \
181 {pr25617-1a.c} \
182 [list \
183 [list "readelf" "-lWSDs" "pr25617-1a-$sec_hdr.rd"] \
184 [list "nm" "-D" "pr25617-1a-no-sec-hdr.nd"] \
186 "pr25617-1a-now-no-sec-hdr.so" \
188 [list \
189 "Build pr25617-1 (-z nosectionheader, $prog_name)" \
190 "$NOPIE_LDFLAGS -Wl,-z,separate-code,--no-as-needed \
191 -Wl,--hash-style=sysv -Wl,-z,nosectionheader \
192 tmpdir/pr25617-1a-no-sec-hdr.so" \
193 "$NOPIE_CFLAGS" \
194 {pr25617-1b.c} \
195 {{readelf -lWSDs pr25617-1-no-sec-hdr.rd} \
196 {nm -D pr25617-1-no-sec-hdr.nd}} \
197 "pr25617-1-no-sec-hdr" \
199 [list \
200 "Build pr25617-1 (PIE, -z nosectionheader, $prog_name)" \
201 "-pie -Wl,-z,separate-code,--no-as-needed,--hash-style=gnu \
202 -Wl,-z,nosectionheader tmpdir/pr25617-1a-now-no-sec-hdr.so" \
203 "-fPIE" \
204 {pr25617-1b.c} \
205 {{readelf -lWSDs pr25617-1-no-sec-hdr.rd} \
206 {nm -D pr25617-1-no-sec-hdr.nd}} \
207 "pr25617-1-pie-no-sec-hdr" \
209 [list \
210 "Build pr25617-1 (static, -z nosectionheader, $prog_name)" \
211 "-static -Wl,-z,separate-code -Wl,-z,nosectionheader" \
212 "" \
213 {pr25617-1a.c pr25617-1b.c} \
214 {{readelf -lSWDs pr25617-1-static-no-sec-hdr.rd}} \
215 "pr25617-1-static-no-sec-hdr" \
219 run_ld_link_exec_tests [list \
220 [list \
221 "Run pr25617-1 (-z nosectionheader, $prog_name)" \
222 "$NOPIE_LDFLAGS -Wl,-z,separate-code,--no-as-needed \
223 -Wl,--hash-style=sysv -Wl,-z,nosectionheader \
224 tmpdir/pr25617-1a-no-sec-hdr.so" \
225 "" \
226 {pr25617-1b.c} \
227 "pr25617-1-no-sec-hdr" \
228 "pass.out" \
229 "$NOPIE_CFLAGS" \
231 [list \
232 "Run pr25617-1 (PIE, -z nosectionheader, $prog_name)" \
233 "-pie -Wl,-z,separate-code,--no-as-needed,--hash-style=gnu \
234 -Wl,-z,nosectionheader tmpdir/pr25617-1a-now-no-sec-hdr.so" \
235 "" \
236 {pr25617-1b.c} \
237 "pr25617-1-pie-no-sec-hdr" \
238 "pass.out" \
239 "-fPIE" \
241 [list \
242 "Run pr25617-1 (static, -z nosectionheader, $prog_name)" \
243 "-static -Wl,-z,separate-code -Wl,-z,nosectionheader" \
244 "" \
245 {pr25617-1a.c pr25617-1b.c} \
246 "pr25617-1-static-no-sec-hdr" \
247 "pass.out" \
251 if { "$prog_name" != "none" } {
252 send_log "$prog --strip-section-headers tmpdir/pr25617-1a-no-sec-hdr.so\n"
253 set got [remote_exec host "$prog --strip-section-headers tmpdir/pr25617-1a-no-sec-hdr.so"]
254 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
255 send_log "$got\n"
256 fail "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
257 return
260 send_log "$READELF -lWSDs tmpdir/pr25617-1a-no-sec-hdr.so > tmpdir/dump.out\n"
261 set got [remote_exec host "$READELF -lWSDs tmpdir/pr25617-1a-no-sec-hdr.so" "" "/dev/null" "tmpdir/dump.out"]
262 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
263 send_log "$got\n"
264 unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
265 return
268 if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/pr25617-1a-no-sec-hdr.rd"] } then {
269 unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
270 return
273 send_log "$NM -D tmpdir/pr25617-1a-no-sec-hdr.so > tmpdir/dump.out\n"
274 set got [remote_exec host "$NM -D tmpdir/pr25617-1a-no-sec-hdr.so" "" "/dev/null" "tmpdir/dump.out"]
275 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
276 send_log "$got\n"
277 unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
278 return
281 if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/pr25617-1a-no-sec-hdr.nd"] } then {
282 unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
283 return
286 if { [isnative] } {
287 send_log "tmpdir/pr25617-1-no-sec-hdr > tmpdir/pr25617-1.out\n"
288 catch "exec tmpdir/pr25617-1-no-sec-hdr > tmpdir/pr25617-1.out" got
289 if ![string match "" $got] then {
290 send_log "$got\n"
291 unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
292 return
295 send_log "diff tmpdir/pr25617-1.out $srcdir/$subdir/pass.out\n"
296 catch "exec diff tmpdir/pr25617-1.out $srcdir/$subdir/pass.out" got
297 if ![string match "" $got] then {
298 send_log "$got\n"
299 fail "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
300 return
304 pass "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
306 send_log "$prog --strip-section-headers tmpdir/pr25617-1a-now-no-sec-hdr.so\n"
307 set got [remote_exec host "$prog --strip-section-headers tmpdir/pr25617-1a-now-no-sec-hdr.so"]
308 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
309 send_log "$got\n"
310 fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
311 return
314 send_log "$READELF -lWSDs tmpdir/pr25617-1a-now-no-sec-hdr.so > tmpdir/dump.out\n"
315 set got [remote_exec host "$READELF -lWSDs tmpdir/pr25617-1a-now-no-sec-hdr.so" "" "/dev/null" "tmpdir/dump.out"]
316 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
317 send_log "$got\n"
318 unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
319 return
322 if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/pr25617-1a-no-sec-hdr.rd"] } then {
323 unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
324 return
327 send_log "$NM -D tmpdir/pr25617-1a-now-no-sec-hdr.so > tmpdir/dump.out\n"
328 set got [remote_exec host "$NM -D tmpdir/pr25617-1a-now-no-sec-hdr.so" "" "/dev/null" "tmpdir/dump.out"]
329 if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
330 send_log "$got\n"
331 unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
332 return
335 if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/pr25617-1a-no-sec-hdr.nd"] } then {
336 unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
337 return
340 if { [isnative] } {
341 send_log "tmpdir/pr25617-1-pie-no-sec-hdr > tmpdir/pr25617-1-pie.out\n"
342 catch "exec tmpdir/pr25617-1-pie-no-sec-hdr > tmpdir/pr25617-1-pie.out" got
343 if ![string match "" $got] then {
344 send_log "$got\n"
345 unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
346 return
349 send_log "diff tmpdir/pr25617-1-pie.out $srcdir/$subdir/pass.out\n"
350 catch "exec diff tmpdir/pr25617-1-pie.out $srcdir/$subdir/pass.out" got
351 if ![string match "" $got] then {
352 send_log "$got\n"
353 fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
354 return
358 set got [run_host_cmd $CC_FOR_TARGET "-o tmpdir/pr25617-1 tmpdir/pr25617-1b.o tmpdir/pr25617-1a-now-no-sec-hdr.so"]
359 if ![string match "*pr25617-1a-now-no-sec-hdr.so*file in wrong format*" $got] then {
360 send_log "$got\n"
361 fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
362 return
365 pass "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
369 binutils_run_test ""
370 binutils_run_test objcopy
371 binutils_run_test strip