home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gbinusrc.zip / emx / gnu / binutils.old / strip.c < prev    next >
C/C++ Source or Header  |  1994-10-04  |  23KB  |  929 lines

  1. /* strip.c -- changed for emx by Eberhard Mattes -- Oct 1994 */
  2.  
  3. /* strip certain symbols from a rel file.
  4.    Copyright (C) 1986 Free Software Foundation, Inc.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 1, or (at your option)
  9.    any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <sys/file.h>
  24. #include <sys/stat.h>
  25. #include <signal.h>
  26. #include "getopt.h"
  27.  
  28. #ifdef USG
  29. #include <fcntl.h>
  30. #include <string.h>
  31. #else
  32. #include <strings.h>
  33. #endif
  34.  
  35. #if !defined(A_OUT) && !defined(MACH_O)
  36. #define A_OUT
  37. #endif
  38.  
  39. #ifdef A_OUT
  40. #ifdef COFF_ENCAPSULATE
  41. #include "a.out.encap.h"
  42. #else
  43. /* On native BSD systems, use the system's own a.out.h.  */
  44. #include <a_out.h>
  45. #endif
  46. #endif
  47.  
  48. #ifdef MACH_O
  49. #ifndef A_OUT
  50. #include <nlist.h>
  51. #include <reloc.h>
  52. #endif
  53. #include <sys/loader.h>
  54. #endif
  55.  
  56. #ifdef nounderscore
  57. #define LPREFIX '.'
  58. #else
  59. #define LPREFIX 'L'
  60. #endif
  61.  
  62. #if defined (sun) && defined (sparc)
  63. /* On the sparc, the name of the relocation info structure is
  64.    different (on SunOS4, "struct relocation_info" does not exist).
  65.    The meaning of the r_index field is the same as r_symbolnum
  66.    in normal relocation_info's for external symbols.  Fortunately,
  67.    we only use the field for external symbols.  */
  68. typedef struct reloc_info_sparc *relocation_info_ptr;
  69. #define RELOCATION_INFO_SYMBOL_NUM(ri) (ri)->r_index
  70. #else /* not Sun and sparc.  */
  71. typedef struct relocation_info *relocation_info_ptr;
  72. #define RELOCATION_INFO_SYMBOL_NUM(ri) (ri)->r_symbolnum
  73. #endif /* not Sun and sparc.  */
  74.  
  75. /* If BSD, we can use `ftruncate'.  */
  76.  
  77. #ifndef USG
  78. #define HAVE_FTRUNCATE
  79. #endif
  80.  
  81. /* Count the number of nlist entries that are for local symbols. */
  82. int local_sym_count;
  83.  
  84. /* Count number of nlist entries that are for local symbols
  85.    whose names don't start with L. */
  86. int non_L_local_sym_count;
  87.  
  88. /* Count the number of nlist entries for debugger info.  */
  89. int debugger_sym_count;
  90.  
  91. /* Count the number of global symbols referenced or defined.  */
  92. int global_sym_count;
  93.  
  94. /* Total number of symbols to be preserved in the current file.  */
  95. int nsyms;
  96.  
  97. /* Number of files specified in the command line. */
  98.  
  99. int number_of_files;
  100.  
  101. /* Kinds of files understood.  */
  102. enum file_type { IS_UNKNOWN, IS_A_OUT, IS_MACH_O };
  103.  
  104. /* Each specified file has a file_entry structure for it.
  105.    These are contained in the vector which file_table points to.  */
  106.  
  107. struct file_entry {
  108.   char *filename;
  109.   enum file_type filetype;    /* what kind of file it is */
  110.  
  111.   /* things obtained from the file's header.  */
  112.   long int trel_offset;        /* offset to text relocation */
  113.   unsigned int trel_size;    /* size of text relocation */
  114.   long int drel_offset;        /* offset to data relocation */
  115.   unsigned int drel_size;    /* size of data relocation */
  116.   long int syms_offset;        /* offset to the symbol table */
  117.   unsigned int syms_size;    /* size of the symbol table */
  118.   long int strs_offset;        /* offset to the string table */
  119.   unsigned int strs_size;    /* size of the string table */
  120.  
  121.   int ss_size;            /* size, in bytes, of symbols_and_strings data */
  122.   struct nlist *symbols_and_strings;
  123.  
  124.   /* offset of the symtab_command in a mach-O file's header */
  125.   long int symtab_cmd_offset;
  126. };
  127.  
  128. struct file_entry *file_table;
  129.  
  130. /* Descriptor on which current file is open.  */
  131.  
  132. int input_desc;
  133.  
  134. /* Stream for writing that file using stdio.  */
  135.  
  136. FILE *outstream;
  137.  
  138. /* 1 => strip all symbols; 2 => strip all debugger symbols */
  139. int strip_symbols;
  140.  
  141. /* 1 => discard locals starting with L; 2 => discard all locals */
  142. int discard_locals;
  143.  
  144. void strip_file ();
  145. int file_open ();
  146. void rewrite_file_symbols(), file_close();
  147. int read_header (), read_entry_symbols (), read_file_symbols ();
  148. void count_file_symbols ();
  149. char *xmalloc ();
  150. char *concat ();
  151.  
  152. main (argc, argv)
  153.      char **argv;
  154.      int argc;
  155. {
  156.   int c;
  157.   extern int optind;
  158.   int ind;
  159.   
  160.   /* structure containing the short options expanded into long form */
  161.   static struct option long_options[] =
  162.     {
  163.       {"strip-all",   0, &strip_symbols,  1},
  164.       {"strip-debug", 0, &strip_symbols,  2},
  165.       {"discard-all", 0, &discard_locals, 2},
  166.       {"discard-locals",   0, &discard_locals, 1},
  167.       {NULL, 0, NULL, 0}
  168.     };
  169.   
  170.   struct file_entry *p;
  171.   int i;
  172.  
  173.   _wildcard (&argc, &argv);
  174.   strip_symbols = 0;   /* default is to strip everything.  */
  175.   discard_locals = 0;
  176.  
  177.   while ((c = getopt_long (argc, argv, "gsSxX", long_options, &ind)) != EOF) 
  178.       switch (c)
  179.     {
  180.     case  0 :
  181.       break;
  182.     case 's':
  183.       strip_symbols = 1;
  184.       break;
  185.     case 'g':
  186.     case 'S':
  187.       strip_symbols = 2;
  188.       break;
  189.     case 'x':
  190.       discard_locals = 2;
  191.       break;
  192.     case 'X':
  193.       discard_locals = 1;
  194.       break;
  195.     default:
  196.       usage ();
  197.     }
  198.  
  199.   /* Default is to strip all symbols.  */
  200.   if (strip_symbols == 0 && discard_locals == 0)
  201.     strip_symbols = 1;
  202.  
  203.   number_of_files = argc - optind;
  204.  
  205.   if (!number_of_files)
  206.     usage ();
  207.  
  208.   p = file_table
  209.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  210.  
  211.   /* Now fill in file_table */
  212.  
  213.   for (i = 0; i < number_of_files; i++)
  214.     {
  215.       p->filename = argv[i + optind];
  216.       p->filetype = IS_UNKNOWN;
  217.       p->trel_offset = p->trel_size = p->drel_offset = p->drel_size = 0;
  218.       p->syms_offset = p->syms_size = p->strs_offset = p->strs_size = 0;
  219.       p->symbols_and_strings = 0;
  220.       p->symtab_cmd_offset = 0;
  221.       p++;
  222.     }
  223.  
  224.   for (i = 0; i < number_of_files; i++)
  225.     strip_file (&file_table[i]);
  226.   exit (0);
  227. }
  228.  
  229. /* process one input file */
  230.  
  231. void
  232. strip_file (entry)
  233.      struct file_entry *entry;
  234. {
  235.   int val;
  236.   int sigint_handled = 0;
  237.   int sighup_handled = 0;
  238.   int sigterm_handled = 0;
  239.  
  240.   local_sym_count = 0;
  241.   non_L_local_sym_count = 0;
  242.   debugger_sym_count = 0;
  243.   global_sym_count = 0;
  244.  
  245.   val = file_open (entry);
  246.   if (val < 0)
  247.     return;
  248.  
  249.   if (strip_symbols != 1)
  250.     /* Read in the existing symbols unless we are discarding everything.  */
  251.     {
  252.       if (read_file_symbols (entry) < 0)
  253.     return;
  254.     }
  255.  
  256.   /* Effectively defer handling of asynchronous kill signals.  */
  257.   /* -- deleted for emx */
  258.   /* Change the file.  */
  259.  
  260.   rewrite_file_symbols (entry);
  261.   if (strip_symbols != 1)
  262.     free (entry->symbols_and_strings);
  263.  
  264.   file_close ();
  265.  
  266.   /* Effectively undefer handling.  */
  267.   /* -- deleted for emx */
  268. }
  269.  
  270. /** Convenient functions for operating on one or all files being processed.  */
  271.  
  272. /* Close the file that is now open.  */
  273.  
  274. void
  275. file_close ()
  276. {
  277.   close (input_desc);
  278.   input_desc = 0;
  279. }
  280.  
  281. /* Open the file specified by 'entry', and return a descriptor.
  282.    The descriptor is also saved in input_desc.  */
  283.  
  284. /* JF this also makes sure the file is in rel format */
  285.  
  286. int
  287. file_open (entry)
  288.      struct file_entry *entry;
  289. {
  290.   int desc;
  291.   int len, magicnum;
  292.  
  293.   desc = open (entry->filename, O_RDWR | O_BINARY, 0);
  294.  
  295.   if (desc > 0)
  296.     {
  297.       input_desc = desc;
  298.       if (read_header (desc, entry) < 0)
  299.     {
  300.       close (desc);
  301.       return -1;
  302.     }
  303.       return desc;
  304.     }
  305.  
  306.   perror_file (entry);
  307.   return -1;
  308. }
  309.  
  310. /* Print the filename of ENTRY on OUTFILE (a stdio stream), then a newline.  */
  311.  
  312. prline_file_name (entry, outfile)
  313.      struct file_entry *entry;
  314.      FILE *outfile;
  315. {
  316.   print_file_name (entry, outfile);
  317.   fprintf (outfile, "\n");
  318. }
  319.  
  320. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  321.  
  322. print_file_name (entry, outfile)
  323.      struct file_entry *entry;
  324.      FILE *outfile;
  325. {
  326.   fprintf (outfile, "%s", entry->filename);
  327. }
  328.  
  329. /* Validate file ENTRY and read its symbol and string sections into core. */
  330.  
  331. int
  332. read_file_symbols (entry)
  333.      struct file_entry *entry;
  334. {
  335.   if (read_entry_symbols (input_desc, entry) < 0)
  336.     return -1;
  337.   count_file_symbols (entry);
  338.   return 0;
  339. }
  340.  
  341. /* Read a file's header and fill in various fields of a file's entry.
  342.    Return -1 on failure.  */
  343.  
  344. int
  345. read_header (desc, entry)
  346.      int desc;
  347.      struct file_entry *entry;
  348. {
  349.   int len;
  350.  
  351. #ifdef A_OUT
  352.   {
  353.     struct exec hdr;
  354.  
  355.     lseek (desc, 0, 0);
  356. #ifdef HEADER_SEEK_FD
  357.     /* Skip the headers that encapsulate our data in some other format
  358.        such as COFF.  */
  359.     HEADER_SEEK_FD (desc);
  360. #endif
  361.     len = read (desc, (char *) &hdr, sizeof (struct exec));
  362.     if (len == sizeof (struct exec) && !N_BADMAG (hdr))
  363.       {
  364.     entry->filetype = IS_A_OUT;
  365. #ifdef N_TRELOFF
  366.     entry->trel_offset = N_TRELOFF (hdr);
  367. #else
  368. #ifdef N_DATOFF
  369.     entry->trel_offset = N_DATOFF (hdr) + hdr.a_data;
  370. #else
  371.     entry->trel_offset = N_TXTOFF (hdr) + hdr.a_text + hdr.a_data;
  372. #endif
  373. #endif
  374.     entry->trel_size = hdr.a_trsize;
  375. #ifdef N_DRELOFF
  376.     entry->drel_offset = N_DRELOFF (hdr);
  377. #else
  378.     entry->drel_offset = entry->trel_offset + entry->trel_size;
  379. #endif
  380.     entry->drel_size = hdr.a_drsize;
  381.     entry->syms_offset = N_SYMOFF(hdr);
  382.     entry->syms_size = hdr.a_syms;
  383.     entry->strs_offset = N_STROFF(hdr);
  384.     if (hdr.a_syms == 0)
  385.       entry->strs_size = 0;
  386.     else
  387.       {
  388.         lseek(desc, entry->strs_offset, 0);
  389.         if (read (desc, (char *) &entry->strs_size, sizeof entry->strs_size)
  390.         != sizeof entry->strs_size)
  391.           {
  392.         error_with_file ("cannot read string table size", entry);
  393.         return -1;
  394.           }
  395.       }
  396.     return 0;
  397.       }
  398.   }
  399. #endif
  400.  
  401. #ifdef MACH_O
  402.   {
  403.     struct mach_header mach_header;
  404.     char *hdrbuf;
  405.     struct load_command *load_command;
  406.     struct segment_command *segment_command;
  407.     struct section *section;
  408.     struct symtab_command *symtab_command;
  409.     int symtab_seen;
  410.     int len, cmd, seg;
  411.  
  412.     symtab_seen = 0;
  413.  
  414.     lseek (desc, 0L, 0);
  415.     len = read (desc, (char *) &mach_header, sizeof (struct mach_header));
  416.     if (len == sizeof (struct mach_header) && mach_header.magic == MH_MAGIC)
  417.       {
  418.     entry->filetype = IS_MACH_O;
  419.     hdrbuf = xmalloc (mach_header.sizeofcmds);
  420.     len = read (desc, hdrbuf, mach_header.sizeofcmds);
  421.     if (len != mach_header.sizeofcmds)
  422.       {
  423.         error_with_file ("failure reading Mach-O load commands", entry);
  424.         return -1;
  425.       }
  426.     load_command = (struct load_command *) hdrbuf;
  427.     for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
  428.       {
  429.         switch (load_command->cmd)
  430.           {
  431.           case LC_SEGMENT:
  432.         segment_command = (struct segment_command *) load_command;
  433.         section = (struct section *) ((char *) (segment_command + 1));
  434.         for (seg = 0; seg < segment_command->nsects; ++seg, ++section)
  435.           {
  436.             if (!strncmp(SECT_TEXT, section->sectname, sizeof section->sectname))
  437.               {
  438.             entry->trel_offset = section->reloff;
  439.             entry->trel_size = section->nreloc * sizeof (struct relocation_info);
  440.               }
  441.             else if (!strncmp(SECT_DATA, section->sectname, sizeof section->sectname))
  442.               {
  443.             entry->drel_offset = section->reloff;
  444.             entry->drel_size = section->nreloc * sizeof (struct relocation_info);
  445.               }
  446.           }
  447.         break;
  448.           case LC_SYMTAB:
  449.         if (symtab_seen)
  450.           error_with_file ("more than one LC_SYMTAB", entry);
  451.         else
  452.           {
  453.             symtab_seen = 1;
  454.             symtab_command = (struct symtab_command *) load_command;
  455.             entry->syms_offset = symtab_command->symoff;
  456.             entry->syms_size = symtab_command->nsyms * sizeof (struct nlist);
  457.             entry->strs_offset = symtab_command->stroff;
  458.             entry->strs_size = symtab_command->strsize;
  459.             entry->symtab_cmd_offset = (char *) load_command - hdrbuf
  460.               + sizeof (struct mach_header);
  461.           }
  462.         break;
  463.           }
  464.         load_command = (struct load_command *)
  465.           ((char *) load_command + load_command->cmdsize);
  466.       }
  467.  
  468.     free (hdrbuf);
  469.  
  470.     if (!symtab_seen)
  471.       {
  472.         error_with_file ("no symbol table", entry);
  473.         return -1;
  474.       }
  475.  
  476.     return 0;
  477.       }
  478.   }
  479. #endif
  480.  
  481.   error_with_file ("not an executable or object file", entry);
  482.   return -1;
  483. }
  484.  
  485. /* Read the symbols and strings of file ENTRY into core.
  486.    Assume it is already open, on descriptor DESC.
  487.    Return -1 on failure.  */
  488.  
  489. int
  490. read_entry_symbols (desc, entry)
  491.      struct file_entry *entry;
  492.      int desc;
  493. {
  494.   int string_size;
  495.  
  496.   entry->ss_size = entry->syms_size + entry->strs_size;
  497.   entry->symbols_and_strings = (struct nlist *) xmalloc (entry->ss_size);
  498.  
  499.   lseek (desc, entry->syms_offset, 0);
  500.   if (entry->ss_size != read (desc, entry->symbols_and_strings, entry->ss_size))
  501.     {
  502.       error_with_file ("premature end of file in symbols/strings", entry);
  503.       return -1;
  504.     }
  505.   return 0;
  506. }
  507.  
  508.  
  509. /* Count the number of symbols of various categories in the file of ENTRY.  */
  510.  
  511. void
  512. count_file_symbols (entry)
  513.      struct file_entry *entry;
  514. {
  515.   struct nlist *p, *end = entry->symbols_and_strings + entry->syms_size / sizeof (struct nlist);
  516.   char *name_base = entry->syms_size + (char *) entry->symbols_and_strings;
  517.  
  518.   for (p = entry->symbols_and_strings; p < end; p++)
  519.     if (p->n_type & N_EXT)
  520.       global_sym_count++;
  521.     else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  522.       {
  523.     if ((p->n_un.n_strx + name_base)[0] != LPREFIX)
  524.       non_L_local_sym_count++;
  525.     local_sym_count++;
  526.       }
  527.     else debugger_sym_count++;
  528. }
  529.  
  530. void write_file_syms (), modify_relocation ();
  531.  
  532. /* Total size of string table strings allocated so far */
  533. int strtab_size;
  534.  
  535. /* Vector whose elements are the strings to go in the string table */
  536. char **strtab_vector;
  537.  
  538. /* Index in strtab_vector at which the next string will be stored */
  539. int strtab_index;
  540.  
  541. int sym_written_count;
  542.  
  543. int
  544. assign_string_table_index (name)
  545.      char *name;
  546. {
  547.   int index = strtab_size;
  548.  
  549.   strtab_size += strlen (name) + 1;
  550.   strtab_vector[strtab_index++] = name;
  551.  
  552.   return index;
  553. }
  554.  
  555. void
  556. rewrite_file_symbols (entry)
  557.      struct file_entry *entry;
  558. {
  559.   int i;
  560.   struct nlist *newsyms;
  561.  
  562.   /* Calculate number of symbols to be preserved.  */
  563.  
  564.   if (strip_symbols == 1)
  565.     nsyms = 0;
  566.   else
  567.     {
  568.       nsyms = global_sym_count;
  569.       if (discard_locals == 1)
  570.     nsyms += non_L_local_sym_count;
  571.       else if (discard_locals == 0)
  572.     nsyms += local_sym_count;
  573.     }
  574.  
  575.   if (strip_symbols == 0)
  576.     nsyms += debugger_sym_count;
  577.  
  578.   strtab_vector = (char **) xmalloc (nsyms * sizeof (char *));
  579.   strtab_index = 0;
  580.  
  581.   strtab_size = 4;
  582.  
  583.   /* Accumulate in 'newsyms' the symbol table to be written.  */
  584.  
  585.   newsyms = (struct nlist *) xmalloc (nsyms * sizeof (struct nlist));
  586.  
  587.   sym_written_count = 0;
  588.  
  589.   if (strip_symbols != 1)
  590.     /* Write into newsyms the symbols we want to keep.  */
  591.     write_file_syms (entry, newsyms);
  592.  
  593.   if (sym_written_count != nsyms)
  594.     {
  595.       fprintf (stderr, "written = %d, expected = %d\n",
  596.            sym_written_count, nsyms);
  597.       abort ();
  598.     }
  599.  
  600.   /* Modify the symbol-numbers in the relocation in the file,
  601.      to preserve its meaning */
  602.   modify_relocation (input_desc, entry);
  603.  
  604. #ifndef    HAVE_FTRUNCATE
  605.   {
  606.     int size = entry->syms_offset, mode;
  607.     char *renamed = (char *)concat ("~", entry->filename, "~");
  608.     char *copy_buffer = (char *)xmalloc (size);
  609.     struct stat statbuf;
  610.  
  611.     lseek (input_desc, 0, 0);
  612.     if (read (input_desc, copy_buffer, size) != size)
  613.       {
  614.     error_with_file ("can't read up to symbol table", entry);
  615.     return;
  616.       }
  617.     mode = fstat (input_desc, &statbuf) ? 0666 : statbuf.st_mode;
  618.     if (rename (entry->filename, renamed))
  619.       {
  620.     perror_file (entry);
  621.     return;
  622.       }
  623.     input_desc = creat (entry->filename, mode);
  624.     if (input_desc < 0)
  625.       {
  626.     perror_file (entry);
  627.     return;
  628.       }
  629.     if (write (input_desc, copy_buffer, size) != size)
  630.       perror_file (entry);
  631.     if (unlink (renamed))
  632.       perror_name (renamed);
  633.     free (copy_buffer);
  634.     free (renamed);
  635.   }
  636. #endif /* not HAVE_FTRUNCATE */
  637.  
  638.   /* Now write contents of NEWSYMS into the file. */
  639.  
  640.   lseek (input_desc, entry->syms_offset, 0);
  641.   write (input_desc, newsyms, nsyms * sizeof (struct nlist));
  642.   free (newsyms);
  643.  
  644.   /* Now write the string table.  */
  645.  
  646.   {
  647.     char *strvec = (char *) xmalloc (strtab_size);
  648.     char *p;
  649.  
  650.     *((long *) strvec) = strtab_size;
  651.  
  652.     p = strvec + sizeof (long);
  653.  
  654.     for (i = 0; i < strtab_index; i++)
  655.       {
  656.     int len = strlen (strtab_vector[i]);
  657.     strcpy (p, strtab_vector[i]);
  658.     *(p+len) = 0;
  659.     p += len + 1;
  660.       }
  661.  
  662.     write (input_desc, strvec, strtab_size);
  663.     free (strvec);
  664.   }
  665.  
  666.   /* Adjust file to be smaller */
  667.  
  668. #ifdef HAVE_FTRUNCATE
  669.   if (ftruncate (input_desc, tell (input_desc)) < 0)
  670.     perror_file (entry);
  671. #endif
  672.  
  673.   /* Write new symbol table size into file header.  */
  674.  
  675. #ifdef A_OUT
  676.   if (entry->filetype == IS_A_OUT)
  677.     {
  678.       struct exec hdr;
  679.  
  680.       lseek (input_desc, 0L, 0);
  681. #ifdef HEADER_SEEK_FD
  682.       HEADER_SEEK_FD (input_desc);
  683. #endif
  684.       read (input_desc, (char *) &hdr, sizeof hdr);
  685.       hdr.a_syms = nsyms * sizeof (struct nlist);
  686.       lseek (input_desc, (long) -sizeof hdr, 1);
  687.       write (input_desc, (char *) &hdr, sizeof hdr);
  688.     }
  689. #endif
  690.  
  691. #ifdef MACH_O
  692.   if (entry->filetype == IS_MACH_O)
  693.     {
  694.       struct symtab_command cmd;
  695.  
  696.       lseek (input_desc, entry->symtab_cmd_offset, 0);
  697.       read (input_desc, (char *) &cmd, sizeof cmd);
  698.       cmd.nsyms = nsyms;
  699.       cmd.stroff = cmd.symoff + cmd.nsyms * sizeof (struct nlist);
  700.       cmd.strsize = strtab_size;
  701.       lseek (input_desc, entry->symtab_cmd_offset, 0);
  702.       write (input_desc, (char *) &cmd, sizeof cmd);
  703.     }
  704. #endif
  705.  
  706.   free (strtab_vector);
  707. }
  708.  
  709. /* Copy into NEWSYMS the symbol entries to be preserved.
  710.    Count them in sym_written_count.  */
  711.  
  712. /* We record, for each symbol written, its symbol number in the resulting file.
  713.    This is so that the relocation can be updated later.
  714.    Since the symbol names will not be needed again,
  715.    this index goes in the `n_strx' field.
  716.    If a symbol is not written, -1 is stored there.  */
  717.  
  718. void
  719. write_file_syms (entry, newsyms)
  720.      struct file_entry *entry;
  721.      struct nlist *newsyms;
  722. {
  723.   struct nlist *p = entry->symbols_and_strings;
  724.   struct nlist *end = p + entry->syms_size / sizeof (struct nlist);
  725.   char *string_base = (char *) end;   /* address of start of file's string table */
  726.   struct nlist *outp = newsyms;
  727.  
  728.   for (; p < end; p++)
  729.     {
  730.       int type = p->n_type;
  731.       int write;
  732.   
  733.       if (p->n_type & N_EXT)
  734.     write = 1;
  735.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  736.     /* ordinary local symbol */
  737.     write = (discard_locals != 2)
  738.         && !(discard_locals == 1 &&
  739.              (p->n_un.n_strx + string_base)[0] == LPREFIX);
  740.       else
  741.     /* debugger symbol */
  742.     write = (strip_symbols == 0);
  743.  
  744.       if (write)
  745.     {
  746.       if (p->n_un.n_strx)
  747.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx + string_base);
  748.  
  749.       *outp++ = *p;
  750.  
  751.       p->n_un.n_strx = sym_written_count++;
  752.     }
  753.       else p->n_un.n_strx = -1;
  754.     }
  755. }
  756.  
  757. /* Read in ENTRY's relocation, alter the symbolnums in it,
  758.    and write it out again.  */
  759.  
  760. void
  761. modify_relocation (desc, entry)
  762.      int desc;
  763.      struct file_entry *entry;
  764. {
  765.   relocation_info_ptr reloc, p, end;
  766.   int size;
  767.   struct nlist *sym_base = (struct nlist *) entry->symbols_and_strings;
  768.   int losing = 0;
  769.   long int offsets[2];
  770.   unsigned int sizes[2];
  771.   int i;
  772.  
  773.   offsets[0] = entry->trel_offset;
  774.   sizes[0] = entry->trel_size;
  775.   offsets[1] = entry->drel_offset;
  776.   sizes[1] = entry->drel_size;
  777.  
  778.   for (i = 0; i < 2; ++i)
  779.     {
  780.       size = sizes[i];
  781.       reloc = (relocation_info_ptr) xmalloc (size);
  782.       lseek (desc, offsets[i], 0);
  783.       read (desc, reloc, size);
  784.  
  785.       p = reloc;
  786.       end = (relocation_info_ptr) (size + (char *) reloc);
  787.       while (p < end)
  788.     {
  789.       if (p->r_extern)
  790.         {
  791.           int newnum = (sym_base == 0 ? -1
  792.                 :((sym_base + RELOCATION_INFO_SYMBOL_NUM(p))
  793.                   -> n_un.n_strx));
  794.           if (newnum < 0)
  795.         {
  796.           if (losing == 0)
  797.             error_with_file ("warning: file is now unlinkable", entry);
  798.           losing = 1;
  799.         }
  800.           RELOCATION_INFO_SYMBOL_NUM(p) = newnum;
  801.         }
  802.       p++;
  803.     }
  804.  
  805.       lseek (desc, offsets[i], 0);
  806.       write (desc, reloc, size);
  807.       free ((char *) reloc);
  808.     }
  809. }
  810.  
  811. /* Report a fatal error.
  812.    STRING is a printf format string and ARG is one arg for it.  */
  813.  
  814. fatal (string, arg)
  815.      char *string, *arg;
  816. {
  817.   fprintf (stderr, "strip: ");
  818.   fprintf (stderr, string, arg);
  819.   fprintf (stderr, "\n");
  820.   exit (1);
  821. }
  822.  
  823. /* Report an error using the message for the last failed system call,
  824.    followed by the string NAME.  */
  825.  
  826. perror_name (name)
  827.      char *name;
  828. {
  829.   char *s;
  830.  
  831.   if (errno < sys_nerr)
  832.     s = concat ("", sys_errlist[errno], " for %s");
  833.   else
  834.     s = "cannot open %s";
  835.   error (s, name);
  836. }
  837.  
  838. /* Report an error using the message for the last failed system call,
  839.    followed by the name of file ENTRY.  */
  840.  
  841. perror_file (entry)
  842.      struct file_entry *entry;
  843. {
  844.   const char *s;
  845.  
  846.   if (errno < sys_nerr)
  847.     s = sys_errlist[errno];
  848.   else
  849.     s = "cannot open";
  850.   error_with_file (s, entry);
  851. }
  852.  
  853. /* Report an error.   STRING is printed, and the filename of ENTRY.  */
  854.  
  855. error_with_file (string, entry, arg1, arg2)
  856.      const char *string;
  857.      struct file_entry *entry;
  858.      int arg1, arg2;
  859. {
  860.   fprintf (stderr, "strip: ");
  861.   print_file_name (entry, stderr);
  862.   fprintf (stderr, ": ");
  863.   fprintf (stderr, string, arg1, arg2);
  864.   fprintf (stderr, "\n");
  865. }
  866.  
  867. /* Report a nonfatal error.
  868.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  869.  
  870. error (string, arg1, arg2, arg3)
  871.      char *string, *arg1, *arg2, *arg3;
  872. {
  873.   fprintf (stderr, string, arg1, arg2, arg3);
  874.   fprintf (stderr, "\n");
  875. }
  876.  
  877. /* Return a newly-allocated string whose contents 
  878.    concatenate those of S1, S2, S3.  */
  879.  
  880. char *
  881. concat (s1, s2, s3)
  882.      char *s1, *s2, *s3;
  883. {
  884.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  885.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  886.  
  887.   strcpy (result, s1);
  888.   strcpy (result + len1, s2);
  889.   strcpy (result + len1 + len2, s3);
  890.   *(result + len1 + len2 + len3) = 0;
  891.  
  892.   return result;
  893. }
  894.  
  895. /* Like malloc but get fatal error if memory is exhausted.  */
  896.  
  897. char *
  898. xmalloc (size)
  899.      int size;
  900. {
  901.   char *result = malloc (size);
  902.   if (!result)
  903.     fatal ("virtual memory exhausted", 0);
  904.   return result;
  905. }
  906.  
  907. #ifdef USG
  908.  
  909. rename (from, to)
  910.      char *from, *to;
  911. {
  912.   (void) unlink (to);
  913.   if (link (from, to) < 0
  914.       || unlink (from) < 0)
  915.     return -1;
  916.   else
  917.     return 0;
  918. }
  919.  
  920. #endif
  921.  
  922. usage ()
  923. {
  924.   fprintf (stderr, "\
  925. Usage: strip [-gsxSX] [+strip-all] [+strip-debug] [+discard-all]\n\
  926.        [+discard-locals] file...\n");
  927.   exit (1);
  928. }
  929.