OSDN Git Service

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