home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / gccmsdos.arj / GNU / BINUTY / LD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-01  |  130.7 KB  |  4,705 lines

  1. /* Linker `ld' for GNU
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman with some help from Eric Albert.
  19.    Set, indirect, and warning symbol features added by Randy Smith.  */
  20.  
  21. #include <ar.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/file.h>
  26. #ifndef sony_news
  27. #include <fcntl.h>
  28. #endif
  29.  
  30. #ifdef COFF_ENCAPSULATE
  31. #include "a.out.encap.h"
  32. #else
  33. #ifdef TOWNS
  34. #include <a_out.h>
  35. #else
  36. #include <a.out.h>
  37. #endif /* TOWNS */
  38. #endif
  39.  
  40. #ifndef N_SET_MAGIC
  41. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  42. #endif
  43.  
  44. /* If compiled with GNU C, use the built-in alloca */
  45. #ifdef __GNUC__
  46. #define alloca __builtin_alloca
  47. #endif
  48.  
  49. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  50.  
  51. #include "stab.h"
  52. #define CORE_ADDR unsigned long    /* For symseg.h */
  53. #include "symseg.h"
  54.  
  55. #ifdef USG
  56. #include <string.h>
  57. #else
  58. #include <strings.h>
  59. #endif
  60.  
  61. /* Determine whether we should attempt to handle (minimally)
  62.    N_BINCL and N_EINCL.  */
  63.  
  64. #if defined (__GNU_STAB__) || defined (N_BINCL)
  65. #define HAVE_SUN_STABS
  66. #endif
  67.  
  68. #define min(a,b) ((a) < (b) ? (a) : (b))
  69.  
  70. /* Macro to control the number of undefined references printed */
  71. #define MAX_UREFS_PRINTED    10
  72.  
  73. /* Size of a page; obtained from the operating system.  */
  74.  
  75. int page_size;
  76.  
  77. /* Name this program was invoked by.  */
  78.  
  79. char *progname;
  80.  
  81. /* System dependencies */
  82.  
  83. /* Define this if names etext, edata and end should not start with `_'.  */
  84. /* #define nounderscore 1 */
  85.  
  86. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  87.    whose native format is different.  */
  88. /* #define NON_NATIVE */
  89.  
  90. /* Define this to specify the default executable format.  */
  91.  
  92. #ifdef hpux
  93. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  94. #endif
  95.  
  96. #ifndef DEFAULT_MAGIC
  97. #define DEFAULT_MAGIC ZMAGIC
  98. #endif
  99.  
  100. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  101.  
  102. #ifndef N_TXTADDR
  103. #if defined(vax) || defined(sony_news)
  104. #define N_TXTADDR(X) 0
  105. #endif
  106. #ifdef is68k
  107. #define N_TXTADDR(x)  (sizeof (struct exec))
  108. #endif
  109. #ifdef sequent
  110. #define    N_TXTADDR(x) (N_ADDRADJ(x))
  111. #endif
  112. #endif
  113.  
  114. #ifndef N_DATADDR
  115. #if defined(vax) || defined(sony_news)
  116. #define N_DATADDR(x) \
  117.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  118.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  119. #endif
  120. #ifdef is68k
  121. #define SEGMENT_SIZE 0x20000
  122. #define N_DATADDR(x) \
  123.     (((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
  124.      : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  125. #endif
  126. #ifdef sequent
  127. #define N_DATADDR(x) \
  128.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  129.     : (page_size+(((x).a_text-1) & ~(page_size-1))))
  130. #endif
  131. #endif
  132.  
  133. /* Define how to initialize system-dependent header fields.  */
  134. #ifdef sun
  135. #ifdef sparc
  136. #define INITIALIZE_HEADER \
  137.   {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  138. #endif
  139. #if defined(mc68010) || defined(m68010)
  140. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  141. #endif
  142. #ifndef INITIALIZE_HEADER
  143. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  144. #endif
  145. #endif
  146. #ifdef ALTOS
  147. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  148. #endif
  149. #ifdef is68k
  150. #ifdef M_68020
  151. /* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
  152.    a_machtype field and so won't recognize the magic number.  To keep
  153.    binary compatibility for now, just ignore it */
  154. #define INITIALIZE_HEADER outheader.a_machtype = 0;
  155. #endif
  156. #endif
  157. #ifdef hpux
  158. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, HP9000S200_ID)
  159. #endif
  160. #if defined(i386) && !defined(sequent)
  161. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  162. #endif
  163.  
  164. #ifdef is68k
  165. /* This enables code to take care of an ugly hack in the ISI OS.
  166.    If a symbol beings with _$, then the object file is included only
  167.    if the rest of the symbol name has been referenced. */
  168. #define DOLLAR_KLUDGE
  169. #endif
  170.  
  171. /*
  172.  * Alloca include.
  173.  */
  174. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  175. #include "alloca.h"
  176. #endif
  177.  
  178. #ifndef L_SET
  179. #define L_SET 0
  180. #endif
  181.  
  182. /*
  183.  * Ok.  Following are the relocation information macros.  If your
  184.  * system cannot use the default set (below), you must define all of these:
  185.  
  186.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  187.  * of structure that is stored in the relocation info section of your
  188.  * a.out files.  Often this is defined in the a.out.h for your system.
  189.  *
  190.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  191.  * <whatever> to be relocated.  *Must be an lvalue*.
  192.  *
  193.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  194.  * external symbol (1), or was it fully resolved upon entering the
  195.  * loader (0) in which case some combination of the value in memory
  196.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  197.  * what the value of the relocation actually was.  *Must be an lvalue*.
  198.  *
  199.  *   RELOC_TYPE (rval): For a non-external relocation, this is the
  200.  * segment to relocate for.
  201.  *
  202.  *   RELOC_SYMBOL (rval): For an external relocation, this is the
  203.  * index of its symbol in the symbol table.  *Must be an lvalue*.
  204.  *
  205.  *   RELOC_MEMORY_ADD_P (rval): This should be 1 if the final
  206.  * relocation value output here should be added to memory; 0, if the
  207.  * section of memory described should simply be set to the relocation
  208.  * value.
  209.  *
  210.  *   RELOC_MEMORY_ADD_P (rval): If this is nonzero, the value previously
  211.  * present in the memory location to be relocated is *added*
  212.  * to the relocation value, to produce the final result.
  213.  * Otherwise, the relocation value is stored in the memory location
  214.  * and the value previously found there is ignored.
  215.  * By default, this is always 1.
  216.  *
  217.  *   RELOC_MEMORY_SUB_P (rval): If this is nonzero, the value previously
  218.  * present in the memory location to be relocated is *subtracted*
  219.  * from the relocation value, to produce the final result.
  220.  * By default, this is always 0.
  221.  *
  222.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  223.  * an extra value to be added to the relocation value based on the
  224.  * individual relocation entry.  *Must be an lvalue if defined*.
  225.  *
  226.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  227.  * pc relative.
  228.  *
  229.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  230.  * final relocation value before putting it where it belongs.
  231.  *
  232.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  233.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  234.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  235.  * do everything in terms of the bit operators below), but having this
  236.  * macro could end up producing better code on machines without fancy
  237.  * bit twiddling.  Also, it's easier to understand/code big/little
  238.  * endian distinctions with this macro.
  239.  *
  240.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  241.  * object described in RELOC_TARGET_SIZE in which the relocation value
  242.  * will go.
  243.  *
  244.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  245.  * with the bits of the relocation value.  It may be assumed by the
  246.  * code that the relocation value will fit into this many bits.  This
  247.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  248.  *
  249.  *
  250.  *        Things I haven't implemented
  251.  *        ----------------------------
  252.  *
  253.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  254.  *
  255.  *    Pc relative relocation for External references.
  256.  *
  257.  *
  258.  */
  259.  
  260. #if defined(sun) && defined(sparc)
  261. /* Sparc (Sun 4) macros */
  262. #undef relocation_info
  263. #define relocation_info                    reloc_info_sparc
  264. #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  265. #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  266. #define RELOC_TYPE(r)                   ((r)->r_index)  
  267. #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  268. #define RELOC_MEMORY_SUB_P(r)        0
  269. #define RELOC_MEMORY_ADD_P(r)           0
  270. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  271. #define RELOC_PCREL_P(r)             \
  272.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  273. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  274. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  275. #define RELOC_TARGET_BITPOS(r)          0
  276. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  277.  
  278. /* Note that these are very dependent on the order of the enums in
  279.    enum reloc_type (in a.out.h); if they change the following must be
  280.    changed */
  281. /* Also note that the last few may be incorrect; I have no information */
  282. static int reloc_target_rightshift[] = {
  283.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  284. };
  285. static int reloc_target_size[] = {
  286.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  287. };
  288. static int reloc_target_bitsize[] = {
  289.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  290. };
  291.  
  292. #define    MAX_ALIGNMENT    (sizeof (double))
  293. #endif
  294.  
  295. #ifdef sequent
  296. #define RELOC_ADDRESS(r)        ((r)->r_address)
  297. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  298. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  299. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  300. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  301. #define RELOC_MEMORY_ADD_P(r)    1
  302. #undef RELOC_ADD_EXTRA
  303. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  304. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  305. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  306. #define RELOC_TARGET_BITPOS(r)    0
  307. #define RELOC_TARGET_BITSIZE(r)    32
  308. #endif
  309.  
  310. /* Default macros */
  311. #ifndef RELOC_ADDRESS
  312. #define RELOC_ADDRESS(r)        ((r)->r_address)
  313. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  314. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  315. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  316. #define RELOC_MEMORY_SUB_P(r)    0
  317. #define RELOC_MEMORY_ADD_P(r)    1
  318. #undef RELOC_ADD_EXTRA
  319. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  320. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  321. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  322. #define RELOC_TARGET_BITPOS(r)    0
  323. #define RELOC_TARGET_BITSIZE(r)    32
  324. #endif
  325.  
  326. #ifndef MAX_ALIGNMENT
  327. #define    MAX_ALIGNMENT    (sizeof (int))
  328. #endif
  329.  
  330. #ifdef nounderscore
  331. #define LPREFIX '.'
  332. #else
  333. #define LPREFIX 'L'
  334. #endif
  335.  
  336.  
  337. /* Special global symbol types understood by GNU LD.  */
  338.  
  339. /* The following type indicates the definition of a symbol as being
  340.    an indirect reference to another symbol.  The other symbol
  341.    appears as an undefined reference, immediately following this symbol.
  342.  
  343.    Indirection is asymmetrical.  The other symbol's value will be used
  344.    to satisfy requests for the indirect symbol, but not vice versa.
  345.    If the other symbol does not have a definition, libraries will
  346.    be searched to find a definition.
  347.  
  348.    So, for example, the following two lines placed in an assembler
  349.    input file would result in an object file which would direct gnu ld
  350.    to resolve all references to symbol "foo" as references to symbol
  351.    "bar". 
  352.  
  353.     .stabs "_foo",11,0,0,0
  354.     .stabs "_bar",1,0,0,0
  355.  
  356.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  357.  
  358. #ifndef N_INDR
  359. #define N_INDR 0xa
  360. #endif
  361.  
  362. /* The following symbols refer to set elements.  These are expected
  363.    only in input to the loader; they should not appear in loader
  364.    output (unless relocatable output is requested).  To be recognized
  365.    by the loader, the input symbols must have their N_EXT bit set.
  366.    All the N_SET[ATDB] symbols with the same name form one set.  The
  367.    loader collects all of these elements at load time and outputs a
  368.    vector for each name.
  369.    Space (an array of 32 bit words) is allocated for the set in the
  370.    data section, and the n_value field of each set element value is
  371.    stored into one word of the array.
  372.    The first word of the array is the length of the set (number of
  373.    elements).  The last word of the vector is set to zero for possible
  374.    use by incremental loaders.  The array is ordered by the linkage
  375.    order; the first symbols which the linker encounters will be first
  376.    in the array.
  377.  
  378.    In C syntax this looks like:
  379.  
  380.     struct set_vector {
  381.       unsigned int length;
  382.       unsigned int vector[length];
  383.       unsigned int always_zero;
  384.     };
  385.  
  386.    Before being placed into the array, each element is relocated
  387.    according to its type.  This allows the loader to create an array
  388.    of pointers to objects automatically.  N_SETA type symbols will not
  389.    be relocated.
  390.  
  391.    The address of the set is made into an N_SETV symbol
  392.    whose name is the same as the name of the set.
  393.    This symbol acts like a N_DATA global symbol
  394.    in that it can satisfy undefined external references.
  395.  
  396.    For the purposes of determining whether or not to load in a library
  397.    file, set element definitions are not considered "real
  398.    definitions"; they will not cause the loading of a library
  399.    member.
  400.  
  401.    If relocatable output is requested, none of this processing is
  402.    done.  The symbols are simply relocated and passed through to the
  403.    output file.
  404.  
  405.    So, for example, the following three lines of assembler code
  406.    (whether in one file or scattered between several different ones)
  407.    will produce a three element vector (total length is five words;
  408.    see above), referenced by the symbol "_xyzzy", which will have the
  409.    addresses of the routines _init1, _init2, and _init3.
  410.  
  411.    *NOTE*: If symbolic addresses are used in the n_value field of the
  412.    defining .stabs, those symbols must be defined in the same file as
  413.    that containing the .stabs.
  414.  
  415.     .stabs "_xyzzy",23,0,0,_init1
  416.     .stabs "_xyzzy",23,0,0,_init2
  417.     .stabs "_xyzzy",23,0,0,_init3
  418.  
  419.    Note that (23 == (N_SETT | N_EXT)).  */
  420.  
  421. #ifndef N_SETA
  422. #define    N_SETA    0x14        /* Absolute set element symbol */
  423. #endif                /* This is input to LD, in a .o file.  */
  424.  
  425. #ifndef N_SETT
  426. #define    N_SETT    0x16        /* Text set element symbol */
  427. #endif                /* This is input to LD, in a .o file.  */
  428.  
  429. #ifndef N_SETD
  430. #define    N_SETD    0x18        /* Data set element symbol */
  431. #endif                /* This is input to LD, in a .o file.  */
  432.  
  433. #ifndef N_SETB
  434. #define    N_SETB    0x1A        /* Bss set element symbol */
  435. #endif                /* This is input to LD, in a .o file.  */
  436.  
  437. /* Macros dealing with the set element symbols defined in a.out.h */
  438. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  439. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  440.  
  441. #ifndef N_SETV
  442. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  443. #endif                /* This is output from LD.  */
  444.  
  445. /* If a this type of symbol is encountered, its name is a warning
  446.    message to print each time the symbol referenced by the next symbol
  447.    table entry is referenced.   
  448.  
  449.    This feature may be used to allow backwards compatibility with
  450.    certain functions (eg. gets) but to discourage programmers from
  451.    their use.
  452.  
  453.    So if, for example, you wanted to have ld print a warning whenever
  454.    the function "gets" was used in their C program, you would add the
  455.    following to the assembler file in which gets is defined:
  456.  
  457.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  458.     .stabs "_gets",1,0,0,0
  459.  
  460.    These .stabs do not necessarily have to be in the same file as the
  461.    gets function, they simply must exist somewhere in the compilation.  */
  462.  
  463. #ifndef N_WARNING
  464. #define N_WARNING 0x1E        /* Warning message to print if symbol
  465.                    included */
  466. #endif                /* This is input to ld */
  467.  
  468. #ifndef __GNU_STAB__
  469.  
  470. /* Line number for the data section.  This is to be used to describe
  471.    the source location of a variable declaration.  */
  472. #ifndef N_DSLINE
  473. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  474. #endif
  475.  
  476. /* Line number for the bss section.  This is to be used to describe
  477.    the source location of a variable declaration.  */
  478. #ifndef N_BSLINE
  479. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  480. #endif
  481.  
  482. #endif /* not __GNU_STAB__ */
  483.  
  484. /* Symbol table */
  485.  
  486. /* Global symbol data is recorded in these structures,
  487.    one for each global symbol.
  488.    They are found via hashing in 'symtab', which points to a vector of buckets.
  489.    Each bucket is a chain of these structures through the link field.  */
  490.  
  491. typedef
  492.   struct glosym
  493.     {
  494.       /* Pointer to next symbol in this symbol's hash bucket.  */
  495.       struct glosym *link;
  496.       /* Name of this symbol.  */
  497.       char *name;
  498.       /* Value of this symbol as a global symbol.  */
  499.       long value;
  500.       /* Chain of external 'nlist's in files for this symbol, both defs
  501.      and refs.  */
  502.       struct nlist *refs;
  503.       /* Any warning message that might be associated with this symbol
  504.          from an N_WARNING symbol encountered. */
  505.       char *warning;
  506.       /* Nonzero means definitions of this symbol as common have been seen,
  507.      and the value here is the largest size specified by any of them.  */
  508.       int max_common_size;
  509.       /* For relocatable_output, records the index of this global sym in the
  510.      symbol table to be written, with the first global sym given index 0.*/
  511.       int def_count;
  512.       /* Nonzero means a definition of this global symbol is known to exist.
  513.      Library members should not be loaded on its account.  */
  514.       char defined;
  515.       /* Nonzero means a reference to this global symbol has been seen
  516.      in a file that is surely being loaded.
  517.      A value higher than 1 is the n_type code for the symbol's
  518.      definition.  */
  519.       char referenced;
  520.       /* A count of the number of undefined references printed for a
  521.      specific symbol.  If a symbol is unresolved at the end of
  522.      digest_symbols (and the loading run is supposed to produce
  523.      relocatable output) do_file_warnings keeps track of how many
  524.      unresolved reference error messages have been printed for
  525.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  526.      messages stop. */
  527.       unsigned char undef_refs;
  528.       /* 1 means that this symbol has multiple definitions.  2 means
  529.          that it has multiple definitions, and some of them are set
  530.      elements, one of which has been printed out already.  */
  531.       unsigned char multiply_defined;
  532.       /* Nonzero means print a message at all refs or defs of this symbol */
  533.       char trace;
  534.     }
  535.   symbol;
  536.  
  537. /* Number of buckets in symbol hash table */
  538. #define    TABSIZE    1009
  539.  
  540. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  541. symbol *symtab[TABSIZE];
  542.  
  543. /* Number of symbols in symbol hash table. */
  544. int num_hash_tab_syms = 0;
  545.  
  546. /* Count the number of nlist entries that are for local symbols.
  547.    This count and the three following counts
  548.    are incremented as as symbols are entered in the symbol table.  */
  549. int local_sym_count;
  550.  
  551. /* Count number of nlist entries that are for local symbols
  552.    whose names don't start with L. */
  553. int non_L_local_sym_count;
  554.  
  555. /* Count the number of nlist entries for debugger info.  */
  556. int debugger_sym_count;
  557.  
  558. /* Count the number of global symbols referenced and not defined.  */
  559. int undefined_global_sym_count;
  560.  
  561. /* Count the number of global symbols multiply defined.  */
  562. int multiple_def_count;
  563.  
  564. /* Count the number of defined global symbols.
  565.    Each symbol is counted only once
  566.    regardless of how many different nlist entries refer to it,
  567.    since the output file will need only one nlist entry for it.
  568.    This count is computed by `digest_symbols';
  569.    it is undefined while symbols are being loaded. */
  570. int defined_global_sym_count;
  571.  
  572. /* Count the number of symbols defined through common declarations.
  573.    This count is kept in symdef_library, linear_library, and
  574.    enter_global_ref.  It is incremented when the defined flag is set
  575.    in a symbol because of a common definition, and decremented when
  576.    the symbol is defined "for real" (ie. by something besides a common
  577.    definition).  */
  578. int common_defined_global_count;
  579.  
  580. /* Count the number of set element type symbols and the number of
  581.    separate vectors which these symbols will fit into.  See the
  582.    GNU a.out.h for more info.
  583.    This count is computed by 'enter_file_symbols' */
  584. int set_symbol_count;
  585. int set_vector_count;
  586.  
  587. /* Define a linked list of strings which define symbols which should
  588.    be treated as set elements even though they aren't.  Any symbol
  589.    with a prefix matching one of these should be treated as a set
  590.    element.
  591.  
  592.    This is to make up for deficiencies in many assemblers which aren't
  593.    willing to pass any stabs through to the loader which they don't
  594.    understand.  */
  595. struct string_list_element {
  596.   char *str;
  597.   struct string_list_element *next;
  598. };
  599.  
  600. struct string_list_element *set_element_prefixes;
  601.  
  602. /* Count the number of definitions done indirectly (ie. done relative
  603.    to the value of some other symbol. */
  604. int global_indirect_count;
  605.  
  606. /* Count the number of warning symbols encountered. */
  607. int warning_count;
  608.  
  609. /* Total number of symbols to be written in the output file.
  610.    Computed by digest_symbols from the variables above.  */
  611. int nsyms;
  612.  
  613.  
  614. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  615.    -e sets this.  */
  616. symbol *entry_symbol;
  617.  
  618. symbol *edata_symbol;   /* the symbol _edata */
  619. symbol *etext_symbol;   /* the symbol _etext */
  620. symbol *end_symbol;    /* the symbol _end */
  621.  
  622. /* Each input file, and each library member ("subfile") being loaded,
  623.    has a `file_entry' structure for it.
  624.  
  625.    For files specified by command args, these are contained in the vector
  626.    which `file_table' points to.
  627.  
  628.    For library members, they are dynamically allocated,
  629.    and chained through the `chain' field.
  630.    The chain is found in the `subfiles' field of the `file_entry'.
  631.    The `file_entry' objects for the members have `superfile' fields pointing
  632.    to the one for the library.  */
  633.  
  634. struct file_entry {
  635.   /* Name of this file.  */
  636.   char *filename;
  637.   /* Name to use for the symbol giving address of text start */
  638.   /* Usually the same as filename, but for a file spec'd with -l
  639.      this is the -l switch itself rather than the filename.  */
  640.   char *local_sym_name;
  641.  
  642.   /* Describe the layout of the contents of the file */
  643.  
  644.   /* The file's a.out header.  */
  645.   struct exec header;
  646.   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  647.   int symseg_offset;
  648.  
  649.   /* Describe data from the file loaded into core */
  650.  
  651.   /* Symbol table of the file.  */
  652.   struct nlist *symbols;
  653.   /* Size in bytes of string table.  */
  654.   int string_size;
  655.   /* Pointer to the string table.
  656.      The string table is not kept in core all the time,
  657.      but when it is in core, its address is here.  */
  658.   char *strings;
  659.  
  660.   /* Next two used only if `relocatable_output' or if needed for */
  661.   /* output of undefined reference line numbers. */
  662.  
  663.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  664.   struct relocation_info *textrel;
  665.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  666.   struct relocation_info *datarel;
  667.  
  668.   /* Relation of this file's segments to the output file */
  669.  
  670.   /* Start of this file's text seg in the output file core image.  */
  671.   int text_start_address;
  672.   /* Start of this file's data seg in the output file core image.  */
  673.   int data_start_address;
  674.   /* Start of this file's bss seg in the output file core image.  */
  675.   int bss_start_address;
  676.   /* Offset in bytes in the output file symbol table
  677.      of the first local symbol for this file.  Set by `write_file_symbols'.  */
  678.   int local_syms_offset;
  679.  
  680.   /* For library members only */
  681.  
  682.   /* For a library, points to chain of entries for the library members.  */
  683.   struct file_entry *subfiles;
  684.   /* For a library member, offset of the member within the archive.
  685.      Zero for files that are not library members.  */
  686.   int starting_offset;
  687.   /* Size of contents of this file, if library member.  */
  688.   int total_size;
  689.   /* For library member, points to the library's own entry.  */
  690.   struct file_entry *superfile;
  691.   /* For library member, points to next entry for next member.  */
  692.   struct file_entry *chain;
  693.  
  694.   /* 1 if file is a library. */
  695.   char library_flag;
  696.  
  697.   /* 1 if file's header has been read into this structure.  */
  698.   char header_read_flag;
  699.  
  700.   /* 1 means search a set of directories for this file.  */
  701.   char search_dirs_flag;
  702.  
  703.   /* 1 means this is base file of incremental load.
  704.      Do not load this file's text or data.
  705.      Also default text_start to after this file's bss. */
  706.   char just_syms_flag;
  707. };
  708.  
  709. /* Vector of entries for input files specified by arguments.
  710.    These are all the input files except for members of specified libraries.  */
  711. struct file_entry *file_table;
  712.  
  713. /* Length of that vector.  */
  714. int number_of_files;
  715.  
  716. /* When loading the text and data, we can avoid doing a close
  717.    and another open between members of the same library.
  718.  
  719.    These two variables remember the file that is currently open.
  720.    Both are zero if no file is open.
  721.  
  722.    See `each_file' and `file_close'.  */
  723.  
  724. struct file_entry *input_file;
  725. int input_desc;
  726.  
  727. /* The name of the file to write; "a.out" by default.  */
  728.  
  729. char *output_filename;
  730.  
  731. /* Descriptor for writing that file with `mywrite'.  */
  732.  
  733. int outdesc;
  734.  
  735. /* Header for that file (filled in by `write_header').  */
  736.  
  737. struct exec outheader;
  738.  
  739. #ifdef COFF_ENCAPSULATE
  740. struct coffheader coffheader;
  741. int need_coff_header;
  742. #endif
  743.  
  744. /* The following are computed by `digest_symbols'.  */
  745.  
  746. int text_size;        /* total size of text of all input files.  */
  747. int data_size;        /* total size of data of all input files.  */
  748. int bss_size;        /* total size of bss of all input files.  */
  749. int text_reloc_size;    /* total size of text relocation of all input files.  */
  750. int data_reloc_size;    /* total size of data relocation of all input */
  751.             /* files.  */
  752.  
  753. /* Specifications of start and length of the area reserved at the end
  754.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  755. int set_sect_start;
  756. int set_sect_size;
  757.  
  758. /* Pointer for in core storage for the above vectors, before they are
  759.    written. */
  760. unsigned long *set_vectors;
  761.  
  762. /* Amount of cleared space to leave between the text and data segments.  */
  763.  
  764. int text_pad;
  765.  
  766. /* Amount of bss segment to include as part of the data segment.  */
  767.  
  768. int data_pad;
  769.  
  770. /* Format of __.SYMDEF:
  771.    First, a longword containing the size of the 'symdef' data that follows.
  772.    Second, zero or more 'symdef' structures.
  773.    Third, a longword containing the length of symbol name strings.
  774.    Fourth, zero or more symbol name strings (each followed by a null).  */
  775.  
  776. struct symdef {
  777.   int symbol_name_string_index;
  778.   int library_member_offset;
  779. };
  780.  
  781. /* Record most of the command options.  */
  782.  
  783. /* Address we assume the text section will be loaded at.
  784.    We relocate symbols and text and data for this, but we do not
  785.    write any padding in the output file for it.  */
  786. int text_start;
  787.  
  788. /* Offset of default entry-pc within the text section.  */
  789. int entry_offset;
  790.  
  791. /* Address we decide the data section will be loaded at.  */
  792. int data_start;
  793.  
  794. /* `text-start' address is normally this much plus a page boundary.
  795.    This is not a user option; it is fixed for each system.  */
  796. int text_start_alignment;
  797.  
  798. /* Nonzero if -T was specified in the command line.
  799.    This prevents text_start from being set later to default values.  */
  800. int T_flag_specified;
  801.  
  802. /* Nonzero if -Tdata was specified in the command line.
  803.    This prevents data_start from being set later to default values.  */
  804. int Tdata_flag_specified;
  805.  
  806. /* Size to pad data section up to.
  807.    We simply increase the size of the data section, padding with zeros,
  808.    and reduce the size of the bss section to match.  */
  809. int specified_data_size;
  810.  
  811. /* Magic number to use for the output file, set by switch.  */
  812. int magic;
  813.  
  814. /* Nonzero means print names of input files as processed.  */
  815. int trace_files;
  816.  
  817. /* Which symbols should be stripped (omitted from the output):
  818.    none, all, or debugger symbols.  */
  819. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  820.  
  821. /* Which local symbols should be omitted:
  822.    none, all, or those starting with L.
  823.    This is irrelevant if STRIP_NONE.  */
  824. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  825.  
  826. /* 1 => write load map.  */
  827. int write_map;
  828.  
  829. /* 1 => write relocation into output file so can re-input it later.  */
  830. int relocatable_output;
  831.  
  832. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  833. int force_common_definition;
  834.  
  835. /* Standard directories to search for files specified by -l.  */
  836. char *standard_search_dirs[] =
  837. #ifdef STANDARD_SEARCH_DIRS
  838.   {STANDARD_SEARCH_DIRS};
  839. #else
  840. #ifdef NON_NATIVE
  841.   {"/usr/local/lib/gnu"};
  842. #else
  843.   {"/lib", "/usr/lib", "/usr/local/lib"};
  844. #endif
  845. #endif
  846.  
  847. /* Actual vector of directories to search;
  848.    this contains those specified with -L plus the standard ones.  */
  849. char **search_dirs;
  850.  
  851. /* Length of the vector `search_dirs'.  */
  852. int n_search_dirs;
  853.  
  854. /* Non zero means to create the output executable. */
  855. /* Cleared by nonfatal errors.  */
  856. int make_executable;
  857.  
  858. /* Force the executable to be output, even if there are non-fatal
  859.    errors */
  860. int force_executable;
  861.  
  862. /* Keep a list of any symbols referenced from the command line (so
  863.    that error messages for these guys can be generated). This list is
  864.    zero terminated. */
  865. struct glosym **cmdline_references;
  866. int cl_refs_allocated;
  867.  
  868. void bcopy (), bzero ();
  869. int malloc (), realloc ();
  870. #ifndef alloca
  871. int alloca ();
  872. #endif
  873. int free ();
  874.  
  875. int xmalloc ();
  876. int xrealloc ();
  877. void fatal ();
  878. void fatal_with_file ();
  879. void perror_name ();
  880. void perror_file ();
  881. void error ();
  882.  
  883. void digest_symbols ();
  884. void print_symbols ();
  885. void load_symbols ();
  886. void decode_command ();
  887. void list_undefined_symbols ();
  888. void list_unresolved_references ();
  889. void write_output ();
  890. void write_header ();
  891. void write_text ();
  892. void read_file_relocation ();
  893. void write_data ();
  894. void write_rel ();
  895. void write_syms ();
  896. void write_symsegs ();
  897. void mywrite ();
  898. void symtab_init ();
  899. void padfile ();
  900. char *concat ();
  901. char *get_file_name ();
  902. symbol *getsym (), *getsym_soft ();
  903.  
  904. int
  905. main (argc, argv)
  906.      char **argv;
  907.      int argc;
  908. {
  909.   page_size = getpagesize ();
  910.   progname = argv[0];
  911.  
  912.   /* Clear the cumulative info on the output file.  */
  913.  
  914.   text_size = 0;
  915.   data_size = 0;
  916.   bss_size = 0;
  917.   text_reloc_size = 0;
  918.   data_reloc_size = 0;
  919.  
  920.   data_pad = 0;
  921.   text_pad = 0;
  922.  
  923.   /* Initialize the data about options.  */
  924.  
  925.   specified_data_size = 0;
  926.   strip_symbols = STRIP_NONE;
  927.   trace_files = 0;
  928.   discard_locals = DISCARD_NONE;
  929.   entry_symbol = 0;
  930.   write_map = 0;
  931.   relocatable_output = 0;
  932.   force_common_definition = 0;
  933.   T_flag_specified = 0;
  934.   Tdata_flag_specified = 0;
  935.   magic = DEFAULT_MAGIC;
  936.   make_executable = 1;
  937.   force_executable = 0;
  938.   set_element_prefixes = 0;
  939.  
  940.   /* Initialize the cumulative counts of symbols.  */
  941.  
  942.   local_sym_count = 0;
  943.   non_L_local_sym_count = 0;
  944.   debugger_sym_count = 0;
  945.   undefined_global_sym_count = 0;
  946.   set_symbol_count = 0;
  947.   set_vector_count = 0;
  948.   global_indirect_count = 0;
  949.   warning_count = 0;
  950.   multiple_def_count = 0;
  951.   common_defined_global_count = 0;
  952.  
  953.   /* Keep a list of symbols referenced from the command line */
  954.   cl_refs_allocated = 10;
  955.   cmdline_references
  956.     = (struct glosym **) xmalloc (cl_refs_allocated
  957.                   * sizeof(struct glosym *));
  958.   *cmdline_references = 0;
  959.  
  960.   /* Completely decode ARGV.  */
  961.  
  962.   decode_command (argc, argv);
  963.  
  964.   /* Create the symbols `etext', `edata' and `end'.  */
  965.  
  966.   if (!relocatable_output)
  967.     symtab_init ();
  968.  
  969.   /* Determine whether to count the header as part of
  970.      the text size, and initialize the text size accordingly.
  971.      This depends on the kind of system and on the output format selected.  */
  972.  
  973.   N_SET_MAGIC (outheader, magic);
  974. #ifdef INITIALIZE_HEADER
  975.   INITIALIZE_HEADER;
  976. #endif
  977.  
  978.   text_size = sizeof (struct exec);
  979. #ifdef COFF_ENCAPSULATE
  980.   if (relocatable_output == 0)
  981.     {
  982.       need_coff_header = 1;
  983.       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  984.       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  985.       text_size += sizeof (struct coffheader);
  986.     }
  987. #endif
  988.  
  989.   text_size -= N_TXTOFF (outheader);
  990.  
  991.   if (text_size < 0)
  992.     text_size = 0;
  993.   entry_offset = text_size;
  994.  
  995.   if (!T_flag_specified && !relocatable_output)
  996.     text_start = N_TXTADDR (outheader);
  997.  
  998.   /* The text-start address is normally this far past a page boundary.  */
  999.   text_start_alignment = text_start % page_size;
  1000.  
  1001.   /* Load symbols of all input files.
  1002.      Also search all libraries and decide which library members to load.  */
  1003.  
  1004.   load_symbols ();
  1005.  
  1006.   /* Compute where each file's sections go, and relocate symbols.  */
  1007.  
  1008.   digest_symbols ();
  1009.  
  1010.   /* Print error messages for any missing symbols, for any warning
  1011.      symbols, and possibly multiple definitions */
  1012.  
  1013.   do_warnings (stderr);
  1014.  
  1015.   /* Print a map, if requested.  */
  1016.  
  1017.   if (write_map) print_symbols (stdout);
  1018.  
  1019.   /* Write the output file.  */
  1020.  
  1021.   if (make_executable || force_executable)
  1022.     write_output ();
  1023.  
  1024.   exit (!make_executable);
  1025. }
  1026.  
  1027. void decode_option ();
  1028.  
  1029. /* Analyze a command line argument.
  1030.    Return 0 if the argument is a filename.
  1031.    Return 1 if the argument is a option complete in itself.
  1032.    Return 2 if the argument is a option which uses an argument.
  1033.  
  1034.    Thus, the value is the number of consecutive arguments
  1035.    that are part of options.  */
  1036.  
  1037. int
  1038. classify_arg (arg)
  1039.      register char *arg;
  1040. {
  1041.   if (*arg != '-') return 0;
  1042.   switch (arg[1])
  1043.     {
  1044.     case 'A':
  1045.     case 'D':
  1046.     case 'e':
  1047.     case 'L':
  1048.     case 'l':
  1049.     case 'o':
  1050.     case 'u':
  1051.     case 'V':
  1052.     case 'y':
  1053.       if (arg[2])
  1054.     return 1;
  1055.       return 2;
  1056.  
  1057.     case 'B':
  1058.       if (! strcmp (&arg[2], "static"))
  1059.     return 1;
  1060.  
  1061.     case 'T':
  1062.       if (arg[2] == 0)
  1063.     return 2;
  1064.       if (! strcmp (&arg[2], "text"))
  1065.     return 2;
  1066.       if (! strcmp (&arg[2], "data"))
  1067.     return 2;
  1068.       return 1;
  1069.     }
  1070.  
  1071.   return 1;
  1072. }
  1073.  
  1074. /* Process the command arguments,
  1075.    setting up file_table with an entry for each input file,
  1076.    and setting variables according to the options.  */
  1077.  
  1078. void
  1079. decode_command (argc, argv)
  1080.      char **argv;
  1081.      int argc;
  1082. {
  1083.   register int i;
  1084.   register struct file_entry *p;
  1085.  
  1086.   number_of_files = 0;
  1087.   output_filename = "a.out";
  1088.  
  1089.   n_search_dirs = 0;
  1090.   search_dirs = (char **) xmalloc (sizeof (char *));
  1091.  
  1092.   /* First compute number_of_files so we know how long to make file_table.  */
  1093.   /* Also process most options completely.  */
  1094.  
  1095.   for (i = 1; i < argc; i++)
  1096.     {
  1097.       register int code = classify_arg (argv[i]);
  1098.       if (code)
  1099.     {
  1100.       if (i + code > argc)
  1101.         fatal ("no argument following %s\n", argv[i]);
  1102.  
  1103.       decode_option (argv[i], argv[i+1]);
  1104.  
  1105.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1106.         number_of_files++;
  1107.  
  1108.       i += code - 1;
  1109.     }
  1110.       else
  1111.     number_of_files++;
  1112.     }
  1113.  
  1114.   if (!number_of_files) {
  1115.     fprintf(stderr, " %d %s %s %s ", argc, argv[0], argv[1], argv[2]);
  1116.     fatal (" no input files", 0);
  1117.     }
  1118.  
  1119.   p = file_table
  1120.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1121.   bzero (p, number_of_files * sizeof (struct file_entry));
  1122.  
  1123.   /* Now scan again and fill in file_table.  */
  1124.   /* All options except -A and -l are ignored here.  */
  1125.  
  1126.   for (i = 1; i < argc; i++)
  1127.     {
  1128.       register int code = classify_arg (argv[i]);
  1129.  
  1130.       if (code)
  1131.     {
  1132.       char *string;
  1133.       if (code == 2)
  1134.         string = argv[i+1];
  1135.       else
  1136.         string = &argv[i][2];
  1137.  
  1138.       if (argv[i][1] == 'A')
  1139.         {
  1140.           if (p != file_table)
  1141.         fatal ("-A specified before an input file other than the first");
  1142.  
  1143.           p->filename = string;
  1144.           p->local_sym_name = string;
  1145.           p->just_syms_flag = 1;
  1146.           p++;
  1147.         }
  1148.       if (argv[i][1] == 'l')
  1149.         {
  1150.           p->filename = concat ("lib", string, ".a");
  1151.           p->local_sym_name = concat ("-l", string, "");
  1152.           p->search_dirs_flag = 1;
  1153.           p++;
  1154.         }
  1155.       i += code - 1;
  1156.     }
  1157.       else
  1158.     {
  1159.       p->filename = argv[i];
  1160.       p->local_sym_name = argv[i];
  1161.       p++;
  1162.     }
  1163.     }
  1164.  
  1165.   /* Now check some option settings for consistency.  */
  1166.  
  1167. #ifdef NMAGIC
  1168.   if ((magic == ZMAGIC || magic == NMAGIC)
  1169. #else
  1170.   if ((magic == ZMAGIC)
  1171. #endif
  1172.       && (text_start - text_start_alignment) & (page_size - 1))
  1173.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1174.  
  1175.   /* Append the standard search directories to the user-specified ones.  */
  1176.   {
  1177.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1178.     n_search_dirs += n;
  1179.     search_dirs
  1180.       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1181.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1182.        n * sizeof (char *));
  1183.   }
  1184. }
  1185.  
  1186.  
  1187. void
  1188. add_cmdline_ref (sp)
  1189.      struct glosym *sp;
  1190. {
  1191.   struct glosym **ptr;
  1192.  
  1193.   for (ptr = cmdline_references;
  1194.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1195.        ptr++)
  1196.     ;
  1197.  
  1198.   if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1199.     {
  1200.       int diff = ptr - cmdline_references;
  1201.       
  1202.       cl_refs_allocated *= 2;
  1203.       cmdline_references = (struct glosym **)
  1204.     xrealloc (cmdline_references,
  1205.          cl_refs_allocated * sizeof (struct glosym *));
  1206.       ptr = cmdline_references + diff;
  1207.     }
  1208.   
  1209.   *ptr++ = sp;
  1210.   *ptr = (struct glosym *) 0;
  1211. }
  1212.     
  1213. int
  1214. set_element_prefixed_p (name)
  1215.      char *name;
  1216. {
  1217.   struct string_list_element *p;
  1218.   int i;
  1219.  
  1220.   for (p = set_element_prefixes; p; p = p->next)
  1221.     {
  1222.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1223.     ;
  1224.  
  1225.       if (p->str[i] == '\0')
  1226.     return 1;
  1227.     }
  1228.   return 0;
  1229. }
  1230.  
  1231. int parse ();
  1232.  
  1233. /* Record an option and arrange to act on it later.
  1234.    ARG should be the following command argument,
  1235.    which may or may not be used by this option.
  1236.  
  1237.    The `l' and `A' options are ignored here since they actually
  1238.    specify input files.  */
  1239.  
  1240. void
  1241. decode_option (swt, arg)
  1242.      register char *swt, *arg;
  1243. {
  1244.   /* We get Bstatic from gcc on suns.  */
  1245.   if (! strcmp (swt + 1, "Bstatic"))
  1246.     return;
  1247.   if (! strcmp (swt + 1, "Ttext"))
  1248.     {
  1249.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1250.       T_flag_specified = 1;
  1251.       return;
  1252.     }
  1253.   if (! strcmp (swt + 1, "Tdata"))
  1254.     {
  1255.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1256.       Tdata_flag_specified = 1;
  1257.       return;
  1258.     }
  1259.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1260.     {
  1261.       force_executable = 1;
  1262.       return;
  1263.     }
  1264.  
  1265.   if (swt[2] != 0)
  1266.     arg = &swt[2];
  1267.  
  1268.   switch (swt[1])
  1269.     {
  1270.     case 'A':
  1271.       return;
  1272.  
  1273.     case 'D':
  1274.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1275.       return;
  1276.  
  1277.     case 'd':
  1278.       force_common_definition = 1;
  1279.       return;
  1280.  
  1281.     case 'e':
  1282.       entry_symbol = getsym (arg);
  1283.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1284.     undefined_global_sym_count++;
  1285.       entry_symbol->referenced = 1;
  1286.       add_cmdline_ref (entry_symbol);
  1287.       return;
  1288.  
  1289.     case 'l':
  1290.       return;
  1291.  
  1292.     case 'L':
  1293.       n_search_dirs++;
  1294.       search_dirs
  1295.     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1296.       search_dirs[n_search_dirs - 1] = arg;
  1297.       return;
  1298.  
  1299.     case 'M':
  1300.       write_map = 1;
  1301.       return;
  1302.  
  1303.     case 'N':
  1304.       magic = OMAGIC;
  1305.       return;
  1306.  
  1307. #ifdef NMAGIC
  1308.     case 'n':
  1309.       magic = NMAGIC;
  1310.       return;
  1311. #endif
  1312.  
  1313.     case 'o':
  1314.       output_filename = arg;
  1315.       return;
  1316.  
  1317.     case 'r':
  1318.       relocatable_output = 1;
  1319.       magic = OMAGIC;
  1320.       text_start = 0;
  1321.       return;
  1322.  
  1323.     case 'S':
  1324.       strip_symbols = STRIP_DEBUGGER;
  1325.       return;
  1326.  
  1327.     case 's':
  1328.       strip_symbols = STRIP_ALL;
  1329.       return;
  1330.  
  1331.     case 'T':
  1332.       text_start = parse (arg, "%x", "invalid argument to -T");
  1333.       T_flag_specified = 1;
  1334.       return;
  1335.  
  1336.     case 't':
  1337.       trace_files = 1;
  1338.       return;
  1339.  
  1340.     case 'u':
  1341.       {
  1342.     register symbol *sp = getsym (arg);
  1343.     if (!sp->defined && !sp->referenced)
  1344.       undefined_global_sym_count++;
  1345.     sp->referenced = 1;
  1346.     add_cmdline_ref (sp);
  1347.       }
  1348.       return;
  1349.  
  1350.     case 'V':
  1351.       {
  1352.     struct string_list_element *new
  1353.       = (struct string_list_element *)
  1354.         xmalloc (sizeof (struct string_list_element));
  1355.  
  1356.     new->str = arg;
  1357.     new->next = set_element_prefixes;
  1358.     set_element_prefixes = new;
  1359.     return;
  1360.       }
  1361.  
  1362.     case 'X':
  1363.       discard_locals = DISCARD_L;
  1364.       return;
  1365.  
  1366.     case 'x':
  1367.       discard_locals = DISCARD_ALL;
  1368.       return;
  1369.  
  1370.     case 'y':
  1371.       {
  1372.     register symbol *sp = getsym (&swt[2]);
  1373.     sp->trace = 1;
  1374.       }
  1375.       return;
  1376.  
  1377.     case 'z':
  1378.       magic = ZMAGIC;
  1379.       return;
  1380.  
  1381.     default:
  1382.       fatal ("invalid command option `%s'", swt);
  1383.     }
  1384. }
  1385.  
  1386. /** Convenient functions for operating on one or all files being */
  1387.  /** loaded.  */
  1388. void print_file_name ();
  1389.  
  1390. /* Call FUNCTION on each input file entry.
  1391.    Do not call for entries for libraries;
  1392.    instead, call once for each library member that is being loaded.
  1393.  
  1394.    FUNCTION receives two arguments: the entry, and ARG.  */
  1395.  
  1396. void
  1397. each_file (function, arg)
  1398.      register void (*function)();
  1399.      register int arg;
  1400. {
  1401.   register int i;
  1402.  
  1403.   for (i = 0; i < number_of_files; i++)
  1404.     {
  1405.       register struct file_entry *entry = &file_table[i];
  1406.       if (entry->library_flag)
  1407.         {
  1408.       register struct file_entry *subentry = entry->subfiles;
  1409.       for (; subentry; subentry = subentry->chain)
  1410.         (*function) (subentry, arg);
  1411.     }
  1412.       else
  1413.     (*function) (entry, arg);
  1414.     }
  1415. }
  1416.  
  1417. /* Call FUNCTION on each input file entry until it returns a non-zero
  1418.    value.  Return this value.
  1419.    Do not call for entries for libraries;
  1420.    instead, call once for each library member that is being loaded.
  1421.  
  1422.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1423.    function returning unsigned long (though this can probably be fudged). */
  1424.  
  1425. unsigned long
  1426. check_each_file (function, arg)
  1427.      register unsigned long (*function)();
  1428.      register int arg;
  1429. {
  1430.   register int i;
  1431.   register unsigned long return_val;
  1432.  
  1433.   for (i = 0; i < number_of_files; i++)
  1434.     {
  1435.       register struct file_entry *entry = &file_table[i];
  1436.       if (entry->library_flag)
  1437.         {
  1438.       register struct file_entry *subentry = entry->subfiles;
  1439.       for (; subentry; subentry = subentry->chain)
  1440.         if (return_val = (*function) (subentry, arg))
  1441.           return return_val;
  1442.     }
  1443.       else
  1444.     if (return_val = (*function) (entry, arg))
  1445.       return return_val;
  1446.     }
  1447.   return 0;
  1448. }
  1449.  
  1450. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1451.  
  1452. void
  1453. each_full_file (function, arg)
  1454.      register void (*function)();
  1455.      register int arg;
  1456. {
  1457.   register int i;
  1458.  
  1459.   for (i = 0; i < number_of_files; i++)
  1460.     {
  1461.       register struct file_entry *entry = &file_table[i];
  1462.       if (entry->just_syms_flag)
  1463.     continue;
  1464.       if (entry->library_flag)
  1465.         {
  1466.       register struct file_entry *subentry = entry->subfiles;
  1467.       for (; subentry; subentry = subentry->chain)
  1468.         (*function) (subentry, arg);
  1469.     }
  1470.       else
  1471.     (*function) (entry, arg);
  1472.     }
  1473. }
  1474.  
  1475. /* Close the input file that is now open.  */
  1476.  
  1477. void
  1478. file_close ()
  1479. {
  1480.   close (input_desc);
  1481.   input_desc = 0;
  1482.   input_file = 0;
  1483. }
  1484.  
  1485. /* Open the input file specified by 'entry', and return a descriptor.
  1486.    The open file is remembered; if the same file is opened twice in a row,
  1487.    a new open is not actually done.  */
  1488.  
  1489. int
  1490. file_open (entry)
  1491.      register struct file_entry *entry;
  1492. {
  1493.   register int desc;
  1494.  
  1495.   if (entry->superfile)
  1496.     return file_open (entry->superfile);
  1497.  
  1498.   if (entry == input_file)
  1499.     return input_desc;
  1500.  
  1501.   if (input_file) file_close ();
  1502.  
  1503.   if (entry->search_dirs_flag)
  1504.     {
  1505.       register char **p = search_dirs;
  1506.       int i;
  1507.  
  1508.       for (i = 0; i < n_search_dirs; i++)
  1509.     {
  1510.       register char *string
  1511.         = concat (search_dirs[i], "/", entry->filename);
  1512.       desc = open (string, O_RDONLY, 0);
  1513.       if (desc > 0)
  1514.         {
  1515.           entry->filename = string;
  1516.           entry->search_dirs_flag = 0;
  1517.           break;
  1518.         }
  1519.       free (string);
  1520.     }
  1521.     }
  1522.   else
  1523.     desc = open (entry->filename, O_RDONLY, 0);
  1524.  
  1525.   if (desc > 0)
  1526.     {
  1527.       input_file = entry;
  1528.       input_desc = desc;
  1529.       return desc;
  1530.     }
  1531.  
  1532.   perror_file (entry);
  1533.   /* NOTREACHED */
  1534. }
  1535.  
  1536. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1537.    and then a newline.  */
  1538.  
  1539. void
  1540. prline_file_name (entry, outfile)
  1541.      struct file_entry *entry;
  1542.      FILE *outfile;
  1543. {
  1544.   print_file_name (entry, outfile);
  1545.   fprintf (outfile, "\n");
  1546. }
  1547.  
  1548. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1549.  
  1550. void
  1551. print_file_name (entry, outfile)
  1552.      struct file_entry *entry;
  1553.      FILE *outfile;
  1554. {
  1555.   if (entry->superfile)
  1556.     {
  1557.       print_file_name (entry->superfile, outfile);
  1558.       fprintf (outfile, "(%s)", entry->filename);
  1559.     }
  1560.   else
  1561.     fprintf (outfile, "%s", entry->filename);
  1562. }
  1563.  
  1564. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1565.  
  1566. char *
  1567. get_file_name (entry)
  1568.      struct file_entry *entry;
  1569. {
  1570.   char *result, *supfile;
  1571.   if (entry->superfile)
  1572.     {
  1573.       supfile = get_file_name (entry->superfile);
  1574.       result = (char *) xmalloc (strlen (supfile)
  1575.                  + strlen (entry->filename) + 3);
  1576.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1577.       free (supfile);
  1578.     }
  1579.   else
  1580.     {
  1581.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1582.       strcpy (result, entry->filename);
  1583.     }
  1584.   return result;
  1585. }
  1586.  
  1587. /* Medium-level input routines for rel files.  */
  1588.  
  1589. /* Read a file's header into the proper place in the file_entry.
  1590.    DESC is the descriptor on which the file is open.
  1591.    ENTRY is the file's entry.  */
  1592.  
  1593. void
  1594. read_header (desc, entry)
  1595.      int desc;
  1596.      register struct file_entry *entry;
  1597. {
  1598.   register int len;
  1599.   struct exec *loc = (struct exec *) &entry->header;
  1600.  
  1601.   lseek (desc, entry->starting_offset, 0);
  1602.   len = read (desc, loc, sizeof (struct exec));
  1603.   if (len != sizeof (struct exec))
  1604.     fatal_with_file ("failure reading header of ", entry);
  1605.   if (N_BADMAG (*loc))
  1606.     fatal_with_file ("bad magic number in ", entry);
  1607.  
  1608.   entry->header_read_flag = 1;
  1609. }
  1610.  
  1611. /* Read the symbols of file ENTRY into core.
  1612.    Assume it is already open, on descriptor DESC.
  1613.    Also read the length of the string table, which follows the symbol table,
  1614.    but don't read the contents of the string table.  */
  1615.  
  1616. void
  1617. read_entry_symbols (desc, entry)
  1618.      struct file_entry *entry;
  1619.      int desc;
  1620. {
  1621.   int str_size;
  1622.  
  1623.   if (!entry->header_read_flag)
  1624.     read_header (desc, entry);
  1625.  
  1626.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1627.  
  1628.   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1629.   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1630.     fatal_with_file ("premature end of file in symbols of ", entry);
  1631.  
  1632.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1633.   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1634.     fatal_with_file ("bad string table size in ", entry);
  1635.  
  1636.   entry->string_size = str_size;
  1637. }
  1638.  
  1639. /* Read the string table of file ENTRY into core.
  1640.    Assume it is already open, on descriptor DESC.
  1641.    Also record whether a GDB symbol segment follows the string table.  */
  1642.  
  1643. void
  1644. read_entry_strings (desc, entry)
  1645.      struct file_entry *entry;
  1646.      int desc;
  1647. {
  1648.   int buffer;
  1649.  
  1650.   if (!entry->header_read_flag)
  1651.     read_header (desc, entry);
  1652.  
  1653.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1654.   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1655.     fatal_with_file ("premature end of file in strings of ", entry);
  1656.  
  1657.   /* While we are here, see if the file has a symbol segment at the end.
  1658.      For a separate file, just try reading some more.
  1659.      For a library member, compare current pos against total size.  */
  1660.   if (entry->superfile)
  1661.     {
  1662.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1663.     return;
  1664.     }
  1665.   else
  1666.     {
  1667.       buffer = read (desc, &buffer, sizeof buffer);
  1668.       if (buffer == 0)
  1669.     return;
  1670.       if (buffer != sizeof buffer)
  1671.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1672.     }
  1673.  
  1674.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1675. }
  1676.  
  1677. /* Read in the symbols of all input files.  */
  1678.  
  1679. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1680. void enter_file_symbols (), enter_global_ref (), search_library ();
  1681.  
  1682. void
  1683. load_symbols ()
  1684. {
  1685.   register int i;
  1686.  
  1687.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1688.  
  1689.   for (i = 0; i < number_of_files; i++)
  1690.     {
  1691.       register struct file_entry *entry = &file_table[i];
  1692.       read_file_symbols (entry);
  1693.     }
  1694.  
  1695.   if (trace_files) fprintf (stderr, "\n");
  1696. }
  1697.  
  1698. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1699.    If it is a library, search it and load the appropriate members
  1700.    (which means calling this function recursively on those members).  */
  1701.  
  1702. void
  1703. read_file_symbols (entry)
  1704.      register struct file_entry *entry;
  1705. {
  1706.   register int desc;
  1707.   register int len;
  1708.   struct exec hdr;
  1709.  
  1710.   desc = file_open (entry);
  1711.  
  1712.   len = read (desc, &hdr, sizeof hdr);
  1713.   if (len != sizeof hdr)
  1714.     fatal_with_file ("failure reading header of ", entry);
  1715.  
  1716.   if (!N_BADMAG (hdr))
  1717.     {
  1718.       read_entry_symbols (desc, entry);
  1719.       entry->strings = (char *) alloca (entry->string_size);
  1720.       read_entry_strings (desc, entry);
  1721.       enter_file_symbols (entry);
  1722.       entry->strings = 0;
  1723.     }
  1724.   else
  1725.     {
  1726.       char armag[SARMAG];
  1727.  
  1728.       lseek (desc, 0, 0);
  1729.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1730.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1731.       entry->library_flag = 1;
  1732.       search_library (desc, entry);
  1733.     }
  1734.  
  1735.   file_close ();
  1736. }
  1737.  
  1738. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  1739.  
  1740. void
  1741. enter_file_symbols (entry)
  1742.      struct file_entry *entry;
  1743. {
  1744.   register struct nlist
  1745.     *p,
  1746.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1747.   int lowest_set_vector = -1;
  1748.  
  1749.   if (trace_files) prline_file_name (entry, stderr);
  1750.  
  1751.   for (p = entry->symbols; p < end; p++)
  1752.     {
  1753.       if (p->n_type == (N_SETV | N_EXT)) continue;
  1754.       if (set_element_prefixes
  1755.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  1756.     p->n_type += (N_SETA - N_ABS);
  1757.  
  1758.       if (SET_ELEMENT_P (p->n_type))
  1759.     {
  1760.       set_symbol_count++;
  1761.       if (!relocatable_output)
  1762.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1763.     }
  1764.       else if (p->n_type == N_WARNING)
  1765.     {
  1766.       char *name = p->n_un.n_strx + entry->strings;
  1767.  
  1768.       /* Grab the next entry.  */
  1769.       p++;
  1770.       if (p->n_type != (N_UNDF | N_EXT))
  1771.         {
  1772.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  1773.                progname, entry->filename);
  1774.           make_executable = 0;
  1775.           p--;        /* Process normally.  */
  1776.         }
  1777.       else
  1778.         {
  1779.           symbol *sp;
  1780.           char *sname = p->n_un.n_strx + entry->strings; 
  1781.           /* Deal with the warning symbol.  */
  1782.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1783.           sp = getsym (sname);
  1784.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  1785.           strcpy (sp->warning, name);
  1786.           warning_count++;
  1787.         }
  1788.     }
  1789.       else if (p->n_type & N_EXT)
  1790.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1791.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1792.     {
  1793.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  1794.         non_L_local_sym_count++;
  1795.       local_sym_count++;
  1796.     }
  1797.       else debugger_sym_count++;
  1798.     }
  1799.  
  1800.    /* Count one for the local symbol that we generate,
  1801.       whose name is the file's name (usually) and whose address
  1802.       is the start of the file's text.  */
  1803.  
  1804.   local_sym_count++;
  1805.   non_L_local_sym_count++;
  1806. }
  1807.  
  1808. /* Enter one global symbol in the hash table.
  1809.    NLIST_P points to the `struct nlist' read from the file
  1810.    that describes the global symbol.  NAME is the symbol's name.
  1811.    ENTRY is the file entry for the file the symbol comes from.
  1812.  
  1813.    The `struct nlist' is modified by placing it on a chain of
  1814.    all such structs that refer to the same global symbol.
  1815.    This chain starts in the `refs' field of the symbol table entry
  1816.    and is chained through the `n_name'.  */
  1817.  
  1818. void
  1819. enter_global_ref (nlist_p, name, entry)
  1820.      register struct nlist *nlist_p;
  1821.      char *name;
  1822.      struct file_entry *entry;
  1823. {
  1824.   register symbol *sp = getsym (name);
  1825.   register int type = nlist_p->n_type;
  1826.   int oldref = sp->referenced;
  1827.   int olddef = sp->defined;
  1828.  
  1829.   nlist_p->n_un.n_name = (char *) sp->refs;
  1830.   sp->refs = nlist_p;
  1831.  
  1832.   sp->referenced = 1;
  1833.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1834.     {
  1835.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  1836.     sp->defined = type;
  1837.  
  1838.       if (oldref && !olddef)
  1839.     /* It used to be undefined and we're defining it.  */
  1840.     undefined_global_sym_count--;
  1841.  
  1842.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  1843.     {
  1844.       /* First definition and it's common.  */
  1845.       common_defined_global_count++;
  1846.       sp->max_common_size = nlist_p->n_value;
  1847.     }
  1848.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  1849.     {
  1850.       /* It used to be common and we're defining it as
  1851.          something else.  */
  1852.       common_defined_global_count--;
  1853.       sp->max_common_size = 0;
  1854.     }
  1855.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  1856.       && sp->max_common_size < nlist_p->n_value)
  1857.     /* It used to be common and this is a new common entry to
  1858.        which we need to pay attention.  */
  1859.     sp->max_common_size = nlist_p->n_value;
  1860.  
  1861.       /* Are we defining it as a set element?  */
  1862.       if (SET_ELEMENT_P (type)
  1863.       && (!olddef || (olddef && sp->max_common_size)))
  1864.     set_vector_count++;
  1865.       /* As an indirection?  */
  1866.       else if (type == (N_INDR | N_EXT))
  1867.     {
  1868.       /* Indirect symbols value should be modified to point
  1869.          a symbol being equivalenced to. */
  1870.       nlist_p->n_value
  1871.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1872.                      + entry->strings);
  1873.       if ((symbol *) nlist_p->n_value == sp)
  1874.         {
  1875.           /* Somebody redefined a symbol to be itself.  */
  1876.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  1877.                entry->filename, name);
  1878.           /* Rewrite this symbol as being a global text symbol
  1879.          with value 0.  */
  1880.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  1881.           nlist_p->n_value = 0;
  1882.           /* Don't make the output executable.  */
  1883.           make_executable = 0;
  1884.         }
  1885.       else
  1886.         global_indirect_count++;
  1887.     }
  1888.     }
  1889.   else
  1890.     if (!oldref)
  1891. #ifndef DOLLAR_KLUDGE
  1892.       undefined_global_sym_count++;
  1893. #else
  1894.       {
  1895.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  1896.       {
  1897.         /* This is an (ISI?) $-conditional; skip it */
  1898.         sp->referenced = 0;
  1899.         if (sp->trace)
  1900.           {
  1901.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  1902.         print_file_name (entry, stderr);
  1903.         fprintf (stderr, "\n");
  1904.           }
  1905.         return;
  1906.       }
  1907.     else
  1908.       undefined_global_sym_count++;
  1909.       }
  1910. #endif
  1911.  
  1912.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  1913.     text_start = nlist_p->n_value;
  1914.  
  1915.   if (sp->trace)
  1916.     {
  1917.       register char *reftype;
  1918.       switch (type & N_TYPE)
  1919.     {
  1920.     case N_UNDF:
  1921.       if (nlist_p->n_value)
  1922.         reftype = "defined as common";
  1923.       else reftype = "referenced";
  1924.       break;
  1925.  
  1926.     case N_ABS:
  1927.       reftype = "defined as absolute";
  1928.       break;
  1929.  
  1930.     case N_TEXT:
  1931.       reftype = "defined in text section";
  1932.       break;
  1933.  
  1934.     case N_DATA:
  1935.       reftype = "defined in data section";
  1936.       break;
  1937.  
  1938.     case N_BSS:
  1939.       reftype = "defined in BSS section";
  1940.       break;
  1941.  
  1942.     case N_SETT:
  1943.       reftype = "is a text set element";
  1944.       break;
  1945.  
  1946.     case N_SETD:
  1947.       reftype = "is a data set element";
  1948.       break;
  1949.  
  1950.     case N_SETB:
  1951.       reftype = "is a BSS set element";
  1952.       break;
  1953.  
  1954.     case N_SETA:
  1955.       reftype = "is an absolute set element";
  1956.       break;
  1957.  
  1958.     case N_SETV:
  1959.       reftype = "defined in data section as vector";
  1960.       break;
  1961.  
  1962.     case N_INDR:
  1963.       reftype = (char *) alloca (23
  1964.                      + strlen ((nlist_p + 1)->n_un.n_strx
  1965.                            + entry->strings));
  1966.       sprintf (reftype, "defined equivalent to %s",
  1967.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  1968.       break;
  1969.  
  1970. #ifdef sequent
  1971.     case N_SHUNDF:
  1972.       reftype = "shared undf";
  1973.       break;
  1974.  
  1975. /* These conflict with cases above.
  1976.     case N_SHDATA:
  1977.       reftype = "shared data";
  1978.       break;
  1979.  
  1980.     case N_SHBSS:
  1981.       reftype = "shared BSS";
  1982.       break;
  1983. */
  1984.     default:
  1985.       reftype = "I don't know this type";
  1986.       break;
  1987. #endif
  1988.     }
  1989.  
  1990.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  1991.       print_file_name (entry, stderr);
  1992.       fprintf (stderr, "\n");
  1993.     }
  1994. }
  1995.  
  1996. /* This return 0 if the given file entry's symbol table does *not*
  1997.    contain the nlist point entry, and it returns the files entry
  1998.    pointer (cast to unsigned long) if it does. */
  1999.  
  2000. unsigned long
  2001. contains_symbol (entry, n_ptr)
  2002.      struct file_entry *entry;
  2003.      register struct nlist *n_ptr;
  2004. {
  2005.   if (n_ptr >= entry->symbols &&
  2006.       n_ptr < (entry->symbols
  2007.            + (entry->header.a_syms / sizeof (struct nlist))))
  2008.     return (unsigned long) entry;
  2009.   return 0;
  2010. }
  2011.  
  2012.  
  2013. /* Searching libraries */
  2014.  
  2015. struct file_entry *decode_library_subfile ();
  2016. void linear_library (), symdef_library ();
  2017.  
  2018. /* Search the library ENTRY, already open on descriptor DESC.
  2019.    This means deciding which library members to load,
  2020.    making a chain of `struct file_entry' for those members,
  2021.    and entering their global symbols in the hash table.  */
  2022.  
  2023. void
  2024. search_library (desc, entry)
  2025.      int desc;
  2026.      struct file_entry *entry;
  2027. {
  2028.   int member_length;
  2029.   register char *name;
  2030.   register struct file_entry *subentry;
  2031.  
  2032.   if (!undefined_global_sym_count) return;
  2033.  
  2034.   /* Examine its first member, which starts SARMAG bytes in.  */
  2035.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2036.   if (!subentry) return;
  2037.  
  2038.   name = subentry->filename;
  2039.   free (subentry);
  2040.  
  2041.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2042.  
  2043.   if (!strcmp (name, "__.SYMDEF"))
  2044.     symdef_library (desc, entry, member_length);
  2045.   else
  2046.     linear_library (desc, entry);
  2047. }
  2048.  
  2049. /* Construct and return a file_entry for a library member.
  2050.    The library's file_entry is library_entry, and the library is open on DESC.
  2051.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2052.    We store the length of the member into *LENGTH_LOC.  */
  2053.  
  2054. struct file_entry *
  2055. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2056.      int desc;
  2057.      struct file_entry *library_entry;
  2058.      int subfile_offset;
  2059.      int *length_loc;
  2060. {
  2061.   int bytes_read;
  2062.   register int namelen;
  2063.   int member_length;
  2064.   register char *name;
  2065.   struct ar_hdr hdr1;
  2066.   register struct file_entry *subentry;
  2067.  
  2068.   lseek (desc, subfile_offset, 0);
  2069.  
  2070.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  2071.   if (!bytes_read)
  2072.     return 0;        /* end of archive */
  2073.  
  2074.   if (sizeof hdr1 != bytes_read)
  2075.     fatal_with_file ("malformed library archive ", library_entry);
  2076.  
  2077.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2078.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2079.  
  2080.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2081.   bzero (subentry, sizeof (struct file_entry));
  2082.  
  2083.   for (namelen = 0;
  2084.        namelen < sizeof hdr1.ar_name
  2085.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2086.        && hdr1.ar_name[namelen] != '/';
  2087.        namelen++);
  2088.  
  2089.   name = (char *) xmalloc (namelen+1);
  2090.   strncpy (name, hdr1.ar_name, namelen);
  2091.   name[namelen] = 0;
  2092.  
  2093.   subentry->filename = name;
  2094.   subentry->local_sym_name = name;
  2095.   subentry->symbols = 0;
  2096.   subentry->strings = 0;
  2097.   subentry->subfiles = 0;
  2098.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2099.   subentry->superfile = library_entry;
  2100.   subentry->library_flag = 0;
  2101.   subentry->header_read_flag = 0;
  2102.   subentry->just_syms_flag = 0;
  2103.   subentry->chain = 0;
  2104.   subentry->total_size = member_length;
  2105.  
  2106.   (*length_loc) = member_length;
  2107.  
  2108.   return subentry;
  2109. }
  2110.  
  2111. int subfile_wanted_p ();
  2112.  
  2113. /* Search a library that has a __.SYMDEF member.
  2114.    DESC is a descriptor on which the library is open.
  2115.      The file pointer is assumed to point at the __.SYMDEF data.
  2116.    ENTRY is the library's file_entry.
  2117.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2118.  
  2119. void
  2120. symdef_library (desc, entry, member_length)
  2121.      int desc;
  2122.      struct file_entry *entry;
  2123.      int member_length;
  2124. {
  2125.   int *symdef_data = (int *) xmalloc (member_length);
  2126.   register struct symdef *symdef_base;
  2127.   char *sym_name_base;
  2128.   int number_of_symdefs;
  2129.   int length_of_strings;
  2130.   int not_finished;
  2131.   int bytes_read;
  2132.   register int i;
  2133.   struct file_entry *prev = 0;
  2134.   int prev_offset = 0;
  2135.  
  2136.   bytes_read = read (desc, symdef_data, member_length);
  2137.   if (bytes_read != member_length)
  2138.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2139.  
  2140.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2141.   if (number_of_symdefs < 0 ||
  2142.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2143.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2144.  
  2145.   symdef_base = (struct symdef *) (symdef_data + 1);
  2146.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2147.  
  2148.   if (length_of_strings < 0
  2149.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2150.       + 2 * sizeof (int) != member_length)
  2151.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2152.  
  2153.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2154.  
  2155.   /* Check all the string indexes for validity.  */
  2156.  
  2157.   for (i = 0; i < number_of_symdefs; i++)
  2158.     {
  2159.       register int index = symdef_base[i].symbol_name_string_index;
  2160.       if (index < 0 || index >= length_of_strings
  2161.       || (index && *(sym_name_base + index - 1)))
  2162.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2163.     }
  2164.  
  2165.   /* Search the symdef data for members to load.
  2166.      Do this until one whole pass finds nothing to load.  */
  2167.  
  2168.   not_finished = 1;
  2169.   while (not_finished)
  2170.     {
  2171.       not_finished = 0;
  2172.  
  2173.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2174.      Load the library members that contain such symbols.  */
  2175.  
  2176.       for (i = 0;
  2177.        (i < number_of_symdefs
  2178.         && (undefined_global_sym_count || common_defined_global_count));
  2179.        i++)
  2180.     if (symdef_base[i].symbol_name_string_index >= 0)
  2181.       {
  2182.         register symbol *sp;
  2183.  
  2184.         sp = getsym_soft (sym_name_base
  2185.                   + symdef_base[i].symbol_name_string_index);
  2186.  
  2187.         /* If we find a symbol that appears to be needed, think carefully
  2188.            about the archive member that the symbol is in.  */
  2189.  
  2190.         if (sp && ((sp->referenced && !sp->defined)
  2191.                || (sp->defined && sp->max_common_size)))
  2192.           {
  2193.         int junk;
  2194.         register int j;
  2195.         register int offset = symdef_base[i].library_member_offset;
  2196.         struct file_entry *subentry;
  2197.  
  2198.         /* Don't think carefully about any archive member
  2199.            more than once in a given pass.  */
  2200.  
  2201.         if (prev_offset == offset)
  2202.           continue;
  2203.         prev_offset = offset;
  2204.  
  2205.         /* Read the symbol table of the archive member.  */
  2206.  
  2207.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2208.         if (subentry == 0)
  2209.           fatal ("invalid offset for %s in symbol table of %s",
  2210.              sym_name_base
  2211.              + symdef_base[i].symbol_name_string_index,
  2212.              entry->filename);
  2213.         read_entry_symbols (desc, subentry);
  2214.         subentry->strings = (char *) malloc (subentry->string_size);
  2215.         read_entry_strings (desc, subentry);
  2216.  
  2217.         /* Now scan the symbol table and decide whether to load.  */
  2218.  
  2219.         if (!subfile_wanted_p (subentry))
  2220.           {
  2221.             free (subentry->symbols);
  2222.             free (subentry);
  2223.           }
  2224.         else
  2225.           {
  2226.             /* This member is needed; load it.
  2227.                Since we are loading something on this pass,
  2228.                we must make another pass through the symdef data.  */
  2229.  
  2230.             not_finished = 1;
  2231.  
  2232.             enter_file_symbols (subentry);
  2233.  
  2234.             if (prev)
  2235.               prev->chain = subentry;
  2236.             else entry->subfiles = subentry;
  2237.             prev = subentry;
  2238.  
  2239.             /* Clear out this member's symbols from the symdef data
  2240.                so that following passes won't waste time on them.  */
  2241.  
  2242.             for (j = 0; j < number_of_symdefs; j++)
  2243.               {
  2244.             if (symdef_base[j].library_member_offset == offset)
  2245.               symdef_base[j].symbol_name_string_index = -1;
  2246.               }
  2247.           }
  2248.  
  2249.         /* We'll read the strings again if we need them again.  */
  2250.         free (subentry->strings);
  2251.         subentry->strings = 0;
  2252.           }
  2253.       }
  2254.     }
  2255.  
  2256.   free (symdef_data);
  2257. }
  2258.  
  2259.  
  2260. /* Handle a subentry for a file with no __.SYMDEF. */
  2261.  
  2262. process_subentry (desc, subentry, entry, prev_addr)
  2263.      int desc;
  2264.      register struct file_entry *subentry;
  2265.      struct file_entry **prev_addr, *entry;
  2266. {
  2267.   register struct file_entry *prev = *prev_addr;
  2268.  
  2269.   read_entry_symbols (desc, subentry);
  2270.   subentry->strings = (char *) alloca (subentry->string_size);
  2271.   read_entry_strings (desc, subentry);
  2272.  
  2273.   if (!subfile_wanted_p (subentry))
  2274.     {
  2275.       free (subentry->symbols);
  2276.       free (subentry);
  2277.     }
  2278.   else
  2279.     {
  2280.       enter_file_symbols (subentry);
  2281.  
  2282.       if (prev)
  2283.     prev->chain = subentry;
  2284.       else
  2285.     entry->subfiles = subentry;
  2286.       prev = subentry;
  2287.       subentry->strings = 0; /* Since space will dissapear on return */
  2288.     }
  2289. }
  2290.  
  2291. /* Search a library that has no __.SYMDEF.
  2292.    ENTRY is the library's file_entry.
  2293.    DESC is the descriptor it is open on.  */
  2294.  
  2295. void
  2296. linear_library (desc, entry)
  2297.      int desc;
  2298.      struct file_entry *entry;
  2299. {
  2300.   struct file_entry *prev = 0;
  2301.   register int this_subfile_offset = SARMAG;
  2302.  
  2303.   while (undefined_global_sym_count || common_defined_global_count)
  2304.     {
  2305.       int member_length;
  2306.       register struct file_entry *subentry;
  2307.  
  2308.       subentry = decode_library_subfile (desc, entry, this_subfile_offset,
  2309.                      &member_length);
  2310.       if (!subentry) return;
  2311.  
  2312.       process_subentry (desc, subentry, entry, &prev);
  2313.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2314.       if (this_subfile_offset & 1) this_subfile_offset++;
  2315.     }
  2316. }
  2317.  
  2318. /* ENTRY is an entry for a library member.
  2319.    Its symbols have been read into core, but not entered.
  2320.    Return nonzero if we ought to load this member.  */
  2321.  
  2322. int
  2323. subfile_wanted_p (entry)
  2324.      struct file_entry *entry;
  2325. {
  2326.   register struct nlist *p;
  2327.   register struct nlist *end
  2328.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2329. #ifdef DOLLAR_KLUDGE
  2330.   register int dollar_cond = 0;
  2331. #endif
  2332.  
  2333.   for (p = entry->symbols; p < end; p++)
  2334.     {
  2335.       register int type = p->n_type;
  2336.       register char *name = p->n_un.n_strx + entry->strings;
  2337.  
  2338.       /* If the symbol has an interesting definition, we could
  2339.      potentially want it.  */
  2340.       if (type & N_EXT
  2341.       && (type != (N_UNDF | N_EXT) || p->n_value
  2342.           
  2343. #ifdef DOLLAR_KLUDGE
  2344.            || name[1] == '$'
  2345. #endif
  2346.           )
  2347.       && !SET_ELEMENT_P (type)
  2348.       && !set_element_prefixed_p (name))
  2349.     {
  2350.       register symbol *sp = getsym_soft (name);
  2351.  
  2352. #ifdef DOLLAR_KLUDGE
  2353.       if (name[1] == '$')
  2354.         {
  2355.           sp = getsym_soft (&name[2]);
  2356.           dollar_cond = 1;
  2357.           if (!sp) continue;
  2358.           if (sp->referenced)
  2359.         {
  2360.           if (write_map)
  2361.             {
  2362.               print_file_name (entry, stdout);
  2363.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2364.             }
  2365.           return 1;
  2366.         }
  2367.           continue;
  2368.         }
  2369. #endif
  2370.  
  2371.       /* If this symbol has not been hashed, we can't be looking for it. */
  2372.  
  2373.       if (!sp) continue;
  2374.  
  2375.       if ((sp->referenced && !sp->defined)
  2376.           || (sp->defined && sp->max_common_size))
  2377.         {
  2378.           /* This is a symbol we are looking for.  It is either
  2379.              not yet defined or defined as a common.  */
  2380. #ifdef DOLLAR_KLUDGE
  2381.           if (dollar_cond) continue;
  2382. #endif
  2383.           if (type == (N_UNDF | N_EXT))
  2384.         {
  2385.           /* Symbol being defined as common.
  2386.              Remember this, but don't load subfile just for this.  */
  2387.  
  2388.           /* If it didn't used to be common, up the count of
  2389.              common symbols.  */
  2390.           if (!sp->max_common_size)
  2391.             common_defined_global_count++;
  2392.  
  2393.           if (sp->max_common_size < p->n_value)
  2394.             sp->max_common_size = p->n_value;
  2395.           if (!sp->defined)
  2396.             undefined_global_sym_count--;
  2397.           sp->defined = 1;
  2398.           continue;
  2399.         }
  2400.  
  2401.           if (write_map)
  2402.         {
  2403.           print_file_name (entry, stdout);
  2404.           fprintf (stdout, " needed due to %s\n", sp->name);
  2405.         }
  2406.           return 1;
  2407.         }
  2408.     }
  2409.     }
  2410.  
  2411.   return 0;
  2412. }
  2413.  
  2414. void consider_file_section_lengths (), relocate_file_addresses ();
  2415.  
  2416. /* Having entered all the global symbols and found the sizes of sections
  2417.    of all files to be linked, make all appropriate deductions from this data.
  2418.  
  2419.    We propagate global symbol values from definitions to references.
  2420.    We compute the layout of the output file and where each input file's
  2421.    contents fit into it.  */
  2422.  
  2423. void
  2424. digest_symbols ()
  2425. {
  2426.   register int i;
  2427.   int setv_fill_count;
  2428.  
  2429.   if (trace_files)
  2430.     fprintf (stderr, "Digesting symbol information:\n\n");
  2431.  
  2432.   /* Compute total size of sections */
  2433.  
  2434.   each_file (consider_file_section_lengths, 0);
  2435.  
  2436.   /* If necessary, pad text section to full page in the file.
  2437.      Include the padding in the text segment size.  */
  2438.  
  2439. #ifdef NMAGIC
  2440.   if (magic == ZMAGIC || magic == NMAGIC)
  2441. #else
  2442.   if (magic == ZMAGIC)
  2443. #endif
  2444.     {
  2445.       int text_end = text_size + N_TXTOFF (outheader);
  2446.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2447.       text_size += text_pad;
  2448.     }
  2449.  
  2450.   outheader.a_text = text_size;
  2451. #ifdef sequent
  2452.   outheader.a_text += N_ADDRADJ (outheader);
  2453. #endif
  2454.  
  2455.   /* Make the data segment address start in memory on a suitable boundary.  */
  2456.  
  2457.   if (! Tdata_flag_specified)
  2458.     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  2459.  
  2460.   /* Make sure bss starts out aligned as much as anyone can want.  */
  2461.  
  2462.   data_size = (data_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2463.  
  2464.   /* Set up the set element vector */
  2465.  
  2466.   if (!relocatable_output)
  2467.     {
  2468.       /* The set sector size is the number of set elements + a word
  2469.          for each symbol for the length word at the beginning of the
  2470.      vector, plus a word for each symbol for a zero at the end of
  2471.      the vector (for incremental linking).  */
  2472.       set_sect_size
  2473.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  2474.       set_sect_start = data_start + data_size;
  2475.       data_size += set_sect_size;
  2476.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2477.       setv_fill_count = 0;
  2478.     }
  2479.  
  2480.   /* Compute start addresses of each file's sections and symbols.  */
  2481.  
  2482.   each_full_file (relocate_file_addresses, 0);
  2483.  
  2484.   /* Now, for each symbol, verify that it is defined globally at most once.
  2485.      Put the global value into the symbol entry.
  2486.      Common symbols are allocated here, in the BSS section.
  2487.      Each defined symbol is given a '->defined' field
  2488.       which is the correct N_ code for its definition,
  2489.       except in the case of common symbols with -r.
  2490.      Then make all the references point at the symbol entry
  2491.      instead of being chained together. */
  2492.  
  2493.   defined_global_sym_count = 0;
  2494.  
  2495.   for (i = 0; i < TABSIZE; i++)
  2496.     {
  2497.       register symbol *sp;
  2498.       for (sp = symtab[i]; sp; sp = sp->link)
  2499.     {
  2500.       /* For each symbol */
  2501.       register struct nlist *p, *next;
  2502.       int defs = 0, com = sp->max_common_size, erred = 0;
  2503.       struct nlist *first_definition;
  2504.       for (p = sp->refs; p; p = next)
  2505.         {
  2506.           register int type = p->n_type;
  2507.  
  2508.           if (SET_ELEMENT_P (type))
  2509.         {
  2510.           if (relocatable_output)
  2511.             fatal ("internal: global ref to set element with -r");
  2512.           if (!defs++)
  2513.             {
  2514.               sp->value = set_sect_start
  2515.             + setv_fill_count++ * sizeof (unsigned long);
  2516.               sp->defined = N_SETV | N_EXT;
  2517.               first_definition = p;
  2518.             }
  2519.           else if ((sp->defined & ~N_EXT) != N_SETV)
  2520.             {
  2521.               sp->multiply_defined = 1;
  2522.               multiple_def_count++;
  2523.             }
  2524.           set_vectors[setv_fill_count++] = p->n_value;
  2525.         }
  2526.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  2527.         {
  2528.           /* non-common definition */
  2529.           if (defs++ && sp->value != p->n_value)
  2530.             {
  2531.               sp->multiply_defined = 1;
  2532.               multiple_def_count++;
  2533.             }
  2534.           sp->value = p->n_value;
  2535.           sp->defined = type;
  2536.           first_definition = p;
  2537.         }
  2538.           next = (struct nlist *) p->n_un.n_name;
  2539.           p->n_un.n_name = (char *) sp;
  2540.         }
  2541.       /* Allocate as common if defined as common and not defined for real */
  2542.       if (com && !defs)
  2543.         {
  2544.           if (!relocatable_output || force_common_definition)
  2545.         {
  2546.           int align = sizeof (int);
  2547.  
  2548.           /* Round up to nearest sizeof (int).  I don't know
  2549.              whether this is necessary or not (given that
  2550.              alignment is taken care of later), but it's
  2551.              traditional, so I'll leave it in.  Note that if
  2552.              this size alignment is ever removed, ALIGN above
  2553.              will have to be initialized to 1 instead of
  2554.              sizeof (int).  */
  2555.           
  2556.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  2557.  
  2558.           while (!(com & align))
  2559.             align <<= 1;
  2560.  
  2561.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  2562.  
  2563.           bss_size = ((((bss_size + data_size + data_start)
  2564.                   + (align - 1)) & (- align))
  2565.                   - data_size - data_start);
  2566.  
  2567.           sp->value = data_start + data_size + bss_size;
  2568.           sp->defined = N_BSS | N_EXT;
  2569.           bss_size += com;
  2570.           if (write_map)
  2571.             printf ("Allocating common %s: %x at %x\n",
  2572.                 sp->name, com, sp->value);
  2573.         }
  2574.           else
  2575.         {
  2576.           sp->defined = 0;
  2577.           undefined_global_sym_count++;
  2578.         }
  2579.         }
  2580.       /* Set length word at front of vector and zero byte at end.
  2581.          Reverse the vector itself to put it in file order.  */
  2582.       if ((sp->defined & ~N_EXT) == N_SETV)
  2583.         {
  2584.           unsigned long length_word_index
  2585.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  2586.           unsigned long i, tmp;
  2587.  
  2588.           set_vectors[length_word_index]
  2589.         = setv_fill_count - 1 - length_word_index;
  2590.  
  2591.           /* Reverse the vector.  */
  2592.           for (i = 1;
  2593.            i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  2594.            i++)
  2595.         {
  2596.           tmp = set_vectors[length_word_index + i];
  2597.           set_vectors[length_word_index + i]
  2598.             = set_vectors[setv_fill_count - i];
  2599.           set_vectors[setv_fill_count - i] = tmp;
  2600.         }
  2601.  
  2602.           set_vectors[setv_fill_count++] = 0;
  2603.         }
  2604.       if (sp->defined)
  2605.         defined_global_sym_count++;
  2606.     }
  2607.     }
  2608.  
  2609.   /* Make sure end of bss is aligned as much as anyone can want.  */
  2610.  
  2611.   bss_size = (bss_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2612.  
  2613.   if (end_symbol)        /* These are null if -r.  */
  2614.     {
  2615.       etext_symbol->value = text_size + text_start;
  2616.       edata_symbol->value = data_start + data_size;
  2617.       end_symbol->value = data_start + data_size + bss_size;
  2618.     }
  2619.  
  2620.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2621.  
  2622.   if (specified_data_size && specified_data_size > data_size)
  2623.     data_pad = specified_data_size - data_size;
  2624.  
  2625.   if (magic == ZMAGIC)
  2626.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  2627.                - data_size;
  2628.  
  2629.   bss_size -= data_pad;
  2630.   if (bss_size < 0) bss_size = 0;
  2631.  
  2632.   data_size += data_pad;
  2633. }
  2634.  
  2635. /* Accumulate the section sizes of input file ENTRY
  2636.    into the section sizes of the output file.  */
  2637.  
  2638. void
  2639. consider_file_section_lengths (entry)
  2640.      register struct file_entry *entry;
  2641. {
  2642.   if (entry->just_syms_flag)
  2643.     return;
  2644.  
  2645.   entry->text_start_address = text_size;
  2646.   /* If there were any vectors, we need to chop them off */
  2647.   text_size += entry->header.a_text;
  2648.   entry->data_start_address = data_size;
  2649.   data_size += entry->header.a_data;
  2650.   entry->bss_start_address = bss_size;
  2651.   bss_size += entry->header.a_bss;
  2652.  
  2653.   text_reloc_size += entry->header.a_trsize;
  2654.   data_reloc_size += entry->header.a_drsize;
  2655. }
  2656.  
  2657. /* Determine where the sections of ENTRY go into the output file,
  2658.    whose total section sizes are already known.
  2659.    Also relocate the addresses of the file's local and debugger symbols.  */
  2660.  
  2661. void
  2662. relocate_file_addresses (entry)
  2663.      register struct file_entry *entry;
  2664. {
  2665.   entry->text_start_address += text_start;
  2666.   /* Note that `data_start' and `data_size' have not yet been
  2667.      adjusted for `data_pad'.  If they had been, we would get the wrong
  2668.      results here.  */
  2669.   entry->data_start_address += data_start;
  2670.   entry->bss_start_address += data_start + data_size;
  2671.  
  2672.   {
  2673.     register struct nlist *p;
  2674.     register struct nlist *end
  2675.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2676.  
  2677.     for (p = entry->symbols; p < end; p++)
  2678.       {
  2679.     /* If this belongs to a section, update it by the section's start address */
  2680.     register int type = p->n_type & N_TYPE;
  2681.  
  2682.     switch (type)
  2683.       {
  2684.       case N_TEXT:
  2685.       case N_SETT:
  2686.         p->n_value += entry->text_start_address;
  2687.         break;
  2688.       case N_DATA:
  2689.       case N_SETV:
  2690.       case N_SETD:
  2691.         /* A symbol whose value is in the data section
  2692.            is present in the input file as if the data section
  2693.            started at an address equal to the length of the file's text.  */
  2694.         p->n_value += entry->data_start_address - entry->header.a_text;
  2695.         break;
  2696.       case N_BSS:
  2697.       case N_SETB:
  2698.         /* likewise for symbols with value in BSS.  */
  2699.         p->n_value += entry->bss_start_address
  2700.           - entry->header.a_text - entry->header.a_data;
  2701.         break;
  2702.       }
  2703.       }
  2704.   }
  2705. }
  2706.  
  2707. void describe_file_sections (), list_file_locals ();
  2708.  
  2709. /* Print a complete or partial map of the output file.  */
  2710.  
  2711. void
  2712. print_symbols (outfile)
  2713.      FILE *outfile;
  2714. {
  2715.   register int i;
  2716.  
  2717.   fprintf (outfile, "\nFiles:\n\n");
  2718.  
  2719.   each_file (describe_file_sections, outfile);
  2720.  
  2721.   fprintf (outfile, "\nGlobal symbols:\n\n");
  2722.  
  2723.   for (i = 0; i < TABSIZE; i++)
  2724.     {
  2725.       register symbol *sp;
  2726.       for (sp = symtab[i]; sp; sp = sp->link)
  2727.     {
  2728.       if (sp->defined == 1)
  2729.         fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  2730.       if (sp->defined)
  2731.         fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
  2732.       else if (sp->referenced)
  2733.         fprintf (outfile, "  %s: undefined\n", sp->name);
  2734.     }
  2735.     }
  2736.  
  2737.   each_file (list_file_locals, outfile);
  2738. }
  2739.  
  2740. void
  2741. describe_file_sections (entry, outfile)
  2742.      struct file_entry *entry;
  2743.      FILE *outfile;
  2744. {
  2745.   fprintf (outfile, "  ");
  2746.   print_file_name (entry, outfile);
  2747.   if (entry->just_syms_flag)
  2748.     fprintf (outfile, " symbols only\n", 0);
  2749.   else
  2750.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2751.          entry->text_start_address, entry->header.a_text,
  2752.          entry->data_start_address, entry->header.a_data,
  2753.          entry->bss_start_address, entry->header.a_bss);
  2754. }
  2755.  
  2756. void
  2757. list_file_locals (entry, outfile)
  2758.      struct file_entry *entry;
  2759.      FILE *outfile;
  2760. {
  2761.   register struct nlist
  2762.     *p,
  2763.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2764.  
  2765.   entry->strings = (char *) alloca (entry->string_size);
  2766.   read_entry_strings (file_open (entry), entry);
  2767.  
  2768.   fprintf (outfile, "\nLocal symbols of ");
  2769.   print_file_name (entry, outfile);
  2770.   fprintf (outfile, ":\n\n");
  2771.  
  2772.   for (p = entry->symbols; p < end; p++)
  2773.     /* If this is a definition,
  2774.        update it if necessary by this file's start address.  */
  2775.     if (!(p->n_type & (N_STAB | N_EXT)))
  2776.       fprintf (outfile, "  %s: 0x%x\n",
  2777.            entry->strings + p->n_un.n_strx, p->n_value);
  2778.  
  2779.   entry->strings = 0;        /* All done with them.  */
  2780. }
  2781.  
  2782.  
  2783. /* Static vars for do_warnings and subroutines of it */
  2784. int list_unresolved_refs;    /* List unresolved refs */
  2785. int list_warning_symbols;    /* List warning syms */
  2786. int list_multiple_defs;        /* List multiple definitions */
  2787.  
  2788. /*
  2789.  * Structure for communication between do_file_warnings and it's
  2790.  * helper routines.  Will in practice be an array of three of these:
  2791.  * 0) Current line, 1) Next line, 2) Source file info.
  2792.  */
  2793. struct line_debug_entry
  2794. {
  2795.   int line;
  2796.   char *filename;
  2797.   struct nlist *sym;
  2798. };
  2799.  
  2800. void qsort ();
  2801. /*
  2802.  * Helper routines for do_file_warnings.
  2803.  */
  2804.  
  2805. /* Return an integer less than, equal to, or greater than 0 as per the
  2806.    relation between the two relocation entries.  Used by qsort.  */
  2807.  
  2808. int
  2809. relocation_entries_relation (rel1, rel2)
  2810.      struct relocation_info *rel1, *rel2;
  2811. {
  2812.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  2813. }
  2814.  
  2815. /* Moves to the next debugging symbol in the file.  USE_DATA_SYMBOLS
  2816.    determines the type of the debugging symbol to look for (DSLINE or
  2817.    SLINE).  STATE_POINTER keeps track of the old and new locatiosn in
  2818.    the file.  It assumes that state_pointer[1] is valid; ie
  2819.    that it.sym points into some entry in the symbol table.  If
  2820.    state_pointer[1].sym == 0, this routine should not be called.  */
  2821.  
  2822. int
  2823. next_debug_entry (use_data_symbols, state_pointer)
  2824.      register int use_data_symbols;
  2825.      /* Next must be passed by reference! */
  2826.      struct line_debug_entry state_pointer[3];
  2827. {
  2828.   register struct line_debug_entry
  2829.     *current = state_pointer,
  2830.     *next = state_pointer + 1,
  2831.     /* Used to store source file */
  2832.     *source = state_pointer + 2;
  2833.   struct file_entry *entry = (struct file_entry *) source->sym;
  2834.  
  2835.   current->sym = next->sym;
  2836.   current->line = next->line;
  2837.   current->filename = next->filename;
  2838.  
  2839.   while (++(next->sym) < (entry->symbols
  2840.               + entry->header.a_syms/sizeof (struct nlist)))
  2841.     {
  2842.       /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  2843.        * may look negative...therefore, must mask to low bits
  2844.        */
  2845.       switch (next->sym->n_type & 0xff) 
  2846.     {
  2847.     case N_SLINE:
  2848.       if (use_data_symbols) continue;
  2849.       next->line = next->sym->n_desc;
  2850.       return 1;
  2851.     case N_DSLINE:
  2852.       if (!use_data_symbols) continue;
  2853.       next->line = next->sym->n_desc;
  2854.       return 1;
  2855. #ifdef HAVE_SUN_STABS
  2856.     case N_EINCL:
  2857.       next->filename = source->filename;
  2858.       continue;
  2859. #endif
  2860.     case N_SO:
  2861.       source->filename = next->sym->n_un.n_strx + entry->strings;
  2862.       source->line++;
  2863. #ifdef HAVE_SUN_STABS
  2864.     case N_BINCL:
  2865. #endif
  2866.     case N_SOL:
  2867.       next->filename
  2868.         = next->sym->n_un.n_strx + entry->strings;
  2869.     default:
  2870.       continue;
  2871.     }
  2872.     }
  2873.   next->sym = (struct nlist *) 0;
  2874.   return 0;
  2875. }
  2876.  
  2877. /* Create a structure to save the state of a scan through the debug
  2878.    symbols.  USE_DATA_SYMBOLS is set if we should be scanning for
  2879.    DSLINE's instead of SLINE's.  entry is the file entry which points
  2880.    at the symbols to use.  */
  2881.  
  2882. struct line_debug_entry *
  2883. init_debug_scan (use_data_symbols, entry)
  2884.      int use_data_symbols;
  2885.      struct file_entry *entry;
  2886. {
  2887.   struct line_debug_entry
  2888.     *state_pointer
  2889.       = (struct line_debug_entry *)
  2890.     xmalloc (3 * sizeof (struct line_debug_entry));
  2891.   register struct line_debug_entry
  2892.     *current = state_pointer,
  2893.     *next = state_pointer + 1,
  2894.     *source = state_pointer + 2; /* Used to store source file */
  2895.  
  2896.   struct nlist *tmp;
  2897.  
  2898.   for (tmp = entry->symbols;
  2899.        tmp < (entry->symbols
  2900.           + entry->header.a_syms/sizeof (struct nlist));
  2901.        tmp++)
  2902.     if (tmp->n_type == (int) N_SO)
  2903.       break;
  2904.  
  2905.   if (tmp >= (entry->symbols
  2906.           + entry->header.a_syms/sizeof (struct nlist)))
  2907.     {
  2908.       /* I believe this translates to "We lose" */
  2909.       current->filename = next->filename = entry->filename;
  2910.       current->line = next->line = -1;
  2911.       current->sym = next->sym = (struct nlist *) 0;
  2912.       return state_pointer;
  2913.     }
  2914.  
  2915.   next->line = source->line = 0;
  2916.   next->filename = source->filename
  2917.     = (tmp->n_un.n_strx + entry->strings);
  2918.   source->sym = (struct nlist *) entry;
  2919.   next->sym = tmp;
  2920.  
  2921.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  2922.  
  2923.   if (!next->sym)        /* No line numbers for this section; */
  2924.                 /* setup output results as appropriate */
  2925.     {
  2926.       if (source->line)
  2927.     {
  2928.       current->filename = source->filename = entry->filename;
  2929.       current->line = -1;    /* Don't print lineno */
  2930.     }
  2931.       else
  2932.     {
  2933.       current->filename = source->filename;
  2934.       current->line = 0;
  2935.     }
  2936.       return state_pointer;
  2937.     }
  2938.  
  2939.  
  2940.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  2941.  
  2942.   return state_pointer;
  2943. }
  2944.  
  2945. /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER
  2946.    which describes the current location in the implied scan through
  2947.    the debug symbols within the file which ADDRESS is within, and
  2948.    returns the source line number which corresponds to ADDRESS.  */
  2949.   
  2950. int
  2951. address_to_line (address, state_pointer)
  2952.      unsigned long address;
  2953.      /* Next must be passed by reference! */
  2954.      struct line_debug_entry state_pointer[3];
  2955. {
  2956.   struct line_debug_entry
  2957.     *current = state_pointer,
  2958.     *next = state_pointer + 1;
  2959.   struct line_debug_entry *tmp_pointer;
  2960.  
  2961.   int use_data_symbols;
  2962.  
  2963.   if (next->sym)
  2964.     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  2965.   else
  2966.     return current->line;
  2967.  
  2968.   /* Go back to the beginning if we've already passed it.  */
  2969.   if (current->sym->n_value > address)
  2970.     {
  2971.       tmp_pointer = init_debug_scan (use_data_symbols,
  2972.                      (struct file_entry *)
  2973.                      ((state_pointer + 2)->sym));
  2974.       state_pointer[0] = tmp_pointer[0];
  2975.       state_pointer[1] = tmp_pointer[1];
  2976.       state_pointer[2] = tmp_pointer[2];
  2977.       free (tmp_pointer);
  2978.     }
  2979.  
  2980.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  2981.   if (current->sym->n_value > address)
  2982.     return -1;
  2983.  
  2984.   while (next->sym
  2985.      && next->sym->n_value <= address
  2986.      && next_debug_entry (use_data_symbols, state_pointer))
  2987.     ;
  2988.   return current->line;
  2989. }
  2990.  
  2991.  
  2992. /* Macros for manipulating bitvectors.  */
  2993. #define    BIT_SET_P(bv, index)    ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
  2994. #define    SET_BIT(bv, index)    ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
  2995.  
  2996. /* This routine will scan through the relocation data of file ENTRY,
  2997.    printing out references to undefined symbols and references to
  2998.    symbols defined in files with N_WARNING symbols.  If DATA_SEGMENT
  2999.    is non-zero, it will scan the data relocation segment (and use
  3000.    N_DSLINE symbols to track line number); otherwise it will scan the
  3001.    text relocation segment.  Warnings will be printed on the output
  3002.    stream OUTFILE.  Eventually, every nlist symbol mapped through will
  3003.    be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when
  3004.    we scan the nlists themselves.  */
  3005.  
  3006. do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector)
  3007.      struct file_entry *entry;
  3008.      int data_segment;
  3009.      FILE *outfile;
  3010.      unsigned char *nlist_bitvector;
  3011. {
  3012.   struct relocation_info
  3013.     *reloc_start = data_segment ? entry->datarel : entry->textrel,
  3014.     *reloc;
  3015.   int reloc_size
  3016.     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  3017.        / sizeof (struct relocation_info));
  3018.   int start_of_segment
  3019.     = (data_segment ? entry->data_start_address : entry->text_start_address);
  3020.   struct nlist *start_of_syms = entry->symbols;
  3021.   struct line_debug_entry *state_pointer
  3022.     = init_debug_scan (data_segment != 0, entry);
  3023.   register struct line_debug_entry
  3024.     *current = state_pointer,
  3025.     *next = state_pointer + 1,
  3026.     *source = state_pointer + 2;
  3027.   /* Assigned to generally static values; should not be written into.  */
  3028.   char *errfmt;
  3029.   /* Assigned to alloca'd values cand copied into; should be freed
  3030.      when done.  */
  3031.   char *errmsg;
  3032.   int invalidate_line_number;
  3033.  
  3034.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3035.      for one lousy error optimization. */
  3036.  
  3037.   qsort (reloc_start, reloc_size, sizeof (struct relocation_info),
  3038.      relocation_entries_relation);
  3039.  
  3040.   for (reloc = reloc_start;
  3041.        reloc < (reloc_start + reloc_size);
  3042.        reloc++)
  3043.     {
  3044.       register struct nlist *s;
  3045.       register symbol *g;
  3046.  
  3047.       /* If the relocation isn't resolved through a symbol, continue */
  3048.       if (!RELOC_EXTERN_P(reloc))
  3049.     continue;
  3050.  
  3051.       s = &(entry->symbols[RELOC_SYMBOL(reloc)]);
  3052.  
  3053.       /* Local symbols shouldn't ever be used by relocation info, so
  3054.      the next should be safe.
  3055.      This is, of course, wrong.  References to local BSS symbols can be
  3056.      the targets of relocation info, and they can (must) be
  3057.      resolved through symbols.  However, these must be defined properly,
  3058.      (the assembler would have caught it otherwise), so we can
  3059.      ignore these cases.  */
  3060.       if (!(s->n_type & N_EXT))
  3061.     continue;
  3062.  
  3063.       g = (symbol *) s->n_un.n_name;
  3064.       errmsg = 0;
  3065.  
  3066.       if (!g->defined && list_unresolved_refs) /* Reference */
  3067.     {
  3068.       /* Mark as being noted by relocation warning pass.  */
  3069.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3070.       
  3071.       if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3072.         continue;
  3073.  
  3074.       /* Undefined symbol which we should mention */
  3075.  
  3076.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3077.         {
  3078.           errfmt = "More undefined symbol %s refs follow";
  3079.           invalidate_line_number = 1;
  3080.         }
  3081.       else
  3082.         {
  3083.           errfmt = "Undefined symbol %s referenced from %s segment";
  3084.           invalidate_line_number = 0;
  3085.         }
  3086.     }
  3087.       else                         /* Defined */
  3088.     {
  3089.       /* Potential symbol warning here */
  3090.       if (!g->warning) continue;
  3091.  
  3092.       /* Mark as being noted by relocation warning pass.  */
  3093.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3094.       
  3095.       errfmt = 0;
  3096.       errmsg = g->warning;
  3097.       invalidate_line_number = 0;
  3098.     }
  3099.       
  3100.  
  3101.       /* If errfmt == 0, errmsg has already been defined.  */
  3102.       if (errfmt != 0)
  3103.     {
  3104.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  3105.       sprintf (errmsg, errfmt, g->name, data_segment ? "data" : "text");
  3106.     }
  3107.  
  3108.       address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  3109.                state_pointer);
  3110.  
  3111.       if (current->line >=0)
  3112.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3113.          invalidate_line_number ? 0 : current->line, errmsg);
  3114.       else
  3115.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3116.  
  3117.       if (errfmt != 0)
  3118.     free (errmsg);
  3119.     }
  3120.  
  3121.   free (state_pointer);
  3122. }
  3123.  
  3124. /* Print on OUTFILE a list of all warnings generated by references
  3125.    and/or definitions in the file ENTRY.  List source file and line
  3126.    number if possible, just the .o file if not. */
  3127.  
  3128. void
  3129. do_file_warnings (entry, outfile)
  3130.      struct file_entry *entry;
  3131.      FILE *outfile;
  3132. {
  3133.   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  3134.   unsigned char *nlist_bitvector
  3135.     = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  3136.   struct line_debug_entry *text_scan, *data_scan;
  3137.   int i;
  3138.   char *errfmt, *file_name;
  3139.   int line_number;
  3140.   int dont_allow_symbol_name;
  3141.  
  3142.   bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
  3143.  
  3144.   /* Read in the files strings if they aren't available */
  3145.   if (!entry->strings)
  3146.     {
  3147.       int desc;
  3148.  
  3149.       entry->strings = (char *) alloca (entry->string_size);
  3150.       desc = file_open (entry);
  3151.       read_entry_strings (desc, entry);
  3152.     }
  3153.  
  3154.   read_file_relocation (entry);
  3155.  
  3156.   /* Do text warnings based on a scan through the relocation info.  */
  3157.   do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
  3158.  
  3159.   /* Do data warnings based on a scan through the relocation info.  */
  3160.   do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
  3161.  
  3162.   /* Scan through all of the nlist entries in this file and pick up
  3163.      anything that the scan through the relocation stuff didn't.  */
  3164.  
  3165.   text_scan = init_debug_scan (0, entry);
  3166.   data_scan = init_debug_scan (1, entry);
  3167.  
  3168.   for (i = 0; i < number_of_syms; i++)
  3169.     {
  3170.       struct nlist *s;
  3171.       struct glosym *g;
  3172.  
  3173.       s = entry->symbols + i;
  3174.  
  3175.       if (!(s->n_type & N_EXT))
  3176.     continue;
  3177.  
  3178.       g = (symbol *) s->n_un.n_name;
  3179.       dont_allow_symbol_name = 0;
  3180.  
  3181.       if (list_multiple_defs && g->multiply_defined)
  3182.     {
  3183.       errfmt = "Definition of symbol %s (multiply defined)";
  3184.       switch (s->n_type)
  3185.         {
  3186.         case N_TEXT | N_EXT:
  3187.           line_number = address_to_line (s->n_value, text_scan);
  3188.           file_name = text_scan[0].filename;
  3189.           break;
  3190.         case N_DATA | N_EXT:
  3191.           line_number = address_to_line (s->n_value, data_scan);
  3192.           file_name = data_scan[0].filename;
  3193.           break;
  3194.         case N_SETA | N_EXT:
  3195.         case N_SETT | N_EXT:
  3196.         case N_SETD | N_EXT:
  3197.         case N_SETB | N_EXT:
  3198.           if (g->multiply_defined == 2)
  3199.         continue;
  3200.           errfmt = "First set element definition of symbol %s (multiply defined)";
  3201.           break;
  3202.         default:
  3203.           continue;        /* Don't print out multiple defs
  3204.                    at references.  */
  3205.         }
  3206.     }
  3207.       else if (BIT_SET_P (nlist_bitvector, i))
  3208.     continue;
  3209.       else if (list_unresolved_refs && !g->defined)
  3210.     {
  3211.       if (g->undef_refs >= MAX_UREFS_PRINTED)
  3212.         continue;
  3213.       
  3214.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3215.         errfmt = "More undefined \"%s\" refs follow";
  3216.       else
  3217.         errfmt = "Undefined symbol \"%s\" referenced";
  3218.       line_number = -1;
  3219.     }
  3220.       else if (g->warning)
  3221.     {
  3222.       /* There are two cases in which we don't want to
  3223.          do this.  The first is if this is a definition instead of
  3224.          a reference.  The second is if it's the reference used by
  3225.          the warning stabs itself.  */
  3226.       if (s->n_type != (N_EXT | N_UNDF)
  3227.           || (i && (s-1)->n_type == N_WARNING))
  3228.         continue;
  3229.  
  3230.       errfmt = g->warning;
  3231.       line_number = -1;
  3232.       dont_allow_symbol_name = 1;
  3233.     }
  3234.       else
  3235.     continue;
  3236.       
  3237.       if (line_number == -1)
  3238.     fprintf (outfile, "%s: ", entry->filename);
  3239.       else
  3240.     fprintf (outfile, "%s:%d: ", file_name, line_number);
  3241.  
  3242.       if (dont_allow_symbol_name)
  3243.     fprintf (outfile, "%s", errfmt);
  3244.       else
  3245.     fprintf (outfile, errfmt, g->name);
  3246.  
  3247.       fputc ('\n', outfile);
  3248.     }
  3249.   free (text_scan);
  3250.   free (data_scan);
  3251.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  3252. }
  3253.  
  3254. do_warnings (outfile)
  3255.      FILE *outfile;
  3256. {
  3257.   int i;
  3258.  
  3259.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3260.   list_warning_symbols = warning_count;
  3261.   list_multiple_defs = multiple_def_count != 0;
  3262.  
  3263.   if (!(list_unresolved_refs ||
  3264.     list_warning_symbols ||
  3265.     list_multiple_defs      ))
  3266.     /* No need to run this routine */
  3267.     return;
  3268.  
  3269.   each_file (do_file_warnings, outfile);
  3270.  
  3271.   if (list_unresolved_refs || list_multiple_defs)
  3272.     make_executable = 0;
  3273. }
  3274.  
  3275. /* Write the output file */
  3276.  
  3277. void
  3278. write_output ()
  3279. {
  3280.   struct stat statbuf;
  3281.   int filemode;
  3282.  
  3283.   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  3284.   if (outdesc < 0) perror_name (output_filename);
  3285.  
  3286.   if (fstat (outdesc, &statbuf) < 0)
  3287.     perror_name (output_filename);
  3288.  
  3289.   filemode = statbuf.st_mode;
  3290.  
  3291.   chmod (output_filename, filemode & ~0111);
  3292.  
  3293.   /* Output the a.out header.  */
  3294.   write_header ();
  3295.  
  3296.   /* Output the text and data segments, relocating as we go.  */
  3297.   write_text ();
  3298.   write_data ();
  3299.  
  3300.   /* Output the merged relocation info, if requested with `-r'.  */
  3301.   if (relocatable_output)
  3302.     write_rel ();
  3303.  
  3304.   /* Output the symbol table (both globals and locals).  */
  3305.   write_syms ();
  3306.  
  3307.   /* Copy any GDB symbol segments from input files.  */
  3308.   write_symsegs ();
  3309.  
  3310.   close (outdesc);
  3311.  
  3312.   if (chmod (output_filename, filemode | 0111) == -1)
  3313.     perror_name (output_filename);
  3314. }
  3315.  
  3316. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3317.  
  3318. void
  3319. write_header ()
  3320. {
  3321.   N_SET_MAGIC (outheader, magic);
  3322.   outheader.a_text = text_size;
  3323. #ifdef sequent
  3324.   outheader.a_text += N_ADDRADJ (outheader);
  3325. #endif
  3326.   outheader.a_data = data_size;
  3327.   outheader.a_bss = bss_size;
  3328.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3329.                : text_start + entry_offset);
  3330. #ifdef COFF_ENCAPSULATE
  3331.   if (need_coff_header)
  3332.     {
  3333.       /* We are encapsulating BSD format within COFF format.  */
  3334.       struct coffscn *tp, *dp, *bp;
  3335.  
  3336.       tp = &coffheader.scns[0];
  3337.       dp = &coffheader.scns[1];
  3338.       bp = &coffheader.scns[2];
  3339.  
  3340.       strcpy (tp->s_name, ".text");
  3341.       tp->s_paddr = text_start;
  3342.       tp->s_vaddr = text_start;
  3343.       tp->s_size = text_size;
  3344.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3345.       tp->s_relptr = 0;
  3346.       tp->s_lnnoptr = 0;
  3347.       tp->s_nreloc = 0;
  3348.       tp->s_nlnno = 0;
  3349.       tp->s_flags = 0x20;
  3350.       strcpy (dp->s_name, ".data");
  3351.       dp->s_paddr = data_start;
  3352.       dp->s_vaddr = data_start;
  3353.       dp->s_size = data_size;
  3354.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  3355.       dp->s_relptr = 0;
  3356.       dp->s_lnnoptr = 0;
  3357.       dp->s_nreloc = 0;
  3358.       dp->s_nlnno = 0;
  3359.       dp->s_flags = 0x40;
  3360.       strcpy (bp->s_name, ".bss");
  3361.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  3362.       bp->s_vaddr = bp->s_paddr;
  3363.       bp->s_size = bss_size;
  3364.       bp->s_scnptr = 0;
  3365.       bp->s_relptr = 0;
  3366.       bp->s_lnnoptr = 0;
  3367.       bp->s_nreloc = 0;
  3368.       bp->s_nlnno = 0;
  3369.       bp->s_flags = 0x80;
  3370.  
  3371.       coffheader.f_magic = COFF_MAGIC;
  3372.       coffheader.f_nscns = 3;
  3373.       /* store an unlikely time so programs can
  3374.        * tell that there is a bsd header
  3375.        */
  3376.       coffheader.f_timdat = 1;
  3377.       coffheader.f_symptr = 0;
  3378.       coffheader.f_nsyms = 0;
  3379.       coffheader.f_opthdr = 28;
  3380.       coffheader.f_flags = 0x103;
  3381.       /* aouthdr */
  3382.       coffheader.magic = ZMAGIC;
  3383.       coffheader.vstamp = 0;
  3384.       coffheader.tsize = tp->s_size;
  3385.       coffheader.dsize = dp->s_size;
  3386.       coffheader.bsize = bp->s_size;
  3387.       coffheader.entry = outheader.a_entry;
  3388.       coffheader.text_start = tp->s_vaddr;
  3389.       coffheader.data_start = dp->s_vaddr;
  3390.     }
  3391. #endif
  3392.  
  3393. #ifdef INITIALIZE_HEADER
  3394.   INITIALIZE_HEADER;
  3395. #endif
  3396.  
  3397.   if (strip_symbols == STRIP_ALL)
  3398.     nsyms = 0;
  3399.   else
  3400.     {
  3401.       nsyms = (defined_global_sym_count
  3402.            + undefined_global_sym_count);
  3403.       if (discard_locals == DISCARD_L)
  3404.     nsyms += non_L_local_sym_count;
  3405.       else if (discard_locals == DISCARD_NONE)
  3406.     nsyms += local_sym_count;
  3407.       /* One extra for following reference on indirects */
  3408.       if (relocatable_output)
  3409.     nsyms += set_symbol_count + global_indirect_count;
  3410.     }
  3411.  
  3412.   if (strip_symbols == STRIP_NONE)
  3413.     nsyms += debugger_sym_count;
  3414.  
  3415.   outheader.a_syms = nsyms * sizeof (struct nlist);
  3416.  
  3417.   if (relocatable_output)
  3418.     {
  3419.       outheader.a_trsize = text_reloc_size;
  3420.       outheader.a_drsize = data_reloc_size;
  3421.     }
  3422.   else
  3423.     {
  3424.       outheader.a_trsize = 0;
  3425.       outheader.a_drsize = 0;
  3426.     }
  3427.  
  3428. #ifdef COFF_ENCAPSULATE
  3429.   if (need_coff_header)
  3430.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  3431. #endif
  3432.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  3433.  
  3434.   /* Output whatever padding is required in the executable file
  3435.      between the header and the start of the text.  */
  3436.  
  3437. #ifndef COFF_ENCAPSULATE
  3438.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  3439. #endif
  3440. }
  3441.  
  3442. /* Relocate the text segment of each input file
  3443.    and write to the output file.  */
  3444.  
  3445. void
  3446. write_text ()
  3447. {
  3448.   if (trace_files)
  3449.     fprintf (stderr, "Copying and relocating text:\n\n");
  3450.  
  3451.   each_full_file (copy_text);
  3452.   file_close ();
  3453.  
  3454.   if (trace_files)
  3455.     fprintf (stderr, "\n");
  3456.  
  3457.   padfile (text_pad, outdesc);
  3458. }
  3459.  
  3460. int
  3461. text_offset (entry)
  3462.      struct file_entry *entry;
  3463. {
  3464.   return entry->starting_offset + N_TXTOFF (entry->header);
  3465. }
  3466.  
  3467. /* Read in all of the relocation information */
  3468.  
  3469. void
  3470. read_relocation ()
  3471. {
  3472.   each_full_file (read_file_relocation);
  3473. }
  3474.  
  3475. /* Read in the relocation sections of ENTRY if necessary */
  3476.  
  3477. void
  3478. read_file_relocation (entry)
  3479.      struct file_entry *entry;
  3480. {
  3481.   register struct relocation_info *reloc;
  3482.   int desc;
  3483.   int read_return;
  3484.  
  3485.   desc = -1;
  3486.   if (!entry->textrel)
  3487.     {
  3488.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  3489.       desc = file_open (entry);
  3490.       lseek (desc,
  3491.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  3492.          L_SET);
  3493.       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  3494.     {
  3495.       fprintf (stderr, "Return from read: %d\n", read_return);
  3496.       fatal_with_file ("premature eof in text relocation of ", entry);
  3497.     }
  3498.       entry->textrel = reloc;
  3499.     }
  3500.  
  3501.   if (!entry->datarel)
  3502.     {
  3503.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  3504.       if (desc == -1) desc = file_open (entry);
  3505.       lseek (desc,
  3506.          text_offset (entry) + entry->header.a_text
  3507.          + entry->header.a_data + entry->header.a_trsize,
  3508.          L_SET);
  3509.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3510.     fatal_with_file ("premature eof in data relocation of ", entry);
  3511.       entry->datarel = reloc;
  3512.     }
  3513. }
  3514.  
  3515. /* Read the text segment contents of ENTRY, relocate them,
  3516.    and write the result to the output file.
  3517.    If `-r', save the text relocation for later reuse.  */
  3518.  
  3519. void
  3520. copy_text (entry)
  3521.      struct file_entry *entry;
  3522. {
  3523.   register char *bytes;
  3524.   register int desc;
  3525.   register struct relocation_info *reloc;
  3526.  
  3527.   if (trace_files)
  3528.     prline_file_name (entry, stderr);
  3529.  
  3530.   desc = file_open (entry);
  3531.  
  3532.   /* Allocate space for the file's text section */
  3533.  
  3534.   bytes = (char *) alloca (entry->header.a_text);
  3535.  
  3536.   /* Deal with relocation information however is appropriate */
  3537.  
  3538.   if (entry->textrel)  reloc = entry->textrel;
  3539.   else if (relocatable_output)
  3540.     {
  3541.       read_file_relocation (entry);
  3542.       reloc = entry->textrel;
  3543.     }
  3544.   else
  3545.     {
  3546.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  3547.       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  3548.       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  3549.     fatal_with_file ("premature eof in text relocation of ", entry);
  3550.     }
  3551.  
  3552.   /* Read the text section into core.  */
  3553.  
  3554.   lseek (desc, text_offset (entry), 0);
  3555.   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  3556.     fatal_with_file ("premature eof in text section of ", entry);
  3557.  
  3558.  
  3559.   /* Relocate the text according to the text relocation.  */
  3560.  
  3561.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  3562.               reloc, entry->header.a_trsize, entry);
  3563.  
  3564.   /* Write the relocated text to the output file.  */
  3565.  
  3566.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  3567. }
  3568.  
  3569. /* Relocate the data segment of each input file
  3570.    and write to the output file.  */
  3571.  
  3572. void
  3573. write_data ()
  3574. {
  3575.   if (trace_files)
  3576.     fprintf (stderr, "Copying and relocating data:\n\n");
  3577.  
  3578.   each_full_file (copy_data);
  3579.   file_close ();
  3580.  
  3581.   /* Write out the set element vectors.  See digest symbols for
  3582.      description of length of the set vector section.  */
  3583.  
  3584.   if (set_vector_count)
  3585.     mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
  3586.          sizeof (unsigned long), outdesc);
  3587.  
  3588.   if (trace_files)
  3589.     fprintf (stderr, "\n");
  3590.  
  3591.   padfile (data_pad, outdesc);
  3592. }
  3593.  
  3594. /* Read the data segment contents of ENTRY, relocate them,
  3595.    and write the result to the output file.
  3596.    If `-r', save the data relocation for later reuse.
  3597.    See comments in `copy_text'.  */
  3598.  
  3599. void
  3600. copy_data (entry)
  3601.      struct file_entry *entry;
  3602. {
  3603.   register struct relocation_info *reloc;
  3604.   register char *bytes;
  3605.   register int desc;
  3606.  
  3607.   if (trace_files)
  3608.     prline_file_name (entry, stderr);
  3609.  
  3610.   desc = file_open (entry);
  3611.  
  3612.   bytes = (char *) alloca (entry->header.a_data);
  3613.  
  3614.   if (entry->datarel) reloc = entry->datarel;
  3615.   else if (relocatable_output)    /* Will need this again */
  3616.     {
  3617.       read_file_relocation (entry);
  3618.       reloc = entry->datarel;
  3619.     }
  3620.   else
  3621.     {
  3622.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  3623.       lseek (desc, text_offset (entry) + entry->header.a_text
  3624.          + entry->header.a_data + entry->header.a_trsize,
  3625.          0);
  3626.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3627.     fatal_with_file ("premature eof in data relocation of ", entry);
  3628.     }
  3629.  
  3630.   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  3631.   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  3632.     fatal_with_file ("premature eof in data section of ", entry);
  3633.  
  3634.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  3635.               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  3636.  
  3637.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  3638. }
  3639.  
  3640. /* Relocate ENTRY's text or data section contents.
  3641.    DATA is the address of the contents, in core.
  3642.    DATA_SIZE is the length of the contents.
  3643.    PC_RELOCATION is the difference between the address of the contents
  3644.      in the output file and its address in the input file.
  3645.    RELOC_INFO is the address of the relocation info, in core.
  3646.    RELOC_SIZE is its length in bytes.  */
  3647. /* This version is about to be severley hacked by Randy.  Hope it
  3648.    works afterwards. */
  3649. void
  3650. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  3651.      char *data;
  3652.      struct relocation_info *reloc_info;
  3653.      struct file_entry *entry;
  3654.      int pc_relocation;
  3655.      int data_size;
  3656.      int reloc_size;
  3657. {
  3658.   register struct relocation_info *p = reloc_info;
  3659.   struct relocation_info *end
  3660.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  3661.   int text_relocation = entry->text_start_address;
  3662.   int data_relocation = entry->data_start_address - entry->header.a_text;
  3663.   int bss_relocation
  3664.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  3665.  
  3666.   for (; p < end; p++)
  3667.     {
  3668.       register int relocation = 0;
  3669.       register int addr = RELOC_ADDRESS(p);
  3670.       register unsigned int mask = 0;
  3671.  
  3672.       if (addr >= data_size)
  3673.     fatal_with_file ("relocation address out of range in ", entry);
  3674.  
  3675.       if (RELOC_EXTERN_P(p))
  3676.     {
  3677.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  3678.       symbol *sp = ((symbol *)
  3679.             (((struct nlist *)
  3680.               (((char *)entry->symbols) + symindex))
  3681.              ->n_un.n_name));
  3682.  
  3683. #ifdef N_INDR
  3684.       /* Resolve indirection */
  3685.       if ((sp->defined & ~N_EXT) == N_INDR)
  3686.         sp = (symbol *) sp->value;
  3687. #endif
  3688.  
  3689.       if (symindex >= entry->header.a_syms)
  3690.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3691.  
  3692.       /* If the symbol is undefined, leave it at zero.  */
  3693.       if (! sp->defined)
  3694.         relocation = 0;
  3695.       else
  3696.         relocation = sp->value;
  3697.     }
  3698.       else switch (RELOC_TYPE(p))
  3699.     {
  3700.     case N_TEXT:
  3701.     case N_TEXT | N_EXT:
  3702.       relocation = text_relocation;
  3703.       break;
  3704.  
  3705.     case N_DATA:
  3706.     case N_DATA | N_EXT:
  3707.       /* A word that points to beginning of the the data section
  3708.          initially contains not 0 but rather the "address" of that section
  3709.          in the input file, which is the length of the file's text.  */
  3710.       relocation = data_relocation;
  3711.       break;
  3712.  
  3713.     case N_BSS:
  3714.     case N_BSS | N_EXT:
  3715.       /* Similarly, an input word pointing to the beginning of the bss
  3716.          initially contains the length of text plus data of the file.  */
  3717.       relocation = bss_relocation;
  3718.       break;
  3719.  
  3720.     case N_ABS:
  3721.     case N_ABS | N_EXT:
  3722.       /* Don't know why this code would occur, but apparently it does.  */
  3723.       break;
  3724.  
  3725.     default:
  3726.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  3727.     }
  3728.  
  3729.       if (RELOC_PCREL_P(p))
  3730.     relocation -= pc_relocation;
  3731.  
  3732. #ifdef RELOC_ADD_EXTRA
  3733.       relocation += RELOC_ADD_EXTRA(p);
  3734.       if (relocatable_output)
  3735.     {
  3736.       /* If this RELOC_ADD_EXTRA is 0, it means that the
  3737.          symbol was external and the relocation does not
  3738.          need a fixup here.  */
  3739.       if (RELOC_ADD_EXTRA (p))
  3740.         {
  3741.           if (! RELOC_PCREL_P (p))
  3742.         RELOC_ADD_EXTRA (p) = relocation;
  3743.           else
  3744.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3745.         }
  3746. #if 0
  3747.       if (! RELOC_PCREL_P (p))
  3748.         {
  3749.           if ((int)p->r_type <= RELOC_32
  3750.           || RELOC_EXTERN_P (p) == 0)
  3751.         RELOC_ADD_EXTRA (p) = relocation;
  3752.         }
  3753.       else if (RELOC_EXTERN_P (p))
  3754.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3755. #endif
  3756.       continue;
  3757.     }
  3758. #endif
  3759.  
  3760.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  3761.  
  3762.       /* Unshifted mask for relocation */
  3763.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  3764.       mask |= mask - 1;
  3765.       relocation &= mask;
  3766.  
  3767.       /* Shift everything up to where it's going to be used */
  3768.       relocation <<= RELOC_TARGET_BITPOS(p);
  3769.       mask <<= RELOC_TARGET_BITPOS(p);
  3770.  
  3771.       switch (RELOC_TARGET_SIZE(p))
  3772.     {
  3773.     case 0:
  3774.       if (RELOC_MEMORY_SUB_P(p))
  3775.         relocation -= mask & *(char *) (data + addr);
  3776.       else if (RELOC_MEMORY_ADD_P(p))
  3777.         relocation += mask & *(char *) (data + addr);
  3778.       *(char *) (data + addr) &= ~mask;
  3779.       *(char *) (data + addr) |= relocation;
  3780.       break;
  3781.  
  3782.     case 1:
  3783.       if (RELOC_MEMORY_SUB_P(p))
  3784.         relocation -= mask & *(short *) (data + addr);
  3785.       else if (RELOC_MEMORY_ADD_P(p))
  3786.         relocation += mask & *(short *) (data + addr);
  3787.       *(short *) (data + addr) &= ~mask;
  3788.       *(short *) (data + addr) |= relocation;
  3789.       break;
  3790.  
  3791.     case 2:
  3792.       if (RELOC_MEMORY_SUB_P(p))
  3793.         relocation -= mask & *(long *) (data + addr);
  3794.       else if (RELOC_MEMORY_ADD_P(p))
  3795.         relocation += mask & *(long *) (data + addr);
  3796.       *(long *) (data + addr) &= ~mask;
  3797.       *(long *) (data + addr) |= relocation;
  3798.       break;
  3799.  
  3800.     default:
  3801.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  3802.     }
  3803.     }
  3804. }
  3805.  
  3806. /* For relocatable_output only: write out the relocation,
  3807.    relocating the addresses-to-be-relocated.  */
  3808.  
  3809. void coptxtrel (), copdatrel ();
  3810.  
  3811. void
  3812. write_rel ()
  3813. {
  3814.   register int i;
  3815.   register int count = 0;
  3816.  
  3817.   if (trace_files)
  3818.     fprintf (stderr, "Writing text relocation:\n\n");
  3819.  
  3820.   /* Assign each global symbol a sequence number, giving the order
  3821.      in which `write_syms' will write it.
  3822.      This is so we can store the proper symbolnum fields
  3823.      in relocation entries we write.  */
  3824.  
  3825.   for (i = 0; i < TABSIZE; i++)
  3826.     {
  3827.       symbol *sp;
  3828.       for (sp = symtab[i]; sp; sp = sp->link)
  3829.     if (sp->referenced || sp->defined)
  3830.       {
  3831.         sp->def_count = count++;
  3832.         /* Leave room for the reference required by N_INDR, if
  3833.            necessary.  */
  3834.         if ((sp->defined & ~N_EXT) == N_INDR)
  3835.           count++;
  3836.       }
  3837.     }
  3838.   /* Correct, because if (reloatable_output), we will also be writing
  3839.      whatever indirect blocks we have.  */
  3840.   if (count != defined_global_sym_count
  3841.       + undefined_global_sym_count + global_indirect_count)
  3842.     fatal ("internal error");
  3843.  
  3844.   /* Write out the relocations of all files, remembered from copy_text.  */
  3845.  
  3846.   each_full_file (coptxtrel);
  3847.  
  3848.   if (trace_files)
  3849.     fprintf (stderr, "\nWriting data relocation:\n\n");
  3850.  
  3851.   each_full_file (copdatrel);
  3852.  
  3853.   if (trace_files)
  3854.     fprintf (stderr, "\n");
  3855. }
  3856.  
  3857. void
  3858. coptxtrel (entry)
  3859.      struct file_entry *entry;
  3860. {
  3861.   register struct relocation_info *p, *end;
  3862.   register int reloc = entry->text_start_address;
  3863.  
  3864.   p = entry->textrel;
  3865.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  3866.   while (p < end)
  3867.     {
  3868.       RELOC_ADDRESS(p) += reloc;
  3869.       if (RELOC_EXTERN_P(p))
  3870.     {
  3871.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  3872.       symbol *symptr = ((symbol *)
  3873.                 (((struct nlist *)
  3874.                   (((char *)entry->symbols) + symindex))
  3875.                  ->n_un.n_name));
  3876.  
  3877.       if (symindex >= entry->header.a_syms)
  3878.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3879.  
  3880. #ifdef N_INDR
  3881.       /* Resolve indirection.  */
  3882.       if ((symptr->defined & ~N_EXT) == N_INDR)
  3883.         symptr = (symbol *) symptr->value;
  3884. #endif
  3885.  
  3886.       /* If the symbol is now defined, change the external relocation
  3887.          to an internal one.  */
  3888.  
  3889.       if (symptr->defined)
  3890.         {
  3891.           RELOC_EXTERN_P(p) = 0;
  3892.           RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  3893. #ifdef RELOC_ADD_EXTRA
  3894.           /* If we aren't going to be adding in the value in
  3895.              memory on the next pass of the loader, then we need
  3896.          to add it in from the relocation entry.  Otherwise
  3897.              the work we did in this pass is lost.  */
  3898.           if (!RELOC_MEMORY_ADD_P(p))
  3899.         RELOC_ADD_EXTRA (p) += symptr->value;
  3900. #endif
  3901.         }
  3902.       else
  3903.         /* Debugger symbols come first, so have to start this
  3904.            after them.  */
  3905.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  3906.                  - defined_global_sym_count
  3907.                  - undefined_global_sym_count
  3908.                  - global_indirect_count);
  3909.     }
  3910.       p++;
  3911.     }
  3912.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  3913. }
  3914.  
  3915. void
  3916. copdatrel (entry)
  3917.      struct file_entry *entry;
  3918. {
  3919.   register struct relocation_info *p, *end;
  3920.   /* Relocate the address of the relocation.
  3921.      Old address is relative to start of the input file's data section.
  3922.      New address is relative to start of the output file's data section.  */
  3923.   register int reloc = entry->data_start_address - text_size;
  3924.  
  3925.   p = entry->datarel;
  3926.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  3927.   while (p < end)
  3928.     {
  3929.       RELOC_ADDRESS(p) += reloc;
  3930.       if (RELOC_EXTERN_P(p))
  3931.     {
  3932.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  3933.       symbol *symptr = ((symbol *)
  3934.                 (((struct nlist *)
  3935.                   (((char *)entry->symbols) + symindex))
  3936.                  ->n_un.n_name));
  3937.       int symtype;
  3938.  
  3939.       if (symindex >= entry->header.a_syms)
  3940.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3941.  
  3942. #ifdef N_INDR
  3943.       /* Resolve indirection.  */
  3944.       if ((symptr->defined & ~N_EXT) == N_INDR)
  3945.         symptr = (symbol *) symptr->value;
  3946. #endif
  3947.  
  3948.        symtype = symptr->defined & N_TYPE;
  3949.  
  3950.       if (force_common_definition
  3951.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  3952.         {
  3953.           RELOC_EXTERN_P(p) = 0;
  3954.           RELOC_SYMBOL(p) = symtype;
  3955.         }
  3956.       else
  3957.         /* Debugger symbols come first, so have to start this
  3958.            after them.  */
  3959.         RELOC_SYMBOL(p)
  3960.           = (((symbol *)
  3961.           (((struct nlist *)
  3962.             (((char *)entry->symbols) + symindex))
  3963.            ->n_un.n_name))
  3964.          ->def_count
  3965.          + nsyms - defined_global_sym_count
  3966.          - undefined_global_sym_count
  3967.          - global_indirect_count);
  3968.     }
  3969.       p++;
  3970.     }
  3971.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  3972. }
  3973.  
  3974. void write_file_syms ();
  3975. void write_string_table ();
  3976.  
  3977. /* Offsets and current lengths of symbol and string tables in output file. */
  3978.  
  3979. int symbol_table_offset;
  3980. int symbol_table_len;
  3981.  
  3982. /* Address in output file where string table starts.  */
  3983. int string_table_offset;
  3984.  
  3985. /* Offset within string table
  3986.    where the strings in `strtab_vector' should be written.  */
  3987. int string_table_len;
  3988.  
  3989. /* Total size of string table strings allocated so far,
  3990.    including strings in `strtab_vector'.  */
  3991. int strtab_size;
  3992.  
  3993. /* Vector whose elements are strings to be added to the string table.  */
  3994. char **strtab_vector;
  3995.  
  3996. /* Vector whose elements are the lengths of those strings.  */
  3997. int *strtab_lens;
  3998.  
  3999. /* Index in `strtab_vector' at which the next string will be stored.  */
  4000. int strtab_index;
  4001.  
  4002. /* Add the string NAME to the output file string table.
  4003.    Record it in `strtab_vector' to be output later.
  4004.    Return the index within the string table that this string will have.  */
  4005.  
  4006. int
  4007. assign_string_table_index (name)
  4008.      char *name;
  4009. {
  4010.   register int index = strtab_size;
  4011.   register int len = strlen (name) + 1;
  4012.  
  4013.   strtab_size += len;
  4014.   strtab_vector[strtab_index] = name;
  4015.   strtab_lens[strtab_index++] = len;
  4016.  
  4017.   return index;
  4018. }
  4019.  
  4020. FILE *outstream = (FILE *) 0;
  4021.  
  4022. /* Write the contents of `strtab_vector' into the string table.
  4023.    This is done once for each file's local&debugger symbols
  4024.    and once for the global symbols.  */
  4025.  
  4026. void
  4027. write_string_table ()
  4028. {
  4029.   register int i;
  4030.  
  4031.   lseek (outdesc, string_table_offset + string_table_len, 0);
  4032.  
  4033. #ifdef TOWNS
  4034.   if (!outstream)
  4035.     outstream = fdopen (outdesc, "wb");
  4036. #else
  4037.   if (!outstream)
  4038.     outstream = fdopen (outdesc, "w");
  4039. #endif
  4040.  
  4041.   for (i = 0; i < strtab_index; i++)
  4042.     {
  4043.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4044.       string_table_len += strtab_lens[i];
  4045.     }
  4046.  
  4047.   fflush (outstream);
  4048.  
  4049.   /* Report I/O error such as disk full.  */
  4050.   if (ferror (outstream))
  4051.     perror_name (output_filename);
  4052. }
  4053.  
  4054. /* Write the symbol table and string table of the output file.  */
  4055.  
  4056. void
  4057. write_syms ()
  4058. {
  4059.   /* Number of symbols written so far.  */
  4060.   int syms_written = 0;
  4061.   register int i;
  4062.   register symbol *sp;
  4063.  
  4064.   /* Buffer big enough for all the global symbols.  One
  4065.      extra struct for each indirect symbol to hold the extra reference
  4066.      following. */
  4067.   struct nlist *buf
  4068.     = (struct nlist *) alloca ((defined_global_sym_count
  4069.                 + undefined_global_sym_count
  4070.                 + global_indirect_count)
  4071.                    * sizeof (struct nlist));
  4072.   /* Pointer for storing into BUF.  */
  4073.   register struct nlist *bufp = buf;
  4074.  
  4075.   /* Size of string table includes the bytes that store the size.  */
  4076.   strtab_size = sizeof strtab_size;
  4077.  
  4078.   symbol_table_offset = N_SYMOFF (outheader);
  4079.   symbol_table_len = 0;
  4080.   string_table_offset = N_STROFF (outheader);
  4081.   string_table_len = strtab_size;
  4082.  
  4083.   if (strip_symbols == STRIP_ALL)
  4084.     return;
  4085.  
  4086.   /* Write the local symbols defined by the various files.  */
  4087.  
  4088.   each_file (write_file_syms, &syms_written);
  4089.   file_close ();
  4090.  
  4091.   /* Now write out the global symbols.  */
  4092.  
  4093.   /* Allocate two vectors that record the data to generate the string
  4094.      table from the global symbols written so far.  This must include
  4095.      extra space for the references following indirect outputs. */
  4096.  
  4097.   strtab_vector = (char **) alloca ((num_hash_tab_syms
  4098.                      + global_indirect_count) * sizeof (char *));
  4099.   strtab_lens = (int *) alloca ((num_hash_tab_syms
  4100.                  + global_indirect_count) * sizeof (int));
  4101.   strtab_index = 0;
  4102.  
  4103.   /* Scan the symbol hash table, bucket by bucket.  */
  4104.  
  4105.   for (i = 0; i < TABSIZE; i++)
  4106.     for (sp = symtab[i]; sp; sp = sp->link)
  4107.       {
  4108.     struct nlist nl;
  4109.  
  4110.     nl.n_other = 0;
  4111.     nl.n_desc = 0;
  4112.  
  4113.     /* Compute a `struct nlist' for the symbol.  */
  4114.  
  4115.     if (sp->defined || sp->referenced)
  4116.       {
  4117.         /* common condition needs to be before undefined condition */
  4118.         /* because unallocated commons are set undefined in */
  4119.         /* digest_symbols */
  4120.         if (sp->defined > 1) /* defined with known type */
  4121.           {
  4122.         /* If the target of an indirect symbol has been
  4123.            defined and we are outputting an executable,
  4124.            resolve the indirection; it's no longer needed */
  4125.         if (!relocatable_output
  4126.             && ((sp->defined & N_TYPE) == N_INDR)
  4127.             && (((symbol *) sp->value)->defined > 1))
  4128.           {
  4129.             symbol *newsp = (symbol *) sp->value;
  4130.             nl.n_type = newsp->defined;
  4131.             nl.n_value = newsp->value;
  4132.           }
  4133.         else
  4134.           {
  4135.             nl.n_type = sp->defined;
  4136.             if (sp->defined != (N_INDR | N_EXT))
  4137.               nl.n_value = sp->value;
  4138.             else
  4139.               nl.n_value = 0;
  4140.           }
  4141.           }
  4142.         else if (sp->max_common_size) /* defined as common but not allocated. */
  4143.           {
  4144.         /* happens only with -r and not -d */
  4145.         /* write out a common definition */
  4146.         nl.n_type = N_UNDF | N_EXT;
  4147.         nl.n_value = sp->max_common_size;
  4148.           }
  4149.         else if (!sp->defined)          /* undefined -- legit only if -r */
  4150.           {
  4151.         nl.n_type = N_UNDF | N_EXT;
  4152.         nl.n_value = 0;
  4153.           }
  4154.         else
  4155.           fatal ("internal error: %s defined in mysterious way", sp->name);
  4156.  
  4157.         /* Allocate string table space for the symbol name.  */
  4158.  
  4159.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  4160.  
  4161.         /* Output to the buffer and count it.  */
  4162.  
  4163.         *bufp++ = nl;
  4164.         syms_written++;
  4165.         if (nl.n_type == (N_INDR | N_EXT))
  4166.           {
  4167.         struct nlist xtra_ref;
  4168.         xtra_ref.n_type == N_EXT | N_UNDF;
  4169.         xtra_ref.n_un.n_strx
  4170.           = assign_string_table_index (((symbol *) sp->value)->name);
  4171.         xtra_ref.n_other = 0;
  4172.         xtra_ref.n_desc = 0;
  4173.         xtra_ref.n_value = 0;
  4174.         *bufp++ = xtra_ref;
  4175.         syms_written++;
  4176.           }
  4177.       }
  4178.       }
  4179.  
  4180.   /* Output the buffer full of `struct nlist's.  */
  4181.  
  4182.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4183.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4184.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4185.  
  4186.   if (syms_written != nsyms)
  4187.     fatal ("internal error: wrong number of symbols written into output file", 0);
  4188.  
  4189.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  4190.     fatal ("internal error: inconsistent symbol table length", 0);
  4191.  
  4192.   /* Now the total string table size is known, so write it.
  4193.      We are already positioned at the right place in the file.  */
  4194.  
  4195.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  4196.  
  4197.   /* Write the strings for the global symbols.  */
  4198.  
  4199.   write_string_table ();
  4200. }
  4201.  
  4202. /* Write the local and debugger symbols of file ENTRY.
  4203.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  4204.  
  4205. /* Note that we do not combine identical names of local symbols.
  4206.    dbx or gdb would be confused if we did that.  */
  4207.  
  4208. void
  4209. write_file_syms (entry, syms_written_addr)
  4210.      struct file_entry *entry;
  4211.      int *syms_written_addr;
  4212. {
  4213.   register struct nlist *p = entry->symbols;
  4214.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  4215.  
  4216.   /* Buffer to accumulate all the syms before writing them.
  4217.      It has one extra slot for the local symbol we generate here.  */
  4218.   struct nlist *buf
  4219.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  4220.   register struct nlist *bufp = buf;
  4221.  
  4222.   /* Upper bound on number of syms to be written here.  */
  4223.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  4224.  
  4225.   /* Make tables that record, for each symbol, its name and its name's length.
  4226.      The elements are filled in by `assign_string_table_index'.  */
  4227.  
  4228.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  4229.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  4230.   strtab_index = 0;
  4231.  
  4232.   /* Generate a local symbol for the start of this file's text.  */
  4233.  
  4234.   if (discard_locals != DISCARD_ALL)
  4235.     {
  4236.       struct nlist nl;
  4237.  
  4238.       nl.n_type = N_TEXT;
  4239.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  4240.       nl.n_value = entry->text_start_address;
  4241.       nl.n_desc = 0;
  4242.       nl.n_other = 0;
  4243.       *bufp++ = nl;
  4244.       (*syms_written_addr)++;
  4245.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  4246.     }
  4247.  
  4248.   /* Read the file's string table.  */
  4249.  
  4250.   entry->strings = (char *) alloca (entry->string_size);
  4251.   read_entry_strings (file_open (entry), entry);
  4252.  
  4253.   for (; p < end; p++)
  4254.     {
  4255.       register int type = p->n_type;
  4256.       register int write = 0;
  4257.  
  4258.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  4259.  
  4260.  
  4261.       if (SET_ELEMENT_P (type))    /* This occurs even if global.  These */
  4262.                 /* types of symbols are never written */
  4263.                 /* globally, though they are stored */
  4264.                 /* globally.  */
  4265.         write = relocatable_output;
  4266.       else if (!(type & (N_STAB | N_EXT)))
  4267.         /* ordinary local symbol */
  4268.     write = ((discard_locals != DISCARD_ALL)
  4269.          && !(discard_locals == DISCARD_L &&
  4270.               (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
  4271.          && type != N_WARNING);
  4272.       else if (!(type & N_EXT))
  4273.     /* debugger symbol */
  4274.         write = (strip_symbols == STRIP_NONE);
  4275.  
  4276.       if (write)
  4277.     {
  4278.       /* If this symbol has a name,
  4279.          allocate space for it in the output string table.  */
  4280.  
  4281.       if (p->n_un.n_strx)
  4282.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  4283.                             + entry->strings);
  4284.  
  4285.       /* Output this symbol to the buffer and count it.  */
  4286.  
  4287.       *bufp++ = *p;
  4288.       (*syms_written_addr)++;
  4289.     }
  4290.     }
  4291.  
  4292.   /* All the symbols are now in BUF; write them.  */
  4293.  
  4294.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4295.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4296.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4297.  
  4298.   /* Write the string-table data for the symbols just written,
  4299.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4300.  
  4301.   write_string_table ();
  4302.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  4303. }
  4304.  
  4305. /* Copy any GDB symbol segments from the input files to the output file.
  4306.    The contents of the symbol segment is copied without change
  4307.    except that we store some information into the beginning of it.  */
  4308.  
  4309. void write_file_symseg ();
  4310.  
  4311. void
  4312. write_symsegs ()
  4313. {
  4314.   each_file (write_file_symseg, 0);
  4315. }
  4316.  
  4317. void
  4318. write_file_symseg (entry)
  4319.      struct file_entry *entry;
  4320. {
  4321.   char buffer[4096];
  4322.   struct symbol_root root;
  4323.   int indesc;
  4324.   int len;
  4325.  
  4326.   if (entry->symseg_offset == 0)
  4327.     return;
  4328.  
  4329.   /* This entry has a symbol segment.  Read the root of the segment.  */
  4330.  
  4331.   indesc = file_open (entry);
  4332.   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  4333.   if (sizeof root != read (indesc, &root, sizeof root))
  4334.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  4335.  
  4336.   /* Store some relocation info into the root.  */
  4337.  
  4338.   root.ldsymoff = entry->local_syms_offset;
  4339.   root.textrel = entry->text_start_address;
  4340.   root.datarel = entry->data_start_address - entry->header.a_text;
  4341.   root.bssrel = entry->bss_start_address
  4342.     - entry->header.a_text - entry->header.a_data;
  4343.   root.databeg = entry->data_start_address - root.datarel;
  4344.   root.bssbeg = entry->bss_start_address - root.bssrel;
  4345.  
  4346.   /* Write the modified root into the output file.  */
  4347.  
  4348.   mywrite (&root, sizeof root, 1, outdesc);
  4349.  
  4350.   /* Copy the rest of the symbol segment unchanged.  */
  4351.  
  4352.   if (entry->superfile)
  4353.     {
  4354.       /* Library member: number of bytes to copy is determined
  4355.      from the member's total size.  */
  4356.  
  4357.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  4358.  
  4359.       while (total > 0)
  4360.     {
  4361.       len = read (indesc, buffer, min (sizeof buffer, total));
  4362.  
  4363.       if (len != min (sizeof buffer, total))
  4364.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  4365.       total -= len;
  4366.       mywrite (buffer, len, 1, outdesc);
  4367.     }
  4368.     }
  4369.   else
  4370.     {
  4371.       /* A separate file: copy until end of file.  */
  4372.  
  4373.       while (len = read (indesc, buffer, sizeof buffer))
  4374.     {
  4375.       mywrite (buffer, len, 1, outdesc);
  4376.       if (len < sizeof buffer)
  4377.         break;
  4378.     }
  4379.     }
  4380.  
  4381.   file_close ();
  4382. }
  4383.  
  4384. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4385.  
  4386. void
  4387. symtab_init ()
  4388. {
  4389. #ifndef nounderscore
  4390.   edata_symbol = getsym ("_edata");
  4391.   etext_symbol = getsym ("_etext");
  4392.   end_symbol = getsym ("_end");
  4393. #else
  4394.   edata_symbol = getsym ("edata");
  4395.   etext_symbol = getsym ("etext");
  4396.   end_symbol = getsym ("end");
  4397. #endif
  4398.  
  4399. #ifdef sun
  4400.   {
  4401.     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  4402.     dynamic_symbol->defined = N_ABS | N_EXT;
  4403.     dynamic_symbol->referenced = 1;
  4404.     dynamic_symbol->value = 0;
  4405.   }
  4406. #endif
  4407. #ifdef sequent
  4408.   {
  4409.     symbol *_387_flt_symbol = getsym ("_387_flt");
  4410.     _387_flt_symbol->defined = N_ABS | N_EXT;
  4411.     _387_flt_symbol->referenced = 1;
  4412.     _387_flt_symbol->value = 0;
  4413.   }
  4414. #endif
  4415.  
  4416.   edata_symbol->defined = N_DATA | N_EXT;
  4417.   etext_symbol->defined = N_TEXT | N_EXT;
  4418.   end_symbol->defined = N_BSS | N_EXT;
  4419.  
  4420.   edata_symbol->referenced = 1;
  4421.   etext_symbol->referenced = 1;
  4422.   end_symbol->referenced = 1;
  4423. }
  4424.  
  4425. /* Compute the hash code for symbol name KEY.  */
  4426.  
  4427. int
  4428. hash_string (key)
  4429.      char *key;
  4430. {
  4431.   register char *cp;
  4432.   register int k;
  4433.  
  4434.   cp = key;
  4435.   k = 0;
  4436.   while (*cp)
  4437.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  4438.  
  4439.   return k;
  4440. }
  4441.  
  4442. /* Get the symbol table entry for the global symbol named KEY.
  4443.    Create one if there is none.  */
  4444.  
  4445. symbol *
  4446. getsym (key)
  4447.      char *key;
  4448. {
  4449.   register int hashval;
  4450.   register symbol *bp;
  4451.  
  4452.   /* Determine the proper bucket.  */
  4453.  
  4454.   hashval = hash_string (key) % TABSIZE;
  4455.  
  4456.   /* Search the bucket.  */
  4457.  
  4458.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4459.     if (! strcmp (key, bp->name))
  4460.       return bp;
  4461.  
  4462.   /* Nothing was found; create a new symbol table entry.  */
  4463.  
  4464.   bp = (symbol *) xmalloc (sizeof (symbol));
  4465.   bp->refs = 0;
  4466.   bp->name = (char *) xmalloc (strlen (key) + 1);
  4467.   strcpy (bp->name, key);
  4468.   bp->defined = 0;
  4469.   bp->referenced = 0;
  4470.   bp->trace = 0;
  4471.   bp->value = 0;
  4472.   bp->max_common_size = 0;
  4473.   bp->warning = 0;
  4474.   bp->undef_refs = 0;
  4475.   bp->multiply_defined = 0;
  4476.  
  4477.   /* Add the entry to the bucket.  */
  4478.  
  4479.   bp->link = symtab[hashval];
  4480.   symtab[hashval] = bp;
  4481.  
  4482.   ++num_hash_tab_syms;
  4483.  
  4484.   return bp;
  4485. }
  4486.  
  4487. /* Like `getsym' but return 0 if the symbol is not already known.  */
  4488.  
  4489. symbol *
  4490. getsym_soft (key)
  4491.      char *key;
  4492. {
  4493.   register int hashval;
  4494.   register symbol *bp;
  4495.  
  4496.   /* Determine which bucket.  */
  4497.  
  4498.   hashval = hash_string (key) % TABSIZE;
  4499.  
  4500.   /* Search the bucket.  */
  4501.  
  4502.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4503.     if (! strcmp (key, bp->name))
  4504.       return bp;
  4505.  
  4506.   return 0;
  4507. }
  4508.  
  4509. /* Report a fatal error.
  4510.    STRING is a printf format string and ARG is one arg for it.  */
  4511.  
  4512. void
  4513. fatal (string, arg)
  4514.      char *string, *arg;
  4515. {
  4516.   fprintf (stderr, "ld: ");
  4517.   fprintf (stderr, string, arg);
  4518.   fprintf (stderr, "\n");
  4519.   exit (1);
  4520. }
  4521.  
  4522. /* Report a fatal error.  The error message is STRING
  4523.    followed by the filename of ENTRY.  */
  4524.  
  4525. void
  4526. fatal_with_file (string, entry)
  4527.      char *string;
  4528.      struct file_entry *entry;
  4529. {
  4530.   fprintf (stderr, "ld: ");
  4531.   fprintf (stderr, string);
  4532.   print_file_name (entry, stderr);
  4533.   fprintf (stderr, "\n");
  4534.   exit (1);
  4535. }
  4536.  
  4537. /* Report a fatal error using the message for the last failed system call,
  4538.    followed by the string NAME.  */
  4539.  
  4540. void
  4541. perror_name (name)
  4542.      char *name;
  4543. {
  4544.   extern int errno, sys_nerr;
  4545.   extern char *sys_errlist[];
  4546.   char *s;
  4547.  
  4548.   if (errno < sys_nerr)
  4549.     s = concat ("", sys_errlist[errno], " for %s");
  4550.   else
  4551.     s = "cannot open %s";
  4552.   fatal (s, name);
  4553. }
  4554.  
  4555. /* Report a fatal error using the message for the last failed system call,
  4556.    followed by the name of file ENTRY.  */
  4557.  
  4558. void
  4559. perror_file (entry)
  4560.      struct file_entry *entry;
  4561. {
  4562.   extern int errno, sys_nerr;
  4563.   extern char *sys_errlist[];
  4564.   char *s;
  4565.  
  4566.   if (errno < sys_nerr)
  4567.     s = concat ("", sys_errlist[errno], " for ");
  4568.   else
  4569.     s = "cannot open ";
  4570.   fatal_with_file (s, entry);
  4571. }
  4572.  
  4573. /* Report a nonfatal error.
  4574.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  4575.  
  4576. void
  4577. error (string, arg1, arg2, arg3)
  4578.      char *string, *arg1, *arg2, *arg3;
  4579. {
  4580.   fprintf (stderr, "%s: ", progname);
  4581.   fprintf (stderr, string, arg1, arg2, arg3);
  4582.   fprintf (stderr, "\n");
  4583. }
  4584.  
  4585.  
  4586. /* Output COUNT*ELTSIZE bytes of data at BUF
  4587.    to the descriptor DESC.  */
  4588.  
  4589. void
  4590. mywrite (buf, count, eltsize, desc)
  4591.      char *buf;
  4592.      int count;
  4593.      int eltsize;
  4594.      int desc;
  4595. {
  4596.   register int val;
  4597.   register int bytes = count * eltsize;
  4598.  
  4599.   while (bytes > 0)
  4600.     {
  4601.       val = write (desc, buf, bytes);
  4602.       if (val <= 0)
  4603.     perror_name (output_filename);
  4604.       buf += val;
  4605.       bytes -= val;
  4606.     }
  4607. }
  4608.  
  4609. /* Output PADDING zero-bytes to descriptor OUTDESC.
  4610.    PADDING may be negative; in that case, do nothing.  */
  4611.  
  4612. void
  4613. padfile (padding, outdesc)
  4614.      int padding;
  4615.      int outdesc;
  4616. {
  4617.   register char *buf;
  4618.   if (padding <= 0)
  4619.     return;
  4620.  
  4621.   buf = (char *) alloca (padding);
  4622.   bzero (buf, padding);
  4623.   mywrite (buf, padding, 1, outdesc);
  4624. }
  4625.  
  4626. /* Return a newly-allocated string
  4627.    whose contents concatenate the strings S1, S2, S3.  */
  4628.  
  4629. char *
  4630. concat (s1, s2, s3)
  4631.      char *s1, *s2, *s3;
  4632. {
  4633.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  4634.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  4635.  
  4636.   strcpy (result, s1);
  4637.   strcpy (result + len1, s2);
  4638.   strcpy (result + len1 + len2, s3);
  4639.   result[len1 + len2 + len3] = 0;
  4640.  
  4641.   return result;
  4642. }
  4643.  
  4644. /* Parse the string ARG using scanf format FORMAT, and return the result.
  4645.    If it does not parse, report fatal error
  4646.    generating the error message using format string ERROR and ARG as arg.  */
  4647.  
  4648. int
  4649. parse (arg, format, error)
  4650.      char *arg, *format;
  4651. {
  4652.   int x;
  4653.   if (1 != sscanf (arg, format, &x))
  4654.     fatal (error, arg);
  4655.   return x;
  4656. }
  4657.  
  4658. /* Like malloc but get fatal error if memory is exhausted.  */
  4659.  
  4660. int
  4661. xmalloc (size)
  4662.      int size;
  4663. {
  4664.   register int result = malloc (size);
  4665.   if (!result)
  4666.     fatal ("virtual memory exhausted", 0);
  4667.   return result;
  4668. }
  4669.  
  4670. /* Like realloc but get fatal error if memory is exhausted.  */
  4671.  
  4672. int
  4673. xrealloc (ptr, size)
  4674.      char *ptr;
  4675.      int size;
  4676. {
  4677.   register int result = realloc (ptr, size);
  4678.   if (!result)
  4679.     fatal ("virtual memory exhausted", 0);
  4680.   return result;
  4681. }
  4682.  
  4683. #ifdef USG
  4684.  
  4685. void
  4686. bzero (p, n)
  4687.      char *p;
  4688. {
  4689.   memset (p, 0, n);
  4690. }
  4691.  
  4692. void
  4693. bcopy (from, to, n)
  4694.      char *from, *to;
  4695. {
  4696.   memcpy (to, from, n);
  4697. }
  4698.  
  4699. getpagesize ()
  4700. {
  4701.   return (4096);
  4702. }
  4703.  
  4704. #endif
  4705.