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