1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright (C) 1990-2024 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
, *oldrawptr
;
501 size_t amt
= sizeof (*rawptr
);
503 rawptr
= bfd_zalloc (abfd
, amt
);
507 oldrawptr
= abfd
->tdata
.aout_data
;
508 abfd
->tdata
.aout_data
= rawptr
;
510 /* Copy the contents of the old tdata struct. */
511 if (oldrawptr
!= NULL
)
512 *abfd
->tdata
.aout_data
= *oldrawptr
;
514 abfd
->tdata
.aout_data
->a
.hdr
= &rawptr
->e
;
515 /* Copy in the internal_exec struct. */
516 *(abfd
->tdata
.aout_data
->a
.hdr
) = *execp
;
517 execp
= abfd
->tdata
.aout_data
->a
.hdr
;
519 /* Set the file flags. */
520 abfd
->flags
= BFD_NO_FLAGS
;
521 if (execp
->a_drsize
|| execp
->a_trsize
)
522 abfd
->flags
|= HAS_RELOC
;
523 /* Setting of EXEC_P has been deferred to the bottom of this function. */
525 abfd
->flags
|= HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
;
526 if (N_DYNAMIC (execp
))
527 abfd
->flags
|= DYNAMIC
;
529 if (N_MAGIC (execp
) == ZMAGIC
)
531 abfd
->flags
|= D_PAGED
| WP_TEXT
;
532 adata (abfd
).magic
= z_magic
;
534 else if (N_IS_QMAGIC (execp
))
536 abfd
->flags
|= D_PAGED
| WP_TEXT
;
537 adata (abfd
).magic
= z_magic
;
538 adata (abfd
).subformat
= q_magic_format
;
540 else if (N_MAGIC (execp
) == NMAGIC
)
542 abfd
->flags
|= WP_TEXT
;
543 adata (abfd
).magic
= n_magic
;
545 else if (N_MAGIC (execp
) == OMAGIC
|| N_IS_BMAGIC (execp
))
546 adata (abfd
).magic
= o_magic
;
548 /* Should have been checked with N_BADMAG before this routine
552 abfd
->start_address
= execp
->a_entry
;
554 abfd
->symcount
= execp
->a_syms
/ sizeof (struct external_nlist
);
556 /* The default relocation entry size is that of traditional V7 Unix. */
557 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
559 /* The default symbol entry size is that of traditional Unix. */
560 obj_symbol_entry_size (abfd
) = EXTERNAL_NLIST_SIZE
;
562 if (! NAME (aout
, make_sections
) (abfd
))
565 obj_datasec (abfd
)->size
= execp
->a_data
;
566 obj_bsssec (abfd
)->size
= execp
->a_bss
;
568 obj_textsec (abfd
)->flags
=
569 (execp
->a_trsize
!= 0
570 ? (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
| SEC_RELOC
)
571 : (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
));
572 obj_datasec (abfd
)->flags
=
573 (execp
->a_drsize
!= 0
574 ? (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_RELOC
)
575 : (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
));
576 obj_bsssec (abfd
)->flags
= SEC_ALLOC
;
578 #ifdef THIS_IS_ONLY_DOCUMENTATION
579 /* The common code can't fill in these things because they depend
580 on either the start address of the text segment, the rounding
581 up of virtual addresses between segments, or the starting file
582 position of the text segment -- all of which varies among different
583 versions of a.out. */
585 /* Call back to the format-dependent code to fill in the rest of the
586 fields and do any further cleanup. Things that should be filled
587 in by the callback: */
588 struct exec
*execp
= exec_hdr (abfd
);
590 obj_textsec (abfd
)->size
= N_TXTSIZE (execp
);
591 /* Data and bss are already filled in since they're so standard. */
593 /* The virtual memory addresses of the sections. */
594 obj_textsec (abfd
)->vma
= N_TXTADDR (execp
);
595 obj_datasec (abfd
)->vma
= N_DATADDR (execp
);
596 obj_bsssec (abfd
)->vma
= N_BSSADDR (execp
);
598 /* The file offsets of the sections. */
599 obj_textsec (abfd
)->filepos
= N_TXTOFF (execp
);
600 obj_datasec (abfd
)->filepos
= N_DATOFF (execp
);
602 /* The file offsets of the relocation info. */
603 obj_textsec (abfd
)->rel_filepos
= N_TRELOFF (execp
);
604 obj_datasec (abfd
)->rel_filepos
= N_DRELOFF (execp
);
606 /* The file offsets of the string table and symbol table. */
607 obj_str_filepos (abfd
) = N_STROFF (execp
);
608 obj_sym_filepos (abfd
) = N_SYMOFF (execp
);
610 /* Determine the architecture and machine type of the object file. */
611 abfd
->obj_arch
= bfd_arch_obscure
;
613 adata (abfd
)->page_size
= TARGET_PAGE_SIZE
;
614 adata (abfd
)->segment_size
= SEGMENT_SIZE
;
615 adata (abfd
)->exec_bytes_size
= EXEC_BYTES_SIZE
;
617 return _bfd_no_cleanup
;
619 /* The architecture is encoded in various ways in various a.out variants,
620 or is not encoded at all in some of them. The relocation size depends
621 on the architecture and the a.out variant. Finally, the return value
622 is the bfd_target vector in use. If an error occurs, return zero and
623 set bfd_error to the appropriate error code.
625 Formats such as b.out, which have additional fields in the a.out
626 header, should cope with them in this callback as well. */
627 #endif /* DOCUMENTATION */
629 result
= (*callback_to_real_object_p
) (abfd
);
631 /* Now that the segment addresses have been worked out, take a better
632 guess at whether the file is executable. If the entry point
633 is within the text segment, assume it is. (This makes files
634 executable even if their entry point address is 0, as long as
635 their text starts at zero.).
637 This test had to be changed to deal with systems where the text segment
638 runs at a different location than the default. The problem is that the
639 entry address can appear to be outside the text segment, thus causing an
640 erroneous conclusion that the file isn't executable.
642 To fix this, we now accept any non-zero entry point as an indication of
643 executability. This will work most of the time, since only the linker
644 sets the entry point, and that is likely to be non-zero for most systems. */
646 if (execp
->a_entry
!= 0
647 || (execp
->a_entry
>= obj_textsec (abfd
)->vma
648 && execp
->a_entry
< (obj_textsec (abfd
)->vma
649 + obj_textsec (abfd
)->size
)
650 && execp
->a_trsize
== 0
651 && execp
->a_drsize
== 0))
652 abfd
->flags
|= EXEC_P
;
656 struct stat stat_buf
;
658 /* The original heuristic doesn't work in some important cases.
659 The a.out file has no information about the text start
660 address. For files (like kernels) linked to non-standard
661 addresses (ld -Ttext nnn) the entry point may not be between
662 the default text start (obj_textsec(abfd)->vma) and
663 (obj_textsec(abfd)->vma) + text size. This is not just a mach
664 issue. Many kernels are loaded at non standard addresses. */
665 if (abfd
->iostream
!= NULL
666 && (abfd
->flags
& BFD_IN_MEMORY
) == 0
667 && (fstat (fileno ((FILE *) (abfd
->iostream
)), &stat_buf
) == 0)
668 && ((stat_buf
.st_mode
& 0111) != 0))
669 abfd
->flags
|= EXEC_P
;
671 #endif /* STAT_FOR_EXEC */
677 bfd_release (abfd
, rawptr
);
678 abfd
->tdata
.aout_data
= oldrawptr
;
684 aout_@var{size}_mkobject
687 bool aout_@var{size}_mkobject, (bfd *abfd);
690 Initialize BFD @var{abfd} for use with a.out files.
694 NAME (aout
, mkobject
) (bfd
*abfd
)
696 struct aout_data_struct
*rawptr
;
697 size_t amt
= sizeof (* rawptr
);
699 bfd_set_error (bfd_error_system_call
);
701 rawptr
= (struct aout_data_struct
*) bfd_zalloc (abfd
, amt
);
705 abfd
->tdata
.aout_data
= rawptr
;
706 exec_hdr (abfd
) = &(rawptr
->e
);
708 obj_textsec (abfd
) = NULL
;
709 obj_datasec (abfd
) = NULL
;
710 obj_bsssec (abfd
) = NULL
;
717 aout_@var{size}_machine_type
720 enum machine_type aout_@var{size}_machine_type
721 (enum bfd_architecture arch,
722 unsigned long machine,
726 Keep track of machine architecture and machine type for
727 a.out's. Return the <<machine_type>> for a particular
728 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
729 and machine can't be represented in a.out format.
731 If the architecture is understood, machine type 0 (default)
732 is always understood.
736 NAME (aout
, machine_type
) (enum bfd_architecture arch
,
737 unsigned long machine
,
740 enum machine_type arch_flags
;
742 arch_flags
= M_UNKNOWN
;
749 || machine
== bfd_mach_sparc
750 || machine
== bfd_mach_sparc_sparclite
751 || machine
== bfd_mach_sparc_sparclite_le
752 || machine
== bfd_mach_sparc_v8plus
753 || machine
== bfd_mach_sparc_v8plusa
754 || machine
== bfd_mach_sparc_v8plusb
755 || machine
== bfd_mach_sparc_v8plusc
756 || machine
== bfd_mach_sparc_v8plusd
757 || machine
== bfd_mach_sparc_v8pluse
758 || machine
== bfd_mach_sparc_v8plusv
759 || machine
== bfd_mach_sparc_v8plusm
760 || machine
== bfd_mach_sparc_v8plusm8
761 || machine
== bfd_mach_sparc_v9
762 || machine
== bfd_mach_sparc_v9a
763 || machine
== bfd_mach_sparc_v9b
764 || machine
== bfd_mach_sparc_v9c
765 || machine
== bfd_mach_sparc_v9d
766 || machine
== bfd_mach_sparc_v9e
767 || machine
== bfd_mach_sparc_v9v
768 || machine
== bfd_mach_sparc_v9m
769 || machine
== bfd_mach_sparc_v9m8
)
770 arch_flags
= M_SPARC
;
771 else if (machine
== bfd_mach_sparc_sparclet
)
772 arch_flags
= M_SPARCLET
;
777 || machine
== bfd_mach_i386_i386
778 || machine
== bfd_mach_i386_i386_intel_syntax
)
791 case bfd_mach_mips3000
:
792 case bfd_mach_mips3900
:
793 arch_flags
= M_MIPS1
;
795 case bfd_mach_mips6000
:
796 arch_flags
= M_MIPS2
;
798 case bfd_mach_mips4000
:
799 case bfd_mach_mips4010
:
800 case bfd_mach_mips4100
:
801 case bfd_mach_mips4300
:
802 case bfd_mach_mips4400
:
803 case bfd_mach_mips4600
:
804 case bfd_mach_mips4650
:
805 case bfd_mach_mips8000
:
806 case bfd_mach_mips9000
:
807 case bfd_mach_mips10000
:
808 case bfd_mach_mips12000
:
809 case bfd_mach_mips14000
:
810 case bfd_mach_mips16000
:
811 case bfd_mach_mips16
:
812 case bfd_mach_mipsisa32
:
813 case bfd_mach_mipsisa32r2
:
814 case bfd_mach_mipsisa32r3
:
815 case bfd_mach_mipsisa32r5
:
816 case bfd_mach_mipsisa32r6
:
818 case bfd_mach_mipsisa64
:
819 case bfd_mach_mipsisa64r2
:
820 case bfd_mach_mipsisa64r3
:
821 case bfd_mach_mipsisa64r5
:
822 case bfd_mach_mipsisa64r6
:
823 case bfd_mach_mips_sb1
:
824 case bfd_mach_mips_xlr
:
825 /* FIXME: These should be MIPS3, MIPS4, MIPS16, MIPS32, etc. */
826 arch_flags
= M_MIPS2
;
829 arch_flags
= M_UNKNOWN
;
837 case 0: arch_flags
= M_NS32532
; break;
838 case 32032: arch_flags
= M_NS32032
; break;
839 case 32532: arch_flags
= M_NS32532
; break;
840 default: arch_flags
= M_UNKNOWN
; break;
849 if (machine
== 0 || machine
== 255)
854 arch_flags
= M_UNKNOWN
;
857 if (arch_flags
!= M_UNKNOWN
)
865 aout_@var{size}_set_arch_mach
868 bool aout_@var{size}_set_arch_mach,
870 enum bfd_architecture arch,
871 unsigned long machine);
874 Set the architecture and the machine of the BFD @var{abfd} to the
875 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
876 can support the architecture required.
880 NAME (aout
, set_arch_mach
) (bfd
*abfd
,
881 enum bfd_architecture arch
,
882 unsigned long machine
)
884 if (! bfd_default_set_arch_mach (abfd
, arch
, machine
))
887 if (arch
!= bfd_arch_unknown
)
891 NAME (aout
, machine_type
) (arch
, machine
, &unknown
);
896 /* Determine the size of a relocation entry. */
901 obj_reloc_entry_size (abfd
) = RELOC_EXT_SIZE
;
904 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
908 return (*aout_backend_info (abfd
)->set_sizes
) (abfd
);
912 adjust_o_magic (bfd
*abfd
, struct internal_exec
*execp
)
914 file_ptr pos
= adata (abfd
).exec_bytes_size
;
917 asection
*text
= obj_textsec (abfd
);
918 asection
*data
= obj_datasec (abfd
);
919 asection
*bss
= obj_bsssec (abfd
);
923 if (!text
->user_set_vma
)
928 pos
+= execp
->a_text
;
929 vma
+= execp
->a_text
;
932 if (!data
->user_set_vma
)
940 execp
->a_text
+= pad
;
947 if (!bss
->user_set_vma
)
955 /* The VMA of the .bss section is set by the VMA of the
956 .data section plus the size of the .data section. We may
957 need to add padding bytes to make this true. */
958 pad
= bss
->vma
- vma
;
963 execp
->a_data
= data
->size
+ pad
;
965 execp
->a_bss
= bss
->size
;
967 N_SET_MAGIC (execp
, OMAGIC
);
971 adjust_z_magic (bfd
*abfd
, struct internal_exec
*execp
)
973 bfd_size_type data_pad
, text_pad
;
975 const struct aout_backend_data
*abdp
;
976 /* TRUE if text includes exec header. */
978 asection
*text
= obj_textsec (abfd
);
979 asection
*data
= obj_datasec (abfd
);
980 asection
*bss
= obj_bsssec (abfd
);
982 abdp
= aout_backend_info (abfd
);
986 && (abdp
->text_includes_header
987 || obj_aout_subformat (abfd
) == q_magic_format
));
988 text
->filepos
= (ztih
989 ? adata (abfd
).exec_bytes_size
990 : adata (abfd
).zmagic_disk_block_size
);
991 if (!text
->user_set_vma
)
993 /* ?? Do we really need to check for relocs here? */
994 text
->vma
= ((abfd
->flags
& HAS_RELOC
)
997 ? abdp
->default_text_vma
+ adata (abfd
).exec_bytes_size
998 : abdp
->default_text_vma
));
1003 /* The .text section is being loaded at an unusual address. We
1004 may need to pad it such that the .data section starts at a page
1007 text_pad
= ((text
->filepos
- text
->vma
)
1008 & (adata (abfd
).page_size
- 1));
1010 text_pad
= (-text
->vma
1011 & (adata (abfd
).page_size
- 1));
1014 /* Find start of data. */
1017 text_end
= text
->filepos
+ execp
->a_text
;
1018 text_pad
+= BFD_ALIGN (text_end
, adata (abfd
).page_size
) - text_end
;
1022 /* Note that if page_size == zmagic_disk_block_size, then
1023 filepos == page_size, and this case is the same as the ztih
1025 text_end
= execp
->a_text
;
1026 text_pad
+= BFD_ALIGN (text_end
, adata (abfd
).page_size
) - text_end
;
1027 text_end
+= text
->filepos
;
1029 execp
->a_text
+= text_pad
;
1032 if (!data
->user_set_vma
)
1035 vma
= text
->vma
+ execp
->a_text
;
1036 data
->vma
= BFD_ALIGN (vma
, adata (abfd
).segment_size
);
1038 if (abdp
&& abdp
->zmagic_mapped_contiguous
)
1040 text_pad
= data
->vma
- (text
->vma
+ execp
->a_text
);
1041 /* Only pad the text section if the data
1042 section is going to be placed after it. */
1044 execp
->a_text
+= text_pad
;
1046 data
->filepos
= text
->filepos
+ execp
->a_text
;
1048 /* Fix up exec header while we're at it. */
1049 if (ztih
&& (!abdp
|| (abdp
&& !abdp
->exec_header_not_counted
)))
1050 execp
->a_text
+= adata (abfd
).exec_bytes_size
;
1051 if (obj_aout_subformat (abfd
) == q_magic_format
)
1052 N_SET_QMAGIC (execp
);
1054 N_SET_MAGIC (execp
, ZMAGIC
);
1056 /* Spec says data section should be rounded up to page boundary. */
1057 execp
->a_data
= align_power (data
->size
, bss
->alignment_power
);
1058 execp
->a_data
= BFD_ALIGN (execp
->a_data
, adata (abfd
).page_size
);
1059 data_pad
= execp
->a_data
- data
->size
;
1062 if (!bss
->user_set_vma
)
1063 bss
->vma
= data
->vma
+ execp
->a_data
;
1064 /* If the BSS immediately follows the data section and extra space
1065 in the page is left after the data section, fudge data
1066 in the header so that the bss section looks smaller by that
1067 amount. We'll start the bss section there, and lie to the OS.
1068 (Note that a linker script, as well as the above assignment,
1069 could have explicitly set the BSS vma to immediately follow
1070 the data section.) */
1071 if (align_power (bss
->vma
, bss
->alignment_power
) == data
->vma
+ execp
->a_data
)
1072 execp
->a_bss
= data_pad
> bss
->size
? 0 : bss
->size
- data_pad
;
1074 execp
->a_bss
= bss
->size
;
1078 adjust_n_magic (bfd
*abfd
, struct internal_exec
*execp
)
1080 file_ptr pos
= adata (abfd
).exec_bytes_size
;
1083 asection
*text
= obj_textsec (abfd
);
1084 asection
*data
= obj_datasec (abfd
);
1085 asection
*bss
= obj_bsssec (abfd
);
1088 text
->filepos
= pos
;
1089 if (!text
->user_set_vma
)
1093 pos
+= execp
->a_text
;
1094 vma
+= execp
->a_text
;
1097 data
->filepos
= pos
;
1098 if (!data
->user_set_vma
)
1099 data
->vma
= BFD_ALIGN (vma
, adata (abfd
).segment_size
);
1102 /* Since BSS follows data immediately, see if it needs alignment. */
1104 pad
= align_power (vma
, bss
->alignment_power
) - vma
;
1105 execp
->a_data
= data
->size
+ pad
;
1106 pos
+= execp
->a_data
;
1109 if (!bss
->user_set_vma
)
1114 /* Fix up exec header. */
1115 execp
->a_bss
= bss
->size
;
1116 N_SET_MAGIC (execp
, NMAGIC
);
1120 NAME (aout
, adjust_sizes_and_vmas
) (bfd
*abfd
)
1122 struct internal_exec
*execp
= exec_hdr (abfd
);
1124 if (! NAME (aout
, make_sections
) (abfd
))
1127 if (adata (abfd
).magic
!= undecided_magic
)
1130 execp
->a_text
= align_power (obj_textsec (abfd
)->size
,
1131 obj_textsec (abfd
)->alignment_power
);
1133 /* Rule (heuristic) for when to pad to a new page. Note that there
1134 are (at least) two ways demand-paged (ZMAGIC) files have been
1135 handled. Most Berkeley-based systems start the text segment at
1136 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1137 segment right after the exec header; the latter is counted in the
1138 text segment size, and is paged in by the kernel with the rest of
1141 /* This perhaps isn't the right way to do this, but made it simpler for me
1142 to understand enough to implement it. Better would probably be to go
1143 right from BFD flags to alignment/positioning characteristics. But the
1144 old code was sloppy enough about handling the flags, and had enough
1145 other magic, that it was a little hard for me to understand. I think
1146 I understand it better now, but I haven't time to do the cleanup this
1149 if (abfd
->flags
& D_PAGED
)
1150 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
1151 adata (abfd
).magic
= z_magic
;
1152 else if (abfd
->flags
& WP_TEXT
)
1153 adata (abfd
).magic
= n_magic
;
1155 adata (abfd
).magic
= o_magic
;
1157 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1159 fprintf (stderr
, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1161 switch (adata (abfd
).magic
)
1163 case n_magic
: str
= "NMAGIC"; break;
1164 case o_magic
: str
= "OMAGIC"; break;
1165 case z_magic
: str
= "ZMAGIC"; break;
1170 obj_textsec (abfd
)->vma
, obj_textsec (abfd
)->size
,
1171 obj_textsec (abfd
)->alignment_power
,
1172 obj_datasec (abfd
)->vma
, obj_datasec (abfd
)->size
,
1173 obj_datasec (abfd
)->alignment_power
,
1174 obj_bsssec (abfd
)->vma
, obj_bsssec (abfd
)->size
,
1175 obj_bsssec (abfd
)->alignment_power
);
1179 switch (adata (abfd
).magic
)
1182 adjust_o_magic (abfd
, execp
);
1185 adjust_z_magic (abfd
, execp
);
1188 adjust_n_magic (abfd
, execp
);
1194 #ifdef BFD_AOUT_DEBUG
1195 fprintf (stderr
, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1196 obj_textsec (abfd
)->vma
, execp
->a_text
,
1197 obj_textsec (abfd
)->filepos
,
1198 obj_datasec (abfd
)->vma
, execp
->a_data
,
1199 obj_datasec (abfd
)->filepos
,
1200 obj_bsssec (abfd
)->vma
, execp
->a_bss
);
1208 aout_@var{size}_new_section_hook
1211 bool aout_@var{size}_new_section_hook,
1216 Called by the BFD in response to a @code{bfd_make_section}
1220 NAME (aout
, new_section_hook
) (bfd
*abfd
, asection
*newsect
)
1222 /* Align to double at least. */
1223 newsect
->alignment_power
= bfd_get_arch_info (abfd
)->section_align_power
;
1225 if (bfd_get_format (abfd
) == bfd_object
)
1227 if (obj_textsec (abfd
) == NULL
&& !strcmp (newsect
->name
, ".text"))
1229 obj_textsec (abfd
)= newsect
;
1230 newsect
->target_index
= N_TEXT
;
1232 else if (obj_datasec (abfd
) == NULL
&& !strcmp (newsect
->name
, ".data"))
1234 obj_datasec (abfd
) = newsect
;
1235 newsect
->target_index
= N_DATA
;
1237 else if (obj_bsssec (abfd
) == NULL
&& !strcmp (newsect
->name
, ".bss"))
1239 obj_bsssec (abfd
) = newsect
;
1240 newsect
->target_index
= N_BSS
;
1244 /* We allow more than three sections internally. */
1245 return _bfd_generic_new_section_hook (abfd
, newsect
);
1249 NAME (aout
, set_section_contents
) (bfd
*abfd
,
1251 const void * location
,
1253 bfd_size_type count
)
1255 if (! abfd
->output_has_begun
)
1257 if (! NAME (aout
, adjust_sizes_and_vmas
) (abfd
))
1261 if (section
== obj_bsssec (abfd
))
1263 bfd_set_error (bfd_error_no_contents
);
1267 if (section
!= obj_textsec (abfd
)
1268 && section
!= obj_datasec (abfd
))
1270 if (aout_section_merge_with_text_p (abfd
, section
))
1271 section
->filepos
= obj_textsec (abfd
)->filepos
+
1272 (section
->vma
- obj_textsec (abfd
)->vma
);
1276 /* xgettext:c-format */
1277 (_("%pB: can not represent section `%pA' in a.out object file format"),
1279 bfd_set_error (bfd_error_nonrepresentable_section
);
1286 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0
1287 || bfd_write (location
, count
, abfd
) != count
)
1294 /* Read the external symbols from an a.out file. */
1297 aout_get_external_symbols (bfd
*abfd
)
1299 if (obj_aout_external_syms (abfd
) == NULL
)
1301 bfd_size_type count
;
1302 struct external_nlist
*syms
= NULL
;
1303 bfd_size_type amt
= exec_hdr (abfd
)->a_syms
;
1305 count
= amt
/ EXTERNAL_NLIST_SIZE
;
1309 /* We allocate using malloc to make the values easy to free
1310 later on. If we put them on the objalloc it might not be
1311 possible to free them. */
1312 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1314 syms
= _bfd_malloc_and_read (abfd
, amt
, amt
);
1318 obj_aout_external_syms (abfd
) = syms
;
1319 obj_aout_external_sym_count (abfd
) = count
;
1322 if (obj_aout_external_strings (abfd
) == NULL
1323 && exec_hdr (abfd
)->a_syms
!= 0)
1325 unsigned char string_chars
[BYTES_IN_WORD
];
1326 bfd_size_type stringsize
;
1328 bfd_size_type amt
= BYTES_IN_WORD
;
1330 /* Get the size of the strings. */
1331 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0
1332 || bfd_read (string_chars
, amt
, abfd
) != amt
)
1334 stringsize
= GET_WORD (abfd
, string_chars
);
1335 if (stringsize
== 0)
1337 else if (stringsize
+ 1 < BYTES_IN_WORD
+ 1
1338 || (size_t) stringsize
!= stringsize
)
1340 bfd_set_error (bfd_error_bad_value
);
1344 strings
= (char *) bfd_malloc (stringsize
+ 1);
1345 if (strings
== NULL
)
1348 if (stringsize
>= BYTES_IN_WORD
)
1350 amt
= stringsize
- BYTES_IN_WORD
;
1351 if (bfd_read (strings
+ BYTES_IN_WORD
, amt
, abfd
) != amt
)
1358 /* Ensure that a zero index yields an empty string. */
1359 if (stringsize
>= BYTES_IN_WORD
)
1360 memset (strings
, 0, BYTES_IN_WORD
);
1362 /* Ensure that the string buffer is NUL terminated. */
1363 strings
[stringsize
] = 0;
1365 obj_aout_external_strings (abfd
) = strings
;
1366 obj_aout_external_string_size (abfd
) = stringsize
;
1372 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1373 and symbol->value fields of CACHE_PTR will be set from the a.out
1374 nlist structure. This function is responsible for setting
1375 symbol->flags and symbol->section, and adjusting symbol->value. */
1378 translate_from_native_sym_flags (bfd
*abfd
, aout_symbol_type
*cache_ptr
)
1382 if ((cache_ptr
->type
& N_STAB
) != 0
1383 || cache_ptr
->type
== N_FN
)
1387 /* This is a debugging symbol. */
1388 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
;
1390 /* Work out the symbol section. */
1391 switch (cache_ptr
->type
& N_TYPE
)
1395 sec
= obj_textsec (abfd
);
1398 sec
= obj_datasec (abfd
);
1401 sec
= obj_bsssec (abfd
);
1405 sec
= bfd_abs_section_ptr
;
1409 cache_ptr
->symbol
.section
= sec
;
1410 cache_ptr
->symbol
.value
-= sec
->vma
;
1415 /* Get the default visibility. This does not apply to all types, so
1416 we just hold it in a local variable to use if wanted. */
1417 if ((cache_ptr
->type
& N_EXT
) == 0)
1418 visible
= BSF_LOCAL
;
1420 visible
= BSF_GLOBAL
;
1422 switch (cache_ptr
->type
)
1425 case N_ABS
: case N_ABS
| N_EXT
:
1426 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1427 cache_ptr
->symbol
.flags
= visible
;
1430 case N_UNDF
| N_EXT
:
1431 if (cache_ptr
->symbol
.value
!= 0)
1433 /* This is a common symbol. */
1434 cache_ptr
->symbol
.flags
= BSF_GLOBAL
;
1435 cache_ptr
->symbol
.section
= bfd_com_section_ptr
;
1439 cache_ptr
->symbol
.flags
= 0;
1440 cache_ptr
->symbol
.section
= bfd_und_section_ptr
;
1444 case N_TEXT
: case N_TEXT
| N_EXT
:
1445 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1446 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1447 cache_ptr
->symbol
.flags
= visible
;
1450 /* N_SETV symbols used to represent set vectors placed in the
1451 data section. They are no longer generated. Theoretically,
1452 it was possible to extract the entries and combine them with
1453 new ones, although I don't know if that was ever actually
1454 done. Unless that feature is restored, treat them as data
1456 case N_SETV
: case N_SETV
| N_EXT
:
1457 case N_DATA
: case N_DATA
| N_EXT
:
1458 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1459 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1460 cache_ptr
->symbol
.flags
= visible
;
1463 case N_BSS
: case N_BSS
| N_EXT
:
1464 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1465 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1466 cache_ptr
->symbol
.flags
= visible
;
1469 case N_SETA
: case N_SETA
| N_EXT
:
1470 case N_SETT
: case N_SETT
| N_EXT
:
1471 case N_SETD
: case N_SETD
| N_EXT
:
1472 case N_SETB
: case N_SETB
| N_EXT
:
1474 /* This code is no longer needed. It used to be used to make
1475 the linker handle set symbols, but they are now handled in
1476 the add_symbols routine instead. */
1477 switch (cache_ptr
->type
& N_TYPE
)
1480 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1483 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1486 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1489 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1493 cache_ptr
->symbol
.flags
|= BSF_CONSTRUCTOR
;
1498 /* This symbol is the text of a warning message. The next
1499 symbol is the symbol to associate the warning with. If a
1500 reference is made to that symbol, a warning is issued. */
1501 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_WARNING
;
1502 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1505 case N_INDR
: case N_INDR
| N_EXT
:
1506 /* An indirect symbol. This consists of two symbols in a row.
1507 The first symbol is the name of the indirection. The second
1508 symbol is the name of the target. A reference to the first
1509 symbol becomes a reference to the second. */
1510 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_INDIRECT
| visible
;
1511 cache_ptr
->symbol
.section
= bfd_ind_section_ptr
;
1515 cache_ptr
->symbol
.section
= bfd_und_section_ptr
;
1516 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1520 cache_ptr
->symbol
.section
= bfd_abs_section_ptr
;
1521 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1525 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1526 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1527 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1531 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1532 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1533 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1537 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1538 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1539 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1546 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1549 translate_to_native_sym_flags (bfd
*abfd
,
1551 struct external_nlist
*sym_pointer
)
1553 bfd_vma value
= cache_ptr
->value
;
1557 /* Mask out any existing type bits in case copying from one section
1559 sym_pointer
->e_type
[0] &= ~N_TYPE
;
1561 sec
= bfd_asymbol_section (cache_ptr
);
1566 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1569 /* xgettext:c-format */
1570 (_("%pB: can not represent section for symbol `%s' in a.out "
1571 "object file format"),
1573 cache_ptr
->name
!= NULL
? cache_ptr
->name
: _("*unknown*"));
1574 bfd_set_error (bfd_error_nonrepresentable_section
);
1578 if (sec
->output_section
!= NULL
)
1580 off
= sec
->output_offset
;
1581 sec
= sec
->output_section
;
1584 if (bfd_is_abs_section (sec
))
1585 sym_pointer
->e_type
[0] |= N_ABS
;
1586 else if (sec
== obj_textsec (abfd
))
1587 sym_pointer
->e_type
[0] |= N_TEXT
;
1588 else if (sec
== obj_datasec (abfd
))
1589 sym_pointer
->e_type
[0] |= N_DATA
;
1590 else if (sec
== obj_bsssec (abfd
))
1591 sym_pointer
->e_type
[0] |= N_BSS
;
1592 else if (bfd_is_und_section (sec
))
1593 sym_pointer
->e_type
[0] = N_UNDF
| N_EXT
;
1594 else if (bfd_is_ind_section (sec
))
1595 sym_pointer
->e_type
[0] = N_INDR
;
1596 else if (bfd_is_com_section (sec
))
1597 sym_pointer
->e_type
[0] = N_UNDF
| N_EXT
;
1600 if (aout_section_merge_with_text_p (abfd
, sec
))
1601 sym_pointer
->e_type
[0] |= N_TEXT
;
1605 /* xgettext:c-format */
1606 (_("%pB: can not represent section `%pA' in a.out object file format"),
1608 bfd_set_error (bfd_error_nonrepresentable_section
);
1613 /* Turn the symbol from section relative to absolute again. */
1614 value
+= sec
->vma
+ off
;
1616 if ((cache_ptr
->flags
& BSF_WARNING
) != 0)
1617 sym_pointer
->e_type
[0] = N_WARNING
;
1619 if ((cache_ptr
->flags
& BSF_DEBUGGING
) != 0)
1620 sym_pointer
->e_type
[0] = ((aout_symbol_type
*) cache_ptr
)->type
;
1621 else if ((cache_ptr
->flags
& BSF_GLOBAL
) != 0)
1622 sym_pointer
->e_type
[0] |= N_EXT
;
1623 else if ((cache_ptr
->flags
& BSF_LOCAL
) != 0)
1624 sym_pointer
->e_type
[0] &= ~N_EXT
;
1626 if ((cache_ptr
->flags
& BSF_CONSTRUCTOR
) != 0)
1628 int type
= ((aout_symbol_type
*) cache_ptr
)->type
;
1632 case N_ABS
: type
= N_SETA
; break;
1633 case N_TEXT
: type
= N_SETT
; break;
1634 case N_DATA
: type
= N_SETD
; break;
1635 case N_BSS
: type
= N_SETB
; break;
1637 sym_pointer
->e_type
[0] = type
;
1640 if ((cache_ptr
->flags
& BSF_WEAK
) != 0)
1644 switch (sym_pointer
->e_type
[0] & N_TYPE
)
1647 case N_ABS
: type
= N_WEAKA
; break;
1648 case N_TEXT
: type
= N_WEAKT
; break;
1649 case N_DATA
: type
= N_WEAKD
; break;
1650 case N_BSS
: type
= N_WEAKB
; break;
1651 case N_UNDF
: type
= N_WEAKU
; break;
1653 sym_pointer
->e_type
[0] = type
;
1656 PUT_WORD (abfd
, value
, sym_pointer
->e_value
);
1661 /* Native-level interface to symbols. */
1664 NAME (aout
, make_empty_symbol
) (bfd
*abfd
)
1666 size_t amt
= sizeof (aout_symbol_type
);
1668 aout_symbol_type
*new_symbol
= (aout_symbol_type
*) bfd_zalloc (abfd
, amt
);
1671 new_symbol
->symbol
.the_bfd
= abfd
;
1673 return &new_symbol
->symbol
;
1676 /* Translate a set of external symbols into internal symbols. */
1679 NAME (aout
, translate_symbol_table
) (bfd
*abfd
,
1680 aout_symbol_type
*in
,
1681 struct external_nlist
*ext
,
1682 bfd_size_type count
,
1684 bfd_size_type strsize
,
1687 struct external_nlist
*ext_end
;
1689 ext_end
= ext
+ count
;
1690 for (; ext
< ext_end
; ext
++, in
++)
1694 x
= GET_WORD (abfd
, ext
->e_strx
);
1695 in
->symbol
.the_bfd
= abfd
;
1697 /* For the normal symbols, the zero index points at the number
1698 of bytes in the string table but is to be interpreted as the
1699 null string. For the dynamic symbols, the number of bytes in
1700 the string table is stored in the __DYNAMIC structure and the
1701 zero index points at an actual string. */
1702 if (x
== 0 && ! dynamic
)
1703 in
->symbol
.name
= "";
1704 else if (x
< strsize
)
1705 in
->symbol
.name
= str
+ x
;
1709 (_("%pB: invalid string offset %" PRIu64
" >= %" PRIu64
),
1710 abfd
, (uint64_t) x
, (uint64_t) strsize
);
1711 bfd_set_error (bfd_error_bad_value
);
1715 in
->symbol
.value
= GET_SWORD (abfd
, ext
->e_value
);
1716 in
->desc
= H_GET_16 (abfd
, ext
->e_desc
);
1717 in
->other
= H_GET_8 (abfd
, ext
->e_other
);
1718 in
->type
= H_GET_8 (abfd
, ext
->e_type
);
1719 in
->symbol
.udata
.p
= NULL
;
1721 if (! translate_from_native_sym_flags (abfd
, in
))
1725 in
->symbol
.flags
|= BSF_DYNAMIC
;
1731 /* We read the symbols into a buffer, which is discarded when this
1732 function exits. We read the strings into a buffer large enough to
1733 hold them all plus all the cached symbol entries. */
1736 NAME (aout
, slurp_symbol_table
) (bfd
*abfd
)
1738 struct external_nlist
*old_external_syms
;
1739 aout_symbol_type
*cached
;
1740 bfd_size_type cached_size
;
1742 /* If there's no work to be done, don't do any. */
1743 if (obj_aout_symbols (abfd
) != NULL
)
1746 old_external_syms
= obj_aout_external_syms (abfd
);
1748 if (! aout_get_external_symbols (abfd
))
1751 cached_size
= obj_aout_external_sym_count (abfd
);
1752 if (cached_size
== 0)
1753 return true; /* Nothing to do. */
1755 cached_size
*= sizeof (aout_symbol_type
);
1756 cached
= (aout_symbol_type
*) bfd_zmalloc (cached_size
);
1760 /* Convert from external symbol information to internal. */
1761 if (! (NAME (aout
, translate_symbol_table
)
1763 obj_aout_external_syms (abfd
),
1764 obj_aout_external_sym_count (abfd
),
1765 obj_aout_external_strings (abfd
),
1766 obj_aout_external_string_size (abfd
),
1773 abfd
->symcount
= obj_aout_external_sym_count (abfd
);
1775 obj_aout_symbols (abfd
) = cached
;
1777 /* It is very likely that anybody who calls this function will not
1778 want the external symbol information, so if it was allocated
1779 because of our call to aout_get_external_symbols, we free it up
1780 right away to save space. */
1781 if (old_external_syms
== NULL
1782 && obj_aout_external_syms (abfd
) != NULL
)
1784 free (obj_aout_external_syms (abfd
));
1785 obj_aout_external_syms (abfd
) = NULL
;
1791 /* We use a hash table when writing out symbols so that we only write
1792 out a particular string once. This helps particularly when the
1793 linker writes out stabs debugging entries, because each different
1794 contributing object file tends to have many duplicate stabs
1797 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1798 if BFD_TRADITIONAL_FORMAT is set. */
1800 /* Get the index of a string in a strtab, adding it if it is not
1803 static inline bfd_size_type
1804 add_to_stringtab (bfd
*abfd
,
1805 struct bfd_strtab_hash
*tab
,
1810 bfd_size_type str_index
;
1812 /* An index of 0 always means the empty string. */
1813 if (str
== 0 || *str
== '\0')
1816 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1817 doesn't understand a hashed string table. */
1819 if ((abfd
->flags
& BFD_TRADITIONAL_FORMAT
) != 0)
1822 str_index
= _bfd_stringtab_add (tab
, str
, hash
, copy
);
1824 if (str_index
!= (bfd_size_type
) -1)
1825 /* Add BYTES_IN_WORD to the return value to account for the
1826 space taken up by the string table size. */
1827 str_index
+= BYTES_IN_WORD
;
1832 /* Write out a strtab. ABFD is already at the right location in the
1836 emit_stringtab (bfd
*abfd
, struct bfd_strtab_hash
*tab
)
1838 bfd_byte buffer
[BYTES_IN_WORD
];
1839 size_t amt
= BYTES_IN_WORD
;
1841 /* The string table starts with the size. */
1842 PUT_WORD (abfd
, _bfd_stringtab_size (tab
) + BYTES_IN_WORD
, buffer
);
1843 if (bfd_write (buffer
, amt
, abfd
) != amt
)
1846 return _bfd_stringtab_emit (abfd
, tab
);
1850 NAME (aout
, write_syms
) (bfd
*abfd
)
1852 unsigned int count
;
1853 asymbol
**generic
= bfd_get_outsymbols (abfd
);
1854 struct bfd_strtab_hash
*strtab
;
1856 strtab
= _bfd_stringtab_init ();
1860 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
1862 asymbol
*g
= generic
[count
];
1864 struct external_nlist nsp
;
1867 indx
= add_to_stringtab (abfd
, strtab
, g
->name
, false);
1868 if (indx
== (bfd_size_type
) -1)
1870 PUT_WORD (abfd
, indx
, (bfd_byte
*) nsp
.e_strx
);
1872 if (bfd_asymbol_flavour (g
) == abfd
->xvec
->flavour
)
1874 H_PUT_16 (abfd
, aout_symbol (g
)->desc
, nsp
.e_desc
);
1875 H_PUT_8 (abfd
, aout_symbol (g
)->other
, nsp
.e_other
);
1876 H_PUT_8 (abfd
, aout_symbol (g
)->type
, nsp
.e_type
);
1880 H_PUT_16 (abfd
, 0, nsp
.e_desc
);
1881 H_PUT_8 (abfd
, 0, nsp
.e_other
);
1882 H_PUT_8 (abfd
, 0, nsp
.e_type
);
1885 if (! translate_to_native_sym_flags (abfd
, g
, &nsp
))
1888 amt
= EXTERNAL_NLIST_SIZE
;
1889 if (bfd_write (&nsp
, amt
, abfd
) != amt
)
1892 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1893 here, at the end. */
1897 if (! emit_stringtab (abfd
, strtab
))
1900 _bfd_stringtab_free (strtab
);
1905 _bfd_stringtab_free (strtab
);
1910 NAME (aout
, canonicalize_symtab
) (bfd
*abfd
, asymbol
**location
)
1912 unsigned int counter
= 0;
1913 aout_symbol_type
*symbase
;
1915 if (!NAME (aout
, slurp_symbol_table
) (abfd
))
1918 for (symbase
= obj_aout_symbols (abfd
);
1919 counter
++ < bfd_get_symcount (abfd
);
1921 *(location
++) = (asymbol
*) (symbase
++);
1923 return bfd_get_symcount (abfd
);
1926 /* Standard reloc stuff. */
1927 /* Output standard relocation information to a file in target byte order. */
1929 extern void NAME (aout
, swap_std_reloc_out
)
1930 (bfd
*, arelent
*, struct reloc_std_external
*);
1933 NAME (aout
, swap_std_reloc_out
) (bfd
*abfd
,
1935 struct reloc_std_external
*natptr
)
1938 asymbol
*sym
= *(g
->sym_ptr_ptr
);
1940 unsigned int r_length
, r_size
;
1942 int r_baserel
, r_jmptable
, r_relative
;
1943 asection
*output_section
= sym
->section
->output_section
;
1945 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
1947 BFD_ASSERT (g
->howto
!= NULL
);
1949 r_size
= bfd_get_reloc_size (g
->howto
);
1950 r_length
= bfd_log2 (r_size
);
1951 if (1u << r_length
!= r_size
)
1953 _bfd_error_handler (_("%pB: unsupported AOUT relocation size: %d"),
1955 bfd_set_error (bfd_error_bad_value
);
1959 r_pcrel
= (int) g
->howto
->pc_relative
; /* Relative to PC? */
1960 /* XXX This relies on relocs coming from a.out files. */
1961 r_baserel
= (g
->howto
->type
& 8) != 0;
1962 r_jmptable
= (g
->howto
->type
& 16) != 0;
1963 r_relative
= (g
->howto
->type
& 32) != 0;
1965 /* Name was clobbered by aout_write_syms to be symbol index. */
1967 /* If this relocation is relative to a symbol then set the
1968 r_index to the symbols index, and the r_extern bit.
1970 Absolute symbols can come in in two ways, either as an offset
1971 from the abs section, or as a symbol which has an abs value.
1972 check for that here. */
1974 if (bfd_is_com_section (output_section
)
1975 || bfd_is_abs_section (output_section
)
1976 || bfd_is_und_section (output_section
)
1977 /* PR gas/3041 a.out relocs against weak symbols
1978 must be treated as if they were against externs. */
1979 || (sym
->flags
& BSF_WEAK
))
1981 if (bfd_abs_section_ptr
->symbol
== sym
)
1983 /* Whoops, looked like an abs symbol, but is
1984 really an offset from the abs section. */
1990 /* Fill in symbol. */
1992 r_index
= (*(g
->sym_ptr_ptr
))->KEEPIT
;
1997 /* Just an ordinary section. */
1999 r_index
= output_section
->target_index
;
2002 /* Now the fun stuff. */
2003 if (bfd_header_big_endian (abfd
))
2005 natptr
->r_index
[0] = r_index
>> 16;
2006 natptr
->r_index
[1] = r_index
>> 8;
2007 natptr
->r_index
[2] = r_index
;
2008 natptr
->r_type
[0] = ((r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
2009 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
2010 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
2011 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
2012 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
2013 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
2017 natptr
->r_index
[2] = r_index
>> 16;
2018 natptr
->r_index
[1] = r_index
>> 8;
2019 natptr
->r_index
[0] = r_index
;
2020 natptr
->r_type
[0] = ((r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
2021 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
2022 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
2023 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
2024 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
2025 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
2029 /* Extended stuff. */
2030 /* Output extended relocation information to a file in target byte order. */
2032 extern void NAME (aout
, swap_ext_reloc_out
)
2033 (bfd
*, arelent
*, struct reloc_ext_external
*);
2036 NAME (aout
, swap_ext_reloc_out
) (bfd
*abfd
,
2038 struct reloc_ext_external
*natptr
)
2042 unsigned int r_type
;
2044 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2045 asection
*output_section
= sym
->section
->output_section
;
2047 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
2049 r_type
= (unsigned int) g
->howto
->type
;
2051 r_addend
= g
->addend
;
2052 if ((sym
->flags
& BSF_SECTION_SYM
) != 0)
2053 r_addend
+= (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2055 /* If this relocation is relative to a symbol then set the
2056 r_index to the symbols index, and the r_extern bit.
2058 Absolute symbols can come in in two ways, either as an offset
2059 from the abs section, or as a symbol which has an abs value.
2060 check for that here. */
2061 if (bfd_is_abs_section (bfd_asymbol_section (sym
)))
2066 else if ((sym
->flags
& BSF_SECTION_SYM
) == 0)
2068 if (bfd_is_und_section (bfd_asymbol_section (sym
))
2069 || (sym
->flags
& BSF_GLOBAL
) != 0)
2073 r_index
= (*(g
->sym_ptr_ptr
))->KEEPIT
;
2077 /* Just an ordinary section. */
2079 r_index
= output_section
->target_index
;
2082 /* Now the fun stuff. */
2083 if (bfd_header_big_endian (abfd
))
2085 natptr
->r_index
[0] = r_index
>> 16;
2086 natptr
->r_index
[1] = r_index
>> 8;
2087 natptr
->r_index
[2] = r_index
;
2088 natptr
->r_type
[0] = ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
2089 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
2093 natptr
->r_index
[2] = r_index
>> 16;
2094 natptr
->r_index
[1] = r_index
>> 8;
2095 natptr
->r_index
[0] = r_index
;
2096 natptr
->r_type
[0] = ((r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
2097 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
));
2100 PUT_WORD (abfd
, r_addend
, natptr
->r_addend
);
2103 /* BFD deals internally with all things based from the section they're
2104 in. so, something in 10 bytes into a text section with a base of
2105 50 would have a symbol (.text+10) and know .text vma was 50.
2107 Aout keeps all it's symbols based from zero, so the symbol would
2108 contain 60. This macro subs the base of each section from the value
2109 to give the true offset from the section. */
2111 #define MOVE_ADDRESS(ad) \
2114 /* Undefined symbol. */ \
2115 if (symbols != NULL && r_index < bfd_get_symcount (abfd)) \
2116 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2118 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2119 cache_ptr->addend = ad; \
2123 /* Defined, section relative. Replace symbol with pointer to \
2124 symbol which points to section. */ \
2128 case N_TEXT | N_EXT: \
2129 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
2130 cache_ptr->addend = ad - su->textsec->vma; \
2133 case N_DATA | N_EXT: \
2134 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
2135 cache_ptr->addend = ad - su->datasec->vma; \
2138 case N_BSS | N_EXT: \
2139 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
2140 cache_ptr->addend = ad - su->bsssec->vma; \
2144 case N_ABS | N_EXT: \
2145 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2146 cache_ptr->addend = ad; \
2152 NAME (aout
, swap_ext_reloc_in
) (bfd
*abfd
,
2153 struct reloc_ext_external
*bytes
,
2156 bfd_size_type symcount
)
2158 unsigned int r_index
;
2160 unsigned int r_type
;
2161 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2163 cache_ptr
->address
= (GET_SWORD (abfd
, bytes
->r_address
));
2165 /* Now the fun stuff. */
2166 if (bfd_header_big_endian (abfd
))
2168 r_index
= (((unsigned int) bytes
->r_index
[0] << 16)
2169 | ((unsigned int) bytes
->r_index
[1] << 8)
2170 | bytes
->r_index
[2]);
2171 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
2172 r_type
= ((bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
2173 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
2177 r_index
= (((unsigned int) bytes
->r_index
[2] << 16)
2178 | ((unsigned int) bytes
->r_index
[1] << 8)
2179 | bytes
->r_index
[0]);
2180 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
2181 r_type
= ((bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
2182 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
2185 if (r_type
< TABLE_SIZE (howto_table_ext
))
2186 cache_ptr
->howto
= howto_table_ext
+ r_type
;
2188 cache_ptr
->howto
= NULL
;
2190 /* Base relative relocs are always against the symbol table,
2191 regardless of the setting of r_extern. r_extern just reflects
2192 whether the symbol the reloc is against is local or global. */
2193 if (r_type
== (unsigned int) RELOC_BASE10
2194 || r_type
== (unsigned int) RELOC_BASE13
2195 || r_type
== (unsigned int) RELOC_BASE22
)
2198 if (r_extern
&& r_index
> symcount
)
2200 /* We could arrange to return an error, but it might be useful
2201 to see the file even if it is bad. */
2206 MOVE_ADDRESS (GET_SWORD (abfd
, bytes
->r_addend
));
2210 NAME (aout
, swap_std_reloc_in
) (bfd
*abfd
,
2211 struct reloc_std_external
*bytes
,
2214 bfd_size_type symcount
)
2216 unsigned int r_index
;
2218 unsigned int r_length
;
2220 int r_baserel
, r_jmptable
, r_relative
;
2221 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2222 unsigned int howto_idx
;
2224 cache_ptr
->address
= H_GET_32 (abfd
, bytes
->r_address
);
2226 /* Now the fun stuff. */
2227 if (bfd_header_big_endian (abfd
))
2229 r_index
= (((unsigned int) bytes
->r_index
[0] << 16)
2230 | ((unsigned int) bytes
->r_index
[1] << 8)
2231 | bytes
->r_index
[2]);
2232 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
2233 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
2234 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
2235 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
2236 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
2237 r_length
= ((bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
2238 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
2242 r_index
= (((unsigned int) bytes
->r_index
[2] << 16)
2243 | ((unsigned int) bytes
->r_index
[1] << 8)
2244 | bytes
->r_index
[0]);
2245 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
2246 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
2247 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
2248 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
2249 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
2250 r_length
= ((bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
2251 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
2254 howto_idx
= (r_length
+ 4 * r_pcrel
+ 8 * r_baserel
2255 + 16 * r_jmptable
+ 32 * r_relative
);
2256 if (howto_idx
< TABLE_SIZE (howto_table_std
))
2258 cache_ptr
->howto
= howto_table_std
+ howto_idx
;
2259 if (cache_ptr
->howto
->type
== (unsigned int) -1)
2260 cache_ptr
->howto
= NULL
;
2263 cache_ptr
->howto
= NULL
;
2265 /* Base relative relocs are always against the symbol table,
2266 regardless of the setting of r_extern. r_extern just reflects
2267 whether the symbol the reloc is against is local or global. */
2271 if (r_extern
&& r_index
>= symcount
)
2273 /* We could arrange to return an error, but it might be useful
2274 to see the file even if it is bad. FIXME: Of course this
2275 means that objdump -r *doesn't* see the actual reloc, and
2276 objcopy silently writes a different reloc. */
2284 /* Read and swap the relocs for a section. */
2287 NAME (aout
, slurp_reloc_table
) (bfd
*abfd
, sec_ptr asect
, asymbol
**symbols
)
2289 bfd_size_type count
;
2290 bfd_size_type reloc_size
;
2292 arelent
*reloc_cache
;
2294 unsigned int counter
= 0;
2298 if (asect
->relocation
)
2301 if (asect
->flags
& SEC_CONSTRUCTOR
)
2304 if (asect
== obj_datasec (abfd
))
2305 reloc_size
= exec_hdr (abfd
)->a_drsize
;
2306 else if (asect
== obj_textsec (abfd
))
2307 reloc_size
= exec_hdr (abfd
)->a_trsize
;
2308 else if (asect
== obj_bsssec (abfd
))
2312 bfd_set_error (bfd_error_invalid_operation
);
2316 each_size
= obj_reloc_entry_size (abfd
);
2317 count
= reloc_size
/ each_size
;
2319 return true; /* Nothing to be done. */
2321 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
2323 relocs
= _bfd_malloc_and_read (abfd
, reloc_size
, reloc_size
);
2327 amt
= count
* sizeof (arelent
);
2328 reloc_cache
= (arelent
*) bfd_zmalloc (amt
);
2329 if (reloc_cache
== NULL
)
2335 cache_ptr
= reloc_cache
;
2336 if (each_size
== RELOC_EXT_SIZE
)
2338 struct reloc_ext_external
*rptr
= (struct reloc_ext_external
*) relocs
;
2340 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2341 MY_swap_ext_reloc_in (abfd
, rptr
, cache_ptr
, symbols
,
2342 (bfd_size_type
) bfd_get_symcount (abfd
));
2346 struct reloc_std_external
*rptr
= (struct reloc_std_external
*) relocs
;
2348 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2349 MY_swap_std_reloc_in (abfd
, rptr
, cache_ptr
, symbols
,
2350 (bfd_size_type
) bfd_get_symcount (abfd
));
2355 asect
->relocation
= reloc_cache
;
2356 asect
->reloc_count
= cache_ptr
- reloc_cache
;
2361 /* Write out a relocation section into an object file. */
2364 NAME (aout
, squirt_out_relocs
) (bfd
*abfd
, asection
*section
)
2367 unsigned char *native
, *natptr
;
2370 unsigned int count
= section
->reloc_count
;
2371 bfd_size_type natsize
;
2373 if (count
== 0 || section
->orelocation
== NULL
)
2376 each_size
= obj_reloc_entry_size (abfd
);
2377 natsize
= (bfd_size_type
) each_size
* count
;
2378 native
= (unsigned char *) bfd_zalloc (abfd
, natsize
);
2382 generic
= section
->orelocation
;
2384 if (each_size
== RELOC_EXT_SIZE
)
2386 for (natptr
= native
;
2388 --count
, natptr
+= each_size
, ++generic
)
2390 /* PR 20921: If the howto field has not been initialised then skip
2392 PR 20929: Similarly for the symbol field. */
2393 if ((*generic
)->howto
== NULL
2394 || (*generic
)->sym_ptr_ptr
== NULL
)
2396 bfd_set_error (bfd_error_invalid_operation
);
2397 _bfd_error_handler (_("%pB: attempt to write out "
2398 "unknown reloc type"), abfd
);
2401 MY_swap_ext_reloc_out (abfd
, *generic
,
2402 (struct reloc_ext_external
*) natptr
);
2407 for (natptr
= native
;
2409 --count
, natptr
+= each_size
, ++generic
)
2411 if ((*generic
)->howto
== NULL
2412 || (*generic
)->sym_ptr_ptr
== NULL
)
2414 bfd_set_error (bfd_error_invalid_operation
);
2415 _bfd_error_handler (_("%pB: attempt to write out "
2416 "unknown reloc type"), abfd
);
2419 MY_swap_std_reloc_out (abfd
, *generic
,
2420 (struct reloc_std_external
*) natptr
);
2424 if (bfd_write (native
, natsize
, abfd
) != natsize
)
2426 bfd_release (abfd
, native
);
2429 bfd_release (abfd
, native
);
2434 /* This is stupid. This function should be a boolean predicate. */
2437 NAME (aout
, canonicalize_reloc
) (bfd
*abfd
,
2442 arelent
*tblptr
= section
->relocation
;
2445 if (section
== obj_bsssec (abfd
))
2451 if (!(tblptr
|| NAME (aout
, slurp_reloc_table
) (abfd
, section
, symbols
)))
2454 if (section
->flags
& SEC_CONSTRUCTOR
)
2456 arelent_chain
*chain
= section
->constructor_chain
;
2457 for (count
= 0; count
< section
->reloc_count
; count
++)
2459 *relptr
++ = &chain
->relent
;
2460 chain
= chain
->next
;
2465 tblptr
= section
->relocation
;
2467 for (count
= 0; count
++ < section
->reloc_count
; )
2469 *relptr
++ = tblptr
++;
2474 return section
->reloc_count
;
2478 NAME (aout
, get_reloc_upper_bound
) (bfd
*abfd
, sec_ptr asect
)
2482 if (asect
->flags
& SEC_CONSTRUCTOR
)
2483 count
= asect
->reloc_count
;
2484 else if (asect
== obj_datasec (abfd
))
2485 count
= exec_hdr (abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
);
2486 else if (asect
== obj_textsec (abfd
))
2487 count
= exec_hdr (abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
);
2488 else if (asect
== obj_bsssec (abfd
))
2492 bfd_set_error (bfd_error_invalid_operation
);
2496 if (count
>= LONG_MAX
/ sizeof (arelent
*)
2497 || _bfd_mul_overflow (count
, obj_reloc_entry_size (abfd
), &raw
))
2499 bfd_set_error (bfd_error_file_too_big
);
2502 if (!bfd_write_p (abfd
))
2504 ufile_ptr filesize
= bfd_get_file_size (abfd
);
2505 if (filesize
!= 0 && raw
> filesize
)
2507 bfd_set_error (bfd_error_file_truncated
);
2511 return (count
+ 1) * sizeof (arelent
*);
2515 NAME (aout
, get_symtab_upper_bound
) (bfd
*abfd
)
2517 if (!NAME (aout
, slurp_symbol_table
) (abfd
))
2520 return (bfd_get_symcount (abfd
)+1) * (sizeof (aout_symbol_type
*));
2524 NAME (aout
, get_lineno
) (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
2525 asymbol
*ignore_symbol ATTRIBUTE_UNUSED
)
2531 NAME (aout
, get_symbol_info
) (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
2535 bfd_symbol_info (symbol
, ret
);
2537 if (ret
->type
== '?')
2539 int type_code
= aout_symbol (symbol
)->type
& 0xff;
2540 const char *stab_name
= bfd_get_stab_name (type_code
);
2541 static char buf
[10];
2543 if (stab_name
== NULL
)
2545 sprintf (buf
, "(%d)", type_code
);
2549 ret
->stab_type
= type_code
;
2550 ret
->stab_other
= (unsigned) (aout_symbol (symbol
)->other
& 0xff);
2551 ret
->stab_desc
= (unsigned) (aout_symbol (symbol
)->desc
& 0xffff);
2552 ret
->stab_name
= stab_name
;
2557 NAME (aout
, print_symbol
) (bfd
*abfd
,
2560 bfd_print_symbol_type how
)
2562 FILE *file
= (FILE *)afile
;
2566 case bfd_print_symbol_name
:
2568 fprintf (file
,"%s", symbol
->name
);
2570 case bfd_print_symbol_more
:
2571 fprintf (file
,"%4x %2x %2x",
2572 (unsigned) (aout_symbol (symbol
)->desc
& 0xffff),
2573 (unsigned) (aout_symbol (symbol
)->other
& 0xff),
2574 (unsigned) (aout_symbol (symbol
)->type
));
2576 case bfd_print_symbol_all
:
2578 const char *section_name
= symbol
->section
->name
;
2580 bfd_print_symbol_vandf (abfd
, (void *)file
, symbol
);
2582 fprintf (file
," %-5s %04x %02x %02x",
2584 (unsigned) (aout_symbol (symbol
)->desc
& 0xffff),
2585 (unsigned) (aout_symbol (symbol
)->other
& 0xff),
2586 (unsigned) (aout_symbol (symbol
)->type
& 0xff));
2588 fprintf (file
," %s", symbol
->name
);
2594 /* If we don't have to allocate more than 1MB to hold the generic
2595 symbols, we use the generic minisymbol methord: it's faster, since
2596 it only translates the symbols once, not multiple times. */
2597 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2599 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2600 symbols. The minisymbol_to_symbol function translates these into
2601 BFD asymbol structures. */
2604 NAME (aout
, read_minisymbols
) (bfd
*abfd
,
2607 unsigned int *sizep
)
2610 /* We could handle the dynamic symbols here as well, but it's
2611 easier to hand them off. */
2612 return _bfd_generic_read_minisymbols (abfd
, dynamic
, minisymsp
, sizep
);
2614 if (! aout_get_external_symbols (abfd
))
2617 if (obj_aout_external_sym_count (abfd
) < MINISYM_THRESHOLD
)
2618 return _bfd_generic_read_minisymbols (abfd
, dynamic
, minisymsp
, sizep
);
2620 *minisymsp
= (void *) obj_aout_external_syms (abfd
);
2622 /* By passing the external symbols back from this routine, we are
2623 giving up control over the memory block. Clear
2624 obj_aout_external_syms, so that we do not try to free it
2626 obj_aout_external_syms (abfd
) = NULL
;
2628 *sizep
= EXTERNAL_NLIST_SIZE
;
2629 return obj_aout_external_sym_count (abfd
);
2632 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2633 unmodified a.out symbol. The SYM argument is a structure returned
2634 by bfd_make_empty_symbol, which we fill in here. */
2637 NAME (aout
, minisymbol_to_symbol
) (bfd
*abfd
,
2639 const void * minisym
,
2643 || obj_aout_external_sym_count (abfd
) < MINISYM_THRESHOLD
)
2644 return _bfd_generic_minisymbol_to_symbol (abfd
, dynamic
, minisym
, sym
);
2646 memset (sym
, 0, sizeof (aout_symbol_type
));
2648 /* We call translate_symbol_table to translate a single symbol. */
2649 if (! (NAME (aout
, translate_symbol_table
)
2651 (aout_symbol_type
*) sym
,
2652 (struct external_nlist
*) minisym
,
2654 obj_aout_external_strings (abfd
),
2655 obj_aout_external_string_size (abfd
),
2662 /* Provided a BFD, a section and an offset into the section, calculate
2663 and return the name of the source file and the line nearest to the
2667 NAME (aout
, find_nearest_line
) (bfd
*abfd
,
2671 const char **filename_ptr
,
2672 const char **functionname_ptr
,
2673 unsigned int *line_ptr
,
2674 unsigned int *disriminator_ptr
)
2676 /* Run down the file looking for the filename, function and linenumber. */
2678 const char *directory_name
= NULL
;
2679 const char *main_file_name
= NULL
;
2680 const char *current_file_name
= NULL
;
2681 const char *line_file_name
= NULL
; /* Value of current_file_name at line number. */
2682 const char *line_directory_name
= NULL
; /* Value of directory_name at line number. */
2683 bfd_vma low_line_vma
= 0;
2684 bfd_vma low_func_vma
= 0;
2686 bfd_size_type filelen
, funclen
;
2689 *filename_ptr
= bfd_get_filename (abfd
);
2690 *functionname_ptr
= NULL
;
2692 if (disriminator_ptr
)
2693 *disriminator_ptr
= 0;
2695 if (symbols
!= NULL
)
2697 for (p
= symbols
; *p
; p
++)
2699 aout_symbol_type
*q
= (aout_symbol_type
*) (*p
);
2704 /* If this looks like a file name symbol, and it comes after
2705 the line number we have found so far, but before the
2706 offset, then we have probably not found the right line
2708 if (q
->symbol
.value
<= offset
2709 && ((q
->symbol
.value
> low_line_vma
2710 && (line_file_name
!= NULL
2712 || (q
->symbol
.value
> low_func_vma
2715 const char *symname
;
2717 symname
= q
->symbol
.name
;
2720 && strlen (symname
) > 2
2721 && strcmp (symname
+ strlen (symname
) - 2, ".o") == 0)
2723 if (q
->symbol
.value
> low_line_vma
)
2726 line_file_name
= NULL
;
2728 if (q
->symbol
.value
> low_func_vma
)
2735 /* If this symbol is less than the offset, but greater than
2736 the line number we have found so far, then we have not
2737 found the right line number. */
2738 if (q
->symbol
.value
<= offset
)
2740 if (q
->symbol
.value
> low_line_vma
)
2743 line_file_name
= NULL
;
2745 if (q
->symbol
.value
> low_func_vma
)
2749 main_file_name
= current_file_name
= q
->symbol
.name
;
2750 /* Look ahead to next symbol to check if that too is an N_SO. */
2754 q
= (aout_symbol_type
*) (*p
);
2755 if (q
->type
!= (int)N_SO
)
2758 /* Found a second N_SO First is directory; second is filename. */
2759 directory_name
= current_file_name
;
2760 main_file_name
= current_file_name
= q
->symbol
.name
;
2761 if (obj_textsec (abfd
) != section
)
2765 current_file_name
= q
->symbol
.name
;
2772 /* We'll keep this if it resolves nearer than the one we have
2774 if (q
->symbol
.value
>= low_line_vma
2775 && q
->symbol
.value
<= offset
)
2777 *line_ptr
= q
->desc
;
2778 low_line_vma
= q
->symbol
.value
;
2779 line_file_name
= current_file_name
;
2780 line_directory_name
= directory_name
;
2785 /* We'll keep this if it is nearer than the one we have already. */
2786 if (q
->symbol
.value
>= low_func_vma
2787 && q
->symbol
.value
<= offset
)
2789 low_func_vma
= q
->symbol
.value
;
2790 func
= (asymbol
*)q
;
2792 else if (q
->symbol
.value
> offset
)
2803 main_file_name
= line_file_name
;
2804 directory_name
= line_directory_name
;
2807 if (main_file_name
== NULL
2808 || IS_ABSOLUTE_PATH (main_file_name
)
2809 || directory_name
== NULL
)
2812 filelen
= strlen (directory_name
) + strlen (main_file_name
);
2817 funclen
= strlen (bfd_asymbol_name (func
));
2819 free (adata (abfd
).line_buf
);
2821 if (filelen
+ funclen
== 0)
2822 adata (abfd
).line_buf
= buf
= NULL
;
2825 buf
= (char *) bfd_malloc (filelen
+ funclen
+ 3);
2826 adata (abfd
).line_buf
= buf
;
2831 if (main_file_name
!= NULL
)
2833 if (IS_ABSOLUTE_PATH (main_file_name
) || directory_name
== NULL
)
2834 *filename_ptr
= main_file_name
;
2838 /* PR binutils/20891: In a corrupt input file both
2839 main_file_name and directory_name can be empty... */
2840 * filename_ptr
= NULL
;
2843 snprintf (buf
, filelen
+ 1, "%s%s", directory_name
,
2845 *filename_ptr
= buf
;
2853 const char *function
= func
->name
;
2858 /* PR binutils/20892: In a corrupt input file func can be empty. */
2859 * functionname_ptr
= NULL
;
2862 /* The caller expects a symbol name. We actually have a
2863 function name, without the leading underscore. Put the
2864 underscore back in, so that the caller gets a symbol name. */
2865 if (bfd_get_symbol_leading_char (abfd
) == '\0')
2866 strcpy (buf
, function
);
2869 buf
[0] = bfd_get_symbol_leading_char (abfd
);
2870 strcpy (buf
+ 1, function
);
2872 /* Have to remove : stuff. */
2873 colon
= strchr (buf
, ':');
2876 *functionname_ptr
= buf
;
2883 NAME (aout
, sizeof_headers
) (bfd
*abfd
,
2884 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
2886 return adata (abfd
).exec_bytes_size
;
2889 /* Throw away most malloc'd and alloc'd information for this BFD. */
2892 NAME (aout
, bfd_free_cached_info
) (bfd
*abfd
)
2894 if ((bfd_get_format (abfd
) == bfd_object
2895 || bfd_get_format (abfd
) == bfd_core
)
2896 && abfd
->tdata
.aout_data
!= NULL
)
2898 #define BFCI_FREE(x) do { free (x); x = NULL; } while (0)
2899 BFCI_FREE (adata (abfd
).line_buf
);
2900 BFCI_FREE (obj_aout_symbols (abfd
));
2901 BFCI_FREE (obj_aout_external_syms (abfd
));
2902 BFCI_FREE (obj_aout_external_strings (abfd
));
2903 for (asection
*o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
2904 BFCI_FREE (o
->relocation
);
2908 return _bfd_generic_bfd_free_cached_info (abfd
);
2911 /* a.out link code. */
2913 /* Routine to create an entry in an a.out link hash table. */
2915 struct bfd_hash_entry
*
2916 NAME (aout
, link_hash_newfunc
) (struct bfd_hash_entry
*entry
,
2917 struct bfd_hash_table
*table
,
2920 struct aout_link_hash_entry
*ret
= (struct aout_link_hash_entry
*) entry
;
2922 /* Allocate the structure if it has not already been allocated by a
2925 ret
= (struct aout_link_hash_entry
*) bfd_hash_allocate (table
,
2930 /* Call the allocation method of the superclass. */
2931 ret
= ((struct aout_link_hash_entry
*)
2932 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2936 /* Set local fields. */
2937 ret
->written
= false;
2941 return (struct bfd_hash_entry
*) ret
;
2944 /* Initialize an a.out link hash table. */
2947 NAME (aout
, link_hash_table_init
) (struct aout_link_hash_table
*table
,
2949 struct bfd_hash_entry
*(*newfunc
)
2950 (struct bfd_hash_entry
*, struct bfd_hash_table
*,
2952 unsigned int entsize
)
2954 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
, entsize
);
2957 /* Create an a.out link hash table. */
2959 struct bfd_link_hash_table
*
2960 NAME (aout
, link_hash_table_create
) (bfd
*abfd
)
2962 struct aout_link_hash_table
*ret
;
2963 size_t amt
= sizeof (* ret
);
2965 ret
= (struct aout_link_hash_table
*) bfd_malloc (amt
);
2969 if (!NAME (aout
, link_hash_table_init
) (ret
, abfd
,
2970 NAME (aout
, link_hash_newfunc
),
2971 sizeof (struct aout_link_hash_entry
)))
2979 /* Add all symbols from an object file to the hash table. */
2982 aout_link_add_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
2984 bool (*add_one_symbol
)
2985 (struct bfd_link_info
*, bfd
*, const char *, flagword
, asection
*,
2986 bfd_vma
, const char *, bool, bool, struct bfd_link_hash_entry
**);
2987 struct external_nlist
*syms
;
2988 bfd_size_type sym_count
;
2991 struct aout_link_hash_entry
**sym_hash
;
2992 struct external_nlist
*p
;
2993 struct external_nlist
*pend
;
2996 syms
= obj_aout_external_syms (abfd
);
2997 sym_count
= obj_aout_external_sym_count (abfd
);
2998 strings
= obj_aout_external_strings (abfd
);
2999 if (info
->keep_memory
)
3004 if (aout_backend_info (abfd
)->add_dynamic_symbols
!= NULL
)
3006 if (! ((*aout_backend_info (abfd
)->add_dynamic_symbols
)
3007 (abfd
, info
, &syms
, &sym_count
, &strings
)))
3012 return true; /* Nothing to do. */
3014 /* We keep a list of the linker hash table entries that correspond
3015 to particular symbols. We could just look them up in the hash
3016 table, but keeping the list is more efficient. Perhaps this
3017 should be conditional on info->keep_memory. */
3018 amt
= sym_count
* sizeof (struct aout_link_hash_entry
*);
3019 sym_hash
= (struct aout_link_hash_entry
**) bfd_alloc (abfd
, amt
);
3020 if (sym_hash
== NULL
)
3022 obj_aout_sym_hashes (abfd
) = sym_hash
;
3024 add_one_symbol
= aout_backend_info (abfd
)->add_one_symbol
;
3025 if (add_one_symbol
== NULL
)
3026 add_one_symbol
= _bfd_generic_link_add_one_symbol
;
3029 pend
= p
+ sym_count
;
3030 for (; p
< pend
; p
++, sym_hash
++)
3041 type
= H_GET_8 (abfd
, p
->e_type
);
3043 /* Ignore debugging symbols. */
3044 if ((type
& N_STAB
) != 0)
3047 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3048 if (GET_WORD (abfd
, p
->e_strx
) >= obj_aout_external_string_size (abfd
))
3050 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3051 value
= GET_WORD (abfd
, p
->e_value
);
3068 /* Ignore symbols that are not externally visible. */
3071 /* Ignore local indirect symbol. */
3076 case N_UNDF
| N_EXT
:
3079 section
= bfd_und_section_ptr
;
3083 section
= bfd_com_section_ptr
;
3086 section
= bfd_abs_section_ptr
;
3088 case N_TEXT
| N_EXT
:
3089 section
= obj_textsec (abfd
);
3090 value
-= bfd_section_vma (section
);
3092 case N_DATA
| N_EXT
:
3093 case N_SETV
| N_EXT
:
3094 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3095 translate_from_native_sym_flags. */
3096 section
= obj_datasec (abfd
);
3097 value
-= bfd_section_vma (section
);
3100 section
= obj_bsssec (abfd
);
3101 value
-= bfd_section_vma (section
);
3103 case N_INDR
| N_EXT
:
3104 /* An indirect symbol. The next symbol is the symbol
3105 which this one really is. */
3106 /* See PR 20925 for a reproducer. */
3110 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3111 if (GET_WORD (abfd
, p
->e_strx
) >= obj_aout_external_string_size (abfd
))
3113 string
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3114 section
= bfd_ind_section_ptr
;
3115 flags
|= BSF_INDIRECT
;
3117 case N_COMM
| N_EXT
:
3118 section
= bfd_com_section_ptr
;
3120 case N_SETA
: case N_SETA
| N_EXT
:
3121 section
= bfd_abs_section_ptr
;
3122 flags
|= BSF_CONSTRUCTOR
;
3124 case N_SETT
: case N_SETT
| N_EXT
:
3125 section
= obj_textsec (abfd
);
3126 flags
|= BSF_CONSTRUCTOR
;
3127 value
-= bfd_section_vma (section
);
3129 case N_SETD
: case N_SETD
| N_EXT
:
3130 section
= obj_datasec (abfd
);
3131 flags
|= BSF_CONSTRUCTOR
;
3132 value
-= bfd_section_vma (section
);
3134 case N_SETB
: case N_SETB
| N_EXT
:
3135 section
= obj_bsssec (abfd
);
3136 flags
|= BSF_CONSTRUCTOR
;
3137 value
-= bfd_section_vma (section
);
3140 /* A warning symbol. The next symbol is the one to warn
3141 about. If there is no next symbol, just look away. */
3146 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3147 if (GET_WORD (abfd
, p
->e_strx
) >= obj_aout_external_string_size (abfd
))
3149 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3150 section
= bfd_und_section_ptr
;
3151 flags
|= BSF_WARNING
;
3154 section
= bfd_und_section_ptr
;
3158 section
= bfd_abs_section_ptr
;
3162 section
= obj_textsec (abfd
);
3163 value
-= bfd_section_vma (section
);
3167 section
= obj_datasec (abfd
);
3168 value
-= bfd_section_vma (section
);
3172 section
= obj_bsssec (abfd
);
3173 value
-= bfd_section_vma (section
);
3178 if (! ((*add_one_symbol
)
3179 (info
, abfd
, name
, flags
, section
, value
, string
, copy
, false,
3180 (struct bfd_link_hash_entry
**) sym_hash
)))
3183 /* Restrict the maximum alignment of a common symbol based on
3184 the architecture, since a.out has no way to represent
3185 alignment requirements of a section in a .o file. FIXME:
3186 This isn't quite right: it should use the architecture of the
3187 output file, not the input files. */
3188 if ((*sym_hash
)->root
.type
== bfd_link_hash_common
3189 && ((*sym_hash
)->root
.u
.c
.p
->alignment_power
>
3190 bfd_get_arch_info (abfd
)->section_align_power
))
3191 (*sym_hash
)->root
.u
.c
.p
->alignment_power
=
3192 bfd_get_arch_info (abfd
)->section_align_power
;
3194 /* If this is a set symbol, and we are not building sets, then
3195 it is possible for the hash entry to not have been set. In
3196 such a case, treat the symbol as not globally defined. */
3197 if ((*sym_hash
)->root
.type
== bfd_link_hash_new
)
3199 BFD_ASSERT ((flags
& BSF_CONSTRUCTOR
) != 0);
3203 if (type
== (N_INDR
| N_EXT
) || type
== N_WARNING
)
3210 /* Free up the internal symbols read from an a.out file. */
3213 aout_link_free_symbols (bfd
*abfd
)
3215 if (obj_aout_external_syms (abfd
) != NULL
)
3217 free ((void *) obj_aout_external_syms (abfd
));
3218 obj_aout_external_syms (abfd
) = NULL
;
3220 if (obj_aout_external_strings (abfd
) != NULL
)
3222 free ((void *) obj_aout_external_strings (abfd
));
3223 obj_aout_external_strings (abfd
) = NULL
;
3228 /* Add symbols from an a.out object file. */
3231 aout_link_add_object_symbols (bfd
*abfd
, struct bfd_link_info
*info
)
3233 if (! aout_get_external_symbols (abfd
))
3235 if (! aout_link_add_symbols (abfd
, info
))
3237 if (! info
->keep_memory
)
3239 if (! aout_link_free_symbols (abfd
))
3245 /* Look through the internal symbols to see if this object file should
3246 be included in the link. We should include this object file if it
3247 defines any symbols which are currently undefined. If this object
3248 file defines a common symbol, then we may adjust the size of the
3249 known symbol but we do not include the object file in the link
3250 (unless there is some other reason to include it). */
3253 aout_link_check_ar_symbols (bfd
*abfd
,
3254 struct bfd_link_info
*info
,
3258 struct external_nlist
*p
;
3259 struct external_nlist
*pend
;
3264 /* Look through all the symbols. */
3265 p
= obj_aout_external_syms (abfd
);
3266 pend
= p
+ obj_aout_external_sym_count (abfd
);
3267 strings
= obj_aout_external_strings (abfd
);
3268 for (; p
< pend
; p
++)
3270 int type
= H_GET_8 (abfd
, p
->e_type
);
3272 struct bfd_link_hash_entry
*h
;
3274 /* Ignore symbols that are not externally visible. This is an
3275 optimization only, as we check the type more thoroughly
3277 if (((type
& N_EXT
) == 0
3278 || (type
& N_STAB
) != 0
3285 if (type
== N_WARNING
3291 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3292 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
3294 /* We are only interested in symbols that are currently
3295 undefined or common. */
3297 || (h
->type
!= bfd_link_hash_undefined
3298 && h
->type
!= bfd_link_hash_common
))
3300 if (type
== (N_INDR
| N_EXT
))
3305 if (type
== (N_TEXT
| N_EXT
)
3306 || type
== (N_DATA
| N_EXT
)
3307 || type
== (N_BSS
| N_EXT
)
3308 || type
== (N_ABS
| N_EXT
)
3309 || type
== (N_INDR
| N_EXT
))
3311 /* This object file defines this symbol. We must link it
3312 in. This is true regardless of whether the current
3313 definition of the symbol is undefined or common.
3315 If the current definition is common, we have a case in
3316 which we have already seen an object file including:
3318 and this object file from the archive includes:
3320 In such a case, whether to include this object is target
3321 dependant for backward compatibility.
3323 FIXME: The SunOS 4.1.3 linker will pull in the archive
3324 element if the symbol is defined in the .data section,
3325 but not if it is defined in the .text section. That
3326 seems a bit crazy to me, and it has not been implemented
3327 yet. However, it might be correct. */
3328 if (h
->type
== bfd_link_hash_common
)
3332 switch (info
->common_skip_ar_symbols
)
3334 case bfd_link_common_skip_none
:
3336 case bfd_link_common_skip_text
:
3337 skip
= (type
== (N_TEXT
| N_EXT
));
3339 case bfd_link_common_skip_data
:
3340 skip
= (type
== (N_DATA
| N_EXT
));
3342 case bfd_link_common_skip_all
:
3351 if (!(*info
->callbacks
3352 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
3358 if (type
== (N_UNDF
| N_EXT
))
3362 value
= GET_WORD (abfd
, p
->e_value
);
3365 /* This symbol is common in the object from the archive
3367 if (h
->type
== bfd_link_hash_undefined
)
3372 symbfd
= h
->u
.undef
.abfd
;
3375 /* This symbol was created as undefined from
3376 outside BFD. We assume that we should link
3377 in the object file. This is done for the -u
3378 option in the linker. */
3379 if (!(*info
->callbacks
3380 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
3385 /* Turn the current link symbol into a common
3386 symbol. It is already on the undefs list. */
3387 h
->type
= bfd_link_hash_common
;
3388 h
->u
.c
.p
= (struct bfd_link_hash_common_entry
*)
3389 bfd_hash_allocate (&info
->hash
->table
,
3390 sizeof (struct bfd_link_hash_common_entry
));
3391 if (h
->u
.c
.p
== NULL
)
3394 h
->u
.c
.size
= value
;
3396 /* FIXME: This isn't quite right. The maximum
3397 alignment of a common symbol should be set by the
3398 architecture of the output file, not of the input
3400 power
= bfd_log2 (value
);
3401 if (power
> bfd_get_arch_info (abfd
)->section_align_power
)
3402 power
= bfd_get_arch_info (abfd
)->section_align_power
;
3403 h
->u
.c
.p
->alignment_power
= power
;
3405 h
->u
.c
.p
->section
= bfd_make_section_old_way (symbfd
,
3410 /* Adjust the size of the common symbol if
3412 if (value
> h
->u
.c
.size
)
3413 h
->u
.c
.size
= value
;
3423 /* This symbol is weak but defined. We must pull it in if
3424 the current link symbol is undefined, but we don't want
3425 it if the current link symbol is common. */
3426 if (h
->type
== bfd_link_hash_undefined
)
3428 if (!(*info
->callbacks
3429 ->add_archive_element
) (info
, abfd
, name
, subsbfd
))
3437 /* We do not need this object file. */
3440 /* Check a single archive element to see if we need to include it in
3441 the link. *PNEEDED is set according to whether this element is
3442 needed in the link or not. This is called from
3443 _bfd_generic_link_add_archive_symbols. */
3446 aout_link_check_archive_element (bfd
*abfd
,
3447 struct bfd_link_info
*info
,
3448 struct bfd_link_hash_entry
*h ATTRIBUTE_UNUSED
,
3449 const char *name ATTRIBUTE_UNUSED
,
3455 if (!aout_get_external_symbols (abfd
))
3459 if (!aout_link_check_ar_symbols (abfd
, info
, pneeded
, &abfd
))
3465 /* Potentially, the add_archive_element hook may have set a
3466 substitute BFD for us. */
3469 if (!info
->keep_memory
3470 && !aout_link_free_symbols (oldbfd
))
3472 if (!aout_get_external_symbols (abfd
))
3475 if (!aout_link_add_symbols (abfd
, info
))
3479 if (!info
->keep_memory
|| !needed
)
3481 if (!aout_link_free_symbols (abfd
))
3488 /* Given an a.out BFD, add symbols to the global hash table as
3492 NAME (aout
, link_add_symbols
) (bfd
*abfd
, struct bfd_link_info
*info
)
3494 switch (bfd_get_format (abfd
))
3497 return aout_link_add_object_symbols (abfd
, info
);
3499 return _bfd_generic_link_add_archive_symbols
3500 (abfd
, info
, aout_link_check_archive_element
);
3502 bfd_set_error (bfd_error_wrong_format
);
3507 /* A hash table used for header files with N_BINCL entries. */
3509 struct aout_link_includes_table
3511 struct bfd_hash_table root
;
3514 /* A linked list of totals that we have found for a particular header
3517 struct aout_link_includes_totals
3519 struct aout_link_includes_totals
*next
;
3523 /* An entry in the header file hash table. */
3525 struct aout_link_includes_entry
3527 struct bfd_hash_entry root
;
3528 /* List of totals we have found for this file. */
3529 struct aout_link_includes_totals
*totals
;
3532 /* Look up an entry in an the header file hash table. */
3534 #define aout_link_includes_lookup(table, string, create, copy) \
3535 ((struct aout_link_includes_entry *) \
3536 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3538 /* During the final link step we need to pass around a bunch of
3539 information, so we do it in an instance of this structure. */
3541 struct aout_final_link_info
3543 /* General link information. */
3544 struct bfd_link_info
*info
;
3547 /* Reloc file positions. */
3548 file_ptr treloff
, dreloff
;
3549 /* File position of symbols. */
3552 struct bfd_strtab_hash
*strtab
;
3553 /* Header file hash table. */
3554 struct aout_link_includes_table includes
;
3555 /* A buffer large enough to hold the contents of any section. */
3557 /* A buffer large enough to hold the relocs of any section. */
3559 /* A buffer large enough to hold the symbol map of any input BFD. */
3561 /* A buffer large enough to hold output symbols of any input BFD. */
3562 struct external_nlist
*output_syms
;
3565 /* The function to create a new entry in the header file hash table. */
3567 static struct bfd_hash_entry
*
3568 aout_link_includes_newfunc (struct bfd_hash_entry
*entry
,
3569 struct bfd_hash_table
*table
,
3572 struct aout_link_includes_entry
*ret
=
3573 (struct aout_link_includes_entry
*) entry
;
3575 /* Allocate the structure if it has not already been allocated by a
3578 ret
= (struct aout_link_includes_entry
*)
3579 bfd_hash_allocate (table
, sizeof (* ret
));
3583 /* Call the allocation method of the superclass. */
3584 ret
= ((struct aout_link_includes_entry
*)
3585 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
3588 /* Set local fields. */
3592 return (struct bfd_hash_entry
*) ret
;
3595 /* Write out a symbol that was not associated with an a.out input
3599 aout_link_write_other_symbol (struct bfd_hash_entry
*bh
, void *data
)
3601 struct aout_link_hash_entry
*h
= (struct aout_link_hash_entry
*) bh
;
3602 struct aout_final_link_info
*flaginfo
= (struct aout_final_link_info
*) data
;
3606 struct external_nlist outsym
;
3610 if (h
->root
.type
== bfd_link_hash_warning
)
3612 h
= (struct aout_link_hash_entry
*) h
->root
.u
.i
.link
;
3613 if (h
->root
.type
== bfd_link_hash_new
)
3617 output_bfd
= flaginfo
->output_bfd
;
3619 if (aout_backend_info (output_bfd
)->write_dynamic_symbol
!= NULL
)
3621 if (! ((*aout_backend_info (output_bfd
)->write_dynamic_symbol
)
3622 (output_bfd
, flaginfo
->info
, h
)))
3624 /* FIXME: No way to handle errors. */
3634 /* An indx of -2 means the symbol must be written. */
3636 && (flaginfo
->info
->strip
== strip_all
3637 || (flaginfo
->info
->strip
== strip_some
3638 && bfd_hash_lookup (flaginfo
->info
->keep_hash
, h
->root
.root
.string
,
3639 false, false) == NULL
)))
3642 switch (h
->root
.type
)
3645 case bfd_link_hash_warning
:
3647 /* Avoid variable not initialized warnings. */
3649 case bfd_link_hash_new
:
3650 /* This can happen for set symbols when sets are not being
3653 case bfd_link_hash_undefined
:
3654 type
= N_UNDF
| N_EXT
;
3657 case bfd_link_hash_defined
:
3658 case bfd_link_hash_defweak
:
3662 sec
= h
->root
.u
.def
.section
->output_section
;
3663 BFD_ASSERT (bfd_is_abs_section (sec
)
3664 || sec
->owner
== output_bfd
);
3665 if (sec
== obj_textsec (output_bfd
))
3666 type
= h
->root
.type
== bfd_link_hash_defined
? N_TEXT
: N_WEAKT
;
3667 else if (sec
== obj_datasec (output_bfd
))
3668 type
= h
->root
.type
== bfd_link_hash_defined
? N_DATA
: N_WEAKD
;
3669 else if (sec
== obj_bsssec (output_bfd
))
3670 type
= h
->root
.type
== bfd_link_hash_defined
? N_BSS
: N_WEAKB
;
3672 type
= h
->root
.type
== bfd_link_hash_defined
? N_ABS
: N_WEAKA
;
3674 val
= (h
->root
.u
.def
.value
3676 + h
->root
.u
.def
.section
->output_offset
);
3679 case bfd_link_hash_common
:
3680 type
= N_UNDF
| N_EXT
;
3681 val
= h
->root
.u
.c
.size
;
3683 case bfd_link_hash_undefweak
:
3687 case bfd_link_hash_indirect
:
3688 /* We ignore these symbols, since the indirected symbol is
3689 already in the hash table. */
3693 H_PUT_8 (output_bfd
, type
, outsym
.e_type
);
3694 H_PUT_8 (output_bfd
, 0, outsym
.e_other
);
3695 H_PUT_16 (output_bfd
, 0, outsym
.e_desc
);
3696 indx
= add_to_stringtab (output_bfd
, flaginfo
->strtab
, h
->root
.root
.string
,
3698 if (indx
== - (bfd_size_type
) 1)
3699 /* FIXME: No way to handle errors. */
3702 PUT_WORD (output_bfd
, indx
, outsym
.e_strx
);
3703 PUT_WORD (output_bfd
, val
, outsym
.e_value
);
3705 amt
= EXTERNAL_NLIST_SIZE
;
3706 if (bfd_seek (output_bfd
, flaginfo
->symoff
, SEEK_SET
) != 0
3707 || bfd_write (&outsym
, amt
, output_bfd
) != amt
)
3708 /* FIXME: No way to handle errors. */
3711 flaginfo
->symoff
+= EXTERNAL_NLIST_SIZE
;
3712 h
->indx
= obj_aout_external_sym_count (output_bfd
);
3713 ++obj_aout_external_sym_count (output_bfd
);
3718 /* Handle a link order which is supposed to generate a reloc. */
3721 aout_link_reloc_link_order (struct aout_final_link_info
*flaginfo
,
3723 struct bfd_link_order
*p
)
3725 struct bfd_link_order_reloc
*pr
;
3728 reloc_howto_type
*howto
;
3729 file_ptr
*reloff_ptr
= NULL
;
3730 struct reloc_std_external srel
;
3731 struct reloc_ext_external erel
;
3737 if (p
->type
== bfd_section_reloc_link_order
)
3740 if (bfd_is_abs_section (pr
->u
.section
))
3741 r_index
= N_ABS
| N_EXT
;
3744 BFD_ASSERT (pr
->u
.section
->owner
== flaginfo
->output_bfd
);
3745 r_index
= pr
->u
.section
->target_index
;
3750 struct aout_link_hash_entry
*h
;
3752 BFD_ASSERT (p
->type
== bfd_symbol_reloc_link_order
);
3754 h
= ((struct aout_link_hash_entry
*)
3755 bfd_wrapped_link_hash_lookup (flaginfo
->output_bfd
, flaginfo
->info
,
3756 pr
->u
.name
, false, false, true));
3762 /* We decided to strip this symbol, but it turns out that we
3763 can't. Note that we lose the other and desc information
3764 here. I don't think that will ever matter for a global
3768 if (!aout_link_write_other_symbol (&h
->root
.root
, flaginfo
))
3774 (*flaginfo
->info
->callbacks
->unattached_reloc
)
3775 (flaginfo
->info
, pr
->u
.name
, NULL
, NULL
, (bfd_vma
) 0);
3780 howto
= bfd_reloc_type_lookup (flaginfo
->output_bfd
, pr
->reloc
);
3783 bfd_set_error (bfd_error_bad_value
);
3787 if (o
== obj_textsec (flaginfo
->output_bfd
))
3788 reloff_ptr
= &flaginfo
->treloff
;
3789 else if (o
== obj_datasec (flaginfo
->output_bfd
))
3790 reloff_ptr
= &flaginfo
->dreloff
;
3794 if (obj_reloc_entry_size (flaginfo
->output_bfd
) == RELOC_STD_SIZE
)
3797 MY_put_reloc (flaginfo
->output_bfd
, r_extern
, r_index
, p
->offset
, howto
,
3805 unsigned int r_length
;
3807 r_pcrel
= (int) howto
->pc_relative
;
3808 r_baserel
= (howto
->type
& 8) != 0;
3809 r_jmptable
= (howto
->type
& 16) != 0;
3810 r_relative
= (howto
->type
& 32) != 0;
3811 r_length
= bfd_log2 (bfd_get_reloc_size (howto
));
3813 PUT_WORD (flaginfo
->output_bfd
, p
->offset
, srel
.r_address
);
3814 if (bfd_header_big_endian (flaginfo
->output_bfd
))
3816 srel
.r_index
[0] = r_index
>> 16;
3817 srel
.r_index
[1] = r_index
>> 8;
3818 srel
.r_index
[2] = r_index
;
3820 ((r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
3821 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
3822 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
3823 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
3824 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
3825 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
3829 srel
.r_index
[2] = r_index
>> 16;
3830 srel
.r_index
[1] = r_index
>> 8;
3831 srel
.r_index
[0] = r_index
;
3833 ((r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
3834 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
3835 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
3836 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
3837 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
3838 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
3842 rel_ptr
= (void *) &srel
;
3844 /* We have to write the addend into the object file, since
3845 standard a.out relocs are in place. It would be more
3846 reliable if we had the current contents of the file here,
3847 rather than assuming zeroes, but we can't read the file since
3848 it was opened using bfd_openw. */
3849 if (pr
->addend
!= 0)
3852 bfd_reloc_status_type r
;
3856 size
= bfd_get_reloc_size (howto
);
3857 buf
= (bfd_byte
*) bfd_zmalloc (size
);
3858 if (buf
== NULL
&& size
!= 0)
3860 r
= MY_relocate_contents (howto
, flaginfo
->output_bfd
,
3861 (bfd_vma
) pr
->addend
, buf
);
3867 case bfd_reloc_outofrange
:
3869 case bfd_reloc_overflow
:
3870 (*flaginfo
->info
->callbacks
->reloc_overflow
)
3871 (flaginfo
->info
, NULL
,
3872 (p
->type
== bfd_section_reloc_link_order
3873 ? bfd_section_name (pr
->u
.section
)
3875 howto
->name
, pr
->addend
, NULL
, NULL
, (bfd_vma
) 0);
3878 ok
= bfd_set_section_contents (flaginfo
->output_bfd
, o
, (void *) buf
,
3879 (file_ptr
) p
->offset
, size
);
3887 #ifdef MY_put_ext_reloc
3888 MY_put_ext_reloc (flaginfo
->output_bfd
, r_extern
, r_index
, p
->offset
,
3889 howto
, &erel
, pr
->addend
);
3891 PUT_WORD (flaginfo
->output_bfd
, p
->offset
, erel
.r_address
);
3893 if (bfd_header_big_endian (flaginfo
->output_bfd
))
3895 erel
.r_index
[0] = r_index
>> 16;
3896 erel
.r_index
[1] = r_index
>> 8;
3897 erel
.r_index
[2] = r_index
;
3899 ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
3900 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
3904 erel
.r_index
[2] = r_index
>> 16;
3905 erel
.r_index
[1] = r_index
>> 8;
3906 erel
.r_index
[0] = r_index
;
3908 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
3909 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
3912 PUT_WORD (flaginfo
->output_bfd
, (bfd_vma
) pr
->addend
, erel
.r_addend
);
3913 #endif /* MY_put_ext_reloc */
3915 rel_ptr
= (void *) &erel
;
3918 amt
= obj_reloc_entry_size (flaginfo
->output_bfd
);
3919 if (bfd_seek (flaginfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0
3920 || bfd_write (rel_ptr
, amt
, flaginfo
->output_bfd
) != amt
)
3923 *reloff_ptr
+= obj_reloc_entry_size (flaginfo
->output_bfd
);
3925 /* Assert that the relocs have not run into the symbols, and that n
3926 the text relocs have not run into the data relocs. */
3927 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (flaginfo
->output_bfd
)
3928 && (reloff_ptr
!= &flaginfo
->treloff
3930 <= obj_datasec (flaginfo
->output_bfd
)->rel_filepos
)));
3935 /* Get the section corresponding to a reloc index. */
3937 static inline asection
*
3938 aout_reloc_index_to_section (bfd
*abfd
, int indx
)
3940 switch (indx
& N_TYPE
)
3942 case N_TEXT
: return obj_textsec (abfd
);
3943 case N_DATA
: return obj_datasec (abfd
);
3944 case N_BSS
: return obj_bsssec (abfd
);
3946 case N_UNDF
: return bfd_abs_section_ptr
;
3952 /* Relocate an a.out section using standard a.out relocs. */
3955 aout_link_input_section_std (struct aout_final_link_info
*flaginfo
,
3957 asection
*input_section
,
3958 struct reloc_std_external
*relocs
,
3959 bfd_size_type rel_size
,
3962 bool (*check_dynamic_reloc
)
3963 (struct bfd_link_info
*, bfd
*, asection
*,
3964 struct aout_link_hash_entry
*, void *, bfd_byte
*, bool *, bfd_vma
*);
3967 struct external_nlist
*syms
;
3969 struct aout_link_hash_entry
**sym_hashes
;
3971 bfd_size_type reloc_count
;
3972 struct reloc_std_external
*rel
;
3973 struct reloc_std_external
*rel_end
;
3975 output_bfd
= flaginfo
->output_bfd
;
3976 check_dynamic_reloc
= aout_backend_info (output_bfd
)->check_dynamic_reloc
;
3978 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
);
3979 BFD_ASSERT (input_bfd
->xvec
->header_byteorder
3980 == output_bfd
->xvec
->header_byteorder
);
3982 relocatable
= bfd_link_relocatable (flaginfo
->info
);
3983 syms
= obj_aout_external_syms (input_bfd
);
3984 strings
= obj_aout_external_strings (input_bfd
);
3985 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
3986 symbol_map
= flaginfo
->symbol_map
;
3988 reloc_count
= rel_size
/ RELOC_STD_SIZE
;
3990 rel_end
= rel
+ reloc_count
;
3991 for (; rel
< rel_end
; rel
++)
3994 unsigned int r_index
;
3998 reloc_howto_type
*howto
;
3999 struct aout_link_hash_entry
*h
= NULL
;
4001 bfd_reloc_status_type r
;
4003 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4005 #ifdef MY_reloc_howto
4006 howto
= MY_reloc_howto (input_bfd
, rel
, r_index
, r_extern
, r_pcrel
);
4012 unsigned int howto_idx
;
4014 if (bfd_header_big_endian (input_bfd
))
4016 r_index
= (((unsigned int) rel
->r_index
[0] << 16)
4017 | ((unsigned int) rel
->r_index
[1] << 8)
4019 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
4020 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
4021 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
4022 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
4023 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
4024 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
4025 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
4029 r_index
= (((unsigned int) rel
->r_index
[2] << 16)
4030 | ((unsigned int) rel
->r_index
[1] << 8)
4032 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
4033 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
4034 r_baserel
= (0 != (rel
->r_type
[0]
4035 & RELOC_STD_BITS_BASEREL_LITTLE
));
4036 r_jmptable
= (0 != (rel
->r_type
[0]
4037 & RELOC_STD_BITS_JMPTABLE_LITTLE
));
4038 r_relative
= (0 != (rel
->r_type
[0]
4039 & RELOC_STD_BITS_RELATIVE_LITTLE
));
4040 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
4041 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
4044 howto_idx
= (r_length
+ 4 * r_pcrel
+ 8 * r_baserel
4045 + 16 * r_jmptable
+ 32 * r_relative
);
4046 if (howto_idx
< TABLE_SIZE (howto_table_std
))
4047 howto
= howto_table_std
+ howto_idx
;
4055 _bfd_error_handler (_("%pB: unsupported relocation type"),
4057 bfd_set_error (bfd_error_bad_value
);
4063 /* We are generating a relocatable output file, and must
4064 modify the reloc accordingly. */
4067 /* If we know the symbol this relocation is against,
4068 convert it into a relocation against a section. This
4069 is what the native linker does. */
4070 h
= sym_hashes
[r_index
];
4072 && (h
->root
.type
== bfd_link_hash_defined
4073 || h
->root
.type
== bfd_link_hash_defweak
))
4075 asection
*output_section
;
4077 /* Change the r_extern value. */
4078 if (bfd_header_big_endian (output_bfd
))
4079 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_BIG
;
4081 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE
;
4083 /* Compute a new r_index. */
4084 output_section
= h
->root
.u
.def
.section
->output_section
;
4085 if (output_section
== obj_textsec (output_bfd
))
4087 else if (output_section
== obj_datasec (output_bfd
))
4089 else if (output_section
== obj_bsssec (output_bfd
))
4094 /* Add the symbol value and the section VMA to the
4095 addend stored in the contents. */
4096 relocation
= (h
->root
.u
.def
.value
4097 + output_section
->vma
4098 + h
->root
.u
.def
.section
->output_offset
);
4102 /* We must change r_index according to the symbol
4104 r_index
= symbol_map
[r_index
];
4110 /* We decided to strip this symbol, but it
4111 turns out that we can't. Note that we
4112 lose the other and desc information here.
4113 I don't think that will ever matter for a
4119 if (!aout_link_write_other_symbol (&h
->root
.root
,
4129 name
= strings
+ GET_WORD (input_bfd
,
4130 syms
[r_index
].e_strx
);
4131 (*flaginfo
->info
->callbacks
->unattached_reloc
)
4132 (flaginfo
->info
, name
,
4133 input_bfd
, input_section
, r_addr
);
4141 /* Write out the new r_index value. */
4142 if (bfd_header_big_endian (output_bfd
))
4144 rel
->r_index
[0] = r_index
>> 16;
4145 rel
->r_index
[1] = r_index
>> 8;
4146 rel
->r_index
[2] = r_index
;
4150 rel
->r_index
[2] = r_index
>> 16;
4151 rel
->r_index
[1] = r_index
>> 8;
4152 rel
->r_index
[0] = r_index
;
4159 /* This is a relocation against a section. We must
4160 adjust by the amount that the section moved. */
4161 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4162 relocation
= (section
->output_section
->vma
4163 + section
->output_offset
4167 /* Change the address of the relocation. */
4168 PUT_WORD (output_bfd
,
4169 r_addr
+ input_section
->output_offset
,
4172 /* Adjust a PC relative relocation by removing the reference
4173 to the original address in the section and including the
4174 reference to the new address. */
4176 relocation
-= (input_section
->output_section
->vma
4177 + input_section
->output_offset
4178 - input_section
->vma
);
4180 #ifdef MY_relocatable_reloc
4181 MY_relocatable_reloc (howto
, output_bfd
, rel
, relocation
, r_addr
);
4184 if (relocation
== 0)
4187 r
= MY_relocate_contents (howto
,
4188 input_bfd
, relocation
,
4195 /* We are generating an executable, and must do a full
4201 h
= sym_hashes
[r_index
];
4204 && (h
->root
.type
== bfd_link_hash_defined
4205 || h
->root
.type
== bfd_link_hash_defweak
))
4207 relocation
= (h
->root
.u
.def
.value
4208 + h
->root
.u
.def
.section
->output_section
->vma
4209 + h
->root
.u
.def
.section
->output_offset
);
4212 && h
->root
.type
== bfd_link_hash_undefweak
)
4224 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4225 relocation
= (section
->output_section
->vma
4226 + section
->output_offset
4229 relocation
+= input_section
->vma
;
4232 if (check_dynamic_reloc
!= NULL
)
4236 if (! ((*check_dynamic_reloc
)
4237 (flaginfo
->info
, input_bfd
, input_section
, h
,
4238 (void *) rel
, contents
, &skip
, &relocation
)))
4244 /* Now warn if a global symbol is undefined. We could not
4245 do this earlier, because check_dynamic_reloc might want
4246 to skip this reloc. */
4247 if (hundef
&& ! bfd_link_pic (flaginfo
->info
) && ! r_baserel
)
4252 name
= h
->root
.root
.string
;
4254 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4255 (*flaginfo
->info
->callbacks
->undefined_symbol
)
4256 (flaginfo
->info
, name
, input_bfd
, input_section
, r_addr
, true);
4259 r
= MY_final_link_relocate (howto
,
4260 input_bfd
, input_section
,
4261 contents
, r_addr
, relocation
,
4265 if (r
!= bfd_reloc_ok
)
4270 case bfd_reloc_outofrange
:
4272 case bfd_reloc_overflow
:
4279 name
= strings
+ GET_WORD (input_bfd
,
4280 syms
[r_index
].e_strx
);
4285 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4286 name
= bfd_section_name (s
);
4288 (*flaginfo
->info
->callbacks
->reloc_overflow
)
4289 (flaginfo
->info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
4290 (bfd_vma
) 0, input_bfd
, input_section
, r_addr
);
4300 /* Relocate an a.out section using extended a.out relocs. */
4303 aout_link_input_section_ext (struct aout_final_link_info
*flaginfo
,
4305 asection
*input_section
,
4306 struct reloc_ext_external
*relocs
,
4307 bfd_size_type rel_size
,
4310 bool (*check_dynamic_reloc
)
4311 (struct bfd_link_info
*, bfd
*, asection
*,
4312 struct aout_link_hash_entry
*, void *, bfd_byte
*, bool *, bfd_vma
*);
4315 struct external_nlist
*syms
;
4317 struct aout_link_hash_entry
**sym_hashes
;
4319 bfd_size_type reloc_count
;
4320 struct reloc_ext_external
*rel
;
4321 struct reloc_ext_external
*rel_end
;
4323 output_bfd
= flaginfo
->output_bfd
;
4324 check_dynamic_reloc
= aout_backend_info (output_bfd
)->check_dynamic_reloc
;
4326 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_EXT_SIZE
);
4327 BFD_ASSERT (input_bfd
->xvec
->header_byteorder
4328 == output_bfd
->xvec
->header_byteorder
);
4330 relocatable
= bfd_link_relocatable (flaginfo
->info
);
4331 syms
= obj_aout_external_syms (input_bfd
);
4332 strings
= obj_aout_external_strings (input_bfd
);
4333 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4334 symbol_map
= flaginfo
->symbol_map
;
4336 reloc_count
= rel_size
/ RELOC_EXT_SIZE
;
4338 rel_end
= rel
+ reloc_count
;
4339 for (; rel
< rel_end
; rel
++)
4342 unsigned int r_index
;
4344 unsigned int r_type
;
4346 struct aout_link_hash_entry
*h
= NULL
;
4347 asection
*r_section
= NULL
;
4350 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4352 if (bfd_header_big_endian (input_bfd
))
4354 r_index
= (((unsigned int) rel
->r_index
[0] << 16)
4355 | ((unsigned int) rel
->r_index
[1] << 8)
4357 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
4358 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
4359 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
4363 r_index
= (((unsigned int) rel
->r_index
[2] << 16)
4364 | ((unsigned int) rel
->r_index
[1] << 8)
4366 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
4367 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
4368 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
4371 r_addend
= GET_SWORD (input_bfd
, rel
->r_addend
);
4373 if (r_type
>= TABLE_SIZE (howto_table_ext
))
4375 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4377 bfd_set_error (bfd_error_bad_value
);
4383 /* We are generating a relocatable output file, and must
4384 modify the reloc accordingly. */
4386 || r_type
== (unsigned int) RELOC_BASE10
4387 || r_type
== (unsigned int) RELOC_BASE13
4388 || r_type
== (unsigned int) RELOC_BASE22
)
4390 /* If we know the symbol this relocation is against,
4391 convert it into a relocation against a section. This
4392 is what the native linker does. */
4393 if (r_type
== (unsigned int) RELOC_BASE10
4394 || r_type
== (unsigned int) RELOC_BASE13
4395 || r_type
== (unsigned int) RELOC_BASE22
)
4398 h
= sym_hashes
[r_index
];
4400 && (h
->root
.type
== bfd_link_hash_defined
4401 || h
->root
.type
== bfd_link_hash_defweak
))
4403 asection
*output_section
;
4405 /* Change the r_extern value. */
4406 if (bfd_header_big_endian (output_bfd
))
4407 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_BIG
;
4409 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE
;
4411 /* Compute a new r_index. */
4412 output_section
= h
->root
.u
.def
.section
->output_section
;
4413 if (output_section
== obj_textsec (output_bfd
))
4415 else if (output_section
== obj_datasec (output_bfd
))
4417 else if (output_section
== obj_bsssec (output_bfd
))
4422 /* Add the symbol value and the section VMA to the
4424 relocation
= (h
->root
.u
.def
.value
4425 + output_section
->vma
4426 + h
->root
.u
.def
.section
->output_offset
);
4428 /* Now RELOCATION is the VMA of the final
4429 destination. If this is a PC relative reloc,
4430 then ADDEND is the negative of the source VMA.
4431 We want to set ADDEND to the difference between
4432 the destination VMA and the source VMA, which
4433 means we must adjust RELOCATION by the change in
4434 the source VMA. This is done below. */
4438 /* We must change r_index according to the symbol
4440 r_index
= symbol_map
[r_index
];
4446 /* We decided to strip this symbol, but it
4447 turns out that we can't. Note that we
4448 lose the other and desc information here.
4449 I don't think that will ever matter for a
4455 if (!aout_link_write_other_symbol (&h
->root
.root
,
4465 name
= strings
+ GET_WORD (input_bfd
,
4466 syms
[r_index
].e_strx
);
4467 (*flaginfo
->info
->callbacks
->unattached_reloc
)
4468 (flaginfo
->info
, name
,
4469 input_bfd
, input_section
, r_addr
);
4476 /* If this is a PC relative reloc, then the addend
4477 is the negative of the source VMA. We must
4478 adjust it by the change in the source VMA. This
4482 /* Write out the new r_index value. */
4483 if (bfd_header_big_endian (output_bfd
))
4485 rel
->r_index
[0] = r_index
>> 16;
4486 rel
->r_index
[1] = r_index
>> 8;
4487 rel
->r_index
[2] = r_index
;
4491 rel
->r_index
[2] = r_index
>> 16;
4492 rel
->r_index
[1] = r_index
>> 8;
4493 rel
->r_index
[0] = r_index
;
4498 /* This is a relocation against a section. We must
4499 adjust by the amount that the section moved. */
4500 r_section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4501 relocation
= (r_section
->output_section
->vma
4502 + r_section
->output_offset
4505 /* If this is a PC relative reloc, then the addend is
4506 the difference in VMA between the destination and the
4507 source. We have just adjusted for the change in VMA
4508 of the destination, so we must also adjust by the
4509 change in VMA of the source. This is done below. */
4512 /* As described above, we must always adjust a PC relative
4513 reloc by the change in VMA of the source. However, if
4514 pcrel_offset is set, then the addend does not include the
4515 location within the section, in which case we don't need
4516 to adjust anything. */
4517 if (howto_table_ext
[r_type
].pc_relative
4518 && ! howto_table_ext
[r_type
].pcrel_offset
)
4519 relocation
-= (input_section
->output_section
->vma
4520 + input_section
->output_offset
4521 - input_section
->vma
);
4523 /* Change the addend if necessary. */
4524 if (relocation
!= 0)
4525 PUT_WORD (output_bfd
, r_addend
+ relocation
, rel
->r_addend
);
4527 /* Change the address of the relocation. */
4528 PUT_WORD (output_bfd
,
4529 r_addr
+ input_section
->output_offset
,
4535 bfd_reloc_status_type r
;
4537 /* We are generating an executable, and must do a full
4543 h
= sym_hashes
[r_index
];
4546 && (h
->root
.type
== bfd_link_hash_defined
4547 || h
->root
.type
== bfd_link_hash_defweak
))
4549 relocation
= (h
->root
.u
.def
.value
4550 + h
->root
.u
.def
.section
->output_section
->vma
4551 + h
->root
.u
.def
.section
->output_offset
);
4554 && h
->root
.type
== bfd_link_hash_undefweak
)
4562 else if (r_type
== (unsigned int) RELOC_BASE10
4563 || r_type
== (unsigned int) RELOC_BASE13
4564 || r_type
== (unsigned int) RELOC_BASE22
)
4566 struct external_nlist
*sym
;
4569 /* For base relative relocs, r_index is always an index
4570 into the symbol table, even if r_extern is 0. */
4571 sym
= syms
+ r_index
;
4572 type
= H_GET_8 (input_bfd
, sym
->e_type
);
4573 if ((type
& N_TYPE
) == N_TEXT
4575 r_section
= obj_textsec (input_bfd
);
4576 else if ((type
& N_TYPE
) == N_DATA
4578 r_section
= obj_datasec (input_bfd
);
4579 else if ((type
& N_TYPE
) == N_BSS
4581 r_section
= obj_bsssec (input_bfd
);
4582 else if ((type
& N_TYPE
) == N_ABS
4584 r_section
= bfd_abs_section_ptr
;
4587 relocation
= (r_section
->output_section
->vma
4588 + r_section
->output_offset
4589 + (GET_WORD (input_bfd
, sym
->e_value
)
4594 r_section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4596 /* If this is a PC relative reloc, then R_ADDEND is the
4597 difference between the two vmas, or
4598 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4600 old_dest_sec == section->vma
4602 old_src_sec == input_section->vma
4604 old_src_off == r_addr
4606 _bfd_final_link_relocate expects RELOCATION +
4607 R_ADDEND to be the VMA of the destination minus
4608 r_addr (the minus r_addr is because this relocation
4609 is not pcrel_offset, which is a bit confusing and
4610 should, perhaps, be changed), or
4613 new_dest_sec == output_section->vma + output_offset
4614 We arrange for this to happen by setting RELOCATION to
4615 new_dest_sec + old_src_sec - old_dest_sec
4617 If this is not a PC relative reloc, then R_ADDEND is
4618 simply the VMA of the destination, so we set
4619 RELOCATION to the change in the destination VMA, or
4620 new_dest_sec - old_dest_sec
4622 relocation
= (r_section
->output_section
->vma
4623 + r_section
->output_offset
4625 if (howto_table_ext
[r_type
].pc_relative
)
4626 relocation
+= input_section
->vma
;
4629 if (check_dynamic_reloc
!= NULL
)
4633 if (! ((*check_dynamic_reloc
)
4634 (flaginfo
->info
, input_bfd
, input_section
, h
,
4635 (void *) rel
, contents
, &skip
, &relocation
)))
4641 /* Now warn if a global symbol is undefined. We could not
4642 do this earlier, because check_dynamic_reloc might want
4643 to skip this reloc. */
4645 && ! bfd_link_pic (flaginfo
->info
)
4646 && r_type
!= (unsigned int) RELOC_BASE10
4647 && r_type
!= (unsigned int) RELOC_BASE13
4648 && r_type
!= (unsigned int) RELOC_BASE22
)
4653 name
= h
->root
.root
.string
;
4655 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4656 (*flaginfo
->info
->callbacks
->undefined_symbol
)
4657 (flaginfo
->info
, name
, input_bfd
, input_section
, r_addr
, true);
4660 if (r_type
!= (unsigned int) RELOC_SPARC_REV32
)
4661 r
= MY_final_link_relocate (howto_table_ext
+ r_type
,
4662 input_bfd
, input_section
,
4663 contents
, r_addr
, relocation
,
4669 x
= bfd_get_32 (input_bfd
, contents
+ r_addr
);
4670 x
= x
+ relocation
+ r_addend
;
4671 bfd_putl32 (/*input_bfd,*/ x
, contents
+ r_addr
);
4675 if (r
!= bfd_reloc_ok
)
4680 case bfd_reloc_outofrange
:
4682 case bfd_reloc_overflow
:
4689 || r_type
== (unsigned int) RELOC_BASE10
4690 || r_type
== (unsigned int) RELOC_BASE13
4691 || r_type
== (unsigned int) RELOC_BASE22
)
4692 name
= strings
+ GET_WORD (input_bfd
,
4693 syms
[r_index
].e_strx
);
4698 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4699 name
= bfd_section_name (s
);
4701 (*flaginfo
->info
->callbacks
->reloc_overflow
)
4702 (flaginfo
->info
, (h
? &h
->root
: NULL
), name
,
4703 howto_table_ext
[r_type
].name
,
4704 r_addend
, input_bfd
, input_section
, r_addr
);
4715 /* Link an a.out section into the output file. */
4718 aout_link_input_section (struct aout_final_link_info
*flaginfo
,
4720 asection
*input_section
,
4721 file_ptr
*reloff_ptr
,
4722 bfd_size_type rel_size
)
4724 bfd_size_type input_size
;
4727 /* Get the section contents. */
4728 input_size
= input_section
->size
;
4729 if (! bfd_get_section_contents (input_bfd
, input_section
,
4730 (void *) flaginfo
->contents
,
4731 (file_ptr
) 0, input_size
))
4734 /* Read in the relocs if we haven't already done it. */
4735 if (aout_section_data (input_section
) != NULL
4736 && aout_section_data (input_section
)->relocs
!= NULL
)
4737 relocs
= aout_section_data (input_section
)->relocs
;
4740 relocs
= flaginfo
->relocs
;
4743 if (bfd_seek (input_bfd
, input_section
->rel_filepos
, SEEK_SET
) != 0
4744 || bfd_read (relocs
, rel_size
, input_bfd
) != rel_size
)
4749 /* Relocate the section contents. */
4750 if (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
)
4752 if (! aout_link_input_section_std (flaginfo
, input_bfd
, input_section
,
4753 (struct reloc_std_external
*) relocs
,
4754 rel_size
, flaginfo
->contents
))
4759 if (! aout_link_input_section_ext (flaginfo
, input_bfd
, input_section
,
4760 (struct reloc_ext_external
*) relocs
,
4761 rel_size
, flaginfo
->contents
))
4765 /* Write out the section contents. */
4766 if (! bfd_set_section_contents (flaginfo
->output_bfd
,
4767 input_section
->output_section
,
4768 (void *) flaginfo
->contents
,
4769 (file_ptr
) input_section
->output_offset
,
4773 /* If we are producing relocatable output, the relocs were
4774 modified, and we now write them out. */
4775 if (bfd_link_relocatable (flaginfo
->info
) && rel_size
> 0)
4777 if (bfd_seek (flaginfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0)
4779 if (bfd_write (relocs
, rel_size
, flaginfo
->output_bfd
) != rel_size
)
4781 *reloff_ptr
+= rel_size
;
4783 /* Assert that the relocs have not run into the symbols, and
4784 that if these are the text relocs they have not run into the
4786 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (flaginfo
->output_bfd
)
4787 && (reloff_ptr
!= &flaginfo
->treloff
4789 <= obj_datasec (flaginfo
->output_bfd
)->rel_filepos
)));
4795 /* Adjust and write out the symbols for an a.out file. Set the new
4796 symbol indices into a symbol_map. */
4799 aout_link_write_symbols (struct aout_final_link_info
*flaginfo
, bfd
*input_bfd
)
4802 bfd_size_type sym_count
;
4804 enum bfd_link_strip strip
;
4805 enum bfd_link_discard discard
;
4806 struct external_nlist
*outsym
;
4807 bfd_size_type strtab_index
;
4808 struct external_nlist
*sym
;
4809 struct external_nlist
*sym_end
;
4810 struct aout_link_hash_entry
**sym_hash
;
4815 output_bfd
= flaginfo
->output_bfd
;
4816 sym_count
= obj_aout_external_sym_count (input_bfd
);
4817 strings
= obj_aout_external_strings (input_bfd
);
4818 strip
= flaginfo
->info
->strip
;
4819 discard
= flaginfo
->info
->discard
;
4820 outsym
= flaginfo
->output_syms
;
4822 /* First write out a symbol for this object file, unless we are
4823 discarding such symbols. */
4824 if (strip
!= strip_all
4825 && (strip
!= strip_some
4826 || bfd_hash_lookup (flaginfo
->info
->keep_hash
,
4827 bfd_get_filename (input_bfd
),
4828 false, false) != NULL
)
4829 && discard
!= discard_all
)
4831 H_PUT_8 (output_bfd
, N_TEXT
, outsym
->e_type
);
4832 H_PUT_8 (output_bfd
, 0, outsym
->e_other
);
4833 H_PUT_16 (output_bfd
, 0, outsym
->e_desc
);
4834 strtab_index
= add_to_stringtab (output_bfd
, flaginfo
->strtab
,
4835 bfd_get_filename (input_bfd
), false);
4836 if (strtab_index
== (bfd_size_type
) -1)
4838 PUT_WORD (output_bfd
, strtab_index
, outsym
->e_strx
);
4839 PUT_WORD (output_bfd
,
4840 (bfd_section_vma (obj_textsec (input_bfd
)->output_section
)
4841 + obj_textsec (input_bfd
)->output_offset
),
4843 ++obj_aout_external_sym_count (output_bfd
);
4849 sym
= obj_aout_external_syms (input_bfd
);
4850 sym_end
= sym
+ sym_count
;
4851 sym_hash
= obj_aout_sym_hashes (input_bfd
);
4852 symbol_map
= flaginfo
->symbol_map
;
4853 memset (symbol_map
, 0, (size_t) sym_count
* sizeof *symbol_map
);
4854 for (; sym
< sym_end
; sym
++, sym_hash
++, symbol_map
++)
4858 struct aout_link_hash_entry
*h
;
4864 /* We set *symbol_map to 0 above for all symbols. If it has
4865 already been set to -1 for this symbol, it means that we are
4866 discarding it because it appears in a duplicate header file.
4867 See the N_BINCL code below. */
4868 if (*symbol_map
== -1)
4871 /* Initialize *symbol_map to -1, which means that the symbol was
4872 not copied into the output file. We will change it later if
4873 we do copy the symbol over. */
4876 type
= H_GET_8 (input_bfd
, sym
->e_type
);
4877 name
= strings
+ GET_WORD (input_bfd
, sym
->e_strx
);
4883 /* Pass this symbol through. It is the target of an
4884 indirect or warning symbol. */
4885 val
= GET_WORD (input_bfd
, sym
->e_value
);
4890 /* Skip this symbol, which is the target of an indirect
4891 symbol that we have changed to no longer be an indirect
4898 struct aout_link_hash_entry
*hresolve
;
4900 /* We have saved the hash table entry for this symbol, if
4901 there is one. Note that we could just look it up again
4902 in the hash table, provided we first check that it is an
4906 /* Use the name from the hash table, in case the symbol was
4909 && h
->root
.type
!= bfd_link_hash_warning
)
4910 name
= h
->root
.root
.string
;
4912 /* If this is an indirect or warning symbol, then change
4913 hresolve to the base symbol. We also change *sym_hash so
4914 that the relocation routines relocate against the real
4917 if (h
!= (struct aout_link_hash_entry
*) NULL
4918 && (h
->root
.type
== bfd_link_hash_indirect
4919 || h
->root
.type
== bfd_link_hash_warning
))
4921 hresolve
= (struct aout_link_hash_entry
*) h
->root
.u
.i
.link
;
4922 while (hresolve
->root
.type
== bfd_link_hash_indirect
4923 || hresolve
->root
.type
== bfd_link_hash_warning
)
4924 hresolve
= ((struct aout_link_hash_entry
*)
4925 hresolve
->root
.u
.i
.link
);
4926 *sym_hash
= hresolve
;
4929 /* If the symbol has already been written out, skip it. */
4933 if ((type
& N_TYPE
) == N_INDR
4934 || type
== N_WARNING
)
4936 *symbol_map
= h
->indx
;
4940 /* See if we are stripping this symbol. */
4946 case strip_debugger
:
4947 if ((type
& N_STAB
) != 0)
4951 if (bfd_hash_lookup (flaginfo
->info
->keep_hash
, name
, false, false)
4966 /* Get the value of the symbol. */
4967 if ((type
& N_TYPE
) == N_TEXT
4969 symsec
= obj_textsec (input_bfd
);
4970 else if ((type
& N_TYPE
) == N_DATA
4972 symsec
= obj_datasec (input_bfd
);
4973 else if ((type
& N_TYPE
) == N_BSS
4975 symsec
= obj_bsssec (input_bfd
);
4976 else if ((type
& N_TYPE
) == N_ABS
4978 symsec
= bfd_abs_section_ptr
;
4979 else if (((type
& N_TYPE
) == N_INDR
4980 && (hresolve
== NULL
4981 || (hresolve
->root
.type
!= bfd_link_hash_defined
4982 && hresolve
->root
.type
!= bfd_link_hash_defweak
4983 && hresolve
->root
.type
!= bfd_link_hash_common
)))
4984 || type
== N_WARNING
)
4986 /* Pass the next symbol through unchanged. The
4987 condition above for indirect symbols is so that if
4988 the indirect symbol was defined, we output it with
4989 the correct definition so the debugger will
4992 val
= GET_WORD (input_bfd
, sym
->e_value
);
4995 else if ((type
& N_STAB
) != 0)
4997 val
= GET_WORD (input_bfd
, sym
->e_value
);
5002 /* If we get here with an indirect symbol, it means that
5003 we are outputting it with a real definition. In such
5004 a case we do not want to output the next symbol,
5005 which is the target of the indirection. */
5006 if ((type
& N_TYPE
) == N_INDR
)
5011 /* We need to get the value from the hash table. We use
5012 hresolve so that if we have defined an indirect
5013 symbol we output the final definition. */
5016 switch (type
& N_TYPE
)
5019 symsec
= obj_textsec (input_bfd
);
5022 symsec
= obj_datasec (input_bfd
);
5025 symsec
= obj_bsssec (input_bfd
);
5028 symsec
= bfd_abs_section_ptr
;
5035 else if (hresolve
->root
.type
== bfd_link_hash_defined
5036 || hresolve
->root
.type
== bfd_link_hash_defweak
)
5038 asection
*input_section
;
5039 asection
*output_section
;
5041 /* This case usually means a common symbol which was
5042 turned into a defined symbol. */
5043 input_section
= hresolve
->root
.u
.def
.section
;
5044 output_section
= input_section
->output_section
;
5045 BFD_ASSERT (bfd_is_abs_section (output_section
)
5046 || output_section
->owner
== output_bfd
);
5047 val
= (hresolve
->root
.u
.def
.value
5048 + bfd_section_vma (output_section
)
5049 + input_section
->output_offset
);
5051 /* Get the correct type based on the section. If
5052 this is a constructed set, force it to be
5053 globally visible. */
5062 if (output_section
== obj_textsec (output_bfd
))
5063 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
5066 else if (output_section
== obj_datasec (output_bfd
))
5067 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
5070 else if (output_section
== obj_bsssec (output_bfd
))
5071 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
5075 type
|= (hresolve
->root
.type
== bfd_link_hash_defined
5079 else if (hresolve
->root
.type
== bfd_link_hash_common
)
5080 val
= hresolve
->root
.u
.c
.size
;
5081 else if (hresolve
->root
.type
== bfd_link_hash_undefweak
)
5090 val
= (symsec
->output_section
->vma
5091 + symsec
->output_offset
5092 + (GET_WORD (input_bfd
, sym
->e_value
)
5095 /* If this is a global symbol set the written flag, and if
5096 it is a local symbol see if we should discard it. */
5100 h
->indx
= obj_aout_external_sym_count (output_bfd
);
5102 else if ((type
& N_TYPE
) != N_SETT
5103 && (type
& N_TYPE
) != N_SETD
5104 && (type
& N_TYPE
) != N_SETB
5105 && (type
& N_TYPE
) != N_SETA
)
5110 case discard_sec_merge
:
5113 if ((type
& N_STAB
) == 0
5114 && bfd_is_local_label_name (input_bfd
, name
))
5128 /* An N_BINCL symbol indicates the start of the stabs
5129 entries for a header file. We need to scan ahead to the
5130 next N_EINCL symbol, ignoring nesting, adding up all the
5131 characters in the symbol names, not including the file
5132 numbers in types (the first number after an open
5134 if (type
== (int) N_BINCL
)
5136 struct external_nlist
*incl_sym
;
5138 struct aout_link_includes_entry
*incl_entry
;
5139 struct aout_link_includes_totals
*t
;
5143 for (incl_sym
= sym
+ 1; incl_sym
< sym_end
; incl_sym
++)
5147 incl_type
= H_GET_8 (input_bfd
, incl_sym
->e_type
);
5148 if (incl_type
== (int) N_EINCL
)
5154 else if (incl_type
== (int) N_BINCL
)
5160 s
= strings
+ GET_WORD (input_bfd
, incl_sym
->e_strx
);
5161 for (; *s
!= '\0'; s
++)
5166 /* Skip the file number. */
5168 while (ISDIGIT (*s
))
5176 /* If we have already included a header file with the
5177 same value, then replace this one with an N_EXCL
5179 copy
= !flaginfo
->info
->keep_memory
;
5180 incl_entry
= aout_link_includes_lookup (&flaginfo
->includes
,
5182 if (incl_entry
== NULL
)
5184 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
5185 if (t
->total
== val
)
5189 /* This is the first time we have seen this header
5190 file with this set of stabs strings. */
5191 t
= (struct aout_link_includes_totals
*)
5192 bfd_hash_allocate (&flaginfo
->includes
.root
,
5197 t
->next
= incl_entry
->totals
;
5198 incl_entry
->totals
= t
;
5204 /* This is a duplicate header file. We must change
5205 it to be an N_EXCL entry, and mark all the
5206 included symbols to prevent outputting them. */
5207 type
= (int) N_EXCL
;
5210 for (incl_sym
= sym
+ 1, incl_map
= symbol_map
+ 1;
5212 incl_sym
++, incl_map
++)
5216 incl_type
= H_GET_8 (input_bfd
, incl_sym
->e_type
);
5217 if (incl_type
== (int) N_EINCL
)
5226 else if (incl_type
== (int) N_BINCL
)
5235 /* Copy this symbol into the list of symbols we are going to
5237 H_PUT_8 (output_bfd
, type
, outsym
->e_type
);
5238 H_PUT_8 (output_bfd
, H_GET_8 (input_bfd
, sym
->e_other
), outsym
->e_other
);
5239 H_PUT_16 (output_bfd
, H_GET_16 (input_bfd
, sym
->e_desc
), outsym
->e_desc
);
5241 if (! flaginfo
->info
->keep_memory
)
5243 /* name points into a string table which we are going to
5244 free. If there is a hash table entry, use that string.
5245 Otherwise, copy name into memory. */
5247 name
= h
->root
.root
.string
;
5251 strtab_index
= add_to_stringtab (output_bfd
, flaginfo
->strtab
,
5253 if (strtab_index
== (bfd_size_type
) -1)
5255 PUT_WORD (output_bfd
, strtab_index
, outsym
->e_strx
);
5256 PUT_WORD (output_bfd
, val
, outsym
->e_value
);
5257 *symbol_map
= obj_aout_external_sym_count (output_bfd
);
5258 ++obj_aout_external_sym_count (output_bfd
);
5262 /* Write out the output symbols we have just constructed. */
5263 if (outsym
> flaginfo
->output_syms
)
5265 bfd_size_type outsym_size
;
5267 if (bfd_seek (output_bfd
, flaginfo
->symoff
, SEEK_SET
) != 0)
5269 outsym_size
= outsym
- flaginfo
->output_syms
;
5270 outsym_size
*= EXTERNAL_NLIST_SIZE
;
5271 if (bfd_write (flaginfo
->output_syms
, outsym_size
, output_bfd
)
5274 flaginfo
->symoff
+= outsym_size
;
5280 /* Link an a.out input BFD into the output file. */
5283 aout_link_input_bfd (struct aout_final_link_info
*flaginfo
, bfd
*input_bfd
)
5285 BFD_ASSERT (bfd_get_format (input_bfd
) == bfd_object
);
5287 /* If this is a dynamic object, it may need special handling. */
5288 if ((input_bfd
->flags
& DYNAMIC
) != 0
5289 && aout_backend_info (input_bfd
)->link_dynamic_object
!= NULL
)
5290 return ((*aout_backend_info (input_bfd
)->link_dynamic_object
)
5291 (flaginfo
->info
, input_bfd
));
5293 /* Get the symbols. We probably have them already, unless
5294 flaginfo->info->keep_memory is FALSE. */
5295 if (! aout_get_external_symbols (input_bfd
))
5298 /* Write out the symbols and get a map of the new indices. The map
5299 is placed into flaginfo->symbol_map. */
5300 if (! aout_link_write_symbols (flaginfo
, input_bfd
))
5303 /* Relocate and write out the sections. These functions use the
5304 symbol map created by aout_link_write_symbols. The linker_mark
5305 field will be set if these sections are to be included in the
5306 link, which will normally be the case. */
5307 if (obj_textsec (input_bfd
)->linker_mark
)
5309 if (! aout_link_input_section (flaginfo
, input_bfd
,
5310 obj_textsec (input_bfd
),
5312 exec_hdr (input_bfd
)->a_trsize
))
5315 if (obj_datasec (input_bfd
)->linker_mark
)
5317 if (! aout_link_input_section (flaginfo
, input_bfd
,
5318 obj_datasec (input_bfd
),
5320 exec_hdr (input_bfd
)->a_drsize
))
5324 /* If we are not keeping memory, we don't need the symbols any
5325 longer. We still need them if we are keeping memory, because the
5326 strings in the hash table point into them. */
5327 if (! flaginfo
->info
->keep_memory
)
5329 if (! aout_link_free_symbols (input_bfd
))
5336 /* Do the final link step. This is called on the output BFD. The
5337 INFO structure should point to a list of BFDs linked through the
5338 link.next field which can be used to find each BFD which takes part
5339 in the output. Also, each section in ABFD should point to a list
5340 of bfd_link_order structures which list all the input sections for
5341 the output section. */
5344 NAME (aout
, final_link
) (bfd
*abfd
,
5345 struct bfd_link_info
*info
,
5346 void (*callback
) (bfd
*, file_ptr
*, file_ptr
*, file_ptr
*))
5348 struct aout_final_link_info aout_info
;
5349 bool includes_hash_initialized
= false;
5351 bfd_size_type trsize
, drsize
;
5352 bfd_size_type max_contents_size
;
5353 bfd_size_type max_relocs_size
;
5354 bfd_size_type max_sym_count
;
5355 struct bfd_link_order
*p
;
5357 bool have_link_order_relocs
;
5359 if (bfd_link_pic (info
))
5360 abfd
->flags
|= DYNAMIC
;
5362 aout_info
.info
= info
;
5363 aout_info
.output_bfd
= abfd
;
5364 aout_info
.contents
= NULL
;
5365 aout_info
.relocs
= NULL
;
5366 aout_info
.symbol_map
= NULL
;
5367 aout_info
.output_syms
= NULL
;
5369 if (!bfd_hash_table_init_n (&aout_info
.includes
.root
,
5370 aout_link_includes_newfunc
,
5371 sizeof (struct aout_link_includes_entry
),
5374 includes_hash_initialized
= true;
5376 /* Figure out the largest section size. Also, if generating
5377 relocatable output, count the relocs. */
5380 max_contents_size
= 0;
5381 max_relocs_size
= 0;
5383 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
5387 if (bfd_link_relocatable (info
))
5389 if (bfd_get_flavour (sub
) == bfd_target_aout_flavour
)
5391 trsize
+= exec_hdr (sub
)->a_trsize
;
5392 drsize
+= exec_hdr (sub
)->a_drsize
;
5396 /* FIXME: We need to identify the .text and .data sections
5397 and call get_reloc_upper_bound and canonicalize_reloc to
5398 work out the number of relocs needed, and then multiply
5399 by the reloc size. */
5401 /* xgettext:c-format */
5402 (_("%pB: relocatable link from %s to %s not supported"),
5403 abfd
, sub
->xvec
->name
, abfd
->xvec
->name
);
5404 bfd_set_error (bfd_error_invalid_operation
);
5409 if (bfd_get_flavour (sub
) == bfd_target_aout_flavour
)
5411 sz
= obj_textsec (sub
)->size
;
5412 if (sz
> max_contents_size
)
5413 max_contents_size
= sz
;
5414 sz
= obj_datasec (sub
)->size
;
5415 if (sz
> max_contents_size
)
5416 max_contents_size
= sz
;
5418 sz
= exec_hdr (sub
)->a_trsize
;
5419 if (sz
> max_relocs_size
)
5420 max_relocs_size
= sz
;
5421 sz
= exec_hdr (sub
)->a_drsize
;
5422 if (sz
> max_relocs_size
)
5423 max_relocs_size
= sz
;
5425 sz
= obj_aout_external_sym_count (sub
);
5426 if (sz
> max_sym_count
)
5431 if (bfd_link_relocatable (info
))
5433 if (obj_textsec (abfd
) != NULL
)
5434 trsize
+= (_bfd_count_link_order_relocs (obj_textsec (abfd
)
5435 ->map_head
.link_order
)
5436 * obj_reloc_entry_size (abfd
));
5437 if (obj_datasec (abfd
) != NULL
)
5438 drsize
+= (_bfd_count_link_order_relocs (obj_datasec (abfd
)
5439 ->map_head
.link_order
)
5440 * obj_reloc_entry_size (abfd
));
5443 exec_hdr (abfd
)->a_trsize
= trsize
;
5444 exec_hdr (abfd
)->a_drsize
= drsize
;
5446 exec_hdr (abfd
)->a_entry
= bfd_get_start_address (abfd
);
5448 /* Adjust the section sizes and vmas according to the magic number.
5449 This sets a_text, a_data and a_bss in the exec_hdr and sets the
5450 filepos for each section. */
5451 if (! NAME (aout
, adjust_sizes_and_vmas
) (abfd
))
5454 /* The relocation and symbol file positions differ among a.out
5455 targets. We are passed a callback routine from the backend
5456 specific code to handle this.
5457 FIXME: At this point we do not know how much space the symbol
5458 table will require. This will not work for any (nonstandard)
5459 a.out target that needs to know the symbol table size before it
5460 can compute the relocation file positions. */
5461 (*callback
) (abfd
, &aout_info
.treloff
, &aout_info
.dreloff
,
5463 obj_textsec (abfd
)->rel_filepos
= aout_info
.treloff
;
5464 obj_datasec (abfd
)->rel_filepos
= aout_info
.dreloff
;
5465 obj_sym_filepos (abfd
) = aout_info
.symoff
;
5467 /* We keep a count of the symbols as we output them. */
5468 obj_aout_external_sym_count (abfd
) = 0;
5470 /* We accumulate the string table as we write out the symbols. */
5471 aout_info
.strtab
= _bfd_stringtab_init ();
5472 if (aout_info
.strtab
== NULL
)
5475 /* Allocate buffers to hold section contents and relocs. */
5476 aout_info
.contents
= (bfd_byte
*) bfd_malloc (max_contents_size
);
5477 aout_info
.relocs
= bfd_malloc (max_relocs_size
);
5478 aout_info
.symbol_map
= (int *) bfd_malloc (max_sym_count
* sizeof (int));
5479 aout_info
.output_syms
= (struct external_nlist
*)
5480 bfd_malloc ((max_sym_count
+ 1) * sizeof (struct external_nlist
));
5481 if ((aout_info
.contents
== NULL
&& max_contents_size
!= 0)
5482 || (aout_info
.relocs
== NULL
&& max_relocs_size
!= 0)
5483 || (aout_info
.symbol_map
== NULL
&& max_sym_count
!= 0)
5484 || aout_info
.output_syms
== NULL
)
5487 /* If we have a symbol named __DYNAMIC, force it out now. This is
5488 required by SunOS. Doing this here rather than in sunos.c is a
5489 hack, but it's easier than exporting everything which would be
5492 struct aout_link_hash_entry
*h
;
5494 h
= aout_link_hash_lookup (aout_hash_table (info
), "__DYNAMIC",
5495 false, false, false);
5497 aout_link_write_other_symbol (&h
->root
.root
, &aout_info
);
5500 /* The most time efficient way to do the link would be to read all
5501 the input object files into memory and then sort out the
5502 information into the output file. Unfortunately, that will
5503 probably use too much memory. Another method would be to step
5504 through everything that composes the text section and write it
5505 out, and then everything that composes the data section and write
5506 it out, and then write out the relocs, and then write out the
5507 symbols. Unfortunately, that requires reading stuff from each
5508 input file several times, and we will not be able to keep all the
5509 input files open simultaneously, and reopening them will be slow.
5511 What we do is basically process one input file at a time. We do
5512 everything we need to do with an input file once--copy over the
5513 section contents, handle the relocation information, and write
5514 out the symbols--and then we throw away the information we read
5515 from it. This approach requires a lot of lseeks of the output
5516 file, which is unfortunate but still faster than reopening a lot
5519 We use the output_has_begun field of the input BFDs to see
5520 whether we have already handled it. */
5521 for (sub
= info
->input_bfds
; sub
!= NULL
; sub
= sub
->link
.next
)
5522 sub
->output_has_begun
= false;
5524 /* Mark all sections which are to be included in the link. This
5525 will normally be every section. We need to do this so that we
5526 can identify any sections which the linker has decided to not
5528 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5530 for (p
= o
->map_head
.link_order
; p
!= NULL
; p
= p
->next
)
5531 if (p
->type
== bfd_indirect_link_order
)
5532 p
->u
.indirect
.section
->linker_mark
= true;
5535 have_link_order_relocs
= false;
5536 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5538 for (p
= o
->map_head
.link_order
;
5542 if (p
->type
== bfd_indirect_link_order
5543 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
5544 == bfd_target_aout_flavour
))
5548 input_bfd
= p
->u
.indirect
.section
->owner
;
5549 if (! input_bfd
->output_has_begun
)
5551 if (! aout_link_input_bfd (&aout_info
, input_bfd
))
5553 input_bfd
->output_has_begun
= true;
5556 else if (p
->type
== bfd_section_reloc_link_order
5557 || p
->type
== bfd_symbol_reloc_link_order
)
5559 /* These are handled below. */
5560 have_link_order_relocs
= true;
5564 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
5570 /* Write out any symbols that we have not already written out. */
5571 bfd_hash_traverse (&info
->hash
->table
,
5572 aout_link_write_other_symbol
,
5575 /* Now handle any relocs we were asked to create by the linker.
5576 These did not come from any input file. We must do these after
5577 we have written out all the symbols, so that we know the symbol
5579 if (have_link_order_relocs
)
5581 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
5583 for (p
= o
->map_head
.link_order
;
5587 if (p
->type
== bfd_section_reloc_link_order
5588 || p
->type
== bfd_symbol_reloc_link_order
)
5590 if (! aout_link_reloc_link_order (&aout_info
, o
, p
))
5597 free (aout_info
.contents
);
5598 aout_info
.contents
= NULL
;
5599 free (aout_info
.relocs
);
5600 aout_info
.relocs
= NULL
;
5601 free (aout_info
.symbol_map
);
5602 aout_info
.symbol_map
= NULL
;
5603 free (aout_info
.output_syms
);
5604 aout_info
.output_syms
= NULL
;
5606 if (includes_hash_initialized
)
5608 bfd_hash_table_free (&aout_info
.includes
.root
);
5609 includes_hash_initialized
= false;
5612 /* Finish up any dynamic linking we may be doing. */
5613 if (aout_backend_info (abfd
)->finish_dynamic_link
!= NULL
)
5615 if (! (*aout_backend_info (abfd
)->finish_dynamic_link
) (abfd
, info
))
5619 /* Update the header information. */
5620 abfd
->symcount
= obj_aout_external_sym_count (abfd
);
5621 exec_hdr (abfd
)->a_syms
= abfd
->symcount
* EXTERNAL_NLIST_SIZE
;
5622 obj_str_filepos (abfd
) = obj_sym_filepos (abfd
) + exec_hdr (abfd
)->a_syms
;
5623 obj_textsec (abfd
)->reloc_count
=
5624 exec_hdr (abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
);
5625 obj_datasec (abfd
)->reloc_count
=
5626 exec_hdr (abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
);
5628 /* Write out the string table, unless there are no symbols. */
5629 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0)
5631 if (abfd
->symcount
> 0)
5633 if (!emit_stringtab (abfd
, aout_info
.strtab
))
5638 bfd_byte b
[BYTES_IN_WORD
];
5640 memset (b
, 0, BYTES_IN_WORD
);
5641 if (bfd_write (b
, BYTES_IN_WORD
, abfd
) != BYTES_IN_WORD
)
5648 free (aout_info
.contents
);
5649 free (aout_info
.relocs
);
5650 free (aout_info
.symbol_map
);
5651 free (aout_info
.output_syms
);
5652 if (includes_hash_initialized
)
5653 bfd_hash_table_free (&aout_info
.includes
.root
);