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