Annotation of src/usr.bin/strip/strip.c, Revision 1.1
1.1 ! deraadt 1: /*
! 2: * Copyright (c) 1988 Regents of the University of California.
! 3: * All rights reserved.
! 4: *
! 5: * Redistribution and use in source and binary forms, with or without
! 6: * modification, are permitted provided that the following conditions
! 7: * are met:
! 8: * 1. Redistributions of source code must retain the above copyright
! 9: * notice, this list of conditions and the following disclaimer.
! 10: * 2. Redistributions in binary form must reproduce the above copyright
! 11: * notice, this list of conditions and the following disclaimer in the
! 12: * documentation and/or other materials provided with the distribution.
! 13: * 3. All advertising materials mentioning features or use of this software
! 14: * must display the following acknowledgement:
! 15: * This product includes software developed by the University of
! 16: * California, Berkeley and its contributors.
! 17: * 4. Neither the name of the University nor the names of its contributors
! 18: * may be used to endorse or promote products derived from this software
! 19: * without specific prior written permission.
! 20: *
! 21: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 24: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 31: * SUCH DAMAGE.
! 32: */
! 33:
! 34: #ifndef lint
! 35: char copyright[] =
! 36: "@(#) Copyright (c) 1988 Regents of the University of California.\n\
! 37: All rights reserved.\n";
! 38: #endif /* not lint */
! 39:
! 40: #ifndef lint
! 41: /*static char sccsid[] = "from: @(#)strip.c 5.8 (Berkeley) 11/6/91";*/
! 42: static char rcsid[] = "$Id: strip.c,v 1.13 1994/03/28 02:17:50 cgd Exp $";
! 43: #endif /* not lint */
! 44:
! 45: #include <sys/types.h>
! 46: #include <sys/stat.h>
! 47: #include <sys/mman.h>
! 48: #include <fcntl.h>
! 49: #include <errno.h>
! 50: #include <a.out.h>
! 51: #include <unistd.h>
! 52: #include <stdio.h>
! 53: #include <stdlib.h>
! 54: #include <string.h>
! 55:
! 56: typedef struct exec EXEC;
! 57: typedef struct nlist NLIST;
! 58:
! 59: #define strx n_un.n_strx
! 60:
! 61: void err __P((const char *fmt, ...));
! 62: int s_stab __P((const char *, int, EXEC *, struct stat *));
! 63: int s_sym __P((const char *, int, EXEC *, struct stat *));
! 64: void usage __P((void));
! 65:
! 66: int xflag = 0;
! 67:
! 68: main(argc, argv)
! 69: int argc;
! 70: char *argv[];
! 71: {
! 72: register int fd, nb;
! 73: EXEC *ep;
! 74: struct stat sb;
! 75: int (*sfcn)__P((const char *, int, EXEC *, struct stat *));
! 76: int ch, errors;
! 77: char *fn;
! 78:
! 79: sfcn = s_sym;
! 80: while ((ch = getopt(argc, argv, "dx")) != EOF)
! 81: switch(ch) {
! 82: case 'x':
! 83: xflag = 1;
! 84: /*FALLTHROUGH*/
! 85: case 'd':
! 86: sfcn = s_stab;
! 87: break;
! 88: case '?':
! 89: default:
! 90: usage();
! 91: }
! 92: argc -= optind;
! 93: argv += optind;
! 94:
! 95: errors = 0;
! 96: #define ERROR(x) errors |= 1; err("%s: %s", fn, strerror(x)); continue;
! 97: while (fn = *argv++) {
! 98: if ((fd = open(fn, O_RDWR)) < 0) {
! 99: ERROR(errno);
! 100: }
! 101: if (fstat(fd, &sb)) {
! 102: (void)close(fd);
! 103: ERROR(errno);
! 104: }
! 105: if (sb.st_size < sizeof(EXEC)) {
! 106: (void)close(fd);
! 107: ERROR(EFTYPE);
! 108: }
! 109: if ((ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE,
! 110: MAP_SHARED, fd, (off_t)0)) == (EXEC *)-1) {
! 111: (void)close(fd);
! 112: ERROR(errno);
! 113: }
! 114: if (N_BADMAG(*ep)) {
! 115: munmap((caddr_t)ep, sb.st_size);
! 116: (void)close(fd);
! 117: ERROR(EFTYPE);
! 118: }
! 119: errors |= sfcn(fn, fd, ep, &sb);
! 120: munmap((caddr_t)ep, sb.st_size);
! 121: if (close(fd)) {
! 122: ERROR(errno);
! 123: }
! 124: }
! 125: #undef ERROR
! 126: exit(errors);
! 127: }
! 128:
! 129: int
! 130: s_sym(fn, fd, ep, sp)
! 131: const char *fn;
! 132: int fd;
! 133: register EXEC *ep;
! 134: struct stat *sp;
! 135: {
! 136: register char *neweof, *mineof;
! 137: int zmagic;
! 138:
! 139: zmagic = ep->a_data &&
! 140: (N_GETMAGIC(*ep) == ZMAGIC || N_GETMAGIC(*ep) == QMAGIC);
! 141:
! 142: /*
! 143: * If no symbols or data/text relocation info and
! 144: * the file data segment size is already minimized, quit.
! 145: */
! 146: if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize) {
! 147: #if 0
! 148: if (!zmagic)
! 149: return 0;
! 150: if (sp->st_size < N_TRELOFF(*ep))
! 151: #endif
! 152: return 0;
! 153: }
! 154:
! 155: /*
! 156: * New file size is the header plus text and data segments; OMAGIC
! 157: * and NMAGIC formats have the text/data immediately following the
! 158: * header. ZMAGIC format wastes the rest of of header page.
! 159: */
! 160: neweof = (char *)ep + N_TRELOFF(*ep);
! 161:
! 162: #if 0
! 163: /*
! 164: * Unfortunately, this can't work correctly without changing the way
! 165: * the loader works. We could cap it at one page, or even fiddle with
! 166: * a_data and a_bss, but this only works for CLBYTES == NBPG. If we
! 167: * are on a system where, e.g., CLBYTES is 8k and NBPG is 4k, and we
! 168: * happen to remove 4.5k, we will lose. And we really don't want to
! 169: * fiddle with pages, because that breaks binary compatibility. Lose.
! 170: */
! 171:
! 172: if (zmagic) {
! 173: /*
! 174: * Get rid of unneeded zeroes at the end of the data segment
! 175: * to reduce the file size even more.
! 176: */
! 177: mineof = (char *)ep + N_DATOFF(*ep);
! 178: while (neweof > mineof && neweof[-1] == '\0')
! 179: neweof--;
! 180: }
! 181: #endif
! 182:
! 183: /* Set symbol size and relocation info values to 0. */
! 184: ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
! 185:
! 186: /* Truncate the file. */
! 187: if (ftruncate(fd, neweof - (char *)ep)) {
! 188: err("%s: %s", fn, strerror(errno));
! 189: return 1;
! 190: }
! 191:
! 192: return 0;
! 193: }
! 194:
! 195: int
! 196: s_stab(fn, fd, ep, sp)
! 197: const char *fn;
! 198: int fd;
! 199: EXEC *ep;
! 200: struct stat *sp;
! 201: {
! 202: register int cnt, len, nsymcnt;
! 203: register char *nstr, *nstrbase, *p, *strbase;
! 204: register NLIST *sym, *nsym;
! 205: NLIST *symbase;
! 206:
! 207: /* Quit if no symbols. */
! 208: if (ep->a_syms == 0)
! 209: return 0;
! 210:
! 211: if (N_SYMOFF(*ep) >= sp->st_size) {
! 212: err("%s: bad symbol table", fn);
! 213: return 1;
! 214: }
! 215:
! 216: /*
! 217: * Initialize old and new symbol pointers. They both point to the
! 218: * beginning of the symbol table in memory, since we're deleting
! 219: * entries.
! 220: */
! 221: sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
! 222:
! 223: /*
! 224: * Allocate space for the new string table, initialize old and
! 225: * new string pointers. Handle the extra long at the beginning
! 226: * of the string table.
! 227: */
! 228: strbase = (char *)ep + N_STROFF(*ep);
! 229: if ((nstrbase = malloc((u_int)*(u_long *)strbase)) == NULL) {
! 230: err("%s", strerror(ENOMEM));
! 231: return 1;
! 232: }
! 233: nstr = nstrbase + sizeof(u_long);
! 234:
! 235: /*
! 236: * Read through the symbol table. For each non-debugging symbol,
! 237: * copy it and save its string in the new string table. Keep
! 238: * track of the number of symbols.
! 239: */
! 240: for (cnt = ep->a_syms / sizeof(NLIST); cnt--; ++sym)
! 241: if (!(sym->n_type & N_STAB) && sym->strx) {
! 242: *nsym = *sym;
! 243: nsym->strx = nstr - nstrbase;
! 244: p = strbase + sym->strx;
! 245: if (xflag &&
! 246: (!(sym->n_type & N_EXT) ||
! 247: (sym->n_type & ~N_EXT) == N_FN ||
! 248: strcmp(p, "gcc_compiled.") == 0 ||
! 249: strcmp(p, "gcc2_compiled.") == 0 ||
! 250: strncmp(p, "___gnu_compiled_", 16) == 0)) {
! 251: continue;
! 252: }
! 253: len = strlen(p) + 1;
! 254: bcopy(p, nstr, len);
! 255: nstr += len;
! 256: ++nsym;
! 257: }
! 258:
! 259: /* Fill in new symbol table size. */
! 260: ep->a_syms = (nsym - symbase) * sizeof(NLIST);
! 261:
! 262: /* Fill in the new size of the string table. */
! 263: *(u_long *)nstrbase = len = nstr - nstrbase;
! 264:
! 265: /*
! 266: * Copy the new string table into place. Nsym should be pointing
! 267: * at the address past the last symbol entry.
! 268: */
! 269: bcopy(nstrbase, (void *)nsym, len);
! 270: free(nstrbase);
! 271:
! 272: /* Truncate to the current length. */
! 273: if (ftruncate(fd, (char *)nsym + len - (char *)ep)) {
! 274: err("%s: %s", fn, strerror(errno));
! 275: return 1;
! 276: }
! 277:
! 278: return 0;
! 279: }
! 280:
! 281: void
! 282: usage()
! 283: {
! 284: (void)fprintf(stderr, "usage: strip [-dx] file ...\n");
! 285: exit(1);
! 286: }
! 287:
! 288: #if __STDC__
! 289: #include <stdarg.h>
! 290: #else
! 291: #include <varargs.h>
! 292: #endif
! 293:
! 294: void
! 295: #if __STDC__
! 296: err(const char *fmt, ...)
! 297: #else
! 298: err(fmt, va_alist)
! 299: char *fmt;
! 300: va_dcl
! 301: #endif
! 302: {
! 303: va_list ap;
! 304: #if __STDC__
! 305: va_start(ap, fmt);
! 306: #else
! 307: va_start(ap);
! 308: #endif
! 309: (void)fprintf(stderr, "strip: ");
! 310: (void)vfprintf(stderr, fmt, ap);
! 311: va_end(ap);
! 312: (void)fprintf(stderr, "\n");
! 313: }