shithub: scc

Download patch

ref: 358742b714ee1245b08578ba057385489814fbd2
parent: 00d5bab0a1975bb9bb2d888f1945595afe1b7bdb
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Oct 30 03:51:04 EDT 2021

nm: Filter out non interesting symbols

Debug, section and file symbols are usually filtered out
because they are not interesting in the output of nm.

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -17,6 +17,15 @@
 	SSHARED = 1 << 7,
 };
 
+enum symtype {
+	SYMNOTYPE,
+	SYMOBJECT,
+	SYMFUNC,
+	SYMSECTION,
+	SYMFILE,
+	SYMCOMMON,
+};
+
 struct obj {
 	char *index;
 	Objops *ops;
@@ -55,6 +64,7 @@
 	int index;
 	int section;
 	char type;
+	int stype;
 };
 
 extern int archive(FILE *fp);
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -125,9 +125,11 @@
 	Symbol **p, *s;
 	size_t n, size;
 	int type = sym->type;
+	int stype = sym->stype;
 
 	if (type == '?' && !fflag
 	|| type == 'N' && !fflag
+	|| stype != SYMFUNC && stype != SYMOBJECT && !fflag
 	|| uflag && type != 'U'
 	|| gflag && !isupper(type)) {
 		return 0;
--- a/src/libmach/coff32/coff32getsym.c
+++ b/src/libmach/coff32/coff32getsym.c
@@ -1,5 +1,6 @@
 #include <ctype.h>
 #include <stdio.h>
+#include <string.h>
 
 #include <scc/mach.h>
 
@@ -7,7 +8,7 @@
 #include "coff32.h"
 
 static int
-typeof(Coff32 *coff, SYMENT *ent)
+typeof(Coff32 *coff, SYMENT *ent, char *name)
 {
 	int c;
 	SCNHDR *scn;
@@ -43,6 +44,19 @@
 	return c;
 }
 
+static int
+stypeof(char *name)
+{
+	if (strcmp(name, ".text") == 0
+	||  strcmp(name, ".data") == 0
+	||  strcmp(name, ".bss") == 0
+	||  strcmp(name, ".rdata") == 0) {
+		return SYMSECTION;
+	} else {
+		return SYMOBJECT;
+	}
+}
+
 static char *
 symname(Coff32 *coff, SYMENT *ent)
 {
@@ -65,7 +79,8 @@
 
 	ent = &coff->ents[n];
 	sym->name = symname(coff, ent);
-	sym->type = typeof(coff, ent);
+	sym->type = typeof(coff, ent, sym->name);
+	sym->stype = stypeof(sym->name);
 	sym->value = ent->n_value;
 	sym->size = (sym->type == 'C') ? ent->n_value : 0;
 	sym->index = n;
--- a/tests/nm/execute/0001-z80.sh
+++ b/tests/nm/execute/0001-z80.sh
@@ -9,9 +9,6 @@
 nm z80.out > $tmp1
 
 cat <<! > $tmp2
-0000000000000000 b .bss
-0000000000000000 d .data
-0000000000000000 t .text
 0000000000000001 B averylongbss
 0000000000000001 D averylongdata
 0000000000000001 T averylongtext
--- a/tests/nm/execute/0004-z80-v.sh
+++ b/tests/nm/execute/0004-z80-v.sh
@@ -10,9 +10,6 @@
 
 cat <<! > $tmp2
                  U text6
-0000000000000000 t .text
-0000000000000000 d .data
-0000000000000000 b .bss
 0000000000000000 T text1
 0000000000000000 D data1
 0000000000000000 B bss1
--- a/tests/nm/execute/0005-z80-A.sh
+++ b/tests/nm/execute/0005-z80-A.sh
@@ -13,9 +13,6 @@
 nm -A f.a z80.out > $tmp1
 
 cat <<! > $tmp2
-f.a[z80.out]: 0000000000000000 b .bss
-f.a[z80.out]: 0000000000000000 d .data
-f.a[z80.out]: 0000000000000000 t .text
 f.a[z80.out]: 0000000000000001 B averylongbss
 f.a[z80.out]: 0000000000000001 D averylongdata
 f.a[z80.out]: 0000000000000001 T averylongtext
@@ -32,9 +29,6 @@
 f.a[z80.out]: 000000000000000a C text4
 f.a[z80.out]: 0000000000000012 C text5
 f.a[z80.out]:                  U text6
-f.a[f.out]: 0000000000000000 b .bss
-f.a[f.out]: 0000000000000000 d .data
-f.a[f.out]: 0000000000000000 t .text
 f.a[f.out]: 0000000000000001 B averylongbss
 f.a[f.out]: 0000000000000001 D averylongdata
 f.a[f.out]: 0000000000000001 T averylongtext
@@ -51,9 +45,6 @@
 f.a[f.out]: 000000000000000a C text4
 f.a[f.out]: 0000000000000012 C text5
 f.a[f.out]:                  U text6
-z80.out: 0000000000000000 b .bss
-z80.out: 0000000000000000 d .data
-z80.out: 0000000000000000 t .text
 z80.out: 0000000000000001 B averylongbss
 z80.out: 0000000000000001 D averylongdata
 z80.out: 0000000000000001 T averylongtext
--- a/tests/nm/execute/0006-z80-o.sh
+++ b/tests/nm/execute/0006-z80-o.sh
@@ -9,9 +9,6 @@
 nm -t o z80.out > $tmp1
 
 cat <<! > $tmp2
-0000000000000000 b .bss
-0000000000000000 d .data
-0000000000000000 t .text
 0000000000000001 B averylongbss
 0000000000000001 D averylongdata
 0000000000000001 T averylongtext
--- a/tests/nm/execute/0007-z80-d.sh
+++ b/tests/nm/execute/0007-z80-d.sh
@@ -9,9 +9,6 @@
 nm -t d z80.out > $tmp1
 
 cat <<! > $tmp2
-0000000000000000 b .bss
-0000000000000000 d .data
-0000000000000000 t .text
 0000000000000001 B averylongbss
 0000000000000001 D averylongdata
 0000000000000001 T averylongtext
--- a/tests/nm/execute/0008-z80-x.sh
+++ b/tests/nm/execute/0008-z80-x.sh
@@ -9,9 +9,6 @@
 nm -t x z80.out > $tmp1
 
 cat <<! > $tmp2
-0000000000000000 b .bss
-0000000000000000 d .data
-0000000000000000 t .text
 0000000000000001 B averylongbss
 0000000000000001 D averylongdata
 0000000000000001 T averylongtext
--- a/tests/nm/execute/0009-z80-P-o.sh
+++ b/tests/nm/execute/0009-z80-P-o.sh
@@ -9,9 +9,6 @@
 nm -P -t o z80.out > $tmp1
 
 cat <<! > $tmp2
-.bss b 0000000000000000 0
-.data d 0000000000000000 0
-.text t 0000000000000000 0
 averylongbss B 0000000000000001 0
 averylongdata D 0000000000000001 0
 averylongtext T 0000000000000001 0
--- a/tests/nm/execute/0010-z80-P-d.sh
+++ b/tests/nm/execute/0010-z80-P-d.sh
@@ -9,9 +9,6 @@
 nm -P -t x z80.out > $tmp1
 
 cat <<! > $tmp2
-.bss b 0000000000000000 0
-.data d 0000000000000000 0
-.text t 0000000000000000 0
 averylongbss B 0000000000000001 0
 averylongdata D 0000000000000001 0
 averylongtext T 0000000000000001 0
--- a/tests/nm/execute/0011-z80-P-x.sh
+++ b/tests/nm/execute/0011-z80-P-x.sh
@@ -9,9 +9,6 @@
 nm -P -t x z80.out > $tmp1
 
 cat <<! > $tmp2
-.bss b 0000000000000000 0
-.data d 0000000000000000 0
-.text t 0000000000000000 0
 averylongbss B 0000000000000001 0
 averylongdata D 0000000000000001 0
 averylongtext T 0000000000000001 0
--- /dev/null
+++ b/tests/nm/execute/0012-z80-f.sh
@@ -1,0 +1,34 @@
+#!/bin/sh
+
+set -e
+
+tmp1=`mktemp`
+tmp2=`mktemp`
+trap "rm -f $tmp1 $tmp2; exit" 0 2 3
+
+nm -f z80.out > $tmp1
+
+cat <<! > $tmp2
+0000000000000000 b .bss
+0000000000000000 d .data
+0000000000000000 N .file
+0000000000000000 t .text
+0000000000000001 B averylongbss
+0000000000000001 D averylongdata
+0000000000000001 T averylongtext
+0000000000000000 B bss1
+0000000000000002 b bss3
+000000000000000a C bss4
+0000000000000012 C bss5
+0000000000000000 D data1
+0000000000000002 d data3
+000000000000000a C data4
+0000000000000012 C data5
+0000000000000000 T text1
+0000000000000002 t text3
+000000000000000a C text4
+0000000000000012 C text5
+                 U text6
+!
+
+diff $tmp1 $tmp2
--- a/tests/nm/execute/master.s
+++ b/tests/nm/execute/master.s
@@ -1,5 +1,5 @@
 	.globl	text1,averylongtext,text5
-	.extern	text6
+	.globl	text6
 	.text
 	.equ	text2,4
 text1:	.byte	0