|
|
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: case N_BFUN: ! 68: case N_TEXT|N_EXT: ! 69: type = S_TEXT; ! 70: break; ! 71: ! 72: case N_DATA: ! 73: case N_DATA|N_EXT: ! 74: case N_BSS: ! 75: case N_BSS|N_EXT: ! 76: type = S_DATA; ! 77: break; ! 78: ! 79: case N_LSYM: ! 80: ltype = S_LSYM; ! 81: type = S_STAB; ! 82: break; ! 83: ! 84: case N_RSYM: ! 85: ltype = S_RSYM; ! 86: type = S_STAB; ! 87: break; ! 88: ! 89: case N_PSYM: ! 90: ltype = S_PSYM; ! 91: type = S_STAB; ! 92: break; ! 93: ! 94: case N_STSYM: ! 95: case N_LCSYM: ! 96: ltype = S_STSYM; ! 97: type = S_STAB; ! 98: break; ! 99: ! 100: default: ! 101: continue; ! 102: } ! 103: if (sbuf == NULL || ++cur >= sbuf + NSYMS) { ! 104: if ((sbuf = (struct sym *)malloc(sizeof(struct sym) * NSYMS)) == NULL) { ! 105: printf("out of mem for syms\n"); ! 106: return; ! 107: } ! 108: cur = sbuf; ! 109: } ! 110: cur->y_type = type; ! 111: cur->y_ltype = ltype; ! 112: cur->y_value = q->n_value; ! 113: cur->y_name = &names[q->n_un.n_strx]; ! 114: cur->y_locals = NULL; ! 115: if (q->n_type == N_BFUN) { ! 116: cur->y_next = curgbl; ! 117: curgbl = cur; ! 118: } ! 119: else if (cur->y_type == S_STAB) { ! 120: if (curgbl == NULL) ! 121: continue; ! 122: cur->y_next = curgbl->y_locals; ! 123: curgbl->y_locals = cur; ! 124: } ! 125: else { ! 126: cur->y_next = symtab; ! 127: symtab = cur; ! 128: } ! 129: } ! 130: } ! 131: for (; curgbl; curgbl = curgbl->y_next) { ! 132: if (curgbl->y_locals == NULL) ! 133: continue; ! 134: for (cur = symtab; cur; cur = cur->y_next) { ! 135: if (cur->y_type != S_TEXT) ! 136: continue; ! 137: if (cur->y_value == curgbl->y_value) { ! 138: cur->y_locals = curgbl->y_locals; ! 139: break; ! 140: } ! 141: } ! 142: } ! 143: } ! 144: ! 145: /* ! 146: * is symbol table entry s == name n? ! 147: * this may depend on awful symbol conventions ! 148: * e.g. _ ! 149: */ ! 150: int ! 151: eqsym(s, n) ! 152: register struct sym *s; ! 153: char *n; ! 154: { ! 155: ! 156: if (strcmp(s->y_name, n) == 0) ! 157: return (1); ! 158: if (s->y_name[0] == '_' && strcmp(&s->y_name[1], n) == 0) ! 159: return (1); ! 160: return (0); ! 161: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.