home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 318 / utilsrc / ld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  90.2 KB  |  3,350 lines

  1. /* Linker `ld' for GNU
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously
  32. and appropriately publish on each copy a valid copyright notice
  33. "Copyright (C) 1988 Free Software Foundation, Inc.", and include
  34. following the copyright notice a verbatim copy of the above disclaimer
  35. of warranty and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be licensed at no charge to all
  47.     third parties on terms identical to those contained in this
  48.     License Agreement (except that you may choose to grant more extensive
  49.     warranty protection to some or all third parties, at your option).
  50.  
  51.     c) You may charge a distribution fee for the physical act of
  52.     transferring a copy, and you may at your option offer warranty
  53.     protection in exchange for a fee.
  54.  
  55. Mere aggregation of another unrelated program with this program (or its
  56. derivative) on a volume of a storage or distribution medium does not bring
  57. the other program under the scope of these terms.
  58.  
  59.   3. You may copy and distribute this program (or a portion or derivative
  60. of it, under Paragraph 2) in object code or executable form under the terms
  61. of Paragraphs 1 and 2 above provided that you also do one of the following:
  62.  
  63.     a) accompany it with the complete corresponding machine-readable
  64.     source code, which must be distributed under the terms of
  65.     Paragraphs 1 and 2 above; or,
  66.  
  67.     b) accompany it with a written offer, valid for at least three
  68.     years, to give any third party free (except for a nominal
  69.     shipping charge) a complete machine-readable copy of the
  70.     corresponding source code, to be distributed under the terms of
  71.     Paragraphs 1 and 2 above; or,
  72.  
  73.     c) accompany it with the information you received as to where the
  74.     corresponding source code may be obtained.  (This alternative is
  75.     allowed only for noncommercial distribution and only if you
  76.     received the program in object code or executable form alone.)
  77.  
  78. For an executable file, complete source code means all the source code for
  79. all modules it contains; but, as a special exception, it need not include
  80. source code for modules which are standard libraries that accompany the
  81. operating system on which the executable file runs.
  82.  
  83.   4. You may not copy, sublicense, distribute or transfer this program
  84. except as expressly provided under this License Agreement.  Any attempt
  85. otherwise to copy, sublicense, distribute or transfer this program is void and
  86. your rights to use the program under this License agreement shall be
  87. automatically terminated.  However, parties who have received computer
  88. software programs from you with this License Agreement will not have
  89. their licenses terminated so long as such parties remain in full compliance.
  90.  
  91.   5. If you wish to incorporate parts of this program into other free
  92. programs whose distribution conditions are different, write to the Free
  93. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  94. worked out a simple rule that can be stated here, but we will often permit
  95. this.  We will be guided by the two goals of preserving the free status of
  96. all derivatives of our free software and of promoting the sharing and reuse of
  97. software.
  98.  
  99.  In other words, you are welcome to use, share and improve this program.
  100.  You are forbidden to forbid anyone else to use, share and improve
  101.  what you give them.   Help stamp out software-hoarding!  */
  102.  
  103. /* Written by Richard Stallman with some help from Eric Albert.  */
  104.  
  105. /* define this if on a system 
  106.  where the names etext, edata and end should not have underscores.  */
  107. /* #define nounderscore 1 */
  108.  
  109. #ifndef atarist
  110.  
  111. #include <a.out.h>
  112. #include <ar.h>
  113. #include <stdio.h>
  114. #include <sys/types.h>
  115. #include <strings.h>
  116. #include <sys/stat.h>
  117. #include "symseg.h"
  118. #include <sys/file.h>
  119.  
  120. #else
  121.  
  122. #include <gnu-out.h>
  123. #include <st-out.h>
  124. #include "ar.h"
  125. #include <stdio.h>
  126. #include <types.h>
  127. #include <string.h>
  128. #include <stat.h>
  129. #include "symseg.h"
  130. #include <file.h>
  131. #include <ctype.h>
  132.  
  133. #define ferror(foo)    0
  134.  
  135. /* stuff used when emitting reloc info here */
  136.  
  137. #define RBUF_SIZE 1024
  138.  
  139. char rbuf[RBUF_SIZE];
  140. int rbuf_size = 0;        /* how many bytes are in there now */
  141. long rbuf_last_pc = -1;        /* last rel pc; -1 means none yet */
  142.  
  143. /* and a stack */
  144. long _stksize = 131072;
  145.  
  146. #endif
  147.  
  148. #define min(a,b) ((a) < (b) ? (a) : (b))
  149.  
  150. /* Size of a page; obtained from the operating system.  */
  151.  
  152. int page_size;
  153.  
  154. /* Symbol table */
  155.  
  156. /* Global symbol data is recorded in these structures,
  157.    one for each global symbol.
  158.    They are found via hashing in 'symtab', which points to a vector of buckets.
  159.    Each bucket is a chain of these structures through the link field.  */
  160.  
  161. typedef
  162.   struct glosym
  163.     {
  164.       /* Pointer to next symbol in this symbol's hash bucket.  */
  165.       struct glosym *link;
  166.       /* Name of this symbol.  */
  167.       char *name;
  168.       /* Value of this symbol as a global symbol.  */
  169.       long value;
  170.       /* Chain of external 'nlist's in files for this symbol, both defs and refs.  */
  171.       struct nlist *refs;
  172.       /* Nonzero means definitions of this symbol as common have been seen,
  173.      and the value here is the largest size specified by any of them.  */
  174.       int max_common_size;
  175.       /* For relocatable_output, records the index of this global sym in the
  176.      symbol table to be written, with the first global sym given index 0.  */
  177.       int def_count;
  178.       /* Nonzero means a definition of this global symbol is known to exist.
  179.      Library members should not be loaded on its account.  */
  180.       char defined;
  181.       /* Nonzero means a reference to this global symbol has been seen
  182.      in a file that is surely being loaded.
  183.      A value higher than 1 is the n_type code for the symbol's definition.  */
  184.       char referenced;
  185.       /* Nonzero means print a message at all refs or defs of this symbol */
  186.       char trace;
  187.     }
  188.   symbol;
  189.  
  190. /* Number of buckets in symbol hash table */
  191. #define    TABSIZE    1009
  192.  
  193. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  194. symbol *symtab[TABSIZE];
  195.  
  196. /* Number of symbols in symbol hash table. */
  197. int num_hash_tab_syms = 0;
  198.  
  199. /* Count the number of nlist entries that are for local symbols.
  200.    This count and the three following counts
  201.    are incremented as as symbols are entered in the symbol table.  */
  202. int local_sym_count;
  203.  
  204. /* Count number of nlist entries that are for local symbols
  205.    whose names don't start with L. */
  206. int non_L_local_sym_count;
  207.  
  208. /* Count the number of nlist entries for debugger info.  */
  209. int debugger_sym_count;
  210.  
  211. /* Count the number of global symbols referenced and not defined.  */
  212. int undefined_global_sym_count;
  213.  
  214. /* Count the number of defined global symbols.
  215.    Each symbol is counted only once
  216.    regardless of how many different nlist entries refer to it,
  217.    since the output file will need only one nlist entry for it.
  218.    This count is computed by `digest_symbols';
  219.    it is undefined while symbols are being loaded. */
  220. int defined_global_sym_count;
  221.  
  222. /* Total number of symbols to be written in the output file.
  223.    Computed by digest_symbols from the variables above.  */
  224. int nsyms;
  225.  
  226.  
  227. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  228.    -e sets this.  */
  229. symbol *entry_symbol;
  230.  
  231. symbol *edata_symbol;   /* the symbol _edata */
  232. symbol *etext_symbol;   /* the symbol _etext */
  233. symbol *end_symbol;    /* the symbol _end */
  234.  
  235. /* Each input file, and each library member ("subfile") being loaded,
  236.    has a `file_entry' structure for it.
  237.  
  238.    For files specified by command args, these are contained in the vector
  239.    which `file_table' points to.
  240.  
  241.    For library members, they are dynamically allocated,
  242.    and chained through the `chain' field.
  243.    The chain is found in the `subfiles' field of the `file_entry'.
  244.    The `file_entry' objects for the members have `superfile' fields pointing
  245.    to the one for the library.  */
  246.  
  247. struct file_entry {
  248.   /* Name of this file.  */
  249.   char *filename;
  250.   /* Name to use for the symbol giving address of text start */
  251.   /* Usually the same as filename, but for a file spec'd with -l
  252.      this is the -l switch itself rather than the filename.  */
  253.   char *local_sym_name;
  254.  
  255.   /* Describe the layout of the contents of the file */
  256.  
  257.   /* The file's a.out header.  */
  258.   struct exec header;
  259.   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  260.   int symseg_offset;
  261.  
  262.   /* Describe data from the file loaded into core */
  263.  
  264.   /* Symbol table of the file.  */
  265.   struct nlist *symbols;
  266.   /* Size in bytes of string table.  */
  267.   int string_size;
  268.   /* Pointer to the string table.
  269.      The string table is not kept in core all the time,
  270.      but when it is in core, its address is here.  */
  271.   char *strings;
  272.  
  273.   /* Next two used only if `relocatable_output' */
  274.  
  275.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  276.   struct relocation_info *textrel;
  277.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  278.   struct relocation_info *datarel;
  279.  
  280.   /* Relation of this file's segments to the output file */
  281.  
  282.   /* Start of this file's text seg in the output file core image.  */
  283.   int text_start_address;
  284.   /* Start of this file's data seg in the output file core image.  */
  285.   int data_start_address;
  286.   /* Start of this file's bss seg in the output file core image.  */
  287.   int bss_start_address;
  288.   /* Offset in bytes in the output file symbol table
  289.      of the first local symbol for this file.  Set by `write_file_symbols'.  */
  290.   int local_syms_offset;                   
  291.  
  292.   /* For library members only */
  293.  
  294.   /* For a library, points to chain of entries for the library members.  */
  295.   struct file_entry *subfiles;
  296.   /* For a library member, offset of the member within the archive.
  297.      Zero for files that are not library members.  */
  298.   int starting_offset;
  299.   /* Size of contents of this file, if library member.  */
  300.   int total_size;
  301.   /* For library member, points to the library's own entry.  */
  302.   struct file_entry *superfile;
  303.   /* For library member, points to next entry for next member.  */
  304.   struct file_entry *chain;
  305.  
  306.   /* 1 if file is a library. */
  307.   char library_flag;
  308.  
  309.   /* 1 if file's header has been read into this structure.  */
  310.   char header_read_flag;
  311.  
  312.   /* 1 means search a set of directories for this file.  */
  313.   char search_dirs_flag;
  314.  
  315.   /* 1 means this is base file of incremental load.
  316.      Do not load this file's text or data.
  317.      Also default text_start to after this file's bss. */
  318.   char just_syms_flag;
  319. };
  320.  
  321. /* Vector of entries for input files specified by arguments.
  322.    These are all the input files except for members of specified libraries.  */
  323. struct file_entry *file_table;
  324.  
  325. /* Length of that vector.  */
  326. int number_of_files;
  327.  
  328. /* When loading the text and data, we can avoid doing a close
  329.    and another open between members of the same library.
  330.  
  331.    These two variables remember the file that is currently open.
  332.    Both are zero if no file is open.
  333.  
  334.    See `each_file' and `file_close'.  */
  335.  
  336. struct file_entry *input_file;
  337. int input_desc;
  338.  
  339. /* The name of the file to write; "a.out" by default.  */
  340.  
  341. char *output_filename;
  342.  
  343. /* Descriptor for writing that file with `mywrite'.  */
  344.  
  345. int outdesc;
  346.  
  347. /* Header for that file (filled in by `write_header').  */
  348.  
  349. #ifdef atarist
  350. struct aexec outheader;
  351. #else
  352. struct exec outheader;
  353. #endif
  354.  
  355. /* The following are computed by `digest_symbols'.  */
  356.  
  357. int text_size;        /* total size of text of all input files.  */
  358. int data_size;        /* total size of data of all input files.  */
  359. int bss_size;        /* total size of bss of all input files.  */
  360. int text_reloc_size;    /* total size of text relocation of all input files.  */
  361. int data_reloc_size;    /* total size of data relocation of all input files.  */
  362.  
  363. /* Amount of cleared space to leave between the text and data segments.  */
  364.  
  365. int text_pad;
  366.  
  367. /* Amount of bss segment to include as part of the data segment.  */
  368.  
  369. int data_pad;
  370.  
  371. /* Format of __.SYMDEF:
  372.    First, a longword containing the size of the 'symdef' data that follows.
  373.    Second, zero or more 'symdef' structures.
  374.    Third, a word containing the length of symbol name strings.
  375.    Fourth, zero or more symbol name strings (each followed by a zero).  */
  376.  
  377. struct symdef {
  378.   int symbol_name_string_index;
  379.   int library_member_offset;
  380. };
  381.  
  382. /* Record most of the command options.  */
  383.  
  384. /* Address we assume the text section will be loaded at.
  385.    We relocate symbols and text and data for this, but we do not
  386.    write any padding in the output file for it.  */
  387. int text_start;
  388.  
  389. /* Offset of default entry-pc within the text section.  */
  390. int entry_offset;
  391.  
  392. /* Address we decide the data section will be loaded at.  */
  393. int data_start;
  394.  
  395. /* `text-start' address is normally this much plus a page boundary.
  396.    This is not a user option; it is fixed for each system.  */
  397. int text_start_alignment;
  398.  
  399. /* Nonzero if -T was specified in the command line.
  400.    This prevents text_start from being set later to default values.  */
  401. int T_flag_specified;
  402.  
  403. /* Size to pad data section up to.
  404.    We simply increase the size of the data section, padding with zeros,
  405.    and reduce the size of the bss section to match.  */
  406. int specified_data_size;
  407.  
  408. /* Magic number to use for the output file, set by switch.  */
  409. int magic;
  410.  
  411. /* Nonzero means print names of input files as processed.  */
  412. int trace_files;
  413.  
  414. /* Which symbols should be stripped (omitted from the output):
  415.    none, all, or debugger symbols.  */
  416. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  417.  
  418. /* Which local symbols should be omitted:
  419.    none, all, or those starting with L.
  420.    This is irrelevant if STRIP_NONE.  */
  421. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  422.  
  423. /* 1 => write load map.  */
  424. int write_map;
  425.  
  426. /* 1 => write relocation into output file so can re-input it later.  */
  427. int relocatable_output;
  428.  
  429. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  430. int force_common_definition;
  431.  
  432. /* Standard directories to search for files specified by -l.  */
  433. #ifdef atarist
  434. char *standard_search_dirs[] = {"\\gnu\\lib", "\\gnu"};
  435. #else
  436. char *standard_search_dirs[] = {"/lib", "/usr/lib", "/usr/local/lib"};
  437. #endif
  438.  
  439. /* Actual vector of directories to search;
  440.    this contains those specified with -L plus the standard ones.  */
  441. char **search_dirs;
  442.  
  443. /* Length of the vector `search_dirs'.  */
  444. int n_search_dirs;
  445.  
  446. /* Nonzero means should make the output file executable when done.  */
  447. /* Cleared by nonfatal errors.  */
  448. int make_executable;
  449.  
  450. void digest_symbols ();
  451. void print_symbols ();
  452. void load_symbols ();
  453. void decode_command ();
  454. void list_undefined_symbols ();
  455. void write_output ();
  456. void write_header ();
  457. void write_text ();
  458. void write_data ();
  459. void write_rel ();
  460. void write_syms ();
  461. void padfile ();
  462. char *concat ();
  463. symbol *getsym (), *getsym_soft ();
  464.  
  465. main (argc, argv)
  466.      char **argv;
  467.      int argc;
  468. {
  469. #ifdef atarist
  470.   page_size = 2;    /* only determines granularity of allocation.
  471.                On ST, we don't care */
  472. #else
  473.   page_size = getpagesize ();
  474. #endif
  475.  
  476.   /* Clear the cumulative info on the output file.  */
  477.  
  478.   text_size = 0;
  479.   data_size = 0;
  480.   bss_size = 0;
  481.   text_reloc_size = 0;
  482.   data_reloc_size = 0;
  483.  
  484.   data_pad = 0;
  485.   text_pad = 0;
  486.  
  487. #ifndef atarist
  488. #ifdef sun
  489.   outheader.a_machtype = M_68020;
  490. #endif
  491. #endif
  492.  
  493.   /* Initialize the data about options.  */
  494.  
  495.   specified_data_size = 0;
  496.   strip_symbols = STRIP_NONE;
  497.   trace_files = 0;
  498.   discard_locals = DISCARD_NONE;
  499.   entry_symbol = 0;
  500.   write_map = 0;
  501.   relocatable_output = 0;
  502.   force_common_definition = 0;
  503.   T_flag_specified = 0;
  504.   magic = ZMAGIC;
  505.   make_executable = 1;
  506.  
  507.   /* Initialize the cumulative counts of symbols.  */
  508.  
  509.   local_sym_count = 0;
  510.   non_L_local_sym_count = 0;
  511.   debugger_sym_count = 0;
  512.   undefined_global_sym_count = 0;
  513.  
  514.   /* Completely decode ARGV.  */
  515.  
  516.   decode_command (argc, argv);
  517.  
  518.   /* Create the symbols `etext', `edata' and `end'.  */
  519.  
  520. #ifndef atarist
  521.   if (!relocatable_output)
  522. #endif            /* always do this on atari */
  523.     symtab_init ();
  524.  
  525.   /* Determine whether to count the header as part of
  526.      the text size, and initialize the text size accordingly.
  527.      This depends on the kind of system and on the output format selected.  */
  528.  
  529. #ifndef atarist
  530.   outheader.a_magic = magic;
  531.   text_size = sizeof (struct exec) - N_TXTOFF (outheader);
  532. #else
  533.   outheader.a_magic = CMAGIC;
  534.   text_size = sizeof (struct exec) - A_TXTOFF (outheader);
  535. #endif
  536.   if (text_size < 0)
  537.     text_size = 0;
  538.   entry_offset = text_size;
  539.  
  540. #ifndef atarist
  541.   if (!T_flag_specified && !relocatable_output)
  542.     text_start = N_TXTADDR (outheader);
  543. #endif
  544.  
  545.   /* The text-start address is normally this far past a page boundary.  */
  546.   text_start_alignment = text_start % page_size;
  547.  
  548.   /* Load symbols of all input files.
  549.      Also search all libraries and decide which library members to load.  */
  550.  
  551.   load_symbols ();
  552.  
  553.   /* Compute where each file's sections go, and relocate symbols.  */
  554.  
  555.   digest_symbols ();
  556.  
  557.   /* Print error messages for any missing symbols.  */
  558.  
  559.   if (!relocatable_output)
  560.     list_undefined_symbols (stderr);
  561.  
  562.   /* Print a map, if requested.  */
  563.  
  564.   if (write_map) print_symbols (stdout);
  565.  
  566.   /* Write the output file.  */
  567.  
  568.   write_output ();
  569.  
  570.   return 0;
  571. }
  572.  
  573. void decode_option ();
  574.  
  575. /* Analyze a command line argument.
  576.    Return 0 if the argument is a filename.
  577.    Return 1 if the argument is a option complete in itself.
  578.    Return 2 if the argument is a option which uses an argument.
  579.  
  580.    Thus, the value is the number of consecutive arguments
  581.    that are part of options.  */
  582.  
  583. int
  584. classify_arg (arg)
  585.      register char *arg;
  586. {
  587.   if (*arg != '-') return 0;
  588.   switch (arg[1])
  589.     {
  590.     case 'A':
  591.     case 'D':
  592.     case 'e':
  593.     case 'L':
  594.     case 'l':
  595.     case 'o':
  596.     case 'T':
  597.     case 'u':
  598.     case 'y':
  599.       if (arg[2])
  600.     return 1;
  601.       return 2;
  602.     }
  603.  
  604.   return 1;
  605. }
  606.  
  607. #ifdef atarist
  608. /* things to grok indirect files */
  609.  
  610. int next_indirect_name(f, buf)
  611. FILE * f;
  612. char * buf;
  613. {
  614.   char c;
  615.   char * s = buf;
  616.  
  617.   for (*buf = '\0' ; (((c = fgetc(f)) != EOF) && (isspace(c))) ; )
  618.     ;            /* skip whitespace */
  619.   if (c == EOF) return(0);    /* lose */
  620.   *s++ = c;
  621.   for ( ; (((c = fgetc(f)) != EOF) && (!isspace(c))) ; )
  622.     *s++ = c;
  623.   *s = '\0';            /* finish it */
  624.   return((strlen(buf) > 0));    /* win if saw any */
  625. }
  626.  
  627. int decode_indirect_file(iname)
  628. /* (declare (values n-new-files)) */
  629. char * iname;
  630. {
  631.   char fn[80];        /* name buffer */
  632.   int i;
  633.   FILE * ifile;
  634.  
  635. #ifdef DEBUG
  636.   fprintf(stderr, "decode_indirect_file('%s')\n", iname);
  637. #endif
  638.   if ((ifile = fopen(iname, "r")) == NULL)
  639.     {
  640.     fprintf (stderr, "Can't open indirect file '%s'\n", iname);
  641.     return(0);
  642.     }
  643.   for (i = 0 ; next_indirect_name(ifile, fn) ; )
  644.     i++;        /* count files */
  645.   fclose(ifile);
  646. #ifdef DEBUG
  647.   fprintf(stderr, "  ->%d\n", i);
  648. #endif
  649.   return(i);        /* return file count */
  650. }
  651.  
  652. struct file_entry * process_indirect_file(p, iname)
  653. /* (declare (values updated_pointer)) */
  654. struct file_entry * p;
  655. char * iname;
  656. {
  657.   char fn[80];        /* name buffer */
  658.   FILE * ifile;
  659.  
  660. #ifdef DEBUG
  661.   fprintf(stderr, "process_indirect_file(%X, '%s')\n", p, iname);
  662. #endif
  663.   if ((ifile = fopen(iname, "r")) == NULL)
  664.     {
  665.     fprintf (stderr, "Can't open indirect file '%s'\n", iname);
  666.     return(0);
  667.     }
  668.   for ( ; next_indirect_name(ifile, fn) ; )
  669.     {
  670. #ifdef DEBUG
  671.     fprintf(stderr, "  file '%s'\n", fn);
  672. #endif
  673.     p->filename = concat(fn, "", "");
  674.     p->local_sym_name = p->filename;
  675.     p++;
  676.     }
  677.   fclose(ifile);
  678. #ifdef DEBUG
  679.   fprintf(stderr, "  ->%X\n", p);
  680. #endif
  681.   return(p);        /* return new pointer */
  682. }
  683.  
  684. #endif
  685.  
  686.  
  687. /* Process the command arguments,
  688.    setting up file_table with an entry for each input file,
  689.    and setting variables according to the options.  */
  690.  
  691. void
  692. decode_command (argc, argv)
  693.      char **argv;
  694.      int argc;
  695. {
  696.   register int i;
  697.   register struct file_entry *p;
  698.  
  699.   number_of_files = 0;
  700.   output_filename = "a.out";
  701.  
  702.   n_search_dirs = 0;
  703.   search_dirs = (char **) xmalloc (sizeof (char *));
  704.  
  705.   /* First compute number_of_files so we know how long to make file_table.  */
  706.   /* Also process most options completely.  */
  707.  
  708.   for (i = 1; i < argc; i++)
  709.     {
  710.       register int code = classify_arg (argv[i]);
  711.       if (code)
  712.     {
  713.       if (i + code > argc)
  714.         fatal ("no argument following %s\n", argv[i]);
  715.  
  716.       decode_option (argv[i], argv[i+1]);
  717.  
  718.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  719.         number_of_files++;
  720.  
  721.       i += code - 1;
  722.     }
  723.       else
  724. #ifdef atarist
  725.     if (argv[i][0] == '@')        /* indirect file? */
  726.       number_of_files += decode_indirect_file(&argv[i][1]);
  727.      else
  728. #endif
  729.     number_of_files++;
  730.     }
  731.  
  732.   if (!number_of_files)
  733.     fatal ("no input files", 0);
  734.  
  735.   p = file_table
  736.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  737.   bzero (p, number_of_files * sizeof (struct file_entry));
  738.  
  739.   /* Now scan again and fill in file_table.  */
  740.   /* All options except -A and -l are ignored here.  */
  741.  
  742.   for (i = 1; i < argc; i++)
  743.     {
  744.       register int code = classify_arg (argv[i]);
  745.  
  746.       if (code)
  747.     {
  748.       char *string;
  749.       if (code == 2)
  750.         string = argv[i+1];
  751.       else
  752.         string = &argv[i][2];
  753.  
  754.       if (argv[i][1] == 'A')
  755.         {
  756.           if (p != file_table)
  757.         fatal ("-A specified before an input file other than the first");
  758.  
  759.           p->filename = string;
  760.           p->local_sym_name = string;
  761.           p->just_syms_flag = 1;
  762.           p++;
  763.         }
  764.       if (argv[i][1] == 'l')
  765.         {
  766. #ifndef atarist
  767. /* what the fuck? */
  768.           p->filename = concat ("lib", string, ".a");
  769. #else
  770.           p->filename = concat (string, "", "");
  771. #endif
  772.           p->local_sym_name = concat ("-l", string, "");
  773.           p->search_dirs_flag = 1;
  774.           p++;
  775.         }
  776.       i += code - 1;
  777.     }
  778.       else
  779. #ifdef atarist
  780.     if (argv[i][0] == '@')
  781.       p = process_indirect_file(p, &argv[i][1]);
  782.      else
  783. #endif
  784.     {
  785.       p->filename = argv[i];
  786.       p->local_sym_name = argv[i];
  787.       p++;
  788.     }
  789.     }
  790.  
  791.   /* Now check some option settings for consistency.  */
  792.  
  793.   if ((magic == ZMAGIC || magic == NMAGIC)
  794.       && (text_start - text_start_alignment) & (page_size - 1))
  795.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  796.  
  797. #ifdef atarist
  798.   {
  799. /* see if there's an env var that says where to search for things */
  800.     char * value = (char * )env_value("GNULIB");
  801.  
  802. #ifdef DEBUG
  803.     fprintf(stderr, "env->%X\n", value);
  804. #endif
  805.     if (value)
  806.     {
  807.     n_search_dirs++;
  808.     search_dirs =
  809.       (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  810.     search_dirs[n_search_dirs-1] = concat(value, "", "");
  811. #ifdef DEBUG
  812.     fprintf(stderr, "... '%s'\n", search_dirs[n_search_dirs-1]);
  813. #endif
  814.     }    
  815.   }
  816. #endif
  817.  
  818.   /* Append the standard search directories to the user-specified ones.  */
  819.   {
  820.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  821.     n_search_dirs += n;
  822.     search_dirs
  823.       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  824.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  825.        n * sizeof (char *));
  826.   }
  827. }
  828.  
  829. /* Record an option and arrange to act on it later.
  830.    ARG should be the following command argument,
  831.    which may or may not be used by this option.
  832.  
  833.    The `l' and `A' options are ignored here since they actually
  834.    specify input files.  */
  835.  
  836. void
  837. decode_option (swt, arg)
  838.      register char *swt, *arg;
  839. {
  840.   if (swt[2] != 0)
  841.     arg = &swt[2];
  842.  
  843.   switch (swt[1])
  844.     {
  845.     case 'A':
  846.       return;
  847.  
  848.     case 'D':
  849.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  850.       return;
  851.  
  852.     case 'd':
  853.       force_common_definition = 1;
  854.       return;
  855.  
  856.     case 'e':
  857.       entry_symbol = getsym (arg);
  858.       if (!entry_symbol->defined && !entry_symbol->referenced)
  859.     undefined_global_sym_count++;
  860.       entry_symbol->referenced = 1;
  861.       return;
  862.  
  863.     case 'l':
  864.       return;
  865.  
  866.     case 'L':
  867.       n_search_dirs++;
  868.       search_dirs
  869.     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  870.       search_dirs[n_search_dirs - 1] = arg;
  871.       return;
  872.       
  873.     case 'M':
  874.       write_map = 1;
  875.       return;
  876.  
  877.     case 'N':
  878.       magic = OMAGIC;
  879.       return;
  880.  
  881.     case 'n':
  882.       magic = NMAGIC;
  883.       return;
  884.  
  885.     case 'o':
  886.       output_filename = arg;
  887.       return;
  888.  
  889.     case 'r':
  890.       relocatable_output = 1;
  891.       magic = OMAGIC;
  892.       text_start = 0;
  893.       return;
  894.  
  895.     case 'S':
  896.       strip_symbols = STRIP_DEBUGGER;
  897.       return;
  898.  
  899.     case 's':
  900.       strip_symbols = STRIP_ALL;
  901.       return;
  902.  
  903.     case 'T':
  904.       text_start = parse (arg, "%x", "invalid argument to -T");
  905.       T_flag_specified = 1;
  906.       return;
  907.  
  908.     case 't':
  909.       trace_files = 1;
  910.       return;
  911.  
  912.     case 'u':
  913.       {
  914.     register symbol *sp = getsym (arg);
  915.     if (!sp->defined && !sp->referenced) undefined_global_sym_count++;
  916.     sp->referenced = 1;
  917.       }
  918.       return;
  919.  
  920.     case 'X':
  921.       discard_locals = DISCARD_L;
  922.       return;
  923.  
  924.     case 'x':
  925.       discard_locals = DISCARD_ALL;
  926.       return;
  927.  
  928.     case 'y':
  929.       {
  930.     register symbol *sp = getsym (&swt[2]);
  931.     sp->trace = 1;
  932.       }
  933.       return;
  934.  
  935.     case 'z':
  936.       magic = ZMAGIC;
  937.       return;
  938.  
  939.     default:
  940.       fatal ("invalid command option `%s'", swt);
  941.     }
  942. }
  943.  
  944. /** Convenient functions for operating on one or all files being loaded.  */
  945.  
  946. /* Call FUNCTION on each input file entry.
  947.    Do not call for entries for libraries;
  948.    instead, call once for each library member that is being loaded.
  949.  
  950.    FUNCTION receives two arguments: the entry, and ARG.  */
  951.  
  952. void
  953. each_file (function, arg)
  954.      register void (*function)();
  955.      register int arg;
  956. {
  957.   register int i;
  958.  
  959.   for (i = 0; i < number_of_files; i++)
  960.     {
  961.       register struct file_entry *entry = &file_table[i];
  962.       if (entry->library_flag)
  963.         {
  964.       register struct file_entry *subentry = entry->subfiles;
  965.       for (; subentry; subentry = subentry->chain)
  966.         (*function) (subentry, arg);
  967.     }
  968.       else
  969.     (*function) (entry, arg);
  970.     }
  971. }
  972.  
  973. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  974.  
  975. void
  976. each_full_file (function, arg)
  977.      register void (*function)();
  978.      register int arg;
  979. {
  980.   register int i;
  981.  
  982.   for (i = 0; i < number_of_files; i++)
  983.     {
  984.       register struct file_entry *entry = &file_table[i];
  985.       if (entry->just_syms_flag)
  986.     continue;
  987.       if (entry->library_flag)
  988.         {
  989.       register struct file_entry *subentry = entry->subfiles;
  990.       for (; subentry; subentry = subentry->chain)
  991.         (*function) (subentry, arg);
  992.     }
  993.       else
  994.     (*function) (entry, arg);
  995.     }
  996. }
  997.  
  998. /* Close the input file that is now open.  */
  999.  
  1000. void
  1001. file_close ()
  1002. {
  1003.   close (input_desc);
  1004.   input_desc = 0;
  1005.   input_file = 0;
  1006. }
  1007.  
  1008. /* Open the input file specified by 'entry', and return a descriptor.
  1009.    The open file is remembered; if the same file is opened twice in a row,
  1010.    a new open is not actually done.  */
  1011.  
  1012. int
  1013. file_open (entry)
  1014.      register struct file_entry *entry;
  1015. {
  1016.   register int desc;
  1017.  
  1018.   if (entry->superfile)
  1019.     return file_open (entry->superfile);
  1020.  
  1021.   if (entry == input_file)
  1022.     return input_desc;
  1023.  
  1024.   if (input_file) file_close ();
  1025.  
  1026.   if (entry->search_dirs_flag)
  1027.     {
  1028.       register char **p = search_dirs;
  1029.       int i;
  1030.  
  1031.       for (i = 0; i < n_search_dirs; i++)
  1032.     {
  1033.       register char *string
  1034. #ifndef atarist
  1035.         = concat (search_dirs[i], "/", entry->filename);
  1036. #else
  1037.         = concat (search_dirs[i], "\\", entry->filename);
  1038. #endif
  1039.       desc = open (string, O_RDONLY, 0);
  1040.       if (desc > 0)
  1041.         {
  1042.           entry->filename = string;
  1043.           entry->search_dirs_flag = 0;
  1044.           break;
  1045.         }
  1046.       free (string);
  1047.     }
  1048.     }
  1049.   else
  1050.     desc = open (entry->filename, O_RDONLY, 0);
  1051.  
  1052.   if (desc > 0)
  1053.     {
  1054.       input_file = entry;
  1055.       input_desc = desc;
  1056.       return desc;
  1057.     }
  1058.  
  1059.   perror_file (entry);
  1060. }
  1061.  
  1062. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1063.    and then a newline.  */
  1064.  
  1065. prline_file_name (entry, outfile)
  1066.      struct file_entry *entry;
  1067.      FILE *outfile;
  1068. {
  1069.   print_file_name (entry, outfile);
  1070.   fprintf (outfile, "\n");
  1071. }
  1072.  
  1073. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1074.  
  1075. print_file_name (entry, outfile)
  1076.      struct file_entry *entry;
  1077.      FILE *outfile;
  1078. {
  1079.   if (entry->superfile)
  1080.     {
  1081.       print_file_name (entry->superfile, outfile);
  1082.       fprintf (outfile, "(%s)", entry->filename);
  1083.     }
  1084.   else
  1085.     fprintf (outfile, "%s", entry->filename);
  1086. }
  1087.  
  1088. /* Medium-level input routines for rel files.  */
  1089.  
  1090. /* Read a file's header into the proper place in the file_entry.
  1091.    DESC is the descriptor on which the file is open.
  1092.    ENTRY is the file's entry.  */
  1093.  
  1094. void
  1095. read_header (desc, entry)
  1096.      int desc;
  1097.      register struct file_entry *entry;
  1098. {
  1099.   register int len;
  1100.   struct exec *loc = &entry->header;
  1101.  
  1102.   lseek (desc, entry->starting_offset, 0);
  1103.   len = read (desc, loc, sizeof (struct exec));
  1104.   if (len != sizeof (struct exec))
  1105.     fatal_with_file ("failure reading header of ", entry);
  1106.   if (N_BADMAG (*loc))
  1107.     fatal_with_file ("bad magic number in ", entry);
  1108.  
  1109.   entry->header_read_flag = 1;
  1110. }
  1111.  
  1112. /* Read the symbols of file ENTRY into core.
  1113.    Assume it is already open, on descriptor DESC.
  1114.    Also read the length of the string table, which follows the symbol table,
  1115.    but don't read the contents of the string table.  */
  1116.  
  1117. void
  1118. read_entry_symbols (desc, entry)
  1119.      struct file_entry *entry;
  1120.      int desc;
  1121. {
  1122.   int str_size;
  1123.  
  1124.   if (!entry->header_read_flag)
  1125.     read_header (desc, entry);
  1126.  
  1127.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1128.  
  1129.   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1130.   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1131.     fatal_with_file ("premature end of file in symbols of ", entry);
  1132.  
  1133.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1134.   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1135.     fatal_with_file ("bad string table size in ", entry);
  1136.  
  1137.   entry->string_size = str_size;
  1138. }
  1139.  
  1140. /* Read the string table of file ENTRY into core.
  1141.    Assume it is already open, on descriptor DESC.
  1142.    Also record whether a GDB symbol segment follows the string table.  */
  1143.  
  1144. void
  1145. read_entry_strings (desc, entry)
  1146.      struct file_entry *entry;
  1147.      int desc;
  1148. {
  1149.   int buffer;
  1150.  
  1151.   if (!entry->header_read_flag)
  1152.     read_header (desc, entry);
  1153.  
  1154.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1155.   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1156.     fatal_with_file ("premature end of file in strings of ", entry);
  1157.  
  1158.   /* While we are here, see if the file has a symbol segment at the end.
  1159.      For a separate file, just try reading some more.
  1160.      For a library member, compare current pos against total size.  */
  1161.   if (entry->superfile)
  1162.     {
  1163.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1164.     return;
  1165.     }
  1166.   else
  1167.     {
  1168.       buffer = read (desc, &buffer, sizeof buffer);
  1169.       if (buffer == 0)
  1170.     return;
  1171.       if (buffer != sizeof buffer)
  1172.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1173.     }
  1174.  
  1175.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1176. }
  1177.  
  1178. /* Read in the symbols of all input files.  */
  1179.  
  1180. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1181. void enter_file_symbols (), enter_global_ref (), search_library ();
  1182.  
  1183. void
  1184. load_symbols ()
  1185. {
  1186.   register int i;
  1187.  
  1188.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1189.  
  1190.   for (i = 0; i < number_of_files; i++)
  1191.     {
  1192.       register struct file_entry *entry = &file_table[i];
  1193.       read_file_symbols (entry);
  1194.     }
  1195.  
  1196.   if (trace_files) fprintf (stderr, "\n");
  1197. }
  1198.  
  1199. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1200.    If it is a library, search it and load the appropriate members
  1201.    (which means calling this function recursively on those members).  */
  1202.  
  1203. void
  1204. read_file_symbols (entry)
  1205.      register struct file_entry *entry;
  1206. {
  1207.   register int desc;
  1208.   register int len;
  1209.   int magicnum;
  1210.  
  1211.   desc = file_open (entry);
  1212.  
  1213.   len = read (desc, &magicnum, sizeof magicnum);
  1214.   if (len != sizeof magicnum)
  1215.     fatal_with_file ("failure reading header of ", entry);
  1216.  
  1217.   if (!N_BADMAG (*((struct exec *)&magicnum)))
  1218.     {
  1219.       read_entry_symbols (desc, entry);
  1220.       entry->strings = (char *) alloca (entry->string_size);
  1221.       read_entry_strings (desc, entry);
  1222.       enter_file_symbols (entry);
  1223.       entry->strings = 0;
  1224.     }
  1225.   else
  1226.     {
  1227.       char armag[SARMAG];
  1228.  
  1229.       lseek (desc, 0, 0);
  1230.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1231.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1232.       entry->library_flag = 1;
  1233.       search_library (desc, entry);
  1234.     }
  1235.  
  1236.   file_close ();
  1237. }
  1238.  
  1239. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  1240.  
  1241. void
  1242. enter_file_symbols (entry)
  1243.      struct file_entry *entry;
  1244. {
  1245.    register struct nlist *p, *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1246.  
  1247.   if (trace_files) prline_file_name (entry, stderr);
  1248.  
  1249.   for (p = entry->symbols; p < end; p++)
  1250.     if (p->n_type & N_EXT)
  1251.       enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1252.     else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1253.       {
  1254.     if ((p->n_un.n_strx + entry->strings)[0] != 'L')
  1255.       non_L_local_sym_count++;
  1256.     local_sym_count++;
  1257.       }
  1258.     else debugger_sym_count++;
  1259.  
  1260.    /* Count one for the local symbol that we generate,
  1261.       whose name is the file's name (usually) and whose address
  1262.       is the start of the file's text.  */
  1263.  
  1264.   local_sym_count++;
  1265.   non_L_local_sym_count++;
  1266. }
  1267.  
  1268. /* Enter one global symbol in the hash table.
  1269.    NLIST_P points to the `struct nlist' read from the file
  1270.    that describes the global symbol.  NAME is the symbol's name.
  1271.    ENTRY is the file entry for the file the symbol comes from.
  1272.  
  1273.    The `struct nlist' is modified by placing it on a chain of
  1274.    all such structs that refer to the same global symbol.
  1275.    This chain starts in the `refs' field of the symbol table entry
  1276.    and is chained through the `n_name'.  */
  1277.  
  1278. void
  1279. enter_global_ref (nlist_p, name, entry)
  1280.      register struct nlist *nlist_p;
  1281.      char *name;
  1282.      struct file_entry *entry;
  1283. {
  1284.   register symbol *sp = getsym (name);
  1285.   register int type = nlist_p->n_type;
  1286.   int oldref = sp->referenced;
  1287.   int olddef = sp->defined;
  1288.  
  1289.   nlist_p->n_un.n_name = (char *) sp->refs;
  1290.   sp->refs = nlist_p;
  1291.  
  1292.   sp->referenced = 1;
  1293.  
  1294.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1295.     {
  1296.       sp->defined = 1;
  1297.       if (oldref && !olddef) undefined_global_sym_count--;
  1298.       /* If this is a common definition, keep track of largest
  1299.      common definition seen for this symbol.  */
  1300.       if (type == (N_UNDF | N_EXT)
  1301.       && sp->max_common_size < nlist_p->n_value)
  1302.     sp->max_common_size = nlist_p->n_value;
  1303.     }
  1304.   else
  1305.     if (!oldref) undefined_global_sym_count++;
  1306.  
  1307.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  1308.     text_start = nlist_p->n_value;
  1309.  
  1310.   if (sp->trace)
  1311.     {
  1312.       register char *reftype;
  1313.       switch (type & N_TYPE)
  1314.     {
  1315.     case N_UNDF:
  1316.       if (nlist_p->n_value)
  1317.         reftype = "defined as common";
  1318.       else reftype = "referenced";
  1319.       break;
  1320.  
  1321.     case N_ABS:
  1322.       reftype = "defined as absolute";
  1323.       break;
  1324.  
  1325.     case N_TEXT:
  1326.       reftype = "defined in text section";
  1327.       break;
  1328.  
  1329.     case N_DATA:
  1330.       reftype = "defined in data section";
  1331.       break;
  1332.  
  1333.     case N_BSS:
  1334.       reftype = "defined in BSS section";
  1335.       break;
  1336.     }
  1337.  
  1338.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  1339.       print_file_name (entry, stderr);
  1340.       fprintf (stderr, "\n");
  1341.     }
  1342. }
  1343.  
  1344. /* Searching libraries */
  1345.  
  1346. struct file_entry *decode_library_subfile ();
  1347. void linear_library (), symdef_library ();
  1348.  
  1349. /* Search the library ENTRY, already open on descriptor DESC.
  1350.    This means deciding which library members to load,
  1351.    making a chain of `struct file_entry' for those members,
  1352.    and entering their global symbols in the hash table.  */
  1353.  
  1354. void
  1355. search_library (desc, entry)
  1356.      int desc;
  1357.      struct file_entry *entry;
  1358. {
  1359.   int member_length;
  1360.   register char *name;
  1361.   register struct file_entry *subentry;
  1362.  
  1363.   if (!undefined_global_sym_count) return;
  1364.  
  1365.   /* Examine its first member, which starts SARMAG bytes in.  */
  1366.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  1367.   if (!subentry) return;
  1368.  
  1369.   name = subentry->filename;
  1370.   free (subentry);
  1371.  
  1372.   /* Search via __.SYMDEF if that exists, else linearly.  */
  1373.  
  1374.   if (!strcmp (name, "__.SYMDEF"))
  1375.     symdef_library (desc, entry, member_length);
  1376.   else
  1377.     linear_library (desc, entry);
  1378. }
  1379.  
  1380. /* Construct and return a file_entry for a library member.
  1381.    The library's file_entry is library_entry, and the library is open on DESC.
  1382.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  1383.    We store the length of the member into *LENGTH_LOC.  */
  1384.  
  1385. struct file_entry *
  1386. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  1387.      int desc;
  1388.      struct file_entry *library_entry;
  1389.      int subfile_offset;
  1390.      int *length_loc;
  1391. {
  1392.   int bytes_read;
  1393.   register int namelen;
  1394.   int member_length;
  1395.   register char *name;
  1396.   struct ar_hdr hdr1;
  1397.   register struct file_entry *subentry;
  1398.  
  1399.   lseek (desc, subfile_offset, 0);
  1400.  
  1401.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  1402.   if (!bytes_read)
  1403.     return 0;        /* end of archive */
  1404.  
  1405.   if (sizeof hdr1 != bytes_read)
  1406.     fatal_with_file ("malformed library archive ", library_entry);
  1407.  
  1408.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  1409.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  1410.  
  1411.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  1412.   bzero (subentry, sizeof (struct file_entry));
  1413.  
  1414.   for (namelen = 0;
  1415.        namelen < sizeof hdr1.ar_name
  1416.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' ';
  1417.        namelen++);
  1418.  
  1419.   name = (char *) xmalloc (namelen+1);
  1420.   strncpy (name, hdr1.ar_name, namelen);
  1421.   name[namelen] = 0;
  1422.  
  1423.   subentry->filename = name;
  1424.   subentry->local_sym_name = name;
  1425.   subentry->symbols = 0;
  1426.   subentry->strings = 0;
  1427.   subentry->subfiles = 0;
  1428.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  1429.   subentry->superfile = library_entry;
  1430.   subentry->library_flag = 0;
  1431.   subentry->header_read_flag = 0;
  1432.   subentry->just_syms_flag = 0;
  1433.   subentry->chain = 0;
  1434.   subentry->total_size = member_length;
  1435.  
  1436.   (*length_loc) = member_length;
  1437.  
  1438.   return subentry;
  1439. }
  1440.  
  1441. /* Search a library that has a __.SYMDEF member.
  1442.    DESC is a descriptor on which the library is open.
  1443.      The file pointer is assumed to point at the __.SYMDEF data.
  1444.    ENTRY is the library's file_entry.
  1445.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  1446.  
  1447. void
  1448. symdef_library (desc, entry, member_length)
  1449.      int desc;
  1450.      struct file_entry *entry;
  1451.      int member_length;
  1452. {
  1453.   int *symdef_data = (int *) xmalloc (member_length);
  1454.   register struct symdef *symdef_base;
  1455.   char *sym_name_base;
  1456.   int number_of_symdefs;
  1457.   int length_of_strings;
  1458.   int not_finished;
  1459.   int bytes_read;
  1460.   register int i;
  1461.   struct file_entry *prev = 0;
  1462.   int prev_offset = 0;
  1463.  
  1464.   bytes_read = read (desc, symdef_data, member_length);
  1465.   if (bytes_read != member_length)
  1466.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  1467.  
  1468.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  1469.   if (number_of_symdefs < 0 ||
  1470.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) >= member_length)
  1471.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  1472.  
  1473.   symdef_base = (struct symdef *) (symdef_data + 1);
  1474.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  1475.  
  1476.   if (length_of_strings < 0
  1477.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  1478.       + 2 * sizeof (int) != member_length)
  1479.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  1480.  
  1481.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  1482.  
  1483.   /* Check all the string indexes for validity.  */
  1484.  
  1485.   for (i = 0; i < number_of_symdefs; i++)
  1486.     {
  1487.       register int index = symdef_base[i].symbol_name_string_index;
  1488.       if (index < 0 || index >= length_of_strings
  1489.       || (index && *(sym_name_base + index - 1)))
  1490.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  1491.     }
  1492.  
  1493.   /* Search the symdef data for members to load.
  1494.      Do this until one whole pass finds nothing to load.  */
  1495.  
  1496.   not_finished = 1;
  1497.   while (not_finished)
  1498.     {
  1499.       not_finished = 0;
  1500.  
  1501.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  1502.      Load the library members that contain such symbols.  */
  1503.  
  1504.       for (i = 0; i < number_of_symdefs && undefined_global_sym_count; i++)
  1505.     if (symdef_base[i].symbol_name_string_index >= 0)
  1506.       {
  1507.         register symbol *sp;
  1508.  
  1509.         sp = getsym_soft (sym_name_base
  1510.                   + symdef_base[i].symbol_name_string_index);
  1511.  
  1512.         /* If we find a symbol that appears to be needed, think carefully
  1513.            about the archive member that the symbol is in.  */
  1514.  
  1515.         if (sp && sp->referenced && !sp->defined)
  1516.           {
  1517.         int junk;
  1518.         register int j;
  1519.         register int offset = symdef_base[i].library_member_offset;
  1520.         struct file_entry *subentry;
  1521.  
  1522.         /* Don't think carefully about any archive member
  1523.            more than once in a given pass.  */
  1524.  
  1525.         if (prev_offset == offset)
  1526.           continue;
  1527.         prev_offset = offset;
  1528.  
  1529.         /* Read the symbol table of the archive member.  */
  1530.  
  1531.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  1532.         read_entry_symbols (desc, subentry);
  1533.         subentry->strings = (char *) malloc (subentry->string_size);
  1534.         read_entry_strings (desc, subentry);
  1535.  
  1536.         /* Now scan the symbol table and decide whether to load.  */
  1537.  
  1538.         if (!subfile_wanted_p (subentry))
  1539.           {
  1540.             free (subentry->symbols);
  1541.             free (subentry);
  1542.           }
  1543.         else
  1544.           {
  1545.             /* This member is needed; load it.
  1546.                Since we are loading something on this pass,
  1547.                we must make another pass through the symdef data.  */
  1548.  
  1549.             not_finished = 1;
  1550.  
  1551.             enter_file_symbols (subentry);
  1552.  
  1553.             if (prev)
  1554.               prev->chain = subentry;
  1555.             else entry->subfiles = subentry;
  1556.             prev = subentry;
  1557.  
  1558.             /* Clear out this member's symbols from the symdef data
  1559.                so that following passes won't waste time on them.  */
  1560.  
  1561.             for (j = 0; j < number_of_symdefs; j++)
  1562.               {
  1563.             if (symdef_base[j].library_member_offset == offset)
  1564.               symdef_base[j].symbol_name_string_index = -1;
  1565.               }
  1566.           }
  1567.  
  1568.         /* We'll read the strings again if we need them again.  */
  1569.         free (subentry->strings);
  1570.           }
  1571.       }
  1572.     }
  1573.  
  1574.   free (symdef_data);
  1575. }
  1576.  
  1577. /* Search a library that has no __.SYMDEF.
  1578.    ENTRY is the library's file_entry.
  1579.    DESC is the descriptor it is open on.  */
  1580.  
  1581. void
  1582. linear_library (desc, entry)
  1583.      int desc;
  1584.      struct file_entry *entry;
  1585. {
  1586.   register struct file_entry *prev = 0;
  1587.   register int this_subfile_offset = SARMAG;
  1588.  
  1589.   while (undefined_global_sym_count)
  1590.     {
  1591.       int member_length;
  1592.       register struct file_entry *subentry;
  1593.  
  1594.       subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
  1595.  
  1596.       if (!subentry) return;
  1597.  
  1598.       read_entry_symbols (desc, subentry);
  1599.       subentry->strings = (char *) alloca (subentry->string_size);
  1600.       read_entry_strings (desc, subentry);
  1601.  
  1602.       if (!subfile_wanted_p (subentry))
  1603.     {
  1604.       free (subentry->symbols);
  1605.       free (subentry);
  1606.     }
  1607.       else
  1608.     {
  1609.       enter_file_symbols (subentry);
  1610.  
  1611.       if (prev)
  1612.         prev->chain = subentry;
  1613.       else entry->subfiles = subentry;
  1614.       prev = subentry;
  1615.     }
  1616.  
  1617.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  1618.       if (this_subfile_offset & 1) this_subfile_offset++;
  1619.     }
  1620. }
  1621.  
  1622. /* ENTRY is an entry for a library member.
  1623.    Its symbols have been read into core, but not entered.
  1624.    Return nonzero if we ought to load this member.  */
  1625.  
  1626. int
  1627. subfile_wanted_p (entry)
  1628.      struct file_entry *entry;
  1629. {
  1630.   register struct nlist *p;
  1631.   register struct nlist *end
  1632.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1633.  
  1634.   for (p = entry->symbols; p < end; p++)
  1635.     {
  1636.       register int type = p->n_type;
  1637.  
  1638.       if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value))
  1639.     {
  1640.       register char *name = p->n_un.n_strx + entry->strings;
  1641.       register symbol *sp = getsym_soft (name);
  1642.  
  1643.       /* If this symbol has not been hashed, we can't be looking for it. */
  1644.  
  1645.       if (!sp) continue;
  1646.  
  1647.       if (sp->referenced && !sp->defined)
  1648.         {
  1649.           /* This is a symbol we are looking for.  */
  1650.           if (type == (N_UNDF | N_EXT))
  1651.         {
  1652.           /* Symbol being defined as common.
  1653.              Remember this, but don't load subfile just for this.  */
  1654.  
  1655.           if (sp->max_common_size < p->n_value)
  1656.             sp->max_common_size = p->n_value;
  1657.           if (!sp->defined)
  1658.             undefined_global_sym_count--;
  1659.           sp->defined = 1;
  1660.           continue;
  1661.         }
  1662.  
  1663.           if (write_map)
  1664.         {
  1665.           print_file_name (entry, stdout);
  1666.           fprintf (stdout, " needed due to %s\n", sp->name);
  1667.         }
  1668.           return 1;
  1669.         }
  1670.     }
  1671.     }
  1672.  
  1673.   return 0;
  1674. }
  1675.  
  1676. void consider_file_section_lengths (), relocate_file_addresses ();
  1677.  
  1678. /* Having entered all the global symbols and found the sizes of sections
  1679.    of all files to be linked, make all appropriate deductions from this data.
  1680.  
  1681.    We propagate global symbol values from definitions to references.
  1682.    We compute the layout of the output file and where each input file's
  1683.    contents fit into it.  */
  1684.  
  1685. void
  1686. digest_symbols ()
  1687. {
  1688.   register int i;
  1689.  
  1690.   if (trace_files)
  1691.     fprintf (stderr, "Digesting symbol information:\n\n");
  1692.  
  1693.   /* Compute total size of sections */
  1694.  
  1695.   each_file (consider_file_section_lengths, 0);
  1696.  
  1697.   /* If necessary, pad text section to full page in the file.
  1698.      Include the padding in the text segment size.  */
  1699.  
  1700. #ifndef atarist
  1701.   if (magic == NMAGIC || magic == ZMAGIC)
  1702.     {
  1703.       int text_end = text_size + N_TXTOFF (outheader);
  1704.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  1705.       text_size += text_pad;
  1706.     }
  1707. #endif
  1708.  
  1709.   outheader.a_text = text_size;
  1710.  
  1711.   /* Make the data segment address start in memory on a suitable boundary.  */
  1712.  
  1713. #ifdef atarist
  1714. /* what does this really do? */
  1715.   data_start = text_start + text_size;
  1716. #else
  1717.   data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  1718. #endif
  1719.  
  1720.   /* Compute start addresses of each file's sections and symbols.  */
  1721.  
  1722.   each_full_file (relocate_file_addresses, 0);
  1723.  
  1724.   /* Now, for each symbol, verify that it is defined globally at most once.
  1725.      Put the global value into the symbol entry.
  1726.      Common symbols are allocated here, in the BSS section.
  1727.      Each defined symbol is given a '->defined' field
  1728.       which is the correct N_ code for its definition,
  1729.       except in the case of common symbols with -r.
  1730.      Then make all the references point at the symbol entry
  1731.      instead of being chained together. */
  1732.  
  1733.   defined_global_sym_count = 0;
  1734.  
  1735.   for (i = 0; i < TABSIZE; i++)
  1736.     {
  1737.       register symbol *sp;
  1738.       for (sp = symtab[i]; sp; sp = sp->link)
  1739.     {
  1740.       register struct nlist *p, *next;
  1741.       int defs = 0, com = sp->max_common_size, erred = 0;
  1742.       for (p = sp->refs; p; p = next)
  1743.         {
  1744.           register int type = p->n_type;
  1745.           if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  1746.         {
  1747.           /* non-common definition */
  1748.           if (defs++ && sp->value != p->n_value)
  1749.             if (!erred++)
  1750.               {
  1751.                 make_executable = 0;
  1752.             error ("multiple definitions of symbol %s", sp->name);
  1753.               }
  1754.           sp->value = p->n_value;
  1755.           sp->defined = type;
  1756.         }
  1757.           next = (struct nlist *) p->n_un.n_name;
  1758.           p->n_un.n_name = (char *) sp;
  1759.         }
  1760.       /* Allocate as common if defined as common and not defined for real */
  1761.       if (com && !defs)
  1762.         {
  1763.           if (!relocatable_output || force_common_definition)
  1764.         {
  1765.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  1766.     
  1767.           sp->value = data_start + data_size + bss_size;
  1768.           sp->defined = N_BSS | N_EXT;
  1769.           bss_size += com;
  1770.           if (write_map)
  1771.             printf ("Allocating common %s: %x at %x\n",
  1772.                 sp->name, com, sp->value);
  1773.         }
  1774.         }
  1775.       if (sp->defined)
  1776.         defined_global_sym_count++;
  1777.     }
  1778.     }
  1779.  
  1780.   etext_symbol->value = text_size + text_start;
  1781.   edata_symbol->value = data_start + data_size;
  1782.   end_symbol->value = data_start + data_size + bss_size;
  1783.  
  1784.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  1785.  
  1786.   if (specified_data_size && specified_data_size > data_size)
  1787.     data_pad = specified_data_size - data_size;
  1788.  
  1789.   if (magic == ZMAGIC)
  1790.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  1791.                - data_size;
  1792.  
  1793.   bss_size -= data_pad;
  1794.   if (bss_size < 0) bss_size = 0;
  1795.  
  1796.   data_size += data_pad;
  1797. }
  1798.  
  1799. /* Accumulate the section sizes of input file ENTRY
  1800.    into the section sizes of the output file.  */
  1801.  
  1802. void
  1803. consider_file_section_lengths (entry)
  1804.      register struct file_entry *entry;
  1805. {
  1806.   if (entry->just_syms_flag)
  1807.     return;
  1808.  
  1809.   entry->text_start_address = text_size;
  1810.   text_size += entry->header.a_text;
  1811.   entry->data_start_address = data_size;
  1812.   data_size += entry->header.a_data;
  1813.   entry->bss_start_address = bss_size;
  1814.   bss_size += entry->header.a_bss;
  1815.  
  1816. #ifdef atarist
  1817. /* something's probably wrong elsewhere that forces this kludge.
  1818.    if bss_size turns out to be odd, add one to it */
  1819.   if (bss_size & 1)
  1820.     bss_size++;
  1821. #endif
  1822.  
  1823.   text_reloc_size += entry->header.a_trsize;
  1824.   data_reloc_size += entry->header.a_drsize;
  1825. }
  1826.  
  1827. /* Determine where the sections of ENTRY go into the output file,
  1828.    whose total section sizes are already known.
  1829.    Also relocate the addresses of the file's local and debugger symbols.  */
  1830.  
  1831. void
  1832. relocate_file_addresses (entry)
  1833.      register struct file_entry *entry;
  1834. {
  1835.   entry->text_start_address += text_start;
  1836.   /* Note that `data_start' and `data_size' have not yet been
  1837.      adjusted for `data_pad'.  If they had been, we would get the wrong
  1838.      results here.  */
  1839.   entry->data_start_address += data_start;
  1840.   entry->bss_start_address += data_start + data_size;
  1841.  
  1842.   {
  1843.     register struct nlist *p;
  1844.     register struct nlist *end
  1845.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1846.  
  1847.     for (p = entry->symbols; p < end; p++)
  1848.       {
  1849.     /* If this belongs to a section, update it by the section's start address */
  1850.     register int type = p->n_type & N_TYPE;
  1851.  
  1852.     if (type == N_TEXT)
  1853.       p->n_value += entry->text_start_address;
  1854.     else if (type == N_DATA)
  1855.       /* A symbol whose value is in the data section
  1856.          is present in the input file as if the data section
  1857.          started at an address equal to the length of the file's text.  */
  1858.       p->n_value += entry->data_start_address - entry->header.a_text;
  1859.     else if (type == N_BSS)
  1860.       /* likewise for symbols with value in BSS.  */
  1861.       p->n_value += entry->bss_start_address
  1862.              - entry->header.a_text - entry->header.a_data;
  1863.       }
  1864.   }
  1865. }
  1866.  
  1867. void describe_file_sections (), list_file_locals ();
  1868.  
  1869. /* Print a complete or partial map of the output file.  */
  1870.  
  1871. void
  1872. print_symbols (outfile)
  1873.      FILE *outfile;
  1874. {
  1875.   register int i;
  1876.  
  1877.   fprintf (outfile, "\nFiles:\n\n");
  1878.  
  1879.   each_file (describe_file_sections, outfile);
  1880.  
  1881.   fprintf (outfile, "\nGlobal symbols:\n\n");
  1882.  
  1883.   for (i = 0; i < TABSIZE; i++)
  1884.     {
  1885.       register symbol *sp;
  1886.       for (sp = symtab[i]; sp; sp = sp->link)
  1887.     {
  1888.       if (sp->defined == 1)
  1889.         fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  1890.       if (sp->defined)
  1891.         fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
  1892.       else if (sp->referenced)
  1893.         fprintf (outfile, "  %s: undefined\n", sp->name);
  1894.     }
  1895.     }
  1896.  
  1897.   each_file (list_file_locals, outfile);
  1898. }
  1899.  
  1900. void
  1901. describe_file_sections (entry, outfile)
  1902.      struct file_entry *entry;
  1903.      FILE *outfile;
  1904. {
  1905.   fprintf (outfile, "  ");
  1906.   print_file_name (entry, outfile);
  1907.   if (entry->just_syms_flag)
  1908.     fprintf (outfile, " symbols only\n", 0);
  1909.   else
  1910.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  1911.          entry->text_start_address, entry->header.a_text,
  1912.          entry->data_start_address, entry->header.a_data,
  1913.          entry->bss_start_address, entry->header.a_bss);
  1914. }
  1915.  
  1916. void
  1917. list_file_locals (entry, outfile)
  1918.      struct file_entry *entry;
  1919.      FILE *outfile;
  1920. {
  1921.   register struct nlist *p, *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1922.  
  1923.   entry->strings = (char *) alloca (entry->string_size);
  1924.   read_entry_strings (file_open (entry), entry);
  1925.  
  1926.   fprintf (outfile, "\nLocal symbols of ");
  1927.   print_file_name (entry, outfile);
  1928.   fprintf (outfile, ":\n\n");
  1929.  
  1930.   for (p = entry->symbols; p < end; p++)
  1931.     /* If this is a definition, 
  1932.        update it if necessary by this file's start address.  */
  1933.     if (!(p->n_type & (N_STAB | N_EXT)))
  1934.       fprintf (outfile, "  %s: 0x%x\n",
  1935.            entry->strings + p->n_un.n_strx, p->n_value);
  1936. }
  1937.  
  1938. /* Print on OUTFILE a list of all the undefined global symbols.
  1939.    If there are any such, it is an error, so clear `make_executable'.  */
  1940.  
  1941. void
  1942. list_undefined_symbols (outfile)
  1943.      FILE *outfile;
  1944. {
  1945.   register int i;
  1946.   register int count = 0;
  1947.  
  1948.   for (i = 0; i < TABSIZE; i++)
  1949.     {
  1950.       register symbol *sp;
  1951.       for (sp = symtab[i]; sp; sp = sp->link)
  1952.     {
  1953.       if (sp->referenced && !sp->defined)
  1954.         {
  1955.           if (!count++)
  1956.         fprintf (outfile, "Undefined symbols:\n");
  1957.           fprintf (outfile, " %s\n", sp->name);
  1958.         }
  1959.     }
  1960.     }
  1961.  
  1962.   if (count)
  1963.     fprintf (outfile, "\n");
  1964.   if (count)
  1965.     make_executable = 0;
  1966. }
  1967.  
  1968. /* Write the output file */ 
  1969.  
  1970. void
  1971. write_output ()
  1972. {
  1973.   struct stat statbuf;
  1974.   int filemode;
  1975.  
  1976.   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  1977.   if (outdesc < 0) perror_name (output_filename);
  1978.  
  1979. #ifdef atarist
  1980.   if (stat (output_filename, &statbuf) < 0)
  1981. #else
  1982.   if (fstat (outdesc, &statbuf) < 0)
  1983. #endif
  1984.     perror_name (output_filename);
  1985.  
  1986.   filemode = statbuf.st_mode;
  1987.  
  1988. #ifndef atarist
  1989.   chmod (output_filename, filemode & ~0111);
  1990. #endif
  1991.  
  1992.   /* Output the a.out header.  */
  1993.   write_header ();
  1994.  
  1995.   /* Output the text and data segments, relocating as we go.  */
  1996.   write_text ();
  1997.   write_data ();
  1998.  
  1999. #ifndef atarist
  2000.   /* Output the merged relocation info, if requested with `-r'.  */
  2001.   if (relocatable_output)
  2002.     write_rel ();
  2003.  
  2004.   /* Output the symbol table (both globals and locals).  */
  2005.   write_syms ();
  2006. #else
  2007. /* atari has symtab section before relocs */
  2008.   write_syms();
  2009.   write_rel();
  2010. #endif
  2011.  
  2012.   /* Copy any GDB symbol segments from input files.  */
  2013. #ifndef atarist        /* turn this on when we get GDB working */
  2014.   write_symsegs ();
  2015. #endif
  2016.  
  2017.   close (outdesc);
  2018.  
  2019. #ifndef atarist
  2020.   if (make_executable)
  2021.     chmod (output_filename, filemode | 0111);
  2022. #endif
  2023. }
  2024.  
  2025. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  2026.  
  2027. void
  2028. write_header ()
  2029. {
  2030. #ifndef atarist
  2031.   outheader.a_magic = magic;
  2032. #ifdef sun
  2033.   outheader.a_machtype = M_68020;
  2034. #endif
  2035. #endif
  2036.   outheader.a_text = text_size;
  2037.   outheader.a_data = data_size;
  2038.   outheader.a_bss = bss_size;
  2039. #ifndef atarist
  2040.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  2041.                : text_start + entry_offset);
  2042. #else
  2043. /* no entry point, always 0 relative */
  2044.   outheader.a_AZero1 = 0;        /* good a place as any to do this */
  2045.   outheader.a_AZero2 = 0;
  2046.   outheader.a_isreloc = ISRELOCINFO;    /* means reloc present (!) */
  2047. #endif
  2048.   if (strip_symbols == STRIP_ALL)
  2049.     nsyms = 0;
  2050.   else
  2051.     {
  2052.       nsyms = defined_global_sym_count + undefined_global_sym_count;
  2053.       if (discard_locals == DISCARD_L)
  2054.     nsyms += non_L_local_sym_count;
  2055.       else if (discard_locals == DISCARD_NONE)
  2056.     nsyms += local_sym_count;
  2057.     }
  2058.  
  2059.   if (strip_symbols == STRIP_NONE)
  2060.     nsyms += debugger_sym_count;
  2061.  
  2062. #ifndef atarist
  2063.   outheader.a_syms = nsyms * sizeof (struct nlist);
  2064. #else
  2065.   outheader.a_syms = nsyms * sizeof (struct asym);
  2066. #endif
  2067.  
  2068. #ifndef atarist
  2069.   if (relocatable_output)
  2070.     {
  2071.       outheader.a_trsize = text_reloc_size;
  2072.       outheader.a_drsize = data_reloc_size;
  2073.     }
  2074.   else
  2075.     {
  2076.       outheader.a_trsize = 0;
  2077.       outheader.a_drsize = 0;
  2078.     }
  2079. #endif
  2080.  
  2081. #ifndef atarist
  2082.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  2083. #else
  2084.   mywrite (&outheader, sizeof outheader, 1, outdesc);
  2085. #endif
  2086.  
  2087.   /* Output whatever padding is required in the executable file
  2088.      between the header and the start of the text.  */
  2089.  
  2090.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  2091. }
  2092.  
  2093. /* Relocate the text segment of each input file
  2094.    and write to the output file.  */
  2095.  
  2096. void
  2097. write_text ()
  2098. {
  2099.   if (trace_files)
  2100.     fprintf (stderr, "Copying and relocating text:\n\n");
  2101.  
  2102.   each_full_file (copy_text);
  2103.   file_close ();
  2104.  
  2105.   if (trace_files)
  2106.     fprintf (stderr, "\n");
  2107.  
  2108.   padfile (text_pad, outdesc);
  2109. }
  2110.  
  2111. int
  2112. text_offset (entry)
  2113.      struct file_entry *entry;
  2114. {
  2115.   return entry->starting_offset + N_TXTOFF (entry->header);
  2116. }
  2117.  
  2118. /* Read the text segment contents of ENTRY, relocate them,
  2119.    and write the result to the output file.
  2120.    If `-r', save the text relocation for later reuse.  */
  2121.  
  2122. void
  2123. copy_text (entry)
  2124.      struct file_entry *entry;
  2125. {
  2126.   register char *bytes;
  2127.   register int desc;
  2128.   register struct relocation_info *reloc;
  2129.  
  2130.   if (trace_files)
  2131.     prline_file_name (entry, stderr);
  2132.  
  2133.   desc = file_open (entry);
  2134.  
  2135.   /* Allocate space for the file's text section and text-relocation.  */
  2136.  
  2137.   bytes = (char *) alloca (entry->header.a_text);
  2138.  
  2139. #ifndef atarist
  2140.   if (relocatable_output)
  2141. #endif        /* on atari, always keep it */
  2142.     reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  2143. #ifndef atarist
  2144.   else
  2145.     reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  2146. #endif
  2147.  
  2148.   /* Read those two sections into core.  */
  2149.  
  2150.   lseek (desc, text_offset (entry), 0);
  2151.   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  2152.     fatal_with_file ("premature eof in text section of ", entry);
  2153.  
  2154.   lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  2155.   if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  2156.     fatal_with_file ("premature eof in text relocation of ", entry);
  2157.  
  2158.   /* Relocate the text according to the text relocation.  */
  2159.  
  2160.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  2161.               reloc, entry->header.a_trsize, entry);
  2162.  
  2163.   /* Write the relocated text to the output file.  */
  2164.  
  2165.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  2166.  
  2167.   /* If `-r', record the text relocation permanently
  2168.      so the combined relocation can be written later.  */
  2169.  
  2170. #ifndef atarist
  2171.   if (relocatable_output)
  2172. #endif
  2173.     entry->textrel = reloc;
  2174. }
  2175.  
  2176. /* Relocate the data segment of each input file
  2177.    and write to the output file.  */
  2178.  
  2179. void
  2180. write_data ()
  2181. {
  2182.   if (trace_files)
  2183.     fprintf (stderr, "Copying and relocating data:\n\n");
  2184.  
  2185.   each_full_file (copy_data);
  2186.   file_close ();
  2187.  
  2188.   if (trace_files)
  2189.     fprintf (stderr, "\n");
  2190.  
  2191.   padfile (data_pad, outdesc);
  2192. }
  2193.  
  2194. /* Read the data segment contents of ENTRY, relocate them,
  2195.    and write the result to the output file.
  2196.    If `-r', save the data relocation for later reuse.
  2197.    See comments in `copy_text'.  */
  2198.  
  2199. void
  2200. copy_data (entry)
  2201.      struct file_entry *entry;
  2202. {
  2203.   register struct relocation_info *reloc;
  2204.   register char *bytes;
  2205.   register int desc;
  2206.  
  2207.   if (trace_files)
  2208.     prline_file_name (entry, stderr);
  2209.  
  2210.   desc = file_open (entry);
  2211.  
  2212.   bytes = (char *) alloca (entry->header.a_data);
  2213.  
  2214. #ifndef atarist
  2215.   if (relocatable_output)
  2216. #endif
  2217.     reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  2218. #ifndef atarist
  2219.   else
  2220.     reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  2221. #endif
  2222.  
  2223.   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  2224.   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  2225.     fatal_with_file ("premature eof in data section of ", entry);
  2226.  
  2227.   lseek (desc, text_offset (entry) + entry->header.a_text
  2228.          + entry->header.a_data + entry->header.a_trsize,
  2229.            0);
  2230.   if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  2231.     fatal_with_file ("premature eof in data relocation of ", entry);
  2232.  
  2233.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  2234.               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  2235.  
  2236.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  2237.  
  2238. #ifndef atarist
  2239.   if (relocatable_output)
  2240. #endif
  2241.     entry->datarel = reloc;
  2242. }
  2243.  
  2244. /* Relocate ENTRY's text or data section contents.
  2245.    DATA is the address of the contents, in core.
  2246.    DATA_SIZE is the length of the contents.
  2247.    PC_RELOCATION is the difference between the address of the contents
  2248.      in the output file and its address in the input file.
  2249.    RELOC_INFO is the address of the relocation info, in core.
  2250.    RELOC_SIZE is its length in bytes.  */
  2251.  
  2252. void
  2253. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  2254.      char *data;
  2255.      struct relocation_info *reloc_info;
  2256.      struct file_entry *entry;
  2257.      int pc_relocation;
  2258.      int data_size;
  2259.      int reloc_size;
  2260. {
  2261.   register struct relocation_info *p = reloc_info;
  2262.   struct relocation_info *end
  2263.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  2264.   int text_relocation = entry->text_start_address;
  2265.   int data_relocation = entry->data_start_address - entry->header.a_text;
  2266.   int bss_relocation
  2267.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  2268.  
  2269.   for (; p < end; p++)
  2270.     {
  2271.       register int relocation = 0;
  2272.       register int addr = p->r_address;
  2273.       register int symbolnum = p->r_symbolnum;
  2274.       register int length = p->r_length;
  2275.  
  2276.       if (addr >= data_size)
  2277.     fatal_with_file ("relocation address out of range in ", entry);
  2278.       if (p->r_extern)
  2279.     {
  2280.       int symindex = symbolnum * sizeof (struct nlist);
  2281.       symbol *sp = ((symbol *)
  2282.             (((struct nlist *)
  2283.               (((char *)entry->symbols) + symindex))
  2284.              ->n_un.n_name));
  2285.  
  2286.       if (symindex >= entry->header.a_syms)
  2287.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  2288.  
  2289.       /* If the symbol is undefined, leave it at zero.  */
  2290.       if (! sp->defined)
  2291.         relocation = 0;
  2292.       else
  2293.         relocation = sp->value;
  2294.     }
  2295.       else switch (symbolnum)
  2296.     {
  2297.     case N_TEXT:
  2298.     case N_TEXT | N_EXT:
  2299.       relocation = text_relocation;
  2300.       break;
  2301.  
  2302.     case N_DATA:
  2303.     case N_DATA | N_EXT:
  2304.       /* A word that points to beginning of the the data section
  2305.          initially contains not 0 but rather the "address" of that section
  2306.          in the input file, which is the length of the file's text.  */
  2307.       relocation = data_relocation;
  2308.       break;
  2309.  
  2310.     case N_BSS:
  2311.     case N_BSS | N_EXT:
  2312.       /* Similarly, an input word pointing to the beginning of the bss
  2313.          initially contains the length of text plus data of the file.  */
  2314.       relocation = bss_relocation;
  2315.       break;
  2316.  
  2317.     default:
  2318.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  2319.     }
  2320.  
  2321.       if (p->r_pcrel)
  2322.     relocation -= pc_relocation;
  2323.  
  2324.       switch (length)
  2325.     {
  2326.     case 0:
  2327.       *(char *) (data + addr) += relocation;
  2328.       break;
  2329.  
  2330.     case 1:
  2331.       *(short *) (data + addr) += relocation;
  2332.       break;
  2333.  
  2334.     case 2:
  2335.       *(int *) (data + addr) += relocation;
  2336.       break;
  2337.  
  2338.     default:
  2339.       fatal_with_file ("invalid relocation field length in ", entry);
  2340.     }
  2341.     }
  2342. }
  2343.  
  2344. /* For relocatable_output only: write out the relocation,
  2345.    relocating the addresses-to-be-relocated.  */
  2346.  
  2347. void coptxtrel (), copdatrel ();
  2348. #ifdef atarist
  2349. void emit_atari_text_relocs (), emit_atari_data_relocs ();
  2350. #endif
  2351.  
  2352. void
  2353. write_rel ()
  2354. {
  2355.   register int i;
  2356.   register int count = 0;
  2357.  
  2358.   if (trace_files)
  2359.     fprintf (stderr, "Writing text relocation:\n\n");
  2360.  
  2361.   /* Assign each global symbol a sequence number, giving the order
  2362.      in which `write_syms' will write it.
  2363.      This is so we can store the proper symbolnum fields
  2364.      in relocation entries we write.  */
  2365.  
  2366.   for (i = 0; i < TABSIZE; i++)
  2367.     {
  2368.       symbol *sp;
  2369.       for (sp = symtab[i]; sp; sp = sp->link)
  2370.     if (sp->referenced || sp->defined)
  2371.       sp->def_count = count++;
  2372.     }
  2373.   if (count != defined_global_sym_count + undefined_global_sym_count)
  2374.     fatal ("internal error");
  2375.  
  2376.   /* Write out the relocations of all files, remembered from copy_text.  */
  2377.  
  2378. #ifndef atarist
  2379.   each_full_file (coptxtrel);
  2380. #else
  2381.   each_full_file (emit_atari_text_relocs);
  2382. #endif
  2383.  
  2384.   if (trace_files)
  2385.     fprintf (stderr, "\nWriting data relocation:\n\n");
  2386.  
  2387. #ifndef atarist
  2388.   each_full_file (copdatrel);
  2389. #else
  2390.   each_full_file (emit_atari_data_relocs);
  2391.   emit_atari_reloc_byte(0);        /* end of relocs */
  2392.   flush_atari_rbuf();
  2393. #endif
  2394.  
  2395.   if (trace_files)
  2396.     fprintf (stderr, "\n");
  2397. }
  2398.  
  2399. #ifdef atarist
  2400. /* code here for dumping atari style reloc info */
  2401.  
  2402. void flush_atari_rbuf()
  2403. {
  2404. /* fprintf(stderr, "flush\n"); */
  2405.   write(outdesc, &rbuf, rbuf_size);
  2406.   rbuf_size = 0;
  2407. }
  2408.  
  2409. void emit_atari_reloc_byte(byte)
  2410. char byte;
  2411. {
  2412.   byte = byte & 0xFF;
  2413. /*fprintf(stderr, "  %X\n", byte);*/
  2414.   if (rbuf_size >= RBUF_SIZE)
  2415.     flush_atari_rbuf();
  2416.   rbuf[rbuf_size++] = byte;
  2417. }
  2418.  
  2419. int emit_atari_reloc(rel_pc)
  2420. long rel_pc;
  2421. {
  2422.   long diff;
  2423.  
  2424. /* fprintf(stderr, "reloc at %X\n", rel_pc);  */
  2425.   if (rbuf_last_pc == -1)        /* first time thru? */
  2426.     {
  2427.     emit_atari_reloc_byte(rel_pc >> 24);
  2428.     emit_atari_reloc_byte(rel_pc >> 16);
  2429.     emit_atari_reloc_byte(rel_pc >> 8);
  2430.     emit_atari_reloc_byte(rel_pc);
  2431.     }
  2432.     else
  2433.     {
  2434.     if (rel_pc <= rbuf_last_pc)
  2435.         {
  2436.         fprintf(stderr, "Relocs out of order; last = %X curr = %X\n",
  2437.                 rbuf_last_pc, rel_pc);
  2438. /*        exit(-999);    */
  2439.         return(0);
  2440.         }
  2441.     diff = rel_pc - rbuf_last_pc;
  2442.     while (diff > 254)
  2443.         {
  2444.         emit_atari_reloc_byte(1);
  2445.         diff -= 254;
  2446.         }
  2447.     emit_atari_reloc_byte(diff);
  2448.     }
  2449.   rbuf_last_pc = rel_pc;
  2450.   return(1);
  2451. }
  2452.  
  2453. int reloc_less_p(reloc_1, reloc_2)
  2454. struct relocation_info * reloc_1, * reloc_2;
  2455. {
  2456.   return(reloc_1->r_address < reloc_2->r_address);
  2457. }
  2458.  
  2459. void emit_atari_text_relocs(entry)
  2460.      struct file_entry *entry;
  2461. {
  2462.   int reloc_num;
  2463.   int n_relocs = entry->header.a_trsize / sizeof (struct relocation_info);
  2464.   long text_base = entry->text_start_address;
  2465.  
  2466.   qsort(entry->textrel, n_relocs, sizeof (struct relocation_info), 
  2467.     reloc_less_p);
  2468.   for (reloc_num = n_relocs - 1 ; reloc_num >= 0 ; reloc_num-- )
  2469.     if (!emit_atari_reloc(entry->textrel[reloc_num].r_address + text_base))
  2470.         fprintf(stderr, "  while processing text rel %d/%d of entry %s\n",
  2471.         reloc_num, n_relocs, entry->filename);
  2472. }
  2473.  
  2474. void emit_atari_data_relocs(entry)
  2475.      struct file_entry *entry;
  2476. {
  2477.   int reloc_num;
  2478.   int n_relocs = entry->header.a_drsize / sizeof (struct relocation_info);
  2479.   long data_base = entry->data_start_address;
  2480.  
  2481.   qsort(entry->datarel, n_relocs, sizeof (struct relocation_info), 
  2482.     reloc_less_p);
  2483.   for (reloc_num = n_relocs - 1 ; reloc_num >= 0 ; reloc_num-- )
  2484.     if (!emit_atari_reloc(entry->datarel[reloc_num].r_address + data_base))
  2485.         fprintf(stderr, "  while processing data rel %d/%d of entry %s\n",
  2486.         reloc_num, n_relocs, entry->filename);
  2487. }
  2488.  
  2489. #endif
  2490.  
  2491. void
  2492. coptxtrel (entry)
  2493.      struct file_entry *entry;
  2494. {
  2495.   register struct relocation_info *p, *end;
  2496.   register int reloc = entry->text_start_address;
  2497.  
  2498.   p = entry->textrel;
  2499.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  2500.   while (p < end)
  2501.     {
  2502.       p->r_address += reloc;
  2503.       if (p->r_extern)
  2504.     {
  2505.       register int symindex = p->r_symbolnum * sizeof (struct nlist);
  2506.       symbol *symptr = ((symbol *)
  2507.                 (((struct nlist *)
  2508.                   (((char *)entry->symbols) + symindex))
  2509.                  ->n_un.n_name));
  2510.  
  2511.       if (symindex >= entry->header.a_syms)
  2512.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  2513.       /* If the symbol is now defined, change the external relocation
  2514.          to an internal one.  */
  2515.       if (symptr->defined)
  2516.         {
  2517.           p->r_extern = 0;
  2518.           p->r_symbolnum = (symptr->defined & N_TYPE);
  2519.         }
  2520.       else
  2521.         p->r_symbolnum = (symptr->def_count + nsyms
  2522.                   - defined_global_sym_count
  2523.                   - undefined_global_sym_count);
  2524.     }
  2525.       p++;
  2526.     }
  2527.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  2528. }
  2529.  
  2530. void
  2531. copdatrel (entry)
  2532.      struct file_entry *entry;
  2533. {
  2534.   register struct relocation_info *p, *end;
  2535.   /* Relocate the address of the relocation.
  2536.      Old address is relative to start of the input file's data section.
  2537.      New address is relative to start of the output file's data section.  */
  2538.   register int reloc = entry->data_start_address - text_size;
  2539.  
  2540.   p = entry->datarel;
  2541.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  2542.   while (p < end)
  2543.     {
  2544.       p->r_address += reloc;
  2545.       if (p->r_extern)
  2546.     {
  2547.       register int symindex = p->r_symbolnum * sizeof (struct nlist);
  2548.       if (symindex >= entry->header.a_syms)
  2549.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  2550.       p->r_symbolnum = ((symbol *)
  2551.                 (((struct nlist *)
  2552.                   (((char *)entry->symbols) + symindex))
  2553.                  ->n_un.n_name))
  2554.                ->def_count
  2555.              + nsyms - defined_global_sym_count - undefined_global_sym_count;
  2556.     }
  2557.       p++;
  2558.     }
  2559.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  2560. }
  2561.  
  2562. void write_file_syms ();
  2563. void write_string_table ();
  2564.  
  2565. /* Offsets and current lengths of symbol and string tables in output file. */
  2566.  
  2567. int symbol_table_offset;
  2568. int symbol_table_len;
  2569.  
  2570. /* Address in output file where string table starts.  */
  2571. int string_table_offset;
  2572.  
  2573. /* Offset within string table
  2574.    where the strings in `strtab_vector' should be written.  */
  2575. int string_table_len;
  2576.  
  2577. /* Total size of string table strings allocated so far,
  2578.    including strings in `strtab_vector'.  */
  2579. int strtab_size;
  2580.  
  2581. /* Vector whose elements are strings to be added to the string table.  */
  2582. char **strtab_vector;
  2583.  
  2584. /* Vector whose elements are the lengths of those strings.  */
  2585. int *strtab_lens;
  2586.  
  2587. /* Index in `strtab_vector' at which the next string will be stored.  */
  2588. int strtab_index;
  2589.  
  2590. /* Add the string NAME to the output file string table.
  2591.    Record it in `strtab_vector' to be output later.
  2592.    Return the index within the string table that this string will have.  */
  2593.  
  2594. int
  2595. assign_string_table_index (name)
  2596.      char *name;
  2597. {
  2598.   register int index = strtab_size;
  2599.   register int len = strlen (name) + 1;
  2600.  
  2601.   strtab_size += len;
  2602.   strtab_vector[strtab_index] = name;
  2603.   strtab_lens[strtab_index++] = len;
  2604.  
  2605.   return index;
  2606. }
  2607.  
  2608. FILE *outstream = (FILE *) 0;
  2609.  
  2610. /* Write the contents of `strtab_vector' into the string table.
  2611.    This is done once for each file's local&debugger symbols
  2612.    and once for the global symbols.  */
  2613.  
  2614. void
  2615. write_string_table ()
  2616. {
  2617.   register int i;
  2618.  
  2619.   lseek (outdesc, string_table_offset + string_table_len, 0);
  2620.  
  2621.   if (!outstream)
  2622.     outstream = fdopen (outdesc, "w");
  2623.  
  2624.   for (i = 0; i < strtab_index; i++)
  2625.     {
  2626.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  2627.       string_table_len += strtab_lens[i];
  2628.     }
  2629.  
  2630.   fflush (outstream);
  2631.  
  2632.   /* Report I/O error such as disk full.  */
  2633.   if (ferror (outstream))
  2634.     perror_name (output_filename);
  2635. }
  2636.  
  2637. /* Write the symbol table and string table of the output file.  */
  2638.  
  2639. #ifdef atarist
  2640. /* this is all different on ataris */
  2641.  
  2642. write_atari_sym(p, str)
  2643. register struct nlist * p;
  2644. char * str;
  2645. {
  2646.   struct asym sym;
  2647.   int i;
  2648.  
  2649. /* fprintf(stderr, "sym %s\n", str); */
  2650.  
  2651.   for (i = 0 ; ((i < 8) && str[i]) ; i++)
  2652.     sym.a_name[i] = str[i];
  2653.   for ( ; i < 8 ; i++)
  2654.     sym.a_name[i] = ' ';
  2655.   switch (p->n_type & N_TYPE)
  2656.     {
  2657.     case N_UNDF: sym.a_type = A_UNDF; break;
  2658.     case N_ABS:  sym.a_type = A_EQU; break; /* ??? */
  2659.     case N_TEXT: sym.a_type = A_TEXT; break;
  2660.     case N_DATA: sym.a_type = A_DATA; break;
  2661.     case N_BSS:  sym.a_type = A_BSS; break;
  2662. /* figure out rest of this later */
  2663.     }
  2664.   sym.a_value = p->n_value;
  2665.   mywrite(&sym, sizeof sym, 1, outdesc);
  2666. }
  2667.  
  2668. void write_atari_syms(entry, syms_written_addr)
  2669. struct file_entry * entry;
  2670. int * syms_written_addr;
  2671. {
  2672.   register struct nlist *p = entry->symbols;
  2673.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  2674.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  2675.  
  2676.   /* Read the file's string table.  */
  2677.  
  2678.   entry->strings = (char *) alloca (entry->string_size);
  2679.   read_entry_strings (file_open (entry), entry);
  2680.  
  2681.   /* Generate a local symbol for the start of this file's text.  */
  2682.  
  2683.   if (discard_locals != DISCARD_ALL)
  2684.     {
  2685.       struct nlist nl;
  2686.  
  2687.       nl.n_type = N_TEXT;
  2688. /*      nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); */
  2689.       nl.n_value = entry->text_start_address;
  2690.       nl.n_desc = 0;
  2691.       nl.n_other = 0;
  2692. /*      *bufp++ = nl;    */
  2693.       write_atari_sym(&nl, entry->local_sym_name);
  2694.       (*syms_written_addr)++;
  2695. /* necessary? */
  2696.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  2697.     }
  2698.  
  2699.   for (; p < end; p++)
  2700.     {
  2701.       register int type = p->n_type;
  2702.       register int write = 0;
  2703.  
  2704.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  2705.  
  2706.       if (!(type & (N_STAB | N_EXT)))
  2707.         /* ordinary local symbol */
  2708.     write = (discard_locals != DISCARD_ALL)
  2709.         && !(discard_locals == DISCARD_L &&
  2710.              (p->n_un.n_strx + entry->strings)[0] == 'L');
  2711.       else if (!(type & N_EXT))
  2712.     /* debugger symbol */
  2713.         write = (strip_symbols == STRIP_NONE);
  2714.  
  2715.       if (write)
  2716.     {
  2717.       /* If this symbol has a name, write it */
  2718.  
  2719.       if (p->n_un.n_strx)
  2720.         {
  2721.         write_atari_sym(p, p->n_un.n_strx + entry->strings);
  2722.         (*syms_written_addr)++;
  2723.         }
  2724.     }
  2725.     }
  2726.   entry->strings = NULL;  
  2727. }
  2728.  
  2729. void write_syms ()
  2730. {
  2731.   int n_syms_written = 0;
  2732.   register symbol *sp;
  2733.   int i;
  2734.  
  2735.   if (strip_symbols == STRIP_ALL)
  2736.     return;
  2737.  
  2738.   each_file(write_atari_syms, &n_syms_written);
  2739.  
  2740.   /* Now write out the global symbols.  */
  2741.  
  2742.   /* Scan the symbol hash table, bucket by bucket.  */
  2743.  
  2744.   for (i = 0; i < TABSIZE; i++)
  2745.     for (sp = symtab[i]; sp; sp = sp->link)
  2746.       {
  2747.     struct nlist nl;
  2748.  
  2749.     nl.n_other = 0;
  2750.     nl.n_desc = 0;
  2751.  
  2752.     /* Compute a `struct nlist' for the symbol.  */
  2753.  
  2754.     if (sp->defined || sp->referenced)
  2755.       {
  2756.         if (!sp->defined)          /* undefined -- legit only if -r */
  2757.           {
  2758.         nl.n_type = N_UNDF | N_EXT;
  2759.         nl.n_value = 0;
  2760.           }
  2761.         else if (sp->defined > 1) /* defined with known type */
  2762.           {
  2763.         nl.n_type = sp->defined;
  2764.         nl.n_value = sp->value;
  2765.           }
  2766.         else if (sp->max_common_size) /* defined as common but not allocated. */
  2767.           {
  2768.         /* happens only with -r and not -d */
  2769.         /* write out a common definition */
  2770.         nl.n_type = N_UNDF | N_EXT;
  2771.         nl.n_value = sp->max_common_size;
  2772.           }
  2773.         else
  2774.           fatal ("internal error: %s defined in mysterious way", sp->name);
  2775.  
  2776.         /* write and count it.  */
  2777.  
  2778.         write_atari_sym(&nl, sp->name);
  2779.         n_syms_written++;
  2780.       }
  2781.       }
  2782.   if (n_syms_written != nsyms)
  2783.     fprintf(stderr, "Bogon alert!  wrote %d syms, expected to write %d\n",
  2784.              n_syms_written, nsyms);
  2785. }
  2786.  
  2787. #else
  2788.  
  2789. void
  2790. write_syms ()
  2791. {
  2792.   /* Number of symbols written so far.  */
  2793.   int syms_written = 0;
  2794.   register int i;
  2795.   register symbol *sp;
  2796.  
  2797.   /* Buffer big enough for all the global symbols.  */
  2798.   struct nlist *buf
  2799.     = (struct nlist *) alloca ((defined_global_sym_count + undefined_global_sym_count)
  2800.                    * sizeof (struct nlist));
  2801.   /* Pointer for storing into BUF.  */
  2802.   register struct nlist *bufp = buf;
  2803.  
  2804.   /* Size of string table includes the bytes that store the size.  */
  2805.   strtab_size = sizeof strtab_size;
  2806.  
  2807.   symbol_table_offset = N_SYMOFF (outheader);
  2808.   symbol_table_len = 0;
  2809.   string_table_offset = N_STROFF (outheader);
  2810.   string_table_len = strtab_size;
  2811.  
  2812.   if (strip_symbols == STRIP_ALL)
  2813.     return;
  2814.  
  2815.   /* Write the local symbols defined by the various files.  */
  2816.  
  2817.   each_file (write_file_syms, &syms_written);
  2818.   file_close ();
  2819.  
  2820.   /* Now write out the global symbols.  */
  2821.  
  2822.   /* Allocate two vectors that record the data to generate the string
  2823.      table from the global symbols written so far.  */
  2824.  
  2825.   strtab_vector = (char **) alloca (num_hash_tab_syms * sizeof (char *));
  2826.   strtab_lens = (int *) alloca (num_hash_tab_syms * sizeof (int));
  2827.   strtab_index = 0;
  2828.  
  2829.   /* Scan the symbol hash table, bucket by bucket.  */
  2830.  
  2831.   for (i = 0; i < TABSIZE; i++)
  2832.     for (sp = symtab[i]; sp; sp = sp->link)
  2833.       {
  2834.     struct nlist nl;
  2835.  
  2836.     nl.n_other = 0;
  2837.     nl.n_desc = 0;
  2838.  
  2839.     /* Compute a `struct nlist' for the symbol.  */
  2840.  
  2841.     if (sp->defined || sp->referenced)
  2842.       {
  2843.         if (!sp->defined)          /* undefined -- legit only if -r */
  2844.           {
  2845.         nl.n_type = N_UNDF | N_EXT;
  2846.         nl.n_value = 0;
  2847.           }
  2848.         else if (sp->defined > 1) /* defined with known type */
  2849.           {
  2850.         nl.n_type = sp->defined;
  2851.         nl.n_value = sp->value;
  2852.           }
  2853.         else if (sp->max_common_size) /* defined as common but not allocated. */
  2854.           {
  2855.         /* happens only with -r and not -d */
  2856.         /* write out a common definition */
  2857.         nl.n_type = N_UNDF | N_EXT;
  2858.         nl.n_value = sp->max_common_size;
  2859.           }
  2860.         else
  2861.           fatal ("internal error: %s defined in mysterious way", sp->name);
  2862.  
  2863.         /* Allocate string table space for the symbol name.  */
  2864.  
  2865.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  2866.  
  2867.         /* Output to the buffer and count it.  */
  2868.  
  2869.         *bufp++ = nl;
  2870.         syms_written++;
  2871.       }
  2872.       }
  2873.  
  2874.   /* Output the buffer full of `struct nlist's.  */
  2875.  
  2876.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  2877.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  2878.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  2879.  
  2880.   if (syms_written != nsyms)
  2881.     fatal ("internal error: wrong number of symbols written into output file", 0);
  2882.  
  2883.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  2884.     fatal ("internal error: inconsistent symbol table length", 0);
  2885.  
  2886.   /* Now the total string table size is known, so write it.
  2887.      We are already positioned at the right place in the file.  */
  2888.  
  2889.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  2890.  
  2891.   /* Write the strings for the global symbols.  */
  2892.  
  2893.   write_string_table ();
  2894. }
  2895. #endif            /* not atari st */
  2896.  
  2897.  
  2898. /* Write the local and debugger symbols of file ENTRY.
  2899.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  2900.  
  2901. /* Note that we do not combine identical names of local symbols.
  2902.    dbx or gdb would be confused if we did that.  */
  2903.  
  2904. void
  2905. write_file_syms (entry, syms_written_addr)
  2906.      struct file_entry *entry;
  2907.      int *syms_written_addr;
  2908. {
  2909.   register struct nlist *p = entry->symbols;
  2910.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  2911.  
  2912.   /* Buffer to accumulate all the syms before writing them.
  2913.      It has one extra slot for the local symbol we generate here.  */
  2914.   struct nlist *buf
  2915.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  2916.   register struct nlist *bufp = buf;
  2917.  
  2918.   /* Upper bound on number of syms to be written here.  */
  2919.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  2920.  
  2921.   /* Make tables that record, for each symbol, its name and its name's length.
  2922.      The elements are filled in by `assign_string_table_index'.  */
  2923.  
  2924.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  2925.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  2926.   strtab_index = 0;
  2927.  
  2928.   /* Generate a local symbol for the start of this file's text.  */
  2929.  
  2930.   if (discard_locals != DISCARD_ALL)
  2931.     {
  2932.       struct nlist nl;
  2933.  
  2934.       nl.n_type = N_TEXT;
  2935.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  2936.       nl.n_value = entry->text_start_address;
  2937.       nl.n_desc = 0;
  2938.       nl.n_other = 0;
  2939.       *bufp++ = nl;
  2940.       (*syms_written_addr)++;
  2941.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  2942.     }
  2943.  
  2944.   /* Read the file's string table.  */
  2945.  
  2946.   entry->strings = (char *) alloca (entry->string_size);
  2947.   read_entry_strings (file_open (entry), entry);
  2948.  
  2949.   for (; p < end; p++)
  2950.     {
  2951.       register int type = p->n_type;
  2952.       register int write = 0;
  2953.  
  2954.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  2955.  
  2956.       if (!(type & (N_STAB | N_EXT)))
  2957.         /* ordinary local symbol */
  2958.     write = (discard_locals != DISCARD_ALL)
  2959.         && !(discard_locals == DISCARD_L &&
  2960.              (p->n_un.n_strx + entry->strings)[0] == 'L');
  2961.       else if (!(type & N_EXT))
  2962.     /* debugger symbol */
  2963.         write = (strip_symbols == STRIP_NONE);
  2964.  
  2965.       if (write)
  2966.     {
  2967.       /* If this symbol has a name,
  2968.          allocate space for it in the output string table.  */
  2969.  
  2970.       if (p->n_un.n_strx)
  2971.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx +
  2972.                             entry->strings);
  2973.  
  2974.       /* Output this symbol to the buffer and count it.  */
  2975.  
  2976.       *bufp++ = *p;
  2977.       (*syms_written_addr)++;
  2978.     }
  2979.     }
  2980.  
  2981.   /* All the symbols are now in BUF; write them.  */
  2982.  
  2983.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0); 
  2984.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  2985.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  2986.  
  2987.   /* Write the string-table data for the symbols just written,
  2988.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  2989.  
  2990.   write_string_table ();
  2991. }
  2992.  
  2993. /* Copy any GDB symbol segments from the input files to the output file.
  2994.    The contents of the symbol segment is copied without change
  2995.    except that we store some information into the beginning of it.  */
  2996.  
  2997. void write_file_symseg ();
  2998.  
  2999. write_symsegs ()
  3000. {
  3001.   each_file (write_file_symseg, 0);
  3002. }
  3003.  
  3004. void
  3005. write_file_symseg (entry)
  3006.      struct file_entry *entry;
  3007. {
  3008. #ifndef atarist        /* this appears to be completely wrong for ST */
  3009.   char buffer[4096];
  3010.   struct symbol_root root;
  3011.   int indesc;
  3012.   int len;
  3013.  
  3014.   if (entry->symseg_offset == 0)
  3015.     return;
  3016.  
  3017.   /* This entry has a symbol segment.  Read the root of the segment.  */
  3018.  
  3019.   indesc = file_open (entry);
  3020.   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  3021.   if (sizeof root != read (indesc, &root, sizeof root))
  3022.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  3023.  
  3024.   /* Store some relocation info into the root.  */
  3025.  
  3026.   root.ldsymoff = entry->local_syms_offset;
  3027.   root.textrel = entry->text_start_address;
  3028.   root.datarel = entry->data_start_address - entry->header.a_text;
  3029.   root.bssrel = entry->bss_start_address
  3030.     - entry->header.a_text - entry->header.a_data;
  3031.  
  3032.   /* Write the modified root into the output file.  */
  3033.  
  3034.   mywrite (&root, sizeof root, 1, outdesc);
  3035.  
  3036.   /* Copy the rest of the symbol segment unchanged.  */
  3037.  
  3038.   if (entry->superfile)
  3039.     {
  3040.       /* Library member: number of bytes to copy is determined
  3041.      from the member's total size.  */
  3042.  
  3043.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  3044.  
  3045.       while (total > 0)
  3046.     {
  3047.       len = read (indesc, buffer, min (sizeof buffer, total));
  3048.  
  3049.       if (len != min (sizeof buffer, total))
  3050.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  3051.       total -= len;
  3052.       mywrite (buffer, len, 1, outdesc);
  3053.     }
  3054.     }
  3055.   else
  3056.     {
  3057.       /* A separate file: copy until end of file.  */
  3058.  
  3059.       while (len = read (indesc, buffer, sizeof buffer))
  3060.     {
  3061.       mywrite (buffer, len, 1, outdesc);
  3062.       if (len < sizeof buffer)
  3063.         break;
  3064.     }
  3065.     }
  3066.  
  3067.   file_close ();
  3068. #endif
  3069. }
  3070.  
  3071. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  3072.  
  3073. symtab_init ()
  3074. {
  3075. #ifndef nounderscore
  3076.   edata_symbol = getsym ("_edata");
  3077.   etext_symbol = getsym ("_etext");
  3078.   end_symbol = getsym ("_end");
  3079. #else
  3080.   edata_symbol = getsym ("edata");
  3081.   etext_symbol = getsym ("etext");
  3082.   end_symbol = getsym ("end");
  3083. #endif
  3084.  
  3085.   edata_symbol->defined = N_DATA | N_EXT;
  3086.   etext_symbol->defined = N_TEXT | N_EXT;
  3087.   end_symbol->defined = N_BSS | N_EXT;
  3088.  
  3089.   edata_symbol->referenced = 1;
  3090.   etext_symbol->referenced = 1;
  3091.   end_symbol->referenced = 1;
  3092. }
  3093.  
  3094. /* Compute the hash code for symbol name KEY.  */
  3095.  
  3096. int
  3097. hash_string (key)
  3098.      char *key;
  3099. {
  3100.   register char *cp;
  3101.   register int k;
  3102.  
  3103.   cp = key;
  3104.   k = 0;
  3105.   while (*cp)
  3106.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  3107.  
  3108.   return k;
  3109. }
  3110.  
  3111. /* Get the symbol table entry for the global symbol named KEY.
  3112.    Create one if there is none.  */
  3113.  
  3114. symbol *
  3115. getsym (key)
  3116.      char *key;
  3117. {
  3118.   register int hashval;
  3119.   register symbol *bp;
  3120.  
  3121.   /* Determine the proper bucket.  */
  3122.  
  3123.   hashval = hash_string (key) % TABSIZE;
  3124.  
  3125.   /* Search the bucket.  */
  3126.  
  3127.   for (bp = symtab[hashval]; bp; bp = bp->link)
  3128.     if (! strcmp (key, bp->name))
  3129.       return bp;
  3130.  
  3131.   /* Nothing was found; create a new symbol table entry.  */
  3132.  
  3133.   bp = (symbol *) xmalloc (sizeof (symbol));
  3134.   bp->refs = 0;
  3135.   bp->name = (char *) xmalloc (strlen (key) + 1);
  3136.   strcpy (bp->name, key);
  3137.   bp->defined = 0;
  3138.   bp->referenced = 0;
  3139.   bp->trace = 0;
  3140.   bp->value = 0;
  3141.   bp->max_common_size = 0;
  3142.  
  3143.   /* Add the entry to the bucket.  */
  3144.  
  3145.   bp->link = symtab[hashval];
  3146.   symtab[hashval] = bp;
  3147.  
  3148.   ++num_hash_tab_syms;
  3149.  
  3150.   return bp;
  3151. }
  3152.  
  3153. /* Like `getsym' but return 0 if the symbol is not already known.  */
  3154.  
  3155. symbol *
  3156. getsym_soft (key)
  3157.      char *key;
  3158. {
  3159.   register int hashval;
  3160.   register symbol *bp;
  3161.  
  3162.   /* Determine which bucket.  */
  3163.  
  3164.   hashval = hash_string (key) % TABSIZE;
  3165.  
  3166.   /* Search the bucket.  */
  3167.  
  3168.   for (bp = symtab[hashval]; bp; bp = bp->link)
  3169.     if (! strcmp (key, bp->name))
  3170.       return bp;
  3171.  
  3172.   return 0;
  3173. }
  3174.  
  3175. /* Report a fatal error.
  3176.    STRING is a printf format string and ARG is one arg for it.  */
  3177.  
  3178. fatal (string, arg)
  3179.      char *string, *arg;
  3180. {
  3181.   fprintf (stderr, "ld: ");
  3182.   fprintf (stderr, string, arg);
  3183.   fprintf (stderr, "\n");
  3184.   exit (1);
  3185. }
  3186.  
  3187. /* Report a fatal error.  The error message is STRING
  3188.    followed by the filename of ENTRY.  */
  3189.  
  3190. fatal_with_file (string, entry)
  3191.      char *string;
  3192.      struct file_entry *entry;
  3193. {
  3194.   fprintf (stderr, "ld: ");
  3195.   fprintf (stderr, string);
  3196.   print_file_name (entry, stderr);
  3197.   fprintf (stderr, "\n");
  3198.   exit (1);
  3199. }
  3200.  
  3201. /* Report a fatal error using the message for the last failed system call,
  3202.    followed by the string NAME.  */
  3203.  
  3204. perror_name (name)
  3205.      char *name;
  3206. {
  3207.   extern int errno, sys_nerr;
  3208.   extern char *sys_errlist[];
  3209.   char *s;
  3210.  
  3211. #ifdef atarist
  3212.   if ((errno > sys_nerr) && (errno < 0))
  3213. #else
  3214.   if (errno < sys_nerr)
  3215. #endif
  3216.     s = concat ("", sys_errlist[errno], " for %s");
  3217.   else
  3218.     s = "cannot open %s";
  3219.   fatal (s, name);
  3220. }
  3221.  
  3222. /* Report a fatal error using the message for the last failed system call,
  3223.    followed by the name of file ENTRY.  */
  3224.  
  3225. perror_file (entry)
  3226.      struct file_entry *entry;
  3227. {
  3228.   extern int errno, sys_nerr;
  3229.   extern char *sys_errlist[];
  3230.   char *s;
  3231.  
  3232. #ifdef atarist
  3233.   if ((errno > sys_nerr) && (errno < 0))
  3234. #else
  3235.   if (errno < sys_nerr)
  3236. #endif
  3237.     s = concat ("", sys_errlist[errno], " for ");
  3238.   else
  3239.     s = "cannot open ";
  3240.   fatal_with_file (s, entry);
  3241. }
  3242.  
  3243. /* Report a nonfatal error.
  3244.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  3245.  
  3246. error (string, arg1, arg2, arg3)
  3247.      char *string, *arg1, *arg2, *arg3;
  3248. {
  3249.   fprintf (stderr, string, arg1, arg2, arg3);
  3250.   fprintf (stderr, "\n");
  3251. }
  3252.  
  3253. /* Output COUNT*ELTSIZE bytes of data at BUF
  3254.    to the descriptor DESC.  */
  3255.  
  3256. mywrite (buf, count, eltsize, desc)
  3257.      char *buf;
  3258.      int count;
  3259.      int eltsize;
  3260.      int desc;
  3261. {
  3262.   register int val;
  3263.   register int bytes = count * eltsize;
  3264.  
  3265.   while (bytes > 0)
  3266.     {
  3267.       val = write (desc, buf, bytes);
  3268.       if (val <= 0)
  3269.     perror_name (output_filename);
  3270.       buf += val;
  3271.       bytes -= val;
  3272.     }
  3273. }
  3274.  
  3275. /* Output PADDING zero-bytes to descriptor OUTDESC.
  3276.    PADDING may be negative; in that case, do nothing.  */
  3277.  
  3278. void
  3279. padfile (padding, outdesc)
  3280.      int padding;
  3281.      int outdesc;
  3282. {
  3283. #ifndef atarist        /* not round here, you don't... */
  3284.   register char *buf;
  3285.   if (padding <= 0)
  3286.     return;
  3287.  
  3288.   buf = (char *) alloca (padding);
  3289.   bzero (buf, padding);
  3290.   mywrite (buf, padding, 1, outdesc);
  3291. #endif
  3292. }
  3293.  
  3294. /* Return a newly-allocated string
  3295.    whose contents concatenate the strings S1, S2, S3.  */
  3296.  
  3297. char *
  3298. concat (s1, s2, s3)
  3299.      char *s1, *s2, *s3;
  3300. {
  3301.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  3302.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  3303.  
  3304.   strcpy (result, s1);
  3305.   strcpy (result + len1, s2);
  3306.   strcpy (result + len1 + len2, s3);
  3307.   result[len1 + len2 + len3] = 0;
  3308.  
  3309.   return result;
  3310. }
  3311.  
  3312. /* Parse the string ARG using scanf format FORMAT, and return the result.
  3313.    If it does not parse, report fatal error
  3314.    generating the error message using format string ERROR and ARG as arg.  */
  3315.  
  3316. int
  3317. parse (arg, format, error)
  3318.      char *arg, *format;
  3319. {
  3320.   int x;
  3321.   if (1 != sscanf (arg, format, &x))
  3322.     fatal (error, arg);
  3323.   return x;
  3324. }
  3325.  
  3326. /* Like malloc but get fatal error if memory is exhausted.  */
  3327.  
  3328. int
  3329. xmalloc (size)
  3330.      int size;
  3331. {
  3332.   register int result = malloc (size);
  3333.   if (!result)
  3334.     fatal ("virtual memory exhausted", 0);
  3335.   return result;
  3336. }
  3337.  
  3338. /* Like realloc but get fatal error if memory is exhausted.  */
  3339.  
  3340. int
  3341. xrealloc (ptr, size)
  3342.      char *ptr;
  3343.      int size;
  3344. {
  3345.   register int result = realloc (ptr, size);
  3346.   if (!result)
  3347.     fatal ("virtual memory exhausted", 0);
  3348.   return result;
  3349. }
  3350.