|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1980 The Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that: (1) source distributions retain this entire copyright ! 7: * notice and comment, and (2) distributions including binaries display ! 8: * the following acknowledgement: ``This product includes software ! 9: * developed by the University of California, Berkeley and its contributors'' ! 10: * in the documentation or other materials provided with the distribution ! 11: * and in all advertising materials mentioning features or use of this ! 12: * software. Neither the name of the University nor the names of its ! 13: * contributors may be used to endorse or promote products derived ! 14: * from this software without specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: char copyright[] = ! 22: "@(#) Copyright (c) 1980 The Regents of the University of California.\n\ ! 23: All rights reserved.\n"; ! 24: #endif /* not lint */ ! 25: ! 26: #ifndef lint ! 27: static char sccsid[] = "@(#)symorder.c 5.4 (Berkeley) 6/1/90"; ! 28: #endif /* not lint */ ! 29: ! 30: /* ! 31: * symorder - reorder symbol table ! 32: */ ! 33: ! 34: #include <stdio.h> ! 35: #include <sys/types.h> ! 36: #include <sys/stat.h> ! 37: #include <a.out.h> ! 38: ! 39: #define SPACE 100 ! 40: ! 41: struct nlist order[SPACE]; ! 42: ! 43: char *savestr(), *index(), *malloc(); ! 44: struct exec exec; ! 45: off_t sa; ! 46: struct stat stb; ! 47: int nsym = 0; ! 48: int symfound = 0; ! 49: char *strings; ! 50: char *newstrings; ! 51: struct nlist *symtab; ! 52: struct nlist *newtab; ! 53: int symsize; ! 54: char asym[BUFSIZ]; ! 55: ! 56: main(argc, argv) ! 57: char **argv; ! 58: { ! 59: register char *ns; ! 60: register struct nlist *symp; ! 61: register struct nlist *p; ! 62: register FILE *f; ! 63: register int i; ! 64: int n, o; ! 65: ! 66: if (argc != 3) { ! 67: fprintf(stderr, "Usage: symorder orderlist file\n"); ! 68: exit(1); ! 69: } ! 70: if ((f = fopen(argv[1], "r")) == NULL) { ! 71: perror(argv[1]); ! 72: exit(1); ! 73: } ! 74: for (p = order; fgets(asym, sizeof asym, f) != NULL; p++, nsym++) { ! 75: for (i = 0; asym[i] && asym[i] != '\n'; i++) ! 76: continue; ! 77: if (asym[i] == '\n') ! 78: asym[i] = 0; ! 79: p->n_un.n_name = savestr(asym); ! 80: } ! 81: fclose(f); ! 82: if ((f = fopen(argv[2], "r")) == NULL) ! 83: perror(argv[2]), exit(1); ! 84: if ((o = open(argv[2], 1)) < 0) ! 85: perror(argv[2]), exit(1); ! 86: if ((fread(&exec, sizeof exec, 1, f)) != 1 || N_BADMAG(exec)) { ! 87: fprintf(stderr, "symorder: %s: bad format\n", argv[2]); ! 88: exit(1); ! 89: } ! 90: if (exec.a_syms == 0) { ! 91: fprintf(stderr, "symorder: %s is stripped\n", argv[2]); ! 92: exit(1); ! 93: } ! 94: fstat(fileno(f), &stb); ! 95: if (stb.st_size < N_STROFF(exec)+sizeof(off_t)) { ! 96: fprintf(stderr, "symorder: %s is in old format or truncated\n", ! 97: argv[2]); ! 98: exit(1); ! 99: } ! 100: sa = N_SYMOFF(exec); ! 101: fseek(f, sa, 0); ! 102: n = exec.a_syms; ! 103: symtab = (struct nlist *)malloc(n); ! 104: if (symtab == (struct nlist *)0) { ! 105: fprintf(stderr, "symorder: Out of core, no space for symtab\n"); ! 106: exit(1); ! 107: } ! 108: if (fread((char *)symtab, 1, n, f) != n) { ! 109: fprintf(stderr, "symorder: Short file "); perror(argv[2]); ! 110: exit(1); ! 111: } ! 112: if (fread((char *)&symsize, sizeof (int), 1, f) != 1 || ! 113: symsize <= 0) { ! 114: fprintf(stderr, "symorder: No strings "); perror(argv[2]); ! 115: exit(1); ! 116: } ! 117: strings = malloc(symsize); ! 118: if (strings == (char *)0) { ! 119: fprintf(stderr,"symorder: Out of core, no space for strings\n"); ! 120: exit(1); ! 121: } ! 122: /* ! 123: * Need to subtract four from symsize here since ! 124: * symsize includes itself, and we've already read ! 125: * it. (6/30/85 chris@maryland) ! 126: */ ! 127: if (fread(strings, 1, symsize - 4, f) != symsize - 4) { ! 128: fprintf(stderr, "symorder: Truncated strings "); ! 129: perror(argv[2]); ! 130: exit(1); ! 131: } ! 132: ! 133: newtab = (struct nlist *)malloc(n); ! 134: if (newtab == (struct nlist *)0) { ! 135: fprintf(stderr, ! 136: "symorder: Out of core, no space for new symtab\n"); ! 137: exit(1); ! 138: } ! 139: i = n / sizeof (struct nlist); ! 140: reorder(symtab, newtab, i); ! 141: free((char *)symtab); ! 142: symtab = newtab; ! 143: ! 144: newstrings = malloc(symsize); ! 145: if (newstrings == (char *)0) { ! 146: fprintf(stderr, ! 147: "symorder: Out of core, no space for newstrings\n"); ! 148: exit(1); ! 149: } ! 150: ns = newstrings; ! 151: for (symp = symtab; --i >= 0; symp++) { ! 152: if (symp->n_un.n_strx == 0) ! 153: continue; ! 154: symp->n_un.n_strx -= sizeof (int); ! 155: if ((unsigned)symp->n_un.n_strx >= symsize) { ! 156: fprintf(stderr,"symorder: Corrupted string pointers\n"); ! 157: exit(1); ! 158: } ! 159: strcpy(ns, &strings[symp->n_un.n_strx]); ! 160: symp->n_un.n_strx = (ns - newstrings) + sizeof (int); ! 161: ns = index(ns, 0) + 1; ! 162: if (ns > &newstrings[symsize]) { ! 163: fprintf(stderr, "symorder: Strings grew longer!\n"); ! 164: exit(1); ! 165: } ! 166: } ! 167: ! 168: lseek(o, sa, 0); ! 169: if (write(o, (char *)symtab, n) != n) { ! 170: fprintf(stderr, "symorder: Write failed "); perror(argv[2]); ! 171: exit(1); ! 172: } ! 173: if (write(o, (char *)&symsize, sizeof (int)) != sizeof (int)) { ! 174: fprintf(stderr, "symorder: Write failed "); perror(argv[2]); ! 175: exit(1); ! 176: } ! 177: if (write(o, newstrings, symsize - 4) != symsize - 4) { ! 178: fprintf(stderr, "symorder: Write failed "); perror(argv[2]); ! 179: exit(1); ! 180: } ! 181: if ((i = nsym - symfound) > 0) { ! 182: fprintf(stderr, "symorder: %d symbol%s not found:\n", ! 183: i, i == 1 ? "" : "s"); ! 184: for (i = 0; i < nsym; i++) { ! 185: if (order[i].n_value == 0) ! 186: printf("%s\n", order[i].n_un.n_name); ! 187: } ! 188: } ! 189: exit(0); ! 190: } ! 191: ! 192: reorder(st1, st2, n) ! 193: register struct nlist *st1, *st2; ! 194: register n; ! 195: { ! 196: register struct nlist *stp = st2 + nsym; ! 197: register i; ! 198: ! 199: while (--n >= 0) { ! 200: i = inlist(st1); ! 201: if (i == -1) ! 202: *stp++ = *st1++; ! 203: else ! 204: st2[i] = *st1++; ! 205: } ! 206: } ! 207: ! 208: inlist(p) ! 209: register struct nlist *p; ! 210: { ! 211: register char *nam; ! 212: register struct nlist *op; ! 213: ! 214: if (p->n_type & N_STAB) ! 215: return (-1); ! 216: if (p->n_un.n_strx == 0) ! 217: return (-1); ! 218: ! 219: nam = &strings[p->n_un.n_strx - sizeof(int)]; ! 220: if (nam >= &strings[symsize]) { ! 221: fprintf(stderr, "symorder: corrupt symtab\n"); ! 222: exit(1); ! 223: } ! 224: ! 225: for (op = &order[nsym]; --op >= order; ) { ! 226: if (strcmp(op->n_un.n_name, nam) != 0) ! 227: continue; ! 228: if (op->n_value == 0) { ! 229: op->n_value++; ! 230: symfound++; ! 231: } ! 232: return (op - order); ! 233: } ! 234: return (-1); ! 235: } ! 236: ! 237: #define NSAVETAB 4096 ! 238: char *savetab; ! 239: int saveleft; ! 240: ! 241: char * ! 242: savestr(cp) ! 243: register char *cp; ! 244: { ! 245: register int len; ! 246: ! 247: len = strlen(cp) + 1; ! 248: if (len > saveleft) { ! 249: saveleft = NSAVETAB; ! 250: if (len > saveleft) ! 251: saveleft = len; ! 252: savetab = (char *)malloc(saveleft); ! 253: if (savetab == 0) { ! 254: fprintf(stderr, ! 255: "symorder: ran out of memory (savestr)\n"); ! 256: exit(1); ! 257: } ! 258: } ! 259: strncpy(savetab, cp, len); ! 260: cp = savetab; ! 261: savetab += len; ! 262: saveleft -= len; ! 263: return (cp); ! 264: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.