update strip with latest from UUNET
authorAndrew Moore <alm@FreeBSD.org>
Sat, 31 Jul 1993 00:48:29 +0000 (00:48 +0000)
committerAndrew Moore <alm@FreeBSD.org>
Sat, 31 Jul 1993 00:48:29 +0000 (00:48 +0000)
usr.bin/strip/Makefile
usr.bin/strip/strip.c

index 4be0ad8..bfb2b48 100644 (file)
@@ -6,5 +6,6 @@ install: maninstall
        install -c -o ${BINOWN} -g ${BINOWN} -m ${BINMODE} strip \
            ${DESTDIR}${BINDIR}
        ./strip ${DESTDIR}${BINDIR}/strip
        install -c -o ${BINOWN} -g ${BINOWN} -m ${BINMODE} strip \
            ${DESTDIR}${BINDIR}
        ./strip ${DESTDIR}${BINDIR}/strip
+       rm -f ./strip
 
 .include <bsd.prog.mk>
 
 .include <bsd.prog.mk>
index 182e8f5..20e1357 100644 (file)
@@ -38,11 +38,12 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)strip.c    5.7 (Berkeley) 5/26/91";
+static char sccsid[] = "@(#)strip.c    5.8 (Berkeley) 11/6/91";
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #endif /* not lint */
 
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/mman.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <a.out.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <a.out.h>
@@ -54,13 +55,13 @@ static char sccsid[] = "@(#)strip.c 5.7 (Berkeley) 5/26/91";
 typedef struct exec EXEC;
 typedef struct nlist NLIST;
 
 typedef struct exec EXEC;
 typedef struct nlist NLIST;
 
+#define        strx    n_un.n_strx
+
 void err __P((const char *fmt, ...));
 void s_stab __P((const char *, int, EXEC *));
 void s_sym __P((const char *, int, EXEC *));
 void usage __P((void));
 
 void err __P((const char *fmt, ...));
 void s_stab __P((const char *, int, EXEC *));
 void s_sym __P((const char *, int, EXEC *));
 void usage __P((void));
 
-int eval;
-
 main(argc, argv)
        int argc;
        char *argv[];
 main(argc, argv)
        int argc;
        char *argv[];
@@ -98,7 +99,7 @@ main(argc, argv)
                if (close(fd))
                        err("%s: %s", fn, strerror(errno));
        }
                if (close(fd))
                        err("%s: %s", fn, strerror(errno));
        }
-       exit(eval);
+       exit(0);
 }
 
 void
 }
 
 void
