|
|
1.1 ! root 1: static char sccsid[] = "@(#)symfix.c 2.4"; ! 2: ! 3: #include <stdio.h> ! 4: #include <fcntl.h> ! 5: #include <a.out.h> ! 6: #include "basic.h" ! 7: ! 8: long lseek(); ! 9: ! 10: typedef struct exec EXECR, *pEXECR; ! 11: #define cbEXECR sizeof(EXECR) ! 12: ! 13: /* stuff directly related to the symbol table */ ! 14: typedef struct SYMS { /* same record as `nlist, but with my names */ ! 15: char name[8]; /* symbol name */ ! 16: int type; /* type flag */ ! 17: uint value; /* something appropriate to the type */ ! 18: } ! 19: SYMR, *pSYMR; ! 20: #define cbSYMR sizeof(SYMR) ! 21: ! 22: ! 23: FLAGT vfDirty; /* indicates current cache has been mofified */ ! 24: short vfnSym; ! 25: short visymMax; /* number of symbols */ ! 26: long vcbSymFirst; /* byte offset to first symbol */ ! 27: #define iCacheMax 1024 /* number of symbols in cache */ ! 28: SYMR vsymCache[iCacheMax]; /* symbol cache */ ! 29: short viCacheMac; /* max index into cache */ ! 30: #define cbSymCache sizeof(vsymCache) ! 31: short viSMin, viSMax; /* min/max values for what is in cache */ ! 32: ! 33: char sbHdrLn[] = "~_l"; ! 34: short cbHdrLn = 3; ! 35: ! 36: ! 37: /* I N I T S Y M F I L E */ ! 38: ! 39: void InitSymfile(sbSymName) ! 40: char *sbSymName; ! 41: { ! 42: short magic, relflg; ! 43: long cbData, cbText; ! 44: EXECR execSym; ! 45: ! 46: /* printf("Opening file %s\n", sbSymName); */ ! 47: vfnSym = open(sbSymName, O_RDWR); ! 48: if (vfnSym < 0) { ! 49: perror(sbSymName); ! 50: exit(1); ! 51: } ! 52: ! 53: if (read(vfnSym, &execSym, cbEXECR) == cbEXECR) { ! 54: visymMax = execSym.a_syms / cbSYMR; ! 55: cbData = execSym.a_data; ! 56: cbText = execSym.a_text; ! 57: vcbSymFirst = cbText + cbData; /* will be adjusted at end */ ! 58: if (execSym.a_flag != 1) ! 59: vcbSymFirst <<= 1; ! 60: vcbSymFirst += cbEXECR; ! 61: } ! 62: else { ! 63: perror("Bad read of file header"); ! 64: exit(1); ! 65: } ! 66: vfDirty = false; ! 67: } /* InitSymfile */ ! 68: ! 69: ! 70: /* W R I T E C A C H E */ ! 71: ! 72: void WriteCache() ! 73: { ! 74: long ret, l1, l2, offset; ! 75: short cbWrite, x; ! 76: ! 77: l1 = viSMin; ! 78: l2 = cbSYMR; ! 79: offset = vcbSymFirst + (l1 * l2); ! 80: /* we write out this buffer before reading in the next */ ! 81: if ((ret=lseek(vfnSym, offset, 0)) < 0l) { ! 82: perror("Bad seek in WriteCache"); ! 83: exit(1); ! 84: } ! 85: l1 = viSMax - viSMin; ! 86: cbWrite = l2 * l1; ! 87: x = write(vfnSym, vsymCache, cbWrite); ! 88: if (x < 0) { ! 89: perror("Bad write in WriteCache"); ! 90: exit(1); ! 91: } ! 92: vfDirty = false; ! 93: } /* WriteCache */ ! 94: ! 95: ! 96: /* S E T C A C H E */ ! 97: ! 98: void SetCache(isym) ! 99: short isym; ! 100: { ! 101: short cRead; ! 102: long l1, l2, ret, offset; ! 103: ! 104: if (isym >= viSMin AND isym < viSMax) ! 105: return; /* it is already in there, somewhere */ ! 106: if (vfDirty) { ! 107: WriteCache(); ! 108: } ! 109: l1 = isym; ! 110: l2 = cbSYMR; ! 111: offset = vcbSymFirst + (l1 * l2); ! 112: if ((ret=lseek(vfnSym, offset, 0)) < 0l) { ! 113: printf("ret = %ld l1 %ld l2 %ld offset %ld\n", ret, l1, l2, offset); ! 114: perror("Bad seek in SetCache"); ! 115: exit(1); ! 116: } ! 117: cRead = read(vfnSym, vsymCache, cbSymCache); ! 118: if (cRead < 0) { ! 119: perror("Bad read in SetCache"); ! 120: exit(1); ! 121: } ! 122: viSMin = isym; ! 123: viCacheMac = cRead / cbSYMR; ! 124: viSMax = isym + viCacheMac; ! 125: } /* SetCache */ ! 126: ! 127: ! 128: /* M U N C H S Y M S */ ! 129: ! 130: void MunchSyms() ! 131: { ! 132: short isym, i; ! 133: pSYMR sym; ! 134: ! 135: viSMin = 0; ! 136: viSMax = -1; ! 137: isym = 0; ! 138: ! 139: while (isym < visymMax) { ! 140: SetCache(isym); ! 141: for (i=(isym-viSMin); i<viCacheMac; i++, isym++) { ! 142: sym = vsymCache + i; ! 143: if (sym->type == N_TEXT) { ! 144: if ( (strncmp(sym->name, sbHdrLn, cbHdrLn) == 0) ! 145: AND ((sym->name[cbHdrLn] == 'N') ! 146: OR (sym->name[cbHdrLn] == 'E')) ) { ! 147: /* this is either line number or proc end number */ ! 148: sym->type = N_ABS; ! 149: vfDirty = true; ! 150: } ! 151: } ! 152: } ! 153: } ! 154: if (vfDirty) ! 155: WriteCache(); ! 156: close(vfnSym); ! 157: } /* MunchSyms */ ! 158: ! 159: ! 160: /* M A I N */ ! 161: ! 162: void main(argc, argv) ! 163: int argc; ! 164: char *argv[]; ! 165: { ! 166: FLAGT fDidOne; ! 167: short i; ! 168: char *cp; ! 169: ! 170: fDidOne = false; ! 171: ! 172: for (i=1; i<argc; ++i) { ! 173: if (*(cp=argv[i]) != '-') { ! 174: fDidOne = true; ! 175: InitSymfile(cp); ! 176: MunchSyms(); ! 177: } ! 178: } /* for */ ! 179: ! 180: if (!fDidOne) { ! 181: /* then let's assume they want a.out done */ ! 182: InitSymfile("a.out"); ! 183: MunchSyms(); ! 184: } ! 185: exit(0); ! 186: } /* main */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.