* emultempl/pe.em (debug_section_p): New bfd_map_over_sections hook.
[binutils.git] / bfd / aoutx.h
blobfccfdeb0d197bff3e450f0685878296560015c9d
1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
25 SECTION
26 a.out backends
28 DESCRIPTION
30 BFD supports a number of different flavours of a.out format,
31 though the major differences are only the sizes of the
32 structures on disk, and the shape of the relocation
33 information.
35 The support is split into a basic support file @file{aoutx.h}
36 and other files which derive functions from the base. One
37 derivation file is @file{aoutf1.h} (for a.out flavour 1), and
38 adds to the basic a.out functions support for sun3, sun4, 386
39 and 29k a.out files, to create a target jump vector for a
40 specific target.
42 This information is further split out into more specific files
43 for each machine, including @file{sunos.c} for sun3 and sun4,
44 @file{newsos3.c} for the Sony NEWS, and @file{demo64.c} for a
45 demonstration of a 64 bit a.out format.
47 The base file @file{aoutx.h} defines general mechanisms for
48 reading and writing records to and from disk and various
49 other methods which BFD requires. It is included by
50 @file{aout32.c} and @file{aout64.c} to form the names
51 <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc.
53 As an example, this is what goes on to make the back end for a
54 sun4, from @file{aout32.c}:
56 | #define ARCH_SIZE 32
57 | #include "aoutx.h"
59 Which exports names:
61 | ...
62 | aout_32_canonicalize_reloc
63 | aout_32_find_nearest_line
64 | aout_32_get_lineno
65 | aout_32_get_reloc_upper_bound
66 | ...
68 from @file{sunos.c}:
70 | #define TARGET_NAME "a.out-sunos-big"
71 | #define VECNAME sunos_big_vec
72 | #include "aoutf1.h"
74 requires all the names from @file{aout32.c}, and produces the jump vector
76 | sunos_big_vec
78 The file @file{host-aout.c} is a special case. It is for a large set
79 of hosts that use ``more or less standard'' a.out files, and
80 for which cross-debugging is not interesting. It uses the
81 standard 32-bit a.out support routines, but determines the
82 file offsets and addresses of the text, data, and BSS
83 sections, the machine architecture and machine type, and the
84 entry point address, in a host-dependent manner. Once these
85 values have been determined, generic code is used to handle
86 the object file.
88 When porting it to run on a new system, you must supply:
90 | HOST_PAGE_SIZE
91 | HOST_SEGMENT_SIZE
92 | HOST_MACHINE_ARCH (optional)
93 | HOST_MACHINE_MACHINE (optional)
94 | HOST_TEXT_START_ADDR
95 | HOST_STACK_END_ADDR
97 in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These
98 values, plus the structures and macros defined in @file{a.out.h} on
99 your host system, will produce a BFD target that will access
100 ordinary a.out files on your host. To configure a new machine
101 to use @file{host-aout.c}, specify:
103 | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
104 | TDEPFILES= host-aout.o trad-core.o
106 in the @file{config/@var{XXX}.mt} file, and modify @file{configure.in}
107 to use the
108 @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
109 configuration is selected. */
111 /* Some assumptions:
112 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
113 Doesn't matter what the setting of WP_TEXT is on output, but it'll
114 get set on input.
115 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
116 * Any BFD with both flags clear is OMAGIC.
117 (Just want to make these explicit, so the conditions tested in this
118 file make sense if you're more familiar with a.out than with BFD.) */
120 #define KEEPIT udata.i
122 #include "sysdep.h"
123 #include "bfd.h"
124 #include "safe-ctype.h"
125 #include "bfdlink.h"
127 #include "libaout.h"
128 #include "libbfd.h"
129 #include "aout/aout64.h"
130 #include "aout/stab_gnu.h"
131 #include "aout/ar.h"
134 SUBSECTION
135 Relocations
137 DESCRIPTION
138 The file @file{aoutx.h} provides for both the @emph{standard}
139 and @emph{extended} forms of a.out relocation records.
141 The standard records contain only an
142 address, a symbol index, and a type field. The extended records
143 (used on 29ks and sparcs) also have a full integer for an
144 addend. */
146 #ifndef CTOR_TABLE_RELOC_HOWTO
147 #define CTOR_TABLE_RELOC_IDX 2
148 #define CTOR_TABLE_RELOC_HOWTO(BFD) \
149 ((obj_reloc_entry_size (BFD) == RELOC_EXT_SIZE \
150 ? howto_table_ext : howto_table_std) \
151 + CTOR_TABLE_RELOC_IDX)
152 #endif
154 #ifndef MY_swap_std_reloc_in
155 #define MY_swap_std_reloc_in NAME (aout, swap_std_reloc_in)
156 #endif
158 #ifndef MY_swap_ext_reloc_in
159 #define MY_swap_ext_reloc_in NAME (aout, swap_ext_reloc_in)
160 #endif
162 #ifndef MY_swap_std_reloc_out
163 #define MY_swap_std_reloc_out NAME (aout, swap_std_reloc_out)
164 #endif
166 #ifndef MY_swap_ext_reloc_out
167 #define MY_swap_ext_reloc_out NAME (aout, swap_ext_reloc_out)
168 #endif
170 #ifndef MY_final_link_relocate
171 #define MY_final_link_relocate _bfd_final_link_relocate
172 #endif
174 #ifndef MY_relocate_contents
175 #define MY_relocate_contents _bfd_relocate_contents
176 #endif
178 #define howto_table_ext NAME (aout, ext_howto_table)
179 #define howto_table_std NAME (aout, std_howto_table)
181 reloc_howto_type howto_table_ext[] =
183 /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
184 HOWTO (RELOC_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "8", FALSE, 0, 0x000000ff, FALSE),
185 HOWTO (RELOC_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, 0, "16", FALSE, 0, 0x0000ffff, FALSE),
186 HOWTO (RELOC_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "32", FALSE, 0, 0xffffffff, FALSE),
187 HOWTO (RELOC_DISP8, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0, "DISP8", FALSE, 0, 0x000000ff, FALSE),
188 HOWTO (RELOC_DISP16, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0, "DISP16", FALSE, 0, 0x0000ffff, FALSE),
189 HOWTO (RELOC_DISP32, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0, "DISP32", FALSE, 0, 0xffffffff, FALSE),
190 HOWTO (RELOC_WDISP30, 2, 2, 30, TRUE, 0, complain_overflow_signed, 0, "WDISP30", FALSE, 0, 0x3fffffff, FALSE),
191 HOWTO (RELOC_WDISP22, 2, 2, 22, TRUE, 0, complain_overflow_signed, 0, "WDISP22", FALSE, 0, 0x003fffff, FALSE),
192 HOWTO (RELOC_HI22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "HI22", FALSE, 0, 0x003fffff, FALSE),
193 HOWTO (RELOC_22, 0, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "22", FALSE, 0, 0x003fffff, FALSE),
194 HOWTO (RELOC_13, 0, 2, 13, FALSE, 0, complain_overflow_bitfield, 0, "13", FALSE, 0, 0x00001fff, FALSE),
195 HOWTO (RELOC_LO10, 0, 2, 10, FALSE, 0, complain_overflow_dont, 0, "LO10", FALSE, 0, 0x000003ff, FALSE),
196 HOWTO (RELOC_SFA_BASE,0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "SFA_BASE", FALSE, 0, 0xffffffff, FALSE),
197 HOWTO (RELOC_SFA_OFF13,0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "SFA_OFF13", FALSE, 0, 0xffffffff, FALSE),
198 HOWTO (RELOC_BASE10, 0, 2, 10, FALSE, 0, complain_overflow_dont, 0, "BASE10", FALSE, 0, 0x000003ff, FALSE),
199 HOWTO (RELOC_BASE13, 0, 2, 13, FALSE, 0, complain_overflow_signed, 0, "BASE13", FALSE, 0, 0x00001fff, FALSE),
200 HOWTO (RELOC_BASE22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "BASE22", FALSE, 0, 0x003fffff, FALSE),
201 HOWTO (RELOC_PC10, 0, 2, 10, TRUE, 0, complain_overflow_dont, 0, "PC10", FALSE, 0, 0x000003ff, TRUE),
202 HOWTO (RELOC_PC22, 10, 2, 22, TRUE, 0, complain_overflow_signed, 0, "PC22", FALSE, 0, 0x003fffff, TRUE),
203 HOWTO (RELOC_JMP_TBL, 2, 2, 30, TRUE, 0, complain_overflow_signed, 0, "JMP_TBL", FALSE, 0, 0x3fffffff, FALSE),
204 HOWTO (RELOC_SEGOFF16,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "SEGOFF16", FALSE, 0, 0x00000000, FALSE),
205 HOWTO (RELOC_GLOB_DAT,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "GLOB_DAT", FALSE, 0, 0x00000000, FALSE),
206 HOWTO (RELOC_JMP_SLOT,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "JMP_SLOT", FALSE, 0, 0x00000000, FALSE),
207 HOWTO (RELOC_RELATIVE,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "RELATIVE", FALSE, 0, 0x00000000, FALSE),
208 HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE",FALSE, 0, 0x00000000, TRUE),
209 HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE",FALSE, 0, 0x00000000, TRUE),
210 #define RELOC_SPARC_REV32 RELOC_WDISP19
211 HOWTO (RELOC_SPARC_REV32, 0, 2, 32, FALSE, 0, complain_overflow_dont, 0,"R_SPARC_REV32",FALSE, 0, 0xffffffff, FALSE),
214 /* Convert standard reloc records to "arelent" format (incl byte swap). */
216 reloc_howto_type howto_table_std[] =
218 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
219 HOWTO ( 0, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,0,"8", TRUE, 0x000000ff,0x000000ff, FALSE),
220 HOWTO ( 1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
221 HOWTO ( 2, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"32", TRUE, 0xffffffff,0xffffffff, FALSE),
222 HOWTO ( 3, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,0,"64", TRUE, 0xdeaddead,0xdeaddead, FALSE),
223 HOWTO ( 4, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0,"DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
224 HOWTO ( 5, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0,"DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
225 HOWTO ( 6, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0,"DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
226 HOWTO ( 7, 0, 4, 64, TRUE, 0, complain_overflow_signed, 0,"DISP64", TRUE, 0xfeedface,0xfeedface, FALSE),
227 HOWTO ( 8, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"GOT_REL", FALSE, 0,0x00000000, FALSE),
228 HOWTO ( 9, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"BASE16", FALSE,0xffffffff,0xffffffff, FALSE),
229 HOWTO (10, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"BASE32", FALSE,0xffffffff,0xffffffff, FALSE),
230 EMPTY_HOWTO (-1),
231 EMPTY_HOWTO (-1),
232 EMPTY_HOWTO (-1),
233 EMPTY_HOWTO (-1),
234 EMPTY_HOWTO (-1),
235 HOWTO (16, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"JMP_TABLE", FALSE, 0,0x00000000, FALSE),
236 EMPTY_HOWTO (-1),
237 EMPTY_HOWTO (-1),
238 EMPTY_HOWTO (-1),
239 EMPTY_HOWTO (-1),
240 EMPTY_HOWTO (-1),
241 EMPTY_HOWTO (-1),
242 EMPTY_HOWTO (-1),
243 EMPTY_HOWTO (-1),
244 EMPTY_HOWTO (-1),
245 EMPTY_HOWTO (-1),
246 EMPTY_HOWTO (-1),
247 EMPTY_HOWTO (-1),
248 EMPTY_HOWTO (-1),
249 EMPTY_HOWTO (-1),
250 EMPTY_HOWTO (-1),
251 HOWTO (32, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"RELATIVE", FALSE, 0,0x00000000, FALSE),
252 EMPTY_HOWTO (-1),
253 EMPTY_HOWTO (-1),
254 EMPTY_HOWTO (-1),
255 EMPTY_HOWTO (-1),
256 EMPTY_HOWTO (-1),
257 EMPTY_HOWTO (-1),
258 EMPTY_HOWTO (-1),
259 HOWTO (40, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"BASEREL", FALSE, 0,0x00000000, FALSE),
262 #define TABLE_SIZE(TABLE) (sizeof (TABLE) / sizeof (TABLE[0]))
264 reloc_howto_type *
265 NAME (aout, reloc_type_lookup) (bfd *abfd, bfd_reloc_code_real_type code)
267 #define EXT(i, j) case i: return & howto_table_ext [j]
268 #define STD(i, j) case i: return & howto_table_std [j]
269 int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
271 if (code == BFD_RELOC_CTOR)
272 switch (bfd_get_arch_info (abfd)->bits_per_address)
274 case 32:
275 code = BFD_RELOC_32;
276 break;
277 case 64:
278 code = BFD_RELOC_64;
279 break;
282 if (ext)
283 switch (code)
285 EXT (BFD_RELOC_8, 0);
286 EXT (BFD_RELOC_16, 1);
287 EXT (BFD_RELOC_32, 2);
288 EXT (BFD_RELOC_HI22, 8);
289 EXT (BFD_RELOC_LO10, 11);
290 EXT (BFD_RELOC_32_PCREL_S2, 6);
291 EXT (BFD_RELOC_SPARC_WDISP22, 7);
292 EXT (BFD_RELOC_SPARC13, 10);
293 EXT (BFD_RELOC_SPARC_GOT10, 14);
294 EXT (BFD_RELOC_SPARC_BASE13, 15);
295 EXT (BFD_RELOC_SPARC_GOT13, 15);
296 EXT (BFD_RELOC_SPARC_GOT22, 16);
297 EXT (BFD_RELOC_SPARC_PC10, 17);
298 EXT (BFD_RELOC_SPARC_PC22, 18);
299 EXT (BFD_RELOC_SPARC_WPLT30, 19);
300 EXT (BFD_RELOC_SPARC_REV32, 26);
301 default:
302 return NULL;
304 else
305 /* std relocs. */
306 switch (code)
308 STD (BFD_RELOC_8, 0);
309 STD (BFD_RELOC_16, 1);
310 STD (BFD_RELOC_32, 2);
311 STD (BFD_RELOC_8_PCREL, 4);
312 STD (BFD_RELOC_16_PCREL, 5);
313 STD (BFD_RELOC_32_PCREL, 6);
314 STD (BFD_RELOC_16_BASEREL, 9);
315 STD (BFD_RELOC_32_BASEREL, 10);
316 default:
317 return NULL;
321 reloc_howto_type *
322 NAME (aout, reloc_name_lookup) (bfd *abfd, const char *r_name)
324 unsigned int i, size;
325 reloc_howto_type *howto_table;
327 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
329 howto_table = howto_table_ext;
330 size = sizeof (howto_table_ext) / sizeof (howto_table_ext[0]);
332 else
334 howto_table = howto_table_std;
335 size = sizeof (howto_table_std) / sizeof (howto_table_std[0]);
338 for (i = 0; i < size; i++)
339 if (howto_table[i].name != NULL
340 && strcasecmp (howto_table[i].name, r_name) == 0)
341 return &howto_table[i];
343 return NULL;
347 SUBSECTION
348 Internal entry points
350 DESCRIPTION
351 @file{aoutx.h} exports several routines for accessing the
352 contents of an a.out file, which are gathered and exported in
353 turn by various format specific files (eg sunos.c).
357 FUNCTION
358 aout_@var{size}_swap_exec_header_in
360 SYNOPSIS
361 void aout_@var{size}_swap_exec_header_in,
362 (bfd *abfd,
363 struct external_exec *bytes,
364 struct internal_exec *execp);
366 DESCRIPTION
367 Swap the information in an executable header @var{raw_bytes} taken
368 from a raw byte stream memory image into the internal exec header
369 structure @var{execp}.
372 #ifndef NAME_swap_exec_header_in
373 void
374 NAME (aout, swap_exec_header_in) (bfd *abfd,
375 struct external_exec *bytes,
376 struct internal_exec *execp)
378 /* The internal_exec structure has some fields that are unused in this
379 configuration (IE for i960), so ensure that all such uninitialized
380 fields are zero'd out. There are places where two of these structs
381 are memcmp'd, and thus the contents do matter. */
382 memset ((void *) execp, 0, sizeof (struct internal_exec));
383 /* Now fill in fields in the execp, from the bytes in the raw data. */
384 execp->a_info = H_GET_32 (abfd, bytes->e_info);
385 execp->a_text = GET_WORD (abfd, bytes->e_text);
386 execp->a_data = GET_WORD (abfd, bytes->e_data);
387 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
388 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
389 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
390 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
391 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
393 #define NAME_swap_exec_header_in NAME (aout, swap_exec_header_in)
394 #endif
397 FUNCTION
398 aout_@var{size}_swap_exec_header_out
400 SYNOPSIS
401 void aout_@var{size}_swap_exec_header_out
402 (bfd *abfd,
403 struct internal_exec *execp,
404 struct external_exec *raw_bytes);
406 DESCRIPTION
407 Swap the information in an internal exec header structure
408 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
410 void
411 NAME (aout, swap_exec_header_out) (bfd *abfd,
412 struct internal_exec *execp,
413 struct external_exec *bytes)
415 /* Now fill in fields in the raw data, from the fields in the exec struct. */
416 H_PUT_32 (abfd, execp->a_info , bytes->e_info);
417 PUT_WORD (abfd, execp->a_text , bytes->e_text);
418 PUT_WORD (abfd, execp->a_data , bytes->e_data);
419 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
420 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
421 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
422 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
423 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
426 /* Make all the section for an a.out file. */
428 bfd_boolean
429 NAME (aout, make_sections) (bfd *abfd)
431 if (obj_textsec (abfd) == NULL && bfd_make_section (abfd, ".text") == NULL)
432 return FALSE;
433 if (obj_datasec (abfd) == NULL && bfd_make_section (abfd, ".data") == NULL)
434 return FALSE;
435 if (obj_bsssec (abfd) == NULL && bfd_make_section (abfd, ".bss") == NULL)
436 return FALSE;
437 return TRUE;
441 FUNCTION
442 aout_@var{size}_some_aout_object_p
444 SYNOPSIS
445 const bfd_target *aout_@var{size}_some_aout_object_p
446 (bfd *abfd,
447 struct internal_exec *execp,
448 const bfd_target *(*callback_to_real_object_p) (bfd *));
450 DESCRIPTION
451 Some a.out variant thinks that the file open in @var{abfd}
452 checking is an a.out file. Do some more checking, and set up
453 for access if it really is. Call back to the calling
454 environment's "finish up" function just before returning, to
455 handle any last-minute setup.
458 const bfd_target *
459 NAME (aout, some_aout_object_p) (bfd *abfd,
460 struct internal_exec *execp,
461 const bfd_target *(*callback_to_real_object_p) (bfd *))
463 struct aout_data_struct *rawptr, *oldrawptr;
464 const bfd_target *result;
465 bfd_size_type amt = sizeof (* rawptr);
467 rawptr = bfd_zalloc (abfd, amt);
468 if (rawptr == NULL)
469 return NULL;
471 oldrawptr = abfd->tdata.aout_data;
472 abfd->tdata.aout_data = rawptr;
474 /* Copy the contents of the old tdata struct.
475 In particular, we want the subformat, since for hpux it was set in
476 hp300hpux.c:swap_exec_header_in and will be used in
477 hp300hpux.c:callback. */
478 if (oldrawptr != NULL)
479 *abfd->tdata.aout_data = *oldrawptr;
481 abfd->tdata.aout_data->a.hdr = &rawptr->e;
482 /* Copy in the internal_exec struct. */
483 *(abfd->tdata.aout_data->a.hdr) = *execp;
484 execp = abfd->tdata.aout_data->a.hdr;
486 /* Set the file flags. */
487 abfd->flags = BFD_NO_FLAGS;
488 if (execp->a_drsize || execp->a_trsize)
489 abfd->flags |= HAS_RELOC;
490 /* Setting of EXEC_P has been deferred to the bottom of this function. */
491 if (execp->a_syms)
492 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
493 if (N_DYNAMIC (*execp))
494 abfd->flags |= DYNAMIC;
496 if (N_MAGIC (*execp) == ZMAGIC)
498 abfd->flags |= D_PAGED | WP_TEXT;
499 adata (abfd).magic = z_magic;
501 else if (N_MAGIC (*execp) == QMAGIC)
503 abfd->flags |= D_PAGED | WP_TEXT;
504 adata (abfd).magic = z_magic;
505 adata (abfd).subformat = q_magic_format;
507 else if (N_MAGIC (*execp) == NMAGIC)
509 abfd->flags |= WP_TEXT;
510 adata (abfd).magic = n_magic;
512 else if (N_MAGIC (*execp) == OMAGIC
513 || N_MAGIC (*execp) == BMAGIC)
514 adata (abfd).magic = o_magic;
515 else
516 /* Should have been checked with N_BADMAG before this routine
517 was called. */
518 abort ();
520 bfd_get_start_address (abfd) = execp->a_entry;
522 obj_aout_symbols (abfd) = NULL;
523 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
525 /* The default relocation entry size is that of traditional V7 Unix. */
526 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
528 /* The default symbol entry size is that of traditional Unix. */
529 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
531 #ifdef USE_MMAP
532 bfd_init_window (&obj_aout_sym_window (abfd));
533 bfd_init_window (&obj_aout_string_window (abfd));
534 #endif
535 obj_aout_external_syms (abfd) = NULL;
536 obj_aout_external_strings (abfd) = NULL;
537 obj_aout_sym_hashes (abfd) = NULL;
539 if (! NAME (aout, make_sections) (abfd))
540 goto error_ret;
542 obj_datasec (abfd)->size = execp->a_data;
543 obj_bsssec (abfd)->size = execp->a_bss;
545 obj_textsec (abfd)->flags =
546 (execp->a_trsize != 0
547 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
548 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
549 obj_datasec (abfd)->flags =
550 (execp->a_drsize != 0
551 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
552 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
553 obj_bsssec (abfd)->flags = SEC_ALLOC;
555 #ifdef THIS_IS_ONLY_DOCUMENTATION
556 /* The common code can't fill in these things because they depend
557 on either the start address of the text segment, the rounding
558 up of virtual addresses between segments, or the starting file
559 position of the text segment -- all of which varies among different
560 versions of a.out. */
562 /* Call back to the format-dependent code to fill in the rest of the
563 fields and do any further cleanup. Things that should be filled
564 in by the callback: */
566 struct exec *execp = exec_hdr (abfd);
568 obj_textsec (abfd)->size = N_TXTSIZE (*execp);
569 /* Data and bss are already filled in since they're so standard. */
571 /* The virtual memory addresses of the sections. */
572 obj_textsec (abfd)->vma = N_TXTADDR (*execp);
573 obj_datasec (abfd)->vma = N_DATADDR (*execp);
574 obj_bsssec (abfd)->vma = N_BSSADDR (*execp);
576 /* The file offsets of the sections. */
577 obj_textsec (abfd)->filepos = N_TXTOFF (*execp);
578 obj_datasec (abfd)->filepos = N_DATOFF (*execp);
580 /* The file offsets of the relocation info. */
581 obj_textsec (abfd)->rel_filepos = N_TRELOFF (*execp);
582 obj_datasec (abfd)->rel_filepos = N_DRELOFF (*execp);
584 /* The file offsets of the string table and symbol table. */
585 obj_str_filepos (abfd) = N_STROFF (*execp);
586 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
588 /* Determine the architecture and machine type of the object file. */
589 switch (N_MACHTYPE (*exec_hdr (abfd)))
591 default:
592 abfd->obj_arch = bfd_arch_obscure;
593 break;
596 adata (abfd)->page_size = TARGET_PAGE_SIZE;
597 adata (abfd)->segment_size = SEGMENT_SIZE;
598 adata (abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
600 return abfd->xvec;
602 /* The architecture is encoded in various ways in various a.out variants,
603 or is not encoded at all in some of them. The relocation size depends
604 on the architecture and the a.out variant. Finally, the return value
605 is the bfd_target vector in use. If an error occurs, return zero and
606 set bfd_error to the appropriate error code.
608 Formats such as b.out, which have additional fields in the a.out
609 header, should cope with them in this callback as well. */
610 #endif /* DOCUMENTATION */
612 result = (*callback_to_real_object_p) (abfd);
614 /* Now that the segment addresses have been worked out, take a better
615 guess at whether the file is executable. If the entry point
616 is within the text segment, assume it is. (This makes files
617 executable even if their entry point address is 0, as long as
618 their text starts at zero.).
620 This test had to be changed to deal with systems where the text segment
621 runs at a different location than the default. The problem is that the
622 entry address can appear to be outside the text segment, thus causing an
623 erroneous conclusion that the file isn't executable.
625 To fix this, we now accept any non-zero entry point as an indication of
626 executability. This will work most of the time, since only the linker
627 sets the entry point, and that is likely to be non-zero for most systems. */
629 if (execp->a_entry != 0
630 || (execp->a_entry >= obj_textsec (abfd)->vma
631 && execp->a_entry < (obj_textsec (abfd)->vma
632 + obj_textsec (abfd)->size)))
633 abfd->flags |= EXEC_P;
634 #ifdef STAT_FOR_EXEC
635 else
637 struct stat stat_buf;
639 /* The original heuristic doesn't work in some important cases.
640 The a.out file has no information about the text start
641 address. For files (like kernels) linked to non-standard
642 addresses (ld -Ttext nnn) the entry point may not be between
643 the default text start (obj_textsec(abfd)->vma) and
644 (obj_textsec(abfd)->vma) + text size. This is not just a mach
645 issue. Many kernels are loaded at non standard addresses. */
646 if (abfd->iostream != NULL
647 && (abfd->flags & BFD_IN_MEMORY) == 0
648 && (fstat (fileno ((FILE *) (abfd->iostream)), &stat_buf) == 0)
649 && ((stat_buf.st_mode & 0111) != 0))
650 abfd->flags |= EXEC_P;
652 #endif /* STAT_FOR_EXEC */
654 if (result)
655 return result;
657 error_ret:
658 bfd_release (abfd, rawptr);
659 abfd->tdata.aout_data = oldrawptr;
660 return NULL;
664 FUNCTION
665 aout_@var{size}_mkobject
667 SYNOPSIS
668 bfd_boolean aout_@var{size}_mkobject, (bfd *abfd);
670 DESCRIPTION
671 Initialize BFD @var{abfd} for use with a.out files.
674 bfd_boolean
675 NAME (aout, mkobject) (bfd *abfd)
677 struct aout_data_struct *rawptr;
678 bfd_size_type amt = sizeof (* rawptr);
680 bfd_set_error (bfd_error_system_call);
682 rawptr = bfd_zalloc (abfd, amt);
683 if (rawptr == NULL)
684 return FALSE;
686 abfd->tdata.aout_data = rawptr;
687 exec_hdr (abfd) = &(rawptr->e);
689 obj_textsec (abfd) = NULL;
690 obj_datasec (abfd) = NULL;
691 obj_bsssec (abfd) = NULL;
693 return TRUE;
697 FUNCTION
698 aout_@var{size}_machine_type
700 SYNOPSIS
701 enum machine_type aout_@var{size}_machine_type
702 (enum bfd_architecture arch,
703 unsigned long machine,
704 bfd_boolean *unknown);
706 DESCRIPTION
707 Keep track of machine architecture and machine type for
708 a.out's. Return the <<machine_type>> for a particular
709 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
710 and machine can't be represented in a.out format.
712 If the architecture is understood, machine type 0 (default)
713 is always understood.
716 enum machine_type
717 NAME (aout, machine_type) (enum bfd_architecture arch,
718 unsigned long machine,
719 bfd_boolean *unknown)
721 enum machine_type arch_flags;
723 arch_flags = M_UNKNOWN;
724 *unknown = TRUE;
726 switch (arch)
728 case bfd_arch_sparc:
729 if (machine == 0
730 || machine == bfd_mach_sparc
731 || machine == bfd_mach_sparc_sparclite
732 || machine == bfd_mach_sparc_sparclite_le
733 || machine == bfd_mach_sparc_v8plus
734 || machine == bfd_mach_sparc_v8plusa
735 || machine == bfd_mach_sparc_v8plusb
736 || machine == bfd_mach_sparc_v9
737 || machine == bfd_mach_sparc_v9a
738 || machine == bfd_mach_sparc_v9b)
739 arch_flags = M_SPARC;
740 else if (machine == bfd_mach_sparc_sparclet)
741 arch_flags = M_SPARCLET;
742 break;
744 case bfd_arch_m68k:
745 switch (machine)
747 case 0: arch_flags = M_68010; break;
748 case bfd_mach_m68000: arch_flags = M_UNKNOWN; *unknown = FALSE; break;
749 case bfd_mach_m68010: arch_flags = M_68010; break;
750 case bfd_mach_m68020: arch_flags = M_68020; break;
751 default: arch_flags = M_UNKNOWN; break;
753 break;
755 case bfd_arch_i386:
756 if (machine == 0
757 || machine == bfd_mach_i386_i386
758 || machine == bfd_mach_i386_i386_intel_syntax)
759 arch_flags = M_386;
760 break;
762 case bfd_arch_arm:
763 if (machine == 0)
764 arch_flags = M_ARM;
765 break;
767 case bfd_arch_mips:
768 switch (machine)
770 case 0:
771 case bfd_mach_mips3000:
772 case bfd_mach_mips3900:
773 arch_flags = M_MIPS1;
774 break;
775 case bfd_mach_mips6000:
776 arch_flags = M_MIPS2;
777 break;
778 case bfd_mach_mips4000:
779 case bfd_mach_mips4010:
780 case bfd_mach_mips4100:
781 case bfd_mach_mips4300:
782 case bfd_mach_mips4400:
783 case bfd_mach_mips4600:
784 case bfd_mach_mips4650:
785 case bfd_mach_mips8000:
786 case bfd_mach_mips9000:
787 case bfd_mach_mips10000:
788 case bfd_mach_mips12000:
789 case bfd_mach_mips14000:
790 case bfd_mach_mips16000:
791 case bfd_mach_mips16:
792 case bfd_mach_mipsisa32:
793 case bfd_mach_mipsisa32r2:
794 case bfd_mach_mips5:
795 case bfd_mach_mipsisa64:
796 case bfd_mach_mipsisa64r2:
797 case bfd_mach_mips_sb1:
798 case bfd_mach_mips_xlr:
799 /* FIXME: These should be MIPS3, MIPS4, MIPS16, MIPS32, etc. */
800 arch_flags = M_MIPS2;
801 break;
802 default:
803 arch_flags = M_UNKNOWN;
804 break;
806 break;
808 case bfd_arch_ns32k:
809 switch (machine)
811 case 0: arch_flags = M_NS32532; break;
812 case 32032: arch_flags = M_NS32032; break;
813 case 32532: arch_flags = M_NS32532; break;
814 default: arch_flags = M_UNKNOWN; break;
816 break;
818 case bfd_arch_vax:
819 *unknown = FALSE;
820 break;
822 case bfd_arch_cris:
823 if (machine == 0 || machine == 255)
824 arch_flags = M_CRIS;
825 break;
827 case bfd_arch_m88k:
828 *unknown = FALSE;
829 break;
831 default:
832 arch_flags = M_UNKNOWN;
835 if (arch_flags != M_UNKNOWN)
836 *unknown = FALSE;
838 return arch_flags;
842 FUNCTION
843 aout_@var{size}_set_arch_mach
845 SYNOPSIS
846 bfd_boolean aout_@var{size}_set_arch_mach,
847 (bfd *,
848 enum bfd_architecture arch,
849 unsigned long machine);
851 DESCRIPTION
852 Set the architecture and the machine of the BFD @var{abfd} to the
853 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
854 can support the architecture required.
857 bfd_boolean
858 NAME (aout, set_arch_mach) (bfd *abfd,
859 enum bfd_architecture arch,
860 unsigned long machine)
862 if (! bfd_default_set_arch_mach (abfd, arch, machine))
863 return FALSE;
865 if (arch != bfd_arch_unknown)
867 bfd_boolean unknown;
869 NAME (aout, machine_type) (arch, machine, &unknown);
870 if (unknown)
871 return FALSE;
874 /* Determine the size of a relocation entry. */
875 switch (arch)
877 case bfd_arch_sparc:
878 case bfd_arch_mips:
879 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
880 break;
881 default:
882 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
883 break;
886 return (*aout_backend_info (abfd)->set_sizes) (abfd);
889 static void
890 adjust_o_magic (bfd *abfd, struct internal_exec *execp)
892 file_ptr pos = adata (abfd).exec_bytes_size;
893 bfd_vma vma = 0;
894 int pad = 0;
896 /* Text. */
897 obj_textsec (abfd)->filepos = pos;
898 if (!obj_textsec (abfd)->user_set_vma)
899 obj_textsec (abfd)->vma = vma;
900 else
901 vma = obj_textsec (abfd)->vma;
903 pos += obj_textsec (abfd)->size;
904 vma += obj_textsec (abfd)->size;
906 /* Data. */
907 if (!obj_datasec (abfd)->user_set_vma)
909 obj_textsec (abfd)->size += pad;
910 pos += pad;
911 vma += pad;
912 obj_datasec (abfd)->vma = vma;
914 else
915 vma = obj_datasec (abfd)->vma;
916 obj_datasec (abfd)->filepos = pos;
917 pos += obj_datasec (abfd)->size;
918 vma += obj_datasec (abfd)->size;
920 /* BSS. */
921 if (!obj_bsssec (abfd)->user_set_vma)
923 obj_datasec (abfd)->size += pad;
924 pos += pad;
925 vma += pad;
926 obj_bsssec (abfd)->vma = vma;
928 else
930 /* The VMA of the .bss section is set by the VMA of the
931 .data section plus the size of the .data section. We may
932 need to add padding bytes to make this true. */
933 pad = obj_bsssec (abfd)->vma - vma;
934 if (pad > 0)
936 obj_datasec (abfd)->size += pad;
937 pos += pad;
940 obj_bsssec (abfd)->filepos = pos;
942 /* Fix up the exec header. */
943 execp->a_text = obj_textsec (abfd)->size;
944 execp->a_data = obj_datasec (abfd)->size;
945 execp->a_bss = obj_bsssec (abfd)->size;
946 N_SET_MAGIC (*execp, OMAGIC);
949 static void
950 adjust_z_magic (bfd *abfd, struct internal_exec *execp)
952 bfd_size_type data_pad, text_pad;
953 file_ptr text_end;
954 const struct aout_backend_data *abdp;
955 /* TRUE if text includes exec header. */
956 bfd_boolean ztih;
958 abdp = aout_backend_info (abfd);
960 /* Text. */
961 ztih = (abdp != NULL
962 && (abdp->text_includes_header
963 || obj_aout_subformat (abfd) == q_magic_format));
964 obj_textsec (abfd)->filepos = (ztih
965 ? adata (abfd).exec_bytes_size
966 : adata (abfd).zmagic_disk_block_size);
967 if (! obj_textsec (abfd)->user_set_vma)
969 /* ?? Do we really need to check for relocs here? */
970 obj_textsec (abfd)->vma = ((abfd->flags & HAS_RELOC)
972 : (ztih
973 ? (abdp->default_text_vma
974 + adata (abfd).exec_bytes_size)
975 : abdp->default_text_vma));
976 text_pad = 0;
978 else
980 /* The .text section is being loaded at an unusual address. We
981 may need to pad it such that the .data section starts at a page
982 boundary. */
983 if (ztih)
984 text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
985 & (adata (abfd).page_size - 1));
986 else
987 text_pad = ((- obj_textsec (abfd)->vma)
988 & (adata (abfd).page_size - 1));
991 /* Find start of data. */
992 if (ztih)
994 text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->size;
995 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
997 else
999 /* Note that if page_size == zmagic_disk_block_size, then
1000 filepos == page_size, and this case is the same as the ztih
1001 case. */
1002 text_end = obj_textsec (abfd)->size;
1003 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1004 text_end += obj_textsec (abfd)->filepos;
1006 obj_textsec (abfd)->size += text_pad;
1007 text_end += text_pad;
1009 /* Data. */
1010 if (!obj_datasec (abfd)->user_set_vma)
1012 bfd_vma vma;
1013 vma = obj_textsec (abfd)->vma + obj_textsec (abfd)->size;
1014 obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1016 if (abdp && abdp->zmagic_mapped_contiguous)
1018 asection * text = obj_textsec (abfd);
1019 asection * data = obj_datasec (abfd);
1021 text_pad = data->vma - (text->vma + text->size);
1022 /* Only pad the text section if the data
1023 section is going to be placed after it. */
1024 if (text_pad > 0)
1025 text->size += text_pad;
1027 obj_datasec (abfd)->filepos = (obj_textsec (abfd)->filepos
1028 + obj_textsec (abfd)->size);
1030 /* Fix up exec header while we're at it. */
1031 execp->a_text = obj_textsec (abfd)->size;
1032 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
1033 execp->a_text += adata (abfd).exec_bytes_size;
1034 if (obj_aout_subformat (abfd) == q_magic_format)
1035 N_SET_MAGIC (*execp, QMAGIC);
1036 else
1037 N_SET_MAGIC (*execp, ZMAGIC);
1039 /* Spec says data section should be rounded up to page boundary. */
1040 obj_datasec (abfd)->size
1041 = align_power (obj_datasec (abfd)->size,
1042 obj_bsssec (abfd)->alignment_power);
1043 execp->a_data = BFD_ALIGN (obj_datasec (abfd)->size,
1044 adata (abfd).page_size);
1045 data_pad = execp->a_data - obj_datasec (abfd)->size;
1047 /* BSS. */
1048 if (!obj_bsssec (abfd)->user_set_vma)
1049 obj_bsssec (abfd)->vma = (obj_datasec (abfd)->vma
1050 + obj_datasec (abfd)->size);
1051 /* If the BSS immediately follows the data section and extra space
1052 in the page is left after the data section, fudge data
1053 in the header so that the bss section looks smaller by that
1054 amount. We'll start the bss section there, and lie to the OS.
1055 (Note that a linker script, as well as the above assignment,
1056 could have explicitly set the BSS vma to immediately follow
1057 the data section.) */
1058 if (align_power (obj_bsssec (abfd)->vma, obj_bsssec (abfd)->alignment_power)
1059 == obj_datasec (abfd)->vma + obj_datasec (abfd)->size)
1060 execp->a_bss = (data_pad > obj_bsssec (abfd)->size
1061 ? 0 : obj_bsssec (abfd)->size - data_pad);
1062 else
1063 execp->a_bss = obj_bsssec (abfd)->size;
1066 static void
1067 adjust_n_magic (bfd *abfd, struct internal_exec *execp)
1069 file_ptr pos = adata (abfd).exec_bytes_size;
1070 bfd_vma vma = 0;
1071 int pad;
1073 /* Text. */
1074 obj_textsec (abfd)->filepos = pos;
1075 if (!obj_textsec (abfd)->user_set_vma)
1076 obj_textsec (abfd)->vma = vma;
1077 else
1078 vma = obj_textsec (abfd)->vma;
1079 pos += obj_textsec (abfd)->size;
1080 vma += obj_textsec (abfd)->size;
1082 /* Data. */
1083 obj_datasec (abfd)->filepos = pos;
1084 if (!obj_datasec (abfd)->user_set_vma)
1085 obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1086 vma = obj_datasec (abfd)->vma;
1088 /* Since BSS follows data immediately, see if it needs alignment. */
1089 vma += obj_datasec (abfd)->size;
1090 pad = align_power (vma, obj_bsssec (abfd)->alignment_power) - vma;
1091 obj_datasec (abfd)->size += pad;
1092 pos += obj_datasec (abfd)->size;
1094 /* BSS. */
1095 if (!obj_bsssec (abfd)->user_set_vma)
1096 obj_bsssec (abfd)->vma = vma;
1097 else
1098 vma = obj_bsssec (abfd)->vma;
1100 /* Fix up exec header. */
1101 execp->a_text = obj_textsec (abfd)->size;
1102 execp->a_data = obj_datasec (abfd)->size;
1103 execp->a_bss = obj_bsssec (abfd)->size;
1104 N_SET_MAGIC (*execp, NMAGIC);
1107 bfd_boolean
1108 NAME (aout, adjust_sizes_and_vmas) (bfd *abfd,
1109 bfd_size_type *text_size,
1110 file_ptr *text_end ATTRIBUTE_UNUSED)
1112 struct internal_exec *execp = exec_hdr (abfd);
1114 if (! NAME (aout, make_sections) (abfd))
1115 return FALSE;
1117 if (adata (abfd).magic != undecided_magic)
1118 return TRUE;
1120 obj_textsec (abfd)->size =
1121 align_power (obj_textsec (abfd)->size,
1122 obj_textsec (abfd)->alignment_power);
1124 *text_size = obj_textsec (abfd)->size;
1125 /* Rule (heuristic) for when to pad to a new page. Note that there
1126 are (at least) two ways demand-paged (ZMAGIC) files have been
1127 handled. Most Berkeley-based systems start the text segment at
1128 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1129 segment right after the exec header; the latter is counted in the
1130 text segment size, and is paged in by the kernel with the rest of
1131 the text. */
1133 /* This perhaps isn't the right way to do this, but made it simpler for me
1134 to understand enough to implement it. Better would probably be to go
1135 right from BFD flags to alignment/positioning characteristics. But the
1136 old code was sloppy enough about handling the flags, and had enough
1137 other magic, that it was a little hard for me to understand. I think
1138 I understand it better now, but I haven't time to do the cleanup this
1139 minute. */
1141 if (abfd->flags & D_PAGED)
1142 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
1143 adata (abfd).magic = z_magic;
1144 else if (abfd->flags & WP_TEXT)
1145 adata (abfd).magic = n_magic;
1146 else
1147 adata (abfd).magic = o_magic;
1149 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1150 #if __GNUC__ >= 2
1151 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1152 ({ char *str;
1153 switch (adata (abfd).magic)
1155 case n_magic: str = "NMAGIC"; break;
1156 case o_magic: str = "OMAGIC"; break;
1157 case z_magic: str = "ZMAGIC"; break;
1158 default: abort ();
1160 str;
1162 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1163 obj_textsec (abfd)->alignment_power,
1164 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1165 obj_datasec (abfd)->alignment_power,
1166 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size,
1167 obj_bsssec (abfd)->alignment_power);
1168 #endif
1169 #endif
1171 switch (adata (abfd).magic)
1173 case o_magic:
1174 adjust_o_magic (abfd, execp);
1175 break;
1176 case z_magic:
1177 adjust_z_magic (abfd, execp);
1178 break;
1179 case n_magic:
1180 adjust_n_magic (abfd, execp);
1181 break;
1182 default:
1183 abort ();
1186 #ifdef BFD_AOUT_DEBUG
1187 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1188 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1189 obj_textsec (abfd)->filepos,
1190 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1191 obj_datasec (abfd)->filepos,
1192 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size);
1193 #endif
1195 return TRUE;
1199 FUNCTION
1200 aout_@var{size}_new_section_hook
1202 SYNOPSIS
1203 bfd_boolean aout_@var{size}_new_section_hook,
1204 (bfd *abfd,
1205 asection *newsect);
1207 DESCRIPTION
1208 Called by the BFD in response to a @code{bfd_make_section}
1209 request.
1211 bfd_boolean
1212 NAME (aout, new_section_hook) (bfd *abfd, asection *newsect)
1214 /* Align to double at least. */
1215 newsect->alignment_power = bfd_get_arch_info (abfd)->section_align_power;
1217 if (bfd_get_format (abfd) == bfd_object)
1219 if (obj_textsec (abfd) == NULL && !strcmp (newsect->name, ".text"))
1221 obj_textsec (abfd)= newsect;
1222 newsect->target_index = N_TEXT;
1224 else if (obj_datasec (abfd) == NULL && !strcmp (newsect->name, ".data"))
1226 obj_datasec (abfd) = newsect;
1227 newsect->target_index = N_DATA;
1229 else if (obj_bsssec (abfd) == NULL && !strcmp (newsect->name, ".bss"))
1231 obj_bsssec (abfd) = newsect;
1232 newsect->target_index = N_BSS;
1236 /* We allow more than three sections internally. */
1237 return _bfd_generic_new_section_hook (abfd, newsect);
1240 bfd_boolean
1241 NAME (aout, set_section_contents) (bfd *abfd,
1242 sec_ptr section,
1243 const void * location,
1244 file_ptr offset,
1245 bfd_size_type count)
1247 file_ptr text_end;
1248 bfd_size_type text_size;
1250 if (! abfd->output_has_begun)
1252 if (! NAME (aout, adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
1253 return FALSE;
1256 if (section == obj_bsssec (abfd))
1258 bfd_set_error (bfd_error_no_contents);
1259 return FALSE;
1262 if (section != obj_textsec (abfd)
1263 && section != obj_datasec (abfd))
1265 if (aout_section_merge_with_text_p (abfd, section))
1266 section->filepos = obj_textsec (abfd)->filepos +
1267 (section->vma - obj_textsec (abfd)->vma);
1268 else
1270 (*_bfd_error_handler)
1271 (_("%s: can not represent section `%s' in a.out object file format"),
1272 bfd_get_filename (abfd), bfd_get_section_name (abfd, section));
1273 bfd_set_error (bfd_error_nonrepresentable_section);
1274 return FALSE;
1278 if (count != 0)
1280 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1281 || bfd_bwrite (location, count, abfd) != count)
1282 return FALSE;
1285 return TRUE;
1288 /* Read the external symbols from an a.out file. */
1290 static bfd_boolean
1291 aout_get_external_symbols (bfd *abfd)
1293 if (obj_aout_external_syms (abfd) == NULL)
1295 bfd_size_type count;
1296 struct external_nlist *syms;
1297 bfd_size_type amt;
1299 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
1300 if (count == 0)
1301 return TRUE; /* Nothing to do. */
1303 #ifdef USE_MMAP
1304 if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd),
1305 exec_hdr (abfd)->a_syms,
1306 &obj_aout_sym_window (abfd), TRUE))
1307 return FALSE;
1308 syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
1309 #else
1310 /* We allocate using malloc to make the values easy to free
1311 later on. If we put them on the objalloc it might not be
1312 possible to free them. */
1313 syms = bfd_malloc (count * EXTERNAL_NLIST_SIZE);
1314 if (syms == NULL)
1315 return FALSE;
1317 amt = exec_hdr (abfd)->a_syms;
1318 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1319 || bfd_bread (syms, amt, abfd) != amt)
1321 free (syms);
1322 return FALSE;
1324 #endif
1326 obj_aout_external_syms (abfd) = syms;
1327 obj_aout_external_sym_count (abfd) = count;
1330 if (obj_aout_external_strings (abfd) == NULL
1331 && exec_hdr (abfd)->a_syms != 0)
1333 unsigned char string_chars[BYTES_IN_WORD];
1334 bfd_size_type stringsize;
1335 char *strings;
1336 bfd_size_type amt = BYTES_IN_WORD;
1338 /* Get the size of the strings. */
1339 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1340 || bfd_bread ((void *) string_chars, amt, abfd) != amt)
1341 return FALSE;
1342 stringsize = GET_WORD (abfd, string_chars);
1344 #ifdef USE_MMAP
1345 if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize,
1346 &obj_aout_string_window (abfd), TRUE))
1347 return FALSE;
1348 strings = (char *) obj_aout_string_window (abfd).data;
1349 #else
1350 strings = bfd_malloc (stringsize + 1);
1351 if (strings == NULL)
1352 return FALSE;
1354 /* Skip space for the string count in the buffer for convenience
1355 when using indexes. */
1356 amt = stringsize - BYTES_IN_WORD;
1357 if (bfd_bread (strings + BYTES_IN_WORD, amt, abfd) != amt)
1359 free (strings);
1360 return FALSE;
1362 #endif
1364 /* Ensure that a zero index yields an empty string. */
1365 strings[0] = '\0';
1367 strings[stringsize - 1] = 0;
1369 obj_aout_external_strings (abfd) = strings;
1370 obj_aout_external_string_size (abfd) = stringsize;
1373 return TRUE;
1376 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1377 and symbol->value fields of CACHE_PTR will be set from the a.out
1378 nlist structure. This function is responsible for setting
1379 symbol->flags and symbol->section, and adjusting symbol->value. */
1381 static bfd_boolean
1382 translate_from_native_sym_flags (bfd *abfd, aout_symbol_type *cache_ptr)
1384 flagword visible;
1386 if ((cache_ptr->type & N_STAB) != 0
1387 || cache_ptr->type == N_FN)
1389 asection *sec;
1391 /* This is a debugging symbol. */
1392 cache_ptr->symbol.flags = BSF_DEBUGGING;
1394 /* Work out the symbol section. */
1395 switch (cache_ptr->type & N_TYPE)
1397 case N_TEXT:
1398 case N_FN:
1399 sec = obj_textsec (abfd);
1400 break;
1401 case N_DATA:
1402 sec = obj_datasec (abfd);
1403 break;
1404 case N_BSS:
1405 sec = obj_bsssec (abfd);
1406 break;
1407 default:
1408 case N_ABS:
1409 sec = bfd_abs_section_ptr;
1410 break;
1413 cache_ptr->symbol.section = sec;
1414 cache_ptr->symbol.value -= sec->vma;
1416 return TRUE;
1419 /* Get the default visibility. This does not apply to all types, so
1420 we just hold it in a local variable to use if wanted. */
1421 if ((cache_ptr->type & N_EXT) == 0)
1422 visible = BSF_LOCAL;
1423 else
1424 visible = BSF_GLOBAL;
1426 switch (cache_ptr->type)
1428 default:
1429 case N_ABS: case N_ABS | N_EXT:
1430 cache_ptr->symbol.section = bfd_abs_section_ptr;
1431 cache_ptr->symbol.flags = visible;
1432 break;
1434 case N_UNDF | N_EXT:
1435 if (cache_ptr->symbol.value != 0)
1437 /* This is a common symbol. */
1438 cache_ptr->symbol.flags = BSF_GLOBAL;
1439 cache_ptr->symbol.section = bfd_com_section_ptr;
1441 else
1443 cache_ptr->symbol.flags = 0;
1444 cache_ptr->symbol.section = bfd_und_section_ptr;
1446 break;
1448 case N_TEXT: case N_TEXT | N_EXT:
1449 cache_ptr->symbol.section = obj_textsec (abfd);
1450 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1451 cache_ptr->symbol.flags = visible;
1452 break;
1454 /* N_SETV symbols used to represent set vectors placed in the
1455 data section. They are no longer generated. Theoretically,
1456 it was possible to extract the entries and combine them with
1457 new ones, although I don't know if that was ever actually
1458 done. Unless that feature is restored, treat them as data
1459 symbols. */
1460 case N_SETV: case N_SETV | N_EXT:
1461 case N_DATA: case N_DATA | N_EXT:
1462 cache_ptr->symbol.section = obj_datasec (abfd);
1463 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1464 cache_ptr->symbol.flags = visible;
1465 break;
1467 case N_BSS: case N_BSS | N_EXT:
1468 cache_ptr->symbol.section = obj_bsssec (abfd);
1469 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1470 cache_ptr->symbol.flags = visible;
1471 break;
1473 case N_SETA: case N_SETA | N_EXT:
1474 case N_SETT: case N_SETT | N_EXT:
1475 case N_SETD: case N_SETD | N_EXT:
1476 case N_SETB: case N_SETB | N_EXT:
1478 /* This code is no longer needed. It used to be used to make
1479 the linker handle set symbols, but they are now handled in
1480 the add_symbols routine instead. */
1481 switch (cache_ptr->type & N_TYPE)
1483 case N_SETA:
1484 cache_ptr->symbol.section = bfd_abs_section_ptr;
1485 break;
1486 case N_SETT:
1487 cache_ptr->symbol.section = obj_textsec (abfd);
1488 break;
1489 case N_SETD:
1490 cache_ptr->symbol.section = obj_datasec (abfd);
1491 break;
1492 case N_SETB:
1493 cache_ptr->symbol.section = obj_bsssec (abfd);
1494 break;
1497 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
1499 break;
1501 case N_WARNING:
1502 /* This symbol is the text of a warning message. The next
1503 symbol is the symbol to associate the warning with. If a
1504 reference is made to that symbol, a warning is issued. */
1505 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1506 cache_ptr->symbol.section = bfd_abs_section_ptr;
1507 break;
1509 case N_INDR: case N_INDR | N_EXT:
1510 /* An indirect symbol. This consists of two symbols in a row.
1511 The first symbol is the name of the indirection. The second
1512 symbol is the name of the target. A reference to the first
1513 symbol becomes a reference to the second. */
1514 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT | visible;
1515 cache_ptr->symbol.section = bfd_ind_section_ptr;
1516 break;
1518 case N_WEAKU:
1519 cache_ptr->symbol.section = bfd_und_section_ptr;
1520 cache_ptr->symbol.flags = BSF_WEAK;
1521 break;
1523 case N_WEAKA:
1524 cache_ptr->symbol.section = bfd_abs_section_ptr;
1525 cache_ptr->symbol.flags = BSF_WEAK;
1526 break;
1528 case N_WEAKT:
1529 cache_ptr->symbol.section = obj_textsec (abfd);
1530 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1531 cache_ptr->symbol.flags = BSF_WEAK;
1532 break;
1534 case N_WEAKD:
1535 cache_ptr->symbol.section = obj_datasec (abfd);
1536 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1537 cache_ptr->symbol.flags = BSF_WEAK;
1538 break;
1540 case N_WEAKB:
1541 cache_ptr->symbol.section = obj_bsssec (abfd);
1542 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1543 cache_ptr->symbol.flags = BSF_WEAK;
1544 break;
1547 return TRUE;
1550 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1552 static bfd_boolean
1553 translate_to_native_sym_flags (bfd *abfd,
1554 asymbol *cache_ptr,
1555 struct external_nlist *sym_pointer)
1557 bfd_vma value = cache_ptr->value;
1558 asection *sec;
1559 bfd_vma off;
1561 /* Mask out any existing type bits in case copying from one section
1562 to another. */
1563 sym_pointer->e_type[0] &= ~N_TYPE;
1565 sec = bfd_get_section (cache_ptr);
1566 off = 0;
1568 if (sec == NULL)
1570 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1571 file. */
1572 (*_bfd_error_handler)
1573 (_("%s: can not represent section for symbol `%s' in a.out object file format"),
1574 bfd_get_filename (abfd),
1575 cache_ptr->name != NULL ? cache_ptr->name : _("*unknown*"));
1576 bfd_set_error (bfd_error_nonrepresentable_section);
1577 return FALSE;
1580 if (sec->output_section != NULL)
1582 off = sec->output_offset;
1583 sec = sec->output_section;
1586 if (bfd_is_abs_section (sec))
1587 sym_pointer->e_type[0] |= N_ABS;
1588 else if (sec == obj_textsec (abfd))
1589 sym_pointer->e_type[0] |= N_TEXT;
1590 else if (sec == obj_datasec (abfd))
1591 sym_pointer->e_type[0] |= N_DATA;
1592 else if (sec == obj_bsssec (abfd))
1593 sym_pointer->e_type[0] |= N_BSS;
1594 else if (bfd_is_und_section (sec))
1595 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1596 else if (bfd_is_ind_section (sec))
1597 sym_pointer->e_type[0] = N_INDR;
1598 else if (bfd_is_com_section (sec))
1599 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1600 else
1602 if (aout_section_merge_with_text_p (abfd, sec))
1603 sym_pointer->e_type[0] |= N_TEXT;
1604 else
1606 (*_bfd_error_handler)
1607 (_("%s: can not represent section `%s' in a.out object file format"),
1608 bfd_get_filename (abfd), bfd_get_section_name (abfd, sec));
1609 bfd_set_error (bfd_error_nonrepresentable_section);
1610 return FALSE;
1614 /* Turn the symbol from section relative to absolute again. */
1615 value += sec->vma + off;
1617 if ((cache_ptr->flags & BSF_WARNING) != 0)
1618 sym_pointer->e_type[0] = N_WARNING;
1620 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1621 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1622 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1623 sym_pointer->e_type[0] |= N_EXT;
1624 else if ((cache_ptr->flags & BSF_LOCAL) != 0)
1625 sym_pointer->e_type[0] &= ~N_EXT;
1627 if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
1629 int type = ((aout_symbol_type *) cache_ptr)->type;
1631 switch (type)
1633 case N_ABS: type = N_SETA; break;
1634 case N_TEXT: type = N_SETT; break;
1635 case N_DATA: type = N_SETD; break;
1636 case N_BSS: type = N_SETB; break;
1638 sym_pointer->e_type[0] = type;
1641 if ((cache_ptr->flags & BSF_WEAK) != 0)
1643 int type;
1645 switch (sym_pointer->e_type[0] & N_TYPE)
1647 default:
1648 case N_ABS: type = N_WEAKA; break;
1649 case N_TEXT: type = N_WEAKT; break;
1650 case N_DATA: type = N_WEAKD; break;
1651 case N_BSS: type = N_WEAKB; break;
1652 case N_UNDF: type = N_WEAKU; break;
1654 sym_pointer->e_type[0] = type;
1657 PUT_WORD (abfd, value, sym_pointer->e_value);
1659 return TRUE;
1662 /* Native-level interface to symbols. */
1664 asymbol *
1665 NAME (aout, make_empty_symbol) (bfd *abfd)
1667 bfd_size_type amt = sizeof (aout_symbol_type);
1669 aout_symbol_type *new = bfd_zalloc (abfd, amt);
1670 if (!new)
1671 return NULL;
1672 new->symbol.the_bfd = abfd;
1674 return &new->symbol;
1677 /* Translate a set of internal symbols into external symbols. */
1679 bfd_boolean
1680 NAME (aout, translate_symbol_table) (bfd *abfd,
1681 aout_symbol_type *in,
1682 struct external_nlist *ext,
1683 bfd_size_type count,
1684 char *str,
1685 bfd_size_type strsize,
1686 bfd_boolean dynamic)
1688 struct external_nlist *ext_end;
1690 ext_end = ext + count;
1691 for (; ext < ext_end; ext++, in++)
1693 bfd_vma x;
1695 x = GET_WORD (abfd, ext->e_strx);
1696 in->symbol.the_bfd = abfd;
1698 /* For the normal symbols, the zero index points at the number
1699 of bytes in the string table but is to be interpreted as the
1700 null string. For the dynamic symbols, the number of bytes in
1701 the string table is stored in the __DYNAMIC structure and the
1702 zero index points at an actual string. */
1703 if (x == 0 && ! dynamic)
1704 in->symbol.name = "";
1705 else if (x < strsize)
1706 in->symbol.name = str + x;
1707 else
1708 return FALSE;
1710 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1711 in->desc = H_GET_16 (abfd, ext->e_desc);
1712 in->other = H_GET_8 (abfd, ext->e_other);
1713 in->type = H_GET_8 (abfd, ext->e_type);
1714 in->symbol.udata.p = NULL;
1716 if (! translate_from_native_sym_flags (abfd, in))
1717 return FALSE;
1719 if (dynamic)
1720 in->symbol.flags |= BSF_DYNAMIC;
1723 return TRUE;
1726 /* We read the symbols into a buffer, which is discarded when this
1727 function exits. We read the strings into a buffer large enough to
1728 hold them all plus all the cached symbol entries. */
1730 bfd_boolean
1731 NAME (aout, slurp_symbol_table) (bfd *abfd)
1733 struct external_nlist *old_external_syms;
1734 aout_symbol_type *cached;
1735 bfd_size_type cached_size;
1737 /* If there's no work to be done, don't do any. */
1738 if (obj_aout_symbols (abfd) != NULL)
1739 return TRUE;
1741 old_external_syms = obj_aout_external_syms (abfd);
1743 if (! aout_get_external_symbols (abfd))
1744 return FALSE;
1746 cached_size = obj_aout_external_sym_count (abfd);
1747 if (cached_size == 0)
1748 return TRUE; /* Nothing to do. */
1750 cached_size *= sizeof (aout_symbol_type);
1751 cached = bfd_zmalloc (cached_size);
1752 if (cached == NULL)
1753 return FALSE;
1755 /* Convert from external symbol information to internal. */
1756 if (! (NAME (aout, translate_symbol_table)
1757 (abfd, cached,
1758 obj_aout_external_syms (abfd),
1759 obj_aout_external_sym_count (abfd),
1760 obj_aout_external_strings (abfd),
1761 obj_aout_external_string_size (abfd),
1762 FALSE)))
1764 free (cached);
1765 return FALSE;
1768 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1770 obj_aout_symbols (abfd) = cached;
1772 /* It is very likely that anybody who calls this function will not
1773 want the external symbol information, so if it was allocated
1774 because of our call to aout_get_external_symbols, we free it up
1775 right away to save space. */
1776 if (old_external_syms == NULL
1777 && obj_aout_external_syms (abfd) != NULL)
1779 #ifdef USE_MMAP
1780 bfd_free_window (&obj_aout_sym_window (abfd));
1781 #else
1782 free (obj_aout_external_syms (abfd));
1783 #endif
1784 obj_aout_external_syms (abfd) = NULL;
1787 return TRUE;
1790 /* We use a hash table when writing out symbols so that we only write
1791 out a particular string once. This helps particularly when the
1792 linker writes out stabs debugging entries, because each different
1793 contributing object file tends to have many duplicate stabs
1794 strings.
1796 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1797 if BFD_TRADITIONAL_FORMAT is set. */
1799 /* Get the index of a string in a strtab, adding it if it is not
1800 already present. */
1802 static inline bfd_size_type
1803 add_to_stringtab (bfd *abfd,
1804 struct bfd_strtab_hash *tab,
1805 const char *str,
1806 bfd_boolean copy)
1808 bfd_boolean hash;
1809 bfd_size_type index;
1811 /* An index of 0 always means the empty string. */
1812 if (str == 0 || *str == '\0')
1813 return 0;
1815 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1816 doesn't understand a hashed string table. */
1817 hash = TRUE;
1818 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1819 hash = FALSE;
1821 index = _bfd_stringtab_add (tab, str, hash, copy);
1823 if (index != (bfd_size_type) -1)
1824 /* Add BYTES_IN_WORD to the return value to account for the
1825 space taken up by the string table size. */
1826 index += BYTES_IN_WORD;
1828 return index;
1831 /* Write out a strtab. ABFD is already at the right location in the
1832 file. */
1834 static bfd_boolean
1835 emit_stringtab (bfd *abfd, struct bfd_strtab_hash *tab)
1837 bfd_byte buffer[BYTES_IN_WORD];
1838 bfd_size_type amt = BYTES_IN_WORD;
1840 /* The string table starts with the size. */
1841 PUT_WORD (abfd, _bfd_stringtab_size (tab) + BYTES_IN_WORD, buffer);
1842 if (bfd_bwrite ((void *) buffer, amt, abfd) != amt)
1843 return FALSE;
1845 return _bfd_stringtab_emit (abfd, tab);
1848 bfd_boolean
1849 NAME (aout, write_syms) (bfd *abfd)
1851 unsigned int count ;
1852 asymbol **generic = bfd_get_outsymbols (abfd);
1853 struct bfd_strtab_hash *strtab;
1855 strtab = _bfd_stringtab_init ();
1856 if (strtab == NULL)
1857 return FALSE;
1859 for (count = 0; count < bfd_get_symcount (abfd); count++)
1861 asymbol *g = generic[count];
1862 bfd_size_type indx;
1863 struct external_nlist nsp;
1864 bfd_size_type amt;
1866 indx = add_to_stringtab (abfd, strtab, g->name, FALSE);
1867 if (indx == (bfd_size_type) -1)
1868 goto error_return;
1869 PUT_WORD (abfd, indx, (bfd_byte *) nsp.e_strx);
1871 if (bfd_asymbol_flavour (g) == abfd->xvec->flavour)
1873 H_PUT_16 (abfd, aout_symbol (g)->desc, nsp.e_desc);
1874 H_PUT_8 (abfd, aout_symbol (g)->other, nsp.e_other);
1875 H_PUT_8 (abfd, aout_symbol (g)->type, nsp.e_type);
1877 else
1879 H_PUT_16 (abfd, 0, nsp.e_desc);
1880 H_PUT_8 (abfd, 0, nsp.e_other);
1881 H_PUT_8 (abfd, 0, nsp.e_type);
1884 if (! translate_to_native_sym_flags (abfd, g, &nsp))
1885 goto error_return;
1887 amt = EXTERNAL_NLIST_SIZE;
1888 if (bfd_bwrite ((void *) &nsp, amt, abfd) != amt)
1889 goto error_return;
1891 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1892 here, at the end. */
1893 g->KEEPIT = count;
1896 if (! emit_stringtab (abfd, strtab))
1897 goto error_return;
1899 _bfd_stringtab_free (strtab);
1901 return TRUE;
1903 error_return:
1904 _bfd_stringtab_free (strtab);
1905 return FALSE;
1908 long
1909 NAME (aout, canonicalize_symtab) (bfd *abfd, asymbol **location)
1911 unsigned int counter = 0;
1912 aout_symbol_type *symbase;
1914 if (!NAME (aout, slurp_symbol_table) (abfd))
1915 return -1;
1917 for (symbase = obj_aout_symbols (abfd);
1918 counter++ < bfd_get_symcount (abfd);
1920 *(location++) = (asymbol *) (symbase++);
1921 *location++ =0;
1922 return bfd_get_symcount (abfd);
1925 /* Standard reloc stuff. */
1926 /* Output standard relocation information to a file in target byte order. */
1928 extern void NAME (aout, swap_std_reloc_out)
1929 (bfd *, arelent *, struct reloc_std_external *);
1931 void
1932 NAME (aout, swap_std_reloc_out) (bfd *abfd,
1933 arelent *g,
1934 struct reloc_std_external *natptr)
1936 int r_index;
1937 asymbol *sym = *(g->sym_ptr_ptr);
1938 int r_extern;
1939 unsigned int r_length;
1940 int r_pcrel;
1941 int r_baserel, r_jmptable, r_relative;
1942 asection *output_section = sym->section->output_section;
1944 PUT_WORD (abfd, g->address, natptr->r_address);
1946 r_length = g->howto->size ; /* Size as a power of two. */
1947 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
1948 /* XXX This relies on relocs coming from a.out files. */
1949 r_baserel = (g->howto->type & 8) != 0;
1950 r_jmptable = (g->howto->type & 16) != 0;
1951 r_relative = (g->howto->type & 32) != 0;
1953 /* Name was clobbered by aout_write_syms to be symbol index. */
1955 /* If this relocation is relative to a symbol then set the
1956 r_index to the symbols index, and the r_extern bit.
1958 Absolute symbols can come in in two ways, either as an offset
1959 from the abs section, or as a symbol which has an abs value.
1960 check for that here. */
1962 if (bfd_is_com_section (output_section)
1963 || bfd_is_abs_section (output_section)
1964 || bfd_is_und_section (output_section)
1965 /* PR gas/3041 a.out relocs against weak symbols
1966 must be treated as if they were against externs. */
1967 || (sym->flags & BSF_WEAK))
1969 if (bfd_abs_section_ptr->symbol == sym)
1971 /* Whoops, looked like an abs symbol, but is
1972 really an offset from the abs section. */
1973 r_index = N_ABS;
1974 r_extern = 0;
1976 else
1978 /* Fill in symbol. */
1979 r_extern = 1;
1980 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
1983 else
1985 /* Just an ordinary section. */
1986 r_extern = 0;
1987 r_index = output_section->target_index;
1990 /* Now the fun stuff. */
1991 if (bfd_header_big_endian (abfd))
1993 natptr->r_index[0] = r_index >> 16;
1994 natptr->r_index[1] = r_index >> 8;
1995 natptr->r_index[2] = r_index;
1996 natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
1997 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
1998 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
1999 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
2000 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
2001 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
2003 else
2005 natptr->r_index[2] = r_index >> 16;
2006 natptr->r_index[1] = r_index >> 8;
2007 natptr->r_index[0] = r_index;
2008 natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
2009 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
2010 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
2011 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
2012 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
2013 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2017 /* Extended stuff. */
2018 /* Output extended relocation information to a file in target byte order. */
2020 extern void NAME (aout, swap_ext_reloc_out)
2021 (bfd *, arelent *, struct reloc_ext_external *);
2023 void
2024 NAME (aout, swap_ext_reloc_out) (bfd *abfd,
2025 arelent *g,
2026 struct reloc_ext_external *natptr)
2028 int r_index;
2029 int r_extern;
2030 unsigned int r_type;
2031 bfd_vma r_addend;
2032 asymbol *sym = *(g->sym_ptr_ptr);
2033 asection *output_section = sym->section->output_section;
2035 PUT_WORD (abfd, g->address, natptr->r_address);
2037 r_type = (unsigned int) g->howto->type;
2039 r_addend = g->addend;
2040 if ((sym->flags & BSF_SECTION_SYM) != 0)
2041 r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
2043 /* If this relocation is relative to a symbol then set the
2044 r_index to the symbols index, and the r_extern bit.
2046 Absolute symbols can come in in two ways, either as an offset
2047 from the abs section, or as a symbol which has an abs value.
2048 check for that here. */
2049 if (bfd_is_abs_section (bfd_get_section (sym)))
2051 r_extern = 0;
2052 r_index = N_ABS;
2054 else if ((sym->flags & BSF_SECTION_SYM) == 0)
2056 if (bfd_is_und_section (bfd_get_section (sym))
2057 || (sym->flags & BSF_GLOBAL) != 0)
2058 r_extern = 1;
2059 else
2060 r_extern = 0;
2061 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2063 else
2065 /* Just an ordinary section. */
2066 r_extern = 0;
2067 r_index = output_section->target_index;
2070 /* Now the fun stuff. */
2071 if (bfd_header_big_endian (abfd))
2073 natptr->r_index[0] = r_index >> 16;
2074 natptr->r_index[1] = r_index >> 8;
2075 natptr->r_index[2] = r_index;
2076 natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
2077 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
2079 else
2081 natptr->r_index[2] = r_index >> 16;
2082 natptr->r_index[1] = r_index >> 8;
2083 natptr->r_index[0] = r_index;
2084 natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
2085 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2088 PUT_WORD (abfd, r_addend, natptr->r_addend);
2091 /* BFD deals internally with all things based from the section they're
2092 in. so, something in 10 bytes into a text section with a base of
2093 50 would have a symbol (.text+10) and know .text vma was 50.
2095 Aout keeps all it's symbols based from zero, so the symbol would
2096 contain 60. This macro subs the base of each section from the value
2097 to give the true offset from the section. */
2099 #define MOVE_ADDRESS(ad) \
2100 if (r_extern) \
2102 /* Undefined symbol. */ \
2103 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2104 cache_ptr->addend = ad; \
2106 else \
2108 /* Defined, section relative. Replace symbol with pointer to \
2109 symbol which points to section. */ \
2110 switch (r_index) \
2112 case N_TEXT: \
2113 case N_TEXT | N_EXT: \
2114 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
2115 cache_ptr->addend = ad - su->textsec->vma; \
2116 break; \
2117 case N_DATA: \
2118 case N_DATA | N_EXT: \
2119 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
2120 cache_ptr->addend = ad - su->datasec->vma; \
2121 break; \
2122 case N_BSS: \
2123 case N_BSS | N_EXT: \
2124 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
2125 cache_ptr->addend = ad - su->bsssec->vma; \
2126 break; \
2127 default: \
2128 case N_ABS: \
2129 case N_ABS | N_EXT: \
2130 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2131 cache_ptr->addend = ad; \
2132 break; \
2136 void
2137 NAME (aout, swap_ext_reloc_in) (bfd *abfd,
2138 struct reloc_ext_external *bytes,
2139 arelent *cache_ptr,
2140 asymbol **symbols,
2141 bfd_size_type symcount)
2143 unsigned int r_index;
2144 int r_extern;
2145 unsigned int r_type;
2146 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2148 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2150 /* Now the fun stuff. */
2151 if (bfd_header_big_endian (abfd))
2153 r_index = (((unsigned int) bytes->r_index[0] << 16)
2154 | ((unsigned int) bytes->r_index[1] << 8)
2155 | bytes->r_index[2]);
2156 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2157 r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2158 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2160 else
2162 r_index = (((unsigned int) bytes->r_index[2] << 16)
2163 | ((unsigned int) bytes->r_index[1] << 8)
2164 | bytes->r_index[0]);
2165 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2166 r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2167 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2170 if (r_type < TABLE_SIZE (howto_table_ext))
2171 cache_ptr->howto = howto_table_ext + r_type;
2172 else
2173 cache_ptr->howto = NULL;
2175 /* Base relative relocs are always against the symbol table,
2176 regardless of the setting of r_extern. r_extern just reflects
2177 whether the symbol the reloc is against is local or global. */
2178 if (r_type == (unsigned int) RELOC_BASE10
2179 || r_type == (unsigned int) RELOC_BASE13
2180 || r_type == (unsigned int) RELOC_BASE22)
2181 r_extern = 1;
2183 if (r_extern && r_index > symcount)
2185 /* We could arrange to return an error, but it might be useful
2186 to see the file even if it is bad. */
2187 r_extern = 0;
2188 r_index = N_ABS;
2191 MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
2194 void
2195 NAME (aout, swap_std_reloc_in) (bfd *abfd,
2196 struct reloc_std_external *bytes,
2197 arelent *cache_ptr,
2198 asymbol **symbols,
2199 bfd_size_type symcount)
2201 unsigned int r_index;
2202 int r_extern;
2203 unsigned int r_length;
2204 int r_pcrel;
2205 int r_baserel, r_jmptable, r_relative;
2206 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2207 unsigned int howto_idx;
2209 cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
2211 /* Now the fun stuff. */
2212 if (bfd_header_big_endian (abfd))
2214 r_index = (((unsigned int) bytes->r_index[0] << 16)
2215 | ((unsigned int) bytes->r_index[1] << 8)
2216 | bytes->r_index[2]);
2217 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2218 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2219 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2220 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2221 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2222 r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2223 >> RELOC_STD_BITS_LENGTH_SH_BIG);
2225 else
2227 r_index = (((unsigned int) bytes->r_index[2] << 16)
2228 | ((unsigned int) bytes->r_index[1] << 8)
2229 | bytes->r_index[0]);
2230 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2231 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2232 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2233 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2234 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2235 r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2236 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
2239 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
2240 + 16 * r_jmptable + 32 * r_relative);
2241 if (howto_idx < TABLE_SIZE (howto_table_std))
2243 cache_ptr->howto = howto_table_std + howto_idx;
2244 if (cache_ptr->howto->type == (unsigned int) -1)
2245 cache_ptr->howto = NULL;
2247 else
2248 cache_ptr->howto = NULL;
2250 /* Base relative relocs are always against the symbol table,
2251 regardless of the setting of r_extern. r_extern just reflects
2252 whether the symbol the reloc is against is local or global. */
2253 if (r_baserel)
2254 r_extern = 1;
2256 if (r_extern && r_index > symcount)
2258 /* We could arrange to return an error, but it might be useful
2259 to see the file even if it is bad. */
2260 r_extern = 0;
2261 r_index = N_ABS;
2264 MOVE_ADDRESS (0);
2267 /* Read and swap the relocs for a section. */
2269 bfd_boolean
2270 NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
2272 bfd_size_type count;
2273 bfd_size_type reloc_size;
2274 void * relocs;
2275 arelent *reloc_cache;
2276 size_t each_size;
2277 unsigned int counter = 0;
2278 arelent *cache_ptr;
2279 bfd_size_type amt;
2281 if (asect->relocation)
2282 return TRUE;
2284 if (asect->flags & SEC_CONSTRUCTOR)
2285 return TRUE;
2287 if (asect == obj_datasec (abfd))
2288 reloc_size = exec_hdr (abfd)->a_drsize;
2289 else if (asect == obj_textsec (abfd))
2290 reloc_size = exec_hdr (abfd)->a_trsize;
2291 else if (asect == obj_bsssec (abfd))
2292 reloc_size = 0;
2293 else
2295 bfd_set_error (bfd_error_invalid_operation);
2296 return FALSE;
2299 if (reloc_size == 0)
2300 return TRUE; /* Nothing to be done. */
2302 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2303 return FALSE;
2305 each_size = obj_reloc_entry_size (abfd);
2307 count = reloc_size / each_size;
2308 if (count == 0)
2309 return TRUE; /* Nothing to be done. */
2311 amt = count * sizeof (arelent);
2312 reloc_cache = bfd_zmalloc (amt);
2313 if (reloc_cache == NULL)
2314 return FALSE;
2316 relocs = bfd_malloc (reloc_size);
2317 if (relocs == NULL)
2319 free (reloc_cache);
2320 return FALSE;
2323 if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
2325 free (relocs);
2326 free (reloc_cache);
2327 return FALSE;
2330 cache_ptr = reloc_cache;
2331 if (each_size == RELOC_EXT_SIZE)
2333 struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;
2335 for (; counter < count; counter++, rptr++, cache_ptr++)
2336 MY_swap_ext_reloc_in (abfd, rptr, cache_ptr, symbols,
2337 (bfd_size_type) bfd_get_symcount (abfd));
2339 else
2341 struct reloc_std_external *rptr = (struct reloc_std_external *) relocs;
2343 for (; counter < count; counter++, rptr++, cache_ptr++)
2344 MY_swap_std_reloc_in (abfd, rptr, cache_ptr, symbols,
2345 (bfd_size_type) bfd_get_symcount (abfd));
2348 free (relocs);
2350 asect->relocation = reloc_cache;
2351 asect->reloc_count = cache_ptr - reloc_cache;
2353 return TRUE;
2356 /* Write out a relocation section into an object file. */
2358 bfd_boolean
2359 NAME (aout, squirt_out_relocs) (bfd *abfd, asection *section)
2361 arelent **generic;
2362 unsigned char *native, *natptr;
2363 size_t each_size;
2365 unsigned int count = section->reloc_count;
2366 bfd_size_type natsize;
2368 if (count == 0 || section->orelocation == NULL)
2369 return TRUE;
2371 each_size = obj_reloc_entry_size (abfd);
2372 natsize = (bfd_size_type) each_size * count;
2373 native = bfd_zalloc (abfd, natsize);
2374 if (!native)
2375 return FALSE;
2377 generic = section->orelocation;
2379 if (each_size == RELOC_EXT_SIZE)
2381 for (natptr = native;
2382 count != 0;
2383 --count, natptr += each_size, ++generic)
2384 MY_swap_ext_reloc_out (abfd, *generic,
2385 (struct reloc_ext_external *) natptr);
2387 else
2389 for (natptr = native;
2390 count != 0;
2391 --count, natptr += each_size, ++generic)
2392 MY_swap_std_reloc_out (abfd, *generic,
2393 (struct reloc_std_external *) natptr);
2396 if (bfd_bwrite ((void *) native, natsize, abfd) != natsize)
2398 bfd_release (abfd, native);
2399 return FALSE;
2401 bfd_release (abfd, native);
2403 return TRUE;
2406 /* This is stupid. This function should be a boolean predicate. */
2408 long
2409 NAME (aout, canonicalize_reloc) (bfd *abfd,
2410 sec_ptr section,
2411 arelent **relptr,
2412 asymbol **symbols)
2414 arelent *tblptr = section->relocation;
2415 unsigned int count;
2417 if (section == obj_bsssec (abfd))
2419 *relptr = NULL;
2420 return 0;
2423 if (!(tblptr || NAME (aout, slurp_reloc_table) (abfd, section, symbols)))
2424 return -1;
2426 if (section->flags & SEC_CONSTRUCTOR)
2428 arelent_chain *chain = section->constructor_chain;
2429 for (count = 0; count < section->reloc_count; count ++)
2431 *relptr ++ = &chain->relent;
2432 chain = chain->next;
2435 else
2437 tblptr = section->relocation;
2439 for (count = 0; count++ < section->reloc_count; )
2441 *relptr++ = tblptr++;
2444 *relptr = 0;
2446 return section->reloc_count;
2449 long
2450 NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
2452 if (bfd_get_format (abfd) != bfd_object)
2454 bfd_set_error (bfd_error_invalid_operation);
2455 return -1;
2458 if (asect->flags & SEC_CONSTRUCTOR)
2459 return sizeof (arelent *) * (asect->reloc_count + 1);
2461 if (asect == obj_datasec (abfd))
2462 return sizeof (arelent *)
2463 * ((exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd))
2464 + 1);
2466 if (asect == obj_textsec (abfd))
2467 return sizeof (arelent *)
2468 * ((exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd))
2469 + 1);
2471 if (asect == obj_bsssec (abfd))
2472 return sizeof (arelent *);
2474 if (asect == obj_bsssec (abfd))
2475 return 0;
2477 bfd_set_error (bfd_error_invalid_operation);
2478 return -1;
2481 long
2482 NAME (aout, get_symtab_upper_bound) (bfd *abfd)
2484 if (!NAME (aout, slurp_symbol_table) (abfd))
2485 return -1;
2487 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2490 alent *
2491 NAME (aout, get_lineno) (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2492 asymbol *ignore_symbol ATTRIBUTE_UNUSED)
2494 return NULL;
2497 void
2498 NAME (aout, get_symbol_info) (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2499 asymbol *symbol,
2500 symbol_info *ret)
2502 bfd_symbol_info (symbol, ret);
2504 if (ret->type == '?')
2506 int type_code = aout_symbol (symbol)->type & 0xff;
2507 const char *stab_name = bfd_get_stab_name (type_code);
2508 static char buf[10];
2510 if (stab_name == NULL)
2512 sprintf (buf, "(%d)", type_code);
2513 stab_name = buf;
2515 ret->type = '-';
2516 ret->stab_type = type_code;
2517 ret->stab_other = (unsigned) (aout_symbol (symbol)->other & 0xff);
2518 ret->stab_desc = (unsigned) (aout_symbol (symbol)->desc & 0xffff);
2519 ret->stab_name = stab_name;
2523 void
2524 NAME (aout, print_symbol) (bfd *abfd,
2525 void * afile,
2526 asymbol *symbol,
2527 bfd_print_symbol_type how)
2529 FILE *file = (FILE *)afile;
2531 switch (how)
2533 case bfd_print_symbol_name:
2534 if (symbol->name)
2535 fprintf (file,"%s", symbol->name);
2536 break;
2537 case bfd_print_symbol_more:
2538 fprintf (file,"%4x %2x %2x",
2539 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2540 (unsigned) (aout_symbol (symbol)->other & 0xff),
2541 (unsigned) (aout_symbol (symbol)->type));
2542 break;
2543 case bfd_print_symbol_all:
2545 const char *section_name = symbol->section->name;
2547 bfd_print_symbol_vandf (abfd, (void *)file, symbol);
2549 fprintf (file," %-5s %04x %02x %02x",
2550 section_name,
2551 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2552 (unsigned) (aout_symbol (symbol)->other & 0xff),
2553 (unsigned) (aout_symbol (symbol)->type & 0xff));
2554 if (symbol->name)
2555 fprintf (file," %s", symbol->name);
2557 break;
2561 /* If we don't have to allocate more than 1MB to hold the generic
2562 symbols, we use the generic minisymbol methord: it's faster, since
2563 it only translates the symbols once, not multiple times. */
2564 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2566 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2567 symbols. The minisymbol_to_symbol function translates these into
2568 BFD asymbol structures. */
2570 long
2571 NAME (aout, read_minisymbols) (bfd *abfd,
2572 bfd_boolean dynamic,
2573 void * *minisymsp,
2574 unsigned int *sizep)
2576 if (dynamic)
2577 /* We could handle the dynamic symbols here as well, but it's
2578 easier to hand them off. */
2579 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2581 if (! aout_get_external_symbols (abfd))
2582 return -1;
2584 if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2585 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2587 *minisymsp = (void *) obj_aout_external_syms (abfd);
2589 /* By passing the external symbols back from this routine, we are
2590 giving up control over the memory block. Clear
2591 obj_aout_external_syms, so that we do not try to free it
2592 ourselves. */
2593 obj_aout_external_syms (abfd) = NULL;
2595 *sizep = EXTERNAL_NLIST_SIZE;
2596 return obj_aout_external_sym_count (abfd);
2599 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2600 unmodified a.out symbol. The SYM argument is a structure returned
2601 by bfd_make_empty_symbol, which we fill in here. */
2603 asymbol *
2604 NAME (aout, minisymbol_to_symbol) (bfd *abfd,
2605 bfd_boolean dynamic,
2606 const void * minisym,
2607 asymbol *sym)
2609 if (dynamic
2610 || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2611 return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2613 memset (sym, 0, sizeof (aout_symbol_type));
2615 /* We call translate_symbol_table to translate a single symbol. */
2616 if (! (NAME (aout, translate_symbol_table)
2617 (abfd,
2618 (aout_symbol_type *) sym,
2619 (struct external_nlist *) minisym,
2620 (bfd_size_type) 1,
2621 obj_aout_external_strings (abfd),
2622 obj_aout_external_string_size (abfd),
2623 FALSE)))
2624 return NULL;
2626 return sym;
2629 /* Provided a BFD, a section and an offset into the section, calculate
2630 and return the name of the source file and the line nearest to the
2631 wanted location. */
2633 bfd_boolean
2634 NAME (aout, find_nearest_line) (bfd *abfd,
2635 asection *section,
2636 asymbol **symbols,
2637 bfd_vma offset,
2638 const char **filename_ptr,
2639 const char **functionname_ptr,
2640 unsigned int *line_ptr)
2642 /* Run down the file looking for the filename, function and linenumber. */
2643 asymbol **p;
2644 const char *directory_name = NULL;
2645 const char *main_file_name = NULL;
2646 const char *current_file_name = NULL;
2647 const char *line_file_name = NULL; /* Value of current_file_name at line number. */
2648 const char *line_directory_name = NULL; /* Value of directory_name at line number. */
2649 bfd_vma low_line_vma = 0;
2650 bfd_vma low_func_vma = 0;
2651 asymbol *func = 0;
2652 bfd_size_type filelen, funclen;
2653 char *buf;
2655 *filename_ptr = abfd->filename;
2656 *functionname_ptr = 0;
2657 *line_ptr = 0;
2659 if (symbols != NULL)
2661 for (p = symbols; *p; p++)
2663 aout_symbol_type *q = (aout_symbol_type *) (*p);
2664 next:
2665 switch (q->type)
2667 case N_TEXT:
2668 /* If this looks like a file name symbol, and it comes after
2669 the line number we have found so far, but before the
2670 offset, then we have probably not found the right line
2671 number. */
2672 if (q->symbol.value <= offset
2673 && ((q->symbol.value > low_line_vma
2674 && (line_file_name != NULL
2675 || *line_ptr != 0))
2676 || (q->symbol.value > low_func_vma
2677 && func != NULL)))
2679 const char *symname;
2681 symname = q->symbol.name;
2682 if (strcmp (symname + strlen (symname) - 2, ".o") == 0)
2684 if (q->symbol.value > low_line_vma)
2686 *line_ptr = 0;
2687 line_file_name = NULL;
2689 if (q->symbol.value > low_func_vma)
2690 func = NULL;
2693 break;
2695 case N_SO:
2696 /* If this symbol is less than the offset, but greater than
2697 the line number we have found so far, then we have not
2698 found the right line number. */
2699 if (q->symbol.value <= offset)
2701 if (q->symbol.value > low_line_vma)
2703 *line_ptr = 0;
2704 line_file_name = NULL;
2706 if (q->symbol.value > low_func_vma)
2707 func = NULL;
2710 main_file_name = current_file_name = q->symbol.name;
2711 /* Look ahead to next symbol to check if that too is an N_SO. */
2712 p++;
2713 if (*p == NULL)
2714 goto done;
2715 q = (aout_symbol_type *) (*p);
2716 if (q->type != (int)N_SO)
2717 goto next;
2719 /* Found a second N_SO First is directory; second is filename. */
2720 directory_name = current_file_name;
2721 main_file_name = current_file_name = q->symbol.name;
2722 if (obj_textsec (abfd) != section)
2723 goto done;
2724 break;
2725 case N_SOL:
2726 current_file_name = q->symbol.name;
2727 break;
2729 case N_SLINE:
2731 case N_DSLINE:
2732 case N_BSLINE:
2733 /* We'll keep this if it resolves nearer than the one we have
2734 already. */
2735 if (q->symbol.value >= low_line_vma
2736 && q->symbol.value <= offset)
2738 *line_ptr = q->desc;
2739 low_line_vma = q->symbol.value;
2740 line_file_name = current_file_name;
2741 line_directory_name = directory_name;
2743 break;
2744 case N_FUN:
2746 /* We'll keep this if it is nearer than the one we have already. */
2747 if (q->symbol.value >= low_func_vma &&
2748 q->symbol.value <= offset)
2750 low_func_vma = q->symbol.value;
2751 func = (asymbol *)q;
2753 else if (q->symbol.value > offset)
2754 goto done;
2756 break;
2761 done:
2762 if (*line_ptr != 0)
2764 main_file_name = line_file_name;
2765 directory_name = line_directory_name;
2768 if (main_file_name == NULL
2769 || IS_ABSOLUTE_PATH (main_file_name)
2770 || directory_name == NULL)
2771 filelen = 0;
2772 else
2773 filelen = strlen (directory_name) + strlen (main_file_name);
2775 if (func == NULL)
2776 funclen = 0;
2777 else
2778 funclen = strlen (bfd_asymbol_name (func));
2780 if (adata (abfd).line_buf != NULL)
2781 free (adata (abfd).line_buf);
2783 if (filelen + funclen == 0)
2784 adata (abfd).line_buf = buf = NULL;
2785 else
2787 buf = bfd_malloc (filelen + funclen + 3);
2788 adata (abfd).line_buf = buf;
2789 if (buf == NULL)
2790 return FALSE;
2793 if (main_file_name != NULL)
2795 if (IS_ABSOLUTE_PATH (main_file_name) || directory_name == NULL)
2796 *filename_ptr = main_file_name;
2797 else
2799 sprintf (buf, "%s%s", directory_name, main_file_name);
2800 *filename_ptr = buf;
2801 buf += filelen + 1;
2805 if (func)
2807 const char *function = func->name;
2808 char *colon;
2810 /* The caller expects a symbol name. We actually have a
2811 function name, without the leading underscore. Put the
2812 underscore back in, so that the caller gets a symbol name. */
2813 if (bfd_get_symbol_leading_char (abfd) == '\0')
2814 strcpy (buf, function);
2815 else
2817 buf[0] = bfd_get_symbol_leading_char (abfd);
2818 strcpy (buf + 1, function);
2820 /* Have to remove : stuff. */
2821 colon = strchr (buf, ':');
2822 if (colon != NULL)
2823 *colon = '\0';
2824 *functionname_ptr = buf;
2827 return TRUE;
2831 NAME (aout, sizeof_headers) (bfd *abfd,
2832 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2834 return adata (abfd).exec_bytes_size;
2837 /* Free all information we have cached for this BFD. We can always
2838 read it again later if we need it. */
2840 bfd_boolean
2841 NAME (aout, bfd_free_cached_info) (bfd *abfd)
2843 asection *o;
2845 if (bfd_get_format (abfd) != bfd_object
2846 || abfd->tdata.aout_data == NULL)
2847 return TRUE;
2849 #define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
2850 BFCI_FREE (obj_aout_symbols (abfd));
2851 #ifdef USE_MMAP
2852 obj_aout_external_syms (abfd) = 0;
2853 bfd_free_window (&obj_aout_sym_window (abfd));
2854 bfd_free_window (&obj_aout_string_window (abfd));
2855 obj_aout_external_strings (abfd) = 0;
2856 #else
2857 BFCI_FREE (obj_aout_external_syms (abfd));
2858 BFCI_FREE (obj_aout_external_strings (abfd));
2859 #endif
2860 for (o = abfd->sections; o != NULL; o = o->next)
2861 BFCI_FREE (o->relocation);
2862 #undef BFCI_FREE
2864 return TRUE;
2867 /* a.out link code. */
2869 /* Routine to create an entry in an a.out link hash table. */
2871 struct bfd_hash_entry *
2872 NAME (aout, link_hash_newfunc) (struct bfd_hash_entry *entry,
2873 struct bfd_hash_table *table,
2874 const char *string)
2876 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2878 /* Allocate the structure if it has not already been allocated by a
2879 subclass. */
2880 if (ret == NULL)
2881 ret = bfd_hash_allocate (table, sizeof (* ret));
2882 if (ret == NULL)
2883 return NULL;
2885 /* Call the allocation method of the superclass. */
2886 ret = ((struct aout_link_hash_entry *)
2887 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2888 table, string));
2889 if (ret)
2891 /* Set local fields. */
2892 ret->written = FALSE;
2893 ret->indx = -1;
2896 return (struct bfd_hash_entry *) ret;
2899 /* Initialize an a.out link hash table. */
2901 bfd_boolean
2902 NAME (aout, link_hash_table_init) (struct aout_link_hash_table *table,
2903 bfd *abfd,
2904 struct bfd_hash_entry *(*newfunc)
2905 (struct bfd_hash_entry *, struct bfd_hash_table *,
2906 const char *),
2907 unsigned int entsize)
2909 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
2912 /* Create an a.out link hash table. */
2914 struct bfd_link_hash_table *
2915 NAME (aout, link_hash_table_create) (bfd *abfd)
2917 struct aout_link_hash_table *ret;
2918 bfd_size_type amt = sizeof (* ret);
2920 ret = bfd_malloc (amt);
2921 if (ret == NULL)
2922 return NULL;
2924 if (!NAME (aout, link_hash_table_init) (ret, abfd,
2925 NAME (aout, link_hash_newfunc),
2926 sizeof (struct aout_link_hash_entry)))
2928 free (ret);
2929 return NULL;
2931 return &ret->root;
2934 /* Add all symbols from an object file to the hash table. */
2936 static bfd_boolean
2937 aout_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2939 bfd_boolean (*add_one_symbol)
2940 (struct bfd_link_info *, bfd *, const char *, flagword, asection *,
2941 bfd_vma, const char *, bfd_boolean, bfd_boolean,
2942 struct bfd_link_hash_entry **);
2943 struct external_nlist *syms;
2944 bfd_size_type sym_count;
2945 char *strings;
2946 bfd_boolean copy;
2947 struct aout_link_hash_entry **sym_hash;
2948 struct external_nlist *p;
2949 struct external_nlist *pend;
2950 bfd_size_type amt;
2952 syms = obj_aout_external_syms (abfd);
2953 sym_count = obj_aout_external_sym_count (abfd);
2954 strings = obj_aout_external_strings (abfd);
2955 if (info->keep_memory)
2956 copy = FALSE;
2957 else
2958 copy = TRUE;
2960 if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
2962 if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
2963 (abfd, info, &syms, &sym_count, &strings)))
2964 return FALSE;
2967 if (sym_count == 0)
2968 return TRUE; /* Nothing to do. */
2970 /* We keep a list of the linker hash table entries that correspond
2971 to particular symbols. We could just look them up in the hash
2972 table, but keeping the list is more efficient. Perhaps this
2973 should be conditional on info->keep_memory. */
2974 amt = sym_count * sizeof (struct aout_link_hash_entry *);
2975 sym_hash = bfd_alloc (abfd, amt);
2976 if (sym_hash == NULL)
2977 return FALSE;
2978 obj_aout_sym_hashes (abfd) = sym_hash;
2980 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
2981 if (add_one_symbol == NULL)
2982 add_one_symbol = _bfd_generic_link_add_one_symbol;
2984 p = syms;
2985 pend = p + sym_count;
2986 for (; p < pend; p++, sym_hash++)
2988 int type;
2989 const char *name;
2990 bfd_vma value;
2991 asection *section;
2992 flagword flags;
2993 const char *string;
2995 *sym_hash = NULL;
2997 type = H_GET_8 (abfd, p->e_type);
2999 /* Ignore debugging symbols. */
3000 if ((type & N_STAB) != 0)
3001 continue;
3003 name = strings + GET_WORD (abfd, p->e_strx);
3004 value = GET_WORD (abfd, p->e_value);
3005 flags = BSF_GLOBAL;
3006 string = NULL;
3007 switch (type)
3009 default:
3010 abort ();
3012 case N_UNDF:
3013 case N_ABS:
3014 case N_TEXT:
3015 case N_DATA:
3016 case N_BSS:
3017 case N_FN_SEQ:
3018 case N_COMM:
3019 case N_SETV:
3020 case N_FN:
3021 /* Ignore symbols that are not externally visible. */
3022 continue;
3023 case N_INDR:
3024 /* Ignore local indirect symbol. */
3025 ++p;
3026 ++sym_hash;
3027 continue;
3029 case N_UNDF | N_EXT:
3030 if (value == 0)
3032 section = bfd_und_section_ptr;
3033 flags = 0;
3035 else
3036 section = bfd_com_section_ptr;
3037 break;
3038 case N_ABS | N_EXT:
3039 section = bfd_abs_section_ptr;
3040 break;
3041 case N_TEXT | N_EXT:
3042 section = obj_textsec (abfd);
3043 value -= bfd_get_section_vma (abfd, section);
3044 break;
3045 case N_DATA | N_EXT:
3046 case N_SETV | N_EXT:
3047 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3048 translate_from_native_sym_flags. */
3049 section = obj_datasec (abfd);
3050 value -= bfd_get_section_vma (abfd, section);
3051 break;
3052 case N_BSS | N_EXT:
3053 section = obj_bsssec (abfd);
3054 value -= bfd_get_section_vma (abfd, section);
3055 break;
3056 case N_INDR | N_EXT:
3057 /* An indirect symbol. The next symbol is the symbol
3058 which this one really is. */
3059 BFD_ASSERT (p + 1 < pend);
3060 ++p;
3061 string = strings + GET_WORD (abfd, p->e_strx);
3062 section = bfd_ind_section_ptr;
3063 flags |= BSF_INDIRECT;
3064 break;
3065 case N_COMM | N_EXT:
3066 section = bfd_com_section_ptr;
3067 break;
3068 case N_SETA: case N_SETA | N_EXT:
3069 section = bfd_abs_section_ptr;
3070 flags |= BSF_CONSTRUCTOR;
3071 break;
3072 case N_SETT: case N_SETT | N_EXT:
3073 section = obj_textsec (abfd);
3074 flags |= BSF_CONSTRUCTOR;
3075 value -= bfd_get_section_vma (abfd, section);
3076 break;
3077 case N_SETD: case N_SETD | N_EXT:
3078 section = obj_datasec (abfd);
3079 flags |= BSF_CONSTRUCTOR;
3080 value -= bfd_get_section_vma (abfd, section);
3081 break;
3082 case N_SETB: case N_SETB | N_EXT:
3083 section = obj_bsssec (abfd);
3084 flags |= BSF_CONSTRUCTOR;
3085 value -= bfd_get_section_vma (abfd, section);
3086 break;
3087 case N_WARNING:
3088 /* A warning symbol. The next symbol is the one to warn
3089 about. If there is no next symbol, just look away. */
3090 if (p + 1 >= pend)
3091 return TRUE;
3092 ++p;
3093 string = name;
3094 name = strings + GET_WORD (abfd, p->e_strx);
3095 section = bfd_und_section_ptr;
3096 flags |= BSF_WARNING;
3097 break;
3098 case N_WEAKU:
3099 section = bfd_und_section_ptr;
3100 flags = BSF_WEAK;
3101 break;
3102 case N_WEAKA:
3103 section = bfd_abs_section_ptr;
3104 flags = BSF_WEAK;
3105 break;
3106 case N_WEAKT:
3107 section = obj_textsec (abfd);
3108 value -= bfd_get_section_vma (abfd, section);
3109 flags = BSF_WEAK;
3110 break;
3111 case N_WEAKD:
3112 section = obj_datasec (abfd);
3113 value -= bfd_get_section_vma (abfd, section);
3114 flags = BSF_WEAK;
3115 break;
3116 case N_WEAKB:
3117 section = obj_bsssec (abfd);
3118 value -= bfd_get_section_vma (abfd, section);
3119 flags = BSF_WEAK;
3120 break;
3123 if (! ((*add_one_symbol)
3124 (info, abfd, name, flags, section, value, string, copy, FALSE,
3125 (struct bfd_link_hash_entry **) sym_hash)))
3126 return FALSE;
3128 /* Restrict the maximum alignment of a common symbol based on
3129 the architecture, since a.out has no way to represent
3130 alignment requirements of a section in a .o file. FIXME:
3131 This isn't quite right: it should use the architecture of the
3132 output file, not the input files. */
3133 if ((*sym_hash)->root.type == bfd_link_hash_common
3134 && ((*sym_hash)->root.u.c.p->alignment_power >
3135 bfd_get_arch_info (abfd)->section_align_power))
3136 (*sym_hash)->root.u.c.p->alignment_power =
3137 bfd_get_arch_info (abfd)->section_align_power;
3139 /* If this is a set symbol, and we are not building sets, then
3140 it is possible for the hash entry to not have been set. In
3141 such a case, treat the symbol as not globally defined. */
3142 if ((*sym_hash)->root.type == bfd_link_hash_new)
3144 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
3145 *sym_hash = NULL;
3148 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3149 ++sym_hash;
3152 return TRUE;
3155 /* Free up the internal symbols read from an a.out file. */
3157 static bfd_boolean
3158 aout_link_free_symbols (bfd *abfd)
3160 if (obj_aout_external_syms (abfd) != NULL)
3162 #ifdef USE_MMAP
3163 bfd_free_window (&obj_aout_sym_window (abfd));
3164 #else
3165 free ((void *) obj_aout_external_syms (abfd));
3166 #endif
3167 obj_aout_external_syms (abfd) = NULL;
3169 if (obj_aout_external_strings (abfd) != NULL)
3171 #ifdef USE_MMAP
3172 bfd_free_window (&obj_aout_string_window (abfd));
3173 #else
3174 free ((void *) obj_aout_external_strings (abfd));
3175 #endif
3176 obj_aout_external_strings (abfd) = NULL;
3178 return TRUE;
3181 /* Add symbols from an a.out object file. */
3183 static bfd_boolean
3184 aout_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3186 if (! aout_get_external_symbols (abfd))
3187 return FALSE;
3188 if (! aout_link_add_symbols (abfd, info))
3189 return FALSE;
3190 if (! info->keep_memory)
3192 if (! aout_link_free_symbols (abfd))
3193 return FALSE;
3195 return TRUE;
3198 /* Look through the internal symbols to see if this object file should
3199 be included in the link. We should include this object file if it
3200 defines any symbols which are currently undefined. If this object
3201 file defines a common symbol, then we may adjust the size of the
3202 known symbol but we do not include the object file in the link
3203 (unless there is some other reason to include it). */
3205 static bfd_boolean
3206 aout_link_check_ar_symbols (bfd *abfd,
3207 struct bfd_link_info *info,
3208 bfd_boolean *pneeded)
3210 struct external_nlist *p;
3211 struct external_nlist *pend;
3212 char *strings;
3214 *pneeded = FALSE;
3216 /* Look through all the symbols. */
3217 p = obj_aout_external_syms (abfd);
3218 pend = p + obj_aout_external_sym_count (abfd);
3219 strings = obj_aout_external_strings (abfd);
3220 for (; p < pend; p++)
3222 int type = H_GET_8 (abfd, p->e_type);
3223 const char *name;
3224 struct bfd_link_hash_entry *h;
3226 /* Ignore symbols that are not externally visible. This is an
3227 optimization only, as we check the type more thoroughly
3228 below. */
3229 if (((type & N_EXT) == 0
3230 || (type & N_STAB) != 0
3231 || type == N_FN)
3232 && type != N_WEAKA
3233 && type != N_WEAKT
3234 && type != N_WEAKD
3235 && type != N_WEAKB)
3237 if (type == N_WARNING
3238 || type == N_INDR)
3239 ++p;
3240 continue;
3243 name = strings + GET_WORD (abfd, p->e_strx);
3244 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
3246 /* We are only interested in symbols that are currently
3247 undefined or common. */
3248 if (h == NULL
3249 || (h->type != bfd_link_hash_undefined
3250 && h->type != bfd_link_hash_common))
3252 if (type == (N_INDR | N_EXT))
3253 ++p;
3254 continue;
3257 if (type == (N_TEXT | N_EXT)
3258 || type == (N_DATA | N_EXT)
3259 || type == (N_BSS | N_EXT)
3260 || type == (N_ABS | N_EXT)
3261 || type == (N_INDR | N_EXT))
3263 /* This object file defines this symbol. We must link it
3264 in. This is true regardless of whether the current
3265 definition of the symbol is undefined or common.
3267 If the current definition is common, we have a case in
3268 which we have already seen an object file including:
3269 int a;
3270 and this object file from the archive includes:
3271 int a = 5;
3272 In such a case, whether to include this object is target
3273 dependant for backward compatibility.
3275 FIXME: The SunOS 4.1.3 linker will pull in the archive
3276 element if the symbol is defined in the .data section,
3277 but not if it is defined in the .text section. That
3278 seems a bit crazy to me, and it has not been implemented
3279 yet. However, it might be correct. */
3280 if (h->type == bfd_link_hash_common)
3282 int skip = 0;
3284 switch (info->common_skip_ar_aymbols)
3286 case bfd_link_common_skip_text:
3287 skip = (type == (N_TEXT | N_EXT));
3288 break;
3289 case bfd_link_common_skip_data:
3290 skip = (type == (N_DATA | N_EXT));
3291 break;
3292 default:
3293 case bfd_link_common_skip_all:
3294 skip = 1;
3295 break;
3298 if (skip)
3299 continue;
3302 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3303 return FALSE;
3304 *pneeded = TRUE;
3305 return TRUE;
3308 if (type == (N_UNDF | N_EXT))
3310 bfd_vma value;
3312 value = GET_WORD (abfd, p->e_value);
3313 if (value != 0)
3315 /* This symbol is common in the object from the archive
3316 file. */
3317 if (h->type == bfd_link_hash_undefined)
3319 bfd *symbfd;
3320 unsigned int power;
3322 symbfd = h->u.undef.abfd;
3323 if (symbfd == NULL)
3325 /* This symbol was created as undefined from
3326 outside BFD. We assume that we should link
3327 in the object file. This is done for the -u
3328 option in the linker. */
3329 if (! (*info->callbacks->add_archive_element) (info,
3330 abfd,
3331 name))
3332 return FALSE;
3333 *pneeded = TRUE;
3334 return TRUE;
3336 /* Turn the current link symbol into a common
3337 symbol. It is already on the undefs list. */
3338 h->type = bfd_link_hash_common;
3339 h->u.c.p = bfd_hash_allocate (&info->hash->table,
3340 sizeof (struct bfd_link_hash_common_entry));
3341 if (h->u.c.p == NULL)
3342 return FALSE;
3344 h->u.c.size = value;
3346 /* FIXME: This isn't quite right. The maximum
3347 alignment of a common symbol should be set by the
3348 architecture of the output file, not of the input
3349 file. */
3350 power = bfd_log2 (value);
3351 if (power > bfd_get_arch_info (abfd)->section_align_power)
3352 power = bfd_get_arch_info (abfd)->section_align_power;
3353 h->u.c.p->alignment_power = power;
3355 h->u.c.p->section = bfd_make_section_old_way (symbfd,
3356 "COMMON");
3358 else
3360 /* Adjust the size of the common symbol if
3361 necessary. */
3362 if (value > h->u.c.size)
3363 h->u.c.size = value;
3368 if (type == N_WEAKA
3369 || type == N_WEAKT
3370 || type == N_WEAKD
3371 || type == N_WEAKB)
3373 /* This symbol is weak but defined. We must pull it in if
3374 the current link symbol is undefined, but we don't want
3375 it if the current link symbol is common. */
3376 if (h->type == bfd_link_hash_undefined)
3378 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3379 return FALSE;
3380 *pneeded = TRUE;
3381 return TRUE;
3386 /* We do not need this object file. */
3387 return TRUE;
3389 /* Check a single archive element to see if we need to include it in
3390 the link. *PNEEDED is set according to whether this element is
3391 needed in the link or not. This is called from
3392 _bfd_generic_link_add_archive_symbols. */
3394 static bfd_boolean
3395 aout_link_check_archive_element (bfd *abfd,
3396 struct bfd_link_info *info,
3397 bfd_boolean *pneeded)
3399 if (! aout_get_external_symbols (abfd))
3400 return FALSE;
3402 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
3403 return FALSE;
3405 if (*pneeded)
3407 if (! aout_link_add_symbols (abfd, info))
3408 return FALSE;
3411 if (! info->keep_memory || ! *pneeded)
3413 if (! aout_link_free_symbols (abfd))
3414 return FALSE;
3417 return TRUE;
3420 /* Given an a.out BFD, add symbols to the global hash table as
3421 appropriate. */
3423 bfd_boolean
3424 NAME (aout, link_add_symbols) (bfd *abfd, struct bfd_link_info *info)
3426 switch (bfd_get_format (abfd))
3428 case bfd_object:
3429 return aout_link_add_object_symbols (abfd, info);
3430 case bfd_archive:
3431 return _bfd_generic_link_add_archive_symbols
3432 (abfd, info, aout_link_check_archive_element);
3433 default:
3434 bfd_set_error (bfd_error_wrong_format);
3435 return FALSE;
3439 /* A hash table used for header files with N_BINCL entries. */
3441 struct aout_link_includes_table
3443 struct bfd_hash_table root;
3446 /* A linked list of totals that we have found for a particular header
3447 file. */
3449 struct aout_link_includes_totals
3451 struct aout_link_includes_totals *next;
3452 bfd_vma total;
3455 /* An entry in the header file hash table. */
3457 struct aout_link_includes_entry
3459 struct bfd_hash_entry root;
3460 /* List of totals we have found for this file. */
3461 struct aout_link_includes_totals *totals;
3464 /* Look up an entry in an the header file hash table. */
3466 #define aout_link_includes_lookup(table, string, create, copy) \
3467 ((struct aout_link_includes_entry *) \
3468 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3470 /* During the final link step we need to pass around a bunch of
3471 information, so we do it in an instance of this structure. */
3473 struct aout_final_link_info
3475 /* General link information. */
3476 struct bfd_link_info *info;
3477 /* Output bfd. */
3478 bfd *output_bfd;
3479 /* Reloc file positions. */
3480 file_ptr treloff, dreloff;
3481 /* File position of symbols. */
3482 file_ptr symoff;
3483 /* String table. */
3484 struct bfd_strtab_hash *strtab;
3485 /* Header file hash table. */
3486 struct aout_link_includes_table includes;
3487 /* A buffer large enough to hold the contents of any section. */
3488 bfd_byte *contents;
3489 /* A buffer large enough to hold the relocs of any section. */
3490 void * relocs;
3491 /* A buffer large enough to hold the symbol map of any input BFD. */
3492 int *symbol_map;
3493 /* A buffer large enough to hold output symbols of any input BFD. */
3494 struct external_nlist *output_syms;
3497 /* The function to create a new entry in the header file hash table. */
3499 static struct bfd_hash_entry *
3500 aout_link_includes_newfunc (struct bfd_hash_entry *entry,
3501 struct bfd_hash_table *table,
3502 const char *string)
3504 struct aout_link_includes_entry *ret =
3505 (struct aout_link_includes_entry *) entry;
3507 /* Allocate the structure if it has not already been allocated by a
3508 subclass. */
3509 if (ret == NULL)
3510 ret = bfd_hash_allocate (table, sizeof (* ret));
3511 if (ret == NULL)
3512 return NULL;
3514 /* Call the allocation method of the superclass. */
3515 ret = ((struct aout_link_includes_entry *)
3516 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3517 if (ret)
3519 /* Set local fields. */
3520 ret->totals = NULL;
3523 return (struct bfd_hash_entry *) ret;
3526 /* Write out a symbol that was not associated with an a.out input
3527 object. */
3529 static bfd_boolean
3530 aout_link_write_other_symbol (struct aout_link_hash_entry *h, void * data)
3532 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
3533 bfd *output_bfd;
3534 int type;
3535 bfd_vma val;
3536 struct external_nlist outsym;
3537 bfd_size_type indx;
3538 bfd_size_type amt;
3540 if (h->root.type == bfd_link_hash_warning)
3542 h = (struct aout_link_hash_entry *) h->root.u.i.link;
3543 if (h->root.type == bfd_link_hash_new)
3544 return TRUE;
3547 output_bfd = finfo->output_bfd;
3549 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
3551 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
3552 (output_bfd, finfo->info, h)))
3554 /* FIXME: No way to handle errors. */
3555 abort ();
3559 if (h->written)
3560 return TRUE;
3562 h->written = TRUE;
3564 /* An indx of -2 means the symbol must be written. */
3565 if (h->indx != -2
3566 && (finfo->info->strip == strip_all
3567 || (finfo->info->strip == strip_some
3568 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
3569 FALSE, FALSE) == NULL)))
3570 return TRUE;
3572 switch (h->root.type)
3574 default:
3575 case bfd_link_hash_warning:
3576 abort ();
3577 /* Avoid variable not initialized warnings. */
3578 return TRUE;
3579 case bfd_link_hash_new:
3580 /* This can happen for set symbols when sets are not being
3581 built. */
3582 return TRUE;
3583 case bfd_link_hash_undefined:
3584 type = N_UNDF | N_EXT;
3585 val = 0;
3586 break;
3587 case bfd_link_hash_defined:
3588 case bfd_link_hash_defweak:
3590 asection *sec;
3592 sec = h->root.u.def.section->output_section;
3593 BFD_ASSERT (bfd_is_abs_section (sec)
3594 || sec->owner == output_bfd);
3595 if (sec == obj_textsec (output_bfd))
3596 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
3597 else if (sec == obj_datasec (output_bfd))
3598 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
3599 else if (sec == obj_bsssec (output_bfd))
3600 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
3601 else
3602 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
3603 type |= N_EXT;
3604 val = (h->root.u.def.value
3605 + sec->vma
3606 + h->root.u.def.section->output_offset);
3608 break;
3609 case bfd_link_hash_common:
3610 type = N_UNDF | N_EXT;
3611 val = h->root.u.c.size;
3612 break;
3613 case bfd_link_hash_undefweak:
3614 type = N_WEAKU;
3615 val = 0;
3616 case bfd_link_hash_indirect:
3617 /* We ignore these symbols, since the indirected symbol is
3618 already in the hash table. */
3619 return TRUE;
3622 H_PUT_8 (output_bfd, type, outsym.e_type);
3623 H_PUT_8 (output_bfd, 0, outsym.e_other);
3624 H_PUT_16 (output_bfd, 0, outsym.e_desc);
3625 indx = add_to_stringtab (output_bfd, finfo->strtab, h->root.root.string,
3626 FALSE);
3627 if (indx == - (bfd_size_type) 1)
3628 /* FIXME: No way to handle errors. */
3629 abort ();
3631 PUT_WORD (output_bfd, indx, outsym.e_strx);
3632 PUT_WORD (output_bfd, val, outsym.e_value);
3634 amt = EXTERNAL_NLIST_SIZE;
3635 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
3636 || bfd_bwrite ((void *) &outsym, amt, output_bfd) != amt)
3637 /* FIXME: No way to handle errors. */
3638 abort ();
3640 finfo->symoff += EXTERNAL_NLIST_SIZE;
3641 h->indx = obj_aout_external_sym_count (output_bfd);
3642 ++obj_aout_external_sym_count (output_bfd);
3644 return TRUE;
3647 /* Handle a link order which is supposed to generate a reloc. */
3649 static bfd_boolean
3650 aout_link_reloc_link_order (struct aout_final_link_info *finfo,
3651 asection *o,
3652 struct bfd_link_order *p)
3654 struct bfd_link_order_reloc *pr;
3655 int r_index;
3656 int r_extern;
3657 reloc_howto_type *howto;
3658 file_ptr *reloff_ptr = NULL;
3659 struct reloc_std_external srel;
3660 struct reloc_ext_external erel;
3661 void * rel_ptr;
3662 bfd_size_type amt;
3664 pr = p->u.reloc.p;
3666 if (p->type == bfd_section_reloc_link_order)
3668 r_extern = 0;
3669 if (bfd_is_abs_section (pr->u.section))
3670 r_index = N_ABS | N_EXT;
3671 else
3673 BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
3674 r_index = pr->u.section->target_index;
3677 else
3679 struct aout_link_hash_entry *h;
3681 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
3682 r_extern = 1;
3683 h = ((struct aout_link_hash_entry *)
3684 bfd_wrapped_link_hash_lookup (finfo->output_bfd, finfo->info,
3685 pr->u.name, FALSE, FALSE, TRUE));
3686 if (h != NULL
3687 && h->indx >= 0)
3688 r_index = h->indx;
3689 else if (h != NULL)
3691 /* We decided to strip this symbol, but it turns out that we
3692 can't. Note that we lose the other and desc information
3693 here. I don't think that will ever matter for a global
3694 symbol. */
3695 h->indx = -2;
3696 h->written = FALSE;
3697 if (! aout_link_write_other_symbol (h, (void *) finfo))
3698 return FALSE;
3699 r_index = h->indx;
3701 else
3703 if (! ((*finfo->info->callbacks->unattached_reloc)
3704 (finfo->info, pr->u.name, NULL, NULL, (bfd_vma) 0)))
3705 return FALSE;
3706 r_index = 0;
3710 howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
3711 if (howto == 0)
3713 bfd_set_error (bfd_error_bad_value);
3714 return FALSE;
3717 if (o == obj_textsec (finfo->output_bfd))
3718 reloff_ptr = &finfo->treloff;
3719 else if (o == obj_datasec (finfo->output_bfd))
3720 reloff_ptr = &finfo->dreloff;
3721 else
3722 abort ();
3724 if (obj_reloc_entry_size (finfo->output_bfd) == RELOC_STD_SIZE)
3726 #ifdef MY_put_reloc
3727 MY_put_reloc (finfo->output_bfd, r_extern, r_index, p->offset, howto,
3728 &srel);
3729 #else
3731 int r_pcrel;
3732 int r_baserel;
3733 int r_jmptable;
3734 int r_relative;
3735 int r_length;
3737 r_pcrel = (int) howto->pc_relative;
3738 r_baserel = (howto->type & 8) != 0;
3739 r_jmptable = (howto->type & 16) != 0;
3740 r_relative = (howto->type & 32) != 0;
3741 r_length = howto->size;
3743 PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
3744 if (bfd_header_big_endian (finfo->output_bfd))
3746 srel.r_index[0] = r_index >> 16;
3747 srel.r_index[1] = r_index >> 8;
3748 srel.r_index[2] = r_index;
3749 srel.r_type[0] =
3750 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
3751 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
3752 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
3753 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
3754 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
3755 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
3757 else
3759 srel.r_index[2] = r_index >> 16;
3760 srel.r_index[1] = r_index >> 8;
3761 srel.r_index[0] = r_index;
3762 srel.r_type[0] =
3763 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
3764 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
3765 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
3766 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
3767 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
3768 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
3771 #endif
3772 rel_ptr = (void *) &srel;
3774 /* We have to write the addend into the object file, since
3775 standard a.out relocs are in place. It would be more
3776 reliable if we had the current contents of the file here,
3777 rather than assuming zeroes, but we can't read the file since
3778 it was opened using bfd_openw. */
3779 if (pr->addend != 0)
3781 bfd_size_type size;
3782 bfd_reloc_status_type r;
3783 bfd_byte *buf;
3784 bfd_boolean ok;
3786 size = bfd_get_reloc_size (howto);
3787 buf = bfd_zmalloc (size);
3788 if (buf == NULL)
3789 return FALSE;
3790 r = MY_relocate_contents (howto, finfo->output_bfd,
3791 (bfd_vma) pr->addend, buf);
3792 switch (r)
3794 case bfd_reloc_ok:
3795 break;
3796 default:
3797 case bfd_reloc_outofrange:
3798 abort ();
3799 case bfd_reloc_overflow:
3800 if (! ((*finfo->info->callbacks->reloc_overflow)
3801 (finfo->info, NULL,
3802 (p->type == bfd_section_reloc_link_order
3803 ? bfd_section_name (finfo->output_bfd,
3804 pr->u.section)
3805 : pr->u.name),
3806 howto->name, pr->addend, NULL, NULL, (bfd_vma) 0)))
3808 free (buf);
3809 return FALSE;
3811 break;
3813 ok = bfd_set_section_contents (finfo->output_bfd, o, (void *) buf,
3814 (file_ptr) p->offset, size);
3815 free (buf);
3816 if (! ok)
3817 return FALSE;
3820 else
3822 #ifdef MY_put_ext_reloc
3823 MY_put_ext_reloc (finfo->output_bfd, r_extern, r_index, p->offset,
3824 howto, &erel, pr->addend);
3825 #else
3826 PUT_WORD (finfo->output_bfd, p->offset, erel.r_address);
3828 if (bfd_header_big_endian (finfo->output_bfd))
3830 erel.r_index[0] = r_index >> 16;
3831 erel.r_index[1] = r_index >> 8;
3832 erel.r_index[2] = r_index;
3833 erel.r_type[0] =
3834 ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
3835 | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
3837 else
3839 erel.r_index[2] = r_index >> 16;
3840 erel.r_index[1] = r_index >> 8;
3841 erel.r_index[0] = r_index;
3842 erel.r_type[0] =
3843 (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
3844 | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
3847 PUT_WORD (finfo->output_bfd, (bfd_vma) pr->addend, erel.r_addend);
3848 #endif /* MY_put_ext_reloc */
3850 rel_ptr = (void *) &erel;
3853 amt = obj_reloc_entry_size (finfo->output_bfd);
3854 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
3855 || bfd_bwrite (rel_ptr, amt, finfo->output_bfd) != amt)
3856 return FALSE;
3858 *reloff_ptr += obj_reloc_entry_size (finfo->output_bfd);
3860 /* Assert that the relocs have not run into the symbols, and that n
3861 the text relocs have not run into the data relocs. */
3862 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
3863 && (reloff_ptr != &finfo->treloff
3864 || (*reloff_ptr
3865 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
3867 return TRUE;
3870 /* Get the section corresponding to a reloc index. */
3872 static INLINE asection *
3873 aout_reloc_index_to_section (bfd *abfd, int indx)
3875 switch (indx & N_TYPE)
3877 case N_TEXT: return obj_textsec (abfd);
3878 case N_DATA: return obj_datasec (abfd);
3879 case N_BSS: return obj_bsssec (abfd);
3880 case N_ABS:
3881 case N_UNDF: return bfd_abs_section_ptr;
3882 default: abort ();
3884 return NULL;
3887 /* Relocate an a.out section using standard a.out relocs. */
3889 static bfd_boolean
3890 aout_link_input_section_std (struct aout_final_link_info *finfo,
3891 bfd *input_bfd,
3892 asection *input_section,
3893 struct reloc_std_external *relocs,
3894 bfd_size_type rel_size,
3895 bfd_byte *contents)
3897 bfd_boolean (*check_dynamic_reloc)
3898 (struct bfd_link_info *, bfd *, asection *,
3899 struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *,
3900 bfd_vma *);
3901 bfd *output_bfd;
3902 bfd_boolean relocatable;
3903 struct external_nlist *syms;
3904 char *strings;
3905 struct aout_link_hash_entry **sym_hashes;
3906 int *symbol_map;
3907 bfd_size_type reloc_count;
3908 struct reloc_std_external *rel;
3909 struct reloc_std_external *rel_end;
3911 output_bfd = finfo->output_bfd;
3912 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
3914 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
3915 BFD_ASSERT (input_bfd->xvec->header_byteorder
3916 == output_bfd->xvec->header_byteorder);
3918 relocatable = finfo->info->relocatable;
3919 syms = obj_aout_external_syms (input_bfd);
3920 strings = obj_aout_external_strings (input_bfd);
3921 sym_hashes = obj_aout_sym_hashes (input_bfd);
3922 symbol_map = finfo->symbol_map;
3924 reloc_count = rel_size / RELOC_STD_SIZE;
3925 rel = relocs;
3926 rel_end = rel + reloc_count;
3927 for (; rel < rel_end; rel++)
3929 bfd_vma r_addr;
3930 int r_index;
3931 int r_extern;
3932 int r_pcrel;
3933 int r_baserel = 0;
3934 reloc_howto_type *howto;
3935 struct aout_link_hash_entry *h = NULL;
3936 bfd_vma relocation;
3937 bfd_reloc_status_type r;
3939 r_addr = GET_SWORD (input_bfd, rel->r_address);
3941 #ifdef MY_reloc_howto
3942 howto = MY_reloc_howto (input_bfd, rel, r_index, r_extern, r_pcrel);
3943 #else
3945 int r_jmptable;
3946 int r_relative;
3947 int r_length;
3948 unsigned int howto_idx;
3950 if (bfd_header_big_endian (input_bfd))
3952 r_index = (((unsigned int) rel->r_index[0] << 16)
3953 | ((unsigned int) rel->r_index[1] << 8)
3954 | rel->r_index[2]);
3955 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
3956 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
3957 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
3958 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
3959 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
3960 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
3961 >> RELOC_STD_BITS_LENGTH_SH_BIG);
3963 else
3965 r_index = (((unsigned int) rel->r_index[2] << 16)
3966 | ((unsigned int) rel->r_index[1] << 8)
3967 | rel->r_index[0]);
3968 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
3969 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
3970 r_baserel = (0 != (rel->r_type[0]
3971 & RELOC_STD_BITS_BASEREL_LITTLE));
3972 r_jmptable= (0 != (rel->r_type[0]
3973 & RELOC_STD_BITS_JMPTABLE_LITTLE));
3974 r_relative= (0 != (rel->r_type[0]
3975 & RELOC_STD_BITS_RELATIVE_LITTLE));
3976 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
3977 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
3980 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
3981 + 16 * r_jmptable + 32 * r_relative);
3982 if (howto_idx < TABLE_SIZE (howto_table_std))
3983 howto = howto_table_std + howto_idx;
3984 else
3985 howto = NULL;
3987 #endif
3989 if (howto == NULL)
3991 (*finfo->info->callbacks->einfo)
3992 (_("%P: %B: unexpected relocation type\n"), input_bfd);
3993 bfd_set_error (bfd_error_bad_value);
3994 return FALSE;
3997 if (relocatable)
3999 /* We are generating a relocatable output file, and must
4000 modify the reloc accordingly. */
4001 if (r_extern)
4003 /* If we know the symbol this relocation is against,
4004 convert it into a relocation against a section. This
4005 is what the native linker does. */
4006 h = sym_hashes[r_index];
4007 if (h != NULL
4008 && (h->root.type == bfd_link_hash_defined
4009 || h->root.type == bfd_link_hash_defweak))
4011 asection *output_section;
4013 /* Change the r_extern value. */
4014 if (bfd_header_big_endian (output_bfd))
4015 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4016 else
4017 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4019 /* Compute a new r_index. */
4020 output_section = h->root.u.def.section->output_section;
4021 if (output_section == obj_textsec (output_bfd))
4022 r_index = N_TEXT;
4023 else if (output_section == obj_datasec (output_bfd))
4024 r_index = N_DATA;
4025 else if (output_section == obj_bsssec (output_bfd))
4026 r_index = N_BSS;
4027 else
4028 r_index = N_ABS;
4030 /* Add the symbol value and the section VMA to the
4031 addend stored in the contents. */
4032 relocation = (h->root.u.def.value
4033 + output_section->vma
4034 + h->root.u.def.section->output_offset);
4036 else
4038 /* We must change r_index according to the symbol
4039 map. */
4040 r_index = symbol_map[r_index];
4042 if (r_index == -1)
4044 if (h != NULL)
4046 /* We decided to strip this symbol, but it
4047 turns out that we can't. Note that we
4048 lose the other and desc information here.
4049 I don't think that will ever matter for a
4050 global symbol. */
4051 if (h->indx < 0)
4053 h->indx = -2;
4054 h->written = FALSE;
4055 if (! aout_link_write_other_symbol (h,
4056 (void *) finfo))
4057 return FALSE;
4059 r_index = h->indx;
4061 else
4063 const char *name;
4065 name = strings + GET_WORD (input_bfd,
4066 syms[r_index].e_strx);
4067 if (! ((*finfo->info->callbacks->unattached_reloc)
4068 (finfo->info, name, input_bfd, input_section,
4069 r_addr)))
4070 return FALSE;
4071 r_index = 0;
4075 relocation = 0;
4078 /* Write out the new r_index value. */
4079 if (bfd_header_big_endian (output_bfd))
4081 rel->r_index[0] = r_index >> 16;
4082 rel->r_index[1] = r_index >> 8;
4083 rel->r_index[2] = r_index;
4085 else
4087 rel->r_index[2] = r_index >> 16;
4088 rel->r_index[1] = r_index >> 8;
4089 rel->r_index[0] = r_index;
4092 else
4094 asection *section;
4096 /* This is a relocation against a section. We must
4097 adjust by the amount that the section moved. */
4098 section = aout_reloc_index_to_section (input_bfd, r_index);
4099 relocation = (section->output_section->vma
4100 + section->output_offset
4101 - section->vma);
4104 /* Change the address of the relocation. */
4105 PUT_WORD (output_bfd,
4106 r_addr + input_section->output_offset,
4107 rel->r_address);
4109 /* Adjust a PC relative relocation by removing the reference
4110 to the original address in the section and including the
4111 reference to the new address. */
4112 if (r_pcrel)
4113 relocation -= (input_section->output_section->vma
4114 + input_section->output_offset
4115 - input_section->vma);
4117 #ifdef MY_relocatable_reloc
4118 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
4119 #endif
4121 if (relocation == 0)
4122 r = bfd_reloc_ok;
4123 else
4124 r = MY_relocate_contents (howto,
4125 input_bfd, relocation,
4126 contents + r_addr);
4128 else
4130 bfd_boolean hundef;
4132 /* We are generating an executable, and must do a full
4133 relocation. */
4134 hundef = FALSE;
4136 if (r_extern)
4138 h = sym_hashes[r_index];
4140 if (h != NULL
4141 && (h->root.type == bfd_link_hash_defined
4142 || h->root.type == bfd_link_hash_defweak))
4144 relocation = (h->root.u.def.value
4145 + h->root.u.def.section->output_section->vma
4146 + h->root.u.def.section->output_offset);
4148 else if (h != NULL
4149 && h->root.type == bfd_link_hash_undefweak)
4150 relocation = 0;
4151 else
4153 hundef = TRUE;
4154 relocation = 0;
4157 else
4159 asection *section;
4161 section = aout_reloc_index_to_section (input_bfd, r_index);
4162 relocation = (section->output_section->vma
4163 + section->output_offset
4164 - section->vma);
4165 if (r_pcrel)
4166 relocation += input_section->vma;
4169 if (check_dynamic_reloc != NULL)
4171 bfd_boolean skip;
4173 if (! ((*check_dynamic_reloc)
4174 (finfo->info, input_bfd, input_section, h,
4175 (void *) rel, contents, &skip, &relocation)))
4176 return FALSE;
4177 if (skip)
4178 continue;
4181 /* Now warn if a global symbol is undefined. We could not
4182 do this earlier, because check_dynamic_reloc might want
4183 to skip this reloc. */
4184 if (hundef && ! finfo->info->shared && ! r_baserel)
4186 const char *name;
4188 if (h != NULL)
4189 name = h->root.root.string;
4190 else
4191 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4192 if (! ((*finfo->info->callbacks->undefined_symbol)
4193 (finfo->info, name, input_bfd, input_section,
4194 r_addr, TRUE)))
4195 return FALSE;
4198 r = MY_final_link_relocate (howto,
4199 input_bfd, input_section,
4200 contents, r_addr, relocation,
4201 (bfd_vma) 0);
4204 if (r != bfd_reloc_ok)
4206 switch (r)
4208 default:
4209 case bfd_reloc_outofrange:
4210 abort ();
4211 case bfd_reloc_overflow:
4213 const char *name;
4215 if (h != NULL)
4216 name = NULL;
4217 else if (r_extern)
4218 name = strings + GET_WORD (input_bfd,
4219 syms[r_index].e_strx);
4220 else
4222 asection *s;
4224 s = aout_reloc_index_to_section (input_bfd, r_index);
4225 name = bfd_section_name (input_bfd, s);
4227 if (! ((*finfo->info->callbacks->reloc_overflow)
4228 (finfo->info, (h ? &h->root : NULL), name,
4229 howto->name, (bfd_vma) 0, input_bfd,
4230 input_section, r_addr)))
4231 return FALSE;
4233 break;
4238 return TRUE;
4241 /* Relocate an a.out section using extended a.out relocs. */
4243 static bfd_boolean
4244 aout_link_input_section_ext (struct aout_final_link_info *finfo,
4245 bfd *input_bfd,
4246 asection *input_section,
4247 struct reloc_ext_external *relocs,
4248 bfd_size_type rel_size,
4249 bfd_byte *contents)
4251 bfd_boolean (*check_dynamic_reloc)
4252 (struct bfd_link_info *, bfd *, asection *,
4253 struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *,
4254 bfd_vma *);
4255 bfd *output_bfd;
4256 bfd_boolean relocatable;
4257 struct external_nlist *syms;
4258 char *strings;
4259 struct aout_link_hash_entry **sym_hashes;
4260 int *symbol_map;
4261 bfd_size_type reloc_count;
4262 struct reloc_ext_external *rel;
4263 struct reloc_ext_external *rel_end;
4265 output_bfd = finfo->output_bfd;
4266 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4268 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
4269 BFD_ASSERT (input_bfd->xvec->header_byteorder
4270 == output_bfd->xvec->header_byteorder);
4272 relocatable = finfo->info->relocatable;
4273 syms = obj_aout_external_syms (input_bfd);
4274 strings = obj_aout_external_strings (input_bfd);
4275 sym_hashes = obj_aout_sym_hashes (input_bfd);
4276 symbol_map = finfo->symbol_map;
4278 reloc_count = rel_size / RELOC_EXT_SIZE;
4279 rel = relocs;
4280 rel_end = rel + reloc_count;
4281 for (; rel < rel_end; rel++)
4283 bfd_vma r_addr;
4284 int r_index;
4285 int r_extern;
4286 unsigned int r_type;
4287 bfd_vma r_addend;
4288 struct aout_link_hash_entry *h = NULL;
4289 asection *r_section = NULL;
4290 bfd_vma relocation;
4292 r_addr = GET_SWORD (input_bfd, rel->r_address);
4294 if (bfd_header_big_endian (input_bfd))
4296 r_index = (((unsigned int) rel->r_index[0] << 16)
4297 | ((unsigned int) rel->r_index[1] << 8)
4298 | rel->r_index[2]);
4299 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
4300 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
4301 >> RELOC_EXT_BITS_TYPE_SH_BIG);
4303 else
4305 r_index = (((unsigned int) rel->r_index[2] << 16)
4306 | ((unsigned int) rel->r_index[1] << 8)
4307 | rel->r_index[0]);
4308 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
4309 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
4310 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
4313 r_addend = GET_SWORD (input_bfd, rel->r_addend);
4315 if (r_type >= TABLE_SIZE (howto_table_ext))
4317 (*finfo->info->callbacks->einfo)
4318 (_("%P: %B: unexpected relocation type\n"), input_bfd);
4319 bfd_set_error (bfd_error_bad_value);
4320 return FALSE;
4323 if (relocatable)
4325 /* We are generating a relocatable output file, and must
4326 modify the reloc accordingly. */
4327 if (r_extern
4328 || r_type == (unsigned int) RELOC_BASE10
4329 || r_type == (unsigned int) RELOC_BASE13
4330 || r_type == (unsigned int) RELOC_BASE22)
4332 /* If we know the symbol this relocation is against,
4333 convert it into a relocation against a section. This
4334 is what the native linker does. */
4335 if (r_type == (unsigned int) RELOC_BASE10
4336 || r_type == (unsigned int) RELOC_BASE13
4337 || r_type == (unsigned int) RELOC_BASE22)
4338 h = NULL;
4339 else
4340 h = sym_hashes[r_index];
4341 if (h != NULL
4342 && (h->root.type == bfd_link_hash_defined
4343 || h->root.type == bfd_link_hash_defweak))
4345 asection *output_section;
4347 /* Change the r_extern value. */
4348 if (bfd_header_big_endian (output_bfd))
4349 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
4350 else
4351 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
4353 /* Compute a new r_index. */
4354 output_section = h->root.u.def.section->output_section;
4355 if (output_section == obj_textsec (output_bfd))
4356 r_index = N_TEXT;
4357 else if (output_section == obj_datasec (output_bfd))
4358 r_index = N_DATA;
4359 else if (output_section == obj_bsssec (output_bfd))
4360 r_index = N_BSS;
4361 else
4362 r_index = N_ABS;
4364 /* Add the symbol value and the section VMA to the
4365 addend. */
4366 relocation = (h->root.u.def.value
4367 + output_section->vma
4368 + h->root.u.def.section->output_offset);
4370 /* Now RELOCATION is the VMA of the final
4371 destination. If this is a PC relative reloc,
4372 then ADDEND is the negative of the source VMA.
4373 We want to set ADDEND to the difference between
4374 the destination VMA and the source VMA, which
4375 means we must adjust RELOCATION by the change in
4376 the source VMA. This is done below. */
4378 else
4380 /* We must change r_index according to the symbol
4381 map. */
4382 r_index = symbol_map[r_index];
4384 if (r_index == -1)
4386 if (h != NULL)
4388 /* We decided to strip this symbol, but it
4389 turns out that we can't. Note that we
4390 lose the other and desc information here.
4391 I don't think that will ever matter for a
4392 global symbol. */
4393 if (h->indx < 0)
4395 h->indx = -2;
4396 h->written = FALSE;
4397 if (! aout_link_write_other_symbol (h,
4398 (void *) finfo))
4399 return FALSE;
4401 r_index = h->indx;
4403 else
4405 const char *name;
4407 name = strings + GET_WORD (input_bfd,
4408 syms[r_index].e_strx);
4409 if (! ((*finfo->info->callbacks->unattached_reloc)
4410 (finfo->info, name, input_bfd, input_section,
4411 r_addr)))
4412 return FALSE;
4413 r_index = 0;
4417 relocation = 0;
4419 /* If this is a PC relative reloc, then the addend
4420 is the negative of the source VMA. We must
4421 adjust it by the change in the source VMA. This
4422 is done below. */
4425 /* Write out the new r_index value. */
4426 if (bfd_header_big_endian (output_bfd))
4428 rel->r_index[0] = r_index >> 16;
4429 rel->r_index[1] = r_index >> 8;
4430 rel->r_index[2] = r_index;
4432 else
4434 rel->r_index[2] = r_index >> 16;
4435 rel->r_index[1] = r_index >> 8;
4436 rel->r_index[0] = r_index;
4439 else
4441 /* This is a relocation against a section. We must
4442 adjust by the amount that the section moved. */
4443 r_section = aout_reloc_index_to_section (input_bfd, r_index);
4444 relocation = (r_section->output_section->vma
4445 + r_section->output_offset
4446 - r_section->vma);
4448 /* If this is a PC relative reloc, then the addend is
4449 the difference in VMA between the destination and the
4450 source. We have just adjusted for the change in VMA
4451 of the destination, so we must also adjust by the
4452 change in VMA of the source. This is done below. */
4455 /* As described above, we must always adjust a PC relative
4456 reloc by the change in VMA of the source. However, if
4457 pcrel_offset is set, then the addend does not include the
4458 location within the section, in which case we don't need
4459 to adjust anything. */
4460 if (howto_table_ext[r_type].pc_relative
4461 && ! howto_table_ext[r_type].pcrel_offset)
4462 relocation -= (input_section->output_section->vma
4463 + input_section->output_offset
4464 - input_section->vma);
4466 /* Change the addend if necessary. */
4467 if (relocation != 0)
4468 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
4470 /* Change the address of the relocation. */
4471 PUT_WORD (output_bfd,
4472 r_addr + input_section->output_offset,
4473 rel->r_address);
4475 else
4477 bfd_boolean hundef;
4478 bfd_reloc_status_type r;
4480 /* We are generating an executable, and must do a full
4481 relocation. */
4482 hundef = FALSE;
4484 if (r_extern)
4486 h = sym_hashes[r_index];
4488 if (h != NULL
4489 && (h->root.type == bfd_link_hash_defined
4490 || h->root.type == bfd_link_hash_defweak))
4492 relocation = (h->root.u.def.value
4493 + h->root.u.def.section->output_section->vma
4494 + h->root.u.def.section->output_offset);
4496 else if (h != NULL
4497 && h->root.type == bfd_link_hash_undefweak)
4498 relocation = 0;
4499 else
4501 hundef = TRUE;
4502 relocation = 0;
4505 else if (r_type == (unsigned int) RELOC_BASE10
4506 || r_type == (unsigned int) RELOC_BASE13
4507 || r_type == (unsigned int) RELOC_BASE22)
4509 struct external_nlist *sym;
4510 int type;
4512 /* For base relative relocs, r_index is always an index
4513 into the symbol table, even if r_extern is 0. */
4514 sym = syms + r_index;
4515 type = H_GET_8 (input_bfd, sym->e_type);
4516 if ((type & N_TYPE) == N_TEXT
4517 || type == N_WEAKT)
4518 r_section = obj_textsec (input_bfd);
4519 else if ((type & N_TYPE) == N_DATA
4520 || type == N_WEAKD)
4521 r_section = obj_datasec (input_bfd);
4522 else if ((type & N_TYPE) == N_BSS
4523 || type == N_WEAKB)
4524 r_section = obj_bsssec (input_bfd);
4525 else if ((type & N_TYPE) == N_ABS
4526 || type == N_WEAKA)
4527 r_section = bfd_abs_section_ptr;
4528 else
4529 abort ();
4530 relocation = (r_section->output_section->vma
4531 + r_section->output_offset
4532 + (GET_WORD (input_bfd, sym->e_value)
4533 - r_section->vma));
4535 else
4537 r_section = aout_reloc_index_to_section (input_bfd, r_index);
4539 /* If this is a PC relative reloc, then R_ADDEND is the
4540 difference between the two vmas, or
4541 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4542 where
4543 old_dest_sec == section->vma
4545 old_src_sec == input_section->vma
4547 old_src_off == r_addr
4549 _bfd_final_link_relocate expects RELOCATION +
4550 R_ADDEND to be the VMA of the destination minus
4551 r_addr (the minus r_addr is because this relocation
4552 is not pcrel_offset, which is a bit confusing and
4553 should, perhaps, be changed), or
4554 new_dest_sec
4555 where
4556 new_dest_sec == output_section->vma + output_offset
4557 We arrange for this to happen by setting RELOCATION to
4558 new_dest_sec + old_src_sec - old_dest_sec
4560 If this is not a PC relative reloc, then R_ADDEND is
4561 simply the VMA of the destination, so we set
4562 RELOCATION to the change in the destination VMA, or
4563 new_dest_sec - old_dest_sec
4565 relocation = (r_section->output_section->vma
4566 + r_section->output_offset
4567 - r_section->vma);
4568 if (howto_table_ext[r_type].pc_relative)
4569 relocation += input_section->vma;
4572 if (check_dynamic_reloc != NULL)
4574 bfd_boolean skip;
4576 if (! ((*check_dynamic_reloc)
4577 (finfo->info, input_bfd, input_section, h,
4578 (void *) rel, contents, &skip, &relocation)))
4579 return FALSE;
4580 if (skip)
4581 continue;
4584 /* Now warn if a global symbol is undefined. We could not
4585 do this earlier, because check_dynamic_reloc might want
4586 to skip this reloc. */
4587 if (hundef
4588 && ! finfo->info->shared
4589 && r_type != (unsigned int) RELOC_BASE10
4590 && r_type != (unsigned int) RELOC_BASE13
4591 && r_type != (unsigned int) RELOC_BASE22)
4593 const char *name;
4595 if (h != NULL)
4596 name = h->root.root.string;
4597 else
4598 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4599 if (! ((*finfo->info->callbacks->undefined_symbol)
4600 (finfo->info, name, input_bfd, input_section,
4601 r_addr, TRUE)))
4602 return FALSE;
4605 if (r_type != (unsigned int) RELOC_SPARC_REV32)
4606 r = MY_final_link_relocate (howto_table_ext + r_type,
4607 input_bfd, input_section,
4608 contents, r_addr, relocation,
4609 r_addend);
4610 else
4612 bfd_vma x;
4614 x = bfd_get_32 (input_bfd, contents + r_addr);
4615 x = x + relocation + r_addend;
4616 bfd_putl32 (/*input_bfd,*/ x, contents + r_addr);
4617 r = bfd_reloc_ok;
4620 if (r != bfd_reloc_ok)
4622 switch (r)
4624 default:
4625 case bfd_reloc_outofrange:
4626 abort ();
4627 case bfd_reloc_overflow:
4629 const char *name;
4631 if (h != NULL)
4632 name = NULL;
4633 else if (r_extern
4634 || r_type == (unsigned int) RELOC_BASE10
4635 || r_type == (unsigned int) RELOC_BASE13
4636 || r_type == (unsigned int) RELOC_BASE22)
4637 name = strings + GET_WORD (input_bfd,
4638 syms[r_index].e_strx);
4639 else
4641 asection *s;
4643 s = aout_reloc_index_to_section (input_bfd, r_index);
4644 name = bfd_section_name (input_bfd, s);
4646 if (! ((*finfo->info->callbacks->reloc_overflow)
4647 (finfo->info, (h ? &h->root : NULL), name,
4648 howto_table_ext[r_type].name,
4649 r_addend, input_bfd, input_section, r_addr)))
4650 return FALSE;
4652 break;
4658 return TRUE;
4661 /* Link an a.out section into the output file. */
4663 static bfd_boolean
4664 aout_link_input_section (struct aout_final_link_info *finfo,
4665 bfd *input_bfd,
4666 asection *input_section,
4667 file_ptr *reloff_ptr,
4668 bfd_size_type rel_size)
4670 bfd_size_type input_size;
4671 void * relocs;
4673 /* Get the section contents. */
4674 input_size = input_section->size;
4675 if (! bfd_get_section_contents (input_bfd, input_section,
4676 (void *) finfo->contents,
4677 (file_ptr) 0, input_size))
4678 return FALSE;
4680 /* Read in the relocs if we haven't already done it. */
4681 if (aout_section_data (input_section) != NULL
4682 && aout_section_data (input_section)->relocs != NULL)
4683 relocs = aout_section_data (input_section)->relocs;
4684 else
4686 relocs = finfo->relocs;
4687 if (rel_size > 0)
4689 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4690 || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
4691 return FALSE;
4695 /* Relocate the section contents. */
4696 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
4698 if (! aout_link_input_section_std (finfo, input_bfd, input_section,
4699 (struct reloc_std_external *) relocs,
4700 rel_size, finfo->contents))
4701 return FALSE;
4703 else
4705 if (! aout_link_input_section_ext (finfo, input_bfd, input_section,
4706 (struct reloc_ext_external *) relocs,
4707 rel_size, finfo->contents))
4708 return FALSE;
4711 /* Write out the section contents. */
4712 if (! bfd_set_section_contents (finfo->output_bfd,
4713 input_section->output_section,
4714 (void *) finfo->contents,
4715 (file_ptr) input_section->output_offset,
4716 input_size))
4717 return FALSE;
4719 /* If we are producing relocatable output, the relocs were
4720 modified, and we now write them out. */
4721 if (finfo->info->relocatable && rel_size > 0)
4723 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4724 return FALSE;
4725 if (bfd_bwrite (relocs, rel_size, finfo->output_bfd) != rel_size)
4726 return FALSE;
4727 *reloff_ptr += rel_size;
4729 /* Assert that the relocs have not run into the symbols, and
4730 that if these are the text relocs they have not run into the
4731 data relocs. */
4732 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4733 && (reloff_ptr != &finfo->treloff
4734 || (*reloff_ptr
4735 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4738 return TRUE;
4741 /* Adjust and write out the symbols for an a.out file. Set the new
4742 symbol indices into a symbol_map. */
4744 static bfd_boolean
4745 aout_link_write_symbols (struct aout_final_link_info *finfo, bfd *input_bfd)
4747 bfd *output_bfd;
4748 bfd_size_type sym_count;
4749 char *strings;
4750 enum bfd_link_strip strip;
4751 enum bfd_link_discard discard;
4752 struct external_nlist *outsym;
4753 bfd_size_type strtab_index;
4754 struct external_nlist *sym;
4755 struct external_nlist *sym_end;
4756 struct aout_link_hash_entry **sym_hash;
4757 int *symbol_map;
4758 bfd_boolean pass;
4759 bfd_boolean skip_next;
4761 output_bfd = finfo->output_bfd;
4762 sym_count = obj_aout_external_sym_count (input_bfd);
4763 strings = obj_aout_external_strings (input_bfd);
4764 strip = finfo->info->strip;
4765 discard = finfo->info->discard;
4766 outsym = finfo->output_syms;
4768 /* First write out a symbol for this object file, unless we are
4769 discarding such symbols. */
4770 if (strip != strip_all
4771 && (strip != strip_some
4772 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
4773 FALSE, FALSE) != NULL)
4774 && discard != discard_all)
4776 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
4777 H_PUT_8 (output_bfd, 0, outsym->e_other);
4778 H_PUT_16 (output_bfd, 0, outsym->e_desc);
4779 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
4780 input_bfd->filename, FALSE);
4781 if (strtab_index == (bfd_size_type) -1)
4782 return FALSE;
4783 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4784 PUT_WORD (output_bfd,
4785 (bfd_get_section_vma (output_bfd,
4786 obj_textsec (input_bfd)->output_section)
4787 + obj_textsec (input_bfd)->output_offset),
4788 outsym->e_value);
4789 ++obj_aout_external_sym_count (output_bfd);
4790 ++outsym;
4793 pass = FALSE;
4794 skip_next = FALSE;
4795 sym = obj_aout_external_syms (input_bfd);
4796 sym_end = sym + sym_count;
4797 sym_hash = obj_aout_sym_hashes (input_bfd);
4798 symbol_map = finfo->symbol_map;
4799 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
4800 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
4802 const char *name;
4803 int type;
4804 struct aout_link_hash_entry *h;
4805 bfd_boolean skip;
4806 asection *symsec;
4807 bfd_vma val = 0;
4808 bfd_boolean copy;
4810 /* We set *symbol_map to 0 above for all symbols. If it has
4811 already been set to -1 for this symbol, it means that we are
4812 discarding it because it appears in a duplicate header file.
4813 See the N_BINCL code below. */
4814 if (*symbol_map == -1)
4815 continue;
4817 /* Initialize *symbol_map to -1, which means that the symbol was
4818 not copied into the output file. We will change it later if
4819 we do copy the symbol over. */
4820 *symbol_map = -1;
4822 type = H_GET_8 (input_bfd, sym->e_type);
4823 name = strings + GET_WORD (input_bfd, sym->e_strx);
4825 h = NULL;
4827 if (pass)
4829 /* Pass this symbol through. It is the target of an
4830 indirect or warning symbol. */
4831 val = GET_WORD (input_bfd, sym->e_value);
4832 pass = FALSE;
4834 else if (skip_next)
4836 /* Skip this symbol, which is the target of an indirect
4837 symbol that we have changed to no longer be an indirect
4838 symbol. */
4839 skip_next = FALSE;
4840 continue;
4842 else
4844 struct aout_link_hash_entry *hresolve;
4846 /* We have saved the hash table entry for this symbol, if
4847 there is one. Note that we could just look it up again
4848 in the hash table, provided we first check that it is an
4849 external symbol. */
4850 h = *sym_hash;
4852 /* Use the name from the hash table, in case the symbol was
4853 wrapped. */
4854 if (h != NULL
4855 && h->root.type != bfd_link_hash_warning)
4856 name = h->root.root.string;
4858 /* If this is an indirect or warning symbol, then change
4859 hresolve to the base symbol. We also change *sym_hash so
4860 that the relocation routines relocate against the real
4861 symbol. */
4862 hresolve = h;
4863 if (h != (struct aout_link_hash_entry *) NULL
4864 && (h->root.type == bfd_link_hash_indirect
4865 || h->root.type == bfd_link_hash_warning))
4867 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
4868 while (hresolve->root.type == bfd_link_hash_indirect
4869 || hresolve->root.type == bfd_link_hash_warning)
4870 hresolve = ((struct aout_link_hash_entry *)
4871 hresolve->root.u.i.link);
4872 *sym_hash = hresolve;
4875 /* If the symbol has already been written out, skip it. */
4876 if (h != NULL
4877 && h->written)
4879 if ((type & N_TYPE) == N_INDR
4880 || type == N_WARNING)
4881 skip_next = TRUE;
4882 *symbol_map = h->indx;
4883 continue;
4886 /* See if we are stripping this symbol. */
4887 skip = FALSE;
4888 switch (strip)
4890 case strip_none:
4891 break;
4892 case strip_debugger:
4893 if ((type & N_STAB) != 0)
4894 skip = TRUE;
4895 break;
4896 case strip_some:
4897 if (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
4898 == NULL)
4899 skip = TRUE;
4900 break;
4901 case strip_all:
4902 skip = TRUE;
4903 break;
4905 if (skip)
4907 if (h != NULL)
4908 h->written = TRUE;
4909 continue;
4912 /* Get the value of the symbol. */
4913 if ((type & N_TYPE) == N_TEXT
4914 || type == N_WEAKT)
4915 symsec = obj_textsec (input_bfd);
4916 else if ((type & N_TYPE) == N_DATA
4917 || type == N_WEAKD)
4918 symsec = obj_datasec (input_bfd);
4919 else if ((type & N_TYPE) == N_BSS
4920 || type == N_WEAKB)
4921 symsec = obj_bsssec (input_bfd);
4922 else if ((type & N_TYPE) == N_ABS
4923 || type == N_WEAKA)
4924 symsec = bfd_abs_section_ptr;
4925 else if (((type & N_TYPE) == N_INDR
4926 && (hresolve == NULL
4927 || (hresolve->root.type != bfd_link_hash_defined
4928 && hresolve->root.type != bfd_link_hash_defweak
4929 && hresolve->root.type != bfd_link_hash_common)))
4930 || type == N_WARNING)
4932 /* Pass the next symbol through unchanged. The
4933 condition above for indirect symbols is so that if
4934 the indirect symbol was defined, we output it with
4935 the correct definition so the debugger will
4936 understand it. */
4937 pass = TRUE;
4938 val = GET_WORD (input_bfd, sym->e_value);
4939 symsec = NULL;
4941 else if ((type & N_STAB) != 0)
4943 val = GET_WORD (input_bfd, sym->e_value);
4944 symsec = NULL;
4946 else
4948 /* If we get here with an indirect symbol, it means that
4949 we are outputting it with a real definition. In such
4950 a case we do not want to output the next symbol,
4951 which is the target of the indirection. */
4952 if ((type & N_TYPE) == N_INDR)
4953 skip_next = TRUE;
4955 symsec = NULL;
4957 /* We need to get the value from the hash table. We use
4958 hresolve so that if we have defined an indirect
4959 symbol we output the final definition. */
4960 if (h == NULL)
4962 switch (type & N_TYPE)
4964 case N_SETT:
4965 symsec = obj_textsec (input_bfd);
4966 break;
4967 case N_SETD:
4968 symsec = obj_datasec (input_bfd);
4969 break;
4970 case N_SETB:
4971 symsec = obj_bsssec (input_bfd);
4972 break;
4973 case N_SETA:
4974 symsec = bfd_abs_section_ptr;
4975 break;
4976 default:
4977 val = 0;
4978 break;
4981 else if (hresolve->root.type == bfd_link_hash_defined
4982 || hresolve->root.type == bfd_link_hash_defweak)
4984 asection *input_section;
4985 asection *output_section;
4987 /* This case usually means a common symbol which was
4988 turned into a defined symbol. */
4989 input_section = hresolve->root.u.def.section;
4990 output_section = input_section->output_section;
4991 BFD_ASSERT (bfd_is_abs_section (output_section)
4992 || output_section->owner == output_bfd);
4993 val = (hresolve->root.u.def.value
4994 + bfd_get_section_vma (output_bfd, output_section)
4995 + input_section->output_offset);
4997 /* Get the correct type based on the section. If
4998 this is a constructed set, force it to be
4999 globally visible. */
5000 if (type == N_SETT
5001 || type == N_SETD
5002 || type == N_SETB
5003 || type == N_SETA)
5004 type |= N_EXT;
5006 type &=~ N_TYPE;
5008 if (output_section == obj_textsec (output_bfd))
5009 type |= (hresolve->root.type == bfd_link_hash_defined
5010 ? N_TEXT
5011 : N_WEAKT);
5012 else if (output_section == obj_datasec (output_bfd))
5013 type |= (hresolve->root.type == bfd_link_hash_defined
5014 ? N_DATA
5015 : N_WEAKD);
5016 else if (output_section == obj_bsssec (output_bfd))
5017 type |= (hresolve->root.type == bfd_link_hash_defined
5018 ? N_BSS
5019 : N_WEAKB);
5020 else
5021 type |= (hresolve->root.type == bfd_link_hash_defined
5022 ? N_ABS
5023 : N_WEAKA);
5025 else if (hresolve->root.type == bfd_link_hash_common)
5026 val = hresolve->root.u.c.size;
5027 else if (hresolve->root.type == bfd_link_hash_undefweak)
5029 val = 0;
5030 type = N_WEAKU;
5032 else
5033 val = 0;
5035 if (symsec != NULL)
5036 val = (symsec->output_section->vma
5037 + symsec->output_offset
5038 + (GET_WORD (input_bfd, sym->e_value)
5039 - symsec->vma));
5041 /* If this is a global symbol set the written flag, and if
5042 it is a local symbol see if we should discard it. */
5043 if (h != NULL)
5045 h->written = TRUE;
5046 h->indx = obj_aout_external_sym_count (output_bfd);
5048 else if ((type & N_TYPE) != N_SETT
5049 && (type & N_TYPE) != N_SETD
5050 && (type & N_TYPE) != N_SETB
5051 && (type & N_TYPE) != N_SETA)
5053 switch (discard)
5055 case discard_none:
5056 case discard_sec_merge:
5057 break;
5058 case discard_l:
5059 if ((type & N_STAB) == 0
5060 && bfd_is_local_label_name (input_bfd, name))
5061 skip = TRUE;
5062 break;
5063 case discard_all:
5064 skip = TRUE;
5065 break;
5067 if (skip)
5069 pass = FALSE;
5070 continue;
5074 /* An N_BINCL symbol indicates the start of the stabs
5075 entries for a header file. We need to scan ahead to the
5076 next N_EINCL symbol, ignoring nesting, adding up all the
5077 characters in the symbol names, not including the file
5078 numbers in types (the first number after an open
5079 parenthesis). */
5080 if (type == (int) N_BINCL)
5082 struct external_nlist *incl_sym;
5083 int nest;
5084 struct aout_link_includes_entry *incl_entry;
5085 struct aout_link_includes_totals *t;
5087 val = 0;
5088 nest = 0;
5089 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
5091 int incl_type;
5093 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
5094 if (incl_type == (int) N_EINCL)
5096 if (nest == 0)
5097 break;
5098 --nest;
5100 else if (incl_type == (int) N_BINCL)
5101 ++nest;
5102 else if (nest == 0)
5104 const char *s;
5106 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
5107 for (; *s != '\0'; s++)
5109 val += *s;
5110 if (*s == '(')
5112 /* Skip the file number. */
5113 ++s;
5114 while (ISDIGIT (*s))
5115 ++s;
5116 --s;
5122 /* If we have already included a header file with the
5123 same value, then replace this one with an N_EXCL
5124 symbol. */
5125 copy = (bfd_boolean) (! finfo->info->keep_memory);
5126 incl_entry = aout_link_includes_lookup (&finfo->includes,
5127 name, TRUE, copy);
5128 if (incl_entry == NULL)
5129 return FALSE;
5130 for (t = incl_entry->totals; t != NULL; t = t->next)
5131 if (t->total == val)
5132 break;
5133 if (t == NULL)
5135 /* This is the first time we have seen this header
5136 file with this set of stabs strings. */
5137 t = bfd_hash_allocate (&finfo->includes.root,
5138 sizeof *t);
5139 if (t == NULL)
5140 return FALSE;
5141 t->total = val;
5142 t->next = incl_entry->totals;
5143 incl_entry->totals = t;
5145 else
5147 int *incl_map;
5149 /* This is a duplicate header file. We must change
5150 it to be an N_EXCL entry, and mark all the
5151 included symbols to prevent outputting them. */
5152 type = (int) N_EXCL;
5154 nest = 0;
5155 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
5156 incl_sym < sym_end;
5157 incl_sym++, incl_map++)
5159 int incl_type;
5161 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
5162 if (incl_type == (int) N_EINCL)
5164 if (nest == 0)
5166 *incl_map = -1;
5167 break;
5169 --nest;
5171 else if (incl_type == (int) N_BINCL)
5172 ++nest;
5173 else if (nest == 0)
5174 *incl_map = -1;
5180 /* Copy this symbol into the list of symbols we are going to
5181 write out. */
5182 H_PUT_8 (output_bfd, type, outsym->e_type);
5183 H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_other), outsym->e_other);
5184 H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
5185 copy = FALSE;
5186 if (! finfo->info->keep_memory)
5188 /* name points into a string table which we are going to
5189 free. If there is a hash table entry, use that string.
5190 Otherwise, copy name into memory. */
5191 if (h != NULL)
5192 name = h->root.root.string;
5193 else
5194 copy = TRUE;
5196 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
5197 name, copy);
5198 if (strtab_index == (bfd_size_type) -1)
5199 return FALSE;
5200 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
5201 PUT_WORD (output_bfd, val, outsym->e_value);
5202 *symbol_map = obj_aout_external_sym_count (output_bfd);
5203 ++obj_aout_external_sym_count (output_bfd);
5204 ++outsym;
5207 /* Write out the output symbols we have just constructed. */
5208 if (outsym > finfo->output_syms)
5210 bfd_size_type outsym_size;
5212 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
5213 return FALSE;
5214 outsym_size = outsym - finfo->output_syms;
5215 outsym_size *= EXTERNAL_NLIST_SIZE;
5216 if (bfd_bwrite ((void *) finfo->output_syms, outsym_size, output_bfd)
5217 != outsym_size)
5218 return FALSE;
5219 finfo->symoff += outsym_size;
5222 return TRUE;
5225 /* Link an a.out input BFD into the output file. */
5227 static bfd_boolean
5228 aout_link_input_bfd (struct aout_final_link_info *finfo, bfd *input_bfd)
5230 bfd_size_type sym_count;
5232 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
5234 /* If this is a dynamic object, it may need special handling. */
5235 if ((input_bfd->flags & DYNAMIC) != 0
5236 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
5237 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
5238 (finfo->info, input_bfd));
5240 /* Get the symbols. We probably have them already, unless
5241 finfo->info->keep_memory is FALSE. */
5242 if (! aout_get_external_symbols (input_bfd))
5243 return FALSE;
5245 sym_count = obj_aout_external_sym_count (input_bfd);
5247 /* Write out the symbols and get a map of the new indices. The map
5248 is placed into finfo->symbol_map. */
5249 if (! aout_link_write_symbols (finfo, input_bfd))
5250 return FALSE;
5252 /* Relocate and write out the sections. These functions use the
5253 symbol map created by aout_link_write_symbols. The linker_mark
5254 field will be set if these sections are to be included in the
5255 link, which will normally be the case. */
5256 if (obj_textsec (input_bfd)->linker_mark)
5258 if (! aout_link_input_section (finfo, input_bfd,
5259 obj_textsec (input_bfd),
5260 &finfo->treloff,
5261 exec_hdr (input_bfd)->a_trsize))
5262 return FALSE;
5264 if (obj_datasec (input_bfd)->linker_mark)
5266 if (! aout_link_input_section (finfo, input_bfd,
5267 obj_datasec (input_bfd),
5268 &finfo->dreloff,
5269 exec_hdr (input_bfd)->a_drsize))
5270 return FALSE;
5273 /* If we are not keeping memory, we don't need the symbols any
5274 longer. We still need them if we are keeping memory, because the
5275 strings in the hash table point into them. */
5276 if (! finfo->info->keep_memory)
5278 if (! aout_link_free_symbols (input_bfd))
5279 return FALSE;
5282 return TRUE;
5285 /* Do the final link step. This is called on the output BFD. The
5286 INFO structure should point to a list of BFDs linked through the
5287 link_next field which can be used to find each BFD which takes part
5288 in the output. Also, each section in ABFD should point to a list
5289 of bfd_link_order structures which list all the input sections for
5290 the output section. */
5292 bfd_boolean
5293 NAME (aout, final_link) (bfd *abfd,
5294 struct bfd_link_info *info,
5295 void (*callback) (bfd *, file_ptr *, file_ptr *, file_ptr *))
5297 struct aout_final_link_info aout_info;
5298 bfd_boolean includes_hash_initialized = FALSE;
5299 bfd *sub;
5300 bfd_size_type trsize, drsize;
5301 bfd_size_type max_contents_size;
5302 bfd_size_type max_relocs_size;
5303 bfd_size_type max_sym_count;
5304 bfd_size_type text_size;
5305 file_ptr text_end;
5306 struct bfd_link_order *p;
5307 asection *o;
5308 bfd_boolean have_link_order_relocs;
5310 if (info->shared)
5311 abfd->flags |= DYNAMIC;
5313 aout_info.info = info;
5314 aout_info.output_bfd = abfd;
5315 aout_info.contents = NULL;
5316 aout_info.relocs = NULL;
5317 aout_info.symbol_map = NULL;
5318 aout_info.output_syms = NULL;
5320 if (!bfd_hash_table_init_n (&aout_info.includes.root,
5321 aout_link_includes_newfunc,
5322 sizeof (struct aout_link_includes_entry),
5323 251))
5324 goto error_return;
5325 includes_hash_initialized = TRUE;
5327 /* Figure out the largest section size. Also, if generating
5328 relocatable output, count the relocs. */
5329 trsize = 0;
5330 drsize = 0;
5331 max_contents_size = 0;
5332 max_relocs_size = 0;
5333 max_sym_count = 0;
5334 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5336 bfd_size_type sz;
5338 if (info->relocatable)
5340 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
5342 trsize += exec_hdr (sub)->a_trsize;
5343 drsize += exec_hdr (sub)->a_drsize;
5345 else
5347 /* FIXME: We need to identify the .text and .data sections
5348 and call get_reloc_upper_bound and canonicalize_reloc to
5349 work out the number of relocs needed, and then multiply
5350 by the reloc size. */
5351 (*_bfd_error_handler)
5352 (_("%s: relocatable link from %s to %s not supported"),
5353 bfd_get_filename (abfd),
5354 sub->xvec->name, abfd->xvec->name);
5355 bfd_set_error (bfd_error_invalid_operation);
5356 goto error_return;
5360 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
5362 sz = obj_textsec (sub)->size;
5363 if (sz > max_contents_size)
5364 max_contents_size = sz;
5365 sz = obj_datasec (sub)->size;
5366 if (sz > max_contents_size)
5367 max_contents_size = sz;
5369 sz = exec_hdr (sub)->a_trsize;
5370 if (sz > max_relocs_size)
5371 max_relocs_size = sz;
5372 sz = exec_hdr (sub)->a_drsize;
5373 if (sz > max_relocs_size)
5374 max_relocs_size = sz;
5376 sz = obj_aout_external_sym_count (sub);
5377 if (sz > max_sym_count)
5378 max_sym_count = sz;
5382 if (info->relocatable)
5384 if (obj_textsec (abfd) != NULL)
5385 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
5386 ->map_head.link_order)
5387 * obj_reloc_entry_size (abfd));
5388 if (obj_datasec (abfd) != NULL)
5389 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
5390 ->map_head.link_order)
5391 * obj_reloc_entry_size (abfd));
5394 exec_hdr (abfd)->a_trsize = trsize;
5395 exec_hdr (abfd)->a_drsize = drsize;
5397 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
5399 /* Adjust the section sizes and vmas according to the magic number.
5400 This sets a_text, a_data and a_bss in the exec_hdr and sets the
5401 filepos for each section. */
5402 if (! NAME (aout, adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
5403 goto error_return;
5405 /* The relocation and symbol file positions differ among a.out
5406 targets. We are passed a callback routine from the backend
5407 specific code to handle this.
5408 FIXME: At this point we do not know how much space the symbol
5409 table will require. This will not work for any (nonstandard)
5410 a.out target that needs to know the symbol table size before it
5411 can compute the relocation file positions. This may or may not
5412 be the case for the hp300hpux target, for example. */
5413 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
5414 &aout_info.symoff);
5415 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
5416 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
5417 obj_sym_filepos (abfd) = aout_info.symoff;
5419 /* We keep a count of the symbols as we output them. */
5420 obj_aout_external_sym_count (abfd) = 0;
5422 /* We accumulate the string table as we write out the symbols. */
5423 aout_info.strtab = _bfd_stringtab_init ();
5424 if (aout_info.strtab == NULL)
5425 goto error_return;
5427 /* Allocate buffers to hold section contents and relocs. */
5428 aout_info.contents = bfd_malloc (max_contents_size);
5429 aout_info.relocs = bfd_malloc (max_relocs_size);
5430 aout_info.symbol_map = bfd_malloc (max_sym_count * sizeof (int *));
5431 aout_info.output_syms = bfd_malloc ((max_sym_count + 1)
5432 * sizeof (struct external_nlist));
5433 if ((aout_info.contents == NULL && max_contents_size != 0)
5434 || (aout_info.relocs == NULL && max_relocs_size != 0)
5435 || (aout_info.symbol_map == NULL && max_sym_count != 0)
5436 || aout_info.output_syms == NULL)
5437 goto error_return;
5439 /* If we have a symbol named __DYNAMIC, force it out now. This is
5440 required by SunOS. Doing this here rather than in sunos.c is a
5441 hack, but it's easier than exporting everything which would be
5442 needed. */
5444 struct aout_link_hash_entry *h;
5446 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
5447 FALSE, FALSE, FALSE);
5448 if (h != NULL)
5449 aout_link_write_other_symbol (h, &aout_info);
5452 /* The most time efficient way to do the link would be to read all
5453 the input object files into memory and then sort out the
5454 information into the output file. Unfortunately, that will
5455 probably use too much memory. Another method would be to step
5456 through everything that composes the text section and write it
5457 out, and then everything that composes the data section and write
5458 it out, and then write out the relocs, and then write out the
5459 symbols. Unfortunately, that requires reading stuff from each
5460 input file several times, and we will not be able to keep all the
5461 input files open simultaneously, and reopening them will be slow.
5463 What we do is basically process one input file at a time. We do
5464 everything we need to do with an input file once--copy over the
5465 section contents, handle the relocation information, and write
5466 out the symbols--and then we throw away the information we read
5467 from it. This approach requires a lot of lseeks of the output
5468 file, which is unfortunate but still faster than reopening a lot
5469 of files.
5471 We use the output_has_begun field of the input BFDs to see
5472 whether we have already handled it. */
5473 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
5474 sub->output_has_begun = FALSE;
5476 /* Mark all sections which are to be included in the link. This
5477 will normally be every section. We need to do this so that we
5478 can identify any sections which the linker has decided to not
5479 include. */
5480 for (o = abfd->sections; o != NULL; o = o->next)
5482 for (p = o->map_head.link_order; p != NULL; p = p->next)
5483 if (p->type == bfd_indirect_link_order)
5484 p->u.indirect.section->linker_mark = TRUE;
5487 have_link_order_relocs = FALSE;
5488 for (o = abfd->sections; o != NULL; o = o->next)
5490 for (p = o->map_head.link_order;
5491 p != NULL;
5492 p = p->next)
5494 if (p->type == bfd_indirect_link_order
5495 && (bfd_get_flavour (p->u.indirect.section->owner)
5496 == bfd_target_aout_flavour))
5498 bfd *input_bfd;
5500 input_bfd = p->u.indirect.section->owner;
5501 if (! input_bfd->output_has_begun)
5503 if (! aout_link_input_bfd (&aout_info, input_bfd))
5504 goto error_return;
5505 input_bfd->output_has_begun = TRUE;
5508 else if (p->type == bfd_section_reloc_link_order
5509 || p->type == bfd_symbol_reloc_link_order)
5511 /* These are handled below. */
5512 have_link_order_relocs = TRUE;
5514 else
5516 if (! _bfd_default_link_order (abfd, info, o, p))
5517 goto error_return;
5522 /* Write out any symbols that we have not already written out. */
5523 aout_link_hash_traverse (aout_hash_table (info),
5524 aout_link_write_other_symbol,
5525 (void *) &aout_info);
5527 /* Now handle any relocs we were asked to create by the linker.
5528 These did not come from any input file. We must do these after
5529 we have written out all the symbols, so that we know the symbol
5530 indices to use. */
5531 if (have_link_order_relocs)
5533 for (o = abfd->sections; o != NULL; o = o->next)
5535 for (p = o->map_head.link_order;
5536 p != NULL;
5537 p = p->next)
5539 if (p->type == bfd_section_reloc_link_order
5540 || p->type == bfd_symbol_reloc_link_order)
5542 if (! aout_link_reloc_link_order (&aout_info, o, p))
5543 goto error_return;
5549 if (aout_info.contents != NULL)
5551 free (aout_info.contents);
5552 aout_info.contents = NULL;
5554 if (aout_info.relocs != NULL)
5556 free (aout_info.relocs);
5557 aout_info.relocs = NULL;
5559 if (aout_info.symbol_map != NULL)
5561 free (aout_info.symbol_map);
5562 aout_info.symbol_map = NULL;
5564 if (aout_info.output_syms != NULL)
5566 free (aout_info.output_syms);
5567 aout_info.output_syms = NULL;
5569 if (includes_hash_initialized)
5571 bfd_hash_table_free (&aout_info.includes.root);
5572 includes_hash_initialized = FALSE;
5575 /* Finish up any dynamic linking we may be doing. */
5576 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
5578 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
5579 goto error_return;
5582 /* Update the header information. */
5583 abfd->symcount = obj_aout_external_sym_count (abfd);
5584 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
5585 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
5586 obj_textsec (abfd)->reloc_count =
5587 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
5588 obj_datasec (abfd)->reloc_count =
5589 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
5591 /* Write out the string table, unless there are no symbols. */
5592 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
5593 goto error_return;
5594 if (abfd->symcount > 0)
5596 if (!emit_stringtab (abfd, aout_info.strtab))
5597 goto error_return;
5599 else
5601 bfd_byte b[BYTES_IN_WORD];
5603 memset (b, 0, BYTES_IN_WORD);
5604 if (bfd_bwrite (b, (bfd_size_type) BYTES_IN_WORD, abfd) != BYTES_IN_WORD)
5605 goto error_return;
5608 return TRUE;
5610 error_return:
5611 if (aout_info.contents != NULL)
5612 free (aout_info.contents);
5613 if (aout_info.relocs != NULL)
5614 free (aout_info.relocs);
5615 if (aout_info.symbol_map != NULL)
5616 free (aout_info.symbol_map);
5617 if (aout_info.output_syms != NULL)
5618 free (aout_info.output_syms);
5619 if (includes_hash_initialized)
5620 bfd_hash_table_free (&aout_info.includes.root);
5621 return FALSE;