@@ -141,117 +142,76 @@ s_stab(fn, fd, ep)
        int fd;
        EXEC *ep;
 {
        int fd;
        EXEC *ep;
 {
+       register int cnt, len, nsymcnt;
+       register char *nstr, *nstrbase, *p, *strbase;
+       register NLIST *sym, *nsym;
        struct stat sb;
        struct stat sb;
-       register NLIST *bsym2, *sym1, *sym2;
-       register u_long nsym1, nsym2;
-       register char *p, *bstr2, *str1, *str2;
-       register int len, symlen;
-       off_t fsize;
-       int nb;
-       char *bp;
+       NLIST *symbase;
 
        /* Quit if no symbols. */
        if (ep->a_syms == 0)
                return;
 
 
        /* Quit if no symbols. */
        if (ep->a_syms == 0)
                return;
 
-       bsym2 = NULL;
-       bp = bstr2 = NULL;
+       /* Map the file. */
+       if (fstat(fd, &sb) ||
+           (ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE,
+           MAP_FILE | MAP_SHARED, fd, (off_t)0)) == (EXEC *)-1)
+               err("%s: %s", fn, strerror(errno));
 
 
-       /* Read the file into memory.                   XXX mmap */
-       if (fstat(fd, &sb))
-               goto syserr;
-       if ((bp = malloc(sb.st_size)) == NULL)
-               goto syserr;
-       if (lseek(fd, 0L, SEEK_SET) == -1)
-               goto syserr;
-       if ((nb = read(fd, bp, (int)sb.st_size)) == -1)
-               goto syserr;
-       if (nb != sb.st_size) {
-               errno = EIO;
-               goto syserr;
-       }
+       /*
+        * Initialize old and new symbol pointers.  They both point to the
+        * beginning of the symbol table in memory, since we're deleting
+        * entries.
+        */
+       sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
 
        /*
 
        /*
-        * Allocate space for new symbol and string tables.  Allocate before
-        * reading the symbol tables so we can do it all in a single pass.
-        * This loses if there weren't any symbols to strip, but that's life.
+        * Allocate space for the new string table, initialize old and
+        * new string pointers.  Handle the extra long at the beginning
+        * of the string table.
         */
         */
-       sym1 = (NLIST *)(bp + N_SYMOFF(*ep));
-       if ((bsym2 = sym2 = malloc((u_int)ep->a_syms)) == NULL) {
-               err("%s", strerror(errno));
-               goto mem;
-       }
-       str1 = bp + N_STROFF(*ep);
-       if ((bstr2 = malloc((u_int)*(u_long *)str1)) == NULL) {
+       strbase = (char *)ep + N_STROFF(*ep);
+       if ((nstrbase = malloc((u_int)*(u_long *)strbase)) == NULL)
                err("%s", strerror(errno));
                err("%s", strerror(errno));
-               goto mem;
-       }
-       str2 = bstr2 + sizeof(u_long);
-       symlen = sizeof(u_long);
+       nstr = nstrbase + sizeof(u_long);
 
        /*
         * Read through the symbol table.  For each non-debugging symbol,
 
        /*
         * Read through the symbol table.  For each non-debugging symbol,
-        * copy it into the new symbol and string tables.  Keep track of
-        * how many symbols are copied and the length of the new string
-        * table.
+        * copy it and save its string in the new string table.  Keep
+        * track of the number of symbols.
         */
         */
-#define        strx    n_un.n_strx
-       nsym2 = 0;
-       for (nsym1 = ep->a_syms / sizeof(NLIST); nsym1--; ++sym1)
-               if (!(sym1->n_type & N_STAB) && sym1->strx) {
-                       *sym2 = *sym1;
-                       sym2->strx = str2 - bstr2;
-                       p = str1 + sym1->strx;
+       for (cnt = ep->a_syms / sizeof(NLIST); cnt--; ++sym)
+               if (!(sym->n_type & N_STAB) && sym->strx) {
+                       *nsym = *sym;
+                       nsym->strx = nstr - nstrbase;
+                       p = strbase + sym->strx;
                        len = strlen(p) + 1;
                        len = strlen(p) + 1;
-                       bcopy(p, str2, len);
-                       symlen += len;
-                       str2 += len;
-                       ++sym2;
-                       ++nsym2;
+                       bcopy(p, nstr, len);
+                       nstr += len;
+                       ++nsym;
                }
 
                }
 
-       /* If no debugging symbols, quit. */
-       if (!nsym2)
-               goto mem;
-
        /* Fill in new symbol table size. */
        /* Fill in new symbol table size. */
-       ep->a_syms = nsym2 * sizeof(NLIST);
-
-       /* Write out the header. */
-       if (lseek(fd, 0L, SEEK_SET) == -1 ||
-           write(fd, ep, sizeof(EXEC)) != sizeof(EXEC))
-               goto syserr;
-
-       /* Write out the symbol table. */
-       if (lseek(fd, N_SYMOFF(*ep), SEEK_SET) == -1 ||
-           write(fd, bsym2, ep->a_syms) != ep->a_syms)
-               goto syserr;
+       ep->a_syms = (nsym - symbase) * sizeof(NLIST);
 
        /* Fill in the new size of the string table. */
 
        /* Fill in the new size of the string table. */
-       *(u_long *)bstr2 = symlen;
+       *(u_long *)nstrbase = len = nstr - nstrbase;
 
 
-       /* Write out the string table. */
-       if (write(fd, bstr2, symlen) != symlen)
-               goto syserr;
+       /*
+        * Copy the new string table into place.  Nsym should be pointing
+        * at the address past the last symbol entry.
+        */
+       bcopy(nstrbase, (void *)nsym, len);
 
        /* Truncate to the current length. */
 
        /* Truncate to the current length. */
-       if ((fsize = lseek(fd, 0L, SEEK_CUR)) == -1)
-               goto syserr;
-       if (ftruncate(fd, fsize))
-syserr:                err("%s: %s", fn, strerror(errno));
-
-mem:   if (bp)
-               free(bp);
-       if (bstr2)
-               free(bstr2);
-       if (bsym2)
-               free(bsym2);
+       if (ftruncate(fd, (char *)nsym + len - (char *)ep))
+               err("%s: %s", fn, strerror(errno));
+       munmap((caddr_t)ep, sb.st_size);
 }
 
 void
 usage()
 {
 }
 
 void
 usage()
 {
-
        (void)fprintf(stderr, "usage: strip [-d] file ...\n");
        exit(1);
 }
        (void)fprintf(stderr, "usage: strip [-d] file ...\n");
        exit(1);
 }
@@ -281,5 +241,5 @@ err(fmt, va_alist)
        (void)vfprintf(stderr, fmt, ap);
        va_end(ap);
        (void)fprintf(stderr, "\n");
        (void)vfprintf(stderr, fmt, ap);
        va_end(ap);
        (void)fprintf(stderr, "\n");
-       eval = 1;
+       exit(1);
 }
 }