New version from uunet. Uses mmap().

This commit is contained in:
mycroft 1993-08-07 04:44:25 +00:00
parent 18a419e9bb
commit 1a8be6e1aa
2 changed files with 53 additions and 92 deletions

View File

@ -1,11 +1,12 @@
# from: @(#)Makefile 5.4 (Berkeley) 5/11/90
# $Id: Makefile,v 1.5 1993/07/30 23:50:52 mycroft Exp $
# $Id: Makefile,v 1.6 1993/08/07 04:44:25 mycroft Exp $
PROG= strip
realinstall:
install ${COPY} -o ${BINOWN} -g ${BINOWN} -m ${BINMODE} strip \
install: maninstall
install -c -o ${BINOWN} -g ${BINOWN} -m ${BINMODE} strip \
${DESTDIR}${BINDIR}
./strip ${DESTDIR}${BINDIR}/strip
-[ "${COPY}" = "" ] && rm -f ./strip
.include <bsd.prog.mk>

View File

@ -38,12 +38,13 @@ char copyright[] =
#endif /* not lint */
#ifndef lint
/*static char sccsid[] = "from: @(#)strip.c 5.7 (Berkeley) 5/26/91";*/
static char rcsid[] = "$Id: strip.c,v 1.3 1993/08/01 18:08:15 mycroft Exp $";
/*static char sccsid[] = "from: @(#)strip.c 5.8 (Berkeley) 11/6/91";*/
static char rcsid[] = "$Id: strip.c,v 1.4 1993/08/07 04:44:27 mycroft Exp $";
#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>
@ -55,13 +56,13 @@ static char rcsid[] = "$Id: strip.c,v 1.3 1993/08/01 18:08:15 mycroft Exp $";
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));
int eval;
main(argc, argv)
int argc;
char *argv[];
@ -99,7 +100,7 @@ main(argc, argv)
if (close(fd))
err("%s: %s", fn, strerror(errno));
}
exit(eval);
exit(0);
}
void
@ -137,117 +138,76 @@ s_stab(fn, fd, ep)
int fd;
EXEC *ep;
{
register int cnt, len, nsymcnt;
register char *nstr, *nstrbase, *p, *strbase;
register NLIST *sym, *nsym;
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;
bsym2 = NULL;
bp = bstr2 = NULL;
/* 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;
}
/* 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));
/*
* 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.
* Initialize old and new symbol pointers. They both point to the
* beginning of the symbol table in memory, since we're deleting
* entries.
*/
sym1 = (NLIST *)(bp + N_SYMOFF(*ep));
if ((bsym2 = sym2 = malloc((u_int)ep->a_syms)) == NULL) {
sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
/*
* Allocate space for the new string table, initialize old and
* new string pointers. Handle the extra long at the beginning
* of the string table.
*/
strbase = (char *)ep + N_STROFF(*ep);
if ((nstrbase = malloc((u_int)*(u_long *)strbase)) == NULL)
err("%s", strerror(errno));
goto mem;
}
str1 = bp + N_STROFF(*ep);
if ((bstr2 = malloc((u_int)*(u_long *)str1)) == NULL) {
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,
* 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;
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. */
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. */
*(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. */
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)fprintf(stderr, "usage: strip [-d] file ...\n");
exit(1);
}
@ -277,5 +237,5 @@ err(fmt, va_alist)
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
eval = 1;
exit(1);
}