home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / utlsrc33.lzh / UTLSRC33 / LD.C < prev    next >
C/C++ Source or Header  |  1993-07-30  |  179KB  |  6,411 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. #ifdef BYTE_SWAP
  22.  
  23. #define SWAP4(y) (((unsigned)(y)>>24) + (((unsigned)(y)>>8)&0xff00) + \
  24.          (((unsigned)(y)<<8)&0xff0000) + ((unsigned)(y)<<24)) 
  25. #define SWAP2(y) ((((unsigned)(y)&0xff00)>>8) + (((unsigned)(y)&0x00ff)<<8))
  26.  
  27. #endif /* BYTE_SWAP */
  28.  
  29. #ifdef CROSSATARI
  30. #  ifdef atarist
  31. #    undef atarist
  32. #  endif
  33. #  ifdef atariminix
  34. #    undef atariminix
  35. #  endif
  36. #endif
  37.  
  38. #include "PatchLev.h"
  39.  
  40. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  41.  
  42. #include <ar.h>
  43. #include <stdio.h>
  44. #include <sys/types.h>
  45. #include <sys/stat.h>
  46. #include <sys/file.h>
  47. #include <fcntl.h>
  48.  
  49. #ifdef COFF_ENCAPSULATE
  50. #include "a.out.encap.h"
  51. #else
  52. #include <a.out.h>
  53. #endif
  54.  
  55. #else
  56.  
  57. #ifdef CROSSATARI
  58. #  include "gnu-out.h"
  59. #  include "gnu-ar.h"
  60. #  ifdef MINIX
  61. #    include "minix-out.h"
  62. #  else
  63. #    include "st-out.h"
  64. #  endif /* MINIX */
  65. #  include <stdio.h>
  66. #  include <sys/types.h>
  67. #  include <string.h>
  68. #  include <sys/stat.h>
  69. #  include <sys/file.h>
  70. #  include <ctype.h>
  71. #  ifndef __GNUC__
  72. #    include <time.h>
  73. #  endif
  74. #else
  75. #  ifdef atariminix
  76. #    include <stdio.h>
  77. #    include <sys/types.h>
  78. #    include <string.h>
  79. #    include <sys/stat.h>
  80. #    include <ctype.h>
  81. #    if 0
  82.             /* if you are *not* using a kernel with */
  83.             /* simon poole fs patches use this      */
  84. #      define EMUL_OPEN3
  85. #      include "open3.h"
  86. #    else
  87. #      include <fcntl.h>
  88. #    endif
  89. #    include "gnu-out.h"
  90. #    include "minix-out.h"
  91. #    include "gnu-ar.h"    /* we use the Gnu ar for minix, not minix ar */
  92. #  else /* atarist */
  93. #    include "gnu-out.h"
  94. #    include <st-out.h>
  95. #    include "gnu-ar.h"
  96. #    include <stdio.h>
  97. #    include <types.h>
  98. #    include <string.h>
  99. #    include <stat.h>
  100. #    include <file.h>
  101. #    include <ctype.h>
  102. #    include <unistd.h>
  103. #  endif
  104. #endif        /* atarist */
  105. #endif
  106.  
  107. /* If compiled with GNU C, use the built-in alloca */
  108. #ifdef __GNUC__
  109. #  ifndef atariminix
  110. #    define alloca __builtin_alloca
  111. #  endif
  112. #endif
  113.  
  114. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  115.  
  116. #include "stab.h"
  117. #define CORE_ADDR unsigned long    /* For symseg.h */
  118. #include "symseg.h"
  119.  
  120. #ifdef USG
  121. #include <string.h>
  122. #else
  123. #include <strings.h>
  124. #endif
  125.  
  126. #if __STDC__
  127. #  include <stdarg.h>
  128. #else
  129. #  include <varargs.h>
  130. #endif
  131.  
  132. /* Determine whether we should attempt to handle (minimally)
  133.    N_BINCL and N_EINCL.  */
  134.  
  135. #if (!(defined(atarist) || defined(atariminix) || defined(CROSSATARI)))
  136. #if defined (__GNU_STAB__) || defined (N_BINCL)
  137. #define HAVE_SUN_STABS
  138. #endif
  139. #endif
  140.  
  141. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  142. /* stuff used when emitting reloc info here */
  143.  
  144. #define RBUF_SIZE 1024
  145.  
  146. #ifdef CROSSATARI
  147. unsigned char rbuf[RBUF_SIZE];
  148. #else
  149. char rbuf[RBUF_SIZE];
  150. #endif
  151. int rbuf_size = 0;        /* how many bytes are in there now */
  152. long rbuf_last_pc = -1;        /* last rel pc; -1 means none yet */
  153.  
  154. #ifdef atarist        /* and a stack */
  155. long _stksize = -1L;
  156. #endif
  157. #endif
  158.  
  159. #ifdef min
  160. #undef min
  161. #endif
  162. #define min(a,b) ((a) < (b) ? (a) : (b))
  163.  
  164. /* Macro to control the number of undefined references printed */
  165. #define MAX_UREFS_PRINTED    10
  166.  
  167. /* Size of a page; obtained from the operating system.  */
  168.  
  169. int page_size;
  170.  
  171. /* Name this program was invoked by.  */
  172.  
  173. char *progname;
  174.  
  175. /* System dependencies */
  176.  
  177. /* Define this if names etext, edata and end should not start with `_'.  */
  178. /* #define nounderscore 1 */
  179.  
  180. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  181.    whose native format is different.  */
  182. /* #define NON_NATIVE */
  183.  
  184. /* Define this to specify the default executable format.  */
  185.  
  186. #ifndef CROSSHPUX
  187. #ifdef hpux
  188. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  189. #endif
  190. #endif
  191.  
  192. #ifndef DEFAULT_MAGIC
  193. #define DEFAULT_MAGIC ZMAGIC
  194. #endif
  195.  
  196. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  197.  
  198. #ifndef N_TXTADDR
  199. #ifdef vax
  200. #define N_TXTADDR(X) 0
  201. #endif
  202. #ifdef is68k
  203. #define N_TXTADDR(x)  (sizeof (struct exec))
  204. #endif
  205. #endif
  206.  
  207. #ifndef N_DATADDR
  208. #ifdef vax
  209. #define N_DATADDR(x) \
  210.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  211.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  212. #endif
  213. #ifdef is68k
  214. #define SEGMENT_SIZE 0x20000
  215. #define N_DATADDR(x) \
  216.     (((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
  217.      : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  218. #endif
  219. #endif
  220.  
  221. #ifndef CROSSHPUX
  222. #ifdef hpux
  223. #define getpagesize() EXEC_PAGESIZE
  224. #endif
  225. #endif
  226.  
  227. #if (defined(atarist) || defined(atariminix) || defined(CROSSATARI))
  228. /* kludgerama, not really used */
  229. #define getpagesize() 2
  230. #endif
  231.  
  232. /* Define how to initialize system-dependent header fields.  */
  233. #ifndef CROSSATARI
  234. #ifdef sun
  235. #ifdef sparc
  236. #define INITIALIZE_HEADER \
  237.   {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  238. #endif
  239. #if defined(mc68010) || defined(m68010) || (TARGET == SUN2)
  240. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  241. #endif
  242. #ifndef INITIALIZE_HEADER
  243. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  244. #endif
  245. #endif
  246. #ifdef is68k
  247. #ifdef M_68020
  248. /* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
  249.    a_machtype field and so won't recognize the magic number.  To keep
  250.    binary compatibility for now, just ignore it */
  251. #define INITIALIZE_HEADER outheader.a_machtype = 0;
  252. #endif
  253. #endif
  254. #ifndef CROSSHPUX
  255. #ifdef hpux
  256. #define INITIALIZE_HEADER outheader.a_machtype = HP9000S200_ID
  257. #endif
  258. #endif
  259. #ifdef i386
  260. #define INITIALIZE_HEADER outheader.a_machtype = M_386
  261. #endif
  262. #endif /* CROSSATARAI */
  263.  
  264. #ifdef is68k
  265. /* This enables code to take care of an ugly hack in the ISI OS.
  266.    If a symbol beings with _$, then the object file is included only
  267.    if the rest of the symbol name has been referenced. */
  268. #define DOLLAR_KLUDGE
  269. #endif
  270.  
  271. /*
  272.  * Alloca include.
  273.  */
  274. #if defined(sun) && defined(sparc)
  275. #ifdef alloca
  276. #undef alloca
  277. #endif
  278. #include <alloca.h>
  279. #endif
  280.  
  281. #ifndef L_SET
  282. #define L_SET 0
  283. #endif
  284.  
  285. /*
  286.  * Ok.  Following are the relocation information macros.  If your
  287.  * system should not be able to use the default set (below), you must
  288.  * define the following:
  289.  
  290.  *   relocation_info: This must be typedef'd (or #define'd to the type
  291.  * of structure that is stored in the relocation info section of your
  292.  * a.out files.  Often this is defined in the a.out.h for your system.
  293.  *
  294.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  295.  * <whatever> to be relocated.  *Must be an lvalue*.
  296.  *
  297.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  298.  * external symbol (1), or was it fully resolved upon entering the
  299.  * loader (0) in which case some combination of the value in memory
  300.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  301.  * what the value of the relocation actually was.  *Must be an lvalue*.
  302.  *
  303.  *   RELOC_TYPE (rval): If this entry was fully resolved upon
  304.  * entering the loader, what type should it be relocated as?
  305.  *
  306.  *   RELOC_SYMBOL (rval): If this entry was not fully resolved upon
  307.  * entering the loader, what is the index of it's symbol in the symbol
  308.  * table?  *Must be a lvalue*.
  309.  *
  310.  *   RELOC_MEMORY_ADD_P (rval): This should return true if the final
  311.  * relocation value output here should be added to memory, or if the
  312.  * section of memory described should simply be set to the relocation
  313.  * value.
  314.  *
  315.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  316.  * an extra value to be added to the relocation value based on the
  317.  * individual relocation entry.
  318.  *
  319.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  320.  * pc relative.
  321.  *
  322.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  323.  * final relocation value before putting it where it belongs.
  324.  *
  325.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  326.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  327.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  328.  * do everything in terms of the bit operators below), but having this
  329.  * macro could end up producing better code on machines without fancy
  330.  * bit twiddling.  Also, it's easier to understand/code big/little
  331.  * endian distinctions with this macro.
  332.  *
  333.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  334.  * object described in RELOC_TARGET_SIZE in which the relocation value
  335.  * will go.
  336.  *
  337.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  338.  * with the bits of the relocation value.  It may be assumed by the
  339.  * code that the relocation value will fit into this many bits.  This
  340.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  341.  *
  342.  *
  343.  *        Things I haven't implemented
  344.  *        ----------------------------
  345.  *
  346.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  347.  *
  348.  *    Pc relative relocation for External references.
  349.  *
  350.  *
  351.  */
  352.  
  353. #if defined(sun) && defined(sparc) && !defined(CROSSATARI)
  354. /* Sparc (Sun 4) macros */
  355. #undef relocation_info
  356. #define relocation_info                    reloc_info_sparc
  357. #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  358. #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  359. #define RELOC_TYPE(r)                   ((r)->r_index)  
  360. #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  361. #define RELOC_MEMORY_ADD_P(r)           0                          
  362. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  363. #define RELOC_PCREL_P(r)             \
  364.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  365. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  366. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  367. #define RELOC_TARGET_BITPOS(r)          0
  368. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  369.  
  370. /* Note that these are very dependent on the order of the enums in
  371.    enum reloc_type (in a.out.h); if they change the following must be
  372.    changed */
  373. /* Also note that the last few may be incorrect; I have no information */
  374. static int reloc_target_rightshift[] = {
  375.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  376. };
  377. static int reloc_target_size[] = {
  378.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  379. };
  380. static int reloc_target_bitsize[] = {
  381.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  382. };
  383. #endif
  384.  
  385. /* Default macros */
  386. #ifndef RELOC_ADDRESS
  387. #define RELOC_ADDRESS(r)        ((r)->r_address)
  388. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  389. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  390. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  391. #define RELOC_MEMORY_ADD_P(r)    1
  392. /* #define RELOC_ADD_EXTRA(r)        0     */ /* Don't need to define */
  393. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  394. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  395. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  396. #define RELOC_TARGET_BITPOS(r)    0
  397. #define RELOC_TARGET_BITSIZE(r)    32
  398. #endif
  399. #if defined(atarist) || defined(CROSSATARI)
  400. #define RELOC_ADD_EXTRA(r)    \
  401.     ((!RELOC_PCREL_P(r) && RELOC_TARGET_SIZE(r) < 2) \
  402.         ? BASE_OFFSET-text_size : 0)
  403. #endif
  404.  
  405. /* Special global symbol types understood by GNU LD.  */
  406.  
  407. /* The following type indicates the definition of a symbol as being
  408.    an indirect reference to another symbol.  The other symbol
  409.    appears as an undefined reference, immediately following this symbol.
  410.  
  411.    Indirection is asymmetrical.  The other symbol's value will be used
  412.    to satisfy requests for the indirect symbol, but not vice versa.
  413.    If the other symbol does not have a definition, libraries will
  414.    be searched to find a definition.  */
  415. #ifndef N_INDR
  416. #define N_INDR 0xa
  417. #endif
  418.  
  419. /* The following symbols refer to set elements.  These are expected
  420.    only in input to the loader; they should not appear in loader
  421.    output (unless relocatable output is requested).  To be recognized
  422.    by the loader, the input symbols must have their N_EXT bit set.
  423.    All the N_SET[ATDB] symbols with the same name form one set.  The
  424.    loader collects all of these elements at load time and outputs a
  425.    vector for each name.
  426.    Space (an array of 32 bit words) is allocated for the set in the
  427.    data section, and the n_value field of each set element value is
  428.    stored into one word of the array.
  429.    The first word of the array is the length of the set (number of
  430.    elements).  The last word of the vector is set to zero for possible
  431.    use by incremental loaders.  The array is ordered by the linkage
  432.    order; the first symbols which the linker encounters will be first
  433.    in the array.
  434.  
  435.    In C syntax this looks like:
  436.  
  437.     struct set_vector {
  438.       unsigned int length;
  439.       unsigned int vector[length];
  440.       unsigned int always_zero;
  441.     };
  442.  
  443.    Before being placed into the array, each element is relocated
  444.    according to its type.  This allows the loader to create an array
  445.    of pointers to objects automatically.  N_SETA type symbols will not
  446.    be relocated.
  447.  
  448.    The address of the set is made into an N_SETV symbol
  449.    whose name is the same as the name of the set.
  450.    This symbol acts like a N_DATA global symbol
  451.    in that it can satisfy undefined external references.
  452.  
  453.    For the purposes of determining whether or not to load in a library
  454.    file, set element definitions are not considered "real
  455.    definitions"; they will not cause the loading of a library
  456.    member.
  457.  
  458.    If relocatable output is requested, none of this processing is
  459.    done.  The symbols are simply relocated and passed through to the
  460.    output file.
  461.  
  462.    So, for example, the following three lines of assembler code
  463.    (whether in one file or scattered between several different ones)
  464.    will produce a three element vector (total length is five words;
  465.    see above), referenced by the symbol "_xyzzy", which will have the
  466.    addresses of the routines _init1, _init2, and _init3.
  467.  
  468.    *NOTE*: If symbolic addresses are used in the n_value field of the
  469.    defining .stabs, those symbols must be defined in the same file as
  470.    that containing the .stabs.
  471.  
  472.     .stabs "_xyzzy",23,0,0,_init1
  473.     .stabs "_xyzzy",23,0,0,_init2
  474.     .stabs "_xyzzy",23,0,0,_init3
  475.  
  476.    Note that (23 == (N_SETT | N_EXT)).  */
  477. #ifndef N_SETA
  478. #define    N_SETA    0x14        /* Absolute set element symbol */
  479. #endif                /* This is input to LD, in a .o file.  */
  480.  
  481. #ifndef N_SETT
  482. #define    N_SETT    0x16        /* Text set element symbol */
  483. #endif                /* This is input to LD, in a .o file.  */
  484.  
  485. #ifndef N_SETD
  486. #define    N_SETD    0x18        /* Data set element symbol */
  487. #endif                /* This is input to LD, in a .o file.  */
  488.  
  489. #ifndef N_SETB
  490. #define    N_SETB    0x1A        /* Bss set element symbol */
  491. #endif                /* This is input to LD, in a .o file.  */
  492.  
  493. /* Macros dealing with the set element symbols defined in a.out.h */
  494. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  495. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  496.  
  497. #ifndef N_SETV
  498. #define N_SETV    0x1C        /* Pointer to set vector in text area.  */
  499. #endif                /* This is output from LD.  */
  500.  
  501. /* If a this type of symbol is encountered, its name is a warning
  502.    message to print each time the symbol referenced by the next symbol
  503.    table entry is referenced.
  504.  
  505.    This feature may be used to allow backwards compatibility with
  506.    certain functions (eg. gets) but to discourage programmers from
  507.    their use.
  508.  
  509.    So if, for example, you wanted to have ld print a warning whenever
  510.    the function "gets" was used in their C program, you would add the
  511.    following to the assembler file in which gets is defined:
  512.  
  513.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  514.     .stabs "_gets",1,0,0,0
  515.  
  516.    These .stabs do not necessarily have to be in the same file as the
  517.    gets function, they simply must exist somewhere in the compilation.  */
  518. #ifndef N_WARNING
  519. #define N_WARNING 0x1E        /* Warning message to print if file included */
  520. #endif                /* This is input to ld */
  521.  
  522. #ifndef __GNU_STAB__
  523.  
  524. /* Line number for the data section.  This is to be used to describe
  525.    the source location of a variable declaration.  */
  526. #ifndef N_DSLINE
  527. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  528. #endif
  529.  
  530. /* Line number for the bss section.  This is to be used to describe
  531.    the source location of a variable declaration.  */
  532. #ifndef N_BSLINE
  533. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  534. #endif
  535.  
  536. #endif /* not __GNU_STAB__ */
  537.  
  538. /* Symbol table */
  539.  
  540. /* Global symbol data is recorded in these structures,
  541.    one for each global symbol.
  542.    They are found via hashing in 'symtab', which points to a vector of buckets.
  543.    Each bucket is a chain of these structures through the link field.  */
  544.  
  545. typedef
  546.   struct glosym
  547.     {
  548.       /* Pointer to next symbol in this symbol's hash bucket.  */
  549.       struct glosym *link;
  550.       /* Name of this symbol.  */
  551.       char *name;
  552.       /* Value of this symbol as a global symbol.  */
  553.       long value;
  554.       /* Chain of external 'nlist's in files for this symbol, both defs
  555.      and refs.  */
  556.       struct nlist *refs;
  557.       /* Any warning message that might be associated with this symbol
  558.          from an N_WARNING symbol encountered. */
  559.       char *warning;
  560.       /* Nonzero means definitions of this symbol as common have been seen,
  561.      and the value here is the largest size specified by any of them.  */
  562.       int max_common_size;
  563.       /* For relocatable_output, records the index of this global sym in the
  564.      symbol table to be written, with the first global sym given index 0.*/
  565.       int def_count;
  566.       /* Nonzero means a definition of this global symbol is known to exist.
  567.      Library members should not be loaded on its account.  */
  568.       char defined;
  569.       /* Nonzero means a reference to this global symbol has been seen
  570.      in a file that is surely being loaded.
  571.      A value higher than 1 is the n_type code for the symbol's
  572.      definition.  */
  573.       char referenced;
  574.       /* A count of the number of undefined references printed for a
  575.      specific symbol.  If a symbol is unresolved at the end of
  576.      digest_symbols (and the loading run is supposed to produce
  577.      relocatable output) do_file_warnings keeps track of how many
  578.      unresolved reference error messages have been printed for
  579.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  580.      messages stop. */
  581.       unsigned char undef_refs;
  582.       /* Nonzero means print a message at all refs or defs of this symbol */
  583.       char trace;
  584.     }
  585.   symbol;
  586.  
  587. /* Demangler for C++. */
  588. extern char *cplus_demangle ();
  589.  
  590. /* Demangler function to use. */
  591. char *(*demangler)() = cplus_demangle;
  592.  
  593. /* Number of buckets in symbol hash table */
  594. #define    TABSIZE    1009
  595.  
  596. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  597. symbol *symtab[TABSIZE];
  598.  
  599. /* Number of symbols in symbol hash table. */
  600. int num_hash_tab_syms = 0;
  601.  
  602. /* Count the number of nlist entries that are for local symbols.
  603.    This count and the three following counts
  604.    are incremented as as symbols are entered in the symbol table.  */
  605. int local_sym_count;
  606.  
  607. /* Count number of nlist entries that are for local symbols
  608.    whose names don't start with L. */
  609. int non_L_local_sym_count;
  610.  
  611. /* Count the number of nlist entries for debugger info.  */
  612. int debugger_sym_count;
  613.  
  614. /* Count the number of global symbols referenced and not defined.  */
  615. int undefined_global_sym_count;
  616.  
  617. /* Count the number of defined global symbols.
  618.    Each symbol is counted only once
  619.    regardless of how many different nlist entries refer to it,
  620.    since the output file will need only one nlist entry for it.
  621.    This count is computed by `digest_symbols';
  622.    it is undefined while symbols are being loaded. */
  623. int defined_global_sym_count;
  624.  
  625. /* Count the number of symbols defined through common declarations.
  626.    This count is kept in symdef_library, linear_library, and
  627.    enter_global_ref.  It is incremented when the defined flag is set
  628.    in a symbol because of a common definition, and decremented when
  629.    the symbol is defined "for real" (ie. by something besides a common
  630.    definition).  */
  631. int common_defined_global_count;
  632.  
  633. /* Count the number of set element type symbols and the number of
  634.    separate vectors which these symbols will fit into.  See the
  635.    GNU a.out.h for more info.
  636.    This count is computed by 'enter_file_symbols' */
  637. int set_symbol_count;
  638. int set_vector_count;
  639.  
  640. /* Count the number of definitions done indirectly (ie. done relative
  641.    to the value of some other symbol. */
  642. int global_indirect_count;
  643.  
  644. /* Count the number of warning symbols encountered. */
  645. int warning_count;
  646.  
  647. /* Total number of symbols to be written in the output file.
  648.    Computed by digest_symbols from the variables above.  */
  649. int nsyms;
  650.  
  651.  
  652. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  653.    -e sets this.  */
  654. symbol *entry_symbol;
  655.  
  656. symbol *edata_symbol;   /* the symbol _edata */
  657. symbol *etext_symbol;   /* the symbol _etext */
  658. symbol *end_symbol;    /* the symbol _end */
  659.  
  660. /* Each input file, and each library member ("subfile") being loaded,
  661.    has a `file_entry' structure for it.
  662.  
  663.    For files specified by command args, these are contained in the vector
  664.    which `file_table' points to.
  665.  
  666.    For library members, they are dynamically allocated,
  667.    and chained through the `chain' field.
  668.    The chain is found in the `subfiles' field of the `file_entry'.
  669.    The `file_entry' objects for the members have `superfile' fields pointing
  670.    to the one for the library.  */
  671.  
  672. struct file_entry {
  673.       /* Name of this file.  */
  674.   char *filename;
  675.       /* Name to use for the symbol giving address of text start */
  676.       /* Usually the same as filename, but for a file spec'd with -l
  677.          this is the -l switch itself rather than the filename.  */
  678.   char *local_sym_name;
  679.  
  680.       /* Describe the layout of the contents of the file */
  681.  
  682.       /* The file's a.out header.  */
  683.   struct exec header;
  684.       /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  685.   int symseg_offset;
  686.  
  687.       /* Describe data from the file loaded into core */
  688.  
  689.       /* Symbol table of the file.  */
  690.   struct nlist *symbols;
  691.       /* Size in bytes of string table.  */
  692.   int string_size;
  693.       /* Pointer to the string table.
  694.          The string table is not kept in core all the time,
  695.          but when it is in core, its address is here.  */
  696.   char *strings;
  697.       /* Pointer to any warning specified in this file by an N_WARNING
  698.          type symbol */
  699.   char *warning;
  700.  
  701.       /* Next two used only if `relocatable_output' or if needed for */
  702.       /* output of undefined reference line numbers. */
  703.  
  704.       /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  705.   struct relocation_info *textrel;
  706.       /* Data reloc info saved by `write_data' for `copdatrel'.  */
  707.   struct relocation_info *datarel;
  708.  
  709.       /* Relation of this file's segments to the output file */
  710.  
  711.       /* Start of this file's text seg in the output file core image.  */
  712.   int text_start_address;
  713.       /* Start of this file's data seg in the output file core image.  */
  714.   int data_start_address;
  715.       /* Start of this file's bss seg in the output file core image.  */
  716.   int bss_start_address;
  717.       /* Offset in bytes in the output file symbol table of the first
  718.          local symbol for this file.  Set by `write_file_symbols'.  */
  719.   int local_syms_offset;
  720.  
  721.       /* For library members only */
  722.  
  723.       /* For a library, points to chain of entries for the library members. */
  724.   struct file_entry *subfiles;
  725.       /* For a library member, offset of the member within the archive.
  726.          Zero for files that are not library members.  */
  727.   int starting_offset;
  728.       /* Size of contents of this file, if library member.  */
  729.   int total_size;
  730.       /* For library member, points to the library's own entry.  */
  731.   struct file_entry *superfile;
  732.       /* For library member, points to next entry for next member.  */
  733.   struct file_entry *chain;
  734.  
  735.       /* 1 if file is a library. */
  736.   char library_flag;
  737.  
  738.       /* 1 if file's header has been read into this structure.  */
  739.   char header_read_flag;
  740.  
  741.       /* 1 means search a set of directories for this file.  */
  742.   char search_dirs_flag;
  743.  
  744.       /* 1 means this is base file of incremental load.
  745.          Do not load this file's text or data.
  746.          Also default text_start to after this file's bss. */
  747.   char just_syms_flag;
  748. };
  749.  
  750. /* Vector of entries for input files specified by arguments.
  751.    These are all the input files except for members of specified libraries.  */
  752. struct file_entry *file_table;
  753.  
  754. /* Length of that vector.  */
  755. int number_of_files;
  756.  
  757. /* When loading the text and data, we can avoid doing a close
  758.    and another open between members of the same library.
  759.  
  760.    These two variables remember the file that is currently open.
  761.    Both are NULL if no file is open.
  762.  
  763.    See `each_file' and `file_close'.  */
  764.  
  765. struct file_entry *input_file;
  766. FILE *input_desc = NULL;
  767.  
  768. /* The name of the file to write; "a.out" by default.  */
  769.  
  770. char *output_filename;
  771.  
  772. /* Descriptor for writing that file with `mywrite'.  */
  773.  
  774. FILE *outdesc = NULL;
  775.  
  776. /* Header for that file (filled in by `write_header').  */
  777.  
  778. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  779. struct aexec outheader;
  780. #else
  781. struct exec outheader;
  782. #endif
  783.  
  784. #ifdef COFF_ENCAPSULATE
  785. struct coffheader coffheader;
  786. int need_coff_header;
  787. #endif
  788.  
  789. /* The following are computed by `digest_symbols'.  */
  790.  
  791. int text_size;        /* total size of text of all input files.  */
  792. int data_size;        /* total size of data of all input files.  */
  793. int bss_size;        /* total size of bss of all input files.  */
  794. int text_reloc_size;    /* total size of text relocation of all input files.  */
  795. int data_reloc_size;    /* total size of data relocation of all input */
  796.             /* files.  */
  797.  
  798. /* Specifications of start and length of the area reserved at the end
  799.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  800. int set_sect_start;
  801. int set_sect_size;
  802.  
  803. /* Pointer for in core storage for the above vectors, before they are
  804.    written. */
  805. unsigned long *set_vectors;
  806.  
  807. /* Amount of cleared space to leave between the text and data segments.  */
  808.  
  809. int text_pad;
  810.  
  811. /* Amount of bss segment to include as part of the data segment.  */
  812.  
  813. int data_pad;
  814.  
  815. /* Format of __.SYMDEF:
  816.    First, a longword containing the size of the 'symdef' data that follows.
  817.    Second, zero or more 'symdef' structures.
  818.    Third, a word containing the length of symbol name strings.
  819.    Fourth, zero or more symbol name strings (each followed by a zero).  */
  820.  
  821. struct symdef {
  822.   int symbol_name_string_index;
  823.   int library_member_offset;
  824. };
  825.  
  826. /* Record most of the command options.  */
  827.  
  828. /* Address we assume the text section will be loaded at.
  829.    We relocate symbols and text and data for this, but we do not
  830.    write any padding in the output file for it.  */
  831. int text_start;
  832.  
  833. /* Offset of default entry-pc within the text section.  */
  834. int entry_offset;
  835.  
  836. /* Address we decide the data section will be loaded at.  */
  837. int data_start;
  838.  
  839. /* `text-start' address is normally this much plus a page boundary.
  840.    This is not a user option; it is fixed for each system.  */
  841. int text_start_alignment;
  842.  
  843. /* Nonzero if -T was specified in the command line.
  844.    This prevents text_start from being set later to default values.  */
  845. int T_flag_specified;
  846.  
  847. /* Nonzero if -Tdata was specified in the command line.
  848.    This prevents data_start from being set later to default values.  */
  849. int Tdata_flag_specified;
  850.  
  851. /* Size to pad data section up to.
  852.    We simply increase the size of the data section, padding with zeros,
  853.    and reduce the size of the bss section to match.  */
  854. int specified_data_size;
  855.  
  856. /* Magic number to use for the output file, set by switch.  */
  857. #ifdef MINIX
  858. long magic;
  859. #else
  860. int magic;
  861. #endif /* MINIX */
  862.  
  863. /* Nonzero means print names of input files as processed.  */
  864. int trace_files;
  865.  
  866. /* Which symbols should be stripped (omitted from the output):
  867.    none, all, or debugger symbols.  */
  868. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  869.  
  870. /* Which local symbols should be omitted:
  871.    none, all, or those starting with L.
  872.    This is irrelevant if STRIP_NONE.  */
  873. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  874.  
  875. /* 1 => write load map.  */
  876. int write_map;
  877.  
  878. /* 1 => write relocation into output file so can re-input it later.  */
  879. int relocatable_output;
  880.  
  881. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  882. int force_common_definition;
  883.  
  884. /* 1 => atari minix special, write symbol table for mdb in out.h format */
  885. #if (defined(MINIX) || defined(atariminix))
  886. /* 1 => write out.h format output file too */
  887. int write_minix_out = 0;
  888. FILE *minix_out_filep;
  889. char minix_out_filename[80]; /* name = strcat(output_filename, ".sym") */
  890. void do_write_minix_out();
  891. #endif
  892.  
  893. /* Standard directories to search for files specified by -l.  */
  894. #ifdef atarist
  895. char *standard_search_dirs[] = {"\\gnu\\lib", "\\gnu"};
  896. #else
  897. #ifdef CROSSATARI
  898. char *standard_search_dirs[] = {CROSSLIB};
  899. #else
  900. #ifdef atariminix
  901. char *standard_search_dirs[] = {"/usr/local/lib"};
  902. #else
  903. char *standard_search_dirs[] = {"/lib", "/usr/lib", "/usr/local/lib"};
  904. #endif
  905. #endif
  906. #endif
  907.  
  908. #if defined(atarist) || defined(CROSSATARI)
  909. /* Write GST compatible symboltable. This format allows the symbols
  910.    to be upto 22 bytes long  */
  911. int gst_symboltable;
  912.  
  913. /* This variable count the number of extended symboltable entries.
  914.    This is done to correct the size of the symboltable in the header. */
  915. int gst_symbols;
  916. #endif
  917.  
  918. /* Actual vector of directories to search;
  919.    this contains those specified with -L plus the standard ones.  */
  920. char **search_dirs;
  921.  
  922. /* Length of the vector `search_dirs'.  */
  923. int n_search_dirs;
  924.  
  925. /* Non zero means to create the output executable. */
  926. /* Cleared by nonfatal errors.  */
  927. int make_executable;
  928.  
  929. /* Force the executable to be output, even if there are non-fatal
  930.    errors */
  931. int force_executable;
  932.  
  933. /* Keep a list of any symbols referenced from the command line (so
  934.    that error messages for these guys can be generated). This list is
  935.    zero terminated. */
  936. struct glosym **cmdline_references;
  937. int cl_refs_allocated;
  938.  
  939. /*
  940.  * Structure for communication between do_file_warnings and it's
  941.  * helper routines.  Will in practice be an array of three of these:
  942.  * 0) Current line, 1) Next line, 2) Source file info.
  943.  */
  944. struct line_debug_entry
  945. {
  946.   int line;
  947.   char *filename;
  948.   struct nlist *sym;
  949. };
  950.  
  951. #if defined(__STDC__) || defined(__cplusplus)
  952. # define P_(s) s
  953. #else
  954. # define P_(s) ()
  955. #endif
  956.  
  957.  
  958. /* ld.c */
  959. int main P_((int argc, char **argv));
  960. int classify_arg P_((char *arg));
  961. int next_indirect_name P_((FILE *f, char *buf));
  962. int decode_indirect_file P_((char *iname));
  963. struct file_entry *process_indirect_file P_((struct file_entry *p, char *iname));
  964. void decode_command P_((int argc, char **argv));
  965. void add_cmdline_ref P_((struct glosym *sp));
  966. void decode_option P_((char *swt, char *arg));
  967. void each_file P_((void (*function )(), void *arg));
  968. unsigned long check_each_file P_((unsigned long (*function )(), void *arg));
  969. void each_full_file P_((void (*function )(), void *arg));
  970. void file_close P_((void));
  971. FILE *file_open P_((struct file_entry *entry));
  972. void prline_file_name P_((struct file_entry *entry, FILE *outfile));
  973. void print_file_name P_((struct file_entry *entry, FILE *outfile));
  974. char *get_file_name P_((struct file_entry *entry));
  975. void read_header P_((FILE *desc, struct file_entry *entry));
  976. void read_entry_symbols P_((FILE *desc, struct file_entry *entry));
  977. void read_entry_strings P_((FILE *desc, struct file_entry *entry));
  978. void load_symbols P_((void));
  979. void read_file_symbols P_((struct file_entry *entry));
  980. void enter_file_symbols P_((struct file_entry *entry));
  981. void enter_global_ref P_((struct nlist *nlist_p, char *name, struct file_entry *entry));
  982. unsigned long contains_symbol P_((struct file_entry *entry, struct nlist *n_ptr));
  983. void search_library P_((FILE *desc, struct file_entry *entry));
  984. struct file_entry *decode_library_subfile P_((FILE *desc, struct file_entry *library_entry, int subfile_offset, int *length_loc));
  985. void symdef_library P_((FILE *desc, struct file_entry *entry, int member_length));
  986. void linear_library P_((FILE *desc, struct file_entry *entry));
  987. int subfile_wanted_p P_((struct file_entry *entry));
  988. void digest_symbols P_((void));
  989. void consider_file_section_lengths P_((struct file_entry *entry));
  990. void relocate_file_addresses P_((struct file_entry *entry));
  991. void print_symbols P_((FILE *outfile));
  992. void describe_file_sections P_((struct file_entry *entry, FILE *outfile));
  993. void list_file_locals P_((struct file_entry *entry, FILE *outfile));
  994. void do_file_warnings P_((struct file_entry *entry, FILE *outfile));
  995. int relocation_entries_relation P_((struct relocation_info *rel1, struct relocation_info *rel2));
  996. int next_debug_entry P_((int use_data_symbols, struct line_debug_entry state_pointer[]));
  997. int address_to_line P_((unsigned long address, struct line_debug_entry state_pointer[]));
  998. struct line_debug_entry *init_debug_scan P_((int use_data_symbols, struct file_entry *entry));
  999. void mark_flagged_symbols P_((struct file_entry *entry, FILE *outfile));
  1000. void do_warnings P_((FILE *outfile));
  1001. void print_files_defining_symbol P_((struct nlist *first_def, struct nlist *rest_of_refs));
  1002. void write_output P_((void));
  1003. void write_header P_((void));
  1004. void write_text P_((void));
  1005. int text_offset P_((struct file_entry *entry));
  1006. void read_relocation P_((void));
  1007. void read_file_relocation P_((struct file_entry *entry));
  1008. void copy_text P_((struct file_entry *entry));
  1009. void write_data P_((void));
  1010. void copy_data P_((struct file_entry *entry));
  1011. void perform_relocation P_((char *data, int pc_relocation, int data_size,
  1012.     struct relocation_info *reloc_info, int reloc_size,
  1013.     struct file_entry *entry, int pure_text));
  1014. void write_rel P_((void));
  1015. void flush_atari_rbuf P_((void));
  1016. int reloc_less_p P_((struct relocation_info *reloc_1, struct relocation_info *reloc_2));
  1017. void emit_atari_text_relocs P_((struct file_entry *entry));
  1018. void emit_atari_data_relocs P_((struct file_entry *entry));
  1019. void emit_atari_set_vectors P_((void));
  1020. void coptxtrel P_((struct file_entry *entry));
  1021. void copdatrel P_((struct file_entry *entry));
  1022. void emit_atari_reloc_byte P_((unsigned int b));
  1023. int emit_atari_reloc P_((long rel_pc));
  1024. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1025. #if (!(defined(MINIX) || defined(atariminix)))
  1026. void write_atari_sym P_((struct nlist *p, char *str));
  1027. #else
  1028. void  write_atari_sym P_((struct nlist *p, char *str, int gflag));
  1029. #endif
  1030. void write_atari_syms P_((struct file_entry *entry, int *syms_written_addr));
  1031. #else  /* for Gnu/Unix */
  1032. void write_string_table P_((void));
  1033. void write_file_syms P_((struct file_entry *entry, int *syms_written_addr));
  1034. #endif /* (defined(CROSSATARI) || defined(atarist) || defined(atariminix)) */
  1035. void write_syms P_((void));
  1036. int assign_string_table_index P_((char *name));
  1037. void write_symsegs P_((void));
  1038. void write_file_symseg P_((struct file_entry *entry));
  1039. void symtab_init P_((void));
  1040. int hash_string P_((char *key));
  1041. symbol * getsym P_((char *key));
  1042. symbol * getsym_soft P_((char *key));
  1043. void fatal P_((char *string, ...));
  1044. void fatal_with_file P_((char *string, struct file_entry *entry));
  1045. void perror_name P_((char *name));
  1046. void perror_file P_((struct file_entry *entry));
  1047. void mywrite P_((void *buf, int count, int eltsize, FILE *desc));
  1048. void padfile P_((int padding, FILE *outdesc));
  1049. char *concat P_((char *s1, char *s2, char *s3));
  1050. int parse P_((char *arg, char *format, char *error));
  1051. void *xmalloc P_((int size));
  1052. void *xrealloc P_((void *ptr, int size));
  1053. int write_minix_sym P_((struct nlist *p, char *str, int gflag, int fflag));
  1054. void write_minix_syms P_((struct file_entry *entry, int *syms_written_addr));
  1055. void do_write_minix_out P_((void));
  1056. unsigned short minix_swap_short P_((unsigned char s[]));
  1057. long minix_swap_long P_((unsigned char s[]));
  1058.  
  1059. #undef P_
  1060.  
  1061. #ifndef bcopy
  1062. void bcopy (), bzero ();
  1063. #endif
  1064. void *malloc (), *realloc ();
  1065. #ifndef alloca
  1066. int alloca ();
  1067. #endif
  1068. int free ();
  1069.  
  1070. void error ();
  1071.  
  1072.  
  1073. int
  1074. main (argc, argv)
  1075.      char **argv;
  1076.      int argc;
  1077. {
  1078. #ifdef atarist
  1079.   _binmode(1);
  1080. #endif
  1081.  
  1082.   page_size = getpagesize ();
  1083.   progname = argv[0];
  1084.  
  1085.   /* Clear the cumulative info on the output file.  */
  1086.  
  1087.   text_size = 0;
  1088.   data_size = 0;
  1089.   bss_size = 0;
  1090.   text_reloc_size = 0;
  1091.   data_reloc_size = 0;
  1092.  
  1093.   data_pad = 0;
  1094.   text_pad = 0;
  1095.  
  1096.   /* Initialize the data about options.  */
  1097.  
  1098. #if defined(atarist) || defined(CROSSATARI)
  1099.   gst_symboltable = 0;
  1100.   gst_symbols = 0;
  1101. #endif
  1102.   specified_data_size = 0;
  1103.   strip_symbols = STRIP_NONE;
  1104.   trace_files = 0;
  1105.   discard_locals = DISCARD_NONE;
  1106.   entry_symbol = 0;
  1107.   write_map = 0;
  1108.   relocatable_output = 0;
  1109.   force_common_definition = 0;
  1110.   T_flag_specified = 0;
  1111.   Tdata_flag_specified = 0;
  1112.   magic = DEFAULT_MAGIC;
  1113.   make_executable = 1;
  1114.   force_executable = 0;
  1115.  
  1116.   /* Initialize the cumulative counts of symbols.  */
  1117.  
  1118.   local_sym_count = 0;
  1119.   non_L_local_sym_count = 0;
  1120.   debugger_sym_count = 0;
  1121.   undefined_global_sym_count = 0;
  1122.   set_symbol_count = 0;
  1123.   set_vector_count = 0;
  1124.   global_indirect_count = 0;
  1125.   warning_count = 0;
  1126.   common_defined_global_count = 0;
  1127.  
  1128.   /* Keep a list of symbols referenced from the command line */
  1129.   cl_refs_allocated = 10;
  1130.   cmdline_references =
  1131.     (struct glosym **) xmalloc (cl_refs_allocated
  1132.                 * sizeof(struct glosym *));
  1133.   *cmdline_references = 0;
  1134.  
  1135.   /* Completely decode ARGV.  */
  1136.  
  1137.   decode_command (argc, argv);
  1138.  
  1139. #if (defined(MINIX) || defined(atariminix))
  1140.   if(write_minix_out == 1)
  1141.   {
  1142.       strcpy(minix_out_filename, output_filename);
  1143.       strcat(minix_out_filename, ".sym");
  1144.       /* if(strlen(minix_out_filename) - strlen(output_filename) > 14)
  1145.      then you are up s*it creek;
  1146.        */
  1147.   }
  1148. #endif
  1149.  
  1150.   /* Create the symbols `etext', `edata' and `end'.  */
  1151.  
  1152. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  1153.   if (!relocatable_output)
  1154. #endif            /* always do this on atari */
  1155.     symtab_init ();
  1156.  
  1157.   /* Determine whether to count the header as part of
  1158.      the text size, and initialize the text size accordingly.
  1159.      This depends on the kind of system and on the output format selected.  */
  1160.  
  1161. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1162.   outheader.a_magic = CMAGIC;
  1163. #else
  1164.   outheader.a_magic = magic;
  1165. #endif
  1166.  
  1167. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  1168. #ifdef INITIALIZE_HEADER
  1169.   INITIALIZE_HEADER;
  1170. #endif
  1171. #endif
  1172.  
  1173. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1174.   text_size = sizeof (struct aexec);
  1175. #else
  1176.   text_size = sizeof (struct exec);
  1177. #endif
  1178.  
  1179. #ifdef COFF_ENCAPSULATE
  1180.   if (relocatable_output == 0)
  1181.     {
  1182.       need_coff_header = 1;
  1183.       /* set A_ENCAP now, since it will change the values of N_TXTOFF, etc */
  1184.       outheader.a_flags |= A_ENCAP;
  1185.       text_size += sizeof (struct coffheader);
  1186.     }
  1187. #endif
  1188.  
  1189. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1190.   text_size -= A_TXTOFF (outheader);
  1191. #else
  1192.   text_size -= N_TXTOFF (outheader);
  1193. #endif
  1194.  
  1195.   if (text_size < 0)
  1196.     text_size = 0;
  1197.   entry_offset = text_size;
  1198.  
  1199. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  1200.   if (!T_flag_specified && !relocatable_output)
  1201.     text_start = N_TXTADDR (outheader);
  1202. #endif
  1203.  
  1204.   /* The text-start address is normally this far past a page boundary.  */
  1205.  
  1206.   text_start_alignment = text_start % page_size;
  1207.  
  1208.   /* Load symbols of all input files.
  1209.      Also search all libraries and decide which library members to load.  */
  1210.  
  1211.   load_symbols ();
  1212.  
  1213.   /* Compute where each file's sections go, and relocate symbols.  */
  1214.  
  1215.   digest_symbols ();
  1216.  
  1217.   /* Print error messages for any missing symbols, for any warning
  1218.      symbols, and possibly multiple definitions */
  1219.  
  1220.   do_warnings (stderr);
  1221.  
  1222.   /* Print a map, if requested.  */
  1223.  
  1224.   if (write_map) print_symbols (stdout);
  1225.  
  1226.   /* Write the output file.  */
  1227.  
  1228.   if (make_executable || force_executable)
  1229.     write_output ();
  1230.  
  1231.   exit( ! make_executable);
  1232. }
  1233.  
  1234. void decode_option ();
  1235.  
  1236. /* Analyze a command line argument.
  1237.    Return 0 if the argument is a filename.
  1238.    Return 1 if the argument is a option complete in itself.
  1239.    Return 2 if the argument is a option which uses an argument.
  1240.  
  1241.    Thus, the value is the number of consecutive arguments
  1242.    that are part of options.  */
  1243.  
  1244. int
  1245. classify_arg (arg)
  1246.      register char *arg;
  1247. {
  1248.   if (*arg != '-') return 0;
  1249.   switch (arg[1])
  1250.     {
  1251.     case 'A':
  1252.     case 'D':
  1253.     case 'e':
  1254.     case 'L':
  1255.     case 'o':
  1256.     case 'u':
  1257.     case 'y':
  1258.       if (arg[2])
  1259.     return 1;
  1260.       return 2;
  1261.  
  1262.     case 'l':
  1263.       if (arg[2])
  1264.     return 1;
  1265.       return 2;
  1266.  
  1267.     case 'T':
  1268.       if (arg[2] == 0)
  1269.     return 2;
  1270.       if (! strcmp (&arg[2], "text"))
  1271.     return 2;
  1272.       if (! strcmp (&arg[2], "data"))
  1273.     return 2;
  1274.       return 1;
  1275.     }
  1276.  
  1277.   return 1;
  1278. }
  1279.  
  1280. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1281. /* things to grok indirect files */
  1282.  
  1283. int next_indirect_name(f, buf)
  1284. FILE * f;
  1285. char * buf;
  1286. {
  1287.   char c;
  1288.   char * s = buf;
  1289.  
  1290.   for (*buf = '\0' ; (((c = fgetc(f)) != EOF) && (isspace(c))) ; )
  1291.     ;            /* skip whitespace */
  1292.   if (c == EOF) return(0);    /* lose */
  1293.   *s++ = c;
  1294.   for ( ; (((c = fgetc(f)) != EOF) && (!isspace(c))) ; )
  1295.     *s++ = c;
  1296.   *s = '\0';            /* finish it */
  1297.   return((strlen(buf) > 0));    /* win if saw any */
  1298. }
  1299.  
  1300. int decode_indirect_file(iname)
  1301. /* (declare (values n-new-files)) */
  1302. char * iname;
  1303. {
  1304.   char fn[80];        /* name buffer */
  1305.   int i;
  1306.   FILE * ifile;
  1307.  
  1308. #ifdef DEBUG
  1309.   fprintf(stderr, "decode_indirect_file('%s')\n", iname);
  1310. #endif
  1311.   if ((ifile = fopen(iname, "r")) == NULL)
  1312.     {
  1313.     fprintf (stderr, "Can't open indirect file '%s'\n", iname);
  1314.     return(0);
  1315.     }
  1316.   for (i = 0 ; next_indirect_name(ifile, fn) ; )
  1317.     i++;        /* count files */
  1318.   fclose(ifile);
  1319. #ifdef DEBUG
  1320.   fprintf(stderr, "  ->%d\n", i);
  1321. #endif
  1322.   return(i);        /* return file count */
  1323. }
  1324.  
  1325. struct file_entry * process_indirect_file(p, iname)
  1326. /* (declare (values updated_pointer)) */
  1327. struct file_entry * p;
  1328. char * iname;
  1329. {
  1330.   char fn[80];        /* name buffer */
  1331.   FILE * ifile;
  1332.  
  1333. #ifdef DEBUG
  1334.   fprintf(stderr, "process_indirect_file(%X, '%s')\n", p, iname);
  1335. #endif
  1336.   if ((ifile = fopen(iname, "r")) == NULL)
  1337.     {
  1338.     fprintf (stderr, "Can't open indirect file '%s'\n", iname);
  1339.     return(0);
  1340.     }
  1341.   for ( ; next_indirect_name(ifile, fn) ; )
  1342.     {
  1343. #ifdef DEBUG
  1344.     fprintf(stderr, "  file '%s'\n", fn);
  1345. #endif
  1346.     p->filename = concat(fn, "", "");
  1347.     p->local_sym_name = p->filename;
  1348.     p++;
  1349.     }
  1350.   fclose(ifile);
  1351. #ifdef DEBUG
  1352.   fprintf(stderr, "  ->%X\n", p);
  1353. #endif
  1354.   return(p);        /* return new pointer */
  1355. }
  1356.  
  1357. #endif
  1358.  
  1359. /* Process the command arguments,
  1360.    setting up file_table with an entry for each input file,
  1361.    and setting variables according to the options.  */
  1362.  
  1363. void
  1364. decode_command (argc, argv)
  1365.      char **argv;
  1366.      int argc;
  1367. {
  1368.   register int i;
  1369.   register struct file_entry *p;
  1370.  
  1371.   number_of_files = 0;
  1372.   output_filename = "a.out";
  1373.  
  1374.   n_search_dirs = 0;
  1375.   search_dirs = (char **) xmalloc (sizeof (char *));
  1376.  
  1377.   /* First compute number_of_files so we know how long to make file_table.  */
  1378.   /* Also process most options completely.  */
  1379.  
  1380.   for (i = 1; i < argc; i++)
  1381.     {
  1382.       register int code = classify_arg (argv[i]);
  1383.       if (code)
  1384.     {
  1385.       if (i + code > argc)
  1386.         fatal ("no argument following %s\n", argv[i]);
  1387.  
  1388.       decode_option (argv[i], argv[i+1]);
  1389.  
  1390.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1391.         number_of_files++;
  1392.  
  1393.       i += code - 1;
  1394.     }
  1395.       else
  1396. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1397.     if (argv[i][0] == '@')        /* indirect file? */
  1398.       number_of_files += decode_indirect_file(&argv[i][1]);
  1399.      else
  1400. #endif
  1401.     number_of_files++;
  1402.     }
  1403.  
  1404.   if (!number_of_files)
  1405.     fatal ("no input files", 0);
  1406.  
  1407.   p = file_table
  1408.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1409.   bzero (p, number_of_files * sizeof (struct file_entry));
  1410.  
  1411.   /* Now scan again and fill in file_table.  */
  1412.   /* All options except -A and -l are ignored here.  */
  1413.  
  1414.   for (i = 1; i < argc; i++)
  1415.     {
  1416.       register int code = classify_arg (argv[i]);
  1417.  
  1418.       if (code)
  1419.     {
  1420.       char *string;
  1421.       if (code == 2)
  1422.         string = argv[i+1];
  1423.       else
  1424.         string = &argv[i][2];
  1425.  
  1426.       if (argv[i][1] == 'A')
  1427.         {
  1428.           if (p != file_table)
  1429.         fatal (
  1430.          "-A specified before an input file other than the first", 0);
  1431.  
  1432.           p->filename = string;
  1433.           p->local_sym_name = string;
  1434.           p->just_syms_flag = 1;
  1435.           p++;
  1436.         }
  1437.       if (argv[i][1] == 'l')
  1438.         {
  1439. #if (defined(atarist) || (defined(CROSSATARI) && (!defined(MINIX))))
  1440.           {
  1441.             char * ext = rindex(string, '.');
  1442.         
  1443.         if (ext)
  1444.             p->filename = concat(string, "", "");    /* just use it */
  1445.             else
  1446.             p->filename = concat(string, "", ".olb");
  1447.           }
  1448. #else
  1449.           p->filename = concat ("lib", string, ".a");
  1450. #endif
  1451.           p->local_sym_name = concat ("-l", string, "");
  1452.           p->search_dirs_flag = 1;
  1453.           p++;
  1454.         }
  1455.       i += code - 1;
  1456.     }
  1457.       else
  1458. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1459.     if (argv[i][0] == '@')
  1460.       p = process_indirect_file(p, &argv[i][1]);
  1461.      else
  1462. #endif
  1463.     {
  1464.       p->filename = argv[i];
  1465.       p->local_sym_name = argv[i];
  1466.       p++;
  1467.     }
  1468.     }
  1469.  
  1470.   /* Now check some option settings for consistency.  */
  1471.  
  1472.   if ((magic == ZMAGIC || magic == NMAGIC)
  1473.       && (text_start - text_start_alignment) & (page_size - 1))
  1474.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1475.  
  1476. #if (defined(atarist) || defined(atariminix) || defined(CROSSATARI))
  1477.    {
  1478.      /* see if there's an env var that says where to search for things */
  1479.  
  1480.      extern char *getenv();
  1481.      char *value = getenv("GNULIB");
  1482.  
  1483.      /* Allow GNULIB to contain more than one directory, so you can take
  1484.       * advantage to your lib RAM-Disk/Harddisk/Floppy(;-)
  1485.       * The dirs are delimited by ';' or ','.        [br]
  1486.       */
  1487. #ifdef DEBUG
  1488.      fprintf(stderr, "env->%X\n", value);
  1489. #endif
  1490.      if (value) {
  1491.      char *glib = (char *)alloca(strlen(value) + 1), *cp, *cpp;
  1492.      
  1493.      strcpy(glib, value);
  1494. #ifdef DEBUG
  1495.      fprintf(stderr, "glib->'%s'\n", glib);
  1496. #endif
  1497.      cp = cpp = glib;
  1498.      while( *cpp != '\0' ) {
  1499. #ifdef atarist
  1500.          while( (*cp != ';') && (*cp != ',') && (*cp != '\0') )
  1501. #else
  1502.          while( (*cp != ':') && (*cp != '\0') )
  1503. #endif
  1504.          cp++;
  1505.          if( *cp != '\0' )
  1506.          *cp++ = '\0';    /* terminate string and advance to next    */
  1507.                  /* else  "*(cpp = cp) = '\0'"  =>> end */
  1508.          n_search_dirs++;
  1509.          search_dirs =
  1510.          (char **) xrealloc((char *)search_dirs,
  1511.                     n_search_dirs*sizeof(char *));
  1512.          search_dirs[n_search_dirs-1] = concat(cpp, "", "");
  1513. #ifdef DEBUG
  1514.          fprintf(stderr, "... '%s'\n", search_dirs[n_search_dirs-1]);
  1515. #endif
  1516.  
  1517.          cpp = cp;        /* ptr to next entry or '\0' (see above) */
  1518.      }
  1519.      }
  1520.   }
  1521. #endif  /* Atari ST special */
  1522.  
  1523.   /* Append the standard search directories to the user-specified ones.  */
  1524.   {
  1525.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1526.     n_search_dirs += n;
  1527.     search_dirs
  1528.       = (char **) xrealloc ((char *)search_dirs,
  1529.                 n_search_dirs * sizeof (char *));
  1530.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1531.        n * sizeof (char *));
  1532.   }
  1533. }
  1534.  
  1535.  
  1536. void
  1537. add_cmdline_ref (sp)
  1538.      struct glosym *sp;
  1539. {
  1540.   struct glosym **ptr;
  1541.  
  1542.   for (ptr = cmdline_references;
  1543.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1544.        ptr++)
  1545.     ;
  1546.  
  1547.   if (ptr == cmdline_references + cl_refs_allocated)
  1548.     {
  1549.       int diff = ptr - cmdline_references;
  1550.       
  1551.       cl_refs_allocated *= 2;
  1552.       cmdline_references = (struct glosym **)
  1553.     xrealloc ((char *)cmdline_references,
  1554.          cl_refs_allocated * sizeof (struct glosym *));
  1555.       ptr = cmdline_references + diff;
  1556.     }
  1557.   
  1558.   *ptr++ = sp;
  1559.   *ptr = (struct glosym *) 0;
  1560. }
  1561.     
  1562. int parse ();
  1563.  
  1564. /* Record an option and arrange to act on it later.
  1565.    ARG should be the following command argument,
  1566.    which may or may not be used by this option.
  1567.  
  1568.    The `l' and `A' options are ignored here since they actually
  1569.    specify input files.  */
  1570.  
  1571. void
  1572. decode_option (swt, arg)
  1573.      register char *swt, *arg;
  1574. {
  1575.   if (! strcmp (swt + 1, "Ttext"))
  1576.     {
  1577.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1578.       T_flag_specified = 1;
  1579.       return;
  1580.     }
  1581.   if (! strcmp (swt + 1, "Tdata"))
  1582.     {
  1583.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1584.       Tdata_flag_specified = 1;
  1585.       return;
  1586.     }
  1587.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1588.     {
  1589.       force_executable = 1;
  1590.       return;
  1591.     }
  1592.  
  1593.   if (swt[2] != 0)
  1594.     arg = &swt[2];
  1595.  
  1596.   switch (swt[1])
  1597.     {
  1598.     case 'A':
  1599.       return;
  1600.  
  1601.     case 'D':
  1602.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1603.       return;
  1604.  
  1605.     case 'd':
  1606.       force_common_definition = 1;
  1607.       return;
  1608.  
  1609.     case 'e':
  1610.       entry_symbol = getsym (arg);
  1611.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1612.     undefined_global_sym_count++;
  1613.       entry_symbol->referenced = 1;
  1614.       add_cmdline_ref (entry_symbol);
  1615.       return;
  1616.  
  1617. #if defined(atarist) || defined(CROSSATARI)
  1618.     case 'G':
  1619.       gst_symboltable = 1;
  1620.       return;
  1621. #endif
  1622.  
  1623.     case 'l':
  1624.       return;
  1625.  
  1626.     case 'L':
  1627.       n_search_dirs++;
  1628.       search_dirs
  1629.     = (char **) xrealloc ((char *)search_dirs,
  1630.                   n_search_dirs * sizeof (char *));
  1631.       search_dirs[n_search_dirs - 1] = arg;
  1632.       return;
  1633.  
  1634.     case 'M':
  1635.       write_map = 1;
  1636.       return;
  1637.  
  1638.     case 'N':
  1639.       magic = OMAGIC;
  1640.       return;
  1641.  
  1642.     case 'n':
  1643.       magic = NMAGIC;
  1644.       return;
  1645.  
  1646.     case 'o':
  1647.       output_filename = arg;
  1648.       return;
  1649.  
  1650.     case 'r':
  1651. #if !(defined(atarist) || defined(CROSSATARI) || defined(atariminix))
  1652.       relocatable_output = 1;
  1653.       magic = OMAGIC;
  1654.       text_start = 0;
  1655. #endif
  1656.       return;
  1657.  
  1658.     case 'S':
  1659.       strip_symbols = STRIP_DEBUGGER;
  1660.       return;
  1661.  
  1662.     case 's':
  1663.       strip_symbols = STRIP_ALL;
  1664.       return;
  1665.  
  1666.     case 'T':
  1667.       text_start = parse (arg, "%x", "invalid argument to -T");
  1668.       T_flag_specified = 1;
  1669.       return;
  1670.  
  1671.     case 't':
  1672.       trace_files = 1;
  1673.       return;
  1674.  
  1675.     case 'u':
  1676.       {
  1677.     register symbol *sp = getsym (arg);
  1678.     if (!sp->defined && !sp->referenced)
  1679.       undefined_global_sym_count++;
  1680.     sp->referenced = 1;
  1681.     add_cmdline_ref (sp);
  1682.       }
  1683.       return;
  1684.  
  1685. #if defined(atarist) || defined(CROSSATARI)
  1686.     case 'v':
  1687. #ifndef __GNUC__
  1688.     {
  1689.       extern time_t time();
  1690.       extern char *ctime();
  1691.       time_t t = time(0L);
  1692.  
  1693.       fprintf (stderr, "`gcc-ld' cross-linker Patchlevel %s for GNU, Atari ST version (%s)\n",
  1694.            PatchLevel, ctime(&t));
  1695.     }
  1696. #else
  1697.       fprintf (stderr, "`gcc-ld' linker Patchlevel %s for GNU, Atari ST version (%s %s)\n",
  1698.            PatchLevel, __DATE__, __TIME__);
  1699. #endif
  1700.       return;
  1701. #endif /* atarist */
  1702.  
  1703.     case 'X':
  1704.       discard_locals = DISCARD_L;
  1705.       return;
  1706.  
  1707.     case 'x':
  1708.       discard_locals = DISCARD_ALL;
  1709.       return;
  1710.  
  1711.     case 'y':
  1712.       {
  1713.     register symbol *sp = getsym (&swt[2]);
  1714.     sp->trace = 1;
  1715.       }
  1716.       return;
  1717.  
  1718. #if (defined(MINIX) || defined(atariminix))
  1719.     case 'z':    /* ok we needed a letter */
  1720.       write_minix_out = 1;
  1721.       return;
  1722. #else
  1723.     case 'z':
  1724. #if !(defined(atarist) || defined(CROSSATARI))
  1725.       magic = ZMAGIC;
  1726. #endif
  1727.       return;
  1728. #endif
  1729.  
  1730.     default:
  1731.       fatal ("invalid command option `%s'", swt);
  1732.     }
  1733. }
  1734.  
  1735. /** Convenient functions for operating on one or all files being */
  1736.  /** loaded.  */
  1737. void print_file_name ();
  1738.  
  1739. /* Call FUNCTION on each input file entry.
  1740.    Do not call for entries for libraries;
  1741.    instead, call once for each library member that is being loaded.
  1742.  
  1743.    FUNCTION receives two arguments: the entry, and ARG.  */
  1744.  
  1745. void
  1746. each_file (function, arg)
  1747.      register void (*function)();
  1748.      register void *arg;
  1749. {
  1750.   register int i;
  1751.  
  1752.   for (i = 0; i < number_of_files; i++)
  1753.     {
  1754.       register struct file_entry *entry = &file_table[i];
  1755.       if (entry->library_flag)
  1756.         {
  1757.       register struct file_entry *subentry = entry->subfiles;
  1758.       for (; subentry; subentry = subentry->chain)
  1759.         (*function) (subentry, arg);
  1760.     }
  1761.       else
  1762.     (*function) (entry, arg);
  1763.     }
  1764. }
  1765.  
  1766. /* Call FUNCTION on each input file entry until it returns a non-zero
  1767.    value.  Return this value.
  1768.    Do not call for entries for libraries;
  1769.    instead, call once for each library member that is being loaded.
  1770.  
  1771.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1772.    function returning unsigned long (though this can probably be fudged). */
  1773.  
  1774. unsigned long
  1775. check_each_file (function, arg)
  1776.      register unsigned long (*function)();
  1777.      register void *arg;
  1778. {
  1779.   register int i;
  1780.   register unsigned long return_val;
  1781.  
  1782.   for (i = 0; i < number_of_files; i++)
  1783.     {
  1784.       register struct file_entry *entry = &file_table[i];
  1785.       if (entry->library_flag)
  1786.         {
  1787.       register struct file_entry *subentry = entry->subfiles;
  1788.       for (; subentry; subentry = subentry->chain)
  1789.         if (return_val = (*function) (subentry, arg))
  1790.           return return_val;
  1791.     }
  1792.       else
  1793.     if (return_val = (*function) (entry, arg))
  1794.       return return_val;
  1795.     }
  1796.   return 0;
  1797. }
  1798.  
  1799. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1800.  
  1801. void
  1802. each_full_file (function, arg)
  1803.      register void (*function)();
  1804.      register void *arg;
  1805. {
  1806.   register int i;
  1807.  
  1808.   for (i = 0; i < number_of_files; i++)
  1809.     {
  1810.       register struct file_entry *entry = &file_table[i];
  1811.       if (entry->just_syms_flag)
  1812.     continue;
  1813.       if (entry->library_flag)
  1814.         {
  1815.       register struct file_entry *subentry = entry->subfiles;
  1816.       for (; subentry; subentry = subentry->chain)
  1817.         (*function) (subentry, arg);
  1818.     }
  1819.       else
  1820.     (*function) (entry, arg);
  1821.     }
  1822. }
  1823.  
  1824. /* Close the input file that is now open.  */
  1825.  
  1826. void
  1827. file_close ()
  1828. {
  1829.   fclose (input_desc);
  1830.   input_desc = 0;
  1831.   input_file = 0;
  1832. }
  1833.  
  1834. /* Open the input file specified by 'entry', and return a descriptor.
  1835.    The open file is remembered; if the same file is opened twice in a row,
  1836.    a new open is not actually done.  */
  1837.  
  1838. FILE *
  1839. file_open (entry)
  1840.      register struct file_entry *entry;
  1841. {
  1842.   register FILE *desc = NULL;
  1843.  
  1844.   if (entry->superfile)
  1845.     return file_open (entry->superfile);
  1846.  
  1847.   if (entry == input_file)
  1848.     return input_desc;
  1849.  
  1850.   if (input_file) file_close ();
  1851.  
  1852.   if (entry->search_dirs_flag && n_search_dirs)
  1853.     {
  1854. /*       register char **p = search_dirs;  */
  1855.       int i;
  1856.  
  1857.       for (i = 0; i < n_search_dirs; i++)
  1858.     {
  1859.       register char *string
  1860. #ifndef atarist
  1861.         = concat (search_dirs[i], "/", entry->filename);
  1862. #else
  1863.         = concat (search_dirs[i], "\\", entry->filename);
  1864. #endif
  1865.       desc = fopen (string, "r");
  1866.       if (desc != NULL)
  1867.         {
  1868. #ifdef _IOFBF
  1869.           if(setvbuf(desc, NULL, _IOFBF, 128L * 1024))
  1870.         perror_name(string);
  1871. #endif
  1872.           entry->filename = string;
  1873.           entry->search_dirs_flag = 0;
  1874.           break;
  1875.         }
  1876.       free (string);
  1877.     }
  1878.     }
  1879.   else
  1880.     desc = fopen (entry->filename, "r");
  1881.  
  1882.   if (desc != NULL)
  1883.     {
  1884. #ifdef _IOFBF
  1885.           if(setvbuf(desc, NULL, _IOFBF, 128L * 1024))
  1886.         perror_name(entry->filename);
  1887. #endif
  1888.       input_file = entry;
  1889.       input_desc = desc;
  1890.       return desc;
  1891.     }
  1892.  
  1893.   perror_file (entry);
  1894.   /* NOTREACHED */
  1895. }
  1896.  
  1897. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1898.    and then a newline.  */
  1899.  
  1900. void
  1901. prline_file_name (entry, outfile)
  1902.      struct file_entry *entry;
  1903.      FILE *outfile;
  1904. {
  1905.   print_file_name (entry, outfile);
  1906.   fprintf (outfile, "\n");
  1907. }
  1908.  
  1909. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1910.  
  1911. void
  1912. print_file_name (entry, outfile)
  1913.      struct file_entry *entry;
  1914.      FILE *outfile;
  1915. {
  1916.   if (entry->superfile)
  1917.     {
  1918.       print_file_name (entry->superfile, outfile);
  1919.       fprintf (outfile, "(%s)", entry->filename);
  1920.     }
  1921.   else
  1922.     fprintf (outfile, "%s", entry->filename);
  1923. }
  1924.  
  1925. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1926.  
  1927. char *
  1928. get_file_name (entry)
  1929.      struct file_entry *entry;
  1930. {
  1931.   char *result, *supfile;
  1932.   if (entry->superfile)
  1933.     {
  1934.       supfile = get_file_name (entry->superfile);
  1935.       result = (char *) xmalloc (strlen (supfile)
  1936.                  + strlen (entry->filename) + 3);
  1937.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1938.       free (supfile);
  1939.     }
  1940.   else
  1941.     {
  1942.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1943.       strcpy (result, entry->filename);
  1944.     }
  1945.   return result;
  1946. }
  1947.  
  1948. /* Medium-level input routines for rel files.  */
  1949.  
  1950. /* Read a file's header into the proper place in the file_entry.
  1951.    DESC is the descriptor on which the file is open.
  1952.    ENTRY is the file's entry.  */
  1953.  
  1954. void
  1955. read_header (desc, entry)
  1956.      FILE *desc;
  1957.      register struct file_entry *entry;
  1958. {
  1959.   register int len;
  1960.   struct exec *loc = (struct exec *) &entry->header;
  1961.  
  1962.   fseek (desc, entry->starting_offset, 0);
  1963.   len = fread (loc, 1,  sizeof (struct exec), desc);
  1964.   if (len != sizeof (struct exec))
  1965.     fatal_with_file ("failure reading header of ", entry);
  1966.  
  1967. #ifdef BYTE_SWAP
  1968. loc->a_info = SWAP4(loc->a_info);    /* always four (4) bytes */
  1969. if (sizeof(loc->a_text)==4)            /* does this need to be ? */
  1970. {  loc->a_text   = SWAP4(loc->a_text);
  1971.    loc->a_data   = SWAP4(loc->a_data);
  1972.    loc->a_bss    = SWAP4(loc->a_bss);
  1973.    loc->a_syms   = SWAP4(loc->a_syms);
  1974.    loc->a_entry  = SWAP4(loc->a_entry);
  1975.    loc->a_trsize = SWAP4(loc->a_trsize);
  1976.    loc->a_drsize = SWAP4(loc->a_drsize); }
  1977. else
  1978. {  loc->a_text   = SWAP4(loc->a_text);
  1979.    loc->a_data   = SWAP4(loc->a_data);
  1980.    loc->a_bss    = SWAP4(loc->a_bss);
  1981.    loc->a_syms   = SWAP4(loc->a_syms);
  1982.    loc->a_entry  = SWAP4(loc->a_entry);
  1983.    loc->a_trsize = SWAP4(loc->a_trsize);
  1984.    loc->a_drsize = SWAP4(loc->a_drsize); }
  1985. #endif /* BYTE_SWAP */
  1986.  
  1987.   if (N_BADMAG (*loc))
  1988.     fatal_with_file ("bad magic number in ", entry);
  1989.  
  1990.   entry->header_read_flag = 1;
  1991. }
  1992.  
  1993. /* Read the symbols of file ENTRY into core.
  1994.    Assume it is already open, on descriptor DESC.
  1995.    Also read the length of the string table, which follows the symbol table,
  1996.    but don't read the contents of the string table.  */
  1997.  
  1998. void
  1999. read_entry_symbols (desc, entry)
  2000.      struct file_entry *entry;
  2001.      FILE *desc;
  2002. {
  2003.   int str_size;
  2004.  
  2005.   if (!entry->header_read_flag)
  2006.     read_header (desc, entry);
  2007.  
  2008.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  2009.  
  2010.   fseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  2011.   if (entry->header.a_syms != fread (entry->symbols, 1, entry->header.a_syms, desc))
  2012.     fatal_with_file ("premature end of file in symbols of ", entry);
  2013.  
  2014.   fseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  2015.   if (sizeof str_size != fread (&str_size, 1, sizeof str_size, desc))
  2016.     fatal_with_file ("bad string table size in ", entry); /***dohicky***/
  2017.  
  2018. #ifdef BYTE_SWAP
  2019. if (sizeof(str_size)==4)
  2020.   str_size = SWAP4(str_size);
  2021. else
  2022.   str_size = SWAP2(str_size);
  2023. #endif /* BYTE_SWAP */
  2024.  
  2025.   entry->string_size = str_size;
  2026. }
  2027.  
  2028. /* Read the string table of file ENTRY into core.
  2029.    Assume it is already open, on descriptor DESC.
  2030.    Also record whether a GDB symbol segment follows the string table.  */
  2031.  
  2032. void
  2033. read_entry_strings (desc, entry)
  2034.      struct file_entry *entry;
  2035.      FILE *desc;
  2036. {
  2037.   int buffer;
  2038.  
  2039.   if (!entry->header_read_flag)
  2040.     read_header (desc, entry);
  2041.  
  2042.   fseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  2043.   if (entry->string_size != fread (entry->strings, 1, entry->string_size, desc))
  2044.     fatal_with_file ("premature end of file in strings of ", entry);
  2045.  
  2046.   /* While we are here, see if the file has a symbol segment at the end.
  2047.      For a separate file, just try reading some more.
  2048.      For a library member, compare current pos against total size.  */
  2049.   if (entry->superfile)
  2050.     {
  2051.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  2052.     return;
  2053.     }
  2054.   else
  2055.     {
  2056.       buffer = fread (&buffer, 1, sizeof buffer, desc);
  2057.       if (buffer == 0)
  2058.     return;
  2059.       if (buffer != sizeof buffer)
  2060.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  2061.     }
  2062.  
  2063.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  2064. }
  2065.  
  2066. /* Read in the symbols of all input files.  */
  2067.  
  2068. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  2069. void enter_file_symbols (), enter_global_ref (), search_library ();
  2070.  
  2071. void
  2072. load_symbols ()
  2073. {
  2074.   register int i;
  2075.  
  2076.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  2077.  
  2078.   for (i = 0; i < number_of_files; i++)
  2079.     {
  2080.       register struct file_entry *entry = &file_table[i];
  2081.       read_file_symbols (entry);
  2082.     }
  2083.  
  2084.   if (trace_files) fprintf (stderr, "\n");
  2085. }
  2086.  
  2087. /* If ENTRY is a rel file, read its symbol and string sections into core.
  2088.    If it is a library, search it and load the appropriate members
  2089.    (which means calling this function recursively on those members).  */
  2090. /* fixed for byte swapping -- ko */
  2091.  
  2092. void
  2093. read_file_symbols (entry)
  2094.      register struct file_entry *entry;
  2095. {
  2096.   register FILE *desc;
  2097.   register int len;
  2098.   int magicnum;
  2099.  
  2100.   desc = file_open (entry);
  2101.  
  2102.   len = fread ( &magicnum, 1, sizeof magicnum, desc);
  2103.   if (len != sizeof magicnum)
  2104.     fatal_with_file ("failure reading header of ", entry);
  2105.  
  2106. #ifdef BYTE_SWAP
  2107. if (sizeof(magicnum)==4)    /* does this need to be so? ko */
  2108.   magicnum = SWAP4(magicnum);
  2109. else
  2110.   magicnum = SWAP2(magicnum);
  2111. #endif
  2112.  
  2113.   if (!N_BADMAG (*((struct exec *)&magicnum)))
  2114.     {
  2115.       read_entry_symbols (desc, entry);
  2116.       entry->strings = (char *) alloca (entry->string_size);
  2117.       read_entry_strings (desc, entry);
  2118.       enter_file_symbols (entry);
  2119.       entry->strings = 0;
  2120.     }
  2121.   else
  2122.     {
  2123.       char armag[SARMAG];
  2124.  
  2125.       fseek (desc, 0, 0);
  2126.       if (SARMAG != fread (armag, 1, SARMAG, desc) || strncmp (armag, ARMAG, SARMAG))
  2127.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  2128.       entry->library_flag = 1;
  2129.       search_library (desc, entry);
  2130.     }
  2131.  
  2132.   file_close ();
  2133. }
  2134.  
  2135. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  2136.  
  2137. void
  2138. enter_file_symbols (entry)
  2139.      struct file_entry *entry;
  2140. {
  2141.    register struct nlist
  2142.      *p,
  2143.      *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2144. #if 0
  2145.    int lowest_set_vector = -1;
  2146. #endif
  2147.  
  2148.   if (trace_files) prline_file_name (entry, stderr);
  2149.  
  2150.   for (p = entry->symbols; p < end; p++)
  2151. #ifdef BYTE_SWAP
  2152. {    /* convert the n_list ??? */
  2153. if (entry->superfile==NULL) {
  2154. p->n_un.n_strx = SWAP4(p->n_un.n_strx);    /* oochie wow-wow ??!!! -- ko */
  2155. p->n_desc      = SWAP2(p->n_desc);
  2156. p->n_value     = SWAP4(p->n_value); }
  2157. #endif /* BYTE_SWAP */
  2158.  
  2159. #if 0
  2160.     /* Currently N_SETV symbols shouldn't be in input to the loader, */
  2161.     /* but I'll leave this here in case they ever are */
  2162.     if ((p->n_type & ~N_EXT) == N_SETV &&
  2163.     (lowest_set_vector = -1 || lowest_set_vector > p->n_value))
  2164.       lowest_set_vector = p->n_value;
  2165.     else
  2166. #endif
  2167.     if (SET_ELEMENT_P (p->n_type))
  2168.       {
  2169.     set_symbol_count++;
  2170.     if (!relocatable_output)
  2171.       enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2172.       }
  2173.     else if (p->n_type == N_WARNING)
  2174.       {
  2175.       char *name = p->n_un.n_strx + entry->strings;
  2176.  
  2177.       /* Grab the next entry.  */
  2178.       p++;
  2179.  
  2180. #ifdef BYTE_SWAP
  2181. if (entry->superfile==NULL) {
  2182. p->n_un.n_strx = SWAP4(p->n_un.n_strx);    /* oochie wow-wow ??!!! -- ko */
  2183. p->n_desc      = SWAP2(p->n_desc);
  2184. p->n_value     = SWAP4(p->n_value); }
  2185. #endif /* BYTE_SWAP */
  2186.  
  2187.       if (p->n_type != (N_UNDF | N_EXT))
  2188.         {
  2189.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  2190.                progname, entry->filename);
  2191.           make_executable = 0;
  2192.           p--;        /* Process normally.  */
  2193.         }
  2194.       else
  2195.         {
  2196.           symbol *sp;
  2197.           char *sname = p->n_un.n_strx + entry->strings;
  2198.           /* Deal with the warning symbol.  */
  2199.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2200.           sp = getsym (sname);
  2201.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  2202.           strcpy (sp->warning, name);
  2203.           warning_count++;
  2204.         }
  2205.       }
  2206.     else if (p->n_type & N_EXT)
  2207.       enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2208.     else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  2209.       {
  2210.     if ((p->n_un.n_strx + entry->strings)[0] != 'L')
  2211.       non_L_local_sym_count++;
  2212.     local_sym_count++;
  2213.       }
  2214.     else debugger_sym_count++;
  2215.  
  2216. #ifdef BYTE_SWAP
  2217. }
  2218. #endif /* BYTE_SWAP */
  2219. #if 0
  2220.   entry->set_vector_offset = lowest_set_vector;
  2221. #endif
  2222.  
  2223.    /* Count one for the local symbol that we generate,
  2224.       whose name is the file's name (usually) and whose address
  2225.       is the start of the file's text.  */
  2226.  
  2227.   local_sym_count++;
  2228.   non_L_local_sym_count++;
  2229. }
  2230.  
  2231. /* Enter one global symbol in the hash table.
  2232.    NLIST_P points to the `struct nlist' read from the file
  2233.    that describes the global symbol.  NAME is the symbol's name.
  2234.    ENTRY is the file entry for the file the symbol comes from.
  2235.  
  2236.    The `struct nlist' is modified by placing it on a chain of
  2237.    all such structs that refer to the same global symbol.
  2238.    This chain starts in the `refs' field of the symbol table entry
  2239.    and is chained through the `n_name'.  */
  2240.  
  2241. void
  2242. enter_global_ref (nlist_p, name, entry)
  2243.      register struct nlist *nlist_p;
  2244.      char *name;
  2245.      struct file_entry *entry;
  2246. {
  2247.   register symbol *sp = getsym (name);
  2248.   register int type = nlist_p->n_type;
  2249.   int oldref = sp->referenced;
  2250.   int olddef = sp->defined;
  2251.  
  2252.   nlist_p->n_un.n_name = (char *) sp->refs;
  2253.   sp->refs = nlist_p;
  2254.  
  2255.   sp->referenced = 1;
  2256.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  2257.     {
  2258.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  2259.     sp->defined = type;
  2260.  
  2261.       if (oldref && !olddef)
  2262.     /* It used to be undefined and we're defining it.  */
  2263.     undefined_global_sym_count--;
  2264.  
  2265. #if 0
  2266.       /* If this is a common definition, keep track of largest
  2267.      common definition seen for this symbol.  */
  2268.       if (type == (N_UNDF | N_EXT)
  2269.       && sp->max_common_size < nlist_p->n_value)
  2270.     sp->max_common_size = nlist_p->n_value;
  2271. #endif
  2272.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  2273.     {
  2274.       /* First definition and it's common.  */
  2275.       common_defined_global_count++;
  2276.       sp->max_common_size = nlist_p->n_value;
  2277.     }
  2278.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  2279.     {
  2280.       /* It used to be common and we're defining it as
  2281.          something else.  */
  2282.       common_defined_global_count--;
  2283.       sp->max_common_size = 0;
  2284.     }
  2285.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  2286.       && sp->max_common_size < nlist_p->n_value)
  2287.     /* It used to be common and this is a new common entry to
  2288.        which we need to pay attention.  */
  2289.     sp->max_common_size = nlist_p->n_value;
  2290.  
  2291.  
  2292.       /* Are we defining it as a set element?  */
  2293. #if 0
  2294.       if (SET_ELEMENT_P (type)) /* potential fix but i dont see why ++jrb */
  2295. #endif
  2296.       if (SET_ELEMENT_P (type)
  2297.       && (!olddef || (olddef && sp->max_common_size)))
  2298.     set_vector_count++;
  2299.       /* Indirect symbols value should be modified to point
  2300.      a symbol being equivalenced to. */
  2301.       if (type == (N_INDR | N_EXT))
  2302.     {
  2303.       nlist_p->n_value =
  2304.         (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  2305.                    + entry->strings);
  2306.       if ((symbol *) nlist_p->n_value == sp)
  2307.         {
  2308.           /* Somebody redefined a symbol to be itself.  */
  2309.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  2310.                entry->filename, name);
  2311.           /* Rewrite this symbol as being a global text symbol
  2312.          with value 0.  */
  2313.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  2314.           nlist_p->n_value = 0;
  2315.           /* Don't make the output executable.  */
  2316.           make_executable = 0;
  2317.         }
  2318.       else
  2319.             global_indirect_count++;
  2320.     }
  2321.     }
  2322.   else
  2323.     if (!oldref)
  2324. #ifndef DOLLAR_KLUDGE
  2325.       undefined_global_sym_count++;
  2326. #else
  2327.       {
  2328.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  2329.       {
  2330.         /* This is an (ISI?) $-conditional; skip it */
  2331.         sp->referenced = 0;
  2332.         if (sp->trace)
  2333.           {
  2334.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  2335.         print_file_name (entry, stderr);
  2336.         fprintf (stderr, "\n");
  2337.           }
  2338.         return;
  2339.       }
  2340.     else
  2341.       undefined_global_sym_count++;
  2342.       }
  2343. #endif
  2344.  
  2345.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  2346.     text_start = nlist_p->n_value;
  2347.  
  2348.   if (sp->trace)
  2349.     {
  2350.       register char *reftype;
  2351.       switch (type & ~N_EXT)
  2352.     {
  2353.     case N_UNDF:
  2354.       if (nlist_p->n_value)
  2355.         reftype = "defined as common";
  2356.       else reftype = "referenced";
  2357.       break;
  2358.  
  2359.     case N_ABS:
  2360.       reftype = "defined as absolute";
  2361.       break;
  2362.  
  2363.     case N_TEXT:
  2364.       reftype = "defined in text section";
  2365.       break;
  2366.  
  2367.     case N_DATA:
  2368.       reftype = "defined in data section";
  2369.       break;
  2370.  
  2371.     case N_BSS:
  2372.       reftype = "defined in BSS section";
  2373.       break;
  2374.  
  2375.     case N_SETT:
  2376.       reftype = "is a text set element";
  2377.       break;
  2378.  
  2379.     case N_SETD:
  2380.       reftype = "is a data set element";
  2381.       break;
  2382.  
  2383.     case N_SETB:
  2384.       reftype = "is a BSS set element";
  2385.       break;
  2386.  
  2387.     case N_SETA:
  2388.       reftype = "is an absolute set element";
  2389.       break;
  2390.  
  2391.     case N_SETV:
  2392.       reftype = "defined in text section as vector";
  2393.       break;
  2394.  
  2395.     case N_INDR:
  2396.       reftype = (char *) alloca (23
  2397.                      + strlen ((nlist_p + 1)->n_un.n_strx
  2398.                            + entry->strings));
  2399.       sprintf (reftype, "defined equivalent to %s",
  2400.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  2401.       break;
  2402.  
  2403.     default:
  2404.       reftype = "I don't know this type";
  2405.       break;
  2406.     }
  2407.  
  2408.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2409.       print_file_name (entry, stderr);
  2410.       fprintf (stderr, "\n");
  2411.     }
  2412. }
  2413.  
  2414. /* This return 0 if the given file entry's symbol table does *not*
  2415.    contain the nlist point entry, and it returns the files entry
  2416.    pointer (cast to unsigned long) if it does. */
  2417.  
  2418. unsigned long
  2419. contains_symbol (entry, n_ptr)
  2420.      struct file_entry *entry;
  2421.      register struct nlist *n_ptr;
  2422. {
  2423.   if (n_ptr >= entry->symbols &&
  2424.       n_ptr < (entry->symbols
  2425.            + (entry->header.a_syms / sizeof (struct nlist))))
  2426.     return (unsigned long) entry;
  2427.   return 0;
  2428. }
  2429.  
  2430.  
  2431. /* Searching libraries */
  2432.  
  2433. struct file_entry *decode_library_subfile ();
  2434. void linear_library (), symdef_library ();
  2435.  
  2436. /* Search the library ENTRY, already open on descriptor DESC.
  2437.    This means deciding which library members to load,
  2438.    making a chain of `struct file_entry' for those members,
  2439.    and entering their global symbols in the hash table.  */
  2440.  
  2441. void
  2442. search_library (desc, entry)
  2443.      FILE *desc;
  2444.      struct file_entry *entry;
  2445. {
  2446.   int member_length;
  2447.   register char *name;
  2448.   register struct file_entry *subentry;
  2449.  
  2450.   if (!undefined_global_sym_count) return;
  2451.  
  2452.   /* Examine its first member, which starts SARMAG bytes in.  */
  2453.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2454.   if (!subentry) return;
  2455.  
  2456.   name = subentry->filename;
  2457.   free (subentry);
  2458.  
  2459.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2460.  
  2461.   if (!strcmp (name, "__.SYMDEF"))
  2462.     symdef_library (desc, entry, member_length);
  2463.   else
  2464.     linear_library (desc, entry);
  2465. }
  2466.  
  2467. /* Construct and return a file_entry for a library member.
  2468.    The library's file_entry is library_entry, and the library is open on DESC.
  2469.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2470.    We store the length of the member into *LENGTH_LOC.  */
  2471.  
  2472. struct file_entry *
  2473. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2474.      FILE *desc;
  2475.      struct file_entry *library_entry;
  2476.      int subfile_offset;
  2477.      int *length_loc;
  2478. {
  2479.   int bytes_read;
  2480.   register int namelen;
  2481.   int member_length;
  2482.   register char *name;
  2483.   struct ar_hdr hdr1;
  2484.   register struct file_entry *subentry;
  2485.  
  2486.   fseek (desc, subfile_offset, 0);
  2487.  
  2488. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  2489.   bytes_read = fread (hdr1.ar_name, 1,
  2490.              sizeof (hdr1.ar_name), desc);
  2491.   bytes_read += fread (hdr1.ar_size, 1,
  2492.               sizeof (hdr1.ar_size), desc);
  2493.   bytes_read += fread (hdr1.ar_date, 1,
  2494.               sizeof (hdr1.ar_date), desc);
  2495.   bytes_read += fread (hdr1.ar_mode, 1,
  2496.               sizeof (hdr1.ar_mode), desc);
  2497.   bytes_read += fread (hdr1.ar_uid, 1,
  2498.               sizeof (hdr1.ar_uid), desc);
  2499.   bytes_read += fread (hdr1.ar_gid, 1,
  2500.               sizeof (hdr1.ar_gid), desc);
  2501.   bytes_read += fread (hdr1.ar_fmag, 1,
  2502.               sizeof (hdr1.ar_fmag), desc);
  2503. #else
  2504.   bytes_read = fread (&hdr1, 1, sizeof hdr1, desc);
  2505. #endif
  2506.   if (!bytes_read)
  2507.     return 0;        /* end of archive */
  2508.  
  2509. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  2510.   if (bytes_read  != (sizeof (hdr1.ar_name) +
  2511.               sizeof (hdr1.ar_size) +
  2512.               sizeof (hdr1.ar_date) +
  2513.               sizeof (hdr1.ar_mode) +
  2514.               sizeof (hdr1.ar_uid) +
  2515.               sizeof (hdr1.ar_gid) +
  2516.               sizeof (hdr1.ar_fmag)))
  2517.     fatal_with_file ("malformed library archive ", library_entry);
  2518. #else
  2519.   if (sizeof hdr1 != bytes_read)
  2520.     fatal_with_file ("malformed library archive ", library_entry);
  2521. #endif
  2522.  
  2523.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2524.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2525.  
  2526.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2527.   bzero (subentry, sizeof (struct file_entry));
  2528.  
  2529.   for (namelen = 0;
  2530.        namelen < sizeof hdr1.ar_name
  2531.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2532.        && hdr1.ar_name[namelen] != '/';
  2533.        namelen++);
  2534.  
  2535.   name = (char *) xmalloc (namelen+1);
  2536.   strncpy (name, hdr1.ar_name, namelen);
  2537.   name[namelen] = 0;
  2538.  
  2539.   subentry->filename = name;
  2540.   subentry->local_sym_name = name;
  2541.   subentry->symbols = 0;
  2542.   subentry->strings = 0;
  2543.   subentry->subfiles = 0;
  2544. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  2545.   subentry->starting_offset = subfile_offset + (sizeof (hdr1.ar_name) +
  2546.                         sizeof (hdr1.ar_size) +
  2547.                         sizeof (hdr1.ar_date) +
  2548.                         sizeof (hdr1.ar_mode) +
  2549.                         sizeof (hdr1.ar_uid) +
  2550.                         sizeof (hdr1.ar_gid) +
  2551.                         sizeof (hdr1.ar_fmag)) ;
  2552. #else
  2553.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2554. #endif
  2555.   subentry->superfile = library_entry;
  2556.   subentry->library_flag = 0;
  2557.   subentry->header_read_flag = 0;
  2558.   subentry->just_syms_flag = 0;
  2559.   subentry->chain = 0;
  2560.   subentry->total_size = member_length;
  2561.  
  2562.   (*length_loc) = member_length;
  2563.  
  2564.   return subentry;
  2565. }
  2566.  
  2567. int subfile_wanted_p ();
  2568.  
  2569. /* Search a library that has a __.SYMDEF member.
  2570.    DESC is a descriptor on which the library is open.
  2571.      The file pointer is assumed to point at the __.SYMDEF data.
  2572.    ENTRY is the library's file_entry.
  2573.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2574.  
  2575. void
  2576. symdef_library (desc, entry, member_length)
  2577.      FILE *desc;
  2578.      struct file_entry *entry;
  2579.      int member_length;
  2580. {
  2581.   int *symdef_data = (int *) xmalloc (member_length);
  2582.   register struct symdef *symdef_base;
  2583.   char *sym_name_base;
  2584.   int number_of_symdefs;
  2585.   int length_of_strings;
  2586.   int not_finished;
  2587.   int bytes_read;
  2588.   register int i;
  2589.   struct file_entry *prev = 0;
  2590.   int prev_offset = 0;
  2591.  
  2592.   bytes_read = fread ( symdef_data, 1, member_length, desc);
  2593.   if (bytes_read != member_length)
  2594.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2595.  
  2596. #ifdef BYTE_SWAP
  2597. if (sizeof(*symdef_data)==4)
  2598.   *symdef_data = SWAP4(*symdef_data);
  2599. else
  2600.   *symdef_data = SWAP2(*symdef_data);
  2601. #endif /* BYTE_SWAP */
  2602.  
  2603.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2604.   if (number_of_symdefs < 0 ||
  2605.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2606.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2607.  
  2608.   symdef_base = (struct symdef *) (symdef_data + 1);
  2609.  
  2610. #ifdef BYTE_SWAP
  2611.   *(int *) (symdef_base + number_of_symdefs) = SWAP4(*(int *) (symdef_base + number_of_symdefs));
  2612. #endif /* BYTE_SWAP */
  2613.  
  2614.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2615.  
  2616.   if (length_of_strings < 0
  2617.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2618.       + 2 * sizeof (int) != member_length)
  2619.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2620.  
  2621.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2622.  
  2623.   /* Check all the string indexes for validity.  */
  2624.  
  2625.   for (i = 0; i < number_of_symdefs; i++)
  2626.     {
  2627.       register int index;
  2628.  
  2629. #ifdef BYTE_SWAP
  2630. if (sizeof(symdef_base[i].symbol_name_string_index)==4)
  2631.   symdef_base[i].symbol_name_string_index = SWAP4(symdef_base[i].symbol_name_string_index);
  2632. else
  2633.   symdef_base[i].symbol_name_string_index = SWAP2(symdef_base[i].symbol_name_string_index);
  2634. #endif /* BYTE_SWAP */
  2635.  
  2636.       index = symdef_base[i].symbol_name_string_index;
  2637.       if (index < 0 || index >= length_of_strings
  2638.       || (index && *(sym_name_base + index - 1)))
  2639.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2640.  
  2641. #ifdef BYTE_SWAP
  2642.       if(sizeof(symdef_base[i].library_member_offset) == 4)
  2643.         symdef_base[i].library_member_offset = SWAP4(symdef_base[i].library_member_offset);
  2644.       else
  2645.         symdef_base[i].library_member_offset = SWAP2(symdef_base[i].library_member_offset);
  2646. #endif
  2647.     }
  2648.  
  2649.   /* Search the symdef data for members to load.
  2650.      Do this until one whole pass finds nothing to load.  */
  2651.  
  2652.   not_finished = 1;
  2653.   while (not_finished)
  2654.     {
  2655.       not_finished = 0;
  2656.  
  2657.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2658.      Load the library members that contain such symbols.  */
  2659.  
  2660.       for (i = 0;
  2661.        (i < number_of_symdefs
  2662.         && (undefined_global_sym_count || common_defined_global_count));
  2663.        i++)
  2664.     if (symdef_base[i].symbol_name_string_index >= 0)
  2665.       {
  2666.         register symbol *sp;
  2667.  
  2668.         sp = getsym_soft (sym_name_base
  2669.                   + symdef_base[i].symbol_name_string_index);
  2670.  
  2671.         /* If we find a symbol that appears to be needed, think carefully
  2672.            about the archive member that the symbol is in.  */
  2673.  
  2674.         if (sp && ((sp->referenced && !sp->defined)
  2675.                || (sp->defined && sp->max_common_size)))
  2676.           {
  2677.         int junk;
  2678.         register int j;
  2679.         register int offset;
  2680.         struct file_entry *subentry;
  2681.  
  2682.                 offset = symdef_base[i].library_member_offset;
  2683.  
  2684.         /* Don't think carefully about any archive member
  2685.            more than once in a given pass.  */
  2686.  
  2687.         if (prev_offset == offset)
  2688.           continue;
  2689.         prev_offset = offset;
  2690.  
  2691.         /* Read the symbol table of the archive member.  */
  2692.  
  2693.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2694.         if (subentry == 0)
  2695.           fatal ("invalid offset for %s in symbol table of %s",
  2696.              sym_name_base
  2697.              + symdef_base[i].symbol_name_string_index,
  2698.              entry->filename);
  2699.         read_entry_symbols (desc, subentry);
  2700.         subentry->strings = (char *) malloc (subentry->string_size);
  2701.         read_entry_strings (desc, subentry);
  2702.  
  2703.         /* Now scan the symbol table and decide whether to load.  */
  2704.  
  2705.         if (!subfile_wanted_p (subentry))
  2706.           {
  2707.             free (subentry->symbols);
  2708.             free (subentry->strings);
  2709.             free (subentry);
  2710.           }
  2711.         else
  2712.           {
  2713.             /* This member is needed; load it.
  2714.                Since we are loading something on this pass,
  2715.                we must make another pass through the symdef data.  */
  2716.  
  2717.             not_finished = 1;
  2718.  
  2719.             enter_file_symbols (subentry);
  2720.  
  2721.             if (prev)
  2722.               prev->chain = subentry;
  2723.             else entry->subfiles = subentry;
  2724.             prev = subentry;
  2725.  
  2726.             /* Clear out this member's symbols from the symdef data
  2727.                so that following passes won't waste time on them.  */
  2728.  
  2729.             for (j = 0; j < number_of_symdefs; j++)
  2730.               {
  2731.             if (symdef_base[j].library_member_offset == offset)
  2732.               symdef_base[j].symbol_name_string_index = -1;
  2733.               }
  2734.             /* We'll read the strings again if we need them again.  */
  2735.             free (subentry->strings);
  2736.             subentry->strings = 0;
  2737.           }
  2738.           }
  2739.       }
  2740.     }
  2741.  
  2742.   free (symdef_data);
  2743. }
  2744.  
  2745. /* Search a library that has no __.SYMDEF.
  2746.    ENTRY is the library's file_entry.
  2747.    DESC is the descriptor it is open on.  */
  2748.  
  2749. void
  2750. linear_library (desc, entry)
  2751.      FILE *desc;
  2752.      struct file_entry *entry;
  2753. {
  2754.   register struct file_entry *prev = 0;
  2755.   register int this_subfile_offset = SARMAG;
  2756.  
  2757.   while (undefined_global_sym_count || common_defined_global_count)
  2758.     {
  2759.       int member_length;
  2760.       register struct file_entry *subentry;
  2761.  
  2762.       subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
  2763.  
  2764.       if (!subentry) return;
  2765.  
  2766.       read_entry_symbols (desc, subentry);
  2767.       subentry->strings = (char *) alloca (subentry->string_size);
  2768.       read_entry_strings (desc, subentry);
  2769.  
  2770.       if (!subfile_wanted_p (subentry))
  2771.     {
  2772.       free (subentry->symbols);
  2773.       free (subentry);
  2774.     }
  2775.       else
  2776.     {
  2777.       enter_file_symbols (subentry);
  2778.  
  2779.       if (prev)
  2780.         prev->chain = subentry;
  2781.       else
  2782.             entry->subfiles = subentry;
  2783.       prev = subentry;
  2784.           subentry->strings = 0; /* Since space will dissapear on return */
  2785.     }
  2786.  
  2787.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2788.       if (this_subfile_offset & 1) this_subfile_offset++;
  2789.     }
  2790. }
  2791.  
  2792. /* ENTRY is an entry for a library member.
  2793.    Its symbols have been read into core, but not entered.
  2794.    Return nonzero if we ought to load this member.  */
  2795.  
  2796. int
  2797. subfile_wanted_p (entry)
  2798.      struct file_entry *entry;
  2799. {
  2800.   register struct nlist *p;
  2801.   register struct nlist *end
  2802.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2803. #ifdef DOLLAR_KLUDGE
  2804.   register int dollar_cond = 0;
  2805. #endif
  2806.  
  2807.   for (p = entry->symbols; p < end; p++)
  2808.     {
  2809.       register int type;
  2810.       register char * name;
  2811.  
  2812.  
  2813. #ifdef BYTE_SWAP    /* convert the n_list ??? */
  2814. p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
  2815. p->n_desc      = SWAP2(p->n_desc);
  2816. p->n_value     = SWAP4(p->n_value);
  2817. #endif /* BYTE_SWAP */
  2818.  
  2819.  
  2820.       type = p->n_type;
  2821.       name = p->n_un.n_strx + entry->strings;
  2822.  
  2823.       if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value
  2824. #ifdef DOLLAR_KLUDGE
  2825.                || name[1] == '$'
  2826. #endif
  2827.                ))
  2828.     {
  2829.       register symbol *sp = getsym_soft (name);
  2830.  
  2831. #ifdef DOLLAR_KLUDGE
  2832.       if (name[1] == '$')
  2833.         {
  2834.           sp = getsym_soft (&name[2]);
  2835.           dollar_cond = 1;
  2836.           if (!sp) continue;
  2837.           if (sp->referenced)
  2838.         {
  2839.           if (write_map)
  2840.             {
  2841.               print_file_name (entry, stdout);
  2842.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2843.             }
  2844. #ifdef BYTE_SWAP    /* finish converting n_list */
  2845. for (p++;p < end; p++)
  2846.   {
  2847.   p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
  2848.   p->n_desc      = SWAP2(p->n_desc);
  2849.   p->n_value     = SWAP4(p->n_value);
  2850.   }
  2851. #endif /* BYTE SWAP */
  2852.           return 1;
  2853.         }
  2854.           continue;
  2855.         }
  2856. #endif
  2857.  
  2858.       /* If this symbol has not been hashed, we can't be looking for it. */
  2859.  
  2860.       if (!sp) continue;
  2861.  
  2862.       /* Note: There has been a lot of discussion about what to
  2863.          when a common definition was previously seen (i.e. when
  2864.          sp->max_common_size > 0).
  2865.          The latest solution is to treat a previous common definition
  2866.          (wrt to subfile_wanted_p) no differently from a real definition.
  2867.          This has the advantage of simplicity and consistency: a common
  2868.          definition is just like a common definition (consistent
  2869.          with strict ANSI C) except that we allow duplicate definitions.
  2870.          Possible disadvantage: May not be the best choice for Fortran,
  2871.          though it is consistent with the standard.
  2872.  
  2873.          An earlier solution:
  2874.          We wanted to see a common definition in the subfile,
  2875.          and note its size, but ignore any other definition
  2876.          if the symbol was already defined (even as a common).
  2877.          This meant that if there were multiple common definitions,
  2878.          the final definition would use the largest size of any of them,
  2879.          as it should.  But if there was a common definition and another
  2880.          definition, like "int pipe;" in a program and "int pipe() {}"
  2881.          in the library, only the common would be used.
  2882.          Disadvantage: a poorly justified kludge.
  2883.  
  2884.          Another previous solution:
  2885.          If the symbol already had a definition as a common symbol,
  2886.          we would want this subfile if some other subfile of the
  2887.          same library that we already need anyway also used the symbol.
  2888.          This seemed like an even more ad hoc decision.
  2889.          It would also cause subfiles to be pulled in that would
  2890.          then conflict with previous entries. I.e. you couldn't
  2891.          have: ld ... start.o libc.a ... if libc.a contained start.o.
  2892.  
  2893.          Other hybrid solutions were also considered.
  2894.       */
  2895.  
  2896.       if (sp->referenced && !sp->defined)
  2897.         {
  2898.           /* This is a symbol we are looking for.  */
  2899. #ifdef DOLLAR_KLUDGE
  2900.           if (dollar_cond) continue;
  2901. #endif
  2902.           if (type == (N_UNDF | N_EXT))
  2903.         {
  2904.           /* Symbol being defined as common.
  2905.              Remember this, but don't load subfile just for this.  */
  2906.  
  2907.           common_defined_global_count++;
  2908.           sp->max_common_size = p->n_value;
  2909.           undefined_global_sym_count--;
  2910.           sp->defined = 1;
  2911.           continue;
  2912.         }
  2913.  
  2914.           if (write_map)
  2915.         {
  2916.           char *nm;
  2917.           if (demangler == NULL || (nm = (*demangler)(sp->name)) == NULL)
  2918.               nm = sp->name;
  2919.           print_file_name (entry, stdout);
  2920.           fprintf (stdout, " needed due to %s\n", nm);
  2921.           if(nm != sp->name)
  2922.              free(nm);
  2923.         }
  2924. #ifdef BYTE_SWAP        /* finish converting n_list */
  2925. for (p++;p < end; p++)
  2926.   {
  2927.   p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
  2928.   p->n_desc      = SWAP2(p->n_desc);
  2929.   p->n_value     = SWAP4(p->n_value);
  2930.   }
  2931. #endif /* BYTE SWAP */
  2932.           return 1;
  2933.         }
  2934.     }
  2935.     }
  2936.  
  2937.   return 0;
  2938. }
  2939.  
  2940. void consider_file_section_lengths (), relocate_file_addresses ();
  2941. void print_files_defining_symbol ();
  2942.  
  2943. /* Having entered all the global symbols and found the sizes of sections
  2944.    of all files to be linked, make all appropriate deductions from this data.
  2945.  
  2946.    We propagate global symbol values from definitions to references.
  2947.    We compute the layout of the output file and where each input file's
  2948.    contents fit into it.  */
  2949.  
  2950. void
  2951. digest_symbols ()
  2952. {
  2953.   register int i;
  2954.   int setv_fill_count;
  2955.  
  2956.   if (trace_files)
  2957.     fprintf (stderr, "Digesting symbol information:\n\n");
  2958.  
  2959.   /* Compute total size of sections */
  2960.  
  2961.   each_file (consider_file_section_lengths, 0);
  2962.  
  2963.   /* Setup the set element vector */
  2964.  
  2965.   if (!relocatable_output)
  2966.     {
  2967.       set_sect_size =
  2968.     (set_symbol_count + 2 * set_vector_count) * sizeof (unsigned long);
  2969.       set_sect_start = text_start + text_size;
  2970.       text_size += set_sect_size;
  2971.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2972.       setv_fill_count = 0;
  2973.     }
  2974.   /* If necessary, pad text section to full page in the file.
  2975.      Include the padding in the text segment size.  */
  2976.  
  2977. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  2978.   if (magic == NMAGIC || magic == ZMAGIC)
  2979.     {
  2980.       int text_end = text_size + N_TXTOFF (outheader);
  2981.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2982.       text_size += text_pad;
  2983.     }
  2984. #endif
  2985.  
  2986.   outheader.a_text = text_size;
  2987.  
  2988.   /* Make the data segment address start in memory on a suitable boundary.  */
  2989.  
  2990. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  2991. /* set beginning of executable's data segment to immediately after
  2992.    text segment.  This is right for the type 0x601A Tos header
  2993.    and also for the standard Stminix header 
  2994. */ 
  2995.   if (! Tdata_flag_specified)
  2996.     data_start = text_start + text_size; 
  2997. #else
  2998.   if (! Tdata_flag_specified)
  2999.     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  3000. #endif
  3001.  
  3002.   /* Compute start addresses of each file's sections and symbols.  */
  3003.  
  3004.   each_full_file (relocate_file_addresses, 0);
  3005.  
  3006.   /* Now, for each symbol, verify that it is defined globally at most once.
  3007.      Put the global value into the symbol entry.
  3008.      Common symbols are allocated here, in the BSS section.
  3009.      Each defined symbol is given a '->defined' field
  3010.       which is the correct N_ code for its definition,
  3011.       except in the case of common symbols with -r.
  3012.      Then make all the references point at the symbol entry
  3013.      instead of being chained together. */
  3014.  
  3015.   defined_global_sym_count = 0;
  3016.  
  3017.   for (i = 0; i < TABSIZE; i++)
  3018.     {
  3019.       register symbol *sp;
  3020.       for (sp = symtab[i]; sp; sp = sp->link)
  3021.     {
  3022.       /* For each symbol */
  3023.       register struct nlist *p, *next;
  3024.       int defs = 0, com = sp->max_common_size, erred = 0;
  3025.       struct nlist *first_definition;
  3026.       for (p = sp->refs; p; p = next)
  3027.         {
  3028.           register int type = p->n_type;
  3029.  
  3030. #if 0
  3031.           if ((type & ~N_EXT) == N_SETV) continue;
  3032. #endif
  3033.           if (SET_ELEMENT_P (type))
  3034.         {
  3035.           if (relocatable_output)
  3036.             fatal ("internal: global ref to set element with -r", 0);
  3037.           if (!defs++)
  3038.             {
  3039.               sp->value = set_sect_start
  3040.             + setv_fill_count++ * sizeof (unsigned long);
  3041.               sp->defined = N_SETV | N_EXT;
  3042.               first_definition = p;
  3043.             }
  3044.           else if ((sp->defined & ~N_EXT) != N_SETV)
  3045.             {
  3046.               make_executable = 0;
  3047.               error ("text symbol `%s' redefined as set vector.  Files:",
  3048.                  sp->name);
  3049.               print_files_defining_symbol (first_definition, p);
  3050.             }
  3051.           set_vectors[setv_fill_count++] = p->n_value;
  3052.         }
  3053.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  3054.         {
  3055.           /* non-common definition */
  3056.           if (defs++ && sp->value != p->n_value)
  3057.             if (!erred++)
  3058.               {
  3059.                 make_executable = 0;
  3060.             error ("multiple definitions of symbol `%s'.  Files:",
  3061.                    sp->name);
  3062.             print_files_defining_symbol (first_definition, p);
  3063.               }
  3064.           sp->value = p->n_value;
  3065.           sp->defined = type;
  3066.           first_definition = p;
  3067.         }
  3068.           next = (struct nlist *) p->n_un.n_name;
  3069.           p->n_un.n_name = (char *) sp;
  3070.         }
  3071.       /* Allocate as common if defined as common and not defined for real */
  3072.       if (com && !defs)
  3073.         {
  3074.           if (!relocatable_output || force_common_definition)
  3075.         {
  3076.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  3077.  
  3078.           sp->value = data_start + data_size + bss_size;
  3079.           sp->defined = N_BSS | N_EXT;
  3080.           bss_size += com;
  3081.           if (write_map)
  3082.                   {
  3083.             char *nm;
  3084.             if (demangler == NULL || (nm = (*demangler)(sp->name)) == NULL)
  3085.               nm = sp->name;
  3086.             printf ("Allocating common %s: %x at %x\n",
  3087.                 nm, com, sp->value);
  3088.             if(nm != sp->name)
  3089.              free(nm);
  3090.                   }
  3091.         }
  3092.           else
  3093.         {
  3094.           sp->defined = 0;
  3095.           undefined_global_sym_count++;
  3096.         }
  3097.         }
  3098.       /* Set length word at front of vector and add zero at the end.
  3099.              Also reverse the vector to put it in file order. */
  3100.       if ((sp->defined & ~N_EXT) == N_SETV)
  3101.         {
  3102.           unsigned long length_word_index =
  3103.         (sp->value - set_sect_start) / sizeof (unsigned long);
  3104.               unsigned long i, tmp;
  3105.  
  3106.           set_vectors[length_word_index]
  3107.             = setv_fill_count - 1 - length_word_index;
  3108.             
  3109.           /* Reverse the vector. */
  3110.           for (i = 1;
  3111.                i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  3112.                i++)
  3113.             {
  3114.               tmp = set_vectors[length_word_index + i];
  3115.               set_vectors[length_word_index + i]
  3116.                 = set_vectors[setv_fill_count - i];
  3117.               set_vectors[setv_fill_count - i] = tmp;
  3118.             }
  3119.             
  3120.           set_vectors[setv_fill_count++] = 0;
  3121.         }
  3122.       if (sp->defined)
  3123.         defined_global_sym_count++;
  3124.     }
  3125.     }
  3126.  
  3127.   if (end_symbol)        /* These are null if -r.  */
  3128.     {
  3129.       etext_symbol->value = text_size + text_start;
  3130.       edata_symbol->value = data_start + data_size;
  3131.       end_symbol->value = data_start + data_size + bss_size;
  3132.     }
  3133.  
  3134.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  3135.  
  3136.   if (specified_data_size && specified_data_size > data_size)
  3137.     data_pad = specified_data_size - data_size;
  3138.  
  3139.   if (magic == ZMAGIC)
  3140.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  3141.                - data_size;
  3142.  
  3143.   bss_size -= data_pad;
  3144.   if (bss_size < 0) bss_size = 0;
  3145.  
  3146.   data_size += data_pad;
  3147. }
  3148.  
  3149. /* Accumulate the section sizes of input file ENTRY
  3150.    into the section sizes of the output file.  */
  3151.  
  3152. void
  3153. consider_file_section_lengths (entry)
  3154.      register struct file_entry *entry;
  3155. {
  3156.   if (entry->just_syms_flag)
  3157.     return;
  3158.  
  3159.   entry->text_start_address = text_size;
  3160.   /* If there were any vectors, we need to chop them off */
  3161. #if 0
  3162.   /* There should never be set vectors in input to the loader */
  3163.   if (entry->set_vector_offset == -1)
  3164. #endif
  3165.   text_size += entry->header.a_text;
  3166. #if 0
  3167.   else
  3168.     text_size += entry->set_vector_offset - N_TXTOFF (entry->header);
  3169. #endif
  3170.   entry->data_start_address = data_size;
  3171.   data_size += entry->header.a_data;
  3172.   entry->bss_start_address = bss_size;
  3173.   bss_size += entry->header.a_bss;
  3174.  
  3175. /* dont know if this is still needed, left it in for safety sake ++jrb */
  3176. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  3177. /* something's probably wrong elsewhere that forces this kludge.
  3178.    if bss_size turns out to be odd, add one to it */
  3179.   if (bss_size & 1)
  3180.     bss_size++;
  3181. #endif
  3182.  
  3183.   text_reloc_size += entry->header.a_trsize;
  3184.   data_reloc_size += entry->header.a_drsize;
  3185. }
  3186.  
  3187. /* Determine where the sections of ENTRY go into the output file,
  3188.    whose total section sizes are already known.
  3189.    Also relocate the addresses of the file's local and debugger symbols.  */
  3190.  
  3191. void
  3192. relocate_file_addresses (entry)
  3193.      register struct file_entry *entry;
  3194. {
  3195.   entry->text_start_address += text_start;
  3196.   /* Note that `data_start' and `data_size' have not yet been
  3197.      adjusted for `data_pad'.  If they had been, we would get the wrong
  3198.      results here.  */
  3199.   entry->data_start_address += data_start;
  3200.   entry->bss_start_address += data_start + data_size;
  3201.  
  3202.   {
  3203.     register struct nlist *p;
  3204.     register struct nlist *end
  3205.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  3206.  
  3207.     for (p = entry->symbols; p < end; p++)
  3208.       {
  3209.     /* If this belongs to a section, update it by the section's start address */
  3210.     register int type = p->n_type & N_TYPE;
  3211.  
  3212.     switch (type)
  3213.       {
  3214.       case N_TEXT:
  3215.       case N_SETT:
  3216.         p->n_value += entry->text_start_address;
  3217.         break;
  3218.       case N_DATA:
  3219.       case N_SETV:
  3220.       case N_SETD:
  3221.         /* A symbol whose value is in the data section
  3222.            is present in the input file as if the data section
  3223.            started at an address equal to the length of the file's text.  */
  3224.         p->n_value += entry->data_start_address - entry->header.a_text;
  3225.         break;
  3226.       case N_BSS:
  3227.       case N_SETB:
  3228.         /* likewise for symbols with value in BSS.  */
  3229.         p->n_value += entry->bss_start_address
  3230.           - entry->header.a_text - entry->header.a_data;
  3231.         break;
  3232.       }
  3233.       }
  3234.   }
  3235. }
  3236.  
  3237. void describe_file_sections (), list_file_locals ();
  3238.  
  3239. /* Print a complete or partial map of the output file.  */
  3240.  
  3241. void
  3242. print_symbols (outfile)
  3243.      FILE *outfile;
  3244. {
  3245.   register int i;
  3246.  
  3247.   fprintf (outfile, "\nFiles:\n\n");
  3248.  
  3249.   each_file (describe_file_sections, outfile);
  3250.  
  3251.   fprintf (outfile, "\nGlobal symbols:\n\n");
  3252.  
  3253.   for (i = 0; i < TABSIZE; i++)
  3254.     {
  3255.       register symbol *sp;
  3256.       for (sp = symtab[i]; sp; sp = sp->link)
  3257.     {
  3258.       char *nm = NULL;
  3259.       if ((sp->defined || sp->referenced) && (demangler != NULL))
  3260.         nm = (*demangler)(sp->name);
  3261.       if(nm == NULL)
  3262.           nm = sp->name;
  3263.       if (sp->defined == 1)
  3264.         fprintf (outfile, "  %s: common, length 0x%x\n", nm, sp->max_common_size);
  3265.       if (sp->defined)
  3266.         fprintf (outfile, "  %s: 0x%x\n", nm, sp->value);
  3267.       else if (sp->referenced)
  3268.         fprintf (outfile, "  %s: undefined\n", nm);
  3269.       if(nm != sp->name)
  3270.         free(nm);
  3271.     }
  3272.     }
  3273.  
  3274.   each_file (list_file_locals, outfile);
  3275. }
  3276.  
  3277. void
  3278. describe_file_sections (entry, outfile)
  3279.      struct file_entry *entry;
  3280.      FILE *outfile;
  3281. {
  3282.   fprintf (outfile, "  ");
  3283.   print_file_name (entry, outfile);
  3284.   if (entry->just_syms_flag)
  3285.     fprintf (outfile, " symbols only\n");
  3286.   else
  3287.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  3288.          entry->text_start_address, entry->header.a_text,
  3289.          entry->data_start_address, entry->header.a_data,
  3290.          entry->bss_start_address, entry->header.a_bss);
  3291. }
  3292.  
  3293. void
  3294. list_file_locals (entry, outfile)
  3295.      struct file_entry *entry;
  3296.      FILE *outfile;
  3297. {
  3298.   register struct nlist *p, *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  3299.  
  3300.   entry->strings = (char *) alloca (entry->string_size);
  3301.   read_entry_strings (file_open (entry), entry);
  3302.  
  3303.   fprintf (outfile, "\nLocal symbols of ");
  3304.   print_file_name (entry, outfile);
  3305.   fprintf (outfile, ":\n\n");
  3306.  
  3307.   for (p = entry->symbols; p < end; p++)
  3308.     /* If this is a definition,
  3309.        update it if necessary by this file's start address.  */
  3310.     if (!((p->n_type & (N_STAB | N_EXT)) || SET_ELEMENT_P(p->n_type)))
  3311.     {
  3312.       char *s = entry->strings + p->n_un.n_strx;
  3313.       char *nm;
  3314.  
  3315.       if (demangler == NULL || (nm = (*demangler)(s)) == NULL)
  3316.           nm = s;
  3317.       fprintf (outfile, "  %s: 0x%x\n", nm, p->n_value);
  3318.       if(nm != s)
  3319.     free(nm);
  3320.       }
  3321.  
  3322.   entry->strings = 0;        /* All done with them.  */
  3323. }
  3324.  
  3325.  
  3326. /* Static vars for do_warnings and subroutines of it */
  3327. int list_unresolved_refs;    /* List unresolved refs */
  3328. int list_warning_symbols;    /* List warning syms */
  3329. int list_multple_defs;        /* List multiple definitions */
  3330.  
  3331.  
  3332. void qsort ();
  3333.  
  3334. struct line_debug_entry *init_debug_scan ();
  3335. int next_debug_entry ();
  3336. int address_to_line ();
  3337. int relocation_entries_relation ();
  3338.  
  3339. /* Print on OUTFILE a list of all warnings generated by references
  3340.    and/or definitions in the file ENTRY.  List source file and line
  3341.    number if possible, just the .o file if not. */
  3342.  
  3343. void
  3344. do_file_warnings (entry, outfile)
  3345.      struct file_entry *entry;
  3346.      FILE *outfile;
  3347. {
  3348.   struct relocation_info *txt_reloc, *data_reloc;
  3349.   struct line_debug_entry *state_pointer;
  3350.   register struct line_debug_entry *current, *next, *source;
  3351.   char *errfmt;                /* Assigned to generally */
  3352.                     /* static values; should not */
  3353.                     /* be written into */
  3354.   char *errmsg;                /* Assigned to malloc'd values */
  3355.                     /* and copied into; should be */
  3356.                     /* freed when done */
  3357.   int invalidate_line_number;
  3358.  
  3359.   /* Read in the files strings if they aren't available */
  3360.   if (!entry->strings)
  3361.     {
  3362.       FILE *desc;
  3363.  
  3364.       entry->strings = (char *) alloca (entry->string_size);
  3365.       desc = file_open (entry);
  3366.       read_entry_strings (desc, entry);
  3367.     }
  3368.  
  3369.   state_pointer = init_debug_scan (0, entry);
  3370.   current = state_pointer;
  3371.   next = state_pointer + 1;
  3372.   source = state_pointer + 2;
  3373.  
  3374.   read_file_relocation (entry);
  3375.  
  3376.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3377.      for one lousy error optimization. */
  3378.  
  3379.   qsort (entry->textrel,
  3380.      entry->header.a_trsize/sizeof (struct relocation_info),
  3381.      sizeof (struct relocation_info),
  3382.      relocation_entries_relation);
  3383.  
  3384.   for (txt_reloc = entry->textrel;
  3385.        txt_reloc < (entry->textrel
  3386.             + entry->header.a_trsize/sizeof (struct relocation_info));
  3387.        txt_reloc++)
  3388.     {
  3389.       register struct nlist *s;
  3390.       register symbol *g;
  3391.       int s_index;
  3392.  
  3393.       /* If the relocation isn't resolved through a symbol, continue */
  3394.       if (!RELOC_EXTERN_P(txt_reloc))
  3395.     continue;
  3396.  
  3397.       s_index = RELOC_SYMBOL(txt_reloc);
  3398.       if (s_index < 0 )
  3399.     fatal_with_file ("bad symbol in relocation table of ", entry);
  3400.       s = &entry->symbols[s_index];
  3401.  
  3402.       /* Local symbols shouldn't ever be used by relocation info, so
  3403.      the next should be safe.
  3404.      This is, of course, wrong.  References to local BSS symbols can be
  3405.      the targets of relocation info, and they can (must) be
  3406.      resolved through symbols.  However, these must be defined properly,
  3407.      (the compiler would have caught it otherwise), so we can
  3408.      ignore these cases.  */
  3409.       if (!(s->n_type & N_EXT))
  3410.     continue;
  3411.  
  3412.       g = (symbol *) s->n_un.n_name;
  3413.  
  3414.       if (!g->defined)                 /* Reference */
  3415.     {
  3416.       if (!list_unresolved_refs ||             /* Don't list any */
  3417.           g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3418.         continue;
  3419.  
  3420.       /* Undefined symbol which we should mention */
  3421.  
  3422.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3423.         {
  3424.           errfmt = "More undefined symbol %s refs follow";
  3425.           invalidate_line_number = 1;
  3426.         }
  3427.       else
  3428.         {
  3429.           errfmt = "Undefined symbol %s referenced from text";
  3430.           invalidate_line_number = 0;
  3431.         }
  3432.     }
  3433.       else                         /* Defined */
  3434.     {
  3435.       /* Potential symbol warning here */
  3436.       if (!g->warning) continue;
  3437.  
  3438.       errfmt = g->warning;
  3439.       invalidate_line_number = 0;
  3440.     }
  3441.  
  3442.       /* If errfmt == 0, errmsg has already been defined.  */
  3443.       if (errfmt != 0)
  3444.     {
  3445.       char *nm;
  3446.  
  3447.       if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL)
  3448.         nm = g->name;
  3449.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1);
  3450.       sprintf (errmsg, errfmt, nm);
  3451.       if (nm != g->name)
  3452.         free (nm);
  3453.     }
  3454.  
  3455.       address_to_line ( (RELOC_ADDRESS (txt_reloc)
  3456.              + entry->text_start_address),
  3457.                state_pointer);
  3458.  
  3459.       if (current->line >=0)
  3460.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3461.          invalidate_line_number ? 0 : current->line, errmsg);
  3462.       else
  3463.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3464.  
  3465.       free (errmsg);
  3466.     }
  3467.  
  3468.   free (state_pointer);
  3469.   state_pointer = init_debug_scan (1, entry);
  3470.   current = state_pointer;
  3471.   next = state_pointer + 1;
  3472.   source = state_pointer + 2;
  3473.  
  3474.   /*
  3475.    * Once again, time to sort the relocation info.
  3476.    */
  3477.  
  3478.   qsort (entry->datarel,
  3479.      entry->header.a_drsize/sizeof (struct relocation_info),
  3480.      sizeof (struct relocation_info),
  3481.      relocation_entries_relation);
  3482.  
  3483.   for (data_reloc = entry->datarel;
  3484.        data_reloc < (entry->datarel
  3485.              + entry->header.a_drsize/sizeof (struct relocation_info));
  3486.        data_reloc++)
  3487.     {
  3488.       register struct nlist *s;
  3489.       register symbol *g;
  3490.       int s_index;
  3491.       
  3492.       /* If the relocation isn't resolved through a symbol, continue */
  3493.       if (!RELOC_EXTERN_P(data_reloc))
  3494.     continue;
  3495.  
  3496.       s_index = RELOC_SYMBOL(data_reloc);
  3497.       if (s_index < 0 )
  3498.     fatal_with_file ("bad symbol in relocation table of ", entry);
  3499.       s = &entry->symbols[s_index];
  3500.  
  3501.       g = (symbol *) s->n_un.n_name;
  3502.  
  3503.       if (!g->defined)                 /* Reference */
  3504.     {
  3505.       if (!list_unresolved_refs ||             /* Don't list any */
  3506.           g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3507.         continue;
  3508.  
  3509.       /* Undefined symbol which we should mention */
  3510.  
  3511.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3512.         {
  3513.           errfmt = "More undefined symbol %s refs follow";
  3514.           invalidate_line_number = 1;
  3515.         }
  3516.       else
  3517.         {
  3518.           errfmt = "Undefined symbol %s referenced from data";
  3519.           invalidate_line_number = 0;
  3520.         }
  3521.     }
  3522.       else                         /* Defined */
  3523.     {
  3524.       /* Potential symbol warning here */
  3525.       if (!g->warning) continue;
  3526.  
  3527.       errfmt = g->warning;
  3528.       invalidate_line_number = 0;
  3529.     }
  3530.       
  3531.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  3532.       sprintf(errmsg, errfmt, g->name);
  3533.       address_to_line ( (RELOC_ADDRESS (data_reloc)
  3534.              + entry->text_start_address),
  3535.                state_pointer);
  3536.  
  3537.       if (current->line >=0)
  3538.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3539.          invalidate_line_number ? 0 : current->line, errmsg);
  3540.       else
  3541.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3542.  
  3543.       free (errmsg);
  3544.     }
  3545.  
  3546.   /* now scan in the non-tex/data sections for undefs */
  3547.   {  
  3548.       struct nlist *s,
  3549.       *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  3550.       symbol *g;
  3551.       
  3552.       for (s = entry->symbols; s < end; s++)
  3553.       {
  3554.           if (!(s->n_type & N_EXT))
  3555.               continue;
  3556.  
  3557.           g = (symbol *) s->n_un.n_name;
  3558.  
  3559.           if(g->defined)
  3560.               continue;
  3561.  
  3562.           if (g->referenced && (g->undef_refs == 0))
  3563.           {
  3564.               char *nm = NULL;
  3565.               if(demangler)
  3566.                   nm = (*demangler)(g->name);
  3567.               if(nm == NULL)
  3568.                   nm = g->name;
  3569.               print_file_name (entry, outfile);
  3570.               fprintf (outfile, ": Undefined symbol \"%s\"\n", nm);
  3571.               if(nm != g->name) free(nm);
  3572.           }
  3573.       }
  3574.   }
  3575.  
  3576.   entry->strings = 0;
  3577. }
  3578. /*
  3579.  * Helper routines for do_file_warnings.
  3580.  */
  3581.  
  3582. /*
  3583.  * Return an integer less than, equal to, or greater than 0 as per the
  3584.  * relation between the two relocation entries.
  3585.  * Used by qsort.
  3586.  */
  3587. int
  3588. relocation_entries_relation (rel1, rel2)
  3589.      struct relocation_info *rel1, *rel2;
  3590. {
  3591.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  3592. }
  3593.  
  3594. /*
  3595.  * Takes the values in state_pointer and moves onward to the next
  3596.  * debug line symbol.  It assumes that state_pointer[1] is valid; ie
  3597.  * that it.sym points into some entry in the symbol table.  If
  3598.  * state_pointer[1].sym == 0, this routine should not be called.
  3599.  */
  3600.  
  3601. int
  3602. next_debug_entry (use_data_symbols, state_pointer)
  3603.      register int use_data_symbols;
  3604.      struct line_debug_entry state_pointer[3]; /* Must be passed by reference! */
  3605. {
  3606.   register struct line_debug_entry
  3607.     *current = state_pointer,
  3608.     *next = state_pointer + 1,
  3609.     *source = state_pointer + 2; /* Used to store source file */
  3610.   struct file_entry *entry = (struct file_entry *) source->sym;
  3611.  
  3612.   current->sym = next->sym;
  3613.   current->line = next->line;
  3614.   current->filename = next->filename;
  3615.  
  3616.   while (++(next->sym) < (entry->symbols
  3617.               + entry->header.a_syms/sizeof (struct nlist)))
  3618.     {
  3619.       switch (next->sym->n_type)
  3620.     {
  3621.     case N_SLINE:
  3622.       if (use_data_symbols) continue;
  3623.       next->line = next->sym->n_desc;
  3624.       return 1;
  3625.     case N_DSLINE:
  3626.       if (!use_data_symbols) continue;
  3627.       next->line = next->sym->n_desc;
  3628.       return 1;
  3629. #ifdef HAVE_SUN_STABS
  3630.     case N_EINCL:
  3631.       next->filename = source->filename;
  3632.       continue;
  3633. #endif
  3634.     case N_SO:
  3635.       source->filename = next->sym->n_un.n_strx + entry->strings;
  3636.       source->line++;
  3637. #ifdef HAVE_SUN_STABS
  3638.     case N_BINCL:
  3639. #endif
  3640.     case N_SOL:
  3641.       next->filename =
  3642.         next->sym->n_un.n_strx + entry->strings;
  3643.     default:
  3644.       continue;
  3645.     }
  3646.     }
  3647.   next->sym = (struct nlist *) 0;
  3648.   return 0;
  3649. }
  3650.  
  3651. int
  3652. address_to_line (address, state_pointer)
  3653.      unsigned long address;
  3654.      /* Next must be passed by reference! */
  3655.      struct line_debug_entry state_pointer[3];
  3656. {
  3657.   struct line_debug_entry
  3658.     *current = state_pointer,
  3659.     *next = state_pointer + 1;
  3660.   struct line_debug_entry *tmp_pointer;
  3661.  
  3662.   int use_data_symbols;
  3663.  
  3664.   if (next->sym)
  3665.     use_data_symbols = (next->sym->n_type & ~N_EXT) == N_DATA;
  3666.   else
  3667.     return current->line;
  3668.  
  3669.   /* Go back to the beginning if we've already passed it.  */
  3670.   if (current->sym->n_value > address)
  3671.     {
  3672.       tmp_pointer = init_debug_scan (use_data_symbols,
  3673.                      (struct file_entry *)
  3674.                      ((state_pointer + 2)->sym));
  3675.       state_pointer[0] = tmp_pointer[0];
  3676.       state_pointer[1] = tmp_pointer[1];
  3677.       state_pointer[2] = tmp_pointer[2];
  3678.       free (tmp_pointer);
  3679.     }
  3680.  
  3681.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  3682.   if (current->sym->n_value > address)
  3683.     return -1;
  3684.  
  3685.   while (next->sym
  3686.      && next->sym->n_value <= address
  3687.      && next_debug_entry (use_data_symbols, state_pointer))
  3688.     ;
  3689.   return current->line;
  3690. }
  3691.  
  3692. struct line_debug_entry *
  3693. init_debug_scan (use_data_symbols, entry)
  3694.      int use_data_symbols;
  3695.      struct file_entry *entry;
  3696. {
  3697.   struct line_debug_entry
  3698.     *state_pointer =
  3699.       (struct line_debug_entry *) xmalloc (3 * sizeof (struct line_debug_entry));
  3700.   register struct line_debug_entry
  3701.     *current = state_pointer,
  3702.     *next = state_pointer + 1,
  3703.     *source = state_pointer + 2; /* Used to store source file */
  3704.  
  3705.   struct nlist *tmp;
  3706.  
  3707.   for (tmp = entry->symbols;
  3708.        tmp < (entry->symbols
  3709.           + entry->header.a_syms/sizeof (struct nlist));
  3710.        tmp++)
  3711.     if (tmp->n_type == (int) N_SO) break;
  3712.  
  3713.   if (tmp >= (entry->symbols
  3714.           + entry->header.a_syms/sizeof (struct nlist)))
  3715.     {
  3716.       /* I believe this translates to "We lose" */
  3717.       current->filename = next->filename = entry->filename;
  3718.       current->line = next->line = -1;
  3719.       current->sym = next->sym = (struct nlist *) 0;
  3720.       return state_pointer;
  3721.     }
  3722.  
  3723.   next->line = source->line = 0;
  3724.   next->filename = source->filename
  3725.     = (tmp->n_un.n_strx + entry->strings);
  3726.   source->sym = (struct nlist *) entry;
  3727.   next->sym = tmp;
  3728.  
  3729.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  3730.  
  3731.   if (!next->sym)        /* No line numbers for this section; */
  3732.                 /* setup output results as appropriate */
  3733.     {
  3734.       if (source->line)
  3735.     {
  3736.       current->filename = source->filename = entry->filename;
  3737.       current->line = -1;    /* Don't print lineno */
  3738.     }
  3739.       else
  3740.     {
  3741.       current->filename = source->filename;
  3742.       current->line = 0;
  3743.     }
  3744.       return state_pointer;
  3745.     }
  3746.  
  3747.  
  3748.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  3749.  
  3750.   return state_pointer;
  3751. }
  3752.  
  3753. void 
  3754. mark_flagged_symbols (entry, outfile)
  3755.      struct file_entry *entry;
  3756.      FILE *outfile;
  3757. {
  3758.   struct nlist *p;
  3759.   
  3760.   if (!entry->warning) return;
  3761.  
  3762.   for (p = entry->symbols;
  3763.        p < entry->symbols + (entry->header.a_syms / sizeof(struct nlist));
  3764.        p++)
  3765.     {
  3766.       unsigned char type = p->n_type;
  3767.       struct glosym *sym;
  3768.       
  3769.      /* Ignore locals and references */
  3770.       if (!(type & N_EXT) || type == N_EXT) continue;
  3771.  
  3772.       sym = (struct glosym *) p->n_un.n_name;
  3773.       if (sym->referenced)
  3774.     ((struct glosym *) p->n_un.n_name)->warning = entry->warning;
  3775.     }
  3776. }
  3777.  
  3778. void
  3779. do_warnings (outfile)
  3780.      FILE *outfile;
  3781. {
  3782.   struct glosym **clrefs;
  3783.  
  3784.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3785.   list_warning_symbols = warning_count;
  3786.   list_multple_defs = 0;        /* Not currently done here */
  3787.  
  3788.   if (!(list_unresolved_refs ||
  3789.     list_warning_symbols ||
  3790.     list_multple_defs      ))
  3791.     /* No need to run this routine */
  3792.     return;
  3793.  
  3794.   if (list_warning_symbols)
  3795.     each_full_file (mark_flagged_symbols, outfile);
  3796.  
  3797.   each_full_file (do_file_warnings, outfile);
  3798.  
  3799.   if (!relocatable_output)
  3800.     for (clrefs = cmdline_references;
  3801.      clrefs < cmdline_references + cl_refs_allocated && *clrefs;
  3802.      clrefs++)
  3803.       if ((*clrefs)->referenced && !(*clrefs)->defined)
  3804.     fprintf(stderr, "Error: Unresolved reference to symbol %s\n",
  3805.         (*clrefs)->name);
  3806.  
  3807.   if (entry_symbol && !entry_symbol->defined) 
  3808.     fprintf(stderr, "%s: error: Entry symbol `%s' never defined.\n",
  3809.         progname, entry_symbol->name);
  3810.   
  3811.   fprintf (outfile, "\n");
  3812.  
  3813.   if (list_unresolved_refs || list_multple_defs)
  3814.     make_executable = 0;
  3815. }
  3816.  
  3817. /* Print the files which have the definitions for a given symbol.
  3818.    FIRST_DEF is the first nlist entry which defines the symbol, and
  3819.    REST_OF_REFS is a chain of nlist entries which may or may not be
  3820.    definitions for this symbol, but which are all references for the
  3821.    symbol.
  3822.  
  3823.    We do the job in this clumsy fashion because of the state our data
  3824.    structures are in in the middle of digest_symbols (from which this
  3825.    is called). */
  3826.  
  3827. void
  3828. print_files_defining_symbol (first_def, rest_of_refs)
  3829.      struct nlist *first_def, *rest_of_refs;
  3830. {
  3831.   struct file_entry *holder;
  3832.   struct nlist *n_ptr;
  3833.  
  3834.   if (holder =
  3835.       (struct file_entry *) check_each_file (contains_symbol, first_def))
  3836.     {
  3837.       fprintf (stderr, " ");
  3838.       prline_file_name (holder, stderr);
  3839.     }
  3840.   else
  3841.     fatal ("internal: file not found containing nlist entry", 0);
  3842.  
  3843.   for (n_ptr = rest_of_refs;
  3844.        n_ptr;
  3845.        n_ptr = (struct nlist *) n_ptr->n_un.n_name)
  3846.     if (n_ptr->n_type & ~N_EXT)
  3847.       if (holder =
  3848.       (struct file_entry *) check_each_file (contains_symbol, n_ptr))
  3849.     {
  3850.       fprintf (stderr, " ");
  3851.       prline_file_name (holder, stderr);
  3852.     }
  3853.       else
  3854.     fatal ("internal: file not found containing nlist entry", 0);
  3855. }
  3856.  
  3857. /* Write the output file */
  3858.  
  3859. void
  3860. write_output ()
  3861. {
  3862.   struct stat statbuf;
  3863.   int filemode;
  3864.  
  3865. /* #ifdef __OLD_OPEN_P__ */
  3866. #if defined(atariminix)
  3867.   /* this is for the benefit people who have a minix kernel without 3 arg open
  3868.      support (ie. no poole fs). previously it was taken care of
  3869.      by emul_open3, but i threw that away, and now assume you have
  3870.      open3 support in the kernel. so this code is just a quick and
  3871.      dirty hack to get a working gcc-ld so that you can then bootstrap
  3872.      a kernel with the poole fs
  3873.   */
  3874.   unlink(output_filename);    /* never mind if it does'nt exist */
  3875.   outdesc = fopen (output_filename, "w+");
  3876.   if (outdesc == NULL) perror_name (output_filename);
  3877. #else
  3878.   outdesc = fopen (output_filename, "w+");
  3879.   if (outdesc == NULL) perror_name (output_filename);
  3880. #endif
  3881.  
  3882. #ifdef _IOFBF
  3883.  if(setvbuf(outdesc, NULL, _IOFBF, 128L * 1024))
  3884.     perror_name(output_filename);
  3885. #endif
  3886.  
  3887.   if (fstat (fileno(outdesc), &statbuf) < 0)
  3888.     perror_name (output_filename);
  3889.  
  3890.   filemode = statbuf.st_mode;
  3891.  
  3892. #ifndef atarist
  3893.   chmod (output_filename, filemode & ~0111);
  3894. #endif
  3895.  
  3896.   /* Output the a.out header.  */
  3897.   write_header ();
  3898.  
  3899.   /* Output the text and data segments, relocating as we go.  */
  3900.   write_text ();
  3901.   write_data ();
  3902.  
  3903. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3904.   /* Output the merged relocation info, if requested with `-r'.  */
  3905.   if (relocatable_output)
  3906.     write_rel ();
  3907.  
  3908.   /* Output the symbol table (both globals and locals).  */
  3909.   write_syms ();
  3910. #else
  3911. /* atari has symtab section before relocs */
  3912.   write_syms();
  3913.   write_rel();
  3914.   if (gst_symboltable && gst_symbols) 
  3915.     {
  3916.       fseek (outdesc, 0L, 0);    /* seek to beginning of file */
  3917.       write_header ();        /* rewrite header */
  3918.     }
  3919. #endif
  3920.  
  3921.   /* Copy any GDB symbol segments from input files.  */
  3922. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3923.   /* see sym-ld.c for atari */
  3924.   write_symsegs ();
  3925. #endif
  3926.  
  3927.   fclose (outdesc);
  3928.  
  3929. #ifndef atarist
  3930.   chmod (output_filename, filemode | 0111);
  3931. #endif
  3932.  
  3933. #if (defined(MINIX) || defined(atariminix))
  3934.   /* write out.h format stuff out for MDB if requested */
  3935.   if(write_minix_out == 1)
  3936.       do_write_minix_out();
  3937. #endif  
  3938. }
  3939.  
  3940. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3941.  
  3942. void
  3943. write_header ()
  3944. {
  3945. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3946.   outheader.a_magic = magic;
  3947. #endif
  3948.   outheader.a_text = text_size;
  3949.   outheader.a_data = data_size;
  3950.   outheader.a_bss = bss_size;
  3951. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3952.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3953.                : text_start + entry_offset);
  3954. #else
  3955.  
  3956. #if (defined(MINIX) || defined(atariminix))
  3957.   outheader.a_versn = A_VERSION;
  3958.   outheader.a_entry = 0;
  3959.   {
  3960.       register long stack = 0x00010000L - (outheader.a_text +
  3961.                            outheader.a_data +
  3962.                            outheader.a_bss);
  3963.       while(stack < 0) stack += 0x00010000L;
  3964.       
  3965.       outheader.a_msize = stack + (outheader.a_text + outheader.a_data +
  3966.                        outheader.a_bss);
  3967.   }
  3968.   
  3969. #else 
  3970.   /* TOS */
  3971.   outheader.a_AZero1 = 0;        /* good a place as any to do this */
  3972.   outheader.a_ldflgs = 0;        /* fastload is NOT the default */
  3973.   if (magic == NMAGIC)
  3974.     outheader.a_ldflgs = F_SHTEXT;    /* sharable text */
  3975.   outheader.a_isreloc = ISRELOCINFO;    /* means reloc present (!) */
  3976. #endif
  3977. #endif
  3978.  
  3979. #ifdef COFF_ENCAPSULATE
  3980.   if (need_coff_header)
  3981.     {
  3982.       /* We are encapsulating BSD format within COFF format.  */
  3983.       struct coffscn *tp, *dp, *bp;
  3984.  
  3985.       tp = &coffheader.scns[0];
  3986.       dp = &coffheader.scns[1];
  3987.       bp = &coffheader.scns[2];
  3988.  
  3989.       strcpy (tp->s_name, ".text");
  3990.       tp->s_paddr = text_start;
  3991.       tp->s_vaddr = text_start;
  3992.       tp->s_size = text_size;
  3993.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3994.       tp->s_relptr = 0;
  3995.       tp->s_lnnoptr = 0;
  3996.       tp->s_nreloc = 0;
  3997.       tp->s_nlnno = 0;
  3998.       tp->s_flags = 0x20;
  3999.       strcpy (dp->s_name, ".data");
  4000.       dp->s_paddr = data_start;
  4001.       dp->s_vaddr = data_start;
  4002.       dp->s_size = data_size;
  4003.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  4004.       dp->s_relptr = 0;
  4005.       dp->s_lnnoptr = 0;
  4006.       dp->s_nreloc = 0;
  4007.       dp->s_nlnno = 0;
  4008.       dp->s_flags = 0x40;
  4009.       strcpy (bp->s_name, ".bss");
  4010.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  4011.       bp->s_vaddr = bp->s_paddr;
  4012.       bp->s_size = bss_size;
  4013.       bp->s_scnptr = 0;
  4014.       bp->s_relptr = 0;
  4015.       bp->s_lnnoptr = 0;
  4016.       bp->s_nreloc = 0;
  4017.       bp->s_nlnno = 0;
  4018.       bp->s_flags = 0x80;
  4019.  
  4020.       coffheader.f_magic = COFF_MAGIC;
  4021.       coffheader.f_nscns = 3;
  4022.       coffheader.f_timdat = 0;
  4023.       coffheader.f_symptr = 0;
  4024.       coffheader.f_nsyms = 0;
  4025.       coffheader.f_opthdr = 28;
  4026.       coffheader.f_flags = 0x103;
  4027.       /* aouthdr */
  4028.       coffheader.magic = ZMAGIC;
  4029.       coffheader.vstamp = 0;
  4030.       coffheader.tsize = tp->s_size;
  4031.       coffheader.dsize = dp->s_size;
  4032.       coffheader.bsize = bp->s_size;
  4033.       coffheader.entry = outheader.a_entry;
  4034.       coffheader.text_start = tp->s_vaddr;
  4035.       coffheader.data_start = dp->s_vaddr;
  4036.     }
  4037. #endif
  4038.  
  4039. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4040. #ifdef INITIALIZE_HEADER
  4041.   INITIALIZE_HEADER;
  4042. #endif
  4043. #endif
  4044.  
  4045.   if (strip_symbols == STRIP_ALL)
  4046.     nsyms = 0;
  4047.   else
  4048.     {
  4049.       nsyms = (defined_global_sym_count
  4050.            + undefined_global_sym_count);
  4051.       if (discard_locals == DISCARD_L)
  4052.     nsyms += non_L_local_sym_count;
  4053.       else if (discard_locals == DISCARD_NONE)
  4054.     nsyms += local_sym_count;
  4055.       /* One extra for following reference on indirects */
  4056.       if (relocatable_output)
  4057.     nsyms += set_symbol_count + global_indirect_count;
  4058. #if defined(atarist) || defined(CROSSATARI)
  4059.       nsyms += set_symbol_count;  /* Cos we always output them if possible */
  4060. #endif
  4061.     }
  4062.  
  4063.   if (strip_symbols == STRIP_NONE)
  4064.     nsyms += debugger_sym_count;
  4065.  
  4066. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4067.   outheader.a_syms = nsyms * sizeof (struct nlist);
  4068. #else
  4069. # ifndef WORD_ALIGNED
  4070.   outheader.a_syms = (nsyms + gst_symbols) * sizeof (struct asym);
  4071. # else
  4072. #   ifdef MINIX
  4073.       outheader.a_syms = (nsyms + gst_symbols) * 16;
  4074. #   else
  4075.       outheader.a_syms = (nsyms + gst_symbols) * 14;
  4076. #   endif
  4077. # endif
  4078. #endif
  4079.  
  4080. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4081.   if (relocatable_output)
  4082.     {
  4083.       outheader.a_trsize = text_reloc_size;
  4084.       outheader.a_drsize = data_reloc_size;
  4085.     }
  4086.   else
  4087.     {
  4088.       outheader.a_trsize = 0;
  4089.       outheader.a_drsize = 0;
  4090.     }
  4091. #endif
  4092.  
  4093. #ifdef COFF_ENCAPSULATE
  4094.   if (need_coff_header)
  4095.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  4096. #endif
  4097. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4098.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  4099. #else
  4100.  
  4101. #ifdef BYTE_SWAP
  4102. outheader.a_magic   = SWAP2(outheader.a_magic);
  4103. outheader.a_text    = SWAP4(outheader.a_text);
  4104. outheader.a_data    = SWAP4(outheader.a_data);
  4105. outheader.a_bss     = SWAP4(outheader.a_bss);
  4106. outheader.a_syms    = SWAP4(outheader.a_syms);
  4107. outheader.a_AZero1  = SWAP4(outheader.a_AZero1);
  4108. outheader.a_ldflgs  = SWAP4(outheader.a_ldflgs); /* only do this once */
  4109. outheader.a_isreloc = SWAP2(outheader.a_isreloc);
  4110. #endif /* BYTE_SWAP */
  4111.  
  4112. # ifndef WORD_ALIGNED
  4113.   mywrite ((char *)&outheader, sizeof outheader, 1, outdesc);
  4114. # else
  4115. #  ifndef MINIX
  4116.      mywrite (&outheader.a_magic  , sizeof outheader.a_magic  , 1, outdesc);
  4117.      mywrite (&outheader.a_text   , sizeof outheader.a_text   , 1, outdesc);
  4118.      mywrite (&outheader.a_data   , sizeof outheader.a_data   , 1, outdesc);
  4119.      mywrite (&outheader.a_bss    , sizeof outheader.a_bss    , 1, outdesc);
  4120.      mywrite (&outheader.a_syms   , sizeof outheader.a_syms   , 1, outdesc);
  4121.      mywrite (&outheader.a_AZero1 , sizeof outheader.a_AZero1 , 1, outdesc);
  4122.      mywrite (&outheader.a_AZero2 , sizeof outheader.a_AZero2 , 1, outdesc);
  4123.      mywrite (&outheader.a_isreloc, sizeof outheader.a_isreloc, 1, outdesc);
  4124. #  else
  4125.      mywrite (&outheader.a_magic  , sizeof outheader.a_magic  , 1, outdesc);
  4126.      mywrite (&outheader.a_versn  , sizeof outheader.a_versn  , 1, outdesc);
  4127.      mywrite (&outheader.a_text   , sizeof outheader.a_text   , 1, outdesc);
  4128.      mywrite (&outheader.a_data   , sizeof outheader.a_data   , 1, outdesc);
  4129.      mywrite (&outheader.a_bss    , sizeof outheader.a_bss    , 1, outdesc);
  4130.      mywrite (&outheader.a_entry  , sizeof outheader.a_entry  , 1, outdesc);
  4131.      mywrite (&outheader.a_msize  , sizeof outheader.a_msize  , 1, outdesc);
  4132.      mywrite (&outheader.a_syms   , sizeof outheader.a_syms   , 1, outdesc);
  4133. #  endif
  4134. # endif
  4135. #endif
  4136. #ifdef BYTE_SWAP    /* switch it back !!! */
  4137. outheader.a_magic   = SWAP2(outheader.a_magic);
  4138. outheader.a_text    = SWAP4(outheader.a_text);
  4139. outheader.a_data    = SWAP4(outheader.a_data);
  4140. outheader.a_bss     = SWAP4(outheader.a_bss);
  4141. outheader.a_syms    = SWAP4(outheader.a_syms);
  4142. outheader.a_AZero1  = SWAP4(outheader.a_AZero1);
  4143. outheader.a_ldflgs = SWAP4(outheader.a_ldflgs); /* only do this once */
  4144. outheader.a_isreloc = SWAP2(outheader.a_isreloc);
  4145. #endif /* BYTE_SWAP */
  4146.  
  4147.   /* Output whatever padding is required in the executable file
  4148.      between the header and the start of the text.  */
  4149.  
  4150. #ifndef COFF_ENCAPSULATE
  4151. #  if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4152.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  4153. #  endif
  4154. #endif
  4155. }
  4156.  
  4157. /* Relocate the text segment of each input file
  4158.    and write to the output file.  */
  4159.  
  4160. void
  4161. write_text ()
  4162. {
  4163.   if (trace_files)
  4164.     fprintf (stderr, "Copying and relocating text:\n\n");
  4165.  
  4166.   each_full_file (copy_text, 0);
  4167.   file_close ();
  4168.  
  4169.   /* Write out the set element vectors */
  4170.  
  4171.   if (set_vector_count)
  4172.     mywrite ((char *) set_vectors,
  4173.          set_symbol_count + 2 * set_vector_count,
  4174.          sizeof (unsigned long), outdesc);
  4175.  
  4176.   if (trace_files)
  4177.     fprintf (stderr, "\n");
  4178.  
  4179.   padfile (text_pad, outdesc);
  4180. }
  4181.  
  4182. int
  4183. text_offset (entry)
  4184.      struct file_entry *entry;
  4185. {
  4186.   return entry->starting_offset + N_TXTOFF (entry->header);
  4187. }
  4188.  
  4189. /* Read in all of the relocation information */
  4190.  
  4191. void
  4192. read_relocation ()
  4193. {
  4194.   each_full_file (read_file_relocation, 0);
  4195. }
  4196.  
  4197. /* Read in the relocation sections of ENTRY if necessary */
  4198.  
  4199. void
  4200. read_file_relocation (entry)
  4201.      struct file_entry *entry;
  4202. {
  4203.   register struct relocation_info *reloc;
  4204.   FILE *desc;
  4205.   int read_return;
  4206.  
  4207.   desc = NULL;
  4208.   if (!entry->textrel)
  4209.     {
  4210.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  4211.       desc = file_open (entry);
  4212.       fseek (desc,
  4213.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  4214.          0);
  4215.       if (entry->header.a_trsize != (read_return = fread (reloc, 1, entry->header.a_trsize, desc)))
  4216.     {
  4217.       fprintf (stderr, "Return from read: %d\n", read_return);
  4218.       fatal_with_file ("premature eof in text relocatino of ", entry);
  4219.     }
  4220.       entry->textrel = reloc;
  4221.     }
  4222.  
  4223.   if (!entry->datarel)
  4224.     {
  4225.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  4226.       if (desc == NULL) desc = file_open (entry);
  4227.       fseek (desc,
  4228.          text_offset (entry) + entry->header.a_text
  4229.          + entry->header.a_data + entry->header.a_trsize,
  4230.          0);
  4231.       if (entry->header.a_drsize != fread (reloc, 1, entry->header.a_drsize, desc))
  4232.     fatal_with_file ("premature eof in text relocation of ", entry);
  4233.       entry->datarel = reloc;
  4234.     }
  4235. }
  4236.  
  4237. /* Read the text segment contents of ENTRY, relocate them,
  4238.    and write the result to the output file.
  4239.    If `-r', save the text relocation for later reuse.  */
  4240.  
  4241. void
  4242. copy_text (entry)
  4243.      struct file_entry *entry;
  4244. {
  4245.   register char *bytes;
  4246.   register FILE *desc;
  4247.   register struct relocation_info *reloc;
  4248.  
  4249.   if (trace_files)
  4250.     prline_file_name (entry, stderr);
  4251.  
  4252.   desc = file_open (entry);
  4253.  
  4254.   /* Allocate space for the file's text section */
  4255.  
  4256.   bytes = (char *) alloca (entry->header.a_text);
  4257.  
  4258.   /* Deal with relocation information however is appropriate */
  4259.  
  4260.   if (entry->textrel)  reloc = entry->textrel;
  4261. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4262.   else if (1)        /* always keep it on atari */
  4263. #else
  4264.   else if (relocatable_output)
  4265. #endif
  4266.     {
  4267.       read_file_relocation (entry);
  4268.       reloc = entry->textrel;
  4269.     }
  4270.   else
  4271.     {
  4272.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  4273.       fseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  4274.       if (entry->header.a_trsize != fread (reloc, 1, entry->header.a_trsize, desc))
  4275.     fatal_with_file ("premature eof in text relocation of ", entry);
  4276.     }
  4277.  
  4278.   /* Read the text section into core.  */
  4279.  
  4280.   fseek (desc, text_offset (entry), 0);
  4281.   if (entry->header.a_text != fread ( bytes, 1, entry->header.a_text, desc))
  4282.     fatal_with_file ("premature eof in text section of ", entry);
  4283.  
  4284.  
  4285.   /* Relocate the text according to the text relocation.  */
  4286.  
  4287.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  4288.               reloc, entry->header.a_trsize, entry, (magic == NMAGIC));
  4289.  
  4290.   /* Write the relocated text to the output file.  */
  4291.  
  4292.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  4293. }
  4294.  
  4295. /* Relocate the data segment of each input file
  4296.    and write to the output file.  */
  4297.  
  4298. void
  4299. write_data ()
  4300. {
  4301.   if (trace_files)
  4302.     fprintf (stderr, "Copying and relocating data:\n\n");
  4303.  
  4304.   each_full_file (copy_data, 0);
  4305.   file_close ();
  4306.  
  4307.   if (trace_files)
  4308.     fprintf (stderr, "\n");
  4309.  
  4310.   padfile (data_pad, outdesc);
  4311. }
  4312.  
  4313. /* Read the data segment contents of ENTRY, relocate them,
  4314.    and write the result to the output file.
  4315.    If `-r', save the data relocation for later reuse.
  4316.    See comments in `copy_text'.  */
  4317.  
  4318. void
  4319. copy_data (entry)
  4320.      struct file_entry *entry;
  4321. {
  4322.   register struct relocation_info *reloc;
  4323.   register char *bytes;
  4324.   register FILE *desc;
  4325.  
  4326.   if (trace_files)
  4327.     prline_file_name (entry, stderr);
  4328.  
  4329.   desc = file_open (entry);
  4330.  
  4331.   bytes = (char *) alloca (entry->header.a_data);
  4332.  
  4333.   if (entry->datarel) reloc = entry->datarel;
  4334. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4335.   else if (1)
  4336. #else
  4337.   else if (relocatable_output)    /* Will need this again */
  4338. #endif
  4339.     {
  4340.       read_file_relocation (entry);
  4341.       reloc = entry->datarel;
  4342.     }
  4343.   else
  4344.     {
  4345.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  4346.       fseek (desc, text_offset (entry) + entry->header.a_text
  4347.          + entry->header.a_data + entry->header.a_trsize,
  4348.          0);
  4349.       if (entry->header.a_drsize != fread (reloc, 1, entry->header.a_drsize, desc))
  4350.     fatal_with_file ("premature eof in data relocation of ", entry);
  4351.     }
  4352.  
  4353.   fseek (desc, text_offset (entry) + entry->header.a_text, 0);
  4354.   if (entry->header.a_data != fread (bytes, 1, entry->header.a_data, desc))
  4355.     fatal_with_file ("premature eof in data section of ", entry);
  4356.  
  4357.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  4358.               entry->header.a_data, reloc, entry->header.a_drsize,
  4359.               entry, 0);
  4360.  
  4361.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  4362. }
  4363.  
  4364. static symbol *
  4365. retsym (p, entry)
  4366.      struct relocation_info *p;
  4367.      struct file_entry *entry;
  4368. {
  4369.   register int symindex;
  4370.   register symbol *sp;
  4371.  
  4372.   symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  4373.  
  4374.   if (symindex >= entry->header.a_syms)
  4375.     fatal_with_file ("relocation symbolnum out of range in ", entry);
  4376.  
  4377.   sp = ((symbol *) (((struct nlist *) (((char *)entry->symbols) + symindex))
  4378.     ->n_un.n_name));
  4379.  
  4380. #ifdef N_INDR
  4381.   /* Resolve indirection */
  4382.   if ((sp->defined & ~N_EXT) == N_INDR)
  4383.     sp = (symbol *) sp->value;
  4384. #endif
  4385.   return sp;
  4386. }
  4387.  
  4388. static char *
  4389. retsymname (p, entry, addr)
  4390.     struct relocation_info *p;
  4391.     struct file_entry *entry;
  4392.     int addr;
  4393. {
  4394.   register struct nlist *n;
  4395.  
  4396.   if (RELOC_EXTERN_P(p)) {
  4397.     register symbol *sp = retsym(p, entry);
  4398.     return sp->name;
  4399.   }
  4400.   for (n=entry->symbols;
  4401.     n<(struct nlist *)((long)entry->symbols + entry->header.a_syms); n++)
  4402.     if (n->n_value == addr)
  4403.     return(entry->strings + n->n_un.n_strx);
  4404.   return "(static symbol)";
  4405. }
  4406.  
  4407. /* Relocate ENTRY's text or data section contents.
  4408.    DATA is the address of the contents, in core.
  4409.    DATA_SIZE is the length of the contents.
  4410.    PC_RELOCATION is the difference between the address of the contents
  4411.      in the output file and its address in the input file.
  4412.    RELOC_INFO is the address of the relocation info, in core.
  4413.    RELOC_SIZE is its length in bytes.  */
  4414. /* This version is about to be severley hacked by Randy.  Hope it
  4415.    works afterwards. */
  4416. void
  4417. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size,
  4418.     entry, pure_text)
  4419.      char *data;
  4420.      struct relocation_info *reloc_info;
  4421.      struct file_entry *entry;
  4422.      int pc_relocation;
  4423.      int data_size;
  4424.      int reloc_size;
  4425.      int pure_text;
  4426. {
  4427.   register struct relocation_info *p = reloc_info;
  4428.   struct relocation_info *end
  4429.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  4430.   int text_relocation = entry->text_start_address;
  4431.   int data_relocation = entry->data_start_address - entry->header.a_text;
  4432.   int bss_relocation
  4433.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  4434.  
  4435.   for (; p < end; p++)
  4436.     {
  4437.       register int relocation = 0;
  4438.       register int addr;
  4439.       register unsigned int mask = 0;
  4440.       int oldreloc;
  4441.  
  4442. #ifdef BYTE_SWAP    /* swap relocation info */
  4443. { /* BITFIELD ALERT BITFIELD ALERT */
  4444. char *p1,*p2,p3;
  4445. p->r_address = SWAP4(p->r_address);
  4446. /* terrible hack to fix bit-fields !!! */
  4447. p1 = (char *)&p->r_address + 4;
  4448. p2 = p1+2;
  4449. p3= *p1; *p1= *p2; *p2=p3;    /* adjust r_symbolnum */
  4450. p1+= 3;                /* adjust everything else */
  4451. *p1 = ((*p1&0xf0)>>4)+((*p1&0x0f)<<4);    /* swap padding into correct spot */
  4452. *p1 = (*p1&0x6)+((*p1&0x8)>>3)+((*p1&0x1)<<3);    /* adjust other field */
  4453. }
  4454. #endif /* BYTE_SWAP */
  4455.  
  4456.       addr = RELOC_ADDRESS(p);
  4457.  
  4458.       if (addr >= data_size)
  4459.     fatal_with_file ("relocation address out of range in ", entry);
  4460.  
  4461.       if (RELOC_EXTERN_P(p))
  4462.     {
  4463.             register symbol *sp;
  4464.       sp = retsym(p, entry);
  4465.  
  4466.       /* If the symbol is undefined, leave it at zero.  */
  4467.       if (! sp->defined)
  4468.         relocation = 0;
  4469.       else
  4470.         relocation = sp->value;
  4471.     }
  4472.       else switch (RELOC_TYPE(p))
  4473.     {
  4474.     case N_TEXT:
  4475.     case N_TEXT | N_EXT:
  4476.       relocation = text_relocation;
  4477.       break;
  4478.  
  4479.     case N_DATA:
  4480.     case N_DATA | N_EXT:
  4481.       /* A word that points to beginning of the the data section
  4482.          initially contains not 0 but rather the "address" of that section
  4483.          in the input file, which is the length of the file's text.  */
  4484.       relocation = data_relocation;
  4485.       break;
  4486.  
  4487.     case N_BSS:
  4488.     case N_BSS | N_EXT:
  4489.       /* Similarly, an input word pointing to the beginning of the bss
  4490.          initially contains the length of text plus data of the file.  */
  4491.       relocation = bss_relocation;
  4492.       break;
  4493.  
  4494.     case N_ABS:
  4495.     case N_ABS | N_EXT:
  4496.       /* Don't know why this code would occur, but apparently it does.  */
  4497.       break;
  4498.  
  4499.     default:
  4500.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  4501.     }
  4502.  
  4503.       if (RELOC_PCREL_P(p))
  4504.     relocation -= pc_relocation;
  4505.  
  4506.       oldreloc = relocation;
  4507. #ifdef RELOC_ADD_EXTRA
  4508.       relocation += RELOC_ADD_EXTRA(p);
  4509. #endif
  4510.  
  4511.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  4512.  
  4513.       /* Unshifted mask for relocation */
  4514.       mask = 1 << (RELOC_TARGET_BITSIZE(p) - 1);
  4515.       mask |= mask - 1;
  4516.       relocation &= mask;
  4517.  
  4518.       /* Shift everything up to where it's going to be used */
  4519.       relocation <<= RELOC_TARGET_BITPOS(p);
  4520.       mask <<= RELOC_TARGET_BITPOS(p);
  4521.  
  4522.       switch (RELOC_TARGET_SIZE(p))
  4523.     {
  4524.     case 0:
  4525.       if (RELOC_MEMORY_ADD_P(p))
  4526.         relocation += mask & *(char *) (data + addr);
  4527.       *(char *) (data + addr) &= ~mask;
  4528.       *(char *) (data + addr) |= relocation;
  4529.       break;
  4530.  
  4531.     case 1:
  4532.     {
  4533.       register int word;
  4534.       if (RELOC_MEMORY_ADD_P(p)) {
  4535.         if (RELOC_PCREL_P(p)||RELOC_EXTERN_P(p))
  4536.           word = *(short *) (data + addr);
  4537.         else
  4538.           word = *(unsigned short *) (data + addr);
  4539. #ifdef BYTE_SWAP
  4540.         word = SWAP2(word);
  4541. #endif
  4542.         relocation += mask & word;
  4543.       }
  4544.       if (relocation > 32767 || relocation < -32768) {
  4545.         if (!RELOC_EXTERN_P(p))
  4546.           if (!entry->strings) {
  4547.         entry->strings = (char *) alloca (entry->string_size);
  4548.         read_entry_strings (file_open (entry), entry);
  4549.           }
  4550.         error ("fatal error: relocation out of range for %s in %s",
  4551.         retsymname(p, entry, oldreloc+word), entry->filename);
  4552.         make_executable = 0;
  4553.       }
  4554.       word &= ~mask;
  4555.       word |= relocation;
  4556. #ifdef BYTE_SWAP
  4557.       word = SWAP2(word);
  4558. #endif
  4559.       *(short *) (data + addr) = word;
  4560.     }
  4561.       break;
  4562.  
  4563.     case 2:
  4564.     {
  4565.       register unsigned long word;
  4566. #ifndef WORD_ALIGNED
  4567.       word = *(long *) (data + addr);
  4568. #ifdef BYTE_SWAP
  4569.       word = SWAP4(word);
  4570. #endif /* BYTE_SWAP */
  4571. #else
  4572.       word = ((unsigned long)(unsigned char)(data[addr])) << 24;
  4573.       word |= ((unsigned long)(unsigned char)(data[addr+1])) << 16;
  4574.       word |= ((unsigned long)(unsigned char)(data[addr+2])) << 8;
  4575.       word |= ((unsigned long)(unsigned char)(data[addr+3]));
  4576. #ifdef BYTE_SWAP
  4577.     /* NOTE: We dont need to swap 4 because we are constructing
  4578.        the word here */
  4579. #endif
  4580.  
  4581. #endif
  4582.  
  4583.       if (RELOC_MEMORY_ADD_P(p))
  4584.         relocation += mask & word;
  4585.  
  4586.       if (pure_text && relocation > text_size+text_start) {
  4587.         if (!RELOC_EXTERN_P(p))
  4588.           if (!entry->strings) {
  4589.         entry->strings = (char *) alloca (entry->string_size);
  4590.         read_entry_strings (file_open (entry), entry);
  4591.           }
  4592.         error ("absolute reference to %s from pure-text in %s",
  4593.         retsymname(p, entry, relocation), entry->filename);
  4594.         make_executable = 0;    /* set exit code */
  4595.       }
  4596.       word &= ~mask;
  4597.       word |= relocation;
  4598. #if (defined(BYTE_SWAP) && (!defined(WORD_ALIGNED)))
  4599.       word = SWAP4(word);
  4600. #endif
  4601. #ifndef WORD_ALIGNED
  4602.       *(long *) (data + addr) = word;
  4603. #else
  4604.       data[addr+3] = word;
  4605.       word >>= 8;
  4606.       data[addr+2] = word;
  4607.       word >>= 8;
  4608.       data[addr+1] = word;
  4609.       word >>= 8;
  4610.       data[addr] = word;
  4611. #endif
  4612.     }
  4613.       break;
  4614.  
  4615.     default:
  4616.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  4617.     }
  4618. #if 0 /*BYTE_SWAP*/        /* swap relocation info */
  4619. { /* BITFIELD ALERT BITFIELD ALERT */    /* switch it back */
  4620. char *p1,*p2,p3;
  4621. p->r_address = SWAP4(p->r_address);
  4622. /* terrible hack to fix bit-fields !!! */
  4623. p1 = (char *)&p->r_address + 4;
  4624. p2 = p1+2;
  4625. p3= *p1; *p1= *p2; *p2=p3;      /* adjust r_symbolnum */
  4626. p1+= 3;                         /* adjust everything else */
  4627. *p1 = ((*p1&0xf0)>>4)+((*p1&0x0f)<<4);  /* swap padding into correct spot */
  4628. *p1 = (*p1&0x6)+((*p1&0x8)>>3)+((*p1&0x1)<<3);  /* adjust other fields */
  4629. }
  4630. #endif /* BYTE_SWAP */
  4631.     }
  4632.     entry->strings = 0;
  4633. }
  4634.  
  4635. /* For relocatable_output only: write out the relocation,
  4636.    relocating the addresses-to-be-relocated.  */
  4637.  
  4638.  
  4639. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4640. void emit_atari_text_relocs (), emit_atari_data_relocs ();
  4641. void emit_atari_set_vectors();
  4642.  
  4643. void flush_atari_rbuf();
  4644. void emit_atari_reloc_byte();
  4645. #else
  4646. void coptxtrel (), copdatrel ();
  4647. #endif
  4648.  
  4649. void
  4650. write_rel ()
  4651. {
  4652.   register int i;
  4653.   register int count = 0;
  4654.  
  4655.   if (trace_files)
  4656.     fprintf (stderr, "Writing text relocation:\n\n");
  4657.  
  4658.   /* Assign each global symbol a sequence number, giving the order
  4659.      in which `write_syms' will write it.
  4660.      This is so we can store the proper symbolnum fields
  4661.      in relocation entries we write.  */
  4662.  
  4663.   for (i = 0; i < TABSIZE; i++)
  4664.     {
  4665.       symbol *sp;
  4666.       for (sp = symtab[i]; sp; sp = sp->link)
  4667.     if (sp->referenced || sp->defined)
  4668.       sp->def_count = count++;
  4669.     }
  4670.   if (count != defined_global_sym_count + undefined_global_sym_count)
  4671.     fatal ("internal error", 0);
  4672.  
  4673.   /* Write out the relocations of all files, remembered from copy_text.  */
  4674.  
  4675. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4676.   each_full_file (coptxtrel, 0);
  4677. #else
  4678.   each_full_file (emit_atari_text_relocs, 0);
  4679.   emit_atari_set_vectors();
  4680. #endif
  4681.  
  4682.   if (trace_files)
  4683.     fprintf (stderr, "\nWriting data relocation:\n\n");
  4684.  
  4685. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4686.   each_full_file (copdatrel, 0);
  4687. #else
  4688.   each_full_file (emit_atari_data_relocs, 0);
  4689.   if (rbuf_last_pc == -1)
  4690.   {
  4691.       emit_atari_reloc_byte(0);
  4692.       emit_atari_reloc_byte(0);
  4693.       emit_atari_reloc_byte(0);
  4694.   }
  4695.   emit_atari_reloc_byte(0);        /* end of relocs */
  4696.   flush_atari_rbuf();
  4697. #endif
  4698.  
  4699.   if (trace_files)
  4700.     fprintf (stderr, "\n");
  4701. }
  4702.  
  4703. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4704. /* code here for dumping atari style reloc info */
  4705.  
  4706. void flush_atari_rbuf()
  4707. {
  4708. /* fprintf(stderr, "flush\n"); */
  4709.   if(rbuf_size > 0)
  4710.     fwrite(rbuf, 1, rbuf_size, outdesc);
  4711.   rbuf_size = 0;
  4712. }
  4713.  
  4714. void emit_atari_reloc_byte(b)
  4715. unsigned int b;
  4716. {
  4717.     unsigned char byte = b & 0xFF;
  4718.  
  4719. /*fprintf(stderr, "  %X\n", byte);*/
  4720.   if (rbuf_size >= RBUF_SIZE)
  4721.     flush_atari_rbuf();
  4722.   rbuf[rbuf_size++] = byte;
  4723. }
  4724.  
  4725. int emit_atari_reloc(rel_pc)
  4726. long rel_pc;
  4727. {
  4728.   long diff;
  4729.  
  4730. /* fprintf(stderr, "reloc at %X\n", rel_pc);  */
  4731.   if (rbuf_last_pc == -1)        /* first time thru? */
  4732.     {
  4733.     emit_atari_reloc_byte(rel_pc >> 24);
  4734.     emit_atari_reloc_byte(rel_pc >> 16);
  4735.     emit_atari_reloc_byte(rel_pc >> 8);
  4736.     emit_atari_reloc_byte(rel_pc);
  4737.     }
  4738.     else
  4739.     {
  4740.     if (rel_pc <= rbuf_last_pc)
  4741.         {
  4742.         fprintf(stderr, "Relocs out of order; last = %X curr = %X\n",
  4743.                 rbuf_last_pc, rel_pc);
  4744. /*        exit(-999);    */
  4745.         return(0);
  4746.         }
  4747.     diff = rel_pc - rbuf_last_pc;
  4748.     while (diff > 254)
  4749.         {
  4750.         emit_atari_reloc_byte(1);
  4751.         diff -= 254;
  4752.         }
  4753.     emit_atari_reloc_byte(diff);
  4754.     }
  4755.   rbuf_last_pc = rel_pc;
  4756.   return(1);
  4757. }
  4758.  
  4759. int reloc_less_p(reloc_1, reloc_2)
  4760. struct relocation_info * reloc_1, * reloc_2;
  4761. {
  4762. #if 0
  4763.   return(reloc_1->r_address < reloc_2->r_address);
  4764. #else
  4765.   return(reloc_2->r_address - reloc_1->r_address);
  4766. #endif
  4767. }
  4768.  
  4769. void emit_atari_text_relocs(entry)
  4770.      struct file_entry *entry;
  4771. {
  4772.   int reloc_num;
  4773.   int n_relocs = entry->header.a_trsize / sizeof (struct relocation_info);
  4774.   long text_base = entry->text_start_address;
  4775.  
  4776.   qsort(entry->textrel, n_relocs, sizeof (struct relocation_info), 
  4777.     reloc_less_p);
  4778.   for (reloc_num = n_relocs - 1 ; reloc_num >= 0 ; reloc_num-- )
  4779.     if ((entry->textrel[reloc_num].r_length == 2) &&
  4780.       !emit_atari_reloc(entry->textrel[reloc_num].r_address + text_base))
  4781.         fprintf(stderr, "  while processing text rel %d/%d of entry %s\n",
  4782.         reloc_num, n_relocs, entry->filename);
  4783. }
  4784.  
  4785. void emit_atari_data_relocs(entry)
  4786.      struct file_entry *entry;
  4787. {
  4788.   int reloc_num;
  4789.   int n_relocs = entry->header.a_drsize / sizeof (struct relocation_info);
  4790.   long data_base = entry->data_start_address;
  4791.  
  4792.   qsort(entry->datarel, n_relocs, sizeof (struct relocation_info), 
  4793.     reloc_less_p);
  4794.   for (reloc_num = n_relocs - 1 ; reloc_num >= 0 ; reloc_num-- )
  4795.     if (!emit_atari_reloc(entry->datarel[reloc_num].r_address + data_base))
  4796.         fprintf(stderr, "  while processing data rel %d/%d of entry %s\n",
  4797.         reloc_num, n_relocs, entry->filename);
  4798. }
  4799.  
  4800.  
  4801. /* Handle set vectors. This kludge should only be necessary for C++.
  4802.  * Skip through the set vectors, outputting relocation for each vector.
  4803.  */
  4804. void emit_atari_set_vectors()
  4805. {
  4806.   unsigned long *current_element = set_vectors;
  4807.   int num_vectors = 0, loop;
  4808.   long num_elements = 0;
  4809.   
  4810.   if (set_sect_size != 0) {
  4811.  
  4812.     current_element = set_vectors;
  4813.     while (num_elements < (set_sect_size / sizeof(unsigned long))) {
  4814.       num_vectors = *current_element++; /* Number of vectors for current sym */
  4815.       num_elements++;
  4816.       for (loop = 1; loop <= num_vectors; loop++) {
  4817.         emit_atari_reloc(set_sect_start + num_elements++ * sizeof (unsigned long));
  4818.         current_element++;
  4819.       }
  4820.       current_element++; num_elements++;  /* There's a zero at the end. */
  4821.     }
  4822.   }
  4823. }
  4824.  
  4825. #endif
  4826.  
  4827. #if (!((defined(CROSSATARI) || defined(atarist) || defined(atariminix))))
  4828. void
  4829. coptxtrel (entry)
  4830.      struct file_entry *entry;
  4831. {
  4832.   register struct relocation_info *p, *end;
  4833.   register int reloc = entry->text_start_address;
  4834.  
  4835.   p = entry->textrel;
  4836.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  4837.   while (p < end)
  4838.     {
  4839.       RELOC_ADDRESS(p) += reloc;
  4840.       if (RELOC_EXTERN_P(p))
  4841.     {
  4842.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4843.       symbol *symptr = ((symbol *)
  4844.                 (((struct nlist *)
  4845.                   (((char *)entry->symbols) + symindex))
  4846.                  ->n_un.n_name));
  4847.  
  4848.       if (symindex >= entry->header.a_syms)
  4849.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4850.  
  4851.       /* If the symbol is now defined, change the external relocation
  4852.          to an internal one.  */
  4853.  
  4854.       if (symptr->defined)
  4855.         {
  4856.           RELOC_EXTERN_P(p) = 0;
  4857.           RELOC_SYMBOL(p) = (symptr->defined & ~N_EXT);
  4858.         }
  4859.       else
  4860.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4861.                  - defined_global_sym_count
  4862.                  - undefined_global_sym_count);
  4863.     }
  4864.       p++;
  4865.     }
  4866.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  4867. }
  4868.  
  4869. void
  4870. copdatrel (entry)
  4871.      struct file_entry *entry;
  4872. {
  4873.   register struct relocation_info *p, *end;
  4874.   /* Relocate the address of the relocation.
  4875.      Old address is relative to start of the input file's data section.
  4876.      New address is relative to start of the output file's data section.  */
  4877.   register int reloc = entry->data_start_address - text_size;
  4878.  
  4879.   p = entry->datarel;
  4880.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  4881.   while (p < end)
  4882.     {
  4883.       RELOC_ADDRESS(p) += reloc;
  4884.       if (RELOC_EXTERN_P(p))
  4885.     {
  4886.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4887.       symbol *symptr = ((symbol *)
  4888.                 (((struct nlist *)
  4889.                   (((char *)entry->symbols) + symindex))
  4890.                  ->n_un.n_name));
  4891.       int symtype = symptr->defined & ~N_EXT;
  4892.  
  4893.       if (symindex >= entry->header.a_syms)
  4894.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4895.       if (force_common_definition
  4896.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4897.         {
  4898.           RELOC_EXTERN_P(p) = 0;
  4899.           RELOC_SYMBOL(p) = symtype;
  4900.         }
  4901.       else
  4902.         RELOC_SYMBOL(p)
  4903.           = (((symbol *)
  4904.           (((struct nlist *)
  4905.             (((char *)entry->symbols) + symindex))
  4906.            ->n_un.n_name))
  4907.          ->def_count
  4908.          + nsyms - defined_global_sym_count
  4909.          - undefined_global_sym_count);
  4910.     }
  4911.       p++;
  4912.     }
  4913.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4914. }
  4915. #endif /* ! atari */
  4916.  
  4917. void write_file_syms ();
  4918. void write_string_table ();
  4919.  
  4920.  
  4921. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4922. /* this is all different on ataris */
  4923.  
  4924. #if (!(defined(MINIX) || defined(atariminix)))
  4925.  
  4926. /* atari TOS version */
  4927.  
  4928. #define N_ATARI_FILENAME 0x1E
  4929.  
  4930. void write_atari_sym(p, str)
  4931. register struct nlist * p;
  4932. char * str;
  4933. {
  4934.   struct asym sym;
  4935.   int i;
  4936.   char *ptr;
  4937.  
  4938.   /* fprintf(stderr, "sym %2ld %s\n", strlen(str), str); */
  4939.  
  4940.   switch (p->n_type & N_TYPE)
  4941.     {
  4942.     case N_UNDF: sym.a_type = A_UNDF; break;
  4943.     case N_ABS:  sym.a_type = A_EQU; break; /* ??? */
  4944.         case N_SETT:
  4945.     case N_TEXT: sym.a_type = A_TEXT; break;
  4946.         case N_SETV:
  4947.         case N_SETD:
  4948.     case N_DATA: sym.a_type = A_DATA; break;
  4949.         case N_SETB:
  4950.     case N_BSS:  sym.a_type = A_BSS; break;
  4951.         case N_ATARI_FILENAME: sym.a_type = A_TFILE; break;
  4952.     default: /* fake it */
  4953.           sym.a_type = A_UNDF;
  4954.       bzero(sym.a_name, sizeof(sym.a_name));
  4955.       strncpy(sym.a_name, str, sizeof(sym.a_name));
  4956.       i = 0;
  4957.       goto fake_out;
  4958.  
  4959. /* figure out rest of this later */
  4960.     }
  4961.   if (p->n_type & N_EXT) /* differenciate between local/global syms */
  4962.     sym.a_type |= A_GLOBL;
  4963.   ptr = &(sym.a_name[0]);
  4964.   i = sizeof(sym.a_name);
  4965.   while (--i >= 0 && ('\0' != (*ptr++ = *str)))
  4966.     str++;
  4967.  
  4968.   /* if i >= 0 then *str == '\0' and if i == 0 there is nothing to fill */
  4969.   if (i > 0)                /* we are done - fill it with 0 */
  4970.     {
  4971.       do {
  4972.     *ptr++ = '\0';
  4973.       } while (--i > 0);
  4974.     }
  4975.   else if (*str && gst_symboltable)    /* if more to write */
  4976.     {
  4977.       sym.a_type |= A_LNAM;
  4978.       i = sizeof sym;
  4979.       gst_symbols++;
  4980.     }
  4981. fake_out:
  4982.   sym.a_value = p->n_value;
  4983.  
  4984. #ifndef WORD_ALIGNED
  4985.   mywrite((char *)&sym, sizeof sym, 1, outdesc);
  4986.   if (i > 0)
  4987.     {
  4988.       (void) strncpy ((char *) &sym, str, (size_t)i);
  4989.       mywrite((char *)&sym, sizeof sym, 1, outdesc);
  4990.     }
  4991. #else
  4992.   mywrite(sym.a_name , sizeof sym.a_name , 1, outdesc);
  4993.   mywrite(&sym.a_type , sizeof sym.a_type , 1, outdesc);
  4994.   mywrite(&sym.a_value, sizeof sym.a_value, 1, outdesc);
  4995.   if (i > 0)
  4996.     {
  4997.       (void) strncpy ((char *) &sym, str, (size_t)i);
  4998.       mywrite(&sym, 14, 1, outdesc);
  4999.     }
  5000. #endif
  5001. }
  5002.  
  5003. void write_atari_syms(entry, syms_written_addr)
  5004. struct file_entry * entry;
  5005. int * syms_written_addr;
  5006. {
  5007.   register struct nlist *p = entry->symbols;
  5008.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  5009. /*   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;  */
  5010.  
  5011.   /* Read the file's string table.  */
  5012.  
  5013.   entry->strings = (char *) alloca (entry->string_size);
  5014.   read_entry_strings (file_open (entry), entry);
  5015.  
  5016.   /* Generate a local symbol for the start of this file's text.  */
  5017.  
  5018.   if (discard_locals != DISCARD_ALL)
  5019.     {
  5020.       struct nlist nl;
  5021.  
  5022.       nl.n_type = N_ATARI_FILENAME;
  5023. /*      nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); */
  5024.       nl.n_value = entry->text_start_address;
  5025.       nl.n_desc = 0;
  5026.       nl.n_other = 0;
  5027. /*      *bufp++ = nl;    */
  5028.       write_atari_sym(&nl, entry->local_sym_name);
  5029.       (*syms_written_addr)++;
  5030. /* necessary? */
  5031.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  5032.     }
  5033.  
  5034.   for (; p < end; p++)
  5035.     {
  5036.       register int type = p->n_type;
  5037.       register int write = 0;
  5038.       int is_a_set_ele;
  5039.  
  5040.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  5041.  
  5042.       if ((is_a_set_ele = SET_ELEMENT_P (type))) /* This occurs even if global.  These */
  5043.                 /* types of symbols are never written */
  5044.                 /* globally, though they are stored */
  5045.                 /* globally.  */
  5046.         write = 1;
  5047.       else if (!(type & (N_STAB | N_EXT)))
  5048.         /* ordinary local symbol */
  5049.     write = (discard_locals != DISCARD_ALL)
  5050.         && !(discard_locals == DISCARD_L &&
  5051.              (p->n_un.n_strx + entry->strings)[0] == 'L');
  5052.       else if (!(type & N_EXT))
  5053.     /* debugger symbol */
  5054.         write = (strip_symbols == STRIP_NONE);
  5055.  
  5056.       if (write)
  5057.     {
  5058.       /* If this symbol has a name, write it */
  5059.  
  5060.       if (p->n_un.n_strx)
  5061.         {
  5062.         write_atari_sym(p, ((is_a_set_ele)?
  5063.         ((symbol *) p->n_un.n_name)->name : p->n_un.n_strx + entry->strings));
  5064. #if 0
  5065.              "SET_ELEMENT" : p->n_un.n_strx + entry->strings));
  5066. #endif
  5067.         (*syms_written_addr)++;
  5068.         }
  5069.     }
  5070.     }
  5071.   entry->strings = NULL;  
  5072. }
  5073.  
  5074. void write_syms ()
  5075. {
  5076.   int n_syms_written = 0;
  5077.   register symbol *sp;
  5078.   int i;
  5079.  
  5080.   if (strip_symbols == STRIP_ALL)
  5081.     return;
  5082.  
  5083.   each_file(write_atari_syms, &n_syms_written);
  5084.  
  5085.   /* Now write out the global symbols.  */
  5086.  
  5087.   /* Scan the symbol hash table, bucket by bucket.  */
  5088.  
  5089.   for (i = 0; i < TABSIZE; i++)
  5090.     for (sp = symtab[i]; sp; sp = sp->link)
  5091.       {
  5092.     struct nlist nl;
  5093.  
  5094.     nl.n_other = 0;
  5095.     nl.n_desc = 0;
  5096.  
  5097.     /* Compute a `struct nlist' for the symbol.  */
  5098.  
  5099.     if (sp->defined || sp->referenced)
  5100.       {
  5101.         if (!sp->defined)          /* undefined -- legit only if -r */
  5102.           {
  5103.         nl.n_type = N_UNDF | N_EXT;
  5104.         nl.n_value = 0;
  5105.           }
  5106.         else if (sp->defined > 1) /* defined with known type */
  5107.           {
  5108.         /* If the target of an indirect symbol has been
  5109.            defined and we are outputting an executable,
  5110.            resolve the indirection; it's no longer needed */
  5111.         if (!relocatable_output
  5112.             && ((sp->defined & ~N_EXT) == N_INDR)
  5113.             && (((symbol *) sp->value)->defined > 1))
  5114.           {
  5115.             symbol *newsp = (symbol *) sp->value;
  5116.             nl.n_type = newsp->defined;
  5117.             nl.n_value = newsp->value;
  5118.           }
  5119.         else
  5120.           {
  5121.             nl.n_type = sp->defined;
  5122.             if (sp->defined != (N_INDR | N_EXT))
  5123.               nl.n_value = sp->value;
  5124.             else
  5125.               nl.n_value = 0;
  5126.           }
  5127.           }
  5128.         else if (sp->max_common_size) /* defined as common but not allocated. */
  5129.           {
  5130.         /* happens only with -r and not -d */
  5131.         /* write out a common definition */
  5132.         nl.n_type = N_UNDF | N_EXT;
  5133.         nl.n_value = sp->max_common_size;
  5134.           }
  5135.         else
  5136.           fatal ("internal error: %s defined in mysterious way", sp->name);
  5137.  
  5138.         /* write and count it.  */
  5139. #ifdef BYTE_SWAP
  5140. nl.n_un.n_strx    = SWAP4(nl.n_un.n_strx);
  5141. nl.n_desc      = SWAP2(nl.n_desc);
  5142. nl.n_value      = SWAP4(nl.n_value);
  5143. #endif
  5144.         write_atari_sym(&nl, sp->name);
  5145.         n_syms_written++;
  5146.       }
  5147.       }
  5148.   /* this is really stretching it, but we have to fake it */
  5149.   if(n_syms_written < nsyms)
  5150.   {
  5151.       struct nlist nl;
  5152.  
  5153.       nl.n_value = 0;
  5154.       nl.n_type = (0x7ffff & N_TYPE); /* make sure it falls into the default */
  5155.                       /* case in write_atari_sym() */
  5156.       
  5157.       while(n_syms_written < nsyms)
  5158.       {
  5159. #ifdef BYTE_SWAP
  5160. nl.n_un.n_strx    = SWAP4(nl.n_un.n_strx);
  5161. nl.n_desc        = SWAP2(nl.n_desc);
  5162. nl.n_value      = SWAP4(nl.n_value);
  5163. #endif
  5164.       write_atari_sym(&nl, "FAKEEAKF");
  5165.       n_syms_written++;
  5166.       }
  5167.       
  5168.   }
  5169.   if (n_syms_written != nsyms)
  5170.     fprintf(stderr, "Bogon alert!  wrote %d syms, expected to write %d\n",
  5171.              n_syms_written, nsyms);
  5172.   
  5173.       
  5174. }
  5175.  
  5176. #else /* Atari Minix */
  5177.  
  5178. void write_atari_sym(p, str, gflag)
  5179. register struct nlist * p;
  5180. char * str;
  5181. int gflag;
  5182. {
  5183.   struct asym sym;
  5184.   int i;
  5185.  
  5186. /* fprintf(stderr, "sym %s\n", str); */
  5187.  
  5188.   for (i = 0 ; ((i < 8) && str[i]) ; i++)
  5189.     sym.a_name[i] = str[i];
  5190.   for ( ; i < 8 ; i++)
  5191.     sym.a_name[i] = ' ';
  5192.   switch (p->n_type & N_TYPE)
  5193.     {
  5194.     case N_UNDF: sym.a_sclass = A_UNDF; break;
  5195.     case N_ABS:  sym.a_sclass = A_ABS; break;
  5196.     case N_TEXT: sym.a_sclass = A_TEXT; break;
  5197.     case N_DATA: sym.a_sclass = A_DATA; break;
  5198.     case N_BSS:  sym.a_sclass = A_BSS; break;
  5199.     default:     sym.a_sclass = 0;
  5200.     }
  5201.   if(gflag)
  5202.       sym.a_sclass |= A_EXT;
  5203.  
  5204.   sym.a_value = p->n_value;
  5205.   sym.a_numaux = sym.a_type = 0;
  5206. #ifndef WORD_ALIGNED
  5207.   mywrite(&sym, sizeof sym, 1, outdesc);
  5208. #else
  5209.   mywrite(&sym.a_name , sizeof sym.a_name , 1, outdesc);
  5210.   mywrite(&sym.a_value, sizeof sym.a_value, 1, outdesc);
  5211.   mywrite(&sym.a_sclass, sizeof sym.a_sclass, 1, outdesc);
  5212.   mywrite(&sym.a_numaux , sizeof sym.a_numaux , 1, outdesc);
  5213.   mywrite(&sym.a_type , sizeof sym.a_type , 1, outdesc);
  5214. #endif
  5215.  
  5216. }
  5217.  
  5218. void write_atari_syms(entry, syms_written_addr)
  5219. struct file_entry * entry;
  5220. int * syms_written_addr;
  5221. {
  5222.   register struct nlist *p = entry->symbols;
  5223.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  5224.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  5225.  
  5226.   /* Read the file's string table.  */
  5227.  
  5228.   entry->strings = (char *) alloca (entry->string_size);
  5229.   read_entry_strings (file_open (entry), entry);
  5230.  
  5231.   /* Generate a local symbol for the start of this file's text.  */
  5232.  
  5233.   if (discard_locals != DISCARD_ALL)
  5234.     {
  5235.       struct nlist nl;
  5236.  
  5237.       nl.n_type = N_TEXT;
  5238. /*      nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); */
  5239.       nl.n_value = entry->text_start_address;
  5240.       nl.n_desc = 0;
  5241.       nl.n_other = 0;
  5242. /*      *bufp++ = nl;    */
  5243.       write_atari_sym(&nl, entry->local_sym_name, 0);
  5244.       (*syms_written_addr)++;
  5245. /* necessary? */
  5246.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  5247.     }
  5248.  
  5249.   for (; p < end; p++)
  5250.     {
  5251.       register int type = p->n_type;
  5252.       register int write = 0;
  5253.  
  5254.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  5255.  
  5256.       if (!(type & (N_STAB | N_EXT)))
  5257.         /* ordinary local symbol */
  5258.     write = (discard_locals != DISCARD_ALL)
  5259.         && !(discard_locals == DISCARD_L &&
  5260.              (p->n_un.n_strx + entry->strings)[0] == 'L');
  5261.       else if (!(type & N_EXT))
  5262.     /* debugger symbol */
  5263.       /*        write = (strip_symbols == STRIP_NONE); */ write = 0;
  5264.       
  5265.  
  5266.       if (write)
  5267.     {
  5268.       /* If this symbol has a name, write it */
  5269.  
  5270.       if (p->n_un.n_strx)
  5271.         {
  5272.         write_atari_sym(p, p->n_un.n_strx + entry->strings, 0);
  5273.         (*syms_written_addr)++;
  5274.         }
  5275.     }
  5276.     }
  5277.   entry->strings = NULL;  
  5278. }
  5279.  
  5280. void write_syms ()
  5281. {
  5282.   int n_syms_written = 0;
  5283.   register symbol *sp;
  5284.   int i;
  5285.  
  5286.   if (strip_symbols == STRIP_ALL)
  5287.     return;
  5288.  
  5289.   each_file(write_atari_syms, &n_syms_written);
  5290.  
  5291.   /* Now write out the global symbols.  */
  5292.  
  5293.   /* Scan the symbol hash table, bucket by bucket.  */
  5294.  
  5295.   for (i = 0; i < TABSIZE; i++)
  5296.     for (sp = symtab[i]; sp; sp = sp->link)
  5297.       {
  5298.     struct nlist nl;
  5299.  
  5300.     nl.n_other = 0;
  5301.     nl.n_desc = 0;
  5302.  
  5303.     /* Compute a `struct nlist' for the symbol.  */
  5304.  
  5305.     if (sp->defined || sp->referenced)
  5306.       {
  5307.         if (!sp->defined)          /* undefined -- legit only if -r */
  5308.           {
  5309.         nl.n_type = N_UNDF | N_EXT;
  5310.         nl.n_value = 0;
  5311.           }
  5312.         else if (sp->defined > 1) /* defined with known type */
  5313.           {
  5314.         nl.n_type = sp->defined;
  5315.         nl.n_value = sp->value;
  5316.           }
  5317.         else if (sp->max_common_size) /* defined as common but not allocated. */
  5318.           {
  5319.         /* happens only with -r and not -d */
  5320.         /* write out a common definition */
  5321.         nl.n_type = N_UNDF | N_EXT;
  5322.         nl.n_value = sp->max_common_size;
  5323.           }
  5324.         else
  5325.           fatal ("internal error: %s defined in mysterious way", sp->name);
  5326.  
  5327.         /* write and count it.  */
  5328.  
  5329.         write_atari_sym(&nl, sp->name, 1);
  5330.         n_syms_written++;
  5331.       }
  5332.       }
  5333.   if (n_syms_written != nsyms)
  5334.   {
  5335.       register long pos;
  5336.       
  5337. #if 0
  5338.       fprintf(stderr, "Bogon alert!  wrote %d syms, expected to write %d\n",
  5339.           n_syms_written, nsyms);
  5340. #endif
  5341.     /* go patch header */
  5342. #ifndef WORD_ALIGNED
  5343.       n_syms_written = n_syms_written * sizeof(struct asym);
  5344. #else
  5345.       n_syms_written = n_syms_written * 16;
  5346. #endif
  5347.       pos = ftell(outdesc);
  5348.       fseek(outdesc, 28L, 0);
  5349.       mywrite(&n_syms_written, 4, 1, outdesc);
  5350.       fseek(outdesc, pos, 0);
  5351.     }
  5352.   
  5353. }
  5354. #endif /* MINIX */
  5355.  
  5356. #else  /* for Gnu/Unix */
  5357.  
  5358. /* Offsets and current lengths of symbol and string tables in output file. */
  5359.  
  5360. int symbol_table_offset;
  5361. int symbol_table_len;
  5362.  
  5363. /* Address in output file where string table starts.  */
  5364. int string_table_offset;
  5365.  
  5366. /* Offset within string table
  5367.    where the strings in `strtab_vector' should be written.  */
  5368. int string_table_len;
  5369.  
  5370. /* Total size of string table strings allocated so far,
  5371.    including strings in `strtab_vector'.  */
  5372. int strtab_size;
  5373.  
  5374. /* Vector whose elements are strings to be added to the string table.  */
  5375. char **strtab_vector;
  5376.  
  5377. /* Vector whose elements are the lengths of those strings.  */
  5378. int *strtab_lens;
  5379.  
  5380. /* Index in `strtab_vector' at which the next string will be stored.  */
  5381. int strtab_index;
  5382.  
  5383. /* Add the string NAME to the output file string table.
  5384.    Record it in `strtab_vector' to be output later.
  5385.    Return the index within the string table that this string will have.  */
  5386.  
  5387. int
  5388. assign_string_table_index (name)
  5389.      char *name;
  5390. {
  5391.   register int index = strtab_size;
  5392.   register int len = strlen (name) + 1;
  5393.  
  5394.   strtab_size += len;
  5395.   strtab_vector[strtab_index] = name;
  5396.   strtab_lens[strtab_index++] = len;
  5397.  
  5398.   return index;
  5399. }
  5400.  
  5401. FILE *outstream = (FILE *) 0;
  5402.  
  5403. /* Write the contents of `strtab_vector' into the string table.
  5404.    This is done once for each file's local&debugger symbols
  5405.    and once for the global symbols.  */
  5406.  
  5407. void
  5408. write_string_table ()
  5409. {
  5410.   register int i;
  5411.  
  5412.   fseek (outdesc, string_table_offset + string_table_len, 0);
  5413.  
  5414.   if (!outstream)
  5415.     outstream = outdesc;
  5416.  
  5417.   for (i = 0; i < strtab_index; i++)
  5418.     {
  5419.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  5420.       string_table_len += strtab_lens[i];
  5421.     }
  5422.  
  5423.   fflush (outstream);
  5424.  
  5425.   /* Report I/O error such as disk full.  */
  5426.   if (ferror (outstream))
  5427.     perror_name (output_filename);
  5428. }
  5429.  
  5430. /* Write the symbol table and string table of the output file.  */
  5431.  
  5432. void
  5433. write_syms ()
  5434. {
  5435.   /* Number of symbols written so far.  */
  5436.   int syms_written = 0;
  5437.   register int i;
  5438.   register symbol *sp;
  5439.  
  5440.   /* Buffer big enough for all the global symbols.  One
  5441.      extra struct for each indirect symbol to hold the extra reference
  5442.      following. */
  5443.   struct nlist *buf
  5444.     = (struct nlist *) alloca ((defined_global_sym_count
  5445.                 + undefined_global_sym_count
  5446.                 + global_indirect_count)
  5447.                    * sizeof (struct nlist));
  5448.   /* Pointer for storing into BUF.  */
  5449.   register struct nlist *bufp = buf;
  5450.  
  5451.   /* Size of string table includes the bytes that store the size.  */
  5452.   strtab_size = sizeof strtab_size;
  5453.  
  5454.   symbol_table_offset = N_SYMOFF (outheader);
  5455.   symbol_table_len = 0;
  5456.   string_table_offset = N_STROFF (outheader);
  5457.   string_table_len = strtab_size;
  5458.  
  5459.   if (strip_symbols == STRIP_ALL)
  5460.     return;
  5461.  
  5462.   /* Write the local symbols defined by the various files.  */
  5463.  
  5464.   each_file (write_file_syms, &syms_written);
  5465.   file_close ();
  5466.  
  5467.   /* Now write out the global symbols.  */
  5468.  
  5469.   /* Allocate two vectors that record the data to generate the string
  5470.      table from the global symbols written so far.  This must include
  5471.      extra space for the references following indirect outputs. */
  5472.  
  5473.   strtab_vector = (char **) alloca ((num_hash_tab_syms
  5474.                      + global_indirect_count) * sizeof (char *));
  5475.   strtab_lens = (int *) alloca ((num_hash_tab_syms
  5476.                  + global_indirect_count) * sizeof (int));
  5477.   strtab_index = 0;
  5478.  
  5479.   /* Scan the symbol hash table, bucket by bucket.  */
  5480.  
  5481.   for (i = 0; i < TABSIZE; i++)
  5482.     for (sp = symtab[i]; sp; sp = sp->link)
  5483.       {
  5484.     struct nlist nl;
  5485.  
  5486.     nl.n_other = 0;
  5487.     nl.n_desc = 0;
  5488.  
  5489.     /* Compute a `struct nlist' for the symbol.  */
  5490.  
  5491.     if (sp->defined || sp->referenced)
  5492.       {
  5493.         /* common condition needs to be before undefined condition */
  5494.         /* because unallocated commons are set undefined in */
  5495.         /* digest_symbols */
  5496.         if (sp->defined > 1) /* defined with known type */
  5497.           {
  5498.         /* If the target of an indirect symbol has been
  5499.            defined and we are outputting an executable,
  5500.            resolve the indirection; it's no longer needed */
  5501.         if (!relocatable_output
  5502.             && ((sp->defined & ~N_EXT) == N_INDR)
  5503.             && (((symbol *) sp->value)->defined > 1))
  5504.           {
  5505.             symbol *newsp = (symbol *) sp->value;
  5506.             nl.n_type = newsp->defined;
  5507.             nl.n_value = newsp->value;
  5508.           }
  5509.         else
  5510.           {
  5511.             nl.n_type = sp->defined;
  5512.             if (sp->defined != (N_INDR | N_EXT))
  5513.               nl.n_value = sp->value;
  5514.             else
  5515.               nl.n_value = 0;
  5516.           }
  5517.           }
  5518.         else if (sp->max_common_size) /* defined as common but not allocated. */
  5519.           {
  5520.         /* happens only with -r and not -d */
  5521.         /* write out a common definition */
  5522.         nl.n_type = N_UNDF | N_EXT;
  5523.         nl.n_value = sp->max_common_size;
  5524.           }
  5525.         else if (!sp->defined)          /* undefined -- legit only if -r */
  5526.           {
  5527.         nl.n_type = N_UNDF | N_EXT;
  5528.         nl.n_value = 0;
  5529.           }
  5530.         else
  5531.           fatal ("internal error: %s defined in mysterious way", sp->name);
  5532.  
  5533.         /* Allocate string table space for the symbol name.  */
  5534.  
  5535.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  5536.  
  5537.         /* Output to the buffer and count it.  */
  5538.  
  5539.         *bufp++ = nl;
  5540.         syms_written++;
  5541.         if (nl.n_type == (N_INDR | N_EXT))
  5542.           {
  5543.         struct nlist xtra_ref;
  5544.         xtra_ref.n_type = N_EXT | N_UNDF;
  5545.         xtra_ref.n_un.n_strx =
  5546.           assign_string_table_index (((symbol *) sp->value)->name);
  5547.         xtra_ref.n_other = 0;
  5548.         xtra_ref.n_desc = 0;
  5549.         xtra_ref.n_value = 0;
  5550.         *bufp++ = xtra_ref;
  5551.         syms_written++;
  5552.           }
  5553.       }
  5554.       }
  5555.  
  5556.   /* Output the buffer full of `struct nlist's.  */
  5557.  
  5558.   fseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  5559.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  5560.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  5561.  
  5562.   if (syms_written != nsyms)
  5563.     fatal ("internal error: wrong number of symbols written into output file", 0);
  5564.  
  5565.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  5566.     fatal ("internal error: inconsistent symbol table length", 0);
  5567.  
  5568.   /* Now the total string table size is known, so write it.
  5569.      We are already positioned at the right place in the file.  */
  5570.  
  5571.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  5572.  
  5573.   /* Write the strings for the global symbols.  */
  5574.  
  5575.   write_string_table ();
  5576. }
  5577.  
  5578. /* Write the local and debugger symbols of file ENTRY.
  5579.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  5580.  
  5581. /* Note that we do not combine identical names of local symbols.
  5582.    dbx or gdb would be confused if we did that.  */
  5583.  
  5584. void
  5585. write_file_syms (entry, syms_written_addr)
  5586.      struct file_entry *entry;
  5587.      int *syms_written_addr;
  5588. {
  5589.   register struct nlist *p = entry->symbols;
  5590.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  5591.  
  5592.   /* Buffer to accumulate all the syms before writing them.
  5593.      It has one extra slot for the local symbol we generate here.  */
  5594.   struct nlist *buf
  5595.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  5596.   register struct nlist *bufp = buf;
  5597.  
  5598.   /* Upper bound on number of syms to be written here.  */
  5599.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  5600.  
  5601.   /* Make tables that record, for each symbol, its name and its name's length.
  5602.      The elements are filled in by `assign_string_table_index'.  */
  5603.  
  5604.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  5605.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  5606.   strtab_index = 0;
  5607.  
  5608.   /* Generate a local symbol for the start of this file's text.  */
  5609.  
  5610.   if (discard_locals != DISCARD_ALL)
  5611.     {
  5612.       struct nlist nl;
  5613.  
  5614.       nl.n_type = N_TEXT;
  5615.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  5616.       nl.n_value = entry->text_start_address;
  5617.       nl.n_desc = 0;
  5618.       nl.n_other = 0;
  5619.       *bufp++ = nl;
  5620.       (*syms_written_addr)++;
  5621.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  5622.     }
  5623.  
  5624.   /* Read the file's string table.  */
  5625.  
  5626.   entry->strings = (char *) alloca (entry->string_size);
  5627.   read_entry_strings (file_open (entry), entry);
  5628.  
  5629.   for (; p < end; p++)
  5630.     {
  5631.       register int type = p->n_type;
  5632.       register int write = 0;
  5633.  
  5634.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  5635.  
  5636.  
  5637.       if (SET_ELEMENT_P (type))     /* This occurs even if global.  These */
  5638.                 /* types of symbols are never written */
  5639.                 /* globally, though they are stored */
  5640.                 /* globally.  */
  5641.         write = relocatable_output;
  5642.       else if (!(type & (N_STAB | N_EXT)))
  5643.         /* ordinary local symbol */
  5644.     write = ((discard_locals != DISCARD_ALL)
  5645.          && !(discard_locals == DISCARD_L &&
  5646.               (p->n_un.n_strx + entry->strings)[0] == 'L')
  5647.          && type != N_WARNING);
  5648.       else if (!(type & N_EXT))
  5649.     /* debugger symbol */
  5650.         write = (strip_symbols == STRIP_NONE);
  5651.  
  5652.       if (write)
  5653.     {
  5654.       /* If this symbol has a name,
  5655.          allocate space for it in the output string table.  */
  5656.  
  5657. #if 0                /* Following no longer true  */
  5658.       /* Sigh.  If this is a set element, it has been entered in */
  5659.       /* the global symbol table *and* is being output as a */
  5660.       /* local.  This means that p->n_un.n_strx has been */
  5661.       /* trashed.  */
  5662.       if (SET_ELEMENT_P (type))
  5663.         p->n_un.n_strx =
  5664.           assign_string_table_index (((symbol *) p->n_un.n_name)->name);
  5665.       else
  5666. #endif
  5667.       if (p->n_un.n_strx)
  5668.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  5669.                             + entry->strings);
  5670.  
  5671.       /* Output this symbol to the buffer and count it.  */
  5672.  
  5673.       *bufp++ = *p;
  5674.       (*syms_written_addr)++;
  5675.     }
  5676.     }
  5677.  
  5678.   /* All the symbols are now in BUF; write them.  */
  5679.  
  5680.   fseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  5681.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  5682.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  5683.  
  5684.   /* Write the string-table data for the symbols just written,
  5685.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  5686.  
  5687.   write_string_table ();
  5688.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  5689. }
  5690. #endif            /* not atari st */
  5691.  
  5692.  
  5693. /* Copy any GDB symbol segments from the input files to the output file.
  5694.    The contents of the symbol segment is copied without change
  5695.    except that we store some information into the beginning of it.  */
  5696.  
  5697. void write_file_symseg ();
  5698.  
  5699. void
  5700. write_symsegs ()
  5701. {
  5702.   each_file (write_file_symseg, 0);
  5703. }
  5704.  
  5705. void
  5706. write_file_symseg (entry)
  5707.      struct file_entry *entry;
  5708. {
  5709.   /* this appears to be completely wrong for ST */
  5710. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  5711.   char buffer[4096];
  5712.   struct symbol_root root;
  5713.   FILE *indesc;
  5714.   int len;
  5715.  
  5716.   if (entry->symseg_offset == 0)
  5717.     return;
  5718.  
  5719.   /* This entry has a symbol segment.  Read the root of the segment.  */
  5720.  
  5721.   indesc = file_open (entry);
  5722.   fseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  5723.   if (sizeof root != fread (&root, 1, sizeof root, indesc))
  5724.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  5725.  
  5726.   /* Store some relocation info into the root.  */
  5727.  
  5728.   root.ldsymoff = entry->local_syms_offset;
  5729.   root.textrel = entry->text_start_address;
  5730.   root.datarel = entry->data_start_address - entry->header.a_text;
  5731.   root.bssrel = entry->bss_start_address
  5732.     - entry->header.a_text - entry->header.a_data;
  5733.   root.databeg = entry->data_start_address - root.datarel;
  5734.   root.bssbeg = entry->bss_start_address - root.bssrel;
  5735.  
  5736.   /* Write the modified root into the output file.  */
  5737.  
  5738.   mywrite (&root, sizeof root, 1, outdesc);
  5739.  
  5740.   /* Copy the rest of the symbol segment unchanged.  */
  5741.  
  5742.   if (entry->superfile)
  5743.     {
  5744.       /* Library member: number of bytes to copy is determined
  5745.      from the member's total size.  */
  5746.  
  5747.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  5748.  
  5749.       while (total > 0)
  5750.     {
  5751.       len = fread (buffer, 1, min (sizeof buffer, total), indesc);
  5752.  
  5753.       if (len != min (sizeof buffer, total))
  5754.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  5755.       total -= len;
  5756.       mywrite (buffer, len, 1, outdesc);
  5757.     }
  5758.     }
  5759.   else
  5760.     {
  5761.       /* A separate file: copy until end of file.  */
  5762.  
  5763.       while (len = fread (buffer, 1, sizeof buffer, indesc))
  5764.     {
  5765.       mywrite (buffer, len, 1, outdesc);
  5766.       if (len < sizeof buffer)
  5767.         break;
  5768.     }
  5769.     }
  5770.  
  5771.   file_close ();
  5772. #endif
  5773. }
  5774.  
  5775. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  5776.  
  5777. void
  5778. symtab_init ()
  5779. {
  5780. #ifndef nounderscore
  5781.   edata_symbol = getsym ("_edata");
  5782.   etext_symbol = getsym ("_etext");
  5783.   end_symbol = getsym ("_end");
  5784. #else
  5785.   edata_symbol = getsym ("edata");
  5786.   etext_symbol = getsym ("etext");
  5787.   end_symbol = getsym ("end");
  5788. #endif
  5789.  
  5790.   edata_symbol->defined = N_DATA | N_EXT;
  5791.   etext_symbol->defined = N_TEXT | N_EXT;
  5792.   end_symbol->defined = N_BSS | N_EXT;
  5793.  
  5794.   edata_symbol->referenced = 1;
  5795.   etext_symbol->referenced = 1;
  5796.   end_symbol->referenced = 1;
  5797. }
  5798.  
  5799. /* Compute the hash code for symbol name KEY.  */
  5800.  
  5801. int
  5802. hash_string (key)
  5803.      char *key;
  5804. {
  5805.   register char *cp;
  5806.   register int k;
  5807.  
  5808.   cp = key;
  5809.   k = 0;
  5810.   while (*cp)
  5811.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  5812.  
  5813.   return k;
  5814. }
  5815.  
  5816. /* Get the symbol table entry for the global symbol named KEY.
  5817.    Create one if there is none.  */
  5818.  
  5819. symbol *
  5820. getsym (key)
  5821.      char *key;
  5822. {
  5823.   register int hashval;
  5824.   register symbol *bp;
  5825.  
  5826.   /* Determine the proper bucket.  */
  5827.  
  5828.   hashval = hash_string (key) % TABSIZE;
  5829.  
  5830.   /* Search the bucket.  */
  5831.  
  5832.   for (bp = symtab[hashval]; bp; bp = bp->link)
  5833.     if (! strcmp (key, bp->name))
  5834.       return bp;
  5835.  
  5836.   /* Nothing was found; create a new symbol table entry.  */
  5837.  
  5838.   bp = (symbol *) xmalloc (sizeof (symbol));
  5839.   bp->refs = 0;
  5840.   bp->name = (char *) xmalloc (strlen (key) + 1);
  5841.   strcpy (bp->name, key);
  5842.   bp->defined = 0;
  5843.   bp->referenced = 0;
  5844.   bp->trace = 0;
  5845.   bp->value = 0;
  5846.   bp->max_common_size = 0;
  5847.   bp->warning = 0;
  5848.   bp->undef_refs = 0;
  5849.   bp->def_count = 0;
  5850.  
  5851.   /* Add the entry to the bucket.  */
  5852.  
  5853.   bp->link = symtab[hashval];
  5854.   symtab[hashval] = bp;
  5855.  
  5856.   ++num_hash_tab_syms;
  5857.  
  5858.   return bp;
  5859. }
  5860.  
  5861. /* Like `getsym' but return 0 if the symbol is not already known.  */
  5862.  
  5863. symbol *
  5864. getsym_soft (key)
  5865.      char *key;
  5866. {
  5867.   register int hashval;
  5868.   register symbol *bp;
  5869.  
  5870.   /* Determine which bucket.  */
  5871.  
  5872.   hashval = hash_string (key) % TABSIZE;
  5873.  
  5874.   /* Search the bucket.  */
  5875.  
  5876.   for (bp = symtab[hashval]; bp; bp = bp->link)
  5877.     if (! strcmp (key, bp->name))
  5878.       return bp;
  5879.  
  5880.   return 0;
  5881. }
  5882.  
  5883. /* Report a fatal error.
  5884.    STRING is a printf format string and ARG is one arg for it.  */
  5885.  
  5886. #if __STDC__
  5887. void
  5888. fatal (char *string, ...)
  5889. {
  5890.     va_list ap;
  5891.     
  5892.     va_start(ap, string);
  5893.     fprintf (stderr, "ld: ");
  5894.     vfprintf (stderr, string, ap);
  5895.     fprintf (stderr, "\n");
  5896.     va_end(ap);
  5897.     exit (1);
  5898. }
  5899. #else
  5900. void
  5901. fatal (va_alist)
  5902. va_dcl
  5903. {
  5904.     va_list ap;
  5905.     char *string;
  5906.  
  5907.     va_start(ap);
  5908.     string = va_arg(ap, char *);
  5909.     fprintf (stderr, "ld: ");
  5910.     vfprintf (stderr, string, ap);
  5911.     fprintf (stderr, "\n");
  5912.     va_end(ap);
  5913.     exit (1);
  5914. }
  5915. #endif
  5916.  
  5917.  
  5918. /* Report a fatal error.  The error message is STRING
  5919.    followed by the filename of ENTRY.  */
  5920.  
  5921. void
  5922. fatal_with_file (string, entry)
  5923.      char *string;
  5924.      struct file_entry *entry;
  5925. {
  5926.   fprintf (stderr, "ld: ");
  5927.   fprintf (stderr, string);
  5928.   print_file_name (entry, stderr);
  5929.   fprintf (stderr, "\n");
  5930.   exit (1);
  5931. }
  5932.  
  5933. /* Report a fatal error using the message for the last failed system call,
  5934.    followed by the string NAME.  */
  5935.  
  5936. void
  5937. perror_name (name)
  5938.      char *name;
  5939. {
  5940.   extern int errno, sys_nerr;
  5941.   extern char *sys_errlist[];
  5942.   char *s;
  5943.  
  5944.   if (errno < sys_nerr)
  5945.     s = concat ("", sys_errlist[errno], " for %s");
  5946.   else
  5947.     s = "cannot open %s";
  5948.   fatal (s, name);
  5949. }
  5950.  
  5951. /* Report a fatal error using the message for the last failed system call,
  5952.    followed by the name of file ENTRY.  */
  5953.  
  5954. void
  5955. perror_file (entry)
  5956.      struct file_entry *entry;
  5957. {
  5958.   extern int errno, sys_nerr;
  5959.   extern char *sys_errlist[];
  5960.   char *s;
  5961.  
  5962.   if (errno < sys_nerr)
  5963.     s = concat ("", sys_errlist[errno], " for ");
  5964.   else
  5965.     s = "cannot open ";
  5966.   fatal_with_file (s, entry);
  5967. }
  5968.  
  5969. /* Report a nonfatal error.
  5970.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  5971.  
  5972. void
  5973. error (string, arg1, arg2, arg3)
  5974.      char *string, *arg1, *arg2, *arg3;
  5975. {
  5976.   fprintf (stderr, "%s: ", progname);
  5977.   fprintf (stderr, string, arg1, arg2, arg3);
  5978.   fprintf (stderr, "\n");
  5979. }
  5980.  
  5981.  
  5982. /* Output COUNT*ELTSIZE bytes of data at BUF
  5983.    to the descriptor DESC.  */
  5984.  
  5985. void
  5986. mywrite (buf, count, eltsize, desc)
  5987.      void *buf;
  5988.      int count;
  5989.      int eltsize;
  5990.      FILE *desc;
  5991. {
  5992.   if((eltsize != 0) && (count != 0))
  5993.   {
  5994.       if(count != fwrite(buf, eltsize, count, desc))
  5995.         perror_name(output_filename);
  5996.   }
  5997. }
  5998.  
  5999. /* Output PADDING zero-bytes to descriptor OUTDESC.
  6000.    PADDING may be negative; in that case, do nothing.  */
  6001.  
  6002. void
  6003. padfile (padding, outdesc)
  6004.      int padding;
  6005.      FILE *outdesc;
  6006. {
  6007.     /* not round here, you don't... */
  6008. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  6009.   register char *buf;
  6010.   if (padding <= 0)
  6011.     return;
  6012.  
  6013.   buf = (char *) alloca (padding);
  6014.   bzero (buf, padding);
  6015.   mywrite (buf, padding, 1, outdesc);
  6016. #endif
  6017. }
  6018.  
  6019. /* Return a newly-allocated string
  6020.    whose contents concatenate the strings S1, S2, S3.  */
  6021.  
  6022. char *
  6023. concat (s1, s2, s3)
  6024.      char *s1, *s2, *s3;
  6025. {
  6026.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  6027.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  6028.  
  6029.   strcpy (result, s1);
  6030.   strcpy (result + len1, s2);
  6031.   strcpy (result + len1 + len2, s3);
  6032.   result[len1 + len2 + len3] = 0;
  6033.  
  6034.   return result;
  6035. }
  6036.  
  6037. /* Parse the string ARG using scanf format FORMAT, and return the result.
  6038.    If it does not parse, report fatal error
  6039.    generating the error message using format string ERROR and ARG as arg.  */
  6040.  
  6041. int
  6042. parse (arg, format, error)
  6043.      char *arg, *format, *error;
  6044. {
  6045.   int x;
  6046.   if (1 != sscanf (arg, format, &x))
  6047.     fatal (error, arg);
  6048.   return x;
  6049. }
  6050.  
  6051. /* Like malloc but get fatal error if memory is exhausted.  */
  6052.  
  6053. void *
  6054. xmalloc (size)
  6055.      int size;
  6056. {
  6057.   register void *result = malloc (size);
  6058.   if (!result)
  6059.     fatal ("virtual memory exhausted", 0);
  6060.   return result;
  6061. }
  6062.  
  6063. /* Like realloc but get fatal error if memory is exhausted.  */
  6064.  
  6065. void *
  6066. xrealloc (ptr, size)
  6067.      void *ptr;
  6068.      int size;
  6069. {
  6070.   register void *result = realloc (ptr, size);
  6071.   if (!result)
  6072.     fatal ("virtual memory exhausted", 0);
  6073.   return result;
  6074. }
  6075.  
  6076. #ifdef USG
  6077.  
  6078. void
  6079. bzero (p, n)
  6080.      char *p;
  6081. {
  6082.   memset (p, 0, n);
  6083. }
  6084.  
  6085. void
  6086. bcopy (from, to, n)
  6087.      char *from, *to;
  6088. {
  6089.   memcpy (to, from, n);
  6090. }
  6091.  
  6092. #ifndef pyr
  6093. getpagesize ()
  6094. {
  6095.   return (4096);
  6096. }
  6097. #endif
  6098.  
  6099. #endif
  6100.  
  6101. #if (defined(MINIX) || defined(atariminix))
  6102. /* Write the symbol table and string table into minix_out_file.  */
  6103.  
  6104. #ifdef SZ_HEAD
  6105. #undef SZ_HEAD        /* conflict with def in out.h */
  6106. #endif
  6107.  
  6108. #ifdef SF_HEAD
  6109. #undef SF_HEAD        /* conflict with def in out.h */
  6110. #endif
  6111.  
  6112. #include <out.h> /* format of output of /usr/lib/ld -- minixSt update#9 */
  6113.  
  6114. FILE *minix_outname_file, *minix_strings_file;
  6115. unsigned short minix_nname;
  6116. long minix_nchar;
  6117. unsigned short minix_swap_short();
  6118. long minix_swap_long();
  6119.  
  6120. write_minix_sym(p, str, gflag, fflag)
  6121. register struct nlist * p;
  6122. char * str;
  6123. int gflag, fflag;
  6124. {
  6125.   struct outname sym;
  6126.   int i;
  6127.  
  6128. /* fprintf(stderr, "sym %s\n", str); */
  6129.  
  6130.   sym.on_foff = minix_nchar;    /* we will patch in offset later */
  6131.   for(i = 0; str[i]; i++)
  6132.   {
  6133.       putc(str[i], minix_strings_file);
  6134.       minix_nchar++;
  6135.   }
  6136.   putc(0, minix_strings_file);
  6137.   minix_nchar++;
  6138.   
  6139.   switch (p->n_type & N_TYPE)
  6140.     {
  6141.     case N_UNDF: sym.on_type = S_TYP & S_UND; break;
  6142.     case N_ABS:  sym.on_type = S_TYP & S_ABS; break;
  6143.     case N_TEXT: sym.on_type = S_TYP & S_MIN; break;
  6144.     case N_DATA: sym.on_type = S_TYP & (S_MIN + 2); break;
  6145.     case N_BSS:  sym.on_type = S_TYP & (S_MIN + 3); break;
  6146.     default:     sym.on_type = 0;
  6147.       /* figure out rest of this later */
  6148.     }
  6149.   if(gflag == 1) 
  6150.       sym.on_type |= S_EXT; 
  6151.   if(fflag == 1)
  6152.       sym.on_type |= S_ETC & S_FIL;
  6153.   sym.on_type = minix_swap_short(&sym.on_type);
  6154.   sym.on_desc = 0;
  6155.   sym.on_valu = p->n_value;
  6156.   sym.on_valu = minix_swap_long(&sym.on_valu);
  6157. #ifndef WORD_ALIGNED
  6158.   fwrite(&sym, sizeof(sym), 1, minix_outname_file);
  6159. #else
  6160.   fwrite(&sym.on_foff, sizeof sym.on_foff, 1, minix_outname_file);
  6161.   fwrite(&sym.on_type, sizeof sym.on_type, 1, minix_outname_file);
  6162.   fwrite(&sym.on_desc, sizeof sym.on_desc, 1, minix_outname_file);
  6163.   fwrite(&sym.on_valu, sizeof sym.on_valu, 1, minix_outname_file);
  6164. #endif  
  6165.   minix_nname++;
  6166.   
  6167. }
  6168.  
  6169. void write_minix_syms(entry, syms_written_addr)
  6170. struct file_entry * entry;
  6171. int * syms_written_addr;
  6172. {
  6173.   register struct nlist *p = entry->symbols;
  6174.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  6175.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  6176.  
  6177.   /* Read the file's string table.  */
  6178.  
  6179.   entry->strings = (char *) alloca (entry->string_size);
  6180.   read_entry_strings (file_open (entry), entry);
  6181.  
  6182.   /* Generate a local symbol for the start of this file's text.  */
  6183.  
  6184.     {
  6185.       struct nlist nl;
  6186.  
  6187.       nl.n_type = N_TEXT;
  6188. /*      nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); */
  6189.       nl.n_value = entry->text_start_address;
  6190.       nl.n_desc = 0;
  6191.       nl.n_other = 0;
  6192. /*      *bufp++ = nl;    */
  6193.       write_minix_sym(&nl, entry->local_sym_name, 0, 1);
  6194.       (*syms_written_addr)++;
  6195. /* necessary? */
  6196.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  6197.     }
  6198.  
  6199.   for (; p < end; p++)
  6200.     {
  6201.       register int type = p->n_type;
  6202.       register int write = 0;
  6203.  
  6204.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  6205.  
  6206.       if (!(type & (N_STAB | N_EXT)))
  6207.         /* ordinary local symbol */
  6208.     write = !((p->n_un.n_strx + entry->strings)[0] == 'L');
  6209.       else if (!(type & N_EXT))
  6210.     /* debugger symbol */
  6211.     write = 0;
  6212.  
  6213.       if (write)
  6214.     {
  6215.       /* If this symbol has a name, write it */
  6216.  
  6217.       if (p->n_un.n_strx)
  6218.         {
  6219.         write_minix_sym(p, p->n_un.n_strx + entry->strings, 0, 0);
  6220.         (*syms_written_addr)++;
  6221.         }
  6222.     }
  6223.     }
  6224.   entry->strings = NULL;  
  6225. }
  6226.  
  6227. void do_write_minix_out ()
  6228. {
  6229.   int n_syms_written = 0;
  6230.   register symbol *sp;
  6231.   int i;
  6232.   char minix_outname_name[10], minix_strings_name[10];
  6233.   struct outhead oh;
  6234.   struct outname sym;
  6235.   long string_off;
  6236.     
  6237.   /* create two tmp files, one for outname structs one for strings, */
  6238.   /* later these two are concatenated into minix_out_file and unlinked */
  6239.   strcpy(minix_outname_name, "ldnXXXXXX");
  6240.   strcpy(minix_strings_name, "ldsXXXXXX");
  6241.   mktemp(minix_outname_name);
  6242.   mktemp(minix_strings_name);
  6243.   if(((minix_outname_file = fopen(minix_outname_name, "w"))
  6244.       == (FILE *)NULL) ||
  6245.      ((minix_strings_file = fopen(minix_strings_name, "w"))
  6246.       == (FILE *)NULL))
  6247.       fatal("Cannot creat tmp files", (char *)NULL);
  6248.   /* make up a out.h format header */
  6249.   oh.oh_magic = O_MAGIC;
  6250.   oh.oh_magic = minix_swap_short(&oh.oh_magic);
  6251.   oh.oh_stamp = O_STAMP;
  6252.   oh.oh_stamp = minix_swap_short(&oh.oh_stamp);
  6253.   oh.oh_flags = 0;
  6254.   oh.oh_nsect = 0;
  6255.   oh.oh_nrelo = 0;
  6256.   oh.oh_nname = 0;    /* will be patched */
  6257.   oh.oh_nemit = 0;
  6258.   oh.oh_nchar = 0;    /* will be patched */
  6259. #ifndef WORD_ALIGNED
  6260.   fwrite(&oh, sizeof(oh), 1, minix_outname_file);
  6261. #else
  6262.   fwrite(&oh.oh_magic, sizeof oh.oh_magic, 1, minix_outname_file);
  6263.   fwrite(&oh.oh_stamp, sizeof oh.oh_stamp, 1, minix_outname_file);
  6264.   fwrite(&oh.oh_flags, sizeof oh.oh_flags, 1, minix_outname_file);
  6265.   fwrite(&oh.oh_nsect, sizeof oh.oh_nsect, 1, minix_outname_file);
  6266.   fwrite(&oh.oh_nrelo, sizeof oh.oh_nrelo, 1, minix_outname_file);
  6267.   fwrite(&oh.oh_nname, sizeof oh.oh_nname, 1, minix_outname_file);
  6268.   fwrite(&oh.oh_nemit, sizeof oh.oh_nemit, 1, minix_outname_file);
  6269.   fwrite(&oh.oh_nchar, sizeof oh.oh_nchar, 1, minix_outname_file);
  6270. #endif
  6271.  
  6272.   minix_nname = 0;
  6273.   minix_nchar = 0L;
  6274.   
  6275.   each_file(write_minix_syms, &n_syms_written);
  6276.  
  6277.   /* Now write out the global symbols.  */
  6278.  
  6279.   /* Scan the symbol hash table, bucket by bucket.  */
  6280.  
  6281.   for (i = 0; i < TABSIZE; i++)
  6282.     for (sp = symtab[i]; sp; sp = sp->link)
  6283.       {
  6284.     struct nlist nl;
  6285.  
  6286.     nl.n_other = 0;
  6287.     nl.n_desc = 0;
  6288.  
  6289.     /* Compute a `struct nlist' for the symbol.  */
  6290.  
  6291.     if (sp->defined || sp->referenced)
  6292.       {
  6293.         if (!sp->defined)          /* undefined -- legit only if -r */
  6294.           {
  6295.         nl.n_type = N_UNDF | N_EXT;
  6296.         nl.n_value = 0;
  6297.           }
  6298.         else if (sp->defined > 1) /* defined with known type */
  6299.           {
  6300.         nl.n_type = sp->defined;
  6301.         nl.n_value = sp->value;
  6302.           }
  6303.         else if (sp->max_common_size) /* defined as common but not allocated. */
  6304.           {
  6305.         /* happens only with -r and not -d */
  6306.         /* write out a common definition */
  6307.         nl.n_type = N_UNDF | N_EXT;
  6308.         nl.n_value = sp->max_common_size;
  6309.           }
  6310.         else
  6311.           fatal ("internal error: %s defined in mysterious way", sp->name);
  6312.  
  6313.         /* write and count it.  */
  6314.  
  6315.         write_minix_sym(&nl, sp->name, 1, 0);
  6316.         n_syms_written++;
  6317.       }
  6318.       }
  6319. #if 0
  6320.   if (n_syms_written != nsyms)
  6321.     fprintf(stderr, "Bogon alert!  wrote %d syms, expected to write %d\n",
  6322.              n_syms_written, nsyms);
  6323. #endif
  6324.  
  6325.   /* close the temp files, open minix_out_file, and concat */
  6326.   fclose(minix_strings_file);
  6327. #ifndef WORD_ALIGNED
  6328.   string_off = minix_nname * sizeof(sym) + sizeof(oh);
  6329. #else
  6330.   string_off = minix_nname * 12 + 20;
  6331. #endif
  6332.   
  6333.   /* patch in oh_nname and oh_nchar */
  6334.   fseek(minix_outname_file, 10L, 0);
  6335.   minix_nname = minix_swap_short(&minix_nname);
  6336.   fwrite(&minix_nname, sizeof(ushort), 1, minix_outname_file);
  6337.   fseek(minix_outname_file, 16L, 0);
  6338.   minix_nchar = minix_swap_long(&minix_nchar);
  6339.   fwrite(&minix_nchar, sizeof(long), 1, minix_outname_file);
  6340.   
  6341.   fclose(minix_outname_file);
  6342.  
  6343.   if(((minix_outname_file = fopen(minix_outname_name, "r"))
  6344.       == (FILE *)NULL) ||
  6345.      ((minix_strings_file = fopen(minix_strings_name, "r"))
  6346.       == (FILE *)NULL))
  6347.       fatal("Cannot open tmp files for read", (char *)NULL);
  6348.   if((minix_out_filep = fopen(minix_out_filename, "w")) == (FILE *)NULL)
  6349.       fatal("Cannot create %s", minix_out_filename);
  6350.  
  6351. #ifndef WORD_ALIGNED
  6352.   fread(&oh, sizeof(oh), 1, minix_outname_file);
  6353.   fwrite(&oh, sizeof(oh), 1, minix_out_filep);
  6354.   while(fread(&sym, sizeof(sym), 1, minix_outname_file) == 1)
  6355.   {
  6356.       sym.on_foff += string_off;
  6357.       sym.on_foff = minix_swap_long(&sym.on_foff);
  6358.       fwrite(&sym, sizeof(sym), 1, minix_out_filep);
  6359.   }
  6360. #else
  6361.   fread(&oh, 20, 1, minix_outname_file);
  6362.   fwrite(&oh, 20, 1, minix_out_filep);
  6363.   while(fread(&sym.on_foff, sizeof sym.on_foff, 1, minix_outname_file) == 1)
  6364.   {
  6365.       fread(&sym.on_type, sizeof sym.on_type, 1, minix_outname_file);
  6366.       fread(&sym.on_desc, sizeof sym.on_desc, 1, minix_outname_file);
  6367.       fread(&sym.on_valu, sizeof sym.on_valu, 1, minix_outname_file);
  6368.       
  6369.       sym.on_foff += string_off;
  6370.       sym.on_foff = minix_swap_long(&sym.on_foff);
  6371.  
  6372.       fwrite(&sym.on_foff, sizeof sym.on_foff, 1, minix_out_filep);
  6373.       fwrite(&sym.on_type, sizeof sym.on_type, 1, minix_out_filep);
  6374.       fwrite(&sym.on_desc, sizeof sym.on_desc, 1, minix_out_filep);
  6375.       fwrite(&sym.on_valu, sizeof sym.on_valu, 1, minix_out_filep);
  6376.  
  6377.   }
  6378. #endif
  6379.  
  6380. #if 0
  6381.   fprintf(stderr,"Curr %ld Str_off %ld\n", ftell(minix_out_filep), string_off);
  6382. #endif
  6383.   
  6384.   while((i = getc(minix_strings_file)) != EOF)
  6385.       putc(i, minix_out_filep);
  6386.   fclose(minix_strings_file);
  6387.   fclose(minix_outname_file);
  6388.   fclose(minix_out_filep);
  6389.  
  6390.   unlink(minix_strings_name);
  6391.   unlink(minix_outname_name);
  6392.   
  6393. }
  6394.  
  6395. unsigned short minix_swap_short(s)
  6396. unsigned char s[];
  6397. {
  6398.     unsigned short i = (s[1] << 8) | s[0];
  6399.     
  6400.     return i;
  6401. }
  6402.  
  6403. long minix_swap_long(s)
  6404. unsigned char s[];
  6405. {
  6406.     unsigned long i = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0];
  6407.     return i;
  6408. }
  6409.  
  6410. #endif
  6411.