|
|
1.1 root 1: typedef unsigned char u_char;
2: typedef unsigned short u_short;
3: typedef unsigned int u_int;
4: typedef unsigned long u_long;
5: typedef struct _physadr { int r[1]; } *physadr;
6: typedef int daddr_t;
7: typedef char * caddr_t;
8: typedef u_short ino_t;
9: typedef int swblk_t;
10: typedef int size_t;
11: typedef int time_t;
12: typedef int label_t[14];
13: typedef short dev_t;
14: typedef int off_t;
15: typedef long portid_t;
16: typedef struct fd_set { int fds_bits[1]; } fd_set;
17: struct ar_hdr {
18: char ar_name[16];
19: char ar_date[12];
20: char ar_uid[6];
21: char ar_gid[6];
22: char ar_mode[8];
23: char ar_size[10];
24: char ar_fmag[2];
25: };
26: extern struct _iobuf {
27: int _cnt;
28: char *_ptr;
29: char *_base;
30: short _flag;
31: char _file;
32: } _iob[20];
33: struct _iobuf *fopen();
34: struct _iobuf *fdopen();
35: struct _iobuf *freopen();
36: long ftell();
37: char *fgets();
38: extern char _ctype[];
39: struct exec {
40: long a_magic;
41: unsigned long a_text;
42: unsigned long a_data;
43: unsigned long a_bss;
44: unsigned long a_syms;
45: unsigned long a_entry;
46: unsigned long a_trsize;
47: unsigned long a_drsize;
48: };
49: struct relocation_info {
50: int r_address;
51: unsigned int r_symbolnum:24,
52: r_pcrel:1,
53: r_length:2,
54: r_extern:1,
55: :4;
56: };
57: struct nlist {
58: union {
59: char *n_name;
60: long n_strx;
61: } n_un;
62: unsigned char n_type;
63: char n_other;
64: short n_desc;
65: unsigned long n_value;
66: };
67: struct stat
68: {
69: dev_t st_dev;
70: ino_t st_ino;
71: unsigned short st_mode;
72: short st_nlink;
73: short st_uid;
74: short st_gid;
75: dev_t st_rdev;
76: off_t st_size;
77: time_t st_atime;
78: time_t st_mtime;
79: time_t st_ctime;
80: };
81: int aflg, gflg, nflg, oflg, pflg, uflg;
82: int rflg = 1;
83: char **xargv;
84: int archive;
85: struct ar_hdr archdr;
86: union {
87: char mag_armag[8+1];
88: struct exec mag_exp;
89: } mag_un;
90: struct _iobuf *fi;
91: off_t off;
92: off_t ftell();
93: char *malloc();
94: char *realloc();
95: char *strp;
96: char *stab();
97: off_t strsiz;
98: int compare();
99: int narg;
100: int errs;
101: main(argc, argv)
102: char **argv;
103: {
104: if (--argc>0 && argv[1][0]=='-' && argv[1][1]!=0) {
105: argv++;
106: while (*++*argv) switch (**argv) {
107: case 'n':
108: nflg++;
109: continue;
110: case 'g':
111: gflg++;
112: continue;
113: case 'u':
114: uflg++;
115: continue;
116: case 'r':
117: rflg = -1;
118: continue;
119: case 'p':
120: pflg++;
121: continue;
122: case 'o':
123: oflg++;
124: continue;
125: case 'a':
126: aflg++;
127: continue;
128: default:
129: fprintf((&_iob[2]), "nm: invalid argument -%c\n",
130: *argv[0]);
131: exit(2);
132: }
133: argc--;
134: }
135: if (argc == 0) {
136: argc = 1;
137: argv[1] = "a.out";
138: }
139: narg = argc;
140: xargv = argv;
141: while (argc--) {
142: ++xargv;
143: namelist();
144: }
145: exit(errs);
146: }
147: namelist()
148: {
149: register int j;
150: archive = 0;
151: fi = fopen(*xargv, "r");
152: if (fi == 0) {
153: error(0, "cannot open");
154: return;
155: }
156: off = 8;
157: fread((char *)&mag_un, 1, sizeof(mag_un), fi);
158: if (mag_un.mag_exp.a_magic == 0177545) {
159: error(0, "old archive");
160: return;
161: }
162: if (strncmp(mag_un.mag_armag, "!<arch>\n", 8)==0)
163: archive++;
164: else if ( (((mag_un.mag_exp).a_magic)!=0407 && ((mag_un.mag_exp).a_magic)!=0410 && ((mag_un.mag_exp).a_magic)!=0413 )) {
165: error(0, "bad format");
166: return;
167: }
168: fseek(fi, 0L, 0);
169: if (archive) {
170: nextel(fi);
171: if (narg > 1)
172: printf("\n%s:\n", *xargv);
173: }
174: do {
175: off_t o;
176: register i, n;
177: struct nlist *symp = 0;
178: struct nlist sym;
179: struct stat stb;
180: fread((char *)&mag_un.mag_exp, 1, sizeof(struct exec), fi);
181: if ( (((mag_un.mag_exp).a_magic)!=0407 && ((mag_un.mag_exp).a_magic)!=0410 && ((mag_un.mag_exp).a_magic)!=0413 ))
182: continue;
183: if (archive == 0)
184: fstat( ((fi)->_file), &stb);
185: o = ( ((mag_un.mag_exp).a_magic==0413 ? 1024 : sizeof (struct exec)) + (mag_un.mag_exp).a_text+(mag_un.mag_exp).a_data + (mag_un.mag_exp).a_trsize+(mag_un.mag_exp).a_drsize) - sizeof (struct exec);
186: fseek(fi, o, 1);
187: n = mag_un.mag_exp.a_syms / sizeof(struct nlist);
188: if (n == 0) {
189: error(0, "no name list");
190: continue;
191: }
192: if ( ( ( ((mag_un.mag_exp).a_magic==0413 ? 1024 : sizeof (struct exec)) + (mag_un.mag_exp).a_text+(mag_un.mag_exp).a_data + (mag_un.mag_exp).a_trsize+(mag_un.mag_exp).a_drsize) + (mag_un.mag_exp).a_syms) + sizeof (off_t) >
193: (archive ? off : stb.st_size))
194: error(1, "old format .o (no string table) or truncated file");
195: i = 0;
196: if (strp)
197: free(strp), strp = 0;
198: while (--n >= 0) {
199: fread((char *)&sym, 1, sizeof(sym), fi);
200: if (gflg && (sym.n_type&01 )==0)
201: continue;
202: if ((sym.n_type&0xe0 ) && (!aflg||gflg||uflg))
203: continue;
204: if (symp==0)
205: symp = (struct nlist *)
206: malloc(sizeof(struct nlist));
207: else
208: symp = (struct nlist *)
209: realloc(symp,
210: (i+1)*sizeof(struct nlist));
211: if (symp == 0)
212: error(1, "out of memory");
213: symp[i++] = sym;
214: }
215: if (archive && ftell(fi)+sizeof(off_t) >= off) {
216: error(0, "no string table (old format .o?)");
217: continue;
218: }
219: if (fread((char *)&strsiz,sizeof(strsiz),1,fi) != 1) {
220: error(0, "no string table (old format .o?)");
221: goto out;
222: }
223: strp = (char *)malloc(strsiz);
224: if (strp == 0)
225: error(1, "ran out of memory");
226: if (fread(strp+sizeof(strsiz),strsiz-sizeof(strsiz),1,fi) != 1)
227: error(1, "error reading string table");
228: for (j = 0; j < i; j++)
229: if (symp[j].n_un.n_strx)
230: symp[j].n_un.n_name =
231: symp[j].n_un.n_strx + strp;
232: else
233: symp[j].n_un.n_name = "";
234: if (pflg==0)
235: qsort(symp, i, sizeof(struct nlist), compare);
236: if ((archive || narg>1) && oflg==0)
237: printf("\n%s:\n", archive ? archdr.ar_name : *xargv);
238: psyms(symp, i);
239: if (symp)
240: free((char *)symp), symp = 0;
241: if (strp)
242: free((char *)strp), strp = 0;
243: } while(archive && nextel(fi));
244: out:
245: fclose(fi);
246: }
247: psyms(symp, nsyms)
248: register struct nlist *symp;
249: int nsyms;
250: {
251: register int n, c;
252: for (n=0; n<nsyms; n++) {
253: c = symp[n].n_type;
254: if (c & 0xe0 ) {
255: if (oflg) {
256: if (archive)
257: printf("%s:", *xargv);
258: printf("%s:", archive ? archdr.ar_name : *xargv);
259: }
260: printf("%08x - %02x %04x %5.5s %s\n",
261: symp[n].n_value,
262: symp[n].n_other & 0xff, symp[n].n_desc & 0xffff,
263: stab(symp[n].n_type & 0xff),
264: symp[n].n_un.n_name);
265: continue;
266: }
267: switch (c&0x1e ) {
268: case 0x0 :
269: c = 'u';
270: if (symp[n].n_value)
271: c = 'c';
272: break;
273: case 0x2 :
274: c = 'a';
275: break;
276: case 0x4 :
277: c = 't';
278: break;
279: case 0x6 :
280: c = 'd';
281: break;
282: case 0x8 :
283: c = 'b';
284: break;
285: case 0x1f :
286: c = 'f';
287: break;
288: }
289: if (uflg && c!='u')
290: continue;
291: if (oflg) {
292: if (archive)
293: printf("%s:", *xargv);
294: printf("%s:", archive ? archdr.ar_name : *xargv);
295: }
296: if (symp[n].n_type&01 )
297: c = toupper(c);
298: if (!uflg) {
299: if (c=='u' || c=='U')
300: printf(" ");
301: else
302: printf("%08x", symp[n].n_value);
303: printf(" %c ", c);
304: }
305: printf("%s\n", symp[n].n_un.n_name);
306: l1: ;
307: }
308: }
309: compare(p1, p2)
310: struct nlist *p1, *p2;
311: {
312: if (nflg) {
313: if (p1->n_value > p2->n_value)
314: return(rflg);
315: if (p1->n_value < p2->n_value)
316: return(-rflg);
317: }
318: return (rflg * strcmp(p1->n_un.n_name, p2->n_un.n_name));
319: }
320: nextel(af)
321: struct _iobuf *af;
322: {
323: register char *cp;
324: register r;
325: long arsize;
326: fseek(af, off, 0);
327: r = fread((char *)&archdr, 1, sizeof(struct ar_hdr), af);
328: if (r != sizeof(struct ar_hdr))
329: return(0);
330: for (cp = archdr.ar_name; cp < &archdr.ar_name[sizeof(archdr.ar_name)]; cp++)
331: if (*cp == ' ')
332: *cp = '\0';
333: arsize = atol(archdr.ar_size);
334: if (arsize & 1)
335: ++arsize;
336: off = ftell(af) + arsize;
337: return(1);
338: }
339: error(n, s)
340: char *s;
341: {
342: fprintf((&_iob[2]), "nm: %s:", *xargv);
343: if (archive) {
344: fprintf((&_iob[2]), "(%s)", archdr.ar_name);
345: fprintf((&_iob[2]), ": ");
346: } else
347: fprintf((&_iob[2]), " ");
348: fprintf((&_iob[2]), "%s\n", s);
349: if (n)
350: exit(2);
351: errs = 1;
352: }
353: struct stabnames {
354: int st_value;
355: char *st_name;
356: } stabnames[] ={
357: 0x20 , "GSYM",
358: 0x22 , "FNAME",
359: 0x24 , "FUN",
360: 0x26 , "STSYM",
361: 0x28 , "LCSYM",
362: 0x40 , "RSYM",
363: 0x44 , "SLINE",
364: 0x60 , "SSYM",
365: 0x64 , "SO",
366: 0x80 , "LSYM",
367: 0x84 , "SOL",
368: 0xa0 , "PSYM",
369: 0xa4 , "ENTRY",
370: 0xc0 , "LBRAC",
371: 0xe0 , "RBRAC",
372: 0xe2 , "BCOMM",
373: 0xe4 , "ECOMM",
374: 0xe8 , "ECOML",
375: 0xfe , "LENG",
376: 0x30 , "PC",
377: 0, 0
378: };
379: char *
380: stab(val)
381: {
382: register struct stabnames *sp;
383: static char prbuf[32];
384: for (sp = stabnames; sp->st_name; sp++)
385: if (sp->st_value == val)
386: return (sp->st_name);
387: sprintf(prbuf, "%02x", val);
388: return (prbuf);
389: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.