home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / strip / strip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-15  |  6.4 KB  |  246 lines

  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[] = "@(#)strip.c    5.8 (Berkeley) 11/6/91";
  42. #endif /* not lint */
  43.  
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <sys/mman.h>
  47. #include <fcntl.h>
  48. #include <errno.h>
  49. #include <a.out.h>
  50. #include <unistd.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54.  
  55. typedef struct exec EXEC;
  56. typedef struct nlist NLIST;
  57.  
  58. #define    strx    n_un.n_strx
  59.  
  60. void err __P((const char *fmt, ...));
  61. void s_stab __P((const char *, int, EXEC *));
  62. void s_sym __P((const char *, int, EXEC *));
  63. void usage __P((void));
  64.  
  65. main(argc, argv)
  66.     int argc;
  67.     char *argv[];
  68. {
  69.     register int fd, nb;
  70.     EXEC head;
  71.     void (*sfcn)__P((const char *, int, EXEC *));
  72.     int ch;
  73.     char *fn;
  74.  
  75.     sfcn = s_sym;
  76.     while ((ch = getopt(argc, argv, "d")) != EOF)
  77.         switch(ch) {
  78.         case 'd':
  79.             sfcn = s_stab;
  80.             break;
  81.         case '?':
  82.         default:
  83.             usage();
  84.         }
  85.     argc -= optind;
  86.     argv += optind;
  87.  
  88.     while (fn = *argv++) {
  89.         if ((fd = open(fn, O_RDWR)) < 0 ||
  90.             (nb = read(fd, &head, sizeof(EXEC))) == -1) {
  91.             err("%s: %s", fn, strerror(errno));
  92.             continue;
  93.         }
  94.         if (nb != sizeof(EXEC) || N_BADMAG(head)) {
  95.             err("%s: %s", fn, strerror(EFTYPE));
  96.             continue;
  97.         }
  98.         sfcn(fn, fd, &head);
  99.         if (close(fd))
  100.             err("%s: %s", fn, strerror(errno));
  101.     }
  102.     exit(0);
  103. }
  104.  
  105. void
  106. s_sym(fn, fd, ep)
  107.     const char *fn;
  108.     int fd;
  109.     register EXEC *ep;
  110. {
  111.     static int pagesize = -1;
  112.     register off_t fsize;
  113.  
  114.     /* If no symbols or data/text relocation info, quit. */
  115.     if (!ep->a_syms && !ep->a_trsize && !ep->a_drsize)
  116.         return;
  117.  
  118.     /*
  119.      * New file size is the header plus text and data segments; OMAGIC
  120.      * and NMAGIC formats have the text/data immediately following the
  121.      * header.  ZMAGIC format wastes the rest of of header page.
  122.      */
  123.     if (ep->a_magic == ZMAGIC)
  124.         fsize = pagesize == -1 ? (pagesize = getpagesize()) : pagesize;
  125.     else
  126.         fsize = sizeof(EXEC);
  127.     fsize += ep->a_text + ep->a_data;
  128.  
  129.     /* Set symbol size and relocation info values to 0. */
  130.     ep->a_syms = ep->a_trsize = ep->a_drsize = 0;
  131.  
  132.     /* Rewrite the header and truncate the file. */
  133.     if (lseek(fd, 0L, SEEK_SET) == -1 ||
  134.         write(fd, ep, sizeof(EXEC)) != sizeof(EXEC) ||
  135.         ftruncate(fd, fsize))
  136.         err("%s: %s", fn, strerror(errno)); 
  137. }
  138.  
  139. void
  140. s_stab(fn, fd, ep)
  141.     const char *fn;
  142.     int fd;
  143.     EXEC *ep;
  144. {
  145.     register int cnt, len, nsymcnt;
  146.     register char *nstr, *nstrbase, *p, *strbase;
  147.     register NLIST *sym, *nsym;
  148.     struct stat sb;
  149.     NLIST *symbase;
  150.  
  151.     /* Quit if no symbols. */
  152.     if (ep->a_syms == 0)
  153.         return;
  154.  
  155.     /* Map the file. */
  156.     if (fstat(fd, &sb) ||
  157.         (ep = (EXEC *)mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE,
  158.         MAP_FILE | MAP_SHARED, fd, (off_t)0)) == (EXEC *)-1)
  159.         err("%s: %s", fn, strerror(errno));
  160.  
  161.     /*
  162.      * Initialize old and new symbol pointers.  They both point to the
  163.      * beginning of the symbol table in memory, since we're deleting
  164.      * entries.
  165.      */
  166.     sym = nsym = symbase = (NLIST *)((char *)ep + N_SYMOFF(*ep));
  167.  
  168.     /*
  169.      * Allocate space for the new string table, initialize old and
  170.      * new string pointers.  Handle the extra long at the beginning
  171.      * of the string table.
  172.      */
  173.     strbase = (char *)ep + N_STROFF(*ep);
  174.     if ((nstrbase = malloc((u_int)*(u_long *)strbase)) == NULL)
  175.         err("%s", strerror(errno));
  176.     nstr = nstrbase + sizeof(u_long);
  177.  
  178.     /*
  179.      * Read through the symbol table.  For each non-debugging symbol,
  180.      * copy it and save its string in the new string table.  Keep
  181.      * track of the number of symbols.
  182.      */
  183.     for (cnt = ep->a_syms / sizeof(NLIST); cnt--; ++sym)
  184.         if (!(sym->n_type & N_STAB) && sym->strx) {
  185.             *nsym = *sym;
  186.             nsym->strx = nstr - nstrbase;
  187.             p = strbase + sym->strx;
  188.             len = strlen(p) + 1;
  189.             bcopy(p, nstr, len);
  190.             nstr += len;
  191.             ++nsym;
  192.         }
  193.  
  194.     /* Fill in new symbol table size. */
  195.     ep->a_syms = (nsym - symbase) * sizeof(NLIST);
  196.  
  197.     /* Fill in the new size of the string table. */
  198.     *(u_long *)nstrbase = len = nstr - nstrbase;
  199.  
  200.     /*
  201.      * Copy the new string table into place.  Nsym should be pointing
  202.      * at the address past the last symbol entry.
  203.      */
  204.     bcopy(nstrbase, (void *)nsym, len);
  205.  
  206.     /* Truncate to the current length. */
  207.     if (ftruncate(fd, (char *)nsym + len - (char *)ep))
  208.         err("%s: %s", fn, strerror(errno));
  209.     munmap((caddr_t)ep, sb.st_size);
  210. }
  211.  
  212. void
  213. usage()
  214. {
  215.     (void)fprintf(stderr, "usage: strip [-d] file ...\n");
  216.     exit(1);
  217. }
  218.  
  219. #if __STDC__
  220. #include <stdarg.h>
  221. #else
  222. #include <varargs.h>
  223. #endif
  224.  
  225. void
  226. #if __STDC__
  227. err(const char *fmt, ...)
  228. #else
  229. err(fmt, va_alist)
  230.     char *fmt;
  231.         va_dcl
  232. #endif
  233. {
  234.     va_list ap;
  235. #if __STDC__
  236.     va_start(ap, fmt);
  237. #else
  238.     va_start(ap);
  239. #endif
  240.     (void)fprintf(stderr, "strip: ");
  241.     (void)vfprintf(stderr, fmt, ap);
  242.     va_end(ap);
  243.     (void)fprintf(stderr, "\n");
  244.     exit(1);
  245. }
  246.