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