1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright (C) 1990-2025 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
28 BFD supports a number of different flavours of a.out format,
29 though the major differences are only the sizes of the
30 structures on disk, and the shape of the relocation
33 The support is split into a basic support file @file{aoutx.h}
34 and other files which derive functions from the base. One
35 derivation file is @file{aoutf1.h} (for a.out flavour 1), and
36 adds to the basic a.out functions support for sun3, sun4, and
37 386 a.out files, to create a target jump vector for a specific
40 This information is further split out into more specific files
41 for each machine, including @file{sunos.c} for sun3 and sun4,
42 and @file{demo64.c} for a demonstration of a 64 bit a.out format.
44 The base file @file{aoutx.h} defines general mechanisms for
45 reading and writing records to and from disk and various
46 other methods which BFD requires. It is included by
47 @file{aout32.c} and @file{aout64.c} to form the names
48 <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc.
50 As an example, this is what goes on to make the back end for a
51 sun4, from @file{aout32.c}:
53 | #define ARCH_SIZE 32
59 | aout_32_canonicalize_reloc
60 | aout_32_find_nearest_line
62 | aout_32_get_reloc_upper_bound
67 | #define TARGET_NAME "a.out-sunos-big"
68 | #define VECNAME sparc_aout_sunos_be_vec
71 requires all the names from @file{aout32.c}, and produces the jump vector
73 | sparc_aout_sunos_be_vec
75 The file @file{host-aout.c} is a special case. It is for a large set
76 of hosts that use ``more or less standard'' a.out files, and
77 for which cross-debugging is not interesting. It uses the
78 standard 32-bit a.out support routines, but determines the
79 file offsets and addresses of the text, data, and BSS
80 sections, the machine architecture and machine type, and the
81 entry point address, in a host-dependent manner. Once these
82 values have been determined, generic code is used to handle
85 When porting it to run on a new system, you must supply:
89 | HOST_MACHINE_ARCH (optional)
90 | HOST_MACHINE_MACHINE (optional)
91 | HOST_TEXT_START_ADDR
94 in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These
95 values, plus the structures and macros defined in @file{a.out.h} on
96 your host system, will produce a BFD target that will access
97 ordinary a.out files on your host. To configure a new machine
98 to use @file{host-aout.c}, specify:
100 | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
101 | TDEPFILES= host-aout.o trad-core.o
103 in the @file{config/@var{XXX}.mt} file, and modify @file{configure.ac}
105 @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
106 configuration is selected. */
109 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
110 Doesn't matter what the setting of WP_TEXT is on output, but it'll
112 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
113 * Any BFD with both flags clear is OMAGIC.
114 (Just want to make these explicit, so the conditions tested in this
115 file make sense if you're more familiar with a.out than with BFD.) */
117 #define KEEPIT udata.i
122 #include "safe-ctype.h"
127 #include "aout/aout64.h"
128 #include "aout/stab_gnu.h"
132 #define N_IS_BMAGIC(x) (N_MAGIC (x) == BMAGIC)
134 #define N_IS_BMAGIC(x) (0)
138 #define N_SET_QMAGIC(x) N_SET_MAGIC (x, QMAGIC)
140 #define N_SET_QMAGIC(x) do { /**/ } while (0)
148 The file @file{aoutx.h} provides for both the @emph{standard}
149 and @emph{extended} forms of a.out relocation records.
151 The standard records contain only an address, a symbol index,
152 and a type field. The extended records also have a full
153 integer for an addend. */
155 #ifndef CTOR_TABLE_RELOC_HOWTO
156 #define CTOR_TABLE_RELOC_IDX 2
157 #define CTOR_TABLE_RELOC_HOWTO(BFD) \
158 ((obj_reloc_entry_size (BFD) == RELOC_EXT_SIZE \
159 ? howto_table_ext : howto_table_std) \
160 + CTOR_TABLE_RELOC_IDX)
163 #ifndef MY_swap_std_reloc_in
164 #define MY_swap_std_reloc_in NAME (aout, swap_std_reloc_in)
167 #ifndef MY_swap_ext_reloc_in
168 #define MY_swap_ext_reloc_in NAME (aout, swap_ext_reloc_in)
171 #ifndef MY_swap_std_reloc_out
172 #define MY_swap_std_reloc_out NAME (aout, swap_std_reloc_out)
175 #ifndef MY_swap_ext_reloc_out
176 #define MY_swap_ext_reloc_out NAME (aout, swap_ext_reloc_out)
179 #ifndef MY_final_link_relocate
180 #define MY_final_link_relocate _bfd_final_link_relocate
183 #ifndef MY_relocate_contents
184 #define MY_relocate_contents _bfd_relocate_contents
187 #define howto_table_ext NAME (aout, ext_howto_table)
188 #define howto_table_std NAME (aout, std_howto_table)
190 reloc_howto_type howto_table_ext
[] =
192 /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
193 HOWTO (RELOC_8
, 0, 1, 8, false, 0, complain_overflow_bitfield
, 0, "8", false, 0, 0x000000ff, false),
194 HOWTO (RELOC_16
, 0, 2, 16, false, 0, complain_overflow_bitfield
, 0, "16", false, 0, 0x0000ffff, false),
195 HOWTO (RELOC_32
, 0, 4, 32, false, 0, complain_overflow_bitfield
, 0, "32", false, 0, 0xffffffff, false),
196 HOWTO (RELOC_DISP8
, 0, 1, 8, true, 0, complain_overflow_signed
, 0, "DISP8", false, 0, 0x000000ff, false),
197 HOWTO (RELOC_DISP16
, 0, 2, 16, true, 0, complain_overflow_signed
, 0, "DISP16", false, 0, 0x0000ffff, false),
198 HOWTO (RELOC_DISP32
, 0, 4, 32, true, 0, complain_overflow_signed
, 0, "DISP32", false, 0, 0xffffffff, false),
199 HOWTO (RELOC_WDISP30
, 2, 4, 30, true, 0, complain_overflow_signed
, 0, "WDISP30", false, 0, 0x3fffffff, false),
200 HOWTO (RELOC_WDISP22
, 2, 4, 22, true, 0, complain_overflow_signed
, 0, "WDISP22", false, 0, 0x003fffff, false),
201 HOWTO (RELOC_HI22
, 10, 4, 22, false, 0, complain_overflow_bitfield
, 0, "HI22", false, 0, 0x003fffff, false),
202 HOWTO (RELOC_22
, 0, 4, 22, false, 0, complain_overflow_bitfield
, 0, "22", false, 0, 0x003fffff, false),
203 HOWTO (RELOC_13
, 0, 4, 13, false, 0, complain_overflow_bitfield
, 0, "13", false, 0, 0x00001fff, false),
204 HOWTO (RELOC_LO10
, 0, 4, 10, false, 0, complain_overflow_dont
, 0, "LO10", false, 0, 0x000003ff, false),
205 HOWTO (RELOC_SFA_BASE
,0, 4, 32, false, 0, complain_overflow_bitfield
, 0, "SFA_BASE", false, 0, 0xffffffff, false),
206 HOWTO (RELOC_SFA_OFF13
,0, 4, 32, false, 0, complain_overflow_bitfield
, 0, "SFA_OFF13", false, 0, 0xffffffff, false),
207 HOWTO (RELOC_BASE10
, 0, 4, 10, false, 0, complain_overflow_dont
, 0, "BASE10", false, 0, 0x000003ff, false),
208 HOWTO (RELOC_BASE13
, 0, 4, 13, false, 0, complain_overflow_signed
, 0, "BASE13", false, 0, 0x00001fff, false),
209 HOWTO (RELOC_BASE22
, 10, 4, 22, false, 0, complain_overflow_bitfield
, 0, "BASE22", false, 0, 0x003fffff, false),
210 HOWTO (RELOC_PC10
, 0, 4, 10, true, 0, complain_overflow_dont
, 0, "PC10", false, 0, 0x000003ff, true),
211 HOWTO (RELOC_PC22
, 10, 4, 22, true, 0, complain_overflow_signed
, 0, "PC22", false, 0, 0x003fffff, true),
212 HOWTO (RELOC_JMP_TBL
, 2, 4, 30, true, 0, complain_overflow_signed
, 0, "JMP_TBL", false, 0, 0x3fffffff, false),
213 HOWTO (RELOC_SEGOFF16
,0, 4, 0, false, 0, complain_overflow_bitfield
, 0, "SEGOFF16", false, 0, 0x00000000, false),
214 HOWTO (RELOC_GLOB_DAT
,0, 4, 0, false, 0, complain_overflow_bitfield
, 0, "GLOB_DAT", false, 0, 0x00000000, false),
215 HOWTO (RELOC_JMP_SLOT
,0, 4, 0, false, 0, complain_overflow_bitfield
, 0, "JMP_SLOT", false, 0, 0x00000000, false),
216 HOWTO (RELOC_RELATIVE
,0, 4, 0, false, 0, complain_overflow_bitfield
, 0, "RELATIVE", false, 0, 0x00000000, false),
217 HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont
, 0, "R_SPARC_NONE",false, 0, 0x00000000, true),
218 HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont
, 0, "R_SPARC_NONE",false, 0, 0x00000000, true),
219 #define RELOC_SPARC_REV32 RELOC_WDISP19
220 HOWTO (RELOC_SPARC_REV32
, 0, 4, 32, false, 0, complain_overflow_dont
, 0,"R_SPARC_REV32",false, 0, 0xffffffff, false),
223 /* Convert standard reloc records to "arelent" format (incl byte swap). */
225 reloc_howto_type howto_table_std
[] =
227 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
228 HOWTO ( 0, 0, 1, 8, false, 0, complain_overflow_bitfield
,0,"8", true, 0x000000ff,0x000000ff, false),
229 HOWTO ( 1, 0, 2, 16, false, 0, complain_overflow_bitfield
,0,"16", true, 0x0000ffff,0x0000ffff, false),
230 HOWTO ( 2, 0, 4, 32, false, 0, complain_overflow_bitfield
,0,"32", true, 0xffffffff,0xffffffff, false),
231 HOWTO ( 3, 0, 8, 64, false, 0, complain_overflow_bitfield
,0,"64", true, 0xdeaddead,0xdeaddead, false),
232 HOWTO ( 4, 0, 1, 8, true, 0, complain_overflow_signed
, 0,"DISP8", true, 0x000000ff,0x000000ff, false),
233 HOWTO ( 5, 0, 2, 16, true, 0, complain_overflow_signed
, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
234 HOWTO ( 6, 0, 4, 32, true, 0, complain_overflow_signed
, 0,"DISP32", true, 0xffffffff,0xffffffff, false),
235 HOWTO ( 7, 0, 8, 64, true, 0, complain_overflow_signed
, 0,"DISP64", true, 0xfeedface,0xfeedface, false),
236 HOWTO ( 8, 0, 4, 0, false, 0, complain_overflow_bitfield
,0,"GOT_REL", false, 0,0x00000000, false),
237 HOWTO ( 9, 0, 2, 16, false, 0, complain_overflow_bitfield
,0,"BASE16", false,0xffffffff,0xffffffff, false),
238 HOWTO (10, 0, 4, 32, false, 0, complain_overflow_bitfield
,0,"BASE32", false,0xffffffff,0xffffffff, false),
244 HOWTO (16, 0, 4, 0, false, 0, complain_overflow_bitfield
,0,"JMP_TABLE", false, 0,0x00000000, false),
260 HOWTO (32, 0, 4, 0, false, 0, complain_overflow_bitfield
,0,"RELATIVE", false, 0,0x00000000, false),
268 HOWTO (40, 0, 4, 0, false, 0, complain_overflow_bitfield
,0,"BASEREL", false, 0,0x00000000, false),
271 #define TABLE_SIZE(TABLE) (sizeof (TABLE) / sizeof (TABLE[0]))
274 NAME (aout
, reloc_type_lookup
) (bfd
*abfd
, bfd_reloc_code_real_type code
)
276 #define EXT(i, j) case i: return & howto_table_ext [j]
277 #define STD(i, j) case i: return & howto_table_std [j]
278 int ext
= obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
;
280 if (code
== BFD_RELOC_CTOR
)
281 switch (bfd_arch_bits_per_address (abfd
))
294 EXT (BFD_RELOC_8
, 0);
295 EXT (BFD_RELOC_16
, 1);
296 EXT (BFD_RELOC_32
, 2);
297 EXT (BFD_RELOC_HI22
, 8);
298 EXT (BFD_RELOC_LO10
, 11);
299 EXT (BFD_RELOC_32_PCREL_S2
, 6);
300 EXT (BFD_RELOC_SPARC_WDISP22
, 7);
301 EXT (BFD_RELOC_SPARC13
, 10);
302 EXT (BFD_RELOC_SPARC_GOT10
, 14);
303 EXT (BFD_RELOC_SPARC_BASE13
, 15);
304 EXT (BFD_RELOC_SPARC_GOT13
, 15);
305 EXT (BFD_RELOC_SPARC_GOT22
, 16);
306 EXT (BFD_RELOC_SPARC_PC10
, 17);
307 EXT (BFD_RELOC_SPARC_PC22
, 18);
308 EXT (BFD_RELOC_SPARC_WPLT30
, 19);
309 EXT (BFD_RELOC_SPARC_REV32
, 26);
317 STD (BFD_RELOC_8
, 0);
318 STD (BFD_RELOC_16
, 1);
319 STD (BFD_RELOC_32
, 2);
320 STD (BFD_RELOC_8_PCREL
, 4);
321 STD (BFD_RELOC_16_PCREL
, 5);
322 STD (BFD_RELOC_32_PCREL
, 6);
323 STD (BFD_RELOC_16_BASEREL
, 9);
324 STD (BFD_RELOC_32_BASEREL
, 10);
331 NAME (aout
, reloc_name_lookup
) (bfd
*abfd
, const char *r_name
)
333 unsigned int i
, size
;
334 reloc_howto_type
*howto_table
;
336 if (obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
)
338 howto_table
= howto_table_ext
;
339 size
= sizeof (howto_table_ext
) / sizeof (howto_table_ext
[0]);
343 howto_table
= howto_table_std
;
344 size
= sizeof (howto_table_std
) / sizeof (howto_table_std
[0]);
347 for (i
= 0; i
< size
; i
++)
348 if (howto_table
[i
].name
!= NULL
349 && strcasecmp (howto_table
[i
].name
, r_name
) == 0)
350 return &howto_table
[i
];
357 Internal entry points
360 @file{aoutx.h} exports several routines for accessing the
361 contents of an a.out file, which are gathered and exported in
362 turn by various format specific files (eg sunos.c).
367 aout_@var{size}_swap_exec_header_in
370 void aout_@var{size}_swap_exec_header_in,
372 struct external_exec *bytes,
373 struct internal_exec *execp);
376 Swap the information in an executable header @var{raw_bytes} taken
377 from a raw byte stream memory image into the internal exec header
378 structure @var{execp}.
381 #ifndef NAME_swap_exec_header_in
383 NAME (aout
, swap_exec_header_in
) (bfd
*abfd
,
384 struct external_exec
*bytes
,
385 struct internal_exec
*execp
)
387 /* The internal_exec structure has some fields that are unused in this
388 configuration (IE for i960), so ensure that all such uninitialized
389 fields are zero'd out. There are places where two of these structs
390 are memcmp'd, and thus the contents do matter. */
391 memset ((void *) execp
, 0, sizeof (struct internal_exec
));
392 /* Now fill in fields in the execp, from the bytes in the raw data. */
393 execp
->a_info
= H_GET_32 (abfd
, bytes
->e_info
);
394 execp
->a_text
= GET_WORD (abfd
, bytes
->e_text
);
395 execp
->a_data
= GET_WORD (abfd
, bytes
->e_data
);
396 execp
->a_bss
= GET_WORD (abfd
, bytes
->e_bss
);
397 execp
->a_syms
= GET_WORD (abfd
, bytes
->e_syms
);
398 execp
->a_entry
= GET_WORD (abfd
, bytes
->e_entry
);
399 execp
->a_trsize
= GET_WORD (abfd
, bytes
->e_trsize
);
400 execp
->a_drsize
= GET_WORD (abfd
, bytes
->e_drsize
);
402 #define NAME_swap_exec_header_in NAME (aout, swap_exec_header_in)
407 aout_@var{size}_swap_exec_header_out
410 bool aout_@var{size}_swap_exec_header_out
412 struct internal_exec *execp,
413 struct external_exec *raw_bytes);
416 Swap the information in an internal exec header structure
417 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
420 NAME (aout
, swap_exec_header_out
) (bfd
*abfd
,
421 struct internal_exec
*execp
,
422 struct external_exec
*bytes
)
424 const char *err
= NULL
;
426 #define MAXVAL(x) ((UINT64_C (1) << (8 * sizeof (x) - 1) << 1) - 1)
427 if ((val
= execp
->a_text
) > MAXVAL (bytes
->e_text
))
429 else if ((val
= execp
->a_data
) > MAXVAL (bytes
->e_data
))
431 else if ((val
= execp
->a_bss
) > MAXVAL (bytes
->e_bss
))
433 else if ((val
= execp
->a_syms
) > MAXVAL (bytes
->e_syms
))
435 else if ((val
= execp
->a_entry
) > MAXVAL (bytes
->e_entry
))
437 else if ((val
= execp
->a_trsize
) > MAXVAL (bytes
->e_trsize
))
439 else if ((val
= execp
->a_drsize
) > MAXVAL (bytes
->e_drsize
))
444 _bfd_error_handler (_("%pB: %#" PRIx64
" overflows header %s field"),
446 bfd_set_error (bfd_error_file_too_big
);
450 /* Now fill in fields in the raw data, from the fields in the exec struct. */
451 H_PUT_32 (abfd
, execp
->a_info
, bytes
->e_info
);
452 PUT_WORD (abfd
, execp
->a_text
, bytes
->e_text
);
453 PUT_WORD (abfd
, execp
->a_data
, bytes
->e_data
);
454 PUT_WORD (abfd
, execp
->a_bss
, bytes
->e_bss
);
455 PUT_WORD (abfd
, execp
->a_syms
, bytes
->e_syms
);
456 PUT_WORD (abfd
, execp
->a_entry
, bytes
->e_entry
);
457 PUT_WORD (abfd
, execp
->a_trsize
, bytes
->e_trsize
);
458 PUT_WORD (abfd
, execp
->a_drsize
, bytes
->e_drsize
);
462 /* Make all the section for an a.out file. */
465 NAME (aout
, make_sections
) (bfd
*abfd
)
467 if (obj_textsec (abfd
) == NULL
&& bfd_make_section (abfd
, ".text") == NULL
)
469 if (obj_datasec (abfd
) == NULL
&& bfd_make_section (abfd
, ".data") == NULL
)
471 if (obj_bsssec (abfd
) == NULL
&& bfd_make_section (abfd
, ".bss") == NULL
)
478 aout_@var{size}_some_aout_object_p
481 bfd_cleanup aout_@var{size}_some_aout_object_p
483 struct internal_exec *execp,
484 bfd_cleanup (*callback_to_real_object_p) (bfd *));
487 Some a.out variant thinks that the file open in @var{abfd}
488 checking is an a.out file. Do some more checking, and set up
489 for access if it really is. Call back to the calling
490 environment's "finish up" function just before returning, to
491 handle any last-minute setup.
495 NAME (aout
, some_aout_object_p
) (bfd
*abfd
,
496 struct internal_exec
*execp
,
497 bfd_cleanup (*callback_to_real_object_p
) (bfd
*))
499 struct aout_data_struct
*rawptr
;
502 rawptr
= bfd_zalloc (abfd
, sizeof (*rawptr
));
505 abfd
->tdata
.aout_data
= rawptr
;
507 abfd
->tdata
.aout_data
->a
.hdr
= &rawptr
->e
;
508 /* Copy in the internal_exec struct. */
509 *(abfd
->tdata
.aout_data
->a
.hdr
) = *execp
;
510 execp
= abfd
->tdata
.aout_data
->a
.hdr
;
512 /* Set the file flags. */
513 abfd
->flags
= BFD_NO_FLAGS
;
514 if (execp
->a_drsize
|| execp
->a_trsize
)
515 abfd
->flags
|= HAS_RELOC
;
516 /* Setting of EXEC_P has been deferred to the bottom of this function. */
518 abfd
->flags
|= HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
;
519 if (N_DYNAMIC (execp
))
520 abfd
->flags
|= DYNAMIC
;
522 if (N_MAGIC (execp
) == ZMAGIC
)
524 abfd
->flags
|= D_PAGED
| WP_TEXT
;
525 adata (abfd
).magic
= z_magic
;
527 else if (N_IS_QMAGIC (execp
))
529 abfd
->flags
|= D_PAGED
| WP_TEXT
;
530 adata (abfd
).magic
= z_magic
;
531 adata (abfd
).subformat
= q_magic_format
;
533 else if (N_MAGIC (execp
) == NMAGIC
)
535 abfd
->flags
|= WP_TEXT
;
536 adata (abfd
).magic
= n_magic
;
538 else if (N_MAGIC (execp
) == OMAGIC
|| N_IS_BMAGIC (execp
))
539 adata (abfd
).magic
= o_magic
;
541 /* Should have been checked with N_BADMAG before this routine
545 abfd
->start_address
= execp
->a_entry
;
547 abfd
->symcount
= execp
->a_syms
/ sizeof (struct external_nlist
);
549 /* The default relocation entry size is that of traditional V7 Unix. */
550 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
552 /* The default symbol entry size is that of traditional Unix. */
553 obj_symbol_entry_size (abfd
) = EXTERNAL_NLIST_SIZE
;
555 if (! NAME (aout
, make_sections
) (abfd
))
558 obj_datasec (abfd
)->size
= execp
->a_data
;
559 obj_bsssec (abfd
)->size
= execp
->a_bss
;
561 obj_textsec (abfd
)->flags
=
562 (execp
->a_trsize
!= 0
563 ? (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
| SEC_RELOC
)
564 : (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
));
565 obj_datasec (abfd
)->flags
=
566 (execp
->a_drsize
!= 0
567 ? (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_RELOC
)
568 : (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
));
569 obj_bsssec (abfd
)->flags
= SEC_ALLOC
;
571 #ifdef THIS_IS_ONLY_DOCUMENTATION
572 /* The common code can't fill in these things because they depend
573 on either the start address of the text segment, the rounding
574 up of virtual addresses between segments, or the starting file
575 position of the text segment -- all of which varies among different
576 versions of a.out. */
578 /* Call back to the format-dependent code to fill in the rest of the
579 fields and do any further cleanup. Things that should be filled
580 in by the callback: */
581 struct exec
*execp
= exec_hdr (abfd
);
583 obj_textsec (abfd
)->size
= N_TXTSIZE (execp
);
584 /* Data and bss are already filled in since they're so standard. */
586 /* The virtual memory addresses of the sections. */
587 obj_textsec (abfd
)->vma
= N_TXTADDR (execp
);
588 obj_datasec (abfd
)->vma
= N_DATADDR (execp
);
589 obj_bsssec (abfd
)->vma
= N_BSSADDR (execp
);
591 /* The file offsets of the sections. */
592 obj_textsec (abfd
)->filepos
= N_TXTOFF (execp
);
593 obj_datasec (abfd
)->filepos
= N_DATOFF (execp
);
595 /* The file offsets of the relocation info. */
596 obj_textsec (abfd
)->rel_filepos
= N_TRELOFF (execp
);
597 obj_datasec (abfd
)->rel_filepos
= N_DRELOFF (execp
);
599 /* The file offsets of the string table and symbol table. */
600 obj_str_filepos (abfd
) = N_STROFF (execp
);
601 obj_sym_filepos (abfd
) = N_SYMOFF (execp
);
603 /* Determine the architecture and machine type of the object file. */
604 abfd
->obj_arch
= bfd_arch_obscure
;
606 adata (abfd
)->page_size
= TARGET_PAGE_SIZE
;
607 adata (abfd
)->segment_size
= SEGMENT_SIZE
;
608 adata (abfd
)->exec_bytes_size
= EXEC_BYTES_SIZE
;
610 return _bfd_no_cleanup
;
612 /* The architecture is encoded in various ways in various a.out variants,
613 or is not encoded at all in some of them. The relocation size depends
614 on the architecture and the a.out variant. Finally, the return value
615 is the bfd_target vector in use. If an error occurs, return zero and
616 set bfd_error to the appropriate error code.
618 Formats such as b.out, which have additional fields in the a.out
619 header, should cope with them in this callback as well. */
620 #endif /* DOCUMENTATION */
622 result
= (*callback_to_real_object_p
) (abfd
);
624 /* Now that the segment addresses have been worked out, take a better
625 guess at whether the file is executable. If the entry point
626 is within the text segment, assume it is. (This makes files
627 executable even if their entry point address is 0, as long as
628 their text starts at zero.).
630 This test had to be changed to deal with systems where the text segment
631 runs at a different location than the default. The problem is that the
632 entry address can appear to be outside the text segment, thus causing an
633 erroneous conclusion that the file isn't executable.
635 To fix this, we now accept any non-zero entry point as an indication of
636 executability. This will work most of the time, since only the linker
637 sets the entry point, and that is likely to be non-zero for most systems. */
639 if (execp
->a_entry
!= 0
640 || (execp
->a_entry
>= obj_textsec (abfd
)->vma
641 && execp
->a_entry
< (obj_textsec (abfd
)->vma
642 + obj_textsec (abfd
)->size
)
643 && execp
->a_trsize
== 0
644 && execp
->a_drsize
== 0))
645 abfd
->flags
|= EXEC_P
;
649 struct stat stat_buf
;
651 /* The original heuristic doesn't work in some important cases.
652 The a.out file has no information about the text start
653 address. For files (like kernels) linked to non-standard
654 addresses (ld -Ttext nnn) the entry point may not be between
655 the default text start (obj_textsec(abfd)->vma) and
656 (obj_textsec(abfd)->vma) + text size. This is not just a mach
657 issue. Many kernels are loaded at non standard addresses. */
658 if (abfd
->iostream
!= NULL
659 && (abfd
->flags
& BFD_IN_MEMORY
) == 0
660 && (fstat (fileno ((FILE *) (abfd
->iostream
)), &stat_buf
) == 0)
661 && ((stat_buf
.st_mode
& 0111) != 0))
662 abfd
->flags
|= EXEC_P
;
664 #endif /* STAT_FOR_EXEC */
670 bfd_release (abfd
, rawptr
);
676 aout_@var{size}_mkobject
679 bool aout_@var{size}_mkobject, (bfd *abfd);
682 Initialize BFD @var{abfd} for use with a.out files.
686 NAME (aout
, mkobject
) (bfd
*abfd
)
688 struct aout_data_struct
*rawptr
;
689 size_t amt
= sizeof (* rawptr
);
691 bfd_set_error (bfd_error_system_call
);
693 rawptr
= (struct aout_data_struct
*) bfd_zalloc (abfd
, amt
);
697 abfd
->tdata
.aout_data
= rawptr
;
698 exec_hdr (abfd
) = &(rawptr
->e
);
700 obj_textsec (abfd
) = NULL
;
701 obj_datasec (abfd
) = NULL
;
702 obj_bsssec (abfd
) = NULL
;
709 aout_@var{size}_machine_type
712 enum machine_type aout_@var{size}_machine_type
713 (enum bfd_architecture arch,
714 unsigned long machine,
718 Keep track of machine architecture and machine type for
719 a.out's. Return the <<machine_type>> for a particular
720 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
721 and machine can't be represented in a.out format.
723 If the architecture is understood, machine type 0 (default)
724 is always understood.
728 NAME (aout
, machine_type
) (enum bfd_architecture arch
,
729 unsigned long machine
,
732 enum machine_type arch_flags
;
734 arch_flags
= M_UNKNOWN
;
741 || machine
== bfd_mach_sparc
742 || machine
== bfd_mach_sparc_sparclite
743 || machine
== bfd_mach_sparc_sparclite_le
744 || machine
== bfd_mach_sparc_v8plus
745 || machine
== bfd_mach_sparc_v8plusa
746 || machine
== bfd_mach_sparc_v8plusb
747 || machine
== bfd_mach_sparc_v8plusc
748 || machine
== bfd_mach_sparc_v8plusd
749 || machine
== bfd_mach_sparc_v8pluse
750 || machine
== bfd_mach_sparc_v8plusv
751 || machine
== bfd_mach_sparc_v8plusm
752 || machine
== bfd_mach_sparc_v8plusm8
753 || machine
== bfd_mach_sparc_v9
754 || machine
== bfd_mach_sparc_v9a
755 || machine
== bfd_mach_sparc_v9b
756 || machine
== bfd_mach_sparc_v9c
757 || machine
== bfd_mach_sparc_v9d
758 || machine
== bfd_mach_sparc_v9e
759 || machine
== bfd_mach_sparc_v9v
760 || machine
== bfd_mach_sparc_v9m
761 || machine
== bfd_mach_sparc_v9m8
)
762 arch_flags
= M_SPARC
;
763 else if (machine
== bfd_mach_sparc_sparclet
)
764 arch_flags
= M_SPARCLET
;
769 || machine
== bfd_mach_i386_i386
770 || machine
== bfd_mach_i386_i386_intel_syntax
)
783 case bfd_mach_mips3000
:
784 case bfd_mach_mips3900
:
785 arch_flags
= M_MIPS1
;
787 case bfd_mach_mips6000
:
788 arch_flags
= M_MIPS2
;
790 case bfd_mach_mips4000
:
791 case bfd_mach_mips4010
:
792 case bfd_mach_mips4100
:
793 case bfd_mach_mips4300
:
794 case bfd_mach_mips4400
:
795 case bfd_mach_mips4600
:
796 case bfd_mach_mips4650
:
797 case bfd_mach_mips8000
:
798 case bfd_mach_mips9000
:
799 case bfd_mach_mips10000
:
800 case bfd_mach_mips12000
:
801 case bfd_mach_mips14000
:
802 case bfd_mach_mips16000
:
803 case bfd_mach_mips16
:
804 case bfd_mach_mipsisa32
:
805 case bfd_mach_mipsisa32r2
:
806 case bfd_mach_mipsisa32r3
:
807 case bfd_mach_mipsisa32r5
:
808 case bfd_mach_mipsisa32r6
:
810 case bfd_mach_mipsisa64
:
811 case bfd_mach_mipsisa64r2
:
812 case bfd_mach_mipsisa64r3
:
813 case bfd_mach_mipsisa64r5
:
814 case bfd_mach_mipsisa64r6
:
815 case bfd_mach_mips_sb1
:
816 case bfd_mach_mips_xlr
:
817 /* FIXME: These should be MIPS3, MIPS4, MIPS16, MIPS32, etc. */
818 arch_flags
= M_MIPS2
;
821 arch_flags
= M_UNKNOWN
;
829 case 0: arch_flags
= M_NS32532
; break;
830 case 32032: arch_flags
= M_NS32032
; break;
831 case 32532: arch_flags
= M_NS32532
; break;
832 default: arch_flags
= M_UNKNOWN
; break;
841 if (machine
== 0 || machine
== 255)
846 arch_flags
= M_UNKNOWN
;
849 if (arch_flags
!= M_UNKNOWN
)
857 aout_@var{size}_set_arch_mach
860 bool aout_@var{size}_set_arch_mach,
862 enum bfd_architecture arch,
863 unsigned long machine);
866 Set the architecture and the machine of the BFD @var{abfd} to the
867 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
868 can support the architecture required.
872 NAME (aout
, set_arch_mach
) (bfd
*abfd
,
873 enum bfd_architecture arch
,
874 unsigned long machine
)
876 if (! bfd_default_set_arch_mach (abfd
, arch
, machine
))
879 if (arch
!= bfd_arch_unknown
)
883 NAME (aout
, machine_type
) (arch
, machine
, &unknown
);
888 /* Determine the size of a relocation entry. */
893 obj_reloc_entry_size (abfd
) = RELOC_EXT_SIZE
;
896 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
900 return (*aout_backend_info (abfd
)->set_sizes
) (abfd
);
904 adjust_o_magic (bfd
*abfd
, struct internal_exec
*execp
)
906 file_ptr pos
= adata (abfd
).exec_bytes_size
;
909 asection
*text
= obj_textsec (abfd
);
910 asection
*data
= obj_datasec (abfd
);
911 asection
*bss
= obj_bsssec (abfd
);
915 if (!text
->user_set_vma
)
920 pos
+= execp
->a_text
;
921 vma
+= execp
->a_text
;
924 if (!data
->user_set_vma
)
932 execp
->a_text
+= pad
;
939 if (!bss
->user_set_vma
)
947 /* The VMA of the .bss section is set by the VMA of the
948 .data section plus the size of the .data section. We may
949 need to add padding bytes to make this true. */
950 pad
= bss
->vma
- vma
;
955 execp
->a_data
= data
->size
+ pad
;
957 execp
->a_bss
= bss
->size
;
959 N_SET_MAGIC (execp
, OMAGIC
);
963 adjust_z_magic (bfd
*abfd
, struct internal_exec
*execp
)
965 bfd_size_type data_pad
, text_pad
;
967 const struct aout_backend_data
*abdp
;
968 /* TRUE if text includes exec header. */
970 asection
*text
= obj_textsec (abfd
);
971 asection
*data
= obj_datasec (abfd
);
972 asection
*bss
= obj_bsssec (abfd
);
974 abdp
= aout_backend_info (abfd
);
978 && (abdp
->text_includes_header
979 || obj_aout_subformat (abfd
) == q_magic_format
));
980 text
->filepos
= (ztih
981 ? adata (abfd
).exec_bytes_size
982 : adata (abfd
).zmagic_disk_block_size
);
983 if (!text
->user_set_vma
)
985 /* ?? Do we really need to check for relocs here? */
986 text
->vma
= ((abfd
->flags
& HAS_RELOC
)
989 ? abdp
->default_text_vma
+ adata (abfd
).exec_bytes_size
990 : abdp
->default_text_vma
));
995 /* The .text section is being loaded at an unusual address. We
996 may need to pad it such that the .data section starts at a page
999 text_pad
= ((text
->filepos
- text
->vma
)
1000 & (adata (abfd
).page_size
- 1));
1002 text_pad
= (-text
->vma
1003 & (adata (abfd
).page_size
- 1));
1006 /* Find start of data. */
1009 text_end
= text
->filepos
+ execp
->a_text
;
1010 text_pad
+= BFD_ALIGN (text_end
, adata (abfd
).page_size
) - text_end
;
1014 /* Note that if page_size == zmagic_disk_block_size, then
1015 filepos == page_size, and this case is the same as the ztih
1017 text_end
= execp
->a_text
;
1018 text_pad
+= BFD_ALIGN (text_end
, adata (abfd
).page_size
) - text_end
;
1019 text_end
+= text
->filepos
;
1021 execp
->a_text
+= text_pad
;
1024 if (!data
->user_set_vma
)
1027 vma
= text
->vma
+ execp
->a_text
;
1028 data
->vma
= BFD_ALIGN (vma
, adata (abfd
).segment_size
);
1030 if (abdp
&& abdp
->zmagic_mapped_contiguous
)
1032 text_pad
= data
->vma
- (text
->vma
+ execp
->a_text
);
1033 /* Only pad the text section if the data
1034 section is going to be placed after it. */
1036 execp
->a_text
+= text_pad
;
1038 data
->filepos
= text
->filepos
+ execp
->a_text
;
1040 /* Fix up exec header while we're at it. */
1041 if (ztih
&& (!abdp
|| (abdp
&& !abdp
->exec_header_not_counted
)))
1042 execp
->a_text
+= adata (abfd
).exec_bytes_size
;
1043 if (obj_aout_subformat (abfd
) == q_magic_format
)
1044 N_SET_QMAGIC (execp
);
1046 N_SET_MAGIC (execp
, ZMAGIC
);
1048 /* Spec says data section should be rounded up to page boundary. */
1049 execp
->a_data
= align_power (data
->size
, bss
->alignment_power
);
1050 execp
->a_data
= BFD_ALIGN (execp
->a_data
, adata (abfd
).page_size
);
1051 data_pad
= execp
->a_data
- data
->size
;
1054 if (!bss
->user_set_vma
)
1055 bss
->vma
= data
->vma
+ execp
->a_data
;
1056 /* If the BSS immediately follows the data section and extra space
1057 in the page is left after the data section, fudge data
1058 in the header so that the bss section looks smaller by that
1059 amount. We'll start the bss section there, and lie to the OS.
1060 (Note that a linker script, as well as the above assignment,
1061 could have explicitly set the BSS vma to immediately follow
1062 the data section.) */
1063 if (align_power (bss
->vma
, bss
->alignment_power
) == data
->vma
+ execp
->a_data
)
1064 execp
->a_bss
= data_pad
> bss
->size
? 0 : bss
->size
- data_pad
;
1066 execp
->a_bss
= bss
->size
;
1070 adjust_n_magic (bfd
*abfd
, struct internal_exec
*execp
)
1072 file_ptr pos
= adata (abfd
).exec_bytes_size
;
1075 asection
*text
= obj_textsec (abfd
);
1076 asection
*data
= obj_datasec (abfd
);
1077 asection
*bss
= obj_bsssec (abfd
);
1080 text
->filepos
= pos
;
1081 if (!text
->user_set_vma
)
1085 pos
+= execp
->a_text
;
1086 vma
+= execp
->a_text
;
1089 data
->filepos
= pos
;
1090 if (!data
->user_set_vma
)
1091 data
->vma
= BFD_ALIGN (vma
, adata (abfd
).segment_size
);
1094 /* Since BSS follows data immediately, see if it needs alignment. */
1096 pad
= align_power (vma
, bss
->alignment_power
) - vma
;
1097 execp
->a_data
= data
->size
+ pad
;
1098 pos
+= execp
->a_data
;
1101 if (!bss
->user_set_vma
)
1106 /* Fix up exec header. */
1107 execp
->a_bss
= bss
->size
;
1108 N_SET_MAGIC (execp
, NMAGIC
);
1112 NAME (aout
, adjust_sizes_and_vmas
) (bfd
*abfd
)
1114 struct internal_exec
*execp
= exec_hdr (abfd
);
1116 if (! NAME (aout
, make_sections
) (abfd
))
1119 if (adata (abfd
).magic
!= undecided_magic
)
1122 execp
->a_text
= align_power (obj_textsec (abfd
)->size
,
1123 obj_textsec (abfd
)->alignment_power
);
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
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
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
;
1147 adata (abfd
).magic
= o_magic
;
1149 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1151 fprintf (stderr
, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
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;
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
);
1171 switch (adata (abfd
).magic
)
1174 adjust_o_magic (abfd
, execp
);
1177 adjust_z_magic (abfd
, execp
);
1180 adjust_n_magic (abfd
, execp
);
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
, execp
->a_text
,
1189 obj_textsec (abfd
)->filepos
,
1190 obj_datasec (abfd
)->vma
, execp
->a_data
,
1191 obj_datasec (abfd
)->filepos
,
1192 obj_bsssec (abfd
)->vma
, execp
->a_bss
);
1200 aout_@var{size}_new_section_hook
1203 bool aout_@var{size}_new_section_hook,
1208 Called by the BFD in response to a @code{bfd_make_section}
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
);
1241 NAME (aout
, set_section_contents
) (bfd
*abfd
,
1243 const void * location
,
1245 bfd_size_type count
)
1247 if (! abfd
->output_has_begun
)
1249 if (! NAME (aout
, adjust_sizes_and_vmas
) (abfd
))
1253 if (section
== obj_bsssec (abfd
))
1255 bfd_set_error (bfd_error_no_contents
);
1259 if (section
!= obj_textsec (abfd
)
1260 && section
!= obj_datasec (abfd
))
1262 if (aout_section_merge_with_text_p (abfd
, section
))
1263 section
->filepos
= obj_textsec (abfd
)->filepos
+
1264 (section
->vma
- obj_textsec (abfd
)->vma
);
1268 /* xgettext:c-format */
1269 (_("%pB: can not represent section `%pA' in a.out object file format"),
1271 bfd_set_error (bfd_error_nonrepresentable_section
);
1278 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
1279 || bfd_write (location
, count
, abfd
) != count
)
1286 /* Read the external symbols from an a.out file. */
1289 aout_get_external_symbols (bfd
*abfd
)
1291 if (obj_aout_external_syms (abfd
) == NULL
)
1293 bfd_size_type count
;
1294 struct external_nlist
*syms
= NULL
;
1295 bfd_size_type amt
= exec_hdr (abfd
)->a_syms
;
1297 count
= amt
/ EXTERNAL_NLIST_SIZE
;
1301 /* We allocate using malloc to make the values easy to free
1302 later on. If we put them on the objalloc it might not be
1303 possible to free them. */
1304 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1306 syms
= _bfd_malloc_and_read (abfd
, amt
, amt
);
1310 obj_aout_external_syms (abfd
) = syms
;
1311 obj_aout_external_sym_count (abfd
) = count
;
1314 if (obj_aout_external_strings (abfd
) == NULL
1315 && exec_hdr (abfd
)->a_syms
!= 0)
1317 unsigned char string_chars
[BYTES_IN_WORD
];
1318 bfd_size_type stringsize
;
1320 bfd_size_type amt
= BYTES_IN_WORD
;
1322 /* Get the size of the strings. */
1323 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0
1324 || bfd_read (string_chars
, amt
, abfd
) != amt
)
1326 stringsize
= GET_WORD (abfd
, string_chars
);
1327 if (stringsize
== 0)
1329 else if (stringsize
+ 1 < BYTES_IN_WORD
+ 1
1330 || (size_t) stringsize
!= stringsize
)
1332 bfd_set_error (bfd_error_bad_value
);
1336 strings
= (char *) bfd_malloc (stringsize
+ 1);
1337 if (strings
== NULL
)
1340 if (stringsize
>= BYTES_IN_WORD
)
1342 amt
= stringsize
- BYTES_IN_WORD
;
1343 if (bfd_read (strings
+ BYTES_IN_WORD
, amt
, abfd
) != amt
)
1350 /* Ensure that a zero index yields an empty string. */
1351 if (stringsize
>= BYTES_IN_WORD
)
1352 memset (strings
, 0, BYTES_IN_WORD
);
1354 /* Ensure that the string buffer is NUL terminated. */
1355 strings
[stringsize
] = 0;
1357 obj_aout_external_strings (abfd
) = strings
;
1358 obj_aout_external_string_size (abfd
) = stringsize
;
1364 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1365 and symbol->value fields of CACHE_PTR will be set from the a.out
1366 nlist structure. This function is responsible for setting
1367 symbol->flags and symbol->section, and adjusting symbol->value. */
1370 translate_from_native_sym_flags (bfd
*abfd
, aout_symbol_type
*cache_ptr
)
1374 if ((cache_ptr
->type
& N_STAB
) != 0
1375 || cache_ptr
->type
== N_FN
)
1379 /* This is a debugging symbol. */
1380 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
;
1382 /* Work out the symbol section. */
1383 switch (cache_ptr
->type
& N_TYPE
)
1387 sec
= obj_textsec (abfd
);
1390 sec
= obj_datasec (abfd
);
1393 sec
= obj_bsssec (abfd
);
1397 sec
= bfd_abs_section_ptr
;
1401 cache_ptr
->symbol
.section
= sec
;
1402 cache_ptr
->symbol
.value
-= sec
->vma
;
1407 /* Get the default visibility. This does not apply to all types, so
1408 we just hold it in a local variable to use if wanted. */
1409 if ((cache_ptr
->type
& N_EXT
) == 0)
1410 visible
= BSF_LOCAL
;
1412 visible
= BSF_GLOBAL
;
1414 switch (cache_ptr
->type
)
1417 case N_ABS
: case N_ABS
| N_EXT
:
1418 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1419 cache_ptr
->symbol
.flags
= visible
;
1422 case N_UNDF
| N_EXT
:
1423 if (cache_ptr
->symbol
.value
!= 0)
1425 /* This is a common symbol. */
1426 cache_ptr
->symbol
.flags
= BSF_GLOBAL
;
1427 cache_ptr
->symbol
.section
= bfd_com_section_ptr
;
1431 cache_ptr
->symbol
.flags
= 0;
1432 cache_ptr
->symbol
.section
= bfd_und_section_ptr
;
1436 case N_TEXT
: case N_TEXT
| N_EXT
:
1437 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1438 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1439 cache_ptr
->symbol
.flags
= visible
;
1442 /* N_SETV symbols used to represent set vectors placed in the
1443 data section. They are no longer generated. Theoretically,
1444 it was possible to extract the entries and combine them with
1445 new ones, although I don't know if that was ever actually
1446 done. Unless that feature is restored, treat them as data
1448 case N_SETV
: case N_SETV
| N_EXT
:
1449 case N_DATA
: case N_DATA
| N_EXT
:
1450 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1451 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1452 cache_ptr
->symbol
.flags
= visible
;
1455 case N_BSS
: case N_BSS
| N_EXT
:
1456 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1457 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1458 cache_ptr
->symbol
.flags
= visible
;
1461 case N_SETA
: case N_SETA
| N_EXT
:
1462 case N_SETT
: case N_SETT
| N_EXT
:
1463 case N_SETD
: case N_SETD
| N_EXT
:
1464 case N_SETB
: case N_SETB
| N_EXT
:
1466 /* This code is no longer needed. It used to be used to make
1467 the linker handle set symbols, but they are now handled in
1468 the add_symbols routine instead. */
1469 switch (cache_ptr
->type
& N_TYPE
)
1472 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1475 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1478 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1481 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1485 cache_ptr
->symbol
.flags
|= BSF_CONSTRUCTOR
;
1490 /* This symbol is the text of a warning message. The next
1491 symbol is the symbol to associate the warning with. If a
1492 reference is made to that symbol, a warning is issued. */
1493 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_WARNING
;
1494 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1497 case N_INDR
: case N_INDR
| N_EXT
:
1498 /* An indirect symbol. This consists of two symbols in a row.
1499 The first symbol is the name of the indirection. The second
1500 symbol is the name of the target. A reference to the first
1501 symbol becomes a reference to the second. */
1502 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_INDIRECT
| visible
;
1503 cache_ptr
->symbol
.section
= bfd_ind_section_ptr
;
1507 cache_ptr
->symbol
.section
= bfd_und_section_ptr
;
1508 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1512 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1513 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1517 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1518 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1519 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1523 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1524 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1525 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1529 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1530 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1531 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1538 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1541 translate_to_native_sym_flags (bfd
*abfd
,
1543 struct external_nlist
*sym_pointer
)
1545 bfd_vma value
= cache_ptr
->value
;
1549 /* Mask out any existing type bits in case copying from one section
1551 sym_pointer
->e_type
[0] &= ~N_TYPE
;
1553 sec
= bfd_asymbol_section (cache_ptr
);
1558 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1561 /* xgettext:c-format */
1562 (_("%pB: can not represent section for symbol `%s' in a.out "
1563 "object file format"),
1565 cache_ptr
->name
!= NULL
? cache_ptr
->name
: _("*unknown*"));
1566 bfd_set_error (bfd_error_nonrepresentable_section
);
1570 if (sec
->output_section
!= NULL
)
1572 off
= sec
->output_offset
;
1573 sec
= sec
->output_section
;
1576 if (bfd_is_abs_section (sec
))
1577 sym_pointer
->e_type
[0] |= N_ABS
;
1578 else if (sec
== obj_textsec (abfd
))
1579 sym_pointer
->e_type
[0] |= N_TEXT
;
1580 else if (sec
== obj_datasec (abfd
))
1581 sym_pointer
->e_type
[0] |= N_DATA
;
1582 else if (sec
== obj_bsssec (abfd
))
1583 sym_pointer
->e_type
[0] |= N_BSS
;
1584 else if (bfd_is_und_section (sec
))
1585 sym_pointer
->e_type
[0] = N_UNDF
| N_EXT
;
1586 else if (bfd_is_ind_section (sec
))
1587 sym_pointer
->e_type
[0] = N_INDR
;
1588 else if (bfd_is_com_section (sec
))
1589 sym_pointer
->e_type
[0] = N_UNDF
| N_EXT
;
1592 if (aout_section_merge_with_text_p (abfd
, sec
))
1593 sym_pointer
->e_type
[0] |= N_TEXT
;
1597 /* xgettext:c-format */
1598 (_("%pB: can not represent section `%pA' in a.out object file format"),
1600 bfd_set_error (bfd_error_nonrepresentable_section
);
1605 /* Turn the symbol from section relative to absolute again. */
1606 value
+= sec
->vma
+ off
;
1608 if ((cache_ptr
->flags
& BSF_WARNING
) != 0)
1609 sym_pointer
->e_type
[0] = N_WARNING
;
1611 if ((cache_ptr
->flags
& BSF_DEBUGGING
) != 0)
1612 sym_pointer
->e_type
[0] = ((aout_symbol_type
*) cache_ptr
)->type
;
1613 else if ((cache_ptr
->flags
& BSF_GLOBAL
) != 0)
1614 sym_pointer
->e_type
[0] |= N_EXT
;
1615 else if ((cache_ptr
->flags
& BSF_LOCAL
) != 0)
1616 sym_pointer
->e_type
[0] &= ~N_EXT
;
1618 if ((cache_ptr
->flags
& BSF_CONSTRUCTOR
) != 0)
1620 int type
= ((aout_symbol_type
*) cache_ptr
)->type
;
1624 case N_ABS
: type
= N_SETA
; break;
1625 case N_TEXT
: type
= N_SETT
; break;
1626 case N_DATA
: type
= N_SETD
; break;
1627 case N_BSS
: type
= N_SETB
; break;
1629 sym_pointer
->e_type
[0] = type
;
1632 if ((cache_ptr
->flags
& BSF_WEAK
) != 0)
1636 switch (sym_pointer
->e_type
[0] & N_TYPE
)
1639 case N_ABS
: type
= N_WEAKA
; break;
1640 case N_TEXT
: type
= N_WEAKT
; break;
1641 case N_DATA
: type
= N_WEAKD
; break;
1642 case N_BSS
: type
= N_WEAKB
; break;
1643 case N_UNDF
: type
= N_WEAKU
; break;
1645 sym_pointer
->e_type
[0] = type
;
1648 PUT_WORD (abfd
, value
, sym_pointer
->e_value
);
1653 /* Native-level interface to symbols. */
1656 NAME (aout
, make_empty_symbol
) (bfd
*abfd
)
1658 size_t amt
= sizeof (aout_symbol_type
);
1660 aout_symbol_type
*new_symbol
= (aout_symbol_type
*) bfd_zalloc (abfd
, amt
);
1663 new_symbol
->symbol
.the_bfd
= abfd
;
1665 return &new_symbol
->symbol
;
1668 /* Translate a set of external symbols into internal symbols. */
1671 NAME (aout
, translate_symbol_table
) (bfd
*abfd
,
1672 aout_symbol_type
*in
,
1673 struct external_nlist
*ext
,
1674 bfd_size_type count
,
1676 bfd_size_type strsize
,
1679 struct external_nlist
*ext_end
;
1681 ext_end
= ext
+ count
;
1682 for (; ext
< ext_end
; ext
++, in
++)
1686 x
= GET_WORD (abfd
, ext
->e_strx
);
1687 in
->symbol
.the_bfd
= abfd
;
1689 /* For the normal symbols, the zero index points at the number
1690 of bytes in the string table but is to be interpreted as the
1691 null string. For the dynamic symbols, the number of bytes in
1692 the string table is stored in the __DYNAMIC structure and the
1693 zero index points at an actual string. */
1694 if (x
== 0 && ! dynamic
)
1695 in
->symbol
.name
= "";
1696 else if (x
< strsize
)
1697 in
->symbol
.name
= str
+ x
;
1701 (_("%pB: invalid string offset %" PRIu64
" >= %" PRIu64
),
1702 abfd
, (uint64_t) x
, (uint64_t) strsize
);
1703 bfd_set_error (bfd_error_bad_value
);
1707 in
->symbol
.value
= GET_SWORD (abfd
, ext
->e_value
);
1708 in
->desc
= H_GET_16 (abfd
, ext
->e_desc
);
1709 in
->other
= H_GET_8 (abfd
, ext
->e_other
);
1710 in
->type
= H_GET_8 (abfd
, ext
->e_type
);
1711 in
->symbol
.udata
.p
= NULL
;
1713 if (! translate_from_native_sym_flags (abfd
, in
))
1717 in
->symbol
.flags
|= BSF_DYNAMIC
;
1723 /* We read the symbols into a buffer, which is discarded when this
1724 function exits. We read the strings into a buffer large enough to
1725 hold them all plus all the cached symbol entries. */
1728 NAME (aout
, slurp_symbol_table
) (bfd
*abfd
)
1730 struct external_nlist
*old_external_syms
;
1731 aout_symbol_type
*cached
;
1732 bfd_size_type cached_size
;
1734 /* If there's no work to be done, don't do any. */
1735 if (obj_aout_symbols (abfd
) != NULL
)
1738 old_external_syms
= obj_aout_external_syms (abfd
);
1740 if (! aout_get_external_symbols (abfd
))
1743 cached_size
= obj_aout_external_sym_count (abfd
);
1744 if (cached_size
== 0)
1745 return true; /* Nothing to do. */
1747 cached_size
*= sizeof (aout_symbol_type
);
1748 cached
= (aout_symbol_type
*) bfd_zmalloc (cached_size
);
1752 /* Convert from external symbol information to internal. */
1753 if (! (NAME (aout
, translate_symbol_table
)
1755 obj_aout_external_syms (abfd
),
1756 obj_aout_external_sym_count (abfd
),
1757 obj_aout_external_strings (abfd
),
1758 obj_aout_external_string_size (abfd
),
1765 abfd
->symcount
= obj_aout_external_sym_count (abfd
);
1767 obj_aout_symbols (abfd
) = cached
;
1769 /* It is very likely that anybody who calls this function will not
1770 want the external symbol information, so if it was allocated
1771 because of our call to aout_get_external_symbols, we free it up
1772 right away to save space. */
1773 if (old_external_syms
== NULL
1774 && obj_aout_external_syms (abfd
) != NULL
)
1776 free (obj_aout_external_syms (abfd
));
1777 obj_aout_external_syms (abfd
) = NULL
;
1783 /* We use a hash table when writing out symbols so that we only write
1784 out a particular string once. This helps particularly when the
1785 linker writes out stabs debugging entries, because each different
1786 contributing object file tends to have many duplicate stabs
1789 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1790 if BFD_TRADITIONAL_FORMAT is set. */
1792 /* Get the index of a string in a strtab, adding it if it is not
1795 static inline bfd_size_type
1796 add_to_stringtab (bfd
*abfd
,
1797 struct bfd_strtab_hash
*tab
,
1802 bfd_size_type str_index
;
1804 /* An index of 0 always means the empty string. */
1805 if (str
== 0 || *str
== '\0')
1808 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1809 doesn't understand a hashed string table. */
1811 if ((abfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1814 str_index
= _bfd_stringtab_add (tab
, str
, hash
, copy
);
1816 if (str_index
!= (bfd_size_type
) -1)
1817 /* Add BYTES_IN_WORD to the return value to account for the
1818 space taken up by the string table size. */
1819 str_index
+= BYTES_IN_WORD
;
1824 /* Write out a strtab. ABFD is already at the right location in the
1828 emit_stringtab (bfd
*abfd
, struct bfd_strtab_hash
*tab
)
1830 bfd_byte buffer
[BYTES_IN_WORD
];
1831 size_t amt
= BYTES_IN_WORD
;
1833 /* The string table starts with the size. */
1834 PUT_WORD (abfd
, _bfd_stringtab_size (tab
) + BYTES_IN_WORD
, buffer
);
1835 if (bfd_write (buffer
, amt
, abfd
) != amt
)
1838 return _bfd_stringtab_emit (abfd
, tab
);
1842 NAME (aout
, write_syms
) (bfd
*abfd
)
1844 unsigned int count
;
1845 asymbol
**generic
= bfd_get_outsymbols (abfd
);
1846 struct bfd_strtab_hash
*strtab
;
1848 strtab
= _bfd_stringtab_init ();
1852 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
1854 asymbol
*g
= generic
[count
];
1856 struct external_nlist nsp
;
1859 indx
= add_to_stringtab (abfd
, strtab
, g
->name
, false);
1860 if (indx
== (bfd_size_type
) -1)
1862 PUT_WORD (abfd
, indx
, (bfd_byte
*) nsp
.e_strx
);
1864 if (bfd_asymbol_flavour (g
) == abfd
->xvec
->flavour
)
1866 H_PUT_16 (abfd
, aout_symbol (g
)->desc
, nsp
.e_desc
);
1867 H_PUT_8 (abfd
, aout_symbol (g
)->other
, nsp
.e_other
);
1868 H_PUT_8 (abfd
, aout_symbol (g
)->type
, nsp
.e_type
);
1872 H_PUT_16 (abfd
, 0, nsp
.e_desc
);
1873 H_PUT_8 (abfd
, 0, nsp
.e_other
);
1874 H_PUT_8 (abfd
, 0, nsp
.e_type
);
1877 if (! translate_to_native_sym_flags (abfd
, g
, &nsp
))
1880 amt
= EXTERNAL_NLIST_SIZE
;
1881 if (bfd_write (&nsp
, amt
, abfd
) != amt
)
1884 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1885 here, at the end. */
1889 if (! emit_stringtab (abfd
, strtab
))
1892 _bfd_stringtab_free (strtab
);
1897 _bfd_stringtab_free (strtab
);
1902 NAME (aout
, canonicalize_symtab
) (bfd
*abfd
, asymbol
**location
)
1904 unsigned int counter
= 0;
1905 aout_symbol_type
*symbase
;
1907 if (!NAME (aout
, slurp_symbol_table
) (abfd
))
1910 for (symbase
= obj_aout_symbols (abfd
);
1911 counter
++ < bfd_get_symcount (abfd
);
1913 *(location
++) = (asymbol
*) (symbase
++);
1915 return bfd_get_symcount (abfd
);
1918 /* Standard reloc stuff. */
1919 /* Output standard relocation information to a file in target byte order. */
1921 extern void NAME (aout
, swap_std_reloc_out
)
1922 (bfd
*, arelent
*, struct reloc_std_external
*);
1925 NAME (aout
, swap_std_reloc_out
) (bfd
*abfd
,
1927 struct reloc_std_external
*natptr
)
1930 asymbol
*sym
= *(g
->sym_ptr_ptr
);
1932 unsigned int r_length
, r_size
;
1934 int r_baserel
, r_jmptable
, r_relative
;
1935 asection
*output_section
= sym
->section
->output_section
;
1937 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
1939 BFD_ASSERT (g
->howto
!= NULL
);
1941 r_size
= bfd_get_reloc_size (g
->howto
);
1942 r_length
= bfd_log2 (r_size
);
1943 if (1u << r_length
!= r_size
)
1945 _bfd_error_handler (_("%pB: unsupported AOUT relocation size: %d"),
1947 bfd_set_error (bfd_error_bad_value
);
1951 r_pcrel
= (int) g
->howto
->pc_relative
; /* Relative to PC? */
1952 /* XXX This relies on relocs coming from a.out files. */
1953 r_baserel
= (g
->howto
->type
& 8) != 0;
1954 r_jmptable
= (g
->howto
->type
& 16) != 0;
1955 r_relative
= (g
->howto
->type
& 32) != 0;
1957 /* Name was clobbered by aout_write_syms to be symbol index. */
1959 /* If this relocation is relative to a symbol then set the
1960 r_index to the symbols index, and the r_extern bit.
1962 Absolute symbols can come in in two ways, either as an offset
1963 from the abs section, or as a symbol which has an abs value.
1964 check for that here. */
1966 if (bfd_is_com_section (output_section
)
1967 || bfd_is_abs_section (output_section
)
1968 || bfd_is_und_section (output_section
)
1969 /* PR gas/3041 a.out relocs against weak symbols
1970 must be treated as if they were against externs. */
1971 || (sym
->flags
& BSF_WEAK
))
1973 if (bfd_abs_section_ptr
->symbol
== sym
)
1975 /* Whoops, looked like an abs symbol, but is
1976 really an offset from the abs section. */
1982 /* Fill in symbol. */
1984 r_index
= (*(g
->sym_ptr_ptr
))->KEEPIT
;
1989 /* Just an ordinary section. */
1991 r_index
= output_section
->target_index
;
1994 /* Now the fun stuff. */
1995 if (bfd_header_big_endian (abfd
))
1997 natptr
->r_index
[0] = r_index
>> 16;
1998 natptr
->r_index
[1] = r_index
>> 8;
1999 natptr
->r_index
[2] = r_index
;
2000 natptr
->r_type
[0] = ((r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
2001 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
2002 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
2003 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
2004 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
2005 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
2009 natptr
->r_index
[2] = r_index
>> 16;
2010 natptr
->r_index
[1] = r_index
>> 8;
2011 natptr
->r_index
[0] = r_index
;
2012 natptr
->r_type
[0] = ((r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
2013 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
2014 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
2015 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
2016 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
2017 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
2021 /* Extended stuff. */
2022 /* Output extended relocation information to a file in target byte order. */
2024 extern void NAME (aout
, swap_ext_reloc_out
)
2025 (bfd
*, arelent
*, struct reloc_ext_external
*);
2028 NAME (aout
, swap_ext_reloc_out
) (bfd
*abfd
,
2030 struct reloc_ext_external
*natptr
)
2034 unsigned int r_type
;
2036 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2037 asection
*output_section
= sym
->section
->output_section
;
2039 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
2041 r_type
= (unsigned int) g
->howto
->type
;
2043 r_addend
= g
->addend
;
2044 if ((sym
->flags
& BSF_SECTION_SYM
) != 0)
2045 r_addend
+= (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2047 /* If this relocation is relative to a symbol then set the
2048 r_index to the symbols index, and the r_extern bit.
2050 Absolute symbols can come in in two ways, either as an offset
2051 from the abs section, or as a symbol which has an abs value.
2052 check for that here. */
2053 if (bfd_is_abs_section (bfd_asymbol_section (sym
)))
2058 else if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
2060 if (bfd_is_und_section (bfd_asymbol_section (sym
))
2061 || (sym
->flags
& BSF_GLOBAL
) != 0)
2065 r_index
= (*(g
->sym_ptr_ptr
))->KEEPIT
;
2069 /* Just an ordinary section. */
2071 r_index
= output_section
->target_index
;
2074 /* Now the fun stuff. */
2075 if (bfd_header_big_endian (abfd
))
2077 natptr
->r_index
[0] = r_index
>> 16;
2078 natptr
->r_index
[1] = r_index
>> 8;
2079 natptr
->r_index
[2] = r_index
;
2080 natptr
->r_type
[0] = ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
2081 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
2085 natptr
->r_index
[2] = r_index
>> 16;
2086 natptr
->r_index
[1] = r_index
>> 8;
2087 natptr
->r_index
[0] = r_index
;
2088 natptr
->r_type
[0] = ((r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
2089 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
));
2092 PUT_WORD (abfd
, r_addend
, natptr
->r_addend
);
2095 /* BFD deals internally with all things based from the section they're
2096 in. so, something in 10 bytes into a text section with a base of
2097 50 would have a symbol (.text+10) and know .text vma was 50.
2099 Aout keeps all it's symbols based from zero, so the symbol would
2100 contain 60. This macro subs the base of each section from the value
2101 to give the true offset from the section. */
2103 #define MOVE_ADDRESS(ad) \
2106 /* Undefined symbol. */ \
2107 if (symbols != NULL && r_index < bfd_get_symcount (abfd)) \
2108 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2110 cache_ptr->sym_ptr_ptr = &bfd_abs_section_ptr->symbol; \
2111 cache_ptr->addend = ad; \
2115 /* Defined, section relative. Replace symbol with pointer to \
2116 symbol which points to section. */ \
2120 case N_TEXT | N_EXT: \
2121 cache_ptr->sym_ptr_ptr = &obj_textsec (abfd)->symbol; \
2122 cache_ptr->addend = ad - su->textsec->vma; \
2125 case N_DATA | N_EXT: \
2126 cache_ptr->sym_ptr_ptr = &obj_datasec (abfd)->symbol; \
2127 cache_ptr->addend = ad - su->datasec->vma; \
2130 case N_BSS | N_EXT: \
2131 cache_ptr->sym_ptr_ptr = &obj_bsssec (abfd)->symbol; \
2132 cache_ptr->addend = ad - su->bsssec->vma; \
2136 case N_ABS | N_EXT: \
2137 cache_ptr->sym_ptr_ptr = &bfd_abs_section_ptr->symbol; \
2138 cache_ptr->addend = ad; \
2144 NAME (aout
, swap_ext_reloc_in
) (bfd
*abfd
,
2145 struct reloc_ext_external
*bytes
,
2148 bfd_size_type symcount
)
2150 unsigned int r_index
;
2152 unsigned int r_type
;
2153 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2155 cache_ptr
->address
= (GET_SWORD (abfd
, bytes
->r_address
));
2157 /* Now the fun stuff. */
2158 if (bfd_header_big_endian (abfd
))
2160 r_index
= (((unsigned int) bytes
->r_index
[0] << 16)
2161 | ((unsigned int) bytes
->r_index
[1] << 8)
2162 | bytes
->r_index
[2]);
2163 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
2164 r_type
= ((bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
2165 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
2169 r_index
= (((unsigned int) bytes
->r_index
[2] << 16)
2170 | ((unsigned int) bytes
->r_index
[1] << 8)
2171 | bytes
->r_index
[0]);
2172 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
2173 r_type
= ((bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
2174 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
2177 if (r_type
< TABLE_SIZE (howto_table_ext
))
2178 cache_ptr
->howto
= howto_table_ext
+ r_type
;
2180 cache_ptr
->howto
= NULL
;
2182 /* Base relative relocs are always against the symbol table,
2183 regardless of the setting of r_extern. r_extern just reflects
2184 whether the symbol the reloc is against is local or global. */
2185 if (r_type
== (unsigned int) RELOC_BASE10
2186 || r_type
== (unsigned int) RELOC_BASE13
2187 || r_type
== (unsigned int) RELOC_BASE22
)
2190 if (r_extern
&& r_index
> symcount
)
2192 /* We could arrange to return an error, but it might be useful
2193 to see the file even if it is bad. */
2198 MOVE_ADDRESS (GET_SWORD (abfd
, bytes
->r_addend
));
2202 NAME (aout
, swap_std_reloc_in
) (bfd
*abfd
,
2203 struct reloc_std_external
*bytes
,
2206 bfd_size_type symcount
)
2208 unsigned int r_index
;
2210 unsigned int r_length
;
2212 int r_baserel
, r_jmptable
, r_relative
;
2213 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2214 unsigned int howto_idx
;
2216 cache_ptr
->address
= H_GET_32 (abfd
, bytes
->r_address
);
2218 /* Now the fun stuff. */
2219 if (bfd_header_big_endian (abfd
))
2221 r_index
= (((unsigned int) bytes
->r_index
[0] << 16)
2222 | ((unsigned int) bytes
->r_index
[1] << 8)
2223 | bytes
->r_index
[2]);
2224 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
2225 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
2226 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
2227 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
2228 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
2229 r_length
= ((bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
2230 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
2234 r_index
= (((unsigned int) bytes
->r_index
[2] << 16)
2235 | ((unsigned int) bytes
->r_index
[1] << 8)
2236 | bytes
->r_index
[0]);
2237 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
2238 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
2239 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
2240 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
2241 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
2242 r_length
= ((bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
2243 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
2246 howto_idx
= (r_length
+ 4 * r_pcrel
+ 8 * r_baserel
2247 + 16 * r_jmptable
+ 32 * r_relative
);
2248 if (howto_idx
< TABLE_SIZE (howto_table_std
))
2250 cache_ptr
->howto
= howto_table_std
+ howto_idx
;
2251 if (cache_ptr
->howto
->type
== (unsigned int) -1)
2252 cache_ptr
->howto
= NULL
;
2255 cache_ptr
->howto
= NULL
;
2257 /* Base relative relocs are always against the symbol table,
2258 regardless of the setting of r_extern. r_extern just reflects
2259 whether the symbol the reloc is against is local or global. */
2263 if (r_extern
&& r_index
>= symcount
)
2265 /* We could arrange to return an error, but it might be useful
2266 to see the file even if it is bad. FIXME: Of course this
2267 means that objdump -r *doesn't* see the actual reloc, and
2268 objcopy silently writes a different reloc. */
2276 /* Read and swap the relocs for a section. */
2279 NAME (aout
, slurp_reloc_table
) (bfd
*abfd
, sec_ptr asect
, asymbol
**symbols
)
2281 bfd_size_type count
;
2282 bfd_size_type reloc_size
;
2284 arelent
*reloc_cache
;
2286 unsigned int counter
= 0;
2290 if (asect
->relocation
)
2293 if (asect
->flags
& SEC_CONSTRUCTOR
)
2296 if (asect
== obj_datasec (abfd
))
2297 reloc_size
= exec_hdr (abfd
)->a_drsize
;
2298 else if (asect
== obj_textsec (abfd
))
2299 reloc_size
= exec_hdr (abfd
)->a_trsize
;
2300 else if (asect
== obj_bsssec (abfd
))
2304 bfd_set_error (bfd_error_invalid_operation
);
2308 each_size
= obj_reloc_entry_size (abfd
);
2309 count
= reloc_size
/ each_size
;
2311 return true; /* Nothing to be done. */
2313 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
2315 relocs
= _bfd_malloc_and_read (abfd
, reloc_size
, reloc_size
);
2319 amt
= count
* sizeof (arelent
);
2320 reloc_cache
= (arelent
*) bfd_zmalloc (amt
);
2321 if (reloc_cache
== NULL
)
2327 cache_ptr
= reloc_cache
;
2328 if (each_size
== RELOC_EXT_SIZE
)
2330 struct reloc_ext_external
*rptr
= (struct reloc_ext_external
*) relocs
;
2332 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2333 MY_swap_ext_reloc_in (abfd
, rptr
, cache_ptr
, symbols
,
2334 (bfd_size_type
) bfd_get_symcount (abfd
));
2338 struct reloc_std_external
*rptr
= (struct reloc_std_external
*) relocs
;
2340 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2341 MY_swap_std_reloc_in (abfd
, rptr
, cache_ptr
, symbols
,
2342 (bfd_size_type
) bfd_get_symcount (abfd
));
2347 asect
->relocation
= reloc_cache
;
2348 asect
->reloc_count
= cache_ptr
- reloc_cache
;
2353 /* Write out a relocation section into an object file. */
2356 NAME (aout
, squirt_out_relocs
) (bfd
*abfd
, asection
*section
)
2359 unsigned char *native
, *natptr
;
2362 unsigned int count
= section
->reloc_count
;
2363 bfd_size_type natsize
;
2365 if (count
== 0 || section
->orelocation
== NULL
)
2368 each_size
= obj_reloc_entry_size (abfd
);
2369 natsize
= (bfd_size_type
) each_size
* count
;
2370 native
= (unsigned char *) bfd_zalloc (abfd
, natsize
);
2374 generic
= section
->orelocation
;
2376 if (each_size
== RELOC_EXT_SIZE
)
2378 for (natptr
= native
;
2380 --count
, natptr
+= each_size
, ++generic
)
2382 /* PR 20921: If the howto field has not been initialised then skip
2384 PR 20929: Similarly for the symbol field. */
2385 if ((*generic
)->howto
== NULL
2386 || (*generic
)->sym_ptr_ptr
== NULL
)
2388 bfd_set_error (bfd_error_invalid_operation
);
2389 _bfd_error_handler (_("%pB: attempt to write out "
2390 "unknown reloc type"), abfd
);
2393 MY_swap_ext_reloc_out (abfd
, *generic
,
2394 (struct reloc_ext_external
*) natptr
);
2399 for (natptr
= native
;
2401 --count
, natptr
+= each_size
, ++generic
)
2403 if ((*generic
)->howto
== NULL
2404 || (*generic
)->sym_ptr_ptr
== NULL
)
2406 bfd_set_error (bfd_error_invalid_operation
);
2407 _bfd_error_handler (_("%pB: attempt to write out "
2408 "unknown reloc type"), abfd
);
2411 MY_swap_std_reloc_out (abfd
, *generic
,
2412 (struct reloc_std_external
*) natptr
);
2416 if (bfd_write (native
, natsize
, abfd
) != natsize
)
2418 bfd_release (abfd
, native
);
2421 bfd_release (abfd
, native
);
2426 /* This is stupid. This function should be a boolean predicate. */
2429 NAME (aout
, canonicalize_reloc
) (bfd
*abfd
,
2434 arelent
*tblptr
= section
->relocation
;
2437 if (section
== obj_bsssec (abfd
))
2443 if (!(tblptr
|| NAME (aout
, slurp_reloc_table
) (abfd
, section
, symbols
)))
2446 if (section
->flags
& SEC_CONSTRUCTOR
)
2448 arelent_chain
*chain
= section
->constructor_chain
;
2449 for (count
= 0; count
< section
->reloc_count
; count
++)
2451 *relptr
++ = &chain
->relent
;
2452 chain
= chain
->next
;
2457 tblptr
= section
->relocation
;
2459 for (count
= 0; count
++ < section
->reloc_count
; )
2461 *relptr
++ = tblptr
++;
2466 return section
->reloc_count
;
2470 NAME (aout
, get_reloc_upper_bound
) (bfd
*abfd
, sec_ptr asect
)
2474 if (asect
->flags
& SEC_CONSTRUCTOR
)
2475 count
= asect
->reloc_count
;
2476 else if (asect
== obj_datasec (abfd
))
2477 count
= exec_hdr (abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
);
2478 else if (asect
== obj_textsec (abfd
))
2479 count
= exec_hdr (abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
);
2480 else if (asect
== obj_bsssec (abfd
))
2484 bfd_set_error (bfd_error_invalid_operation
);
2488 if (count
>= LONG_MAX
/ sizeof (arelent
*)
2489 || _bfd_mul_overflow (count
, obj_reloc_entry_size (abfd
), &raw
))
2491 bfd_set_error (bfd_error_file_too_big
);
2494 if (!bfd_write_p (abfd
))
2496 ufile_ptr filesize
= bfd_get_file_size (abfd
);
2497 if (filesize
!= 0 && raw
> filesize
)
2499 bfd_set_error (bfd_error_file_truncated
);
2503 return (count
+ 1) * sizeof (arelent
*);
2507 NAME (aout
, get_symtab_upper_bound
) (bfd
*abfd
)
2509 if (!NAME (aout
, slurp_symbol_table
) (abfd
))
2512 return (bfd_get_symcount (abfd
)+1) * (sizeof (aout_symbol_type
*));
2516 NAME (aout
, get_lineno
) (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
2517 asymbol
*ignore_symbol ATTRIBUTE_UNUSED
)
2523 NAME (aout
, get_symbol_info
) (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
2527 bfd_symbol_info (symbol
, ret
);
2529 if (ret
->type
== '?')
2531 int type_code
= aout_symbol (symbol
)->type
& 0xff;
2532 const char *stab_name
= bfd_get_stab_name (type_code
);
2533 static char buf
[10];
2535 if (stab_name
== NULL
)
2537 sprintf (buf
, "(%d)", type_code
);
2541 ret
->stab_type
= type_code
;
2542 ret
->stab_other
= (unsigned) (aout_symbol (symbol
)->other
& 0xff);
2543 ret
->stab_desc
= (unsigned) (aout_symbol (symbol
)->desc
& 0xffff);
2544 ret
->stab_name
= stab_name
;
2549 NAME (aout
, print_symbol
) (bfd
*abfd
,
2552 bfd_print_symbol_type how
)
2554 FILE *file
= (FILE *)afile
;
2558 case bfd_print_symbol_name
:
2560 fprintf (file
,"%s", symbol
->name
);
2562 case bfd_print_symbol_more
:
2563 fprintf (file
,"%4x %2x %2x",
2564 (unsigned) (aout_symbol (symbol
)->desc
& 0xffff),
2565 (unsigned) (aout_symbol (symbol
)->other
& 0xff),
2566 (unsigned) (aout_symbol (symbol
)->type
));
2568 case bfd_print_symbol_all
:
2570 const char *section_name
= symbol
->section
->name
;
2572 bfd_print_symbol_vandf (abfd
, (void *)file
, symbol
);
2574 fprintf (file
," %-5s %04x %02x %02x",
2576 (unsigned) (aout_symbol (symbol
)->desc
& 0xffff),
2577 (unsigned) (aout_symbol (symbol
)->other
& 0xff),
2578 (unsigned) (aout_symbol (symbol
)->type
& 0xff));
2580 fprintf (file
," %s", symbol
->name
);
2586 /* If we don't have to allocate more than 1MB to hold the generic
2587 symbols, we use the generic minisymbol methord: it's faster, since
2588 it only translates the symbols once, not multiple times. */
2589 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2591 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2592 symbols. The minisymbol_to_symbol function translates these into
2593 BFD asymbol structures. */
2596 NAME (aout
, read_minisymbols
) (bfd
*abfd
,
2599 unsigned int *sizep
)
2602 /* We could handle the dynamic symbols here as well, but it's
2603 easier to hand them off. */
2604 return _bfd_generic_read_minisymbols (abfd
, dynamic
, minisymsp
, sizep
);
2606 if (! aout_get_external_symbols (abfd
))
2609 if (obj_aout_external_sym_count (abfd
) < MINISYM_THRESHOLD
)
2610 return _bfd_generic_read_minisymbols (abfd
, dynamic
, minisymsp
, sizep
);
2612 *minisymsp
= (void *) obj_aout_external_syms (abfd
);
2614 /* By passing the external symbols back from this routine, we are
2615 giving up control over the memory block. Clear
2616 obj_aout_external_syms, so that we do not try to free it
2618 obj_aout_external_syms (abfd
) = NULL
;
2620 *sizep
= EXTERNAL_NLIST_SIZE
;
2621 return obj_aout_external_sym_count (abfd
);
2624 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2625 unmodified a.out symbol. The SYM argument is a structure returned
2626 by bfd_make_empty_symbol, which we fill in here. */
2629 NAME (aout
, minisymbol_to_symbol
) (bfd
*abfd
,
2631 const void * minisym
,
2635 || obj_aout_external_sym_count (abfd
) < MINISYM_THRESHOLD
)
2636 return _bfd_generic_minisymbol_to_symbol (abfd
, dynamic
, minisym
, sym
);
2638 memset (sym
, 0, sizeof (aout_symbol_type
));
2640 /* We call translate_symbol_table to translate a single symbol. */
2641 if (! (NAME (aout
, translate_symbol_table
)
2643 (aout_symbol_type
*) sym
,
2644 (struct external_nlist
*) minisym
,
2646 obj_aout_external_strings (abfd
),
2647 obj_aout_external_string_size (abfd
),
2654 /* Provided a BFD, a section and an offset into the section, calculate
2655 and return the name of the source file and the line nearest to the
2659 NAME (aout
, find_nearest_line
) (bfd
*abfd
,
2663 const char **filename_ptr
,
2664 const char **functionname_ptr
,
2665 unsigned int *line_ptr
,
2666 unsigned int *disriminator_ptr
)
2668 /* Run down the file looking for the filename, function and linenumber. */
2670 const char *directory_name
= NULL
;
2671 const char *main_file_name
= NULL
;
2672 const char *current_file_name
= NULL
;
2673 const char *line_file_name
= NULL
; /* Value of current_file_name at line number. */
2674 const char *line_directory_name
= NULL
; /* Value of directory_name at line number. */
2675 bfd_vma low_line_vma
= 0;
2676 bfd_vma low_func_vma
= 0;
2678 bfd_size_type filelen
, funclen
;
2681 *filename_ptr
= bfd_get_filename (abfd
);
2682 *functionname_ptr
= NULL
;
2684 if (disriminator_ptr
)
2685 *disriminator_ptr
= 0;
2687 if (symbols
!= NULL
)
2689 for (p
= symbols
; *p
; p
++)
2691 aout_symbol_type
*q
= (aout_symbol_type
*) (*p
);
2696 /* If this looks like a file name symbol, and it comes after
2697 the line number we have found so far, but before the
2698 offset, then we have probably not found the right line
2700 if (q
->symbol
.value
<= offset
2701 && ((q
->symbol
.value
> low_line_vma
2702 && (line_file_name
!= NULL
2704 || (q
->symbol
.value
> low_func_vma
2707 const char *symname
;
2709 symname
= q
->symbol
.name
;
2712 && strlen (symname
) > 2
2713 && strcmp (symname
+ strlen (symname
) - 2, ".o") == 0)
2715 if (q
->symbol
.value
> low_line_vma
)
2718 line_file_name
= NULL
;
2720 if (q
->symbol
.value
> low_func_vma
)
2727 /* If this symbol is less than the offset, but greater than
2728 the line number we have found so far, then we have not
2729 found the right line number. */
2730 if (q
->symbol
.value
<= offset
)
2732 if (q
->symbol
.value
> low_line_vma
)
2735 line_file_name
= NULL
;
2737 if (q
->symbol
.value
> low_func_vma
)
2741 main_file_name
= current_file_name
= q
->symbol
.name
;
2742 /* Look ahead to next symbol to check if that too is an N_SO. */
2746 q
= (aout_symbol_type
*) (*p
);
2747 if (q
->type
!= (int)N_SO
)
2750 /* Found a second N_SO First is directory; second is filename. */
2751 directory_name
= current_file_name
;
2752 main_file_name
= current_file_name
= q
->symbol
.name
;
2753 if (obj_textsec (abfd
) != section
)
2757 current_file_name
= q
->symbol
.name
;
2764 /* We'll keep this if it resolves nearer than the one we have
2766 if (q
->symbol
.value
>= low_line_vma
2767 && q
->symbol
.value
<= offset
)
2769 *line_ptr
= q
->desc
;
2770 low_line_vma
= q
->symbol
.value
;
2771 line_file_name
= current_file_name
;
2772 line_directory_name
= directory_name
;
2777 /* We'll keep this if it is nearer than the one we have already. */
2778 if (q
->symbol
.value
>= low_func_vma
2779 && q
->symbol
.value
<= offset
)
2781 low_func_vma
= q
->symbol
.value
;
2782 func
= (asymbol
*)q
;
2784 else if (q
->symbol
.value
> offset
)
2795 main_file_name
= line_file_name
;
2796 directory_name
= line_directory_name
;
2799 if (main_file_name
== NULL
2800 || IS_ABSOLUTE_PATH (main_file_name
)
2801 || directory_name
== NULL
)
2804 filelen
= strlen (directory_name
) + strlen (main_file_name
);
2809 funclen
= strlen (bfd_asymbol_name (func
));
2811 free (adata (abfd
).line_buf
);
2813 if (filelen
+ funclen
== 0)
2814 adata (abfd
).line_buf
= buf
= NULL
;
2817 buf
= (char *) bfd_malloc (filelen
+ funclen
+ 3);
2818 adata (abfd
).line_buf
= buf
;
2823 if (main_file_name
!= NULL
)
2825 if (IS_ABSOLUTE_PATH (main_file_name
) || directory_name
== NULL
)
2826 *filename_ptr
= main_file_name
;
2830 /* PR binutils/20891: In a corrupt input file both
2831 main_file_name and directory_name can be empty... */
2832 * filename_ptr
= NULL
;
2835 snprintf (buf
, filelen
+ 1, "%s%s", directory_name
,
2837 *filename_ptr
= buf
;
2845 const char *function
= func
->name
;
2850 /* PR binutils/20892: In a corrupt input file func can be empty. */
2851 * functionname_ptr
= NULL
;
2854 /* The caller expects a symbol name. We actually have a
2855 function name, without the leading underscore. Put the
2856 underscore back in, so that the caller gets a symbol name. */
2857 if (bfd_get_symbol_leading_char (abfd
) == '\0')
2858 strcpy (buf
, function
);
2861 buf
[0] = bfd_get_symbol_leading_char (abfd
);
2862 strcpy (buf
+ 1, function
);
2864 /* Have to remove : stuff. */
2865 colon
= strchr (buf
, ':');
2868 *functionname_ptr
= buf
;
2875 NAME (aout
, sizeof_headers
) (bfd
*abfd
,
2876 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
2878 return adata (abfd
).exec_bytes_size
;
2881 /* Throw away most malloc'd and alloc'd information for this BFD. */
2884 NAME (aout
, bfd_free_cached_info
) (bfd
*abfd
)
2886 if ((bfd_get_format (abfd
) == bfd_object
2887 || bfd_get_format (abfd
) == bfd_core
)
2888 && abfd
->tdata
.aout_data
!= NULL
)
2890 #define BFCI_FREE(x) do { free (x); x = NULL; } while (0)
2891 BFCI_FREE (adata (abfd
).line_buf
);
2892 BFCI_FREE (obj_aout_symbols (abfd
));
2893 BFCI_FREE (obj_aout_external_syms (abfd
));
2894 BFCI_FREE (obj_aout_external_strings (abfd
));
2895 for (asection
*o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2896 BFCI_FREE (o
->relocation
);
2900 return _bfd_generic_bfd_free_cached_info (abfd
);
2903 /* a.out link code. */
2905 /* Routine to create an entry in an a.out link hash table. */
2907 struct bfd_hash_entry
*
2908 NAME (aout
, link_hash_newfunc
) (struct bfd_hash_entry
*entry
,
2909 struct bfd_hash_table
*table
,
2912 struct aout_link_hash_entry
*ret
= (struct aout_link_hash_entry
*) entry
;
2914 /* Allocate the structure if it has not already been allocated by a
2917 ret
= (struct aout_link_hash_entry
*) bfd_hash_allocate (table
,
2922 /* Call the allocation method of the superclass. */
2923 ret
= ((struct aout_link_hash_entry
*)
2924 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2928 /* Set local fields. */
2929 ret
->written
= false;
2933 return (struct bfd_hash_entry
*) ret
;
2936 /* Initialize an a.out link hash table. */
2939 NAME (aout
, link_hash_table_init
) (struct aout_link_hash_table
*table
,
2941 struct bfd_hash_entry
*(*newfunc
)
2942 (struct bfd_hash_entry
*, struct bfd_hash_table
*,
2944 unsigned int entsize
)
2946 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
2949 /* Create an a.out link hash table. */
2951 struct bfd_link_hash_table
*
2952 NAME (aout
, link_hash_table_create
) (bfd
*abfd
)
2954 struct aout_link_hash_table
*ret
;
2955 size_t amt
= sizeof (* ret
);
2957 ret
= (struct aout_link_hash_table
*) bfd_malloc (amt
);
2961 if (!NAME (aout
, link_hash_table_init
) (ret
, abfd
,
2962 NAME (aout
, link_hash_newfunc
),
2963 sizeof (struct aout_link_hash_entry
)))
2971 /* Add all symbols from an object file to the hash table. */
2974 aout_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2976 bool (*add_one_symbol
)
2977 (struct bfd_link_info
*, bfd
*, const char *, flagword
, asection
*,
2978 bfd_vma
, const char *, bool, bool, struct bfd_link_hash_entry
**);
2979 struct external_nlist
*syms
;
2980 bfd_size_type sym_count
;
2983 struct aout_link_hash_entry
**sym_hash
;
2984 struct external_nlist
*p
;
2985 struct external_nlist
*pend
;
2988 syms
= obj_aout_external_syms (abfd
);
2989 sym_count
= obj_aout_external_sym_count (abfd
);
2990 strings
= obj_aout_external_strings (abfd
);
2991 if (info
->keep_memory
)
2996 if (aout_backend_info (abfd
)->add_dynamic_symbols
!= NULL
)
2998 if (! ((*aout_backend_info (abfd
)->add_dynamic_symbols
)
2999 (abfd
, info
, &syms
, &sym_count
, &strings
)))
3004 return true; /* Nothing to do. */
3006 /* We keep a list of the linker hash table entries that correspond
3007 to particular symbols. We could just look them up in the hash
3008 table, but keeping the list is more efficient. Perhaps this
3009 should be conditional on info->keep_memory. */
3010 amt
= sym_count
* sizeof (struct aout_link_hash_entry
*);
3011 sym_hash
= (struct aout_link_hash_entry
**) bfd_alloc (abfd
, amt
);
3012 if (sym_hash
== NULL
)
3014 obj_aout_sym_hashes (abfd
) = sym_hash
;
3016 add_one_symbol
= aout_backend_info (abfd
)->add_one_symbol
;
3017 if (add_one_symbol
== NULL
)
3018 add_one_symbol
= _bfd_generic_link_add_one_symbol
;
3021 pend
= p
+ sym_count
;
3022 for (; p
< pend
; p
++, sym_hash
++)
3033 type
= H_GET_8 (abfd
, p
->e_type
);
3035 /* Ignore debugging symbols. */
3036 if ((type
& N_STAB
) != 0)
3039 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3040 if (GET_WORD (abfd
, p
->e_strx
) >= obj_aout_external_string_size (abfd
))
3042 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3043 value
= GET_WORD (abfd
, p
->e_value
);
3060 /* Ignore symbols that are not externally visible. */
3063 /* Ignore local indirect symbol. */
3068 case N_UNDF
| N_EXT
:
3071 section
= bfd_und_section_ptr
;
3075 section
= bfd_com_section_ptr
;
3078 section
= bfd_abs_section_ptr
;
3080 case N_TEXT
| N_EXT
:
3081 section
= obj_textsec (abfd
);
3082 value
-= bfd_section_vma (section
);
3084 case N_DATA
| N_EXT
:
3085 case N_SETV
| N_EXT
:
3086 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3087 translate_from_native_sym_flags. */
3088 section
= obj_datasec (abfd
);
3089 value
-= bfd_section_vma (section
);
3092 section
= obj_bsssec (abfd
);
3093 value
-= bfd_section_vma (section
);
3095 case N_INDR
| N_EXT
:
3096 /* An indirect symbol. The next symbol is the symbol
3097 which this one really is. */
3098 /* See PR 20925 for a reproducer. */
3102 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3103 if (GET_WORD (abfd
, p
->e_strx
) >= obj_aout_external_string_size (abfd
))
3105 string
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3106 section
= bfd_ind_section_ptr
;
3107 flags
|= BSF_INDIRECT
;
3109 case N_COMM
| N_EXT
:
3110 section
= bfd_com_section_ptr
;
3112 case N_SETA
: case N_SETA
| N_EXT
:
3113 section
= bfd_abs_section_ptr
;
3114 flags
|= BSF_CONSTRUCTOR
;
3116 case N_SETT
: case N_SETT
| N_EXT
:
3117 section
= obj_textsec (abfd
);
3118 flags
|= BSF_CONSTRUCTOR
;
3119 value
-= bfd_section_vma (section
);
3121 case N_SETD
: case N_SETD
| N_EXT
:
3122 section
= obj_datasec (abfd
);
3123 flags
|= BSF_CONSTRUCTOR
;
3124 value
-= bfd_section_vma (section
);
3126 case N_SETB
: case N_SETB
| N_EXT
:
3127 section
= obj_bsssec (abfd
);
3128 flags
|= BSF_CONSTRUCTOR
;
3129 value
-= bfd_section_vma (section
);
3132 /* A warning symbol. The next symbol is the one to warn
3133 about. If there is no next symbol, just look away. */
3138 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3139 if (GET_WORD (abfd
, p
->e_strx
) >= obj_aout_external_string_size (abfd
))
3141 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3142 section
= bfd_und_section_ptr
;
3143 flags
|= BSF_WARNING
;
3146 section
= bfd_und_section_ptr
;
3150 section
= bfd_abs_section_ptr
;
3154 section
= obj_textsec (abfd
);
3155 value
-= bfd_section_vma (section
);
3159 section
= obj_datasec (abfd
);
3160 value
-= bfd_section_vma (section
);
3164 section
= obj_bsssec (abfd
);
3165 value
-= bfd_section_vma (section
);
3170 if (! ((*add_one_symbol
)
3171 (info
, abfd
, name
, flags
, section
, value
, string
, copy
, false,
3172 (struct bfd_link_hash_entry
**) sym_hash
)))
3175 /* Restrict the maximum alignment of a common symbol based on
3176 the architecture, since a.out has no way to represent
3177 alignment requirements of a section in a .o file. FIXME:
3178 This isn't quite right: it should use the architecture of the
3179 output file, not the input files. */
3180 if ((*sym_hash
)->root
.type
== bfd_link_hash_common
3181 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
>
3182 bfd_get_arch_info (abfd
)->section_align_power
))
3183 (*sym_hash
)->root
.u
.c
.p
->alignment_power
=
3184 bfd_get_arch_info (abfd
)->section_align_power
;
3186 /* If this is a set symbol, and we are not building sets, then
3187 it is possible for the hash entry to not have been set. In
3188 such a case, treat the symbol as not globally defined. */
3189 if ((*sym_hash
)->root
.type
== bfd_link_hash_new
)
3191 BFD_ASSERT ((flags
& BSF_CONSTRUCTOR
) != 0);
3195 if (type
== (N_INDR
| N_EXT
) || type
== N_WARNING
)
3202 /* Free up the internal symbols read from an a.out file. */
3205 aout_link_free_symbols (bfd
*abfd
)
3207 if (obj_aout_external_syms (abfd
) != NULL
)
3209 free ((void *) obj_aout_external_syms (abfd
));
3210 obj_aout_external_syms (abfd
) = NULL
;
3212 if (obj_aout_external_strings (abfd
) != NULL
)
3214 free ((void *) obj_aout_external_strings (abfd
));
3215 obj_aout_external_strings (abfd
) = NULL
;
3220 /* Add symbols from an a.out object file. */
3223 aout_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3225 if (! aout_get_external_symbols (abfd
))
3227 if (! aout_link_add_symbols (abfd
, info
))
3229 if (! info
->keep_memory
)
3231 if (! aout_link_free_symbols (abfd
))
3237 /* Look through the internal symbols to see if this object file should
3238 be included in the link. We should include this object file if it
3239 defines any symbols which are currently undefined. If this object
3240 file defines a common symbol, then we may adjust the size of the
3241 known symbol but we do not include the object file in the link
3242 (unless there is some other reason to include it). */
3245 aout_link_check_ar_symbols (bfd
*abfd
,
3246 struct bfd_link_info
*info
,
3250 struct external_nlist
*p
;
3251 struct external_nlist
*pend
;
3256 /* Look through all the symbols. */
3257 p
= obj_aout_external_syms (abfd
);
3258 pend
= p
+ obj_aout_external_sym_count (abfd
);
3259 strings
= obj_aout_external_strings (abfd
);
3260 for (; p
< pend
; p
++)
3262 int type
= H_GET_8 (abfd
, p
->e_type
);
3264 struct bfd_link_hash_entry
*h
;
3266 /* Ignore symbols that are not externally visible. This is an
3267 optimization only, as we check the type more thoroughly
3269 if (((type
& N_EXT
) == 0
3270 || (type
& N_STAB
) != 0
3277 if (type
== N_WARNING
3283 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3284 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
3286 /* We are only interested in symbols that are currently
3287 undefined or common. */
3289 || (h
->type
!= bfd_link_hash_undefined
3290 && h
->type
!= bfd_link_hash_common
))
3292 if (type
== (N_INDR
| N_EXT
))
3297 if (type
== (N_TEXT
| N_EXT
)
3298 || type
== (N_DATA
| N_EXT
)
3299 || type
== (N_BSS
| N_EXT
)
3300 || type
== (N_ABS
| N_EXT
)
3301 || type
== (N_INDR
| N_EXT
))
3303 /* This object file defines this symbol. We must link it
3304 in. This is true regardless of whether the current
3305 definition of the symbol is undefined or common.
3307 If the current definition is common, we have a case in
3308 which we have already seen an object file including:
3310 and this object file from the archive includes:
3312 In such a case, whether to include this object is target
3313 dependant for backward compatibility.
3315 FIXME: The SunOS 4.1.3 linker will pull in the archive
3316 element if the symbol is defined in the .data section,
3317 but not if it is defined in the .text section. That
3318 seems a bit crazy to me, and it has not been implemented
3319 yet. However, it might be correct. */
3320 if (h
->type
== bfd_link_hash_common
)
3324 switch (info
->common_skip_ar_symbols
)
3326 case bfd_link_common_skip_none
:
3328 case bfd_link_common_skip_text
:
3329 skip
= (type
== (N_TEXT
| N_EXT
));
3331 case bfd_link_common_skip_data
:
3332 skip
= (type
== (N_DATA
| N_EXT
));
3334 case bfd_link_common_skip_all
:
3343 if (!(*info
->callbacks
3344 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
3350 if (type
== (N_UNDF
| N_EXT
))
3354 value
= GET_WORD (abfd
, p
->e_value
);
3357 /* This symbol is common in the object from the archive
3359 if (h
->type
== bfd_link_hash_undefined
)
3364 symbfd
= h
->u
.undef
.abfd
;
3367 /* This symbol was created as undefined from
3368 outside BFD. We assume that we should link
3369 in the object file. This is done for the -u
3370 option in the linker. */
3371 if (!(*info
->callbacks
3372 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
3377 /* Turn the current link symbol into a common
3378 symbol. It is already on the undefs list. */
3379 h
->type
= bfd_link_hash_common
;
3380 h
->u
.c
.p
= (struct bfd_link_hash_common_entry
*)
3381 bfd_hash_allocate (&info
->hash
->table
,
3382 sizeof (struct bfd_link_hash_common_entry
));
3383 if (h
->u
.c
.p
== NULL
)
3386 h
->u
.c
.size
= value
;
3388 /* FIXME: This isn't quite right. The maximum
3389 alignment of a common symbol should be set by the
3390 architecture of the output file, not of the input
3392 power
= bfd_log2 (value
);
3393 if (power
> bfd_get_arch_info (abfd
)->section_align_power
)
3394 power
= bfd_get_arch_info (abfd
)->section_align_power
;
3395 h
->u
.c
.p
->alignment_power
= power
;
3397 h
->u
.c
.p
->section
= bfd_make_section_old_way (symbfd
,
3402 /* Adjust the size of the common symbol if
3404 if (value
> h
->u
.c
.size
)
3405 h
->u
.c
.size
= value
;
3415 /* This symbol is weak but defined. We must pull it in if
3416 the current link symbol is undefined, but we don't want
3417 it if the current link symbol is common. */
3418 if (h
->type
== bfd_link_hash_undefined
)
3420 if (!(*info
->callbacks
3421 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
3429 /* We do not need this object file. */
3432 /* Check a single archive element to see if we need to include it in
3433 the link. *PNEEDED is set according to whether this element is
3434 needed in the link or not. This is called from
3435 _bfd_generic_link_add_archive_symbols. */
3438 aout_link_check_archive_element (bfd
*abfd
,
3439 struct bfd_link_info
*info
,
3440 struct bfd_link_hash_entry
*h ATTRIBUTE_UNUSED
,
3441 const char *name ATTRIBUTE_UNUSED
,
3447 if (!aout_get_external_symbols (abfd
))
3451 if (!aout_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
3457 /* Potentially, the add_archive_element hook may have set a
3458 substitute BFD for us. */
3461 if (!info
->keep_memory
3462 && !aout_link_free_symbols (oldbfd
))
3464 if (!aout_get_external_symbols (abfd
))
3467 if (!aout_link_add_symbols (abfd
, info
))
3471 if (!info
->keep_memory
|| !needed
)
3473 if (!aout_link_free_symbols (abfd
))
3480 /* Given an a.out BFD, add symbols to the global hash table as
3484 NAME (aout
, link_add_symbols
) (bfd
*abfd
, struct bfd_link_info
*info
)
3486 switch (bfd_get_format (abfd
))
3489 return aout_link_add_object_symbols (abfd
, info
);
3491 return _bfd_generic_link_add_archive_symbols
3492 (abfd
, info
, aout_link_check_archive_element
);
3494 bfd_set_error (bfd_error_wrong_format
);
3499 /* A hash table used for header files with N_BINCL entries. */
3501 struct aout_link_includes_table
3503 struct bfd_hash_table root
;
3506 /* A linked list of totals that we have found for a particular header
3509 struct aout_link_includes_totals
3511 struct aout_link_includes_totals
*next
;
3515 /* An entry in the header file hash table. */
3517 struct aout_link_includes_entry
3519 struct bfd_hash_entry root
;
3520 /* List of totals we have found for this file. */
3521 struct aout_link_includes_totals
*totals
;
3524 /* Look up an entry in an the header file hash table. */
3526 #define aout_link_includes_lookup(table, string, create, copy) \
3527 ((struct aout_link_includes_entry *) \
3528 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3530 /* During the final link step we need to pass around a bunch of
3531 information, so we do it in an instance of this structure. */
3533 struct aout_final_link_info
3535 /* General link information. */
3536 struct bfd_link_info
*info
;
3539 /* Reloc file positions. */
3540 file_ptr treloff
, dreloff
;
3541 /* File position of symbols. */
3544 struct bfd_strtab_hash
*strtab
;
3545 /* Header file hash table. */
3546 struct aout_link_includes_table includes
;
3547 /* A buffer large enough to hold the contents of any section. */
3549 /* A buffer large enough to hold the relocs of any section. */
3551 /* A buffer large enough to hold the symbol map of any input BFD. */
3553 /* A buffer large enough to hold output symbols of any input BFD. */
3554 struct external_nlist
*output_syms
;
3557 /* The function to create a new entry in the header file hash table. */
3559 static struct bfd_hash_entry
*
3560 aout_link_includes_newfunc (struct bfd_hash_entry
*entry
,
3561 struct bfd_hash_table
*table
,
3564 struct aout_link_includes_entry
*ret
=
3565 (struct aout_link_includes_entry
*) entry
;
3567 /* Allocate the structure if it has not already been allocated by a
3570 ret
= (struct aout_link_includes_entry
*)
3571 bfd_hash_allocate (table
, sizeof (* ret
));
3575 /* Call the allocation method of the superclass. */
3576 ret
= ((struct aout_link_includes_entry
*)
3577 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
3580 /* Set local fields. */
3584 return (struct bfd_hash_entry
*) ret
;
3587 /* Write out a symbol that was not associated with an a.out input
3591 aout_link_write_other_symbol (struct bfd_hash_entry
*bh
, void *data
)
3593 struct aout_link_hash_entry
*h
= (struct aout_link_hash_entry
*) bh
;
3594 struct aout_final_link_info
*flaginfo
= (struct aout_final_link_info
*) data
;
3598 struct external_nlist outsym
;
3602 if (h
->root
.type
== bfd_link_hash_warning
)
3604 h
= (struct aout_link_hash_entry
*) h
->root
.u
.i
.link
;
3605 if (h
->root
.type
== bfd_link_hash_new
)
3609 output_bfd
= flaginfo
->output_bfd
;
3611 if (aout_backend_info (output_bfd
)->write_dynamic_symbol
!= NULL
)
3613 if (! ((*aout_backend_info (output_bfd
)->write_dynamic_symbol
)
3614 (output_bfd
, flaginfo
->info
, h
)))
3616 /* FIXME: No way to handle errors. */
3626 /* An indx of -2 means the symbol must be written. */
3628 && (flaginfo
->info
->strip
== strip_all
3629 || (flaginfo
->info
->strip
== strip_some
3630 && bfd_hash_lookup (flaginfo
->info
->keep_hash
, h
->root
.root
.string
,
3631 false, false) == NULL
)))
3634 switch (h
->root
.type
)
3637 case bfd_link_hash_warning
:
3639 /* Avoid variable not initialized warnings. */
3641 case bfd_link_hash_new
:
3642 /* This can happen for set symbols when sets are not being
3645 case bfd_link_hash_undefined
:
3646 type
= N_UNDF
| N_EXT
;
3649 case bfd_link_hash_defined
:
3650 case bfd_link_hash_defweak
:
3654 sec
= h
->root
.u
.def
.section
->output_section
;
3655 BFD_ASSERT (bfd_is_abs_section (sec
)
3656 || sec
->owner
== output_bfd
);
3657 if (sec
== obj_textsec (output_bfd
))
3658 type
= h
->root
.type
== bfd_link_hash_defined
? N_TEXT
: N_WEAKT
;
3659 else if (sec
== obj_datasec (output_bfd
))
3660 type
= h
->root
.type
== bfd_link_hash_defined
? N_DATA
: N_WEAKD
;
3661 else if (sec
== obj_bsssec (output_bfd
))
3662 type
= h
->root
.type
== bfd_link_hash_defined
? N_BSS
: N_WEAKB
;
3664 type
= h
->root
.type
== bfd_link_hash_defined
? N_ABS
: N_WEAKA
;
3666 val
= (h
->root
.u
.def
.value
3668 + h
->root
.u
.def
.section
->output_offset
);
3671 case bfd_link_hash_common
:
3672 type
= N_UNDF
| N_EXT
;
3673 val
= h
->root
.u
.c
.size
;
3675 case bfd_link_hash_undefweak
:
3679 case bfd_link_hash_indirect
:
3680 /* We ignore these symbols, since the indirected symbol is
3681 already in the hash table. */
3685 H_PUT_8 (output_bfd
, type
, outsym
.e_type
);
3686 H_PUT_8 (output_bfd
, 0, outsym
.e_other
);
3687 H_PUT_16 (output_bfd
, 0, outsym
.e_desc
);
3688 indx
= add_to_stringtab (output_bfd
, flaginfo
->strtab
, h
->root
.root
.string
,
3690 if (indx
== - (bfd_size_type
) 1)
3691 /* FIXME: No way to handle errors. */
3694 PUT_WORD (output_bfd
, indx
, outsym
.e_strx
);
3695 PUT_WORD (output_bfd
, val
, outsym
.e_value
);
3697 amt
= EXTERNAL_NLIST_SIZE
;
3698 if (bfd_seek (output_bfd
, flaginfo
->symoff
, SEEK_SET
) != 0
3699 || bfd_write (&outsym
, amt
, output_bfd
) != amt
)
3700 /* FIXME: No way to handle errors. */
3703 flaginfo
->symoff
+= EXTERNAL_NLIST_SIZE
;
3704 h
->indx
= obj_aout_external_sym_count (output_bfd
);
3705 ++obj_aout_external_sym_count (output_bfd
);
3710 /* Handle a link order which is supposed to generate a reloc. */
3713 aout_link_reloc_link_order (struct aout_final_link_info
*flaginfo
,
3715 struct bfd_link_order
*p
)
3717 struct bfd_link_order_reloc
*pr
;
3720 reloc_howto_type
*howto
;
3721 file_ptr
*reloff_ptr
= NULL
;
3722 struct reloc_std_external srel
;
3723 struct reloc_ext_external erel
;
3729 if (p
->type
== bfd_section_reloc_link_order
)
3732 if (bfd_is_abs_section (pr
->u
.section
))
3733 r_index
= N_ABS
| N_EXT
;
3736 BFD_ASSERT (pr
->u
.section
->owner
== flaginfo
->output_bfd
);
3737 r_index
= pr
->u
.section
->target_index
;
3742 struct aout_link_hash_entry
*h
;
3744 BFD_ASSERT (p
->type
== bfd_symbol_reloc_link_order
);
3746 h
= ((struct aout_link_hash_entry
*)
3747 bfd_wrapped_link_hash_lookup (flaginfo
->output_bfd
, flaginfo
->info
,
3748 pr
->u
.name
, false, false, true));
3754 /* We decided to strip this symbol, but it turns out that we
3755 can't. Note that we lose the other and desc information
3756 here. I don't think that will ever matter for a global
3760 if (!aout_link_write_other_symbol (&h
->root
.root
, flaginfo
))
3766 (*flaginfo
->info
->callbacks
->unattached_reloc
)
3767 (flaginfo
->info
, pr
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
3772 howto
= bfd_reloc_type_lookup (flaginfo
->output_bfd
, pr
->reloc
);
3775 bfd_set_error (bfd_error_bad_value
);
3779 if (o
== obj_textsec (flaginfo
->output_bfd
))
3780 reloff_ptr
= &flaginfo
->treloff
;
3781 else if (o
== obj_datasec (flaginfo
->output_bfd
))
3782 reloff_ptr
= &flaginfo
->dreloff
;
3786 if (obj_reloc_entry_size (flaginfo
->output_bfd
) == RELOC_STD_SIZE
)
3789 MY_put_reloc (flaginfo
->output_bfd
, r_extern
, r_index
, p
->offset
, howto
,
3797 unsigned int r_length
;
3799 r_pcrel
= (int) howto
->pc_relative
;
3800 r_baserel
= (howto
->type
& 8) != 0;
3801 r_jmptable
= (howto
->type
& 16) != 0;
3802 r_relative
= (howto
->type
& 32) != 0;
3803 r_length
= bfd_log2 (bfd_get_reloc_size (howto
));
3805 PUT_WORD (flaginfo
->output_bfd
, p
->offset
, srel
.r_address
);
3806 if (bfd_header_big_endian (flaginfo
->output_bfd
))
3808 srel
.r_index
[0] = r_index
>> 16;
3809 srel
.r_index
[1] = r_index
>> 8;
3810 srel
.r_index
[2] = r_index
;
3812 ((r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
3813 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
3814 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
3815 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
3816 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
3817 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
3821 srel
.r_index
[2] = r_index
>> 16;
3822 srel
.r_index
[1] = r_index
>> 8;
3823 srel
.r_index
[0] = r_index
;
3825 ((r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
3826 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
3827 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
3828 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
3829 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
3830 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
3834 rel_ptr
= (void *) &srel
;
3836 /* We have to write the addend into the object file, since
3837 standard a.out relocs are in place. It would be more
3838 reliable if we had the current contents of the file here,
3839 rather than assuming zeroes, but we can't read the file since
3840 it was opened using bfd_openw. */
3841 if (pr
->addend
!= 0)
3844 bfd_reloc_status_type r
;
3848 size
= bfd_get_reloc_size (howto
);
3849 buf
= (bfd_byte
*) bfd_zmalloc (size
);
3850 if (buf
== NULL
&& size
!= 0)
3852 r
= MY_relocate_contents (howto
, flaginfo
->output_bfd
,
3853 (bfd_vma
) pr
->addend
, buf
);
3859 case bfd_reloc_outofrange
:
3861 case bfd_reloc_overflow
:
3862 (*flaginfo
->info
->callbacks
->reloc_overflow
)
3863 (flaginfo
->info
, NULL
,
3864 (p
->type
== bfd_section_reloc_link_order
3865 ? bfd_section_name (pr
->u
.section
)
3867 howto
->name
, pr
->addend
, NULL
, NULL
, (bfd_vma
) 0);
3870 ok
= bfd_set_section_contents (flaginfo
->output_bfd
, o
, (void *) buf
,
3871 (file_ptr
) p
->offset
, size
);
3879 #ifdef MY_put_ext_reloc
3880 MY_put_ext_reloc (flaginfo
->output_bfd
, r_extern
, r_index
, p
->offset
,
3881 howto
, &erel
, pr
->addend
);
3883 PUT_WORD (flaginfo
->output_bfd
, p
->offset
, erel
.r_address
);
3885 if (bfd_header_big_endian (flaginfo
->output_bfd
))
3887 erel
.r_index
[0] = r_index
>> 16;
3888 erel
.r_index
[1] = r_index
>> 8;
3889 erel
.r_index
[2] = r_index
;
3891 ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
3892 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
3896 erel
.r_index
[2] = r_index
>> 16;
3897 erel
.r_index
[1] = r_index
>> 8;
3898 erel
.r_index
[0] = r_index
;
3900 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
3901 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
3904 PUT_WORD (flaginfo
->output_bfd
, (bfd_vma
) pr
->addend
, erel
.r_addend
);
3905 #endif /* MY_put_ext_reloc */
3907 rel_ptr
= (void *) &erel
;
3910 amt
= obj_reloc_entry_size (flaginfo
->output_bfd
);
3911 if (bfd_seek (flaginfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0
3912 || bfd_write (rel_ptr
, amt
, flaginfo
->output_bfd
) != amt
)
3915 *reloff_ptr
+= obj_reloc_entry_size (flaginfo
->output_bfd
);
3917 /* Assert that the relocs have not run into the symbols, and that n
3918 the text relocs have not run into the data relocs. */
3919 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (flaginfo
->output_bfd
)
3920 && (reloff_ptr
!= &flaginfo
->treloff
3922 <= obj_datasec (flaginfo
->output_bfd
)->rel_filepos
)));
3927 /* Get the section corresponding to a reloc index. */
3929 static inline asection
*
3930 aout_reloc_index_to_section (bfd
*abfd
, int indx
)
3932 switch (indx
& N_TYPE
)
3934 case N_TEXT
: return obj_textsec (abfd
);
3935 case N_DATA
: return obj_datasec (abfd
);
3936 case N_BSS
: return obj_bsssec (abfd
);
3938 case N_UNDF
: return bfd_abs_section_ptr
;
3944 /* Relocate an a.out section using standard a.out relocs. */
3947 aout_link_input_section_std (struct aout_final_link_info
*flaginfo
,
3949 asection
*input_section
,
3950 struct reloc_std_external
*relocs
,
3951 bfd_size_type rel_size
,
3954 bool (*check_dynamic_reloc
)
3955 (struct bfd_link_info
*, bfd
*, asection
*,
3956 struct aout_link_hash_entry
*, void *, bfd_byte
*, bool *, bfd_vma
*);
3959 struct external_nlist
*syms
;
3961 struct aout_link_hash_entry
**sym_hashes
;
3963 bfd_size_type reloc_count
;
3964 struct reloc_std_external
*rel
;
3965 struct reloc_std_external
*rel_end
;
3967 output_bfd
= flaginfo
->output_bfd
;
3968 check_dynamic_reloc
= aout_backend_info (output_bfd
)->check_dynamic_reloc
;
3970 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
);
3971 BFD_ASSERT (input_bfd
->xvec
->header_byteorder
3972 == output_bfd
->xvec
->header_byteorder
);
3974 relocatable
= bfd_link_relocatable (flaginfo
->info
);
3975 syms
= obj_aout_external_syms (input_bfd
);
3976 strings
= obj_aout_external_strings (input_bfd
);
3977 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
3978 symbol_map
= flaginfo
->symbol_map
;
3980 reloc_count
= rel_size
/ RELOC_STD_SIZE
;
3982 rel_end
= rel
+ reloc_count
;
3983 for (; rel
< rel_end
; rel
++)
3986 unsigned int r_index
;
3990 reloc_howto_type
*howto
;
3991 struct aout_link_hash_entry
*h
= NULL
;
3993 bfd_reloc_status_type r
;
3995 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
3997 #ifdef MY_reloc_howto
3998 howto
= MY_reloc_howto (input_bfd
, rel
, r_index
, r_extern
, r_pcrel
);
4004 unsigned int howto_idx
;
4006 if (bfd_header_big_endian (input_bfd
))
4008 r_index
= (((unsigned int) rel
->r_index
[0] << 16)
4009 | ((unsigned int) rel
->r_index
[1] << 8)
4011 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
4012 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
4013 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
4014 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
4015 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
4016 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
4017 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
4021 r_index
= (((unsigned int) rel
->r_index
[2] << 16)
4022 | ((unsigned int) rel
->r_index
[1] << 8)
4024 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
4025 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
4026 r_baserel
= (0 != (rel
->r_type
[0]
4027 & RELOC_STD_BITS_BASEREL_LITTLE
));
4028 r_jmptable
= (0 != (rel
->r_type
[0]
4029 & RELOC_STD_BITS_JMPTABLE_LITTLE
));
4030 r_relative
= (0 != (rel
->r_type
[0]
4031 & RELOC_STD_BITS_RELATIVE_LITTLE
));
4032 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
4033 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
4036 howto_idx
= (r_length
+ 4 * r_pcrel
+ 8 * r_baserel
4037 + 16 * r_jmptable
+ 32 * r_relative
);
4038 if (howto_idx
< TABLE_SIZE (howto_table_std
))
4039 howto
= howto_table_std
+ howto_idx
;
4047 _bfd_error_handler (_("%pB: unsupported relocation type"),
4049 bfd_set_error (bfd_error_bad_value
);
4055 /* We are generating a relocatable output file, and must
4056 modify the reloc accordingly. */
4059 /* If we know the symbol this relocation is against,
4060 convert it into a relocation against a section. This
4061 is what the native linker does. */
4062 h
= sym_hashes
[r_index
];
4064 && (h
->root
.type
== bfd_link_hash_defined
4065 || h
->root
.type
== bfd_link_hash_defweak
))
4067 asection
*output_section
;
4069 /* Change the r_extern value. */
4070 if (bfd_header_big_endian (output_bfd
))
4071 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_BIG
;
4073 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE
;
4075 /* Compute a new r_index. */
4076 output_section
= h
->root
.u
.def
.section
->output_section
;
4077 if (output_section
== obj_textsec (output_bfd
))
4079 else if (output_section
== obj_datasec (output_bfd
))
4081 else if (output_section
== obj_bsssec (output_bfd
))
4086 /* Add the symbol value and the section VMA to the
4087 addend stored in the contents. */
4088 relocation
= (h
->root
.u
.def
.value
4089 + output_section
->vma
4090 + h
->root
.u
.def
.section
->output_offset
);
4094 /* We must change r_index according to the symbol
4096 r_index
= symbol_map
[r_index
];
4102 /* We decided to strip this symbol, but it
4103 turns out that we can't. Note that we
4104 lose the other and desc information here.
4105 I don't think that will ever matter for a
4111 if (!aout_link_write_other_symbol (&h
->root
.root
,
4121 name
= strings
+ GET_WORD (input_bfd
,
4122 syms
[r_index
].e_strx
);
4123 (*flaginfo
->info
->callbacks
->unattached_reloc
)
4124 (flaginfo
->info
, name
,
4125 input_bfd
, input_section
, r_addr
);
4133 /* Write out the new r_index value. */
4134 if (bfd_header_big_endian (output_bfd
))
4136 rel
->r_index
[0] = r_index
>> 16;
4137 rel
->r_index
[1] = r_index
>> 8;
4138 rel
->r_index
[2] = r_index
;
4142 rel
->r_index
[2] = r_index
>> 16;
4143 rel
->r_index
[1] = r_index
>> 8;
4144 rel
->r_index
[0] = r_index
;
4151 /* This is a relocation against a section. We must
4152 adjust by the amount that the section moved. */
4153 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4154 relocation
= (section
->output_section
->vma
4155 + section
->output_offset
4159 /* Change the address of the relocation. */
4160 PUT_WORD (output_bfd
,
4161 r_addr
+ input_section
->output_offset
,
4164 /* Adjust a PC relative relocation by removing the reference
4165 to the original address in the section and including the
4166 reference to the new address. */
4168 relocation
-= (input_section
->output_section
->vma
4169 + input_section
->output_offset
4170 - input_section
->vma
);
4172 #ifdef MY_relocatable_reloc
4173 MY_relocatable_reloc (howto
, output_bfd
, rel
, relocation
, r_addr
);
4176 if (relocation
== 0)
4179 r
= MY_relocate_contents (howto
,
4180 input_bfd
, relocation
,
4187 /* We are generating an executable, and must do a full
4193 h
= sym_hashes
[r_index
];
4196 && (h
->root
.type
== bfd_link_hash_defined
4197 || h
->root
.type
== bfd_link_hash_defweak
))
4199 relocation
= (h
->root
.u
.def
.value
4200 + h
->root
.u
.def
.section
->output_section
->vma
4201 + h
->root
.u
.def
.section
->output_offset
);
4204 && h
->root
.type
== bfd_link_hash_undefweak
)
4216 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4217 relocation
= (section
->output_section
->vma
4218 + section
->output_offset
4221 relocation
+= input_section
->vma
;
4224 if (check_dynamic_reloc
!= NULL
)
4228 if (! ((*check_dynamic_reloc
)
4229 (flaginfo
->info
, input_bfd
, input_section
, h
,
4230 (void *) rel
, contents
, &skip
, &relocation
)))
4236 /* Now warn if a global symbol is undefined. We could not
4237 do this earlier, because check_dynamic_reloc might want
4238 to skip this reloc. */
4239 if (hundef
&& ! bfd_link_pic (flaginfo
->info
) && ! r_baserel
)
4244 name
= h
->root
.root
.string
;
4246 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4247 (*flaginfo
->info
->callbacks
->undefined_symbol
)
4248 (flaginfo
->info
, name
, input_bfd
, input_section
, r_addr
, true);
4251 r
= MY_final_link_relocate (howto
,
4252 input_bfd
, input_section
,
4253 contents
, r_addr
, relocation
,
4257 if (r
!= bfd_reloc_ok
)
4262 case bfd_reloc_outofrange
:
4264 case bfd_reloc_overflow
:
4271 name
= strings
+ GET_WORD (input_bfd
,
4272 syms
[r_index
].e_strx
);
4277 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4278 name
= bfd_section_name (s
);
4280 (*flaginfo
->info
->callbacks
->reloc_overflow
)
4281 (flaginfo
->info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
4282 (bfd_vma
) 0, input_bfd
, input_section
, r_addr
);
4292 /* Relocate an a.out section using extended a.out relocs. */
4295 aout_link_input_section_ext (struct aout_final_link_info
*flaginfo
,
4297 asection
*input_section
,
4298 struct reloc_ext_external
*relocs
,
4299 bfd_size_type rel_size
,
4302 bool (*check_dynamic_reloc
)
4303 (struct bfd_link_info
*, bfd
*, asection
*,
4304 struct aout_link_hash_entry
*, void *, bfd_byte
*, bool *, bfd_vma
*);
4307 struct external_nlist
*syms
;
4309 struct aout_link_hash_entry
**sym_hashes
;
4311 bfd_size_type reloc_count
;
4312 struct reloc_ext_external
*rel
;
4313 struct reloc_ext_external
*rel_end
;
4315 output_bfd
= flaginfo
->output_bfd
;
4316 check_dynamic_reloc
= aout_backend_info (output_bfd
)->check_dynamic_reloc
;
4318 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_EXT_SIZE
);
4319 BFD_ASSERT (input_bfd
->xvec
->header_byteorder
4320 == output_bfd
->xvec
->header_byteorder
);
4322 relocatable
= bfd_link_relocatable (flaginfo
->info
);
4323 syms
= obj_aout_external_syms (input_bfd
);
4324 strings
= obj_aout_external_strings (input_bfd
);
4325 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4326 symbol_map
= flaginfo
->symbol_map
;
4328 reloc_count
= rel_size
/ RELOC_EXT_SIZE
;
4330 rel_end
= rel
+ reloc_count
;
4331 for (; rel
< rel_end
; rel
++)
4334 unsigned int r_index
;
4336 unsigned int r_type
;
4338 struct aout_link_hash_entry
*h
= NULL
;
4339 asection
*r_section
= NULL
;
4342 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4344 if (bfd_header_big_endian (input_bfd
))
4346 r_index
= (((unsigned int) rel
->r_index
[0] << 16)
4347 | ((unsigned int) rel
->r_index
[1] << 8)
4349 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
4350 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
4351 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
4355 r_index
= (((unsigned int) rel
->r_index
[2] << 16)
4356 | ((unsigned int) rel
->r_index
[1] << 8)
4358 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
4359 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
4360 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
4363 r_addend
= GET_SWORD (input_bfd
, rel
->r_addend
);
4365 if (r_type
>= TABLE_SIZE (howto_table_ext
))
4367 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4369 bfd_set_error (bfd_error_bad_value
);
4375 /* We are generating a relocatable output file, and must
4376 modify the reloc accordingly. */
4378 || r_type
== (unsigned int) RELOC_BASE10
4379 || r_type
== (unsigned int) RELOC_BASE13
4380 || r_type
== (unsigned int) RELOC_BASE22
)
4382 /* If we know the symbol this relocation is against,
4383 convert it into a relocation against a section. This
4384 is what the native linker does. */
4385 if (r_type
== (unsigned int) RELOC_BASE10
4386 || r_type
== (unsigned int) RELOC_BASE13
4387 || r_type
== (unsigned int) RELOC_BASE22
)
4390 h
= sym_hashes
[r_index
];
4392 && (h
->root
.type
== bfd_link_hash_defined
4393 || h
->root
.type
== bfd_link_hash_defweak
))
4395 asection
*output_section
;
4397 /* Change the r_extern value. */
4398 if (bfd_header_big_endian (output_bfd
))
4399 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_BIG
;
4401 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE
;
4403 /* Compute a new r_index. */
4404 output_section
= h
->root
.u
.def
.section
->output_section
;
4405 if (output_section
== obj_textsec (output_bfd
))
4407 else if (output_section
== obj_datasec (output_bfd
))
4409 else if (output_section
== obj_bsssec (output_bfd
))
4414 /* Add the symbol value and the section VMA to the
4416 relocation
= (h
->root
.u
.def
.value
4417 + output_section
->vma
4418 + h
->root
.u
.def
.section
->output_offset
);
4420 /* Now RELOCATION is the VMA of the final
4421 destination. If this is a PC relative reloc,
4422 then ADDEND is the negative of the source VMA.
4423 We want to set ADDEND to the difference between
4424 the destination VMA and the source VMA, which
4425 means we must adjust RELOCATION by the change in
4426 the source VMA. This is done below. */
4430 /* We must change r_index according to the symbol
4432 r_index
= symbol_map
[r_index
];
4438 /* We decided to strip this symbol, but it
4439 turns out that we can't. Note that we
4440 lose the other and desc information here.
4441 I don't think that will ever matter for a
4447 if (!aout_link_write_other_symbol (&h
->root
.root
,
4457 name
= strings
+ GET_WORD (input_bfd
,
4458 syms
[r_index
].e_strx
);
4459 (*flaginfo
->info
->callbacks
->unattached_reloc
)
4460 (flaginfo
->info
, name
,
4461 input_bfd
, input_section
, r_addr
);
4468 /* If this is a PC relative reloc, then the addend
4469 is the negative of the source VMA. We must
4470 adjust it by the change in the source VMA. This
4474 /* Write out the new r_index value. */
4475 if (bfd_header_big_endian (output_bfd
))
4477 rel
->r_index
[0] = r_index
>> 16;
4478 rel
->r_index
[1] = r_index
>> 8;
4479 rel
->r_index
[2] = r_index
;
4483 rel
->r_index
[2] = r_index
>> 16;
4484 rel
->r_index
[1] = r_index
>> 8;
4485 rel
->r_index
[0] = r_index
;
4490 /* This is a relocation against a section. We must
4491 adjust by the amount that the section moved. */
4492 r_section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4493 relocation
= (r_section
->output_section
->vma
4494 + r_section
->output_offset
4497 /* If this is a PC relative reloc, then the addend is
4498 the difference in VMA between the destination and the
4499 source. We have just adjusted for the change in VMA
4500 of the destination, so we must also adjust by the
4501 change in VMA of the source. This is done below. */
4504 /* As described above, we must always adjust a PC relative
4505 reloc by the change in VMA of the source. However, if
4506 pcrel_offset is set, then the addend does not include the
4507 location within the section, in which case we don't need
4508 to adjust anything. */
4509 if (howto_table_ext
[r_type
].pc_relative
4510 && ! howto_table_ext
[r_type
].pcrel_offset
)
4511 relocation
-= (input_section
->output_section
->vma
4512 + input_section
->output_offset
4513 - input_section
->vma
);
4515 /* Change the addend if necessary. */
4516 if (relocation
!= 0)
4517 PUT_WORD (output_bfd
, r_addend
+ relocation
, rel
->r_addend
);
4519 /* Change the address of the relocation. */
4520 PUT_WORD (output_bfd
,
4521 r_addr
+ input_section
->output_offset
,
4527 bfd_reloc_status_type r
;
4529 /* We are generating an executable, and must do a full
4535 h
= sym_hashes
[r_index
];
4538 && (h
->root
.type
== bfd_link_hash_defined
4539 || h
->root
.type
== bfd_link_hash_defweak
))
4541 relocation
= (h
->root
.u
.def
.value
4542 + h
->root
.u
.def
.section
->output_section
->vma
4543 + h
->root
.u
.def
.section
->output_offset
);
4546 && h
->root
.type
== bfd_link_hash_undefweak
)
4554 else if (r_type
== (unsigned int) RELOC_BASE10
4555 || r_type
== (unsigned int) RELOC_BASE13
4556 || r_type
== (unsigned int) RELOC_BASE22
)
4558 struct external_nlist
*sym
;
4561 /* For base relative relocs, r_index is always an index
4562 into the symbol table, even if r_extern is 0. */
4563 sym
= syms
+ r_index
;
4564 type
= H_GET_8 (input_bfd
, sym
->e_type
);
4565 if ((type
& N_TYPE
) == N_TEXT
4567 r_section
= obj_textsec (input_bfd
);
4568 else if ((type
& N_TYPE
) == N_DATA
4570 r_section
= obj_datasec (input_bfd
);
4571 else if ((type
& N_TYPE
) == N_BSS
4573 r_section
= obj_bsssec (input_bfd
);
4574 else if ((type
& N_TYPE
) == N_ABS
4576 r_section
= bfd_abs_section_ptr
;
4579 relocation
= (r_section
->output_section
->vma
4580 + r_section
->output_offset
4581 + (GET_WORD (input_bfd
, sym
->e_value
)
4586 r_section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4588 /* If this is a PC relative reloc, then R_ADDEND is the
4589 difference between the two vmas, or
4590 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4592 old_dest_sec == section->vma
4594 old_src_sec == input_section->vma
4596 old_src_off == r_addr
4598 _bfd_final_link_relocate expects RELOCATION +
4599 R_ADDEND to be the VMA of the destination minus
4600 r_addr (the minus r_addr is because this relocation
4601 is not pcrel_offset, which is a bit confusing and
4602 should, perhaps, be changed), or
4605 new_dest_sec == output_section->vma + output_offset
4606 We arrange for this to happen by setting RELOCATION to
4607 new_dest_sec + old_src_sec - old_dest_sec
4609 If this is not a PC relative reloc, then R_ADDEND is
4610 simply the VMA of the destination, so we set
4611 RELOCATION to the change in the destination VMA, or
4612 new_dest_sec - old_dest_sec
4614 relocation
= (r_section
->output_section
->vma
4615 + r_section
->output_offset
4617 if (howto_table_ext
[r_type
].pc_relative
)
4618 relocation
+= input_section
->vma
;
4621 if (check_dynamic_reloc
!= NULL
)
4625 if (! ((*check_dynamic_reloc
)
4626 (flaginfo
->info
, input_bfd
, input_section
, h
,
4627 (void *) rel
, contents
, &skip
, &relocation
)))
4633 /* Now warn if a global symbol is undefined. We could not
4634 do this earlier, because check_dynamic_reloc might want
4635 to skip this reloc. */
4637 && ! bfd_link_pic (flaginfo
->info
)
4638 && r_type
!= (unsigned int) RELOC_BASE10
4639 && r_type
!= (unsigned int) RELOC_BASE13
4640 && r_type
!= (unsigned int) RELOC_BASE22
)
4645 name
= h
->root
.root
.string
;
4647 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4648 (*flaginfo
->info
->callbacks
->undefined_symbol
)
4649 (flaginfo
->info
, name
, input_bfd
, input_section
, r_addr
, true);
4652 if (r_type
!= (unsigned int) RELOC_SPARC_REV32
)
4653 r
= MY_final_link_relocate (howto_table_ext
+ r_type
,
4654 input_bfd
, input_section
,
4655 contents
, r_addr
, relocation
,
4661 x
= bfd_get_32 (input_bfd
, contents
+ r_addr
);
4662 x
= x
+ relocation
+ r_addend
;
4663 bfd_putl32 (/*input_bfd,*/ x
, contents
+ r_addr
);
4667 if (r
!= bfd_reloc_ok
)
4672 case bfd_reloc_outofrange
:
4674 case bfd_reloc_overflow
:
4681 || r_type
== (unsigned int) RELOC_BASE10
4682 || r_type
== (unsigned int) RELOC_BASE13
4683 || r_type
== (unsigned int) RELOC_BASE22
)
4684 name
= strings
+ GET_WORD (input_bfd
,
4685 syms
[r_index
].e_strx
);
4690 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4691 name
= bfd_section_name (s
);
4693 (*flaginfo
->info
->callbacks
->reloc_overflow
)
4694 (flaginfo
->info
, (h
? &h
->root
: NULL
), name
,
4695 howto_table_ext
[r_type
].name
,
4696 r_addend
, input_bfd
, input_section
, r_addr
);
4707 /* Link an a.out section into the output file. */
4710 aout_link_input_section (struct aout_final_link_info
*flaginfo
,
4712 asection
*input_section
,
4713 file_ptr
*reloff_ptr
,
4714 bfd_size_type rel_size
)
4716 bfd_size_type input_size
;
4719 /* Get the section contents. */
4720 input_size
= input_section
->size
;
4721 if (! bfd_get_section_contents (input_bfd
, input_section
,
4722 (void *) flaginfo
->contents
,
4723 (file_ptr
) 0, input_size
))
4726 relocs
= flaginfo
->relocs
;
4729 if (bfd_seek (input_bfd
, input_section
->rel_filepos
, SEEK_SET
) != 0
4730 || bfd_read (relocs
, rel_size
, input_bfd
) != rel_size
)
4734 /* Relocate the section contents. */
4735 if (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
)
4737 if (! aout_link_input_section_std (flaginfo
, input_bfd
, input_section
,
4738 (struct reloc_std_external
*) relocs
,
4739 rel_size
, flaginfo
->contents
))
4744 if (! aout_link_input_section_ext (flaginfo
, input_bfd
, input_section
,
4745 (struct reloc_ext_external
*) relocs
,
4746 rel_size
, flaginfo
->contents
))
4750 /* Write out the section contents. */
4751 if (! bfd_set_section_contents (flaginfo
->output_bfd
,
4752 input_section
->output_section
,
4753 (void *) flaginfo
->contents
,
4754 (file_ptr
) input_section
->output_offset
,
4758 /* If we are producing relocatable output, the relocs were
4759 modified, and we now write them out. */
4760 if (bfd_link_relocatable (flaginfo
->info
) && rel_size
> 0)
4762 if (bfd_seek (flaginfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0)
4764 if (bfd_write (relocs
, rel_size
, flaginfo
->output_bfd
) != rel_size
)
4766 *reloff_ptr
+= rel_size
;
4768 /* Assert that the relocs have not run into the symbols, and
4769 that if these are the text relocs they have not run into the
4771 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (flaginfo
->output_bfd
)
4772 && (reloff_ptr
!= &flaginfo
->treloff
4774 <= obj_datasec (flaginfo
->output_bfd
)->rel_filepos
)));
4780 /* Adjust and write out the symbols for an a.out file. Set the new
4781 symbol indices into a symbol_map. */
4784 aout_link_write_symbols (struct aout_final_link_info
*flaginfo
, bfd
*input_bfd
)
4787 bfd_size_type sym_count
;
4789 enum bfd_link_strip strip
;
4790 enum bfd_link_discard discard
;
4791 struct external_nlist
*outsym
;
4792 bfd_size_type strtab_index
;
4793 struct external_nlist
*sym
;
4794 struct external_nlist
*sym_end
;
4795 struct aout_link_hash_entry
**sym_hash
;
4800 output_bfd
= flaginfo
->output_bfd
;
4801 sym_count
= obj_aout_external_sym_count (input_bfd
);
4802 strings
= obj_aout_external_strings (input_bfd
);
4803 strip
= flaginfo
->info
->strip
;
4804 discard
= flaginfo
->info
->discard
;
4805 outsym
= flaginfo
->output_syms
;
4807 /* First write out a symbol for this object file, unless we are
4808 discarding such symbols. */
4809 if (strip
!= strip_all
4810 && (strip
!= strip_some
4811 || bfd_hash_lookup (flaginfo
->info
->keep_hash
,
4812 bfd_get_filename (input_bfd
),
4813 false, false) != NULL
)
4814 && discard
!= discard_all
)
4816 H_PUT_8 (output_bfd
, N_TEXT
, outsym
->e_type
);
4817 H_PUT_8 (output_bfd
, 0, outsym
->e_other
);
4818 H_PUT_16 (output_bfd
, 0, outsym
->e_desc
);
4819 strtab_index
= add_to_stringtab (output_bfd
, flaginfo
->strtab
,
4820 bfd_get_filename (input_bfd
), false);
4821 if (strtab_index
== (bfd_size_type
) -1)
4823 PUT_WORD (output_bfd
, strtab_index
, outsym
->e_strx
);
4824 PUT_WORD (output_bfd
,
4825 (bfd_section_vma (obj_textsec (input_bfd
)->output_section
)
4826 + obj_textsec (input_bfd
)->output_offset
),
4828 ++obj_aout_external_sym_count (output_bfd
);
4834 sym
= obj_aout_external_syms (input_bfd
);
4835 sym_end
= sym
+ sym_count
;
4836 sym_hash
= obj_aout_sym_hashes (input_bfd
);
4837 symbol_map
= flaginfo
->symbol_map
;
4838 memset (symbol_map
, 0, (size_t) sym_count
* sizeof *symbol_map
);
4839 for (; sym
< sym_end
; sym
++, sym_hash
++, symbol_map
++)
4843 struct aout_link_hash_entry
*h
;
4849 /* We set *symbol_map to 0 above for all symbols. If it has
4850 already been set to -1 for this symbol, it means that we are
4851 discarding it because it appears in a duplicate header file.
4852 See the N_BINCL code below. */
4853 if (*symbol_map
== -1)
4856 /* Initialize *symbol_map to -1, which means that the symbol was
4857 not copied into the output file. We will change it later if
4858 we do copy the symbol over. */
4861 type
= H_GET_8 (input_bfd
, sym
->e_type
);
4862 name
= strings
+ GET_WORD (input_bfd
, sym
->e_strx
);
4868 /* Pass this symbol through. It is the target of an
4869 indirect or warning symbol. */
4870 val
= GET_WORD (input_bfd
, sym
->e_value
);
4875 /* Skip this symbol, which is the target of an indirect
4876 symbol that we have changed to no longer be an indirect
4883 struct aout_link_hash_entry
*hresolve
;
4885 /* We have saved the hash table entry for this symbol, if
4886 there is one. Note that we could just look it up again
4887 in the hash table, provided we first check that it is an
4891 /* Use the name from the hash table, in case the symbol was
4894 && h
->root
.type
!= bfd_link_hash_warning
)
4895 name
= h
->root
.root
.string
;
4897 /* If this is an indirect or warning symbol, then change
4898 hresolve to the base symbol. We also change *sym_hash so
4899 that the relocation routines relocate against the real
4902 if (h
!= (struct aout_link_hash_entry
*) NULL
4903 && (h
->root
.type
== bfd_link_hash_indirect
4904 || h
->root
.type
== bfd_link_hash_warning
))
4906 hresolve
= (struct aout_link_hash_entry
*) h
->root
.u
.i
.link
;
4907 while (hresolve
->root
.type
== bfd_link_hash_indirect
4908 || hresolve
->root
.type
== bfd_link_hash_warning
)
4909 hresolve
= ((struct aout_link_hash_entry
*)
4910 hresolve
->root
.u
.i
.link
);
4911 *sym_hash
= hresolve
;
4914 /* If the symbol has already been written out, skip it. */
4918 if ((type
& N_TYPE
) == N_INDR
4919 || type
== N_WARNING
)
4921 *symbol_map
= h
->indx
;
4925 /* See if we are stripping this symbol. */
4931 case strip_debugger
:
4932 if ((type
& N_STAB
) != 0)
4936 if (bfd_hash_lookup (flaginfo
->info
->keep_hash
, name
, false, false)
4951 /* Get the value of the symbol. */
4952 if ((type
& N_TYPE
) == N_TEXT
4954 symsec
= obj_textsec (input_bfd
);
4955 else if ((type
& N_TYPE
) == N_DATA
4957 symsec
= obj_datasec (input_bfd
);
4958 else if ((type
& N_TYPE
) == N_BSS
4960 symsec
= obj_bsssec (input_bfd
);
4961 else if ((type
& N_TYPE
) == N_ABS
4963 symsec
= bfd_abs_section_ptr
;
4964 else if (((type
& N_TYPE
) == N_INDR
4965 && (hresolve
== NULL
4966 || (hresolve
->root
.type
!= bfd_link_hash_defined
4967 && hresolve
->root
.type
!= bfd_link_hash_defweak
4968 && hresolve
->root
.type
!= bfd_link_hash_common
)))
4969 || type
== N_WARNING
)
4971 /* Pass the next symbol through unchanged. The
4972 condition above for indirect symbols is so that if
4973 the indirect symbol was defined, we output it with
4974 the correct definition so the debugger will
4977 val
= GET_WORD (input_bfd
, sym
->e_value
);
4980 else if ((type
& N_STAB
) != 0)
4982 val
= GET_WORD (input_bfd
, sym
->e_value
);
4987 /* If we get here with an indirect symbol, it means that
4988 we are outputting it with a real definition. In such
4989 a case we do not want to output the next symbol,
4990 which is the target of the indirection. */
4991 if ((type
& N_TYPE
) == N_INDR
)
4996 /* We need to get the value from the hash table. We use
4997 hresolve so that if we have defined an indirect
4998 symbol we output the final definition. */
5001 switch (type
& N_TYPE
)
5004 symsec
= obj_textsec (input_bfd
);
5007 symsec
= obj_datasec (input_bfd
);
5010 symsec
= obj_bsssec (input_bfd
);
5013 symsec
= bfd_abs_section_ptr
;
5020 else if (hresolve
->root
.type
== bfd_link_hash_defined
5021 || hresolve
->root
.type
== bfd_link_hash_defweak
)
5023 asection
*input_section
;
5024 asection
*output_section
;
5026 /* This case usually means a common symbol which was
5027 turned into a defined symbol. */
5028 input_section
= hresolve
->root
.u
.def
.section
;
5029 output_section
= input_section
->output_section
;
5030 BFD_ASSERT (bfd_is_abs_section (output_section
)
5031 || output_section
->owner
== output_bfd
);
5032 val
= (hresolve
->root
.u
.def
.value
5033 + bfd_section_vma (output_section
)
5034 + input_section
->output_offset
);
5036 /* Get the correct type based on the section. If
5037 this is a constructed set, force it to be
5038 globally visible. */
5047 if (output_section
== obj_textsec (output_bfd
))
5048 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
5051 else if (output_section
== obj_datasec (output_bfd
))
5052 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
5055 else if (output_section
== obj_bsssec (output_bfd
))
5056 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
5060 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
5064 else if (hresolve
->root
.type
== bfd_link_hash_common
)
5065 val
= hresolve
->root
.u
.c
.size
;
5066 else if (hresolve
->root
.type
== bfd_link_hash_undefweak
)
5075 val
= (symsec
->output_section
->vma
5076 + symsec
->output_offset
5077 + (GET_WORD (input_bfd
, sym
->e_value
)
5080 /* If this is a global symbol set the written flag, and if
5081 it is a local symbol see if we should discard it. */
5085 h
->indx
= obj_aout_external_sym_count (output_bfd
);
5087 else if ((type
& N_TYPE
) != N_SETT
5088 && (type
& N_TYPE
) != N_SETD
5089 && (type
& N_TYPE
) != N_SETB
5090 && (type
& N_TYPE
) != N_SETA
)
5095 case discard_sec_merge
:
5098 if ((type
& N_STAB
) == 0
5099 && bfd_is_local_label_name (input_bfd
, name
))
5113 /* An N_BINCL symbol indicates the start of the stabs
5114 entries for a header file. We need to scan ahead to the
5115 next N_EINCL symbol, ignoring nesting, adding up all the
5116 characters in the symbol names, not including the file
5117 numbers in types (the first number after an open
5119 if (type
== (int) N_BINCL
)
5121 struct external_nlist
*incl_sym
;
5123 struct aout_link_includes_entry
*incl_entry
;
5124 struct aout_link_includes_totals
*t
;
5128 for (incl_sym
= sym
+ 1; incl_sym
< sym_end
; incl_sym
++)
5132 incl_type
= H_GET_8 (input_bfd
, incl_sym
->e_type
);
5133 if (incl_type
== (int) N_EINCL
)
5139 else if (incl_type
== (int) N_BINCL
)
5145 s
= strings
+ GET_WORD (input_bfd
, incl_sym
->e_strx
);
5146 for (; *s
!= '\0'; s
++)
5151 /* Skip the file number. */
5153 while (ISDIGIT (*s
))
5161 /* If we have already included a header file with the
5162 same value, then replace this one with an N_EXCL
5164 copy
= !flaginfo
->info
->keep_memory
;
5165 incl_entry
= aout_link_includes_lookup (&flaginfo
->includes
,
5167 if (incl_entry
== NULL
)
5169 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
5170 if (t
->total
== val
)
5174 /* This is the first time we have seen this header
5175 file with this set of stabs strings. */
5176 t
= (struct aout_link_includes_totals
*)
5177 bfd_hash_allocate (&flaginfo
->includes
.root
,
5182 t
->next
= incl_entry
->totals
;
5183 incl_entry
->totals
= t
;
5189 /* This is a duplicate header file. We must change
5190 it to be an N_EXCL entry, and mark all the
5191 included symbols to prevent outputting them. */
5192 type
= (int) N_EXCL
;
5195 for (incl_sym
= sym
+ 1, incl_map
= symbol_map
+ 1;
5197 incl_sym
++, incl_map
++)
5201 incl_type
= H_GET_8 (input_bfd
, incl_sym
->e_type
);
5202 if (incl_type
== (int) N_EINCL
)
5211 else if (incl_type
== (int) N_BINCL
)
5220 /* Copy this symbol into the list of symbols we are going to
5222 H_PUT_8 (output_bfd
, type
, outsym
->e_type
);
5223 H_PUT_8 (output_bfd
, H_GET_8 (input_bfd
, sym
->e_other
), outsym
->e_other
);
5224 H_PUT_16 (output_bfd
, H_GET_16 (input_bfd
, sym
->e_desc
), outsym
->e_desc
);
5226 if (! flaginfo
->info
->keep_memory
)
5228 /* name points into a string table which we are going to
5229 free. If there is a hash table entry, use that string.
5230 Otherwise, copy name into memory. */
5232 name
= h
->root
.root
.string
;
5236 strtab_index
= add_to_stringtab (output_bfd
, flaginfo
->strtab
,
5238 if (strtab_index
== (bfd_size_type
) -1)
5240 PUT_WORD (output_bfd
, strtab_index
, outsym
->e_strx
);
5241 PUT_WORD (output_bfd
, val
, outsym
->e_value
);
5242 *symbol_map
= obj_aout_external_sym_count (output_bfd
);
5243 ++obj_aout_external_sym_count (output_bfd
);
5247 /* Write out the output symbols we have just constructed. */
5248 if (outsym
> flaginfo
->output_syms
)
5250 bfd_size_type outsym_size
;
5252 if (bfd_seek (output_bfd
, flaginfo
->symoff
, SEEK_SET
) != 0)
5254 outsym_size
= outsym
- flaginfo
->output_syms
;
5255 outsym_size
*= EXTERNAL_NLIST_SIZE
;
5256 if (bfd_write (flaginfo
->output_syms
, outsym_size
, output_bfd
)
5259 flaginfo
->symoff
+= outsym_size
;
5265 /* Link an a.out input BFD into the output file. */
5268 aout_link_input_bfd (struct aout_final_link_info
*flaginfo
, bfd
*input_bfd
)
5270 BFD_ASSERT (bfd_get_format (input_bfd
) == bfd_object
);
5272 /* If this is a dynamic object, it may need special handling. */
5273 if ((input_bfd
->flags
& DYNAMIC
) != 0
5274 && aout_backend_info (input_bfd
)->link_dynamic_object
!= NULL
)
5275 return ((*aout_backend_info (input_bfd
)->link_dynamic_object
)
5276 (flaginfo
->info
, input_bfd
));
5278 /* Get the symbols. We probably have them already, unless
5279 flaginfo->info->keep_memory is FALSE. */
5280 if (! aout_get_external_symbols (input_bfd
))
5283 /* Write out the symbols and get a map of the new indices. The map
5284 is placed into flaginfo->symbol_map. */
5285 if (! aout_link_write_symbols (flaginfo
, input_bfd
))
5288 /* Relocate and write out the sections. These functions use the
5289 symbol map created by aout_link_write_symbols. The linker_mark
5290 field will be set if these sections are to be included in the
5291 link, which will normally be the case. */
5292 if (obj_textsec (input_bfd
)->linker_mark
)
5294 if (! aout_link_input_section (flaginfo
, input_bfd
,
5295 obj_textsec (input_bfd
),
5297 exec_hdr (input_bfd
)->a_trsize
))
5300 if (obj_datasec (input_bfd
)->linker_mark
)
5302 if (! aout_link_input_section (flaginfo
, input_bfd
,
5303 obj_datasec (input_bfd
),
5305 exec_hdr (input_bfd
)->a_drsize
))
5309 /* If we are not keeping memory, we don't need the symbols any
5310 longer. We still need them if we are keeping memory, because the
5311 strings in the hash table point into them. */
5312 if (! flaginfo
->info
->keep_memory
)
5314 if (! aout_link_free_symbols (input_bfd
))
5321 /* Do the final link step. This is called on the output BFD. The
5322 INFO structure should point to a list of BFDs linked through the
5323 link.next field which can be used to find each BFD which takes part
5324 in the output. Also, each section in ABFD should point to a list
5325 of bfd_link_order structures which list all the input sections for
5326 the output section. */
5329 NAME (aout
, final_link
) (bfd
*abfd
,
5330 struct bfd_link_info
*info
,
5331 void (*callback
) (bfd
*, file_ptr
*, file_ptr
*, file_ptr
*))
5333 struct aout_final_link_info aout_info
;
5334 bool includes_hash_initialized
= false;
5336 bfd_size_type trsize
, drsize
;
5337 bfd_size_type max_contents_size
;
5338 bfd_size_type max_relocs_size
;
5339 bfd_size_type max_sym_count
;
5340 struct bfd_link_order
*p
;
5342 bool have_link_order_relocs
;
5344 if (bfd_link_pic (info
))
5345 abfd
->flags
|= DYNAMIC
;
5347 aout_info
.info
= info
;
5348 aout_info
.output_bfd
= abfd
;
5349 aout_info
.contents
= NULL
;
5350 aout_info
.relocs
= NULL
;
5351 aout_info
.symbol_map
= NULL
;
5352 aout_info
.output_syms
= NULL
;
5354 if (!bfd_hash_table_init_n (&aout_info
.includes
.root
,
5355 aout_link_includes_newfunc
,
5356 sizeof (struct aout_link_includes_entry
),
5359 includes_hash_initialized
= true;
5361 /* Figure out the largest section size. Also, if generating
5362 relocatable output, count the relocs. */
5365 max_contents_size
= 0;
5366 max_relocs_size
= 0;
5368 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
5372 if (bfd_link_relocatable (info
))
5374 if (bfd_get_flavour (sub
) == bfd_target_aout_flavour
)
5376 trsize
+= exec_hdr (sub
)->a_trsize
;
5377 drsize
+= exec_hdr (sub
)->a_drsize
;
5381 /* FIXME: We need to identify the .text and .data sections
5382 and call get_reloc_upper_bound and canonicalize_reloc to
5383 work out the number of relocs needed, and then multiply
5384 by the reloc size. */
5386 /* xgettext:c-format */
5387 (_("%pB: relocatable link from %s to %s not supported"),
5388 abfd
, sub
->xvec
->name
, abfd
->xvec
->name
);
5389 bfd_set_error (bfd_error_invalid_operation
);
5394 if (bfd_get_flavour (sub
) == bfd_target_aout_flavour
)
5396 sz
= obj_textsec (sub
)->size
;
5397 if (sz
> max_contents_size
)
5398 max_contents_size
= sz
;
5399 sz
= obj_datasec (sub
)->size
;
5400 if (sz
> max_contents_size
)
5401 max_contents_size
= sz
;
5403 sz
= exec_hdr (sub
)->a_trsize
;
5404 if (sz
> max_relocs_size
)
5405 max_relocs_size
= sz
;
5406 sz
= exec_hdr (sub
)->a_drsize
;
5407 if (sz
> max_relocs_size
)
5408 max_relocs_size
= sz
;
5410 sz
= obj_aout_external_sym_count (sub
);
5411 if (sz
> max_sym_count
)
5416 if (bfd_link_relocatable (info
))
5418 if (obj_textsec (abfd
) != NULL
)
5419 trsize
+= (_bfd_count_link_order_relocs (obj_textsec (abfd
)
5420 ->map_head
.link_order
)
5421 * obj_reloc_entry_size (abfd
));
5422 if (obj_datasec (abfd
) != NULL
)
5423 drsize
+= (_bfd_count_link_order_relocs (obj_datasec (abfd
)
5424 ->map_head
.link_order
)
5425 * obj_reloc_entry_size (abfd
));
5428 exec_hdr (abfd
)->a_trsize
= trsize
;
5429 exec_hdr (abfd
)->a_drsize
= drsize
;
5431 exec_hdr (abfd
)->a_entry
= bfd_get_start_address (abfd
);
5433 /* Adjust the section sizes and vmas according to the magic number.
5434 This sets a_text, a_data and a_bss in the exec_hdr and sets the
5435 filepos for each section. */
5436 if (! NAME (aout
, adjust_sizes_and_vmas
) (abfd
))
5439 /* The relocation and symbol file positions differ among a.out
5440 targets. We are passed a callback routine from the backend
5441 specific code to handle this.
5442 FIXME: At this point we do not know how much space the symbol
5443 table will require. This will not work for any (nonstandard)
5444 a.out target that needs to know the symbol table size before it
5445 can compute the relocation file positions. */
5446 (*callback
) (abfd
, &aout_info
.treloff
, &aout_info
.dreloff
,
5448 obj_textsec (abfd
)->rel_filepos
= aout_info
.treloff
;
5449 obj_datasec (abfd
)->rel_filepos
= aout_info
.dreloff
;
5450 obj_sym_filepos (abfd
) = aout_info
.symoff
;
5452 /* We keep a count of the symbols as we output them. */
5453 obj_aout_external_sym_count (abfd
) = 0;
5455 /* We accumulate the string table as we write out the symbols. */
5456 aout_info
.strtab
= _bfd_stringtab_init ();
5457 if (aout_info
.strtab
== NULL
)
5460 /* Allocate buffers to hold section contents and relocs. */
5461 aout_info
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5462 aout_info
.relocs
= bfd_malloc (max_relocs_size
);
5463 aout_info
.symbol_map
= (int *) bfd_malloc (max_sym_count
* sizeof (int));
5464 aout_info
.output_syms
= (struct external_nlist
*)
5465 bfd_malloc ((max_sym_count
+ 1) * sizeof (struct external_nlist
));
5466 if ((aout_info
.contents
== NULL
&& max_contents_size
!= 0)
5467 || (aout_info
.relocs
== NULL
&& max_relocs_size
!= 0)
5468 || (aout_info
.symbol_map
== NULL
&& max_sym_count
!= 0)
5469 || aout_info
.output_syms
== NULL
)
5472 /* If we have a symbol named __DYNAMIC, force it out now. This is
5473 required by SunOS. Doing this here rather than in sunos.c is a
5474 hack, but it's easier than exporting everything which would be
5477 struct aout_link_hash_entry
*h
;
5479 h
= aout_link_hash_lookup (aout_hash_table (info
), "__DYNAMIC",
5480 false, false, false);
5482 aout_link_write_other_symbol (&h
->root
.root
, &aout_info
);
5485 /* The most time efficient way to do the link would be to read all
5486 the input object files into memory and then sort out the
5487 information into the output file. Unfortunately, that will
5488 probably use too much memory. Another method would be to step
5489 through everything that composes the text section and write it
5490 out, and then everything that composes the data section and write
5491 it out, and then write out the relocs, and then write out the
5492 symbols. Unfortunately, that requires reading stuff from each
5493 input file several times, and we will not be able to keep all the
5494 input files open simultaneously, and reopening them will be slow.
5496 What we do is basically process one input file at a time. We do
5497 everything we need to do with an input file once--copy over the
5498 section contents, handle the relocation information, and write
5499 out the symbols--and then we throw away the information we read
5500 from it. This approach requires a lot of lseeks of the output
5501 file, which is unfortunate but still faster than reopening a lot
5504 We use the output_has_begun field of the input BFDs to see
5505 whether we have already handled it. */
5506 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
5507 sub
->output_has_begun
= false;
5509 /* Mark all sections which are to be included in the link. This
5510 will normally be every section. We need to do this so that we
5511 can identify any sections which the linker has decided to not
5513 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5515 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5516 if (p
->type
== bfd_indirect_link_order
)
5517 p
->u
.indirect
.section
->linker_mark
= true;
5520 have_link_order_relocs
= false;
5521 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5523 for (p
= o
->map_head
.link_order
;
5527 if (p
->type
== bfd_indirect_link_order
5528 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
5529 == bfd_target_aout_flavour
))
5533 input_bfd
= p
->u
.indirect
.section
->owner
;
5534 if (! input_bfd
->output_has_begun
)
5536 if (! aout_link_input_bfd (&aout_info
, input_bfd
))
5538 input_bfd
->output_has_begun
= true;
5541 else if (p
->type
== bfd_section_reloc_link_order
5542 || p
->type
== bfd_symbol_reloc_link_order
)
5544 /* These are handled below. */
5545 have_link_order_relocs
= true;
5549 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5555 /* Write out any symbols that we have not already written out. */
5556 bfd_hash_traverse (&info
->hash
->table
,
5557 aout_link_write_other_symbol
,
5560 /* Now handle any relocs we were asked to create by the linker.
5561 These did not come from any input file. We must do these after
5562 we have written out all the symbols, so that we know the symbol
5564 if (have_link_order_relocs
)
5566 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5568 for (p
= o
->map_head
.link_order
;
5572 if (p
->type
== bfd_section_reloc_link_order
5573 || p
->type
== bfd_symbol_reloc_link_order
)
5575 if (! aout_link_reloc_link_order (&aout_info
, o
, p
))
5582 free (aout_info
.contents
);
5583 aout_info
.contents
= NULL
;
5584 free (aout_info
.relocs
);
5585 aout_info
.relocs
= NULL
;
5586 free (aout_info
.symbol_map
);
5587 aout_info
.symbol_map
= NULL
;
5588 free (aout_info
.output_syms
);
5589 aout_info
.output_syms
= NULL
;
5591 if (includes_hash_initialized
)
5593 bfd_hash_table_free (&aout_info
.includes
.root
);
5594 includes_hash_initialized
= false;
5597 /* Finish up any dynamic linking we may be doing. */
5598 if (aout_backend_info (abfd
)->finish_dynamic_link
!= NULL
)
5600 if (! (*aout_backend_info (abfd
)->finish_dynamic_link
) (abfd
, info
))
5604 /* Update the header information. */
5605 abfd
->symcount
= obj_aout_external_sym_count (abfd
);
5606 exec_hdr (abfd
)->a_syms
= abfd
->symcount
* EXTERNAL_NLIST_SIZE
;
5607 obj_str_filepos (abfd
) = obj_sym_filepos (abfd
) + exec_hdr (abfd
)->a_syms
;
5608 obj_textsec (abfd
)->reloc_count
=
5609 exec_hdr (abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
);
5610 obj_datasec (abfd
)->reloc_count
=
5611 exec_hdr (abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
);
5613 /* Write out the string table, unless there are no symbols. */
5614 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0)
5616 if (abfd
->symcount
> 0)
5618 if (!emit_stringtab (abfd
, aout_info
.strtab
))
5623 bfd_byte b
[BYTES_IN_WORD
];
5625 memset (b
, 0, BYTES_IN_WORD
);
5626 if (bfd_write (b
, BYTES_IN_WORD
, abfd
) != BYTES_IN_WORD
)
5633 free (aout_info
.contents
);
5634 free (aout_info
.relocs
);
5635 free (aout_info
.symbol_map
);
5636 free (aout_info
.output_syms
);
5637 if (includes_hash_initialized
)
5638 bfd_hash_table_free (&aout_info
.includes
.root
);