home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / bfd / aoutx.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-09  |  146.3 KB  |  5,101 lines

  1. /* BFD semi-generic back-end for a.out binaries.
  2.    Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /*
  22. SECTION
  23.     a.out backends
  24.  
  25.  
  26. DESCRIPTION
  27.  
  28.     BFD supports a number of different flavours of a.out format,
  29.     though the major differences are only the sizes of the
  30.     structures on disk, and the shape of the relocation
  31.     information.
  32.  
  33.     The support is split into a basic support file @file{aoutx.h}
  34.     and other files which derive functions from the base. One
  35.     derivation file is @file{aoutf1.h} (for a.out flavour 1), and
  36.     adds to the basic a.out functions support for sun3, sun4, 386
  37.     and 29k a.out files, to create a target jump vector for a
  38.     specific target.
  39.  
  40.     This information is further split out into more specific files
  41.     for each machine, including @file{sunos.c} for sun3 and sun4,
  42.     @file{newsos3.c} for the Sony NEWS, and @file{demo64.c} for a
  43.     demonstration of a 64 bit a.out format.
  44.  
  45.     The base file @file{aoutx.h} defines general mechanisms for
  46.     reading and writing records to and from disk and various
  47.     other methods which BFD requires. It is included by
  48.     @file{aout32.c} and @file{aout64.c} to form the names
  49.     <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc.
  50.  
  51.     As an example, this is what goes on to make the back end for a
  52.     sun4, from @file{aout32.c}:
  53.  
  54. |    #define ARCH_SIZE 32
  55. |    #include "aoutx.h"
  56.  
  57.     Which exports names:
  58.  
  59. |    ...
  60. |    aout_32_canonicalize_reloc
  61. |    aout_32_find_nearest_line
  62. |    aout_32_get_lineno
  63. |    aout_32_get_reloc_upper_bound
  64. |    ...
  65.  
  66.     from @file{sunos.c}:
  67.  
  68. |    #define TARGET_NAME "a.out-sunos-big"
  69. |    #define VECNAME    sunos_big_vec
  70. |    #include "aoutf1.h"
  71.  
  72.     requires all the names from @file{aout32.c}, and produces the jump vector
  73.  
  74. |    sunos_big_vec
  75.  
  76.     The file @file{host-aout.c} is a special case.  It is for a large set
  77.     of hosts that use ``more or less standard'' a.out files, and
  78.     for which cross-debugging is not interesting.  It uses the
  79.     standard 32-bit a.out support routines, but determines the
  80.     file offsets and addresses of the text, data, and BSS
  81.     sections, the machine architecture and machine type, and the
  82.     entry point address, in a host-dependent manner.  Once these
  83.     values have been determined, generic code is used to handle
  84.     the  object file.
  85.  
  86.     When porting it to run on a new system, you must supply:
  87.  
  88. |        HOST_PAGE_SIZE
  89. |        HOST_SEGMENT_SIZE
  90. |        HOST_MACHINE_ARCH       (optional)
  91. |        HOST_MACHINE_MACHINE    (optional)
  92. |        HOST_TEXT_START_ADDR
  93. |        HOST_STACK_END_ADDR
  94.  
  95.     in the file @file{../include/sys/h-@var{XXX}.h} (for your host).  These
  96.     values, plus the structures and macros defined in @file{a.out.h} on
  97.     your host system, will produce a BFD target that will access
  98.     ordinary a.out files on your host. To configure a new machine
  99.     to use @file{host-aout.c}, specify:
  100.  
  101. |    TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
  102. |    TDEPFILES= host-aout.o trad-core.o
  103.  
  104.     in the @file{config/@var{XXX}.mt} file, and modify @file{configure.in}
  105.     to use the
  106.     @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
  107.     configuration is selected.
  108.  
  109. */
  110.  
  111. /* Some assumptions:
  112.    * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
  113.      Doesn't matter what the setting of WP_TEXT is on output, but it'll
  114.      get set on input.
  115.    * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
  116.    * Any BFD with both flags clear is OMAGIC.
  117.    (Just want to make these explicit, so the conditions tested in this
  118.    file make sense if you're more familiar with a.out than with BFD.)  */
  119.  
  120. #define KEEPIT flags
  121. #define KEEPITTYPE int
  122.  
  123. #include <string.h>        /* For strchr and friends */
  124. #include "bfd.h"
  125. #include <sysdep.h>
  126. #include "bfdlink.h"
  127.  
  128. #include "libaout.h"
  129. #include "libbfd.h"
  130. #include "aout/aout64.h"
  131. #include "aout/stab_gnu.h"
  132. #include "aout/ar.h"
  133.  
  134. static boolean aout_get_external_symbols PARAMS ((bfd *));
  135. static boolean translate_from_native_sym_flags
  136.   PARAMS ((bfd *, aout_symbol_type *));
  137. /*Amiga hack - used in amigados.c, must be global */
  138. /*static*/ boolean translate_to_native_sym_flags
  139.   PARAMS ((bfd *, asymbol *, struct external_nlist *));
  140.  
  141. /*
  142. SUBSECTION
  143.     Relocations
  144.  
  145. DESCRIPTION
  146.     The file @file{aoutx.h} provides for both the @emph{standard}
  147.     and @emph{extended} forms of a.out relocation records.
  148.  
  149.     The standard records contain only an
  150.     address, a symbol index, and a type field. The extended records
  151.     (used on 29ks and sparcs) also have a full integer for an
  152.     addend.
  153.  
  154. */
  155. #ifndef CTOR_TABLE_RELOC_HOWTO
  156. #define CTOR_TABLE_RELOC_IDX 2
  157. #define CTOR_TABLE_RELOC_HOWTO(BFD) ((obj_reloc_entry_size(BFD) == RELOC_EXT_SIZE \
  158.          ? howto_table_ext : howto_table_std) \
  159.         + CTOR_TABLE_RELOC_IDX)
  160. #endif
  161.  
  162. #ifndef MY_swap_std_reloc_in
  163. #define MY_swap_std_reloc_in NAME(aout,swap_std_reloc_in)
  164. #endif
  165.  
  166. #ifndef MY_swap_std_reloc_out
  167. #define MY_swap_std_reloc_out NAME(aout,swap_std_reloc_out)
  168. #endif
  169.  
  170. #define howto_table_ext NAME(aout,ext_howto_table)
  171. #define howto_table_std NAME(aout,std_howto_table)
  172.  
  173. reloc_howto_type howto_table_ext[] =
  174. {
  175.   /* type           rs   size bsz  pcrel bitpos ovrf                  sf name          part_inpl readmask setmask pcdone */
  176.   HOWTO(RELOC_8,      0,  0,      8,  false, 0, complain_overflow_bitfield,0,"8",        false, 0,0x000000ff, false),
  177.   HOWTO(RELOC_16,     0,  1,     16, false, 0, complain_overflow_bitfield,0,"16",       false, 0,0x0000ffff, false),
  178.   HOWTO(RELOC_32,     0,  2,     32, false, 0, complain_overflow_bitfield,0,"32",       false, 0,0xffffffff, false),
  179.   HOWTO(RELOC_DISP8,  0,  0,     8,  true,  0, complain_overflow_signed,0,"DISP8",     false, 0,0x000000ff, false),
  180.   HOWTO(RELOC_DISP16, 0,  1,     16, true,  0, complain_overflow_signed,0,"DISP16",     false, 0,0x0000ffff, false),
  181.   HOWTO(RELOC_DISP32, 0,  2,     32, true,  0, complain_overflow_signed,0,"DISP32",     false, 0,0xffffffff, false),
  182.   HOWTO(RELOC_WDISP30,2,  2,     30, true,  0, complain_overflow_signed,0,"WDISP30",     false, 0,0x3fffffff, false),
  183.   HOWTO(RELOC_WDISP22,2,  2,     22, true,  0, complain_overflow_signed,0,"WDISP22",     false, 0,0x003fffff, false),
  184.   HOWTO(RELOC_HI22,   10, 2,     22, false, 0, complain_overflow_bitfield,0,"HI22",    false, 0,0x003fffff, false),
  185.   HOWTO(RELOC_22,     0,  2,     22, false, 0, complain_overflow_bitfield,0,"22",       false, 0,0x003fffff, false),
  186.   HOWTO(RELOC_13,     0,  2,     13, false, 0, complain_overflow_bitfield,0,"13",       false, 0,0x00001fff, false),
  187.   HOWTO(RELOC_LO10,   0,  2,     10, false, 0, complain_overflow_dont,0,"LO10",     false, 0,0x000003ff, false),
  188.   HOWTO(RELOC_SFA_BASE,0, 2,     32, false, 0, complain_overflow_bitfield,0,"SFA_BASE", false, 0,0xffffffff, false),
  189.   HOWTO(RELOC_SFA_OFF13,0,2,     32, false, 0, complain_overflow_bitfield,0,"SFA_OFF13",false, 0,0xffffffff, false),
  190.   HOWTO(RELOC_BASE10, 0,  2,     16, false, 0, complain_overflow_bitfield,0,"BASE10",   false, 0,0x0000ffff, false),
  191.   HOWTO(RELOC_BASE13, 0,  2,    13, false, 0, complain_overflow_bitfield,0,"BASE13",   false, 0,0x00001fff, false),
  192.   HOWTO(RELOC_BASE22, 0,  2,    0,  false, 0, complain_overflow_bitfield,0,"BASE22",   false, 0,0x00000000, false),
  193.   HOWTO(RELOC_PC10,   0,  2,    10, false, 0, complain_overflow_bitfield,0,"PC10",    false, 0,0x000003ff, false),
  194.   HOWTO(RELOC_PC22,   0,  2,    22, false, 0, complain_overflow_bitfield,0,"PC22",    false, 0,0x003fffff, false),
  195.   HOWTO(RELOC_JMP_TBL,0,  2,    32, false, 0, complain_overflow_bitfield,0,"JMP_TBL",    false, 0,0xffffffff, false),
  196.   HOWTO(RELOC_SEGOFF16,0, 2,    0,  false, 0, complain_overflow_bitfield,0,"SEGOFF16",    false, 0,0x00000000, false),
  197.   HOWTO(RELOC_GLOB_DAT,0, 2,    0,  false, 0, complain_overflow_bitfield,0,"GLOB_DAT",    false, 0,0x00000000, false),
  198.   HOWTO(RELOC_JMP_SLOT,0, 2,    0,  false, 0, complain_overflow_bitfield,0,"JMP_SLOT",    false, 0,0x00000000, false),
  199.   HOWTO(RELOC_RELATIVE,0, 2,    0,  false, 0, complain_overflow_bitfield,0,"RELATIVE",    false, 0,0x00000000, false),
  200. };
  201.  
  202. /* Convert standard reloc records to "arelent" format (incl byte swap).  */
  203.  
  204. reloc_howto_type howto_table_std[] = {
  205.   /* type              rs size bsz  pcrel bitpos ovrf                     sf name     part_inpl readmask  setmask    pcdone */
  206. HOWTO( 0,           0,  0,      8,  false, 0, complain_overflow_bitfield,0,"8",        true, 0x000000ff,0x000000ff, false),
  207. HOWTO( 1,           0,  1,     16, false, 0, complain_overflow_bitfield,0,"16",    true, 0x0000ffff,0x0000ffff, false),
  208. HOWTO( 2,           0,  2,     32, false, 0, complain_overflow_bitfield,0,"32",    true, 0xffffffff,0xffffffff, false),
  209. HOWTO( 3,           0,  4,     64, false, 0, complain_overflow_bitfield,0,"64",    true, 0xdeaddead,0xdeaddead, false),
  210. HOWTO( 4,           0,  0,     8,  true,  0, complain_overflow_signed,  0,"DISP8",    true, 0x000000ff,0x000000ff, false),
  211. HOWTO( 5,           0,  1,     16, true,  0, complain_overflow_signed,  0,"DISP16",    true, 0x0000ffff,0x0000ffff, false),
  212. HOWTO( 6,           0,  2,     32, true,  0, complain_overflow_signed,  0,"DISP32",    true, 0xffffffff,0xffffffff, false),
  213. HOWTO( 7,           0,  4,     64, true,  0, complain_overflow_signed,  0,"DISP64",    true, 0xfeedface,0xfeedface, false),
  214. HOWTO( 8,           0,  2,    0, false, 0, complain_overflow_bitfield,0,"GOT_REL",    false,         0,0x00000000, false),
  215. HOWTO( 9,           0,  1,   16, false, 0, complain_overflow_bitfield,0,"BASE16",    false,0xffffffff,0xffffffff, false),
  216. HOWTO(10,           0,  2,   32, false, 0, complain_overflow_bitfield,0,"BASE32",    false,0xffffffff,0xffffffff, false),
  217. { -1 },
  218. { -1 },
  219. { -1 },
  220. { -1 },
  221. { -1 },
  222.   HOWTO(16,           0,  2,     0, false, 0, complain_overflow_bitfield,0,"JMP_TABLE", false,         0,0x00000000, false),
  223. { -1 },
  224. { -1 },
  225. { -1 },
  226. { -1 },
  227. { -1 },
  228. { -1 },
  229. { -1 },
  230. { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 },
  231.   HOWTO(32,           0,  2,     0, false, 0, complain_overflow_bitfield,0,"RELATIVE",  false,         0,0x00000000, false),
  232. { -1 },
  233. { -1 },
  234. { -1 },
  235. { -1 },
  236. { -1 },
  237. { -1 },
  238. { -1 },
  239.   HOWTO(40,           0,  2,     0, false, 0, complain_overflow_bitfield,0,"BASEREL",   false,         0,0x00000000, false),
  240. };
  241.  
  242. #define TABLE_SIZE(TABLE)    (sizeof(TABLE)/sizeof(TABLE[0]))
  243.  
  244. reloc_howto_type *
  245. NAME(aout,reloc_type_lookup) (abfd,code)
  246.      bfd *abfd;
  247.      bfd_reloc_code_real_type code;
  248. {
  249. #define EXT(i,j)    case i: return &howto_table_ext[j]
  250. #define STD(i,j)    case i: return &howto_table_std[j]
  251.   int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
  252.   if (code == BFD_RELOC_CTOR)
  253.     switch (bfd_get_arch_info (abfd)->bits_per_address)
  254.       {
  255.       case 32:
  256.     code = BFD_RELOC_32;
  257.     break;
  258.       case 64:
  259.     code = BFD_RELOC_64;
  260.     break;
  261.       }
  262.   if (ext)
  263.     switch (code)
  264.       {
  265.     EXT (BFD_RELOC_32, 2);
  266.     EXT (BFD_RELOC_HI22, 8);
  267.     EXT (BFD_RELOC_LO10, 11);
  268.     EXT (BFD_RELOC_32_PCREL_S2, 6);
  269.     EXT (BFD_RELOC_SPARC_WDISP22, 7);
  270.     EXT (BFD_RELOC_SPARC13, 10);
  271.     EXT (BFD_RELOC_SPARC_BASE13, 15);
  272.       default: return (reloc_howto_type *) NULL;
  273.       }
  274.   else
  275.     /* std relocs */
  276.     switch (code)
  277.       {
  278.     STD (BFD_RELOC_16, 1);
  279.     STD (BFD_RELOC_32, 2);
  280.     STD (BFD_RELOC_8_PCREL, 4);
  281.     STD (BFD_RELOC_16_PCREL, 5);
  282.     STD (BFD_RELOC_32_PCREL, 6);
  283.     STD (BFD_RELOC_16_BASEREL, 9);
  284.     STD (BFD_RELOC_32_BASEREL, 10);
  285.       default: return (reloc_howto_type *) NULL;
  286.       }
  287. }
  288.  
  289. /*
  290. SUBSECTION
  291.     Internal entry points
  292.  
  293. DESCRIPTION
  294.     @file{aoutx.h} exports several routines for accessing the
  295.     contents of an a.out file, which are gathered and exported in
  296.     turn by various format specific files (eg sunos.c).
  297.  
  298. */
  299.  
  300. /*
  301. FUNCTION
  302.      aout_@var{size}_swap_exec_header_in
  303.  
  304. SYNOPSIS
  305.     void aout_@var{size}_swap_exec_header_in,
  306.            (bfd *abfd,
  307.             struct external_exec *raw_bytes,
  308.             struct internal_exec *execp);
  309.  
  310. DESCRIPTION
  311.     Swap the information in an executable header @var{raw_bytes} taken
  312.     from a raw byte stream memory image into the internal exec header
  313.     structure @var{execp}.
  314. */
  315.  
  316. #ifndef NAME_swap_exec_header_in
  317. void
  318. NAME(aout,swap_exec_header_in) (abfd, raw_bytes, execp)
  319.      bfd *abfd;
  320.      struct external_exec *raw_bytes;
  321.      struct internal_exec *execp;
  322. {
  323.   struct external_exec *bytes = (struct external_exec *)raw_bytes;
  324.  
  325.   /* The internal_exec structure has some fields that are unused in this
  326.      configuration (IE for i960), so ensure that all such uninitialized
  327.      fields are zero'd out.  There are places where two of these structs
  328.      are memcmp'd, and thus the contents do matter. */
  329.   memset ((PTR) execp, 0, sizeof (struct internal_exec));
  330.   /* Now fill in fields in the execp, from the bytes in the raw data.  */
  331.   execp->a_info   = bfd_h_get_32 (abfd, bytes->e_info);
  332.   execp->a_text   = GET_WORD (abfd, bytes->e_text);
  333.   execp->a_data   = GET_WORD (abfd, bytes->e_data);
  334.   execp->a_bss    = GET_WORD (abfd, bytes->e_bss);
  335.   execp->a_syms   = GET_WORD (abfd, bytes->e_syms);
  336.   execp->a_entry  = GET_WORD (abfd, bytes->e_entry);
  337.   execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
  338.   execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
  339. }
  340. #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
  341. #endif
  342.  
  343. /*
  344. FUNCTION
  345.     aout_@var{size}_swap_exec_header_out
  346.  
  347. SYNOPSIS
  348.     void aout_@var{size}_swap_exec_header_out
  349.       (bfd *abfd,
  350.        struct internal_exec *execp,
  351.        struct external_exec *raw_bytes);
  352.  
  353. DESCRIPTION
  354.     Swap the information in an internal exec header structure
  355.     @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
  356. */
  357. void
  358. NAME(aout,swap_exec_header_out) (abfd, execp, raw_bytes)
  359.      bfd *abfd;
  360.      struct internal_exec *execp;
  361.      struct external_exec *raw_bytes;
  362. {
  363.   struct external_exec *bytes = (struct external_exec *)raw_bytes;
  364.  
  365.   /* Now fill in fields in the raw data, from the fields in the exec struct. */
  366.   bfd_h_put_32 (abfd, execp->a_info  , bytes->e_info);
  367.   PUT_WORD (abfd, execp->a_text  , bytes->e_text);
  368.   PUT_WORD (abfd, execp->a_data  , bytes->e_data);
  369.   PUT_WORD (abfd, execp->a_bss   , bytes->e_bss);
  370.   PUT_WORD (abfd, execp->a_syms  , bytes->e_syms);
  371.   PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
  372.   PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
  373.   PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
  374. }
  375.  
  376. /* Make all the section for an a.out file.  */
  377.  
  378. boolean
  379. NAME(aout,make_sections) (abfd)
  380.      bfd *abfd;
  381. {
  382.   if (obj_textsec (abfd) == (asection *) NULL
  383.       && bfd_make_section (abfd, ".text") == (asection *) NULL)
  384.     return false;
  385.   if (obj_datasec (abfd) == (asection *) NULL
  386.       && bfd_make_section (abfd, ".data") == (asection *) NULL)
  387.     return false;
  388.   if (obj_bsssec (abfd) == (asection *) NULL
  389.       && bfd_make_section (abfd, ".bss") == (asection *) NULL)
  390.     return false;
  391.   return true;
  392. }
  393.  
  394. /*
  395. FUNCTION
  396.     aout_@var{size}_some_aout_object_p
  397.  
  398. SYNOPSIS
  399.     const bfd_target *aout_@var{size}_some_aout_object_p
  400.      (bfd *abfd,
  401.       const bfd_target *(*callback_to_real_object_p)());
  402.  
  403. DESCRIPTION
  404.     Some a.out variant thinks that the file open in @var{abfd}
  405.     checking is an a.out file.  Do some more checking, and set up
  406.     for access if it really is.  Call back to the calling
  407.     environment's "finish up" function just before returning, to
  408.     handle any last-minute setup.
  409. */
  410.  
  411. const bfd_target *
  412. NAME(aout,some_aout_object_p) (abfd, execp, callback_to_real_object_p)
  413.      bfd *abfd;
  414.      struct internal_exec *execp;
  415.      const bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *));
  416. {
  417.   struct aout_data_struct *rawptr, *oldrawptr;
  418.   const bfd_target *result;
  419.  
  420.   rawptr = (struct aout_data_struct  *) bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
  421.   if (rawptr == NULL) {
  422.     bfd_set_error (bfd_error_no_memory);
  423.     return 0;
  424.   }
  425.  
  426.   oldrawptr = abfd->tdata.aout_data;
  427.   abfd->tdata.aout_data = rawptr;
  428.  
  429.   /* Copy the contents of the old tdata struct.
  430.      In particular, we want the subformat, since for hpux it was set in
  431.      hp300hpux.c:swap_exec_header_in and will be used in
  432.      hp300hpux.c:callback.  */
  433.   if (oldrawptr != NULL)
  434.     *abfd->tdata.aout_data = *oldrawptr;
  435.  
  436.   abfd->tdata.aout_data->a.hdr = &rawptr->e;
  437.   *(abfd->tdata.aout_data->a.hdr) = *execp;    /* Copy in the internal_exec struct */
  438.   execp = abfd->tdata.aout_data->a.hdr;
  439.  
  440.   /* Set the file flags */
  441.   abfd->flags = NO_FLAGS;
  442.   if (execp->a_drsize || execp->a_trsize)
  443.     abfd->flags |= HAS_RELOC;
  444.   /* Setting of EXEC_P has been deferred to the bottom of this function */
  445.   if (execp->a_syms)
  446.     abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
  447.   if (N_DYNAMIC(*execp))
  448.     abfd->flags |= DYNAMIC;
  449.  
  450.   if (N_MAGIC (*execp) == ZMAGIC)
  451.     {
  452.       abfd->flags |= D_PAGED | WP_TEXT;
  453.       adata (abfd).magic = z_magic;
  454.     }
  455.   else if (N_MAGIC (*execp) == QMAGIC)
  456.     {
  457.       abfd->flags |= D_PAGED | WP_TEXT;
  458.       adata (abfd).magic = z_magic;
  459.       adata (abfd).subformat = q_magic_format;
  460.     }
  461.   else if (N_MAGIC (*execp) == NMAGIC)
  462.     {
  463.       abfd->flags |= WP_TEXT;
  464.       adata (abfd).magic = n_magic;
  465.     }
  466.   else if (N_MAGIC (*execp) == OMAGIC
  467.        || N_MAGIC (*execp) == BMAGIC)
  468.     adata (abfd).magic = o_magic;
  469.   else
  470.     {
  471.       /* Should have been checked with N_BADMAG before this routine
  472.      was called.  */
  473.       abort ();
  474.     }
  475.  
  476.   bfd_get_start_address (abfd) = execp->a_entry;
  477.  
  478.   obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
  479.   bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
  480.  
  481.   /* The default relocation entry size is that of traditional V7 Unix.  */
  482.   obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  483.  
  484.   /* The default symbol entry size is that of traditional Unix. */
  485.   obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
  486.  
  487.   obj_aout_external_syms (abfd) = NULL;
  488.   obj_aout_external_strings (abfd) = NULL;
  489.   obj_aout_sym_hashes (abfd) = NULL;
  490.  
  491.   if (! NAME(aout,make_sections) (abfd))
  492.     return NULL;
  493.  
  494.   obj_datasec (abfd)->_raw_size = execp->a_data;
  495.   obj_bsssec (abfd)->_raw_size = execp->a_bss;
  496.  
  497.   obj_textsec (abfd)->flags =
  498.     (execp->a_trsize != 0
  499.      ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
  500.      : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
  501.   obj_datasec (abfd)->flags =
  502.     (execp->a_drsize != 0
  503.      ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
  504.      : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
  505.   obj_bsssec (abfd)->flags = SEC_ALLOC;
  506.  
  507. #ifdef THIS_IS_ONLY_DOCUMENTATION
  508.   /* The common code can't fill in these things because they depend
  509.      on either the start address of the text segment, the rounding
  510.      up of virtual addresses between segments, or the starting file
  511.      position of the text segment -- all of which varies among different
  512.      versions of a.out.  */
  513.  
  514.   /* Call back to the format-dependent code to fill in the rest of the
  515.      fields and do any further cleanup.  Things that should be filled
  516.      in by the callback:  */
  517.  
  518.   struct exec *execp = exec_hdr (abfd);
  519.  
  520.   obj_textsec (abfd)->size = N_TXTSIZE(*execp);
  521.   obj_textsec (abfd)->raw_size = N_TXTSIZE(*execp);
  522.   /* data and bss are already filled in since they're so standard */
  523.  
  524.   /* The virtual memory addresses of the sections */
  525.   obj_textsec (abfd)->vma = N_TXTADDR(*execp);
  526.   obj_datasec (abfd)->vma = N_DATADDR(*execp);
  527.   obj_bsssec  (abfd)->vma = N_BSSADDR(*execp);
  528.  
  529.   /* The file offsets of the sections */
  530.   obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
  531.   obj_datasec (abfd)->filepos = N_DATOFF(*execp);
  532.  
  533.   /* The file offsets of the relocation info */
  534.   obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
  535.   obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
  536.  
  537.   /* The file offsets of the string table and symbol table.  */
  538.   obj_str_filepos (abfd) = N_STROFF (*execp);
  539.   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
  540.  
  541.   /* Determine the architecture and machine type of the object file.  */
  542.   switch (N_MACHTYPE (*exec_hdr (abfd))) {
  543.   default:
  544.     abfd->obj_arch = bfd_arch_obscure;
  545.     break;
  546.   }
  547.  
  548.   adata(abfd)->page_size = PAGE_SIZE;
  549.   adata(abfd)->segment_size = SEGMENT_SIZE;
  550.   adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
  551.  
  552.   return abfd->xvec;
  553.  
  554.   /* The architecture is encoded in various ways in various a.out variants,
  555.      or is not encoded at all in some of them.  The relocation size depends
  556.      on the architecture and the a.out variant.  Finally, the return value
  557.      is the bfd_target vector in use.  If an error occurs, return zero and
  558.      set bfd_error to the appropriate error code.
  559.  
  560.      Formats such as b.out, which have additional fields in the a.out
  561.      header, should cope with them in this callback as well.  */
  562. #endif                /* DOCUMENTATION */
  563.  
  564.   result = (*callback_to_real_object_p)(abfd);
  565.  
  566.   /* Now that the segment addresses have been worked out, take a better
  567.      guess at whether the file is executable.  If the entry point
  568.      is within the text segment, assume it is.  (This makes files
  569.      executable even if their entry point address is 0, as long as
  570.      their text starts at zero.).  */
  571.   if ((execp->a_entry >= obj_textsec(abfd)->vma) &&
  572.       (execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size))
  573.     abfd->flags |= EXEC_P;
  574. #ifdef STAT_FOR_EXEC
  575.   else
  576.     {
  577.       struct stat stat_buf;
  578.  
  579.       /* The original heuristic doesn't work in some important cases.
  580.         The a.out file has no information about the text start
  581.         address.  For files (like kernels) linked to non-standard
  582.         addresses (ld -Ttext nnn) the entry point may not be between
  583.         the default text start (obj_textsec(abfd)->vma) and
  584.         (obj_textsec(abfd)->vma) + text size.  This is not just a mach
  585.         issue.  Many kernels are loaded at non standard addresses.  */
  586.       if (abfd->iostream
  587.       && (fstat(fileno((FILE *) (abfd->iostream)), &stat_buf) == 0)
  588.       && ((stat_buf.st_mode & 0111) != 0))
  589.     abfd->flags |= EXEC_P;
  590.     }
  591. #endif /* STAT_FOR_EXEC */
  592.  
  593.   if (result)
  594.     {
  595. #if 0 /* These should be set correctly anyways.  */
  596.       abfd->sections = obj_textsec (abfd);
  597.       obj_textsec (abfd)->next = obj_datasec (abfd);
  598.       obj_datasec (abfd)->next = obj_bsssec (abfd);
  599. #endif
  600.     }
  601.   else
  602.     {
  603.       free (rawptr);
  604.       abfd->tdata.aout_data = oldrawptr;
  605.     }
  606.   return result;
  607. }
  608.  
  609. /*
  610. FUNCTION
  611.     aout_@var{size}_mkobject
  612.  
  613. SYNOPSIS
  614.     boolean aout_@var{size}_mkobject, (bfd *abfd);
  615.  
  616. DESCRIPTION
  617.     Initialize BFD @var{abfd} for use with a.out files.
  618. */
  619.  
  620. boolean
  621. NAME(aout,mkobject) (abfd)
  622.      bfd *abfd;
  623. {
  624.   struct aout_data_struct  *rawptr;
  625.  
  626.   bfd_set_error (bfd_error_system_call);
  627.  
  628.   /* Use an intermediate variable for clarity */
  629.   rawptr = (struct aout_data_struct *)bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
  630.  
  631.   if (rawptr == NULL) {
  632.     bfd_set_error (bfd_error_no_memory);
  633.     return false;
  634.   }
  635.  
  636.   abfd->tdata.aout_data = rawptr;
  637.   exec_hdr (abfd) = &(rawptr->e);
  638.  
  639.   obj_textsec (abfd) = (asection *)NULL;
  640.   obj_datasec (abfd) = (asection *)NULL;
  641.   obj_bsssec (abfd) = (asection *)NULL;
  642.  
  643.   return true;
  644. }
  645.  
  646.  
  647. /*
  648. FUNCTION
  649.     aout_@var{size}_machine_type
  650.  
  651. SYNOPSIS
  652.     enum machine_type  aout_@var{size}_machine_type
  653.      (enum bfd_architecture arch,
  654.       unsigned long machine));
  655.  
  656. DESCRIPTION
  657.     Keep track of machine architecture and machine type for
  658.     a.out's. Return the <<machine_type>> for a particular
  659.     architecture and machine, or <<M_UNKNOWN>> if that exact architecture
  660.     and machine can't be represented in a.out format.
  661.  
  662.     If the architecture is understood, machine type 0 (default)
  663.     is always understood.
  664. */
  665.  
  666. enum machine_type
  667. NAME(aout,machine_type) (arch, machine, unknown)
  668.      enum bfd_architecture arch;
  669.      unsigned long machine;
  670.      boolean *unknown;
  671. {
  672.   enum machine_type arch_flags;
  673.  
  674.   arch_flags = M_UNKNOWN;
  675.   *unknown = true;
  676.  
  677.   switch (arch) {
  678.   case bfd_arch_sparc:
  679.     if (machine == 0)    arch_flags = M_SPARC;
  680.     break;
  681.  
  682.   case bfd_arch_m68k:
  683.     switch (machine) {
  684.     case 0:        arch_flags = M_68010; break;
  685.     case 68000:        arch_flags = M_UNKNOWN;    *unknown = false; break;
  686.     case 68010:        arch_flags = M_68010; break;
  687.     case 68020:        arch_flags = M_68020; break;
  688.     default:        arch_flags = M_UNKNOWN; break;
  689.     }
  690.     break;
  691.  
  692.   case bfd_arch_i386:
  693.     if (machine == 0)    arch_flags = M_386;
  694.     break;
  695.  
  696.   case bfd_arch_a29k:
  697.     if (machine == 0)    arch_flags = M_29K;
  698.     break;
  699.  
  700.   case bfd_arch_arm:
  701.     if (machine == 0)    arch_flags = M_ARM;
  702.     break;
  703.     
  704.   case bfd_arch_mips:
  705.     switch (machine) {
  706.     case 0:
  707.     case 2000:
  708.     case 3000:          arch_flags = M_MIPS1; break;
  709.     case 4000:
  710.     case 4400:
  711.     case 6000:          arch_flags = M_MIPS2; break;
  712.     default:            arch_flags = M_UNKNOWN; break;
  713.     }
  714.     break;
  715.  
  716.   case bfd_arch_ns32k:
  717.     switch (machine) {
  718.     case 0:            arch_flags = M_NS32532; break;
  719.     case 32032:        arch_flags = M_NS32032; break;
  720.     case 32532:        arch_flags = M_NS32532; break;
  721.     default:        arch_flags = M_UNKNOWN; break;
  722.     }
  723.     break;
  724.  
  725.   case bfd_arch_vax:
  726.     *unknown = false;
  727.     break;
  728.  
  729.  
  730.   default:
  731.     arch_flags = M_UNKNOWN;
  732.   }
  733.  
  734.   if (arch_flags != M_UNKNOWN)
  735.     *unknown = false;
  736.  
  737.   return arch_flags;
  738. }
  739.  
  740.  
  741. /*
  742. FUNCTION
  743.     aout_@var{size}_set_arch_mach
  744.  
  745. SYNOPSIS
  746.     boolean aout_@var{size}_set_arch_mach,
  747.      (bfd *,
  748.       enum bfd_architecture arch,
  749.       unsigned long machine));
  750.  
  751. DESCRIPTION
  752.     Set the architecture and the machine of the BFD @var{abfd} to the
  753.     values @var{arch} and @var{machine}.  Verify that @var{abfd}'s format
  754.     can support the architecture required.
  755. */
  756.  
  757. boolean
  758. NAME(aout,set_arch_mach) (abfd, arch, machine)
  759.      bfd *abfd;
  760.      enum bfd_architecture arch;
  761.      unsigned long machine;
  762. {
  763.   if (! bfd_default_set_arch_mach (abfd, arch, machine))
  764.     return false;
  765.  
  766.   if (arch != bfd_arch_unknown)
  767.     {
  768.       boolean unknown;
  769.  
  770.       NAME(aout,machine_type) (arch, machine, &unknown);
  771.       if (unknown)
  772.     return false;
  773.     }
  774.  
  775.   /* Determine the size of a relocation entry */
  776.   switch (arch) {
  777.   case bfd_arch_sparc:
  778.   case bfd_arch_a29k:
  779.   case bfd_arch_mips:
  780.     obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
  781.     break;
  782.   default:
  783.     obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  784.     break;
  785.   }
  786.  
  787.   return (*aout_backend_info(abfd)->set_sizes) (abfd);
  788. }
  789.  
  790. static void
  791. adjust_o_magic (abfd, execp)
  792.      bfd *abfd;
  793.      struct internal_exec *execp;
  794. {
  795.   file_ptr pos = adata (abfd).exec_bytes_size;
  796.   bfd_vma vma = 0;
  797.   int pad = 0;
  798.  
  799.   /* Text.  */
  800.   obj_textsec(abfd)->filepos = pos;
  801.   if (!obj_textsec(abfd)->user_set_vma)
  802.     obj_textsec(abfd)->vma = vma;
  803.   else
  804.     vma = obj_textsec(abfd)->vma;
  805.  
  806.   pos += obj_textsec(abfd)->_raw_size;
  807.   vma += obj_textsec(abfd)->_raw_size;
  808.  
  809.   /* Data.  */
  810.   if (!obj_datasec(abfd)->user_set_vma)
  811.     {
  812. #if 0        /* ?? Does alignment in the file image really matter? */
  813.       pad = align_power (vma, obj_datasec(abfd)->alignment_power) - vma;
  814. #endif
  815.       obj_textsec(abfd)->_raw_size += pad;
  816.       pos += pad;
  817.       vma += pad;
  818.       obj_datasec(abfd)->vma = vma;
  819.     }
  820.   else
  821.     vma = obj_datasec(abfd)->vma;
  822.   obj_datasec(abfd)->filepos = pos;
  823.   pos += obj_datasec(abfd)->_raw_size;
  824.   vma += obj_datasec(abfd)->_raw_size;
  825.  
  826.   /* BSS.  */
  827.   if (!obj_bsssec(abfd)->user_set_vma)
  828.     {
  829. #if 0
  830.       pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
  831. #endif
  832.       obj_datasec(abfd)->_raw_size += pad;
  833.       pos += pad;
  834.       vma += pad;
  835.       obj_bsssec(abfd)->vma = vma;
  836.     }
  837.   else
  838.     {
  839.       /* The VMA of the .bss section is set by the the VMA of the
  840.          .data section plus the size of the .data section.  We may
  841.          need to add padding bytes to make this true.  */
  842.       pad = obj_bsssec (abfd)->vma - vma;
  843.       if (pad > 0)
  844.     {
  845.       obj_datasec (abfd)->_raw_size += pad;
  846.       pos += pad;
  847.     }
  848.     }
  849.   obj_bsssec(abfd)->filepos = pos;
  850.  
  851.   /* Fix up the exec header.  */
  852.   execp->a_text = obj_textsec(abfd)->_raw_size;
  853.   execp->a_data = obj_datasec(abfd)->_raw_size;
  854.   execp->a_bss = obj_bsssec(abfd)->_raw_size;
  855.   N_SET_MAGIC (*execp, OMAGIC);
  856. }
  857.  
  858. static void
  859. adjust_z_magic (abfd, execp)
  860.      bfd *abfd;
  861.      struct internal_exec *execp;
  862. {
  863.   bfd_size_type data_pad, text_pad;
  864.   file_ptr text_end;
  865.   CONST struct aout_backend_data *abdp;
  866.   int ztih;            /* Nonzero if text includes exec header.  */
  867.   
  868.   abdp = aout_backend_info (abfd);
  869.  
  870.   /* Text.  */
  871.   ztih = (abdp != NULL
  872.       && (abdp->text_includes_header
  873.           || obj_aout_subformat (abfd) == q_magic_format));
  874.   obj_textsec(abfd)->filepos = (ztih
  875.                 ? adata(abfd).exec_bytes_size
  876.                 : adata(abfd).zmagic_disk_block_size);
  877.   if (! obj_textsec(abfd)->user_set_vma)
  878.     {
  879.       /* ?? Do we really need to check for relocs here?  */
  880.       obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC)
  881.                 ? 0
  882.                 : (ztih
  883.                    ? (abdp->default_text_vma
  884.                       + adata(abfd).exec_bytes_size)
  885.                    : abdp->default_text_vma));
  886.       text_pad = 0;
  887.     }
  888.   else
  889.     {
  890.       /* The .text section is being loaded at an unusual address.  We
  891.          may need to pad it such that the .data section starts at a page
  892.          boundary.  */
  893.       if (ztih)
  894.     text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
  895.             & (adata (abfd).page_size - 1));
  896.       else
  897.     text_pad = ((- obj_textsec (abfd)->vma)
  898.             & (adata (abfd).page_size - 1));
  899.     }
  900.  
  901.   /* Find start of data.  */
  902.   if (ztih)
  903.     {
  904.       text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->_raw_size;
  905.       text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
  906.     }
  907.   else
  908.     {
  909.       /* Note that if page_size == zmagic_disk_block_size, then
  910.      filepos == page_size, and this case is the same as the ztih
  911.      case.  */
  912.       text_end = obj_textsec (abfd)->_raw_size;
  913.       text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
  914.       text_end += obj_textsec (abfd)->filepos;
  915.     }
  916.   obj_textsec(abfd)->_raw_size += text_pad;
  917.   text_end += text_pad;
  918.  
  919.   /* Data.  */
  920.   if (!obj_datasec(abfd)->user_set_vma)
  921.     {
  922.       bfd_vma vma;
  923.       vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size;
  924.       obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
  925.     }
  926.   if (abdp && abdp->zmagic_mapped_contiguous)
  927.     {
  928.       text_pad = (obj_datasec(abfd)->vma
  929.           - obj_textsec(abfd)->vma
  930.           - obj_textsec(abfd)->_raw_size);
  931.       obj_textsec(abfd)->_raw_size += text_pad;
  932.     }
  933.   obj_datasec(abfd)->filepos = (obj_textsec(abfd)->filepos
  934.                 + obj_textsec(abfd)->_raw_size);
  935.   
  936.   /* Fix up exec header while we're at it.  */
  937.   execp->a_text = obj_textsec(abfd)->_raw_size;
  938.   if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
  939.     execp->a_text += adata(abfd).exec_bytes_size;
  940.   if (obj_aout_subformat (abfd) == q_magic_format)
  941.     N_SET_MAGIC (*execp, QMAGIC);
  942.   else
  943.     N_SET_MAGIC (*execp, ZMAGIC);
  944.  
  945.   /* Spec says data section should be rounded up to page boundary.  */
  946.   obj_datasec(abfd)->_raw_size
  947.     = align_power (obj_datasec(abfd)->_raw_size,
  948.            obj_bsssec(abfd)->alignment_power);
  949.   execp->a_data = BFD_ALIGN (obj_datasec(abfd)->_raw_size,
  950.                  adata(abfd).page_size);
  951.   data_pad = execp->a_data - obj_datasec(abfd)->_raw_size;
  952.  
  953.   /* BSS.  */
  954.   if (!obj_bsssec(abfd)->user_set_vma)
  955.     obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma
  956.                  + obj_datasec(abfd)->_raw_size);
  957.   /* If the BSS immediately follows the data section and extra space
  958.      in the page is left after the data section, fudge data
  959.      in the header so that the bss section looks smaller by that
  960.      amount.  We'll start the bss section there, and lie to the OS.
  961.      (Note that a linker script, as well as the above assignment,
  962.      could have explicitly set the BSS vma to immediately follow
  963.      the data section.)  */
  964.   if (align_power (obj_bsssec(abfd)->vma, obj_bsssec(abfd)->alignment_power)
  965.       == obj_datasec(abfd)->vma + obj_datasec(abfd)->_raw_size)
  966.     execp->a_bss = (data_pad > obj_bsssec(abfd)->_raw_size) ? 0 :
  967.       obj_bsssec(abfd)->_raw_size - data_pad;
  968.   else
  969.     execp->a_bss = obj_bsssec(abfd)->_raw_size;
  970. }
  971.  
  972. static void
  973. adjust_n_magic (abfd, execp)
  974.      bfd *abfd;
  975.      struct internal_exec *execp;
  976. {
  977.   file_ptr pos = adata(abfd).exec_bytes_size;
  978.   bfd_vma vma = 0;
  979.   int pad;
  980.   
  981.   /* Text.  */
  982.   obj_textsec(abfd)->filepos = pos;
  983.   if (!obj_textsec(abfd)->user_set_vma)
  984.     obj_textsec(abfd)->vma = vma;
  985.   else
  986.     vma = obj_textsec(abfd)->vma;
  987.   pos += obj_textsec(abfd)->_raw_size;
  988.   vma += obj_textsec(abfd)->_raw_size;
  989.  
  990.   /* Data.  */
  991.   obj_datasec(abfd)->filepos = pos;
  992.   if (!obj_datasec(abfd)->user_set_vma)
  993.     obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
  994.   vma = obj_datasec(abfd)->vma;
  995.   
  996.   /* Since BSS follows data immediately, see if it needs alignment.  */
  997.   vma += obj_datasec(abfd)->_raw_size;
  998.   pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
  999.   obj_datasec(abfd)->_raw_size += pad;
  1000.   pos += obj_datasec(abfd)->_raw_size;
  1001.  
  1002.   /* BSS.  */
  1003.   if (!obj_bsssec(abfd)->user_set_vma)
  1004.     obj_bsssec(abfd)->vma = vma;
  1005.   else
  1006.     vma = obj_bsssec(abfd)->vma;
  1007.  
  1008.   /* Fix up exec header.  */
  1009.   execp->a_text = obj_textsec(abfd)->_raw_size;
  1010.   execp->a_data = obj_datasec(abfd)->_raw_size;
  1011.   execp->a_bss = obj_bsssec(abfd)->_raw_size;
  1012.   N_SET_MAGIC (*execp, NMAGIC);
  1013. }
  1014.  
  1015. boolean
  1016. NAME(aout,adjust_sizes_and_vmas) (abfd, text_size, text_end)
  1017.      bfd *abfd;
  1018.      bfd_size_type *text_size;
  1019.      file_ptr *text_end;
  1020. {
  1021.   struct internal_exec *execp = exec_hdr (abfd);
  1022.  
  1023.   if (! NAME(aout,make_sections) (abfd))
  1024.     return false;
  1025.  
  1026.   if (adata(abfd).magic != undecided_magic)
  1027.     return true;
  1028.  
  1029.   obj_textsec(abfd)->_raw_size =
  1030.     align_power(obj_textsec(abfd)->_raw_size,
  1031.         obj_textsec(abfd)->alignment_power);
  1032.  
  1033.   *text_size = obj_textsec (abfd)->_raw_size;
  1034.   /* Rule (heuristic) for when to pad to a new page.  Note that there
  1035.      are (at least) two ways demand-paged (ZMAGIC) files have been
  1036.      handled.  Most Berkeley-based systems start the text segment at
  1037.      (PAGE_SIZE).  However, newer versions of SUNOS start the text
  1038.      segment right after the exec header; the latter is counted in the
  1039.      text segment size, and is paged in by the kernel with the rest of
  1040.      the text. */
  1041.  
  1042.   /* This perhaps isn't the right way to do this, but made it simpler for me
  1043.      to understand enough to implement it.  Better would probably be to go
  1044.      right from BFD flags to alignment/positioning characteristics.  But the
  1045.      old code was sloppy enough about handling the flags, and had enough
  1046.      other magic, that it was a little hard for me to understand.  I think
  1047.      I understand it better now, but I haven't time to do the cleanup this
  1048.      minute.  */
  1049.  
  1050.   if (abfd->flags & D_PAGED)
  1051.     /* Whether or not WP_TEXT is set -- let D_PAGED override.  */
  1052.     adata(abfd).magic = z_magic;
  1053.   else if (abfd->flags & WP_TEXT)
  1054.     adata(abfd).magic = n_magic;
  1055.   else
  1056.     adata(abfd).magic = o_magic;
  1057.  
  1058. #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
  1059. #if __GNUC__ >= 2
  1060.   fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
  1061.        ({ char *str;
  1062.           switch (adata(abfd).magic) {
  1063.           case n_magic: str = "NMAGIC"; break;
  1064.           case o_magic: str = "OMAGIC"; break;
  1065.           case z_magic: str = "ZMAGIC"; break;
  1066.           default: abort ();
  1067.           }
  1068.           str;
  1069.         }),
  1070.        obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
  1071.            obj_textsec(abfd)->alignment_power,
  1072.        obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
  1073.            obj_datasec(abfd)->alignment_power,
  1074.        obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size,
  1075.            obj_bsssec(abfd)->alignment_power);
  1076. #endif
  1077. #endif
  1078.  
  1079.   switch (adata(abfd).magic)
  1080.     {
  1081.     case o_magic:
  1082.       adjust_o_magic (abfd, execp);
  1083.       break;
  1084.     case z_magic:
  1085.       adjust_z_magic (abfd, execp);
  1086.       break;
  1087.     case n_magic:
  1088.       adjust_n_magic (abfd, execp);
  1089.       break;
  1090.     default:
  1091.       abort ();
  1092.     }
  1093.  
  1094. #ifdef BFD_AOUT_DEBUG
  1095.   fprintf (stderr, "       text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
  1096.        obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
  1097.            obj_textsec(abfd)->filepos,
  1098.        obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
  1099.            obj_datasec(abfd)->filepos,
  1100.        obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size);
  1101. #endif
  1102.  
  1103.   return true;
  1104. }
  1105.  
  1106. /*
  1107. FUNCTION
  1108.     aout_@var{size}_new_section_hook
  1109.  
  1110. SYNOPSIS
  1111.         boolean aout_@var{size}_new_section_hook,
  1112.        (bfd *abfd,
  1113.         asection *newsect));
  1114.  
  1115. DESCRIPTION
  1116.     Called by the BFD in response to a @code{bfd_make_section}
  1117.     request.
  1118. */
  1119. boolean
  1120. NAME(aout,new_section_hook) (abfd, newsect)
  1121.      bfd *abfd;
  1122.      asection *newsect;
  1123. {
  1124.   /* align to double at least */
  1125.   newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
  1126.  
  1127.  
  1128.   if (bfd_get_format (abfd) == bfd_object)
  1129.   {
  1130.     if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) {
  1131.     obj_textsec(abfd)= newsect;
  1132.     newsect->target_index = N_TEXT;
  1133.     return true;
  1134.       }
  1135.  
  1136.     if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) {
  1137.     obj_datasec(abfd) = newsect;
  1138.     newsect->target_index = N_DATA;
  1139.     return true;
  1140.       }
  1141.  
  1142.     if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) {
  1143.     obj_bsssec(abfd) = newsect;
  1144.     newsect->target_index = N_BSS;
  1145.     return true;
  1146.       }
  1147.  
  1148.   }
  1149.  
  1150.   /* We allow more than three sections internally */
  1151.   return true;
  1152. }
  1153.  
  1154. boolean
  1155. NAME(aout,set_section_contents) (abfd, section, location, offset, count)
  1156.      bfd *abfd;
  1157.      sec_ptr section;
  1158.      PTR location;
  1159.      file_ptr offset;
  1160.      bfd_size_type count;
  1161. {
  1162.   file_ptr text_end;
  1163.   bfd_size_type text_size;
  1164.  
  1165.   if (abfd->output_has_begun == false)
  1166.       {
  1167.     if (NAME(aout,adjust_sizes_and_vmas) (abfd,
  1168.                           &text_size,
  1169.                           &text_end) == false)
  1170.       return false;
  1171.       }
  1172.  
  1173.   /* regardless, once we know what we're doing, we might as well get going */
  1174.   if (section != obj_bsssec(abfd))
  1175.       {
  1176.     if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
  1177.       return false;
  1178.  
  1179.     if (count) {
  1180.       return (bfd_write ((PTR)location, 1, count, abfd) == count) ?
  1181.         true : false;
  1182.     }
  1183.     return true;
  1184.       }
  1185.   return true;
  1186. }
  1187.  
  1188. /* Read the external symbols from an a.out file.  */
  1189.  
  1190. static boolean
  1191. aout_get_external_symbols (abfd)
  1192.      bfd *abfd;
  1193. {
  1194.   if (obj_aout_external_syms (abfd) == (struct external_nlist *) NULL)
  1195.     {
  1196.       bfd_size_type count;
  1197.       struct external_nlist *syms;
  1198.  
  1199.       count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
  1200.  
  1201.       /* We allocate using malloc to make the values easy to free
  1202.      later on.  If we put them on the obstack it might not be
  1203.      possible to free them.  */
  1204.       syms = ((struct external_nlist *)
  1205.           malloc ((size_t) count * EXTERNAL_NLIST_SIZE));
  1206.       if (syms == (struct external_nlist *) NULL && count != 0)
  1207.     {
  1208.       bfd_set_error (bfd_error_no_memory);
  1209.       return false;
  1210.     }
  1211.  
  1212.       if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
  1213.       || (bfd_read (syms, 1, exec_hdr (abfd)->a_syms, abfd)
  1214.           != exec_hdr (abfd)->a_syms))
  1215.     {
  1216.       free (syms);
  1217.       return false;
  1218.     }
  1219.  
  1220.       obj_aout_external_syms (abfd) = syms;
  1221.       obj_aout_external_sym_count (abfd) = count;
  1222.     }
  1223.       
  1224.   if (obj_aout_external_strings (abfd) == NULL
  1225.       && exec_hdr (abfd)->a_syms != 0)
  1226.     {
  1227.       unsigned char string_chars[BYTES_IN_WORD];
  1228.       bfd_size_type stringsize;
  1229.       char *strings;
  1230.  
  1231.       /* Get the size of the strings.  */
  1232.       if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
  1233.       || (bfd_read ((PTR) string_chars, BYTES_IN_WORD, 1, abfd)
  1234.           != BYTES_IN_WORD))
  1235.     return false;
  1236.       stringsize = GET_WORD (abfd, string_chars);
  1237.  
  1238.       strings = (char *) malloc ((size_t) stringsize + 1);
  1239.       if (strings == NULL)
  1240.     {
  1241.       bfd_set_error (bfd_error_no_memory);
  1242.       return false;
  1243.     }
  1244.  
  1245.       /* Skip space for the string count in the buffer for convenience
  1246.      when using indexes.  */
  1247.       if (bfd_read (strings + BYTES_IN_WORD, 1, stringsize - BYTES_IN_WORD,
  1248.             abfd)
  1249.       != stringsize - BYTES_IN_WORD)
  1250.     {
  1251.       free (strings);
  1252.       return false;
  1253.     }
  1254.  
  1255.       /* Ensure that a zero index yields an empty string.  */
  1256.       strings[0] = '\0';
  1257.  
  1258.       /* Sanity preservation.  */
  1259.       strings[stringsize] = '\0';
  1260.  
  1261.       obj_aout_external_strings (abfd) = strings;
  1262.       obj_aout_external_string_size (abfd) = stringsize;
  1263.     }
  1264.  
  1265.   return true;
  1266. }
  1267.  
  1268. /* Translate an a.out symbol into a BFD symbol.  The desc, other, type
  1269.    and symbol->value fields of CACHE_PTR will be set from the a.out
  1270.    nlist structure.  This function is responsible for setting
  1271.    symbol->flags and symbol->section, and adjusting symbol->value.  */
  1272.  
  1273. static boolean
  1274. translate_from_native_sym_flags (abfd, cache_ptr)
  1275.      bfd *abfd;
  1276.      aout_symbol_type *cache_ptr;
  1277. {
  1278.   flagword visible;
  1279.  
  1280.   if ((cache_ptr->type & N_STAB) != 0
  1281.       || cache_ptr->type == N_FN)
  1282.     {
  1283.       asection *sec;
  1284.  
  1285.       /* This is a debugging symbol.  */
  1286.  
  1287.       cache_ptr->symbol.flags = BSF_DEBUGGING;
  1288.  
  1289.       /* Work out the symbol section.  */
  1290.       switch (cache_ptr->type & N_TYPE)
  1291.     {
  1292.     case N_TEXT:
  1293.     case N_FN:
  1294.       sec = obj_textsec (abfd);
  1295.       break;
  1296.     case N_DATA:
  1297.       sec = obj_datasec (abfd);
  1298.       break;
  1299.     case N_BSS:
  1300.       sec = obj_bsssec (abfd);
  1301.       break;
  1302.     default:
  1303.     case N_ABS:
  1304.       sec = bfd_abs_section_ptr;
  1305.       break;
  1306.     }
  1307.  
  1308.       cache_ptr->symbol.section = sec;
  1309.       cache_ptr->symbol.value -= sec->vma;
  1310.  
  1311.       return true;
  1312.     }
  1313.  
  1314.   /* Get the default visibility.  This does not apply to all types, so
  1315.      we just hold it in a local variable to use if wanted.  */
  1316.   if ((cache_ptr->type & N_EXT) == 0)
  1317.     visible = BSF_LOCAL;
  1318.   else
  1319.     visible = BSF_GLOBAL;
  1320.  
  1321.   switch (cache_ptr->type)
  1322.     {
  1323.     default:
  1324.     case N_ABS: case N_ABS | N_EXT:
  1325.       cache_ptr->symbol.section = bfd_abs_section_ptr;
  1326.       cache_ptr->symbol.flags = visible;
  1327.       break;
  1328.  
  1329.     case N_UNDF | N_EXT:
  1330.       if (cache_ptr->symbol.value != 0)
  1331.     {
  1332.       /* This is a common symbol.  */
  1333.       cache_ptr->symbol.flags = BSF_GLOBAL;
  1334.       cache_ptr->symbol.section = bfd_com_section_ptr;
  1335.     }
  1336.       else
  1337.     {
  1338.       cache_ptr->symbol.flags = 0;
  1339.       cache_ptr->symbol.section = bfd_und_section_ptr;
  1340.     }
  1341.       break;
  1342.  
  1343.     case N_TEXT: case N_TEXT | N_EXT:
  1344.       cache_ptr->symbol.section = obj_textsec (abfd);
  1345.       cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1346.       cache_ptr->symbol.flags = visible;
  1347.       break;
  1348.  
  1349.       /* N_SETV symbols used to represent set vectors placed in the
  1350.      data section.  They are no longer generated.  Theoretically,
  1351.      it was possible to extract the entries and combine them with
  1352.      new ones, although I don't know if that was ever actually
  1353.      done.  Unless that feature is restored, treat them as data
  1354.      symbols.  */
  1355.     case N_SETV: case N_SETV | N_EXT:
  1356.     case N_DATA: case N_DATA | N_EXT:
  1357.       cache_ptr->symbol.section = obj_datasec (abfd);
  1358.       cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1359.       cache_ptr->symbol.flags = visible;
  1360.       break;
  1361.  
  1362.     case N_BSS: case N_BSS | N_EXT:
  1363.       cache_ptr->symbol.section = obj_bsssec (abfd);
  1364.       cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1365.       cache_ptr->symbol.flags = visible;
  1366.       break;
  1367.  
  1368.     case N_SETA: case N_SETA | N_EXT:
  1369.     case N_SETT: case N_SETT | N_EXT:
  1370.     case N_SETD: case N_SETD | N_EXT:
  1371.     case N_SETB: case N_SETB | N_EXT:
  1372.       {
  1373.     asection *section;
  1374.     arelent_chain *reloc;
  1375.     asection *into_section;
  1376.  
  1377.     /* This is a set symbol.  The name of the symbol is the name
  1378.        of the set (e.g., __CTOR_LIST__).  The value of the symbol
  1379.        is the value to add to the set.  We create a section with
  1380.        the same name as the symbol, and add a reloc to insert the
  1381.        appropriate value into the section.
  1382.  
  1383.        This action is actually obsolete; it used to make the
  1384.        linker do the right thing, but the linker no longer uses
  1385.        this function.  */
  1386.  
  1387.     section = bfd_get_section_by_name (abfd, cache_ptr->symbol.name);
  1388.     if (section == NULL)
  1389.       {
  1390.         char *copy;
  1391.  
  1392.         copy = bfd_alloc (abfd, strlen (cache_ptr->symbol.name) + 1);
  1393.         if (copy == NULL)
  1394.           {
  1395.         bfd_set_error (bfd_error_no_memory);
  1396.         return false;
  1397.           }
  1398.  
  1399.         strcpy (copy, cache_ptr->symbol.name);
  1400.         section = bfd_make_section (abfd, copy);
  1401.         if (section == NULL)
  1402.           return false;
  1403.       }
  1404.  
  1405.     reloc = (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
  1406.     if (reloc == NULL)
  1407.       {
  1408.         bfd_set_error (bfd_error_no_memory);
  1409.         return false;
  1410.       }
  1411.  
  1412.     /* Build a relocation entry for the constructor.  */
  1413.     switch (cache_ptr->type & N_TYPE)
  1414.       {
  1415.       case N_SETA:
  1416.         into_section = bfd_abs_section_ptr;
  1417.         cache_ptr->type = N_ABS;
  1418.         break;
  1419.       case N_SETT:
  1420.         into_section = obj_textsec (abfd);
  1421.         cache_ptr->type = N_TEXT;
  1422.         break;
  1423.       case N_SETD:
  1424.         into_section = obj_datasec (abfd);
  1425.         cache_ptr->type = N_DATA;
  1426.         break;
  1427.       case N_SETB:
  1428.         into_section = obj_bsssec (abfd);
  1429.         cache_ptr->type = N_BSS;
  1430.         break;
  1431.       }
  1432.  
  1433.     /* Build a relocation pointing into the constructor section
  1434.        pointing at the symbol in the set vector specified.  */
  1435.     reloc->relent.addend = cache_ptr->symbol.value;
  1436.     cache_ptr->symbol.section = into_section;
  1437.     reloc->relent.sym_ptr_ptr = into_section->symbol_ptr_ptr;
  1438.  
  1439.     /* We modify the symbol to belong to a section depending upon
  1440.        the name of the symbol, and add to the size of the section
  1441.        to contain a pointer to the symbol. Build a reloc entry to
  1442.        relocate to this symbol attached to this section.  */
  1443.     section->flags = SEC_CONSTRUCTOR | SEC_RELOC;
  1444.  
  1445.     section->reloc_count++;
  1446.     section->alignment_power = 2;
  1447.  
  1448.     reloc->next = section->constructor_chain;
  1449.     section->constructor_chain = reloc;
  1450.     reloc->relent.address = section->_raw_size;
  1451.     section->_raw_size += BYTES_IN_WORD;
  1452.  
  1453.     reloc->relent.howto = CTOR_TABLE_RELOC_HOWTO(abfd);
  1454.  
  1455.     cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
  1456.       }
  1457.       break;
  1458.  
  1459.     case N_WARNING:
  1460.       /* This symbol is the text of a warning message.  The next
  1461.      symbol is the symbol to associate the warning with.  If a
  1462.      reference is made to that symbol, a warning is issued.  */
  1463.       cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
  1464.  
  1465.       /* @@ Stuffing pointers into integers is a no-no.  We can
  1466.      usually get away with it if the integer is large enough
  1467.      though.  */
  1468.       if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
  1469.     abort ();
  1470.       cache_ptr->symbol.value = (bfd_vma) (cache_ptr + 1);
  1471.  
  1472.       cache_ptr->symbol.section = bfd_abs_section_ptr;
  1473.  
  1474.       break;
  1475.  
  1476.     case N_INDR: case N_INDR | N_EXT:
  1477.       /* An indirect symbol.  This consists of two symbols in a row.
  1478.      The first symbol is the name of the indirection.  The second
  1479.      symbol is the name of the target.  A reference to the first
  1480.      symbol becomes a reference to the second.  */
  1481.       cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT | visible;
  1482.  
  1483.       /* @@ Stuffing pointers into integers is a no-no.  We can
  1484.      usually get away with it if the integer is large enough
  1485.      though.  */
  1486.       if (sizeof (cache_ptr + 1) > sizeof (bfd_vma))
  1487.     abort ();
  1488.       cache_ptr->symbol.value = (bfd_vma) (cache_ptr + 1);
  1489.  
  1490.       cache_ptr->symbol.section = bfd_ind_section_ptr;
  1491.  
  1492.       break;
  1493.  
  1494.     case N_WEAKU:
  1495.       cache_ptr->symbol.section = bfd_und_section_ptr;
  1496.       cache_ptr->symbol.flags = BSF_WEAK;
  1497.       break;
  1498.  
  1499.     case N_WEAKA:
  1500.       cache_ptr->symbol.section = bfd_abs_section_ptr;
  1501.       cache_ptr->symbol.flags = BSF_WEAK;
  1502.       break;
  1503.  
  1504.     case N_WEAKT:
  1505.       cache_ptr->symbol.section = obj_textsec (abfd);
  1506.       cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1507.       cache_ptr->symbol.flags = BSF_WEAK;
  1508.       break;
  1509.  
  1510.     case N_WEAKD:
  1511.       cache_ptr->symbol.section = obj_datasec (abfd);
  1512.       cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1513.       cache_ptr->symbol.flags = BSF_WEAK;
  1514.       break;
  1515.  
  1516.     case N_WEAKB:
  1517.       cache_ptr->symbol.section = obj_bsssec (abfd);
  1518.       cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
  1519.       cache_ptr->symbol.flags = BSF_WEAK;
  1520.       break;
  1521.     }
  1522.  
  1523.   return true;
  1524. }
  1525.  
  1526. /* Set the fields of SYM_POINTER according to CACHE_PTR.  */
  1527.  
  1528. /*static*/ boolean
  1529. translate_to_native_sym_flags (abfd, cache_ptr, sym_pointer)
  1530.      bfd *abfd;
  1531.      asymbol *cache_ptr;
  1532.      struct external_nlist *sym_pointer;
  1533. {
  1534.   bfd_vma value = cache_ptr->value;
  1535.  
  1536.   /* Mask out any existing type bits in case copying from one section
  1537.      to another.  */
  1538.   sym_pointer->e_type[0] &= ~N_TYPE;
  1539.  
  1540.   if (bfd_is_abs_section (bfd_get_section (cache_ptr)))
  1541.     sym_pointer->e_type[0] |= N_ABS;
  1542.   else if (bfd_get_section (cache_ptr) == obj_textsec (abfd)
  1543.        || (bfd_get_section (cache_ptr)->output_section
  1544.            == obj_textsec (abfd)))
  1545.     sym_pointer->e_type[0] |= N_TEXT;
  1546.   else if (bfd_get_section (cache_ptr) == obj_datasec (abfd)
  1547.        || (bfd_get_section (cache_ptr)->output_section
  1548.            == obj_datasec (abfd)))
  1549.     sym_pointer->e_type[0] |= N_DATA;
  1550.   else if (bfd_get_section (cache_ptr) == obj_bsssec (abfd)
  1551.        || (bfd_get_section (cache_ptr)->output_section
  1552.            == obj_bsssec (abfd)))
  1553.     sym_pointer->e_type[0] |= N_BSS;
  1554.   else if (bfd_get_section (cache_ptr) == NULL)
  1555.     {
  1556.       /* Protect the bfd_is_com_section call.  This case occurs, e.g.,
  1557.      for the *DEBUG* section of a COFF file.  */
  1558.       bfd_set_error (bfd_error_nonrepresentable_section);
  1559.       return false;
  1560.     }
  1561.   else if (bfd_is_und_section (bfd_get_section (cache_ptr)))
  1562.     sym_pointer->e_type[0] = N_UNDF | N_EXT;
  1563.   else if (bfd_is_ind_section (bfd_get_section (cache_ptr)))
  1564.     sym_pointer->e_type[0] = N_INDR;
  1565.   else if (bfd_is_com_section (bfd_get_section (cache_ptr)))
  1566.     sym_pointer->e_type[0] = N_UNDF | N_EXT;
  1567.   else
  1568.     {
  1569.       bfd_set_error (bfd_error_nonrepresentable_section);
  1570.       return false;
  1571.     }
  1572.  
  1573.   /* Turn the symbol from section relative to absolute again */
  1574.   if (cache_ptr->section->output_section != NULL)
  1575.     value += (cache_ptr->section->output_section->vma
  1576.           + cache_ptr->section->output_offset);
  1577.   else
  1578.     value += cache_ptr->section->vma;
  1579.  
  1580.   if ((cache_ptr->flags & BSF_WARNING) != 0)
  1581.     sym_pointer->e_type[0] = N_WARNING;
  1582.  
  1583.   if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
  1584.     sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
  1585.   else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
  1586.     sym_pointer->e_type[0] |= N_EXT;
  1587.  
  1588.   if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
  1589.     {
  1590.       int type = ((aout_symbol_type *) cache_ptr)->type;
  1591.       switch (type)
  1592.     {
  1593.     case N_ABS:    type = N_SETA; break;
  1594.     case N_TEXT:    type = N_SETT; break;
  1595.     case N_DATA:    type = N_SETD; break;
  1596.     case N_BSS:    type = N_SETB; break;
  1597.     }
  1598.       sym_pointer->e_type[0] = type;
  1599.     }
  1600.  
  1601.   if ((cache_ptr->flags & BSF_WEAK) != 0)
  1602.     {
  1603.       int type;
  1604.  
  1605.       switch (sym_pointer->e_type[0] & N_TYPE)
  1606.     {
  1607.     default:
  1608.     case N_ABS:    type = N_WEAKA; break;
  1609.     case N_TEXT:    type = N_WEAKT; break;
  1610.     case N_DATA:    type = N_WEAKD; break;
  1611.     case N_BSS:    type = N_WEAKB; break;
  1612.     case N_UNDF:    type = N_WEAKU; break;
  1613.     }
  1614.       sym_pointer->e_type[0] = type;
  1615.     }
  1616.  
  1617.   PUT_WORD(abfd, value, sym_pointer->e_value);
  1618.  
  1619.   return true;
  1620. }
  1621.  
  1622. /* Native-level interface to symbols. */
  1623.  
  1624. asymbol *
  1625. NAME(aout,make_empty_symbol) (abfd)
  1626.      bfd *abfd;
  1627. {
  1628.   aout_symbol_type  *new =
  1629.     (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type));
  1630.   if (!new)
  1631.     {
  1632.       bfd_set_error (bfd_error_no_memory);
  1633.       return NULL;
  1634.     }
  1635.   new->symbol.the_bfd = abfd;
  1636.  
  1637.   return &new->symbol;
  1638. }
  1639.  
  1640. /* Translate a set of internal symbols into external symbols.  */
  1641.  
  1642. boolean
  1643. NAME(aout,translate_symbol_table) (abfd, in, ext, count, str, strsize, dynamic)
  1644.      bfd *abfd;
  1645.      aout_symbol_type *in;
  1646.      struct external_nlist *ext;
  1647.      bfd_size_type count;
  1648.      char *str;
  1649.      bfd_size_type strsize;
  1650.      boolean dynamic;
  1651. {
  1652.   struct external_nlist *ext_end;
  1653.  
  1654.   ext_end = ext + count;
  1655.   for (; ext < ext_end; ext++, in++)
  1656.     {
  1657.       bfd_vma x;
  1658.  
  1659.       x = GET_WORD (abfd, ext->e_strx);
  1660.       in->symbol.the_bfd = abfd;
  1661.  
  1662.       /* For the normal symbols, the zero index points at the number
  1663.      of bytes in the string table but is to be interpreted as the
  1664.      null string.  For the dynamic symbols, the number of bytes in
  1665.      the string table is stored in the __DYNAMIC structure and the
  1666.      zero index points at an actual string.  */
  1667.       if (x == 0 && ! dynamic)
  1668.     in->symbol.name = "";
  1669.       else if (x < strsize)
  1670.     in->symbol.name = str + x;
  1671.       else
  1672.     return false;
  1673.  
  1674.       in->symbol.value = GET_SWORD (abfd,  ext->e_value);
  1675.       in->desc = bfd_h_get_16 (abfd, ext->e_desc);
  1676.       in->other = bfd_h_get_8 (abfd, ext->e_other);
  1677.       in->type = bfd_h_get_8 (abfd,  ext->e_type);
  1678.       in->symbol.udata.p = NULL;
  1679.  
  1680.       if (! translate_from_native_sym_flags (abfd, in))
  1681.     return false;
  1682.  
  1683.       if (dynamic)
  1684.     in->symbol.flags |= BSF_DYNAMIC;
  1685.     }
  1686.  
  1687.   return true;
  1688. }
  1689.  
  1690. /* We read the symbols into a buffer, which is discarded when this
  1691.    function exits.  We read the strings into a buffer large enough to
  1692.    hold them all plus all the cached symbol entries. */
  1693.  
  1694. boolean
  1695. NAME(aout,slurp_symbol_table) (abfd)
  1696.      bfd *abfd;
  1697. {
  1698.   struct external_nlist *old_external_syms;
  1699.   aout_symbol_type *cached;
  1700.   size_t cached_size;
  1701.  
  1702.   /* If there's no work to be done, don't do any */
  1703.   if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL)
  1704.     return true;
  1705.  
  1706.   old_external_syms = obj_aout_external_syms (abfd);
  1707.  
  1708.   if (! aout_get_external_symbols (abfd))
  1709.     return false;
  1710.  
  1711.   cached_size = (obj_aout_external_sym_count (abfd)
  1712.          * sizeof (aout_symbol_type));
  1713.   cached = (aout_symbol_type *) malloc (cached_size);
  1714.   if (cached == NULL && cached_size != 0)
  1715.     {
  1716.       bfd_set_error (bfd_error_no_memory);
  1717.       return false;
  1718.     }
  1719.   if (cached_size != 0)
  1720.     memset (cached, 0, cached_size);
  1721.  
  1722.   /* Convert from external symbol information to internal.  */
  1723.   if (! (NAME(aout,translate_symbol_table)
  1724.      (abfd, cached,
  1725.       obj_aout_external_syms (abfd),
  1726.       obj_aout_external_sym_count (abfd),
  1727.       obj_aout_external_strings (abfd),
  1728.       obj_aout_external_string_size (abfd),
  1729.       false)))
  1730.     {
  1731.       free (cached);
  1732.       return false;
  1733.     }
  1734.  
  1735.   bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
  1736.  
  1737.   obj_aout_symbols (abfd) = cached;
  1738.  
  1739.   /* It is very likely that anybody who calls this function will not
  1740.      want the external symbol information, so if it was allocated
  1741.      because of our call to aout_get_external_symbols, we free it up
  1742.      right away to save space.  */
  1743.   if (old_external_syms == (struct external_nlist *) NULL
  1744.       && obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
  1745.     {
  1746.       free (obj_aout_external_syms (abfd));
  1747.       obj_aout_external_syms (abfd) = NULL;
  1748.     }
  1749.  
  1750.   return true;
  1751. }
  1752.  
  1753. /* We use a hash table when writing out symbols so that we only write
  1754.    out a particular string once.  This helps particularly when the
  1755.    linker writes out stabs debugging entries, because each different
  1756.    contributing object file tends to have many duplicate stabs
  1757.    strings.
  1758.  
  1759.    This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
  1760.    if BFD_TRADITIONAL_FORMAT is set.  */
  1761.  
  1762. static bfd_size_type add_to_stringtab
  1763.   PARAMS ((bfd *, struct bfd_strtab_hash *, const char *, boolean));
  1764. static boolean emit_stringtab PARAMS ((bfd *, struct bfd_strtab_hash *));
  1765.  
  1766. /* Get the index of a string in a strtab, adding it if it is not
  1767.    already present.  */
  1768.  
  1769. static INLINE bfd_size_type
  1770. add_to_stringtab (abfd, tab, str, copy)
  1771.      bfd *abfd;
  1772.      struct bfd_strtab_hash *tab;
  1773.      const char *str;
  1774.      boolean copy;
  1775. {
  1776.   boolean hash;
  1777.   bfd_size_type index;
  1778.  
  1779.   /* An index of 0 always means the empty string.  */
  1780.   if (str == 0 || *str == '\0')
  1781.     return 0;
  1782.  
  1783.   /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
  1784.      doesn't understand a hashed string table.  */
  1785.   hash = true;
  1786.   if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
  1787.     hash = false;
  1788.  
  1789.   index = _bfd_stringtab_add (tab, str, hash, copy);
  1790.  
  1791.   if (index != (bfd_size_type) -1)
  1792.     {
  1793.       /* Add BYTES_IN_WORD to the return value to account for the
  1794.      space taken up by the string table size.  */
  1795.       index += BYTES_IN_WORD;
  1796.     }
  1797.  
  1798.   return index;
  1799. }
  1800.  
  1801. /* Write out a strtab.  ABFD is already at the right location in the
  1802.    file.  */
  1803.  
  1804. static boolean
  1805. emit_stringtab (abfd, tab)
  1806.      register bfd *abfd;
  1807.      struct bfd_strtab_hash *tab;
  1808. {
  1809.   bfd_byte buffer[BYTES_IN_WORD];
  1810.  
  1811.   /* The string table starts with the size.  */
  1812.   PUT_WORD (abfd, _bfd_stringtab_size (tab) + BYTES_IN_WORD, buffer);
  1813.   if (bfd_write ((PTR) buffer, 1, BYTES_IN_WORD, abfd) != BYTES_IN_WORD)
  1814.     return false;
  1815.  
  1816.   return _bfd_stringtab_emit (abfd, tab);
  1817. }
  1818.  
  1819. boolean
  1820. NAME(aout,write_syms) (abfd)
  1821.      bfd *abfd;
  1822. {
  1823.   unsigned int count ;
  1824.   asymbol **generic = bfd_get_outsymbols (abfd);
  1825.   struct bfd_strtab_hash *strtab;
  1826.  
  1827.   strtab = _bfd_stringtab_init ();
  1828.   if (strtab == NULL)
  1829.     return false;
  1830.  
  1831.   for (count = 0; count < bfd_get_symcount (abfd); count++)
  1832.     {
  1833.       asymbol *g = generic[count];
  1834.       bfd_size_type indx;
  1835.       struct external_nlist nsp;
  1836.  
  1837.       indx = add_to_stringtab (abfd, strtab, g->name, false);
  1838.       if (indx == (bfd_size_type) -1)
  1839.     goto error_return;
  1840.       PUT_WORD (abfd, indx, (bfd_byte *) nsp.e_strx);
  1841.  
  1842.       if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
  1843.     {
  1844.       bfd_h_put_16(abfd, aout_symbol(g)->desc,  nsp.e_desc);
  1845.       bfd_h_put_8(abfd, aout_symbol(g)->other,  nsp.e_other);
  1846.       bfd_h_put_8(abfd, aout_symbol(g)->type,  nsp.e_type);
  1847.     }
  1848.       else
  1849.     {
  1850.       bfd_h_put_16(abfd,0, nsp.e_desc);
  1851.       bfd_h_put_8(abfd, 0, nsp.e_other);
  1852.       bfd_h_put_8(abfd, 0, nsp.e_type);
  1853.     }
  1854.  
  1855.       if (! translate_to_native_sym_flags (abfd, g, &nsp))
  1856.     goto error_return;
  1857.  
  1858.       if (bfd_write((PTR)&nsp,1,EXTERNAL_NLIST_SIZE, abfd)
  1859.       != EXTERNAL_NLIST_SIZE)
  1860.     goto error_return;
  1861.  
  1862.       /* NB: `KEEPIT' currently overlays `flags', so set this only
  1863.      here, at the end.  */
  1864.       g->KEEPIT = count;
  1865.     }
  1866.  
  1867.   if (! emit_stringtab (abfd, strtab))
  1868.     goto error_return;
  1869.  
  1870.   _bfd_stringtab_free (strtab);
  1871.  
  1872.   return true;
  1873.  
  1874. error_return:
  1875.   _bfd_stringtab_free (strtab);
  1876.   return false;
  1877. }
  1878.  
  1879.  
  1880. long
  1881. NAME(aout,get_symtab) (abfd, location)
  1882.      bfd *abfd;
  1883.      asymbol **location;
  1884. {
  1885.     unsigned int counter = 0;
  1886.     aout_symbol_type *symbase;
  1887.  
  1888.     if (!NAME(aout,slurp_symbol_table)(abfd))
  1889.       return -1;
  1890.  
  1891.     for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
  1892.       *(location++) = (asymbol *)( symbase++);
  1893.     *location++ =0;
  1894.     return bfd_get_symcount (abfd);
  1895. }
  1896.  
  1897.  
  1898. /* Standard reloc stuff */
  1899. /* Output standard relocation information to a file in target byte order. */
  1900.  
  1901. void
  1902. NAME(aout,swap_std_reloc_out) (abfd, g, natptr)
  1903.      bfd *abfd;
  1904.      arelent *g;
  1905.      struct reloc_std_external *natptr;
  1906. {
  1907.   int r_index;
  1908.   asymbol *sym = *(g->sym_ptr_ptr);
  1909.   int r_extern;
  1910.   unsigned int r_length;
  1911.   int r_pcrel;
  1912.   int r_baserel, r_jmptable, r_relative;
  1913.   asection *output_section = sym->section->output_section;
  1914.  
  1915.   PUT_WORD(abfd, g->address, natptr->r_address);
  1916.  
  1917.   r_length = g->howto->size ;    /* Size as a power of two */
  1918.   r_pcrel  = (int) g->howto->pc_relative; /* Relative to PC? */
  1919.  
  1920. #ifdef __amigados__
  1921.   /* Changed for cooperation with AMIGA backend */
  1922.   /* This only applies, if aout flavour    191194 ST*/
  1923.   /* XXX This relies on relocs coming from a.out files.  */
  1924.   /* FIXME! "#ifdef __amigados__" is the wrong way to select this code. */
  1925.   if (bfd_asymbol_bfd(sym)->xvec->flavour==bfd_target_aout_flavour)
  1926.     {
  1927.       r_baserel = (g->howto->type & 8) != 0;
  1928.       r_jmptable = (g->howto->type & 16) != 0;
  1929.       r_relative = (g->howto->type & 32) != 0;
  1930.     }
  1931.   else
  1932.     {
  1933.       r_baserel=r_jmptable=r_relative=0;
  1934.     }
  1935. #else
  1936.   r_baserel = (g->howto->type & 8) != 0;
  1937.   r_jmptable = (g->howto->type & 16) != 0;
  1938.   r_relative = (g->howto->type & 32) != 0;
  1939. #endif
  1940.  
  1941. #if 0
  1942.   /* For a standard reloc, the addend is in the object file.  */
  1943.   r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
  1944. #endif
  1945.  
  1946.   /* name was clobbered by aout_write_syms to be symbol index */
  1947.  
  1948.   /* If this relocation is relative to a symbol then set the
  1949.      r_index to the symbols index, and the r_extern bit.
  1950.  
  1951.      Absolute symbols can come in in two ways, either as an offset
  1952.      from the abs section, or as a symbol which has an abs value.
  1953.      check for that here
  1954.      */
  1955.  
  1956.  
  1957.   if (bfd_is_com_section (output_section)
  1958.       || bfd_is_abs_section (output_section)
  1959.       || bfd_is_und_section (output_section))
  1960.     {
  1961.       if (bfd_abs_section_ptr->symbol == sym)
  1962.       {
  1963.     /* Whoops, looked like an abs symbol, but is really an offset
  1964.        from the abs section */
  1965.     r_index = 0;
  1966.     r_extern = 0;
  1967.        }
  1968.       else
  1969.       {
  1970.     /* Fill in symbol */
  1971.     r_extern = 1;
  1972.     r_index =  stoi((*(g->sym_ptr_ptr))->KEEPIT);
  1973.  
  1974.       }
  1975.     }
  1976.   else
  1977.     {
  1978.       /* Just an ordinary section */
  1979.       r_extern = 0;
  1980.       r_index  = output_section->target_index;
  1981.     }
  1982.  
  1983.   /* now the fun stuff */
  1984.   if (abfd->xvec->header_byteorder_big_p != false) {
  1985.       natptr->r_index[0] = r_index >> 16;
  1986.       natptr->r_index[1] = r_index >> 8;
  1987.       natptr->r_index[2] = r_index;
  1988.       natptr->r_type[0] =
  1989.        (r_extern?    RELOC_STD_BITS_EXTERN_BIG: 0)
  1990.     | (r_pcrel?     RELOC_STD_BITS_PCREL_BIG: 0)
  1991.      | (r_baserel?   RELOC_STD_BITS_BASEREL_BIG: 0)
  1992.       | (r_jmptable?  RELOC_STD_BITS_JMPTABLE_BIG: 0)
  1993.        | (r_relative?  RELOC_STD_BITS_RELATIVE_BIG: 0)
  1994.         | (r_length <<  RELOC_STD_BITS_LENGTH_SH_BIG);
  1995.     } else {
  1996.     natptr->r_index[2] = r_index >> 16;
  1997.     natptr->r_index[1] = r_index >> 8;
  1998.     natptr->r_index[0] = r_index;
  1999.     natptr->r_type[0] =
  2000.      (r_extern?    RELOC_STD_BITS_EXTERN_LITTLE: 0)
  2001.       | (r_pcrel?     RELOC_STD_BITS_PCREL_LITTLE: 0)
  2002.        | (r_baserel?   RELOC_STD_BITS_BASEREL_LITTLE: 0)
  2003.         | (r_jmptable?  RELOC_STD_BITS_JMPTABLE_LITTLE: 0)
  2004.          | (r_relative?  RELOC_STD_BITS_RELATIVE_LITTLE: 0)
  2005.           | (r_length <<  RELOC_STD_BITS_LENGTH_SH_LITTLE);
  2006.       }
  2007. }
  2008.  
  2009.  
  2010. /* Extended stuff */
  2011. /* Output extended relocation information to a file in target byte order. */
  2012.  
  2013. void
  2014. NAME(aout,swap_ext_reloc_out) (abfd, g, natptr)
  2015.      bfd *abfd;
  2016.      arelent *g;
  2017.      register struct reloc_ext_external *natptr;
  2018. {
  2019.   int r_index;
  2020.   int r_extern;
  2021.   unsigned int r_type;
  2022.   unsigned int r_addend;
  2023.   asymbol *sym = *(g->sym_ptr_ptr);
  2024.   asection *output_section = sym->section->output_section;
  2025.  
  2026.   PUT_WORD (abfd, g->address, natptr->r_address);
  2027.  
  2028.   r_type = (unsigned int) g->howto->type;
  2029.  
  2030.   r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
  2031.  
  2032.   /* If this relocation is relative to a symbol then set the
  2033.      r_index to the symbols index, and the r_extern bit.
  2034.  
  2035.      Absolute symbols can come in in two ways, either as an offset
  2036.      from the abs section, or as a symbol which has an abs value.
  2037.      check for that here.  */
  2038.  
  2039.   if (bfd_is_com_section (output_section)
  2040.       || bfd_is_abs_section (output_section)
  2041.       || bfd_is_und_section (output_section))
  2042.   {
  2043.     if (bfd_abs_section_ptr->symbol == sym)
  2044.     {
  2045.       /* Whoops, looked like an abs symbol, but is really an offset
  2046.      from the abs section */
  2047.       r_index = 0;
  2048.       r_extern = 0;
  2049.      }
  2050.     else
  2051.     {
  2052.       r_extern = 1;
  2053.       r_index =  stoi((*(g->sym_ptr_ptr))->KEEPIT);
  2054.     }
  2055.   }
  2056.   else
  2057.   {
  2058.     /* Just an ordinary section */
  2059.     r_extern = 0;
  2060.     r_index  = output_section->target_index;
  2061.   }
  2062.  
  2063.   /* now the fun stuff */
  2064.   if (abfd->xvec->header_byteorder_big_p != false) {
  2065.     natptr->r_index[0] = r_index >> 16;
  2066.     natptr->r_index[1] = r_index >> 8;
  2067.     natptr->r_index[2] = r_index;
  2068.     natptr->r_type[0] =
  2069.       ((r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0)
  2070.        | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
  2071.   } else {
  2072.     natptr->r_index[2] = r_index >> 16;
  2073.     natptr->r_index[1] = r_index >> 8;
  2074.     natptr->r_index[0] = r_index;
  2075.     natptr->r_type[0] =
  2076.      (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0)
  2077.       | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
  2078.   }
  2079.  
  2080.   PUT_WORD (abfd, r_addend, natptr->r_addend);
  2081. }
  2082.  
  2083. /* BFD deals internally with all things based from the section they're
  2084.    in. so, something in 10 bytes into a text section  with a base of
  2085.    50 would have a symbol (.text+10) and know .text vma was 50.
  2086.  
  2087.    Aout keeps all it's symbols based from zero, so the symbol would
  2088.    contain 60. This macro subs the base of each section from the value
  2089.    to give the true offset from the section */
  2090.  
  2091.  
  2092. #define MOVE_ADDRESS(ad)                               \
  2093.   if (r_extern) {                            \
  2094.    /* undefined symbol */                        \
  2095.      cache_ptr->sym_ptr_ptr = symbols + r_index;            \
  2096.      cache_ptr->addend = ad;                        \
  2097.      } else {                                \
  2098.     /* defined, section relative. replace symbol with pointer to        \
  2099.        symbol which points to section  */                \
  2100.     switch (r_index) {                            \
  2101.     case N_TEXT:                            \
  2102.     case N_TEXT | N_EXT:                        \
  2103.       cache_ptr->sym_ptr_ptr  = obj_textsec(abfd)->symbol_ptr_ptr;    \
  2104.       cache_ptr->addend = ad  - su->textsec->vma;            \
  2105.       break;                                \
  2106.     case N_DATA:                            \
  2107.     case N_DATA | N_EXT:                        \
  2108.       cache_ptr->sym_ptr_ptr  = obj_datasec(abfd)->symbol_ptr_ptr;    \
  2109.       cache_ptr->addend = ad - su->datasec->vma;            \
  2110.       break;                                \
  2111.     case N_BSS:                                \
  2112.     case N_BSS | N_EXT:                            \
  2113.       cache_ptr->sym_ptr_ptr  = obj_bsssec(abfd)->symbol_ptr_ptr;    \
  2114.       cache_ptr->addend = ad - su->bsssec->vma;                \
  2115.       break;                                \
  2116.     default:                                \
  2117.     case N_ABS:                                \
  2118.     case N_ABS | N_EXT:                            \
  2119.      cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;    \
  2120.       cache_ptr->addend = ad;                        \
  2121.       break;                                \
  2122.     }                                    \
  2123.   }                                     \
  2124.  
  2125. void
  2126. NAME(aout,swap_ext_reloc_in) (abfd, bytes, cache_ptr, symbols)
  2127.      bfd *abfd;
  2128.      struct reloc_ext_external *bytes;
  2129.      arelent *cache_ptr;
  2130.      asymbol **symbols;
  2131. {
  2132.   int r_index;
  2133.   int r_extern;
  2134.   unsigned int r_type;
  2135.   struct aoutdata *su = &(abfd->tdata.aout_data->a);
  2136.  
  2137.   cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
  2138.  
  2139.   /* now the fun stuff */
  2140.   if (abfd->xvec->header_byteorder_big_p != false) {
  2141.     r_index =  (bytes->r_index[0] << 16)
  2142.          | (bytes->r_index[1] << 8)
  2143.          |  bytes->r_index[2];
  2144.     r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
  2145.     r_type   =       (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
  2146.                       >> RELOC_EXT_BITS_TYPE_SH_BIG;
  2147.   } else {
  2148.     r_index =  (bytes->r_index[2] << 16)
  2149.          | (bytes->r_index[1] << 8)
  2150.          |  bytes->r_index[0];
  2151.     r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
  2152.     r_type   =       (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
  2153.                       >> RELOC_EXT_BITS_TYPE_SH_LITTLE;
  2154.   }
  2155.  
  2156.   cache_ptr->howto =  howto_table_ext + r_type;
  2157.   MOVE_ADDRESS(GET_SWORD(abfd, bytes->r_addend));
  2158. }
  2159.  
  2160. void
  2161. NAME(aout,swap_std_reloc_in) (abfd, bytes, cache_ptr, symbols)
  2162.      bfd *abfd;
  2163.      struct reloc_std_external *bytes;
  2164.      arelent *cache_ptr;
  2165.      asymbol **symbols;
  2166. {
  2167.   int r_index;
  2168.   int r_extern;
  2169.   unsigned int r_length;
  2170.   int r_pcrel;
  2171.   int r_baserel, r_jmptable, r_relative;
  2172.   struct aoutdata  *su = &(abfd->tdata.aout_data->a);
  2173.   int howto_idx;
  2174.  
  2175.   cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
  2176.  
  2177.   /* now the fun stuff */
  2178.   if (abfd->xvec->header_byteorder_big_p != false) {
  2179.     r_index =  (bytes->r_index[0] << 16)
  2180.       | (bytes->r_index[1] << 8)
  2181.     |  bytes->r_index[2];
  2182.     r_extern  = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
  2183.     r_pcrel   = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
  2184.     r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
  2185.     r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
  2186.     r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
  2187.     r_length  =       (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
  2188.                   >> RELOC_STD_BITS_LENGTH_SH_BIG;
  2189.   } else {
  2190.     r_index =  (bytes->r_index[2] << 16)
  2191.       | (bytes->r_index[1] << 8)
  2192.     |  bytes->r_index[0];
  2193.     r_extern  = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
  2194.     r_pcrel   = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
  2195.     r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
  2196.     r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
  2197.     r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
  2198.     r_length  =       (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
  2199.                   >> RELOC_STD_BITS_LENGTH_SH_LITTLE;
  2200.   }
  2201.  
  2202.   howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel
  2203.           + 16 * r_jmptable + 32 * r_relative;
  2204.   BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
  2205.   cache_ptr->howto =  howto_table_std + howto_idx;
  2206.   BFD_ASSERT (cache_ptr->howto->type != -1);
  2207.  
  2208.   MOVE_ADDRESS(0);
  2209. }
  2210.  
  2211. /* Read and swap the relocs for a section.  */
  2212.  
  2213. boolean
  2214. NAME(aout,slurp_reloc_table) (abfd, asect, symbols)
  2215.      bfd *abfd;
  2216.      sec_ptr asect;
  2217.      asymbol **symbols;
  2218. {
  2219.   unsigned int count;
  2220.   bfd_size_type reloc_size;
  2221.   PTR relocs;
  2222.   arelent *reloc_cache;
  2223.   size_t each_size;
  2224.   unsigned int counter = 0;
  2225.   arelent *cache_ptr;
  2226.  
  2227.   if (asect->relocation)
  2228.     return true;
  2229.  
  2230.   if (asect->flags & SEC_CONSTRUCTOR)
  2231.     return true;
  2232.  
  2233.   if (asect == obj_datasec (abfd))
  2234.     reloc_size = exec_hdr(abfd)->a_drsize;
  2235.   else if (asect == obj_textsec (abfd))
  2236.     reloc_size = exec_hdr(abfd)->a_trsize;
  2237.   else if (asect == obj_bsssec (abfd))
  2238.     reloc_size = 0;
  2239.   else
  2240.     {
  2241.       bfd_set_error (bfd_error_invalid_operation);
  2242.       return false;
  2243.     }
  2244.  
  2245.   if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
  2246.     return false;
  2247.  
  2248.   each_size = obj_reloc_entry_size (abfd);
  2249.  
  2250.   count = reloc_size / each_size;
  2251.  
  2252.   reloc_cache = (arelent *) malloc ((size_t) (count * sizeof (arelent)));
  2253.   if (reloc_cache == NULL && count != 0)
  2254.     {
  2255.       bfd_set_error (bfd_error_no_memory);
  2256.       return false;
  2257.     }
  2258.   memset (reloc_cache, 0, count * sizeof (arelent));
  2259.  
  2260.   relocs = malloc (reloc_size);
  2261.   if (relocs == NULL && reloc_size != 0)
  2262.     {
  2263.       free (reloc_cache);
  2264.       bfd_set_error (bfd_error_no_memory);
  2265.       return false;
  2266.     }
  2267.  
  2268.   if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size)
  2269.     {
  2270.       free (relocs);
  2271.       free (reloc_cache);
  2272.       return false;
  2273.     }
  2274.  
  2275.   cache_ptr = reloc_cache;
  2276.   if (each_size == RELOC_EXT_SIZE)
  2277.     {
  2278.       register struct reloc_ext_external *rptr =
  2279.     (struct reloc_ext_external *) relocs;
  2280.  
  2281.       for (; counter < count; counter++, rptr++, cache_ptr++)
  2282.     NAME(aout,swap_ext_reloc_in) (abfd, rptr, cache_ptr, symbols);
  2283.     }
  2284.   else
  2285.     {
  2286.       register struct reloc_std_external *rptr =
  2287.     (struct reloc_std_external *) relocs;
  2288.  
  2289.       for (; counter < count; counter++, rptr++, cache_ptr++)
  2290.     MY_swap_std_reloc_in(abfd, rptr, cache_ptr, symbols);
  2291.     }
  2292.  
  2293.   free (relocs);
  2294.  
  2295.   asect->relocation = reloc_cache;
  2296.   asect->reloc_count = cache_ptr - reloc_cache;
  2297.  
  2298.   return true;
  2299. }
  2300.  
  2301. /* Write out a relocation section into an object file.  */
  2302.  
  2303. boolean
  2304. NAME(aout,squirt_out_relocs) (abfd, section)
  2305.      bfd *abfd;
  2306.      asection *section;
  2307. {
  2308.   arelent **generic;
  2309.   unsigned char *native, *natptr;
  2310.   size_t each_size;
  2311.  
  2312.   unsigned int count = section->reloc_count;
  2313.   size_t natsize;
  2314.  
  2315.   if (count == 0) return true;
  2316.  
  2317.   each_size = obj_reloc_entry_size (abfd);
  2318.   natsize = each_size * count;
  2319.   native = (unsigned char *) bfd_zalloc (abfd, natsize);
  2320.   if (!native) {
  2321.     bfd_set_error (bfd_error_no_memory);
  2322.     return false;
  2323.   }
  2324.  
  2325.   generic = section->orelocation;
  2326.  
  2327.   if (each_size == RELOC_EXT_SIZE)
  2328.     {
  2329.       for (natptr = native;
  2330.        count != 0;
  2331.        --count, natptr += each_size, ++generic)
  2332.     NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr);
  2333.     }
  2334.   else
  2335.     {
  2336.       for (natptr = native;
  2337.        count != 0;
  2338.        --count, natptr += each_size, ++generic)
  2339.     MY_swap_std_reloc_out(abfd, *generic, (struct reloc_std_external *)natptr);
  2340.     }
  2341.  
  2342.   if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
  2343.     bfd_release(abfd, native);
  2344.     return false;
  2345.   }
  2346.   bfd_release (abfd, native);
  2347.  
  2348.   return true;
  2349. }
  2350.  
  2351. /* This is stupid.  This function should be a boolean predicate */
  2352. long
  2353. NAME(aout,canonicalize_reloc) (abfd, section, relptr, symbols)
  2354.      bfd *abfd;
  2355.      sec_ptr section;
  2356.      arelent **relptr;
  2357.      asymbol **symbols;
  2358. {
  2359.   arelent *tblptr = section->relocation;
  2360.   unsigned int count;
  2361.  
  2362.   if (section == obj_bsssec (abfd))
  2363.     {
  2364.       *relptr = NULL;
  2365.       return 0;
  2366.     }
  2367.  
  2368.   if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
  2369.     return -1;
  2370.  
  2371.   if (section->flags & SEC_CONSTRUCTOR) {
  2372.     arelent_chain *chain = section->constructor_chain;
  2373.     for (count = 0; count < section->reloc_count; count ++) {
  2374.       *relptr ++ = &chain->relent;
  2375.       chain = chain->next;
  2376.     }
  2377.   }
  2378.   else {
  2379.     tblptr = section->relocation;
  2380.  
  2381.     for (count = 0; count++ < section->reloc_count;)
  2382.       {
  2383.     *relptr++ = tblptr++;
  2384.       }
  2385.   }
  2386.   *relptr = 0;
  2387.  
  2388.   return section->reloc_count;
  2389. }
  2390.  
  2391. long
  2392. NAME(aout,get_reloc_upper_bound) (abfd, asect)
  2393.      bfd *abfd;
  2394.      sec_ptr asect;
  2395. {
  2396.   if (bfd_get_format (abfd) != bfd_object) {
  2397.     bfd_set_error (bfd_error_invalid_operation);
  2398.     return -1;
  2399.   }
  2400.   if (asect->flags & SEC_CONSTRUCTOR) {
  2401.     return (sizeof (arelent *) * (asect->reloc_count+1));
  2402.   }
  2403.  
  2404.   if (asect == obj_datasec (abfd))
  2405.     return (sizeof (arelent *)
  2406.         * ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
  2407.            + 1));
  2408.  
  2409.   if (asect == obj_textsec (abfd))
  2410.     return (sizeof (arelent *)
  2411.         * ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
  2412.            + 1));
  2413.  
  2414.   if (asect == obj_bsssec (abfd))
  2415.     return sizeof (arelent *);
  2416.  
  2417.   if (asect == obj_bsssec (abfd))
  2418.     return 0;
  2419.  
  2420. #ifdef __amigados__    /* FIXME */
  2421.   fprintf(stderr,"Hmmm, sec=%s, %lx, .text=%lx, .data=%lx, .bss=%lx\n",
  2422.       asect->name,asect,obj_textsec(abfd),obj_datasec(abfd),obj_bsssec(abfd));
  2423. #endif
  2424.  
  2425.   bfd_set_error (bfd_error_invalid_operation);
  2426.   return -1;
  2427. }
  2428.  
  2429.  
  2430. long
  2431. NAME(aout,get_symtab_upper_bound) (abfd)
  2432.      bfd *abfd;
  2433. {
  2434.   if (!NAME(aout,slurp_symbol_table)(abfd))
  2435.     return -1;
  2436.  
  2437.   return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
  2438. }
  2439.  
  2440. /*ARGSUSED*/
  2441.  alent *
  2442. NAME(aout,get_lineno) (ignore_abfd, ignore_symbol)
  2443.      bfd *ignore_abfd;
  2444.      asymbol *ignore_symbol;
  2445. {
  2446. return (alent *)NULL;
  2447. }
  2448.  
  2449. /*ARGSUSED*/
  2450. void
  2451. NAME(aout,get_symbol_info) (ignore_abfd, symbol, ret)
  2452.      bfd *ignore_abfd;
  2453.      asymbol *symbol;
  2454.      symbol_info *ret;
  2455. {
  2456.   bfd_symbol_info (symbol, ret);
  2457.  
  2458.   if (ret->type == '?')
  2459.     {
  2460.       int type_code = aout_symbol(symbol)->type & 0xff;
  2461.       CONST char *stab_name = aout_stab_name(type_code);
  2462.       static char buf[10];
  2463.  
  2464.       if (stab_name == NULL)
  2465.     {
  2466.       sprintf(buf, "(%d)", type_code);
  2467.       stab_name = buf;
  2468.     }
  2469.       ret->type = '-';
  2470.       ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
  2471.       ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
  2472.       ret->stab_name = stab_name;
  2473.     }
  2474. }
  2475.  
  2476. /*ARGSUSED*/
  2477. void
  2478. NAME(aout,print_symbol) (ignore_abfd, afile, symbol, how)
  2479.      bfd *ignore_abfd;
  2480.      PTR afile;
  2481.      asymbol *symbol;
  2482.      bfd_print_symbol_type how;
  2483. {
  2484.   FILE *file = (FILE *)afile;
  2485.  
  2486.   switch (how) {
  2487.   case bfd_print_symbol_name:
  2488.     if (symbol->name)
  2489.       fprintf(file,"%s", symbol->name);
  2490.     break;
  2491.   case bfd_print_symbol_more:
  2492.     fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
  2493.         (unsigned)(aout_symbol(symbol)->other & 0xff),
  2494.         (unsigned)(aout_symbol(symbol)->type));
  2495.     break;
  2496.   case bfd_print_symbol_all:
  2497.     {
  2498.    CONST char *section_name = symbol->section->name;
  2499.  
  2500.  
  2501.       bfd_print_symbol_vandf((PTR)file,symbol);
  2502.  
  2503.       fprintf(file," %-5s %04x %02x %02x",
  2504.           section_name,
  2505.           (unsigned)(aout_symbol(symbol)->desc & 0xffff),
  2506.           (unsigned)(aout_symbol(symbol)->other & 0xff),
  2507.           (unsigned)(aout_symbol(symbol)->type  & 0xff));
  2508.       if (symbol->name)
  2509.         fprintf(file," %s", symbol->name);
  2510.     }
  2511.     break;
  2512.   }
  2513. }
  2514.  
  2515. /*
  2516.  provided a BFD, a section and an offset into the section, calculate
  2517.  and return the name of the source file and the line nearest to the
  2518.  wanted location.
  2519. */
  2520.  
  2521. boolean
  2522. NAME(aout,find_nearest_line)
  2523.      (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
  2524.      bfd *abfd;
  2525.      asection *section;
  2526.      asymbol **symbols;
  2527.      bfd_vma offset;
  2528.      CONST char **filename_ptr;
  2529.      CONST char **functionname_ptr;
  2530.      unsigned int *line_ptr;
  2531. {
  2532.   /* Run down the file looking for the filename, function and linenumber */
  2533.   asymbol **p;
  2534.   static  char buffer[100];
  2535.   static  char filename_buffer[200];
  2536.   CONST char *directory_name = NULL;
  2537.   CONST char *main_file_name = NULL;
  2538.   CONST char *current_file_name = NULL;
  2539.   CONST char *line_file_name = NULL; /* Value of current_file_name at line number. */
  2540.   bfd_vma high_line_vma = ~0;
  2541.   bfd_vma low_func_vma = 0;
  2542.   asymbol *func = 0;
  2543.   *filename_ptr = abfd->filename;
  2544.   *functionname_ptr = 0;
  2545.   *line_ptr = 0;
  2546.   if (symbols != (asymbol **)NULL) {
  2547.     for (p = symbols; *p; p++) {
  2548.       aout_symbol_type  *q = (aout_symbol_type *)(*p);
  2549.     next:
  2550.       switch (q->type){
  2551.       case N_SO:
  2552.     main_file_name = current_file_name = q->symbol.name;
  2553.     /* Look ahead to next symbol to check if that too is an N_SO. */
  2554.     p++;
  2555.     if (*p == NULL)
  2556.       break;
  2557.     q = (aout_symbol_type *)(*p);
  2558.     if (q->type != (int)N_SO)
  2559.       goto next;
  2560.  
  2561.     /* Found a second N_SO  First is directory; second is filename. */
  2562.     directory_name = current_file_name;
  2563.     main_file_name = current_file_name = q->symbol.name;
  2564.     if (obj_textsec(abfd) != section)
  2565.       goto done;
  2566.     break;
  2567.       case N_SOL:
  2568.     current_file_name = q->symbol.name;
  2569.     break;
  2570.  
  2571.       case N_SLINE:
  2572.  
  2573.       case N_DSLINE:
  2574.       case N_BSLINE:
  2575.     /* We'll keep this if it resolves nearer than the one we have already */
  2576.     if (q->symbol.value >= offset &&
  2577.         q->symbol.value < high_line_vma) {
  2578.       *line_ptr = q->desc;
  2579.       high_line_vma = q->symbol.value;
  2580.       line_file_name = current_file_name;
  2581.     }
  2582.     break;
  2583.       case N_FUN:
  2584.     {
  2585.       /* We'll keep this if it is nearer than the one we have already */
  2586.       if (q->symbol.value >= low_func_vma &&
  2587.           q->symbol.value <= offset) {
  2588.         low_func_vma = q->symbol.value;
  2589.         func = (asymbol *)q;
  2590.       }
  2591.       if (*line_ptr && func) {
  2592.         CONST char *function = func->name;
  2593.         char *p;
  2594.  
  2595.         /* The caller expects a symbol name.  We actually have a
  2596.            function name, without the leading underscore.  Put the
  2597.            underscore back in, so that the caller gets a symbol
  2598.            name.  */
  2599.         if (bfd_get_symbol_leading_char (abfd) == '\0')
  2600.           strncpy (buffer, function, sizeof (buffer) - 1);
  2601.         else
  2602.           {
  2603.         buffer[0] = bfd_get_symbol_leading_char (abfd);
  2604.         strncpy (buffer + 1, function, sizeof (buffer) - 2);
  2605.           }
  2606.         buffer[sizeof(buffer)-1] = 0;
  2607.         /* Have to remove : stuff */
  2608.         p = strchr(buffer,':');
  2609.         if (p != NULL) { *p = '\0'; }
  2610.         *functionname_ptr = buffer;
  2611.         goto done;
  2612.  
  2613.       }
  2614.     }
  2615.     break;
  2616.       }
  2617.     }
  2618.   }
  2619.  
  2620.  done:
  2621.   if (*line_ptr)
  2622.     main_file_name = line_file_name;
  2623.   if (main_file_name) {
  2624.       if (main_file_name[0] == '/' || directory_name == NULL)
  2625.       *filename_ptr = main_file_name;
  2626.       else {
  2627.       sprintf(filename_buffer, "%.140s%.50s",
  2628.           directory_name, main_file_name);
  2629.       *filename_ptr = filename_buffer;
  2630.       }
  2631.   }
  2632.   return true;
  2633.  
  2634. }
  2635.  
  2636. /*ARGSUSED*/
  2637. int
  2638. NAME(aout,sizeof_headers) (abfd, execable)
  2639.      bfd *abfd;
  2640.      boolean execable;
  2641. {
  2642.   return adata(abfd).exec_bytes_size;
  2643. }
  2644.  
  2645. /* Free all information we have cached for this BFD.  We can always
  2646.    read it again later if we need it.  */
  2647.  
  2648. boolean
  2649. NAME(aout,bfd_free_cached_info) (abfd)
  2650.      bfd *abfd;
  2651. {
  2652.   asection *o;
  2653.  
  2654.   if (bfd_get_format (abfd) != bfd_object)
  2655.     return true;
  2656.  
  2657. #define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
  2658.   BFCI_FREE (obj_aout_symbols (abfd));
  2659.   BFCI_FREE (obj_aout_external_syms (abfd));
  2660.   BFCI_FREE (obj_aout_external_strings (abfd));
  2661.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  2662.     BFCI_FREE (o->relocation);
  2663. #undef BFCI_FREE
  2664.  
  2665.   return true;
  2666. }
  2667.  
  2668. /* a.out link code.  */
  2669.  
  2670. static boolean aout_link_add_object_symbols
  2671.   PARAMS ((bfd *, struct bfd_link_info *));
  2672. static boolean aout_link_check_archive_element
  2673.   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
  2674. static boolean aout_link_free_symbols PARAMS ((bfd *));
  2675. static boolean aout_link_check_ar_symbols
  2676.   PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
  2677. static boolean aout_link_add_symbols
  2678.   PARAMS ((bfd *, struct bfd_link_info *));
  2679.  
  2680. /* Routine to create an entry in an a.out link hash table.  */
  2681.  
  2682. struct bfd_hash_entry *
  2683. NAME(aout,link_hash_newfunc) (entry, table, string)
  2684.      struct bfd_hash_entry *entry;
  2685.      struct bfd_hash_table *table;
  2686.      const char *string;
  2687. {
  2688.   struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
  2689.  
  2690.   /* Allocate the structure if it has not already been allocated by a
  2691.      subclass.  */
  2692.   if (ret == (struct aout_link_hash_entry *) NULL)
  2693.     ret = ((struct aout_link_hash_entry *)
  2694.        bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
  2695.   if (ret == (struct aout_link_hash_entry *) NULL)
  2696.     {
  2697.       bfd_set_error (bfd_error_no_memory);
  2698.       return (struct bfd_hash_entry *) ret;
  2699.     }
  2700.  
  2701.   /* Call the allocation method of the superclass.  */
  2702.   ret = ((struct aout_link_hash_entry *)
  2703.      _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  2704.                  table, string));
  2705.   if (ret)
  2706.     {
  2707.       /* Set local fields.  */
  2708.       ret->written = false;
  2709.       ret->indx = -1;
  2710.     }
  2711.  
  2712.   return (struct bfd_hash_entry *) ret;
  2713. }
  2714.  
  2715. /* Initialize an a.out link hash table.  */
  2716.  
  2717. boolean
  2718. NAME(aout,link_hash_table_init) (table, abfd, newfunc)
  2719.      struct aout_link_hash_table *table;
  2720.      bfd *abfd;
  2721.      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
  2722.                         struct bfd_hash_table *,
  2723.                         const char *));
  2724. {
  2725.   return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
  2726. }
  2727.  
  2728. /* Create an a.out link hash table.  */
  2729.  
  2730. struct bfd_link_hash_table *
  2731. NAME(aout,link_hash_table_create) (abfd)
  2732.      bfd *abfd;
  2733. {
  2734.   struct aout_link_hash_table *ret;
  2735.  
  2736.   ret = ((struct aout_link_hash_table *)
  2737.      malloc (sizeof (struct aout_link_hash_table)));
  2738.   if (ret == (struct aout_link_hash_table *) NULL)
  2739.       {
  2740.     bfd_set_error (bfd_error_no_memory);
  2741.     return (struct bfd_link_hash_table *) NULL;
  2742.       }
  2743.   if (! NAME(aout,link_hash_table_init) (ret, abfd,
  2744.                      NAME(aout,link_hash_newfunc)))
  2745.     {
  2746.       free (ret);
  2747.       return (struct bfd_link_hash_table *) NULL;
  2748.     }
  2749.   return &ret->root;
  2750. }
  2751.  
  2752. /* Given an a.out BFD, add symbols to the global hash table as
  2753.    appropriate.  */
  2754.  
  2755. boolean
  2756. NAME(aout,link_add_symbols) (abfd, info)
  2757.      bfd *abfd;
  2758.      struct bfd_link_info *info;
  2759. {
  2760.   bfd *first;
  2761.  
  2762.   switch (bfd_get_format (abfd))
  2763.     {
  2764.     case bfd_object:
  2765.       return aout_link_add_object_symbols (abfd, info);
  2766.     case bfd_archive:
  2767.       first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
  2768.       if (first == NULL)
  2769.     return false;
  2770.       if (! bfd_check_format (first, bfd_object))
  2771.     return false;
  2772.       if (bfd_get_flavour (first) != bfd_target_aout_flavour)
  2773.     {
  2774.       /* On Linux, we may have an ELF archive which got recognized
  2775.              as an a.out archive.  Therefore, we treat all archives as
  2776.              though they were actually of the flavour of their first
  2777.              element.  */
  2778.       return (*first->xvec->_bfd_link_add_symbols) (abfd, info);
  2779.     }
  2780.       return _bfd_generic_link_add_archive_symbols
  2781.     (abfd, info, aout_link_check_archive_element);
  2782.     default:
  2783.       bfd_set_error (bfd_error_wrong_format);
  2784.       return false;
  2785.     }
  2786. }
  2787.  
  2788. /* Add symbols from an a.out object file.  */
  2789.  
  2790. static boolean
  2791. aout_link_add_object_symbols (abfd, info)
  2792.      bfd *abfd;
  2793.      struct bfd_link_info *info;
  2794. {
  2795.   if (! aout_get_external_symbols (abfd))
  2796.     return false;
  2797.   if (! aout_link_add_symbols (abfd, info))
  2798.     return false;
  2799.   if (! info->keep_memory)
  2800.     {
  2801.       if (! aout_link_free_symbols (abfd))
  2802.     return false;
  2803.     }
  2804.   return true;
  2805. }
  2806.  
  2807. /* Check a single archive element to see if we need to include it in
  2808.    the link.  *PNEEDED is set according to whether this element is
  2809.    needed in the link or not.  This is called from
  2810.    _bfd_generic_link_add_archive_symbols.  */
  2811.  
  2812. static boolean
  2813. aout_link_check_archive_element (abfd, info, pneeded)
  2814.      bfd *abfd;
  2815.      struct bfd_link_info *info;
  2816.      boolean *pneeded;
  2817. {
  2818.   if (! aout_get_external_symbols (abfd))
  2819.     return false;
  2820.  
  2821.   if (! aout_link_check_ar_symbols (abfd, info, pneeded))
  2822.     return false;
  2823.  
  2824.   if (*pneeded)
  2825.     {
  2826.       if (! aout_link_add_symbols (abfd, info))
  2827.     return false;
  2828.     }
  2829.  
  2830.   if (! info->keep_memory || ! *pneeded)
  2831.     {
  2832.       if (! aout_link_free_symbols (abfd))
  2833.     return false;
  2834.     }
  2835.  
  2836.   return true;
  2837. }
  2838.  
  2839. /* Free up the internal symbols read from an a.out file.  */
  2840.  
  2841. static boolean
  2842. aout_link_free_symbols (abfd)
  2843.      bfd *abfd;
  2844. {
  2845.   if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
  2846.     {
  2847.       free ((PTR) obj_aout_external_syms (abfd));
  2848.       obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
  2849.     }
  2850.   if (obj_aout_external_strings (abfd) != (char *) NULL)
  2851.     {
  2852.       free ((PTR) obj_aout_external_strings (abfd));
  2853.       obj_aout_external_strings (abfd) = (char *) NULL;
  2854.     }
  2855.   return true;
  2856. }
  2857.  
  2858. /* Look through the internal symbols to see if this object file should
  2859.    be included in the link.  We should include this object file if it
  2860.    defines any symbols which are currently undefined.  If this object
  2861.    file defines a common symbol, then we may adjust the size of the
  2862.    known symbol but we do not include the object file in the link
  2863.    (unless there is some other reason to include it).  */
  2864.  
  2865. static boolean
  2866. aout_link_check_ar_symbols (abfd, info, pneeded)
  2867.      bfd *abfd;
  2868.      struct bfd_link_info *info;
  2869.      boolean *pneeded;
  2870. {
  2871.   register struct external_nlist *p;
  2872.   struct external_nlist *pend;
  2873.   char *strings;
  2874.  
  2875.   *pneeded = false;
  2876.  
  2877.   /* Look through all the symbols.  */
  2878.   p = obj_aout_external_syms (abfd);
  2879.   pend = p + obj_aout_external_sym_count (abfd);
  2880.   strings = obj_aout_external_strings (abfd);
  2881.   for (; p < pend; p++)
  2882.     {
  2883.       int type = bfd_h_get_8 (abfd, p->e_type);
  2884.       const char *name;
  2885.       struct bfd_link_hash_entry *h;
  2886.  
  2887.       /* Ignore symbols that are not externally visible.  This is an
  2888.      optimization only, as we check the type more thoroughly
  2889.      below.  */
  2890.       if (((type & N_EXT) == 0
  2891.        || (type & N_STAB) != 0
  2892.        || type == N_FN)
  2893.       && type != N_WEAKA
  2894.       && type != N_WEAKT
  2895.       && type != N_WEAKD
  2896.       && type != N_WEAKB)
  2897.     {
  2898.       if (type == N_WARNING
  2899.           || type == N_INDR)
  2900.         ++p;
  2901.       continue;
  2902.     }
  2903.  
  2904.       name = strings + GET_WORD (abfd, p->e_strx);
  2905.       h = bfd_link_hash_lookup (info->hash, name, false, false, true);
  2906.  
  2907.       /* We are only interested in symbols that are currently
  2908.      undefined or common.  */
  2909.       if (h == (struct bfd_link_hash_entry *) NULL
  2910.       || (h->type != bfd_link_hash_undefined
  2911.           && h->type != bfd_link_hash_common))
  2912.     {
  2913.       if (type == (N_INDR | N_EXT))
  2914.         ++p;
  2915.       continue;
  2916.     }
  2917.  
  2918.       if (type == (N_TEXT | N_EXT)
  2919.       || type == (N_DATA | N_EXT)
  2920.       || type == (N_BSS | N_EXT)
  2921.       || type == (N_ABS | N_EXT)
  2922.       || type == (N_INDR | N_EXT))
  2923.     {
  2924.       /* This object file defines this symbol.  We must link it
  2925.          in.  This is true regardless of whether the current
  2926.          definition of the symbol is undefined or common.  If the
  2927.          current definition is common, we have a case in which we
  2928.          have already seen an object file including
  2929.              int a;
  2930.          and this object file from the archive includes
  2931.              int a = 5;
  2932.          In such a case we must include this object file.
  2933.  
  2934.          FIXME: The SunOS 4.1.3 linker will pull in the archive
  2935.          element if the symbol is defined in the .data section,
  2936.          but not if it is defined in the .text section.  That
  2937.          seems a bit crazy to me, and I haven't implemented it.
  2938.          However, it might be correct.  */
  2939.       if (! (*info->callbacks->add_archive_element) (info, abfd, name))
  2940.         return false;
  2941.       *pneeded = true;
  2942.       return true;
  2943.     }
  2944.  
  2945.       if (type == (N_UNDF | N_EXT))
  2946.     {
  2947.       bfd_vma value;
  2948.  
  2949.       value = GET_WORD (abfd, p->e_value);
  2950.       if (value != 0)
  2951.         {
  2952.           /* This symbol is common in the object from the archive
  2953.          file.  */
  2954.           if (h->type == bfd_link_hash_undefined)
  2955.         {
  2956.           bfd *symbfd;
  2957.           unsigned int power;
  2958.  
  2959.           symbfd = h->u.undef.abfd;
  2960.           if (symbfd == (bfd *) NULL)
  2961.             {
  2962.               /* This symbol was created as undefined from
  2963.              outside BFD.  We assume that we should link
  2964.              in the object file.  This is done for the -u
  2965.              option in the linker.  */
  2966.               if (! (*info->callbacks->add_archive_element) (info,
  2967.                                      abfd,
  2968.                                      name))
  2969.             return false;
  2970.               *pneeded = true;
  2971.               return true;
  2972.             }
  2973.           /* Turn the current link symbol into a common
  2974.              symbol.  It is already on the undefs list.  */
  2975.           h->type = bfd_link_hash_common;
  2976.           h->u.c.size = value;
  2977.           if (h->u.c.size != value)
  2978.             {
  2979.               /* The size did not fit in the bitfield.  */
  2980.               bfd_set_error (bfd_error_bad_value);
  2981.               return false;
  2982.             }
  2983.  
  2984.           /* FIXME: This isn't quite right.  The maximum
  2985.              alignment of a common symbol should be set by the
  2986.              architecture of the output file, not of the input
  2987.              file.  */
  2988.           power = bfd_log2 (value);
  2989.           if (power > bfd_get_arch_info (abfd)->section_align_power)
  2990.             power = bfd_get_arch_info (abfd)->section_align_power;
  2991.           h->u.c.alignment_power = power;
  2992.  
  2993.           h->u.c.section = bfd_make_section_old_way (symbfd,
  2994.                                  "COMMON");
  2995.         }
  2996.           else
  2997.         {
  2998.           /* Adjust the size of the common symbol if
  2999.              necessary.  */
  3000.           if (value > h->u.c.size)
  3001.             h->u.c.size = value;
  3002.         }
  3003.         }
  3004.     }
  3005.  
  3006.       if (type == N_WEAKA
  3007.       || type == N_WEAKT
  3008.       || type == N_WEAKD
  3009.       || type == N_WEAKB)
  3010.     {
  3011.       /* This symbol is weak but defined.  We must pull it in if
  3012.          the current link symbol is undefined, but we don't want
  3013.          it if the current link symbol is common.  */
  3014.       if (h->type == bfd_link_hash_undefined)
  3015.         {
  3016.           if (! (*info->callbacks->add_archive_element) (info, abfd, name))
  3017.         return false;
  3018.           *pneeded = true;
  3019.           return true;
  3020.         }
  3021.     }
  3022.     }
  3023.  
  3024.   /* We do not need this object file.  */
  3025.   return true;
  3026. }
  3027.  
  3028. /* Add all symbols from an object file to the hash table.  */
  3029.  
  3030. static boolean
  3031. aout_link_add_symbols (abfd, info)
  3032.      bfd *abfd;
  3033.      struct bfd_link_info *info;
  3034. {
  3035.   boolean (*add_one_symbol) PARAMS ((struct bfd_link_info *, bfd *,
  3036.                      const char *, flagword, asection *,
  3037.                      bfd_vma, const char *, boolean,
  3038.                      boolean,
  3039.                      struct bfd_link_hash_entry **));
  3040.   bfd_size_type sym_count;
  3041.   char *strings;
  3042.   boolean copy;
  3043.   struct aout_link_hash_entry **sym_hash;
  3044.   register struct external_nlist *p;
  3045.   struct external_nlist *pend;
  3046.  
  3047.   sym_count = obj_aout_external_sym_count (abfd);
  3048.   strings = obj_aout_external_strings (abfd);
  3049.   if (info->keep_memory)
  3050.     copy = false;
  3051.   else
  3052.     copy = true;
  3053.  
  3054.   /* We keep a list of the linker hash table entries that correspond
  3055.      to particular symbols.  We could just look them up in the hash
  3056.      table, but keeping the list is more efficient.  Perhaps this
  3057.      should be conditional on info->keep_memory.  */
  3058.   sym_hash = ((struct aout_link_hash_entry **)
  3059.           bfd_alloc (abfd,
  3060.              ((size_t) sym_count
  3061.               * sizeof (struct aout_link_hash_entry *))));
  3062.   if (sym_hash == NULL && sym_count != 0)
  3063.     {
  3064.       bfd_set_error (bfd_error_no_memory);
  3065.       return false;
  3066.     }
  3067.   obj_aout_sym_hashes (abfd) = sym_hash;
  3068.  
  3069.   if ((abfd->flags & DYNAMIC) != 0
  3070.       && aout_backend_info (abfd)->add_dynamic_symbols != NULL)
  3071.     {
  3072.       if (! (*aout_backend_info (abfd)->add_dynamic_symbols) (abfd, info))
  3073.     return false;
  3074.     }
  3075.  
  3076.   add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
  3077.   if (add_one_symbol == NULL)
  3078.     add_one_symbol = _bfd_generic_link_add_one_symbol;
  3079.  
  3080.   p = obj_aout_external_syms (abfd);
  3081.   pend = p + sym_count;
  3082.   for (; p < pend; p++, sym_hash++)
  3083.     {
  3084.       int type;
  3085.       const char *name;
  3086.       bfd_vma value;
  3087.       asection *section;
  3088.       flagword flags;
  3089.       const char *string;
  3090.  
  3091.       *sym_hash = NULL;
  3092.  
  3093.       type = bfd_h_get_8 (abfd, p->e_type);
  3094.  
  3095.       /* Ignore debugging symbols.  */
  3096.       if ((type & N_STAB) != 0)
  3097.     continue;
  3098.  
  3099.       name = strings + GET_WORD (abfd, p->e_strx);
  3100.       value = GET_WORD (abfd, p->e_value);
  3101.       flags = BSF_GLOBAL;
  3102.       string = NULL;
  3103.       switch (type)
  3104.     {
  3105.     default:
  3106.       abort ();
  3107.  
  3108.     case N_UNDF:
  3109.     case N_ABS:
  3110.     case N_TEXT:
  3111.     case N_DATA:
  3112.     case N_BSS:
  3113.     case N_FN_SEQ:
  3114.     case N_COMM:
  3115.     case N_SETV:
  3116.     case N_FN:
  3117.       /* Ignore symbols that are not externally visible.  */
  3118.       continue;
  3119.     case N_INDR:
  3120.       /* Ignore local indirect symbol.  */
  3121.       ++p;
  3122.       ++sym_hash;
  3123.       continue;
  3124.  
  3125.     case N_UNDF | N_EXT:
  3126.       if (value == 0)
  3127.         {
  3128.           section = bfd_und_section_ptr;
  3129.           flags = 0;
  3130.         }
  3131.       else
  3132.         section = bfd_com_section_ptr;
  3133.       break;
  3134.     case N_ABS | N_EXT:
  3135.       section = bfd_abs_section_ptr;
  3136.       break;
  3137.     case N_TEXT | N_EXT:
  3138.       section = obj_textsec (abfd);
  3139.       value -= bfd_get_section_vma (abfd, section);
  3140.       break;
  3141.     case N_DATA | N_EXT:
  3142.     case N_SETV | N_EXT:
  3143.       /* Treat N_SETV symbols as N_DATA symbol; see comment in
  3144.          translate_from_native_sym_flags.  */
  3145.       section = obj_datasec (abfd);
  3146.       value -= bfd_get_section_vma (abfd, section);
  3147.       break;
  3148.     case N_BSS | N_EXT:
  3149.       section = obj_bsssec (abfd);
  3150.       value -= bfd_get_section_vma (abfd, section);
  3151.       break;
  3152.     case N_INDR | N_EXT:
  3153.       /* An indirect symbol.  The next symbol is the symbol
  3154.          which this one really is.  */
  3155.       BFD_ASSERT (p + 1 < pend);
  3156.       ++p;
  3157.       string = strings + GET_WORD (abfd, p->e_strx);
  3158.       section = bfd_ind_section_ptr;
  3159.       flags |= BSF_INDIRECT;
  3160.       break;
  3161.     case N_COMM | N_EXT:
  3162.       section = bfd_com_section_ptr;
  3163.       break;
  3164.     case N_SETA: case N_SETA | N_EXT:
  3165.       section = bfd_abs_section_ptr;
  3166.       flags |= BSF_CONSTRUCTOR;
  3167.       break;
  3168.     case N_SETT: case N_SETT | N_EXT:
  3169.       section = obj_textsec (abfd);
  3170.       flags |= BSF_CONSTRUCTOR;
  3171.       value -= bfd_get_section_vma (abfd, section);
  3172.       break;
  3173.     case N_SETD: case N_SETD | N_EXT:
  3174.       section = obj_datasec (abfd);
  3175.       flags |= BSF_CONSTRUCTOR;
  3176.       value -= bfd_get_section_vma (abfd, section);
  3177.       break;
  3178.     case N_SETB: case N_SETB | N_EXT:
  3179.       section = obj_bsssec (abfd);
  3180.       flags |= BSF_CONSTRUCTOR;
  3181.       value -= bfd_get_section_vma (abfd, section);
  3182.       break;
  3183.     case N_WARNING:
  3184.       /* A warning symbol.  The next symbol is the one to warn
  3185.          about.  */
  3186.       BFD_ASSERT (p + 1 < pend);
  3187.       ++p;
  3188.       string = name;
  3189.       name = strings + GET_WORD (abfd, p->e_strx);
  3190.       section = bfd_und_section_ptr;
  3191.       flags |= BSF_WARNING;
  3192.       break;
  3193.     case N_WEAKU:
  3194.       section = bfd_und_section_ptr;
  3195.       flags = BSF_WEAK;
  3196.       break;
  3197.     case N_WEAKA:
  3198.       section = bfd_abs_section_ptr;
  3199.       flags = BSF_WEAK;
  3200.       break;
  3201.     case N_WEAKT:
  3202.       section = obj_textsec (abfd);
  3203.       value -= bfd_get_section_vma (abfd, section);
  3204.       flags = BSF_WEAK;
  3205.       break;
  3206.     case N_WEAKD:
  3207.       section = obj_datasec (abfd);
  3208.       value -= bfd_get_section_vma (abfd, section);
  3209.       flags = BSF_WEAK;
  3210.       break;
  3211.     case N_WEAKB:
  3212.       section = obj_bsssec (abfd);
  3213.       value -= bfd_get_section_vma (abfd, section);
  3214.       flags = BSF_WEAK;
  3215.       break;
  3216.     }
  3217.  
  3218.       if (! ((*add_one_symbol)
  3219.          (info, abfd, name, flags, section, value, string, copy, false,
  3220.           (struct bfd_link_hash_entry **) sym_hash)))
  3221.     return false;
  3222.  
  3223.       /* Restrict the maximum alignment of a common symbol based on
  3224.      the architecture, since a.out has no way to represent
  3225.      alignment requirements of a section in a .o file.  FIXME:
  3226.      This isn't quite right: it should use the architecture of the
  3227.      output file, not the input files.  */
  3228.       if ((*sym_hash)->root.type == bfd_link_hash_common
  3229.       && ((*sym_hash)->root.u.c.alignment_power >
  3230.           bfd_get_arch_info (abfd)->section_align_power))
  3231.     (*sym_hash)->root.u.c.alignment_power =
  3232.       bfd_get_arch_info (abfd)->section_align_power;
  3233.  
  3234.       /* If this is a set symbol, and we are not building sets, then
  3235.      it is possible for the hash entry to not have been set.  In
  3236.      such a case, treat the symbol as not globally defined.  */
  3237.       if ((*sym_hash)->root.type == bfd_link_hash_new)
  3238.     {
  3239.       BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
  3240.       *sym_hash = NULL;
  3241.     }
  3242.  
  3243.       if (type == (N_INDR | N_EXT) || type == N_WARNING)
  3244.     ++sym_hash;
  3245.     }
  3246.  
  3247.   return true;
  3248. }
  3249.  
  3250. /* During the final link step we need to pass around a bunch of
  3251.    information, so we do it in an instance of this structure.  */
  3252.  
  3253. struct aout_final_link_info
  3254. {
  3255.   /* General link information.  */
  3256.   struct bfd_link_info *info;
  3257.   /* Output bfd.  */
  3258.   bfd *output_bfd;
  3259.   /* Reloc file positions.  */
  3260.   file_ptr treloff, dreloff;
  3261.   /* File position of symbols.  */
  3262.   file_ptr symoff;
  3263.   /* String table.  */
  3264.   struct bfd_strtab_hash *strtab;
  3265.   /* A buffer large enough to hold the contents of any section.  */
  3266.   bfd_byte *contents;
  3267.   /* A buffer large enough to hold the relocs of any section.  */
  3268.   PTR relocs;
  3269.   /* A buffer large enough to hold the symbol map of any input BFD.  */
  3270.   int *symbol_map;
  3271.   /* A buffer large enough to hold output symbols of any input BFD.  */
  3272.   struct external_nlist *output_syms;
  3273. };
  3274.  
  3275. static boolean aout_link_input_bfd
  3276.   PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
  3277. static boolean aout_link_write_symbols
  3278.   PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
  3279. static boolean aout_link_write_other_symbol
  3280.   PARAMS ((struct aout_link_hash_entry *, PTR));
  3281. static boolean aout_link_input_section
  3282.   PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
  3283.        asection *input_section, file_ptr *reloff_ptr,
  3284.        bfd_size_type rel_size));
  3285. static boolean aout_link_input_section_std
  3286.   PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
  3287.        asection *input_section, struct reloc_std_external *,
  3288.        bfd_size_type rel_size, bfd_byte *contents));
  3289. static boolean aout_link_input_section_ext
  3290.   PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
  3291.        asection *input_section, struct reloc_ext_external *,
  3292.        bfd_size_type rel_size, bfd_byte *contents));
  3293. static INLINE asection *aout_reloc_index_to_section
  3294.   PARAMS ((bfd *, int));
  3295. static boolean aout_link_reloc_link_order
  3296.   PARAMS ((struct aout_final_link_info *, asection *,
  3297.        struct bfd_link_order *));
  3298.  
  3299. /* Do the final link step.  This is called on the output BFD.  The
  3300.    INFO structure should point to a list of BFDs linked through the
  3301.    link_next field which can be used to find each BFD which takes part
  3302.    in the output.  Also, each section in ABFD should point to a list
  3303.    of bfd_link_order structures which list all the input sections for
  3304.    the output section.  */
  3305.  
  3306. boolean
  3307. NAME(aout,final_link) (abfd, info, callback)
  3308.      bfd *abfd;
  3309.      struct bfd_link_info *info;
  3310.      void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
  3311. {
  3312.   struct aout_final_link_info aout_info;
  3313.   register bfd *sub;
  3314.   bfd_size_type trsize, drsize;
  3315.   size_t max_contents_size;
  3316.   size_t max_relocs_size;
  3317.   size_t max_sym_count;
  3318.   bfd_size_type text_size;
  3319.   file_ptr text_end;
  3320.   register struct bfd_link_order *p;
  3321.   asection *o;
  3322.   boolean have_link_order_relocs;
  3323.  
  3324.   aout_info.info = info;
  3325.   aout_info.output_bfd = abfd;
  3326.   aout_info.contents = NULL;
  3327.   aout_info.relocs = NULL;
  3328.  
  3329.   /* Figure out the largest section size.  Also, if generating
  3330.      relocateable output, count the relocs.  */
  3331.   trsize = 0;
  3332.   drsize = 0;
  3333.   max_contents_size = 0;
  3334.   max_relocs_size = 0;
  3335.   max_sym_count = 0;
  3336.   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  3337.     {
  3338.       size_t sz;
  3339.  
  3340.       if (info->relocateable)
  3341.     {
  3342.       if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
  3343.         {
  3344.           trsize += exec_hdr (sub)->a_trsize;
  3345.           drsize += exec_hdr (sub)->a_drsize;
  3346.         }
  3347.       else
  3348.         {
  3349.           /* FIXME: We need to identify the .text and .data sections
  3350.          and call get_reloc_upper_bound and canonicalize_reloc to
  3351.          work out the number of relocs needed, and then multiply
  3352.          by the reloc size.  */
  3353.           abort ();
  3354.         }
  3355.     }
  3356.  
  3357.       if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
  3358.     {
  3359.       sz = bfd_section_size (sub, obj_textsec (sub));
  3360.       if (sz > max_contents_size)
  3361.         max_contents_size = sz;
  3362.       sz = bfd_section_size (sub, obj_datasec (sub));
  3363.       if (sz > max_contents_size)
  3364.         max_contents_size = sz;
  3365.  
  3366.       sz = exec_hdr (sub)->a_trsize;
  3367.       if (sz > max_relocs_size)
  3368.         max_relocs_size = sz;
  3369.       sz = exec_hdr (sub)->a_drsize;
  3370.       if (sz > max_relocs_size)
  3371.         max_relocs_size = sz;
  3372.  
  3373.       sz = obj_aout_external_sym_count (sub);
  3374.       if (sz > max_sym_count)
  3375.         max_sym_count = sz;
  3376.     }
  3377.     }
  3378.  
  3379.   if (info->relocateable)
  3380.     {
  3381.       if (obj_textsec (abfd) != (asection *) NULL)
  3382.     trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
  3383.                          ->link_order_head)
  3384.            * obj_reloc_entry_size (abfd));
  3385.       if (obj_datasec (abfd) != (asection *) NULL)
  3386.     drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
  3387.                          ->link_order_head)
  3388.            * obj_reloc_entry_size (abfd));
  3389.     }
  3390.  
  3391.   exec_hdr (abfd)->a_trsize = trsize;
  3392.   exec_hdr (abfd)->a_drsize = drsize;
  3393.  
  3394.   exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
  3395.  
  3396.   /* Adjust the section sizes and vmas according to the magic number.
  3397.      This sets a_text, a_data and a_bss in the exec_hdr and sets the
  3398.      filepos for each section.  */
  3399.   if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
  3400.     goto error_return;
  3401.  
  3402.   /* The relocation and symbol file positions differ among a.out
  3403.      targets.  We are passed a callback routine from the backend
  3404.      specific code to handle this.
  3405.      FIXME: At this point we do not know how much space the symbol
  3406.      table will require.  This will not work for any (nonstandard)
  3407.      a.out target that needs to know the symbol table size before it
  3408.      can compute the relocation file positions.  This may or may not
  3409.      be the case for the hp300hpux target, for example.  */
  3410.   (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
  3411.            &aout_info.symoff);
  3412.   obj_textsec (abfd)->rel_filepos = aout_info.treloff;
  3413.   obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
  3414.   obj_sym_filepos (abfd) = aout_info.symoff;
  3415.  
  3416.   /* We keep a count of the symbols as we output them.  */
  3417.   obj_aout_external_sym_count (abfd) = 0;
  3418.  
  3419.   /* We accumulate the string table as we write out the symbols.  */
  3420.   aout_info.strtab = _bfd_stringtab_init ();
  3421.   if (aout_info.strtab == NULL)
  3422.     goto error_return;
  3423.  
  3424.   /* Allocate buffers to hold section contents and relocs.  */
  3425.   aout_info.contents = (bfd_byte *) malloc (max_contents_size);
  3426.   aout_info.relocs = (PTR) malloc (max_relocs_size);
  3427.   aout_info.symbol_map = (int *) malloc (max_sym_count * sizeof (int *));
  3428.   aout_info.output_syms = ((struct external_nlist *)
  3429.                malloc ((max_sym_count + 1)
  3430.                    * sizeof (struct external_nlist)));
  3431.   if ((aout_info.contents == NULL && max_contents_size != 0)
  3432.       || (aout_info.relocs == NULL && max_relocs_size != 0)
  3433.       || (aout_info.symbol_map == NULL && max_sym_count != 0)
  3434.       || aout_info.output_syms == NULL)
  3435.     {
  3436.       bfd_set_error (bfd_error_no_memory);
  3437.       goto error_return;
  3438.     }
  3439.  
  3440.   /* The most time efficient way to do the link would be to read all
  3441.      the input object files into memory and then sort out the
  3442.      information into the output file.  Unfortunately, that will
  3443.      probably use too much memory.  Another method would be to step
  3444.      through everything that composes the text section and write it
  3445.      out, and then everything that composes the data section and write
  3446.      it out, and then write out the relocs, and then write out the
  3447.      symbols.  Unfortunately, that requires reading stuff from each
  3448.      input file several times, and we will not be able to keep all the
  3449.      input files open simultaneously, and reopening them will be slow.
  3450.  
  3451.      What we do is basically process one input file at a time.  We do
  3452.      everything we need to do with an input file once--copy over the
  3453.      section contents, handle the relocation information, and write
  3454.      out the symbols--and then we throw away the information we read
  3455.      from it.  This approach requires a lot of lseeks of the output
  3456.      file, which is unfortunate but still faster than reopening a lot
  3457.      of files.
  3458.  
  3459.      We use the output_has_begun field of the input BFDs to see
  3460.      whether we have already handled it.  */
  3461.   for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
  3462.     sub->output_has_begun = false;
  3463.  
  3464.   have_link_order_relocs = false;
  3465.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  3466.     {
  3467.       for (p = o->link_order_head;
  3468.        p != (struct bfd_link_order *) NULL;
  3469.        p = p->next)
  3470.     {
  3471.       if (p->type == bfd_indirect_link_order
  3472.           && (bfd_get_flavour (p->u.indirect.section->owner)
  3473.           == bfd_target_aout_flavour))
  3474.         {
  3475.           bfd *input_bfd;
  3476.  
  3477.           input_bfd = p->u.indirect.section->owner;
  3478.           if (! input_bfd->output_has_begun)
  3479.         {
  3480.           if (! aout_link_input_bfd (&aout_info, input_bfd))
  3481.             goto error_return;
  3482.           input_bfd->output_has_begun = true;
  3483.         }
  3484.         }
  3485.       else if (p->type == bfd_section_reloc_link_order
  3486.            || p->type == bfd_symbol_reloc_link_order)
  3487.         {
  3488.           /* These are handled below.  */
  3489.           have_link_order_relocs = true;
  3490.         }
  3491.       else
  3492.         {
  3493.           if (! _bfd_default_link_order (abfd, info, o, p))
  3494.         goto error_return;
  3495.         }
  3496.     }
  3497.     }
  3498.  
  3499.   /* Write out any symbols that we have not already written out.  */
  3500.   aout_link_hash_traverse (aout_hash_table (info),
  3501.                aout_link_write_other_symbol,
  3502.                (PTR) &aout_info);
  3503.  
  3504.   /* Now handle any relocs we were asked to create by the linker.
  3505.      These did not come from any input file.  We must do these after
  3506.      we have written out all the symbols, so that we know the symbol
  3507.      indices to use.  */
  3508.   if (have_link_order_relocs)
  3509.     {
  3510.       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  3511.     {
  3512.       for (p = o->link_order_head;
  3513.            p != (struct bfd_link_order *) NULL;
  3514.            p = p->next)
  3515.         {
  3516.           if (p->type == bfd_section_reloc_link_order
  3517.           || p->type == bfd_symbol_reloc_link_order)
  3518.         {
  3519.           if (! aout_link_reloc_link_order (&aout_info, o, p))
  3520.             goto error_return;
  3521.         }
  3522.         }
  3523.     }
  3524.     }
  3525.  
  3526.   if (aout_info.contents != NULL)
  3527.     {
  3528.       free (aout_info.contents);
  3529.       aout_info.contents = NULL;
  3530.     }
  3531.   if (aout_info.relocs != NULL)
  3532.     {
  3533.       free (aout_info.relocs);
  3534.       aout_info.relocs = NULL;
  3535.     }
  3536.   if (aout_info.symbol_map != NULL)
  3537.     {
  3538.       free (aout_info.symbol_map);
  3539.       aout_info.symbol_map = NULL;
  3540.     }
  3541.   if (aout_info.output_syms != NULL)
  3542.     {
  3543.       free (aout_info.output_syms);
  3544.       aout_info.output_syms = NULL;
  3545.     }
  3546.  
  3547.   /* Finish up any dynamic linking we may be doing.  */
  3548.   if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
  3549.     {
  3550.       if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
  3551.     goto error_return;
  3552.     }
  3553.  
  3554.   /* Update the header information.  */
  3555.   abfd->symcount = obj_aout_external_sym_count (abfd);
  3556.   exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
  3557.   obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
  3558.   obj_textsec (abfd)->reloc_count =
  3559.     exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
  3560.   obj_datasec (abfd)->reloc_count =
  3561.     exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
  3562.  
  3563.   /* Write out the string table.  */
  3564.   if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
  3565.     goto error_return;
  3566.   return emit_stringtab (abfd, aout_info.strtab);
  3567.  
  3568.  error_return:
  3569.   if (aout_info.contents != NULL)
  3570.     free (aout_info.contents);
  3571.   if (aout_info.relocs != NULL)
  3572.     free (aout_info.relocs);
  3573.   if (aout_info.symbol_map != NULL)
  3574.     free (aout_info.symbol_map);
  3575.   if (aout_info.output_syms != NULL)
  3576.     free (aout_info.output_syms);
  3577.   return false;
  3578. }
  3579.  
  3580. /* Link an a.out input BFD into the output file.  */
  3581.  
  3582. static boolean
  3583. aout_link_input_bfd (finfo, input_bfd)
  3584.      struct aout_final_link_info *finfo;
  3585.      bfd *input_bfd;
  3586. {
  3587.   bfd_size_type sym_count;
  3588.  
  3589.   BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
  3590.  
  3591.   /* If this is a dynamic object, it may need special handling.  */
  3592.   if ((input_bfd->flags & DYNAMIC) != 0
  3593.       && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
  3594.     {
  3595.       return ((*aout_backend_info (input_bfd)->link_dynamic_object)
  3596.           (finfo->info, input_bfd));
  3597.     }
  3598.  
  3599.   /* Get the symbols.  We probably have them already, unless
  3600.      finfo->info->keep_memory is false.  */
  3601.   if (! aout_get_external_symbols (input_bfd))
  3602.     return false;
  3603.  
  3604.   sym_count = obj_aout_external_sym_count (input_bfd);
  3605.  
  3606.   /* Write out the symbols and get a map of the new indices.  The map
  3607.      is placed into finfo->symbol_map.  */
  3608.   if (! aout_link_write_symbols (finfo, input_bfd))
  3609.     return false;
  3610.  
  3611.   /* Relocate and write out the sections.  These functions use the
  3612.      symbol map created by aout_link_write_symbols.  */
  3613.   if (! aout_link_input_section (finfo, input_bfd,
  3614.                  obj_textsec (input_bfd),
  3615.                  &finfo->treloff,
  3616.                  exec_hdr (input_bfd)->a_trsize)
  3617.       || ! aout_link_input_section (finfo, input_bfd,
  3618.                     obj_datasec (input_bfd),
  3619.                     &finfo->dreloff,
  3620.                     exec_hdr (input_bfd)->a_drsize))
  3621.     return false;
  3622.  
  3623.   /* If we are not keeping memory, we don't need the symbols any
  3624.      longer.  We still need them if we are keeping memory, because the
  3625.      strings in the hash table point into them.  */
  3626.   if (! finfo->info->keep_memory)
  3627.     {
  3628.       if (! aout_link_free_symbols (input_bfd))
  3629.     return false;
  3630.     }
  3631.  
  3632.   return true;
  3633. }
  3634.  
  3635. /* Adjust and write out the symbols for an a.out file.  Set the new
  3636.    symbol indices into a symbol_map.  */
  3637.  
  3638. static boolean
  3639. aout_link_write_symbols (finfo, input_bfd)
  3640.      struct aout_final_link_info *finfo;
  3641.      bfd *input_bfd;
  3642. {
  3643.   bfd *output_bfd;
  3644.   bfd_size_type sym_count;
  3645.   char *strings;
  3646.   enum bfd_link_strip strip;
  3647.   enum bfd_link_discard discard;
  3648.   struct external_nlist *outsym;
  3649.   bfd_size_type strtab_index;
  3650.   register struct external_nlist *sym;
  3651.   struct external_nlist *sym_end;
  3652.   struct aout_link_hash_entry **sym_hash;
  3653.   int *symbol_map;
  3654.   boolean pass;
  3655.   boolean skip_next;
  3656.  
  3657.   output_bfd = finfo->output_bfd;
  3658.   sym_count = obj_aout_external_sym_count (input_bfd);
  3659.   strings = obj_aout_external_strings (input_bfd);
  3660.   strip = finfo->info->strip;
  3661.   discard = finfo->info->discard;
  3662.   outsym = finfo->output_syms;
  3663.  
  3664.   /* First write out a symbol for this object file, unless we are
  3665.      discarding such symbols.  */
  3666.   if (strip != strip_all
  3667.       && (strip != strip_some
  3668.       || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
  3669.                   false, false) != NULL)
  3670.       && discard != discard_all)
  3671.     {
  3672.       bfd_h_put_8 (output_bfd, N_TEXT, outsym->e_type);
  3673.       bfd_h_put_8 (output_bfd, 0, outsym->e_other);
  3674.       bfd_h_put_16 (output_bfd, (bfd_vma) 0, outsym->e_desc);
  3675.       strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
  3676.                        input_bfd->filename, false);
  3677.       if (strtab_index == (bfd_size_type) -1)
  3678.     return false;
  3679.       PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
  3680.       PUT_WORD (output_bfd,
  3681.         (bfd_get_section_vma (output_bfd,
  3682.                       obj_textsec (input_bfd)->output_section)
  3683.          + obj_textsec (input_bfd)->output_offset),
  3684.         outsym->e_value);
  3685.       ++obj_aout_external_sym_count (output_bfd);
  3686.       ++outsym;
  3687.     }
  3688.  
  3689.   pass = false;
  3690.   skip_next = false;
  3691.   sym = obj_aout_external_syms (input_bfd);
  3692.   sym_end = sym + sym_count;
  3693.   sym_hash = obj_aout_sym_hashes (input_bfd);
  3694.   symbol_map = finfo->symbol_map;
  3695.   for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
  3696.     {
  3697.       const char *name;
  3698.       int type;
  3699.       struct aout_link_hash_entry *h;
  3700.       boolean skip;
  3701.       asection *symsec;
  3702.       bfd_vma val = 0;
  3703.       boolean copy;
  3704.  
  3705.       *symbol_map = -1;
  3706.  
  3707.       type = bfd_h_get_8 (input_bfd, sym->e_type);
  3708.       name = strings + GET_WORD (input_bfd, sym->e_strx);
  3709.  
  3710.       h = NULL;
  3711.  
  3712.       if (pass)
  3713.     {
  3714.       /* Pass this symbol through.  It is the target of an
  3715.          indirect or warning symbol.  */
  3716.       val = GET_WORD (input_bfd, sym->e_value);
  3717.       pass = false;
  3718.     }
  3719.       else if (skip_next)
  3720.     {
  3721.       /* Skip this symbol, which is the target of an indirect
  3722.          symbol that we have changed to no longer be an indirect
  3723.          symbol.  */
  3724.       skip_next = false;
  3725.       continue;
  3726.     }
  3727.       else
  3728.     {
  3729.       struct aout_link_hash_entry *hresolve;
  3730.  
  3731.       /* We have saved the hash table entry for this symbol, if
  3732.          there is one.  Note that we could just look it up again
  3733.          in the hash table, provided we first check that it is an
  3734.          external symbol. */
  3735.       h = *sym_hash;
  3736.  
  3737.       /* If this is an indirect or warning symbol, then change
  3738.          hresolve to the base symbol.  We also change *sym_hash so
  3739.          that the relocation routines relocate against the real
  3740.          symbol.  */
  3741.       hresolve = h;
  3742.       if (h != (struct aout_link_hash_entry *) NULL
  3743.           && (h->root.type == bfd_link_hash_indirect
  3744.           || h->root.type == bfd_link_hash_warning))
  3745.         {
  3746.           hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
  3747.           while (hresolve->root.type == bfd_link_hash_indirect
  3748.              || hresolve->root.type == bfd_link_hash_warning)
  3749.         hresolve = ((struct aout_link_hash_entry *)
  3750.                 hresolve->root.u.i.link);
  3751.           *sym_hash = hresolve;
  3752.         }
  3753.  
  3754.       /* If the symbol has already been written out, skip it.  */
  3755.       if (h != (struct aout_link_hash_entry *) NULL
  3756.           && h->root.type != bfd_link_hash_warning
  3757.           && h->written)
  3758.         {
  3759.           if ((type & N_TYPE) == N_INDR
  3760.           || type == N_WARNING)
  3761.         skip_next = true;
  3762.           *symbol_map = h->indx;
  3763.           continue;
  3764.         }
  3765.  
  3766.       /* See if we are stripping this symbol.  */
  3767.       skip = false;
  3768.       switch (strip)
  3769.         {
  3770.         case strip_none:
  3771.           break;
  3772.         case strip_debugger:
  3773.           if ((type & N_STAB) != 0)
  3774.         skip = true;
  3775.           break;
  3776.         case strip_some:
  3777.           if (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
  3778.           == NULL)
  3779.         skip = true;
  3780.           break;
  3781.         case strip_all:
  3782.           skip = true;
  3783.           break;
  3784.         }
  3785.       if (skip)
  3786.         {
  3787.           if (h != (struct aout_link_hash_entry *) NULL)
  3788.         h->written = true;
  3789.           continue;
  3790.         }
  3791.  
  3792.       /* Get the value of the symbol.  */
  3793.       if ((type & N_TYPE) == N_TEXT
  3794.           || type == N_WEAKT)
  3795.         symsec = obj_textsec (input_bfd);
  3796.       else if ((type & N_TYPE) == N_DATA
  3797.            || type == N_WEAKD)
  3798.         symsec = obj_datasec (input_bfd);
  3799.       else if ((type & N_TYPE) == N_BSS
  3800.            || type == N_WEAKB)
  3801.         symsec = obj_bsssec (input_bfd);
  3802.       else if ((type & N_TYPE) == N_ABS
  3803.            || type == N_WEAKA)
  3804.         symsec = bfd_abs_section_ptr;
  3805.       else if (((type & N_TYPE) == N_INDR
  3806.             && (hresolve == (struct aout_link_hash_entry *) NULL
  3807.             || (hresolve->root.type != bfd_link_hash_defined
  3808.                 && hresolve->root.type != bfd_link_hash_common)))
  3809.            || type == N_WARNING)
  3810.         {
  3811.           /* Pass the next symbol through unchanged.  The
  3812.          condition above for indirect symbols is so that if
  3813.          the indirect symbol was defined, we output it with
  3814.          the correct definition so the debugger will
  3815.          understand it.  */
  3816.           pass = true;
  3817.           val = GET_WORD (input_bfd, sym->e_value);
  3818.           symsec = NULL;
  3819.         }
  3820.       else if ((type & N_STAB) != 0)
  3821.         {
  3822.           val = GET_WORD (input_bfd, sym->e_value);
  3823.           symsec = NULL;
  3824.         }
  3825.       else
  3826.         {
  3827.           /* If we get here with an indirect symbol, it means that
  3828.          we are outputting it with a real definition.  In such
  3829.          a case we do not want to output the next symbol,
  3830.          which is the target of the indirection.  */
  3831.           if ((type & N_TYPE) == N_INDR)
  3832.         skip_next = true;
  3833.  
  3834.           symsec = NULL;
  3835.  
  3836.           /* We need to get the value from the hash table.  We use
  3837.          hresolve so that if we have defined an indirect
  3838.          symbol we output the final definition.  */
  3839.           if (h == (struct aout_link_hash_entry *) NULL)
  3840.         {
  3841.           switch (type & N_TYPE)
  3842.             {
  3843.             case N_SETT:
  3844.               symsec = obj_textsec (input_bfd);
  3845.               break;
  3846.             case N_SETD:
  3847.               symsec = obj_datasec (input_bfd);
  3848.               break;
  3849.             case N_SETB:
  3850.               symsec = obj_bsssec (input_bfd);
  3851.               break;
  3852.             case N_SETA:
  3853.               symsec = bfd_abs_section_ptr;
  3854.               break;
  3855.             default:
  3856.               val = 0;
  3857.               break;
  3858.             }
  3859.         }
  3860.           else if (hresolve->root.type == bfd_link_hash_defined)
  3861.         {
  3862.           asection *input_section;
  3863.           asection *output_section;
  3864.  
  3865.           /* This case means a common symbol which was turned
  3866.              into a defined symbol.  */
  3867.           input_section = hresolve->root.u.def.section;
  3868.           output_section = input_section->output_section;
  3869.           BFD_ASSERT (bfd_is_abs_section (output_section)
  3870.                   || output_section->owner == output_bfd);
  3871.           val = (hresolve->root.u.def.value
  3872.              + bfd_get_section_vma (output_bfd, output_section)
  3873.              + input_section->output_offset);
  3874.  
  3875.           /* Get the correct type based on the section.  If
  3876.              this is a constructed set, force it to be
  3877.              globally visible.  */
  3878.           if (type == N_SETT
  3879.               || type == N_SETD
  3880.               || type == N_SETB
  3881.               || type == N_SETA)
  3882.             type |= N_EXT;
  3883.  
  3884.           type &=~ N_TYPE;
  3885.  
  3886.           if (output_section == obj_textsec (output_bfd))
  3887.             type |= N_TEXT;
  3888.           else if (output_section == obj_datasec (output_bfd))
  3889.             type |= N_DATA;
  3890.           else if (output_section == obj_bsssec (output_bfd))
  3891.             type |= N_BSS;
  3892.           else
  3893.             type |= N_ABS;
  3894.         }
  3895.           else if (hresolve->root.type == bfd_link_hash_common)
  3896.         val = hresolve->root.u.c.size;
  3897.           else if (hresolve->root.type == bfd_link_hash_weak)
  3898.         {
  3899.           val = 0;
  3900.           type = N_WEAKU;
  3901.         }
  3902.           else
  3903.         val = 0;
  3904.         }
  3905.       if (symsec != (asection *) NULL)
  3906.         val = (symsec->output_section->vma
  3907.            + symsec->output_offset
  3908.            + (GET_WORD (input_bfd, sym->e_value)
  3909.               - symsec->vma));
  3910.  
  3911.       /* If this is a global symbol set the written flag, and if
  3912.          it is a local symbol see if we should discard it.  */
  3913.       if (h != (struct aout_link_hash_entry *) NULL)
  3914.         {
  3915.           h->written = true;
  3916.           h->indx = obj_aout_external_sym_count (output_bfd);
  3917.         }
  3918.       else if ((type & N_TYPE) != N_SETT
  3919.            && (type & N_TYPE) != N_SETD
  3920.            && (type & N_TYPE) != N_SETB
  3921.            && (type & N_TYPE) != N_SETA)
  3922.         {
  3923.           switch (discard)
  3924.         {
  3925.         case discard_none:
  3926.           break;
  3927.         case discard_l:
  3928.           if (*name == *finfo->info->lprefix
  3929.               && (finfo->info->lprefix_len == 1
  3930.               || strncmp (name, finfo->info->lprefix,
  3931.                       finfo->info->lprefix_len) == 0))
  3932.             skip = true;
  3933.           break;
  3934.         case discard_all:
  3935.           skip = true;
  3936.           break;
  3937.         }
  3938.           if (skip)
  3939.         {
  3940.           pass = false;
  3941.           continue;
  3942.         }
  3943.         }
  3944.     }
  3945.  
  3946.       /* Copy this symbol into the list of symbols we are going to
  3947.      write out.  */
  3948.       bfd_h_put_8 (output_bfd, type, outsym->e_type);
  3949.       bfd_h_put_8 (output_bfd, bfd_h_get_8 (input_bfd, sym->e_other),
  3950.            outsym->e_other);
  3951.       bfd_h_put_16 (output_bfd, bfd_h_get_16 (input_bfd, sym->e_desc),
  3952.             outsym->e_desc);
  3953.       copy = false;
  3954.       if (! finfo->info->keep_memory)
  3955.     {
  3956.       /* name points into a string table which we are going to
  3957.          free.  If there is a hash table entry, use that string.
  3958.          Otherwise, copy name into memory.  */
  3959.       if (h != (struct aout_link_hash_entry *) NULL)
  3960.         name = h->root.root.string;
  3961.       else
  3962.         copy = true;
  3963.     }
  3964.       strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
  3965.                        name, copy);
  3966.       if (strtab_index == (bfd_size_type) -1)
  3967.     return false;
  3968.       PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
  3969.       PUT_WORD (output_bfd, val, outsym->e_value);
  3970.       *symbol_map = obj_aout_external_sym_count (output_bfd);
  3971.       ++obj_aout_external_sym_count (output_bfd);
  3972.       ++outsym;
  3973.     }
  3974.  
  3975.   /* Write out the output symbols we have just constructed.  */
  3976.   if (outsym > finfo->output_syms)
  3977.     {
  3978.       bfd_size_type outsym_count;
  3979.  
  3980.       if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
  3981.     return false;
  3982.       outsym_count = outsym - finfo->output_syms;
  3983.       if (bfd_write ((PTR) finfo->output_syms,
  3984.              (bfd_size_type) EXTERNAL_NLIST_SIZE,
  3985.              (bfd_size_type) outsym_count, output_bfd)
  3986.       != outsym_count * EXTERNAL_NLIST_SIZE)
  3987.     return false;
  3988.       finfo->symoff += outsym_count * EXTERNAL_NLIST_SIZE;
  3989.     }
  3990.  
  3991.   return true;
  3992. }
  3993.  
  3994. /* Write out a symbol that was not associated with an a.out input
  3995.    object.  */
  3996.  
  3997. static boolean
  3998. aout_link_write_other_symbol (h, data)
  3999.      struct aout_link_hash_entry *h;
  4000.      PTR data;
  4001. {
  4002.   struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
  4003.   bfd *output_bfd;
  4004.   int type;
  4005.   bfd_vma val;
  4006.   struct external_nlist outsym;
  4007.   bfd_size_type indx;
  4008.  
  4009.   output_bfd = finfo->output_bfd;
  4010.  
  4011.   if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
  4012.     {
  4013.       if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
  4014.          (output_bfd, finfo->info, h)))
  4015.     {
  4016.       /* FIXME: No way to handle errors.  */
  4017.       abort ();
  4018.     }
  4019.     }
  4020.  
  4021.   if (h->written)
  4022.     return true;
  4023.  
  4024.   h->written = true;
  4025.  
  4026.   /* An indx of -2 means the symbol must be written.  */
  4027.   if (h->indx != -2
  4028.       && (finfo->info->strip == strip_all
  4029.       || (finfo->info->strip == strip_some
  4030.           && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
  4031.                   false, false) == NULL)))
  4032.     return true;
  4033.  
  4034.   switch (h->root.type)
  4035.     {
  4036.     default:
  4037.       abort ();
  4038.       /* Avoid variable not initialized warnings.  */
  4039.       return true;
  4040.     case bfd_link_hash_new:
  4041.       /* This can happen for set symbols when sets are not being
  4042.          built.  */
  4043.       return true;
  4044.     case bfd_link_hash_undefined:
  4045.       type = N_UNDF | N_EXT;
  4046.       val = 0;
  4047.       break;
  4048.     case bfd_link_hash_defined:
  4049.       {
  4050.     asection *sec;
  4051.  
  4052.     sec = h->root.u.def.section->output_section;
  4053.     BFD_ASSERT (bfd_is_abs_section (sec)
  4054.             || sec->owner == output_bfd);
  4055.     if (sec == obj_textsec (output_bfd))
  4056.       type = N_TEXT | N_EXT;
  4057.     else if (sec == obj_datasec (output_bfd))
  4058.       type = N_DATA | N_EXT;
  4059.     else if (sec == obj_bsssec (output_bfd))
  4060.       type = N_BSS | N_EXT;
  4061.     else
  4062.       type = N_ABS | N_EXT;
  4063.     val = (h->root.u.def.value
  4064.            + sec->vma
  4065.            + h->root.u.def.section->output_offset);
  4066.       }
  4067.       break;
  4068.     case bfd_link_hash_common:
  4069.       type = N_UNDF | N_EXT;
  4070.       val = h->root.u.c.size;
  4071.       break;
  4072.     case bfd_link_hash_weak:
  4073.       type = N_WEAKU;
  4074.       val = 0;
  4075.     case bfd_link_hash_indirect:
  4076.     case bfd_link_hash_warning:
  4077.       /* FIXME: Ignore these for now.  The circumstances under which
  4078.      they should be written out are not clear to me.  */
  4079.       return true;
  4080.     }
  4081.  
  4082.   bfd_h_put_8 (output_bfd, type, outsym.e_type);
  4083.   bfd_h_put_8 (output_bfd, 0, outsym.e_other);
  4084.   bfd_h_put_16 (output_bfd, 0, outsym.e_desc);
  4085.   indx = add_to_stringtab (output_bfd, finfo->strtab, h->root.root.string,
  4086.                false);
  4087.   if (indx == (bfd_size_type) -1)
  4088.     {
  4089.       /* FIXME: No way to handle errors.  */
  4090.       abort ();
  4091.     }
  4092.   PUT_WORD (output_bfd, indx, outsym.e_strx);
  4093.   PUT_WORD (output_bfd, val, outsym.e_value);
  4094.  
  4095.   if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
  4096.       || bfd_write ((PTR) &outsym, (bfd_size_type) EXTERNAL_NLIST_SIZE,
  4097.             (bfd_size_type) 1, output_bfd) != EXTERNAL_NLIST_SIZE)
  4098.     {
  4099.       /* FIXME: No way to handle errors.  */
  4100.       abort ();
  4101.     }
  4102.  
  4103.   finfo->symoff += EXTERNAL_NLIST_SIZE;
  4104.   h->indx = obj_aout_external_sym_count (output_bfd);
  4105.   ++obj_aout_external_sym_count (output_bfd);
  4106.  
  4107.   return true;
  4108. }
  4109.  
  4110. /* Link an a.out section into the output file.  */
  4111.  
  4112. static boolean
  4113. aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
  4114.              rel_size)
  4115.      struct aout_final_link_info *finfo;
  4116.      bfd *input_bfd;
  4117.      asection *input_section;
  4118.      file_ptr *reloff_ptr;
  4119.      bfd_size_type rel_size;
  4120. {
  4121.   bfd_size_type input_size;
  4122.   PTR relocs;
  4123.  
  4124.   /* Get the section contents.  */
  4125.   input_size = bfd_section_size (input_bfd, input_section);
  4126.   if (! bfd_get_section_contents (input_bfd, input_section,
  4127.                   (PTR) finfo->contents,
  4128.                   (file_ptr) 0, input_size))
  4129.     return false;
  4130.  
  4131.   /* Read in the relocs if we haven't already done it.  */
  4132.   if (aout_section_data (input_section) != NULL
  4133.       && aout_section_data (input_section)->relocs != NULL)
  4134.     relocs = aout_section_data (input_section)->relocs;
  4135.   else
  4136.     {
  4137.       relocs = finfo->relocs;
  4138.       if (rel_size > 0)
  4139.     {
  4140.       if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
  4141.           || bfd_read (relocs, 1, rel_size, input_bfd) != rel_size)
  4142.         return false;
  4143.     }
  4144.     }
  4145.  
  4146.   /* Relocate the section contents.  */
  4147.   if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
  4148.     {
  4149.       if (! aout_link_input_section_std (finfo, input_bfd, input_section,
  4150.                      (struct reloc_std_external *) relocs,
  4151.                      rel_size, finfo->contents))
  4152.     return false;
  4153.     }
  4154.   else
  4155.     {
  4156.       if (! aout_link_input_section_ext (finfo, input_bfd, input_section,
  4157.                      (struct reloc_ext_external *) relocs,
  4158.                      rel_size, finfo->contents))
  4159.     return false;
  4160.     }
  4161.  
  4162.   /* Write out the section contents.  */
  4163.   if (! bfd_set_section_contents (finfo->output_bfd,
  4164.                   input_section->output_section,
  4165.                   (PTR) finfo->contents,
  4166.                   input_section->output_offset,
  4167.                   input_size))
  4168.     return false;
  4169.  
  4170.   /* If we are producing relocateable output, the relocs were
  4171.      modified, and we now write them out.  */
  4172.   if (finfo->info->relocateable && rel_size > 0)
  4173.     {
  4174.       if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
  4175.     return false;
  4176.       if (bfd_write (relocs, (bfd_size_type) 1, rel_size, finfo->output_bfd)
  4177.       != rel_size)
  4178.     return false;
  4179.       *reloff_ptr += rel_size;
  4180.  
  4181.       /* Assert that the relocs have not run into the symbols, and
  4182.      that if these are the text relocs they have not run into the
  4183.      data relocs.  */
  4184.       BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
  4185.           && (reloff_ptr != &finfo->treloff
  4186.               || (*reloff_ptr
  4187.               <= obj_datasec (finfo->output_bfd)->rel_filepos)));
  4188.     }
  4189.  
  4190.   return true;
  4191. }
  4192.  
  4193. /* Get the section corresponding to a reloc index.  */
  4194.  
  4195. static INLINE asection *
  4196. aout_reloc_index_to_section (abfd, indx)
  4197.      bfd *abfd;
  4198.      int indx;
  4199. {
  4200.   switch (indx & N_TYPE)
  4201.     {
  4202.     case N_TEXT:
  4203.       return obj_textsec (abfd);
  4204.     case N_DATA:
  4205.       return obj_datasec (abfd);
  4206.     case N_BSS:
  4207.       return obj_bsssec (abfd);
  4208.     case N_ABS:
  4209.     case N_UNDF:
  4210.       return bfd_abs_section_ptr;
  4211.     default:
  4212.       abort ();
  4213.     }
  4214. }
  4215.  
  4216. /* Relocate an a.out section using standard a.out relocs.  */
  4217.  
  4218. static boolean
  4219. aout_link_input_section_std (finfo, input_bfd, input_section, relocs,
  4220.                  rel_size, contents)
  4221.      struct aout_final_link_info *finfo;
  4222.      bfd *input_bfd;
  4223.      asection *input_section;
  4224.      struct reloc_std_external *relocs;
  4225.      bfd_size_type rel_size;
  4226.      bfd_byte *contents;
  4227. {
  4228.   boolean (*check_dynamic_reloc) PARAMS ((struct bfd_link_info *,
  4229.                       bfd *, asection *,
  4230.                       struct aout_link_hash_entry *,
  4231.                       PTR, boolean *));
  4232.   bfd *output_bfd;
  4233.   boolean relocateable;
  4234.   struct external_nlist *syms;
  4235.   char *strings;
  4236.   struct aout_link_hash_entry **sym_hashes;
  4237.   int *symbol_map;
  4238.   bfd_size_type reloc_count;
  4239.   register struct reloc_std_external *rel;
  4240.   struct reloc_std_external *rel_end;
  4241.  
  4242.   output_bfd = finfo->output_bfd;
  4243.   check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
  4244.  
  4245.   BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
  4246.   BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
  4247.           == output_bfd->xvec->header_byteorder_big_p);
  4248.  
  4249.   relocateable = finfo->info->relocateable;
  4250.   syms = obj_aout_external_syms (input_bfd);
  4251.   strings = obj_aout_external_strings (input_bfd);
  4252.   sym_hashes = obj_aout_sym_hashes (input_bfd);
  4253.   symbol_map = finfo->symbol_map;
  4254.  
  4255.   reloc_count = rel_size / RELOC_STD_SIZE;
  4256.   rel = relocs;
  4257.   rel_end = rel + reloc_count;
  4258.   for (; rel < rel_end; rel++)
  4259.     {
  4260.       bfd_vma r_addr;
  4261.       int r_index;
  4262.       int r_extern;
  4263.       int r_pcrel;
  4264.       int r_baserel;
  4265.       int r_jmptable;
  4266.       int r_relative;
  4267.       int r_length;
  4268.       int howto_idx;
  4269.       reloc_howto_type *howto;
  4270.       bfd_vma relocation;
  4271.       bfd_reloc_status_type r;
  4272.  
  4273.       r_addr = GET_SWORD (input_bfd, rel->r_address);
  4274.  
  4275. #ifdef MY_reloc_howto
  4276.       howto = MY_reloc_howto(input_bfd, rel, r_index, r_extern, r_pcrel);
  4277. #else      
  4278.       if (input_bfd->xvec->header_byteorder_big_p)
  4279.     {
  4280.       r_index   =  ((rel->r_index[0] << 16)
  4281.             | (rel->r_index[1] << 8)
  4282.             | rel->r_index[2]);
  4283.       r_extern  = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
  4284.       r_pcrel   = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
  4285.       r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
  4286.       r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
  4287.       r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
  4288.       r_length  = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
  4289.                >> RELOC_STD_BITS_LENGTH_SH_BIG);
  4290.     }
  4291.       else
  4292.     {
  4293.       r_index   = ((rel->r_index[2] << 16)
  4294.                | (rel->r_index[1] << 8)
  4295.                | rel->r_index[0]);
  4296.       r_extern  = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
  4297.       r_pcrel   = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
  4298.       r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
  4299.       r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
  4300.       r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
  4301.       r_length  = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
  4302.                >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
  4303.     }
  4304.  
  4305.       howto_idx = r_length + 4 * r_pcrel + 8 * r_baserel
  4306.           + 16 * r_jmptable + 32 * r_relative;
  4307.       BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_std));
  4308.       howto = howto_table_std + howto_idx;
  4309. #endif
  4310.  
  4311.       if (relocateable)
  4312.     {
  4313.       /* We are generating a relocateable output file, and must
  4314.          modify the reloc accordingly.  */
  4315.       if (r_extern)
  4316.         {
  4317.           struct aout_link_hash_entry *h;
  4318.  
  4319.           /* If we know the symbol this relocation is against,
  4320.          convert it into a relocation against a section.  This
  4321.          is what the native linker does.  */
  4322.           h = sym_hashes[r_index];
  4323.           if (h != (struct aout_link_hash_entry *) NULL
  4324.           && h->root.type == bfd_link_hash_defined)
  4325.         {
  4326.           asection *output_section;
  4327.  
  4328.           /* Change the r_extern value.  */
  4329.           if (output_bfd->xvec->header_byteorder_big_p)
  4330.             rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
  4331.           else
  4332.             rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
  4333.  
  4334.           /* Compute a new r_index.  */
  4335.           output_section = h->root.u.def.section->output_section;
  4336.           if (output_section == obj_textsec (output_bfd))
  4337.             r_index = N_TEXT;
  4338.           else if (output_section == obj_datasec (output_bfd))
  4339.             r_index = N_DATA;
  4340.           else if (output_section == obj_bsssec (output_bfd))
  4341.             r_index = N_BSS;
  4342.           else
  4343.             r_index = N_ABS;
  4344.  
  4345.           /* Add the symbol value and the section VMA to the
  4346.              addend stored in the contents.  */
  4347.           relocation = (h->root.u.def.value
  4348.                 + output_section->vma
  4349.                 + h->root.u.def.section->output_offset);
  4350.         }
  4351.           else
  4352.         {
  4353.           /* We must change r_index according to the symbol
  4354.              map.  */
  4355.           r_index = symbol_map[r_index];
  4356.  
  4357.           if (r_index == -1)
  4358.             {
  4359.               if (h != NULL)
  4360.             {
  4361.               /* We decided to strip this symbol, but it
  4362.                              turns out that we can't.  Note that we
  4363.                              lose the other and desc information here.
  4364.                              I don't think that will ever matter for a
  4365.                              global symbol.  */
  4366.               if (h->indx < 0)
  4367.                 {
  4368.                   h->indx = -2;
  4369.                   h->written = false;
  4370.                   if (! aout_link_write_other_symbol (h,
  4371.                                   (PTR) finfo))
  4372.                 return false;
  4373.                 }
  4374.               r_index = h->indx;
  4375.             }
  4376.               else
  4377.             {
  4378.               const char *name;
  4379.  
  4380.               name = strings + GET_WORD (input_bfd,
  4381.                              syms[r_index].e_strx);
  4382.               if (! ((*finfo->info->callbacks->unattached_reloc)
  4383.                  (finfo->info, name, input_bfd, input_section,
  4384.                   r_addr)))
  4385.                 return false;
  4386.               r_index = 0;
  4387.             }
  4388.             }
  4389.  
  4390.           relocation = 0;
  4391.         }
  4392.  
  4393.           /* Write out the new r_index value.  */
  4394.           if (output_bfd->xvec->header_byteorder_big_p)
  4395.         {
  4396.           rel->r_index[0] = r_index >> 16;
  4397.           rel->r_index[1] = r_index >> 8;
  4398.           rel->r_index[2] = r_index;
  4399.         }
  4400.           else
  4401.         {
  4402.           rel->r_index[2] = r_index >> 16;
  4403.           rel->r_index[1] = r_index >> 8;
  4404.           rel->r_index[0] = r_index;
  4405.         }
  4406.         }
  4407.       else
  4408.         {
  4409.           asection *section;
  4410.  
  4411.           /* This is a relocation against a section.  We must
  4412.          adjust by the amount that the section moved.  */
  4413.           section = aout_reloc_index_to_section (input_bfd, r_index);
  4414.           relocation = (section->output_section->vma
  4415.                 + section->output_offset
  4416.                 - section->vma);
  4417.         }
  4418.  
  4419.       /* Change the address of the relocation.  */
  4420.       PUT_WORD (output_bfd,
  4421.             r_addr + input_section->output_offset,
  4422.             rel->r_address);
  4423.  
  4424.       /* Adjust a PC relative relocation by removing the reference
  4425.          to the original address in the section and including the
  4426.          reference to the new address.  */
  4427.       if (r_pcrel)
  4428.         relocation -= (input_section->output_section->vma
  4429.                + input_section->output_offset
  4430.                - input_section->vma);
  4431.  
  4432.       if (relocation == 0)
  4433.         r = bfd_reloc_ok;
  4434.       else
  4435.         r = _bfd_relocate_contents (howto,
  4436.                     input_bfd, relocation,
  4437.                     contents + r_addr);
  4438.     }
  4439.       else
  4440.     {
  4441.       /* We are generating an executable, and must do a full
  4442.          relocation.  */
  4443.       if (r_extern)
  4444.         {
  4445.           struct aout_link_hash_entry *h;
  4446.  
  4447.           h = sym_hashes[r_index];
  4448.  
  4449.           if (check_dynamic_reloc != NULL)
  4450.         {
  4451.           boolean skip;
  4452.  
  4453.           if (! ((*check_dynamic_reloc)
  4454.              (finfo->info, input_bfd, input_section, h,
  4455.               (PTR) rel, &skip)))
  4456.             return false;
  4457.           if (skip)
  4458.             continue;
  4459.         }
  4460.  
  4461.           if (h != (struct aout_link_hash_entry *) NULL
  4462.           && h->root.type == bfd_link_hash_defined)
  4463.         {
  4464.           relocation = (h->root.u.def.value
  4465.                 + h->root.u.def.section->output_section->vma
  4466.                 + h->root.u.def.section->output_offset);
  4467.         }
  4468.           else if (h != (struct aout_link_hash_entry *) NULL
  4469.                && h->root.type == bfd_link_hash_weak)
  4470.         relocation = 0;
  4471.           else
  4472.         {
  4473.           const char *name;
  4474.  
  4475.           name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
  4476.           if (! ((*finfo->info->callbacks->undefined_symbol)
  4477.              (finfo->info, name, input_bfd, input_section,
  4478.               r_addr)))
  4479.             return false;
  4480.           relocation = 0;
  4481.         }
  4482.         }
  4483.       else
  4484.         {
  4485.           asection *section;
  4486.  
  4487.           section = aout_reloc_index_to_section (input_bfd, r_index);
  4488.           relocation = (section->output_section->vma
  4489.                 + section->output_offset
  4490.                 - section->vma);
  4491.           if (r_pcrel)
  4492.         relocation += input_section->vma;
  4493.         }
  4494.  
  4495.       r = _bfd_final_link_relocate (howto,
  4496.                     input_bfd, input_section,
  4497.                     contents, r_addr, relocation,
  4498.                     (bfd_vma) 0);
  4499.     }
  4500.  
  4501.       if (r != bfd_reloc_ok)
  4502.     {
  4503.       switch (r)
  4504.         {
  4505.         default:
  4506.         case bfd_reloc_outofrange:
  4507.           abort ();
  4508.         case bfd_reloc_overflow:
  4509.           {
  4510.         const char *name;
  4511.  
  4512.         if (r_extern)
  4513.           name = strings + GET_WORD (input_bfd,
  4514.                          syms[r_index].e_strx);
  4515.         else
  4516.           {
  4517.             asection *s;
  4518.  
  4519.             s = aout_reloc_index_to_section (input_bfd, r_index);
  4520.             name = bfd_section_name (input_bfd, s);
  4521.           }
  4522.         if (! ((*finfo->info->callbacks->reloc_overflow)
  4523.                (finfo->info, name, howto->name,
  4524.             (bfd_vma) 0, input_bfd, input_section, r_addr)))
  4525.           return false;
  4526.           }
  4527.           break;
  4528.         }
  4529.     }
  4530.     }
  4531.  
  4532.   return true;
  4533. }
  4534.  
  4535. /* Relocate an a.out section using extended a.out relocs.  */
  4536.  
  4537. static boolean
  4538. aout_link_input_section_ext (finfo, input_bfd, input_section, relocs,
  4539.                  rel_size, contents)
  4540.      struct aout_final_link_info *finfo;
  4541.      bfd *input_bfd;
  4542.      asection *input_section;
  4543.      struct reloc_ext_external *relocs;
  4544.      bfd_size_type rel_size;
  4545.      bfd_byte *contents;
  4546. {
  4547.   boolean (*check_dynamic_reloc) PARAMS ((struct bfd_link_info *,
  4548.                       bfd *, asection *,
  4549.                       struct aout_link_hash_entry *,
  4550.                       PTR, boolean *));
  4551.   bfd *output_bfd;
  4552.   boolean relocateable;
  4553.   struct external_nlist *syms;
  4554.   char *strings;
  4555.   struct aout_link_hash_entry **sym_hashes;
  4556.   int *symbol_map;
  4557.   bfd_size_type reloc_count;
  4558.   register struct reloc_ext_external *rel;
  4559.   struct reloc_ext_external *rel_end;
  4560.  
  4561.   output_bfd = finfo->output_bfd;
  4562.   check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
  4563.  
  4564.   BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
  4565.   BFD_ASSERT (input_bfd->xvec->header_byteorder_big_p
  4566.           == output_bfd->xvec->header_byteorder_big_p);
  4567.  
  4568.   relocateable = finfo->info->relocateable;
  4569.   syms = obj_aout_external_syms (input_bfd);
  4570.   strings = obj_aout_external_strings (input_bfd);
  4571.   sym_hashes = obj_aout_sym_hashes (input_bfd);
  4572.   symbol_map = finfo->symbol_map;
  4573.  
  4574.   reloc_count = rel_size / RELOC_EXT_SIZE;
  4575.   rel = relocs;
  4576.   rel_end = rel + reloc_count;
  4577.   for (; rel < rel_end; rel++)
  4578.     {
  4579.       bfd_vma r_addr;
  4580.       int r_index;
  4581.       int r_extern;
  4582.       int r_type;
  4583.       bfd_vma r_addend;
  4584.       bfd_vma relocation;
  4585.  
  4586.       r_addr = GET_SWORD (input_bfd, rel->r_address);
  4587.  
  4588.       if (input_bfd->xvec->header_byteorder_big_p)
  4589.     {
  4590.       r_index  = ((rel->r_index[0] << 16)
  4591.               | (rel->r_index[1] << 8)
  4592.               | rel->r_index[2]);
  4593.       r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
  4594.       r_type   = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
  4595.               >> RELOC_EXT_BITS_TYPE_SH_BIG);
  4596.     }
  4597.       else
  4598.     {
  4599.       r_index  = ((rel->r_index[2] << 16)
  4600.               | (rel->r_index[1] << 8)
  4601.               | rel->r_index[0]);
  4602.       r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
  4603.       r_type   = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
  4604.               >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
  4605.     }
  4606.  
  4607.       r_addend = GET_SWORD (input_bfd, rel->r_addend);
  4608.  
  4609.       BFD_ASSERT (r_type >= 0
  4610.           && r_type < TABLE_SIZE (howto_table_ext));
  4611.  
  4612.       if (relocateable)
  4613.     {
  4614.       /* We are generating a relocateable output file, and must
  4615.          modify the reloc accordingly.  */
  4616.       if (r_extern)
  4617.         {
  4618.           struct aout_link_hash_entry *h;
  4619.  
  4620.           /* If we know the symbol this relocation is against,
  4621.          convert it into a relocation against a section.  This
  4622.          is what the native linker does.  */
  4623.           h = sym_hashes[r_index];
  4624.           if (h != (struct aout_link_hash_entry *) NULL
  4625.           && h->root.type == bfd_link_hash_defined)
  4626.         {
  4627.           asection *output_section;
  4628.  
  4629.           /* Change the r_extern value.  */
  4630.           if (output_bfd->xvec->header_byteorder_big_p)
  4631.             rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
  4632.           else
  4633.             rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
  4634.  
  4635.           /* Compute a new r_index.  */
  4636.           output_section = h->root.u.def.section->output_section;
  4637.           if (output_section == obj_textsec (output_bfd))
  4638.             r_index = N_TEXT;
  4639.           else if (output_section == obj_datasec (output_bfd))
  4640.             r_index = N_DATA;
  4641.           else if (output_section == obj_bsssec (output_bfd))
  4642.             r_index = N_BSS;
  4643.           else
  4644.             r_index = N_ABS;
  4645.  
  4646.           /* Add the symbol value and the section VMA to the
  4647.              addend.  */
  4648.           relocation = (h->root.u.def.value
  4649.                 + output_section->vma
  4650.                 + h->root.u.def.section->output_offset);
  4651.  
  4652.           /* Now RELOCATION is the VMA of the final
  4653.              destination.  If this is a PC relative reloc,
  4654.              then ADDEND is the negative of the source VMA.
  4655.              We want to set ADDEND to the difference between
  4656.              the destination VMA and the source VMA, which
  4657.              means we must adjust RELOCATION by the change in
  4658.              the source VMA.  This is done below.  */
  4659.         }
  4660.           else
  4661.         {
  4662.           /* We must change r_index according to the symbol
  4663.              map.  */
  4664.           r_index = symbol_map[r_index];
  4665.  
  4666.           if (r_index == -1)
  4667.             {
  4668.               if (h != NULL)
  4669.             {
  4670.               /* We decided to strip this symbol, but it
  4671.                              turns out that we can't.  Note that we
  4672.                              lose the other and desc information here.
  4673.                              I don't think that will ever matter for a
  4674.                              global symbol.  */
  4675.               if (h->indx < 0)
  4676.                 {
  4677.                   h->indx = -2;
  4678.                   h->written = false;
  4679.                   if (! aout_link_write_other_symbol (h,
  4680.                                   (PTR) finfo))
  4681.                 return false;
  4682.                 }
  4683.               r_index = h->indx;
  4684.             }
  4685.               else
  4686.             {
  4687.               const char *name;
  4688.  
  4689.               name = strings + GET_WORD (input_bfd,
  4690.                              syms[r_index].e_strx);
  4691.               if (! ((*finfo->info->callbacks->unattached_reloc)
  4692.                  (finfo->info, name, input_bfd, input_section,
  4693.                   r_addr)))
  4694.                 return false;
  4695.               r_index = 0;
  4696.             }
  4697.             }
  4698.  
  4699.           relocation = 0;
  4700.  
  4701.           /* If this is a PC relative reloc, then the addend
  4702.              is the negative of the source VMA.  We must
  4703.              adjust it by the change in the source VMA.  This
  4704.              is done below.  */
  4705.         }
  4706.  
  4707.           /* Write out the new r_index value.  */
  4708.           if (output_bfd->xvec->header_byteorder_big_p)
  4709.         {
  4710.           rel->r_index[0] = r_index >> 16;
  4711.           rel->r_index[1] = r_index >> 8;
  4712.           rel->r_index[2] = r_index;
  4713.         }
  4714.           else
  4715.         {
  4716.           rel->r_index[2] = r_index >> 16;
  4717.           rel->r_index[1] = r_index >> 8;
  4718.           rel->r_index[0] = r_index;
  4719.         }
  4720.         }
  4721.       else
  4722.         {
  4723.           asection *section;
  4724.  
  4725.           /* This is a relocation against a section.  We must
  4726.          adjust by the amount that the section moved.  */
  4727.           section = aout_reloc_index_to_section (input_bfd, r_index);
  4728.           relocation = (section->output_section->vma
  4729.                 + section->output_offset
  4730.                 - section->vma);
  4731.  
  4732.           /* If this is a PC relative reloc, then the addend is
  4733.          the difference in VMA between the destination and the
  4734.          source.  We have just adjusted for the change in VMA
  4735.          of the destination, so we must also adjust by the
  4736.          change in VMA of the source.  This is done below.  */
  4737.         }
  4738.  
  4739.       /* As described above, we must always adjust a PC relative
  4740.          reloc by the change in VMA of the source.  */
  4741.       if (howto_table_ext[r_type].pc_relative)
  4742.         relocation -= (input_section->output_section->vma
  4743.                + input_section->output_offset
  4744.                - input_section->vma);
  4745.  
  4746.       /* Change the addend if necessary.  */
  4747.       if (relocation != 0)
  4748.         PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
  4749.  
  4750.       /* Change the address of the relocation.  */
  4751.       PUT_WORD (output_bfd,
  4752.             r_addr + input_section->output_offset,
  4753.             rel->r_address);
  4754.     }
  4755.       else
  4756.     {
  4757.       bfd_reloc_status_type r;
  4758.  
  4759.       /* We are generating an executable, and must do a full
  4760.          relocation.  */
  4761.       if (r_extern)
  4762.         {
  4763.           struct aout_link_hash_entry *h;
  4764.  
  4765.           h = sym_hashes[r_index];
  4766.  
  4767.           if (check_dynamic_reloc != NULL)
  4768.         {
  4769.           boolean skip;
  4770.  
  4771.           if (! ((*check_dynamic_reloc)
  4772.              (finfo->info, input_bfd, input_section, h,
  4773.               (PTR) rel, &skip)))
  4774.             return false;
  4775.           if (skip)
  4776.             continue;
  4777.         }
  4778.  
  4779.           if (h != (struct aout_link_hash_entry *) NULL
  4780.           && h->root.type == bfd_link_hash_defined)
  4781.         {
  4782.           relocation = (h->root.u.def.value
  4783.                 + h->root.u.def.section->output_section->vma
  4784.                 + h->root.u.def.section->output_offset);
  4785.         }
  4786.           else if (h != (struct aout_link_hash_entry *) NULL
  4787.                && h->root.type == bfd_link_hash_weak)
  4788.         relocation = 0;
  4789.           else
  4790.         {
  4791.           const char *name;
  4792.  
  4793.           name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
  4794.           if (! ((*finfo->info->callbacks->undefined_symbol)
  4795.              (finfo->info, name, input_bfd, input_section,
  4796.               r_addr)))
  4797.             return false;
  4798.           relocation = 0;
  4799.         }
  4800.         }
  4801.       else
  4802.         {
  4803.           asection *section;
  4804.  
  4805.           section = aout_reloc_index_to_section (input_bfd, r_index);
  4806.  
  4807.           /* If this is a PC relative reloc, then R_ADDEND is the
  4808.          difference between the two vmas, or
  4809.            old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
  4810.          where
  4811.            old_dest_sec == section->vma
  4812.          and
  4813.            old_src_sec == input_section->vma
  4814.          and
  4815.            old_src_off == r_addr
  4816.  
  4817.          _bfd_final_link_relocate expects RELOCATION +
  4818.          R_ADDEND to be the VMA of the destination minus
  4819.          r_addr (the minus r_addr is because this relocation
  4820.          is not pcrel_offset, which is a bit confusing and
  4821.          should, perhaps, be changed), or
  4822.            new_dest_sec
  4823.          where
  4824.            new_dest_sec == output_section->vma + output_offset
  4825.          We arrange for this to happen by setting RELOCATION to
  4826.            new_dest_sec + old_src_sec - old_dest_sec
  4827.  
  4828.          If this is not a PC relative reloc, then R_ADDEND is
  4829.          simply the VMA of the destination, so we set
  4830.          RELOCATION to the change in the destination VMA, or
  4831.            new_dest_sec - old_dest_sec
  4832.          */
  4833.           relocation = (section->output_section->vma
  4834.                 + section->output_offset
  4835.                 - section->vma);
  4836.           if (howto_table_ext[r_type].pc_relative)
  4837.         relocation += input_section->vma;
  4838.         }
  4839.  
  4840.       r = _bfd_final_link_relocate (howto_table_ext + r_type,
  4841.                     input_bfd, input_section,
  4842.                     contents, r_addr, relocation,
  4843.                     r_addend);
  4844.       if (r != bfd_reloc_ok)
  4845.         {
  4846.           switch (r)
  4847.         {
  4848.         default:
  4849.         case bfd_reloc_outofrange:
  4850.           abort ();
  4851.         case bfd_reloc_overflow:
  4852.           {
  4853.             const char *name;
  4854.  
  4855.             if (r_extern)
  4856.               name = strings + GET_WORD (input_bfd,
  4857.                          syms[r_index].e_strx);
  4858.             else
  4859.               {
  4860.             asection *s;
  4861.  
  4862.             s = aout_reloc_index_to_section (input_bfd, r_index);
  4863.             name = bfd_section_name (input_bfd, s);
  4864.               }
  4865.             if (! ((*finfo->info->callbacks->reloc_overflow)
  4866.                (finfo->info, name, howto_table_ext[r_type].name,
  4867.                 r_addend, input_bfd, input_section, r_addr)))
  4868.               return false;
  4869.           }
  4870.           break;
  4871.         }
  4872.         }
  4873.     }
  4874.     }
  4875.  
  4876.   return true;
  4877. }
  4878.  
  4879. /* Handle a link order which is supposed to generate a reloc.  */
  4880.  
  4881. static boolean
  4882. aout_link_reloc_link_order (finfo, o, p)
  4883.      struct aout_final_link_info *finfo;
  4884.      asection *o;
  4885.      struct bfd_link_order *p;
  4886. {
  4887.   struct bfd_link_order_reloc *pr;
  4888.   int r_index;
  4889.   int r_extern;
  4890.   reloc_howto_type *howto;
  4891.   file_ptr *reloff_ptr;
  4892.   struct reloc_std_external srel;
  4893.   struct reloc_ext_external erel;
  4894.   PTR rel_ptr;
  4895.  
  4896.   pr = p->u.reloc.p;
  4897.  
  4898.   if (p->type == bfd_section_reloc_link_order)
  4899.     {
  4900.       r_extern = 0;
  4901.       if (bfd_is_abs_section (pr->u.section))
  4902.     r_index = N_ABS | N_EXT;
  4903.       else
  4904.     {
  4905.       BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
  4906.       r_index = pr->u.section->target_index;
  4907.     }
  4908.     }
  4909.   else
  4910.     {
  4911.       struct aout_link_hash_entry *h;
  4912.  
  4913.       BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
  4914.       r_extern = 1;
  4915.       h = aout_link_hash_lookup (aout_hash_table (finfo->info),
  4916.                  pr->u.name, false, false, true);
  4917.       if (h != (struct aout_link_hash_entry *) NULL
  4918.       && h->indx >= 0)
  4919.     r_index = h->indx;
  4920.       else if (h != NULL)
  4921.     {
  4922.       /* We decided to strip this symbol, but it turns out that we
  4923.          can't.  Note that we lose the other and desc information
  4924.          here.  I don't think that will ever matter for a global
  4925.          symbol.  */
  4926.       h->indx = -2;
  4927.       h->written = false;
  4928.       if (! aout_link_write_other_symbol (h, (PTR) finfo))
  4929.         return false;
  4930.       r_index = h->indx;
  4931.     }
  4932.       else
  4933.     {
  4934.       if (! ((*finfo->info->callbacks->unattached_reloc)
  4935.          (finfo->info, pr->u.name, (bfd *) NULL,
  4936.           (asection *) NULL, (bfd_vma) 0)))
  4937.         return false;
  4938.       r_index = 0;
  4939.     }
  4940.     }
  4941.  
  4942.   howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
  4943.   if (howto == 0)
  4944.     {
  4945.       bfd_set_error (bfd_error_bad_value);
  4946.       return false;
  4947.     }
  4948.  
  4949.   if (o == obj_textsec (finfo->output_bfd))
  4950.     reloff_ptr = &finfo->treloff;
  4951.   else if (o == obj_datasec (finfo->output_bfd))
  4952.     reloff_ptr = &finfo->dreloff;
  4953.   else
  4954.     abort ();
  4955.  
  4956.   if (obj_reloc_entry_size (finfo->output_bfd) == RELOC_STD_SIZE)
  4957.     {
  4958.       int r_pcrel;
  4959.       int r_baserel;
  4960.       int r_jmptable;
  4961.       int r_relative;
  4962.       int r_length;
  4963.  
  4964. #ifdef MY_put_reloc
  4965.       MY_put_reloc(finfo->output_bfd, r_extern, r_index, p->offset, howto, &srel);
  4966. #else
  4967.       r_pcrel = howto->pc_relative;
  4968.       r_baserel = (howto->type & 8) != 0;
  4969.       r_jmptable = (howto->type & 16) != 0;
  4970.       r_relative = (howto->type & 32) != 0;
  4971.       r_length = howto->size;
  4972.  
  4973.       PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
  4974.       if (finfo->output_bfd->xvec->header_byteorder_big_p)
  4975.     {
  4976.       srel.r_index[0] = r_index >> 16;
  4977.       srel.r_index[1] = r_index >> 8;
  4978.       srel.r_index[2] = r_index;
  4979.       srel.r_type[0] =
  4980.         ((r_extern ?     RELOC_STD_BITS_EXTERN_BIG : 0)
  4981.          | (r_pcrel ?    RELOC_STD_BITS_PCREL_BIG : 0)
  4982.          | (r_baserel ?  RELOC_STD_BITS_BASEREL_BIG : 0)
  4983.          | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
  4984.          | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
  4985.          | (r_length <<  RELOC_STD_BITS_LENGTH_SH_BIG));
  4986.     }
  4987.       else
  4988.     {
  4989.       srel.r_index[2] = r_index >> 16;
  4990.       srel.r_index[1] = r_index >> 8;
  4991.       srel.r_index[0] = r_index;
  4992.       srel.r_type[0] =
  4993.         ((r_extern ?     RELOC_STD_BITS_EXTERN_LITTLE : 0)
  4994.          | (r_pcrel ?    RELOC_STD_BITS_PCREL_LITTLE : 0)
  4995.          | (r_baserel ?  RELOC_STD_BITS_BASEREL_LITTLE : 0)
  4996.          | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
  4997.          | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
  4998.          | (r_length <<  RELOC_STD_BITS_LENGTH_SH_LITTLE));
  4999.     }
  5000. #endif
  5001.       rel_ptr = (PTR) &srel;
  5002.  
  5003.       /* We have to write the addend into the object file, since
  5004.      standard a.out relocs are in place.  It would be more
  5005.      reliable if we had the current contents of the file here,
  5006.      rather than assuming zeroes, but we can't read the file since
  5007.      it was opened using bfd_openw.  */
  5008.       if (pr->addend != 0)
  5009.     {
  5010.       bfd_size_type size;
  5011.       bfd_reloc_status_type r;
  5012.       bfd_byte *buf;
  5013.       boolean ok;
  5014.  
  5015.       size = bfd_get_reloc_size (howto);
  5016.       buf = (bfd_byte *) bfd_zmalloc (size);
  5017.       if (buf == (bfd_byte *) NULL)
  5018.         {
  5019.           bfd_set_error (bfd_error_no_memory);
  5020.           return false;
  5021.         }
  5022.       r = _bfd_relocate_contents (howto, finfo->output_bfd,
  5023.                       pr->addend, buf);
  5024.       switch (r)
  5025.         {
  5026.         case bfd_reloc_ok:
  5027.           break;
  5028.         default:
  5029.         case bfd_reloc_outofrange:
  5030.           abort ();
  5031.         case bfd_reloc_overflow:
  5032.           if (! ((*finfo->info->callbacks->reloc_overflow)
  5033.              (finfo->info,
  5034.               (p->type == bfd_section_reloc_link_order
  5035.                ? bfd_section_name (finfo->output_bfd,
  5036.                        pr->u.section)
  5037.                : pr->u.name),
  5038.               howto->name, pr->addend, (bfd *) NULL,
  5039.               (asection *) NULL, (bfd_vma) 0)))
  5040.         {
  5041.           free (buf);
  5042.           return false;
  5043.         }
  5044.           break;
  5045.         }
  5046.       ok = bfd_set_section_contents (finfo->output_bfd, o,
  5047.                      (PTR) buf,
  5048.                      (file_ptr) p->offset,
  5049.                      size);
  5050.       free (buf);
  5051.       if (! ok)
  5052.         return false;
  5053.     }
  5054.     }
  5055.   else
  5056.     {
  5057.       PUT_WORD (finfo->output_bfd, p->offset, erel.r_address);
  5058.  
  5059.       if (finfo->output_bfd->xvec->header_byteorder_big_p)
  5060.     {
  5061.       erel.r_index[0] = r_index >> 16;
  5062.       erel.r_index[1] = r_index >> 8;
  5063.       erel.r_index[2] = r_index;
  5064.       erel.r_type[0] =
  5065.         ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
  5066.          | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
  5067.     }
  5068.       else
  5069.     {
  5070.       erel.r_index[2] = r_index >> 16;
  5071.       erel.r_index[1] = r_index >> 8;
  5072.       erel.r_index[0] = r_index;
  5073.       erel.r_type[0] =
  5074.         (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
  5075.           | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
  5076.     }
  5077.  
  5078.       PUT_WORD (finfo->output_bfd, pr->addend, erel.r_addend);
  5079.  
  5080.       rel_ptr = (PTR) &erel;
  5081.     }
  5082.  
  5083.   if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
  5084.       || (bfd_write (rel_ptr, (bfd_size_type) 1,
  5085.              obj_reloc_entry_size (finfo->output_bfd),
  5086.              finfo->output_bfd)
  5087.       != obj_reloc_entry_size (finfo->output_bfd)))
  5088.     return false;
  5089.  
  5090.   *reloff_ptr += obj_reloc_entry_size (finfo->output_bfd);
  5091.  
  5092.   /* Assert that the relocs have not run into the symbols, and that n
  5093.      the text relocs have not run into the data relocs.  */
  5094.   BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
  5095.           && (reloff_ptr != &finfo->treloff
  5096.           || (*reloff_ptr
  5097.               <= obj_datasec (finfo->output_bfd)->rel_filepos)));
  5098.  
  5099.   return true;
  5100. }
  5101.