home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / util_src / ld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-29  |  145.5 KB  |  5,476 lines

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