home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / gnu / utils / bug / 1377 < prev    next >
Encoding:
Text File  |  1992-08-18  |  23.2 KB  |  968 lines

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