OSDN Git Service

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