1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright (C) 1990-2018 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 @file{newsos3.c} for the Sony NEWS, and @file{demo64.c} for a
43 demonstration of a 64 bit a.out format.
45 The base file @file{aoutx.h} defines general mechanisms for
46 reading and writing records to and from disk and various
47 other methods which BFD requires. It is included by
48 @file{aout32.c} and @file{aout64.c} to form the names
49 <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc.
51 As an example, this is what goes on to make the back end for a
52 sun4, from @file{aout32.c}:
54 | #define ARCH_SIZE 32
60 | aout_32_canonicalize_reloc
61 | aout_32_find_nearest_line
63 | aout_32_get_reloc_upper_bound
68 | #define TARGET_NAME "a.out-sunos-big"
69 | #define VECNAME sparc_aout_sunos_be_vec
72 requires all the names from @file{aout32.c}, and produces the jump vector
74 | sparc_aout_sunos_be_vec
76 The file @file{host-aout.c} is a special case. It is for a large set
77 of hosts that use ``more or less standard'' a.out files, and
78 for which cross-debugging is not interesting. It uses the
79 standard 32-bit a.out support routines, but determines the
80 file offsets and addresses of the text, data, and BSS
81 sections, the machine architecture and machine type, and the
82 entry point address, in a host-dependent manner. Once these
83 values have been determined, generic code is used to handle
86 When porting it to run on a new system, you must supply:
90 | HOST_MACHINE_ARCH (optional)
91 | HOST_MACHINE_MACHINE (optional)
92 | HOST_TEXT_START_ADDR
95 in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These
96 values, plus the structures and macros defined in @file{a.out.h} on
97 your host system, will produce a BFD target that will access
98 ordinary a.out files on your host. To configure a new machine
99 to use @file{host-aout.c}, specify:
101 | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
102 | TDEPFILES= host-aout.o trad-core.o
104 in the @file{config/@var{XXX}.mt} file, and modify @file{configure.ac}
106 @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
107 configuration is selected. */
110 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
111 Doesn't matter what the setting of WP_TEXT is on output, but it'll
113 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
114 * Any BFD with both flags clear is OMAGIC.
115 (Just want to make these explicit, so the conditions tested in this
116 file make sense if you're more familiar with a.out than with BFD.) */
118 #define KEEPIT udata.i
122 #include "safe-ctype.h"
127 #include "aout/aout64.h"
128 #include "aout/stab_gnu.h"
136 The file @file{aoutx.h} provides for both the @emph{standard}
137 and @emph{extended} forms of a.out relocation records.
139 The standard records contain only an address, a symbol index,
140 and a type field. The extended records also have a full
141 integer for an addend. */
143 #ifndef CTOR_TABLE_RELOC_HOWTO
144 #define CTOR_TABLE_RELOC_IDX 2
145 #define CTOR_TABLE_RELOC_HOWTO(BFD) \
146 ((obj_reloc_entry_size (BFD) == RELOC_EXT_SIZE \
147 ? howto_table_ext : howto_table_std) \
148 + CTOR_TABLE_RELOC_IDX)
151 #ifndef MY_swap_std_reloc_in
152 #define MY_swap_std_reloc_in NAME (aout, swap_std_reloc_in)
155 #ifndef MY_swap_ext_reloc_in
156 #define MY_swap_ext_reloc_in NAME (aout, swap_ext_reloc_in)
159 #ifndef MY_swap_std_reloc_out
160 #define MY_swap_std_reloc_out NAME (aout, swap_std_reloc_out)
163 #ifndef MY_swap_ext_reloc_out
164 #define MY_swap_ext_reloc_out NAME (aout, swap_ext_reloc_out)
167 #ifndef MY_final_link_relocate
168 #define MY_final_link_relocate _bfd_final_link_relocate
171 #ifndef MY_relocate_contents
172 #define MY_relocate_contents _bfd_relocate_contents
175 #define howto_table_ext NAME (aout, ext_howto_table)
176 #define howto_table_std NAME (aout, std_howto_table)
178 reloc_howto_type howto_table_ext[] =
180 /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
181 HOWTO (RELOC_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 0, "8", FALSE, 0, 0x000000ff, FALSE),
182 HOWTO (RELOC_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, 0, "16", FALSE, 0, 0x0000ffff, FALSE),
183 HOWTO (RELOC_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "32", FALSE, 0, 0xffffffff, FALSE),
184 HOWTO (RELOC_DISP8, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0, "DISP8", FALSE, 0, 0x000000ff, FALSE),
185 HOWTO (RELOC_DISP16, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0, "DISP16", FALSE, 0, 0x0000ffff, FALSE),
186 HOWTO (RELOC_DISP32, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0, "DISP32", FALSE, 0, 0xffffffff, FALSE),
187 HOWTO (RELOC_WDISP30, 2, 2, 30, TRUE, 0, complain_overflow_signed, 0, "WDISP30", FALSE, 0, 0x3fffffff, FALSE),
188 HOWTO (RELOC_WDISP22, 2, 2, 22, TRUE, 0, complain_overflow_signed, 0, "WDISP22", FALSE, 0, 0x003fffff, FALSE),
189 HOWTO (RELOC_HI22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "HI22", FALSE, 0, 0x003fffff, FALSE),
190 HOWTO (RELOC_22, 0, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "22", FALSE, 0, 0x003fffff, FALSE),
191 HOWTO (RELOC_13, 0, 2, 13, FALSE, 0, complain_overflow_bitfield, 0, "13", FALSE, 0, 0x00001fff, FALSE),
192 HOWTO (RELOC_LO10, 0, 2, 10, FALSE, 0, complain_overflow_dont, 0, "LO10", FALSE, 0, 0x000003ff, FALSE),
193 HOWTO (RELOC_SFA_BASE,0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "SFA_BASE", FALSE, 0, 0xffffffff, FALSE),
194 HOWTO (RELOC_SFA_OFF13,0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0, "SFA_OFF13", FALSE, 0, 0xffffffff, FALSE),
195 HOWTO (RELOC_BASE10, 0, 2, 10, FALSE, 0, complain_overflow_dont, 0, "BASE10", FALSE, 0, 0x000003ff, FALSE),
196 HOWTO (RELOC_BASE13, 0, 2, 13, FALSE, 0, complain_overflow_signed, 0, "BASE13", FALSE, 0, 0x00001fff, FALSE),
197 HOWTO (RELOC_BASE22, 10, 2, 22, FALSE, 0, complain_overflow_bitfield, 0, "BASE22", FALSE, 0, 0x003fffff, FALSE),
198 HOWTO (RELOC_PC10, 0, 2, 10, TRUE, 0, complain_overflow_dont, 0, "PC10", FALSE, 0, 0x000003ff, TRUE),
199 HOWTO (RELOC_PC22, 10, 2, 22, TRUE, 0, complain_overflow_signed, 0, "PC22", FALSE, 0, 0x003fffff, TRUE),
200 HOWTO (RELOC_JMP_TBL, 2, 2, 30, TRUE, 0, complain_overflow_signed, 0, "JMP_TBL", FALSE, 0, 0x3fffffff, FALSE),
201 HOWTO (RELOC_SEGOFF16,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "SEGOFF16", FALSE, 0, 0x00000000, FALSE),
202 HOWTO (RELOC_GLOB_DAT,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "GLOB_DAT", FALSE, 0, 0x00000000, FALSE),
203 HOWTO (RELOC_JMP_SLOT,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "JMP_SLOT", FALSE, 0, 0x00000000, FALSE),
204 HOWTO (RELOC_RELATIVE,0, 2, 0, FALSE, 0, complain_overflow_bitfield, 0, "RELATIVE", FALSE, 0, 0x00000000, FALSE),
205 HOWTO (0, 0, 3, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE",FALSE, 0, 0x00000000, TRUE),
206 HOWTO (0, 0, 3, 0, FALSE, 0, complain_overflow_dont, 0, "R_SPARC_NONE",FALSE, 0, 0x00000000, TRUE),
207 #define RELOC_SPARC_REV32 RELOC_WDISP19
208 HOWTO (RELOC_SPARC_REV32, 0, 2, 32, FALSE, 0, complain_overflow_dont, 0,"R_SPARC_REV32",FALSE, 0, 0xffffffff, FALSE),
211 /* Convert standard reloc records to "arelent" format (incl byte swap). */
213 reloc_howto_type howto_table_std[] =
215 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone. */
216 HOWTO ( 0, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,0,"8", TRUE, 0x000000ff,0x000000ff, FALSE),
217 HOWTO ( 1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
218 HOWTO ( 2, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"32", TRUE, 0xffffffff,0xffffffff, FALSE),
219 HOWTO ( 3, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,0,"64", TRUE, 0xdeaddead,0xdeaddead, FALSE),
220 HOWTO ( 4, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0,"DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
221 HOWTO ( 5, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0,"DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
222 HOWTO ( 6, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0,"DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
223 HOWTO ( 7, 0, 4, 64, TRUE, 0, complain_overflow_signed, 0,"DISP64", TRUE, 0xfeedface,0xfeedface, FALSE),
224 HOWTO ( 8, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"GOT_REL", FALSE, 0,0x00000000, FALSE),
225 HOWTO ( 9, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"BASE16", FALSE,0xffffffff,0xffffffff, FALSE),
226 HOWTO (10, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"BASE32", FALSE,0xffffffff,0xffffffff, FALSE),
232 HOWTO (16, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"JMP_TABLE", FALSE, 0,0x00000000, FALSE),
248 HOWTO (32, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"RELATIVE", FALSE, 0,0x00000000, FALSE),
256 HOWTO (40, 0, 2, 0, FALSE, 0, complain_overflow_bitfield,0,"BASEREL", FALSE, 0,0x00000000, FALSE),
259 #define TABLE_SIZE(TABLE) (sizeof (TABLE) / sizeof (TABLE[0]))
262 NAME (aout, reloc_type_lookup) (bfd *abfd, bfd_reloc_code_real_type code)
264 #define EXT(i, j) case i: return & howto_table_ext [j]
265 #define STD(i, j) case i: return & howto_table_std [j]
266 int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
268 if (code == BFD_RELOC_CTOR)
269 switch (bfd_arch_bits_per_address (abfd))
282 EXT (BFD_RELOC_8, 0);
283 EXT (BFD_RELOC_16, 1);
284 EXT (BFD_RELOC_32, 2);
285 EXT (BFD_RELOC_HI22, 8);
286 EXT (BFD_RELOC_LO10, 11);
287 EXT (BFD_RELOC_32_PCREL_S2, 6);
288 EXT (BFD_RELOC_SPARC_WDISP22, 7);
289 EXT (BFD_RELOC_SPARC13, 10);
290 EXT (BFD_RELOC_SPARC_GOT10, 14);
291 EXT (BFD_RELOC_SPARC_BASE13, 15);
292 EXT (BFD_RELOC_SPARC_GOT13, 15);
293 EXT (BFD_RELOC_SPARC_GOT22, 16);
294 EXT (BFD_RELOC_SPARC_PC10, 17);
295 EXT (BFD_RELOC_SPARC_PC22, 18);
296 EXT (BFD_RELOC_SPARC_WPLT30, 19);
297 EXT (BFD_RELOC_SPARC_REV32, 26);
305 STD (BFD_RELOC_8, 0);
306 STD (BFD_RELOC_16, 1);
307 STD (BFD_RELOC_32, 2);
308 STD (BFD_RELOC_8_PCREL, 4);
309 STD (BFD_RELOC_16_PCREL, 5);
310 STD (BFD_RELOC_32_PCREL, 6);
311 STD (BFD_RELOC_16_BASEREL, 9);
312 STD (BFD_RELOC_32_BASEREL, 10);
319 NAME (aout, reloc_name_lookup) (bfd *abfd, const char *r_name)
321 unsigned int i, size;
322 reloc_howto_type *howto_table;
324 if (obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE)
326 howto_table = howto_table_ext;
327 size = sizeof (howto_table_ext) / sizeof (howto_table_ext[0]);
331 howto_table = howto_table_std;
332 size = sizeof (howto_table_std) / sizeof (howto_table_std[0]);
335 for (i = 0; i < size; i++)
336 if (howto_table[i].name != NULL
337 && strcasecmp (howto_table[i].name, r_name) == 0)
338 return &howto_table[i];
345 Internal entry points
348 @file{aoutx.h} exports several routines for accessing the
349 contents of an a.out file, which are gathered and exported in
350 turn by various format specific files (eg sunos.c).
355 aout_@var{size}_swap_exec_header_in
358 void aout_@var{size}_swap_exec_header_in,
360 struct external_exec *bytes,
361 struct internal_exec *execp);
364 Swap the information in an executable header @var{raw_bytes} taken
365 from a raw byte stream memory image into the internal exec header
366 structure @var{execp}.
369 #ifndef NAME_swap_exec_header_in
371 NAME (aout, swap_exec_header_in) (bfd *abfd,
372 struct external_exec *bytes,
373 struct internal_exec *execp)
375 /* The internal_exec structure has some fields that are unused in this
376 configuration (IE for i960), so ensure that all such uninitialized
377 fields are zero'd out. There are places where two of these structs
378 are memcmp'd, and thus the contents do matter. */
379 memset ((void *) execp, 0, sizeof (struct internal_exec));
380 /* Now fill in fields in the execp, from the bytes in the raw data. */
381 execp->a_info = H_GET_32 (abfd, bytes->e_info);
382 execp->a_text = GET_WORD (abfd, bytes->e_text);
383 execp->a_data = GET_WORD (abfd, bytes->e_data);
384 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
385 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
386 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
387 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
388 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
390 #define NAME_swap_exec_header_in NAME (aout, swap_exec_header_in)
395 aout_@var{size}_swap_exec_header_out
398 void aout_@var{size}_swap_exec_header_out
400 struct internal_exec *execp,
401 struct external_exec *raw_bytes);
404 Swap the information in an internal exec header structure
405 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
408 NAME (aout, swap_exec_header_out) (bfd *abfd,
409 struct internal_exec *execp,
410 struct external_exec *bytes)
412 /* Now fill in fields in the raw data, from the fields in the exec struct. */
413 H_PUT_32 (abfd, execp->a_info , bytes->e_info);
414 PUT_WORD (abfd, execp->a_text , bytes->e_text);
415 PUT_WORD (abfd, execp->a_data , bytes->e_data);
416 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
417 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
418 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
419 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
420 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
423 /* Make all the section for an a.out file. */
426 NAME (aout, make_sections) (bfd *abfd)
428 if (obj_textsec (abfd) == NULL && bfd_make_section (abfd, ".text") == NULL)
430 if (obj_datasec (abfd) == NULL && bfd_make_section (abfd, ".data") == NULL)
432 if (obj_bsssec (abfd) == NULL && bfd_make_section (abfd, ".bss") == NULL)
439 aout_@var{size}_some_aout_object_p
442 const bfd_target *aout_@var{size}_some_aout_object_p
444 struct internal_exec *execp,
445 const bfd_target *(*callback_to_real_object_p) (bfd *));
448 Some a.out variant thinks that the file open in @var{abfd}
449 checking is an a.out file. Do some more checking, and set up
450 for access if it really is. Call back to the calling
451 environment's "finish up" function just before returning, to
452 handle any last-minute setup.
456 NAME (aout, some_aout_object_p) (bfd *abfd,
457 struct internal_exec *execp,
458 const bfd_target *(*callback_to_real_object_p) (bfd *))
460 struct aout_data_struct *rawptr, *oldrawptr;
461 const bfd_target *result;
462 bfd_size_type amt = sizeof (* rawptr);
464 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
468 oldrawptr = abfd->tdata.aout_data;
469 abfd->tdata.aout_data = rawptr;
471 /* Copy the contents of the old tdata struct.
472 In particular, we want the subformat, since for hpux it was set in
473 hp300hpux.c:swap_exec_header_in and will be used in
474 hp300hpux.c:callback. */
475 if (oldrawptr != NULL)
476 *abfd->tdata.aout_data = *oldrawptr;
478 abfd->tdata.aout_data->a.hdr = &rawptr->e;
479 /* Copy in the internal_exec struct. */
480 *(abfd->tdata.aout_data->a.hdr) = *execp;
481 execp = abfd->tdata.aout_data->a.hdr;
483 /* Set the file flags. */
484 abfd->flags = BFD_NO_FLAGS;
485 if (execp->a_drsize || execp->a_trsize)
486 abfd->flags |= HAS_RELOC;
487 /* Setting of EXEC_P has been deferred to the bottom of this function. */
489 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
490 if (N_DYNAMIC (execp))
491 abfd->flags |= DYNAMIC;
493 if (N_MAGIC (execp) == ZMAGIC)
495 abfd->flags |= D_PAGED | WP_TEXT;
496 adata (abfd).magic = z_magic;
498 else if (N_MAGIC (execp) == QMAGIC)
500 abfd->flags |= D_PAGED | WP_TEXT;
501 adata (abfd).magic = z_magic;
502 adata (abfd).subformat = q_magic_format;
504 else if (N_MAGIC (execp) == NMAGIC)
506 abfd->flags |= WP_TEXT;
507 adata (abfd).magic = n_magic;
509 else if (N_MAGIC (execp) == OMAGIC
510 || N_MAGIC (execp) == BMAGIC)
511 adata (abfd).magic = o_magic;
513 /* Should have been checked with N_BADMAG before this routine
517 bfd_get_start_address (abfd) = execp->a_entry;
519 obj_aout_symbols (abfd) = NULL;
520 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
522 /* The default relocation entry size is that of traditional V7 Unix. */
523 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
525 /* The default symbol entry size is that of traditional Unix. */
526 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
529 bfd_init_window (&obj_aout_sym_window (abfd));
530 bfd_init_window (&obj_aout_string_window (abfd));
532 obj_aout_external_syms (abfd) = NULL;
533 obj_aout_external_strings (abfd) = NULL;
534 obj_aout_sym_hashes (abfd) = NULL;
536 if (! NAME (aout, make_sections) (abfd))
539 obj_datasec (abfd)->size = execp->a_data;
540 obj_bsssec (abfd)->size = execp->a_bss;
542 obj_textsec (abfd)->flags =
543 (execp->a_trsize != 0
544 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
545 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
546 obj_datasec (abfd)->flags =
547 (execp->a_drsize != 0
548 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
549 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
550 obj_bsssec (abfd)->flags = SEC_ALLOC;
552 #ifdef THIS_IS_ONLY_DOCUMENTATION
553 /* The common code can't fill in these things because they depend
554 on either the start address of the text segment, the rounding
555 up of virtual addresses between segments, or the starting file
556 position of the text segment -- all of which varies among different
557 versions of a.out. */
559 /* Call back to the format-dependent code to fill in the rest of the
560 fields and do any further cleanup. Things that should be filled
561 in by the callback: */
563 struct exec *execp = exec_hdr (abfd);
565 obj_textsec (abfd)->size = N_TXTSIZE (execp);
566 /* Data and bss are already filled in since they're so standard. */
568 /* The virtual memory addresses of the sections. */
569 obj_textsec (abfd)->vma = N_TXTADDR (execp);
570 obj_datasec (abfd)->vma = N_DATADDR (execp);
571 obj_bsssec (abfd)->vma = N_BSSADDR (execp);
573 /* The file offsets of the sections. */
574 obj_textsec (abfd)->filepos = N_TXTOFF (execp);
575 obj_datasec (abfd)->filepos = N_DATOFF (execp);
577 /* The file offsets of the relocation info. */
578 obj_textsec (abfd)->rel_filepos = N_TRELOFF (execp);
579 obj_datasec (abfd)->rel_filepos = N_DRELOFF (execp);
581 /* The file offsets of the string table and symbol table. */
582 obj_str_filepos (abfd) = N_STROFF (execp);
583 obj_sym_filepos (abfd) = N_SYMOFF (execp);
585 /* Determine the architecture and machine type of the object file. */
586 switch (N_MACHTYPE (exec_hdr (abfd)))
589 abfd->obj_arch = bfd_arch_obscure;
593 adata (abfd)->page_size = TARGET_PAGE_SIZE;
594 adata (abfd)->segment_size = SEGMENT_SIZE;
595 adata (abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
599 /* The architecture is encoded in various ways in various a.out variants,
600 or is not encoded at all in some of them. The relocation size depends
601 on the architecture and the a.out variant. Finally, the return value
602 is the bfd_target vector in use. If an error occurs, return zero and
603 set bfd_error to the appropriate error code.
605 Formats such as b.out, which have additional fields in the a.out
606 header, should cope with them in this callback as well. */
607 #endif /* DOCUMENTATION */
609 result = (*callback_to_real_object_p) (abfd);
611 /* Now that the segment addresses have been worked out, take a better
612 guess at whether the file is executable. If the entry point
613 is within the text segment, assume it is. (This makes files
614 executable even if their entry point address is 0, as long as
615 their text starts at zero.).
617 This test had to be changed to deal with systems where the text segment
618 runs at a different location than the default. The problem is that the
619 entry address can appear to be outside the text segment, thus causing an
620 erroneous conclusion that the file isn't executable.
622 To fix this, we now accept any non-zero entry point as an indication of
623 executability. This will work most of the time, since only the linker
624 sets the entry point, and that is likely to be non-zero for most systems. */
626 if (execp->a_entry != 0
627 || (execp->a_entry >= obj_textsec (abfd)->vma
628 && execp->a_entry < (obj_textsec (abfd)->vma
629 + obj_textsec (abfd)->size)
630 && execp->a_trsize == 0
631 && execp->a_drsize == 0))
632 abfd->flags |= EXEC_P;
636 struct stat stat_buf;
638 /* The original heuristic doesn't work in some important cases.
639 The a.out file has no information about the text start
640 address. For files (like kernels) linked to non-standard
641 addresses (ld -Ttext nnn) the entry point may not be between
642 the default text start (obj_textsec(abfd)->vma) and
643 (obj_textsec(abfd)->vma) + text size. This is not just a mach
644 issue. Many kernels are loaded at non standard addresses. */
645 if (abfd->iostream != NULL
646 && (abfd->flags & BFD_IN_MEMORY) == 0
647 && (fstat (fileno ((FILE *) (abfd->iostream)), &stat_buf) == 0)
648 && ((stat_buf.st_mode & 0111) != 0))
649 abfd->flags |= EXEC_P;
651 #endif /* STAT_FOR_EXEC */
657 bfd_release (abfd, rawptr);
658 abfd->tdata.aout_data = oldrawptr;
664 aout_@var{size}_mkobject
667 bfd_boolean aout_@var{size}_mkobject, (bfd *abfd);
670 Initialize BFD @var{abfd} for use with a.out files.
674 NAME (aout, mkobject) (bfd *abfd)
676 struct aout_data_struct *rawptr;
677 bfd_size_type amt = sizeof (* rawptr);
679 bfd_set_error (bfd_error_system_call);
681 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
685 abfd->tdata.aout_data = rawptr;
686 exec_hdr (abfd) = &(rawptr->e);
688 obj_textsec (abfd) = NULL;
689 obj_datasec (abfd) = NULL;
690 obj_bsssec (abfd) = NULL;
697 aout_@var{size}_machine_type
700 enum machine_type aout_@var{size}_machine_type
701 (enum bfd_architecture arch,
702 unsigned long machine,
703 bfd_boolean *unknown);
706 Keep track of machine architecture and machine type for
707 a.out's. Return the <<machine_type>> for a particular
708 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
709 and machine can't be represented in a.out format.
711 If the architecture is understood, machine type 0 (default)
712 is always understood.
716 NAME (aout, machine_type) (enum bfd_architecture arch,
717 unsigned long machine,
718 bfd_boolean *unknown)
720 enum machine_type arch_flags;
722 arch_flags = M_UNKNOWN;
729 || machine == bfd_mach_sparc
730 || machine == bfd_mach_sparc_sparclite
731 || machine == bfd_mach_sparc_sparclite_le
732 || machine == bfd_mach_sparc_v8plus
733 || machine == bfd_mach_sparc_v8plusa
734 || machine == bfd_mach_sparc_v8plusb
735 || machine == bfd_mach_sparc_v8plusc
736 || machine == bfd_mach_sparc_v8plusd
737 || machine == bfd_mach_sparc_v8pluse
738 || machine == bfd_mach_sparc_v8plusv
739 || machine == bfd_mach_sparc_v8plusm
740 || machine == bfd_mach_sparc_v8plusm8
741 || machine == bfd_mach_sparc_v9
742 || machine == bfd_mach_sparc_v9a
743 || machine == bfd_mach_sparc_v9b
744 || machine == bfd_mach_sparc_v9c
745 || machine == bfd_mach_sparc_v9d
746 || machine == bfd_mach_sparc_v9e
747 || machine == bfd_mach_sparc_v9v
748 || machine == bfd_mach_sparc_v9m
749 || machine == bfd_mach_sparc_v9m8)
750 arch_flags = M_SPARC;
751 else if (machine == bfd_mach_sparc_sparclet)
752 arch_flags = M_SPARCLET;
758 case 0: arch_flags = M_68010; break;
759 case bfd_mach_m68000: arch_flags = M_UNKNOWN; *unknown = FALSE; break;
760 case bfd_mach_m68010: arch_flags = M_68010; break;
761 case bfd_mach_m68020: arch_flags = M_68020; break;
762 default: arch_flags = M_UNKNOWN; break;
768 || machine == bfd_mach_i386_i386
769 || machine == bfd_mach_i386_i386_intel_syntax)
782 case bfd_mach_mips3000:
783 case bfd_mach_mips3900:
784 arch_flags = M_MIPS1;
786 case bfd_mach_mips6000:
787 arch_flags = M_MIPS2;
789 case bfd_mach_mips4000:
790 case bfd_mach_mips4010:
791 case bfd_mach_mips4100:
792 case bfd_mach_mips4300:
793 case bfd_mach_mips4400:
794 case bfd_mach_mips4600:
795 case bfd_mach_mips4650:
796 case bfd_mach_mips8000:
797 case bfd_mach_mips9000:
798 case bfd_mach_mips10000:
799 case bfd_mach_mips12000:
800 case bfd_mach_mips14000:
801 case bfd_mach_mips16000:
802 case bfd_mach_mips16:
803 case bfd_mach_mipsisa32:
804 case bfd_mach_mipsisa32r2:
805 case bfd_mach_mipsisa32r3:
806 case bfd_mach_mipsisa32r5:
807 case bfd_mach_mipsisa32r6:
809 case bfd_mach_mipsisa64:
810 case bfd_mach_mipsisa64r2:
811 case bfd_mach_mipsisa64r3:
812 case bfd_mach_mipsisa64r5:
813 case bfd_mach_mipsisa64r6:
814 case bfd_mach_mips_sb1:
815 case bfd_mach_mips_xlr:
816 /* FIXME: These should be MIPS3, MIPS4, MIPS16, MIPS32, etc. */
817 arch_flags = M_MIPS2;
820 arch_flags = M_UNKNOWN;
828 case 0: arch_flags = M_NS32532; break;
829 case 32032: arch_flags = M_NS32032; break;
830 case 32532: arch_flags = M_NS32532; break;
831 default: arch_flags = M_UNKNOWN; break;
840 if (machine == 0 || machine == 255)
849 arch_flags = M_UNKNOWN;
852 if (arch_flags != M_UNKNOWN)
860 aout_@var{size}_set_arch_mach
863 bfd_boolean aout_@var{size}_set_arch_mach,
865 enum bfd_architecture arch,
866 unsigned long machine);
869 Set the architecture and the machine of the BFD @var{abfd} to the
870 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
871 can support the architecture required.
875 NAME (aout, set_arch_mach) (bfd *abfd,
876 enum bfd_architecture arch,
877 unsigned long machine)
879 if (! bfd_default_set_arch_mach (abfd, arch, machine))
882 if (arch != bfd_arch_unknown)
886 NAME (aout, machine_type) (arch, machine, &unknown);
891 /* Determine the size of a relocation entry. */
896 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
899 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
903 return (*aout_backend_info (abfd)->set_sizes) (abfd);
907 adjust_o_magic (bfd *abfd, struct internal_exec *execp)
909 file_ptr pos = adata (abfd).exec_bytes_size;
914 obj_textsec (abfd)->filepos = pos;
915 if (!obj_textsec (abfd)->user_set_vma)
916 obj_textsec (abfd)->vma = vma;
918 vma = obj_textsec (abfd)->vma;
920 pos += obj_textsec (abfd)->size;
921 vma += obj_textsec (abfd)->size;
924 if (!obj_datasec (abfd)->user_set_vma)
926 obj_textsec (abfd)->size += pad;
929 obj_datasec (abfd)->vma = vma;
932 vma = obj_datasec (abfd)->vma;
933 obj_datasec (abfd)->filepos = pos;
934 pos += obj_datasec (abfd)->size;
935 vma += obj_datasec (abfd)->size;
938 if (!obj_bsssec (abfd)->user_set_vma)
940 obj_datasec (abfd)->size += pad;
943 obj_bsssec (abfd)->vma = vma;
947 /* The VMA of the .bss section is set by the VMA of the
948 .data section plus the size of the .data section. We may
949 need to add padding bytes to make this true. */
950 pad = obj_bsssec (abfd)->vma - vma;
953 obj_datasec (abfd)->size += pad;
957 obj_bsssec (abfd)->filepos = pos;
959 /* Fix up the exec header. */
960 execp->a_text = obj_textsec (abfd)->size;
961 execp->a_data = obj_datasec (abfd)->size;
962 execp->a_bss = obj_bsssec (abfd)->size;
963 N_SET_MAGIC (execp, OMAGIC);
967 adjust_z_magic (bfd *abfd, struct internal_exec *execp)
969 bfd_size_type data_pad, text_pad;
971 const struct aout_backend_data *abdp;
972 /* TRUE if text includes exec header. */
975 abdp = aout_backend_info (abfd);
979 && (abdp->text_includes_header
980 || obj_aout_subformat (abfd) == q_magic_format));
981 obj_textsec (abfd)->filepos = (ztih
982 ? adata (abfd).exec_bytes_size
983 : adata (abfd).zmagic_disk_block_size);
984 if (! obj_textsec (abfd)->user_set_vma)
986 /* ?? Do we really need to check for relocs here? */
987 obj_textsec (abfd)->vma = ((abfd->flags & HAS_RELOC)
990 ? (abdp->default_text_vma
991 + adata (abfd).exec_bytes_size)
992 : abdp->default_text_vma));
997 /* The .text section is being loaded at an unusual address. We
998 may need to pad it such that the .data section starts at a page
1001 text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
1002 & (adata (abfd).page_size - 1));
1004 text_pad = ((- obj_textsec (abfd)->vma)
1005 & (adata (abfd).page_size - 1));
1008 /* Find start of data. */
1011 text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->size;
1012 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1016 /* Note that if page_size == zmagic_disk_block_size, then
1017 filepos == page_size, and this case is the same as the ztih
1019 text_end = obj_textsec (abfd)->size;
1020 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1021 text_end += obj_textsec (abfd)->filepos;
1023 obj_textsec (abfd)->size += text_pad;
1024 text_end += text_pad;
1027 if (!obj_datasec (abfd)->user_set_vma)
1030 vma = obj_textsec (abfd)->vma + obj_textsec (abfd)->size;
1031 obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1033 if (abdp && abdp->zmagic_mapped_contiguous)
1035 asection * text = obj_textsec (abfd);
1036 asection * data = obj_datasec (abfd);
1038 text_pad = data->vma - (text->vma + text->size);
1039 /* Only pad the text section if the data
1040 section is going to be placed after it. */
1042 text->size += text_pad;
1044 obj_datasec (abfd)->filepos = (obj_textsec (abfd)->filepos
1045 + obj_textsec (abfd)->size);
1047 /* Fix up exec header while we're at it. */
1048 execp->a_text = obj_textsec (abfd)->size;
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_MAGIC (execp, QMAGIC);
1054 N_SET_MAGIC (execp, ZMAGIC);
1056 /* Spec says data section should be rounded up to page boundary. */
1057 obj_datasec (abfd)->size
1058 = align_power (obj_datasec (abfd)->size,
1059 obj_bsssec (abfd)->alignment_power);
1060 execp->a_data = BFD_ALIGN (obj_datasec (abfd)->size,
1061 adata (abfd).page_size);
1062 data_pad = execp->a_data - obj_datasec (abfd)->size;
1065 if (!obj_bsssec (abfd)->user_set_vma)
1066 obj_bsssec (abfd)->vma = (obj_datasec (abfd)->vma
1067 + obj_datasec (abfd)->size);
1068 /* If the BSS immediately follows the data section and extra space
1069 in the page is left after the data section, fudge data
1070 in the header so that the bss section looks smaller by that
1071 amount. We'll start the bss section there, and lie to the OS.
1072 (Note that a linker script, as well as the above assignment,
1073 could have explicitly set the BSS vma to immediately follow
1074 the data section.) */
1075 if (align_power (obj_bsssec (abfd)->vma, obj_bsssec (abfd)->alignment_power)
1076 == obj_datasec (abfd)->vma + obj_datasec (abfd)->size)
1077 execp->a_bss = (data_pad > obj_bsssec (abfd)->size
1078 ? 0 : obj_bsssec (abfd)->size - data_pad);
1080 execp->a_bss = obj_bsssec (abfd)->size;
1084 adjust_n_magic (bfd *abfd, struct internal_exec *execp)
1086 file_ptr pos = adata (abfd).exec_bytes_size;
1091 obj_textsec (abfd)->filepos = pos;
1092 if (!obj_textsec (abfd)->user_set_vma)
1093 obj_textsec (abfd)->vma = vma;
1095 vma = obj_textsec (abfd)->vma;
1096 pos += obj_textsec (abfd)->size;
1097 vma += obj_textsec (abfd)->size;
1100 obj_datasec (abfd)->filepos = pos;
1101 if (!obj_datasec (abfd)->user_set_vma)
1102 obj_datasec (abfd)->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1103 vma = obj_datasec (abfd)->vma;
1105 /* Since BSS follows data immediately, see if it needs alignment. */
1106 vma += obj_datasec (abfd)->size;
1107 pad = align_power (vma, obj_bsssec (abfd)->alignment_power) - vma;
1108 obj_datasec (abfd)->size += pad;
1109 pos += obj_datasec (abfd)->size;
1112 if (!obj_bsssec (abfd)->user_set_vma)
1113 obj_bsssec (abfd)->vma = vma;
1115 vma = obj_bsssec (abfd)->vma;
1117 /* Fix up exec header. */
1118 execp->a_text = obj_textsec (abfd)->size;
1119 execp->a_data = obj_datasec (abfd)->size;
1120 execp->a_bss = obj_bsssec (abfd)->size;
1121 N_SET_MAGIC (execp, NMAGIC);
1125 NAME (aout, adjust_sizes_and_vmas) (bfd *abfd)
1127 struct internal_exec *execp = exec_hdr (abfd);
1129 if (! NAME (aout, make_sections) (abfd))
1132 if (adata (abfd).magic != undecided_magic)
1135 obj_textsec (abfd)->size =
1136 align_power (obj_textsec (abfd)->size,
1137 obj_textsec (abfd)->alignment_power);
1139 /* Rule (heuristic) for when to pad to a new page. Note that there
1140 are (at least) two ways demand-paged (ZMAGIC) files have been
1141 handled. Most Berkeley-based systems start the text segment at
1142 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1143 segment right after the exec header; the latter is counted in the
1144 text segment size, and is paged in by the kernel with the rest of
1147 /* This perhaps isn't the right way to do this, but made it simpler for me
1148 to understand enough to implement it. Better would probably be to go
1149 right from BFD flags to alignment/positioning characteristics. But the
1150 old code was sloppy enough about handling the flags, and had enough
1151 other magic, that it was a little hard for me to understand. I think
1152 I understand it better now, but I haven't time to do the cleanup this
1155 if (abfd->flags & D_PAGED)
1156 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
1157 adata (abfd).magic = z_magic;
1158 else if (abfd->flags & WP_TEXT)
1159 adata (abfd).magic = n_magic;
1161 adata (abfd).magic = o_magic;
1163 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1165 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1167 switch (adata (abfd).magic)
1169 case n_magic: str = "NMAGIC"; break;
1170 case o_magic: str = "OMAGIC"; break;
1171 case z_magic: str = "ZMAGIC"; break;
1176 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1177 obj_textsec (abfd)->alignment_power,
1178 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1179 obj_datasec (abfd)->alignment_power,
1180 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size,
1181 obj_bsssec (abfd)->alignment_power);
1185 switch (adata (abfd).magic)
1188 adjust_o_magic (abfd, execp);
1191 adjust_z_magic (abfd, execp);
1194 adjust_n_magic (abfd, execp);
1200 #ifdef BFD_AOUT_DEBUG
1201 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1202 obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1203 obj_textsec (abfd)->filepos,
1204 obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1205 obj_datasec (abfd)->filepos,
1206 obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size);
1214 aout_@var{size}_new_section_hook
1217 bfd_boolean aout_@var{size}_new_section_hook,
1222 Called by the BFD in response to a @code{bfd_make_section}
1226 NAME (aout, new_section_hook) (bfd *abfd, asection *newsect)
1228 /* Align to double at least. */
1229 newsect->alignment_power = bfd_get_arch_info (abfd)->section_align_power;
1231 if (bfd_get_format (abfd) == bfd_object)
1233 if (obj_textsec (abfd) == NULL && !strcmp (newsect->name, ".text"))
1235 obj_textsec (abfd)= newsect;
1236 newsect->target_index = N_TEXT;
1238 else if (obj_datasec (abfd) == NULL && !strcmp (newsect->name, ".data"))
1240 obj_datasec (abfd) = newsect;
1241 newsect->target_index = N_DATA;
1243 else if (obj_bsssec (abfd) == NULL && !strcmp (newsect->name, ".bss"))
1245 obj_bsssec (abfd) = newsect;
1246 newsect->target_index = N_BSS;
1250 /* We allow more than three sections internally. */
1251 return _bfd_generic_new_section_hook (abfd, newsect);
1255 NAME (aout, set_section_contents) (bfd *abfd,
1257 const void * location,
1259 bfd_size_type count)
1261 if (! abfd->output_has_begun)
1263 if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
1267 if (section == obj_bsssec (abfd))
1269 bfd_set_error (bfd_error_no_contents);
1273 if (section != obj_textsec (abfd)
1274 && section != obj_datasec (abfd))
1276 if (aout_section_merge_with_text_p (abfd, section))
1277 section->filepos = obj_textsec (abfd)->filepos +
1278 (section->vma - obj_textsec (abfd)->vma);
1282 /* xgettext:c-format */
1283 (_("%pB: can not represent section `%pA' in a.out object file format"),
1285 bfd_set_error (bfd_error_nonrepresentable_section);
1292 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1293 || bfd_bwrite (location, count, abfd) != count)
1300 /* Read the external symbols from an a.out file. */
1303 aout_get_external_symbols (bfd *abfd)
1305 if (obj_aout_external_syms (abfd) == NULL)
1307 bfd_size_type count;
1308 struct external_nlist *syms;
1309 bfd_size_type amt = exec_hdr (abfd)->a_syms;
1311 count = amt / EXTERNAL_NLIST_SIZE;
1313 return TRUE; /* Nothing to do. */
1316 if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd), amt,
1317 &obj_aout_sym_window (abfd), TRUE))
1319 syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
1321 /* We allocate using malloc to make the values easy to free
1322 later on. If we put them on the objalloc it might not be
1323 possible to free them. */
1324 syms = (struct external_nlist *) bfd_malloc (amt);
1328 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1329 || bfd_bread (syms, amt, abfd) != amt)
1336 obj_aout_external_syms (abfd) = syms;
1337 obj_aout_external_sym_count (abfd) = count;
1340 if (obj_aout_external_strings (abfd) == NULL
1341 && exec_hdr (abfd)->a_syms != 0)
1343 unsigned char string_chars[BYTES_IN_WORD];
1344 bfd_size_type stringsize;
1346 bfd_size_type amt = BYTES_IN_WORD;
1348 /* Get the size of the strings. */
1349 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1350 || bfd_bread ((void *) string_chars, amt, abfd) != amt)
1352 stringsize = GET_WORD (abfd, string_chars);
1353 if (stringsize == 0)
1355 else if (stringsize < BYTES_IN_WORD
1356 || (size_t) stringsize != stringsize)
1358 bfd_set_error (bfd_error_bad_value);
1363 if (stringsize >= BYTES_IN_WORD)
1365 if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize,
1366 &obj_aout_string_window (abfd), TRUE))
1368 strings = (char *) obj_aout_string_window (abfd).data;
1373 strings = (char *) bfd_malloc (stringsize);
1374 if (strings == NULL)
1377 if (stringsize >= BYTES_IN_WORD)
1379 /* Keep the string count in the buffer for convenience
1380 when indexing with e_strx. */
1381 amt = stringsize - BYTES_IN_WORD;
1382 if (bfd_bread (strings + BYTES_IN_WORD, amt, abfd) != amt)
1389 /* Ensure that a zero index yields an empty string. */
1392 strings[stringsize - 1] = 0;
1394 obj_aout_external_strings (abfd) = strings;
1395 obj_aout_external_string_size (abfd) = stringsize;
1401 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1402 and symbol->value fields of CACHE_PTR will be set from the a.out
1403 nlist structure. This function is responsible for setting
1404 symbol->flags and symbol->section, and adjusting symbol->value. */
1407 translate_from_native_sym_flags (bfd *abfd, aout_symbol_type *cache_ptr)
1411 if ((cache_ptr->type & N_STAB) != 0
1412 || cache_ptr->type == N_FN)
1416 /* This is a debugging symbol. */
1417 cache_ptr->symbol.flags = BSF_DEBUGGING;
1419 /* Work out the symbol section. */
1420 switch (cache_ptr->type & N_TYPE)
1424 sec = obj_textsec (abfd);
1427 sec = obj_datasec (abfd);
1430 sec = obj_bsssec (abfd);
1434 sec = bfd_abs_section_ptr;
1438 cache_ptr->symbol.section = sec;
1439 cache_ptr->symbol.value -= sec->vma;
1444 /* Get the default visibility. This does not apply to all types, so
1445 we just hold it in a local variable to use if wanted. */
1446 if ((cache_ptr->type & N_EXT) == 0)
1447 visible = BSF_LOCAL;
1449 visible = BSF_GLOBAL;
1451 switch (cache_ptr->type)
1454 case N_ABS: case N_ABS | N_EXT:
1455 cache_ptr->symbol.section = bfd_abs_section_ptr;
1456 cache_ptr->symbol.flags = visible;
1459 case N_UNDF | N_EXT:
1460 if (cache_ptr->symbol.value != 0)
1462 /* This is a common symbol. */
1463 cache_ptr->symbol.flags = BSF_GLOBAL;
1464 cache_ptr->symbol.section = bfd_com_section_ptr;
1468 cache_ptr->symbol.flags = 0;
1469 cache_ptr->symbol.section = bfd_und_section_ptr;
1473 case N_TEXT: case N_TEXT | N_EXT:
1474 cache_ptr->symbol.section = obj_textsec (abfd);
1475 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1476 cache_ptr->symbol.flags = visible;
1479 /* N_SETV symbols used to represent set vectors placed in the
1480 data section. They are no longer generated. Theoretically,
1481 it was possible to extract the entries and combine them with
1482 new ones, although I don't know if that was ever actually
1483 done. Unless that feature is restored, treat them as data
1485 case N_SETV: case N_SETV | N_EXT:
1486 case N_DATA: case N_DATA | N_EXT:
1487 cache_ptr->symbol.section = obj_datasec (abfd);
1488 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1489 cache_ptr->symbol.flags = visible;
1492 case N_BSS: case N_BSS | N_EXT:
1493 cache_ptr->symbol.section = obj_bsssec (abfd);
1494 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1495 cache_ptr->symbol.flags = visible;
1498 case N_SETA: case N_SETA | N_EXT:
1499 case N_SETT: case N_SETT | N_EXT:
1500 case N_SETD: case N_SETD | N_EXT:
1501 case N_SETB: case N_SETB | N_EXT:
1503 /* This code is no longer needed. It used to be used to make
1504 the linker handle set symbols, but they are now handled in
1505 the add_symbols routine instead. */
1506 switch (cache_ptr->type & N_TYPE)
1509 cache_ptr->symbol.section = bfd_abs_section_ptr;
1512 cache_ptr->symbol.section = obj_textsec (abfd);
1515 cache_ptr->symbol.section = obj_datasec (abfd);
1518 cache_ptr->symbol.section = obj_bsssec (abfd);
1522 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
1527 /* This symbol is the text of a warning message. The next
1528 symbol is the symbol to associate the warning with. If a
1529 reference is made to that symbol, a warning is issued. */
1530 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1531 cache_ptr->symbol.section = bfd_abs_section_ptr;
1534 case N_INDR: case N_INDR | N_EXT:
1535 /* An indirect symbol. This consists of two symbols in a row.
1536 The first symbol is the name of the indirection. The second
1537 symbol is the name of the target. A reference to the first
1538 symbol becomes a reference to the second. */
1539 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT | visible;
1540 cache_ptr->symbol.section = bfd_ind_section_ptr;
1544 cache_ptr->symbol.section = bfd_und_section_ptr;
1545 cache_ptr->symbol.flags = BSF_WEAK;
1549 cache_ptr->symbol.section = bfd_abs_section_ptr;
1550 cache_ptr->symbol.flags = BSF_WEAK;
1554 cache_ptr->symbol.section = obj_textsec (abfd);
1555 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1556 cache_ptr->symbol.flags = BSF_WEAK;
1560 cache_ptr->symbol.section = obj_datasec (abfd);
1561 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1562 cache_ptr->symbol.flags = BSF_WEAK;
1566 cache_ptr->symbol.section = obj_bsssec (abfd);
1567 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1568 cache_ptr->symbol.flags = BSF_WEAK;
1575 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1578 translate_to_native_sym_flags (bfd *abfd,
1580 struct external_nlist *sym_pointer)
1582 bfd_vma value = cache_ptr->value;
1586 /* Mask out any existing type bits in case copying from one section
1588 sym_pointer->e_type[0] &= ~N_TYPE;
1590 sec = bfd_get_section (cache_ptr);
1595 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1598 /* xgettext:c-format */
1599 (_("%pB: can not represent section for symbol `%s' in a.out "
1600 "object file format"),
1602 cache_ptr->name != NULL ? cache_ptr->name : _("*unknown*"));
1603 bfd_set_error (bfd_error_nonrepresentable_section);
1607 if (sec->output_section != NULL)
1609 off = sec->output_offset;
1610 sec = sec->output_section;
1613 if (bfd_is_abs_section (sec))
1614 sym_pointer->e_type[0] |= N_ABS;
1615 else if (sec == obj_textsec (abfd))
1616 sym_pointer->e_type[0] |= N_TEXT;
1617 else if (sec == obj_datasec (abfd))
1618 sym_pointer->e_type[0] |= N_DATA;
1619 else if (sec == obj_bsssec (abfd))
1620 sym_pointer->e_type[0] |= N_BSS;
1621 else if (bfd_is_und_section (sec))
1622 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1623 else if (bfd_is_ind_section (sec))
1624 sym_pointer->e_type[0] = N_INDR;
1625 else if (bfd_is_com_section (sec))
1626 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1629 if (aout_section_merge_with_text_p (abfd, sec))
1630 sym_pointer->e_type[0] |= N_TEXT;
1634 /* xgettext:c-format */
1635 (_("%pB: can not represent section `%pA' in a.out object file format"),
1637 bfd_set_error (bfd_error_nonrepresentable_section);
1642 /* Turn the symbol from section relative to absolute again. */
1643 value += sec->vma + off;
1645 if ((cache_ptr->flags & BSF_WARNING) != 0)
1646 sym_pointer->e_type[0] = N_WARNING;
1648 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1649 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1650 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1651 sym_pointer->e_type[0] |= N_EXT;
1652 else if ((cache_ptr->flags & BSF_LOCAL) != 0)
1653 sym_pointer->e_type[0] &= ~N_EXT;
1655 if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
1657 int type = ((aout_symbol_type *) cache_ptr)->type;
1661 case N_ABS: type = N_SETA; break;
1662 case N_TEXT: type = N_SETT; break;
1663 case N_DATA: type = N_SETD; break;
1664 case N_BSS: type = N_SETB; break;
1666 sym_pointer->e_type[0] = type;
1669 if ((cache_ptr->flags & BSF_WEAK) != 0)
1673 switch (sym_pointer->e_type[0] & N_TYPE)
1676 case N_ABS: type = N_WEAKA; break;
1677 case N_TEXT: type = N_WEAKT; break;
1678 case N_DATA: type = N_WEAKD; break;
1679 case N_BSS: type = N_WEAKB; break;
1680 case N_UNDF: type = N_WEAKU; break;
1682 sym_pointer->e_type[0] = type;
1685 PUT_WORD (abfd, value, sym_pointer->e_value);
1690 /* Native-level interface to symbols. */
1693 NAME (aout, make_empty_symbol) (bfd *abfd)
1695 bfd_size_type amt = sizeof (aout_symbol_type);
1697 aout_symbol_type *new_symbol = (aout_symbol_type *) bfd_zalloc (abfd, amt);
1700 new_symbol->symbol.the_bfd = abfd;
1702 return &new_symbol->symbol;
1705 /* Translate a set of internal symbols into external symbols. */
1708 NAME (aout, translate_symbol_table) (bfd *abfd,
1709 aout_symbol_type *in,
1710 struct external_nlist *ext,
1711 bfd_size_type count,
1713 bfd_size_type strsize,
1714 bfd_boolean dynamic)
1716 struct external_nlist *ext_end;
1718 ext_end = ext + count;
1719 for (; ext < ext_end; ext++, in++)
1723 x = GET_WORD (abfd, ext->e_strx);
1724 in->symbol.the_bfd = abfd;
1726 /* For the normal symbols, the zero index points at the number
1727 of bytes in the string table but is to be interpreted as the
1728 null string. For the dynamic symbols, the number of bytes in
1729 the string table is stored in the __DYNAMIC structure and the
1730 zero index points at an actual string. */
1731 if (x == 0 && ! dynamic)
1732 in->symbol.name = "";
1733 else if (x < strsize)
1734 in->symbol.name = str + x;
1738 (_("%pB: invalid string offset %" PRIu64 " >= %" PRIu64),
1739 abfd, (uint64_t) x, (uint64_t) strsize);
1740 bfd_set_error (bfd_error_bad_value);
1744 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1745 in->desc = H_GET_16 (abfd, ext->e_desc);
1746 in->other = H_GET_8 (abfd, ext->e_other);
1747 in->type = H_GET_8 (abfd, ext->e_type);
1748 in->symbol.udata.p = NULL;
1750 if (! translate_from_native_sym_flags (abfd, in))
1754 in->symbol.flags |= BSF_DYNAMIC;
1760 /* We read the symbols into a buffer, which is discarded when this
1761 function exits. We read the strings into a buffer large enough to
1762 hold them all plus all the cached symbol entries. */
1765 NAME (aout, slurp_symbol_table) (bfd *abfd)
1767 struct external_nlist *old_external_syms;
1768 aout_symbol_type *cached;
1769 bfd_size_type cached_size;
1771 /* If there's no work to be done, don't do any. */
1772 if (obj_aout_symbols (abfd) != NULL)
1775 old_external_syms = obj_aout_external_syms (abfd);
1777 if (! aout_get_external_symbols (abfd))
1780 cached_size = obj_aout_external_sym_count (abfd);
1781 if (cached_size == 0)
1782 return TRUE; /* Nothing to do. */
1784 cached_size *= sizeof (aout_symbol_type);
1785 cached = (aout_symbol_type *) bfd_zmalloc (cached_size);
1789 /* Convert from external symbol information to internal. */
1790 if (! (NAME (aout, translate_symbol_table)
1792 obj_aout_external_syms (abfd),
1793 obj_aout_external_sym_count (abfd),
1794 obj_aout_external_strings (abfd),
1795 obj_aout_external_string_size (abfd),
1802 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1804 obj_aout_symbols (abfd) = cached;
1806 /* It is very likely that anybody who calls this function will not
1807 want the external symbol information, so if it was allocated
1808 because of our call to aout_get_external_symbols, we free it up
1809 right away to save space. */
1810 if (old_external_syms == NULL
1811 && obj_aout_external_syms (abfd) != NULL)
1814 bfd_free_window (&obj_aout_sym_window (abfd));
1816 free (obj_aout_external_syms (abfd));
1818 obj_aout_external_syms (abfd) = NULL;
1824 /* We use a hash table when writing out symbols so that we only write
1825 out a particular string once. This helps particularly when the
1826 linker writes out stabs debugging entries, because each different
1827 contributing object file tends to have many duplicate stabs
1830 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1831 if BFD_TRADITIONAL_FORMAT is set. */
1833 /* Get the index of a string in a strtab, adding it if it is not
1836 static inline bfd_size_type
1837 add_to_stringtab (bfd *abfd,
1838 struct bfd_strtab_hash *tab,
1843 bfd_size_type str_index;
1845 /* An index of 0 always means the empty string. */
1846 if (str == 0 || *str == '\0')
1849 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1850 doesn't understand a hashed string table. */
1852 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1855 str_index = _bfd_stringtab_add (tab, str, hash, copy);
1857 if (str_index != (bfd_size_type) -1)
1858 /* Add BYTES_IN_WORD to the return value to account for the
1859 space taken up by the string table size. */
1860 str_index += BYTES_IN_WORD;
1865 /* Write out a strtab. ABFD is already at the right location in the
1869 emit_stringtab (bfd *abfd, struct bfd_strtab_hash *tab)
1871 bfd_byte buffer[BYTES_IN_WORD];
1872 bfd_size_type amt = BYTES_IN_WORD;
1874 /* The string table starts with the size. */
1875 PUT_WORD (abfd, _bfd_stringtab_size (tab) + BYTES_IN_WORD, buffer);
1876 if (bfd_bwrite ((void *) buffer, amt, abfd) != amt)
1879 return _bfd_stringtab_emit (abfd, tab);
1883 NAME (aout, write_syms) (bfd *abfd)
1885 unsigned int count ;
1886 asymbol **generic = bfd_get_outsymbols (abfd);
1887 struct bfd_strtab_hash *strtab;
1889 strtab = _bfd_stringtab_init ();
1893 for (count = 0; count < bfd_get_symcount (abfd); count++)
1895 asymbol *g = generic[count];
1897 struct external_nlist nsp;
1900 indx = add_to_stringtab (abfd, strtab, g->name, FALSE);
1901 if (indx == (bfd_size_type) -1)
1903 PUT_WORD (abfd, indx, (bfd_byte *) nsp.e_strx);
1905 if (bfd_asymbol_flavour (g) == abfd->xvec->flavour)
1907 H_PUT_16 (abfd, aout_symbol (g)->desc, nsp.e_desc);
1908 H_PUT_8 (abfd, aout_symbol (g)->other, nsp.e_other);
1909 H_PUT_8 (abfd, aout_symbol (g)->type, nsp.e_type);
1913 H_PUT_16 (abfd, 0, nsp.e_desc);
1914 H_PUT_8 (abfd, 0, nsp.e_other);
1915 H_PUT_8 (abfd, 0, nsp.e_type);
1918 if (! translate_to_native_sym_flags (abfd, g, &nsp))
1921 amt = EXTERNAL_NLIST_SIZE;
1922 if (bfd_bwrite ((void *) &nsp, amt, abfd) != amt)
1925 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1926 here, at the end. */
1930 if (! emit_stringtab (abfd, strtab))
1933 _bfd_stringtab_free (strtab);
1938 _bfd_stringtab_free (strtab);
1943 NAME (aout, canonicalize_symtab) (bfd *abfd, asymbol **location)
1945 unsigned int counter = 0;
1946 aout_symbol_type *symbase;
1948 if (!NAME (aout, slurp_symbol_table) (abfd))
1951 for (symbase = obj_aout_symbols (abfd);
1952 counter++ < bfd_get_symcount (abfd);
1954 *(location++) = (asymbol *) (symbase++);
1956 return bfd_get_symcount (abfd);
1959 /* Standard reloc stuff. */
1960 /* Output standard relocation information to a file in target byte order. */
1962 extern void NAME (aout, swap_std_reloc_out)
1963 (bfd *, arelent *, struct reloc_std_external *);
1966 NAME (aout, swap_std_reloc_out) (bfd *abfd,
1968 struct reloc_std_external *natptr)
1971 asymbol *sym = *(g->sym_ptr_ptr);
1973 unsigned int r_length;
1975 int r_baserel, r_jmptable, r_relative;
1976 asection *output_section = sym->section->output_section;
1978 PUT_WORD (abfd, g->address, natptr->r_address);
1980 BFD_ASSERT (g->howto != NULL);
1981 r_length = g->howto->size ; /* Size as a power of two. */
1982 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
1983 /* XXX This relies on relocs coming from a.out files. */
1984 r_baserel = (g->howto->type & 8) != 0;
1985 r_jmptable = (g->howto->type & 16) != 0;
1986 r_relative = (g->howto->type & 32) != 0;
1988 /* Name was clobbered by aout_write_syms to be symbol index. */
1990 /* If this relocation is relative to a symbol then set the
1991 r_index to the symbols index, and the r_extern bit.
1993 Absolute symbols can come in in two ways, either as an offset
1994 from the abs section, or as a symbol which has an abs value.
1995 check for that here. */
1997 if (bfd_is_com_section (output_section)
1998 || bfd_is_abs_section (output_section)
1999 || bfd_is_und_section (output_section)
2000 /* PR gas/3041 a.out relocs against weak symbols
2001 must be treated as if they were against externs. */
2002 || (sym->flags & BSF_WEAK))
2004 if (bfd_abs_section_ptr->symbol == sym)
2006 /* Whoops, looked like an abs symbol, but is
2007 really an offset from the abs section. */
2013 /* Fill in symbol. */
2015 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2020 /* Just an ordinary section. */
2022 r_index = output_section->target_index;
2025 /* Now the fun stuff. */
2026 if (bfd_header_big_endian (abfd))
2028 natptr->r_index[0] = r_index >> 16;
2029 natptr->r_index[1] = r_index >> 8;
2030 natptr->r_index[2] = r_index;
2031 natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
2032 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
2033 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
2034 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
2035 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
2036 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
2040 natptr->r_index[2] = r_index >> 16;
2041 natptr->r_index[1] = r_index >> 8;
2042 natptr->r_index[0] = r_index;
2043 natptr->r_type[0] = ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
2044 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
2045 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
2046 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
2047 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
2048 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
2052 /* Extended stuff. */
2053 /* Output extended relocation information to a file in target byte order. */
2055 extern void NAME (aout, swap_ext_reloc_out)
2056 (bfd *, arelent *, struct reloc_ext_external *);
2059 NAME (aout, swap_ext_reloc_out) (bfd *abfd,
2061 struct reloc_ext_external *natptr)
2065 unsigned int r_type;
2067 asymbol *sym = *(g->sym_ptr_ptr);
2068 asection *output_section = sym->section->output_section;
2070 PUT_WORD (abfd, g->address, natptr->r_address);
2072 r_type = (unsigned int) g->howto->type;
2074 r_addend = g->addend;
2075 if ((sym->flags & BSF_SECTION_SYM) != 0)
2076 r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
2078 /* If this relocation is relative to a symbol then set the
2079 r_index to the symbols index, and the r_extern bit.
2081 Absolute symbols can come in in two ways, either as an offset
2082 from the abs section, or as a symbol which has an abs value.
2083 check for that here. */
2084 if (bfd_is_abs_section (bfd_get_section (sym)))
2089 else if ((sym->flags & BSF_SECTION_SYM) == 0)
2091 if (bfd_is_und_section (bfd_get_section (sym))
2092 || (sym->flags & BSF_GLOBAL) != 0)
2096 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2100 /* Just an ordinary section. */
2102 r_index = output_section->target_index;
2105 /* Now the fun stuff. */
2106 if (bfd_header_big_endian (abfd))
2108 natptr->r_index[0] = r_index >> 16;
2109 natptr->r_index[1] = r_index >> 8;
2110 natptr->r_index[2] = r_index;
2111 natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
2112 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG));
2116 natptr->r_index[2] = r_index >> 16;
2117 natptr->r_index[1] = r_index >> 8;
2118 natptr->r_index[0] = r_index;
2119 natptr->r_type[0] = ((r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
2120 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE));
2123 PUT_WORD (abfd, r_addend, natptr->r_addend);
2126 /* BFD deals internally with all things based from the section they're
2127 in. so, something in 10 bytes into a text section with a base of
2128 50 would have a symbol (.text+10) and know .text vma was 50.
2130 Aout keeps all it's symbols based from zero, so the symbol would
2131 contain 60. This macro subs the base of each section from the value
2132 to give the true offset from the section. */
2134 #define MOVE_ADDRESS(ad) \
2137 /* Undefined symbol. */ \
2138 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2139 cache_ptr->addend = ad; \
2143 /* Defined, section relative. Replace symbol with pointer to \
2144 symbol which points to section. */ \
2148 case N_TEXT | N_EXT: \
2149 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
2150 cache_ptr->addend = ad - su->textsec->vma; \
2153 case N_DATA | N_EXT: \
2154 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
2155 cache_ptr->addend = ad - su->datasec->vma; \
2158 case N_BSS | N_EXT: \
2159 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
2160 cache_ptr->addend = ad - su->bsssec->vma; \
2164 case N_ABS | N_EXT: \
2165 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2166 cache_ptr->addend = ad; \
2172 NAME (aout, swap_ext_reloc_in) (bfd *abfd,
2173 struct reloc_ext_external *bytes,
2176 bfd_size_type symcount)
2178 unsigned int r_index;
2180 unsigned int r_type;
2181 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2183 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2185 /* Now the fun stuff. */
2186 if (bfd_header_big_endian (abfd))
2188 r_index = (((unsigned int) bytes->r_index[0] << 16)
2189 | ((unsigned int) bytes->r_index[1] << 8)
2190 | bytes->r_index[2]);
2191 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2192 r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2193 >> RELOC_EXT_BITS_TYPE_SH_BIG);
2197 r_index = (((unsigned int) bytes->r_index[2] << 16)
2198 | ((unsigned int) bytes->r_index[1] << 8)
2199 | bytes->r_index[0]);
2200 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2201 r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2202 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
2205 if (r_type < TABLE_SIZE (howto_table_ext))
2206 cache_ptr->howto = howto_table_ext + r_type;
2208 cache_ptr->howto = NULL;
2210 /* Base relative relocs are always against the symbol table,
2211 regardless of the setting of r_extern. r_extern just reflects
2212 whether the symbol the reloc is against is local or global. */
2213 if (r_type == (unsigned int) RELOC_BASE10
2214 || r_type == (unsigned int) RELOC_BASE13
2215 || r_type == (unsigned int) RELOC_BASE22)
2218 if (r_extern && r_index > symcount)
2220 /* We could arrange to return an error, but it might be useful
2221 to see the file even if it is bad. */
2226 MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
2230 NAME (aout, swap_std_reloc_in) (bfd *abfd,
2231 struct reloc_std_external *bytes,
2234 bfd_size_type symcount)
2236 unsigned int r_index;
2238 unsigned int r_length;
2240 int r_baserel, r_jmptable, r_relative;
2241 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2242 unsigned int howto_idx;
2244 cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
2246 /* Now the fun stuff. */
2247 if (bfd_header_big_endian (abfd))
2249 r_index = (((unsigned int) bytes->r_index[0] << 16)
2250 | ((unsigned int) bytes->r_index[1] << 8)
2251 | bytes->r_index[2]);
2252 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2253 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2254 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2255 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2256 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2257 r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2258 >> RELOC_STD_BITS_LENGTH_SH_BIG);
2262 r_index = (((unsigned int) bytes->r_index[2] << 16)
2263 | ((unsigned int) bytes->r_index[1] << 8)
2264 | bytes->r_index[0]);
2265 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2266 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2267 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2268 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2269 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2270 r_length = ((bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2271 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
2274 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
2275 + 16 * r_jmptable + 32 * r_relative);
2276 if (howto_idx < TABLE_SIZE (howto_table_std))
2278 cache_ptr->howto = howto_table_std + howto_idx;
2279 if (cache_ptr->howto->type == (unsigned int) -1)
2280 cache_ptr->howto = NULL;
2283 cache_ptr->howto = NULL;
2285 /* Base relative relocs are always against the symbol table,
2286 regardless of the setting of r_extern. r_extern just reflects
2287 whether the symbol the reloc is against is local or global. */
2291 if (r_extern && r_index >= symcount)
2293 /* We could arrange to return an error, but it might be useful
2294 to see the file even if it is bad. FIXME: Of course this
2295 means that objdump -r *doesn't* see the actual reloc, and
2296 objcopy silently writes a different reloc. */
2304 /* Read and swap the relocs for a section. */
2307 NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
2309 bfd_size_type count;
2310 bfd_size_type reloc_size;
2312 arelent *reloc_cache;
2314 unsigned int counter = 0;
2318 if (asect->relocation)
2321 if (asect->flags & SEC_CONSTRUCTOR)
2324 if (asect == obj_datasec (abfd))
2325 reloc_size = exec_hdr (abfd)->a_drsize;
2326 else if (asect == obj_textsec (abfd))
2327 reloc_size = exec_hdr (abfd)->a_trsize;
2328 else if (asect == obj_bsssec (abfd))
2332 bfd_set_error (bfd_error_invalid_operation);
2336 if (reloc_size == 0)
2337 return TRUE; /* Nothing to be done. */
2339 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2342 each_size = obj_reloc_entry_size (abfd);
2344 count = reloc_size / each_size;
2346 return TRUE; /* Nothing to be done. */
2348 amt = count * sizeof (arelent);
2349 reloc_cache = (arelent *) bfd_zmalloc (amt);
2350 if (reloc_cache == NULL)
2353 relocs = bfd_malloc (reloc_size);
2360 if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
2367 cache_ptr = reloc_cache;
2368 if (each_size == RELOC_EXT_SIZE)
2370 struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;
2372 for (; counter < count; counter++, rptr++, cache_ptr++)
2373 MY_swap_ext_reloc_in (abfd, rptr, cache_ptr, symbols,
2374 (bfd_size_type) bfd_get_symcount (abfd));
2378 struct reloc_std_external *rptr = (struct reloc_std_external *) relocs;
2380 for (; counter < count; counter++, rptr++, cache_ptr++)
2381 MY_swap_std_reloc_in (abfd, rptr, cache_ptr, symbols,
2382 (bfd_size_type) bfd_get_symcount (abfd));
2387 asect->relocation = reloc_cache;
2388 asect->reloc_count = cache_ptr - reloc_cache;
2393 /* Write out a relocation section into an object file. */
2396 NAME (aout, squirt_out_relocs) (bfd *abfd, asection *section)
2399 unsigned char *native, *natptr;
2402 unsigned int count = section->reloc_count;
2403 bfd_size_type natsize;
2405 if (count == 0 || section->orelocation == NULL)
2408 each_size = obj_reloc_entry_size (abfd);
2409 natsize = (bfd_size_type) each_size * count;
2410 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2414 generic = section->orelocation;
2416 if (each_size == RELOC_EXT_SIZE)
2418 for (natptr = native;
2420 --count, natptr += each_size, ++generic)
2422 /* PR 20921: If the howto field has not been initialised then skip
2424 PR 20929: Similarly for the symbol field. */
2425 if ((*generic)->howto == NULL
2426 || (*generic)->sym_ptr_ptr == NULL)
2428 bfd_set_error (bfd_error_invalid_operation);
2429 _bfd_error_handler (_("\
2430 %pB: attempt to write out unknown reloc type"), abfd);
2433 MY_swap_ext_reloc_out (abfd, *generic,
2434 (struct reloc_ext_external *) natptr);
2439 for (natptr = native;
2441 --count, natptr += each_size, ++generic)
2443 if ((*generic)->howto == NULL
2444 || (*generic)->sym_ptr_ptr == NULL)
2446 bfd_set_error (bfd_error_invalid_operation);
2447 _bfd_error_handler (_("\
2448 %pB: attempt to write out unknown reloc type"), abfd);
2451 MY_swap_std_reloc_out (abfd, *generic,
2452 (struct reloc_std_external *) natptr);
2456 if (bfd_bwrite ((void *) native, natsize, abfd) != natsize)
2458 bfd_release (abfd, native);
2461 bfd_release (abfd, native);
2466 /* This is stupid. This function should be a boolean predicate. */
2469 NAME (aout, canonicalize_reloc) (bfd *abfd,
2474 arelent *tblptr = section->relocation;
2477 if (section == obj_bsssec (abfd))
2483 if (!(tblptr || NAME (aout, slurp_reloc_table) (abfd, section, symbols)))
2486 if (section->flags & SEC_CONSTRUCTOR)
2488 arelent_chain *chain = section->constructor_chain;
2489 for (count = 0; count < section->reloc_count; count ++)
2491 *relptr ++ = &chain->relent;
2492 chain = chain->next;
2497 tblptr = section->relocation;
2499 for (count = 0; count++ < section->reloc_count; )
2501 *relptr++ = tblptr++;
2506 return section->reloc_count;
2510 NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
2512 if (bfd_get_format (abfd) != bfd_object)
2514 bfd_set_error (bfd_error_invalid_operation);
2518 if (asect->flags & SEC_CONSTRUCTOR)
2519 return sizeof (arelent *) * (asect->reloc_count + 1);
2521 if (asect == obj_datasec (abfd))
2522 return sizeof (arelent *)
2523 * ((exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd))
2526 if (asect == obj_textsec (abfd))
2527 return sizeof (arelent *)
2528 * ((exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd))
2531 if (asect == obj_bsssec (abfd))
2532 return sizeof (arelent *);
2534 if (asect == obj_bsssec (abfd))
2537 bfd_set_error (bfd_error_invalid_operation);
2542 NAME (aout, get_symtab_upper_bound) (bfd *abfd)
2544 if (!NAME (aout, slurp_symbol_table) (abfd))
2547 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2551 NAME (aout, get_lineno) (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2552 asymbol *ignore_symbol ATTRIBUTE_UNUSED)
2558 NAME (aout, get_symbol_info) (bfd *ignore_abfd ATTRIBUTE_UNUSED,
2562 bfd_symbol_info (symbol, ret);
2564 if (ret->type == '?')
2566 int type_code = aout_symbol (symbol)->type & 0xff;
2567 const char *stab_name = bfd_get_stab_name (type_code);
2568 static char buf[10];
2570 if (stab_name == NULL)
2572 sprintf (buf, "(%d)", type_code);
2576 ret->stab_type = type_code;
2577 ret->stab_other = (unsigned) (aout_symbol (symbol)->other & 0xff);
2578 ret->stab_desc = (unsigned) (aout_symbol (symbol)->desc & 0xffff);
2579 ret->stab_name = stab_name;
2584 NAME (aout, print_symbol) (bfd *abfd,
2587 bfd_print_symbol_type how)
2589 FILE *file = (FILE *)afile;
2593 case bfd_print_symbol_name:
2595 fprintf (file,"%s", symbol->name);
2597 case bfd_print_symbol_more:
2598 fprintf (file,"%4x %2x %2x",
2599 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2600 (unsigned) (aout_symbol (symbol)->other & 0xff),
2601 (unsigned) (aout_symbol (symbol)->type));
2603 case bfd_print_symbol_all:
2605 const char *section_name = symbol->section->name;
2607 bfd_print_symbol_vandf (abfd, (void *)file, symbol);
2609 fprintf (file," %-5s %04x %02x %02x",
2611 (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2612 (unsigned) (aout_symbol (symbol)->other & 0xff),
2613 (unsigned) (aout_symbol (symbol)->type & 0xff));
2615 fprintf (file," %s", symbol->name);
2621 /* If we don't have to allocate more than 1MB to hold the generic
2622 symbols, we use the generic minisymbol methord: it's faster, since
2623 it only translates the symbols once, not multiple times. */
2624 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2626 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2627 symbols. The minisymbol_to_symbol function translates these into
2628 BFD asymbol structures. */
2631 NAME (aout, read_minisymbols) (bfd *abfd,
2632 bfd_boolean dynamic,
2634 unsigned int *sizep)
2637 /* We could handle the dynamic symbols here as well, but it's
2638 easier to hand them off. */
2639 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2641 if (! aout_get_external_symbols (abfd))
2644 if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2645 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2647 *minisymsp = (void *) obj_aout_external_syms (abfd);
2649 /* By passing the external symbols back from this routine, we are
2650 giving up control over the memory block. Clear
2651 obj_aout_external_syms, so that we do not try to free it
2653 obj_aout_external_syms (abfd) = NULL;
2655 *sizep = EXTERNAL_NLIST_SIZE;
2656 return obj_aout_external_sym_count (abfd);
2659 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2660 unmodified a.out symbol. The SYM argument is a structure returned
2661 by bfd_make_empty_symbol, which we fill in here. */
2664 NAME (aout, minisymbol_to_symbol) (bfd *abfd,
2665 bfd_boolean dynamic,
2666 const void * minisym,
2670 || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2671 return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2673 memset (sym, 0, sizeof (aout_symbol_type));
2675 /* We call translate_symbol_table to translate a single symbol. */
2676 if (! (NAME (aout, translate_symbol_table)
2678 (aout_symbol_type *) sym,
2679 (struct external_nlist *) minisym,
2681 obj_aout_external_strings (abfd),
2682 obj_aout_external_string_size (abfd),
2689 /* Provided a BFD, a section and an offset into the section, calculate
2690 and return the name of the source file and the line nearest to the
2694 NAME (aout, find_nearest_line) (bfd *abfd,
2698 const char **filename_ptr,
2699 const char **functionname_ptr,
2700 unsigned int *line_ptr,
2701 unsigned int *disriminator_ptr)
2703 /* Run down the file looking for the filename, function and linenumber. */
2705 const char *directory_name = NULL;
2706 const char *main_file_name = NULL;
2707 const char *current_file_name = NULL;
2708 const char *line_file_name = NULL; /* Value of current_file_name at line number. */
2709 const char *line_directory_name = NULL; /* Value of directory_name at line number. */
2710 bfd_vma low_line_vma = 0;
2711 bfd_vma low_func_vma = 0;
2713 bfd_size_type filelen, funclen;
2716 *filename_ptr = abfd->filename;
2717 *functionname_ptr = NULL;
2719 if (disriminator_ptr)
2720 *disriminator_ptr = 0;
2722 if (symbols != NULL)
2724 for (p = symbols; *p; p++)
2726 aout_symbol_type *q = (aout_symbol_type *) (*p);
2731 /* If this looks like a file name symbol, and it comes after
2732 the line number we have found so far, but before the
2733 offset, then we have probably not found the right line
2735 if (q->symbol.value <= offset
2736 && ((q->symbol.value > low_line_vma
2737 && (line_file_name != NULL
2739 || (q->symbol.value > low_func_vma
2742 const char *symname;
2744 symname = q->symbol.name;
2745 if (strcmp (symname + strlen (symname) - 2, ".o") == 0)
2747 if (q->symbol.value > low_line_vma)
2750 line_file_name = NULL;
2752 if (q->symbol.value > low_func_vma)
2759 /* If this symbol is less than the offset, but greater than
2760 the line number we have found so far, then we have not
2761 found the right line number. */
2762 if (q->symbol.value <= offset)
2764 if (q->symbol.value > low_line_vma)
2767 line_file_name = NULL;
2769 if (q->symbol.value > low_func_vma)
2773 main_file_name = current_file_name = q->symbol.name;
2774 /* Look ahead to next symbol to check if that too is an N_SO. */
2778 q = (aout_symbol_type *) (*p);
2779 if (q->type != (int)N_SO)
2782 /* Found a second N_SO First is directory; second is filename. */
2783 directory_name = current_file_name;
2784 main_file_name = current_file_name = q->symbol.name;
2785 if (obj_textsec (abfd) != section)
2789 current_file_name = q->symbol.name;
2796 /* We'll keep this if it resolves nearer than the one we have
2798 if (q->symbol.value >= low_line_vma
2799 && q->symbol.value <= offset)
2801 *line_ptr = q->desc;
2802 low_line_vma = q->symbol.value;
2803 line_file_name = current_file_name;
2804 line_directory_name = directory_name;
2809 /* We'll keep this if it is nearer than the one we have already. */
2810 if (q->symbol.value >= low_func_vma &&
2811 q->symbol.value <= offset)
2813 low_func_vma = q->symbol.value;
2814 func = (asymbol *)q;
2816 else if (q->symbol.value > offset)
2827 main_file_name = line_file_name;
2828 directory_name = line_directory_name;
2831 if (main_file_name == NULL
2832 || IS_ABSOLUTE_PATH (main_file_name)
2833 || directory_name == NULL)
2836 filelen = strlen (directory_name) + strlen (main_file_name);
2841 funclen = strlen (bfd_asymbol_name (func));
2843 if (adata (abfd).line_buf != NULL)
2844 free (adata (abfd).line_buf);
2846 if (filelen + funclen == 0)
2847 adata (abfd).line_buf = buf = NULL;
2850 buf = (char *) bfd_malloc (filelen + funclen + 3);
2851 adata (abfd).line_buf = buf;
2856 if (main_file_name != NULL)
2858 if (IS_ABSOLUTE_PATH (main_file_name) || directory_name == NULL)
2859 *filename_ptr = main_file_name;
2863 /* PR binutils/20891: In a corrupt input file both
2864 main_file_name and directory_name can be empty... */
2865 * filename_ptr = NULL;
2868 snprintf (buf, filelen + 1, "%s%s", directory_name,
2870 *filename_ptr = buf;
2878 const char *function = func->name;
2883 /* PR binutils/20892: In a corrupt input file func can be empty. */
2884 * functionname_ptr = NULL;
2887 /* The caller expects a symbol name. We actually have a
2888 function name, without the leading underscore. Put the
2889 underscore back in, so that the caller gets a symbol name. */
2890 if (bfd_get_symbol_leading_char (abfd) == '\0')
2891 strcpy (buf, function);
2894 buf[0] = bfd_get_symbol_leading_char (abfd);
2895 strcpy (buf + 1, function);
2897 /* Have to remove : stuff. */
2898 colon = strchr (buf, ':');
2901 *functionname_ptr = buf;
2908 NAME (aout, sizeof_headers) (bfd *abfd,
2909 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2911 return adata (abfd).exec_bytes_size;
2914 /* Free all information we have cached for this BFD. We can always
2915 read it again later if we need it. */
2918 NAME (aout, bfd_free_cached_info) (bfd *abfd)
2922 if (bfd_get_format (abfd) != bfd_object
2923 || abfd->tdata.aout_data == NULL)
2926 #define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
2927 BFCI_FREE (obj_aout_symbols (abfd));
2929 obj_aout_external_syms (abfd) = 0;
2930 bfd_free_window (&obj_aout_sym_window (abfd));
2931 bfd_free_window (&obj_aout_string_window (abfd));
2932 obj_aout_external_strings (abfd) = 0;
2934 BFCI_FREE (obj_aout_external_syms (abfd));
2935 BFCI_FREE (obj_aout_external_strings (abfd));
2937 for (o = abfd->sections; o != NULL; o = o->next)
2938 BFCI_FREE (o->relocation);
2944 /* a.out link code. */
2946 /* Routine to create an entry in an a.out link hash table. */
2948 struct bfd_hash_entry *
2949 NAME (aout, link_hash_newfunc) (struct bfd_hash_entry *entry,
2950 struct bfd_hash_table *table,
2953 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2955 /* Allocate the structure if it has not already been allocated by a
2958 ret = (struct aout_link_hash_entry *) bfd_hash_allocate (table,
2963 /* Call the allocation method of the superclass. */
2964 ret = ((struct aout_link_hash_entry *)
2965 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2969 /* Set local fields. */
2970 ret->written = FALSE;
2974 return (struct bfd_hash_entry *) ret;
2977 /* Initialize an a.out link hash table. */
2980 NAME (aout, link_hash_table_init) (struct aout_link_hash_table *table,
2982 struct bfd_hash_entry *(*newfunc)
2983 (struct bfd_hash_entry *, struct bfd_hash_table *,
2985 unsigned int entsize)
2987 return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
2990 /* Create an a.out link hash table. */
2992 struct bfd_link_hash_table *
2993 NAME (aout, link_hash_table_create) (bfd *abfd)
2995 struct aout_link_hash_table *ret;
2996 bfd_size_type amt = sizeof (* ret);
2998 ret = (struct aout_link_hash_table *) bfd_malloc (amt);
3002 if (!NAME (aout, link_hash_table_init) (ret, abfd,
3003 NAME (aout, link_hash_newfunc),
3004 sizeof (struct aout_link_hash_entry)))
3012 /* Add all symbols from an object file to the hash table. */
3015 aout_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
3017 bfd_boolean (*add_one_symbol)
3018 (struct bfd_link_info *, bfd *, const char *, flagword, asection *,
3019 bfd_vma, const char *, bfd_boolean, bfd_boolean,
3020 struct bfd_link_hash_entry **);
3021 struct external_nlist *syms;
3022 bfd_size_type sym_count;
3025 struct aout_link_hash_entry **sym_hash;
3026 struct external_nlist *p;
3027 struct external_nlist *pend;
3030 syms = obj_aout_external_syms (abfd);
3031 sym_count = obj_aout_external_sym_count (abfd);
3032 strings = obj_aout_external_strings (abfd);
3033 if (info->keep_memory)
3038 if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
3040 if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
3041 (abfd, info, &syms, &sym_count, &strings)))
3046 return TRUE; /* Nothing to do. */
3048 /* We keep a list of the linker hash table entries that correspond
3049 to particular symbols. We could just look them up in the hash
3050 table, but keeping the list is more efficient. Perhaps this
3051 should be conditional on info->keep_memory. */
3052 amt = sym_count * sizeof (struct aout_link_hash_entry *);
3053 sym_hash = (struct aout_link_hash_entry **) bfd_alloc (abfd, amt);
3054 if (sym_hash == NULL)
3056 obj_aout_sym_hashes (abfd) = sym_hash;
3058 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
3059 if (add_one_symbol == NULL)
3060 add_one_symbol = _bfd_generic_link_add_one_symbol;
3063 pend = p + sym_count;
3064 for (; p < pend; p++, sym_hash++)
3075 type = H_GET_8 (abfd, p->e_type);
3077 /* Ignore debugging symbols. */
3078 if ((type & N_STAB) != 0)
3081 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3082 if (GET_WORD (abfd, p->e_strx) >= obj_aout_external_string_size (abfd))
3084 name = strings + GET_WORD (abfd, p->e_strx);
3085 value = GET_WORD (abfd, p->e_value);
3102 /* Ignore symbols that are not externally visible. */
3105 /* Ignore local indirect symbol. */
3110 case N_UNDF | N_EXT:
3113 section = bfd_und_section_ptr;
3117 section = bfd_com_section_ptr;
3120 section = bfd_abs_section_ptr;
3122 case N_TEXT | N_EXT:
3123 section = obj_textsec (abfd);
3124 value -= bfd_get_section_vma (abfd, section);
3126 case N_DATA | N_EXT:
3127 case N_SETV | N_EXT:
3128 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3129 translate_from_native_sym_flags. */
3130 section = obj_datasec (abfd);
3131 value -= bfd_get_section_vma (abfd, section);
3134 section = obj_bsssec (abfd);
3135 value -= bfd_get_section_vma (abfd, section);
3137 case N_INDR | N_EXT:
3138 /* An indirect symbol. The next symbol is the symbol
3139 which this one really is. */
3140 /* See PR 20925 for a reproducer. */
3144 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3145 if (GET_WORD (abfd, p->e_strx) >= obj_aout_external_string_size (abfd))
3147 string = strings + GET_WORD (abfd, p->e_strx);
3148 section = bfd_ind_section_ptr;
3149 flags |= BSF_INDIRECT;
3151 case N_COMM | N_EXT:
3152 section = bfd_com_section_ptr;
3154 case N_SETA: case N_SETA | N_EXT:
3155 section = bfd_abs_section_ptr;
3156 flags |= BSF_CONSTRUCTOR;
3158 case N_SETT: case N_SETT | N_EXT:
3159 section = obj_textsec (abfd);
3160 flags |= BSF_CONSTRUCTOR;
3161 value -= bfd_get_section_vma (abfd, section);
3163 case N_SETD: case N_SETD | N_EXT:
3164 section = obj_datasec (abfd);
3165 flags |= BSF_CONSTRUCTOR;
3166 value -= bfd_get_section_vma (abfd, section);
3168 case N_SETB: case N_SETB | N_EXT:
3169 section = obj_bsssec (abfd);
3170 flags |= BSF_CONSTRUCTOR;
3171 value -= bfd_get_section_vma (abfd, section);
3174 /* A warning symbol. The next symbol is the one to warn
3175 about. If there is no next symbol, just look away. */
3180 /* PR 19629: Corrupt binaries can contain illegal string offsets. */
3181 if (GET_WORD (abfd, p->e_strx) >= obj_aout_external_string_size (abfd))
3183 name = strings + GET_WORD (abfd, p->e_strx);
3184 section = bfd_und_section_ptr;
3185 flags |= BSF_WARNING;
3188 section = bfd_und_section_ptr;
3192 section = bfd_abs_section_ptr;
3196 section = obj_textsec (abfd);
3197 value -= bfd_get_section_vma (abfd, section);
3201 section = obj_datasec (abfd);
3202 value -= bfd_get_section_vma (abfd, section);
3206 section = obj_bsssec (abfd);
3207 value -= bfd_get_section_vma (abfd, section);
3212 if (! ((*add_one_symbol)
3213 (info, abfd, name, flags, section, value, string, copy, FALSE,
3214 (struct bfd_link_hash_entry **) sym_hash)))
3217 /* Restrict the maximum alignment of a common symbol based on
3218 the architecture, since a.out has no way to represent
3219 alignment requirements of a section in a .o file. FIXME:
3220 This isn't quite right: it should use the architecture of the
3221 output file, not the input files. */
3222 if ((*sym_hash)->root.type == bfd_link_hash_common
3223 && ((*sym_hash)->root.u.c.p->alignment_power >
3224 bfd_get_arch_info (abfd)->section_align_power))
3225 (*sym_hash)->root.u.c.p->alignment_power =
3226 bfd_get_arch_info (abfd)->section_align_power;
3228 /* If this is a set symbol, and we are not building sets, then
3229 it is possible for the hash entry to not have been set. In
3230 such a case, treat the symbol as not globally defined. */
3231 if ((*sym_hash)->root.type == bfd_link_hash_new)
3233 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
3237 if (type == (N_INDR | N_EXT) || type == N_WARNING)
3244 /* Free up the internal symbols read from an a.out file. */
3247 aout_link_free_symbols (bfd *abfd)
3249 if (obj_aout_external_syms (abfd) != NULL)
3252 bfd_free_window (&obj_aout_sym_window (abfd));
3254 free ((void *) obj_aout_external_syms (abfd));
3256 obj_aout_external_syms (abfd) = NULL;
3258 if (obj_aout_external_strings (abfd) != NULL)
3261 bfd_free_window (&obj_aout_string_window (abfd));
3263 free ((void *) obj_aout_external_strings (abfd));
3265 obj_aout_external_strings (abfd) = NULL;
3270 /* Add symbols from an a.out object file. */
3273 aout_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3275 if (! aout_get_external_symbols (abfd))
3277 if (! aout_link_add_symbols (abfd, info))
3279 if (! info->keep_memory)
3281 if (! aout_link_free_symbols (abfd))
3287 /* Look through the internal symbols to see if this object file should
3288 be included in the link. We should include this object file if it
3289 defines any symbols which are currently undefined. If this object
3290 file defines a common symbol, then we may adjust the size of the
3291 known symbol but we do not include the object file in the link
3292 (unless there is some other reason to include it). */
3295 aout_link_check_ar_symbols (bfd *abfd,
3296 struct bfd_link_info *info,
3297 bfd_boolean *pneeded,
3300 struct external_nlist *p;
3301 struct external_nlist *pend;
3306 /* Look through all the symbols. */
3307 p = obj_aout_external_syms (abfd);
3308 pend = p + obj_aout_external_sym_count (abfd);
3309 strings = obj_aout_external_strings (abfd);
3310 for (; p < pend; p++)
3312 int type = H_GET_8 (abfd, p->e_type);
3314 struct bfd_link_hash_entry *h;
3316 /* Ignore symbols that are not externally visible. This is an
3317 optimization only, as we check the type more thoroughly
3319 if (((type & N_EXT) == 0
3320 || (type & N_STAB) != 0
3327 if (type == N_WARNING
3333 name = strings + GET_WORD (abfd, p->e_strx);
3334 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
3336 /* We are only interested in symbols that are currently
3337 undefined or common. */
3339 || (h->type != bfd_link_hash_undefined
3340 && h->type != bfd_link_hash_common))
3342 if (type == (N_INDR | N_EXT))
3347 if (type == (N_TEXT | N_EXT)
3348 || type == (N_DATA | N_EXT)
3349 || type == (N_BSS | N_EXT)
3350 || type == (N_ABS | N_EXT)
3351 || type == (N_INDR | N_EXT))
3353 /* This object file defines this symbol. We must link it
3354 in. This is true regardless of whether the current
3355 definition of the symbol is undefined or common.
3357 If the current definition is common, we have a case in
3358 which we have already seen an object file including:
3360 and this object file from the archive includes:
3362 In such a case, whether to include this object is target
3363 dependant for backward compatibility.
3365 FIXME: The SunOS 4.1.3 linker will pull in the archive
3366 element if the symbol is defined in the .data section,
3367 but not if it is defined in the .text section. That
3368 seems a bit crazy to me, and it has not been implemented
3369 yet. However, it might be correct. */
3370 if (h->type == bfd_link_hash_common)
3374 switch (info->common_skip_ar_symbols)
3376 case bfd_link_common_skip_none:
3378 case bfd_link_common_skip_text:
3379 skip = (type == (N_TEXT | N_EXT));
3381 case bfd_link_common_skip_data:
3382 skip = (type == (N_DATA | N_EXT));
3384 case bfd_link_common_skip_all:
3393 if (!(*info->callbacks
3394 ->add_archive_element) (info, abfd, name, subsbfd))
3400 if (type == (N_UNDF | N_EXT))
3404 value = GET_WORD (abfd, p->e_value);
3407 /* This symbol is common in the object from the archive
3409 if (h->type == bfd_link_hash_undefined)
3414 symbfd = h->u.undef.abfd;
3417 /* This symbol was created as undefined from
3418 outside BFD. We assume that we should link
3419 in the object file. This is done for the -u
3420 option in the linker. */
3421 if (!(*info->callbacks
3422 ->add_archive_element) (info, abfd, name, subsbfd))
3427 /* Turn the current link symbol into a common
3428 symbol. It is already on the undefs list. */
3429 h->type = bfd_link_hash_common;
3430 h->u.c.p = (struct bfd_link_hash_common_entry *)
3431 bfd_hash_allocate (&info->hash->table,
3432 sizeof (struct bfd_link_hash_common_entry));
3433 if (h->u.c.p == NULL)
3436 h->u.c.size = value;
3438 /* FIXME: This isn't quite right. The maximum
3439 alignment of a common symbol should be set by the
3440 architecture of the output file, not of the input
3442 power = bfd_log2 (value);
3443 if (power > bfd_get_arch_info (abfd)->section_align_power)
3444 power = bfd_get_arch_info (abfd)->section_align_power;
3445 h->u.c.p->alignment_power = power;
3447 h->u.c.p->section = bfd_make_section_old_way (symbfd,
3452 /* Adjust the size of the common symbol if
3454 if (value > h->u.c.size)
3455 h->u.c.size = value;
3465 /* This symbol is weak but defined. We must pull it in if
3466 the current link symbol is undefined, but we don't want
3467 it if the current link symbol is common. */
3468 if (h->type == bfd_link_hash_undefined)
3470 if (!(*info->callbacks
3471 ->add_archive_element) (info, abfd, name, subsbfd))
3479 /* We do not need this object file. */
3482 /* Check a single archive element to see if we need to include it in
3483 the link. *PNEEDED is set according to whether this element is
3484 needed in the link or not. This is called from
3485 _bfd_generic_link_add_archive_symbols. */
3488 aout_link_check_archive_element (bfd *abfd,
3489 struct bfd_link_info *info,
3490 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
3491 const char *name ATTRIBUTE_UNUSED,
3492 bfd_boolean *pneeded)
3497 if (!aout_get_external_symbols (abfd))
3501 if (!aout_link_check_ar_symbols (abfd, info, pneeded, &abfd))
3507 /* Potentially, the add_archive_element hook may have set a
3508 substitute BFD for us. */
3511 if (!info->keep_memory
3512 && !aout_link_free_symbols (oldbfd))
3514 if (!aout_get_external_symbols (abfd))
3517 if (!aout_link_add_symbols (abfd, info))
3521 if (!info->keep_memory || !needed)
3523 if (!aout_link_free_symbols (abfd))
3530 /* Given an a.out BFD, add symbols to the global hash table as
3534 NAME (aout, link_add_symbols) (bfd *abfd, struct bfd_link_info *info)
3536 switch (bfd_get_format (abfd))
3539 return aout_link_add_object_symbols (abfd, info);
3541 return _bfd_generic_link_add_archive_symbols
3542 (abfd, info, aout_link_check_archive_element);
3544 bfd_set_error (bfd_error_wrong_format);
3549 /* A hash table used for header files with N_BINCL entries. */
3551 struct aout_link_includes_table
3553 struct bfd_hash_table root;
3556 /* A linked list of totals that we have found for a particular header
3559 struct aout_link_includes_totals
3561 struct aout_link_includes_totals *next;
3565 /* An entry in the header file hash table. */
3567 struct aout_link_includes_entry
3569 struct bfd_hash_entry root;
3570 /* List of totals we have found for this file. */
3571 struct aout_link_includes_totals *totals;
3574 /* Look up an entry in an the header file hash table. */
3576 #define aout_link_includes_lookup(table, string, create, copy) \
3577 ((struct aout_link_includes_entry *) \
3578 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3580 /* During the final link step we need to pass around a bunch of
3581 information, so we do it in an instance of this structure. */
3583 struct aout_final_link_info
3585 /* General link information. */
3586 struct bfd_link_info *info;
3589 /* Reloc file positions. */
3590 file_ptr treloff, dreloff;
3591 /* File position of symbols. */
3594 struct bfd_strtab_hash *strtab;
3595 /* Header file hash table. */
3596 struct aout_link_includes_table includes;
3597 /* A buffer large enough to hold the contents of any section. */
3599 /* A buffer large enough to hold the relocs of any section. */
3601 /* A buffer large enough to hold the symbol map of any input BFD. */
3603 /* A buffer large enough to hold output symbols of any input BFD. */
3604 struct external_nlist *output_syms;
3607 /* The function to create a new entry in the header file hash table. */
3609 static struct bfd_hash_entry *
3610 aout_link_includes_newfunc (struct bfd_hash_entry *entry,
3611 struct bfd_hash_table *table,
3614 struct aout_link_includes_entry *ret =
3615 (struct aout_link_includes_entry *) entry;
3617 /* Allocate the structure if it has not already been allocated by a
3620 ret = (struct aout_link_includes_entry *)
3621 bfd_hash_allocate (table, sizeof (* ret));
3625 /* Call the allocation method of the superclass. */
3626 ret = ((struct aout_link_includes_entry *)
3627 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3630 /* Set local fields. */
3634 return (struct bfd_hash_entry *) ret;
3637 /* Write out a symbol that was not associated with an a.out input
3641 aout_link_write_other_symbol (struct bfd_hash_entry *bh, void *data)
3643 struct aout_link_hash_entry *h = (struct aout_link_hash_entry *) bh;
3644 struct aout_final_link_info *flaginfo = (struct aout_final_link_info *) data;
3648 struct external_nlist outsym;
3652 if (h->root.type == bfd_link_hash_warning)
3654 h = (struct aout_link_hash_entry *) h->root.u.i.link;
3655 if (h->root.type == bfd_link_hash_new)
3659 output_bfd = flaginfo->output_bfd;
3661 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
3663 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
3664 (output_bfd, flaginfo->info, h)))
3666 /* FIXME: No way to handle errors. */
3676 /* An indx of -2 means the symbol must be written. */
3678 && (flaginfo->info->strip == strip_all
3679 || (flaginfo->info->strip == strip_some
3680 && bfd_hash_lookup (flaginfo->info->keep_hash, h->root.root.string,
3681 FALSE, FALSE) == NULL)))
3684 switch (h->root.type)
3687 case bfd_link_hash_warning:
3689 /* Avoid variable not initialized warnings. */
3691 case bfd_link_hash_new:
3692 /* This can happen for set symbols when sets are not being
3695 case bfd_link_hash_undefined:
3696 type = N_UNDF | N_EXT;
3699 case bfd_link_hash_defined:
3700 case bfd_link_hash_defweak:
3704 sec = h->root.u.def.section->output_section;
3705 BFD_ASSERT (bfd_is_abs_section (sec)
3706 || sec->owner == output_bfd);
3707 if (sec == obj_textsec (output_bfd))
3708 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
3709 else if (sec == obj_datasec (output_bfd))
3710 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
3711 else if (sec == obj_bsssec (output_bfd))
3712 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
3714 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
3716 val = (h->root.u.def.value
3718 + h->root.u.def.section->output_offset);
3721 case bfd_link_hash_common:
3722 type = N_UNDF | N_EXT;
3723 val = h->root.u.c.size;
3725 case bfd_link_hash_undefweak:
3729 case bfd_link_hash_indirect:
3730 /* We ignore these symbols, since the indirected symbol is
3731 already in the hash table. */
3735 H_PUT_8 (output_bfd, type, outsym.e_type);
3736 H_PUT_8 (output_bfd, 0, outsym.e_other);
3737 H_PUT_16 (output_bfd, 0, outsym.e_desc);
3738 indx = add_to_stringtab (output_bfd, flaginfo->strtab, h->root.root.string,
3740 if (indx == - (bfd_size_type) 1)
3741 /* FIXME: No way to handle errors. */
3744 PUT_WORD (output_bfd, indx, outsym.e_strx);
3745 PUT_WORD (output_bfd, val, outsym.e_value);
3747 amt = EXTERNAL_NLIST_SIZE;
3748 if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0
3749 || bfd_bwrite ((void *) &outsym, amt, output_bfd) != amt)
3750 /* FIXME: No way to handle errors. */
3753 flaginfo->symoff += EXTERNAL_NLIST_SIZE;
3754 h->indx = obj_aout_external_sym_count (output_bfd);
3755 ++obj_aout_external_sym_count (output_bfd);
3760 /* Handle a link order which is supposed to generate a reloc. */
3763 aout_link_reloc_link_order (struct aout_final_link_info *flaginfo,
3765 struct bfd_link_order *p)
3767 struct bfd_link_order_reloc *pr;
3770 reloc_howto_type *howto;
3771 file_ptr *reloff_ptr = NULL;
3772 struct reloc_std_external srel;
3773 struct reloc_ext_external erel;
3779 if (p->type == bfd_section_reloc_link_order)
3782 if (bfd_is_abs_section (pr->u.section))
3783 r_index = N_ABS | N_EXT;
3786 BFD_ASSERT (pr->u.section->owner == flaginfo->output_bfd);
3787 r_index = pr->u.section->target_index;
3792 struct aout_link_hash_entry *h;
3794 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
3796 h = ((struct aout_link_hash_entry *)
3797 bfd_wrapped_link_hash_lookup (flaginfo->output_bfd, flaginfo->info,
3798 pr->u.name, FALSE, FALSE, TRUE));
3804 /* We decided to strip this symbol, but it turns out that we
3805 can't. Note that we lose the other and desc information
3806 here. I don't think that will ever matter for a global
3810 if (!aout_link_write_other_symbol (&h->root.root, flaginfo))
3816 (*flaginfo->info->callbacks->unattached_reloc)
3817 (flaginfo->info, pr->u.name, NULL, NULL, (bfd_vma) 0);
3822 howto = bfd_reloc_type_lookup (flaginfo->output_bfd, pr->reloc);
3825 bfd_set_error (bfd_error_bad_value);
3829 if (o == obj_textsec (flaginfo->output_bfd))
3830 reloff_ptr = &flaginfo->treloff;
3831 else if (o == obj_datasec (flaginfo->output_bfd))
3832 reloff_ptr = &flaginfo->dreloff;
3836 if (obj_reloc_entry_size (flaginfo->output_bfd) == RELOC_STD_SIZE)
3839 MY_put_reloc (flaginfo->output_bfd, r_extern, r_index, p->offset, howto,
3849 r_pcrel = (int) howto->pc_relative;
3850 r_baserel = (howto->type & 8) != 0;
3851 r_jmptable = (howto->type & 16) != 0;
3852 r_relative = (howto->type & 32) != 0;
3853 r_length = howto->size;
3855 PUT_WORD (flaginfo->output_bfd, p->offset, srel.r_address);
3856 if (bfd_header_big_endian (flaginfo->output_bfd))
3858 srel.r_index[0] = r_index >> 16;
3859 srel.r_index[1] = r_index >> 8;
3860 srel.r_index[2] = r_index;
3862 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
3863 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
3864 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
3865 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
3866 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
3867 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
3871 srel.r_index[2] = r_index >> 16;
3872 srel.r_index[1] = r_index >> 8;
3873 srel.r_index[0] = r_index;
3875 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
3876 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
3877 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
3878 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
3879 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
3880 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
3884 rel_ptr = (void *) &srel;
3886 /* We have to write the addend into the object file, since
3887 standard a.out relocs are in place. It would be more
3888 reliable if we had the current contents of the file here,
3889 rather than assuming zeroes, but we can't read the file since
3890 it was opened using bfd_openw. */
3891 if (pr->addend != 0)
3894 bfd_reloc_status_type r;
3898 size = bfd_get_reloc_size (howto);
3899 buf = (bfd_byte *) bfd_zmalloc (size);
3900 if (buf == NULL && size != 0)
3902 r = MY_relocate_contents (howto, flaginfo->output_bfd,
3903 (bfd_vma) pr->addend, buf);
3909 case bfd_reloc_outofrange:
3911 case bfd_reloc_overflow:
3912 (*flaginfo->info->callbacks->reloc_overflow)
3913 (flaginfo->info, NULL,
3914 (p->type == bfd_section_reloc_link_order
3915 ? bfd_section_name (flaginfo->output_bfd,
3918 howto->name, pr->addend, NULL, NULL, (bfd_vma) 0);
3921 ok = bfd_set_section_contents (flaginfo->output_bfd, o, (void *) buf,
3922 (file_ptr) p->offset, size);
3930 #ifdef MY_put_ext_reloc
3931 MY_put_ext_reloc (flaginfo->output_bfd, r_extern, r_index, p->offset,
3932 howto, &erel, pr->addend);
3934 PUT_WORD (flaginfo->output_bfd, p->offset, erel.r_address);
3936 if (bfd_header_big_endian (flaginfo->output_bfd))
3938 erel.r_index[0] = r_index >> 16;
3939 erel.r_index[1] = r_index >> 8;
3940 erel.r_index[2] = r_index;
3942 ((r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
3943 | (howto->type << RELOC_EXT_BITS_TYPE_SH_BIG));
3947 erel.r_index[2] = r_index >> 16;
3948 erel.r_index[1] = r_index >> 8;
3949 erel.r_index[0] = r_index;
3951 (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
3952 | (howto->type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
3955 PUT_WORD (flaginfo->output_bfd, (bfd_vma) pr->addend, erel.r_addend);
3956 #endif /* MY_put_ext_reloc */
3958 rel_ptr = (void *) &erel;
3961 amt = obj_reloc_entry_size (flaginfo->output_bfd);
3962 if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
3963 || bfd_bwrite (rel_ptr, amt, flaginfo->output_bfd) != amt)
3966 *reloff_ptr += obj_reloc_entry_size (flaginfo->output_bfd);
3968 /* Assert that the relocs have not run into the symbols, and that n
3969 the text relocs have not run into the data relocs. */
3970 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
3971 && (reloff_ptr != &flaginfo->treloff
3973 <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
3978 /* Get the section corresponding to a reloc index. */
3980 static INLINE asection *
3981 aout_reloc_index_to_section (bfd *abfd, int indx)
3983 switch (indx & N_TYPE)
3985 case N_TEXT: return obj_textsec (abfd);
3986 case N_DATA: return obj_datasec (abfd);
3987 case N_BSS: return obj_bsssec (abfd);
3989 case N_UNDF: return bfd_abs_section_ptr;
3995 /* Relocate an a.out section using standard a.out relocs. */
3998 aout_link_input_section_std (struct aout_final_link_info *flaginfo,
4000 asection *input_section,
4001 struct reloc_std_external *relocs,
4002 bfd_size_type rel_size,
4005 bfd_boolean (*check_dynamic_reloc)
4006 (struct bfd_link_info *, bfd *, asection *,
4007 struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *,
4010 bfd_boolean relocatable;
4011 struct external_nlist *syms;
4013 struct aout_link_hash_entry **sym_hashes;
4015 bfd_size_type reloc_count;
4016 struct reloc_std_external *rel;
4017 struct reloc_std_external *rel_end;
4019 output_bfd = flaginfo->output_bfd;
4020 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4022 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE);
4023 BFD_ASSERT (input_bfd->xvec->header_byteorder
4024 == output_bfd->xvec->header_byteorder);
4026 relocatable = bfd_link_relocatable (flaginfo->info);
4027 syms = obj_aout_external_syms (input_bfd);
4028 strings = obj_aout_external_strings (input_bfd);
4029 sym_hashes = obj_aout_sym_hashes (input_bfd);
4030 symbol_map = flaginfo->symbol_map;
4032 reloc_count = rel_size / RELOC_STD_SIZE;
4034 rel_end = rel + reloc_count;
4035 for (; rel < rel_end; rel++)
4042 reloc_howto_type *howto;
4043 struct aout_link_hash_entry *h = NULL;
4045 bfd_reloc_status_type r;
4047 r_addr = GET_SWORD (input_bfd, rel->r_address);
4049 #ifdef MY_reloc_howto
4050 howto = MY_reloc_howto (input_bfd, rel, r_index, r_extern, r_pcrel);
4056 unsigned int howto_idx;
4058 if (bfd_header_big_endian (input_bfd))
4060 r_index = (((unsigned int) rel->r_index[0] << 16)
4061 | ((unsigned int) rel->r_index[1] << 8)
4063 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
4064 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
4065 r_baserel = (0 != (rel->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
4066 r_jmptable= (0 != (rel->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
4067 r_relative= (0 != (rel->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
4068 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
4069 >> RELOC_STD_BITS_LENGTH_SH_BIG);
4073 r_index = (((unsigned int) rel->r_index[2] << 16)
4074 | ((unsigned int) rel->r_index[1] << 8)
4076 r_extern = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
4077 r_pcrel = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
4078 r_baserel = (0 != (rel->r_type[0]
4079 & RELOC_STD_BITS_BASEREL_LITTLE));
4080 r_jmptable= (0 != (rel->r_type[0]
4081 & RELOC_STD_BITS_JMPTABLE_LITTLE));
4082 r_relative= (0 != (rel->r_type[0]
4083 & RELOC_STD_BITS_RELATIVE_LITTLE));
4084 r_length = ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
4085 >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
4088 howto_idx = (r_length + 4 * r_pcrel + 8 * r_baserel
4089 + 16 * r_jmptable + 32 * r_relative);
4090 if (howto_idx < TABLE_SIZE (howto_table_std))
4091 howto = howto_table_std + howto_idx;
4099 _bfd_error_handler (_("%pB: unsupported relocation type"),
4101 bfd_set_error (bfd_error_bad_value);
4107 /* We are generating a relocatable output file, and must
4108 modify the reloc accordingly. */
4111 /* If we know the symbol this relocation is against,
4112 convert it into a relocation against a section. This
4113 is what the native linker does. */
4114 h = sym_hashes[r_index];
4116 && (h->root.type == bfd_link_hash_defined
4117 || h->root.type == bfd_link_hash_defweak))
4119 asection *output_section;
4121 /* Change the r_extern value. */
4122 if (bfd_header_big_endian (output_bfd))
4123 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_BIG;
4125 rel->r_type[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE;
4127 /* Compute a new r_index. */
4128 output_section = h->root.u.def.section->output_section;
4129 if (output_section == obj_textsec (output_bfd))
4131 else if (output_section == obj_datasec (output_bfd))
4133 else if (output_section == obj_bsssec (output_bfd))
4138 /* Add the symbol value and the section VMA to the
4139 addend stored in the contents. */
4140 relocation = (h->root.u.def.value
4141 + output_section->vma
4142 + h->root.u.def.section->output_offset);
4146 /* We must change r_index according to the symbol
4148 r_index = symbol_map[r_index];
4154 /* We decided to strip this symbol, but it
4155 turns out that we can't. Note that we
4156 lose the other and desc information here.
4157 I don't think that will ever matter for a
4163 if (!aout_link_write_other_symbol (&h->root.root,
4173 name = strings + GET_WORD (input_bfd,
4174 syms[r_index].e_strx);
4175 (*flaginfo->info->callbacks->unattached_reloc)
4176 (flaginfo->info, name,
4177 input_bfd, input_section, r_addr);
4185 /* Write out the new r_index value. */
4186 if (bfd_header_big_endian (output_bfd))
4188 rel->r_index[0] = r_index >> 16;
4189 rel->r_index[1] = r_index >> 8;
4190 rel->r_index[2] = r_index;
4194 rel->r_index[2] = r_index >> 16;
4195 rel->r_index[1] = r_index >> 8;
4196 rel->r_index[0] = r_index;
4203 /* This is a relocation against a section. We must
4204 adjust by the amount that the section moved. */
4205 section = aout_reloc_index_to_section (input_bfd, r_index);
4206 relocation = (section->output_section->vma
4207 + section->output_offset
4211 /* Change the address of the relocation. */
4212 PUT_WORD (output_bfd,
4213 r_addr + input_section->output_offset,
4216 /* Adjust a PC relative relocation by removing the reference
4217 to the original address in the section and including the
4218 reference to the new address. */
4220 relocation -= (input_section->output_section->vma
4221 + input_section->output_offset
4222 - input_section->vma);
4224 #ifdef MY_relocatable_reloc
4225 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
4228 if (relocation == 0)
4231 r = MY_relocate_contents (howto,
4232 input_bfd, relocation,
4239 /* We are generating an executable, and must do a full
4245 h = sym_hashes[r_index];
4248 && (h->root.type == bfd_link_hash_defined
4249 || h->root.type == bfd_link_hash_defweak))
4251 relocation = (h->root.u.def.value
4252 + h->root.u.def.section->output_section->vma
4253 + h->root.u.def.section->output_offset);
4256 && h->root.type == bfd_link_hash_undefweak)
4268 section = aout_reloc_index_to_section (input_bfd, r_index);
4269 relocation = (section->output_section->vma
4270 + section->output_offset
4273 relocation += input_section->vma;
4276 if (check_dynamic_reloc != NULL)
4280 if (! ((*check_dynamic_reloc)
4281 (flaginfo->info, input_bfd, input_section, h,
4282 (void *) rel, contents, &skip, &relocation)))
4288 /* Now warn if a global symbol is undefined. We could not
4289 do this earlier, because check_dynamic_reloc might want
4290 to skip this reloc. */
4291 if (hundef && ! bfd_link_pic (flaginfo->info) && ! r_baserel)
4296 name = h->root.root.string;
4298 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4299 (*flaginfo->info->callbacks->undefined_symbol)
4300 (flaginfo->info, name, input_bfd, input_section, r_addr, TRUE);
4303 r = MY_final_link_relocate (howto,
4304 input_bfd, input_section,
4305 contents, r_addr, relocation,
4309 if (r != bfd_reloc_ok)
4314 case bfd_reloc_outofrange:
4316 case bfd_reloc_overflow:
4323 name = strings + GET_WORD (input_bfd,
4324 syms[r_index].e_strx);
4329 s = aout_reloc_index_to_section (input_bfd, r_index);
4330 name = bfd_section_name (input_bfd, s);
4332 (*flaginfo->info->callbacks->reloc_overflow)
4333 (flaginfo->info, (h ? &h->root : NULL), name, howto->name,
4334 (bfd_vma) 0, input_bfd, input_section, r_addr);
4344 /* Relocate an a.out section using extended a.out relocs. */
4347 aout_link_input_section_ext (struct aout_final_link_info *flaginfo,
4349 asection *input_section,
4350 struct reloc_ext_external *relocs,
4351 bfd_size_type rel_size,
4354 bfd_boolean (*check_dynamic_reloc)
4355 (struct bfd_link_info *, bfd *, asection *,
4356 struct aout_link_hash_entry *, void *, bfd_byte *, bfd_boolean *,
4359 bfd_boolean relocatable;
4360 struct external_nlist *syms;
4362 struct aout_link_hash_entry **sym_hashes;
4364 bfd_size_type reloc_count;
4365 struct reloc_ext_external *rel;
4366 struct reloc_ext_external *rel_end;
4368 output_bfd = flaginfo->output_bfd;
4369 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4371 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_EXT_SIZE);
4372 BFD_ASSERT (input_bfd->xvec->header_byteorder
4373 == output_bfd->xvec->header_byteorder);
4375 relocatable = bfd_link_relocatable (flaginfo->info);
4376 syms = obj_aout_external_syms (input_bfd);
4377 strings = obj_aout_external_strings (input_bfd);
4378 sym_hashes = obj_aout_sym_hashes (input_bfd);
4379 symbol_map = flaginfo->symbol_map;
4381 reloc_count = rel_size / RELOC_EXT_SIZE;
4383 rel_end = rel + reloc_count;
4384 for (; rel < rel_end; rel++)
4389 unsigned int r_type;
4391 struct aout_link_hash_entry *h = NULL;
4392 asection *r_section = NULL;
4395 r_addr = GET_SWORD (input_bfd, rel->r_address);
4397 if (bfd_header_big_endian (input_bfd))
4399 r_index = (((unsigned int) rel->r_index[0] << 16)
4400 | ((unsigned int) rel->r_index[1] << 8)
4402 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
4403 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
4404 >> RELOC_EXT_BITS_TYPE_SH_BIG);
4408 r_index = (((unsigned int) rel->r_index[2] << 16)
4409 | ((unsigned int) rel->r_index[1] << 8)
4411 r_extern = (0 != (rel->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
4412 r_type = ((rel->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
4413 >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
4416 r_addend = GET_SWORD (input_bfd, rel->r_addend);
4418 if (r_type >= TABLE_SIZE (howto_table_ext))
4420 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4422 bfd_set_error (bfd_error_bad_value);
4428 /* We are generating a relocatable output file, and must
4429 modify the reloc accordingly. */
4431 || r_type == (unsigned int) RELOC_BASE10
4432 || r_type == (unsigned int) RELOC_BASE13
4433 || r_type == (unsigned int) RELOC_BASE22)
4435 /* If we know the symbol this relocation is against,
4436 convert it into a relocation against a section. This
4437 is what the native linker does. */
4438 if (r_type == (unsigned int) RELOC_BASE10
4439 || r_type == (unsigned int) RELOC_BASE13
4440 || r_type == (unsigned int) RELOC_BASE22)
4443 h = sym_hashes[r_index];
4445 && (h->root.type == bfd_link_hash_defined
4446 || h->root.type == bfd_link_hash_defweak))
4448 asection *output_section;
4450 /* Change the r_extern value. */
4451 if (bfd_header_big_endian (output_bfd))
4452 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_BIG;
4454 rel->r_type[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE;
4456 /* Compute a new r_index. */
4457 output_section = h->root.u.def.section->output_section;
4458 if (output_section == obj_textsec (output_bfd))
4460 else if (output_section == obj_datasec (output_bfd))
4462 else if (output_section == obj_bsssec (output_bfd))
4467 /* Add the symbol value and the section VMA to the
4469 relocation = (h->root.u.def.value
4470 + output_section->vma
4471 + h->root.u.def.section->output_offset);
4473 /* Now RELOCATION is the VMA of the final
4474 destination. If this is a PC relative reloc,
4475 then ADDEND is the negative of the source VMA.
4476 We want to set ADDEND to the difference between
4477 the destination VMA and the source VMA, which
4478 means we must adjust RELOCATION by the change in
4479 the source VMA. This is done below. */
4483 /* We must change r_index according to the symbol
4485 r_index = symbol_map[r_index];
4491 /* We decided to strip this symbol, but it
4492 turns out that we can't. Note that we
4493 lose the other and desc information here.
4494 I don't think that will ever matter for a
4500 if (!aout_link_write_other_symbol (&h->root.root,
4510 name = strings + GET_WORD (input_bfd,
4511 syms[r_index].e_strx);
4512 (*flaginfo->info->callbacks->unattached_reloc)
4513 (flaginfo->info, name,
4514 input_bfd, input_section, r_addr);
4521 /* If this is a PC relative reloc, then the addend
4522 is the negative of the source VMA. We must
4523 adjust it by the change in the source VMA. This
4527 /* Write out the new r_index value. */
4528 if (bfd_header_big_endian (output_bfd))
4530 rel->r_index[0] = r_index >> 16;
4531 rel->r_index[1] = r_index >> 8;
4532 rel->r_index[2] = r_index;
4536 rel->r_index[2] = r_index >> 16;
4537 rel->r_index[1] = r_index >> 8;
4538 rel->r_index[0] = r_index;
4543 /* This is a relocation against a section. We must
4544 adjust by the amount that the section moved. */
4545 r_section = aout_reloc_index_to_section (input_bfd, r_index);
4546 relocation = (r_section->output_section->vma
4547 + r_section->output_offset
4550 /* If this is a PC relative reloc, then the addend is
4551 the difference in VMA between the destination and the
4552 source. We have just adjusted for the change in VMA
4553 of the destination, so we must also adjust by the
4554 change in VMA of the source. This is done below. */
4557 /* As described above, we must always adjust a PC relative
4558 reloc by the change in VMA of the source. However, if
4559 pcrel_offset is set, then the addend does not include the
4560 location within the section, in which case we don't need
4561 to adjust anything. */
4562 if (howto_table_ext[r_type].pc_relative
4563 && ! howto_table_ext[r_type].pcrel_offset)
4564 relocation -= (input_section->output_section->vma
4565 + input_section->output_offset
4566 - input_section->vma);
4568 /* Change the addend if necessary. */
4569 if (relocation != 0)
4570 PUT_WORD (output_bfd, r_addend + relocation, rel->r_addend);
4572 /* Change the address of the relocation. */
4573 PUT_WORD (output_bfd,
4574 r_addr + input_section->output_offset,
4580 bfd_reloc_status_type r;
4582 /* We are generating an executable, and must do a full
4588 h = sym_hashes[r_index];
4591 && (h->root.type == bfd_link_hash_defined
4592 || h->root.type == bfd_link_hash_defweak))
4594 relocation = (h->root.u.def.value
4595 + h->root.u.def.section->output_section->vma
4596 + h->root.u.def.section->output_offset);
4599 && h->root.type == bfd_link_hash_undefweak)
4607 else if (r_type == (unsigned int) RELOC_BASE10
4608 || r_type == (unsigned int) RELOC_BASE13
4609 || r_type == (unsigned int) RELOC_BASE22)
4611 struct external_nlist *sym;
4614 /* For base relative relocs, r_index is always an index
4615 into the symbol table, even if r_extern is 0. */
4616 sym = syms + r_index;
4617 type = H_GET_8 (input_bfd, sym->e_type);
4618 if ((type & N_TYPE) == N_TEXT
4620 r_section = obj_textsec (input_bfd);
4621 else if ((type & N_TYPE) == N_DATA
4623 r_section = obj_datasec (input_bfd);
4624 else if ((type & N_TYPE) == N_BSS
4626 r_section = obj_bsssec (input_bfd);
4627 else if ((type & N_TYPE) == N_ABS
4629 r_section = bfd_abs_section_ptr;
4632 relocation = (r_section->output_section->vma
4633 + r_section->output_offset
4634 + (GET_WORD (input_bfd, sym->e_value)
4639 r_section = aout_reloc_index_to_section (input_bfd, r_index);
4641 /* If this is a PC relative reloc, then R_ADDEND is the
4642 difference between the two vmas, or
4643 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4645 old_dest_sec == section->vma
4647 old_src_sec == input_section->vma
4649 old_src_off == r_addr
4651 _bfd_final_link_relocate expects RELOCATION +
4652 R_ADDEND to be the VMA of the destination minus
4653 r_addr (the minus r_addr is because this relocation
4654 is not pcrel_offset, which is a bit confusing and
4655 should, perhaps, be changed), or
4658 new_dest_sec == output_section->vma + output_offset
4659 We arrange for this to happen by setting RELOCATION to
4660 new_dest_sec + old_src_sec - old_dest_sec
4662 If this is not a PC relative reloc, then R_ADDEND is
4663 simply the VMA of the destination, so we set
4664 RELOCATION to the change in the destination VMA, or
4665 new_dest_sec - old_dest_sec
4667 relocation = (r_section->output_section->vma
4668 + r_section->output_offset
4670 if (howto_table_ext[r_type].pc_relative)
4671 relocation += input_section->vma;
4674 if (check_dynamic_reloc != NULL)
4678 if (! ((*check_dynamic_reloc)
4679 (flaginfo->info, input_bfd, input_section, h,
4680 (void *) rel, contents, &skip, &relocation)))
4686 /* Now warn if a global symbol is undefined. We could not
4687 do this earlier, because check_dynamic_reloc might want
4688 to skip this reloc. */
4690 && ! bfd_link_pic (flaginfo->info)
4691 && r_type != (unsigned int) RELOC_BASE10
4692 && r_type != (unsigned int) RELOC_BASE13
4693 && r_type != (unsigned int) RELOC_BASE22)
4698 name = h->root.root.string;
4700 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4701 (*flaginfo->info->callbacks->undefined_symbol)
4702 (flaginfo->info, name, input_bfd, input_section, r_addr, TRUE);
4705 if (r_type != (unsigned int) RELOC_SPARC_REV32)
4706 r = MY_final_link_relocate (howto_table_ext + r_type,
4707 input_bfd, input_section,
4708 contents, r_addr, relocation,
4714 x = bfd_get_32 (input_bfd, contents + r_addr);
4715 x = x + relocation + r_addend;
4716 bfd_putl32 (/*input_bfd,*/ x, contents + r_addr);
4720 if (r != bfd_reloc_ok)
4725 case bfd_reloc_outofrange:
4727 case bfd_reloc_overflow:
4734 || r_type == (unsigned int) RELOC_BASE10
4735 || r_type == (unsigned int) RELOC_BASE13
4736 || r_type == (unsigned int) RELOC_BASE22)
4737 name = strings + GET_WORD (input_bfd,
4738 syms[r_index].e_strx);
4743 s = aout_reloc_index_to_section (input_bfd, r_index);
4744 name = bfd_section_name (input_bfd, s);
4746 (*flaginfo->info->callbacks->reloc_overflow)
4747 (flaginfo->info, (h ? &h->root : NULL), name,
4748 howto_table_ext[r_type].name,
4749 r_addend, input_bfd, input_section, r_addr);
4760 /* Link an a.out section into the output file. */
4763 aout_link_input_section (struct aout_final_link_info *flaginfo,
4765 asection *input_section,
4766 file_ptr *reloff_ptr,
4767 bfd_size_type rel_size)
4769 bfd_size_type input_size;
4772 /* Get the section contents. */
4773 input_size = input_section->size;
4774 if (! bfd_get_section_contents (input_bfd, input_section,
4775 (void *) flaginfo->contents,
4776 (file_ptr) 0, input_size))
4779 /* Read in the relocs if we haven't already done it. */
4780 if (aout_section_data (input_section) != NULL
4781 && aout_section_data (input_section)->relocs != NULL)
4782 relocs = aout_section_data (input_section)->relocs;
4785 relocs = flaginfo->relocs;
4788 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4789 || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
4794 /* Relocate the section contents. */
4795 if (obj_reloc_entry_size (input_bfd) == RELOC_STD_SIZE)
4797 if (! aout_link_input_section_std (flaginfo, input_bfd, input_section,
4798 (struct reloc_std_external *) relocs,
4799 rel_size, flaginfo->contents))
4804 if (! aout_link_input_section_ext (flaginfo, input_bfd, input_section,
4805 (struct reloc_ext_external *) relocs,
4806 rel_size, flaginfo->contents))
4810 /* Write out the section contents. */
4811 if (! bfd_set_section_contents (flaginfo->output_bfd,
4812 input_section->output_section,
4813 (void *) flaginfo->contents,
4814 (file_ptr) input_section->output_offset,
4818 /* If we are producing relocatable output, the relocs were
4819 modified, and we now write them out. */
4820 if (bfd_link_relocatable (flaginfo->info) && rel_size > 0)
4822 if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4824 if (bfd_bwrite (relocs, rel_size, flaginfo->output_bfd) != rel_size)
4826 *reloff_ptr += rel_size;
4828 /* Assert that the relocs have not run into the symbols, and
4829 that if these are the text relocs they have not run into the
4831 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
4832 && (reloff_ptr != &flaginfo->treloff
4834 <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
4840 /* Adjust and write out the symbols for an a.out file. Set the new
4841 symbol indices into a symbol_map. */
4844 aout_link_write_symbols (struct aout_final_link_info *flaginfo, bfd *input_bfd)
4847 bfd_size_type sym_count;
4849 enum bfd_link_strip strip;
4850 enum bfd_link_discard discard;
4851 struct external_nlist *outsym;
4852 bfd_size_type strtab_index;
4853 struct external_nlist *sym;
4854 struct external_nlist *sym_end;
4855 struct aout_link_hash_entry **sym_hash;
4858 bfd_boolean skip_next;
4860 output_bfd = flaginfo->output_bfd;
4861 sym_count = obj_aout_external_sym_count (input_bfd);
4862 strings = obj_aout_external_strings (input_bfd);
4863 strip = flaginfo->info->strip;
4864 discard = flaginfo->info->discard;
4865 outsym = flaginfo->output_syms;
4867 /* First write out a symbol for this object file, unless we are
4868 discarding such symbols. */
4869 if (strip != strip_all
4870 && (strip != strip_some
4871 || bfd_hash_lookup (flaginfo->info->keep_hash, input_bfd->filename,
4872 FALSE, FALSE) != NULL)
4873 && discard != discard_all)
4875 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
4876 H_PUT_8 (output_bfd, 0, outsym->e_other);
4877 H_PUT_16 (output_bfd, 0, outsym->e_desc);
4878 strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
4879 input_bfd->filename, FALSE);
4880 if (strtab_index == (bfd_size_type) -1)
4882 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4883 PUT_WORD (output_bfd,
4884 (bfd_get_section_vma (output_bfd,
4885 obj_textsec (input_bfd)->output_section)
4886 + obj_textsec (input_bfd)->output_offset),
4888 ++obj_aout_external_sym_count (output_bfd);
4894 sym = obj_aout_external_syms (input_bfd);
4895 sym_end = sym + sym_count;
4896 sym_hash = obj_aout_sym_hashes (input_bfd);
4897 symbol_map = flaginfo->symbol_map;
4898 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
4899 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
4903 struct aout_link_hash_entry *h;
4909 /* We set *symbol_map to 0 above for all symbols. If it has
4910 already been set to -1 for this symbol, it means that we are
4911 discarding it because it appears in a duplicate header file.
4912 See the N_BINCL code below. */
4913 if (*symbol_map == -1)
4916 /* Initialize *symbol_map to -1, which means that the symbol was
4917 not copied into the output file. We will change it later if
4918 we do copy the symbol over. */
4921 type = H_GET_8 (input_bfd, sym->e_type);
4922 name = strings + GET_WORD (input_bfd, sym->e_strx);
4928 /* Pass this symbol through. It is the target of an
4929 indirect or warning symbol. */
4930 val = GET_WORD (input_bfd, sym->e_value);
4935 /* Skip this symbol, which is the target of an indirect
4936 symbol that we have changed to no longer be an indirect
4943 struct aout_link_hash_entry *hresolve;
4945 /* We have saved the hash table entry for this symbol, if
4946 there is one. Note that we could just look it up again
4947 in the hash table, provided we first check that it is an
4951 /* Use the name from the hash table, in case the symbol was
4954 && h->root.type != bfd_link_hash_warning)
4955 name = h->root.root.string;
4957 /* If this is an indirect or warning symbol, then change
4958 hresolve to the base symbol. We also change *sym_hash so
4959 that the relocation routines relocate against the real
4962 if (h != (struct aout_link_hash_entry *) NULL
4963 && (h->root.type == bfd_link_hash_indirect
4964 || h->root.type == bfd_link_hash_warning))
4966 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
4967 while (hresolve->root.type == bfd_link_hash_indirect
4968 || hresolve->root.type == bfd_link_hash_warning)
4969 hresolve = ((struct aout_link_hash_entry *)
4970 hresolve->root.u.i.link);
4971 *sym_hash = hresolve;
4974 /* If the symbol has already been written out, skip it. */
4978 if ((type & N_TYPE) == N_INDR
4979 || type == N_WARNING)
4981 *symbol_map = h->indx;
4985 /* See if we are stripping this symbol. */
4991 case strip_debugger:
4992 if ((type & N_STAB) != 0)
4996 if (bfd_hash_lookup (flaginfo->info->keep_hash, name, FALSE, FALSE)
5011 /* Get the value of the symbol. */
5012 if ((type & N_TYPE) == N_TEXT
5014 symsec = obj_textsec (input_bfd);
5015 else if ((type & N_TYPE) == N_DATA
5017 symsec = obj_datasec (input_bfd);
5018 else if ((type & N_TYPE) == N_BSS
5020 symsec = obj_bsssec (input_bfd);
5021 else if ((type & N_TYPE) == N_ABS
5023 symsec = bfd_abs_section_ptr;
5024 else if (((type & N_TYPE) == N_INDR
5025 && (hresolve == NULL
5026 || (hresolve->root.type != bfd_link_hash_defined
5027 && hresolve->root.type != bfd_link_hash_defweak
5028 && hresolve->root.type != bfd_link_hash_common)))
5029 || type == N_WARNING)
5031 /* Pass the next symbol through unchanged. The
5032 condition above for indirect symbols is so that if
5033 the indirect symbol was defined, we output it with
5034 the correct definition so the debugger will
5037 val = GET_WORD (input_bfd, sym->e_value);
5040 else if ((type & N_STAB) != 0)
5042 val = GET_WORD (input_bfd, sym->e_value);
5047 /* If we get here with an indirect symbol, it means that
5048 we are outputting it with a real definition. In such
5049 a case we do not want to output the next symbol,
5050 which is the target of the indirection. */
5051 if ((type & N_TYPE) == N_INDR)
5056 /* We need to get the value from the hash table. We use
5057 hresolve so that if we have defined an indirect
5058 symbol we output the final definition. */
5061 switch (type & N_TYPE)
5064 symsec = obj_textsec (input_bfd);
5067 symsec = obj_datasec (input_bfd);
5070 symsec = obj_bsssec (input_bfd);
5073 symsec = bfd_abs_section_ptr;
5080 else if (hresolve->root.type == bfd_link_hash_defined
5081 || hresolve->root.type == bfd_link_hash_defweak)
5083 asection *input_section;
5084 asection *output_section;
5086 /* This case usually means a common symbol which was
5087 turned into a defined symbol. */
5088 input_section = hresolve->root.u.def.section;
5089 output_section = input_section->output_section;
5090 BFD_ASSERT (bfd_is_abs_section (output_section)
5091 || output_section->owner == output_bfd);
5092 val = (hresolve->root.u.def.value
5093 + bfd_get_section_vma (output_bfd, output_section)
5094 + input_section->output_offset);
5096 /* Get the correct type based on the section. If
5097 this is a constructed set, force it to be
5098 globally visible. */
5107 if (output_section == obj_textsec (output_bfd))
5108 type |= (hresolve->root.type == bfd_link_hash_defined
5111 else if (output_section == obj_datasec (output_bfd))
5112 type |= (hresolve->root.type == bfd_link_hash_defined
5115 else if (output_section == obj_bsssec (output_bfd))
5116 type |= (hresolve->root.type == bfd_link_hash_defined
5120 type |= (hresolve->root.type == bfd_link_hash_defined
5124 else if (hresolve->root.type == bfd_link_hash_common)
5125 val = hresolve->root.u.c.size;
5126 else if (hresolve->root.type == bfd_link_hash_undefweak)
5135 val = (symsec->output_section->vma
5136 + symsec->output_offset
5137 + (GET_WORD (input_bfd, sym->e_value)
5140 /* If this is a global symbol set the written flag, and if
5141 it is a local symbol see if we should discard it. */
5145 h->indx = obj_aout_external_sym_count (output_bfd);
5147 else if ((type & N_TYPE) != N_SETT
5148 && (type & N_TYPE) != N_SETD
5149 && (type & N_TYPE) != N_SETB
5150 && (type & N_TYPE) != N_SETA)
5155 case discard_sec_merge:
5158 if ((type & N_STAB) == 0
5159 && bfd_is_local_label_name (input_bfd, name))
5173 /* An N_BINCL symbol indicates the start of the stabs
5174 entries for a header file. We need to scan ahead to the
5175 next N_EINCL symbol, ignoring nesting, adding up all the
5176 characters in the symbol names, not including the file
5177 numbers in types (the first number after an open
5179 if (type == (int) N_BINCL)
5181 struct external_nlist *incl_sym;
5183 struct aout_link_includes_entry *incl_entry;
5184 struct aout_link_includes_totals *t;
5188 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
5192 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
5193 if (incl_type == (int) N_EINCL)
5199 else if (incl_type == (int) N_BINCL)
5205 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
5206 for (; *s != '\0'; s++)
5211 /* Skip the file number. */
5213 while (ISDIGIT (*s))
5221 /* If we have already included a header file with the
5222 same value, then replace this one with an N_EXCL
5224 copy = (bfd_boolean) (! flaginfo->info->keep_memory);
5225 incl_entry = aout_link_includes_lookup (&flaginfo->includes,
5227 if (incl_entry == NULL)
5229 for (t = incl_entry->totals; t != NULL; t = t->next)
5230 if (t->total == val)
5234 /* This is the first time we have seen this header
5235 file with this set of stabs strings. */
5236 t = (struct aout_link_includes_totals *)
5237 bfd_hash_allocate (&flaginfo->includes.root,
5242 t->next = incl_entry->totals;
5243 incl_entry->totals = t;
5249 /* This is a duplicate header file. We must change
5250 it to be an N_EXCL entry, and mark all the
5251 included symbols to prevent outputting them. */
5252 type = (int) N_EXCL;
5255 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
5257 incl_sym++, incl_map++)
5261 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
5262 if (incl_type == (int) N_EINCL)
5271 else if (incl_type == (int) N_BINCL)
5280 /* Copy this symbol into the list of symbols we are going to
5282 H_PUT_8 (output_bfd, type, outsym->e_type);
5283 H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_other), outsym->e_other);
5284 H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
5286 if (! flaginfo->info->keep_memory)
5288 /* name points into a string table which we are going to
5289 free. If there is a hash table entry, use that string.
5290 Otherwise, copy name into memory. */
5292 name = h->root.root.string;
5296 strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
5298 if (strtab_index == (bfd_size_type) -1)
5300 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
5301 PUT_WORD (output_bfd, val, outsym->e_value);
5302 *symbol_map = obj_aout_external_sym_count (output_bfd);
5303 ++obj_aout_external_sym_count (output_bfd);
5307 /* Write out the output symbols we have just constructed. */
5308 if (outsym > flaginfo->output_syms)
5310 bfd_size_type outsym_size;
5312 if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0)
5314 outsym_size = outsym - flaginfo->output_syms;
5315 outsym_size *= EXTERNAL_NLIST_SIZE;
5316 if (bfd_bwrite ((void *) flaginfo->output_syms, outsym_size, output_bfd)
5319 flaginfo->symoff += outsym_size;
5325 /* Link an a.out input BFD into the output file. */
5328 aout_link_input_bfd (struct aout_final_link_info *flaginfo, bfd *input_bfd)
5330 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
5332 /* If this is a dynamic object, it may need special handling. */
5333 if ((input_bfd->flags & DYNAMIC) != 0
5334 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
5335 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
5336 (flaginfo->info, input_bfd));
5338 /* Get the symbols. We probably have them already, unless
5339 flaginfo->info->keep_memory is FALSE. */
5340 if (! aout_get_external_symbols (input_bfd))
5343 /* Write out the symbols and get a map of the new indices. The map
5344 is placed into flaginfo->symbol_map. */
5345 if (! aout_link_write_symbols (flaginfo, input_bfd))
5348 /* Relocate and write out the sections. These functions use the
5349 symbol map created by aout_link_write_symbols. The linker_mark
5350 field will be set if these sections are to be included in the
5351 link, which will normally be the case. */
5352 if (obj_textsec (input_bfd)->linker_mark)
5354 if (! aout_link_input_section (flaginfo, input_bfd,
5355 obj_textsec (input_bfd),
5357 exec_hdr (input_bfd)->a_trsize))
5360 if (obj_datasec (input_bfd)->linker_mark)
5362 if (! aout_link_input_section (flaginfo, input_bfd,
5363 obj_datasec (input_bfd),
5365 exec_hdr (input_bfd)->a_drsize))
5369 /* If we are not keeping memory, we don't need the symbols any
5370 longer. We still need them if we are keeping memory, because the
5371 strings in the hash table point into them. */
5372 if (! flaginfo->info->keep_memory)
5374 if (! aout_link_free_symbols (input_bfd))
5381 /* Do the final link step. This is called on the output BFD. The
5382 INFO structure should point to a list of BFDs linked through the
5383 link.next field which can be used to find each BFD which takes part
5384 in the output. Also, each section in ABFD should point to a list
5385 of bfd_link_order structures which list all the input sections for
5386 the output section. */
5389 NAME (aout, final_link) (bfd *abfd,
5390 struct bfd_link_info *info,
5391 void (*callback) (bfd *, file_ptr *, file_ptr *, file_ptr *))
5393 struct aout_final_link_info aout_info;
5394 bfd_boolean includes_hash_initialized = FALSE;
5396 bfd_size_type trsize, drsize;
5397 bfd_size_type max_contents_size;
5398 bfd_size_type max_relocs_size;
5399 bfd_size_type max_sym_count;
5400 struct bfd_link_order *p;
5402 bfd_boolean have_link_order_relocs;
5404 if (bfd_link_pic (info))
5405 abfd->flags |= DYNAMIC;
5407 aout_info.info = info;
5408 aout_info.output_bfd = abfd;
5409 aout_info.contents = NULL;
5410 aout_info.relocs = NULL;
5411 aout_info.symbol_map = NULL;
5412 aout_info.output_syms = NULL;
5414 if (!bfd_hash_table_init_n (&aout_info.includes.root,
5415 aout_link_includes_newfunc,
5416 sizeof (struct aout_link_includes_entry),
5419 includes_hash_initialized = TRUE;
5421 /* Figure out the largest section size. Also, if generating
5422 relocatable output, count the relocs. */
5425 max_contents_size = 0;
5426 max_relocs_size = 0;
5428 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
5432 if (bfd_link_relocatable (info))
5434 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
5436 trsize += exec_hdr (sub)->a_trsize;
5437 drsize += exec_hdr (sub)->a_drsize;
5441 /* FIXME: We need to identify the .text and .data sections
5442 and call get_reloc_upper_bound and canonicalize_reloc to
5443 work out the number of relocs needed, and then multiply
5444 by the reloc size. */
5446 /* xgettext:c-format */
5447 (_("%pB: relocatable link from %s to %s not supported"),
5448 abfd, sub->xvec->name, abfd->xvec->name);
5449 bfd_set_error (bfd_error_invalid_operation);
5454 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
5456 sz = obj_textsec (sub)->size;
5457 if (sz > max_contents_size)
5458 max_contents_size = sz;
5459 sz = obj_datasec (sub)->size;
5460 if (sz > max_contents_size)
5461 max_contents_size = sz;
5463 sz = exec_hdr (sub)->a_trsize;
5464 if (sz > max_relocs_size)
5465 max_relocs_size = sz;
5466 sz = exec_hdr (sub)->a_drsize;
5467 if (sz > max_relocs_size)
5468 max_relocs_size = sz;
5470 sz = obj_aout_external_sym_count (sub);
5471 if (sz > max_sym_count)
5476 if (bfd_link_relocatable (info))
5478 if (obj_textsec (abfd) != NULL)
5479 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
5480 ->map_head.link_order)
5481 * obj_reloc_entry_size (abfd));
5482 if (obj_datasec (abfd) != NULL)
5483 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
5484 ->map_head.link_order)
5485 * obj_reloc_entry_size (abfd));
5488 exec_hdr (abfd)->a_trsize = trsize;
5489 exec_hdr (abfd)->a_drsize = drsize;
5491 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
5493 /* Adjust the section sizes and vmas according to the magic number.
5494 This sets a_text, a_data and a_bss in the exec_hdr and sets the
5495 filepos for each section. */
5496 if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
5499 /* The relocation and symbol file positions differ among a.out
5500 targets. We are passed a callback routine from the backend
5501 specific code to handle this.
5502 FIXME: At this point we do not know how much space the symbol
5503 table will require. This will not work for any (nonstandard)
5504 a.out target that needs to know the symbol table size before it
5505 can compute the relocation file positions. This may or may not
5506 be the case for the hp300hpux target, for example. */
5507 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
5509 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
5510 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
5511 obj_sym_filepos (abfd) = aout_info.symoff;
5513 /* We keep a count of the symbols as we output them. */
5514 obj_aout_external_sym_count (abfd) = 0;
5516 /* We accumulate the string table as we write out the symbols. */
5517 aout_info.strtab = _bfd_stringtab_init ();
5518 if (aout_info.strtab == NULL)
5521 /* Allocate buffers to hold section contents and relocs. */
5522 aout_info.contents = (bfd_byte *) bfd_malloc (max_contents_size);
5523 aout_info.relocs = bfd_malloc (max_relocs_size);
5524 aout_info.symbol_map = (int *) bfd_malloc (max_sym_count * sizeof (int));
5525 aout_info.output_syms = (struct external_nlist *)
5526 bfd_malloc ((max_sym_count + 1) * sizeof (struct external_nlist));
5527 if ((aout_info.contents == NULL && max_contents_size != 0)
5528 || (aout_info.relocs == NULL && max_relocs_size != 0)
5529 || (aout_info.symbol_map == NULL && max_sym_count != 0)
5530 || aout_info.output_syms == NULL)
5533 /* If we have a symbol named __DYNAMIC, force it out now. This is
5534 required by SunOS. Doing this here rather than in sunos.c is a
5535 hack, but it's easier than exporting everything which would be
5538 struct aout_link_hash_entry *h;
5540 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
5541 FALSE, FALSE, FALSE);
5543 aout_link_write_other_symbol (&h->root.root, &aout_info);
5546 /* The most time efficient way to do the link would be to read all
5547 the input object files into memory and then sort out the
5548 information into the output file. Unfortunately, that will
5549 probably use too much memory. Another method would be to step
5550 through everything that composes the text section and write it
5551 out, and then everything that composes the data section and write
5552 it out, and then write out the relocs, and then write out the
5553 symbols. Unfortunately, that requires reading stuff from each
5554 input file several times, and we will not be able to keep all the
5555 input files open simultaneously, and reopening them will be slow.
5557 What we do is basically process one input file at a time. We do
5558 everything we need to do with an input file once--copy over the
5559 section contents, handle the relocation information, and write
5560 out the symbols--and then we throw away the information we read
5561 from it. This approach requires a lot of lseeks of the output
5562 file, which is unfortunate but still faster than reopening a lot
5565 We use the output_has_begun field of the input BFDs to see
5566 whether we have already handled it. */
5567 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
5568 sub->output_has_begun = FALSE;
5570 /* Mark all sections which are to be included in the link. This
5571 will normally be every section. We need to do this so that we
5572 can identify any sections which the linker has decided to not
5574 for (o = abfd->sections; o != NULL; o = o->next)
5576 for (p = o->map_head.link_order; p != NULL; p = p->next)
5577 if (p->type == bfd_indirect_link_order)
5578 p->u.indirect.section->linker_mark = TRUE;
5581 have_link_order_relocs = FALSE;
5582 for (o = abfd->sections; o != NULL; o = o->next)
5584 for (p = o->map_head.link_order;
5588 if (p->type == bfd_indirect_link_order
5589 && (bfd_get_flavour (p->u.indirect.section->owner)
5590 == bfd_target_aout_flavour))
5594 input_bfd = p->u.indirect.section->owner;
5595 if (! input_bfd->output_has_begun)
5597 if (! aout_link_input_bfd (&aout_info, input_bfd))
5599 input_bfd->output_has_begun = TRUE;
5602 else if (p->type == bfd_section_reloc_link_order
5603 || p->type == bfd_symbol_reloc_link_order)
5605 /* These are handled below. */
5606 have_link_order_relocs = TRUE;
5610 if (! _bfd_default_link_order (abfd, info, o, p))
5616 /* Write out any symbols that we have not already written out. */
5617 bfd_hash_traverse (&info->hash->table,
5618 aout_link_write_other_symbol,
5621 /* Now handle any relocs we were asked to create by the linker.
5622 These did not come from any input file. We must do these after
5623 we have written out all the symbols, so that we know the symbol
5625 if (have_link_order_relocs)
5627 for (o = abfd->sections; o != NULL; o = o->next)
5629 for (p = o->map_head.link_order;
5633 if (p->type == bfd_section_reloc_link_order
5634 || p->type == bfd_symbol_reloc_link_order)
5636 if (! aout_link_reloc_link_order (&aout_info, o, p))
5643 if (aout_info.contents != NULL)
5645 free (aout_info.contents);
5646 aout_info.contents = NULL;
5648 if (aout_info.relocs != NULL)
5650 free (aout_info.relocs);
5651 aout_info.relocs = NULL;
5653 if (aout_info.symbol_map != NULL)
5655 free (aout_info.symbol_map);
5656 aout_info.symbol_map = NULL;
5658 if (aout_info.output_syms != NULL)
5660 free (aout_info.output_syms);
5661 aout_info.output_syms = NULL;
5663 if (includes_hash_initialized)
5665 bfd_hash_table_free (&aout_info.includes.root);
5666 includes_hash_initialized = FALSE;
5669 /* Finish up any dynamic linking we may be doing. */
5670 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
5672 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
5676 /* Update the header information. */
5677 abfd->symcount = obj_aout_external_sym_count (abfd);
5678 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
5679 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
5680 obj_textsec (abfd)->reloc_count =
5681 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
5682 obj_datasec (abfd)->reloc_count =
5683 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
5685 /* Write out the string table, unless there are no symbols. */
5686 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0)
5688 if (abfd->symcount > 0)
5690 if (!emit_stringtab (abfd, aout_info.strtab))
5695 bfd_byte b[BYTES_IN_WORD];
5697 memset (b, 0, BYTES_IN_WORD);
5698 if (bfd_bwrite (b, (bfd_size_type) BYTES_IN_WORD, abfd) != BYTES_IN_WORD)
5705 if (aout_info.contents != NULL)
5706 free (aout_info.contents);
5707 if (aout_info.relocs != NULL)
5708 free (aout_info.relocs);
5709 if (aout_info.symbol_map != NULL)
5710 free (aout_info.symbol_map);
5711 if (aout_info.output_syms != NULL)
5712 free (aout_info.output_syms);
5713 if (includes_hash_initialized)
5714 bfd_hash_table_free (&aout_info.includes.root);