1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
3 Written by Cygnus Support.
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 2 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., 675 Mass Ave, Cambridge, MA 02139, 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, 386
37 and 29k a.out files, to create a target jump vector for a
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
69 | #define TARGET_NAME "a.out-sunos-big"
70 | #define VECNAME sunos_big_vec
73 requires all the names from @file{aout32.c}, and produces the jump vector
77 The file @file{host-aout.c} is a special case. It is for a large set
78 of hosts that use ``more or less standard'' a.out files, and
79 for which cross-debugging is not interesting. It uses the
80 standard 32-bit a.out support routines, but determines the
81 file offsets and addresses of the text, data, and BSS
82 sections, the machine architecture and machine type, and the
83 entry point address, in a host-dependent manner. Once these
84 values have been determined, generic code is used to handle
87 When porting it to run on a new system, you must supply:
91 | HOST_MACHINE_ARCH (optional)
92 | HOST_MACHINE_MACHINE (optional)
93 | HOST_TEXT_START_ADDR
96 in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These
97 values, plus the structures and macros defined in @file{a.out.h} on
98 your host system, will produce a BFD target that will access
99 ordinary a.out files on your host. To configure a new machine
100 to use @file{host-aout.c}, specify:
102 | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
103 | TDEPFILES= host-aout.o trad-core.o
105 in the @file{config/@var{XXX}.mt} file, and modify @file{configure.in}
107 @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
108 configuration is selected.
113 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
114 Doesn't matter what the setting of WP_TEXT is on output, but it'll
116 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
117 * Any BFD with both flags clear is OMAGIC.
118 (Just want to make these explicit, so the conditions tested in this
119 file make sense if you're more familiar with a.out than with BFD.) */
122 #define KEEPITTYPE int
124 #include <string.h> /* For strchr and friends */
131 #include "aout/aout64.h"
132 #include "aout/stab_gnu.h"
135 static boolean aout_get_external_symbols
PARAMS ((bfd
*));
136 static boolean translate_from_native_sym_flags
137 PARAMS ((bfd
*, aout_symbol_type
*));
138 static boolean translate_to_native_sym_flags
139 PARAMS ((bfd
*, asymbol
*, struct external_nlist
*));
146 The file @file{aoutx.h} provides for both the @emph{standard}
147 and @emph{extended} forms of a.out relocation records.
149 The standard records contain only an
150 address, a symbol index, and a type field. The extended records
151 (used on 29ks and sparcs) also have a full integer for an
155 #define CTOR_TABLE_RELOC_IDX 2
157 #define howto_table_ext NAME(aout,ext_howto_table)
158 #define howto_table_std NAME(aout,std_howto_table)
160 reloc_howto_type howto_table_ext
[] =
162 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
163 HOWTO(RELOC_8
, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", false, 0,0x000000ff, false),
164 HOWTO(RELOC_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", false, 0,0x0000ffff, false),
165 HOWTO(RELOC_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", false, 0,0xffffffff, false),
166 HOWTO(RELOC_DISP8
, 0, 0, 8, true, 0, complain_overflow_signed
,0,"DISP8", false, 0,0x000000ff, false),
167 HOWTO(RELOC_DISP16
, 0, 1, 16, true, 0, complain_overflow_signed
,0,"DISP16", false, 0,0x0000ffff, false),
168 HOWTO(RELOC_DISP32
, 0, 2, 32, true, 0, complain_overflow_signed
,0,"DISP32", false, 0,0xffffffff, false),
169 HOWTO(RELOC_WDISP30
,2, 2, 30, true, 0, complain_overflow_signed
,0,"WDISP30", false, 0,0x3fffffff, false),
170 HOWTO(RELOC_WDISP22
,2, 2, 22, true, 0, complain_overflow_signed
,0,"WDISP22", false, 0,0x003fffff, false),
171 HOWTO(RELOC_HI22
, 10, 2, 22, false, 0, complain_overflow_bitfield
,0,"HI22", false, 0,0x003fffff, false),
172 HOWTO(RELOC_22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"22", false, 0,0x003fffff, false),
173 HOWTO(RELOC_13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"13", false, 0,0x00001fff, false),
174 HOWTO(RELOC_LO10
, 0, 2, 10, false, 0, complain_overflow_dont
,0,"LO10", false, 0,0x000003ff, false),
175 HOWTO(RELOC_SFA_BASE
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_BASE", false, 0,0xffffffff, false),
176 HOWTO(RELOC_SFA_OFF13
,0,2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_OFF13",false, 0,0xffffffff, false),
177 HOWTO(RELOC_BASE10
, 0, 2, 16, false, 0, complain_overflow_bitfield
,0,"BASE10", false, 0,0x0000ffff, false),
178 HOWTO(RELOC_BASE13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"BASE13", false, 0,0x00001fff, false),
179 HOWTO(RELOC_BASE22
, 0, 2, 0, false, 0, complain_overflow_bitfield
,0,"BASE22", false, 0,0x00000000, false),
180 HOWTO(RELOC_PC10
, 0, 2, 10, false, 0, complain_overflow_bitfield
,0,"PC10", false, 0,0x000003ff, false),
181 HOWTO(RELOC_PC22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"PC22", false, 0,0x003fffff, false),
182 HOWTO(RELOC_JMP_TBL
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"JMP_TBL", false, 0,0xffffffff, false),
183 HOWTO(RELOC_SEGOFF16
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"SEGOFF16", false, 0,0x00000000, false),
184 HOWTO(RELOC_GLOB_DAT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"GLOB_DAT", false, 0,0x00000000, false),
185 HOWTO(RELOC_JMP_SLOT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"JMP_SLOT", false, 0,0x00000000, false),
186 HOWTO(RELOC_RELATIVE
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"RELATIVE", false, 0,0x00000000, false),
189 /* Convert standard reloc records to "arelent" format (incl byte swap). */
191 reloc_howto_type howto_table_std
[] = {
192 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
193 HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", true, 0x000000ff,0x000000ff, false),
194 HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", true, 0x0000ffff,0x0000ffff, false),
195 HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", true, 0xffffffff,0xffffffff, false),
196 HOWTO( 3, 0, 4, 64, false, 0, complain_overflow_bitfield
,0,"64", true, 0xdeaddead,0xdeaddead, false),
197 HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed
, 0,"DISP8", true, 0x000000ff,0x000000ff, false),
198 HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed
, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
199 HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed
, 0,"DISP32", true, 0xffffffff,0xffffffff, false),
200 HOWTO( 7, 0, 4, 64, true, 0, complain_overflow_signed
, 0,"DISP64", true, 0xfeedface,0xfeedface, false),
202 HOWTO( 9, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"BASE16", false,0xffffffff,0xffffffff, false),
203 HOWTO(10, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"BASE32", false,0xffffffff,0xffffffff, false),
209 HOWTO(16, 0, 2, 0, false, 0, complain_overflow_bitfield
,0,"JMP_TABLE", false, 0,0x00000000, false),
217 { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 }, { -1 },
218 HOWTO(32, 0, 2, 0, false, 0, complain_overflow_bitfield
,0,"RELATIVE", false, 0,0x00000000, false),
226 HOWTO(40, 0, 2, 0, false, 0, complain_overflow_bitfield
,0,"BASEREL", false, 0,0x00000000, false),
229 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
231 CONST
struct reloc_howto_struct
*
232 NAME(aout
,reloc_type_lookup
) (abfd
,code
)
234 bfd_reloc_code_real_type code
;
236 #define EXT(i,j) case i: return &howto_table_ext[j]
237 #define STD(i,j) case i: return &howto_table_std[j]
238 int ext
= obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
;
239 if (code
== BFD_RELOC_CTOR
)
240 switch (bfd_get_arch_info (abfd
)->bits_per_address
)
252 EXT (BFD_RELOC_32
, 2);
253 EXT (BFD_RELOC_HI22
, 8);
254 EXT (BFD_RELOC_LO10
, 11);
255 EXT (BFD_RELOC_32_PCREL_S2
, 6);
256 EXT (BFD_RELOC_SPARC_WDISP22
, 7);
257 EXT (BFD_RELOC_SPARC13
, 10);
258 EXT (BFD_RELOC_SPARC_BASE13
, 15);
259 default: return (CONST
struct reloc_howto_struct
*) 0;
265 STD (BFD_RELOC_16
, 1);
266 STD (BFD_RELOC_32
, 2);
267 STD (BFD_RELOC_8_PCREL
, 4);
268 STD (BFD_RELOC_16_PCREL
, 5);
269 STD (BFD_RELOC_32_PCREL
, 6);
270 STD (BFD_RELOC_16_BASEREL
, 9);
271 STD (BFD_RELOC_32_BASEREL
, 10);
272 default: return (CONST
struct reloc_howto_struct
*) 0;
278 Internal entry points
281 @file{aoutx.h} exports several routines for accessing the
282 contents of an a.out file, which are gathered and exported in
283 turn by various format specific files (eg sunos.c).
289 aout_@var{size}_swap_exec_header_in
292 void aout_@var{size}_swap_exec_header_in,
294 struct external_exec *raw_bytes,
295 struct internal_exec *execp);
298 Swap the information in an executable header @var{raw_bytes} taken
299 from a raw byte stream memory image into the internal exec header
300 structure @var{execp}.
303 #ifndef NAME_swap_exec_header_in
305 NAME(aout
,swap_exec_header_in
) (abfd
, raw_bytes
, execp
)
307 struct external_exec
*raw_bytes
;
308 struct internal_exec
*execp
;
310 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
312 /* The internal_exec structure has some fields that are unused in this
313 configuration (IE for i960), so ensure that all such uninitialized
314 fields are zero'd out. There are places where two of these structs
315 are memcmp'd, and thus the contents do matter. */
316 memset ((PTR
) execp
, 0, sizeof (struct internal_exec
));
317 /* Now fill in fields in the execp, from the bytes in the raw data. */
318 execp
->a_info
= bfd_h_get_32 (abfd
, bytes
->e_info
);
319 execp
->a_text
= GET_WORD (abfd
, bytes
->e_text
);
320 execp
->a_data
= GET_WORD (abfd
, bytes
->e_data
);
321 execp
->a_bss
= GET_WORD (abfd
, bytes
->e_bss
);
322 execp
->a_syms
= GET_WORD (abfd
, bytes
->e_syms
);
323 execp
->a_entry
= GET_WORD (abfd
, bytes
->e_entry
);
324 execp
->a_trsize
= GET_WORD (abfd
, bytes
->e_trsize
);
325 execp
->a_drsize
= GET_WORD (abfd
, bytes
->e_drsize
);
327 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
332 aout_@var{size}_swap_exec_header_out
335 void aout_@var{size}_swap_exec_header_out
337 struct internal_exec *execp,
338 struct external_exec *raw_bytes);
341 Swap the information in an internal exec header structure
342 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
345 NAME(aout
,swap_exec_header_out
) (abfd
, execp
, raw_bytes
)
347 struct internal_exec
*execp
;
348 struct external_exec
*raw_bytes
;
350 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
352 /* Now fill in fields in the raw data, from the fields in the exec struct. */
353 bfd_h_put_32 (abfd
, execp
->a_info
, bytes
->e_info
);
354 PUT_WORD (abfd
, execp
->a_text
, bytes
->e_text
);
355 PUT_WORD (abfd
, execp
->a_data
, bytes
->e_data
);
356 PUT_WORD (abfd
, execp
->a_bss
, bytes
->e_bss
);
357 PUT_WORD (abfd
, execp
->a_syms
, bytes
->e_syms
);
358 PUT_WORD (abfd
, execp
->a_entry
, bytes
->e_entry
);
359 PUT_WORD (abfd
, execp
->a_trsize
, bytes
->e_trsize
);
360 PUT_WORD (abfd
, execp
->a_drsize
, bytes
->e_drsize
);
363 /* Make all the section for an a.out file. */
366 NAME(aout
,make_sections
) (abfd
)
369 if (obj_textsec (abfd
) == (asection
*) NULL
370 && bfd_make_section (abfd
, ".text") == (asection
*) NULL
)
372 if (obj_datasec (abfd
) == (asection
*) NULL
373 && bfd_make_section (abfd
, ".data") == (asection
*) NULL
)
375 if (obj_bsssec (abfd
) == (asection
*) NULL
376 && bfd_make_section (abfd
, ".bss") == (asection
*) NULL
)
383 aout_@var{size}_some_aout_object_p
386 bfd_target *aout_@var{size}_some_aout_object_p
388 bfd_target *(*callback_to_real_object_p)());
391 Some a.out variant thinks that the file open in @var{abfd}
392 checking is an a.out file. Do some more checking, and set up
393 for access if it really is. Call back to the calling
394 environment's "finish up" function just before returning, to
395 handle any last-minute setup.
399 NAME(aout
,some_aout_object_p
) (abfd
, execp
, callback_to_real_object_p
)
401 struct internal_exec
*execp
;
402 bfd_target
*(*callback_to_real_object_p
) PARAMS ((bfd
*));
404 struct aout_data_struct
*rawptr
, *oldrawptr
;
407 rawptr
= (struct aout_data_struct
*) bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
408 if (rawptr
== NULL
) {
409 bfd_set_error (bfd_error_no_memory
);
413 oldrawptr
= abfd
->tdata
.aout_data
;
414 abfd
->tdata
.aout_data
= rawptr
;
416 /* Copy the contents of the old tdata struct.
417 In particular, we want the subformat, since for hpux it was set in
418 hp300hpux.c:swap_exec_header_in and will be used in
419 hp300hpux.c:callback. */
420 if (oldrawptr
!= NULL
)
421 *abfd
->tdata
.aout_data
= *oldrawptr
;
423 abfd
->tdata
.aout_data
->a
.hdr
= &rawptr
->e
;
424 *(abfd
->tdata
.aout_data
->a
.hdr
) = *execp
; /* Copy in the internal_exec struct */
425 execp
= abfd
->tdata
.aout_data
->a
.hdr
;
427 /* Set the file flags */
428 abfd
->flags
= NO_FLAGS
;
429 if (execp
->a_drsize
|| execp
->a_trsize
)
430 abfd
->flags
|= HAS_RELOC
;
431 /* Setting of EXEC_P has been deferred to the bottom of this function */
433 abfd
->flags
|= HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
;
434 if (N_DYNAMIC(*execp
))
435 abfd
->flags
|= DYNAMIC
;
437 if (N_MAGIC (*execp
) == ZMAGIC
)
439 abfd
->flags
|= D_PAGED
|WP_TEXT
;
440 adata(abfd
).magic
= z_magic
;
442 else if (N_MAGIC (*execp
) == NMAGIC
)
444 abfd
->flags
|= WP_TEXT
;
445 adata(abfd
).magic
= n_magic
;
448 adata(abfd
).magic
= o_magic
;
450 bfd_get_start_address (abfd
) = execp
->a_entry
;
452 obj_aout_symbols (abfd
) = (aout_symbol_type
*)NULL
;
453 bfd_get_symcount (abfd
) = execp
->a_syms
/ sizeof (struct external_nlist
);
455 /* The default relocation entry size is that of traditional V7 Unix. */
456 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
458 /* The default symbol entry size is that of traditional Unix. */
459 obj_symbol_entry_size (abfd
) = EXTERNAL_NLIST_SIZE
;
461 obj_aout_external_syms (abfd
) = NULL
;
462 obj_aout_external_strings (abfd
) = NULL
;
463 obj_aout_sym_hashes (abfd
) = NULL
;
465 if (! NAME(aout
,make_sections
) (abfd
))
468 obj_datasec (abfd
)->_raw_size
= execp
->a_data
;
469 obj_bsssec (abfd
)->_raw_size
= execp
->a_bss
;
471 /* If this object is dynamically linked, we assume that both
472 sections have relocs. This does no real harm, even though it may
474 obj_textsec (abfd
)->flags
=
475 (execp
->a_trsize
!= 0 || (abfd
->flags
& DYNAMIC
) != 0
476 ? (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
| SEC_RELOC
)
477 : (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
));
478 obj_datasec (abfd
)->flags
=
479 (execp
->a_drsize
!= 0 || (abfd
->flags
& DYNAMIC
) != 0
480 ? (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_RELOC
)
481 : (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
));
482 obj_bsssec (abfd
)->flags
= SEC_ALLOC
;
484 #ifdef THIS_IS_ONLY_DOCUMENTATION
485 /* The common code can't fill in these things because they depend
486 on either the start address of the text segment, the rounding
487 up of virtual addresses between segments, or the starting file
488 position of the text segment -- all of which varies among different
489 versions of a.out. */
491 /* Call back to the format-dependent code to fill in the rest of the
492 fields and do any further cleanup. Things that should be filled
493 in by the callback: */
495 struct exec
*execp
= exec_hdr (abfd
);
497 obj_textsec (abfd
)->size
= N_TXTSIZE(*execp
);
498 obj_textsec (abfd
)->raw_size
= N_TXTSIZE(*execp
);
499 /* data and bss are already filled in since they're so standard */
501 /* The virtual memory addresses of the sections */
502 obj_textsec (abfd
)->vma
= N_TXTADDR(*execp
);
503 obj_datasec (abfd
)->vma
= N_DATADDR(*execp
);
504 obj_bsssec (abfd
)->vma
= N_BSSADDR(*execp
);
506 /* The file offsets of the sections */
507 obj_textsec (abfd
)->filepos
= N_TXTOFF(*execp
);
508 obj_datasec (abfd
)->filepos
= N_DATOFF(*execp
);
510 /* The file offsets of the relocation info */
511 obj_textsec (abfd
)->rel_filepos
= N_TRELOFF(*execp
);
512 obj_datasec (abfd
)->rel_filepos
= N_DRELOFF(*execp
);
514 /* The file offsets of the string table and symbol table. */
515 obj_str_filepos (abfd
) = N_STROFF (*execp
);
516 obj_sym_filepos (abfd
) = N_SYMOFF (*execp
);
518 /* Determine the architecture and machine type of the object file. */
519 switch (N_MACHTYPE (*exec_hdr (abfd
))) {
521 abfd
->obj_arch
= bfd_arch_obscure
;
525 adata(abfd
)->page_size
= PAGE_SIZE
;
526 adata(abfd
)->segment_size
= SEGMENT_SIZE
;
527 adata(abfd
)->exec_bytes_size
= EXEC_BYTES_SIZE
;
531 /* The architecture is encoded in various ways in various a.out variants,
532 or is not encoded at all in some of them. The relocation size depends
533 on the architecture and the a.out variant. Finally, the return value
534 is the bfd_target vector in use. If an error occurs, return zero and
535 set bfd_error to the appropriate error code.
537 Formats such as b.out, which have additional fields in the a.out
538 header, should cope with them in this callback as well. */
539 #endif /* DOCUMENTATION */
541 result
= (*callback_to_real_object_p
)(abfd
);
543 /* Now that the segment addresses have been worked out, take a better
544 guess at whether the file is executable. If the entry point
545 is within the text segment, assume it is. (This makes files
546 executable even if their entry point address is 0, as long as
547 their text starts at zero.)
549 At some point we should probably break down and stat the file and
550 declare it executable if (one of) its 'x' bits are on... */
551 if ((execp
->a_entry
>= obj_textsec(abfd
)->vma
) &&
552 (execp
->a_entry
< obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
))
553 abfd
->flags
|= EXEC_P
;
556 #if 0 /* These should be set correctly anyways. */
557 abfd
->sections
= obj_textsec (abfd
);
558 obj_textsec (abfd
)->next
= obj_datasec (abfd
);
559 obj_datasec (abfd
)->next
= obj_bsssec (abfd
);
565 abfd
->tdata
.aout_data
= oldrawptr
;
572 aout_@var{size}_mkobject
575 boolean aout_@var{size}_mkobject, (bfd *abfd);
578 Initialize BFD @var{abfd} for use with a.out files.
582 NAME(aout
,mkobject
) (abfd
)
585 struct aout_data_struct
*rawptr
;
587 bfd_set_error (bfd_error_system_call
);
589 /* Use an intermediate variable for clarity */
590 rawptr
= (struct aout_data_struct
*)bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
592 if (rawptr
== NULL
) {
593 bfd_set_error (bfd_error_no_memory
);
597 abfd
->tdata
.aout_data
= rawptr
;
598 exec_hdr (abfd
) = &(rawptr
->e
);
600 obj_textsec (abfd
) = (asection
*)NULL
;
601 obj_datasec (abfd
) = (asection
*)NULL
;
602 obj_bsssec (abfd
) = (asection
*)NULL
;
610 aout_@var{size}_machine_type
613 enum machine_type aout_@var{size}_machine_type
614 (enum bfd_architecture arch,
615 unsigned long machine));
618 Keep track of machine architecture and machine type for
619 a.out's. Return the <<machine_type>> for a particular
620 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
621 and machine can't be represented in a.out format.
623 If the architecture is understood, machine type 0 (default)
624 is always understood.
628 NAME(aout
,machine_type
) (arch
, machine
, unknown
)
629 enum bfd_architecture arch
;
630 unsigned long machine
;
633 enum machine_type arch_flags
;
635 arch_flags
= M_UNKNOWN
;
640 if (machine
== 0) arch_flags
= M_SPARC
;
645 case 0: arch_flags
= M_68010
; break;
646 case 68000: arch_flags
= M_UNKNOWN
; *unknown
= false; break;
647 case 68010: arch_flags
= M_68010
; break;
648 case 68020: arch_flags
= M_68020
; break;
649 default: arch_flags
= M_UNKNOWN
; break;
654 if (machine
== 0) arch_flags
= M_386
;
658 if (machine
== 0) arch_flags
= M_29K
;
665 case 3000: arch_flags
= M_MIPS1
; break;
668 case 6000: arch_flags
= M_MIPS2
; break;
669 default: arch_flags
= M_UNKNOWN
; break;
674 arch_flags
= M_UNKNOWN
;
677 if (arch_flags
!= M_UNKNOWN
)
686 aout_@var{size}_set_arch_mach
689 boolean aout_@var{size}_set_arch_mach,
691 enum bfd_architecture arch,
692 unsigned long machine));
695 Set the architecture and the machine of the BFD @var{abfd} to the
696 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
697 can support the architecture required.
701 NAME(aout
,set_arch_mach
) (abfd
, arch
, machine
)
703 enum bfd_architecture arch
;
704 unsigned long machine
;
706 if (! bfd_default_set_arch_mach (abfd
, arch
, machine
))
709 if (arch
!= bfd_arch_unknown
)
713 NAME(aout
,machine_type
) (arch
, machine
, &unknown
);
718 /* Determine the size of a relocation entry */
723 obj_reloc_entry_size (abfd
) = RELOC_EXT_SIZE
;
726 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
730 return (*aout_backend_info(abfd
)->set_sizes
) (abfd
);
734 adjust_o_magic (abfd
, execp
)
736 struct internal_exec
*execp
;
738 file_ptr pos
= adata (abfd
).exec_bytes_size
;
743 obj_textsec(abfd
)->filepos
= pos
;
744 pos
+= obj_textsec(abfd
)->_raw_size
;
745 vma
+= obj_textsec(abfd
)->_raw_size
;
748 if (!obj_datasec(abfd
)->user_set_vma
)
750 #if 0 /* ?? Does alignment in the file image really matter? */
751 pad
= align_power (vma
, obj_datasec(abfd
)->alignment_power
) - vma
;
753 obj_textsec(abfd
)->_raw_size
+= pad
;
756 obj_datasec(abfd
)->vma
= vma
;
758 obj_datasec(abfd
)->filepos
= pos
;
759 pos
+= obj_datasec(abfd
)->_raw_size
;
760 vma
+= obj_datasec(abfd
)->_raw_size
;
763 if (!obj_bsssec(abfd
)->user_set_vma
)
766 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
768 obj_datasec(abfd
)->_raw_size
+= pad
;
771 obj_bsssec(abfd
)->vma
= vma
;
773 obj_bsssec(abfd
)->filepos
= pos
;
775 /* Fix up the exec header. */
776 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
777 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
778 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
779 N_SET_MAGIC (*execp
, OMAGIC
);
783 adjust_z_magic (abfd
, execp
)
785 struct internal_exec
*execp
;
787 bfd_size_type data_pad
, text_pad
;
789 CONST
struct aout_backend_data
*abdp
;
790 int ztih
; /* Nonzero if text includes exec header. */
792 abdp
= aout_backend_info (abfd
);
795 ztih
= abdp
&& abdp
->text_includes_header
;
796 obj_textsec(abfd
)->filepos
= (ztih
797 ? adata(abfd
).exec_bytes_size
798 : adata(abfd
).page_size
);
799 if (! obj_textsec(abfd
)->user_set_vma
)
800 /* ?? Do we really need to check for relocs here? */
801 obj_textsec(abfd
)->vma
= ((abfd
->flags
& HAS_RELOC
)
804 ? (abdp
->default_text_vma
805 + adata(abfd
).exec_bytes_size
)
806 : abdp
->default_text_vma
));
807 /* Could take strange alignment of text section into account here? */
809 /* Find start of data. */
810 text_end
= obj_textsec(abfd
)->filepos
+ obj_textsec(abfd
)->_raw_size
;
811 text_pad
= BFD_ALIGN (text_end
, adata(abfd
).page_size
) - text_end
;
812 obj_textsec(abfd
)->_raw_size
+= text_pad
;
813 text_end
+= text_pad
;
816 if (!obj_datasec(abfd
)->user_set_vma
)
819 vma
= obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
;
820 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
822 if (abdp
&& abdp
->zmagic_mapped_contiguous
)
824 text_pad
= (obj_datasec(abfd
)->vma
825 - obj_textsec(abfd
)->vma
826 - obj_textsec(abfd
)->_raw_size
);
827 obj_textsec(abfd
)->_raw_size
+= text_pad
;
829 obj_datasec(abfd
)->filepos
= (obj_textsec(abfd
)->filepos
830 + obj_textsec(abfd
)->_raw_size
);
832 /* Fix up exec header while we're at it. */
833 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
834 if (ztih
&& (!abdp
|| (abdp
&& !abdp
->exec_header_not_counted
)))
835 execp
->a_text
+= adata(abfd
).exec_bytes_size
;
836 N_SET_MAGIC (*execp
, ZMAGIC
);
838 /* Spec says data section should be rounded up to page boundary. */
839 obj_datasec(abfd
)->_raw_size
840 = align_power (obj_datasec(abfd
)->_raw_size
,
841 obj_bsssec(abfd
)->alignment_power
);
842 execp
->a_data
= BFD_ALIGN (obj_datasec(abfd
)->_raw_size
,
843 adata(abfd
).page_size
);
844 data_pad
= execp
->a_data
- obj_datasec(abfd
)->_raw_size
;
847 if (!obj_bsssec(abfd
)->user_set_vma
)
848 obj_bsssec(abfd
)->vma
= (obj_datasec(abfd
)->vma
849 + obj_datasec(abfd
)->_raw_size
);
850 /* If the BSS immediately follows the data section and extra space
851 in the page is left after the data section, fudge data
852 in the header so that the bss section looks smaller by that
853 amount. We'll start the bss section there, and lie to the OS.
854 (Note that a linker script, as well as the above assignment,
855 could have explicitly set the BSS vma to immediately follow
856 the data section.) */
857 if (align_power (obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->alignment_power
)
858 == obj_datasec(abfd
)->vma
+ obj_datasec(abfd
)->_raw_size
)
859 execp
->a_bss
= (data_pad
> obj_bsssec(abfd
)->_raw_size
) ? 0 :
860 obj_bsssec(abfd
)->_raw_size
- data_pad
;
862 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
866 adjust_n_magic (abfd
, execp
)
868 struct internal_exec
*execp
;
870 file_ptr pos
= adata(abfd
).exec_bytes_size
;
875 obj_textsec(abfd
)->filepos
= pos
;
876 if (!obj_textsec(abfd
)->user_set_vma
)
877 obj_textsec(abfd
)->vma
= vma
;
879 vma
= obj_textsec(abfd
)->vma
;
880 pos
+= obj_textsec(abfd
)->_raw_size
;
881 vma
+= obj_textsec(abfd
)->_raw_size
;
884 obj_datasec(abfd
)->filepos
= pos
;
885 if (!obj_datasec(abfd
)->user_set_vma
)
886 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
887 vma
= obj_datasec(abfd
)->vma
;
889 /* Since BSS follows data immediately, see if it needs alignment. */
890 vma
+= obj_datasec(abfd
)->_raw_size
;
891 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
892 obj_datasec(abfd
)->_raw_size
+= pad
;
893 pos
+= obj_datasec(abfd
)->_raw_size
;
896 if (!obj_bsssec(abfd
)->user_set_vma
)
897 obj_bsssec(abfd
)->vma
= vma
;
899 vma
= obj_bsssec(abfd
)->vma
;
901 /* Fix up exec header. */
902 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
903 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
904 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
905 N_SET_MAGIC (*execp
, NMAGIC
);
909 NAME(aout
,adjust_sizes_and_vmas
) (abfd
, text_size
, text_end
)
911 bfd_size_type
*text_size
;
914 struct internal_exec
*execp
= exec_hdr (abfd
);
916 if (! NAME(aout
,make_sections
) (abfd
))
919 if (adata(abfd
).magic
!= undecided_magic
) return true;
921 obj_textsec(abfd
)->_raw_size
=
922 align_power(obj_textsec(abfd
)->_raw_size
,
923 obj_textsec(abfd
)->alignment_power
);
925 *text_size
= obj_textsec (abfd
)->_raw_size
;
926 /* Rule (heuristic) for when to pad to a new page. Note that there
927 are (at least) two ways demand-paged (ZMAGIC) files have been
928 handled. Most Berkeley-based systems start the text segment at
929 (PAGE_SIZE). However, newer versions of SUNOS start the text
930 segment right after the exec header; the latter is counted in the
931 text segment size, and is paged in by the kernel with the rest of
934 /* This perhaps isn't the right way to do this, but made it simpler for me
935 to understand enough to implement it. Better would probably be to go
936 right from BFD flags to alignment/positioning characteristics. But the
937 old code was sloppy enough about handling the flags, and had enough
938 other magic, that it was a little hard for me to understand. I think
939 I understand it better now, but I haven't time to do the cleanup this
942 if (abfd
->flags
& D_PAGED
)
943 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
944 /* @@ What about QMAGIC? */
945 adata(abfd
).magic
= z_magic
;
946 else if (abfd
->flags
& WP_TEXT
)
947 adata(abfd
).magic
= n_magic
;
949 adata(abfd
).magic
= o_magic
;
951 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
953 fprintf (stderr
, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
955 switch (adata(abfd
).magic
) {
956 case n_magic
: str
= "NMAGIC"; break;
957 case o_magic
: str
= "OMAGIC"; break;
958 case z_magic
: str
= "ZMAGIC"; break;
963 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
,
964 obj_textsec(abfd
)->alignment_power
,
965 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
,
966 obj_datasec(abfd
)->alignment_power
,
967 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
,
968 obj_bsssec(abfd
)->alignment_power
);
972 switch (adata(abfd
).magic
)
975 adjust_o_magic (abfd
, execp
);
978 adjust_z_magic (abfd
, execp
);
981 adjust_n_magic (abfd
, execp
);
987 #ifdef BFD_AOUT_DEBUG
988 fprintf (stderr
, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
989 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
,
990 obj_textsec(abfd
)->filepos
,
991 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
,
992 obj_datasec(abfd
)->filepos
,
993 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
);
1001 aout_@var{size}_new_section_hook
1004 boolean aout_@var{size}_new_section_hook,
1006 asection *newsect));
1009 Called by the BFD in response to a @code{bfd_make_section}
1013 NAME(aout
,new_section_hook
) (abfd
, newsect
)
1017 /* align to double at least */
1018 newsect
->alignment_power
= bfd_get_arch_info(abfd
)->section_align_power
;
1021 if (bfd_get_format (abfd
) == bfd_object
)
1023 if (obj_textsec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".text")) {
1024 obj_textsec(abfd
)= newsect
;
1025 newsect
->target_index
= N_TEXT
;
1029 if (obj_datasec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".data")) {
1030 obj_datasec(abfd
) = newsect
;
1031 newsect
->target_index
= N_DATA
;
1035 if (obj_bsssec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".bss")) {
1036 obj_bsssec(abfd
) = newsect
;
1037 newsect
->target_index
= N_BSS
;
1043 /* We allow more than three sections internally */
1048 NAME(aout
,set_section_contents
) (abfd
, section
, location
, offset
, count
)
1053 bfd_size_type count
;
1056 bfd_size_type text_size
;
1058 if (abfd
->output_has_begun
== false)
1060 if (NAME(aout
,adjust_sizes_and_vmas
) (abfd
,
1062 &text_end
) == false)
1066 /* regardless, once we know what we're doing, we might as well get going */
1067 if (section
!= obj_bsssec(abfd
))
1069 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0)
1073 return (bfd_write ((PTR
)location
, 1, count
, abfd
) == count
) ?
1081 /* Read the external symbols from an a.out file. */
1084 aout_get_external_symbols (abfd
)
1087 if (obj_aout_external_syms (abfd
) == (struct external_nlist
*) NULL
)
1089 bfd_size_type count
;
1090 struct external_nlist
*syms
;
1092 count
= exec_hdr (abfd
)->a_syms
/ EXTERNAL_NLIST_SIZE
;
1094 /* We allocate using malloc to make the values easy to free
1095 later on. If we put them on the obstack it might not be
1096 possible to free them. */
1097 syms
= ((struct external_nlist
*)
1098 malloc ((size_t) count
* EXTERNAL_NLIST_SIZE
));
1099 if (syms
== (struct external_nlist
*) NULL
&& count
!= 0)
1101 bfd_set_error (bfd_error_no_memory
);
1105 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1106 || (bfd_read (syms
, 1, exec_hdr (abfd
)->a_syms
, abfd
)
1107 != exec_hdr (abfd
)->a_syms
))
1113 obj_aout_external_syms (abfd
) = syms
;
1114 obj_aout_external_sym_count (abfd
) = count
;
1117 if (obj_aout_external_strings (abfd
) == NULL
1118 && exec_hdr (abfd
)->a_syms
!= 0)
1120 unsigned char string_chars
[BYTES_IN_WORD
];
1121 bfd_size_type stringsize
;
1124 /* Get the size of the strings. */
1125 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0
1126 || (bfd_read ((PTR
) string_chars
, BYTES_IN_WORD
, 1, abfd
)
1129 stringsize
= GET_WORD (abfd
, string_chars
);
1131 strings
= (char *) malloc ((size_t) stringsize
+ 1);
1132 if (strings
== NULL
)
1134 bfd_set_error (bfd_error_no_memory
);
1138 /* Skip space for the string count in the buffer for convenience
1139 when using indexes. */
1140 if (bfd_read (strings
+ BYTES_IN_WORD
, 1, stringsize
- BYTES_IN_WORD
,
1142 != stringsize
- BYTES_IN_WORD
)
1148 /* Sanity preservation. */
1149 strings
[stringsize
] = '\0';
1151 obj_aout_external_strings (abfd
) = strings
;
1152 obj_aout_external_string_size (abfd
) = stringsize
;
1158 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1159 and symbol->value fields of CACHE_PTR will be set from the a.out
1160 nlist structure. This function is responsible for setting
1161 symbol->flags and symbol->section, and adjusting symbol->value. */
1164 translate_from_native_sym_flags (abfd
, cache_ptr
)
1166 aout_symbol_type
*cache_ptr
;
1170 if ((cache_ptr
->type
& N_STAB
) != 0
1171 || cache_ptr
->type
== N_FN
)
1175 /* This is a debugging symbol. */
1177 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
;
1179 /* Work out the symbol section. */
1180 switch (cache_ptr
->type
& N_TYPE
)
1184 sec
= obj_textsec (abfd
);
1187 sec
= obj_datasec (abfd
);
1190 sec
= obj_bsssec (abfd
);
1194 sec
= &bfd_abs_section
;
1198 cache_ptr
->symbol
.section
= sec
;
1199 cache_ptr
->symbol
.value
-= sec
->vma
;
1204 /* Get the default visibility. This does not apply to all types, so
1205 we just hold it in a local variable to use if wanted. */
1206 if ((cache_ptr
->type
& N_EXT
) == 0)
1207 visible
= BSF_LOCAL
;
1209 visible
= BSF_GLOBAL
;
1211 switch (cache_ptr
->type
)
1214 case N_ABS
: case N_ABS
| N_EXT
:
1215 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1216 cache_ptr
->symbol
.flags
= visible
;
1219 case N_UNDF
| N_EXT
:
1220 if (cache_ptr
->symbol
.value
!= 0)
1222 /* This is a common symbol. */
1223 cache_ptr
->symbol
.flags
= BSF_GLOBAL
;
1224 cache_ptr
->symbol
.section
= &bfd_com_section
;
1228 cache_ptr
->symbol
.flags
= 0;
1229 cache_ptr
->symbol
.section
= &bfd_und_section
;
1233 case N_TEXT
: case N_TEXT
| N_EXT
:
1234 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1235 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1236 cache_ptr
->symbol
.flags
= visible
;
1239 case N_DATA
: case N_DATA
| N_EXT
:
1240 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1241 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1242 cache_ptr
->symbol
.flags
= visible
;
1245 case N_BSS
: case N_BSS
| N_EXT
:
1246 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1247 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1248 cache_ptr
->symbol
.flags
= visible
;
1251 case N_SETA
: case N_SETA
| N_EXT
:
1252 case N_SETT
: case N_SETT
| N_EXT
:
1253 case N_SETD
: case N_SETD
| N_EXT
:
1254 case N_SETB
: case N_SETB
| N_EXT
:
1257 arelent_chain
*reloc
;
1258 asection
*into_section
;
1260 /* This is a set symbol. The name of the symbol is the name
1261 of the set (e.g., __CTOR_LIST__). The value of the symbol
1262 is the value to add to the set. We create a section with
1263 the same name as the symbol, and add a reloc to insert the
1264 appropriate value into the section.
1266 This action is actually obsolete; it used to make the
1267 linker do the right thing, but the linker no longer uses
1270 section
= bfd_get_section_by_name (abfd
, cache_ptr
->symbol
.name
);
1271 if (section
== NULL
)
1275 copy
= bfd_alloc (abfd
, strlen (cache_ptr
->symbol
.name
) + 1);
1278 bfd_set_error (bfd_error_no_memory
);
1282 strcpy (copy
, cache_ptr
->symbol
.name
);
1283 section
= bfd_make_section (abfd
, copy
);
1284 if (section
== NULL
)
1288 reloc
= (arelent_chain
*) bfd_alloc (abfd
, sizeof (arelent_chain
));
1291 bfd_set_error (bfd_error_no_memory
);
1295 /* Build a relocation entry for the constructor. */
1296 switch (cache_ptr
->type
& N_TYPE
)
1299 into_section
= &bfd_abs_section
;
1300 cache_ptr
->type
= N_ABS
;
1303 into_section
= obj_textsec (abfd
);
1304 cache_ptr
->type
= N_TEXT
;
1307 into_section
= obj_datasec (abfd
);
1308 cache_ptr
->type
= N_DATA
;
1311 into_section
= obj_bsssec (abfd
);
1312 cache_ptr
->type
= N_BSS
;
1316 /* Build a relocation pointing into the constructor section
1317 pointing at the symbol in the set vector specified. */
1318 reloc
->relent
.addend
= cache_ptr
->symbol
.value
;
1319 cache_ptr
->symbol
.section
= into_section
;
1320 reloc
->relent
.sym_ptr_ptr
= into_section
->symbol_ptr_ptr
;
1322 /* We modify the symbol to belong to a section depending upon
1323 the name of the symbol, and add to the size of the section
1324 to contain a pointer to the symbol. Build a reloc entry to
1325 relocate to this symbol attached to this section. */
1326 section
->flags
= SEC_CONSTRUCTOR
| SEC_RELOC
;
1328 section
->reloc_count
++;
1329 section
->alignment_power
= 2;
1331 reloc
->next
= section
->constructor_chain
;
1332 section
->constructor_chain
= reloc
;
1333 reloc
->relent
.address
= section
->_raw_size
;
1334 section
->_raw_size
+= BYTES_IN_WORD
;
1336 if (obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
)
1337 reloc
->relent
.howto
= howto_table_ext
+ CTOR_TABLE_RELOC_IDX
;
1339 reloc
->relent
.howto
= howto_table_std
+ CTOR_TABLE_RELOC_IDX
;
1341 cache_ptr
->symbol
.flags
|= BSF_CONSTRUCTOR
;
1346 /* This symbol is the text of a warning message. The next
1347 symbol is the symbol to associate the warning with. If a
1348 reference is made to that symbol, a warning is issued. */
1349 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_WARNING
;
1351 /* @@ Stuffing pointers into integers is a no-no. We can
1352 usually get away with it if the integer is large enough
1354 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1356 cache_ptr
->symbol
.value
= (bfd_vma
) (cache_ptr
+ 1);
1358 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1362 case N_INDR
: case N_INDR
| N_EXT
:
1363 /* An indirect symbol. This consists of two symbols in a row.
1364 The first symbol is the name of the indirection. The second
1365 symbol is the name of the target. A reference to the first
1366 symbol becomes a reference to the second. */
1367 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_INDIRECT
| visible
;
1369 /* @@ Stuffing pointers into integers is a no-no. We can
1370 usually get away with it if the integer is large enough
1372 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1374 cache_ptr
->symbol
.value
= (bfd_vma
) (cache_ptr
+ 1);
1376 cache_ptr
->symbol
.section
= &bfd_ind_section
;
1381 cache_ptr
->symbol
.section
= &bfd_und_section
;
1382 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1386 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1387 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1391 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1392 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1393 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1397 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1398 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1399 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1403 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1404 cache_ptr
->symbol
.value
-= cache_ptr
->symbol
.section
->vma
;
1405 cache_ptr
->symbol
.flags
= BSF_WEAK
;
1412 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1415 translate_to_native_sym_flags (abfd
, cache_ptr
, sym_pointer
)
1418 struct external_nlist
*sym_pointer
;
1420 bfd_vma value
= cache_ptr
->value
;
1422 /* Mask out any existing type bits in case copying from one section
1424 sym_pointer
->e_type
[0] &= ~N_TYPE
;
1426 if (bfd_get_section (cache_ptr
) == &bfd_abs_section
)
1427 sym_pointer
->e_type
[0] |= N_ABS
;
1428 else if (bfd_get_section (cache_ptr
) == obj_textsec (abfd
))
1429 sym_pointer
->e_type
[0] |= N_TEXT
;
1430 else if (bfd_get_section (cache_ptr
) == obj_datasec (abfd
))
1431 sym_pointer
->e_type
[0] |= N_DATA
;
1432 else if (bfd_get_section (cache_ptr
) == obj_bsssec (abfd
))
1433 sym_pointer
->e_type
[0] |= N_BSS
;
1434 else if (bfd_get_section (cache_ptr
) == &bfd_und_section
)
1435 sym_pointer
->e_type
[0] = N_UNDF
| N_EXT
;
1436 else if (bfd_get_section (cache_ptr
) == &bfd_ind_section
)
1437 sym_pointer
->e_type
[0] = N_INDR
;
1438 else if (bfd_get_section (cache_ptr
) == NULL
)
1440 /* Protect the bfd_is_com_section call. This case occurs, e.g.,
1441 for the *DEBUG* section of a COFF file. */
1442 bfd_set_error (bfd_error_nonrepresentable_section
);
1445 else if (bfd_is_com_section (bfd_get_section (cache_ptr
)))
1446 sym_pointer
->e_type
[0] = N_UNDF
| N_EXT
;
1449 bfd_set_error (bfd_error_nonrepresentable_section
);
1453 /* Turn the symbol from section relative to absolute again */
1454 value
+= cache_ptr
->section
->vma
;
1456 if ((cache_ptr
->flags
& BSF_WARNING
) != 0)
1457 sym_pointer
->e_type
[0] = N_WARNING
;
1459 if ((cache_ptr
->flags
& BSF_DEBUGGING
) != 0)
1460 sym_pointer
->e_type
[0] = ((aout_symbol_type
*) cache_ptr
)->type
;
1461 else if ((cache_ptr
->flags
& BSF_GLOBAL
) != 0)
1462 sym_pointer
->e_type
[0] |= N_EXT
;
1464 if ((cache_ptr
->flags
& BSF_CONSTRUCTOR
) != 0)
1466 int type
= ((aout_symbol_type
*) cache_ptr
)->type
;
1469 case N_ABS
: type
= N_SETA
; break;
1470 case N_TEXT
: type
= N_SETT
; break;
1471 case N_DATA
: type
= N_SETD
; break;
1472 case N_BSS
: type
= N_SETB
; break;
1474 sym_pointer
->e_type
[0] = type
;
1477 if ((cache_ptr
->flags
& BSF_WEAK
) != 0)
1481 switch (sym_pointer
->e_type
[0] & N_TYPE
)
1484 case N_ABS
: type
= N_WEAKA
; break;
1485 case N_TEXT
: type
= N_WEAKT
; break;
1486 case N_DATA
: type
= N_WEAKD
; break;
1487 case N_BSS
: type
= N_WEAKB
; break;
1488 case N_UNDF
: type
= N_WEAKU
; break;
1490 sym_pointer
->e_type
[0] = type
;
1493 PUT_WORD(abfd
, value
, sym_pointer
->e_value
);
1498 /* Native-level interface to symbols. */
1501 NAME(aout
,make_empty_symbol
) (abfd
)
1504 aout_symbol_type
*new =
1505 (aout_symbol_type
*)bfd_zalloc (abfd
, sizeof (aout_symbol_type
));
1508 bfd_set_error (bfd_error_no_memory
);
1511 new->symbol
.the_bfd
= abfd
;
1513 return &new->symbol
;
1516 /* Translate a set of internal symbols into external symbols. */
1519 NAME(aout
,translate_symbol_table
) (abfd
, in
, ext
, count
, str
, strsize
, dynamic
)
1521 aout_symbol_type
*in
;
1522 struct external_nlist
*ext
;
1523 bfd_size_type count
;
1525 bfd_size_type strsize
;
1528 struct external_nlist
*ext_end
;
1530 ext_end
= ext
+ count
;
1531 for (; ext
< ext_end
; ext
++, in
++)
1535 x
= GET_WORD (abfd
, ext
->e_strx
);
1536 in
->symbol
.the_bfd
= abfd
;
1538 /* For the normal symbols, the zero index points at the number
1539 of bytes in the string table but is to be interpreted as the
1540 null string. For the dynamic symbols, the number of bytes in
1541 the string table is stored in the __DYNAMIC structure and the
1542 zero index points at an actual string. */
1543 if (x
== 0 && ! dynamic
)
1544 in
->symbol
.name
= "";
1545 else if (x
< strsize
)
1546 in
->symbol
.name
= str
+ x
;
1550 in
->symbol
.value
= GET_SWORD (abfd
, ext
->e_value
);
1551 in
->desc
= bfd_h_get_16 (abfd
, ext
->e_desc
);
1552 in
->other
= bfd_h_get_8 (abfd
, ext
->e_other
);
1553 in
->type
= bfd_h_get_8 (abfd
, ext
->e_type
);
1554 in
->symbol
.udata
= 0;
1556 if (! translate_from_native_sym_flags (abfd
, in
))
1560 in
->symbol
.flags
|= BSF_DYNAMIC
;
1566 /* We read the symbols into a buffer, which is discarded when this
1567 function exits. We read the strings into a buffer large enough to
1568 hold them all plus all the cached symbol entries. */
1571 NAME(aout
,slurp_symbol_table
) (abfd
)
1574 struct external_nlist
*old_external_syms
;
1575 aout_symbol_type
*cached
;
1578 /* If there's no work to be done, don't do any */
1579 if (obj_aout_symbols (abfd
) != (aout_symbol_type
*) NULL
)
1582 old_external_syms
= obj_aout_external_syms (abfd
);
1584 if (! aout_get_external_symbols (abfd
))
1587 if (obj_aout_external_sym_count (abfd
) == 0)
1589 bfd_set_error (bfd_error_no_symbols
);
1593 cached_size
= (obj_aout_external_sym_count (abfd
)
1594 * sizeof (aout_symbol_type
));
1595 cached
= (aout_symbol_type
*) malloc (cached_size
);
1598 bfd_set_error (bfd_error_no_memory
);
1601 memset (cached
, 0, cached_size
);
1603 /* Convert from external symbol information to internal. */
1604 if (! (NAME(aout
,translate_symbol_table
)
1606 obj_aout_external_syms (abfd
),
1607 obj_aout_external_sym_count (abfd
),
1608 obj_aout_external_strings (abfd
),
1609 obj_aout_external_string_size (abfd
),
1616 bfd_get_symcount (abfd
) = obj_aout_external_sym_count (abfd
);
1618 obj_aout_symbols (abfd
) = cached
;
1620 /* It is very likely that anybody who calls this function will not
1621 want the external symbol information, so if it was allocated
1622 because of our call to aout_get_external_symbols, we free it up
1623 right away to save space. */
1624 if (old_external_syms
== (struct external_nlist
*) NULL
1625 && obj_aout_external_syms (abfd
) != (struct external_nlist
*) NULL
)
1627 free (obj_aout_external_syms (abfd
));
1628 obj_aout_external_syms (abfd
) = NULL
;
1634 /* Possible improvements:
1635 + look for strings matching trailing substrings of other strings
1636 + better data structures? balanced trees?
1637 + smaller per-string or per-symbol data? re-use some of the symbol's
1639 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1640 construct the entire symbol table at once, we could get by with smaller
1641 amounts of VM? (What effect does that have on the string table
1643 + rip this out of here, put it into its own file in bfd or libiberty, so
1644 coff and elf can use it too. I'll work on this soon, but have more
1645 pressing tasks right now.
1647 A hash table might(?) be more efficient for handling exactly the cases that
1648 are handled now, but for trailing substring matches, I think we want to
1649 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1650 order, nor look only for exact-match or not-match. I don't think a hash
1651 table would be very useful for that, and I don't feel like fleshing out two
1652 completely different implementations. [raeburn:930419.0331EDT] */
1654 struct stringtab_entry
{
1655 /* Hash value for this string. Only useful so long as we aren't doing
1656 substring matches. */
1659 /* Next node to look at, depending on whether the hash value of the string
1660 being searched for is less than or greater than the hash value of the
1661 current node. For now, `equal to' is lumped in with `greater than', for
1662 space efficiency. It's not a common enough case to warrant another field
1663 to be used for all nodes. */
1664 struct stringtab_entry
*less
;
1665 struct stringtab_entry
*greater
;
1667 /* The string itself. */
1670 /* The index allocated for this string. */
1671 bfd_size_type index
;
1673 #ifdef GATHER_STATISTICS
1674 /* How many references have there been to this string? (Not currently used;
1675 could be dumped out for anaylsis, if anyone's interested.) */
1676 unsigned long count
;
1679 /* Next node in linked list, in suggested output order. */
1680 struct stringtab_entry
*next_to_output
;
1683 struct stringtab_data
{
1684 /* Tree of string table entries. */
1685 struct stringtab_entry
*strings
;
1687 /* Fudge factor used to center top node of tree. */
1690 /* Next index value to issue. */
1691 bfd_size_type index
;
1693 /* Index used for empty strings. Cached here because checking for them
1694 is really easy, and we can avoid searching the tree. */
1695 bfd_size_type empty_string_index
;
1697 /* These fields indicate the two ends of a singly-linked list that indicates
1698 the order strings should be written out in. Use this order, and no
1699 seeking will need to be done, so output efficiency should be maximized. */
1700 struct stringtab_entry
**end
;
1701 struct stringtab_entry
*output_order
;
1703 #ifdef GATHER_STATISTICS
1704 /* Number of strings which duplicate strings already in the table. */
1705 unsigned long duplicates
;
1707 /* Number of bytes saved by not having to write all the duplicate strings. */
1708 unsigned long bytes_saved
;
1710 /* Number of zero-length strings. Currently, these all turn into
1711 references to the null byte at the end of the first string. In some
1712 cases (possibly not all? explore this...), it should be possible to
1713 simply write out a zero index value. */
1714 unsigned long empty_strings
;
1716 /* Number of times the hash values matched but the strings were different.
1717 Note that this includes the number of times the other string(s) occurs, so
1718 there may only be two strings hashing to the same value, even if this
1719 number is very large. */
1720 unsigned long bad_hash_matches
;
1722 /* Null strings aren't counted in this one.
1723 This will probably only be nonzero if we've got an input file
1724 which was produced by `ld -r' (i.e., it's already been processed
1725 through this code). Under some operating systems, native tools
1726 may make all empty strings have the same index; but the pointer
1727 check won't catch those, because to get to that stage we'd already
1728 have to compute the checksum, which requires reading the string,
1729 so we short-circuit that case with empty_string_index above. */
1730 unsigned long pointer_matches
;
1732 /* Number of comparisons done. I figure with the algorithms in use below,
1733 the average number of comparisons done (per symbol) should be roughly
1734 log-base-2 of the number of unique strings. */
1735 unsigned long n_compares
;
1739 /* Some utility functions for the string table code. */
1741 /* For speed, only hash on the first this many bytes of strings.
1742 This number was chosen by profiling ld linking itself, with -g. */
1743 #define HASHMAXLEN 25
1745 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1747 static INLINE
unsigned int
1749 unsigned char *string
;
1750 register unsigned int len
;
1752 register unsigned int sum
= 0;
1754 if (len
> HASHMAXLEN
)
1762 HASH_CHAR (*string
++);
1768 stringtab_init (tab
)
1769 struct stringtab_data
*tab
;
1772 tab
->output_order
= 0;
1774 tab
->end
= &tab
->output_order
;
1776 /* Initial string table length includes size of length field. */
1777 tab
->index
= BYTES_IN_WORD
;
1778 tab
->empty_string_index
= -1;
1779 #ifdef GATHER_STATISTICS
1780 tab
->duplicates
= 0;
1781 tab
->empty_strings
= 0;
1782 tab
->bad_hash_matches
= 0;
1783 tab
->pointer_matches
= 0;
1784 tab
->bytes_saved
= 0;
1785 tab
->n_compares
= 0;
1790 compare (entry
, str
, hash
)
1791 struct stringtab_entry
*entry
;
1795 return hash
- entry
->hash
;
1798 #ifdef GATHER_STATISTICS
1799 /* Don't want to have to link in math library with all bfd applications... */
1800 static INLINE
double
1808 return ((d
> 1.41) ? 0.5 : 0) + n
;
1812 /* Main string table routines. */
1813 /* Returns index in string table. Whether or not this actually adds an
1814 entry into the string table should be irrelevant -- it just has to
1815 return a valid index. */
1816 static bfd_size_type
1817 add_to_stringtab (abfd
, str
, tab
)
1820 struct stringtab_data
*tab
;
1822 struct stringtab_entry
**ep
;
1823 register struct stringtab_entry
*entry
;
1824 unsigned int hashval
, len
;
1828 bfd_size_type index
;
1829 CONST bfd_size_type minus_one
= -1;
1831 #ifdef GATHER_STATISTICS
1832 tab
->empty_strings
++;
1834 index
= tab
->empty_string_index
;
1835 if (index
!= minus_one
)
1838 #ifdef GATHER_STATISTICS
1845 /* Need to find it. */
1846 entry
= tab
->strings
;
1849 index
= entry
->index
+ strlen (entry
->string
);
1850 tab
->empty_string_index
= index
;
1858 /* The hash_zero value is chosen such that the first symbol gets a value of
1859 zero. With a balanced tree, this wouldn't be very useful, but without it,
1860 we might get a more even split at the top level, instead of skewing it
1861 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1862 hashval
= hash (str
, len
) ^ tab
->hash_zero
;
1866 tab
->hash_zero
= hashval
;
1876 #ifdef GATHER_STATISTICS
1879 cmp
= compare (entry
, str
, hashval
);
1880 /* The not-equal cases are more frequent, so check them first. */
1882 ep
= &entry
->greater
;
1887 if (entry
->string
== str
)
1889 #ifdef GATHER_STATISTICS
1890 tab
->pointer_matches
++;
1894 /* Compare the first bytes to save a function call if they
1896 if (entry
->string
[0] == str
[0] && !strcmp (entry
->string
, str
))
1899 #ifdef GATHER_STATISTICS
1901 tab
->bytes_saved
+= len
+ 1;
1904 /* If we're in the linker, and the new string is from a new
1905 input file which might have already had these reductions
1906 run over it, we want to keep the new string pointer. I
1907 don't think we're likely to see any (or nearly as many,
1908 at least) cases where a later string is in the same location
1909 as an earlier one rather than this one. */
1910 entry
->string
= str
;
1911 return entry
->index
;
1913 #ifdef GATHER_STATISTICS
1914 tab
->bad_hash_matches
++;
1916 ep
= &entry
->greater
;
1920 /* If we get here, nothing that's in the table already matched.
1921 EP points to the `next' field at the end of the chain; stick a
1922 new entry on here. */
1924 entry
= (struct stringtab_entry
*)
1925 bfd_alloc_by_size_t (abfd
, sizeof (struct stringtab_entry
));
1928 bfd_set_error (bfd_error_no_memory
);
1929 abort(); /* FIXME */
1932 entry
->less
= entry
->greater
= 0;
1933 entry
->hash
= hashval
;
1934 entry
->index
= tab
->index
;
1935 entry
->string
= str
;
1936 entry
->next_to_output
= 0;
1937 #ifdef GATHER_STATISTICS
1941 BFD_ASSERT (*tab
->end
== 0);
1942 *(tab
->end
) = entry
;
1943 tab
->end
= &entry
->next_to_output
;
1944 BFD_ASSERT (*tab
->end
== 0);
1947 tab
->index
+= len
+ 1;
1949 tab
->empty_string_index
= entry
->index
;
1951 BFD_ASSERT (*ep
== 0);
1953 return entry
->index
;
1957 emit_strtab (abfd
, tab
)
1959 struct stringtab_data
*tab
;
1961 struct stringtab_entry
*entry
;
1962 #ifdef GATHER_STATISTICS
1966 /* Be sure to put string length into correct byte ordering before writing
1968 char buffer
[BYTES_IN_WORD
];
1970 PUT_WORD (abfd
, tab
->index
, (unsigned char *) buffer
);
1971 if (bfd_write ((PTR
) buffer
, 1, BYTES_IN_WORD
, abfd
) != BYTES_IN_WORD
)
1974 for (entry
= tab
->output_order
; entry
; entry
= entry
->next_to_output
)
1976 size_t len
= strlen (entry
->string
) + 1;
1978 if (bfd_write ((PTR
) entry
->string
, 1, len
, abfd
) != len
)
1981 #ifdef GATHER_STATISTICS
1986 #ifdef GATHER_STATISTICS
1987 /* Short form only, for now.
1988 To do: Specify output file. Conditionalize on environment? Detailed
1989 analysis if desired. */
1991 int n_syms
= bfd_get_symcount (abfd
);
1993 fprintf (stderr
, "String table data for output file:\n");
1994 fprintf (stderr
, " %8d symbols output\n", n_syms
);
1995 fprintf (stderr
, " %8d duplicate strings\n", tab
->duplicates
);
1996 fprintf (stderr
, " %8d empty strings\n", tab
->empty_strings
);
1997 fprintf (stderr
, " %8d unique strings output\n", count
);
1998 fprintf (stderr
, " %8d pointer matches\n", tab
->pointer_matches
);
1999 fprintf (stderr
, " %8d bytes saved\n", tab
->bytes_saved
);
2000 fprintf (stderr
, " %8d bad hash matches\n", tab
->bad_hash_matches
);
2001 fprintf (stderr
, " %8d hash-val comparisons\n", tab
->n_compares
);
2004 double n_compares
= tab
->n_compares
;
2005 double avg_compares
= n_compares
/ n_syms
;
2006 /* The second value here should usually be near one. */
2008 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
2009 avg_compares
, avg_compares
/ log2 (count
));
2016 generic = bfd_get_outsymbols(abfd);
2017 for (count = 0; count < bfd_get_symcount(abfd); count++)
2019 asymbol *g = *(generic++);
2023 size_t length = strlen(g->name)+1;
2024 bfd_write((PTR)g->name, 1, length, abfd);
2026 g->KEEPIT = (KEEPITTYPE) count;
2033 NAME(aout
,write_syms
) (abfd
)
2036 unsigned int count
;
2037 asymbol
**generic
= bfd_get_outsymbols (abfd
);
2038 struct stringtab_data strtab
;
2040 stringtab_init (&strtab
);
2042 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
2044 asymbol
*g
= generic
[count
];
2045 struct external_nlist nsp
;
2048 PUT_WORD (abfd
, add_to_stringtab (abfd
, g
->name
, &strtab
),
2049 (unsigned char *) nsp
.e_strx
);
2051 PUT_WORD (abfd
, 0, (unsigned char *)nsp
.e_strx
);
2053 if (bfd_asymbol_flavour(g
) == abfd
->xvec
->flavour
)
2055 bfd_h_put_16(abfd
, aout_symbol(g
)->desc
, nsp
.e_desc
);
2056 bfd_h_put_8(abfd
, aout_symbol(g
)->other
, nsp
.e_other
);
2057 bfd_h_put_8(abfd
, aout_symbol(g
)->type
, nsp
.e_type
);
2061 bfd_h_put_16(abfd
,0, nsp
.e_desc
);
2062 bfd_h_put_8(abfd
, 0, nsp
.e_other
);
2063 bfd_h_put_8(abfd
, 0, nsp
.e_type
);
2066 if (! translate_to_native_sym_flags (abfd
, g
, &nsp
))
2069 if (bfd_write((PTR
)&nsp
,1,EXTERNAL_NLIST_SIZE
, abfd
)
2070 != EXTERNAL_NLIST_SIZE
)
2073 /* NB: `KEEPIT' currently overlays `flags', so set this only
2074 here, at the end. */
2078 return emit_strtab (abfd
, &strtab
);
2083 NAME(aout
,get_symtab
) (abfd
, location
)
2087 unsigned int counter
= 0;
2088 aout_symbol_type
*symbase
;
2090 if (!NAME(aout
,slurp_symbol_table
)(abfd
))
2093 for (symbase
= obj_aout_symbols(abfd
); counter
++ < bfd_get_symcount (abfd
);)
2094 *(location
++) = (asymbol
*)( symbase
++);
2096 return bfd_get_symcount (abfd
);
2100 /* Standard reloc stuff */
2101 /* Output standard relocation information to a file in target byte order. */
2104 NAME(aout
,swap_std_reloc_out
) (abfd
, g
, natptr
)
2107 struct reloc_std_external
*natptr
;
2110 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2112 unsigned int r_length
;
2114 int r_baserel
, r_jmptable
, r_relative
;
2115 asection
*output_section
= sym
->section
->output_section
;
2117 PUT_WORD(abfd
, g
->address
, natptr
->r_address
);
2119 r_length
= g
->howto
->size
; /* Size as a power of two */
2120 r_pcrel
= (int) g
->howto
->pc_relative
; /* Relative to PC? */
2121 /* XXX This relies on relocs coming from a.out files. */
2122 r_baserel
= (g
->howto
->type
& 8) != 0;
2123 r_jmptable
= (g
->howto
->type
& 16) != 0;
2124 r_relative
= (g
->howto
->type
& 32) != 0;
2127 /* For a standard reloc, the addend is in the object file. */
2128 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2131 /* name was clobbered by aout_write_syms to be symbol index */
2133 /* If this relocation is relative to a symbol then set the
2134 r_index to the symbols index, and the r_extern bit.
2136 Absolute symbols can come in in two ways, either as an offset
2137 from the abs section, or as a symbol which has an abs value.
2142 if (bfd_is_com_section (output_section
)
2143 || output_section
== &bfd_abs_section
2144 || output_section
== &bfd_und_section
)
2146 if (bfd_abs_section
.symbol
== sym
)
2148 /* Whoops, looked like an abs symbol, but is really an offset
2149 from the abs section */
2155 /* Fill in symbol */
2157 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
2163 /* Just an ordinary section */
2165 r_index
= output_section
->target_index
;
2168 /* now the fun stuff */
2169 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2170 natptr
->r_index
[0] = r_index
>> 16;
2171 natptr
->r_index
[1] = r_index
>> 8;
2172 natptr
->r_index
[2] = r_index
;
2174 (r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
2175 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
2176 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
2177 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
2178 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
2179 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
);
2181 natptr
->r_index
[2] = r_index
>> 16;
2182 natptr
->r_index
[1] = r_index
>> 8;
2183 natptr
->r_index
[0] = r_index
;
2185 (r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
2186 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
2187 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
2188 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
2189 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
2190 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
);
2195 /* Extended stuff */
2196 /* Output extended relocation information to a file in target byte order. */
2199 NAME(aout
,swap_ext_reloc_out
) (abfd
, g
, natptr
)
2202 register struct reloc_ext_external
*natptr
;
2206 unsigned int r_type
;
2207 unsigned int r_addend
;
2208 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2209 asection
*output_section
= sym
->section
->output_section
;
2211 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
2213 r_type
= (unsigned int) g
->howto
->type
;
2215 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2217 /* If this relocation is relative to a symbol then set the
2218 r_index to the symbols index, and the r_extern bit.
2220 Absolute symbols can come in in two ways, either as an offset
2221 from the abs section, or as a symbol which has an abs value.
2222 check for that here. */
2224 if (bfd_is_com_section (output_section
)
2225 || output_section
== &bfd_abs_section
2226 || output_section
== &bfd_und_section
)
2228 if (bfd_abs_section
.symbol
== sym
)
2230 /* Whoops, looked like an abs symbol, but is really an offset
2231 from the abs section */
2238 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
2243 /* Just an ordinary section */
2245 r_index
= output_section
->target_index
;
2248 /* now the fun stuff */
2249 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2250 natptr
->r_index
[0] = r_index
>> 16;
2251 natptr
->r_index
[1] = r_index
>> 8;
2252 natptr
->r_index
[2] = r_index
;
2254 ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
2255 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
2257 natptr
->r_index
[2] = r_index
>> 16;
2258 natptr
->r_index
[1] = r_index
>> 8;
2259 natptr
->r_index
[0] = r_index
;
2261 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
2262 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
2265 PUT_WORD (abfd
, r_addend
, natptr
->r_addend
);
2268 /* BFD deals internally with all things based from the section they're
2269 in. so, something in 10 bytes into a text section with a base of
2270 50 would have a symbol (.text+10) and know .text vma was 50.
2272 Aout keeps all it's symbols based from zero, so the symbol would
2273 contain 60. This macro subs the base of each section from the value
2274 to give the true offset from the section */
2277 #define MOVE_ADDRESS(ad) \
2279 /* undefined symbol */ \
2280 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2281 cache_ptr->addend = ad; \
2283 /* defined, section relative. replace symbol with pointer to \
2284 symbol which points to section */ \
2285 switch (r_index) { \
2287 case N_TEXT | N_EXT: \
2288 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2289 cache_ptr->addend = ad - su->textsec->vma; \
2292 case N_DATA | N_EXT: \
2293 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2294 cache_ptr->addend = ad - su->datasec->vma; \
2297 case N_BSS | N_EXT: \
2298 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2299 cache_ptr->addend = ad - su->bsssec->vma; \
2303 case N_ABS | N_EXT: \
2304 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2305 cache_ptr->addend = ad; \
2311 NAME(aout
,swap_ext_reloc_in
) (abfd
, bytes
, cache_ptr
, symbols
)
2313 struct reloc_ext_external
*bytes
;
2319 unsigned int r_type
;
2320 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2322 cache_ptr
->address
= (GET_SWORD (abfd
, bytes
->r_address
));
2324 /* now the fun stuff */
2325 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2326 r_index
= (bytes
->r_index
[0] << 16)
2327 | (bytes
->r_index
[1] << 8)
2328 | bytes
->r_index
[2];
2329 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
2330 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
2331 >> RELOC_EXT_BITS_TYPE_SH_BIG
;
2333 r_index
= (bytes
->r_index
[2] << 16)
2334 | (bytes
->r_index
[1] << 8)
2335 | bytes
->r_index
[0];
2336 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
2337 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
2338 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
;
2341 cache_ptr
->howto
= howto_table_ext
+ r_type
;
2342 MOVE_ADDRESS(GET_SWORD(abfd
, bytes
->r_addend
));
2346 NAME(aout
,swap_std_reloc_in
) (abfd
, bytes
, cache_ptr
, symbols
)
2348 struct reloc_std_external
*bytes
;
2354 unsigned int r_length
;
2356 int r_baserel
, r_jmptable
, r_relative
;
2357 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2360 cache_ptr
->address
= bfd_h_get_32 (abfd
, bytes
->r_address
);
2362 /* now the fun stuff */
2363 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2364 r_index
= (bytes
->r_index
[0] << 16)
2365 | (bytes
->r_index
[1] << 8)
2366 | bytes
->r_index
[2];
2367 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
2368 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
2369 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
2370 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
2371 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
2372 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
2373 >> RELOC_STD_BITS_LENGTH_SH_BIG
;
2375 r_index
= (bytes
->r_index
[2] << 16)
2376 | (bytes
->r_index
[1] << 8)
2377 | bytes
->r_index
[0];
2378 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
2379 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
2380 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
2381 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
2382 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
2383 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
2384 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
;
2387 howto_idx
= r_length
+ 4 * r_pcrel
+ 8 * r_baserel
2388 + 16 * r_jmptable
+ 32 * r_relative
;
2389 BFD_ASSERT (howto_idx
< TABLE_SIZE (howto_table_std
));
2390 cache_ptr
->howto
= howto_table_std
+ howto_idx
;
2391 BFD_ASSERT (cache_ptr
->howto
->type
!= -1);
2396 /* Read and swap the relocs for a section. */
2399 NAME(aout
,slurp_reloc_table
) (abfd
, asect
, symbols
)
2405 bfd_size_type reloc_size
;
2407 arelent
*reloc_cache
;
2409 unsigned int counter
= 0;
2412 if (asect
->relocation
)
2415 if (asect
->flags
& SEC_CONSTRUCTOR
)
2418 if (asect
== obj_datasec (abfd
))
2419 reloc_size
= exec_hdr(abfd
)->a_drsize
;
2420 else if (asect
== obj_textsec (abfd
))
2421 reloc_size
= exec_hdr(abfd
)->a_trsize
;
2424 bfd_set_error (bfd_error_invalid_operation
);
2428 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
2431 each_size
= obj_reloc_entry_size (abfd
);
2433 count
= reloc_size
/ each_size
;
2435 reloc_cache
= (arelent
*) malloc ((size_t) (count
* sizeof (arelent
)));
2436 if (reloc_cache
== NULL
&& count
!= 0)
2438 bfd_set_error (bfd_error_no_memory
);
2441 memset (reloc_cache
, 0, count
* sizeof (arelent
));
2443 relocs
= malloc (reloc_size
);
2444 if (relocs
== NULL
&& reloc_size
!= 0)
2447 bfd_set_error (bfd_error_no_memory
);
2451 if (bfd_read (relocs
, 1, reloc_size
, abfd
) != reloc_size
)
2458 cache_ptr
= reloc_cache
;
2459 if (each_size
== RELOC_EXT_SIZE
)
2461 register struct reloc_ext_external
*rptr
=
2462 (struct reloc_ext_external
*) relocs
;
2464 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2465 NAME(aout
,swap_ext_reloc_in
) (abfd
, rptr
, cache_ptr
, symbols
);
2469 register struct reloc_std_external
*rptr
=
2470 (struct reloc_std_external
*) relocs
;
2472 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2473 NAME(aout
,swap_std_reloc_in
) (abfd
, rptr
, cache_ptr
, symbols
);
2478 asect
->relocation
= reloc_cache
;
2479 asect
->reloc_count
= cache_ptr
- reloc_cache
;
2484 /* Write out a relocation section into an object file. */
2487 NAME(aout
,squirt_out_relocs
) (abfd
, section
)
2492 unsigned char *native
, *natptr
;
2495 unsigned int count
= section
->reloc_count
;
2498 if (count
== 0) return true;
2500 each_size
= obj_reloc_entry_size (abfd
);
2501 natsize
= each_size
* count
;
2502 native
= (unsigned char *) bfd_zalloc (abfd
, natsize
);
2504 bfd_set_error (bfd_error_no_memory
);
2508 generic
= section
->orelocation
;
2510 if (each_size
== RELOC_EXT_SIZE
)
2512 for (natptr
= native
;
2514 --count
, natptr
+= each_size
, ++generic
)
2515 NAME(aout
,swap_ext_reloc_out
) (abfd
, *generic
, (struct reloc_ext_external
*)natptr
);
2519 for (natptr
= native
;
2521 --count
, natptr
+= each_size
, ++generic
)
2522 NAME(aout
,swap_std_reloc_out
)(abfd
, *generic
, (struct reloc_std_external
*)natptr
);
2525 if ( bfd_write ((PTR
) native
, 1, natsize
, abfd
) != natsize
) {
2526 bfd_release(abfd
, native
);
2529 bfd_release (abfd
, native
);
2534 /* This is stupid. This function should be a boolean predicate */
2536 NAME(aout
,canonicalize_reloc
) (abfd
, section
, relptr
, symbols
)
2542 arelent
*tblptr
= section
->relocation
;
2545 if (section
== obj_bsssec (abfd
))
2551 if (!(tblptr
|| NAME(aout
,slurp_reloc_table
)(abfd
, section
, symbols
)))
2554 if (section
->flags
& SEC_CONSTRUCTOR
) {
2555 arelent_chain
*chain
= section
->constructor_chain
;
2556 for (count
= 0; count
< section
->reloc_count
; count
++) {
2557 *relptr
++ = &chain
->relent
;
2558 chain
= chain
->next
;
2562 tblptr
= section
->relocation
;
2564 for (count
= 0; count
++ < section
->reloc_count
;)
2566 *relptr
++ = tblptr
++;
2571 return section
->reloc_count
;
2575 NAME(aout
,get_reloc_upper_bound
) (abfd
, asect
)
2579 if (bfd_get_format (abfd
) != bfd_object
) {
2580 bfd_set_error (bfd_error_invalid_operation
);
2583 if (asect
->flags
& SEC_CONSTRUCTOR
) {
2584 return (sizeof (arelent
*) * (asect
->reloc_count
+1));
2587 if (asect
== obj_datasec (abfd
))
2588 return (sizeof (arelent
*)
2589 * ((exec_hdr(abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
))
2592 if (asect
== obj_textsec (abfd
))
2593 return (sizeof (arelent
*)
2594 * ((exec_hdr(abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
))
2597 if (asect
== obj_bsssec (abfd
))
2598 return sizeof (arelent
*);
2600 bfd_set_error (bfd_error_invalid_operation
);
2606 NAME(aout
,get_symtab_upper_bound
) (abfd
)
2609 if (!NAME(aout
,slurp_symbol_table
)(abfd
))
2612 return (bfd_get_symcount (abfd
)+1) * (sizeof (aout_symbol_type
*));
2617 NAME(aout
,get_lineno
) (ignore_abfd
, ignore_symbol
)
2619 asymbol
*ignore_symbol
;
2621 return (alent
*)NULL
;
2626 NAME(aout
,get_symbol_info
) (ignore_abfd
, symbol
, ret
)
2631 bfd_symbol_info (symbol
, ret
);
2633 if (ret
->type
== '?')
2635 int type_code
= aout_symbol(symbol
)->type
& 0xff;
2636 CONST
char *stab_name
= aout_stab_name(type_code
);
2637 static char buf
[10];
2639 if (stab_name
== NULL
)
2641 sprintf(buf
, "(%d)", type_code
);
2645 ret
->stab_other
= (unsigned)(aout_symbol(symbol
)->other
& 0xff);
2646 ret
->stab_desc
= (unsigned)(aout_symbol(symbol
)->desc
& 0xffff);
2647 ret
->stab_name
= stab_name
;
2653 NAME(aout
,print_symbol
) (ignore_abfd
, afile
, symbol
, how
)
2657 bfd_print_symbol_type how
;
2659 FILE *file
= (FILE *)afile
;
2662 case bfd_print_symbol_name
:
2664 fprintf(file
,"%s", symbol
->name
);
2666 case bfd_print_symbol_more
:
2667 fprintf(file
,"%4x %2x %2x",(unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2668 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2669 (unsigned)(aout_symbol(symbol
)->type
));
2671 case bfd_print_symbol_all
:
2673 CONST
char *section_name
= symbol
->section
->name
;
2676 bfd_print_symbol_vandf((PTR
)file
,symbol
);
2678 fprintf(file
," %-5s %04x %02x %02x",
2680 (unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2681 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2682 (unsigned)(aout_symbol(symbol
)->type
& 0xff));
2684 fprintf(file
," %s", symbol
->name
);
2691 provided a BFD, a section and an offset into the section, calculate
2692 and return the name of the source file and the line nearest to the
2697 NAME(aout
,find_nearest_line
)
2698 (abfd
, section
, symbols
, offset
, filename_ptr
, functionname_ptr
, line_ptr
)
2703 CONST
char **filename_ptr
;
2704 CONST
char **functionname_ptr
;
2705 unsigned int *line_ptr
;
2707 /* Run down the file looking for the filename, function and linenumber */
2709 static char buffer
[100];
2710 static char filename_buffer
[200];
2711 CONST
char *directory_name
= NULL
;
2712 CONST
char *main_file_name
= NULL
;
2713 CONST
char *current_file_name
= NULL
;
2714 CONST
char *line_file_name
= NULL
; /* Value of current_file_name at line number. */
2715 bfd_vma high_line_vma
= ~0;
2716 bfd_vma low_func_vma
= 0;
2718 *filename_ptr
= abfd
->filename
;
2719 *functionname_ptr
= 0;
2721 if (symbols
!= (asymbol
**)NULL
) {
2722 for (p
= symbols
; *p
; p
++) {
2723 aout_symbol_type
*q
= (aout_symbol_type
*)(*p
);
2727 main_file_name
= current_file_name
= q
->symbol
.name
;
2728 /* Look ahead to next symbol to check if that too is an N_SO. */
2732 q
= (aout_symbol_type
*)(*p
);
2733 if (q
->type
!= (int)N_SO
)
2736 /* Found a second N_SO First is directory; second is filename. */
2737 directory_name
= current_file_name
;
2738 main_file_name
= current_file_name
= q
->symbol
.name
;
2739 if (obj_textsec(abfd
) != section
)
2743 current_file_name
= q
->symbol
.name
;
2750 /* We'll keep this if it resolves nearer than the one we have already */
2751 if (q
->symbol
.value
>= offset
&&
2752 q
->symbol
.value
< high_line_vma
) {
2753 *line_ptr
= q
->desc
;
2754 high_line_vma
= q
->symbol
.value
;
2755 line_file_name
= current_file_name
;
2760 /* We'll keep this if it is nearer than the one we have already */
2761 if (q
->symbol
.value
>= low_func_vma
&&
2762 q
->symbol
.value
<= offset
) {
2763 low_func_vma
= q
->symbol
.value
;
2764 func
= (asymbol
*)q
;
2766 if (*line_ptr
&& func
) {
2767 CONST
char *function
= func
->name
;
2770 /* The caller expects a symbol name. We actually have a
2771 function name, without the leading underscore. Put the
2772 underscore back in, so that the caller gets a symbol
2774 if (bfd_get_symbol_leading_char (abfd
) == '\0')
2775 strncpy (buffer
, function
, sizeof (buffer
) - 1);
2778 buffer
[0] = bfd_get_symbol_leading_char (abfd
);
2779 strncpy (buffer
+ 1, function
, sizeof (buffer
) - 2);
2781 buffer
[sizeof(buffer
)-1] = 0;
2782 /* Have to remove : stuff */
2783 p
= strchr(buffer
,':');
2784 if (p
!= NULL
) { *p
= '\0'; }
2785 *functionname_ptr
= buffer
;
2797 main_file_name
= line_file_name
;
2798 if (main_file_name
) {
2799 if (main_file_name
[0] == '/' || directory_name
== NULL
)
2800 *filename_ptr
= main_file_name
;
2802 sprintf(filename_buffer
, "%.140s%.50s",
2803 directory_name
, main_file_name
);
2804 *filename_ptr
= filename_buffer
;
2813 NAME(aout
,sizeof_headers
) (abfd
, execable
)
2817 return adata(abfd
).exec_bytes_size
;
2820 /* Free all information we have cached for this BFD. We can always
2821 read it again later if we need it. */
2824 NAME(aout
,bfd_free_cached_info
) (abfd
)
2829 if (bfd_get_format (abfd
) != bfd_object
)
2832 #define FREE(x) if (x != NULL) { free (x); x = NULL; }
2833 FREE (obj_aout_symbols (abfd
));
2834 FREE (obj_aout_external_syms (abfd
));
2835 FREE (obj_aout_external_strings (abfd
));
2836 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
2837 FREE (o
->relocation
);
2843 /* a.out link code. */
2845 static boolean aout_link_add_object_symbols
2846 PARAMS ((bfd
*, struct bfd_link_info
*));
2847 static boolean aout_link_check_archive_element
2848 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
2849 static boolean aout_link_free_symbols
PARAMS ((bfd
*));
2850 static boolean aout_link_check_ar_symbols
2851 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*pneeded
));
2852 static boolean aout_link_add_symbols
2853 PARAMS ((bfd
*, struct bfd_link_info
*));
2855 /* Routine to create an entry in an a.out link hash table. */
2857 struct bfd_hash_entry
*
2858 NAME(aout
,link_hash_newfunc
) (entry
, table
, string
)
2859 struct bfd_hash_entry
*entry
;
2860 struct bfd_hash_table
*table
;
2863 struct aout_link_hash_entry
*ret
= (struct aout_link_hash_entry
*) entry
;
2865 /* Allocate the structure if it has not already been allocated by a
2867 if (ret
== (struct aout_link_hash_entry
*) NULL
)
2868 ret
= ((struct aout_link_hash_entry
*)
2869 bfd_hash_allocate (table
, sizeof (struct aout_link_hash_entry
)));
2870 if (ret
== (struct aout_link_hash_entry
*) NULL
)
2872 bfd_set_error (bfd_error_no_memory
);
2873 return (struct bfd_hash_entry
*) ret
;
2876 /* Call the allocation method of the superclass. */
2877 ret
= ((struct aout_link_hash_entry
*)
2878 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2882 /* Set local fields. */
2883 ret
->written
= false;
2887 return (struct bfd_hash_entry
*) ret
;
2890 /* Initialize an a.out link hash table. */
2893 NAME(aout
,link_hash_table_init
) (table
, abfd
, newfunc
)
2894 struct aout_link_hash_table
*table
;
2896 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
2897 struct bfd_hash_table
*,
2900 return _bfd_link_hash_table_init (&table
->root
, abfd
, newfunc
);
2903 /* Create an a.out link hash table. */
2905 struct bfd_link_hash_table
*
2906 NAME(aout
,link_hash_table_create
) (abfd
)
2909 struct aout_link_hash_table
*ret
;
2911 ret
= ((struct aout_link_hash_table
*)
2912 malloc (sizeof (struct aout_link_hash_table
)));
2913 if (ret
== (struct aout_link_hash_table
*) NULL
)
2915 bfd_set_error (bfd_error_no_memory
);
2916 return (struct bfd_link_hash_table
*) NULL
;
2918 if (! NAME(aout
,link_hash_table_init
) (ret
, abfd
,
2919 NAME(aout
,link_hash_newfunc
)))
2922 return (struct bfd_link_hash_table
*) NULL
;
2927 /* Given an a.out BFD, add symbols to the global hash table as
2931 NAME(aout
,link_add_symbols
) (abfd
, info
)
2933 struct bfd_link_info
*info
;
2935 switch (bfd_get_format (abfd
))
2938 return aout_link_add_object_symbols (abfd
, info
);
2940 return _bfd_generic_link_add_archive_symbols
2941 (abfd
, info
, aout_link_check_archive_element
);
2943 bfd_set_error (bfd_error_wrong_format
);
2948 /* Add symbols from an a.out object file. */
2951 aout_link_add_object_symbols (abfd
, info
)
2953 struct bfd_link_info
*info
;
2955 if (! aout_get_external_symbols (abfd
))
2957 if (! aout_link_add_symbols (abfd
, info
))
2959 if (! info
->keep_memory
)
2961 if (! aout_link_free_symbols (abfd
))
2967 /* Check a single archive element to see if we need to include it in
2968 the link. *PNEEDED is set according to whether this element is
2969 needed in the link or not. This is called from
2970 _bfd_generic_link_add_archive_symbols. */
2973 aout_link_check_archive_element (abfd
, info
, pneeded
)
2975 struct bfd_link_info
*info
;
2978 if (! aout_get_external_symbols (abfd
))
2981 if (! aout_link_check_ar_symbols (abfd
, info
, pneeded
))
2986 if (! aout_link_add_symbols (abfd
, info
))
2990 /* We keep around the symbols even if we aren't going to use this
2991 object file, because we may want to reread it. This doesn't
2992 waste too much memory, because it isn't all that common to read
2993 an archive element but not need it. */
2994 if (! info
->keep_memory
)
2996 if (! aout_link_free_symbols (abfd
))
3003 /* Free up the internal symbols read from an a.out file. */
3006 aout_link_free_symbols (abfd
)
3009 if (obj_aout_external_syms (abfd
) != (struct external_nlist
*) NULL
)
3011 free ((PTR
) obj_aout_external_syms (abfd
));
3012 obj_aout_external_syms (abfd
) = (struct external_nlist
*) NULL
;
3014 if (obj_aout_external_strings (abfd
) != (char *) NULL
)
3016 free ((PTR
) obj_aout_external_strings (abfd
));
3017 obj_aout_external_strings (abfd
) = (char *) NULL
;
3022 /* Look through the internal symbols to see if this object file should
3023 be included in the link. We should include this object file if it
3024 defines any symbols which are currently undefined. If this object
3025 file defines a common symbol, then we may adjust the size of the
3026 known symbol but we do not include the object file in the link
3027 (unless there is some other reason to include it). */
3030 aout_link_check_ar_symbols (abfd
, info
, pneeded
)
3032 struct bfd_link_info
*info
;
3035 register struct external_nlist
*p
;
3036 struct external_nlist
*pend
;
3041 /* Look through all the symbols. */
3042 p
= obj_aout_external_syms (abfd
);
3043 pend
= p
+ obj_aout_external_sym_count (abfd
);
3044 strings
= obj_aout_external_strings (abfd
);
3045 for (; p
< pend
; p
++)
3047 int type
= bfd_h_get_8 (abfd
, p
->e_type
);
3049 struct bfd_link_hash_entry
*h
;
3051 /* Ignore symbols that are not externally visible. This is an
3052 optimization only, as we check the type more thoroughly
3054 if ((type
& N_EXT
) == 0
3060 if (type
== N_WARNING
3066 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3067 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
3069 /* We are only interested in symbols that are currently
3070 undefined or common. */
3071 if (h
== (struct bfd_link_hash_entry
*) NULL
3072 || (h
->type
!= bfd_link_hash_undefined
3073 && h
->type
!= bfd_link_hash_common
))
3075 if (type
== (N_INDR
| N_EXT
))
3080 if (type
== (N_TEXT
| N_EXT
)
3081 || type
== (N_DATA
| N_EXT
)
3082 || type
== (N_BSS
| N_EXT
)
3083 || type
== (N_ABS
| N_EXT
)
3084 || type
== (N_INDR
| N_EXT
))
3086 /* This object file defines this symbol. We must link it
3087 in. This is true regardless of whether the current
3088 definition of the symbol is undefined or common. If the
3089 current definition is common, we have a case in which we
3090 have already seen an object file including
3092 and this object file from the archive includes
3094 In such a case we must include this object file.
3096 FIXME: The SunOS 4.1.3 linker will pull in the archive
3097 element if the symbol is defined in the .data section,
3098 but not if it is defined in the .text section. That
3099 seems a bit crazy to me, and I haven't implemented it.
3100 However, it might be correct. */
3101 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
3107 if (type
== (N_UNDF
| N_EXT
))
3111 value
= GET_WORD (abfd
, p
->e_value
);
3114 /* This symbol is common in the object from the archive
3116 if (h
->type
== bfd_link_hash_undefined
)
3120 symbfd
= h
->u
.undef
.abfd
;
3121 if (symbfd
== (bfd
*) NULL
)
3123 /* This symbol was created as undefined from
3124 outside BFD. We assume that we should link
3125 in the object file. This is done for the -u
3126 option in the linker. */
3127 if (! (*info
->callbacks
->add_archive_element
) (info
,
3134 /* Turn the current link symbol into a common
3135 symbol. It is already on the undefs list. */
3136 h
->type
= bfd_link_hash_common
;
3137 h
->u
.c
.size
= value
;
3138 h
->u
.c
.section
= bfd_make_section_old_way (symbfd
,
3143 /* Adjust the size of the common symbol if
3145 if (value
> h
->u
.c
.size
)
3146 h
->u
.c
.size
= value
;
3156 /* This symbol is weak but defined. We must pull it in if
3157 the current link symbol is undefined, but we don't want
3158 it if the current link symbol is common. */
3159 if (h
->type
== bfd_link_hash_undefined
)
3161 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
3169 /* We do not need this object file. */
3173 /* Add all symbols from an object file to the hash table. */
3176 aout_link_add_symbols (abfd
, info
)
3178 struct bfd_link_info
*info
;
3180 boolean (*add_one_symbol
) PARAMS ((struct bfd_link_info
*, bfd
*,
3181 const char *, flagword
, asection
*,
3182 bfd_vma
, const char *, boolean
,
3184 struct bfd_link_hash_entry
**));
3185 bfd_size_type sym_count
;
3188 struct aout_link_hash_entry
**sym_hash
;
3189 register struct external_nlist
*p
;
3190 struct external_nlist
*pend
;
3192 sym_count
= obj_aout_external_sym_count (abfd
);
3193 strings
= obj_aout_external_strings (abfd
);
3194 if (info
->keep_memory
)
3199 /* We keep a list of the linker hash table entries that correspond
3200 to particular symbols. We could just look them up in the hash
3201 table, but keeping the list is more efficient. Perhaps this
3202 should be conditional on info->keep_memory. */
3203 sym_hash
= ((struct aout_link_hash_entry
**)
3206 * sizeof (struct aout_link_hash_entry
*))));
3207 if (sym_hash
== NULL
&& sym_count
!= 0)
3209 bfd_set_error (bfd_error_no_memory
);
3212 obj_aout_sym_hashes (abfd
) = sym_hash
;
3214 if ((abfd
->flags
& DYNAMIC
) != 0
3215 && aout_backend_info (abfd
)->add_dynamic_symbols
!= NULL
)
3217 if (! (*aout_backend_info (abfd
)->add_dynamic_symbols
) (abfd
, info
))
3221 add_one_symbol
= aout_backend_info (abfd
)->add_one_symbol
;
3222 if (add_one_symbol
== NULL
)
3223 add_one_symbol
= _bfd_generic_link_add_one_symbol
;
3225 p
= obj_aout_external_syms (abfd
);
3226 pend
= p
+ sym_count
;
3227 for (; p
< pend
; p
++, sym_hash
++)
3238 type
= bfd_h_get_8 (abfd
, p
->e_type
);
3240 /* Ignore debugging symbols. */
3241 if ((type
& N_STAB
) != 0)
3244 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3245 value
= GET_WORD (abfd
, p
->e_value
);
3262 /* Ignore symbols that are not externally visible. */
3265 /* Ignore local indirect symbol. */
3270 case N_UNDF
| N_EXT
:
3273 section
= &bfd_und_section
;
3277 section
= &bfd_com_section
;
3280 section
= &bfd_abs_section
;
3282 case N_TEXT
| N_EXT
:
3283 section
= obj_textsec (abfd
);
3284 value
-= bfd_get_section_vma (abfd
, section
);
3286 case N_DATA
| N_EXT
:
3287 section
= obj_datasec (abfd
);
3288 value
-= bfd_get_section_vma (abfd
, section
);
3291 section
= obj_bsssec (abfd
);
3292 value
-= bfd_get_section_vma (abfd
, section
);
3294 case N_INDR
| N_EXT
:
3295 /* An indirect symbol. The next symbol is the symbol
3296 which this one really is. */
3297 BFD_ASSERT (p
+ 1 < pend
);
3299 string
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3300 section
= &bfd_ind_section
;
3301 flags
|= BSF_INDIRECT
;
3303 case N_COMM
| N_EXT
:
3304 section
= &bfd_com_section
;
3306 case N_SETA
: case N_SETA
| N_EXT
:
3307 section
= &bfd_abs_section
;
3308 flags
|= BSF_CONSTRUCTOR
;
3310 case N_SETT
: case N_SETT
| N_EXT
:
3311 section
= obj_textsec (abfd
);
3312 flags
|= BSF_CONSTRUCTOR
;
3313 value
-= bfd_get_section_vma (abfd
, section
);
3315 case N_SETD
: case N_SETD
| N_EXT
:
3316 section
= obj_datasec (abfd
);
3317 flags
|= BSF_CONSTRUCTOR
;
3318 value
-= bfd_get_section_vma (abfd
, section
);
3320 case N_SETB
: case N_SETB
| N_EXT
:
3321 section
= obj_bsssec (abfd
);
3322 flags
|= BSF_CONSTRUCTOR
;
3323 value
-= bfd_get_section_vma (abfd
, section
);
3326 /* A warning symbol. The next symbol is the one to warn
3328 BFD_ASSERT (p
+ 1 < pend
);
3331 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3332 section
= &bfd_und_section
;
3333 flags
|= BSF_WARNING
;
3336 section
= &bfd_und_section
;
3340 section
= &bfd_abs_section
;
3344 section
= obj_textsec (abfd
);
3345 value
-= bfd_get_section_vma (abfd
, section
);
3349 section
= obj_datasec (abfd
);
3350 value
-= bfd_get_section_vma (abfd
, section
);
3354 section
= obj_bsssec (abfd
);
3355 value
-= bfd_get_section_vma (abfd
, section
);
3360 if (! ((*add_one_symbol
)
3361 (info
, abfd
, name
, flags
, section
, value
, string
, copy
, false,
3362 (struct bfd_link_hash_entry
**) sym_hash
)))
3365 if (type
== (N_INDR
| N_EXT
) || type
== N_WARNING
)
3372 /* During the final link step we need to pass around a bunch of
3373 information, so we do it in an instance of this structure. */
3375 struct aout_final_link_info
3377 /* General link information. */
3378 struct bfd_link_info
*info
;
3381 /* Reloc file positions. */
3382 file_ptr treloff
, dreloff
;
3383 /* File position of symbols. */
3386 struct stringtab_data strtab
;
3389 static boolean aout_link_input_bfd
3390 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
));
3391 static boolean aout_link_write_symbols
3392 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
, int *symbol_map
));
3393 static boolean aout_link_write_other_symbol
3394 PARAMS ((struct aout_link_hash_entry
*, PTR
));
3395 static boolean aout_link_input_section
3396 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3397 asection
*input_section
, file_ptr
*reloff_ptr
,
3398 bfd_size_type rel_size
, int *symbol_map
));
3399 static boolean aout_link_input_section_std
3400 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3401 asection
*input_section
, struct reloc_std_external
*,
3402 bfd_size_type rel_size
, bfd_byte
*contents
, int *symbol_map
));
3403 static boolean aout_link_input_section_ext
3404 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3405 asection
*input_section
, struct reloc_ext_external
*,
3406 bfd_size_type rel_size
, bfd_byte
*contents
, int *symbol_map
));
3407 static INLINE asection
*aout_reloc_index_to_section
3408 PARAMS ((bfd
*, int));
3409 static boolean aout_link_reloc_link_order
3410 PARAMS ((struct aout_final_link_info
*, asection
*,
3411 struct bfd_link_order
*));
3413 /* Do the final link step. This is called on the output BFD. The
3414 INFO structure should point to a list of BFDs linked through the
3415 link_next field which can be used to find each BFD which takes part
3416 in the output. Also, each section in ABFD should point to a list
3417 of bfd_link_order structures which list all the input sections for
3418 the output section. */
3421 NAME(aout
,final_link
) (abfd
, info
, callback
)
3423 struct bfd_link_info
*info
;
3424 void (*callback
) PARAMS ((bfd
*, file_ptr
*, file_ptr
*, file_ptr
*));
3426 struct aout_final_link_info aout_info
;
3428 bfd_size_type text_size
;
3430 register struct bfd_link_order
*p
;
3432 boolean have_link_order_relocs
;
3434 aout_info
.info
= info
;
3435 aout_info
.output_bfd
= abfd
;
3437 if (! info
->relocateable
)
3439 exec_hdr (abfd
)->a_trsize
= 0;
3440 exec_hdr (abfd
)->a_drsize
= 0;
3444 bfd_size_type trsize
, drsize
;
3446 /* Count up the relocation sizes. */
3449 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
3451 if (bfd_get_flavour (abfd
) == bfd_target_aout_flavour
)
3453 trsize
+= exec_hdr (sub
)->a_trsize
;
3454 drsize
+= exec_hdr (sub
)->a_drsize
;
3458 /* FIXME: We need to identify the .text and .data sections
3459 and call get_reloc_upper_bound and canonicalize_reloc to
3460 work out the number of relocs needed, and then multiply
3461 by the reloc size. */
3465 if (obj_textsec (abfd
) != (asection
*) NULL
)
3466 trsize
+= (_bfd_count_link_order_relocs (obj_textsec (abfd
)
3468 * obj_reloc_entry_size (abfd
));
3469 exec_hdr (abfd
)->a_trsize
= trsize
;
3470 if (obj_datasec (abfd
) != (asection
*) NULL
)
3471 drsize
+= (_bfd_count_link_order_relocs (obj_datasec (abfd
)
3473 * obj_reloc_entry_size (abfd
));
3474 exec_hdr (abfd
)->a_drsize
= drsize
;
3477 exec_hdr (abfd
)->a_entry
= bfd_get_start_address (abfd
);
3479 /* Adjust the section sizes and vmas according to the magic number.
3480 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3481 filepos for each section. */
3482 if (! NAME(aout
,adjust_sizes_and_vmas
) (abfd
, &text_size
, &text_end
))
3485 /* The relocation and symbol file positions differ among a.out
3486 targets. We are passed a callback routine from the backend
3487 specific code to handle this.
3488 FIXME: At this point we do not know how much space the symbol
3489 table will require. This will not work for any (nonstandard)
3490 a.out target that needs to know the symbol table size before it
3491 can compute the relocation file positions. This may or may not
3492 be the case for the hp300hpux target, for example. */
3493 (*callback
) (abfd
, &aout_info
.treloff
, &aout_info
.dreloff
,
3495 obj_textsec (abfd
)->rel_filepos
= aout_info
.treloff
;
3496 obj_datasec (abfd
)->rel_filepos
= aout_info
.dreloff
;
3497 obj_sym_filepos (abfd
) = aout_info
.symoff
;
3499 /* We keep a count of the symbols as we output them. */
3500 obj_aout_external_sym_count (abfd
) = 0;
3502 /* We accumulate the string table as we write out the symbols. */
3503 stringtab_init (&aout_info
.strtab
);
3505 /* The most time efficient way to do the link would be to read all
3506 the input object files into memory and then sort out the
3507 information into the output file. Unfortunately, that will
3508 probably use too much memory. Another method would be to step
3509 through everything that composes the text section and write it
3510 out, and then everything that composes the data section and write
3511 it out, and then write out the relocs, and then write out the
3512 symbols. Unfortunately, that requires reading stuff from each
3513 input file several times, and we will not be able to keep all the
3514 input files open simultaneously, and reopening them will be slow.
3516 What we do is basically process one input file at a time. We do
3517 everything we need to do with an input file once--copy over the
3518 section contents, handle the relocation information, and write
3519 out the symbols--and then we throw away the information we read
3520 from it. This approach requires a lot of lseeks of the output
3521 file, which is unfortunate but still faster than reopening a lot
3524 We use the output_has_begun field of the input BFDs to see
3525 whether we have already handled it. */
3526 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
3527 sub
->output_has_begun
= false;
3529 have_link_order_relocs
= false;
3530 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3532 for (p
= o
->link_order_head
;
3533 p
!= (struct bfd_link_order
*) NULL
;
3536 if (p
->type
== bfd_indirect_link_order
3537 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3538 == bfd_target_aout_flavour
))
3542 input_bfd
= p
->u
.indirect
.section
->owner
;
3543 if (! input_bfd
->output_has_begun
)
3545 if (! aout_link_input_bfd (&aout_info
, input_bfd
))
3547 input_bfd
->output_has_begun
= true;
3550 else if (p
->type
== bfd_section_reloc_link_order
3551 || p
->type
== bfd_symbol_reloc_link_order
)
3553 /* These are handled below. */
3554 have_link_order_relocs
= true;
3558 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3564 /* Write out any symbols that we have not already written out. */
3565 aout_link_hash_traverse (aout_hash_table (info
),
3566 aout_link_write_other_symbol
,
3569 /* Now handle any relocs we were asked to create by the linker.
3570 These did not come from any input file. We must do these after
3571 we have written out all the symbols, so that we know the symbol
3573 if (have_link_order_relocs
)
3575 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3577 for (p
= o
->link_order_head
;
3578 p
!= (struct bfd_link_order
*) NULL
;
3581 if (p
->type
== bfd_section_reloc_link_order
3582 || p
->type
== bfd_symbol_reloc_link_order
)
3584 if (! aout_link_reloc_link_order (&aout_info
, o
, p
))
3591 /* Finish up any dynamic linking we may be doing. */
3592 if (aout_backend_info (abfd
)->finish_dynamic_link
!= NULL
)
3594 if (! (*aout_backend_info (abfd
)->finish_dynamic_link
) (abfd
, info
))
3598 /* Update the header information. */
3599 abfd
->symcount
= obj_aout_external_sym_count (abfd
);
3600 exec_hdr (abfd
)->a_syms
= abfd
->symcount
* EXTERNAL_NLIST_SIZE
;
3601 obj_str_filepos (abfd
) = obj_sym_filepos (abfd
) + exec_hdr (abfd
)->a_syms
;
3602 obj_textsec (abfd
)->reloc_count
=
3603 exec_hdr (abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
);
3604 obj_datasec (abfd
)->reloc_count
=
3605 exec_hdr (abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
);
3607 /* Write out the string table. */
3608 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0)
3610 return emit_strtab (abfd
, &aout_info
.strtab
);
3613 /* Link an a.out input BFD into the output file. */
3616 aout_link_input_bfd (finfo
, input_bfd
)
3617 struct aout_final_link_info
*finfo
;
3620 bfd_size_type sym_count
;
3621 int *symbol_map
= NULL
;
3623 BFD_ASSERT (bfd_get_format (input_bfd
) == bfd_object
);
3625 /* If this is a dynamic object, it may need special handling. */
3626 if ((input_bfd
->flags
& DYNAMIC
) != 0
3627 && aout_backend_info (input_bfd
)->link_dynamic_object
!= NULL
)
3629 return ((*aout_backend_info (input_bfd
)->link_dynamic_object
)
3630 (finfo
->info
, input_bfd
));
3633 /* Get the symbols. We probably have them already, unless
3634 finfo->info->keep_memory is false. */
3635 if (! aout_get_external_symbols (input_bfd
))
3638 sym_count
= obj_aout_external_sym_count (input_bfd
);
3639 symbol_map
= (int *) malloc ((size_t) sym_count
* sizeof (int));
3640 if (symbol_map
== NULL
&& sym_count
!= 0)
3642 bfd_set_error (bfd_error_no_memory
);
3646 /* Write out the symbols and get a map of the new indices. */
3647 if (! aout_link_write_symbols (finfo
, input_bfd
, symbol_map
))
3650 /* Relocate and write out the sections. */
3651 if (! aout_link_input_section (finfo
, input_bfd
,
3652 obj_textsec (input_bfd
),
3654 exec_hdr (input_bfd
)->a_trsize
,
3656 || ! aout_link_input_section (finfo
, input_bfd
,
3657 obj_datasec (input_bfd
),
3659 exec_hdr (input_bfd
)->a_drsize
,
3663 /* If we are not keeping memory, we don't need the symbols any
3664 longer. We still need them if we are keeping memory, because the
3665 strings in the hash table point into them. */
3666 if (! finfo
->info
->keep_memory
)
3668 if (! aout_link_free_symbols (input_bfd
))
3672 if (symbol_map
!= NULL
)
3676 if (symbol_map
!= NULL
)
3681 /* Adjust and write out the symbols for an a.out file. Set the new
3682 symbol indices into a symbol_map. */
3685 aout_link_write_symbols (finfo
, input_bfd
, symbol_map
)
3686 struct aout_final_link_info
*finfo
;
3691 bfd_size_type sym_count
;
3693 enum bfd_link_strip strip
;
3694 enum bfd_link_discard discard
;
3695 struct external_nlist
*output_syms
= NULL
;
3696 struct external_nlist
*outsym
;
3697 register struct external_nlist
*sym
;
3698 struct external_nlist
*sym_end
;
3699 struct aout_link_hash_entry
**sym_hash
;
3701 boolean skip_indirect
;
3703 output_bfd
= finfo
->output_bfd
;
3704 sym_count
= obj_aout_external_sym_count (input_bfd
);
3705 strings
= obj_aout_external_strings (input_bfd
);
3706 strip
= finfo
->info
->strip
;
3707 discard
= finfo
->info
->discard
;
3708 output_syms
= ((struct external_nlist
*)
3709 malloc ((size_t) (sym_count
+ 1) * EXTERNAL_NLIST_SIZE
));
3710 if (output_syms
== NULL
)
3712 bfd_set_error (bfd_error_no_memory
);
3715 outsym
= output_syms
;
3717 /* First write out a symbol for this object file, unless we are
3718 discarding such symbols. */
3719 if (strip
!= strip_all
3720 && (strip
!= strip_some
3721 || bfd_hash_lookup (finfo
->info
->keep_hash
, input_bfd
->filename
,
3722 false, false) != NULL
)
3723 && discard
!= discard_all
)
3725 bfd_h_put_8 (output_bfd
, N_TEXT
, outsym
->e_type
);
3726 bfd_h_put_8 (output_bfd
, 0, outsym
->e_other
);
3727 bfd_h_put_16 (output_bfd
, (bfd_vma
) 0, outsym
->e_desc
);
3728 PUT_WORD (output_bfd
,
3729 add_to_stringtab (output_bfd
, input_bfd
->filename
,
3732 PUT_WORD (output_bfd
,
3733 (bfd_get_section_vma (output_bfd
,
3734 obj_textsec (input_bfd
)->output_section
)
3735 + obj_textsec (input_bfd
)->output_offset
),
3737 ++obj_aout_external_sym_count (output_bfd
);
3742 skip_indirect
= false;
3743 sym
= obj_aout_external_syms (input_bfd
);
3744 sym_end
= sym
+ sym_count
;
3745 sym_hash
= obj_aout_sym_hashes (input_bfd
);
3746 for (; sym
< sym_end
; sym
++, sym_hash
++, symbol_map
++)
3750 struct aout_link_hash_entry
*h
;
3757 type
= bfd_h_get_8 (input_bfd
, sym
->e_type
);
3758 name
= strings
+ GET_WORD (input_bfd
, sym
->e_strx
);
3764 /* Pass this symbol through. It is the target of an
3765 indirect or warning symbol. */
3766 val
= GET_WORD (input_bfd
, sym
->e_value
);
3769 else if (skip_indirect
)
3771 /* Skip this symbol, which is the target of an indirect
3772 symbol that we have changed to no longer be an indirect
3774 skip_indirect
= false;
3779 struct aout_link_hash_entry
*hresolve
;
3781 /* We have saved the hash table entry for this symbol, if
3782 there is one. Note that we could just look it up again
3783 in the hash table, provided we first check that it is an
3787 /* If this is an indirect or warning symbol, then change
3788 hresolve to the base symbol. We also change *sym_hash so
3789 that the relocation routines relocate against the real
3792 if (h
!= (struct aout_link_hash_entry
*) NULL
3793 && (h
->root
.type
== bfd_link_hash_indirect
3794 || h
->root
.type
== bfd_link_hash_warning
))
3796 hresolve
= (struct aout_link_hash_entry
*) h
->root
.u
.i
.link
;
3797 while (hresolve
->root
.type
== bfd_link_hash_indirect
3798 || hresolve
->root
.type
== bfd_link_hash_warning
)
3799 hresolve
= ((struct aout_link_hash_entry
*)
3800 hresolve
->root
.u
.i
.link
);
3801 *sym_hash
= hresolve
;
3804 /* If the symbol has already been written out, skip it. */
3805 if (h
!= (struct aout_link_hash_entry
*) NULL
3806 && h
->root
.type
!= bfd_link_hash_warning
3809 if ((type
& N_TYPE
) == N_INDR
)
3810 skip_indirect
= true;
3811 *symbol_map
= h
->indx
;
3815 /* See if we are stripping this symbol. */
3821 case strip_debugger
:
3822 if ((type
& N_STAB
) != 0)
3826 if (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
3836 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3841 /* Get the value of the symbol. */
3842 if ((type
& N_TYPE
) == N_TEXT
3844 symsec
= obj_textsec (input_bfd
);
3845 else if ((type
& N_TYPE
) == N_DATA
3847 symsec
= obj_datasec (input_bfd
);
3848 else if ((type
& N_TYPE
) == N_BSS
3850 symsec
= obj_bsssec (input_bfd
);
3851 else if ((type
& N_TYPE
) == N_ABS
3853 symsec
= &bfd_abs_section
;
3854 else if (((type
& N_TYPE
) == N_INDR
3855 && (hresolve
== (struct aout_link_hash_entry
*) NULL
3856 || (hresolve
->root
.type
!= bfd_link_hash_defined
3857 && hresolve
->root
.type
!= bfd_link_hash_common
)))
3858 || type
== N_WARNING
)
3860 /* Pass the next symbol through unchanged. The
3861 condition above for indirect symbols is so that if
3862 the indirect symbol was defined, we output it with
3863 the correct definition so the debugger will
3866 val
= GET_WORD (input_bfd
, sym
->e_value
);
3869 else if ((type
& N_STAB
) != 0)
3871 val
= GET_WORD (input_bfd
, sym
->e_value
);
3876 /* If we get here with an indirect symbol, it means that
3877 we are outputting it with a real definition. In such
3878 a case we do not want to output the next symbol,
3879 which is the target of the indirection. */
3880 if ((type
& N_TYPE
) == N_INDR
)
3881 skip_indirect
= true;
3883 /* We need to get the value from the hash table. We use
3884 hresolve so that if we have defined an indirect
3885 symbol we output the final definition. */
3886 if (h
== (struct aout_link_hash_entry
*) NULL
)
3888 else if (hresolve
->root
.type
== bfd_link_hash_defined
)
3890 asection
*input_section
;
3891 asection
*output_section
;
3893 /* This case means a common symbol which was turned
3894 into a defined symbol. */
3895 input_section
= hresolve
->root
.u
.def
.section
;
3896 output_section
= input_section
->output_section
;
3897 BFD_ASSERT (output_section
== &bfd_abs_section
3898 || output_section
->owner
== output_bfd
);
3899 val
= (hresolve
->root
.u
.def
.value
3900 + bfd_get_section_vma (output_bfd
, output_section
)
3901 + input_section
->output_offset
);
3903 /* Get the correct type based on the section. If
3904 this is a constructed set, force it to be
3905 globally visible. */
3914 if (output_section
== obj_textsec (output_bfd
))
3916 else if (output_section
== obj_datasec (output_bfd
))
3918 else if (output_section
== obj_bsssec (output_bfd
))
3923 else if (hresolve
->root
.type
== bfd_link_hash_common
)
3924 val
= hresolve
->root
.u
.c
.size
;
3925 else if (hresolve
->root
.type
== bfd_link_hash_weak
)
3935 if (symsec
!= (asection
*) NULL
)
3936 val
= (symsec
->output_section
->vma
3937 + symsec
->output_offset
3938 + (GET_WORD (input_bfd
, sym
->e_value
)
3941 /* If this is a global symbol set the written flag, and if
3942 it is a local symbol see if we should discard it. */
3943 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3946 h
->indx
= obj_aout_external_sym_count (output_bfd
);
3955 if (*name
== *finfo
->info
->lprefix
3956 && (finfo
->info
->lprefix_len
== 1
3957 || strncmp (name
, finfo
->info
->lprefix
,
3958 finfo
->info
->lprefix_len
) == 0))
3973 /* Copy this symbol into the list of symbols we are going to
3975 bfd_h_put_8 (output_bfd
, type
, outsym
->e_type
);
3976 bfd_h_put_8 (output_bfd
, bfd_h_get_8 (input_bfd
, sym
->e_other
),
3978 bfd_h_put_16 (output_bfd
, bfd_h_get_16 (input_bfd
, sym
->e_desc
),
3980 if (! finfo
->info
->keep_memory
)
3982 /* name points into a string table which we are going to
3983 free. If there is a hash table entry, use that string.
3984 Otherwise, copy name into memory. */
3985 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3986 name
= (*sym_hash
)->root
.root
.string
;
3991 n
= bfd_alloc (output_bfd
, strlen (name
) + 1);
3996 PUT_WORD (output_bfd
,
3997 add_to_stringtab (output_bfd
, name
, &finfo
->strtab
),
3999 PUT_WORD (output_bfd
, val
, outsym
->e_value
);
4000 *symbol_map
= obj_aout_external_sym_count (output_bfd
);
4001 ++obj_aout_external_sym_count (output_bfd
);
4005 /* Write out the output symbols we have just constructed. */
4006 if (outsym
> output_syms
)
4008 bfd_size_type outsym_count
;
4010 if (bfd_seek (output_bfd
, finfo
->symoff
, SEEK_SET
) != 0)
4012 outsym_count
= outsym
- output_syms
;
4013 if (bfd_write ((PTR
) output_syms
, (bfd_size_type
) EXTERNAL_NLIST_SIZE
,
4014 (bfd_size_type
) outsym_count
, output_bfd
)
4015 != outsym_count
* EXTERNAL_NLIST_SIZE
)
4017 finfo
->symoff
+= outsym_count
* EXTERNAL_NLIST_SIZE
;
4020 if (output_syms
!= NULL
)
4024 if (output_syms
!= NULL
)
4029 /* Write out a symbol that was not associated with an a.out input
4033 aout_link_write_other_symbol (h
, data
)
4034 struct aout_link_hash_entry
*h
;
4037 struct aout_final_link_info
*finfo
= (struct aout_final_link_info
*) data
;
4041 struct external_nlist outsym
;
4043 output_bfd
= finfo
->output_bfd
;
4045 if (aout_backend_info (output_bfd
)->write_dynamic_symbol
!= NULL
)
4047 if (! ((*aout_backend_info (output_bfd
)->write_dynamic_symbol
)
4048 (output_bfd
, finfo
->info
, h
)))
4050 /* FIXME: No way to handle errors. */
4060 if (finfo
->info
->strip
== strip_all
4061 || (finfo
->info
->strip
== strip_some
4062 && bfd_hash_lookup (finfo
->info
->keep_hash
, h
->root
.root
.string
,
4063 false, false) == NULL
))
4066 switch (h
->root
.type
)
4069 case bfd_link_hash_new
:
4071 /* Avoid variable not initialized warnings. */
4073 case bfd_link_hash_undefined
:
4074 type
= N_UNDF
| N_EXT
;
4077 case bfd_link_hash_defined
:
4081 sec
= h
->root
.u
.def
.section
->output_section
;
4082 BFD_ASSERT (sec
== &bfd_abs_section
4083 || sec
->owner
== output_bfd
);
4084 if (sec
== obj_textsec (output_bfd
))
4085 type
= N_TEXT
| N_EXT
;
4086 else if (sec
== obj_datasec (output_bfd
))
4087 type
= N_DATA
| N_EXT
;
4088 else if (sec
== obj_bsssec (output_bfd
))
4089 type
= N_BSS
| N_EXT
;
4091 type
= N_ABS
| N_EXT
;
4092 val
= (h
->root
.u
.def
.value
4094 + h
->root
.u
.def
.section
->output_offset
);
4097 case bfd_link_hash_common
:
4098 type
= N_UNDF
| N_EXT
;
4099 val
= h
->root
.u
.c
.size
;
4101 case bfd_link_hash_weak
:
4104 case bfd_link_hash_indirect
:
4105 case bfd_link_hash_warning
:
4106 /* FIXME: Ignore these for now. The circumstances under which
4107 they should be written out are not clear to me. */
4111 bfd_h_put_8 (output_bfd
, type
, outsym
.e_type
);
4112 bfd_h_put_8 (output_bfd
, 0, outsym
.e_other
);
4113 bfd_h_put_16 (output_bfd
, 0, outsym
.e_desc
);
4114 PUT_WORD (output_bfd
,
4115 add_to_stringtab (output_bfd
, h
->root
.root
.string
, &finfo
->strtab
),
4117 PUT_WORD (output_bfd
, val
, outsym
.e_value
);
4119 if (bfd_seek (output_bfd
, finfo
->symoff
, SEEK_SET
) != 0
4120 || bfd_write ((PTR
) &outsym
, (bfd_size_type
) EXTERNAL_NLIST_SIZE
,
4121 (bfd_size_type
) 1, output_bfd
) != EXTERNAL_NLIST_SIZE
)
4123 /* FIXME: No way to handle errors. */
4127 finfo
->symoff
+= EXTERNAL_NLIST_SIZE
;
4128 h
->indx
= obj_aout_external_sym_count (output_bfd
);
4129 ++obj_aout_external_sym_count (output_bfd
);
4134 /* Link an a.out section into the output file. */
4137 aout_link_input_section (finfo
, input_bfd
, input_section
, reloff_ptr
,
4138 rel_size
, symbol_map
)
4139 struct aout_final_link_info
*finfo
;
4141 asection
*input_section
;
4142 file_ptr
*reloff_ptr
;
4143 bfd_size_type rel_size
;
4146 bfd_size_type input_size
;
4147 bfd_byte
*contents
= NULL
;
4149 PTR free_relocs
= NULL
;
4151 /* Get the section contents. */
4152 input_size
= bfd_section_size (input_bfd
, input_section
);
4153 contents
= (bfd_byte
*) malloc (input_size
);
4154 if (contents
== NULL
&& input_size
!= 0)
4156 bfd_set_error (bfd_error_no_memory
);
4159 if (! bfd_get_section_contents (input_bfd
, input_section
, (PTR
) contents
,
4160 (file_ptr
) 0, input_size
))
4163 /* Read in the relocs if we haven't already done it. */
4164 if (aout_section_data (input_section
) != NULL
4165 && aout_section_data (input_section
)->relocs
!= NULL
)
4166 relocs
= aout_section_data (input_section
)->relocs
;
4169 relocs
= free_relocs
= (PTR
) malloc (rel_size
);
4170 if (relocs
== NULL
&& rel_size
!= 0)
4172 bfd_set_error (bfd_error_no_memory
);
4175 if (bfd_seek (input_bfd
, input_section
->rel_filepos
, SEEK_SET
) != 0
4176 || bfd_read (relocs
, 1, rel_size
, input_bfd
) != rel_size
)
4180 /* Relocate the section contents. */
4181 if (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
)
4183 if (! aout_link_input_section_std (finfo
, input_bfd
, input_section
,
4184 (struct reloc_std_external
*) relocs
,
4185 rel_size
, contents
, symbol_map
))
4190 if (! aout_link_input_section_ext (finfo
, input_bfd
, input_section
,
4191 (struct reloc_ext_external
*) relocs
,
4192 rel_size
, contents
, symbol_map
))
4196 /* Write out the section contents. */
4197 if (! bfd_set_section_contents (finfo
->output_bfd
,
4198 input_section
->output_section
,
4200 input_section
->output_offset
,
4204 /* If we are producing relocateable output, the relocs were
4205 modified, and we now write them out. */
4206 if (finfo
->info
->relocateable
)
4208 if (bfd_seek (finfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0)
4210 if (bfd_write (relocs
, (bfd_size_type
) 1, rel_size
, finfo
->output_bfd
)
4213 *reloff_ptr
+= rel_size
;
4215 /* Assert that the relocs have not run into the symbols, and
4216 that if these are the text relocs they have not run into the
4218 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (finfo
->output_bfd
)
4219 && (reloff_ptr
!= &finfo
->treloff
4221 <= obj_datasec (finfo
->output_bfd
)->rel_filepos
)));
4224 if (free_relocs
!= NULL
)
4226 if (contents
!= NULL
)
4230 if (free_relocs
!= NULL
)
4232 if (contents
!= NULL
)
4237 /* Get the section corresponding to a reloc index. */
4239 static INLINE asection
*
4240 aout_reloc_index_to_section (abfd
, indx
)
4244 switch (indx
& N_TYPE
)
4247 return obj_textsec (abfd
);
4249 return obj_datasec (abfd
);
4251 return obj_bsssec (abfd
);
4254 return &bfd_abs_section
;
4260 /* Relocate an a.out section using standard a.out relocs. */
4263 aout_link_input_section_std (finfo
, input_bfd
, input_section
, relocs
,
4264 rel_size
, contents
, symbol_map
)
4265 struct aout_final_link_info
*finfo
;
4267 asection
*input_section
;
4268 struct reloc_std_external
*relocs
;
4269 bfd_size_type rel_size
;
4273 boolean (*check_dynamic_reloc
) PARAMS ((struct bfd_link_info
*,
4275 struct aout_link_hash_entry
*,
4278 boolean relocateable
;
4279 struct external_nlist
*syms
;
4281 struct aout_link_hash_entry
**sym_hashes
;
4282 bfd_size_type reloc_count
;
4283 register struct reloc_std_external
*rel
;
4284 struct reloc_std_external
*rel_end
;
4286 output_bfd
= finfo
->output_bfd
;
4287 check_dynamic_reloc
= aout_backend_info (output_bfd
)->check_dynamic_reloc
;
4289 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
);
4290 BFD_ASSERT (input_bfd
->xvec
->header_byteorder_big_p
4291 == output_bfd
->xvec
->header_byteorder_big_p
);
4293 relocateable
= finfo
->info
->relocateable
;
4294 syms
= obj_aout_external_syms (input_bfd
);
4295 strings
= obj_aout_external_strings (input_bfd
);
4296 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4298 reloc_count
= rel_size
/ RELOC_STD_SIZE
;
4300 rel_end
= rel
+ reloc_count
;
4301 for (; rel
< rel_end
; rel
++)
4313 bfd_reloc_status_type r
;
4315 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4317 if (input_bfd
->xvec
->header_byteorder_big_p
)
4319 r_index
= ((rel
->r_index
[0] << 16)
4320 | (rel
->r_index
[1] << 8)
4322 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
4323 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
4324 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
4325 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
4326 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
4327 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
4328 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
4332 r_index
= ((rel
->r_index
[2] << 16)
4333 | (rel
->r_index
[1] << 8)
4335 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
4336 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
4337 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
4338 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
4339 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
4340 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
4341 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
4344 howto_idx
= r_length
+ 4 * r_pcrel
+ 8 * r_baserel
4345 + 16 * r_jmptable
+ 32 * r_relative
;
4346 BFD_ASSERT (howto_idx
< TABLE_SIZE (howto_table_std
));
4350 /* We are generating a relocateable output file, and must
4351 modify the reloc accordingly. */
4354 struct aout_link_hash_entry
*h
;
4356 /* If we know the symbol this relocation is against,
4357 convert it into a relocation against a section. This
4358 is what the native linker does. */
4359 h
= sym_hashes
[r_index
];
4360 if (h
!= (struct aout_link_hash_entry
*) NULL
4361 && h
->root
.type
== bfd_link_hash_defined
)
4363 asection
*output_section
;
4365 /* Change the r_extern value. */
4366 if (output_bfd
->xvec
->header_byteorder_big_p
)
4367 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_BIG
;
4369 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE
;
4371 /* Compute a new r_index. */
4372 output_section
= h
->root
.u
.def
.section
->output_section
;
4373 if (output_section
== obj_textsec (output_bfd
))
4375 else if (output_section
== obj_datasec (output_bfd
))
4377 else if (output_section
== obj_bsssec (output_bfd
))
4382 /* Add the symbol value and the section VMA to the
4383 addend stored in the contents. */
4384 relocation
= (h
->root
.u
.def
.value
4385 + output_section
->vma
4386 + h
->root
.u
.def
.section
->output_offset
);
4390 /* We must change r_index according to the symbol
4392 r_index
= symbol_map
[r_index
];
4398 name
= strings
+ GET_WORD (input_bfd
,
4399 syms
[r_index
].e_strx
);
4400 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4401 (finfo
->info
, name
, input_bfd
, input_section
,
4410 /* Write out the new r_index value. */
4411 if (output_bfd
->xvec
->header_byteorder_big_p
)
4413 rel
->r_index
[0] = r_index
>> 16;
4414 rel
->r_index
[1] = r_index
>> 8;
4415 rel
->r_index
[2] = r_index
;
4419 rel
->r_index
[2] = r_index
>> 16;
4420 rel
->r_index
[1] = r_index
>> 8;
4421 rel
->r_index
[0] = r_index
;
4428 /* This is a relocation against a section. We must
4429 adjust by the amount that the section moved. */
4430 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4431 relocation
= (section
->output_section
->vma
4432 + section
->output_offset
4436 /* Change the address of the relocation. */
4437 PUT_WORD (output_bfd
,
4438 r_addr
+ input_section
->output_offset
,
4441 /* Adjust a PC relative relocation by removing the reference
4442 to the original address in the section and including the
4443 reference to the new address. */
4445 relocation
-= (input_section
->output_section
->vma
4446 + input_section
->output_offset
4447 - input_section
->vma
);
4449 if (relocation
== 0)
4452 r
= _bfd_relocate_contents (howto_table_std
+ howto_idx
,
4453 input_bfd
, relocation
,
4458 /* We are generating an executable, and must do a full
4462 struct aout_link_hash_entry
*h
;
4464 h
= sym_hashes
[r_index
];
4466 if (check_dynamic_reloc
!= NULL
)
4470 if (! ((*check_dynamic_reloc
)
4471 (finfo
->info
, input_bfd
, input_section
, h
,
4478 if (h
!= (struct aout_link_hash_entry
*) NULL
4479 && h
->root
.type
== bfd_link_hash_defined
)
4481 relocation
= (h
->root
.u
.def
.value
4482 + h
->root
.u
.def
.section
->output_section
->vma
4483 + h
->root
.u
.def
.section
->output_offset
);
4485 else if (h
!= (struct aout_link_hash_entry
*) NULL
4486 && h
->root
.type
== bfd_link_hash_weak
)
4492 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4493 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4494 (finfo
->info
, name
, input_bfd
, input_section
,
4504 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4505 relocation
= (section
->output_section
->vma
4506 + section
->output_offset
4509 relocation
+= input_section
->vma
;
4512 r
= _bfd_final_link_relocate (howto_table_std
+ howto_idx
,
4513 input_bfd
, input_section
,
4514 contents
, r_addr
, relocation
,
4518 if (r
!= bfd_reloc_ok
)
4523 case bfd_reloc_outofrange
:
4525 case bfd_reloc_overflow
:
4530 name
= strings
+ GET_WORD (input_bfd
,
4531 syms
[r_index
].e_strx
);
4536 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4537 name
= bfd_section_name (input_bfd
, s
);
4539 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4540 (finfo
->info
, name
, howto_table_std
[howto_idx
].name
,
4541 (bfd_vma
) 0, input_bfd
, input_section
, r_addr
)))
4552 /* Relocate an a.out section using extended a.out relocs. */
4555 aout_link_input_section_ext (finfo
, input_bfd
, input_section
, relocs
,
4556 rel_size
, contents
, symbol_map
)
4557 struct aout_final_link_info
*finfo
;
4559 asection
*input_section
;
4560 struct reloc_ext_external
*relocs
;
4561 bfd_size_type rel_size
;
4565 boolean (*check_dynamic_reloc
) PARAMS ((struct bfd_link_info
*,
4567 struct aout_link_hash_entry
*,
4570 boolean relocateable
;
4571 struct external_nlist
*syms
;
4573 struct aout_link_hash_entry
**sym_hashes
;
4574 bfd_size_type reloc_count
;
4575 register struct reloc_ext_external
*rel
;
4576 struct reloc_ext_external
*rel_end
;
4578 output_bfd
= finfo
->output_bfd
;
4579 check_dynamic_reloc
= aout_backend_info (output_bfd
)->check_dynamic_reloc
;
4581 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_EXT_SIZE
);
4582 BFD_ASSERT (input_bfd
->xvec
->header_byteorder_big_p
4583 == output_bfd
->xvec
->header_byteorder_big_p
);
4585 relocateable
= finfo
->info
->relocateable
;
4586 syms
= obj_aout_external_syms (input_bfd
);
4587 strings
= obj_aout_external_strings (input_bfd
);
4588 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4590 reloc_count
= rel_size
/ RELOC_EXT_SIZE
;
4592 rel_end
= rel
+ reloc_count
;
4593 for (; rel
< rel_end
; rel
++)
4602 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4604 if (input_bfd
->xvec
->header_byteorder_big_p
)
4606 r_index
= ((rel
->r_index
[0] << 16)
4607 | (rel
->r_index
[1] << 8)
4609 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
4610 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
4611 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
4615 r_index
= ((rel
->r_index
[2] << 16)
4616 | (rel
->r_index
[1] << 8)
4618 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
4619 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
4620 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
4623 r_addend
= GET_SWORD (input_bfd
, rel
->r_addend
);
4625 BFD_ASSERT (r_type
>= 0
4626 && r_type
< TABLE_SIZE (howto_table_ext
));
4630 /* We are generating a relocateable output file, and must
4631 modify the reloc accordingly. */
4634 struct aout_link_hash_entry
*h
;
4636 /* If we know the symbol this relocation is against,
4637 convert it into a relocation against a section. This
4638 is what the native linker does. */
4639 h
= sym_hashes
[r_index
];
4640 if (h
!= (struct aout_link_hash_entry
*) NULL
4641 && h
->root
.type
== bfd_link_hash_defined
)
4643 asection
*output_section
;
4645 /* Change the r_extern value. */
4646 if (output_bfd
->xvec
->header_byteorder_big_p
)
4647 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_BIG
;
4649 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE
;
4651 /* Compute a new r_index. */
4652 output_section
= h
->root
.u
.def
.section
->output_section
;
4653 if (output_section
== obj_textsec (output_bfd
))
4655 else if (output_section
== obj_datasec (output_bfd
))
4657 else if (output_section
== obj_bsssec (output_bfd
))
4662 /* Add the symbol value and the section VMA to the
4664 relocation
= (h
->root
.u
.def
.value
4665 + output_section
->vma
4666 + h
->root
.u
.def
.section
->output_offset
);
4668 /* Now RELOCATION is the VMA of the final
4669 destination. If this is a PC relative reloc,
4670 then ADDEND is the negative of the source VMA.
4671 We want to set ADDEND to the difference between
4672 the destination VMA and the source VMA, which
4673 means we must adjust RELOCATION by the change in
4674 the source VMA. This is done below. */
4678 /* We must change r_index according to the symbol
4680 r_index
= symbol_map
[r_index
];
4687 + GET_WORD (input_bfd
, syms
[r_index
].e_strx
));
4688 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4689 (finfo
->info
, name
, input_bfd
, input_section
,
4697 /* If this is a PC relative reloc, then the addend
4698 is the negative of the source VMA. We must
4699 adjust it by the change in the source VMA. This
4703 /* Write out the new r_index value. */
4704 if (output_bfd
->xvec
->header_byteorder_big_p
)
4706 rel
->r_index
[0] = r_index
>> 16;
4707 rel
->r_index
[1] = r_index
>> 8;
4708 rel
->r_index
[2] = r_index
;
4712 rel
->r_index
[2] = r_index
>> 16;
4713 rel
->r_index
[1] = r_index
>> 8;
4714 rel
->r_index
[0] = r_index
;
4721 /* This is a relocation against a section. We must
4722 adjust by the amount that the section moved. */
4723 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4724 relocation
= (section
->output_section
->vma
4725 + section
->output_offset
4728 /* If this is a PC relative reloc, then the addend is
4729 the difference in VMA between the destination and the
4730 source. We have just adjusted for the change in VMA
4731 of the destination, so we must also adjust by the
4732 change in VMA of the source. This is done below. */
4735 /* As described above, we must always adjust a PC relative
4736 reloc by the change in VMA of the source. */
4737 if (howto_table_ext
[r_type
].pc_relative
)
4738 relocation
-= (input_section
->output_section
->vma
4739 + input_section
->output_offset
4740 - input_section
->vma
);
4742 /* Change the addend if necessary. */
4743 if (relocation
!= 0)
4744 PUT_WORD (output_bfd
, r_addend
+ relocation
, rel
->r_addend
);
4746 /* Change the address of the relocation. */
4747 PUT_WORD (output_bfd
,
4748 r_addr
+ input_section
->output_offset
,
4753 bfd_reloc_status_type r
;
4755 /* We are generating an executable, and must do a full
4759 struct aout_link_hash_entry
*h
;
4761 h
= sym_hashes
[r_index
];
4763 if (check_dynamic_reloc
!= NULL
)
4767 if (! ((*check_dynamic_reloc
)
4768 (finfo
->info
, input_bfd
, input_section
, h
,
4775 if (h
!= (struct aout_link_hash_entry
*) NULL
4776 && h
->root
.type
== bfd_link_hash_defined
)
4778 relocation
= (h
->root
.u
.def
.value
4779 + h
->root
.u
.def
.section
->output_section
->vma
4780 + h
->root
.u
.def
.section
->output_offset
);
4782 else if (h
!= (struct aout_link_hash_entry
*) NULL
4783 && h
->root
.type
== bfd_link_hash_weak
)
4789 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4790 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4791 (finfo
->info
, name
, input_bfd
, input_section
,
4801 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4803 /* If this is a PC relative reloc, then R_ADDEND is the
4804 difference between the two vmas, or
4805 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4807 old_dest_sec == section->vma
4809 old_src_sec == input_section->vma
4811 old_src_off == r_addr
4813 _bfd_final_link_relocate expects RELOCATION +
4814 R_ADDEND to be the VMA of the destination minus
4815 r_addr (the minus r_addr is because this relocation
4816 is not pcrel_offset, which is a bit confusing and
4817 should, perhaps, be changed), or
4820 new_dest_sec == output_section->vma + output_offset
4821 We arrange for this to happen by setting RELOCATION to
4822 new_dest_sec + old_src_sec - old_dest_sec
4824 If this is not a PC relative reloc, then R_ADDEND is
4825 simply the VMA of the destination, so we set
4826 RELOCATION to the change in the destination VMA, or
4827 new_dest_sec - old_dest_sec
4829 relocation
= (section
->output_section
->vma
4830 + section
->output_offset
4832 if (howto_table_ext
[r_type
].pc_relative
)
4833 relocation
+= input_section
->vma
;
4836 r
= _bfd_final_link_relocate (howto_table_ext
+ r_type
,
4837 input_bfd
, input_section
,
4838 contents
, r_addr
, relocation
,
4840 if (r
!= bfd_reloc_ok
)
4845 case bfd_reloc_outofrange
:
4847 case bfd_reloc_overflow
:
4852 name
= strings
+ GET_WORD (input_bfd
,
4853 syms
[r_index
].e_strx
);
4858 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4859 name
= bfd_section_name (input_bfd
, s
);
4861 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4862 (finfo
->info
, name
, howto_table_ext
[r_type
].name
,
4863 r_addend
, input_bfd
, input_section
, r_addr
)))
4875 /* Handle a link order which is supposed to generate a reloc. */
4878 aout_link_reloc_link_order (finfo
, o
, p
)
4879 struct aout_final_link_info
*finfo
;
4881 struct bfd_link_order
*p
;
4883 struct bfd_link_order_reloc
*pr
;
4886 const reloc_howto_type
*howto
;
4887 file_ptr
*reloff_ptr
;
4888 struct reloc_std_external srel
;
4889 struct reloc_ext_external erel
;
4894 if (p
->type
== bfd_section_reloc_link_order
)
4897 if (pr
->u
.section
== &bfd_abs_section
)
4898 r_index
= N_ABS
| N_EXT
;
4901 BFD_ASSERT (pr
->u
.section
->owner
== finfo
->output_bfd
);
4902 r_index
= pr
->u
.section
->target_index
;
4907 struct aout_link_hash_entry
*h
;
4909 BFD_ASSERT (p
->type
== bfd_symbol_reloc_link_order
);
4911 h
= aout_link_hash_lookup (aout_hash_table (finfo
->info
),
4912 pr
->u
.name
, false, false, true);
4913 if (h
!= (struct aout_link_hash_entry
*) NULL
4918 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4919 (finfo
->info
, pr
->u
.name
, (bfd
*) NULL
,
4920 (asection
*) NULL
, (bfd_vma
) 0)))
4926 howto
= bfd_reloc_type_lookup (finfo
->output_bfd
, pr
->reloc
);
4927 if (howto
== (const reloc_howto_type
*) NULL
)
4929 bfd_set_error (bfd_error_bad_value
);
4933 if (o
== obj_textsec (finfo
->output_bfd
))
4934 reloff_ptr
= &finfo
->treloff
;
4935 else if (o
== obj_datasec (finfo
->output_bfd
))
4936 reloff_ptr
= &finfo
->dreloff
;
4940 if (obj_reloc_entry_size (finfo
->output_bfd
) == RELOC_STD_SIZE
)
4948 r_pcrel
= howto
->pc_relative
;
4949 r_baserel
= (howto
->type
& 8) != 0;
4950 r_jmptable
= (howto
->type
& 16) != 0;
4951 r_relative
= (howto
->type
& 32) != 0;
4952 r_length
= howto
->size
;
4954 PUT_WORD (finfo
->output_bfd
, p
->offset
, srel
.r_address
);
4955 if (finfo
->output_bfd
->xvec
->header_byteorder_big_p
)
4957 srel
.r_index
[0] = r_index
>> 16;
4958 srel
.r_index
[1] = r_index
>> 8;
4959 srel
.r_index
[2] = r_index
;
4961 ((r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
4962 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
4963 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
4964 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
4965 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
4966 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
4970 srel
.r_index
[2] = r_index
>> 16;
4971 srel
.r_index
[1] = r_index
>> 8;
4972 srel
.r_index
[0] = r_index
;
4974 ((r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
4975 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
4976 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
4977 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
4978 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
4979 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
4982 rel_ptr
= (PTR
) &srel
;
4984 /* We have to write the addend into the object file, since
4985 standard a.out relocs are in place. It would be more
4986 reliable if we had the current contents of the file here,
4987 rather than assuming zeroes, but we can't read the file since
4988 it was opened using bfd_openw. */
4989 if (pr
->addend
!= 0)
4992 bfd_reloc_status_type r
;
4996 size
= bfd_get_reloc_size (howto
);
4997 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4998 if (buf
== (bfd_byte
*) NULL
)
5000 bfd_set_error (bfd_error_no_memory
);
5003 r
= _bfd_relocate_contents (howto
, finfo
->output_bfd
,
5010 case bfd_reloc_outofrange
:
5012 case bfd_reloc_overflow
:
5013 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
5015 (p
->type
== bfd_section_reloc_link_order
5016 ? bfd_section_name (finfo
->output_bfd
,
5019 howto
->name
, pr
->addend
, (bfd
*) NULL
,
5020 (asection
*) NULL
, (bfd_vma
) 0)))
5027 ok
= bfd_set_section_contents (finfo
->output_bfd
, o
,
5029 (file_ptr
) p
->offset
,
5038 PUT_WORD (finfo
->output_bfd
, p
->offset
, erel
.r_address
);
5040 if (finfo
->output_bfd
->xvec
->header_byteorder_big_p
)
5042 erel
.r_index
[0] = r_index
>> 16;
5043 erel
.r_index
[1] = r_index
>> 8;
5044 erel
.r_index
[2] = r_index
;
5046 ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
5047 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
5051 erel
.r_index
[2] = r_index
>> 16;
5052 erel
.r_index
[1] = r_index
>> 8;
5053 erel
.r_index
[0] = r_index
;
5055 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
5056 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
5059 PUT_WORD (finfo
->output_bfd
, pr
->addend
, erel
.r_addend
);
5061 rel_ptr
= (PTR
) &erel
;
5064 if (bfd_seek (finfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0
5065 || (bfd_write (rel_ptr
, (bfd_size_type
) 1,
5066 obj_reloc_entry_size (finfo
->output_bfd
),
5068 != obj_reloc_entry_size (finfo
->output_bfd
)))
5071 *reloff_ptr
+= obj_reloc_entry_size (finfo
->output_bfd
);
5073 /* Assert that the relocs have not run into the symbols, and that n
5074 the text relocs have not run into the data relocs. */
5075 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (finfo
->output_bfd
)
5076 && (reloff_ptr
!= &finfo
->treloff
5078 <= obj_datasec (finfo
->output_bfd
)->rel_filepos
)));