home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ld / dist / patch / ld.diffs < prev    next >
Encoding:
Text File  |  1990-10-26  |  150.4 KB  |  5,407 lines

  1. *** ld.original    Thu Oct 25 22:09:56 1990
  2. --- /tmp/jhh/binutils/ld.c    Thu Jun 21 14:52:49 1990
  3. ***************
  4. *** 23,37 ****
  5. --- 23,60 ----
  6.   #include <sys/types.h>
  7.   #include <sys/stat.h>
  8.   #include <sys/file.h>
  9. + #ifndef USG
  10. + #include <sys/time.h>
  11. + #include <sys/resource.h>
  12. + #endif
  13.   #ifndef sony_news
  14.   #include <fcntl.h>
  15.   #endif
  16.   
  17. + #if !defined(A_OUT) && !defined(MACH_O)
  18. + #define A_OUT
  19. + #endif
  20. + #ifdef A_OUT
  21.   #ifdef COFF_ENCAPSULATE
  22.   #include "a.out.encap.h"
  23.   #else
  24.   #include <a.out.h>
  25.   #endif
  26. + #endif
  27. + #ifdef MACH_O
  28. + #ifndef A_OUT
  29. + #include <nlist.h>
  30. + #include <reloc.h>
  31. + #endif
  32. + #ifndef N_TEXT
  33. + #define N_TEXT 0x04
  34. + #define N_DATA 0x06
  35. + #define N_BSS 0x08
  36. + #endif
  37. + #include <sys/loader.h>
  38. + #endif
  39.   
  40.   #ifndef N_SET_MAGIC
  41.   #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  42. ***************
  43. *** 39,47 ****
  44.   
  45.   /* If compiled with GNU C, use the built-in alloca */
  46.   #ifdef __GNUC__
  47. ! #define alloca __builtin_alloca
  48.   #endif
  49.   
  50.   /* Always use the GNU version of debugging symbol type codes, if possible.  */
  51.   
  52.   #include "stab.h"
  53. --- 62,78 ----
  54.   
  55.   /* If compiled with GNU C, use the built-in alloca */
  56.   #ifdef __GNUC__
  57. ! # define alloca __builtin_alloca
  58. ! #else
  59. ! # if defined(sun) && defined(sparc)
  60. ! #  include "alloca.h"
  61. ! # else
  62. ! char *alloca ();
  63. ! # endif
  64.   #endif
  65.   
  66. + #include "getopt.h"
  67.   /* Always use the GNU version of debugging symbol type codes, if possible.  */
  68.   
  69.   #include "stab.h"
  70. ***************
  71. *** 86,102 ****
  72.   /* Define this to specify the default executable format.  */
  73.   
  74.   #ifdef hpux
  75. ! #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  76.   #endif
  77.   
  78. - #ifndef DEFAULT_MAGIC
  79. - #define DEFAULT_MAGIC ZMAGIC
  80. - #endif
  81.   /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  82.   
  83.   #ifndef N_TXTADDR
  84. ! #if defined(vax) || defined(sony_news)
  85.   #define N_TXTADDR(X) 0
  86.   #endif
  87.   #ifdef is68k
  88. --- 117,129 ----
  89.   /* Define this to specify the default executable format.  */
  90.   
  91.   #ifdef hpux
  92. ! #define DEFAULT_OUTPUT_STYLE OUTPUT_READONLY_TEXT
  93.   #endif
  94.   
  95.   /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  96.   
  97.   #ifndef N_TXTADDR
  98. ! #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  99.   #define N_TXTADDR(X) 0
  100.   #endif
  101.   #ifdef is68k
  102. ***************
  103. *** 105,114 ****
  104.   #ifdef sequent
  105.   #define    N_TXTADDR(x) (N_ADDRADJ(x))
  106.   #endif
  107.   #endif
  108.   
  109.   #ifndef N_DATADDR
  110. ! #if defined(vax) || defined(sony_news)
  111.   #define N_DATADDR(x) \
  112.       (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  113.       : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  114. --- 132,144 ----
  115.   #ifdef sequent
  116.   #define    N_TXTADDR(x) (N_ADDRADJ(x))
  117.   #endif
  118. + #ifdef NeXT
  119. + #define N_TXTADDR(X) ((X).a_magic == ZMAGIC ? page_size : 0)
  120. + #endif
  121.   #endif
  122.   
  123.   #ifndef N_DATADDR
  124. ! #if defined(vax) || defined(sony_news) || defined(hp300) || defined(pyr)
  125.   #define N_DATADDR(x) \
  126.       (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  127.       : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  128. ***************
  129. *** 124,130 ****
  130. --- 154,172 ----
  131.       (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  132.       : (page_size+(((x).a_text-1) & ~(page_size-1))))
  133.   #endif
  134. + #ifdef NeXT
  135. + #define N_DATADDR(X) \
  136. +     (((X).a_magic==ZMAGIC)?(N_TXTADDR(X)+(X).a_text+0xFFFF)&~0xFFFF \
  137. +      :N_TXTADDR(X)+(X).a_text)
  138.   #endif
  139. + #endif
  140. + /* The "address" of the data segment in a relocatable file.
  141. +    The text address of a relocatable file is always
  142. +    considered to be zero (instead of the value of N_TXTADDR, which
  143. +    is what the address is in an executable), so we need to subtract
  144. +    N_TXTADDR from N_DATADDR to get the "address" for the input file.  */
  145. + #define DATA_ADDR_DOT_O(hdr) (N_DATADDR(hdr) - N_TXTADDR(hdr))
  146.   
  147.   /* Define how to initialize system-dependent header fields.  */
  148.   #ifdef sun
  149. ***************
  150. *** 131,144 ****
  151.   #ifdef sparc
  152.   #define INITIALIZE_HEADER \
  153.     {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  154. ! #endif
  155. ! #if defined(mc68010) || defined(m68010)
  156. ! #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  157. ! #endif
  158. ! #ifndef INITIALIZE_HEADER
  159. ! #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  160. ! #endif
  161. ! #endif
  162.   #ifdef ALTOS
  163.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  164.   #endif
  165. --- 173,195 ----
  166.   #ifdef sparc
  167.   #define INITIALIZE_HEADER \
  168.     {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  169. ! #endif /* sparc.  */
  170. ! #if defined(mc68000)
  171. ! /* Set the machine type according to the machine type of the .o files.
  172. !    If they are all sun2 (68010), then the type of the executable is sun2.
  173. !    If any is sun3 (68020), then the type of the executable is sun3.
  174. !    This is consistent with the Sun loader and more useful than having
  175. !    it depend on which machine you are on when you run ld.  */
  176. ! static int sun_machtype = M_68010;
  177. ! #define INITIALIZE_HEADER outheader.a_machtype = sun_machtype
  178. ! #define READ_HEADER_HOOK(machtype) \
  179. !   if (machtype == M_68020)           \
  180. !     {                     \
  181. !       sun_machtype = M_68020;         \
  182. !     }
  183. ! #endif /* mc68000.  */
  184. ! #endif /* Sun.  */
  185.   #ifdef ALTOS
  186.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  187.   #endif
  188. ***************
  189. *** 155,161 ****
  190.   #endif
  191.   #if defined(i386) && !defined(sequent)
  192.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  193. ! #endif
  194.   
  195.   #ifdef is68k
  196.   /* This enables code to take care of an ugly hack in the ISI OS.
  197. --- 206,218 ----
  198.   #endif
  199.   #if defined(i386) && !defined(sequent)
  200.   #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  201. ! #endif /* Sequent symmetry.  */
  202. ! #if defined(hp300)
  203. ! #define INITIALIZE_HEADER outheader.a_mid = MID_HP300
  204. ! #endif /* hp300.  */
  205. ! #ifdef pyr
  206. ! #define INITIALIZE_HEADER outheader.a_machid = PYR90X
  207. ! #endif /* Pyramid.  */
  208.   
  209.   #ifdef is68k
  210.   /* This enables code to take care of an ugly hack in the ISI OS.
  211. ***************
  212. *** 164,179 ****
  213.   #define DOLLAR_KLUDGE
  214.   #endif
  215.   
  216. ! /*
  217. !  * Alloca include.
  218. !  */
  219. ! #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  220. ! #include "alloca.h"
  221. ! #endif
  222.   #ifndef L_SET
  223.   #define L_SET 0
  224.   #endif
  225.   
  226.   /*
  227.    * Ok.  Following are the relocation information macros.  If your
  228. --- 221,234 ----
  229.   #define DOLLAR_KLUDGE
  230.   #endif
  231.   
  232. ! /* Values for 3rd argument to lseek().  */
  233.   #ifndef L_SET
  234.   #define L_SET 0
  235.   #endif
  236. + /* This is called L_INCR in BSD, but SEEK_CUR in POSIX.  */
  237. + #ifndef SEEK_CUR
  238. + #define SEEK_CUR 1
  239. + #endif
  240.   
  241.   /*
  242.    * Ok.  Following are the relocation information macros.  If your
  243. ***************
  244. *** 193,199 ****
  245.    * what the value of the relocation actually was.  *Must be an lvalue*.
  246.    *
  247.    *   RELOC_TYPE (rval): For a non-external relocation, this is the
  248. !  * segment to relocate for.
  249.    *
  250.    *   RELOC_SYMBOL (rval): For an external relocation, this is the
  251.    * index of its symbol in the symbol table.  *Must be an lvalue*.
  252. --- 248,254 ----
  253.    * what the value of the relocation actually was.  *Must be an lvalue*.
  254.    *
  255.    *   RELOC_TYPE (rval): For a non-external relocation, this is the
  256. !  * segment to relocate for.  *Must be an lvalue.*
  257.    *
  258.    *   RELOC_SYMBOL (rval): For an external relocation, this is the
  259.    * index of its symbol in the symbol table.  *Must be an lvalue*.
  260. ***************
  261. *** 257,269 ****
  262.   /* Sparc (Sun 4) macros */
  263.   #undef relocation_info
  264.   #define relocation_info                    reloc_info_sparc
  265. ! #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  266. ! #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  267. ! #define RELOC_TYPE(r)                   ((r)->r_index)  
  268. ! #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  269.   #define RELOC_MEMORY_SUB_P(r)        0
  270.   #define RELOC_MEMORY_ADD_P(r)           0
  271. ! #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  272.   #define RELOC_PCREL_P(r)             \
  273.           ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  274.   #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  275. --- 312,324 ----
  276.   /* Sparc (Sun 4) macros */
  277.   #undef relocation_info
  278.   #define relocation_info                    reloc_info_sparc
  279. ! #define RELOC_ADDRESS(r)        ((r)->r_address)
  280. ! #define RELOC_EXTERN_P(r)               ((r)->r_extern)
  281. ! #define RELOC_TYPE(r)                   ((r)->r_index)
  282. ! #define RELOC_SYMBOL(r)                 ((r)->r_index)
  283.   #define RELOC_MEMORY_SUB_P(r)        0
  284.   #define RELOC_MEMORY_ADD_P(r)           0
  285. ! #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)
  286.   #define RELOC_PCREL_P(r)             \
  287.           ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  288.   #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  289. ***************
  290. *** 344,350 ****
  291.      So, for example, the following two lines placed in an assembler
  292.      input file would result in an object file which would direct gnu ld
  293.      to resolve all references to symbol "foo" as references to symbol
  294. !    "bar". 
  295.   
  296.       .stabs "_foo",11,0,0,0
  297.       .stabs "_bar",1,0,0,0
  298. --- 399,405 ----
  299.      So, for example, the following two lines placed in an assembler
  300.      input file would result in an object file which would direct gnu ld
  301.      to resolve all references to symbol "foo" as references to symbol
  302. !    "bar".
  303.   
  304.       .stabs "_foo",11,0,0,0
  305.       .stabs "_bar",1,0,0,0
  306. ***************
  307. *** 440,446 ****
  308.   
  309.   /* If a this type of symbol is encountered, its name is a warning
  310.      message to print each time the symbol referenced by the next symbol
  311. !    table entry is referenced.   
  312.   
  313.      This feature may be used to allow backwards compatibility with
  314.      certain functions (eg. gets) but to discourage programmers from
  315. --- 495,501 ----
  316.   
  317.   /* If a this type of symbol is encountered, its name is a warning
  318.      message to print each time the symbol referenced by the next symbol
  319. !    table entry is referenced.
  320.   
  321.      This feature may be used to allow backwards compatibility with
  322.      certain functions (eg. gets) but to discourage programmers from
  323. ***************
  324. *** 502,508 ****
  325.         /* Nonzero means definitions of this symbol as common have been seen,
  326.        and the value here is the largest size specified by any of them.  */
  327.         int max_common_size;
  328. !       /* For relocatable_output, records the index of this global sym in the
  329.        symbol table to be written, with the first global sym given index 0.*/
  330.         int def_count;
  331.         /* Nonzero means a definition of this global symbol is known to exist.
  332. --- 557,563 ----
  333.         /* Nonzero means definitions of this symbol as common have been seen,
  334.        and the value here is the largest size specified by any of them.  */
  335.         int max_common_size;
  336. !       /* For OUTPUT_RELOCATABLE, records the index of this global sym in the
  337.        symbol table to be written, with the first global sym given index 0.*/
  338.         int def_count;
  339.         /* Nonzero means a definition of this global symbol is known to exist.
  340. ***************
  341. *** 530,535 ****
  342. --- 585,599 ----
  343.       }
  344.     symbol;
  345.   
  346. + /* Demangler for C++.  */
  347. + extern char *cplus_demangle ();
  348. + /* Demangler function to use.  We unconditionally enable the C++ demangler
  349. +    because we assume any name it successfully demangles was probably produced
  350. +    by the C++ compiler.  Enabling it only if -lg++ was specified seems too
  351. +    much of a kludge.  */
  352. + char *(*demangler)() = cplus_demangle;
  353.   /* Number of buckets in symbol hash table */
  354.   #define    TABSIZE    1009
  355.   
  356. ***************
  357. *** 611,620 ****
  358. --- 675,694 ----
  359.      -e sets this.  */
  360.   symbol *entry_symbol;
  361.   
  362. + /* These can be NULL if we don't actually have such a symbol.  */
  363.   symbol *edata_symbol;   /* the symbol _edata */
  364.   symbol *etext_symbol;   /* the symbol _etext */
  365.   symbol *end_symbol;    /* the symbol _end */
  366. + /* We also need __{edata,etext,end} so that they can safely
  367. +    be used from an ANSI library.  */
  368. + symbol *edata_symbol_alt;
  369. + symbol *etext_symbol_alt;
  370. + symbol *end_symbol_alt;
  371.   
  372. + /* Kinds of files potentially understood by the linker. */
  373. + enum file_type { IS_UNKNOWN, IS_ARCHIVE, IS_A_OUT, IS_MACH_O };
  374.   /* Each input file, and each library member ("subfile") being loaded,
  375.      has a `file_entry' structure for it.
  376.   
  377. ***************
  378. *** 630,659 ****
  379.   struct file_entry {
  380.     /* Name of this file.  */
  381.     char *filename;
  382.     /* Name to use for the symbol giving address of text start */
  383.     /* Usually the same as filename, but for a file spec'd with -l
  384.        this is the -l switch itself rather than the filename.  */
  385.     char *local_sym_name;
  386.   
  387. !   /* Describe the layout of the contents of the file */
  388.   
  389. !   /* The file's a.out header.  */
  390. !   struct exec header;
  391. !   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  392. !   int symseg_offset;
  393.   
  394.     /* Describe data from the file loaded into core */
  395.   
  396.     /* Symbol table of the file.  */
  397.     struct nlist *symbols;
  398. !   /* Size in bytes of string table.  */
  399. !   int string_size;
  400.     /* Pointer to the string table.
  401.        The string table is not kept in core all the time,
  402.        but when it is in core, its address is here.  */
  403.     char *strings;
  404.   
  405. !   /* Next two used only if `relocatable_output' or if needed for */
  406.     /* output of undefined reference line numbers. */
  407.   
  408.     /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  409. --- 704,771 ----
  410.   struct file_entry {
  411.     /* Name of this file.  */
  412.     char *filename;
  413. +   /* What kind of file this is. */
  414. +   enum file_type file_type;
  415.     /* Name to use for the symbol giving address of text start */
  416.     /* Usually the same as filename, but for a file spec'd with -l
  417.        this is the -l switch itself rather than the filename.  */
  418.     char *local_sym_name;
  419.   
  420. !   /* Describe the layout of the contents of the file.  */
  421.   
  422. !   /* The text section. */
  423. !   unsigned long int orig_text_address;
  424. !   unsigned long int text_size;
  425. !   long int text_offset;
  426. !   /* Text relocation. */
  427. !   unsigned long int text_reloc_size;
  428. !   long int text_reloc_offset;
  429. !   /* The data section. */
  430. !   unsigned long int orig_data_address;
  431. !   unsigned long int data_size;
  432. !   long int data_offset;
  433. !   /* Data relocation. */
  434. !   unsigned long int data_reloc_size;
  435. !   long int data_reloc_offset;
  436. !   /* The bss section. */
  437. !   unsigned long int orig_bss_address;
  438. !   unsigned long int bss_size;
  439. !   /* The symbol and string tables. */
  440. !   unsigned long int syms_size;
  441. !   long int syms_offset;
  442. !   unsigned long int strs_size;
  443. !   long int strs_offset;
  444. !   /* The GDB symbol segment, if any. */
  445. !   unsigned long int symseg_size;
  446. !   long int symseg_offset;
  447. ! #ifdef MACH_O
  448. !   /* Section ordinals from the Mach-O load commands.  These
  449. !      are compared with the n_sect fields of symbols.  */
  450. !   int text_ordinal;
  451. !   int data_ordinal;
  452. !   int bss_ordinal;
  453. ! #endif
  454.   
  455.     /* Describe data from the file loaded into core */
  456.   
  457.     /* Symbol table of the file.  */
  458.     struct nlist *symbols;
  459.     /* Pointer to the string table.
  460.        The string table is not kept in core all the time,
  461.        but when it is in core, its address is here.  */
  462.     char *strings;
  463.   
  464. !   /* Next two used only if OUTPUT_RELOCATABLE or if needed for */
  465.     /* output of undefined reference line numbers. */
  466.   
  467.     /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  468. ***************
  469. *** 724,750 ****
  470.   
  471.   char *output_filename;
  472.   
  473. ! /* Descriptor for writing that file with `mywrite'.  */
  474.   
  475. ! int outdesc;
  476.   
  477. ! /* Header for that file (filled in by `write_header').  */
  478.   
  479. ! struct exec outheader;
  480.   
  481. ! #ifdef COFF_ENCAPSULATE
  482. ! struct coffheader coffheader;
  483. ! int need_coff_header;
  484.   #endif
  485.   
  486.   /* The following are computed by `digest_symbols'.  */
  487.   
  488. ! int text_size;        /* total size of text of all input files.  */
  489. ! int data_size;        /* total size of data of all input files.  */
  490. ! int bss_size;        /* total size of bss of all input files.  */
  491. ! int text_reloc_size;    /* total size of text relocation of all input files.  */
  492. ! int data_reloc_size;    /* total size of data relocation of all input */
  493. !             /* files.  */
  494.   
  495.   /* Specifications of start and length of the area reserved at the end
  496.      of the text segment for the set vectors.  Computed in 'digest_symbols' */
  497. --- 836,905 ----
  498.   
  499.   char *output_filename;
  500.   
  501. ! /* What kind of output file to write.  */
  502.   
  503. ! enum file_type output_file_type;
  504.   
  505. ! #ifndef DEFAULT_OUTPUT_FILE_TYPE
  506. ! #ifdef MACH_O
  507. ! #define DEFAULT_OUTPUT_FILE_TYPE IS_MACH_O
  508. ! #else
  509. ! #define DEFAULT_OUTPUT_FILE_TYPE IS_A_OUT
  510. ! #endif
  511. ! #endif
  512. ! /* What `style' of output file to write.  For BSD a.out files
  513. !    this specifies OMAGIC, NMAGIC, or ZMAGIC.  For Mach-O files
  514. !    this switches between MH_OBJECT and two flavors of MH_EXECUTE.  */
  515.   
  516. ! enum output_style
  517. !   {
  518. !     OUTPUT_UNSPECIFIED,
  519. !     OUTPUT_RELOCATABLE,        /* -r */
  520. !     OUTPUT_WRITABLE_TEXT,    /* -N */
  521. !     OUTPUT_READONLY_TEXT,    /* -n */
  522. !     OUTPUT_DEMAND_PAGED        /* -Z (default) */
  523. !   };
  524. ! enum output_style output_style;
  525.   
  526. ! #ifndef DEFAULT_OUTPUT_STYLE
  527. ! #define DEFAULT_OUTPUT_STYLE OUTPUT_DEMAND_PAGED
  528.   #endif
  529.   
  530. + /* Descriptor for writing that file with `mywrite'.  */
  531. + int outdesc;
  532.   /* The following are computed by `digest_symbols'.  */
  533.   
  534. ! int text_size;            /* total size of text of all input files.  */
  535. ! int text_header_size;        /* size of the file header if included in the
  536. !                    text size.  */
  537. ! int data_size;            /* total size of data of all input files.  */
  538. ! int bss_size;            /* total size of bss of all input files.  */
  539. ! int text_reloc_size;        /* total size of text relocation of all input files.  */
  540. ! int data_reloc_size;        /* total size of data relocation of all input
  541. !                    files.  */
  542. !   
  543. ! /* The following are computed by write_header().  */
  544. ! long int output_text_offset;    /* file offset of the text section.  */
  545. ! long int output_data_offset;    /* file offset of the data section.  */
  546. ! long int output_trel_offset;    /* file offset of the text relocation info.  */
  547. ! long int output_drel_offset;    /* file offset of the data relocation info.  */
  548. ! long int output_syms_offset;    /* file offset of the symbol table.  */
  549. ! long int output_strs_offset;    /* file offset of the string table.  */
  550. ! /* The following are incrementally computed by write_syms(); we keep
  551. !    them here so we can examine their values afterwards.  */
  552. ! unsigned int output_syms_size;    /* total bytes of symbol table output. */
  553. ! unsigned int output_strs_size;    /* total bytes of string table output. */
  554. ! /* This can only be computed after the size of the string table is known.  */
  555. ! long int output_symseg_offset;    /* file offset of the symbol segment (if any).  */
  556. ! /* Incrementally computed by write_file_symseg().  */
  557. ! unsigned int output_symseg_size;
  558.   
  559.   /* Specifications of start and length of the area reserved at the end
  560.      of the text segment for the set vectors.  Computed in 'digest_symbols' */
  561. ***************
  562. *** 755,766 ****
  563.      written. */
  564.   unsigned long *set_vectors;
  565.   
  566. ! /* Amount of cleared space to leave between the text and data segments.  */
  567.   
  568.   int text_pad;
  569.   
  570. ! /* Amount of bss segment to include as part of the data segment.  */
  571.   int data_pad;
  572.   
  573.   /* Format of __.SYMDEF:
  574. --- 910,922 ----
  575.      written. */
  576.   unsigned long *set_vectors;
  577.   
  578. ! /* Amount of cleared space to leave at the end of the text segment.  */
  579.   
  580.   int text_pad;
  581.   
  582. ! /* Amount of padding at end of data segment.  This has two parts:
  583. !    That which is before the bss segment, and that which overlaps
  584. !    with the bss segment.  */
  585.   int data_pad;
  586.   
  587.   /* Format of __.SYMDEF:
  588. ***************
  589. *** 781,796 ****
  590.      write any padding in the output file for it.  */
  591.   int text_start;
  592.   
  593. - /* Offset of default entry-pc within the text section.  */
  594. - int entry_offset;
  595.   /* Address we decide the data section will be loaded at.  */
  596.   int data_start;
  597.   
  598. - /* `text-start' address is normally this much plus a page boundary.
  599. -    This is not a user option; it is fixed for each system.  */
  600. - int text_start_alignment;
  601.   /* Nonzero if -T was specified in the command line.
  602.      This prevents text_start from being set later to default values.  */
  603.   int T_flag_specified;
  604. --- 937,945 ----
  605. ***************
  606. *** 804,812 ****
  607.      and reduce the size of the bss section to match.  */
  608.   int specified_data_size;
  609.   
  610. - /* Magic number to use for the output file, set by switch.  */
  611. - int magic;
  612.   /* Nonzero means print names of input files as processed.  */
  613.   int trace_files;
  614.   
  615. --- 953,958 ----
  616. ***************
  617. *** 822,831 ****
  618.   /* 1 => write load map.  */
  619.   int write_map;
  620.   
  621. ! /* 1 => write relocation into output file so can re-input it later.  */
  622. ! int relocatable_output;
  623. ! /* 1 => assign space to common symbols even if `relocatable_output'.  */
  624.   int force_common_definition;
  625.   
  626.   /* Standard directories to search for files specified by -l.  */
  627. --- 968,974 ----
  628.   /* 1 => write load map.  */
  629.   int write_map;
  630.   
  631. ! /* 1 => assign space to common symbols even if OUTPUT_RELOCATABLE. */
  632.   int force_common_definition;
  633.   
  634.   /* Standard directories to search for files specified by -l.  */
  635. ***************
  636. *** 840,845 ****
  637. --- 983,991 ----
  638.   #endif
  639.   #endif
  640.   
  641. + /* If set STANDARD_SEARCH_DIRS is not searched.  */
  642. + int no_standard_dirs;
  643.   /* Actual vector of directories to search;
  644.      this contains those specified with -L plus the standard ones.  */
  645.   char **search_dirs;
  646. ***************
  647. *** 847,854 ****
  648.   /* Length of the vector `search_dirs'.  */
  649.   int n_search_dirs;
  650.   
  651. ! /* Non zero means to create the output executable. */
  652. ! /* Cleared by nonfatal errors.  */
  653.   int make_executable;
  654.   
  655.   /* Force the executable to be output, even if there are non-fatal
  656. --- 993,1000 ----
  657.   /* Length of the vector `search_dirs'.  */
  658.   int n_search_dirs;
  659.   
  660. ! /* Non zero means to create the output executable.
  661. !    Cleared by nonfatal errors.  */
  662.   int make_executable;
  663.   
  664.   /* Force the executable to be output, even if there are non-fatal
  665. ***************
  666. *** 861,875 ****
  667.   struct glosym **cmdline_references;
  668.   int cl_refs_allocated;
  669.   
  670.   void bcopy (), bzero ();
  671. - int malloc (), realloc ();
  672. - #ifndef alloca
  673. - int alloca ();
  674.   #endif
  675. ! int free ();
  676.   
  677. ! int xmalloc ();
  678. ! int xrealloc ();
  679.   void fatal ();
  680.   void fatal_with_file ();
  681.   void perror_name ();
  682. --- 1007,1021 ----
  683.   struct glosym **cmdline_references;
  684.   int cl_refs_allocated;
  685.   
  686. + #ifndef bcopy
  687.   void bcopy (), bzero ();
  688.   #endif
  689. ! char *malloc (), *realloc ();
  690. ! void free ();
  691.   
  692. ! char *xmalloc ();
  693. ! char *xrealloc ();
  694. ! void usage ();
  695.   void fatal ();
  696.   void fatal_with_file ();
  697.   void perror_name ();
  698. ***************
  699. *** 876,881 ****
  700. --- 1022,1030 ----
  701.   void perror_file ();
  702.   void error ();
  703.   
  704. + int parse ();
  705. + void initialize_text_start ();
  706. + void initialize_data_start ();
  707.   void digest_symbols ();
  708.   void print_symbols ();
  709.   void load_symbols ();
  710. ***************
  711. *** 905,910 ****
  712. --- 1054,1070 ----
  713.     page_size = getpagesize ();
  714.     progname = argv[0];
  715.   
  716. + #ifdef RLIMIT_STACK
  717. +   /* Avoid dumping core on large .o files.  */
  718. +   {
  719. +     struct rlimit rl;
  720. +     getrlimit (RLIMIT_STACK, &rl);
  721. +     rl.rlim_cur = rl.rlim_max;
  722. +     setrlimit (RLIMIT_STACK, &rl);
  723. +   }
  724. + #endif
  725.     /* Clear the cumulative info on the output file.  */
  726.   
  727.     text_size = 0;
  728. ***************
  729. *** 924,934 ****
  730.     discard_locals = DISCARD_NONE;
  731.     entry_symbol = 0;
  732.     write_map = 0;
  733. -   relocatable_output = 0;
  734.     force_common_definition = 0;
  735.     T_flag_specified = 0;
  736.     Tdata_flag_specified = 0;
  737. -   magic = DEFAULT_MAGIC;
  738.     make_executable = 1;
  739.     force_executable = 0;
  740.     set_element_prefixes = 0;
  741. --- 1084,1092 ----
  742. ***************
  743. *** 947,952 ****
  744. --- 1105,1111 ----
  745.     common_defined_global_count = 0;
  746.   
  747.     /* Keep a list of symbols referenced from the command line */
  748.     cl_refs_allocated = 10;
  749.     cmdline_references
  750.       = (struct glosym **) xmalloc (cl_refs_allocated
  751. ***************
  752. *** 957,1004 ****
  753.   
  754.     decode_command (argc, argv);
  755.   
  756. -   /* Create the symbols `etext', `edata' and `end'.  */
  757. -   if (!relocatable_output)
  758. -     symtab_init ();
  759. -   /* Determine whether to count the header as part of
  760. -      the text size, and initialize the text size accordingly.
  761. -      This depends on the kind of system and on the output format selected.  */
  762. -   N_SET_MAGIC (outheader, magic);
  763. - #ifdef INITIALIZE_HEADER
  764. -   INITIALIZE_HEADER;
  765. - #endif
  766. -   text_size = sizeof (struct exec);
  767. - #ifdef COFF_ENCAPSULATE
  768. -   if (relocatable_output == 0)
  769. -     {
  770. -       need_coff_header = 1;
  771. -       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  772. -       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  773. -       text_size += sizeof (struct coffheader);
  774. -     }
  775. - #endif
  776. -   text_size -= N_TXTOFF (outheader);
  777. -   if (text_size < 0)
  778. -     text_size = 0;
  779. -   entry_offset = text_size;
  780. -   if (!T_flag_specified && !relocatable_output)
  781. -     text_start = N_TXTADDR (outheader);
  782. -   /* The text-start address is normally this far past a page boundary.  */
  783. -   text_start_alignment = text_start % page_size;
  784.     /* Load symbols of all input files.
  785.        Also search all libraries and decide which library members to load.  */
  786.   
  787.     load_symbols ();
  788.   
  789.     /* Compute where each file's sections go, and relocate symbols.  */
  790.   
  791.     digest_symbols ();
  792. --- 1116,1133 ----
  793.   
  794.     decode_command (argc, argv);
  795.   
  796.     /* Load symbols of all input files.
  797.        Also search all libraries and decide which library members to load.  */
  798.   
  799.     load_symbols ();
  800.   
  801. +   /* Create various built-in symbols.  This must occur after
  802. +      all input files are loaded so that a user program can have a
  803. +      symbol named etext (for example).  */
  804. +   if (output_style != OUTPUT_RELOCATABLE)
  805. +     symtab_init ();
  806.     /* Compute where each file's sections go, and relocate symbols.  */
  807.   
  808.     digest_symbols ();
  809. ***************
  810. *** 1020,1071 ****
  811.     exit (!make_executable);
  812.   }
  813.   
  814. ! void decode_option ();
  815. ! /* Analyze a command line argument.
  816. !    Return 0 if the argument is a filename.
  817. !    Return 1 if the argument is a option complete in itself.
  818. !    Return 2 if the argument is a option which uses an argument.
  819. !    Thus, the value is the number of consecutive arguments
  820. !    that are part of options.  */
  821. ! int
  822. ! classify_arg (arg)
  823. !      register char *arg;
  824. ! {
  825. !   if (*arg != '-') return 0;
  826. !   switch (arg[1])
  827. !     {
  828. !     case 'A':
  829. !     case 'D':
  830. !     case 'e':
  831. !     case 'L':
  832. !     case 'l':
  833. !     case 'o':
  834. !     case 'u':
  835. !     case 'V':
  836. !     case 'y':
  837. !       if (arg[2])
  838. !     return 1;
  839. !       return 2;
  840. !     case 'B':
  841. !       if (! strcmp (&arg[2], "static"))
  842. !     return 1;
  843. !     case 'T':
  844. !       if (arg[2] == 0)
  845. !     return 2;
  846. !       if (! strcmp (&arg[2], "text"))
  847. !     return 2;
  848. !       if (! strcmp (&arg[2], "data"))
  849. !     return 2;
  850. !       return 1;
  851. !     }
  852.   
  853. !   return 1;
  854. ! }
  855.   
  856.   /* Process the command arguments,
  857.      setting up file_table with an entry for each input file,
  858. --- 1149,1190 ----
  859.     exit (!make_executable);
  860.   }
  861.   
  862. ! void add_cmdline_ref ();
  863.   
  864. ! static struct option longopts[] =
  865. ! {
  866. !   {"d", 0, 0, 'd'},
  867. !   {"dc", 0, 0, 'd'},        /* For Sun compatibility. */
  868. !   {"dp", 0, 0, 'd'},        /* For Sun compatibility. */
  869. !   {"e", 1, 0, 'e'},
  870. !   {"n", 0, 0, 'n'},
  871. !   {"noinhibit-exec", 0, 0, 130},
  872. !   {"nostdlib", 0, 0, 133},
  873. !   {"o", 1, 0, 'o'},
  874. !   {"r", 0, 0, 'r'},
  875. !   {"s", 0, 0, 's'},
  876. !   {"t", 0, 0, 't'},
  877. !   {"u", 1, 0, 'u'},
  878. !   {"x", 0, 0, 'x'},
  879. !   {"z", 0, 0, 'z'},
  880. !   {"A", 1, 0, 'A'},
  881. !   {"Bstatic", 0, 0, 129},    /* For Sun compatibility. */
  882. !   {"D", 1, 0, 'D'},
  883. !   {"M", 0, 0, 'M'},
  884. !   {"N", 0, 0, 'N'},
  885. !   {"S", 0, 0, 'S'},
  886. !   {"T", 1, 0, 'T'},
  887. !   {"Ttext", 1, 0, 'T'},
  888. !   {"Tdata", 1, 0, 132},
  889. !   {"V", 1, 0, 'V'},
  890. !   {"X", 0, 0, 'X'},
  891. !   {0, 0, 0, 0}
  892. ! };
  893. ! /* Since the Unix ld accepts -lfoo, -Lfoo, and -yfoo, we must also.
  894. !    This effectively prevents any long options from starting with
  895. !    one of these letters. */
  896. ! #define SHORTOPTS "-l:y:L:"
  897.   
  898.   /* Process the command arguments,
  899.      setting up file_table with an entry for each input file,
  900. ***************
  901. *** 1076,1082 ****
  902.        char **argv;
  903.        int argc;
  904.   {
  905. !   register int i;
  906.     register struct file_entry *p;
  907.   
  908.     number_of_files = 0;
  909. --- 1195,1201 ----
  910.        char **argv;
  911.        int argc;
  912.   {
  913. !   int optc, longind;
  914.     register struct file_entry *p;
  915.   
  916.     number_of_files = 0;
  917. ***************
  918. *** 1085,1180 ****
  919.     n_search_dirs = 0;
  920.     search_dirs = (char **) xmalloc (sizeof (char *));
  921.   
  922. !   /* First compute number_of_files so we know how long to make file_table.  */
  923. !   /* Also process most options completely.  */
  924.   
  925. !   for (i = 1; i < argc; i++)
  926. !     {
  927. !       register int code = classify_arg (argv[i]);
  928. !       if (code)
  929. !     {
  930. !       if (i + code > argc)
  931. !         fatal ("no argument following %s\n", argv[i]);
  932. !       decode_option (argv[i], argv[i+1]);
  933. !       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  934. !         number_of_files++;
  935. !       i += code - 1;
  936.       }
  937. -       else
  938. -     number_of_files++;
  939.       }
  940.   
  941.     if (!number_of_files)
  942. !     fatal ("no input files", 0);
  943.   
  944.     p = file_table
  945.       = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  946.     bzero (p, number_of_files * sizeof (struct file_entry));
  947.   
  948. !   /* Now scan again and fill in file_table.  */
  949. !   /* All options except -A and -l are ignored here.  */
  950.   
  951. !   for (i = 1; i < argc; i++)
  952. !     {
  953. !       register int code = classify_arg (argv[i]);
  954. !       if (code)
  955. !     {
  956. !       char *string;
  957. !       if (code == 2)
  958. !         string = argv[i+1];
  959. !       else
  960. !         string = &argv[i][2];
  961. !       if (argv[i][1] == 'A')
  962. !         {
  963. !           if (p != file_table)
  964. !         fatal ("-A specified before an input file other than the first");
  965. !           p->filename = string;
  966. !           p->local_sym_name = string;
  967. !           p->just_syms_flag = 1;
  968. !           p++;
  969. !         }
  970. !       if (argv[i][1] == 'l')
  971. !         {
  972. !           p->filename = concat ("lib", string, ".a");
  973. !           p->local_sym_name = concat ("-l", string, "");
  974. !           p->search_dirs_flag = 1;
  975. !           p++;
  976. !         }
  977. !       i += code - 1;
  978. !     }
  979. !       else
  980.       {
  981. !       p->filename = argv[i];
  982. !       p->local_sym_name = argv[i];
  983.         p++;
  984.       }
  985.       }
  986.   
  987.     /* Now check some option settings for consistency.  */
  988.   
  989. ! #ifdef NMAGIC
  990. !   if ((magic == ZMAGIC || magic == NMAGIC)
  991. ! #else
  992. !   if ((magic == ZMAGIC)
  993. ! #endif
  994.         && (text_start - text_start_alignment) & (page_size - 1))
  995. !     fatal ("-T argument not multiple of page size, with sharable output", 0);
  996.   
  997.     /* Append the standard search directories to the user-specified ones.  */
  998. !   {
  999. !     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1000. !     n_search_dirs += n;
  1001. !     search_dirs
  1002. !       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1003. !     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1004. !        n * sizeof (char *));
  1005. !   }
  1006.   }
  1007.   
  1008.   
  1009. --- 1204,1437 ----
  1010.     n_search_dirs = 0;
  1011.     search_dirs = (char **) xmalloc (sizeof (char *));
  1012.   
  1013. !   /* First compute number_of_files so we know how long to make file_table.
  1014. !      Also process most options completely.  */
  1015.   
  1016. !   while ((optc = getopt_long_only (argc, argv, SHORTOPTS, longopts, &longind))
  1017. !      != EOF)
  1018. !     {
  1019. !       if (optc == 0)
  1020. !     optc = longopts[longind].val;
  1021. !       switch (optc)
  1022. !     {
  1023. !     case '?':
  1024. !       usage (0, 0);
  1025. !       break;
  1026. !     case 1:
  1027. !       /* Non-option argument. */
  1028. !       number_of_files++;
  1029. !       break;
  1030. !     case 'd':
  1031. !       force_common_definition = 1;
  1032. !       break;
  1033. !     case 'e':
  1034. !       entry_symbol = getsym (optarg);
  1035. !       if (!entry_symbol->defined && !entry_symbol->referenced)
  1036. !         undefined_global_sym_count++;
  1037. !       entry_symbol->referenced = 1;
  1038. !       add_cmdline_ref (entry_symbol);
  1039. !       break;
  1040. !     case 'l':
  1041. !       number_of_files++;
  1042. !       break;
  1043. !     case 'n':
  1044. !       if (output_style && output_style != OUTPUT_READONLY_TEXT)
  1045. !         fatal ("illegal combination of -n with -N, -r, or -z", (char *) 0);
  1046. !       output_style = OUTPUT_READONLY_TEXT;
  1047. !       break;
  1048. !     case 130:        /* -noinhibit-exec */
  1049. !       force_executable = 1;
  1050. !       break;
  1051. !     case 133:        /* -nostdlib */
  1052. !       no_standard_dirs = 1;
  1053. !       break;
  1054. !     case 'o':
  1055. !       output_filename = optarg;
  1056. !       break;
  1057. !     case 'r':
  1058. !       if (output_style && output_style != OUTPUT_RELOCATABLE)
  1059. !         fatal ("illegal combination of -r with -N, -n, or -z", (char *) 0);
  1060. !       output_style = OUTPUT_RELOCATABLE;
  1061. !       text_start = 0;
  1062. !       break;
  1063. !     case 's':
  1064. !       strip_symbols = STRIP_ALL;
  1065. !       break;
  1066. !     case 't':
  1067. !       trace_files = 1;
  1068. !       break;
  1069. !     case 'u':
  1070. !       {
  1071. !         register symbol *sp = getsym (optarg);
  1072. !         if (!sp->defined && !sp->referenced)
  1073. !           undefined_global_sym_count++;
  1074. !         sp->referenced = 1;
  1075. !         add_cmdline_ref (sp);
  1076. !       }
  1077. !       break;
  1078. !     case 'x':
  1079. !       discard_locals = DISCARD_ALL;
  1080. !       break;
  1081. !       
  1082. !     case 'y':
  1083. !       {
  1084. !         register symbol *sp = getsym (optarg);
  1085. !         sp->trace = 1;
  1086. !       }
  1087. !       break;
  1088. !     case 'z':
  1089. !       if (output_style && output_style != OUTPUT_DEMAND_PAGED)
  1090. !         fatal ("illegal combination of -z with -N, -n, or -r", (char *) 0);
  1091. !       output_style = OUTPUT_DEMAND_PAGED;
  1092. !       break;
  1093. !     case 'A':
  1094. !       number_of_files++;
  1095. !       break;
  1096. !     case 129:        /* -Bstatic. */
  1097. !       /* Ignore. */
  1098. !       break;
  1099. !     case 'D':
  1100. !       specified_data_size = parse (optarg, "%x", "invalid argument to -D");
  1101. !       break;
  1102. !     case 'L':
  1103. !       n_search_dirs++;
  1104. !       search_dirs = (char **)
  1105. !         xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1106. !       search_dirs[n_search_dirs - 1] = optarg;
  1107. !       break;
  1108. !       
  1109. !     case 'M':
  1110. !       write_map = 1;
  1111. !       break;
  1112. !     case 'N':
  1113. !       if (output_style && output_style != OUTPUT_WRITABLE_TEXT)
  1114. !         fatal ("illegal combination of -N with -n, -r, or -z", (char *) 0);
  1115. !       output_style = OUTPUT_WRITABLE_TEXT;
  1116. !       break;
  1117. !     case 'S':
  1118. !       strip_symbols = STRIP_DEBUGGER;
  1119. !       break;
  1120. !     case 'T':
  1121. !       text_start = parse (optarg, "%x", "invalid argument to -Ttext");
  1122. !       T_flag_specified = 1;
  1123. !       break;
  1124. !     case 132:        /* -Tdata addr */
  1125. !       data_start = parse (optarg, "%x", "invalid argument to -Tdata");
  1126. !       Tdata_flag_specified = 1;
  1127. !       break;
  1128. !     case 'V':
  1129. !       {
  1130. !         struct string_list_element *new
  1131. !           = (struct string_list_element *)
  1132. !         xmalloc (sizeof (struct string_list_element));
  1133. !         
  1134. !         new->str = optarg;
  1135. !         new->next = set_element_prefixes;
  1136. !         set_element_prefixes = new;
  1137. !       }
  1138. !       break;
  1139. !     case 'X':
  1140. !       discard_locals = DISCARD_L;
  1141. !       break;
  1142.       }
  1143.       }
  1144.   
  1145.     if (!number_of_files)
  1146. !     usage ("no input files", 0);
  1147.   
  1148.     p = file_table
  1149.       = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1150.     bzero (p, number_of_files * sizeof (struct file_entry));
  1151.   
  1152. !   /* Now scan again and fill in file_table.
  1153. !      All options except -A and -l are ignored here.  */
  1154.   
  1155. !   optind = 0;            /* Reset getopt. */
  1156. !   while ((optc = getopt_long_only (argc, argv, SHORTOPTS, longopts, &longind))
  1157. !      != EOF)
  1158. !     {
  1159. !       if (optc == 0)
  1160. !     optc = longopts[longind].val;
  1161. !       switch (optc)
  1162.       {
  1163. !     case 1:
  1164. !       /* Non-option argument. */
  1165. !       p->filename = optarg;
  1166. !       p->local_sym_name = optarg;
  1167. !       p++;
  1168. !       break;
  1169. !     case 'A':
  1170. !       if (p != file_table)
  1171. !         usage ("-A specified before an input file other than the first");
  1172. !       p->filename = optarg;
  1173. !       p->local_sym_name = optarg;
  1174. !       p->just_syms_flag = 1;
  1175. !       p++;
  1176. !       break;
  1177. !     case 'l':
  1178. !       p->filename = concat ("lib", optarg, ".a");
  1179. !       p->local_sym_name = concat ("-l", optarg, "");
  1180. !       p->search_dirs_flag = 1;
  1181.         p++;
  1182. +       break;
  1183.       }
  1184.       }
  1185.   
  1186. +   if (!output_file_type)
  1187. +     output_file_type = DEFAULT_OUTPUT_FILE_TYPE;
  1188. +   if (!output_style)
  1189. +     output_style = DEFAULT_OUTPUT_STYLE;
  1190. + #if 0
  1191. +   /* THIS CONSISTENCY CHECK BELONGS SOMEWHERE ELSE.  */
  1192.     /* Now check some option settings for consistency.  */
  1193.   
  1194. !   if ((output_style == OUTPUT_READONLY_TEXT || output_style == OUTPUT_DEMAND_PAGED)
  1195.         && (text_start - text_start_alignment) & (page_size - 1))
  1196. !     usage ("-T argument not multiple of page size, with sharable output", 0);
  1197. ! #endif
  1198.   
  1199.     /* Append the standard search directories to the user-specified ones.  */
  1200. !   if (!no_standard_dirs)
  1201. !     {
  1202. !       int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1203. !       n_search_dirs += n;
  1204. !       search_dirs
  1205. !     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1206. !       bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1207. !          n * sizeof (char *));
  1208. !     }
  1209.   }
  1210.   
  1211.   
  1212. ***************
  1213. *** 1192,1198 ****
  1214.     if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1215.       {
  1216.         int diff = ptr - cmdline_references;
  1217. !       
  1218.         cl_refs_allocated *= 2;
  1219.         cmdline_references = (struct glosym **)
  1220.       xrealloc (cmdline_references,
  1221. --- 1449,1455 ----
  1222.     if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1223.       {
  1224.         int diff = ptr - cmdline_references;
  1225.         cl_refs_allocated *= 2;
  1226.         cmdline_references = (struct glosym **)
  1227.       xrealloc (cmdline_references,
  1228. ***************
  1229. *** 1199,1209 ****
  1230.            cl_refs_allocated * sizeof (struct glosym *));
  1231.         ptr = cmdline_references + diff;
  1232.       }
  1233. !   
  1234.     *ptr++ = sp;
  1235.     *ptr = (struct glosym *) 0;
  1236.   }
  1237. !     
  1238.   int
  1239.   set_element_prefixed_p (name)
  1240.        char *name;
  1241. --- 1456,1466 ----
  1242.            cl_refs_allocated * sizeof (struct glosym *));
  1243.         ptr = cmdline_references + diff;
  1244.       }
  1245.     *ptr++ = sp;
  1246.     *ptr = (struct glosym *) 0;
  1247.   }
  1248.   int
  1249.   set_element_prefixed_p (name)
  1250.        char *name;
  1251. ***************
  1252. *** 1221,1384 ****
  1253.       }
  1254.     return 0;
  1255.   }
  1256. - int parse ();
  1257. - /* Record an option and arrange to act on it later.
  1258. -    ARG should be the following command argument,
  1259. -    which may or may not be used by this option.
  1260. -    The `l' and `A' options are ignored here since they actually
  1261. -    specify input files.  */
  1262. - void
  1263. - decode_option (swt, arg)
  1264. -      register char *swt, *arg;
  1265. - {
  1266. -   /* We get Bstatic from gcc on suns.  */
  1267. -   if (! strcmp (swt + 1, "Bstatic"))
  1268. -     return;
  1269. -   if (! strcmp (swt + 1, "Ttext"))
  1270. -     {
  1271. -       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1272. -       T_flag_specified = 1;
  1273. -       return;
  1274. -     }
  1275. -   if (! strcmp (swt + 1, "Tdata"))
  1276. -     {
  1277. -       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1278. -       Tdata_flag_specified = 1;
  1279. -       return;
  1280. -     }
  1281. -   if (! strcmp (swt + 1, "noinhibit-exec"))
  1282. -     {
  1283. -       force_executable = 1;
  1284. -       return;
  1285. -     }
  1286. -   if (swt[2] != 0)
  1287. -     arg = &swt[2];
  1288. -   switch (swt[1])
  1289. -     {
  1290. -     case 'A':
  1291. -       return;
  1292. -     case 'D':
  1293. -       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1294. -       return;
  1295. -     case 'd':
  1296. -       force_common_definition = 1;
  1297. -       return;
  1298. -     case 'e':
  1299. -       entry_symbol = getsym (arg);
  1300. -       if (!entry_symbol->defined && !entry_symbol->referenced)
  1301. -     undefined_global_sym_count++;
  1302. -       entry_symbol->referenced = 1;
  1303. -       add_cmdline_ref (entry_symbol);
  1304. -       return;
  1305. -     case 'l':
  1306. -       return;
  1307. -     case 'L':
  1308. -       n_search_dirs++;
  1309. -       search_dirs
  1310. -     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1311. -       search_dirs[n_search_dirs - 1] = arg;
  1312. -       return;
  1313. -     case 'M':
  1314. -       write_map = 1;
  1315. -       return;
  1316. -     case 'N':
  1317. -       magic = OMAGIC;
  1318. -       return;
  1319. - #ifdef NMAGIC
  1320. -     case 'n':
  1321. -       magic = NMAGIC;
  1322. -       return;
  1323. - #endif
  1324. -     case 'o':
  1325. -       output_filename = arg;
  1326. -       return;
  1327. -     case 'r':
  1328. -       relocatable_output = 1;
  1329. -       magic = OMAGIC;
  1330. -       text_start = 0;
  1331. -       return;
  1332. -     case 'S':
  1333. -       strip_symbols = STRIP_DEBUGGER;
  1334. -       return;
  1335. -     case 's':
  1336. -       strip_symbols = STRIP_ALL;
  1337. -       return;
  1338. -     case 'T':
  1339. -       text_start = parse (arg, "%x", "invalid argument to -T");
  1340. -       T_flag_specified = 1;
  1341. -       return;
  1342. -     case 't':
  1343. -       trace_files = 1;
  1344. -       return;
  1345. -     case 'u':
  1346. -       {
  1347. -     register symbol *sp = getsym (arg);
  1348. -     if (!sp->defined && !sp->referenced)
  1349. -       undefined_global_sym_count++;
  1350. -     sp->referenced = 1;
  1351. -     add_cmdline_ref (sp);
  1352. -       }
  1353. -       return;
  1354. -     case 'V':
  1355. -       {
  1356. -     struct string_list_element *new
  1357. -       = (struct string_list_element *)
  1358. -         xmalloc (sizeof (struct string_list_element));
  1359. -     new->str = arg;
  1360. -     new->next = set_element_prefixes;
  1361. -     set_element_prefixes = new;
  1362. -     return;
  1363. -       }
  1364. -     case 'X':
  1365. -       discard_locals = DISCARD_L;
  1366. -       return;
  1367. -     case 'x':
  1368. -       discard_locals = DISCARD_ALL;
  1369. -       return;
  1370. -     case 'y':
  1371. -       {
  1372. -     register symbol *sp = getsym (&swt[2]);
  1373. -     sp->trace = 1;
  1374. -       }
  1375. -       return;
  1376. -     case 'z':
  1377. -       magic = ZMAGIC;
  1378. -       return;
  1379. -     default:
  1380. -       fatal ("invalid command option `%s'", swt);
  1381. -     }
  1382. - }
  1383.   
  1384. ! /** Convenient functions for operating on one or all files being */
  1385. !  /** loaded.  */
  1386.   void print_file_name ();
  1387.   
  1388.   /* Call FUNCTION on each input file entry.
  1389. --- 1478,1486 ----
  1390.       }
  1391.     return 0;
  1392.   }
  1393.   
  1394. ! /* Convenient functions for operating on one or all files being
  1395. !    loaded.  */
  1396.   void print_file_name ();
  1397.   
  1398.   /* Call FUNCTION on each input file entry.
  1399. ***************
  1400. *** 1494,1502 ****
  1401.   
  1402.     if (input_file) file_close ();
  1403.   
  1404. !   if (entry->search_dirs_flag)
  1405.       {
  1406. -       register char **p = search_dirs;
  1407.         int i;
  1408.   
  1409.         for (i = 0; i < n_search_dirs; i++)
  1410. --- 1596,1603 ----
  1411.   
  1412.     if (input_file) file_close ();
  1413.   
  1414. !   if (entry->search_dirs_flag && n_search_dirs)
  1415.       {
  1416.         int i;
  1417.   
  1418.         for (i = 0; i < n_search_dirs; i++)
  1419. ***************
  1420. *** 1580,1588 ****
  1421.   
  1422.   /* Medium-level input routines for rel files.  */
  1423.   
  1424. ! /* Read a file's header into the proper place in the file_entry.
  1425.      DESC is the descriptor on which the file is open.
  1426. !    ENTRY is the file's entry.  */
  1427.   
  1428.   void
  1429.   read_header (desc, entry)
  1430. --- 1681,1971 ----
  1431.   
  1432.   /* Medium-level input routines for rel files.  */
  1433.   
  1434. ! /* Determine whether the given ENTRY is an archive, a BSD a.out file,
  1435. !    a Mach-O file, or whatever.  DESC is the descriptor on which the
  1436. !    file is open.  */
  1437. ! void
  1438. ! deduce_file_type(desc, entry)
  1439. !      int desc;
  1440. !      struct file_entry *entry;
  1441. ! {
  1442. !   int len;
  1443. !   {
  1444. !     char magic[SARMAG];
  1445. !     
  1446. !     lseek (desc, entry->starting_offset, 0);
  1447. !     len = read (desc, magic, SARMAG);
  1448. !     if (len == SARMAG && !strncmp(magic, ARMAG, SARMAG))
  1449. !       {
  1450. !     entry->file_type = IS_ARCHIVE;
  1451. !     return;
  1452. !       }
  1453. !   }
  1454. ! #ifdef A_OUT
  1455. !   {
  1456. !     struct exec hdr;
  1457. !     lseek (desc, entry->starting_offset, 0);
  1458. ! #ifdef COFF_ENCAPSULATE
  1459. !     if (entry->just_syms_flag)
  1460. !       /* Since a file given with -A will have a coff header, unlike normal
  1461. !     input files, we need to skip over it.  */
  1462. !       lseek (desc, sizeof (coffheader), SEEK_CUR);
  1463. ! #endif
  1464. !     len = read (desc, (char *) &hdr, sizeof (struct exec));
  1465. !     if (len == sizeof (struct exec) && !N_BADMAG (hdr))
  1466. !       {
  1467. !     entry->file_type = IS_A_OUT;
  1468. !     return;
  1469. !       }
  1470. !   }
  1471. ! #endif
  1472. ! #ifdef MACH_O
  1473. !   {
  1474. !     struct mach_header hdr;
  1475. !     lseek (desc, entry->starting_offset, 0);
  1476. !     len = read (desc, (char *) &hdr, sizeof (struct mach_header));
  1477. !     if (len == sizeof (struct mach_header) && hdr.magic == MH_MAGIC)
  1478. !       {
  1479. !     entry->file_type = IS_MACH_O;
  1480. !     return;
  1481. !       }
  1482. !   }
  1483. ! #endif
  1484. !   fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1485. ! }
  1486. ! #ifdef A_OUT
  1487. ! /* Read an a.out file's header and set up the fields of
  1488. !    the ENTRY accordingly.  DESC is the descriptor on which
  1489. !    the file is open.  */
  1490. ! void
  1491. ! read_a_out_header (desc, entry)
  1492. !      int desc;
  1493. !      struct file_entry *entry;
  1494. ! {
  1495. !   register int len;
  1496. !   struct exec hdr;
  1497. !   struct stat st;
  1498. !   lseek (desc, entry->starting_offset, 0);
  1499. ! #ifdef COFF_ENCAPSULATE
  1500. !   if (entry->just_syms_flag)
  1501. !     /* Since a file given with -A will have a coff header, unlike normal
  1502. !        input files, we need to skip over it.  */
  1503. !     lseek (desc, sizeof (coffheader), SEEK_CUR);
  1504. ! #endif
  1505. !   read (desc, (char *) &hdr, sizeof (struct exec));
  1506. ! #ifdef READ_HEADER_HOOK
  1507. !   READ_HEADER_HOOK(hdr.a_machtype);
  1508. ! #endif
  1509. !   if (entry->just_syms_flag)
  1510. !     entry->orig_text_address = N_TXTADDR(hdr);
  1511. !   else
  1512. !     entry->orig_text_address = 0;
  1513. !   entry->text_size = hdr.a_text;
  1514. !   entry->text_offset = N_TXTOFF(hdr);
  1515. !   entry->text_reloc_size = hdr.a_trsize;
  1516. ! #ifdef N_TRELOFF
  1517. !   entry->text_reloc_offset = N_TRELOFF(hdr);
  1518. ! #else
  1519. ! #ifdef N_DATOFF
  1520. !   entry->text_reloc_offset = N_DATOFF(hdr) + hdr.a_data;
  1521. ! #else
  1522. !   entry->text_reloc_offset = N_TXTOFF(hdr) + hdr.a_text + hdr.a_data;
  1523. ! #endif
  1524. ! #endif
  1525. !   if (entry->just_syms_flag)
  1526. !     entry->orig_data_address = N_DATADDR(hdr);
  1527. !   else
  1528. !     entry->orig_data_address = entry->text_size;
  1529. !   entry->data_size = hdr.a_data;
  1530. ! #ifdef N_DATOFF
  1531. !   entry->data_offset = N_DATOFF(hdr);
  1532. ! #else
  1533. !   entry->data_offset = N_TXTOFF(hdr) + hdr.a_text;
  1534. ! #endif
  1535. !   entry->data_reloc_size = hdr.a_drsize;
  1536. ! #ifdef N_DRELOFF
  1537. !   entry->data_reloc_offset = N_DRELOFF(hdr);
  1538. ! #else
  1539. !   entry->data_reloc_offset = entry->text_reloc_offset + entry->text_reloc_size;
  1540. ! #endif
  1541. ! #ifdef N_BSSADDR
  1542. !   if (entry->just_syms_flag)
  1543. !     entry->orig_bss_address = N_BSSADDR(hdr);
  1544. !   else
  1545. ! #endif
  1546. !   entry->orig_bss_address = entry->orig_data_address + entry->data_size;
  1547. !   entry->bss_size = hdr.a_bss;
  1548. !   entry->syms_size = hdr.a_syms;
  1549. !   entry->syms_offset = N_SYMOFF(hdr);
  1550. !   entry->strs_offset = N_STROFF(hdr);
  1551. !   lseek(desc, entry->starting_offset + entry->strs_offset, 0);
  1552. !   if (read(desc, (char *) &entry->strs_size, sizeof (unsigned long int))
  1553. !       != sizeof (unsigned long int))
  1554. !     fatal_with_file ("failure reading string table size of ", entry);
  1555. !   if (!entry->superfile)
  1556. !     {
  1557. !       fstat(desc, &st);
  1558. !       if (st.st_size > entry->strs_offset + entry->strs_size)
  1559. !     {
  1560. !       entry->symseg_size = st.st_size - (entry->strs_offset + entry->strs_size);
  1561. !       entry->symseg_offset = entry->strs_offset + entry->strs_size;
  1562. !     }
  1563. !     }
  1564. !   else
  1565. !     if (entry->total_size > entry->strs_offset + entry->strs_size)
  1566. !       {
  1567. !     entry->symseg_size = entry->total_size - (entry->strs_offset + entry->strs_size);
  1568. !     entry->symseg_offset = entry->strs_offset + entry->strs_size;
  1569. !       }
  1570. ! }
  1571. ! #endif
  1572. ! #ifdef MACH_O
  1573. ! /* Read a Mach-O file's header.  DESC is the descriptor on which the
  1574. !    file is open, and ENTRY is the file's entry.  */
  1575. ! void
  1576. ! read_mach_o_header (desc, entry)
  1577. !      int desc;
  1578. !      struct file_entry *entry;
  1579. ! {
  1580. !   struct mach_header mach_header;
  1581. !   char *hdrbuf;
  1582. !   struct load_command *load_command;
  1583. !   struct segment_command *segment_command;
  1584. !   struct section *section;
  1585. !   struct symtab_command *symtab_command;
  1586. ! #ifdef LC_SYMSEG
  1587. !   struct symseg_command *symseg_command;
  1588. ! #endif;
  1589. !   int ordinal;
  1590. !   int symtab_seen, symseg_seen;
  1591. !   int len, cmd, seg;
  1592. !   entry->text_ordinal = entry->data_ordinal = entry->bss_ordinal = 0;
  1593. !   symtab_seen = symseg_seen = 0;
  1594. !   ordinal = 1;
  1595. !   lseek (desc, entry->starting_offset, 0);
  1596. !   len = read (desc, (char *) &mach_header, sizeof (struct mach_header));
  1597. !   if (len != sizeof (struct mach_header))
  1598. !     fatal_with_file ("failure reading Mach-O header of ", entry);
  1599. !   if (mach_header.filetype != MH_OBJECT && mach_header.filetype != MH_EXECUTE)
  1600. !     fatal_with_file ("unsupported Mach-O file type (not MH_OBJECT or MH_EXECUTE) in ", entry);
  1601. !   hdrbuf = xmalloc (mach_header.sizeofcmds);
  1602. !   len = read (desc, hdrbuf, mach_header.sizeofcmds);
  1603. !   if (len != mach_header.sizeofcmds)
  1604. !     fatal_with_file ("failure reading Mach-O load commands of ", entry);
  1605. !   load_command = (struct load_command *) hdrbuf;
  1606. !   for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
  1607. !     {
  1608. !       switch (load_command->cmd)
  1609. !     {
  1610. !     case LC_SEGMENT:
  1611. !       segment_command = (struct segment_command *) load_command;
  1612. !       section = (struct section *) ((char *) (segment_command + 1));
  1613. !       for (seg = 0; seg < segment_command->nsects; ++seg, ++section, ++ordinal)
  1614. !         {
  1615. !           if (!strncmp(SECT_TEXT, section->sectname, sizeof section->sectname))
  1616. !         if (entry->text_ordinal)
  1617. !           fatal_with_file ("more than one __text section in ", entry);
  1618. !         else
  1619. !           {
  1620. !             entry->text_ordinal = ordinal;
  1621. !             entry->orig_text_address = section->addr;
  1622. !             entry->text_size = section->size;
  1623. !             entry->text_offset = section->offset;
  1624. !             entry->text_reloc_size = section->nreloc * sizeof (struct relocation_info);
  1625. !             entry->text_reloc_offset = section->reloff;
  1626. !           }
  1627. !           else if (!strncmp(SECT_DATA, section->sectname, sizeof section->sectname))
  1628. !         if (entry->data_ordinal)
  1629. !           fatal_with_file ("more than one __data section in ", entry);
  1630. !         else
  1631. !           {
  1632. !             entry->data_ordinal = ordinal;
  1633. !             entry->orig_data_address = section->addr;
  1634. !             entry->data_size = section->size;
  1635. !             entry->data_offset = section->offset;
  1636. !             entry->data_reloc_size = section->nreloc * sizeof (struct relocation_info);
  1637. !             entry->data_reloc_offset = section->reloff;
  1638. !           }
  1639. !           else if (!strncmp(SECT_BSS, section->sectname, sizeof section->sectname))
  1640. !         if (entry->bss_ordinal)
  1641. !           fatal_with_file ("more than one __bss section in ", entry);
  1642. !         else
  1643. !           {
  1644. !             entry->bss_ordinal = ordinal;
  1645. !             entry->orig_bss_address = section->addr;
  1646. !             entry->bss_size = section->size;
  1647. !           }
  1648. !           else
  1649. !         if (section->size != 0)
  1650. !           fprintf (stderr, "%s: warning: unknown section `%.*s' in %s\n",
  1651. !                progname, sizeof section->sectname, section->sectname,
  1652. !                entry->filename);
  1653. !         }
  1654. !       break;
  1655. !     case LC_SYMTAB:
  1656. !       if (symtab_seen)
  1657. !           fatal_with_file ("more than one LC_SYMTAB in ", entry);
  1658. !       else
  1659. !         {
  1660. !           symtab_seen = 1;
  1661. !           symtab_command = (struct symtab_command *) load_command;
  1662. !           entry->syms_size = symtab_command->nsyms * sizeof (struct nlist);
  1663. !           entry->syms_offset = symtab_command->symoff;
  1664. !           entry->strs_size = symtab_command->strsize;
  1665. !           entry->strs_offset = symtab_command->stroff;
  1666. !         }
  1667. !       break;
  1668. ! #ifdef LC_SYMSEG
  1669. !     case LC_SYMSEG:
  1670. !       if (symseg_seen)
  1671. !         fatal_with_file ("more than one LC_SYMSEG in ", entry);
  1672. !       else
  1673. !         {
  1674. !           symseg_seen = 1;
  1675. !           symseg_command = (struct symseg_command *) load_command;
  1676. !           entry->symseg_size = symseg_command->size;
  1677. !           entry->symseg_offset = symseg_command->offset;
  1678. !         }
  1679. !       break;
  1680. ! #endif
  1681. !     }
  1682. !       load_command = (struct load_command *)
  1683. !     ((char *) load_command + load_command->cmdsize);
  1684. !     }
  1685. !   free (hdrbuf);
  1686. !   if (!symtab_seen)
  1687. !     fprintf (stderr, "%s: no symbol table in %s\n", progname, entry->filename);
  1688. ! }
  1689. ! #endif
  1690. ! /* Read a file's header info into the proper place in the file_entry.
  1691.      DESC is the descriptor on which the file is open.
  1692. !    ENTRY is the file's entry.
  1693. !    Switch in the file_type to determine the appropriate actual
  1694. !    header reading routine to call.  */
  1695.   
  1696.   void
  1697.   read_header (desc, entry)
  1698. ***************
  1699. *** 1589,1611 ****
  1700.        int desc;
  1701.        register struct file_entry *entry;
  1702.   {
  1703. !   register int len;
  1704. !   struct exec *loc = (struct exec *) &entry->header;
  1705.   
  1706. !   lseek (desc, entry->starting_offset, 0);
  1707. !   len = read (desc, loc, sizeof (struct exec));
  1708. !   if (len != sizeof (struct exec))
  1709. !     fatal_with_file ("failure reading header of ", entry);
  1710. !   if (N_BADMAG (*loc))
  1711. !     fatal_with_file ("bad magic number in ", entry);
  1712.   
  1713.     entry->header_read_flag = 1;
  1714.   }
  1715.   
  1716.   /* Read the symbols of file ENTRY into core.
  1717. !    Assume it is already open, on descriptor DESC.
  1718. !    Also read the length of the string table, which follows the symbol table,
  1719. !    but don't read the contents of the string table.  */
  1720.   
  1721.   void
  1722.   read_entry_symbols (desc, entry)
  1723. --- 1972,2009 ----
  1724.        int desc;
  1725.        register struct file_entry *entry;
  1726.   {
  1727. !   if (!entry->file_type)
  1728. !     deduce_file_type (desc, entry);
  1729.   
  1730. !   switch (entry->file_type)
  1731. !     {
  1732. !     case IS_ARCHIVE:
  1733. !     default:
  1734. !       /* Should never happen. */
  1735. !       abort ();
  1736. ! #ifdef A_OUT
  1737. !     case IS_A_OUT:
  1738. !       read_a_out_header (desc, entry);
  1739. !       break;
  1740. ! #endif
  1741.   
  1742. + #ifdef MACH_O
  1743. +     case IS_MACH_O:
  1744. +       read_mach_o_header (desc, entry);
  1745. +       break;
  1746. + #endif
  1747. +     }
  1748.     entry->header_read_flag = 1;
  1749.   }
  1750.   
  1751. + #ifdef MACH_O
  1752. + void translate_mach_o_symbols ();
  1753. + #endif
  1754.   /* Read the symbols of file ENTRY into core.
  1755. !    Assume it is already open, on descriptor DESC.  */
  1756.   
  1757.   void
  1758.   read_entry_symbols (desc, entry)
  1759. ***************
  1760. *** 1617,1638 ****
  1761.     if (!entry->header_read_flag)
  1762.       read_header (desc, entry);
  1763.   
  1764. !   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1765.   
  1766. !   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1767. !   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1768.       fatal_with_file ("premature end of file in symbols of ", entry);
  1769.   
  1770. !   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1771. !   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1772. !     fatal_with_file ("bad string table size in ", entry);
  1773. !   entry->string_size = str_size;
  1774.   }
  1775.   
  1776.   /* Read the string table of file ENTRY into core.
  1777. !    Assume it is already open, on descriptor DESC.
  1778. !    Also record whether a GDB symbol segment follows the string table.  */
  1779.   
  1780.   void
  1781.   read_entry_strings (desc, entry)
  1782. --- 2015,2034 ----
  1783.     if (!entry->header_read_flag)
  1784.       read_header (desc, entry);
  1785.   
  1786. !   entry->symbols = (struct nlist *) xmalloc (entry->syms_size);
  1787.   
  1788. !   lseek (desc, entry->syms_offset + entry->starting_offset, 0);
  1789. !   if (entry->syms_size != read (desc, entry->symbols, entry->syms_size))
  1790.       fatal_with_file ("premature end of file in symbols of ", entry);
  1791.   
  1792. ! #ifdef MACH_O
  1793. !   if (entry->file_type == IS_MACH_O)
  1794. !     translate_mach_o_symbols (entry);
  1795. ! #endif
  1796.   }
  1797.   
  1798.   /* Read the string table of file ENTRY into core.
  1799. !    Assume it is already open, on descriptor DESC.  */
  1800.   
  1801.   void
  1802.   read_entry_strings (desc, entry)
  1803. ***************
  1804. *** 1644,1671 ****
  1805.     if (!entry->header_read_flag)
  1806.       read_header (desc, entry);
  1807.   
  1808. !   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1809. !   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1810.       fatal_with_file ("premature end of file in strings of ", entry);
  1811. -   /* While we are here, see if the file has a symbol segment at the end.
  1812. -      For a separate file, just try reading some more.
  1813. -      For a library member, compare current pos against total size.  */
  1814. -   if (entry->superfile)
  1815. -     {
  1816. -       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1817. -     return;
  1818. -     }
  1819. -   else
  1820. -     {
  1821. -       buffer = read (desc, &buffer, sizeof buffer);
  1822. -       if (buffer == 0)
  1823. -     return;
  1824. -       if (buffer != sizeof buffer)
  1825. -     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1826. -     }
  1827. -   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1828.   }
  1829.   
  1830.   /* Read in the symbols of all input files.  */
  1831. --- 2040,2048 ----
  1832.     if (!entry->header_read_flag)
  1833.       read_header (desc, entry);
  1834.   
  1835. !   lseek (desc, entry->strs_offset + entry->starting_offset, 0);
  1836. !   if (entry->strs_size != read (desc, entry->strings, entry->strs_size))
  1837.       fatal_with_file ("premature end of file in strings of ", entry);
  1838.   }
  1839.   
  1840.   /* Read in the symbols of all input files.  */
  1841. ***************
  1842. *** 1698,1730 ****
  1843.        register struct file_entry *entry;
  1844.   {
  1845.     register int desc;
  1846. -   register int len;
  1847. -   struct exec hdr;
  1848.   
  1849.     desc = file_open (entry);
  1850.   
  1851. !   len = read (desc, &hdr, sizeof hdr);
  1852. !   if (len != sizeof hdr)
  1853. !     fatal_with_file ("failure reading header of ", entry);
  1854.   
  1855. !   if (!N_BADMAG (hdr))
  1856.       {
  1857.         read_entry_symbols (desc, entry);
  1858. !       entry->strings = (char *) alloca (entry->string_size);
  1859.         read_entry_strings (desc, entry);
  1860.         enter_file_symbols (entry);
  1861.         entry->strings = 0;
  1862.       }
  1863. -   else
  1864. -     {
  1865. -       char armag[SARMAG];
  1866. -       lseek (desc, 0, 0);
  1867. -       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1868. -     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1869. -       entry->library_flag = 1;
  1870. -       search_library (desc, entry);
  1871. -     }
  1872.   
  1873.     file_close ();
  1874.   }
  1875. --- 2075,2099 ----
  1876.        register struct file_entry *entry;
  1877.   {
  1878.     register int desc;
  1879.   
  1880.     desc = file_open (entry);
  1881.   
  1882. !   if (!entry->file_type)
  1883. !     deduce_file_type (desc, entry);
  1884.   
  1885. !   if (entry->file_type == IS_ARCHIVE)
  1886. !     {
  1887. !       entry->library_flag = 1;
  1888. !       search_library (desc, entry);
  1889. !     }
  1890. !   else
  1891.       {
  1892.         read_entry_symbols (desc, entry);
  1893. !       entry->strings = (char *) alloca (entry->strs_size);
  1894.         read_entry_strings (desc, entry);
  1895.         enter_file_symbols (entry);
  1896.         entry->strings = 0;
  1897.       }
  1898.   
  1899.     file_close ();
  1900.   }
  1901. ***************
  1902. *** 1737,1744 ****
  1903.   {
  1904.     register struct nlist
  1905.       *p,
  1906. !     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1907. !   int lowest_set_vector = -1;
  1908.   
  1909.     if (trace_files) prline_file_name (entry, stderr);
  1910.   
  1911. --- 2106,2112 ----
  1912.   {
  1913.     register struct nlist
  1914.       *p,
  1915. !     *end = entry->symbols + entry->syms_size / sizeof (struct nlist);
  1916.   
  1917.     if (trace_files) prline_file_name (entry, stderr);
  1918.   
  1919. ***************
  1920. *** 1752,1758 ****
  1921.         if (SET_ELEMENT_P (p->n_type))
  1922.       {
  1923.         set_symbol_count++;
  1924. !       if (!relocatable_output)
  1925.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1926.       }
  1927.         else if (p->n_type == N_WARNING)
  1928. --- 2120,2126 ----
  1929.         if (SET_ELEMENT_P (p->n_type))
  1930.       {
  1931.         set_symbol_count++;
  1932. !       if (output_style != OUTPUT_RELOCATABLE)
  1933.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1934.       }
  1935.         else if (p->n_type == N_WARNING)
  1936. ***************
  1937. *** 1771,1777 ****
  1938.         else
  1939.           {
  1940.             symbol *sp;
  1941. !           char *sname = p->n_un.n_strx + entry->strings; 
  1942.             /* Deal with the warning symbol.  */
  1943.             enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1944.             sp = getsym (sname);
  1945. --- 2139,2145 ----
  1946.         else
  1947.           {
  1948.             symbol *sp;
  1949. !           char *sname = p->n_un.n_strx + entry->strings;
  1950.             /* Deal with the warning symbol.  */
  1951.             enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1952.             sp = getsym (sname);
  1953. ***************
  1954. *** 1862,1869 ****
  1955. --- 2230,2242 ----
  1956.         /* Indirect symbols value should be modified to point
  1957.            a symbol being equivalenced to. */
  1958.         nlist_p->n_value
  1959. + #ifndef NeXT
  1960.           = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  1961.                        + entry->strings);
  1962. + #else
  1963. +         /* NeXT also has indirection but they do it weirdly. */
  1964. +         = (unsigned int) getsym (nlist_p->n_value + entry->strings);
  1965. + #endif
  1966.         if ((symbol *) nlist_p->n_value == sp)
  1967.           {
  1968.             /* Somebody redefined a symbol to be itself.  */
  1969. ***************
  1970. *** 1909,1915 ****
  1971.     if (sp->trace)
  1972.       {
  1973.         register char *reftype;
  1974. !       switch (type & N_TYPE)
  1975.       {
  1976.       case N_UNDF:
  1977.         if (nlist_p->n_value)
  1978. --- 2282,2288 ----
  1979.     if (sp->trace)
  1980.       {
  1981.         register char *reftype;
  1982. !       switch (type & ~N_EXT)
  1983.       {
  1984.       case N_UNDF:
  1985.         if (nlist_p->n_value)
  1986. ***************
  1987. *** 1954,1964 ****
  1988.         break;
  1989.   
  1990.       case N_INDR:
  1991. !       reftype = (char *) alloca (23
  1992. !                      + strlen ((nlist_p + 1)->n_un.n_strx
  1993. !                            + entry->strings));
  1994.         sprintf (reftype, "defined equivalent to %s",
  1995. !            (nlist_p + 1)->n_un.n_strx + entry->strings);
  1996.         break;
  1997.   
  1998.   #ifdef sequent
  1999. --- 2327,2335 ----
  2000.         break;
  2001.   
  2002.       case N_INDR:
  2003. !       reftype = (char *) alloca (23 + strlen (((symbol *) nlist_p->n_value)->name));
  2004.         sprintf (reftype, "defined equivalent to %s",
  2005. !            ((symbol *) nlist_p->n_value)->name);
  2006.         break;
  2007.   
  2008.   #ifdef sequent
  2009. ***************
  2010. *** 1975,1984 ****
  2011.         reftype = "shared BSS";
  2012.         break;
  2013.   */
  2014.       default:
  2015.         reftype = "I don't know this type";
  2016.         break;
  2017. - #endif
  2018.       }
  2019.   
  2020.         fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2021. --- 2346,2356 ----
  2022.         reftype = "shared BSS";
  2023.         break;
  2024.   */
  2025. + #endif
  2026.       default:
  2027.         reftype = "I don't know this type";
  2028.         break;
  2029.       }
  2030.   
  2031.         fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2032. ***************
  2033. *** 1998,2004 ****
  2034.   {
  2035.     if (n_ptr >= entry->symbols &&
  2036.         n_ptr < (entry->symbols
  2037. !            + (entry->header.a_syms / sizeof (struct nlist))))
  2038.       return (unsigned long) entry;
  2039.     return 0;
  2040.   }
  2041. --- 2370,2376 ----
  2042.   {
  2043.     if (n_ptr >= entry->symbols &&
  2044.         n_ptr < (entry->symbols
  2045. !            + (entry->syms_size / sizeof (struct nlist))))
  2046.       return (unsigned long) entry;
  2047.     return 0;
  2048.   }
  2049. ***************
  2050. *** 2205,2211 ****
  2051.                + symdef_base[i].symbol_name_string_index,
  2052.                entry->filename);
  2053.           read_entry_symbols (desc, subentry);
  2054. !         subentry->strings = (char *) malloc (subentry->string_size);
  2055.           read_entry_strings (desc, subentry);
  2056.   
  2057.           /* Now scan the symbol table and decide whether to load.  */
  2058. --- 2577,2583 ----
  2059.                + symdef_base[i].symbol_name_string_index,
  2060.                entry->filename);
  2061.           read_entry_symbols (desc, subentry);
  2062. !         subentry->strings = xmalloc (subentry->strs_size);
  2063.           read_entry_strings (desc, subentry);
  2064.   
  2065.           /* Now scan the symbol table and decide whether to load.  */
  2066. ***************
  2067. *** 2213,2218 ****
  2068. --- 2585,2591 ----
  2069.           if (!subfile_wanted_p (subentry))
  2070.             {
  2071.               free (subentry->symbols);
  2072. +             free (subentry->strings);
  2073.               free (subentry);
  2074.             }
  2075.           else
  2076. ***************
  2077. *** 2238,2248 ****
  2078.               if (symdef_base[j].library_member_offset == offset)
  2079.                 symdef_base[j].symbol_name_string_index = -1;
  2080.                 }
  2081. -           }
  2082.   
  2083. !         /* We'll read the strings again if we need them again.  */
  2084. !         free (subentry->strings);
  2085. !         subentry->strings = 0;
  2086.             }
  2087.         }
  2088.       }
  2089. --- 2611,2621 ----
  2090.               if (symdef_base[j].library_member_offset == offset)
  2091.                 symdef_base[j].symbol_name_string_index = -1;
  2092.                 }
  2093.   
  2094. !             /* We'll read the strings again if we need them again.  */
  2095. !             free (subentry->strings);
  2096. !             subentry->strings = 0;
  2097. !           }
  2098.             }
  2099.         }
  2100.       }
  2101. ***************
  2102. *** 2258,2267 ****
  2103.        register struct file_entry *subentry;
  2104.        struct file_entry **prev_addr, *entry;
  2105.   {
  2106. -   register struct file_entry *prev = *prev_addr;
  2107.     read_entry_symbols (desc, subentry);
  2108. !   subentry->strings = (char *) alloca (subentry->string_size);
  2109.     read_entry_strings (desc, subentry);
  2110.   
  2111.     if (!subfile_wanted_p (subentry))
  2112. --- 2631,2638 ----
  2113.        register struct file_entry *subentry;
  2114.        struct file_entry **prev_addr, *entry;
  2115.   {
  2116.     read_entry_symbols (desc, subentry);
  2117. !   subentry->strings = (char *) alloca (subentry->strs_size);
  2118.     read_entry_strings (desc, subentry);
  2119.   
  2120.     if (!subfile_wanted_p (subentry))
  2121. ***************
  2122. *** 2273,2283 ****
  2123.       {
  2124.         enter_file_symbols (subentry);
  2125.   
  2126. !       if (prev)
  2127. !     prev->chain = subentry;
  2128.         else
  2129.       entry->subfiles = subentry;
  2130. !       prev = subentry;
  2131.         subentry->strings = 0; /* Since space will dissapear on return */
  2132.       }
  2133.   }
  2134. --- 2644,2654 ----
  2135.       {
  2136.         enter_file_symbols (subentry);
  2137.   
  2138. !       if (*prev_addr)
  2139. !     (*prev_addr)->chain = subentry;
  2140.         else
  2141.       entry->subfiles = subentry;
  2142. !       *prev_addr = subentry;
  2143.         subentry->strings = 0; /* Since space will dissapear on return */
  2144.       }
  2145.   }
  2146. ***************
  2147. *** 2319,2325 ****
  2148.   {
  2149.     register struct nlist *p;
  2150.     register struct nlist *end
  2151. !     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2152.   #ifdef DOLLAR_KLUDGE
  2153.     register int dollar_cond = 0;
  2154.   #endif
  2155. --- 2690,2696 ----
  2156.   {
  2157.     register struct nlist *p;
  2158.     register struct nlist *end
  2159. !     = entry->symbols + entry->syms_size / sizeof (struct nlist);
  2160.   #ifdef DOLLAR_KLUDGE
  2161.     register int dollar_cond = 0;
  2162.   #endif
  2163. ***************
  2164. *** 2333,2339 ****
  2165.        potentially want it.  */
  2166.         if (type & N_EXT
  2167.         && (type != (N_UNDF | N_EXT) || p->n_value
  2168. !           
  2169.   #ifdef DOLLAR_KLUDGE
  2170.              || name[1] == '$'
  2171.   #endif
  2172. --- 2704,2710 ----
  2173.        potentially want it.  */
  2174.         if (type & N_EXT
  2175.         && (type != (N_UNDF | N_EXT) || p->n_value
  2176.   #ifdef DOLLAR_KLUDGE
  2177.              || name[1] == '$'
  2178.   #endif
  2179. ***************
  2180. *** 2367,2372 ****
  2181. --- 2738,2745 ----
  2182.         if (!sp) continue;
  2183.   
  2184.         if ((sp->referenced && !sp->defined)
  2185. +           /* NB.  This needs to be changed so that, e.g., "int pipe;" won't import
  2186. +          pipe() from the library.  But the bug fix kingdon made was wrong.  */
  2187.             || (sp->defined && sp->max_common_size))
  2188.           {
  2189.             /* This is a symbol we are looking for.  It is either
  2190. ***************
  2191. *** 2423,2428 ****
  2192. --- 2796,2807 ----
  2193.     if (trace_files)
  2194.       fprintf (stderr, "Digesting symbol information:\n\n");
  2195.   
  2196. +   /* Initialize the text_start address; this depends on the output file formats.  */
  2197. +   initialize_text_start ();
  2198. +   text_size = text_header_size;
  2199.     /* Compute total size of sections */
  2200.   
  2201.     each_file (consider_file_section_lengths, 0);
  2202. ***************
  2203. *** 2430,2463 ****
  2204.     /* If necessary, pad text section to full page in the file.
  2205.        Include the padding in the text segment size.  */
  2206.   
  2207. ! #ifdef NMAGIC
  2208. !   if (magic == ZMAGIC || magic == NMAGIC)
  2209. ! #else
  2210. !   if (magic == ZMAGIC)
  2211. ! #endif
  2212.       {
  2213. !       int text_end = text_size + N_TXTOFF (outheader);
  2214. !       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2215.         text_size += text_pad;
  2216.       }
  2217.   
  2218. !   outheader.a_text = text_size;
  2219. ! #ifdef sequent
  2220. !   outheader.a_text += N_ADDRADJ (outheader);
  2221. ! #endif
  2222. !   /* Make the data segment address start in memory on a suitable boundary.  */
  2223.   
  2224. !   if (! Tdata_flag_specified)
  2225. !     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  2226.   
  2227.     /* Make sure bss starts out aligned as much as anyone can want.  */
  2228.   
  2229. !   data_size = (data_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2230.   
  2231.     /* Set up the set element vector */
  2232.   
  2233. !   if (!relocatable_output)
  2234.       {
  2235.         /* The set sector size is the number of set elements + a word
  2236.            for each symbol for the length word at the beginning of the
  2237. --- 2809,2836 ----
  2238.     /* If necessary, pad text section to full page in the file.
  2239.        Include the padding in the text segment size.  */
  2240.   
  2241. !   if (output_style == OUTPUT_READONLY_TEXT || output_style == OUTPUT_DEMAND_PAGED)
  2242.       {
  2243. !       text_pad = ((text_size + page_size - 1) & (- page_size)) - text_size;
  2244.         text_size += text_pad;
  2245.       }
  2246.   
  2247. !   /* Now that the text_size is known, initialize the data start address;
  2248. !      this depends on text_size as well as the output file format.  */
  2249.   
  2250. !   initialize_data_start ();
  2251.   
  2252.     /* Make sure bss starts out aligned as much as anyone can want.  */
  2253. +   {
  2254. +     int new_data_size = (data_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2255.   
  2256. !     data_pad += new_data_size - data_size;
  2257. !     data_size = new_data_size;
  2258. !   }
  2259.   
  2260.     /* Set up the set element vector */
  2261.   
  2262. !   if (output_style != OUTPUT_RELOCATABLE)
  2263.       {
  2264.         /* The set sector size is the number of set elements + a word
  2265.            for each symbol for the length word at the beginning of the
  2266. ***************
  2267. *** 2493,2499 ****
  2268.       {
  2269.         /* For each symbol */
  2270.         register struct nlist *p, *next;
  2271. !       int defs = 0, com = sp->max_common_size, erred = 0;
  2272.         struct nlist *first_definition;
  2273.         for (p = sp->refs; p; p = next)
  2274.           {
  2275. --- 2866,2872 ----
  2276.       {
  2277.         /* For each symbol */
  2278.         register struct nlist *p, *next;
  2279. !       int defs = 0, com = sp->max_common_size;
  2280.         struct nlist *first_definition;
  2281.         for (p = sp->refs; p; p = next)
  2282.           {
  2283. ***************
  2284. *** 2501,2507 ****
  2285.   
  2286.             if (SET_ELEMENT_P (type))
  2287.           {
  2288. !           if (relocatable_output)
  2289.               fatal ("internal: global ref to set element with -r");
  2290.             if (!defs++)
  2291.               {
  2292. --- 2874,2880 ----
  2293.   
  2294.             if (SET_ELEMENT_P (type))
  2295.           {
  2296. !           if (output_style == OUTPUT_RELOCATABLE)
  2297.               fatal ("internal: global ref to set element with -r");
  2298.             if (!defs++)
  2299.               {
  2300. ***************
  2301. *** 2535,2541 ****
  2302.         /* Allocate as common if defined as common and not defined for real */
  2303.         if (com && !defs)
  2304.           {
  2305. !           if (!relocatable_output || force_common_definition)
  2306.           {
  2307.             int align = sizeof (int);
  2308.   
  2309. --- 2908,2914 ----
  2310.         /* Allocate as common if defined as common and not defined for real */
  2311.         if (com && !defs)
  2312.           {
  2313. !           if (output_style != OUTPUT_RELOCATABLE || force_common_definition)
  2314.           {
  2315.             int align = sizeof (int);
  2316.   
  2317. ***************
  2318. *** 2546,2552 ****
  2319.                this size alignment is ever removed, ALIGN above
  2320.                will have to be initialized to 1 instead of
  2321.                sizeof (int).  */
  2322. !           
  2323.             com = (com + sizeof (int) - 1) & (- sizeof (int));
  2324.   
  2325.             while (!(com & align))
  2326. --- 2919,2925 ----
  2327.                this size alignment is ever removed, ALIGN above
  2328.                will have to be initialized to 1 instead of
  2329.                sizeof (int).  */
  2330.             com = (com + sizeof (int) - 1) & (- sizeof (int));
  2331.   
  2332.             while (!(com & align))
  2333. ***************
  2334. *** 2604,2629 ****
  2335.   
  2336.     bss_size = (bss_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2337.   
  2338. !   if (end_symbol)        /* These are null if -r.  */
  2339. !     {
  2340. !       etext_symbol->value = text_size + text_start;
  2341. !       edata_symbol->value = data_start + data_size;
  2342. !       end_symbol->value = data_start + data_size + bss_size;
  2343. !     }
  2344.   
  2345.     /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2346.   
  2347. !   if (specified_data_size && specified_data_size > data_size)
  2348. !     data_pad = specified_data_size - data_size;
  2349.   
  2350. !   if (magic == ZMAGIC)
  2351. !     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  2352. !                - data_size;
  2353. !   bss_size -= data_pad;
  2354. !   if (bss_size < 0) bss_size = 0;
  2355.   
  2356. !   data_size += data_pad;
  2357.   }
  2358.   
  2359.   /* Accumulate the section sizes of input file ENTRY
  2360. --- 2977,3029 ----
  2361.   
  2362.     bss_size = (bss_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2363.   
  2364. !   /* Give values to _end and friends.  */
  2365. !   {
  2366. !     int end_value = data_start + data_size + bss_size;
  2367. !     if (end_symbol)
  2368. !       end_symbol->value = end_value;
  2369. !     if (end_symbol_alt)
  2370. !       end_symbol_alt->value = end_value;
  2371. !   }
  2372. !   {
  2373. !     int etext_value = text_size + text_start;
  2374. !     if (etext_symbol)
  2375. !       etext_symbol->value = etext_value;
  2376. !     if (etext_symbol_alt)
  2377. !       etext_symbol_alt->value = etext_value;
  2378. !   }
  2379. !   {
  2380. !     int edata_value = data_start + data_size;
  2381. !     if (edata_symbol)
  2382. !       edata_symbol->value = edata_value;
  2383. !     if (edata_symbol_alt)
  2384. !       edata_symbol_alt->value = edata_value;
  2385. !   }
  2386.   
  2387.     /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2388.   
  2389. !   {
  2390. !     /* The amount of data_pad that we are computing now.  This is the
  2391. !        part which overlaps with bss.  What was computed previously
  2392. !        goes before bss.  */
  2393. !     int data_pad_additional = 0;
  2394. !     
  2395. !     if (specified_data_size && specified_data_size > data_size)
  2396. !       data_pad_additional = specified_data_size - data_size;
  2397. !     if (output_style == OUTPUT_DEMAND_PAGED)
  2398. !       data_pad_additional =
  2399. !     ((data_pad_additional + data_size + page_size - 1) & (- page_size)) - data_size;
  2400. !     bss_size -= data_pad_additional;
  2401. !     if (bss_size < 0) bss_size = 0;
  2402.   
  2403. !     data_size += data_pad_additional;
  2404.   
  2405. !     data_pad += data_pad_additional;
  2406. !   }
  2407.   }
  2408.   
  2409.   /* Accumulate the section sizes of input file ENTRY
  2410. ***************
  2411. *** 2638,2651 ****
  2412.   
  2413.     entry->text_start_address = text_size;
  2414.     /* If there were any vectors, we need to chop them off */
  2415. !   text_size += entry->header.a_text;
  2416.     entry->data_start_address = data_size;
  2417. !   data_size += entry->header.a_data;
  2418.     entry->bss_start_address = bss_size;
  2419. !   bss_size += entry->header.a_bss;
  2420.   
  2421. !   text_reloc_size += entry->header.a_trsize;
  2422. !   data_reloc_size += entry->header.a_drsize;
  2423.   }
  2424.   
  2425.   /* Determine where the sections of ENTRY go into the output file,
  2426. --- 3038,3051 ----
  2427.   
  2428.     entry->text_start_address = text_size;
  2429.     /* If there were any vectors, we need to chop them off */
  2430. !   text_size += entry->text_size;
  2431.     entry->data_start_address = data_size;
  2432. !   data_size += entry->data_size;
  2433.     entry->bss_start_address = bss_size;
  2434. !   bss_size += entry->bss_size;
  2435.   
  2436. !   text_reloc_size += entry->text_reloc_size;
  2437. !   data_reloc_size += entry->data_reloc_size;
  2438.   }
  2439.   
  2440.   /* Determine where the sections of ENTRY go into the output file,
  2441. ***************
  2442. *** 2657,2665 ****
  2443.        register struct file_entry *entry;
  2444.   {
  2445.     entry->text_start_address += text_start;
  2446. !   /* Note that `data_start' and `data_size' have not yet been
  2447. !      adjusted for `data_pad'.  If they had been, we would get the wrong
  2448. !      results here.  */
  2449.     entry->data_start_address += data_start;
  2450.     entry->bss_start_address += data_start + data_size;
  2451.   
  2452. --- 3057,3066 ----
  2453.        register struct file_entry *entry;
  2454.   {
  2455.     entry->text_start_address += text_start;
  2456. !   /* Note that `data_start' and `data_size' have not yet been adjusted
  2457. !      for the portion of data_pad which overlaps with bss.  If they had
  2458. !      been, we would get the wrong results here.  */
  2459.     entry->data_start_address += data_start;
  2460.     entry->bss_start_address += data_start + data_size;
  2461.   
  2462. ***************
  2463. *** 2666,2697 ****
  2464.     {
  2465.       register struct nlist *p;
  2466.       register struct nlist *end
  2467. !       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2468.   
  2469.       for (p = entry->symbols; p < end; p++)
  2470.         {
  2471.       /* If this belongs to a section, update it by the section's start address */
  2472. !     register int type = p->n_type & N_TYPE;
  2473.   
  2474.       switch (type)
  2475.         {
  2476.         case N_TEXT:
  2477.         case N_SETT:
  2478. !         p->n_value += entry->text_start_address;
  2479.           break;
  2480.         case N_DATA:
  2481.         case N_SETV:
  2482.         case N_SETD:
  2483. !         /* A symbol whose value is in the data section
  2484. !            is present in the input file as if the data section
  2485. !            started at an address equal to the length of the file's text.  */
  2486. !         p->n_value += entry->data_start_address - entry->header.a_text;
  2487.           break;
  2488.         case N_BSS:
  2489.         case N_SETB:
  2490.           /* likewise for symbols with value in BSS.  */
  2491. !         p->n_value += entry->bss_start_address
  2492. !           - entry->header.a_text - entry->header.a_data;
  2493.           break;
  2494.         }
  2495.         }
  2496. --- 3067,3098 ----
  2497.     {
  2498.       register struct nlist *p;
  2499.       register struct nlist *end
  2500. !       = entry->symbols + entry->syms_size / sizeof (struct nlist);
  2501.   
  2502.       for (p = entry->symbols; p < end; p++)
  2503.         {
  2504.       /* If this belongs to a section, update it by the section's start address */
  2505. !     register int type = p->n_type & ~N_EXT;
  2506.   
  2507.       switch (type)
  2508.         {
  2509.         case N_TEXT:
  2510.         case N_SETT:
  2511. !         p->n_value += entry->text_start_address - entry->orig_text_address;
  2512.           break;
  2513.         case N_DATA:
  2514.         case N_SETV:
  2515.         case N_SETD:
  2516. !         /* Data segment symbol.  Subtract the address of the
  2517. !            data segment in the input file, and add the address
  2518. !            of this input file's data segment in the output file.  */
  2519. !         p->n_value +=
  2520. !           entry->data_start_address - entry->orig_data_address;
  2521.           break;
  2522.         case N_BSS:
  2523.         case N_SETB:
  2524.           /* likewise for symbols with value in BSS.  */
  2525. !         p->n_value += entry->bss_start_address - entry->orig_bss_address;
  2526.           break;
  2527.         }
  2528.         }
  2529. ***************
  2530. *** 2742,2750 ****
  2531.       fprintf (outfile, " symbols only\n", 0);
  2532.     else
  2533.       fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2534. !          entry->text_start_address, entry->header.a_text,
  2535. !          entry->data_start_address, entry->header.a_data,
  2536. !          entry->bss_start_address, entry->header.a_bss);
  2537.   }
  2538.   
  2539.   void
  2540. --- 3143,3151 ----
  2541.       fprintf (outfile, " symbols only\n", 0);
  2542.     else
  2543.       fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2544. !          entry->text_start_address, entry->text_size,
  2545. !          entry->data_start_address, entry->data_size,
  2546. !          entry->bss_start_address, entry->bss_size);
  2547.   }
  2548.   
  2549.   void
  2550. ***************
  2551. *** 2754,2762 ****
  2552.   {
  2553.     register struct nlist
  2554.       *p,
  2555. !     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2556.   
  2557. !   entry->strings = (char *) alloca (entry->string_size);
  2558.     read_entry_strings (file_open (entry), entry);
  2559.   
  2560.     fprintf (outfile, "\nLocal symbols of ");
  2561. --- 3155,3163 ----
  2562.   {
  2563.     register struct nlist
  2564.       *p,
  2565. !     *end = entry->symbols + entry->syms_size / sizeof (struct nlist);
  2566.   
  2567. !   entry->strings = (char *) alloca (entry->strs_size);
  2568.     read_entry_strings (file_open (entry), entry);
  2569.   
  2570.     fprintf (outfile, "\nLocal symbols of ");
  2571. ***************
  2572. *** 2831,2842 ****
  2573.     current->filename = next->filename;
  2574.   
  2575.     while (++(next->sym) < (entry->symbols
  2576. !               + entry->header.a_syms/sizeof (struct nlist)))
  2577.       {
  2578.         /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  2579.          * may look negative...therefore, must mask to low bits
  2580.          */
  2581. !       switch (next->sym->n_type & 0xff) 
  2582.       {
  2583.       case N_SLINE:
  2584.         if (use_data_symbols) continue;
  2585. --- 3232,3243 ----
  2586.     current->filename = next->filename;
  2587.   
  2588.     while (++(next->sym) < (entry->symbols
  2589. !               + entry->syms_size/sizeof (struct nlist)))
  2590.       {
  2591.         /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  2592.          * may look negative...therefore, must mask to low bits
  2593.          */
  2594. !       switch (next->sym->n_type & 0xff)
  2595.       {
  2596.       case N_SLINE:
  2597.         if (use_data_symbols) continue;
  2598. ***************
  2599. *** 2891,2903 ****
  2600.   
  2601.     for (tmp = entry->symbols;
  2602.          tmp < (entry->symbols
  2603. !           + entry->header.a_syms/sizeof (struct nlist));
  2604.          tmp++)
  2605.       if (tmp->n_type == (int) N_SO)
  2606.         break;
  2607.   
  2608.     if (tmp >= (entry->symbols
  2609. !           + entry->header.a_syms/sizeof (struct nlist)))
  2610.       {
  2611.         /* I believe this translates to "We lose" */
  2612.         current->filename = next->filename = entry->filename;
  2613. --- 3292,3304 ----
  2614.   
  2615.     for (tmp = entry->symbols;
  2616.          tmp < (entry->symbols
  2617. !           + entry->syms_size/sizeof (struct nlist));
  2618.          tmp++)
  2619.       if (tmp->n_type == (int) N_SO)
  2620.         break;
  2621.   
  2622.     if (tmp >= (entry->symbols
  2623. !           + entry->syms_size/sizeof (struct nlist)))
  2624.       {
  2625.         /* I believe this translates to "We lose" */
  2626.         current->filename = next->filename = entry->filename;
  2627. ***************
  2628. *** 2940,2946 ****
  2629.      which describes the current location in the implied scan through
  2630.      the debug symbols within the file which ADDRESS is within, and
  2631.      returns the source line number which corresponds to ADDRESS.  */
  2632. !   
  2633.   int
  2634.   address_to_line (address, state_pointer)
  2635.        unsigned long address;
  2636. --- 3341,3347 ----
  2637.      which describes the current location in the implied scan through
  2638.      the debug symbols within the file which ADDRESS is within, and
  2639.      returns the source line number which corresponds to ADDRESS.  */
  2640.   int
  2641.   address_to_line (address, state_pointer)
  2642.        unsigned long address;
  2643. ***************
  2644. *** 2955,2961 ****
  2645.     int use_data_symbols;
  2646.   
  2647.     if (next->sym)
  2648. !     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  2649.     else
  2650.       return current->line;
  2651.   
  2652. --- 3356,3362 ----
  2653.     int use_data_symbols;
  2654.   
  2655.     if (next->sym)
  2656. !     use_data_symbols = (next->sym->n_type & ~N_EXT) == N_DATA;
  2657.     else
  2658.       return current->line;
  2659.   
  2660. ***************
  2661. *** 3007,3013 ****
  2662.       *reloc_start = data_segment ? entry->datarel : entry->textrel,
  2663.       *reloc;
  2664.     int reloc_size
  2665. !     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  2666.          / sizeof (struct relocation_info));
  2667.     int start_of_segment
  2668.       = (data_segment ? entry->data_start_address : entry->text_start_address);
  2669. --- 3408,3414 ----
  2670.       *reloc_start = data_segment ? entry->datarel : entry->textrel,
  2671.       *reloc;
  2672.     int reloc_size
  2673. !     = ((data_segment ? entry->data_reloc_size : entry->text_reloc_size)
  2674.          / sizeof (struct relocation_info));
  2675.     int start_of_segment
  2676.       = (data_segment ? entry->data_start_address : entry->text_start_address);
  2677. ***************
  2678. *** 3014,3023 ****
  2679.     struct nlist *start_of_syms = entry->symbols;
  2680.     struct line_debug_entry *state_pointer
  2681.       = init_debug_scan (data_segment != 0, entry);
  2682. !   register struct line_debug_entry
  2683. !     *current = state_pointer,
  2684. !     *next = state_pointer + 1,
  2685. !     *source = state_pointer + 2;
  2686.     /* Assigned to generally static values; should not be written into.  */
  2687.     char *errfmt;
  2688.     /* Assigned to alloca'd values cand copied into; should be freed
  2689. --- 3415,3421 ----
  2690.     struct nlist *start_of_syms = entry->symbols;
  2691.     struct line_debug_entry *state_pointer
  2692.       = init_debug_scan (data_segment != 0, entry);
  2693. !   register struct line_debug_entry *current = state_pointer;
  2694.     /* Assigned to generally static values; should not be written into.  */
  2695.     char *errfmt;
  2696.     /* Assigned to alloca'd values cand copied into; should be freed
  2697. ***************
  2698. *** 3061,3067 ****
  2699.       {
  2700.         /* Mark as being noted by relocation warning pass.  */
  2701.         SET_BIT (nlist_bitvector, s - start_of_syms);
  2702. !       
  2703.         if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  2704.           continue;
  2705.   
  2706. --- 3459,3465 ----
  2707.       {
  2708.         /* Mark as being noted by relocation warning pass.  */
  2709.         SET_BIT (nlist_bitvector, s - start_of_syms);
  2710.         if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  2711.           continue;
  2712.   
  2713. ***************
  2714. *** 3085,3102 ****
  2715.   
  2716.         /* Mark as being noted by relocation warning pass.  */
  2717.         SET_BIT (nlist_bitvector, s - start_of_syms);
  2718. !       
  2719.         errfmt = 0;
  2720.         errmsg = g->warning;
  2721.         invalidate_line_number = 0;
  2722.       }
  2723. !       
  2724.   
  2725.         /* If errfmt == 0, errmsg has already been defined.  */
  2726.         if (errfmt != 0)
  2727.       {
  2728. !       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  2729. !       sprintf (errmsg, errfmt, g->name, data_segment ? "data" : "text");
  2730.       }
  2731.   
  2732.         address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  2733. --- 3483,3506 ----
  2734.   
  2735.         /* Mark as being noted by relocation warning pass.  */
  2736.         SET_BIT (nlist_bitvector, s - start_of_syms);
  2737.         errfmt = 0;
  2738.         errmsg = g->warning;
  2739.         invalidate_line_number = 0;
  2740.       }
  2741.   
  2742.         /* If errfmt == 0, errmsg has already been defined.  */
  2743.         if (errfmt != 0)
  2744.       {
  2745. !       char *nm;
  2746. !       if (!demangler || !(nm = (*demangler)(g->name)))
  2747. !         nm = g->name;
  2748. !       errmsg = xmalloc (strlen (errfmt) + strlen (nm) + 1);
  2749. !       sprintf (errmsg, errfmt, nm, data_segment ? "data" : "text");
  2750. !       if (nm != g->name)
  2751. !         free (nm);
  2752.       }
  2753.   
  2754.         address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  2755. ***************
  2756. *** 3103,3112 ****
  2757.                  state_pointer);
  2758.   
  2759.         if (current->line >=0)
  2760. !     fprintf (outfile, "%s:%d: %s\n", current->filename,
  2761. !          invalidate_line_number ? 0 : current->line, errmsg);
  2762. !       else
  2763. !     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  2764.   
  2765.         if (errfmt != 0)
  2766.       free (errmsg);
  2767. --- 3507,3523 ----
  2768.                  state_pointer);
  2769.   
  2770.         if (current->line >=0)
  2771. !     {
  2772. !       fprintf (outfile, "%s:%d (", current->filename,
  2773. !            invalidate_line_number ? 0 : current->line);
  2774. !       print_file_name (entry, outfile);
  2775. !       fprintf (outfile, "): %s\n", errmsg);
  2776. !     }
  2777. !       else
  2778. !     {
  2779. !       print_file_name(entry, outfile);
  2780. !       fprintf(outfile, ": %s\n", errmsg);
  2781. !     }
  2782.   
  2783.         if (errfmt != 0)
  2784.       free (errmsg);
  2785. ***************
  2786. *** 3124,3130 ****
  2787.        struct file_entry *entry;
  2788.        FILE *outfile;
  2789.   {
  2790. !   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  2791.     unsigned char *nlist_bitvector
  2792.       = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  2793.     struct line_debug_entry *text_scan, *data_scan;
  2794. --- 3535,3541 ----
  2795.        struct file_entry *entry;
  2796.        FILE *outfile;
  2797.   {
  2798. !   int number_of_syms = entry->syms_size / sizeof (struct nlist);
  2799.     unsigned char *nlist_bitvector
  2800.       = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  2801.     struct line_debug_entry *text_scan, *data_scan;
  2802. ***************
  2803. *** 3140,3146 ****
  2804.       {
  2805.         int desc;
  2806.   
  2807. !       entry->strings = (char *) alloca (entry->string_size);
  2808.         desc = file_open (entry);
  2809.         read_entry_strings (desc, entry);
  2810.       }
  2811. --- 3551,3557 ----
  2812.       {
  2813.         int desc;
  2814.   
  2815. !       entry->strings = (char *) alloca (entry->strs_size);
  2816.         desc = file_open (entry);
  2817.         read_entry_strings (desc, entry);
  2818.       }
  2819. ***************
  2820. *** 3204,3210 ****
  2821.       {
  2822.         if (g->undef_refs >= MAX_UREFS_PRINTED)
  2823.           continue;
  2824. !       
  2825.         if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  2826.           errfmt = "More undefined \"%s\" refs follow";
  2827.         else
  2828. --- 3615,3621 ----
  2829.       {
  2830.         if (g->undef_refs >= MAX_UREFS_PRINTED)
  2831.           continue;
  2832.         if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  2833.           errfmt = "More undefined \"%s\" refs follow";
  2834.         else
  2835. ***************
  2836. *** 3227,3242 ****
  2837.       }
  2838.         else
  2839.       continue;
  2840. !       
  2841.         if (line_number == -1)
  2842. !     fprintf (outfile, "%s: ", entry->filename);
  2843.         else
  2844. !     fprintf (outfile, "%s:%d: ", file_name, line_number);
  2845.   
  2846.         if (dont_allow_symbol_name)
  2847.       fprintf (outfile, "%s", errfmt);
  2848.         else
  2849. !     fprintf (outfile, errfmt, g->name);
  2850.   
  2851.         fputc ('\n', outfile);
  2852.       }
  2853. --- 3638,3670 ----
  2854.       }
  2855.         else
  2856.       continue;
  2857.         if (line_number == -1)
  2858. !     {
  2859. !       print_file_name (entry, outfile);
  2860. !       fprintf (outfile, ": ");
  2861. !     }
  2862.         else
  2863. !     {
  2864. !       fprintf (outfile, "%s:%d (", file_name, line_number);
  2865. !       print_file_name (entry, outfile);
  2866. !       fprintf (outfile, "): ");
  2867. !     }
  2868.   
  2869.         if (dont_allow_symbol_name)
  2870.       fprintf (outfile, "%s", errfmt);
  2871.         else
  2872. !     {
  2873. !       char *nm;
  2874. !       if (!demangler || !(nm = (*demangler)(g->name)))
  2875. !         fprintf (outfile, errfmt, g->name);
  2876. !       else
  2877. !         {
  2878. !           fprintf (outfile, errfmt, nm);
  2879. !           free (nm);
  2880. !         }
  2881. !     }
  2882.   
  2883.         fputc ('\n', outfile);
  2884.       }
  2885. ***************
  2886. *** 3248,3256 ****
  2887.   do_warnings (outfile)
  2888.        FILE *outfile;
  2889.   {
  2890. !   int i;
  2891. !   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  2892.     list_warning_symbols = warning_count;
  2893.     list_multiple_defs = multiple_def_count != 0;
  2894.   
  2895. --- 3676,3682 ----
  2896.   do_warnings (outfile)
  2897.        FILE *outfile;
  2898.   {
  2899. !   list_unresolved_refs = output_style != OUTPUT_RELOCATABLE && undefined_global_sym_count;
  2900.     list_warning_symbols = warning_count;
  2901.     list_multiple_defs = multiple_def_count != 0;
  2902.   
  2903. ***************
  2904. *** 3266,3326 ****
  2905.       make_executable = 0;
  2906.   }
  2907.   
  2908. ! /* Write the output file */
  2909. ! void
  2910. ! write_output ()
  2911. ! {
  2912. !   struct stat statbuf;
  2913. !   int filemode;
  2914. !   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  2915. !   if (outdesc < 0) perror_name (output_filename);
  2916.   
  2917. !   if (fstat (outdesc, &statbuf) < 0)
  2918. !     perror_name (output_filename);
  2919.   
  2920. !   filemode = statbuf.st_mode;
  2921.   
  2922. !   chmod (output_filename, filemode & ~0111);
  2923.   
  2924. !   /* Output the a.out header.  */
  2925. !   write_header ();
  2926.   
  2927. !   /* Output the text and data segments, relocating as we go.  */
  2928. !   write_text ();
  2929. !   write_data ();
  2930.   
  2931. !   /* Output the merged relocation info, if requested with `-r'.  */
  2932. !   if (relocatable_output)
  2933. !     write_rel ();
  2934.   
  2935. !   /* Output the symbol table (both globals and locals).  */
  2936. !   write_syms ();
  2937.   
  2938. !   /* Copy any GDB symbol segments from input files.  */
  2939. !   write_symsegs ();
  2940.   
  2941. !   close (outdesc);
  2942.   
  2943. !   if (chmod (output_filename, filemode | 0111) == -1)
  2944. !     perror_name (output_filename);
  2945.   }
  2946. ! void modify_location (), perform_relocation (), copy_text (), copy_data ();
  2947.   
  2948.   void
  2949. ! write_header ()
  2950.   {
  2951. -   N_SET_MAGIC (outheader, magic);
  2952.     outheader.a_text = text_size;
  2953.   #ifdef sequent
  2954.     outheader.a_text += N_ADDRADJ (outheader);
  2955.   #endif
  2956.     outheader.a_data = data_size;
  2957.     outheader.a_bss = bss_size;
  2958.     outheader.a_entry = (entry_symbol ? entry_symbol->value
  2959. !                : text_start + entry_offset);
  2960.   #ifdef COFF_ENCAPSULATE
  2961.     if (need_coff_header)
  2962.       {
  2963. --- 3692,3795 ----
  2964.       make_executable = 0;
  2965.   }
  2966.   
  2967. ! #ifdef A_OUT
  2968.   
  2969. ! /* Stuff pertaining to creating a.out files. */
  2970.   
  2971. ! /* The a.out header. */
  2972.   
  2973. ! struct exec outheader;
  2974.   
  2975. ! #ifdef COFF_ENCAPSULATE
  2976. ! int need_coff_header;
  2977. ! struct coffheader coffheader;
  2978. ! #endif
  2979.   
  2980. ! /* Compute text_start and text_header_size for an a.out file.  */
  2981.   
  2982. ! void
  2983. ! initialize_a_out_text_start ()
  2984. ! {
  2985. !   int magic;
  2986.   
  2987. !   switch (output_style)
  2988. !     {
  2989. !     case OUTPUT_RELOCATABLE:
  2990. !     case OUTPUT_WRITABLE_TEXT:
  2991. !       magic = OMAGIC;
  2992. !       break;
  2993. !     case OUTPUT_READONLY_TEXT:
  2994. ! #ifdef NMAGIC
  2995. !       magic = NMAGIC;
  2996. !       break;
  2997. ! #endif
  2998. !     case OUTPUT_DEMAND_PAGED:
  2999. !       magic = ZMAGIC;
  3000. !       break;
  3001. !     default:
  3002. !       fatal ("unknown output style found (bug in ld)", (char *) 0);
  3003. !       break;
  3004. !     }
  3005.   
  3006. !   /* Determine whether to count the header as part of
  3007. !      the text size, and initialize the text size accordingly.
  3008. !      This depends on the kind of system and on the output format selected.  */
  3009. !   N_SET_MAGIC (outheader, magic);
  3010. ! #ifdef INITIALIZE_HEADER
  3011. !   INITIALIZE_HEADER;
  3012. ! #endif
  3013.   
  3014. !   text_header_size = sizeof (struct exec);
  3015. ! #ifdef COFF_ENCAPSULATE
  3016. !   /* Don't write the coff header for the output of ld -A (since
  3017. !      it is not executable by the kernel anyway).  */
  3018. !   if (output_style != OUTPUT_RELOCATABLE && !file_table[0].just_syms_flag)
  3019. !     {
  3020. !       need_coff_header = 1;
  3021. !       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  3022. !       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  3023. !       text_header_size += sizeof (struct coffheader);
  3024. !     }
  3025. ! #endif
  3026. !   if (text_header_size <= N_TXTOFF (outheader))
  3027. !     text_header_size = 0;
  3028. !   else
  3029. !     text_header_size -= N_TXTOFF (outheader);
  3030. ! #ifdef _N_BASEADDR
  3031. !   /* SunOS 4.1 N_TXTADDR depends on the value of outheader.a_entry. */
  3032. !   outheader.a_entry = N_PAGSIZ(outheader);
  3033. ! #endif
  3034.   
  3035. !   if (!T_flag_specified && output_style != OUTPUT_RELOCATABLE)
  3036. !     text_start = N_TXTADDR (outheader);
  3037.   }
  3038. ! /* Compute data_start once text_size is known. */
  3039.   
  3040.   void
  3041. ! initialize_a_out_data_start ()
  3042.   {
  3043.     outheader.a_text = text_size;
  3044.   #ifdef sequent
  3045.     outheader.a_text += N_ADDRADJ (outheader);
  3046. +   if (entry_symbol == 0)
  3047. +     entry_symbol = getsym ("start");
  3048.   #endif
  3049. +   if (! Tdata_flag_specified)
  3050. +     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  3051. + }
  3052. + /* Compute offsets of various pieces of the a.out output file.  */
  3053. + void
  3054. + compute_a_out_section_offsets ()
  3055. + {
  3056.     outheader.a_data = data_size;
  3057.     outheader.a_bss = bss_size;
  3058.     outheader.a_entry = (entry_symbol ? entry_symbol->value
  3059. !                : text_start + text_header_size);
  3060.   #ifdef COFF_ENCAPSULATE
  3061.     if (need_coff_header)
  3062.       {
  3063. ***************
  3064. *** 3384,3393 ****
  3065.       }
  3066.   #endif
  3067.   
  3068. - #ifdef INITIALIZE_HEADER
  3069. -   INITIALIZE_HEADER;
  3070. - #endif
  3071.     if (strip_symbols == STRIP_ALL)
  3072.       nsyms = 0;
  3073.     else
  3074. --- 3853,3858 ----
  3075. ***************
  3076. *** 3399,3406 ****
  3077.         else if (discard_locals == DISCARD_NONE)
  3078.       nsyms += local_sym_count;
  3079.         /* One extra for following reference on indirects */
  3080. !       if (relocatable_output)
  3081.       nsyms += set_symbol_count + global_indirect_count;
  3082.       }
  3083.   
  3084.     if (strip_symbols == STRIP_NONE)
  3085. --- 3864,3875 ----
  3086.         else if (discard_locals == DISCARD_NONE)
  3087.       nsyms += local_sym_count;
  3088.         /* One extra for following reference on indirects */
  3089. !       if (output_style == OUTPUT_RELOCATABLE)
  3090. ! #ifndef NeXT
  3091.       nsyms += set_symbol_count + global_indirect_count;
  3092. + #else
  3093. +         nsyms += set_symbol_count;
  3094. + #endif
  3095.       }
  3096.   
  3097.     if (strip_symbols == STRIP_NONE)
  3098. ***************
  3099. *** 3408,3414 ****
  3100.   
  3101.     outheader.a_syms = nsyms * sizeof (struct nlist);
  3102.   
  3103. !   if (relocatable_output)
  3104.       {
  3105.         outheader.a_trsize = text_reloc_size;
  3106.         outheader.a_drsize = data_reloc_size;
  3107. --- 3877,3883 ----
  3108.   
  3109.     outheader.a_syms = nsyms * sizeof (struct nlist);
  3110.   
  3111. !   if (output_style == OUTPUT_RELOCATABLE)
  3112.       {
  3113.         outheader.a_trsize = text_reloc_size;
  3114.         outheader.a_drsize = data_reloc_size;
  3115. ***************
  3116. *** 3419,3428 ****
  3117. --- 3888,3935 ----
  3118.         outheader.a_drsize = 0;
  3119.       }
  3120.   
  3121. +   /* Initialize the various file offsets.  */
  3122. +   output_text_offset = N_TXTOFF (outheader);
  3123. + #ifdef N_DATOFF
  3124. +   output_data_offset = N_DATOFF (outheader);
  3125. + #else
  3126. +   output_data_offset = output_text_offset + text_size;
  3127. + #endif
  3128. + #ifdef N_TRELOFF
  3129. +   output_trel_offset = N_TRELOFF (outheader);
  3130. + #else
  3131. +   output_trel_offset = output_data_offset + data_size;
  3132. + #endif
  3133. + #ifdef N_DRELOFF
  3134. +   output_drel_offset = N_DRELOFF (outheader);
  3135. + #else
  3136. +   output_drel_offset = output_trel_offset + text_reloc_size;
  3137. + #endif
  3138. +   output_syms_offset = N_SYMOFF (outheader);
  3139. +   output_strs_offset = N_STROFF (outheader);
  3140. + }
  3141. + /* Compute more section offsets once the size of the string table is known.  */
  3142. + void
  3143. + compute_more_a_out_section_offsets ()
  3144. + {
  3145. +   output_symseg_offset = output_strs_offset + output_strs_size;
  3146. + }
  3147. + /* Write the a.out header once everything else is known.  */
  3148. + void
  3149. + write_a_out_header ()
  3150. + {
  3151. +   lseek (outdesc, 0L, 0);
  3152.   #ifdef COFF_ENCAPSULATE
  3153.     if (need_coff_header)
  3154.       mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  3155.   #endif
  3156.     mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  3157.   
  3158.     /* Output whatever padding is required in the executable file
  3159. ***************
  3160. *** 3432,3438 ****
  3161. --- 3939,4570 ----
  3162.     padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  3163.   #endif
  3164.   }
  3165. + #endif
  3166. + #ifdef MACH_O
  3167. + /* Stuff pertaining to creating Mach-O files. */
  3168. + /* Convert the Mach-O style n_sect references into something the rest
  3169. +    of the loader can understand.  */
  3170. + void
  3171. + translate_mach_o_symbols (entry)
  3172. +      struct file_entry *entry;
  3173. + {
  3174. +   int i, n, g;
  3175. +   struct nlist *sym;
  3176. +   n = entry->syms_size / sizeof (struct nlist);
  3177. +   for (i = 0; i < n; ++i)
  3178. +     if (((sym = &entry->symbols[i])->n_type & ~N_EXT) == N_SECT)
  3179. +       {
  3180. +     if (sym->n_sect == entry->text_ordinal)
  3181. +       sym->n_type = (sym->n_type & N_EXT) | N_TEXT;
  3182. +     else if (sym->n_sect == entry->data_ordinal)
  3183. +       sym->n_type = (sym->n_type & N_EXT) | N_DATA;
  3184. +     else if (sym->n_sect == entry->bss_ordinal)
  3185. +       sym->n_type = (sym->n_type & N_EXT) | N_BSS;
  3186. +     else
  3187. +       fatal_with_file ("unknown section referenced in symbols of ", entry);
  3188. +     sym->n_sect = 0;
  3189. +       }
  3190. +     else if ((sym = &entry->symbols[i])->n_type == N_SLINE)
  3191. +       {
  3192. +     if (sym->n_sect == entry->text_ordinal)
  3193. +       sym->n_type = N_SLINE;
  3194. +     else if (sym->n_sect == entry->data_ordinal)
  3195. +       sym->n_type = N_DSLINE;
  3196. +     else if (sym->n_sect == entry->bss_ordinal)
  3197. +       sym->n_type = N_BSLINE;
  3198. +     else
  3199. +       fatal_with_file ("unknown section referenced in debugging symbols of ", entry);
  3200. +       }
  3201. + }
  3202. + /* Convert Mach-O style relocation info into a.out style relocation
  3203. +    info internally.  */
  3204. + void
  3205. + translate_mach_o_relocation (entry, reloc, count)
  3206. +      struct file_entry *entry;
  3207. +      struct relocation_info *reloc;
  3208. +      int count;
  3209. + {
  3210. +   int i;
  3211. +   for (i = 0; i < count; ++i)
  3212. +     if (!RELOC_EXTERN_P(&reloc[i]))
  3213. +       if (RELOC_TYPE(&reloc[i]) == R_ABS)
  3214. +     RELOC_TYPE(&reloc[i]) = N_ABS;
  3215. +       else if (RELOC_TYPE(&reloc[i]) == entry->text_ordinal)
  3216. +     RELOC_TYPE(&reloc[i]) = N_TEXT;
  3217. +       else if (RELOC_TYPE(&reloc[i]) == entry->data_ordinal)
  3218. +     RELOC_TYPE(&reloc[i]) = N_DATA;
  3219. +       else if (RELOC_TYPE(&reloc[i]) == entry->bss_ordinal)
  3220. +     RELOC_TYPE(&reloc[i]) = N_BSS;
  3221. +       else
  3222. +     fatal_with_file ("unknown section ordinal in relocation info of ", entry);
  3223. + }
  3224. + /* Header structure for OUTPUT_RELOCATABLE.  */
  3225. + struct
  3226. + {
  3227. +   struct mach_header header;
  3228. +   struct segment_command segment;
  3229. +   struct section text;
  3230. +   struct section data;
  3231. +   struct section bss;
  3232. +   struct symtab_command symtab;
  3233. + #ifdef LC_SYMSEG
  3234. +   struct symseg_command symseg;
  3235. + #endif
  3236. + } m_object;
  3237. + #ifdef NeXT
  3238. + #define CPU_TYPE CPU_TYPE_MC68030
  3239. + #define CPU_SUBTYPE CPU_SUBTYPE_NeXT
  3240. + #define THREAD_FLAVOR NeXT_THREAD_STATE_REGS
  3241. + #define THREAD_COUNT NeXT_THREAD_STATE_REGS_COUNT
  3242. + typedef struct NeXT_thread_state_regs thread_state;
  3243. + #define thread_state_entry_field pc
  3244. + #endif
  3245. + /* Header structure for all executable output forms.  */
  3246. + struct
  3247. + {
  3248. +   struct mach_header header;
  3249. +   struct segment_command pagezero;
  3250. +   struct segment_command text_segment;
  3251. +   struct section text;
  3252. +   struct segment_command data_segment;
  3253. +   struct section data;
  3254. +   struct section bss;
  3255. +   struct thread_command unixthread;
  3256. +   unsigned long int flavor;
  3257. +   unsigned long int count;
  3258. +   thread_state state;
  3259. +   struct symtab_command symtab;
  3260. + #ifdef LC_SYMSEG
  3261. +   struct symseg_command symseg;
  3262. + #endif
  3263. + } m_exec;
  3264. + /* Compute text_start and text_header_size for an a.out file.  */
  3265. + void
  3266. + initialize_mach_o_text_start ()
  3267. + {
  3268. +   if (output_style != OUTPUT_RELOCATABLE)
  3269. +     {
  3270. +       text_header_size = sizeof m_exec;
  3271. +       if (!T_flag_specified && output_style != OUTPUT_RELOCATABLE)
  3272. +     /* We reserve the first page of an executable to trap NULL dereferences.  */
  3273. +     text_start = page_size;
  3274. +     }
  3275. + }
  3276. + /* Compute data_start once text_size is known.  */
  3277. + void
  3278. + initialize_mach_o_data_start ()
  3279. + {
  3280. +   if (! Tdata_flag_specified)
  3281. +     data_start = text_start + text_size;
  3282. + }
  3283. + /* Compute offsets of various pieces of the Mach-O output file.  */
  3284. + void
  3285. + compute_mach_o_section_offsets ()
  3286. + {
  3287. +   int header_size, trsize, drsize;
  3288. +   switch (output_style)
  3289. +     {
  3290. +     case OUTPUT_RELOCATABLE:
  3291. +       header_size = sizeof m_object;
  3292. +       break;
  3293. +     default:
  3294. +       header_size = sizeof m_exec;
  3295. +       break;
  3296. +     }
  3297. +   if (strip_symbols == STRIP_ALL)
  3298. +     nsyms = 0;
  3299. +   else
  3300. +     {
  3301. +       nsyms = (defined_global_sym_count
  3302. +            + undefined_global_sym_count);
  3303. +       if (discard_locals == DISCARD_L)
  3304. +     nsyms += non_L_local_sym_count;
  3305. +       else if (discard_locals == DISCARD_NONE)
  3306. +     nsyms += local_sym_count;
  3307. +       /* One extra for following reference on indirects */
  3308. +       if (output_style == OUTPUT_RELOCATABLE)
  3309. + #ifndef NeXT
  3310. +     nsyms += set_symbol_count + global_indirect_count;
  3311. + #else
  3312. +         nsyms += set_symbol_count;
  3313. + #endif
  3314. +     }
  3315. +   if (strip_symbols == STRIP_NONE)
  3316. +     nsyms += debugger_sym_count;
  3317. +   if (output_style != OUTPUT_RELOCATABLE)
  3318. +     output_text_offset = header_size;
  3319. +   output_data_offset = output_text_offset + text_size;
  3320. +   output_trel_offset = output_data_offset + data_size;
  3321. +   if (output_style == OUTPUT_RELOCATABLE)
  3322. +     trsize = text_reloc_size, drsize = data_reloc_size;
  3323. +   else
  3324. +     trsize = drsize = 0;
  3325. +   output_drel_offset = output_trel_offset + trsize;
  3326. +   output_syms_offset = output_drel_offset + drsize;
  3327. +   output_strs_offset = output_syms_offset + nsyms * sizeof (struct nlist);
  3328. + }
  3329. + /* Compute more section offsets once the size of the string table is known.  */
  3330. + void
  3331. + compute_more_mach_o_section_offsets ()
  3332. + {
  3333. +   output_symseg_offset = output_strs_offset + output_strs_size;
  3334. + }
  3335. + /* Write the Mach-O header once everything else is known.  */
  3336. + void
  3337. + write_mach_o_header ()
  3338. + {
  3339. +   struct mach_header header;
  3340. +   struct section text, data, bss;
  3341. +   struct symtab_command symtab;
  3342. + #ifdef LC_SYMSEG
  3343. +   struct symseg_command symseg;
  3344. + #endif
  3345. +   thread_state state;
  3346. +   lseek (outdesc, 0L, 0);
  3347. +   header.magic = MH_MAGIC;
  3348. +   header.cputype = CPU_TYPE;
  3349. +   header.cpusubtype = CPU_SUBTYPE;
  3350. +   header.filetype = output_style == OUTPUT_RELOCATABLE ? MH_OBJECT : MH_EXECUTE;
  3351. + #ifdef LC_SYMSEG
  3352. +   switch (output_style)
  3353. +     {
  3354. +     case OUTPUT_RELOCATABLE:
  3355. +       header.ncmds = 3;
  3356. +       header.sizeofcmds = sizeof m_object - sizeof header;
  3357. +       break;
  3358. +     default:
  3359. +       header.ncmds = 6;
  3360. +       header.sizeofcmds = sizeof m_exec - sizeof header;
  3361. +       break;
  3362. +     }
  3363. + #else
  3364. +   switch (output_style)
  3365. +     {
  3366. +     case OUTPUT_RELOCATABLE:
  3367. +       header.ncmds = 2;
  3368. +       header.sizeofcmds = sizeof m_object - sizeof header;
  3369. +       break;
  3370. +     default:
  3371. +       header.ncmds = 5;
  3372. +       header.sizeofcmds = sizeof m_exec - sizeof header;
  3373. +       break;
  3374. +     }
  3375. + #endif
  3376. +   header.flags = undefined_global_sym_count ? 0 : MH_NOUNDEFS;
  3377. +   bzero((char *) &text, sizeof text);
  3378. +   strncpy(text.sectname, SECT_TEXT, sizeof text.sectname);
  3379. +   strncpy(text.segname, SEG_TEXT, sizeof text.segname);
  3380. +   text.addr = text_start;
  3381. +   text.size = text_size;
  3382. +   text.offset = output_text_offset;
  3383. +   text.align = text.addr % sizeof (double) ? sizeof (int) : sizeof (double);
  3384. +   text.reloff = output_trel_offset;
  3385. +   text.nreloc = output_style == OUTPUT_RELOCATABLE
  3386. +     ? text_reloc_size / sizeof (struct relocation_info) : 0;
  3387. +   text.flags = 0;
  3388. +   bzero((char *) &data, sizeof data);
  3389. +   strncpy(data.sectname, SECT_DATA, sizeof data.sectname);
  3390. +   strncpy(data.segname, output_style == OUTPUT_WRITABLE_TEXT ? SEG_TEXT : SEG_DATA,
  3391. +       sizeof data.segname);
  3392. +   data.addr = data_start;
  3393. +   data.size = data_size;
  3394. +   data.offset = output_data_offset;
  3395. +   data.align = data.addr % sizeof (double) ? sizeof (int) : sizeof (double);
  3396. +   data.reloff = output_drel_offset;
  3397. +   data.nreloc = output_style == OUTPUT_RELOCATABLE
  3398. +     ? data_reloc_size / sizeof (struct relocation_info) : 0;
  3399. +   data.flags = 0;
  3400. +   bzero((char *) &bss, sizeof bss);
  3401. +   strncpy(bss.sectname, SECT_BSS, sizeof data.sectname);
  3402. +   strncpy(bss.segname, output_style == OUTPUT_WRITABLE_TEXT ? SEG_TEXT : SEG_DATA,
  3403. +       sizeof bss.segname);
  3404. +   bss.addr = data_start + data_size;
  3405. +   bss.size = bss_size;
  3406. +   bss.align = bss.addr % sizeof (double) ? sizeof (int) : sizeof (double);
  3407. +   bss.reloff = 0;
  3408. +   bss.nreloc = 0;
  3409. +   bss.flags = S_ZEROFILL;
  3410. +   symtab.cmd = LC_SYMTAB;
  3411. +   symtab.cmdsize = sizeof symtab;
  3412. +   symtab.symoff = output_syms_offset;
  3413. +   symtab.nsyms = output_syms_size / sizeof (struct nlist);
  3414. +   symtab.stroff = output_strs_offset;
  3415. +   symtab.strsize = output_strs_size;
  3416. + #ifdef LC_SYMSEG
  3417. +   symseg.cmd = LC_SYMSEG;
  3418. +   symseg.cmdsize = sizeof symseg;
  3419. +   symseg.offset = output_symseg_offset;
  3420. +   symseg.size = output_symseg_size;
  3421. + #endif
  3422. +   switch (output_style)
  3423. +     {
  3424. +     case OUTPUT_RELOCATABLE:
  3425. +       m_object.header = header;
  3426. +       m_object.segment.cmd = LC_SEGMENT;
  3427. +       m_object.segment.cmdsize = sizeof (struct segment_command) + 3 * sizeof (struct section);
  3428. +       strncpy(m_object.segment.segname, SEG_TEXT, sizeof m_object.segment.segname);
  3429. +       m_object.segment.vmaddr = 0;
  3430. +       m_object.segment.vmsize = text.size + data.size + bss.size;
  3431. +       m_object.segment.fileoff = text.offset;
  3432. +       m_object.segment.filesize = text.size + data.size;
  3433. +       m_object.segment.maxprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  3434. +       m_object.segment.initprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  3435. +       m_object.segment.nsects = 3;
  3436. +       m_object.segment.flags = 0;
  3437. +       m_object.text = text;
  3438. +       m_object.data = data;
  3439. +       m_object.bss = bss;
  3440. +       m_object.symtab = symtab;
  3441. + #ifdef LC_SYMSEG
  3442. +       m_object.symseg = symseg;
  3443. + #endif
  3444. +       mywrite((char *) &m_object, 1, sizeof m_object, outdesc);
  3445. +       break;
  3446. +     default:
  3447. +       m_exec.header = header;
  3448. +       m_exec.pagezero.cmd = LC_SEGMENT;
  3449. +       m_exec.pagezero.cmdsize = sizeof (struct segment_command);
  3450. +       strncpy(m_exec.pagezero.segname, SEG_PAGEZERO, sizeof m_exec.pagezero.segname);
  3451. +       m_exec.pagezero.vmaddr = 0;
  3452. +       m_exec.pagezero.vmsize = page_size;
  3453. +       m_exec.pagezero.fileoff = 0;
  3454. +       m_exec.pagezero.filesize = 0;
  3455. +       m_exec.pagezero.maxprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  3456. +       m_exec.pagezero.initprot = 0;
  3457. +       m_exec.pagezero.nsects = 0;
  3458. +       m_exec.pagezero.flags = 0;
  3459. +       m_exec.text_segment.cmd = LC_SEGMENT;
  3460. +       m_exec.text_segment.cmdsize = sizeof (struct segment_command) + sizeof (struct section);
  3461. +       strncpy(m_exec.text_segment.segname, SEG_TEXT, sizeof m_exec.text_segment.segname);
  3462. +       m_exec.text_segment.vmaddr = text_start;
  3463. +       m_exec.text_segment.vmsize = text_size;
  3464. +       m_exec.text_segment.fileoff = output_text_offset;
  3465. +       m_exec.text_segment.filesize = text_size;
  3466. +       m_exec.text_segment.maxprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  3467. +       m_exec.text_segment.initprot = VM_PROT_READ | VM_PROT_EXECUTE;
  3468. +       if (output_style == OUTPUT_WRITABLE_TEXT)
  3469. +     m_exec.text_segment.initprot |= VM_PROT_WRITE;
  3470. +       m_exec.text_segment.nsects = 1;
  3471. +       m_exec.text_segment.flags = 0;
  3472. +       m_exec.text = text;
  3473. +       m_exec.data_segment.cmd = LC_SEGMENT;
  3474. +       m_exec.data_segment.cmdsize = sizeof (struct segment_command) + 2 * sizeof (struct section);
  3475. +       strncpy(m_exec.data_segment.segname, SEG_DATA, sizeof m_exec.data_segment.segname);
  3476. +       m_exec.data_segment.vmaddr = data_start;
  3477. +       m_exec.data_segment.vmsize = data_size + bss_size;
  3478. +       m_exec.data_segment.fileoff = output_data_offset;
  3479. +       m_exec.data_segment.filesize = data_size;
  3480. +       m_exec.data_segment.maxprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  3481. +       m_exec.data_segment.initprot = VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
  3482. +       m_exec.data_segment.nsects = 2;
  3483. +       m_exec.data_segment.flags = 0;
  3484. +       m_exec.data = data;
  3485. +       m_exec.bss = bss;
  3486. +       m_exec.unixthread.cmd = LC_UNIXTHREAD;
  3487. +       m_exec.unixthread.cmdsize
  3488. +     = sizeof (struct thread_command) + 2 * sizeof (long int) + sizeof (thread_state);
  3489. +       m_exec.flavor = THREAD_FLAVOR;
  3490. +       m_exec.count = THREAD_COUNT;
  3491. +       m_exec.state.thread_state_entry_field = entry_symbol
  3492. +     ? entry_symbol->value : text_start + text_header_size;
  3493. +       m_exec.symtab = symtab;
  3494. + #ifdef LC_SYMSEG
  3495. +       m_exec.symseg = symseg;
  3496. + #endif
  3497. +       mywrite((char *) &m_exec, 1, sizeof m_exec, outdesc);
  3498. +       break;
  3499. +     }
  3500. + }
  3501. + /* Translate a.out style symbols into Mach-O style symbols.  */
  3502. + void
  3503. + generate_mach_o_symbols (syms, nsyms)
  3504. +      struct nlist *syms;
  3505. +      int nsyms;
  3506. + {
  3507. +   int i;
  3508. +   for (i = 0; i < nsyms; ++i)
  3509. +     switch (syms[i].n_type)
  3510. +       {
  3511. +       case N_TEXT:
  3512. +       case N_TEXT | N_EXT:
  3513. +     syms[i].n_type = syms[i].n_type & N_EXT | N_SECT;
  3514. +     syms[i].n_sect = 1;    /* text section ordinal */
  3515. +     break;
  3516. +       case N_DATA:
  3517. +       case N_DATA | N_EXT:
  3518. +     syms[i].n_type = syms[i].n_type & N_EXT | N_SECT;
  3519. +     syms[i].n_sect = 2;    /* data section ordinal */
  3520. +     break;
  3521. +       case N_BSS:
  3522. +       case N_BSS | N_EXT:
  3523. +     syms[i].n_type = syms[i].n_type & N_EXT | N_BSS;
  3524. +     syms[i].n_sect = 3;    /* bss section ordinal */
  3525. +     break;
  3526. +       case N_SLINE:
  3527. +     syms[i].n_type = N_SLINE;
  3528. +     syms[i].n_sect = 1;    /* text section ordinal */
  3529. +     break;
  3530. +       case N_DSLINE:
  3531. +     syms[i].n_type = N_SLINE;
  3532. +     syms[i].n_sect = 2;    /* data section ordinal */
  3533. +     break;
  3534. +       case N_BSLINE:
  3535. +     syms[i].n_type = N_SLINE;
  3536. +     syms[i].n_sect = 3;    /* bss section ordinal */
  3537. +     break;
  3538. +       }
  3539. + }
  3540. + /* Translate a.out style relocation info into Mach-O style relocation
  3541. +    info.  */
  3542. + void
  3543. + generate_mach_o_relocations (reloc, nreloc)
  3544. +      struct relocation_info *reloc;
  3545. +      int nreloc;
  3546. + {
  3547. +   int i;
  3548. +   for (i = 0; i < nreloc; ++i)
  3549. +     if (!RELOC_EXTERN_P (&reloc[i]))
  3550. +       switch (RELOC_TYPE (&reloc[i]))
  3551. +     {
  3552. +     case N_ABS:
  3553. +     case N_ABS | N_EXT:
  3554. +       RELOC_TYPE (&reloc[i]) = R_ABS;
  3555. +       break;
  3556. +     case N_TEXT:
  3557. +     case N_TEXT | N_EXT:
  3558. +       RELOC_TYPE (&reloc[i]) = 1; /* output text section ordinal */
  3559. +       break;
  3560. +     case N_DATA:
  3561. +     case N_DATA | N_EXT:
  3562. +       RELOC_TYPE (&reloc[i]) = 2; /* output data section ordinal */
  3563. +       break;
  3564. +     case N_BSS:
  3565. +     case N_BSS | N_EXT:
  3566. +       RELOC_TYPE (&reloc[i]) = 3; /* output bss section ordinal */
  3567. +       break;
  3568. +     }
  3569. + }
  3570. + #endif
  3571. + /* The following functions are simple switches according to the
  3572. +    output style.  */
  3573. + /* Compute text_start and text_header_size as appropriate for the
  3574. +    output format.  */
  3575. + void
  3576. + initialize_text_start ()
  3577. + {
  3578. + #ifdef A_OUT
  3579. +   if (output_file_type == IS_A_OUT)
  3580. +     {
  3581. +       initialize_a_out_text_start ();
  3582. +       return;
  3583. +     }
  3584. + #endif
  3585. + #ifdef MACH_O
  3586. +   if (output_file_type == IS_MACH_O)
  3587. +     {
  3588. +       initialize_mach_o_text_start ();
  3589. +       return;
  3590. +     }
  3591. + #endif
  3592. +   fatal ("unknown output file type (enum file_type)", (char *) 0);
  3593. + }
  3594. + /* Initialize data_start as appropriate to the output format, once text_size
  3595. +    is known.  */
  3596. + void
  3597. + initialize_data_start ()
  3598. + {
  3599. + #ifdef A_OUT
  3600. +   if (output_file_type == IS_A_OUT)
  3601. +     {
  3602. +       initialize_a_out_data_start ();
  3603. +       return;
  3604. +     }
  3605. + #endif
  3606. + #ifdef MACH_O
  3607. +   if (output_file_type == IS_MACH_O)
  3608. +     {
  3609. +       initialize_mach_o_data_start ();
  3610. +       return;
  3611. +     }
  3612. + #endif
  3613. +   fatal ("unknown output file type (enum file_type)", (char *) 0);
  3614. + }
  3615. + /* Compute offsets of the various sections within the output file.  */
  3616. + void
  3617. + compute_section_offsets ()
  3618. + {
  3619. + #ifdef A_OUT
  3620. +   if (output_file_type == IS_A_OUT)
  3621. +     {
  3622. +       compute_a_out_section_offsets ();
  3623. +       return;
  3624. +     }
  3625. + #endif
  3626. + #ifdef MACH_O
  3627. +   if (output_file_type == IS_MACH_O)
  3628. +     {
  3629. +       compute_mach_o_section_offsets ();
  3630. +       return;
  3631. +     }
  3632. + #endif
  3633. +   fatal ("unknown output file type (enum file_type)", (char *) 0);
  3634. + }
  3635. + /* Compute more section offsets, once the size of the string table
  3636. +    is finalized.  */
  3637. + void
  3638. + compute_more_section_offsets ()
  3639. + {
  3640. + #ifdef A_OUT
  3641. +   if (output_file_type == IS_A_OUT)
  3642. +     {
  3643. +       compute_more_a_out_section_offsets ();
  3644. +       return;
  3645. +     }
  3646. + #endif
  3647. + #ifdef MACH_O
  3648. +   if (output_file_type == IS_MACH_O)
  3649. +     {
  3650. +       compute_more_mach_o_section_offsets ();
  3651. +       return;
  3652. +     }
  3653. + #endif
  3654. +   fatal ("unknown output file type (enum file_type)", (char *) 0);
  3655. + }
  3656. + /* Write the output file header, once everything is known.  */
  3657. + void
  3658. + write_header ()
  3659. + {
  3660. + #ifdef A_OUT
  3661. +   if (output_file_type == IS_A_OUT)
  3662. +     {
  3663. +       write_a_out_header ();
  3664. +       return;
  3665. +     }
  3666. + #endif
  3667. + #ifdef MACH_O
  3668. +   if (output_file_type == IS_MACH_O)
  3669. +     {
  3670. +       write_mach_o_header ();
  3671. +       return;
  3672. +     }
  3673. + #endif
  3674. +   fatal ("unknown output file type (enum file_type)", (char *) 0);
  3675. + }
  3676. + /* Write the output file */
  3677. + void
  3678. + write_output ()
  3679. + {
  3680. +   struct stat statbuf;
  3681. +   int filemode, mask;
  3682. +   /* Remove the old file in case it is owned by someone else.
  3683. +      This prevents spurious "not owner" error messages.
  3684. +      Don't check for errors from unlink; we don't really care
  3685. +      whether it worked.
  3686. +      Note that this means that if the output file is hard linked,
  3687. +      the other names will still have the old contents.  This is
  3688. +      the way Unix ld works; I'm going to consider it a feature.  */
  3689. +   (void) unlink (output_filename);
  3690. +   
  3691. +   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  3692. +   if (outdesc < 0) perror_name (output_filename);
  3693. +   if (fstat (outdesc, &statbuf) < 0)
  3694. +     perror_name (output_filename);
  3695. +   filemode = statbuf.st_mode;
  3696. +   chmod (output_filename, filemode & ~0111);
  3697. +   /* Calculate the offsets of the various pieces of the output file.  */
  3698. +   compute_section_offsets ();
  3699. +   /* Output the text and data segments, relocating as we go.  */
  3700. +   write_text ();
  3701. +   write_data ();
  3702. +   /* Output the merged relocation info, if requested with `-r'.  */
  3703. +   if (output_style == OUTPUT_RELOCATABLE)
  3704. +     write_rel ();
  3705. +   /* Output the symbol table (both globals and locals).  */
  3706. +   write_syms ();
  3707. +   /* At this point the total size of the symbol table and string table
  3708. +      are finalized.  */
  3709. +   compute_more_section_offsets ();
  3710. +   /* Copy any GDB symbol segments from input files.  */
  3711. +   write_symsegs ();
  3712. +   /* Now that everything is known about the output file, write its header.  */
  3713. +   write_header ();
  3714. +   close (outdesc);
  3715. +   mask = umask (0);
  3716. +   umask (mask);
  3717. +   if (chmod (output_filename, filemode | (0111 & ~mask)) == -1)
  3718. +     perror_name (output_filename);
  3719. + }
  3720.   
  3721. + void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3722.   /* Relocate the text segment of each input file
  3723.      and write to the output file.  */
  3724.   
  3725. ***************
  3726. *** 3442,3448 ****
  3727.     if (trace_files)
  3728.       fprintf (stderr, "Copying and relocating text:\n\n");
  3729.   
  3730. !   each_full_file (copy_text);
  3731.     file_close ();
  3732.   
  3733.     if (trace_files)
  3734. --- 4574,4582 ----
  3735.     if (trace_files)
  3736.       fprintf (stderr, "Copying and relocating text:\n\n");
  3737.   
  3738. !   lseek (outdesc, output_text_offset + text_header_size, 0);
  3739. !   each_full_file (copy_text, 0);
  3740.     file_close ();
  3741.   
  3742.     if (trace_files)
  3743. ***************
  3744. *** 3451,3469 ****
  3745.     padfile (text_pad, outdesc);
  3746.   }
  3747.   
  3748. - int
  3749. - text_offset (entry)
  3750. -      struct file_entry *entry;
  3751. - {
  3752. -   return entry->starting_offset + N_TXTOFF (entry->header);
  3753. - }
  3754.   /* Read in all of the relocation information */
  3755.   
  3756.   void
  3757.   read_relocation ()
  3758.   {
  3759. !   each_full_file (read_file_relocation);
  3760.   }
  3761.   
  3762.   /* Read in the relocation sections of ENTRY if necessary */
  3763. --- 4585,4596 ----
  3764.     padfile (text_pad, outdesc);
  3765.   }
  3766.   
  3767.   /* Read in all of the relocation information */
  3768.   
  3769.   void
  3770.   read_relocation ()
  3771.   {
  3772. !   each_full_file (read_file_relocation, 0);
  3773.   }
  3774.   
  3775.   /* Read in the relocation sections of ENTRY if necessary */
  3776. ***************
  3777. *** 3479,3490 ****
  3778.     desc = -1;
  3779.     if (!entry->textrel)
  3780.       {
  3781. !       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  3782.         desc = file_open (entry);
  3783. !       lseek (desc,
  3784. !          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  3785. !          L_SET);
  3786. !       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  3787.       {
  3788.         fprintf (stderr, "Return from read: %d\n", read_return);
  3789.         fatal_with_file ("premature eof in text relocation of ", entry);
  3790. --- 4606,4615 ----
  3791.     desc = -1;
  3792.     if (!entry->textrel)
  3793.       {
  3794. !       reloc = (struct relocation_info *) xmalloc (entry->text_reloc_size);
  3795.         desc = file_open (entry);
  3796. !       lseek (desc, entry->starting_offset + entry->text_reloc_offset, L_SET);
  3797. !       if (entry->text_reloc_size != (read_return = read (desc, reloc, entry->text_reloc_size)))
  3798.       {
  3799.         fprintf (stderr, "Return from read: %d\n", read_return);
  3800.         fatal_with_file ("premature eof in text relocation of ", entry);
  3801. ***************
  3802. *** 3494,3509 ****
  3803.   
  3804.     if (!entry->datarel)
  3805.       {
  3806. !       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  3807.         if (desc == -1) desc = file_open (entry);
  3808. !       lseek (desc,
  3809. !          text_offset (entry) + entry->header.a_text
  3810. !          + entry->header.a_data + entry->header.a_trsize,
  3811. !          L_SET);
  3812. !       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3813.       fatal_with_file ("premature eof in data relocation of ", entry);
  3814.         entry->datarel = reloc;
  3815.       }
  3816.   }
  3817.   
  3818.   /* Read the text segment contents of ENTRY, relocate them,
  3819. --- 4619,4641 ----
  3820.   
  3821.     if (!entry->datarel)
  3822.       {
  3823. !       reloc = (struct relocation_info *) xmalloc (entry->data_reloc_size);
  3824.         if (desc == -1) desc = file_open (entry);
  3825. !       lseek (desc, entry->starting_offset + entry->data_reloc_offset, L_SET);
  3826. !       if (entry->data_reloc_size != read (desc, reloc, entry->data_reloc_size))
  3827.       fatal_with_file ("premature eof in data relocation of ", entry);
  3828.         entry->datarel = reloc;
  3829.       }
  3830. + #ifdef MACH_O
  3831. +   if (entry->file_type == IS_MACH_O)
  3832. +     {
  3833. +       translate_mach_o_relocation (entry, entry->textrel,
  3834. +                    entry->text_reloc_size / sizeof (struct relocation_info));
  3835. +       translate_mach_o_relocation (entry, entry->datarel,
  3836. +                    entry->data_reloc_size / sizeof (struct relocation_info));
  3837. +     }
  3838. + #endif
  3839.   }
  3840.   
  3841.   /* Read the text segment contents of ENTRY, relocate them,
  3842. ***************
  3843. *** 3525,3536 ****
  3844.   
  3845.     /* Allocate space for the file's text section */
  3846.   
  3847. !   bytes = (char *) alloca (entry->header.a_text);
  3848.   
  3849.     /* Deal with relocation information however is appropriate */
  3850.   
  3851.     if (entry->textrel)  reloc = entry->textrel;
  3852. !   else if (relocatable_output)
  3853.       {
  3854.         read_file_relocation (entry);
  3855.         reloc = entry->textrel;
  3856. --- 4657,4668 ----
  3857.   
  3858.     /* Allocate space for the file's text section */
  3859.   
  3860. !   bytes = (char *) alloca (entry->text_size);
  3861.   
  3862.     /* Deal with relocation information however is appropriate */
  3863.   
  3864.     if (entry->textrel)  reloc = entry->textrel;
  3865. !   else if (output_style == OUTPUT_RELOCATABLE)
  3866.       {
  3867.         read_file_relocation (entry);
  3868.         reloc = entry->textrel;
  3869. ***************
  3870. *** 3537,3563 ****
  3871.       }
  3872.     else
  3873.       {
  3874. !       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  3875. !       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  3876. !       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  3877.       fatal_with_file ("premature eof in text relocation of ", entry);
  3878.       }
  3879.   
  3880.     /* Read the text section into core.  */
  3881.   
  3882. !   lseek (desc, text_offset (entry), 0);
  3883. !   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  3884.       fatal_with_file ("premature eof in text section of ", entry);
  3885.   
  3886.     /* Relocate the text according to the text relocation.  */
  3887.   
  3888. !   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  3889. !               reloc, entry->header.a_trsize, entry);
  3890.   
  3891.     /* Write the relocated text to the output file.  */
  3892.   
  3893. !   mywrite (bytes, 1, entry->header.a_text, outdesc);
  3894.   }
  3895.   
  3896.   /* Relocate the data segment of each input file
  3897. --- 4669,4699 ----
  3898.       }
  3899.     else
  3900.       {
  3901. !       reloc = (struct relocation_info *) alloca (entry->text_reloc_size);
  3902. !       lseek (desc, entry->starting_offset + entry->text_reloc_offset, L_SET);
  3903. !       if (entry->text_reloc_size != read (desc, reloc, entry->text_reloc_size))
  3904.       fatal_with_file ("premature eof in text relocation of ", entry);
  3905. + #ifdef MACH_O
  3906. +       if (entry->file_type == IS_MACH_O)
  3907. +     translate_mach_o_relocation (entry, reloc,
  3908. +                      entry->text_reloc_size / sizeof (struct relocation_info));
  3909. + #endif
  3910.       }
  3911.   
  3912.     /* Read the text section into core.  */
  3913.   
  3914. !   lseek (desc, entry->starting_offset + entry->text_offset, L_SET);
  3915. !   if (entry->text_size != read (desc, bytes, entry->text_size))
  3916.       fatal_with_file ("premature eof in text section of ", entry);
  3917.   
  3918.     /* Relocate the text according to the text relocation.  */
  3919.   
  3920. !   perform_relocation (bytes, entry->text_start_address - entry->orig_text_address,
  3921. !               entry->text_size, reloc, entry->text_reloc_size, entry);
  3922.   
  3923.     /* Write the relocated text to the output file.  */
  3924.   
  3925. !   mywrite (bytes, 1, entry->text_size, outdesc);
  3926.   }
  3927.   
  3928.   /* Relocate the data segment of each input file
  3929. ***************
  3930. *** 3569,3575 ****
  3931.     if (trace_files)
  3932.       fprintf (stderr, "Copying and relocating data:\n\n");
  3933.   
  3934. !   each_full_file (copy_data);
  3935.     file_close ();
  3936.   
  3937.     /* Write out the set element vectors.  See digest symbols for
  3938. --- 4705,4713 ----
  3939.     if (trace_files)
  3940.       fprintf (stderr, "Copying and relocating data:\n\n");
  3941.   
  3942. !   lseek (outdesc, output_data_offset, 0);
  3943. !   each_full_file (copy_data, 0);
  3944.     file_close ();
  3945.   
  3946.     /* Write out the set element vectors.  See digest symbols for
  3947. ***************
  3948. *** 3603,3612 ****
  3949.   
  3950.     desc = file_open (entry);
  3951.   
  3952. !   bytes = (char *) alloca (entry->header.a_data);
  3953.   
  3954.     if (entry->datarel) reloc = entry->datarel;
  3955. !   else if (relocatable_output)    /* Will need this again */
  3956.       {
  3957.         read_file_relocation (entry);
  3958.         reloc = entry->datarel;
  3959. --- 4741,4750 ----
  3960.   
  3961.     desc = file_open (entry);
  3962.   
  3963. !   bytes = (char *) alloca (entry->data_size);
  3964.   
  3965.     if (entry->datarel) reloc = entry->datarel;
  3966. !   else if (output_style == OUTPUT_RELOCATABLE)    /* Will need this again */
  3967.       {
  3968.         read_file_relocation (entry);
  3969.         reloc = entry->datarel;
  3970. ***************
  3971. *** 3613,3634 ****
  3972.       }
  3973.     else
  3974.       {
  3975. !       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  3976. !       lseek (desc, text_offset (entry) + entry->header.a_text
  3977. !          + entry->header.a_data + entry->header.a_trsize,
  3978. !          0);
  3979. !       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3980.       fatal_with_file ("premature eof in data relocation of ", entry);
  3981.       }
  3982.   
  3983. !   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  3984. !   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  3985.       fatal_with_file ("premature eof in data section of ", entry);
  3986.   
  3987. !   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  3988. !               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  3989.   
  3990. !   mywrite (bytes, 1, entry->header.a_data, outdesc);
  3991.   }
  3992.   
  3993.   /* Relocate ENTRY's text or data section contents.
  3994. --- 4751,4775 ----
  3995.       }
  3996.     else
  3997.       {
  3998. !       reloc = (struct relocation_info *) alloca (entry->data_reloc_size);
  3999. !       lseek (desc, entry->starting_offset + entry->data_reloc_offset, L_SET);
  4000. !       if (entry->data_reloc_size != read (desc, reloc, entry->data_reloc_size))
  4001.       fatal_with_file ("premature eof in data relocation of ", entry);
  4002. + #ifdef MACH_O
  4003. +       if (entry->file_type == IS_MACH_O)
  4004. +     translate_mach_o_relocation (entry, reloc,
  4005. +                      entry->data_reloc_size / sizeof (struct relocation_info));
  4006. + #endif
  4007.       }
  4008.   
  4009. !   lseek (desc, entry->starting_offset + entry->data_offset, L_SET);
  4010. !   if (entry->data_size != read (desc, bytes, entry->data_size))
  4011.       fatal_with_file ("premature eof in data section of ", entry);
  4012.   
  4013. !   perform_relocation (bytes, entry->data_start_address - entry->orig_data_address,
  4014. !               entry->data_size, reloc, entry->data_reloc_size, entry);
  4015.   
  4016. !   mywrite (bytes, 1, entry->data_size, outdesc);
  4017.   }
  4018.   
  4019.   /* Relocate ENTRY's text or data section contents.
  4020. ***************
  4021. *** 3638,3644 ****
  4022.        in the output file and its address in the input file.
  4023.      RELOC_INFO is the address of the relocation info, in core.
  4024.      RELOC_SIZE is its length in bytes.  */
  4025. ! /* This version is about to be severley hacked by Randy.  Hope it
  4026.      works afterwards. */
  4027.   void
  4028.   perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  4029. --- 4779,4785 ----
  4030.        in the output file and its address in the input file.
  4031.      RELOC_INFO is the address of the relocation info, in core.
  4032.      RELOC_SIZE is its length in bytes.  */
  4033. ! /* This version is about to be severly hacked by Randy.  Hope it
  4034.      works afterwards. */
  4035.   void
  4036.   perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  4037. ***************
  4038. *** 3652,3661 ****
  4039.     register struct relocation_info *p = reloc_info;
  4040.     struct relocation_info *end
  4041.       = reloc_info + reloc_size / sizeof (struct relocation_info);
  4042. !   int text_relocation = entry->text_start_address;
  4043. !   int data_relocation = entry->data_start_address - entry->header.a_text;
  4044. !   int bss_relocation
  4045. !     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  4046.   
  4047.     for (; p < end; p++)
  4048.       {
  4049. --- 4793,4801 ----
  4050.     register struct relocation_info *p = reloc_info;
  4051.     struct relocation_info *end
  4052.       = reloc_info + reloc_size / sizeof (struct relocation_info);
  4053. !   int text_relocation = entry->text_start_address - entry->orig_text_address;
  4054. !   int data_relocation = entry->data_start_address - entry->orig_data_address;
  4055. !   int bss_relocation = entry->bss_start_address - entry->orig_bss_address;
  4056.   
  4057.     for (; p < end; p++)
  4058.       {
  4059. ***************
  4060. *** 3680,3686 ****
  4061.           sp = (symbol *) sp->value;
  4062.   #endif
  4063.   
  4064. !       if (symindex >= entry->header.a_syms)
  4065.           fatal_with_file ("relocation symbolnum out of range in ", entry);
  4066.   
  4067.         /* If the symbol is undefined, leave it at zero.  */
  4068. --- 4820,4826 ----
  4069.           sp = (symbol *) sp->value;
  4070.   #endif
  4071.   
  4072. !       if (symindex >= entry->syms_size)
  4073.           fatal_with_file ("relocation symbolnum out of range in ", entry);
  4074.   
  4075.         /* If the symbol is undefined, leave it at zero.  */
  4076. ***************
  4077. *** 3698,3713 ****
  4078.   
  4079.       case N_DATA:
  4080.       case N_DATA | N_EXT:
  4081. -       /* A word that points to beginning of the the data section
  4082. -          initially contains not 0 but rather the "address" of that section
  4083. -          in the input file, which is the length of the file's text.  */
  4084.         relocation = data_relocation;
  4085.         break;
  4086.   
  4087.       case N_BSS:
  4088.       case N_BSS | N_EXT:
  4089. -       /* Similarly, an input word pointing to the beginning of the bss
  4090. -          initially contains the length of text plus data of the file.  */
  4091.         relocation = bss_relocation;
  4092.         break;
  4093.   
  4094. --- 4838,4848 ----
  4095. ***************
  4096. *** 3725,3731 ****
  4097.   
  4098.   #ifdef RELOC_ADD_EXTRA
  4099.         relocation += RELOC_ADD_EXTRA(p);
  4100. !       if (relocatable_output)
  4101.       {
  4102.         /* If this RELOC_ADD_EXTRA is 0, it means that the
  4103.            symbol was external and the relocation does not
  4104. --- 4860,4866 ----
  4105.   
  4106.   #ifdef RELOC_ADD_EXTRA
  4107.         relocation += RELOC_ADD_EXTRA(p);
  4108. !       if (output_style == OUTPUT_RELOCATABLE)
  4109.       {
  4110.         /* If this RELOC_ADD_EXTRA is 0, it means that the
  4111.            symbol was external and the relocation does not
  4112. ***************
  4113. *** 3783,3788 ****
  4114. --- 4918,4939 ----
  4115.         break;
  4116.   
  4117.       case 2:
  4118. + #ifdef CROSS_LINKER
  4119. +       /* This is necessary if the host has stricter alignment
  4120. +          than the target.  Too slow to use all the time.
  4121. +          Also doesn't deal with differing byte-order.  */
  4122. +       {
  4123. +         /* Thing to relocate.  */
  4124. +         long thing;
  4125. +         bcopy (data + addr, &thing, sizeof (thing));
  4126. +         if (RELOC_MEMORY_SUB_P (p))
  4127. +           relocation -= mask & thing;
  4128. +         else if (RELOC_MEMORY_ADD_P (p))
  4129. +           relocation += mask & thing;
  4130. +         thing = (thing & ~mask) | relocation;
  4131. +         bcopy (&thing, data + addr, sizeof (thing));
  4132. +       }
  4133. + #else /* not CROSS_LINKER */
  4134.         if (RELOC_MEMORY_SUB_P(p))
  4135.           relocation -= mask & *(long *) (data + addr);
  4136.         else if (RELOC_MEMORY_ADD_P(p))
  4137. ***************
  4138. *** 3789,3794 ****
  4139. --- 4940,4946 ----
  4140.           relocation += mask & *(long *) (data + addr);
  4141.         *(long *) (data + addr) &= ~mask;
  4142.         *(long *) (data + addr) |= relocation;
  4143. + #endif /* not CROSS_LINKER */
  4144.         break;
  4145.   
  4146.       default:
  4147. ***************
  4148. *** 3797,3803 ****
  4149.       }
  4150.   }
  4151.   
  4152. ! /* For relocatable_output only: write out the relocation,
  4153.      relocating the addresses-to-be-relocated.  */
  4154.   
  4155.   void coptxtrel (), copdatrel ();
  4156. --- 4949,4955 ----
  4157.       }
  4158.   }
  4159.   
  4160. ! /* For OUTPUT_RELOCATABLE only: write out the relocation,
  4161.      relocating the addresses-to-be-relocated.  */
  4162.   
  4163.   void coptxtrel (), copdatrel ();
  4164. ***************
  4165. *** 3823,3848 ****
  4166.       if (sp->referenced || sp->defined)
  4167.         {
  4168.           sp->def_count = count++;
  4169.           /* Leave room for the reference required by N_INDR, if
  4170.              necessary.  */
  4171.           if ((sp->defined & ~N_EXT) == N_INDR)
  4172.             count++;
  4173.         }
  4174.       }
  4175. !   /* Correct, because if (reloatable_output), we will also be writing
  4176.        whatever indirect blocks we have.  */
  4177.     if (count != defined_global_sym_count
  4178.         + undefined_global_sym_count + global_indirect_count)
  4179.       fatal ("internal error");
  4180.   
  4181.     /* Write out the relocations of all files, remembered from copy_text.  */
  4182.   
  4183. !   each_full_file (coptxtrel);
  4184.   
  4185.     if (trace_files)
  4186.       fprintf (stderr, "\nWriting data relocation:\n\n");
  4187.   
  4188. !   each_full_file (copdatrel);
  4189.   
  4190.     if (trace_files)
  4191.       fprintf (stderr, "\n");
  4192. --- 4975,5009 ----
  4193.       if (sp->referenced || sp->defined)
  4194.         {
  4195.           sp->def_count = count++;
  4196. + #ifndef NeXT
  4197.           /* Leave room for the reference required by N_INDR, if
  4198.              necessary.  */
  4199.           if ((sp->defined & ~N_EXT) == N_INDR)
  4200.             count++;
  4201. + #endif
  4202.         }
  4203.       }
  4204. !   /* Correct, because if (OUTPUT_RELOCATABLE), we will also be writing
  4205.        whatever indirect blocks we have.  */
  4206. + #ifndef NeXT
  4207.     if (count != defined_global_sym_count
  4208.         + undefined_global_sym_count + global_indirect_count)
  4209. + #else
  4210. +   if (count != defined_global_sym_count
  4211. +       + undefined_global_sym_count)
  4212. + #endif
  4213.       fatal ("internal error");
  4214.   
  4215.     /* Write out the relocations of all files, remembered from copy_text.  */
  4216.   
  4217. !   lseek (outdesc, output_trel_offset, 0);
  4218. !   each_full_file (coptxtrel, 0);
  4219.   
  4220.     if (trace_files)
  4221.       fprintf (stderr, "\nWriting data relocation:\n\n");
  4222.   
  4223. !   lseek (outdesc, output_drel_offset, 0);
  4224. !   each_full_file (copdatrel, 0);
  4225.   
  4226.     if (trace_files)
  4227.       fprintf (stderr, "\n");
  4228. ***************
  4229. *** 3853,3862 ****
  4230.        struct file_entry *entry;
  4231.   {
  4232.     register struct relocation_info *p, *end;
  4233. !   register int reloc = entry->text_start_address;
  4234.   
  4235.     p = entry->textrel;
  4236. !   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  4237.     while (p < end)
  4238.       {
  4239.         RELOC_ADDRESS(p) += reloc;
  4240. --- 5014,5023 ----
  4241.        struct file_entry *entry;
  4242.   {
  4243.     register struct relocation_info *p, *end;
  4244. !   register int reloc = entry->text_start_address - text_start;
  4245.   
  4246.     p = entry->textrel;
  4247. !   end = (struct relocation_info *) (entry->text_reloc_size + (char *) p);
  4248.     while (p < end)
  4249.       {
  4250.         RELOC_ADDRESS(p) += reloc;
  4251. ***************
  4252. *** 3868,3874 ****
  4253.                     (((char *)entry->symbols) + symindex))
  4254.                    ->n_un.n_name));
  4255.   
  4256. !       if (symindex >= entry->header.a_syms)
  4257.           fatal_with_file ("relocation symbolnum out of range in ", entry);
  4258.   
  4259.   #ifdef N_INDR
  4260. --- 5029,5035 ----
  4261.                     (((char *)entry->symbols) + symindex))
  4262.                    ->n_un.n_name));
  4263.   
  4264. !       if (symindex >= entry->syms_size)
  4265.           fatal_with_file ("relocation symbolnum out of range in ", entry);
  4266.   
  4267.   #ifdef N_INDR
  4268. ***************
  4269. *** 3883,3889 ****
  4270.         if (symptr->defined)
  4271.           {
  4272.             RELOC_EXTERN_P(p) = 0;
  4273. !           RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  4274.   #ifdef RELOC_ADD_EXTRA
  4275.             /* If we aren't going to be adding in the value in
  4276.                memory on the next pass of the loader, then we need
  4277. --- 5044,5050 ----
  4278.         if (symptr->defined)
  4279.           {
  4280.             RELOC_EXTERN_P(p) = 0;
  4281. !           RELOC_SYMBOL(p) = (symptr->defined & ~N_EXT);
  4282.   #ifdef RELOC_ADD_EXTRA
  4283.             /* If we aren't going to be adding in the value in
  4284.                memory on the next pass of the loader, then we need
  4285. ***************
  4286. *** 3896,3909 ****
  4287.         else
  4288.           /* Debugger symbols come first, so have to start this
  4289.              after them.  */
  4290.             RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4291.                    - defined_global_sym_count
  4292.                    - undefined_global_sym_count
  4293.                    - global_indirect_count);
  4294.       }
  4295.         p++;
  4296.       }
  4297. !   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  4298.   }
  4299.   
  4300.   void
  4301. --- 5057,5083 ----
  4302.         else
  4303.           /* Debugger symbols come first, so have to start this
  4304.              after them.  */
  4305. + #ifndef NeXT
  4306.             RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4307.                    - defined_global_sym_count
  4308.                    - undefined_global_sym_count
  4309.                    - global_indirect_count);
  4310. + #else
  4311. +           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4312. +                  - defined_global_sym_count
  4313. +                  - undefined_global_sym_count);
  4314. + #endif
  4315.       }
  4316.         p++;
  4317.       }
  4318. ! #ifdef MACH_O
  4319. !   if (output_file_type == IS_MACH_O)
  4320. !     generate_mach_o_relocations(entry->textrel,
  4321. !                 entry->text_reloc_size / sizeof (struct relocation_info));
  4322. ! #endif
  4323. !   mywrite (entry->textrel, 1, entry->text_reloc_size, outdesc);
  4324.   }
  4325.   
  4326.   void
  4327. ***************
  4328. *** 3913,3923 ****
  4329.     register struct relocation_info *p, *end;
  4330.     /* Relocate the address of the relocation.
  4331.        Old address is relative to start of the input file's data section.
  4332. !      New address is relative to start of the output file's data section.  */
  4333. !   register int reloc = entry->data_start_address - text_size;
  4334.   
  4335.     p = entry->datarel;
  4336. !   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  4337.     while (p < end)
  4338.       {
  4339.         RELOC_ADDRESS(p) += reloc;
  4340. --- 5087,5100 ----
  4341.     register struct relocation_info *p, *end;
  4342.     /* Relocate the address of the relocation.
  4343.        Old address is relative to start of the input file's data section.
  4344. !      New address is relative to start of the output file's data section.
  4345.   
  4346. +      So the amount we need to relocate it by is the offset of this
  4347. +      input file's data section within the output file's data section.  */
  4348. +   register int reloc = entry->data_start_address - data_start;
  4349.     p = entry->datarel;
  4350. !   end = (struct relocation_info *) (entry->data_reloc_size + (char *) p);
  4351.     while (p < end)
  4352.       {
  4353.         RELOC_ADDRESS(p) += reloc;
  4354. ***************
  4355. *** 3930,3936 ****
  4356.                    ->n_un.n_name));
  4357.         int symtype;
  4358.   
  4359. !       if (symindex >= entry->header.a_syms)
  4360.           fatal_with_file ("relocation symbolnum out of range in ", entry);
  4361.   
  4362.   #ifdef N_INDR
  4363. --- 5107,5113 ----
  4364.                    ->n_un.n_name));
  4365.         int symtype;
  4366.   
  4367. !       if (symindex >= entry->syms_size)
  4368.           fatal_with_file ("relocation symbolnum out of range in ", entry);
  4369.   
  4370.   #ifdef N_INDR
  4371. ***************
  4372. *** 3939,3945 ****
  4373.           symptr = (symbol *) symptr->value;
  4374.   #endif
  4375.   
  4376. !        symtype = symptr->defined & N_TYPE;
  4377.   
  4378.         if (force_common_definition
  4379.             || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4380. --- 5116,5122 ----
  4381.           symptr = (symbol *) symptr->value;
  4382.   #endif
  4383.   
  4384. !       symtype = symptr->defined & ~N_EXT;
  4385.   
  4386.         if (force_common_definition
  4387.             || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4388. ***************
  4389. *** 3950,3955 ****
  4390. --- 5127,5133 ----
  4391.         else
  4392.           /* Debugger symbols come first, so have to start this
  4393.              after them.  */
  4394. + #ifndef NeXT
  4395.           RELOC_SYMBOL(p)
  4396.             = (((symbol *)
  4397.             (((struct nlist *)
  4398. ***************
  4399. *** 3959,3985 ****
  4400.            + nsyms - defined_global_sym_count
  4401.            - undefined_global_sym_count
  4402.            - global_indirect_count);
  4403.       }
  4404.         p++;
  4405.       }
  4406. !   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4407.   }
  4408.   
  4409.   void write_file_syms ();
  4410.   void write_string_table ();
  4411.   
  4412. - /* Offsets and current lengths of symbol and string tables in output file. */
  4413. - int symbol_table_offset;
  4414. - int symbol_table_len;
  4415. - /* Address in output file where string table starts.  */
  4416. - int string_table_offset;
  4417. - /* Offset within string table
  4418. -    where the strings in `strtab_vector' should be written.  */
  4419. - int string_table_len;
  4420.   /* Total size of string table strings allocated so far,
  4421.      including strings in `strtab_vector'.  */
  4422.   int strtab_size;
  4423. --- 5137,5167 ----
  4424.            + nsyms - defined_global_sym_count
  4425.            - undefined_global_sym_count
  4426.            - global_indirect_count);
  4427. + #else
  4428. +         RELOC_SYMBOL(p)
  4429. +           = (((symbol *)
  4430. +           (((struct nlist *)
  4431. +             (((char *)entry->symbols) + symindex))
  4432. +            ->n_un.n_name))
  4433. +          ->def_count
  4434. +          + nsyms - defined_global_sym_count
  4435. +          - undefined_global_sym_count);
  4436. + #endif
  4437.       }
  4438.         p++;
  4439.       }
  4440. ! #ifdef MACH_O
  4441. !   if (output_file_type == IS_MACH_O)
  4442. !     generate_mach_o_relocations(entry->datarel,
  4443. !                 entry->data_reloc_size / sizeof (struct relocation_info));
  4444. ! #endif
  4445. !   mywrite (entry->datarel, 1, entry->data_reloc_size, outdesc);
  4446.   }
  4447.   
  4448.   void write_file_syms ();
  4449.   void write_string_table ();
  4450.   
  4451.   /* Total size of string table strings allocated so far,
  4452.      including strings in `strtab_vector'.  */
  4453.   int strtab_size;
  4454. ***************
  4455. *** 4022,4028 ****
  4456.   {
  4457.     register int i;
  4458.   
  4459. !   lseek (outdesc, string_table_offset + string_table_len, 0);
  4460.   
  4461.     if (!outstream)
  4462.       outstream = fdopen (outdesc, "w");
  4463. --- 5204,5210 ----
  4464.   {
  4465.     register int i;
  4466.   
  4467. !   lseek (outdesc, output_strs_offset + output_strs_size, 0);
  4468.   
  4469.     if (!outstream)
  4470.       outstream = fdopen (outdesc, "w");
  4471. ***************
  4472. *** 4030,4036 ****
  4473.     for (i = 0; i < strtab_index; i++)
  4474.       {
  4475.         fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4476. !       string_table_len += strtab_lens[i];
  4477.       }
  4478.   
  4479.     fflush (outstream);
  4480. --- 5212,5218 ----
  4481.     for (i = 0; i < strtab_index; i++)
  4482.       {
  4483.         fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4484. !       output_strs_size += strtab_lens[i];
  4485.       }
  4486.   
  4487.     fflush (outstream);
  4488. ***************
  4489. *** 4054,4063 ****
  4490. --- 5236,5251 ----
  4491.        extra struct for each indirect symbol to hold the extra reference
  4492.        following. */
  4493.     struct nlist *buf
  4494. + #ifndef NeXT
  4495.       = (struct nlist *) alloca ((defined_global_sym_count
  4496.                   + undefined_global_sym_count
  4497.                   + global_indirect_count)
  4498.                      * sizeof (struct nlist));
  4499. + #else
  4500. +     = (struct nlist *) alloca ((defined_global_sym_count
  4501. +                 + undefined_global_sym_count)
  4502. +                    * sizeof (struct nlist));
  4503. + #endif
  4504.     /* Pointer for storing into BUF.  */
  4505.     register struct nlist *bufp = buf;
  4506.   
  4507. ***************
  4508. *** 4064,4073 ****
  4509.     /* Size of string table includes the bytes that store the size.  */
  4510.     strtab_size = sizeof strtab_size;
  4511.   
  4512. !   symbol_table_offset = N_SYMOFF (outheader);
  4513. !   symbol_table_len = 0;
  4514. !   string_table_offset = N_STROFF (outheader);
  4515. !   string_table_len = strtab_size;
  4516.   
  4517.     if (strip_symbols == STRIP_ALL)
  4518.       return;
  4519. --- 5252,5259 ----
  4520.     /* Size of string table includes the bytes that store the size.  */
  4521.     strtab_size = sizeof strtab_size;
  4522.   
  4523. !   output_syms_size = 0;
  4524. !   output_strs_size = strtab_size;
  4525.   
  4526.     if (strip_symbols == STRIP_ALL)
  4527.       return;
  4528. ***************
  4529. *** 4096,4102 ****
  4530. --- 5282,5292 ----
  4531.         {
  4532.       struct nlist nl;
  4533.   
  4534. + #ifdef N_SECT
  4535. +     nl.n_sect = 0;
  4536. + #else
  4537.       nl.n_other = 0;
  4538. + #endif
  4539.       nl.n_desc = 0;
  4540.   
  4541.       /* Compute a `struct nlist' for the symbol.  */
  4542. ***************
  4543. *** 4111,4118 ****
  4544.           /* If the target of an indirect symbol has been
  4545.              defined and we are outputting an executable,
  4546.              resolve the indirection; it's no longer needed */
  4547. !         if (!relocatable_output
  4548. !             && ((sp->defined & N_TYPE) == N_INDR)
  4549.               && (((symbol *) sp->value)->defined > 1))
  4550.             {
  4551.               symbol *newsp = (symbol *) sp->value;
  4552. --- 5301,5308 ----
  4553.           /* If the target of an indirect symbol has been
  4554.              defined and we are outputting an executable,
  4555.              resolve the indirection; it's no longer needed */
  4556. !         if (output_style != OUTPUT_RELOCATABLE
  4557. !             && ((sp->defined & ~N_EXT) == N_INDR)
  4558.               && (((symbol *) sp->value)->defined > 1))
  4559.             {
  4560.               symbol *newsp = (symbol *) sp->value;
  4561. ***************
  4562. *** 4152,4188 ****
  4563.           *bufp++ = nl;
  4564.           syms_written++;
  4565.           if (nl.n_type == (N_INDR | N_EXT))
  4566.             {
  4567.           struct nlist xtra_ref;
  4568.           xtra_ref.n_type == N_EXT | N_UNDF;
  4569.           xtra_ref.n_un.n_strx
  4570.             = assign_string_table_index (((symbol *) sp->value)->name);
  4571.           xtra_ref.n_other = 0;
  4572.           xtra_ref.n_desc = 0;
  4573.           xtra_ref.n_value = 0;
  4574.           *bufp++ = xtra_ref;
  4575.           syms_written++;
  4576.             }
  4577.         }
  4578.         }
  4579.   
  4580.     /* Output the buffer full of `struct nlist's.  */
  4581.   
  4582. !   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4583.     mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4584. !   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4585.   
  4586.     if (syms_written != nsyms)
  4587.       fatal ("internal error: wrong number of symbols written into output file", 0);
  4588.   
  4589. !   if (symbol_table_offset + symbol_table_len != string_table_offset)
  4590. !     fatal ("internal error: inconsistent symbol table length", 0);
  4591.   
  4592. !   /* Now the total string table size is known, so write it.
  4593. !      We are already positioned at the right place in the file.  */
  4594.   
  4595. -   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  4596.     /* Write the strings for the global symbols.  */
  4597.   
  4598.     write_string_table ();
  4599. --- 5342,5389 ----
  4600.           *bufp++ = nl;
  4601.           syms_written++;
  4602.           if (nl.n_type == (N_INDR | N_EXT))
  4603. + #ifndef NeXT
  4604.             {
  4605.           struct nlist xtra_ref;
  4606.           xtra_ref.n_type == N_EXT | N_UNDF;
  4607.           xtra_ref.n_un.n_strx
  4608.             = assign_string_table_index (((symbol *) sp->value)->name);
  4609. + #ifdef N_SECT
  4610. +         xtra_ref.n_sect = 0;
  4611. + #else
  4612.           xtra_ref.n_other = 0;
  4613. + #endif
  4614.           xtra_ref.n_desc = 0;
  4615.           xtra_ref.n_value = 0;
  4616.           *bufp++ = xtra_ref;
  4617.           syms_written++;
  4618.             }
  4619. + #else
  4620. +         nl.n_value = assign_string_table_index (((symbol *) sp->value)->name);
  4621. + #endif
  4622.         }
  4623.         }
  4624.   
  4625. + #ifdef MACH_O
  4626. +   if (output_file_type == IS_MACH_O)
  4627. +     generate_mach_o_symbols(buf, bufp - buf);
  4628. + #endif
  4629.     /* Output the buffer full of `struct nlist's.  */
  4630.   
  4631. !   lseek (outdesc, output_syms_offset + output_syms_size, 0);
  4632.     mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4633. !   output_syms_size += sizeof (struct nlist) * (bufp - buf);
  4634.   
  4635.     if (syms_written != nsyms)
  4636.       fatal ("internal error: wrong number of symbols written into output file", 0);
  4637.   
  4638. !   /* Now the total string table size is known, so write it into the
  4639. !      first word of the string table.  */
  4640.   
  4641. !   lseek (outdesc, output_strs_offset, 0);
  4642. !   mywrite (&strtab_size, sizeof (int), 1, outdesc);
  4643.   
  4644.     /* Write the strings for the global symbols.  */
  4645.   
  4646.     write_string_table ();
  4647. ***************
  4648. *** 4200,4215 ****
  4649.        int *syms_written_addr;
  4650.   {
  4651.     register struct nlist *p = entry->symbols;
  4652. !   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  4653.   
  4654.     /* Buffer to accumulate all the syms before writing them.
  4655.        It has one extra slot for the local symbol we generate here.  */
  4656.     struct nlist *buf
  4657. !     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  4658.     register struct nlist *bufp = buf;
  4659.   
  4660.     /* Upper bound on number of syms to be written here.  */
  4661. !   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  4662.   
  4663.     /* Make tables that record, for each symbol, its name and its name's length.
  4664.        The elements are filled in by `assign_string_table_index'.  */
  4665. --- 5401,5416 ----
  4666.        int *syms_written_addr;
  4667.   {
  4668.     register struct nlist *p = entry->symbols;
  4669. !   register struct nlist *end = p + entry->syms_size / sizeof (struct nlist);
  4670.   
  4671.     /* Buffer to accumulate all the syms before writing them.
  4672.        It has one extra slot for the local symbol we generate here.  */
  4673.     struct nlist *buf
  4674. !     = (struct nlist *) alloca (entry->syms_size + sizeof (struct nlist));
  4675.     register struct nlist *bufp = buf;
  4676.   
  4677.     /* Upper bound on number of syms to be written here.  */
  4678. !   int max_syms = (entry->syms_size / sizeof (struct nlist)) + 1;
  4679.   
  4680.     /* Make tables that record, for each symbol, its name and its name's length.
  4681.        The elements are filled in by `assign_string_table_index'.  */
  4682. ***************
  4683. *** 4228,4234 ****
  4684. --- 5429,5439 ----
  4685.         nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  4686.         nl.n_value = entry->text_start_address;
  4687.         nl.n_desc = 0;
  4688. + #ifdef N_SECT
  4689. +       nl.n_sect = 0;
  4690. + #else
  4691.         nl.n_other = 0;
  4692. + #endif
  4693.         *bufp++ = nl;
  4694.         (*syms_written_addr)++;
  4695.         entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  4696. ***************
  4697. *** 4236,4242 ****
  4698.   
  4699.     /* Read the file's string table.  */
  4700.   
  4701. !   entry->strings = (char *) alloca (entry->string_size);
  4702.     read_entry_strings (file_open (entry), entry);
  4703.   
  4704.     for (; p < end; p++)
  4705. --- 5441,5447 ----
  4706.   
  4707.     /* Read the file's string table.  */
  4708.   
  4709. !   entry->strings = (char *) alloca (entry->strs_size);
  4710.     read_entry_strings (file_open (entry), entry);
  4711.   
  4712.     for (; p < end; p++)
  4713. ***************
  4714. *** 4251,4257 ****
  4715.                   /* types of symbols are never written */
  4716.                   /* globally, though they are stored */
  4717.                   /* globally.  */
  4718. !         write = relocatable_output;
  4719.         else if (!(type & (N_STAB | N_EXT)))
  4720.           /* ordinary local symbol */
  4721.       write = ((discard_locals != DISCARD_ALL)
  4722. --- 5456,5462 ----
  4723.                   /* types of symbols are never written */
  4724.                   /* globally, though they are stored */
  4725.                   /* globally.  */
  4726. !         write = output_style == OUTPUT_RELOCATABLE;
  4727.         else if (!(type & (N_STAB | N_EXT)))
  4728.           /* ordinary local symbol */
  4729.       write = ((discard_locals != DISCARD_ALL)
  4730. ***************
  4731. *** 4278,4288 ****
  4732.       }
  4733.       }
  4734.   
  4735.     /* All the symbols are now in BUF; write them.  */
  4736.   
  4737. !   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4738.     mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4739. !   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4740.   
  4741.     /* Write the string-table data for the symbols just written,
  4742.        using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4743. --- 5483,5498 ----
  4744.       }
  4745.       }
  4746.   
  4747. + #ifdef MACH_O
  4748. +   if (output_file_type == IS_MACH_O)
  4749. +     generate_mach_o_symbols(buf, bufp - buf);
  4750. + #endif
  4751.     /* All the symbols are now in BUF; write them.  */
  4752.   
  4753. !   lseek (outdesc, output_syms_offset + output_syms_size, 0);
  4754.     mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4755. !   output_syms_size += sizeof (struct nlist) * (bufp - buf);
  4756.   
  4757.     /* Write the string-table data for the symbols just written,
  4758.        using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4759. ***************
  4760. *** 4300,4305 ****
  4761. --- 5510,5516 ----
  4762.   void
  4763.   write_symsegs ()
  4764.   {
  4765. +   lseek (outdesc, output_symseg_offset, 0);
  4766.     each_file (write_file_symseg, 0);
  4767.   }
  4768.   
  4769. ***************
  4770. *** 4309,4320 ****
  4771.   {
  4772.     char buffer[4096];
  4773.     struct symbol_root root;
  4774. !   int indesc;
  4775. !   int len;
  4776.   
  4777. !   if (entry->symseg_offset == 0)
  4778.       return;
  4779.   
  4780.     /* This entry has a symbol segment.  Read the root of the segment.  */
  4781.   
  4782.     indesc = file_open (entry);
  4783. --- 5520,5532 ----
  4784.   {
  4785.     char buffer[4096];
  4786.     struct symbol_root root;
  4787. !   int indesc, len, total;
  4788.   
  4789. !   if (entry->symseg_size == 0)
  4790.       return;
  4791.   
  4792. +   output_symseg_size += entry->symseg_size;
  4793.     /* This entry has a symbol segment.  Read the root of the segment.  */
  4794.   
  4795.     indesc = file_open (entry);
  4796. ***************
  4797. *** 4325,4334 ****
  4798.     /* Store some relocation info into the root.  */
  4799.   
  4800.     root.ldsymoff = entry->local_syms_offset;
  4801. !   root.textrel = entry->text_start_address;
  4802. !   root.datarel = entry->data_start_address - entry->header.a_text;
  4803. !   root.bssrel = entry->bss_start_address
  4804. !     - entry->header.a_text - entry->header.a_data;
  4805.     root.databeg = entry->data_start_address - root.datarel;
  4806.     root.bssbeg = entry->bss_start_address - root.bssrel;
  4807.   
  4808. --- 5537,5545 ----
  4809.     /* Store some relocation info into the root.  */
  4810.   
  4811.     root.ldsymoff = entry->local_syms_offset;
  4812. !   root.textrel = entry->text_start_address - entry->orig_text_address;
  4813. !   root.datarel = entry->data_start_address - entry->orig_data_address;
  4814. !   root.bssrel = entry->bss_start_address - entry->orig_bss_address;
  4815.     root.databeg = entry->data_start_address - root.datarel;
  4816.     root.bssbeg = entry->bss_start_address - root.bssrel;
  4817.   
  4818. ***************
  4819. *** 4338,4414 ****
  4820.   
  4821.     /* Copy the rest of the symbol segment unchanged.  */
  4822.   
  4823. !   if (entry->superfile)
  4824. !     {
  4825. !       /* Library member: number of bytes to copy is determined
  4826. !      from the member's total size.  */
  4827. !       int total = entry->total_size - entry->symseg_offset - sizeof root;
  4828.   
  4829. !       while (total > 0)
  4830. !     {
  4831. !       len = read (indesc, buffer, min (sizeof buffer, total));
  4832. !       if (len != min (sizeof buffer, total))
  4833. !         fatal_with_file ("premature end of file in symbol segment of ", entry);
  4834. !       total -= len;
  4835. !       mywrite (buffer, len, 1, outdesc);
  4836. !     }
  4837. !     }
  4838. !   else
  4839. !     {
  4840. !       /* A separate file: copy until end of file.  */
  4841. !       while (len = read (indesc, buffer, sizeof buffer))
  4842. !     {
  4843. !       mywrite (buffer, len, 1, outdesc);
  4844. !       if (len < sizeof buffer)
  4845. !         break;
  4846. !     }
  4847.       }
  4848.   
  4849.     file_close ();
  4850.   }
  4851.   
  4852.   /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4853.   
  4854.   void
  4855.   symtab_init ()
  4856.   {
  4857. ! #ifndef nounderscore
  4858. !   edata_symbol = getsym ("_edata");
  4859. !   etext_symbol = getsym ("_etext");
  4860. !   end_symbol = getsym ("_end");
  4861. ! #else
  4862. !   edata_symbol = getsym ("edata");
  4863. !   etext_symbol = getsym ("etext");
  4864. !   end_symbol = getsym ("end");
  4865. ! #endif
  4866.   
  4867.   #ifdef sun
  4868.     {
  4869. !     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  4870. !     dynamic_symbol->defined = N_ABS | N_EXT;
  4871. !     dynamic_symbol->referenced = 1;
  4872. !     dynamic_symbol->value = 0;
  4873.     }
  4874. ! #endif
  4875. ! #ifdef sequent
  4876. !   {
  4877. !     symbol *_387_flt_symbol = getsym ("_387_flt");
  4878. !     _387_flt_symbol->defined = N_ABS | N_EXT;
  4879. !     _387_flt_symbol->referenced = 1;
  4880. !     _387_flt_symbol->value = 0;
  4881. !   }
  4882. ! #endif
  4883. !   edata_symbol->defined = N_DATA | N_EXT;
  4884. !   etext_symbol->defined = N_TEXT | N_EXT;
  4885. !   end_symbol->defined = N_BSS | N_EXT;
  4886. !   edata_symbol->referenced = 1;
  4887. !   etext_symbol->referenced = 1;
  4888. !   end_symbol->referenced = 1;
  4889.   }
  4890.   
  4891.   /* Compute the hash code for symbol name KEY.  */
  4892. --- 5549,5646 ----
  4893.   
  4894.     /* Copy the rest of the symbol segment unchanged.  */
  4895.   
  4896. !   total = entry->symseg_size - sizeof root;
  4897.   
  4898. !   while (total > 0)
  4899. !     {
  4900. !       len = read (indesc, buffer, min (sizeof buffer, total));
  4901. !       if (len != min (sizeof buffer, total))
  4902. !     fatal_with_file ("premature end of file in symbol segment of ", entry);
  4903. !       total -= len;
  4904. !       mywrite (buffer, len, 1, outdesc);
  4905.       }
  4906.   
  4907.     file_close ();
  4908.   }
  4909.   
  4910. + /* Define a special symbol (etext, edata, or end).  NAME is the
  4911. +    name of the symbol, with a leading underscore (whether or not this
  4912. +    system uses such underscores).  TYPE is its type (e.g. N_DATA | N_EXT).
  4913. +    Store a symbol * for the symbol in *SYM if SYM is non-NULL.  */
  4914. + static void
  4915. + symbol_define (name, type, sym)
  4916. +      /* const */ char *name;
  4917. +      int type;
  4918. +      symbol **sym;
  4919. + {
  4920. +   symbol *thesym;
  4921. + #if defined(nounderscore)
  4922. +   /* Skip the leading underscore.  */
  4923. +   name++;
  4924. + #endif
  4925. +   thesym = getsym (name);
  4926. +   if (thesym->defined)
  4927. +     {
  4928. +       /* The symbol is defined in some input file.  Don't mess with it.  */
  4929. +       if (sym)
  4930. +     *sym = 0;
  4931. +     }
  4932. +   else
  4933. +     {
  4934. +       if (thesym->referenced)
  4935. +     /* The symbol was not defined, and we are defining it now.  */
  4936. +     undefined_global_sym_count--;
  4937. +       thesym->defined = type;
  4938. +       thesym->referenced = 1;
  4939. +       if (sym)
  4940. +     *sym = thesym;
  4941. +     }
  4942. + }
  4943.   /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4944.   
  4945.   void
  4946.   symtab_init ()
  4947.   {
  4948. !   symbol_define ("_edata", N_DATA | N_EXT, &edata_symbol);
  4949. !   symbol_define ("_etext", N_TEXT | N_EXT, &etext_symbol);
  4950. !   symbol_define ("_end", N_BSS | N_EXT, &end_symbol);
  4951. !   /* Either _edata or __edata (C names) is OK as far as ANSI is concerned
  4952. !      (see section 4.1.2.1).  In general, it is best to use __foo and
  4953. !      not worry about the confusing rules for the _foo namespace.
  4954. !      But HPUX 7.0 uses _edata, so we might as weel be consistent.  */
  4955. !   symbol_define ("__edata", N_DATA | N_EXT, &edata_symbol_alt);
  4956. !   symbol_define ("__etext", N_TEXT | N_EXT, &etext_symbol_alt);
  4957. !   symbol_define ("__end", N_BSS | N_EXT, &end_symbol_alt);
  4958.   
  4959.   #ifdef sun
  4960.     {
  4961. !     symbol *dynamic_symbol;
  4962. !     symbol_define ("__DYNAMIC", N_ABS | N_EXT, &dynamic_symbol);
  4963. !     if (dynamic_symbol)
  4964. !       dynamic_symbol->value = 0;
  4965. !   }
  4966. ! #endif
  4967. ! #ifdef sequent
  4968. !   {
  4969. !     symbol *i387_flt_symbol;
  4970. !     symbol_define ("_387_flt", N_ABS | N_EXT, &i387_flt_symbol);
  4971. !     if (i387_flt_symbol)
  4972. !       i387_flt_symbol->value = 0;
  4973. !   }
  4974. ! #endif
  4975. ! #ifdef NeXT
  4976. !   {
  4977. !     symbol *shlib_init_symbol;
  4978. !     symbol_define ("__shared_library_initialization", N_UNDF | N_EXT, &shlib_init_symbol);
  4979. !     if (shlib_init_symbol)
  4980. !       shlib_init_symbol->max_common_size = sizeof (long int);
  4981.     }
  4982. ! #endif
  4983.   }
  4984.   
  4985.   /* Compute the hash code for symbol name KEY.  */
  4986. ***************
  4987. *** 4495,4500 ****
  4988. --- 5727,5754 ----
  4989.     return 0;
  4990.   }
  4991.   
  4992. + /* Report a usage error.
  4993. +    Like fatal except prints a usage summary.  */
  4994. + void
  4995. + usage (string, arg)
  4996. +      char *string, *arg;
  4997. + {
  4998. +   if (string)
  4999. +     {
  5000. +       fprintf (stderr, "%s: ", progname);
  5001. +       fprintf (stderr, string, arg);
  5002. +       fprintf (stderr, "\n");
  5003. +     }
  5004. +   fprintf (stderr, "\
  5005. + Usage: %s [-d] [-dc] [-dp] [-e symbol] [-l lib] [-n] [-noinhibit-exec]\n\
  5006. +        [-nostdlib] [-o file] [-r] [-s] [-t] [-u symbol] [-x] [-y symbol]\n\
  5007. +        [-z] [-A file] [-Bstatic] [-D size] [-L libdir] [-M] [-N]\n\
  5008. +        [-S] [-T[{text,data}] addr] [-V prefix] [-X] [file...]\n",
  5009. +        progname);
  5010. +   exit (1);
  5011. + }
  5012.   /* Report a fatal error.
  5013.      STRING is a printf format string and ARG is one arg for it.  */
  5014.   
  5015. ***************
  5016. *** 4502,4508 ****
  5017.   fatal (string, arg)
  5018.        char *string, *arg;
  5019.   {
  5020. !   fprintf (stderr, "ld: ");
  5021.     fprintf (stderr, string, arg);
  5022.     fprintf (stderr, "\n");
  5023.     exit (1);
  5024. --- 5756,5762 ----
  5025.   fatal (string, arg)
  5026.        char *string, *arg;
  5027.   {
  5028. !   fprintf (stderr, "%s: ", progname);
  5029.     fprintf (stderr, string, arg);
  5030.     fprintf (stderr, "\n");
  5031.     exit (1);
  5032. ***************
  5033. *** 4516,4522 ****
  5034.        char *string;
  5035.        struct file_entry *entry;
  5036.   {
  5037. !   fprintf (stderr, "ld: ");
  5038.     fprintf (stderr, string);
  5039.     print_file_name (entry, stderr);
  5040.     fprintf (stderr, "\n");
  5041. --- 5770,5776 ----
  5042.        char *string;
  5043.        struct file_entry *entry;
  5044.   {
  5045. !   fprintf (stderr, "%s: ", progname);
  5046.     fprintf (stderr, string);
  5047.     print_file_name (entry, stderr);
  5048.     fprintf (stderr, "\n");
  5049. ***************
  5050. *** 4646,4656 ****
  5051.   
  5052.   /* Like malloc but get fatal error if memory is exhausted.  */
  5053.   
  5054. ! int
  5055.   xmalloc (size)
  5056.        int size;
  5057.   {
  5058. !   register int result = malloc (size);
  5059.     if (!result)
  5060.       fatal ("virtual memory exhausted", 0);
  5061.     return result;
  5062. --- 5900,5910 ----
  5063.   
  5064.   /* Like malloc but get fatal error if memory is exhausted.  */
  5065.   
  5066. ! char *
  5067.   xmalloc (size)
  5068.        int size;
  5069.   {
  5070. !   register char *result = malloc (size);
  5071.     if (!result)
  5072.       fatal ("virtual memory exhausted", 0);
  5073.     return result;
  5074. ***************
  5075. *** 4658,4669 ****
  5076.   
  5077.   /* Like realloc but get fatal error if memory is exhausted.  */
  5078.   
  5079. ! int
  5080.   xrealloc (ptr, size)
  5081.        char *ptr;
  5082.        int size;
  5083.   {
  5084. !   register int result = realloc (ptr, size);
  5085.     if (!result)
  5086.       fatal ("virtual memory exhausted", 0);
  5087.     return result;
  5088. --- 5912,5923 ----
  5089.   
  5090.   /* Like realloc but get fatal error if memory is exhausted.  */
  5091.   
  5092. ! char *
  5093.   xrealloc (ptr, size)
  5094.        char *ptr;
  5095.        int size;
  5096.   {
  5097. !   register char *result = realloc (ptr, size);
  5098.     if (!result)
  5099.       fatal ("virtual memory exhausted", 0);
  5100.     return result;
  5101. ***************
  5102. *** 4690,4693 ****
  5103. --- 5944,5955 ----
  5104.     return (4096);
  5105.   }
  5106.   
  5107. + #endif
  5108. + #if defined(sun) && defined(sparc)
  5109. + int
  5110. + getpagesize ()
  5111. + {
  5112. +   return 8192;
  5113. + }
  5114.   #endif
  5115.