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,
21 # Written by H.J. Lu (hongjiu.lu@intel.com)
24 if { ![is_elf_format] } {
28 proc binutils_test { prog_name ld_options test readelf_expected nm_expected} {
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"
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"
55 unresolved "$test_name"
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 {
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 {
72 unresolved "$test_name"
76 if { [regexp_diff "tmpdir/$test.out" "$srcdir/$subdir/$readelf_expected"] } then {
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 {
86 unresolved "$test_name"
90 if { [regexp_diff "tmpdir/$test.out" "$srcdir/$subdir/$nm_expected"] } then {
99 if { [istarget "mips*-*-*"] } {
100 set gnu_hash_style "sysv"
102 set gnu_hash_style "gnu"
105 binutils_test objcopy "--hash-style=both" start start-noheader.rd \
107 binutils_test objcopy "--hash-style=gnu" start start-noheader.rd \
109 binutils_test objcopy "--hash-style=sysv" start start-noheader.rd \
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 \
119 binutils_test strip "--hash-style=gnu" start start-noheader.rd \
121 binutils_test strip "--hash-style=sysv" start start-noheader.rd \
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] } {
135 proc binutils_run_test { prog } {
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" == "" } {
154 { set prog $objcopy }
159 fail "Build pr25617-1a-no-sec-hdr.so ($prog_name)"
165 run_cc_link_tests [list \
167 "Build pr25617-1a-no-sec-hdr.so ($prog_name)" \
168 "-shared -Wl,-z,separate-code,--hash-style=sysv" \
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" \
178 "Build pr25617-1a-now-no-sec-hdr.so ($prog_name)" \
179 "-shared -Wl,-z,separate-code,-z,now,--hash-style=gnu" \
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" \
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" \
195 {{readelf -lWSDs pr25617-1-no-sec-hdr.rd} \
196 {nm -D pr25617-1-no-sec-hdr.nd}} \
197 "pr25617-1-no-sec-hdr" \
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" \
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" \
210 "Build pr25617-1 (static, -z nosectionheader, $prog_name)" \
211 "-static -Wl,-z,separate-code -Wl,-z,nosectionheader" \
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 \
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" \
227 "pr25617-1-no-sec-hdr" \
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" \
237 "pr25617-1-pie-no-sec-hdr" \
242 "Run pr25617-1 (static, -z nosectionheader, $prog_name)" \
243 "-static -Wl,-z,separate-code -Wl,-z,nosectionheader" \
245 {pr25617-1a.c pr25617-1b.c} \
246 "pr25617-1-static-no-sec-hdr" \
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 {
256 fail "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
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 {
264 unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
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)"
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 {
277 unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
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)"
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 {
291 unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
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 {
299 fail "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
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 {
310 fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
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 {
318 unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
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)"
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 {
331 unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
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)"
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 {
345 unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
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 {
353 fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
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 {
361 fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
365 pass "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
370 binutils_run_test objcopy
371 binutils_run_test strip