File: no-section-header.exp

package info (click to toggle)
binutils 2.46-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 417,664 kB
  • sloc: ansic: 1,487,508; asm: 829,455; cpp: 216,692; exp: 80,524; makefile: 73,157; sh: 24,213; yacc: 15,060; lisp: 13,632; perl: 13,404; lex: 1,714; ada: 1,681; pascal: 1,446; cs: 879; python: 637; java: 478; sed: 191; xml: 95; awk: 25
file content (371 lines) | stat: -rw-r--r-- 12,218 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# Expect script for -z nosectionheader and --strip-section-headers tests
# Copyright (C) 2023-2026 Free Software Foundation, Inc.
#
# This file is part of the GNU Binutils.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Written by H.J. Lu (hongjiu.lu@intel.com)
#

if { ![istarget *-*-gnu*] } {
    return
}

proc binutils_test { prog_name ld_options test readelf_expected nm_expected} {
    global as
    global ld
    global READELF
    global NM
    global objcopy
    global strip
    global srcdir
    global subdir
    global link_output

    eval set prog \$$prog_name

    set test_name "$prog_name --strip-section-headers $ld_options ($test)"

    if { ![ld_assemble $as $srcdir/$subdir/$test.s tmpdir/$test.o ] } {
	unresolved "$test_name"
	return
    }

    append ld_options " -z separate-code -z stack-size=0"
    if { ![ld_link $ld tmpdir/$test "$ld_options tmpdir/$test.o"] } {
	if { [string match "*not supported*" $link_output]
	     || [string match "*unrecognized option*" $link_output]
	     || [string match "*-z .* ignored*" $link_output] } {
	    unsupported "$ld_options is not supported by this target"
	} else {
	    unresolved "$test_name"
	}
	return
    }

    send_log "$prog --strip-section-headers tmpdir/$test\n"
    set got [remote_exec host "$prog --strip-section-headers tmpdir/$test"]
    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	send_log "$got\n"
	fail "$test_name"
	return
    }

    send_log "$READELF -lSDs --wide tmpdir/$test > tmpdir/$test.out\n"
    set got [remote_exec host "$READELF -lSDs --wide tmpdir/$test" "" "/dev/null" "tmpdir/$test.out"]
    if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	send_log "$got\n"
	unresolved "$test_name"
	return
    }

    if { [regexp_diff "tmpdir/$test.out" "$srcdir/$subdir/$readelf_expected"] } then {
	fail "$test_name"
	return
    }

    if { [string match "*-shared *" $ld_options] } {
	send_log "$NM -D tmpdir/$test > tmpdir/$test.out\n"
	set got [remote_exec host "$NM -D tmpdir/$test" "" "/dev/null" "tmpdir/$test.out"]
	if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	    send_log "$got\n"
	    unresolved "$test_name"
	    return
	}

	if { [regexp_diff "tmpdir/$test.out" "$srcdir/$subdir/$nm_expected"] } then {
	    fail "$test_name"
	    return
	}
    }

    pass "$test_name"
}

if { [istarget "mips*-*-*"] } {
    set gnu_hash_style "sysv"
} else {
    set gnu_hash_style "gnu"
}

binutils_test objcopy "--hash-style=both" start start-noheader.rd \
	start-noheader.nd
binutils_test objcopy "--hash-style=gnu" start start-noheader.rd \
	start-noheader.nd
binutils_test objcopy "--hash-style=sysv" start start-noheader.rd \
	start-noheader.nd
binutils_test objcopy "--hash-style=both -shared" start \
	start-shared-noheader-$gnu_hash_style.rd start-shared-noheader.nd
binutils_test objcopy "--hash-style=gnu -shared" start \
	start-shared-noheader-$gnu_hash_style.rd start-shared-noheader.nd
binutils_test objcopy "--hash-style=sysv -shared" start \
	start-shared-noheader-sysv.rd start-shared-noheader.nd
binutils_test strip "--hash-style=both" start start-noheader.rd \
	start-noheader.nd
binutils_test strip "--hash-style=gnu" start start-noheader.rd \
	start-noheader.nd
binutils_test strip "--hash-style=sysv" start start-noheader.rd \
	start-noheader.nd
binutils_test strip "--hash-style=both -shared" start \
	start-shared-noheader-$gnu_hash_style.rd start-shared-noheader.nd
binutils_test strip "--hash-style=gnu -shared" start \
	start-shared-noheader-$gnu_hash_style.rd start-shared-noheader.nd
binutils_test strip "--hash-style=sysv -shared" start \
	start-shared-noheader-sysv.rd start-shared-noheader.nd


if { ![check_compiler_available] || ![check_shared_lib_support] } {
    return
}

