home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / programm / programi / gcc_9112.lzh / gnu-src / ld.c < prev   
Encoding:
C/C++ Source or Header  |  1991-10-09  |  186.2 KB  |  6,904 lines

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