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