proc binutils_run_test { prog } {
    global CC_FOR_TARGET
    global READELF
    global NM
    global objcopy
    global strip
    global srcdir
    global subdir
    # Add $NOPIE_CFLAGS and $NOPIE_LDFLAGS if non-PIE is required.
    global NOPIE_CFLAGS NOPIE_LDFLAGS

    set sec_hdr "sec-hdr"
    if { "$prog" == "" } {
	set prog_name none
    } else {
	set prog_name $prog
	set ld_options ""
	switch -- $prog {
	    objcopy
		{ set prog $objcopy }
	    strip
		{ set prog $strip }
	    default
		{
		    fail "Build pr25617-1a-no-sec-hdr.so ($prog_name)"
		    break
		}
	}
    }

    run_cc_link_tests [list \
	[list \
	    "Build pr25617-1a-no-sec-hdr.so ($prog_name)" \
	    "-shared -Wl,-z,separate-code,--hash-style=sysv" \
	    "-fPIC" \
	    {pr25617-1a.c} \
	    [list \
		[list "readelf" "-lWSDs" "pr25617-1a-$sec_hdr.rd"] \
		[list "nm" "-D" "pr25617-1a-no-sec-hdr.nd"] \
	    ]\
	    "pr25617-1a-no-sec-hdr.so" \
	] \
	[list \
	    "Build pr25617-1a-now-no-sec-hdr.so ($prog_name)" \
	    "-shared -Wl,-z,separate-code,-z,now,--hash-style=gnu" \
	    "-fPIC" \
	    {pr25617-1a.c} \
	    [list \
		[list "readelf" "-lWSDs" "pr25617-1a-$sec_hdr.rd"] \
		[list "nm" "-D" "pr25617-1a-no-sec-hdr.nd"] \
	    ]\
	    "pr25617-1a-now-no-sec-hdr.so" \
	] \
	[list \
	    "Build pr25617-1 (-z nosectionheader, $prog_name)" \
	    "$NOPIE_LDFLAGS -Wl,-z,separate-code,--no-as-needed \
	     -Wl,--hash-style=sysv -Wl,-z,nosectionheader \
	     tmpdir/pr25617-1a-no-sec-hdr.so" \
	    "$NOPIE_CFLAGS" \
	    {pr25617-1b.c} \
	    {{readelf -lWSDs pr25617-1-no-sec-hdr.rd} \
	     {nm -D pr25617-1-no-sec-hdr.nd}} \
	    "pr25617-1-no-sec-hdr" \
	] \
	[list \
	    "Build pr25617-1 (PIE, -z nosectionheader, $prog_name)" \
	    "-pie -Wl,-z,separate-code,--no-as-needed,--hash-style=gnu \
	     -Wl,-z,nosectionheader tmpdir/pr25617-1a-now-no-sec-hdr.so" \
	    "-fPIE" \
	    {pr25617-1b.c} \
	    {{readelf -lWSDs pr25617-1-no-sec-hdr.rd} \
	     {nm -D pr25617-1-no-sec-hdr.nd}} \
	    "pr25617-1-pie-no-sec-hdr" \
	] \
	[list \
	    "Build pr25617-1 (static, -z nosectionheader, $prog_name)" \
	    "-static -Wl,-z,separate-code -Wl,-z,nosectionheader" \
	    "" \
	    {pr25617-1a.c pr25617-1b.c} \
	    {{readelf -lSWDs pr25617-1-static-no-sec-hdr.rd}} \
	    "pr25617-1-static-no-sec-hdr" \
	] \
    ]

    run_ld_link_exec_tests [list \
	[list \
	    "Run pr25617-1 (-z nosectionheader, $prog_name)" \
	    "$NOPIE_LDFLAGS -Wl,-z,separate-code,--no-as-needed \
	     -Wl,--hash-style=sysv -Wl,-z,nosectionheader \
	     tmpdir/pr25617-1a-no-sec-hdr.so" \
	    "" \
	    {pr25617-1b.c} \
	    "pr25617-1-no-sec-hdr" \
	    "pass.out" \
	    "$NOPIE_CFLAGS" \
	] \
	[list \
	    "Run pr25617-1 (PIE, -z nosectionheader, $prog_name)" \
	    "-pie -Wl,-z,separate-code,--no-as-needed,--hash-style=gnu \
	     -Wl,-z,nosectionheader tmpdir/pr25617-1a-now-no-sec-hdr.so" \
	    "" \
	    {pr25617-1b.c} \
	    "pr25617-1-pie-no-sec-hdr" \
	    "pass.out" \
	    "-fPIE" \
	] \
	[list \
	    "Run pr25617-1 (static, -z nosectionheader, $prog_name)" \
	    "-static -Wl,-z,separate-code -Wl,-z,nosectionheader" \
	    "" \
	    {pr25617-1a.c pr25617-1b.c} \
	    "pr25617-1-static-no-sec-hdr" \
	    "pass.out" \
	] \
    ]

    if { "$prog_name" != "none" } {
	send_log "$prog --strip-section-headers tmpdir/pr25617-1a-no-sec-hdr.so\n"
	set got [remote_exec host "$prog --strip-section-headers tmpdir/pr25617-1a-no-sec-hdr.so"]
	if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	    send_log "$got\n"
	    fail "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
	    return
	}

	send_log "$READELF -lWSDs tmpdir/pr25617-1a-no-sec-hdr.so > tmpdir/dump.out\n"
	set got [remote_exec host "$READELF -lWSDs tmpdir/pr25617-1a-no-sec-hdr.so" "" "/dev/null" "tmpdir/dump.out"]
	if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	    send_log "$got\n"
	    unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
	    return
	}

	if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/pr25617-1a-no-sec-hdr.rd"] } then {
	    unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
	    return
	}

	send_log "$NM -D tmpdir/pr25617-1a-no-sec-hdr.so > tmpdir/dump.out\n"
	set got [remote_exec host "$NM -D tmpdir/pr25617-1a-no-sec-hdr.so" "" "/dev/null" "tmpdir/dump.out"]
	if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	    send_log "$got\n"
	    unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
	    return
	}

	if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/pr25617-1a-no-sec-hdr.nd"] } then {
	    unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
	    return
	}

	if { [isnative] } {
	    send_log "tmpdir/pr25617-1-no-sec-hdr > tmpdir/pr25617-1.out\n"
	    catch "exec tmpdir/pr25617-1-no-sec-hdr > tmpdir/pr25617-1.out" got
	    if ![string match "" $got] then {
		send_log "$got\n"
		unresolved "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
		return
	    }

	    send_log "diff tmpdir/pr25617-1.out $srcdir/$subdir/pass.out\n"
	    catch "exec diff tmpdir/pr25617-1.out $srcdir/$subdir/pass.out" got
	    if ![string match "" $got] then {
		send_log "$got\n"
		fail "Update pr25617-1a-no-sec-hdr.so ($prog_name)"
		return
	    }
	}

	pass "Update pr25617-1a-no-sec-hdr.so ($prog_name)"

	send_log "$prog --strip-section-headers tmpdir/pr25617-1a-now-no-sec-hdr.so\n"
	set got [remote_exec host "$prog --strip-section-headers tmpdir/pr25617-1a-now-no-sec-hdr.so"]
	if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	    send_log "$got\n"
	    fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
	    return
	}

	send_log "$READELF -lWSDs tmpdir/pr25617-1a-now-no-sec-hdr.so > tmpdir/dump.out\n"
	set got [remote_exec host "$READELF -lWSDs tmpdir/pr25617-1a-now-no-sec-hdr.so" "" "/dev/null" "tmpdir/dump.out"]
	if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	    send_log "$got\n"
	    unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
	    return
	}

	if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/pr25617-1a-no-sec-hdr.rd"] } then {
	    unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
	    return
	}

	send_log "$NM -D tmpdir/pr25617-1a-now-no-sec-hdr.so > tmpdir/dump.out\n"
	set got [remote_exec host "$NM -D tmpdir/pr25617-1a-now-no-sec-hdr.so" "" "/dev/null" "tmpdir/dump.out"]
	if { [lindex $got 0] != 0 || ![string match "" [lindex $got 1]] } then {
	    send_log "$got\n"
	    unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
	    return
	}

	if { [regexp_diff "tmpdir/dump.out" "$srcdir/$subdir/pr25617-1a-no-sec-hdr.nd"] } then {
	    unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
	    return
	}

	if { [isnative] } {
	    send_log "tmpdir/pr25617-1-pie-no-sec-hdr > tmpdir/pr25617-1-pie.out\n"
	    catch "exec tmpdir/pr25617-1-pie-no-sec-hdr > tmpdir/pr25617-1-pie.out" got
	    if ![string match "" $got] then {
		send_log "$got\n"
		unresolved "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
		return
	    }

	    send_log "diff tmpdir/pr25617-1-pie.out $srcdir/$subdir/pass.out\n"
	    catch "exec diff tmpdir/pr25617-1-pie.out $srcdir/$subdir/pass.out" got
	    if ![string match "" $got] then {
		send_log "$got\n"
		fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
		return
	    }
	}

	set got [run_host_cmd $CC_FOR_TARGET "-o tmpdir/pr25617-1 tmpdir/pr25617-1b.o tmpdir/pr25617-1a-now-no-sec-hdr.so"]
	if ![string match "*pr25617-1a-now-no-sec-hdr.so*file in wrong format*" $got] then {
	    send_log "$got\n"
	    fail "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
	    return
	}

	pass "Update pr25617-1a-now-no-sec-hdr.so ($prog_name)"
     }
}

binutils_run_test ""
binutils_run_test objcopy
binutils_run_test strip