|
|
1.1 root 1: /*
2: * init the symbol table
3: */
4:
5: #include "defs.h"
6: #include "sym.h"
7: #include <a.out.h>
8: #include <stab.h>
9: #include <sys/types.h>
10:
11: #define NSYMS 300
12: #define BIGBUF (4*4096)
13:
14: extern struct sym *symtab;
15:
16: syminit(h)
17: struct exec *h;
18: {
19: register struct nlist *q;
20: register n, m;
21: int strsiz;
22: struct nlist space[BIGBUF/sizeof(struct nlist)];
23: struct sym *sbuf;
24: register struct sym *cur;
25: struct sym *curgbl;
26: register char *names;
27: register int type, ltype;
28: char *malloc();
29:
30: symtab = NULL;
31: if (h->a_syms == 0)
32: return; /* stripped */
33: lseek(fsym, (off_t)N_STROFF(*h), 0);
34: if (read(fsym, &strsiz, sizeof(strsiz)) != sizeof(strsiz)) {
35: printf("can't get string table size\n");
36: return;
37: }
38: if ((names = malloc(strsiz)) == NULL) {
39: printf("no mem for strings\n");
40: return;
41: }
42: lseek(fsym, (off_t)N_STROFF(*h), 0);
43: if (read(fsym, names, strsiz) != strsiz) {
44: printf("error reading strings\n");
45: return;
46: }
47: lseek(fsym, (off_t)N_SYMOFF(*h), 0);
48: curgbl = sbuf = cur = NULL;
49: for (n = h->a_syms; n > 0 ;) {
50: m = read(fsym, (char *)space, min(n, sizeof(space)));
51: if (m <= 0)
52: break;
53: n -= m;
54: for (q = space; m > 0; q++, m-= sizeof(space[0])) {
55: if (q->n_un.n_strx == 0)
56: continue;
57: switch (q->n_type) {
58: case N_ABS:
59: case N_ABS|N_EXT:
60: type = S_ABS;
61: break;
62:
63: case N_TEXT:
64: if (names[q->n_un.n_strx] != '_')
65: continue;
66: /* fall in */
67: #if NOTDEF
68: case N_BFUN:
69: #endif
70: case N_TEXT|N_EXT:
71: type = S_TEXT;
72: break;
73:
74: case N_DATA:
75: case N_DATA|N_EXT:
76: case N_BSS:
77: case N_BSS|N_EXT:
78: type = S_DATA;
79: break;
80:
81: #if NOTDEF
82: case N_LSYM:
83: ltype = S_LSYM;
84: type = S_STAB;
85: break;
86:
87: case N_RSYM:
88: ltype = S_RSYM;
89: type = S_STAB;
90: break;
91:
92: case N_PSYM:
93: ltype = S_PSYM;
94: type = S_STAB;
95: break;
96:
97: case N_STSYM:
98: case N_LCSYM:
99: ltype = S_STSYM;
100: type = S_STAB;
101: break;
102: #endif
103:
104: default:
105: continue;
106: }
107: if (sbuf == NULL || ++cur >= sbuf + NSYMS) {
108: if ((sbuf = (struct sym *)malloc(sizeof(struct sym) * NSYMS)) == NULL) {
109: printf("out of mem for syms\n");
110: return;
111: }
112: cur = sbuf;
113: }
114: cur->y_type = type;
115: cur->y_ltype = ltype;
116: cur->y_value = q->n_value;
117: cur->y_name = &names[q->n_un.n_strx];
118: cur->y_locals = NULL;
119: if (q->n_type == N_FUN) {
120: cur->y_next = curgbl;
121: curgbl = cur;
122: }
123: else if (cur->y_type == S_STAB) {
124: if (curgbl == NULL)
125: continue;
126: cur->y_next = curgbl->y_locals;
127: curgbl->y_locals = cur;
128: }
129: else {
130: cur->y_next = symtab;
131: symtab = cur;
132: }
133: }
134: }
135: for (; curgbl; curgbl = curgbl->y_next) {
136: if (curgbl->y_locals == NULL)
137: continue;
138: for (cur = symtab; cur; cur = cur->y_next) {
139: if (cur->y_type != S_TEXT)
140: continue;
141: if (cur->y_value == curgbl->y_value) {
142: cur->y_locals = curgbl->y_locals;
143: break;
144: }
145: }
146: }
147: }
148:
149: /*
150: * is symbol table entry s == name n?
151: * this may depend on awful symbol conventions
152: * e.g. _
153: */
154: int
155: eqsym(s, n)
156: register struct sym *s;
157: char *n;
158: {
159:
160: if (strcmp(s->y_name, n) == 0)
161: return (1);
162: if (s->y_name[0] == '_' && strcmp(&s->y_name[1], n) == 0)
163: return (1);
164: return (0);
165: }
166:
167: static
168: min(a, b)
169: {
170: if (a < b)
171: return (a);
172: else
173: return (b);
174: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.