home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / aoutx.h < prev    next >
C/C++ Source or Header  |  1996-09-28  |  166KB  |  5,643 lines

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