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