|
|
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:
9: #define NSYMS 30
10: #define BIGBUF 4096
11:
12: extern struct sym *symtab;
13:
14: syminit(h)
15: struct exec *h;
16: {
17: register struct nlist *q;
18: register long n, m;
19: struct nlist space[BIGBUF/sizeof(struct nlist)];
20: struct sym *sbuf;
21: register struct sym *cur;
22: struct sym *curgbl;
23: register int type, ltype;
24: long off;
25: char *malloc();
26: char *savename();
27: long lmin();
28:
29: symtab = NULL;
30: if (h->a_syms == 0)
31: return; /* stripped */
32: off = h->a_text + h->a_data;
33: if ((h->a_flag & 01) == 0)
34: off += off; /* space for reloc data */
35: lseek(fsym, off + sizeof(*h), 0);
36: curgbl = sbuf = cur = NULL;
37: for (n = h->a_syms; n > 0 ;) {
38: m = read(fsym, (char *)space, lmin(n, (long)sizeof(space)));
39: if (m <= 0)
40: break;
41: n -= m;
42: for (q = space; m > 0; q++, m-= sizeof(space[0])) {
43: switch (q->n_type) {
44: case N_ABS|N_EXT:
45: type = S_ABS;
46: break;
47:
48: case N_TEXT:
49: case N_TEXT|N_EXT:
50: type = S_TEXT;
51: break;
52:
53: case N_DATA:
54: case N_BSS: /* ?? file statics ?? */
55: case N_DATA|N_EXT:
56: case N_BSS|N_EXT:
57: type = S_DATA;
58: break;
59:
60: case N_ABS:
61: ltype = S_LSYM;
62: type = S_STAB;
63: break;
64:
65: default:
66: continue;
67: }
68: if (sbuf == NULL || ++cur >= sbuf + NSYMS) {
69: if ((sbuf = (struct sym *)malloc(sizeof(struct sym) * NSYMS)) == NULL) {
70: printf("out of mem for syms\n");
71: return;
72: }
73: cur = sbuf;
74: }
75: cur->y_type = type;
76: cur->y_ltype = ltype;
77: cur->y_value = q->n_value;
78: cur->y_name = savename(q->n_name, sizeof(q->n_name));
79: cur->y_locals = NULL;
80: if (cur->y_type == S_TEXT && q->n_name[0] == '~') {
81: cur->y_next = curgbl;
82: curgbl = cur;
83: }
84: else if (cur->y_type == S_STAB) {
85: if (curgbl == NULL)
86: continue;
87: cur->y_next = curgbl->y_locals;
88: curgbl->y_locals = cur;
89: }
90: else {
91: cur->y_next = symtab;
92: symtab = cur;
93: }
94: }
95: }
96: for (; curgbl; curgbl = curgbl->y_next) {
97: if (curgbl->y_locals == NULL)
98: continue;
99: for (cur = symtab; cur; cur = cur->y_next) {
100: if (cur->y_type != S_TEXT)
101: continue;
102: if (cur->y_value == curgbl->y_value) {
103: cur->y_locals = curgbl->y_locals;
104: break;
105: }
106: }
107: }
108: }
109:
110: static char *
111: savename(n, sz)
112: char *n;
113: register int sz;
114: {
115: char *p;
116: char *malloc();
117:
118: if ((p = malloc(sz+1)) == NULL)
119: return ("");
120: strncpy(p, n, sz);
121: p[sz] = 0;
122: return (p);
123: }
124:
125: eqsym(sp, n)
126: struct sym *sp;
127: char *n;
128: {
129:
130: return (strncmp(sp->y_name, n, 16) == 0);
131: }
132:
133: static
134: long
135: lmin(a, b)
136: long a, b;
137: {
138: if (a < b)
139: return (a);
140: else
141: return (b);
142: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.