xref: /csrg-svn/usr.bin/nm/nm.c (revision 859)
1*859Sbill static	char sccsid[] = "@(#)nm.c 3.3 09/09/80";
2619Sbill /*
3619Sbill  * nm - print name list; VAX string table version
4619Sbill  */
5619Sbill #include <sys/types.h>
6653Sbill #include <ar.h>
7619Sbill #include <stdio.h>
8619Sbill #include <ctype.h>
9653Sbill #include <a.out.h>
10619Sbill #include <stab.h>
11619Sbill #include <pagsiz.h>
12619Sbill #include <stat.h>
13619Sbill 
14619Sbill #define	SELECT	archive ? archdr.ar_name : *xargv
15619Sbill 
16619Sbill int	aflg, gflg, nflg, oflg, pflg, uflg;
17619Sbill int	rflg = 1;
18619Sbill char	**xargv;
19619Sbill int	archive;
20619Sbill struct	ar_hdr	archdr;
21619Sbill union {
22619Sbill 	char	mag_armag[SARMAG+1];
23619Sbill 	struct	exec mag_exp;
24619Sbill } mag_un;
25619Sbill #define	OARMAG	0177545
26619Sbill FILE	*fi;
27619Sbill off_t	off;
28619Sbill off_t	ftell();
29619Sbill char	*malloc();
30619Sbill char	*realloc();
31619Sbill char	*strp;
32619Sbill char	*stab();
33619Sbill off_t	strsiz;
34619Sbill int	compare();
35619Sbill int	narg;
36619Sbill int	errs;
37619Sbill 
38619Sbill main(argc, argv)
39619Sbill char **argv;
40619Sbill {
41619Sbill 
42619Sbill 	if (--argc>0 && argv[1][0]=='-' && argv[1][1]!=0) {
43619Sbill 		argv++;
44619Sbill 		while (*++*argv) switch (**argv) {
45619Sbill 
46619Sbill 		case 'n':
47619Sbill 			nflg++;
48619Sbill 			continue;
49619Sbill 		case 'g':
50619Sbill 			gflg++;
51619Sbill 			continue;
52619Sbill 		case 'u':
53619Sbill 			uflg++;
54619Sbill 			continue;
55619Sbill 		case 'r':
56619Sbill 			rflg = -1;
57619Sbill 			continue;
58619Sbill 		case 'p':
59619Sbill 			pflg++;
60619Sbill 			continue;
61619Sbill 		case 'o':
62619Sbill 			oflg++;
63619Sbill 			continue;
64619Sbill 		case 'a':
65619Sbill 			aflg++;
66619Sbill 			continue;
67619Sbill 		default:
68619Sbill 			fprintf(stderr, "nm: invalid argument -%c\n",
69619Sbill 			    *argv[0]);
70619Sbill 			exit(2);
71619Sbill 		}
72619Sbill 		argc--;
73619Sbill 	}
74619Sbill 	if (argc == 0) {
75619Sbill 		argc = 1;
76619Sbill 		argv[1] = "a.out";
77619Sbill 	}
78619Sbill 	narg = argc;
79619Sbill 	xargv = argv;
80619Sbill 	while (argc--) {
81619Sbill 		++xargv;
82619Sbill 		namelist();
83619Sbill 	}
84619Sbill 	exit(errs);
85619Sbill }
86619Sbill 
87619Sbill namelist()
88619Sbill {
89619Sbill 	register int j;
90619Sbill 
91619Sbill 	archive = 0;
92619Sbill 	fi = fopen(*xargv, "r");
93619Sbill 	if (fi == NULL) {
94619Sbill 		error(0, "cannot open");
95619Sbill 		return;
96619Sbill 	}
97619Sbill 	off = SARMAG;
98619Sbill 	fread((char *)&mag_un, 1, sizeof(mag_un), fi);
99619Sbill 	if (mag_un.mag_exp.a_magic == OARMAG) {
100619Sbill 		error(0, "old archive");
101619Sbill 		return;
102619Sbill 	}
103619Sbill 	if (strncmp(mag_un.mag_armag, ARMAG, SARMAG)==0)
104619Sbill 		archive++;
105619Sbill 	else if (N_BADMAG(mag_un.mag_exp)) {
106619Sbill 		error(0, "bad format");
107619Sbill 		return;
108619Sbill 	}
109619Sbill 	fseek(fi, 0L, 0);
110619Sbill 	if (archive) {
111619Sbill 		nextel(fi);
112619Sbill 		if (narg > 1)
113619Sbill 			printf("\n%s:\n", *xargv);
114619Sbill 	}
115619Sbill 	do {
116619Sbill 		off_t o;
117619Sbill 		register i, n, c;
118619Sbill 		struct nlist *symp = NULL;
119619Sbill 		struct nlist sym;
120619Sbill 		struct stat stb;
121619Sbill 
122619Sbill 		fread((char *)&mag_un.mag_exp, 1, sizeof(struct exec), fi);
123619Sbill 		if (N_BADMAG(mag_un.mag_exp))
124619Sbill 			continue;
125619Sbill 		if (archive == 0)
126619Sbill 			fstat(fileno(fi), &stb);
127619Sbill 		o = N_SYMOFF(mag_un.mag_exp) - sizeof (struct exec);
128619Sbill 		fseek(fi, o, 1);
129619Sbill 		n = mag_un.mag_exp.a_syms / sizeof(struct nlist);
130619Sbill 		if (n == 0) {
131619Sbill 			error(0, "no name list");
132619Sbill 			continue;
133619Sbill 		}
134619Sbill 		if (N_STROFF(mag_un.mag_exp) + sizeof (off_t) >
135653Sbill 		    (archive ? off : stb.st_size))
136619Sbill 			error(1, "old format .o (no string table) or truncated file");
137619Sbill 		i = 0;
138619Sbill 		if (strp)
139619Sbill 			free(strp), strp = 0;
140619Sbill 		while (--n >= 0) {
141619Sbill 			fread((char *)&sym, 1, sizeof(sym), fi);
142619Sbill 			if (gflg && (sym.n_type&N_EXT)==0)
143619Sbill 				continue;
144619Sbill 			if ((sym.n_type&N_STAB) && (!aflg||gflg||uflg))
145619Sbill 				continue;
146619Sbill 			if (symp==NULL)
147619Sbill 				symp = (struct nlist *)
148619Sbill 				    malloc(sizeof(struct nlist));
149619Sbill 			else
150619Sbill 				symp = (struct nlist *)
151619Sbill 				    realloc(symp,
152619Sbill 					(i+1)*sizeof(struct nlist));
153619Sbill 			if (symp == NULL)
154619Sbill 				error(1, "out of memory");
155619Sbill 			symp[i++] = sym;
156619Sbill 		}
157619Sbill 		if (archive && ftell(fi)+sizeof(off_t) >= off) {
158619Sbill 			error(0, "no string table (old format .o?)");
159619Sbill 			continue;
160619Sbill 		}
161619Sbill 		if (fread((char *)&strsiz,sizeof(strsiz),1,fi) != 1) {
162619Sbill 			error(0, "no string table (old format .o?)");
163619Sbill 			goto out;
164619Sbill 		}
165619Sbill 		strp = (char *)malloc(strsiz);
166619Sbill 		if (strp == NULL)
167619Sbill 			error(1, "ran out of memory");
168619Sbill 		if (fread(strp+sizeof(strsiz),strsiz-sizeof(strsiz),1,fi) != 1)
169619Sbill 			error(1, "error reading string table");
170619Sbill 		for (j = 0; j < i; j++)
171619Sbill 			if (symp[j].n_un.n_strx)
172619Sbill 				symp[j].n_un.n_name =
173619Sbill 				    symp[j].n_un.n_strx + strp;
174619Sbill 			else
175619Sbill 				symp[j].n_un.n_name = "";
176619Sbill 		if (pflg==0)
177619Sbill 			qsort(symp, i, sizeof(struct nlist), compare);
178619Sbill 		if ((archive || narg>1) && oflg==0)
179619Sbill 			printf("\n%s:\n", SELECT);
180619Sbill 		psyms(symp, i);
181619Sbill 		if (symp)
182619Sbill 			free((char *)symp), symp = 0;
183619Sbill 		if (strp)
184619Sbill 			free((char *)strp), strp = 0;
185619Sbill 	} while(archive && nextel(fi));
186619Sbill out:
187619Sbill 	fclose(fi);
188619Sbill }
189619Sbill 
190619Sbill psyms(symp, nsyms)
191619Sbill 	register struct nlist *symp;
192619Sbill 	int nsyms;
193619Sbill {
194619Sbill 	register int n, c;
195619Sbill 
196619Sbill 	for (n=0; n<nsyms; n++) {
197619Sbill 		c = symp[n].n_type;
198619Sbill 		if (c & N_STAB) {
199619Sbill 			if (oflg) {
200619Sbill 				if (archive)
201619Sbill 					printf("%s:", *xargv);
202619Sbill 				printf("%s:", SELECT);
203619Sbill 			}
204619Sbill 			printf("%08x - %02x %04x %5.5s %s\n",
205619Sbill 			    symp[n].n_value,
206619Sbill 			    symp[n].n_other & 0xff, symp[n].n_desc & 0xffff,
207619Sbill 			    stab(symp[n].n_type & 0xff),
208619Sbill 			    symp[n].n_un.n_name);
209619Sbill 			continue;
210619Sbill 		}
211619Sbill 		switch (c&N_TYPE) {
212619Sbill 
213619Sbill 		case N_UNDF:
214619Sbill 			c = 'u';
215619Sbill 			if (symp[n].n_value)
216619Sbill 				c = 'c';
217619Sbill 			break;
218619Sbill 		case N_ABS:
219619Sbill 			c = 'a';
220619Sbill 			break;
221619Sbill 		case N_TEXT:
222619Sbill 			c = 't';
223619Sbill 			break;
224619Sbill 		case N_DATA:
225619Sbill 			c = 'd';
226619Sbill 			break;
227619Sbill 		case N_BSS:
228619Sbill 			c = 'b';
229619Sbill 			break;
230619Sbill 		case N_FN:
231619Sbill 			c = 'f';
232619Sbill 			break;
233619Sbill 		}
234619Sbill 		if (uflg && c!='u')
235619Sbill 			continue;
236619Sbill 		if (oflg) {
237619Sbill 			if (archive)
238619Sbill 				printf("%s:", *xargv);
239619Sbill 			printf("%s:", SELECT);
240619Sbill 		}
241619Sbill 		if (symp[n].n_type&N_EXT)
242619Sbill 			c = toupper(c);
243619Sbill 		if (!uflg) {
244619Sbill 			if (c=='u' || c=='U')
245619Sbill 				printf("        ");
246619Sbill 			else
247619Sbill 				printf(N_FORMAT, symp[n].n_value);
248619Sbill 			printf(" %c ", c);
249619Sbill 		}
250619Sbill 		printf("%s\n", symp[n].n_un.n_name);
251619Sbill l1:		;
252619Sbill 	}
253619Sbill }
254619Sbill 
255619Sbill compare(p1, p2)
256619Sbill struct nlist *p1, *p2;
257619Sbill {
258619Sbill 	register i;
259619Sbill 
260619Sbill 	if (nflg) {
261619Sbill 		if (p1->n_value > p2->n_value)
262619Sbill 			return(rflg);
263619Sbill 		if (p1->n_value < p2->n_value)
264619Sbill 			return(-rflg);
265619Sbill 	}
266619Sbill 	return (rflg * strcmp(p1->n_un.n_name, p2->n_un.n_name));
267619Sbill }
268619Sbill 
269619Sbill nextel(af)
270619Sbill FILE *af;
271619Sbill {
272619Sbill 	register char *cp;
273619Sbill 	register r;
274619Sbill 	long arsize;
275619Sbill 
276619Sbill 	fseek(af, off, 0);
277619Sbill 	r = fread((char *)&archdr, 1, sizeof(struct ar_hdr), af);
278619Sbill 	if (r != sizeof(struct ar_hdr))
279619Sbill 		return(0);
280619Sbill 	for (cp = archdr.ar_name; cp < &archdr.ar_name[sizeof(archdr.ar_name)]; cp++)
281619Sbill 		if (*cp == ' ')
282619Sbill 			*cp = '\0';
283619Sbill 	arsize = atol(archdr.ar_size);
284619Sbill 	if (arsize & 1)
285619Sbill 		++arsize;
286619Sbill 	off = ftell(af) + arsize;	/* beginning of next element */
287619Sbill 	return(1);
288619Sbill }
289619Sbill 
290619Sbill error(n, s)
291619Sbill char *s;
292619Sbill {
293619Sbill 	fprintf(stderr, "nm: %s:", *xargv);
294619Sbill 	if (archive) {
295619Sbill 		fprintf(stderr, "(%s)", archdr.ar_name);
296619Sbill 		fprintf(stderr, ": ");
297619Sbill 	} else
298619Sbill 		fprintf(stderr, " ");
299619Sbill 	fprintf(stderr, "%s\n", s);
300619Sbill 	if (n)
301619Sbill 		exit(2);
302619Sbill 	errs = 1;
303619Sbill }
304619Sbill 
305619Sbill struct	stabnames {
306619Sbill 	int	st_value;
307619Sbill 	char	*st_name;
308619Sbill } stabnames[] ={
309619Sbill 	N_GSYM, "GSYM",
310619Sbill 	N_FNAME, "FNAME",
311619Sbill 	N_FUN, "FUN",
312619Sbill 	N_STSYM, "STSYM",
313619Sbill 	N_LCSYM, "LCSYM",
314619Sbill 	N_RSYM, "RSYM",
315619Sbill 	N_SLINE, "SLINE",
316619Sbill 	N_SSYM, "SSYM",
317619Sbill 	N_SO, "SO",
318619Sbill 	N_LSYM, "LSYM",
319619Sbill 	N_SOL, "SOL",
320619Sbill 	N_PSYM, "PSYM",
321619Sbill 	N_ENTRY, "ENTRY",
322619Sbill 	N_LBRAC, "LBRAC",
323619Sbill 	N_RBRAC, "RBRAC",
324619Sbill 	N_BCOMM, "BCOMM",
325619Sbill 	N_ECOMM, "ECOMM",
326619Sbill 	N_ECOML, "ECOML",
327619Sbill 	N_LENG, "LENG",
328*859Sbill 	N_PC, "PC",
329619Sbill 	0, 0
330619Sbill };
331619Sbill 
332619Sbill char *
333619Sbill stab(val)
334619Sbill {
335619Sbill 	register struct stabnames *sp;
336619Sbill 	static char prbuf[32];
337619Sbill 
338619Sbill 	for (sp = stabnames; sp->st_name; sp++)
339619Sbill 		if (sp->st_value == val)
340619Sbill 			return (sp->st_name);
341619Sbill 	sprintf(prbuf, "%02x", val);
342619Sbill 	return (prbuf);
343619Sbill }
344