1#   Copyright (C) 1993-2021 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
16
17# Please email any bugs, comments, and/or additions to this file to:
18# bug-dejagnu@prep.ai.mit.edu
19
20# This file was written by Rob Savoye <rob@cygnus.com>
21# and rewritten by Ian Lance Taylor <ian@cygnus.com>
22
23if ![is_remote host] {
24    if {[which $NM] == 0} then {
25        perror "$NM does not exist"
26        return
27    }
28}
29
30send_user "Version [binutil_version $NM]"
31
32
33if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
34    fail "nm (assembling)"
35} else {
36
37    if [is_remote host] {
38	set tempfile [remote_download host tmpdir/bintest.o]
39    } else {
40	set tempfile tmpdir/bintest.o
41    }
42
43    # Test nm with no arguments.
44
45    # This test does not work correctly on ECOFF targets, because ECOFF
46    # stores most symbols twice, which messes up the nm output.
47    setup_xfail "alpha*-*-osf*"
48    setup_xfail "mips*-*-ultrix*" "mips*-*-ecoff*" "mips*-*-irix4*"
49    setup_xfail "mips*-*-riscos*" "mips*-*-sysv3*"
50
51    # This test does not work correctly on XCOFF targets, because XCOFF
52    # does not enter static symbols in the symbol table.
53    if [is_xcoff_format] {
54	setup_xfail *-*-*
55    }
56
57    set got [binutils_run $NM "$NMFLAGS $tempfile"]
58
59    if [info exists vars] then { unset vars }
60    while {[regexp "(\[a-zA-Z\]) (\[a-z_\]*_symbol)(.*)" $got all type symbol rest]} {
61	set vars($symbol) $type
62	set got $rest
63    }
64
65    if {![info exists vars(text_symbol)] \
66	    || $vars(text_symbol) != "T" \
67	    || ![info exists vars(data_symbol)] \
68	    || $vars(data_symbol) != "D" \
69	    || ![info exists vars(common_symbol)] \
70	    || $vars(common_symbol) != "C" \
71	    || ![info exists vars(external_symbol)] \
72	    || $vars(external_symbol) != "U" \
73	    || ![info exists vars(static_text_symbol)] \
74	    || $vars(static_text_symbol) != "t" \
75	    || ![info exists vars(static_data_symbol)] \
76	    || $vars(static_data_symbol) != "d"} {
77	fail "nm (no arguments)"
78    } else {
79	pass "nm (no arguments)"
80    }
81
82    # Test nm -g
83
84    set got [binutils_run $NM "$NMFLAGS -g $tempfile"]
85
86    if [info exists vars] then { unset vars }
87    while {[regexp "(\[a-z_\]*_symbol)(.*)" $got all symbol rest]} {
88	set vars($symbol) 1
89	set got $rest
90    }
91
92    if {![info exists vars(text_symbol)] \
93	    || ![info exists vars(data_symbol)] \
94	    || ![info exists vars(common_symbol)] \
95	    || ![info exists vars(external_symbol)] \
96	    || [info exists vars(static_text_symbol)] \
97	    || [info exists vars(static_data_symbol)]} {
98	fail "nm -g"
99    } else {
100	pass "nm -g"
101    }
102
103    # Test nm -P
104
105    # This test does not work correctly on ECOFF targets, because ECOFF
106    # stores most symbols twice, which messes up the nm output.
107    setup_xfail "alpha*-*-osf*"
108    setup_xfail "mips*-*-ultrix*" "mips*-*-ecoff*" "mips*-*-irix4*"
109    setup_xfail "mips*-*-riscos*" "mips*-*-sysv3*"
110
111    # This test does not work correctly on XCOFF targets, because XCOFF
112    # does not enter static symbols in the symbol table.
113    if [is_xcoff_format] {
114	setup_xfail *-*-*
115    }
116
117    set got [binutils_run $NM "$NMFLAGS -P $tempfile"]
118
119    set want "common_symbol C \[0\]*4.*data_symbol D \[0-9a-fA-F\]*.*external_symbol U.*static_data_symbol d \[0-9a-fA-F\]*.*static_text_symbol t \[0-9a-fA-F\]*.*text_symbol T \[0-9a-fA-F\]*"
120
121    if [regexp $want $got] then {
122	pass "nm -P"
123    } else {
124	fail "nm -P"
125    }
126
127    # Test nm -t d
128    # Look for leading zeroes and only the digits 0..9 in the actual value.
129    set got [binutils_run $NM "$NMFLAGS -t d $tempfile"]
130    if [regexp "0+\[1-9\]\[0-9\]* T text_symbol3" $got] then {
131	pass "nm -t d"
132    } else {
133	fail "nm -t d"
134    }
135
136    # Test nm --format=posix
137    # ref: PR 24507 - no leading zeros.
138    set got [binutils_run $NM "$NMFLAGS --format=posix $tempfile"]
139    if [regexp "text_symbol3 T \[1-9a-f\]\[0-9a-f\]*" $got] then {
140	pass "nm --format posix"
141    } else {
142	fail "nm --format posix"
143    }
144}
145
146# Test nm --size-sort
147
148# The target exceptions here are intended for targets that have ELF as
149# an intermediate format or otherwise require the ELF-variant syntax
150# for proper size annotation.  It would be nice if is_elf_format found
151# all the ELF variants, but adding the patterns here to that proc then
152# introduces a whole slew of new regressions in the GAS and LD testsuites.
153if {   [is_elf_format]
154    || [istarget *-*-beos]
155    || [istarget *-*-dragonfly*]
156    || [istarget *-*-*elf]
157    || [istarget *-*-freebsd*]
158    || [istarget *-*-lynxos*]
159    || [istarget *-*-moss*]
160    || [istarget "mmix-knuth-mmixware"]
161    || [istarget *-*-nto*]
162    || [istarget *-*-rdos*]
163    || [istarget *-*-tpf*]
164    || [istarget *-*-uclinux*]
165    || [istarget ia64-*-*vms*]
166    || [istarget *-*-vxworks*]
167    || [istarget wasm32-*-*]
168    || [istarget bpf-*-*]} {
169    set nm_1_src "nm-elf-1.s"
170} else {
171    set nm_1_src "nm-1.s"
172}
173
174if {![binutils_assemble $srcdir/$subdir/$nm_1_src tmpdir/nm-1.o]} then {
175    fail "nm --size-sort (assembling)"
176} else {
177
178    if [is_remote host] {
179	set tempfile [remote_download host tmpdir/nm-1.o]
180    } else {
181	set tempfile tmpdir/nm-1.o
182    }
183
184    # This test does not work correctly on ECOFF targets, because ECOFF
185    # stores most symbols twice, which messes up the nm output.
186    setup_xfail "alpha*-*-osf*"
187    setup_xfail "mips*-*-ultrix*" "mips*-*-ecoff*" "mips*-*-irix4*"
188    setup_xfail "mips*-*-riscos*" "mips*-*-sysv3*"
189
190    set got [binutils_run $NM "$NMFLAGS --size-sort $tempfile"]
191
192    set want "0*4 T text_symbol3.*0*8 T text_symbol2.*0*c T text_symbol1"
193
194    if [regexp $want $got] then {
195	pass "nm --size-sort"
196    } else {
197	fail "nm --size-sort"
198    }
199}
200
201if [is_elf_format] {
202    # PR binutils/12753
203    # Test nm -g on a unique global symbol.
204    if {![binutils_assemble $srcdir/$subdir/unique.s tmpdir/unique.o]} then {
205	unsupported "nm -g on unique symbols"
206    } else {
207	if [is_remote host] {
208	    set tmpfile [remote_download host tmpdir/unique.o]
209	} else {
210	    set tmpfile tmpdir/unique.o
211	}
212
213	set got [binutils_run $NM "$NMFLAGS -g $tmpfile"]
214
215	if [regexp "u foo" $got] then {
216	    pass "nm -g on unique symbols"
217	} else {
218	    fail "nm -g on unique symbols"
219	}
220
221	if { $verbose < 1 } {
222	    remote_file host delete "tmpdir/unique.o"
223	}
224    }
225
226    # PR binutils/20751
227    # Test nm --with-symbol-versions
228
229    if {![binutils_assemble $srcdir/$subdir/nm-ver.s tmpdir/nm-ver.o]} then {
230	fail "nm --with-symbol-versions (assembling)"
231    } else {
232	if [is_remote host] {
233	    set tmpfile [remote_download host tmpdir/nm-ver.o]
234	} else {
235	    set tmpfile tmpdir/nm-ver.o
236	}
237
238	set got [binutils_run $NM "$NMFLAGS --with-symbol-versions --format sysv $tmpfile"]
239
240	if {! [regexp "foo@VER_1" $got]} then {
241	    fail "nm --with-symbol-versions (grep for @VER_1)"
242	} else {
243	    if {! [regexp "foo@VER_1" $got]} then {
244		fail "nm --with-symbol-versions (grep for @@VER_2)"
245	    } else {
246		pass "nm --with-symbol-versions"
247	    }
248	}
249
250	if { $verbose < 1 } {
251	    remote_file host delete "tmpdir/nm-ver.o"
252	}
253    }
254
255    # PR binutils/25676
256    # Test nm --line-numbers on DWARF-4 debug info.
257    set testname "nm --line-numbers on DWARF-4 debug info"
258
259    # The RISCV target does not (currently) support .uleb128.
260    setup_xfail "riscv*-*-*"
261    # The SH targets complain that the pseudo-ops used to construct
262    # the DWARF data are misaligned.
263    setup_xfail "sh*-*-*"
264    # The pre-compiled dwarf info in dw4.s is not compatible with the
265    # ALPHA, HPPA, IA64 and MIPS targets.
266    setup_xfail "alpha*-*-*" "hppa*-*-*" "ia64*-*-*" "mips*-*-*"
267    # Assembling the source file triggers an ICE in the FT32 assembler.
268    # FIXME: Fix the ICE...
269    setup_xfail "ft32-*-*"
270    # The AVR, MSP430, NDS32, PRU and XTENSA targets do not assemble the
271    # (64-bit) source file.
272    setup_xfail "avr-*-*" "msp430-*-*" "nds32*-*-*" "pru-*-*" "xtensa-*-*"
273
274    if {![binutils_assemble $srcdir/$subdir/dw4.s tmpdir/dw4.o]} then {
275	fail "$testname (assembly)"
276    } else {
277	# The test source is only intended for 64-bit targets.
278	# FIXME: Create a 32-bit version of this test.
279	if {! [is_elf64 tmpdir/dw4.o]} {
280	    unsupported "$testname (needs a 64-bit target)"
281	} else {
282	    if [is_remote host] {
283		set tmpfile [remote_download host tmpdir/dw4r.o]
284	    } else {
285		set tmpfile tmpdir/dw4.o
286	    }
287
288	    set got [binutils_run $NM "$NMFLAGS --line-numbers $tmpfile"]
289
290	    if {! [regexp "g_my_externd_global.*tests/main.c:3" $got]} then {
291		fail "$testname (grep for externd global file/line)"
292	    } else {
293		pass $testname
294	    }
295	}
296
297	if { $verbose < 1 } {
298	    remote_file host delete $tmpfile
299	}
300    }
301
302    # PR 22967
303    # Test nm --ifunc-chars on a indirect symbols.
304
305    # The following targets are known to not support ifuncs.
306    setup_xfail "alpha*-*-*"
307    setup_xfail "arm*-elf" "arm*-*-nto*" "arm*-*-netbsdelf*"
308    setup_xfail "*-*-hpux*"
309    setup_xfail "mips*-*-*" "tx39*-*-*"
310    setup_xfail "msp430*-*-*"
311    setup_xfail "visium*-*-*"
312    setup_xfail "x86_64-*-cloudabi*"
313
314    set testname "nm --ifunc-chars"
315    if {![binutils_assemble $srcdir/$subdir/ifunc.s tmpdir/ifunc.o]} then {
316	fail "$testname (assembly)"
317    } else {
318	if [is_remote host] {
319	    set tmpfile [remote_download host tmpdir/ifunc.o]
320	} else {
321	    set tmpfile tmpdir/ifunc.o
322	}
323
324	set got [binutils_run $NM "$NMFLAGS --ifunc-chars=Ff $tmpfile"]
325
326	if [regexp "F global_foo" $got] then {
327	    pass "$testname (global ifunc)"
328	} else {
329	    fail "$testname (global ifunc)"
330	}
331
332	if [regexp "f local_foo" $got] then {
333	    pass "$testname (local ifunc)"
334	} else {
335	    fail "$testname (local ifunc)"
336	}
337
338	if { $verbose < 1 } {
339	    remote_file host delete "tmpdir/ifunc.o"
340	}
341    }
342
343
344}
345
346# There are certainly other tests that could be run.
347