|
|
1.1 ! root 1: /* ! 2: * Strip symbols, lines, and relocation from executable. ! 3: */ ! 4: #include <misc.h> ! 5: #include <errno.h> ! 6: #include <coff.h> ! 7: #include <setjmp.h> ! 8: #include <sys/stat.h> ! 9: ! 10: extern char *alloca(); ! 11: ! 12: static jmp_buf env; /* setjmp longjmp buffer */ ! 13: static char *filen; /* current file in process */ ! 14: static int errCt; ! 15: ! 16: /* ! 17: * Put message and longjmp to next file. ! 18: */ ! 19: void ! 20: fatal(s) ! 21: char *s; ! 22: { ! 23: fprintf(stderr, "strip: %s %r.\n", filen, &s); ! 24: errCt++; ! 25: longjmp(env, 1); ! 26: } ! 27: ! 28: /* ! 29: * Strip a file ! 30: */ ! 31: strip() ! 32: { ! 33: register SCNHDR *sh; ! 34: register FILEHDR *fh; ! 35: int fd, rv; ! 36: long i, top, hi; ! 37: struct stat sb; ! 38: ! 39: /* inhale input file */ ! 40: if (-1 == (fd = open(filen, 0))) ! 41: fatal("Can't open for read"); ! 42: ! 43: if (-1 == fstat(fd, &sb)) ! 44: fatal("Can't find file attributes"); ! 45: ! 46: fh = alloca(sb.st_size); /* get space to inhale file */ ! 47: ! 48: rv = read(fd, fh, sb.st_size); ! 49: close(fd); ! 50: ! 51: if (-1 == rv) ! 52: fatal("Error in read"); ! 53: ! 54: if ((fh->f_magic != C_386_MAGIC) || ! 55: !fh->f_opthdr || ! 56: !(fh->f_flags & F_EXEC)) ! 57: fatal("Not COFF executable"); ! 58: ! 59: fh->f_symptr = fh->f_nsyms = 0; ! 60: fh->f_flags |= F_RELFLG | F_LNNO | F_LSYMS; ! 61: ! 62: /* pass segments and find top address */ ! 63: sh = ((char *)fh) + fh->f_opthdr + sizeof(*fh); ! 64: top = (long)(sh + fh->f_nscns); ! 65: for (top = i = 0; i < fh->f_nscns; i++, sh++) { ! 66: /* find top of sector data */ ! 67: if (sh->s_scnptr && (sh->s_flags != STYP_BSS)) { ! 68: hi = sh->s_size + sh->s_scnptr; ! 69: if (top < hi) ! 70: top = hi; ! 71: } ! 72: sh->s_relptr = sh->s_lnnoptr = sh->s_nreloc = sh->s_nlnno = 0; ! 73: } ! 74: ! 75: if (top > sb.st_size) ! 76: fatal("Corrupt file"); ! 77: ! 78: if (top < sb.st_size) { /* file not already stripped */ ! 79: /* exhale stripped file */ ! 80: if (-1 == (fd = creat(filen, sb.st_mode))) ! 81: fatal("Can't create new copy of file"); ! 82: ! 83: rv = write(fd, fh, top); ! 84: close(fd); ! 85: ! 86: if (-1 == rv) ! 87: fatal("Error in write"); ! 88: } ! 89: } ! 90: ! 91: main(argc, argv) ! 92: char *argv[]; ! 93: { ! 94: register int i; ! 95: ! 96: for (i = 1; i < argc; i++) { ! 97: filen = argv[i]; ! 98: if (!setjmp(env)) ! 99: strip(); ! 100: } ! 101: ! 102: if(!errCt) ! 103: return (0); ! 104: ! 105: fprintf(stderr, "%d error(s) flagged\n", errCt); ! 106: return (1); ! 107: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.