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
125 #include <string.h> /* For strchr and friends */
132 #include "aout/aout64.h"
133 #include "aout/stab_gnu.h"
136 static boolean translate_symbol_table
PARAMS ((bfd
*, aout_symbol_type
*,
137 struct external_nlist
*,
138 bfd_size_type
, char *,
147 The file @file{aoutx.h} provides for both the @emph{standard}
148 and @emph{extended} forms of a.out relocation records.
150 The standard records contain only an
151 address, a symbol index, and a type field. The extended records
152 (used on 29ks and sparcs) also have a full integer for an
156 #define CTOR_TABLE_RELOC_IDX 2
158 #define howto_table_ext NAME(aout,ext_howto_table)
159 #define howto_table_std NAME(aout,std_howto_table)
161 reloc_howto_type howto_table_ext
[] =
163 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
164 HOWTO(RELOC_8
, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", false, 0,0x000000ff, false),
165 HOWTO(RELOC_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", false, 0,0x0000ffff, false),
166 HOWTO(RELOC_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", false, 0,0xffffffff, false),
167 HOWTO(RELOC_DISP8
, 0, 0, 8, true, 0, complain_overflow_signed
,0,"DISP8", false, 0,0x000000ff, false),
168 HOWTO(RELOC_DISP16
, 0, 1, 16, true, 0, complain_overflow_signed
,0,"DISP16", false, 0,0x0000ffff, false),
169 HOWTO(RELOC_DISP32
, 0, 2, 32, true, 0, complain_overflow_signed
,0,"DISP32", false, 0,0xffffffff, false),
170 HOWTO(RELOC_WDISP30
,2, 2, 30, true, 0, complain_overflow_signed
,0,"WDISP30", false, 0,0x3fffffff, false),
171 HOWTO(RELOC_WDISP22
,2, 2, 22, true, 0, complain_overflow_signed
,0,"WDISP22", false, 0,0x003fffff, false),
172 HOWTO(RELOC_HI22
, 10, 2, 22, false, 0, complain_overflow_bitfield
,0,"HI22", false, 0,0x003fffff, false),
173 HOWTO(RELOC_22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"22", false, 0,0x003fffff, false),
174 HOWTO(RELOC_13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"13", false, 0,0x00001fff, false),
175 HOWTO(RELOC_LO10
, 0, 2, 10, false, 0, complain_overflow_dont
,0,"LO10", false, 0,0x000003ff, false),
176 HOWTO(RELOC_SFA_BASE
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_BASE", false, 0,0xffffffff, false),
177 HOWTO(RELOC_SFA_OFF13
,0,2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_OFF13",false, 0,0xffffffff, false),
178 HOWTO(RELOC_BASE10
, 0, 2, 16, false, 0, complain_overflow_bitfield
,0,"BASE10", false, 0,0x0000ffff, false),
179 HOWTO(RELOC_BASE13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"BASE13", false, 0,0x00001fff, false),
180 HOWTO(RELOC_BASE22
, 0, 2, 0, false, 0, complain_overflow_bitfield
,0,"BASE22", false, 0,0x00000000, false),
181 HOWTO(RELOC_PC10
, 0, 2, 10, false, 0, complain_overflow_bitfield
,0,"PC10", false, 0,0x000003ff, false),
182 HOWTO(RELOC_PC22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"PC22", false, 0,0x003fffff, false),
183 HOWTO(RELOC_JMP_TBL
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"JMP_TBL", false, 0,0xffffffff, false),
184 HOWTO(RELOC_SEGOFF16
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"SEGOFF16", false, 0,0x00000000, false),
185 HOWTO(RELOC_GLOB_DAT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"GLOB_DAT", false, 0,0x00000000, false),
186 HOWTO(RELOC_JMP_SLOT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"JMP_SLOT", false, 0,0x00000000, false),
187 HOWTO(RELOC_RELATIVE
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"RELATIVE", false, 0,0x00000000, false),
190 /* Convert standard reloc records to "arelent" format (incl byte swap). */
192 reloc_howto_type howto_table_std
[] = {
193 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
194 HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", true, 0x000000ff,0x000000ff, false),
195 HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", true, 0x0000ffff,0x0000ffff, false),
196 HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", true, 0xffffffff,0xffffffff, false),
197 HOWTO( 3, 0, 4, 64, false, 0, complain_overflow_bitfield
,0,"64", true, 0xdeaddead,0xdeaddead, false),
198 HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed
, 0,"DISP8", true, 0x000000ff,0x000000ff, false),
199 HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed
, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
200 HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed
, 0,"DISP32", true, 0xffffffff,0xffffffff, false),
201 HOWTO( 7, 0, 4, 64, true, 0, complain_overflow_signed
, 0,"DISP64", true, 0xfeedface,0xfeedface, false),
203 HOWTO( 9, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"BASE16", false,0xffffffff,0xffffffff, false),
204 HOWTO(10, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"BASE32", false,0xffffffff,0xffffffff, false),
207 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
209 CONST
struct reloc_howto_struct
*
210 NAME(aout
,reloc_type_lookup
) (abfd
,code
)
212 bfd_reloc_code_real_type code
;
214 #define EXT(i,j) case i: return &howto_table_ext[j]
215 #define STD(i,j) case i: return &howto_table_std[j]
216 int ext
= obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
;
217 if (code
== BFD_RELOC_CTOR
)
218 switch (bfd_get_arch_info (abfd
)->bits_per_address
)
230 EXT (BFD_RELOC_32
, 2);
231 EXT (BFD_RELOC_HI22
, 8);
232 EXT (BFD_RELOC_LO10
, 11);
233 EXT (BFD_RELOC_32_PCREL_S2
, 6);
234 EXT (BFD_RELOC_SPARC_WDISP22
, 7);
235 EXT (BFD_RELOC_SPARC13
, 10);
236 EXT (BFD_RELOC_SPARC_BASE13
, 15);
237 default: return (CONST
struct reloc_howto_struct
*) 0;
243 STD (BFD_RELOC_16
, 1);
244 STD (BFD_RELOC_32
, 2);
245 STD (BFD_RELOC_8_PCREL
, 4);
246 STD (BFD_RELOC_16_PCREL
, 5);
247 STD (BFD_RELOC_32_PCREL
, 6);
248 STD (BFD_RELOC_16_BASEREL
, 9);
249 STD (BFD_RELOC_32_BASEREL
, 10);
250 default: return (CONST
struct reloc_howto_struct
*) 0;
256 Internal entry points
259 @file{aoutx.h} exports several routines for accessing the
260 contents of an a.out file, which are gathered and exported in
261 turn by various format specific files (eg sunos.c).
267 aout_@var{size}_swap_exec_header_in
270 void aout_@var{size}_swap_exec_header_in,
272 struct external_exec *raw_bytes,
273 struct internal_exec *execp);
276 Swap the information in an executable header @var{raw_bytes} taken
277 from a raw byte stream memory image into the internal exec header
278 structure @var{execp}.
281 #ifndef NAME_swap_exec_header_in
283 NAME(aout
,swap_exec_header_in
) (abfd
, raw_bytes
, execp
)
285 struct external_exec
*raw_bytes
;
286 struct internal_exec
*execp
;
288 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
290 /* The internal_exec structure has some fields that are unused in this
291 configuration (IE for i960), so ensure that all such uninitialized
292 fields are zero'd out. There are places where two of these structs
293 are memcmp'd, and thus the contents do matter. */
294 memset ((PTR
) execp
, 0, sizeof (struct internal_exec
));
295 /* Now fill in fields in the execp, from the bytes in the raw data. */
296 execp
->a_info
= bfd_h_get_32 (abfd
, bytes
->e_info
);
297 execp
->a_text
= GET_WORD (abfd
, bytes
->e_text
);
298 execp
->a_data
= GET_WORD (abfd
, bytes
->e_data
);
299 execp
->a_bss
= GET_WORD (abfd
, bytes
->e_bss
);
300 execp
->a_syms
= GET_WORD (abfd
, bytes
->e_syms
);
301 execp
->a_entry
= GET_WORD (abfd
, bytes
->e_entry
);
302 execp
->a_trsize
= GET_WORD (abfd
, bytes
->e_trsize
);
303 execp
->a_drsize
= GET_WORD (abfd
, bytes
->e_drsize
);
305 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
310 aout_@var{size}_swap_exec_header_out
313 void aout_@var{size}_swap_exec_header_out
315 struct internal_exec *execp,
316 struct external_exec *raw_bytes);
319 Swap the information in an internal exec header structure
320 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
323 NAME(aout
,swap_exec_header_out
) (abfd
, execp
, raw_bytes
)
325 struct internal_exec
*execp
;
326 struct external_exec
*raw_bytes
;
328 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
330 /* Now fill in fields in the raw data, from the fields in the exec struct. */
331 bfd_h_put_32 (abfd
, execp
->a_info
, bytes
->e_info
);
332 PUT_WORD (abfd
, execp
->a_text
, bytes
->e_text
);
333 PUT_WORD (abfd
, execp
->a_data
, bytes
->e_data
);
334 PUT_WORD (abfd
, execp
->a_bss
, bytes
->e_bss
);
335 PUT_WORD (abfd
, execp
->a_syms
, bytes
->e_syms
);
336 PUT_WORD (abfd
, execp
->a_entry
, bytes
->e_entry
);
337 PUT_WORD (abfd
, execp
->a_trsize
, bytes
->e_trsize
);
338 PUT_WORD (abfd
, execp
->a_drsize
, bytes
->e_drsize
);
345 aout_@var{size}_some_aout_object_p
348 bfd_target *aout_@var{size}_some_aout_object_p
350 bfd_target *(*callback_to_real_object_p)());
353 Some a.out variant thinks that the file open in @var{abfd}
354 checking is an a.out file. Do some more checking, and set up
355 for access if it really is. Call back to the calling
356 environment's "finish up" function just before returning, to
357 handle any last-minute setup.
361 NAME(aout
,some_aout_object_p
) (abfd
, execp
, callback_to_real_object_p
)
363 struct internal_exec
*execp
;
364 bfd_target
*(*callback_to_real_object_p
) PARAMS ((bfd
*));
366 struct aout_data_struct
*rawptr
, *oldrawptr
;
369 rawptr
= (struct aout_data_struct
*) bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
370 if (rawptr
== NULL
) {
371 bfd_set_error (bfd_error_no_memory
);
375 oldrawptr
= abfd
->tdata
.aout_data
;
376 abfd
->tdata
.aout_data
= rawptr
;
378 /* Copy the contents of the old tdata struct.
379 In particular, we want the subformat, since for hpux it was set in
380 hp300hpux.c:swap_exec_header_in and will be used in
381 hp300hpux.c:callback. */
382 if (oldrawptr
!= NULL
)
383 *abfd
->tdata
.aout_data
= *oldrawptr
;
385 abfd
->tdata
.aout_data
->a
.hdr
= &rawptr
->e
;
386 *(abfd
->tdata
.aout_data
->a
.hdr
) = *execp
; /* Copy in the internal_exec struct */
387 execp
= abfd
->tdata
.aout_data
->a
.hdr
;
389 /* Set the file flags */
390 abfd
->flags
= NO_FLAGS
;
391 if (execp
->a_drsize
|| execp
->a_trsize
)
392 abfd
->flags
|= HAS_RELOC
;
393 /* Setting of EXEC_P has been deferred to the bottom of this function */
395 abfd
->flags
|= HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
;
396 if (N_DYNAMIC(*execp
))
397 abfd
->flags
|= DYNAMIC
;
399 if (N_MAGIC (*execp
) == ZMAGIC
)
401 abfd
->flags
|= D_PAGED
|WP_TEXT
;
402 adata(abfd
).magic
= z_magic
;
404 else if (N_MAGIC (*execp
) == NMAGIC
)
406 abfd
->flags
|= WP_TEXT
;
407 adata(abfd
).magic
= n_magic
;
410 adata(abfd
).magic
= o_magic
;
412 bfd_get_start_address (abfd
) = execp
->a_entry
;
414 obj_aout_symbols (abfd
) = (aout_symbol_type
*)NULL
;
415 bfd_get_symcount (abfd
) = execp
->a_syms
/ sizeof (struct external_nlist
);
417 /* The default relocation entry size is that of traditional V7 Unix. */
418 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
420 /* The default symbol entry size is that of traditional Unix. */
421 obj_symbol_entry_size (abfd
) = EXTERNAL_NLIST_SIZE
;
423 obj_aout_external_syms (abfd
) = NULL
;
424 obj_aout_external_strings (abfd
) = NULL
;
425 obj_aout_sym_hashes (abfd
) = NULL
;
427 /* Create the sections. This is raunchy, but bfd_close wants to reclaim
430 obj_textsec (abfd
) = bfd_make_section_old_way (abfd
, ".text");
431 obj_datasec (abfd
) = bfd_make_section_old_way (abfd
, ".data");
432 obj_bsssec (abfd
) = bfd_make_section_old_way (abfd
, ".bss");
435 (void)bfd_make_section (abfd
, ".text");
436 (void)bfd_make_section (abfd
, ".data");
437 (void)bfd_make_section (abfd
, ".bss");
440 obj_datasec (abfd
)->_raw_size
= execp
->a_data
;
441 obj_bsssec (abfd
)->_raw_size
= execp
->a_bss
;
443 /* If this object is dynamically linked, we assume that both
444 sections have relocs. This does no real harm, even though it may
446 obj_textsec (abfd
)->flags
=
447 (execp
->a_trsize
!= 0 || (abfd
->flags
& DYNAMIC
) != 0
448 ? (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
| SEC_RELOC
)
449 : (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
));
450 obj_datasec (abfd
)->flags
=
451 (execp
->a_drsize
!= 0 || (abfd
->flags
& DYNAMIC
) != 0
452 ? (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_RELOC
)
453 : (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
));
454 obj_bsssec (abfd
)->flags
= SEC_ALLOC
;
456 #ifdef THIS_IS_ONLY_DOCUMENTATION
457 /* The common code can't fill in these things because they depend
458 on either the start address of the text segment, the rounding
459 up of virtual addresses between segments, or the starting file
460 position of the text segment -- all of which varies among different
461 versions of a.out. */
463 /* Call back to the format-dependent code to fill in the rest of the
464 fields and do any further cleanup. Things that should be filled
465 in by the callback: */
467 struct exec
*execp
= exec_hdr (abfd
);
469 obj_textsec (abfd
)->size
= N_TXTSIZE(*execp
);
470 obj_textsec (abfd
)->raw_size
= N_TXTSIZE(*execp
);
471 /* data and bss are already filled in since they're so standard */
473 /* The virtual memory addresses of the sections */
474 obj_textsec (abfd
)->vma
= N_TXTADDR(*execp
);
475 obj_datasec (abfd
)->vma
= N_DATADDR(*execp
);
476 obj_bsssec (abfd
)->vma
= N_BSSADDR(*execp
);
478 /* The file offsets of the sections */
479 obj_textsec (abfd
)->filepos
= N_TXTOFF(*execp
);
480 obj_datasec (abfd
)->filepos
= N_DATOFF(*execp
);
482 /* The file offsets of the relocation info */
483 obj_textsec (abfd
)->rel_filepos
= N_TRELOFF(*execp
);
484 obj_datasec (abfd
)->rel_filepos
= N_DRELOFF(*execp
);
486 /* The file offsets of the string table and symbol table. */
487 obj_str_filepos (abfd
) = N_STROFF (*execp
);
488 obj_sym_filepos (abfd
) = N_SYMOFF (*execp
);
490 /* Determine the architecture and machine type of the object file. */
491 switch (N_MACHTYPE (*exec_hdr (abfd
))) {
493 abfd
->obj_arch
= bfd_arch_obscure
;
497 adata(abfd
)->page_size
= PAGE_SIZE
;
498 adata(abfd
)->segment_size
= SEGMENT_SIZE
;
499 adata(abfd
)->exec_bytes_size
= EXEC_BYTES_SIZE
;
503 /* The architecture is encoded in various ways in various a.out variants,
504 or is not encoded at all in some of them. The relocation size depends
505 on the architecture and the a.out variant. Finally, the return value
506 is the bfd_target vector in use. If an error occurs, return zero and
507 set bfd_error to the appropriate error code.
509 Formats such as b.out, which have additional fields in the a.out
510 header, should cope with them in this callback as well. */
511 #endif /* DOCUMENTATION */
513 result
= (*callback_to_real_object_p
)(abfd
);
515 /* Now that the segment addresses have been worked out, take a better
516 guess at whether the file is executable. If the entry point
517 is within the text segment, assume it is. (This makes files
518 executable even if their entry point address is 0, as long as
519 their text starts at zero.)
521 At some point we should probably break down and stat the file and
522 declare it executable if (one of) its 'x' bits are on... */
523 if ((execp
->a_entry
>= obj_textsec(abfd
)->vma
) &&
524 (execp
->a_entry
< obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
))
525 abfd
->flags
|= EXEC_P
;
528 #if 0 /* These should be set correctly anyways. */
529 abfd
->sections
= obj_textsec (abfd
);
530 obj_textsec (abfd
)->next
= obj_datasec (abfd
);
531 obj_datasec (abfd
)->next
= obj_bsssec (abfd
);
537 abfd
->tdata
.aout_data
= oldrawptr
;
544 aout_@var{size}_mkobject
547 boolean aout_@var{size}_mkobject, (bfd *abfd);
550 Initialize BFD @var{abfd} for use with a.out files.
554 NAME(aout
,mkobject
) (abfd
)
557 struct aout_data_struct
*rawptr
;
559 bfd_set_error (bfd_error_system_call
);
561 /* Use an intermediate variable for clarity */
562 rawptr
= (struct aout_data_struct
*)bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
564 if (rawptr
== NULL
) {
565 bfd_set_error (bfd_error_no_memory
);
569 abfd
->tdata
.aout_data
= rawptr
;
570 exec_hdr (abfd
) = &(rawptr
->e
);
572 /* For simplicity's sake we just make all the sections right here. */
574 obj_textsec (abfd
) = (asection
*)NULL
;
575 obj_datasec (abfd
) = (asection
*)NULL
;
576 obj_bsssec (abfd
) = (asection
*)NULL
;
577 bfd_make_section (abfd
, ".text");
578 bfd_make_section (abfd
, ".data");
579 bfd_make_section (abfd
, ".bss");
580 bfd_make_section (abfd
, BFD_ABS_SECTION_NAME
);
581 bfd_make_section (abfd
, BFD_UND_SECTION_NAME
);
582 bfd_make_section (abfd
, BFD_COM_SECTION_NAME
);
590 aout_@var{size}_machine_type
593 enum machine_type aout_@var{size}_machine_type
594 (enum bfd_architecture arch,
595 unsigned long machine));
598 Keep track of machine architecture and machine type for
599 a.out's. Return the <<machine_type>> for a particular
600 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
601 and machine can't be represented in a.out format.
603 If the architecture is understood, machine type 0 (default)
604 is always understood.
608 NAME(aout
,machine_type
) (arch
, machine
)
609 enum bfd_architecture arch
;
610 unsigned long machine
;
612 enum machine_type arch_flags
;
614 arch_flags
= M_UNKNOWN
;
618 if (machine
== 0) arch_flags
= M_SPARC
;
623 case 0: arch_flags
= M_68010
; break;
624 case 68000: arch_flags
= M_UNKNOWN
; break;
625 case 68010: arch_flags
= M_68010
; break;
626 case 68020: arch_flags
= M_68020
; break;
627 default: arch_flags
= M_UNKNOWN
; break;
632 if (machine
== 0) arch_flags
= M_386
;
636 if (machine
== 0) arch_flags
= M_29K
;
643 case 3000: arch_flags
= M_MIPS1
; break;
646 case 6000: arch_flags
= M_MIPS2
; break;
647 default: arch_flags
= M_UNKNOWN
; break;
652 arch_flags
= M_UNKNOWN
;
660 aout_@var{size}_set_arch_mach
663 boolean aout_@var{size}_set_arch_mach,
665 enum bfd_architecture arch,
666 unsigned long machine));
669 Set the architecture and the machine of the BFD @var{abfd} to the
670 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
671 can support the architecture required.
675 NAME(aout
,set_arch_mach
) (abfd
, arch
, machine
)
677 enum bfd_architecture arch
;
678 unsigned long machine
;
680 if (! bfd_default_set_arch_mach (abfd
, arch
, machine
))
683 if (arch
!= bfd_arch_unknown
&&
684 NAME(aout
,machine_type
) (arch
, machine
) == M_UNKNOWN
)
685 return false; /* We can't represent this type */
687 /* Determine the size of a relocation entry */
692 obj_reloc_entry_size (abfd
) = RELOC_EXT_SIZE
;
695 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
699 return (*aout_backend_info(abfd
)->set_sizes
) (abfd
);
703 adjust_o_magic (abfd
, execp
)
705 struct internal_exec
*execp
;
707 file_ptr pos
= adata (abfd
).exec_bytes_size
;
712 obj_textsec(abfd
)->filepos
= pos
;
713 pos
+= obj_textsec(abfd
)->_raw_size
;
714 vma
+= obj_textsec(abfd
)->_raw_size
;
717 if (!obj_datasec(abfd
)->user_set_vma
)
719 #if 0 /* ?? Does alignment in the file image really matter? */
720 pad
= align_power (vma
, obj_datasec(abfd
)->alignment_power
) - vma
;
722 obj_textsec(abfd
)->_raw_size
+= pad
;
725 obj_datasec(abfd
)->vma
= vma
;
727 obj_datasec(abfd
)->filepos
= pos
;
728 pos
+= obj_datasec(abfd
)->_raw_size
;
729 vma
+= obj_datasec(abfd
)->_raw_size
;
732 if (!obj_bsssec(abfd
)->user_set_vma
)
735 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
737 obj_datasec(abfd
)->_raw_size
+= pad
;
740 obj_bsssec(abfd
)->vma
= vma
;
742 obj_bsssec(abfd
)->filepos
= pos
;
744 /* Fix up the exec header. */
745 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
746 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
747 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
748 N_SET_MAGIC (*execp
, OMAGIC
);
752 adjust_z_magic (abfd
, execp
)
754 struct internal_exec
*execp
;
756 bfd_size_type data_pad
, text_pad
;
758 CONST
struct aout_backend_data
*abdp
;
759 int ztih
; /* Nonzero if text includes exec header. */
761 abdp
= aout_backend_info (abfd
);
764 ztih
= abdp
&& abdp
->text_includes_header
;
765 obj_textsec(abfd
)->filepos
= (ztih
766 ? adata(abfd
).exec_bytes_size
767 : adata(abfd
).page_size
);
768 if (! obj_textsec(abfd
)->user_set_vma
)
769 /* ?? Do we really need to check for relocs here? */
770 obj_textsec(abfd
)->vma
= ((abfd
->flags
& HAS_RELOC
)
773 ? (abdp
->default_text_vma
774 + adata(abfd
).exec_bytes_size
)
775 : abdp
->default_text_vma
));
776 /* Could take strange alignment of text section into account here? */
778 /* Find start of data. */
779 text_end
= obj_textsec(abfd
)->filepos
+ obj_textsec(abfd
)->_raw_size
;
780 text_pad
= BFD_ALIGN (text_end
, adata(abfd
).page_size
) - text_end
;
781 obj_textsec(abfd
)->_raw_size
+= text_pad
;
782 text_end
+= text_pad
;
785 if (!obj_datasec(abfd
)->user_set_vma
)
788 vma
= obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
;
789 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
791 if (abdp
&& abdp
->zmagic_mapped_contiguous
)
793 text_pad
= (obj_datasec(abfd
)->vma
794 - obj_textsec(abfd
)->vma
795 - obj_textsec(abfd
)->_raw_size
);
796 obj_textsec(abfd
)->_raw_size
+= text_pad
;
798 obj_datasec(abfd
)->filepos
= (obj_textsec(abfd
)->filepos
799 + obj_textsec(abfd
)->_raw_size
);
801 /* Fix up exec header while we're at it. */
802 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
803 if (ztih
&& (!abdp
|| (abdp
&& !abdp
->exec_header_not_counted
)))
804 execp
->a_text
+= adata(abfd
).exec_bytes_size
;
805 N_SET_MAGIC (*execp
, ZMAGIC
);
807 /* Spec says data section should be rounded up to page boundary. */
808 obj_datasec(abfd
)->_raw_size
809 = align_power (obj_datasec(abfd
)->_raw_size
,
810 obj_bsssec(abfd
)->alignment_power
);
811 execp
->a_data
= BFD_ALIGN (obj_datasec(abfd
)->_raw_size
,
812 adata(abfd
).page_size
);
813 data_pad
= execp
->a_data
- obj_datasec(abfd
)->_raw_size
;
816 if (!obj_bsssec(abfd
)->user_set_vma
)
817 obj_bsssec(abfd
)->vma
= (obj_datasec(abfd
)->vma
818 + obj_datasec(abfd
)->_raw_size
);
819 /* If the BSS immediately follows the data section and extra space
820 in the page is left after the data section, fudge data
821 in the header so that the bss section looks smaller by that
822 amount. We'll start the bss section there, and lie to the OS.
823 (Note that a linker script, as well as the above assignment,
824 could have explicitly set the BSS vma to immediately follow
825 the data section.) */
826 if (align_power (obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->alignment_power
)
827 == obj_datasec(abfd
)->vma
+ obj_datasec(abfd
)->_raw_size
)
828 execp
->a_bss
= (data_pad
> obj_bsssec(abfd
)->_raw_size
) ? 0 :
829 obj_bsssec(abfd
)->_raw_size
- data_pad
;
831 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
835 adjust_n_magic (abfd
, execp
)
837 struct internal_exec
*execp
;
839 file_ptr pos
= adata(abfd
).exec_bytes_size
;
844 obj_textsec(abfd
)->filepos
= pos
;
845 if (!obj_textsec(abfd
)->user_set_vma
)
846 obj_textsec(abfd
)->vma
= vma
;
848 vma
= obj_textsec(abfd
)->vma
;
849 pos
+= obj_textsec(abfd
)->_raw_size
;
850 vma
+= obj_textsec(abfd
)->_raw_size
;
853 obj_datasec(abfd
)->filepos
= pos
;
854 if (!obj_datasec(abfd
)->user_set_vma
)
855 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
856 vma
= obj_datasec(abfd
)->vma
;
858 /* Since BSS follows data immediately, see if it needs alignment. */
859 vma
+= obj_datasec(abfd
)->_raw_size
;
860 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
861 obj_datasec(abfd
)->_raw_size
+= pad
;
862 pos
+= obj_datasec(abfd
)->_raw_size
;
865 if (!obj_bsssec(abfd
)->user_set_vma
)
866 obj_bsssec(abfd
)->vma
= vma
;
868 vma
= obj_bsssec(abfd
)->vma
;
870 /* Fix up exec header. */
871 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
872 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
873 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
874 N_SET_MAGIC (*execp
, NMAGIC
);
878 NAME(aout
,adjust_sizes_and_vmas
) (abfd
, text_size
, text_end
)
880 bfd_size_type
*text_size
;
883 struct internal_exec
*execp
= exec_hdr (abfd
);
885 if ((obj_textsec (abfd
) == NULL
) || (obj_datasec (abfd
) == NULL
))
887 bfd_set_error (bfd_error_invalid_operation
);
890 if (adata(abfd
).magic
!= undecided_magic
) return true;
892 obj_textsec(abfd
)->_raw_size
=
893 align_power(obj_textsec(abfd
)->_raw_size
,
894 obj_textsec(abfd
)->alignment_power
);
896 *text_size
= obj_textsec (abfd
)->_raw_size
;
897 /* Rule (heuristic) for when to pad to a new page. Note that there
898 are (at least) two ways demand-paged (ZMAGIC) files have been
899 handled. Most Berkeley-based systems start the text segment at
900 (PAGE_SIZE). However, newer versions of SUNOS start the text
901 segment right after the exec header; the latter is counted in the
902 text segment size, and is paged in by the kernel with the rest of
905 /* This perhaps isn't the right way to do this, but made it simpler for me
906 to understand enough to implement it. Better would probably be to go
907 right from BFD flags to alignment/positioning characteristics. But the
908 old code was sloppy enough about handling the flags, and had enough
909 other magic, that it was a little hard for me to understand. I think
910 I understand it better now, but I haven't time to do the cleanup this
913 if (abfd
->flags
& D_PAGED
)
914 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
915 /* @@ What about QMAGIC? */
916 adata(abfd
).magic
= z_magic
;
917 else if (abfd
->flags
& WP_TEXT
)
918 adata(abfd
).magic
= n_magic
;
920 adata(abfd
).magic
= o_magic
;
922 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
924 fprintf (stderr
, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
926 switch (adata(abfd
).magic
) {
927 case n_magic
: str
= "NMAGIC"; break;
928 case o_magic
: str
= "OMAGIC"; break;
929 case z_magic
: str
= "ZMAGIC"; break;
934 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
,
935 obj_textsec(abfd
)->alignment_power
,
936 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
,
937 obj_datasec(abfd
)->alignment_power
,
938 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
,
939 obj_bsssec(abfd
)->alignment_power
);
943 switch (adata(abfd
).magic
)
946 adjust_o_magic (abfd
, execp
);
949 adjust_z_magic (abfd
, execp
);
952 adjust_n_magic (abfd
, execp
);
958 #ifdef BFD_AOUT_DEBUG
959 fprintf (stderr
, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
960 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
,
961 obj_textsec(abfd
)->filepos
,
962 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
,
963 obj_datasec(abfd
)->filepos
,
964 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
);
972 aout_@var{size}_new_section_hook
975 boolean aout_@var{size}_new_section_hook,
980 Called by the BFD in response to a @code{bfd_make_section}
984 NAME(aout
,new_section_hook
) (abfd
, newsect
)
988 /* align to double at least */
989 newsect
->alignment_power
= bfd_get_arch_info(abfd
)->section_align_power
;
992 if (bfd_get_format (abfd
) == bfd_object
)
994 if (obj_textsec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".text")) {
995 obj_textsec(abfd
)= newsect
;
996 newsect
->target_index
= N_TEXT
| N_EXT
;
1000 if (obj_datasec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".data")) {
1001 obj_datasec(abfd
) = newsect
;
1002 newsect
->target_index
= N_DATA
| N_EXT
;
1006 if (obj_bsssec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".bss")) {
1007 obj_bsssec(abfd
) = newsect
;
1008 newsect
->target_index
= N_BSS
| N_EXT
;
1014 /* We allow more than three sections internally */
1019 NAME(aout
,set_section_contents
) (abfd
, section
, location
, offset
, count
)
1024 bfd_size_type count
;
1027 bfd_size_type text_size
;
1029 if (abfd
->output_has_begun
== false)
1031 if (NAME(aout
,adjust_sizes_and_vmas
) (abfd
,
1033 &text_end
) == false)
1037 /* regardless, once we know what we're doing, we might as well get going */
1038 if (section
!= obj_bsssec(abfd
))
1040 bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
);
1043 return (bfd_write ((PTR
)location
, 1, count
, abfd
) == count
) ?
1051 /* Classify stabs symbols */
1053 #define sym_in_text_section(sym) \
1054 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
1056 #define sym_in_data_section(sym) \
1057 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
1059 #define sym_in_bss_section(sym) \
1060 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
1062 /* Symbol is undefined if type is N_UNDF|N_EXT and if it has
1063 zero in the "value" field. Nonzeroes there are fortrancommon
1065 #define sym_is_undefined(sym) \
1066 ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0)
1068 /* Symbol is a global definition if N_EXT is on and if it has
1069 a nonzero type field. */
1070 #define sym_is_global_defn(sym) \
1071 (((sym)->type & N_EXT) && (sym)->type & N_TYPE)
1073 /* Symbol is debugger info if any bits outside N_TYPE or N_EXT
1075 #define sym_is_debugger_info(sym) \
1076 (((sym)->type & ~(N_EXT | N_TYPE)) || (sym)->type == N_FN)
1078 #define sym_is_fortrancommon(sym) \
1079 (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0)
1081 /* Symbol is absolute if it has N_ABS set */
1082 #define sym_is_absolute(sym) \
1083 (((sym)->type & N_TYPE)== N_ABS)
1086 #define sym_is_indirect(sym) \
1087 (((sym)->type & N_ABS)== N_ABS)
1089 /* Only in their own functions for ease of debugging; when sym flags have
1090 stabilised these should be inlined into their (single) caller */
1093 translate_from_native_sym_flags (sym_pointer
, cache_ptr
, abfd
)
1094 struct external_nlist
*sym_pointer
;
1095 aout_symbol_type
* cache_ptr
;
1098 cache_ptr
->symbol
.section
= 0;
1099 switch (cache_ptr
->type
& N_TYPE
)
1101 case N_SETA
: case N_SETA
| N_EXT
:
1102 case N_SETT
: case N_SETT
| N_EXT
:
1103 case N_SETD
: case N_SETD
| N_EXT
:
1104 case N_SETB
: case N_SETB
| N_EXT
:
1106 char *copy
= bfd_alloc (abfd
, strlen (cache_ptr
->symbol
.name
) + 1);
1108 asection
*into_section
;
1109 arelent_chain
*reloc
= (arelent_chain
*) bfd_alloc (abfd
, sizeof (arelent_chain
));
1111 if (!copy
|| !reloc
)
1113 bfd_set_error (bfd_error_no_memory
);
1117 strcpy (copy
, cache_ptr
->symbol
.name
);
1119 /* Make sure that this bfd has a section with the right contructor
1121 section
= bfd_get_section_by_name (abfd
, copy
);
1123 section
= bfd_make_section (abfd
, copy
);
1125 /* Build a relocation entry for the constructor */
1126 switch ((cache_ptr
->type
& N_TYPE
))
1128 case N_SETA
: case N_SETA
| N_EXT
:
1129 into_section
= &bfd_abs_section
;
1130 cache_ptr
->type
= N_ABS
;
1132 case N_SETT
: case N_SETT
| N_EXT
:
1133 into_section
= (asection
*) obj_textsec (abfd
);
1134 cache_ptr
->type
= N_TEXT
;
1136 case N_SETD
: case N_SETD
| N_EXT
:
1137 into_section
= (asection
*) obj_datasec (abfd
);
1138 cache_ptr
->type
= N_DATA
;
1140 case N_SETB
: case N_SETB
| N_EXT
:
1141 into_section
= (asection
*) obj_bsssec (abfd
);
1142 cache_ptr
->type
= N_BSS
;
1145 bfd_set_error (bfd_error_bad_value
);
1149 /* Build a relocation pointing into the constuctor section
1150 pointing at the symbol in the set vector specified */
1152 reloc
->relent
.addend
= cache_ptr
->symbol
.value
;
1153 cache_ptr
->symbol
.section
= into_section
->symbol
->section
;
1154 reloc
->relent
.sym_ptr_ptr
= into_section
->symbol_ptr_ptr
;
1157 /* We modify the symbol to belong to a section depending upon the
1158 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
1159 really care, and add to the size of the section to contain a
1160 pointer to the symbol. Build a reloc entry to relocate to this
1161 symbol attached to this section. */
1163 section
->flags
= SEC_CONSTRUCTOR
| SEC_RELOC
;
1166 section
->reloc_count
++;
1167 section
->alignment_power
= 2;
1169 reloc
->next
= section
->constructor_chain
;
1170 section
->constructor_chain
= reloc
;
1171 reloc
->relent
.address
= section
->_raw_size
;
1172 section
->_raw_size
+= sizeof (int *);
1175 = (obj_reloc_entry_size(abfd
) == RELOC_EXT_SIZE
1176 ? howto_table_ext
: howto_table_std
)
1177 + CTOR_TABLE_RELOC_IDX
;
1178 cache_ptr
->symbol
.flags
|= BSF_CONSTRUCTOR
;
1182 if (cache_ptr
->type
== N_WARNING
)
1184 /* This symbol is the text of a warning message, the next symbol
1185 is the symbol to associate the warning with */
1186 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_WARNING
;
1188 /* @@ Stuffing pointers into integers is a no-no.
1189 We can usually get away with it if the integer is
1190 large enough though. */
1191 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1193 cache_ptr
->symbol
.value
= (bfd_vma
) ((cache_ptr
+ 1));
1195 /* We don't use a warning symbol's section, but we need
1196 it to be nonzero for the sanity check below, so
1197 pick one arbitrarily. */
1198 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1200 /* We furgle with the next symbol in place.
1201 We don't want it to be undefined, we'll trample the type */
1202 (sym_pointer
+ 1)->e_type
[0] = 0xff;
1205 if ((cache_ptr
->type
| N_EXT
) == (N_INDR
| N_EXT
))
1207 /* Two symbols in a row for an INDR message. The first symbol
1208 contains the name we will match, the second symbol contains
1209 the name the first name is translated into. It is supplied to
1210 us undefined. This is good, since we want to pull in any files
1212 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_INDIRECT
;
1214 /* @@ Stuffing pointers into integers is a no-no.
1215 We can usually get away with it if the integer is
1216 large enough though. */
1217 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1220 cache_ptr
->symbol
.value
= (bfd_vma
) ((cache_ptr
+ 1));
1221 cache_ptr
->symbol
.section
= &bfd_ind_section
;
1224 else if (sym_is_debugger_info (cache_ptr
))
1226 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
;
1227 /* Work out the section correct for this symbol */
1228 switch (cache_ptr
->type
& N_TYPE
)
1232 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1233 cache_ptr
->symbol
.value
-= obj_textsec (abfd
)->vma
;
1236 cache_ptr
->symbol
.value
-= obj_datasec (abfd
)->vma
;
1237 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1240 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1241 cache_ptr
->symbol
.value
-= obj_bsssec (abfd
)->vma
;
1245 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1252 if (sym_is_fortrancommon (cache_ptr
))
1254 cache_ptr
->symbol
.flags
= 0;
1255 cache_ptr
->symbol
.section
= &bfd_com_section
;
1263 /* In a.out, the value of a symbol is always relative to the
1264 * start of the file, if this is a data symbol we'll subtract
1265 * the size of the text section to get the section relative
1266 * value. If this is a bss symbol (which would be strange)
1267 * we'll subtract the size of the previous two sections
1268 * to find the section relative address.
1271 if (sym_in_text_section (cache_ptr
))
1273 cache_ptr
->symbol
.value
-= obj_textsec (abfd
)->vma
;
1274 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1276 else if (sym_in_data_section (cache_ptr
))
1278 cache_ptr
->symbol
.value
-= obj_datasec (abfd
)->vma
;
1279 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1281 else if (sym_in_bss_section (cache_ptr
))
1283 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1284 cache_ptr
->symbol
.value
-= obj_bsssec (abfd
)->vma
;
1286 else if (sym_is_undefined (cache_ptr
))
1288 cache_ptr
->symbol
.flags
= 0;
1289 cache_ptr
->symbol
.section
= &bfd_und_section
;
1291 else if (sym_is_absolute (cache_ptr
))
1293 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1296 if (sym_is_global_defn (cache_ptr
))
1298 cache_ptr
->symbol
.flags
= BSF_GLOBAL
| BSF_EXPORT
;
1300 else if (! sym_is_undefined (cache_ptr
))
1302 cache_ptr
->symbol
.flags
= BSF_LOCAL
;
1306 if (cache_ptr
->symbol
.section
== 0)
1313 translate_to_native_sym_flags (sym_pointer
, cache_ptr
, abfd
)
1314 struct external_nlist
*sym_pointer
;
1318 bfd_vma value
= cache_ptr
->value
;
1320 /* mask out any existing type bits in case copying from one section
1322 sym_pointer
->e_type
[0] &= ~N_TYPE
;
1324 /* We attempt to order these tests by decreasing frequency of success,
1325 according to tcov when linking the linker. */
1326 if (bfd_get_output_section(cache_ptr
) == &bfd_abs_section
) {
1327 sym_pointer
->e_type
[0] |= N_ABS
;
1329 else if (bfd_get_output_section(cache_ptr
) == obj_textsec (abfd
)) {
1330 sym_pointer
->e_type
[0] |= N_TEXT
;
1332 else if (bfd_get_output_section(cache_ptr
) == obj_datasec (abfd
)) {
1333 sym_pointer
->e_type
[0] |= N_DATA
;
1335 else if (bfd_get_output_section(cache_ptr
) == obj_bsssec (abfd
)) {
1336 sym_pointer
->e_type
[0] |= N_BSS
;
1338 else if (bfd_get_output_section(cache_ptr
) == &bfd_und_section
) {
1339 sym_pointer
->e_type
[0] = (N_UNDF
| N_EXT
);
1341 else if (bfd_get_output_section(cache_ptr
) == &bfd_ind_section
) {
1342 sym_pointer
->e_type
[0] = N_INDR
;
1344 else if (bfd_get_output_section(cache_ptr
) == NULL
) {
1345 /* Protect the bfd_is_com_section call.
1346 This case occurs, e.g., for the *DEBUG* section of a COFF file. */
1347 bfd_set_error (bfd_error_nonrepresentable_section
);
1350 else if (bfd_is_com_section (bfd_get_output_section (cache_ptr
))) {
1351 sym_pointer
->e_type
[0] = (N_UNDF
| N_EXT
);
1354 bfd_set_error (bfd_error_nonrepresentable_section
);
1358 /* Turn the symbol from section relative to absolute again */
1360 value
+= cache_ptr
->section
->output_section
->vma
+ cache_ptr
->section
->output_offset
;
1363 if (cache_ptr
->flags
& (BSF_WARNING
)) {
1364 sym_pointer
->e_type
[0] = N_WARNING
;
1365 (sym_pointer
+1)->e_type
[0] = 1;
1368 if (cache_ptr
->flags
& BSF_DEBUGGING
) {
1369 sym_pointer
->e_type
[0] = ((aout_symbol_type
*)cache_ptr
)->type
;
1371 else if (cache_ptr
->flags
& (BSF_GLOBAL
| BSF_EXPORT
)) {
1372 sym_pointer
->e_type
[0] |= N_EXT
;
1374 if (cache_ptr
->flags
& BSF_CONSTRUCTOR
) {
1375 int type
= ((aout_symbol_type
*)cache_ptr
)->type
;
1378 case N_ABS
: type
= N_SETA
; break;
1379 case N_TEXT
: type
= N_SETT
; break;
1380 case N_DATA
: type
= N_SETD
; break;
1381 case N_BSS
: type
= N_SETB
; break;
1383 sym_pointer
->e_type
[0] = type
;
1386 PUT_WORD(abfd
, value
, sym_pointer
->e_value
);
1391 /* Native-level interface to symbols. */
1395 NAME(aout
,make_empty_symbol
) (abfd
)
1398 aout_symbol_type
*new =
1399 (aout_symbol_type
*)bfd_zalloc (abfd
, sizeof (aout_symbol_type
));
1402 bfd_set_error (bfd_error_no_memory
);
1405 new->symbol
.the_bfd
= abfd
;
1407 return &new->symbol
;
1410 /* Translate a set of internal symbols into external symbols. */
1413 translate_symbol_table (abfd
, in
, ext
, count
, str
, strsize
, dynamic
)
1415 aout_symbol_type
*in
;
1416 struct external_nlist
*ext
;
1417 bfd_size_type count
;
1419 bfd_size_type strsize
;
1422 struct external_nlist
*ext_end
;
1424 ext_end
= ext
+ count
;
1425 for (; ext
< ext_end
; ext
++, in
++)
1429 x
= GET_WORD (abfd
, ext
->e_strx
);
1430 in
->symbol
.the_bfd
= abfd
;
1432 /* For the normal symbols, the zero index points at the number
1433 of bytes in the string table but is to be interpreted as the
1434 null string. For the dynamic symbols, the number of bytes in
1435 the string table is stored in the __DYNAMIC structure and the
1436 zero index points at an actual string. */
1437 if (x
== 0 && ! dynamic
)
1438 in
->symbol
.name
= "";
1439 else if (x
< strsize
)
1440 in
->symbol
.name
= str
+ x
;
1444 in
->symbol
.value
= GET_SWORD (abfd
, ext
->e_value
);
1445 in
->desc
= bfd_h_get_16 (abfd
, ext
->e_desc
);
1446 in
->other
= bfd_h_get_8 (abfd
, ext
->e_other
);
1447 in
->type
= bfd_h_get_8 (abfd
, ext
->e_type
);
1448 in
->symbol
.udata
= 0;
1450 if (!translate_from_native_sym_flags (ext
, in
, abfd
))
1454 in
->symbol
.flags
|= BSF_DYNAMIC
;
1460 /* We read the symbols into a buffer, which is discarded when this
1461 function exits. We read the strings into a buffer large enough to
1462 hold them all plus all the cached symbol entries. */
1465 NAME(aout
,slurp_symbol_table
) (abfd
)
1468 bfd_size_type symbol_size
;
1469 bfd_size_type string_size
;
1470 unsigned char string_chars
[BYTES_IN_WORD
];
1471 struct external_nlist
*syms
;
1473 aout_symbol_type
*cached
;
1474 bfd_size_type dynsym_count
= 0;
1475 struct external_nlist
*dynsyms
= NULL
;
1476 char *dynstrs
= NULL
;
1477 bfd_size_type dynstr_size
;
1479 /* If there's no work to be done, don't do any */
1480 if (obj_aout_symbols (abfd
) != (aout_symbol_type
*)NULL
) return true;
1481 symbol_size
= exec_hdr(abfd
)->a_syms
;
1482 if (symbol_size
== 0)
1484 bfd_set_error (bfd_error_no_symbols
);
1488 bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
);
1489 if (bfd_read ((PTR
)string_chars
, BYTES_IN_WORD
, 1, abfd
) != BYTES_IN_WORD
)
1491 string_size
= GET_WORD (abfd
, string_chars
);
1493 /* If this is a dynamic object, see if we can get the dynamic symbol
1495 if ((bfd_get_file_flags (abfd
) & DYNAMIC
) != 0
1496 && aout_backend_info (abfd
)->read_dynamic_symbols
)
1498 dynsym_count
= ((*aout_backend_info (abfd
)->read_dynamic_symbols
)
1499 (abfd
, &dynsyms
, &dynstrs
, &dynstr_size
));
1500 if (dynsym_count
== (bfd_size_type
) -1)
1504 strings
= (char *) bfd_alloc (abfd
, string_size
+ 1);
1505 cached
= ((aout_symbol_type
*)
1507 ((bfd_get_symcount (abfd
) + dynsym_count
)
1508 * sizeof (aout_symbol_type
))));
1510 /* Don't allocate on the obstack, so we can free it easily. */
1511 syms
= (struct external_nlist
*) malloc(symbol_size
);
1512 if (!strings
|| !cached
|| !syms
)
1514 bfd_set_error (bfd_error_no_memory
);
1517 bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
);
1518 if (bfd_read ((PTR
)syms
, 1, symbol_size
, abfd
) != symbol_size
)
1524 bfd_release (abfd
, cached
);
1526 bfd_release (abfd
, strings
);
1530 bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
);
1531 if (bfd_read ((PTR
)strings
, 1, string_size
, abfd
) != string_size
)
1535 strings
[string_size
] = 0; /* Just in case. */
1537 /* OK, now walk the new symtable, cacheing symbol properties */
1538 if (! translate_symbol_table (abfd
, cached
, syms
, bfd_get_symcount (abfd
),
1539 strings
, string_size
, false))
1541 if (dynsym_count
> 0)
1543 if (! translate_symbol_table (abfd
, cached
+ bfd_get_symcount (abfd
),
1544 dynsyms
, dynsym_count
, dynstrs
,
1548 bfd_get_symcount (abfd
) += dynsym_count
;
1551 obj_aout_symbols (abfd
) = cached
;
1558 /* Possible improvements:
1559 + look for strings matching trailing substrings of other strings
1560 + better data structures? balanced trees?
1561 + smaller per-string or per-symbol data? re-use some of the symbol's
1563 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1564 construct the entire symbol table at once, we could get by with smaller
1565 amounts of VM? (What effect does that have on the string table
1567 + rip this out of here, put it into its own file in bfd or libiberty, so
1568 coff and elf can use it too. I'll work on this soon, but have more
1569 pressing tasks right now.
1571 A hash table might(?) be more efficient for handling exactly the cases that
1572 are handled now, but for trailing substring matches, I think we want to
1573 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1574 order, nor look only for exact-match or not-match. I don't think a hash
1575 table would be very useful for that, and I don't feel like fleshing out two
1576 completely different implementations. [raeburn:930419.0331EDT] */
1578 struct stringtab_entry
{
1579 /* Hash value for this string. Only useful so long as we aren't doing
1580 substring matches. */
1583 /* Next node to look at, depending on whether the hash value of the string
1584 being searched for is less than or greater than the hash value of the
1585 current node. For now, `equal to' is lumped in with `greater than', for
1586 space efficiency. It's not a common enough case to warrant another field
1587 to be used for all nodes. */
1588 struct stringtab_entry
*less
;
1589 struct stringtab_entry
*greater
;
1591 /* The string itself. */
1594 /* The index allocated for this string. */
1595 bfd_size_type index
;
1597 #ifdef GATHER_STATISTICS
1598 /* How many references have there been to this string? (Not currently used;
1599 could be dumped out for anaylsis, if anyone's interested.) */
1600 unsigned long count
;
1603 /* Next node in linked list, in suggested output order. */
1604 struct stringtab_entry
*next_to_output
;
1607 struct stringtab_data
{
1608 /* Tree of string table entries. */
1609 struct stringtab_entry
*strings
;
1611 /* Fudge factor used to center top node of tree. */
1614 /* Next index value to issue. */
1615 bfd_size_type index
;
1617 /* Index used for empty strings. Cached here because checking for them
1618 is really easy, and we can avoid searching the tree. */
1619 bfd_size_type empty_string_index
;
1621 /* These fields indicate the two ends of a singly-linked list that indicates
1622 the order strings should be written out in. Use this order, and no
1623 seeking will need to be done, so output efficiency should be maximized. */
1624 struct stringtab_entry
**end
;
1625 struct stringtab_entry
*output_order
;
1627 #ifdef GATHER_STATISTICS
1628 /* Number of strings which duplicate strings already in the table. */
1629 unsigned long duplicates
;
1631 /* Number of bytes saved by not having to write all the duplicate strings. */
1632 unsigned long bytes_saved
;
1634 /* Number of zero-length strings. Currently, these all turn into
1635 references to the null byte at the end of the first string. In some
1636 cases (possibly not all? explore this...), it should be possible to
1637 simply write out a zero index value. */
1638 unsigned long empty_strings
;
1640 /* Number of times the hash values matched but the strings were different.
1641 Note that this includes the number of times the other string(s) occurs, so
1642 there may only be two strings hashing to the same value, even if this
1643 number is very large. */
1644 unsigned long bad_hash_matches
;
1646 /* Null strings aren't counted in this one.
1647 This will probably only be nonzero if we've got an input file
1648 which was produced by `ld -r' (i.e., it's already been processed
1649 through this code). Under some operating systems, native tools
1650 may make all empty strings have the same index; but the pointer
1651 check won't catch those, because to get to that stage we'd already
1652 have to compute the checksum, which requires reading the string,
1653 so we short-circuit that case with empty_string_index above. */
1654 unsigned long pointer_matches
;
1656 /* Number of comparisons done. I figure with the algorithms in use below,
1657 the average number of comparisons done (per symbol) should be roughly
1658 log-base-2 of the number of unique strings. */
1659 unsigned long n_compares
;
1663 /* Some utility functions for the string table code. */
1665 /* For speed, only hash on the first this many bytes of strings.
1666 This number was chosen by profiling ld linking itself, with -g. */
1667 #define HASHMAXLEN 25
1669 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1671 static INLINE
unsigned int
1673 unsigned char *string
;
1674 register unsigned int len
;
1676 register unsigned int sum
= 0;
1678 if (len
> HASHMAXLEN
)
1686 HASH_CHAR (*string
++);
1692 stringtab_init (tab
)
1693 struct stringtab_data
*tab
;
1696 tab
->output_order
= 0;
1698 tab
->end
= &tab
->output_order
;
1700 /* Initial string table length includes size of length field. */
1701 tab
->index
= BYTES_IN_WORD
;
1702 tab
->empty_string_index
= -1;
1703 #ifdef GATHER_STATISTICS
1704 tab
->duplicates
= 0;
1705 tab
->empty_strings
= 0;
1706 tab
->bad_hash_matches
= 0;
1707 tab
->pointer_matches
= 0;
1708 tab
->bytes_saved
= 0;
1709 tab
->n_compares
= 0;
1714 compare (entry
, str
, hash
)
1715 struct stringtab_entry
*entry
;
1719 return hash
- entry
->hash
;
1722 #ifdef GATHER_STATISTICS
1723 /* Don't want to have to link in math library with all bfd applications... */
1724 static INLINE
double
1732 return ((d
> 1.41) ? 0.5 : 0) + n
;
1736 /* Main string table routines. */
1737 /* Returns index in string table. Whether or not this actually adds an
1738 entry into the string table should be irrelevant -- it just has to
1739 return a valid index. */
1740 static bfd_size_type
1741 add_to_stringtab (abfd
, str
, tab
)
1744 struct stringtab_data
*tab
;
1746 struct stringtab_entry
**ep
;
1747 register struct stringtab_entry
*entry
;
1748 unsigned int hashval
, len
;
1752 bfd_size_type index
;
1753 CONST bfd_size_type minus_one
= -1;
1755 #ifdef GATHER_STATISTICS
1756 tab
->empty_strings
++;
1758 index
= tab
->empty_string_index
;
1759 if (index
!= minus_one
)
1762 #ifdef GATHER_STATISTICS
1769 /* Need to find it. */
1770 entry
= tab
->strings
;
1773 index
= entry
->index
+ strlen (entry
->string
);
1774 tab
->empty_string_index
= index
;
1782 /* The hash_zero value is chosen such that the first symbol gets a value of
1783 zero. With a balanced tree, this wouldn't be very useful, but without it,
1784 we might get a more even split at the top level, instead of skewing it
1785 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1786 hashval
= hash (str
, len
) ^ tab
->hash_zero
;
1790 tab
->hash_zero
= hashval
;
1800 #ifdef GATHER_STATISTICS
1803 cmp
= compare (entry
, str
, hashval
);
1804 /* The not-equal cases are more frequent, so check them first. */
1806 ep
= &entry
->greater
;
1811 if (entry
->string
== str
)
1813 #ifdef GATHER_STATISTICS
1814 tab
->pointer_matches
++;
1818 /* Compare the first bytes to save a function call if they
1820 if (entry
->string
[0] == str
[0] && !strcmp (entry
->string
, str
))
1823 #ifdef GATHER_STATISTICS
1825 tab
->bytes_saved
+= len
+ 1;
1828 /* If we're in the linker, and the new string is from a new
1829 input file which might have already had these reductions
1830 run over it, we want to keep the new string pointer. I
1831 don't think we're likely to see any (or nearly as many,
1832 at least) cases where a later string is in the same location
1833 as an earlier one rather than this one. */
1834 entry
->string
= str
;
1835 return entry
->index
;
1837 #ifdef GATHER_STATISTICS
1838 tab
->bad_hash_matches
++;
1840 ep
= &entry
->greater
;
1844 /* If we get here, nothing that's in the table already matched.
1845 EP points to the `next' field at the end of the chain; stick a
1846 new entry on here. */
1848 entry
= (struct stringtab_entry
*)
1849 bfd_alloc_by_size_t (abfd
, sizeof (struct stringtab_entry
));
1852 bfd_set_error (bfd_error_no_memory
);
1853 abort(); /* FIXME */
1856 entry
->less
= entry
->greater
= 0;
1857 entry
->hash
= hashval
;
1858 entry
->index
= tab
->index
;
1859 entry
->string
= str
;
1860 entry
->next_to_output
= 0;
1861 #ifdef GATHER_STATISTICS
1865 assert (*tab
->end
== 0);
1866 *(tab
->end
) = entry
;
1867 tab
->end
= &entry
->next_to_output
;
1868 assert (*tab
->end
== 0);
1871 tab
->index
+= len
+ 1;
1873 tab
->empty_string_index
= entry
->index
;
1877 return entry
->index
;
1881 emit_strtab (abfd
, tab
)
1883 struct stringtab_data
*tab
;
1885 struct stringtab_entry
*entry
;
1886 #ifdef GATHER_STATISTICS
1890 /* Be sure to put string length into correct byte ordering before writing
1892 char buffer
[BYTES_IN_WORD
];
1894 PUT_WORD (abfd
, tab
->index
, (unsigned char *) buffer
);
1895 bfd_write ((PTR
) buffer
, 1, BYTES_IN_WORD
, abfd
);
1897 for (entry
= tab
->output_order
; entry
; entry
= entry
->next_to_output
)
1899 bfd_write ((PTR
) entry
->string
, 1, strlen (entry
->string
) + 1, abfd
);
1900 #ifdef GATHER_STATISTICS
1905 #ifdef GATHER_STATISTICS
1906 /* Short form only, for now.
1907 To do: Specify output file. Conditionalize on environment? Detailed
1908 analysis if desired. */
1910 int n_syms
= bfd_get_symcount (abfd
);
1912 fprintf (stderr
, "String table data for output file:\n");
1913 fprintf (stderr
, " %8d symbols output\n", n_syms
);
1914 fprintf (stderr
, " %8d duplicate strings\n", tab
->duplicates
);
1915 fprintf (stderr
, " %8d empty strings\n", tab
->empty_strings
);
1916 fprintf (stderr
, " %8d unique strings output\n", count
);
1917 fprintf (stderr
, " %8d pointer matches\n", tab
->pointer_matches
);
1918 fprintf (stderr
, " %8d bytes saved\n", tab
->bytes_saved
);
1919 fprintf (stderr
, " %8d bad hash matches\n", tab
->bad_hash_matches
);
1920 fprintf (stderr
, " %8d hash-val comparisons\n", tab
->n_compares
);
1923 double n_compares
= tab
->n_compares
;
1924 double avg_compares
= n_compares
/ n_syms
;
1925 /* The second value here should usually be near one. */
1927 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
1928 avg_compares
, avg_compares
/ log2 (count
));
1935 generic = bfd_get_outsymbols(abfd);
1936 for (count = 0; count < bfd_get_symcount(abfd); count++)
1938 asymbol *g = *(generic++);
1942 size_t length = strlen(g->name)+1;
1943 bfd_write((PTR)g->name, 1, length, abfd);
1945 g->KEEPIT = (KEEPITTYPE) count;
1950 NAME(aout
,write_syms
) (abfd
)
1953 unsigned int count
;
1954 asymbol
**generic
= bfd_get_outsymbols (abfd
);
1955 struct stringtab_data strtab
;
1957 stringtab_init (&strtab
);
1959 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
1961 asymbol
*g
= generic
[count
];
1962 struct external_nlist nsp
;
1965 PUT_WORD (abfd
, add_to_stringtab (abfd
, g
->name
, &strtab
),
1966 (unsigned char *) nsp
.e_strx
);
1968 PUT_WORD (abfd
, 0, (unsigned char *)nsp
.e_strx
);
1970 if (bfd_asymbol_flavour(g
) == abfd
->xvec
->flavour
)
1972 bfd_h_put_16(abfd
, aout_symbol(g
)->desc
, nsp
.e_desc
);
1973 bfd_h_put_8(abfd
, aout_symbol(g
)->other
, nsp
.e_other
);
1974 bfd_h_put_8(abfd
, aout_symbol(g
)->type
, nsp
.e_type
);
1978 bfd_h_put_16(abfd
,0, nsp
.e_desc
);
1979 bfd_h_put_8(abfd
, 0, nsp
.e_other
);
1980 bfd_h_put_8(abfd
, 0, nsp
.e_type
);
1983 if (! translate_to_native_sym_flags (&nsp
, g
, abfd
))
1986 if (bfd_write((PTR
)&nsp
,1,EXTERNAL_NLIST_SIZE
, abfd
)
1987 != EXTERNAL_NLIST_SIZE
)
1990 /* NB: `KEEPIT' currently overlays `flags', so set this only
1991 here, at the end. */
1995 emit_strtab (abfd
, &strtab
);
2002 NAME(aout
,get_symtab
) (abfd
, location
)
2006 unsigned int counter
= 0;
2007 aout_symbol_type
*symbase
;
2009 if (!NAME(aout
,slurp_symbol_table
)(abfd
))
2012 for (symbase
= obj_aout_symbols(abfd
); counter
++ < bfd_get_symcount (abfd
);)
2013 *(location
++) = (asymbol
*)( symbase
++);
2015 return bfd_get_symcount (abfd
);
2019 /* Standard reloc stuff */
2020 /* Output standard relocation information to a file in target byte order. */
2023 NAME(aout
,swap_std_reloc_out
) (abfd
, g
, natptr
)
2026 struct reloc_std_external
*natptr
;
2029 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2031 unsigned int r_length
;
2033 int r_baserel
, r_jmptable
, r_relative
;
2034 asection
*output_section
= sym
->section
->output_section
;
2036 PUT_WORD(abfd
, g
->address
, natptr
->r_address
);
2038 r_length
= g
->howto
->size
; /* Size as a power of two */
2039 r_pcrel
= (int) g
->howto
->pc_relative
; /* Relative to PC? */
2040 /* XXX This relies on relocs coming from a.out files. */
2041 r_baserel
= (g
->howto
->type
& 8) != 0;
2042 /* r_jmptable, r_relative??? FIXME-soon */
2047 /* For a standard reloc, the addend is in the object file. */
2048 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2051 /* name was clobbered by aout_write_syms to be symbol index */
2053 /* If this relocation is relative to a symbol then set the
2054 r_index to the symbols index, and the r_extern bit.
2056 Absolute symbols can come in in two ways, either as an offset
2057 from the abs section, or as a symbol which has an abs value.
2062 if (bfd_is_com_section (output_section
)
2063 || output_section
== &bfd_abs_section
2064 || output_section
== &bfd_und_section
)
2066 if (bfd_abs_section
.symbol
== sym
)
2068 /* Whoops, looked like an abs symbol, but is really an offset
2069 from the abs section */
2075 /* Fill in symbol */
2077 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
2083 /* Just an ordinary section */
2085 r_index
= output_section
->target_index
;
2088 /* now the fun stuff */
2089 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2090 natptr
->r_index
[0] = r_index
>> 16;
2091 natptr
->r_index
[1] = r_index
>> 8;
2092 natptr
->r_index
[2] = r_index
;
2094 (r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
2095 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
2096 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
2097 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
2098 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
2099 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
);
2101 natptr
->r_index
[2] = r_index
>> 16;
2102 natptr
->r_index
[1] = r_index
>> 8;
2103 natptr
->r_index
[0] = r_index
;
2105 (r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
2106 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
2107 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
2108 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
2109 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
2110 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
);
2115 /* Extended stuff */
2116 /* Output extended relocation information to a file in target byte order. */
2119 NAME(aout
,swap_ext_reloc_out
) (abfd
, g
, natptr
)
2122 register struct reloc_ext_external
*natptr
;
2126 unsigned int r_type
;
2127 unsigned int r_addend
;
2128 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2129 asection
*output_section
= sym
->section
->output_section
;
2131 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
2133 r_type
= (unsigned int) g
->howto
->type
;
2135 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2137 /* If this relocation is relative to a symbol then set the
2138 r_index to the symbols index, and the r_extern bit.
2140 Absolute symbols can come in in two ways, either as an offset
2141 from the abs section, or as a symbol which has an abs value.
2142 check for that here. */
2144 if (bfd_is_com_section (output_section
)
2145 || output_section
== &bfd_abs_section
2146 || output_section
== &bfd_und_section
)
2148 if (bfd_abs_section
.symbol
== sym
)
2150 /* Whoops, looked like an abs symbol, but is really an offset
2151 from the abs section */
2158 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_EXT_BITS_EXTERN_BIG
: 0)
2175 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
2177 natptr
->r_index
[2] = r_index
>> 16;
2178 natptr
->r_index
[1] = r_index
>> 8;
2179 natptr
->r_index
[0] = r_index
;
2181 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
2182 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
2185 PUT_WORD (abfd
, r_addend
, natptr
->r_addend
);
2188 /* BFD deals internally with all things based from the section they're
2189 in. so, something in 10 bytes into a text section with a base of
2190 50 would have a symbol (.text+10) and know .text vma was 50.
2192 Aout keeps all it's symbols based from zero, so the symbol would
2193 contain 60. This macro subs the base of each section from the value
2194 to give the true offset from the section */
2197 #define MOVE_ADDRESS(ad) \
2199 /* undefined symbol */ \
2200 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2201 cache_ptr->addend = ad; \
2203 /* defined, section relative. replace symbol with pointer to \
2204 symbol which points to section */ \
2205 switch (r_index) { \
2207 case N_TEXT | N_EXT: \
2208 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2209 cache_ptr->addend = ad - su->textsec->vma; \
2212 case N_DATA | N_EXT: \
2213 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2214 cache_ptr->addend = ad - su->datasec->vma; \
2217 case N_BSS | N_EXT: \
2218 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2219 cache_ptr->addend = ad - su->bsssec->vma; \
2223 case N_ABS | N_EXT: \
2224 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2225 cache_ptr->addend = ad; \
2231 NAME(aout
,swap_ext_reloc_in
) (abfd
, bytes
, cache_ptr
, symbols
)
2233 struct reloc_ext_external
*bytes
;
2239 unsigned int r_type
;
2240 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2242 cache_ptr
->address
= (GET_SWORD (abfd
, bytes
->r_address
));
2244 /* now the fun stuff */
2245 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2246 r_index
= (bytes
->r_index
[0] << 16)
2247 | (bytes
->r_index
[1] << 8)
2248 | bytes
->r_index
[2];
2249 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
2250 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
2251 >> RELOC_EXT_BITS_TYPE_SH_BIG
;
2253 r_index
= (bytes
->r_index
[2] << 16)
2254 | (bytes
->r_index
[1] << 8)
2255 | bytes
->r_index
[0];
2256 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
2257 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
2258 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
;
2261 cache_ptr
->howto
= howto_table_ext
+ r_type
;
2262 MOVE_ADDRESS(GET_SWORD(abfd
, bytes
->r_addend
));
2266 NAME(aout
,swap_std_reloc_in
) (abfd
, bytes
, cache_ptr
, symbols
)
2268 struct reloc_std_external
*bytes
;
2274 unsigned int r_length
;
2276 int r_baserel
, r_jmptable
, r_relative
;
2277 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2280 cache_ptr
->address
= bfd_h_get_32 (abfd
, bytes
->r_address
);
2282 /* now the fun stuff */
2283 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2284 r_index
= (bytes
->r_index
[0] << 16)
2285 | (bytes
->r_index
[1] << 8)
2286 | bytes
->r_index
[2];
2287 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
2288 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
2289 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
2290 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
2291 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
2292 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
2293 >> RELOC_STD_BITS_LENGTH_SH_BIG
;
2295 r_index
= (bytes
->r_index
[2] << 16)
2296 | (bytes
->r_index
[1] << 8)
2297 | bytes
->r_index
[0];
2298 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
2299 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
2300 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
2301 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
2302 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
2303 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
2304 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
;
2307 howto_idx
= r_length
+ 4 * r_pcrel
+ 8 * r_baserel
;
2308 BFD_ASSERT (howto_idx
< TABLE_SIZE (howto_table_std
));
2309 cache_ptr
->howto
= howto_table_std
+ howto_idx
;
2310 BFD_ASSERT (cache_ptr
->howto
->type
!= -1);
2311 BFD_ASSERT (r_jmptable
== 0);
2312 BFD_ASSERT (r_relative
== 0);
2313 /* FIXME-soon: Roll jmptable, relative bits into howto setting */
2321 NAME(aout
,slurp_reloc_table
) (abfd
, asect
, symbols
)
2327 bfd_size_type reloc_size
;
2329 bfd_size_type dynrel_count
= 0;
2331 arelent
*reloc_cache
;
2333 unsigned int counter
= 0;
2336 if (asect
->relocation
) return true;
2338 if (asect
->flags
& SEC_CONSTRUCTOR
) return true;
2340 if (asect
== obj_datasec (abfd
))
2341 reloc_size
= exec_hdr(abfd
)->a_drsize
;
2342 else if (asect
== obj_textsec (abfd
))
2343 reloc_size
= exec_hdr(abfd
)->a_trsize
;
2346 bfd_set_error (bfd_error_invalid_operation
);
2350 if ((bfd_get_file_flags (abfd
) & DYNAMIC
) != 0
2351 && aout_backend_info (abfd
)->read_dynamic_relocs
)
2353 dynrel_count
= ((*aout_backend_info (abfd
)->read_dynamic_relocs
)
2355 if (dynrel_count
== (bfd_size_type
) -1)
2359 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2360 each_size
= obj_reloc_entry_size (abfd
);
2362 count
= reloc_size
/ each_size
;
2364 reloc_cache
= ((arelent
*)
2366 (size_t) ((count
+ dynrel_count
)
2367 * sizeof (arelent
))));
2371 bfd_set_error (bfd_error_no_memory
);
2375 relocs
= (PTR
) bfd_alloc (abfd
, reloc_size
);
2378 bfd_release (abfd
, reloc_cache
);
2382 if (bfd_read (relocs
, 1, reloc_size
, abfd
) != reloc_size
)
2384 bfd_release (abfd
, relocs
);
2385 bfd_release (abfd
, reloc_cache
);
2386 bfd_set_error (bfd_error_system_call
);
2390 cache_ptr
= reloc_cache
;
2391 if (each_size
== RELOC_EXT_SIZE
)
2393 register struct reloc_ext_external
*rptr
=
2394 (struct reloc_ext_external
*) relocs
;
2396 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2397 NAME(aout
,swap_ext_reloc_in
) (abfd
, rptr
, cache_ptr
, symbols
);
2401 register struct reloc_std_external
*rptr
2402 = (struct reloc_std_external
*) relocs
;
2404 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2405 NAME(aout
,swap_std_reloc_in
) (abfd
, rptr
, cache_ptr
, symbols
);
2408 if (dynrel_count
> 0)
2412 /* The dynamic symbols are at the end of the symbol table. */
2413 for (dynsyms
= symbols
;
2414 *dynsyms
!= NULL
&& ((*dynsyms
)->flags
& BSF_DYNAMIC
) == 0;
2418 /* Swap in the dynamic relocs. These relocs may be for either
2419 section, so we must discard ones we don't want. */
2421 if (each_size
== RELOC_EXT_SIZE
)
2423 register struct reloc_ext_external
*rptr
2424 = (struct reloc_ext_external
*) dynrels
;
2426 for (; counter
< dynrel_count
; counter
++, rptr
++, cache_ptr
++)
2428 NAME(aout
,swap_ext_reloc_in
) (abfd
, rptr
, cache_ptr
, dynsyms
);
2429 cache_ptr
->address
-= bfd_get_section_vma (abfd
, asect
);
2430 if (cache_ptr
->address
>= bfd_section_size (abfd
, asect
))
2436 register struct reloc_std_external
*rptr
2437 = (struct reloc_std_external
*) dynrels
;
2439 for (; counter
< dynrel_count
; counter
++, rptr
++, cache_ptr
++)
2441 NAME(aout
,swap_std_reloc_in
) (abfd
, rptr
, cache_ptr
, dynsyms
);
2442 cache_ptr
->address
-= bfd_get_section_vma (abfd
, asect
);
2443 if (cache_ptr
->address
>= bfd_section_size (abfd
, asect
))
2449 bfd_release (abfd
,relocs
);
2450 asect
->relocation
= reloc_cache
;
2451 asect
->reloc_count
= cache_ptr
- reloc_cache
;
2457 /* Write out a relocation section into an object file. */
2460 NAME(aout
,squirt_out_relocs
) (abfd
, section
)
2465 unsigned char *native
, *natptr
;
2468 unsigned int count
= section
->reloc_count
;
2471 if (count
== 0) return true;
2473 each_size
= obj_reloc_entry_size (abfd
);
2474 natsize
= each_size
* count
;
2475 native
= (unsigned char *) bfd_zalloc (abfd
, natsize
);
2477 bfd_set_error (bfd_error_no_memory
);
2481 generic
= section
->orelocation
;
2483 if (each_size
== RELOC_EXT_SIZE
)
2485 for (natptr
= native
;
2487 --count
, natptr
+= each_size
, ++generic
)
2488 NAME(aout
,swap_ext_reloc_out
) (abfd
, *generic
, (struct reloc_ext_external
*)natptr
);
2492 for (natptr
= native
;
2494 --count
, natptr
+= each_size
, ++generic
)
2495 NAME(aout
,swap_std_reloc_out
)(abfd
, *generic
, (struct reloc_std_external
*)natptr
);
2498 if ( bfd_write ((PTR
) native
, 1, natsize
, abfd
) != natsize
) {
2499 bfd_release(abfd
, native
);
2502 bfd_release (abfd
, native
);
2507 /* This is stupid. This function should be a boolean predicate */
2509 NAME(aout
,canonicalize_reloc
) (abfd
, section
, relptr
, symbols
)
2515 arelent
*tblptr
= section
->relocation
;
2518 if (!(tblptr
|| NAME(aout
,slurp_reloc_table
)(abfd
, section
, symbols
)))
2521 if (section
->flags
& SEC_CONSTRUCTOR
) {
2522 arelent_chain
*chain
= section
->constructor_chain
;
2523 for (count
= 0; count
< section
->reloc_count
; count
++) {
2524 *relptr
++ = &chain
->relent
;
2525 chain
= chain
->next
;
2529 tblptr
= section
->relocation
;
2530 if (!tblptr
) return 0;
2532 for (count
= 0; count
++ < section
->reloc_count
;)
2534 *relptr
++ = tblptr
++;
2539 return section
->reloc_count
;
2543 NAME(aout
,get_reloc_upper_bound
) (abfd
, asect
)
2547 bfd_size_type dynrel_count
= 0;
2549 if (bfd_get_format (abfd
) != bfd_object
) {
2550 bfd_set_error (bfd_error_invalid_operation
);
2553 if (asect
->flags
& SEC_CONSTRUCTOR
) {
2554 return (sizeof (arelent
*) * (asect
->reloc_count
+1));
2557 if ((bfd_get_file_flags (abfd
) & DYNAMIC
) != 0
2558 && aout_backend_info (abfd
)->read_dynamic_relocs
)
2562 dynrel_count
= ((*aout_backend_info (abfd
)->read_dynamic_relocs
)
2564 if (dynrel_count
== (bfd_size_type
) -1)
2568 if (asect
== obj_datasec (abfd
))
2569 return (sizeof (arelent
*) *
2570 ((exec_hdr(abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
))
2571 + dynrel_count
+ 1));
2573 if (asect
== obj_textsec (abfd
))
2574 return (sizeof (arelent
*) *
2575 ((exec_hdr(abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
))
2576 + dynrel_count
+ 1));
2578 bfd_set_error (bfd_error_invalid_operation
);
2584 NAME(aout
,get_symtab_upper_bound
) (abfd
)
2587 if (!NAME(aout
,slurp_symbol_table
)(abfd
))
2590 return (bfd_get_symcount (abfd
)+1) * (sizeof (aout_symbol_type
*));
2595 NAME(aout
,get_lineno
) (ignore_abfd
, ignore_symbol
)
2597 asymbol
*ignore_symbol
;
2599 return (alent
*)NULL
;
2604 NAME(aout
,get_symbol_info
) (ignore_abfd
, symbol
, ret
)
2609 bfd_symbol_info (symbol
, ret
);
2611 if (ret
->type
== '?')
2613 int type_code
= aout_symbol(symbol
)->type
& 0xff;
2614 CONST
char *stab_name
= aout_stab_name(type_code
);
2615 static char buf
[10];
2617 if (stab_name
== NULL
)
2619 sprintf(buf
, "(%d)", type_code
);
2623 ret
->stab_other
= (unsigned)(aout_symbol(symbol
)->other
& 0xff);
2624 ret
->stab_desc
= (unsigned)(aout_symbol(symbol
)->desc
& 0xffff);
2625 ret
->stab_name
= stab_name
;
2631 NAME(aout
,print_symbol
) (ignore_abfd
, afile
, symbol
, how
)
2635 bfd_print_symbol_type how
;
2637 FILE *file
= (FILE *)afile
;
2640 case bfd_print_symbol_name
:
2642 fprintf(file
,"%s", symbol
->name
);
2644 case bfd_print_symbol_more
:
2645 fprintf(file
,"%4x %2x %2x",(unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2646 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2647 (unsigned)(aout_symbol(symbol
)->type
));
2649 case bfd_print_symbol_all
:
2651 CONST
char *section_name
= symbol
->section
->name
;
2654 bfd_print_symbol_vandf((PTR
)file
,symbol
);
2656 fprintf(file
," %-5s %04x %02x %02x",
2658 (unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2659 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2660 (unsigned)(aout_symbol(symbol
)->type
& 0xff));
2662 fprintf(file
," %s", symbol
->name
);
2669 provided a BFD, a section and an offset into the section, calculate
2670 and return the name of the source file and the line nearest to the
2675 NAME(aout
,find_nearest_line
)
2676 (abfd
, section
, symbols
, offset
, filename_ptr
, functionname_ptr
, line_ptr
)
2681 CONST
char **filename_ptr
;
2682 CONST
char **functionname_ptr
;
2683 unsigned int *line_ptr
;
2685 /* Run down the file looking for the filename, function and linenumber */
2687 static char buffer
[100];
2688 static char filename_buffer
[200];
2689 CONST
char *directory_name
= NULL
;
2690 CONST
char *main_file_name
= NULL
;
2691 CONST
char *current_file_name
= NULL
;
2692 CONST
char *line_file_name
= NULL
; /* Value of current_file_name at line number. */
2693 bfd_vma high_line_vma
= ~0;
2694 bfd_vma low_func_vma
= 0;
2696 *filename_ptr
= abfd
->filename
;
2697 *functionname_ptr
= 0;
2699 if (symbols
!= (asymbol
**)NULL
) {
2700 for (p
= symbols
; *p
; p
++) {
2701 aout_symbol_type
*q
= (aout_symbol_type
*)(*p
);
2705 main_file_name
= current_file_name
= q
->symbol
.name
;
2706 /* Look ahead to next symbol to check if that too is an N_SO. */
2710 q
= (aout_symbol_type
*)(*p
);
2711 if (q
->type
!= (int)N_SO
)
2714 /* Found a second N_SO First is directory; second is filename. */
2715 directory_name
= current_file_name
;
2716 main_file_name
= current_file_name
= q
->symbol
.name
;
2717 if (obj_textsec(abfd
) != section
)
2721 current_file_name
= q
->symbol
.name
;
2728 /* We'll keep this if it resolves nearer than the one we have already */
2729 if (q
->symbol
.value
>= offset
&&
2730 q
->symbol
.value
< high_line_vma
) {
2731 *line_ptr
= q
->desc
;
2732 high_line_vma
= q
->symbol
.value
;
2733 line_file_name
= current_file_name
;
2738 /* We'll keep this if it is nearer than the one we have already */
2739 if (q
->symbol
.value
>= low_func_vma
&&
2740 q
->symbol
.value
<= offset
) {
2741 low_func_vma
= q
->symbol
.value
;
2742 func
= (asymbol
*)q
;
2744 if (*line_ptr
&& func
) {
2745 CONST
char *function
= func
->name
;
2748 /* The caller expects a symbol name. We actually have a
2749 function name, without the leading underscore. Put the
2750 underscore back in, so that the caller gets a symbol
2752 if (bfd_get_symbol_leading_char (abfd
) == '\0')
2753 strncpy (buffer
, function
, sizeof (buffer
) - 1);
2756 buffer
[0] = bfd_get_symbol_leading_char (abfd
);
2757 strncpy (buffer
+ 1, function
, sizeof (buffer
) - 2);
2759 buffer
[sizeof(buffer
)-1] = 0;
2760 /* Have to remove : stuff */
2761 p
= strchr(buffer
,':');
2762 if (p
!= NULL
) { *p
= '\0'; }
2763 *functionname_ptr
= buffer
;
2775 main_file_name
= line_file_name
;
2776 if (main_file_name
) {
2777 if (main_file_name
[0] == '/' || directory_name
== NULL
)
2778 *filename_ptr
= main_file_name
;
2780 sprintf(filename_buffer
, "%.140s%.50s",
2781 directory_name
, main_file_name
);
2782 *filename_ptr
= filename_buffer
;
2791 NAME(aout
,sizeof_headers
) (abfd
, execable
)
2795 return adata(abfd
).exec_bytes_size
;
2798 /* a.out link code. */
2800 /* a.out linker hash table entries. */
2802 struct aout_link_hash_entry
2804 struct bfd_link_hash_entry root
;
2805 /* Symbol index in output file. */
2809 /* a.out linker hash table. */
2811 struct aout_link_hash_table
2813 struct bfd_link_hash_table root
;
2816 static struct bfd_hash_entry
*aout_link_hash_newfunc
2817 PARAMS ((struct bfd_hash_entry
*entry
,
2818 struct bfd_hash_table
*table
,
2819 const char *string
));
2820 static boolean aout_link_add_object_symbols
2821 PARAMS ((bfd
*, struct bfd_link_info
*));
2822 static boolean aout_link_check_archive_element
2823 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
2824 static boolean aout_link_get_symbols
PARAMS ((bfd
*));
2825 static boolean aout_link_free_symbols
PARAMS ((bfd
*));
2826 static boolean aout_link_check_ar_symbols
2827 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*pneeded
));
2828 static boolean aout_link_add_symbols
2829 PARAMS ((bfd
*, struct bfd_link_info
*));
2831 /* Routine to create an entry in an a.out link hash table. */
2833 static struct bfd_hash_entry
*
2834 aout_link_hash_newfunc (entry
, table
, string
)
2835 struct bfd_hash_entry
*entry
;
2836 struct bfd_hash_table
*table
;
2839 struct aout_link_hash_entry
*ret
= (struct aout_link_hash_entry
*) entry
;
2841 /* Allocate the structure if it has not already been allocated by a
2843 if (ret
== (struct aout_link_hash_entry
*) NULL
)
2844 ret
= ((struct aout_link_hash_entry
*)
2845 bfd_hash_allocate (table
, sizeof (struct aout_link_hash_entry
)));
2846 if (ret
== (struct aout_link_hash_entry
*) NULL
)
2848 bfd_set_error (bfd_error_no_memory
);
2849 return (struct bfd_hash_entry
*) ret
;
2852 /* Call the allocation method of the superclass. */
2853 ret
= ((struct aout_link_hash_entry
*)
2854 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2857 /* Set local fields. */
2860 return (struct bfd_hash_entry
*) ret
;
2863 /* Create an a.out link hash table. */
2865 struct bfd_link_hash_table
*
2866 NAME(aout
,link_hash_table_create
) (abfd
)
2869 struct aout_link_hash_table
*ret
;
2871 ret
= ((struct aout_link_hash_table
*)
2872 malloc (sizeof (struct aout_link_hash_table
)));
2873 if (ret
== (struct aout_link_hash_table
*) NULL
)
2875 bfd_set_error (bfd_error_no_memory
);
2876 return (struct bfd_link_hash_table
*) NULL
;
2878 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
2879 aout_link_hash_newfunc
))
2882 return (struct bfd_link_hash_table
*) NULL
;
2887 /* Look up an entry in an a.out link hash table. */
2889 #define aout_link_hash_lookup(table, string, create, copy, follow) \
2890 ((struct aout_link_hash_entry *) \
2891 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
2893 /* Traverse an a.out link hash table. */
2895 #define aout_link_hash_traverse(table, func, info) \
2896 (bfd_link_hash_traverse \
2898 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
2901 /* Get the a.out link hash table from the info structure. This is
2904 #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash))
2906 /* Given an a.out BFD, add symbols to the global hash table as
2910 NAME(aout
,link_add_symbols
) (abfd
, info
)
2912 struct bfd_link_info
*info
;
2914 switch (bfd_get_format (abfd
))
2917 return aout_link_add_object_symbols (abfd
, info
);
2919 return _bfd_generic_link_add_archive_symbols
2920 (abfd
, info
, aout_link_check_archive_element
);
2922 bfd_set_error (bfd_error_wrong_format
);
2927 /* Add symbols from an a.out object file. */
2930 aout_link_add_object_symbols (abfd
, info
)
2932 struct bfd_link_info
*info
;
2934 if (! aout_link_get_symbols (abfd
))
2936 if (! aout_link_add_symbols (abfd
, info
))
2938 if (! info
->keep_memory
)
2940 if (! aout_link_free_symbols (abfd
))
2946 /* Check a single archive element to see if we need to include it in
2947 the link. *PNEEDED is set according to whether this element is
2948 needed in the link or not. This is called from
2949 _bfd_generic_link_add_archive_symbols. */
2952 aout_link_check_archive_element (abfd
, info
, pneeded
)
2954 struct bfd_link_info
*info
;
2957 if (! aout_link_get_symbols (abfd
))
2960 if (! aout_link_check_ar_symbols (abfd
, info
, pneeded
))
2965 if (! aout_link_add_symbols (abfd
, info
))
2969 /* We keep around the symbols even if we aren't going to use this
2970 object file, because we may want to reread it. This doesn't
2971 waste too much memory, because it isn't all that common to read
2972 an archive element but not need it. */
2973 if (! info
->keep_memory
)
2975 if (! aout_link_free_symbols (abfd
))
2982 /* Read the internal symbols from an a.out file. */
2985 aout_link_get_symbols (abfd
)
2988 bfd_size_type count
;
2989 struct external_nlist
*syms
;
2990 unsigned char string_chars
[BYTES_IN_WORD
];
2991 bfd_size_type stringsize
;
2994 if (obj_aout_external_syms (abfd
) != (struct external_nlist
*) NULL
)
2996 /* We already have them. */
3000 count
= exec_hdr (abfd
)->a_syms
/ EXTERNAL_NLIST_SIZE
;
3002 /* We allocate using malloc to make the values easy to free
3003 later on. If we put them on the obstack it might not be possible
3005 syms
= ((struct external_nlist
*)
3006 malloc ((size_t) count
* EXTERNAL_NLIST_SIZE
));
3007 if (syms
== (struct external_nlist
*) NULL
&& count
!= 0)
3009 bfd_set_error (bfd_error_no_memory
);
3013 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
3014 || (bfd_read ((PTR
) syms
, 1, exec_hdr (abfd
)->a_syms
, abfd
)
3015 != exec_hdr (abfd
)->a_syms
))
3018 /* Get the size of the strings. */
3019 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0
3020 || (bfd_read ((PTR
) string_chars
, BYTES_IN_WORD
, 1, abfd
)
3023 stringsize
= GET_WORD (abfd
, string_chars
);
3024 strings
= (char *) malloc ((size_t) stringsize
);
3025 if (strings
== NULL
&& stringsize
!= 0)
3027 bfd_set_error (bfd_error_no_memory
);
3031 /* Skip space for the string count in the buffer for convenience
3032 when using indexes. */
3033 if (bfd_read (strings
+ BYTES_IN_WORD
, 1, stringsize
- BYTES_IN_WORD
, abfd
)
3034 != stringsize
- BYTES_IN_WORD
)
3037 /* Save the data. */
3038 obj_aout_external_syms (abfd
) = syms
;
3039 obj_aout_external_sym_count (abfd
) = count
;
3040 obj_aout_external_strings (abfd
) = strings
;
3045 /* Free up the internal symbols read from an a.out file. */
3048 aout_link_free_symbols (abfd
)
3051 if (obj_aout_external_syms (abfd
) != (struct external_nlist
*) NULL
)
3053 free ((PTR
) obj_aout_external_syms (abfd
));
3054 obj_aout_external_syms (abfd
) = (struct external_nlist
*) NULL
;
3056 if (obj_aout_external_strings (abfd
) != (char *) NULL
)
3058 free ((PTR
) obj_aout_external_strings (abfd
));
3059 obj_aout_external_strings (abfd
) = (char *) NULL
;
3064 /* Look through the internal symbols to see if this object file should
3065 be included in the link. We should include this object file if it
3066 defines any symbols which are currently undefined. If this object
3067 file defines a common symbol, then we may adjust the size of the
3068 known symbol but we do not include the object file in the link
3069 (unless there is some other reason to include it). */
3072 aout_link_check_ar_symbols (abfd
, info
, pneeded
)
3074 struct bfd_link_info
*info
;
3077 register struct external_nlist
*p
;
3078 struct external_nlist
*pend
;
3083 /* Look through all the symbols. */
3084 p
= obj_aout_external_syms (abfd
);
3085 pend
= p
+ obj_aout_external_sym_count (abfd
);
3086 strings
= obj_aout_external_strings (abfd
);
3087 for (; p
< pend
; p
++)
3089 int type
= bfd_h_get_8 (abfd
, p
->e_type
);
3091 struct bfd_link_hash_entry
*h
;
3093 /* Ignore symbols that are not externally visible. */
3094 if ((type
& N_EXT
) == 0)
3096 if (type
== N_WARNING
3102 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3103 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
3105 /* We are only interested in symbols that are currently
3106 undefined or common. */
3107 if (h
== (struct bfd_link_hash_entry
*) NULL
3108 || (h
->type
!= bfd_link_hash_undefined
3109 && h
->type
!= bfd_link_hash_common
))
3111 if (type
== (N_INDR
| N_EXT
))
3116 if (type
== (N_TEXT
| N_EXT
)
3117 || type
== (N_DATA
| N_EXT
)
3118 || type
== (N_BSS
| N_EXT
)
3119 || type
== (N_ABS
| N_EXT
)
3120 || type
== (N_INDR
| N_EXT
))
3122 /* This object file defines this symbol. We must link it
3123 in. This is true regardless of whether the current
3124 definition of the symbol is undefined or common. If the
3125 current definition is common, we have a case in which we
3126 have already seen an object file including
3128 and this object file from the archive includes
3130 In such a case we must include this object file. */
3131 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
3137 if (type
== (N_UNDF
| N_EXT
))
3141 value
= GET_WORD (abfd
, p
->e_value
);
3144 /* This symbol is common in the object from the archive
3146 if (h
->type
== bfd_link_hash_undefined
)
3150 symbfd
= h
->u
.undef
.abfd
;
3151 if (symbfd
== (bfd
*) NULL
)
3153 /* This symbol was created as undefined from
3154 outside BFD. We assume that we should link
3155 in the object file. This is done for the -u
3156 option in the linker. */
3157 if (! (*info
->callbacks
->add_archive_element
) (info
,
3164 /* Turn the current link symbol into a common
3165 symbol. It is already on the undefs list. */
3166 h
->type
= bfd_link_hash_common
;
3167 h
->u
.c
.size
= value
;
3168 h
->u
.c
.section
= bfd_make_section_old_way (symbfd
,
3173 /* Adjust the size of the common symbol if
3175 if (value
> h
->u
.c
.size
)
3176 h
->u
.c
.size
= value
;
3182 /* We do not need this object file. */
3186 /* Add all symbols from an object file to the hash table. */
3189 aout_link_add_symbols (abfd
, info
)
3191 struct bfd_link_info
*info
;
3193 bfd_size_type sym_count
;
3196 struct aout_link_hash_entry
**sym_hash
;
3197 register struct external_nlist
*p
;
3198 struct external_nlist
*pend
;
3200 sym_count
= obj_aout_external_sym_count (abfd
);
3201 strings
= obj_aout_external_strings (abfd
);
3202 if (info
->keep_memory
)
3207 /* We keep a list of the linker hash table entries that correspond
3208 to particular symbols. We could just look them up in the hash
3209 table, but keeping the list is more efficient. Perhaps this
3210 should be conditional on info->keep_memory. */
3211 sym_hash
= ((struct aout_link_hash_entry
**)
3214 * sizeof (struct aout_link_hash_entry
*))));
3217 bfd_set_error (bfd_error_no_memory
);
3220 obj_aout_sym_hashes (abfd
) = sym_hash
;
3222 p
= obj_aout_external_syms (abfd
);
3223 pend
= p
+ sym_count
;
3224 for (; p
< pend
; p
++, sym_hash
++)
3235 type
= bfd_h_get_8 (abfd
, p
->e_type
);
3237 /* Ignore debugging symbols. */
3238 if ((type
& N_STAB
) != 0)
3241 /* Ignore symbols that are not external. */
3242 if ((type
& N_EXT
) == 0
3243 && type
!= N_WARNING
3249 /* If this is an N_INDR symbol we must skip the next entry,
3250 which is the symbol to indirect to (actually, an N_INDR
3251 symbol without N_EXT set is pretty useless). */
3260 /* Ignore N_FN symbols (these appear to have N_EXT set). */
3264 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3265 value
= GET_WORD (abfd
, p
->e_value
);
3272 case N_UNDF
| N_EXT
:
3274 section
= &bfd_com_section
;
3276 section
= &bfd_und_section
;
3279 section
= &bfd_abs_section
;
3281 case N_TEXT
| N_EXT
:
3282 section
= obj_textsec (abfd
);
3283 value
-= bfd_get_section_vma (abfd
, section
);
3285 case N_DATA
| N_EXT
:
3286 section
= obj_datasec (abfd
);
3287 value
-= bfd_get_section_vma (abfd
, section
);
3290 section
= obj_bsssec (abfd
);
3291 value
-= bfd_get_section_vma (abfd
, section
);
3293 case N_INDR
| N_EXT
:
3294 /* An indirect symbol. The next symbol is the symbol
3295 which this one really is. */
3296 BFD_ASSERT (p
+ 1 < pend
);
3298 string
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3299 section
= &bfd_ind_section
;
3300 flags
|= BSF_INDIRECT
;
3302 case N_COMM
| N_EXT
:
3303 section
= &bfd_com_section
;
3305 case N_SETA
: case N_SETA
| N_EXT
:
3306 section
= &bfd_abs_section
;
3307 flags
|= BSF_CONSTRUCTOR
;
3309 case N_SETT
: case N_SETT
| N_EXT
:
3310 section
= obj_textsec (abfd
);
3311 flags
|= BSF_CONSTRUCTOR
;
3312 value
-= bfd_get_section_vma (abfd
, section
);
3314 case N_SETD
: case N_SETD
| N_EXT
:
3315 section
= obj_datasec (abfd
);
3316 flags
|= BSF_CONSTRUCTOR
;
3317 value
-= bfd_get_section_vma (abfd
, section
);
3319 case N_SETB
: case N_SETB
| N_EXT
:
3320 section
= obj_bsssec (abfd
);
3321 flags
|= BSF_CONSTRUCTOR
;
3322 value
-= bfd_get_section_vma (abfd
, section
);
3325 /* A warning symbol. The next symbol is the one to warn
3327 BFD_ASSERT (p
+ 1 < pend
);
3330 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3331 section
= &bfd_und_section
;
3332 flags
|= BSF_WARNING
;
3336 if (! (_bfd_generic_link_add_one_symbol
3337 (info
, abfd
, name
, flags
, section
, value
, string
, copy
, false,
3338 (struct bfd_link_hash_entry
**) sym_hash
)))
3341 if (type
== (N_INDR
| N_EXT
) || type
== N_WARNING
)
3348 /* During the final link step we need to pass around a bunch of
3349 information, so we do it in an instance of this structure. */
3351 struct aout_final_link_info
3353 /* General link information. */
3354 struct bfd_link_info
*info
;
3357 /* Reloc file positions. */
3358 file_ptr treloff
, dreloff
;
3359 /* File position of symbols. */
3362 struct stringtab_data strtab
;
3365 static boolean aout_link_input_bfd
3366 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
));
3367 static boolean aout_link_write_symbols
3368 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
, int *symbol_map
));
3369 static boolean aout_link_write_other_symbol
3370 PARAMS ((struct aout_link_hash_entry
*, PTR
));
3371 static boolean aout_link_input_section
3372 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3373 asection
*input_section
, file_ptr
*reloff_ptr
,
3374 bfd_size_type rel_size
, int *symbol_map
));
3375 static boolean aout_link_input_section_std
3376 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3377 asection
*input_section
, struct reloc_std_external
*,
3378 bfd_size_type rel_size
, bfd_byte
*contents
, int *symbol_map
));
3379 static boolean aout_link_input_section_ext
3380 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3381 asection
*input_section
, struct reloc_ext_external
*,
3382 bfd_size_type rel_size
, bfd_byte
*contents
, int *symbol_map
));
3383 static INLINE asection
*aout_reloc_index_to_section
3384 PARAMS ((bfd
*, int));
3385 static boolean aout_link_reloc_link_order
3386 PARAMS ((struct aout_final_link_info
*, asection
*,
3387 struct bfd_link_order
*));
3389 /* Do the final link step. This is called on the output BFD. The
3390 INFO structure should point to a list of BFDs linked through the
3391 link_next field which can be used to find each BFD which takes part
3392 in the output. Also, each section in ABFD should point to a list
3393 of bfd_link_order structures which list all the input sections for
3394 the output section. */
3397 NAME(aout
,final_link
) (abfd
, info
, callback
)
3399 struct bfd_link_info
*info
;
3400 void (*callback
) PARAMS ((bfd
*, file_ptr
*, file_ptr
*, file_ptr
*));
3402 struct aout_final_link_info aout_info
;
3404 bfd_size_type text_size
;
3406 register struct bfd_link_order
*p
;
3408 boolean have_link_order_relocs
;
3410 aout_info
.info
= info
;
3411 aout_info
.output_bfd
= abfd
;
3413 if (! info
->relocateable
)
3415 exec_hdr (abfd
)->a_trsize
= 0;
3416 exec_hdr (abfd
)->a_drsize
= 0;
3420 bfd_size_type trsize
, drsize
;
3422 /* Count up the relocation sizes. */
3425 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
3427 if (bfd_get_flavour (abfd
) == bfd_target_aout_flavour
)
3429 trsize
+= exec_hdr (sub
)->a_trsize
;
3430 drsize
+= exec_hdr (sub
)->a_drsize
;
3434 /* FIXME: We need to identify the .text and .data sections
3435 and call get_reloc_upper_bound and canonicalize_reloc to
3436 work out the number of relocs needed, and then multiply
3437 by the reloc size. */
3441 trsize
+= (_bfd_count_link_order_relocs (obj_textsec (abfd
)
3443 * obj_reloc_entry_size (abfd
));
3444 exec_hdr (abfd
)->a_trsize
= trsize
;
3445 drsize
+= (_bfd_count_link_order_relocs (obj_datasec (abfd
)
3447 * obj_reloc_entry_size (abfd
));
3448 exec_hdr (abfd
)->a_drsize
= drsize
;
3451 exec_hdr (abfd
)->a_entry
= bfd_get_start_address (abfd
);
3453 /* Adjust the section sizes and vmas according to the magic number.
3454 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3455 filepos for each section. */
3456 if (! NAME(aout
,adjust_sizes_and_vmas
) (abfd
, &text_size
, &text_end
))
3459 /* The relocation and symbol file positions differ among a.out
3460 targets. We are passed a callback routine from the backend
3461 specific code to handle this.
3462 FIXME: At this point we do not know how much space the symbol
3463 table will require. This will not work for any (nonstandard)
3464 a.out target that needs to know the symbol table size before it
3465 can compute the relocation file positions. This may or may not
3466 be the case for the hp300hpux target, for example. */
3467 (*callback
) (abfd
, &aout_info
.treloff
, &aout_info
.dreloff
,
3469 obj_textsec (abfd
)->rel_filepos
= aout_info
.treloff
;
3470 obj_datasec (abfd
)->rel_filepos
= aout_info
.dreloff
;
3471 obj_sym_filepos (abfd
) = aout_info
.symoff
;
3473 /* We keep a count of the symbols as we output them. */
3474 obj_aout_external_sym_count (abfd
) = 0;
3476 /* We accumulate the string table as we write out the symbols. */
3477 stringtab_init (&aout_info
.strtab
);
3479 /* The most time efficient way to do the link would be to read all
3480 the input object files into memory and then sort out the
3481 information into the output file. Unfortunately, that will
3482 probably use too much memory. Another method would be to step
3483 through everything that composes the text section and write it
3484 out, and then everything that composes the data section and write
3485 it out, and then write out the relocs, and then write out the
3486 symbols. Unfortunately, that requires reading stuff from each
3487 input file several times, and we will not be able to keep all the
3488 input files open simultaneously, and reopening them will be slow.
3490 What we do is basically process one input file at a time. We do
3491 everything we need to do with an input file once--copy over the
3492 section contents, handle the relocation information, and write
3493 out the symbols--and then we throw away the information we read
3494 from it. This approach requires a lot of lseeks of the output
3495 file, which is unfortunate but still faster than reopening a lot
3498 We use the output_has_begun field of the input BFDs to see
3499 whether we have already handled it. */
3500 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
3501 sub
->output_has_begun
= false;
3503 have_link_order_relocs
= false;
3504 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3506 for (p
= o
->link_order_head
;
3507 p
!= (struct bfd_link_order
*) NULL
;
3510 if (p
->type
== bfd_indirect_link_order
3511 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3512 == bfd_target_aout_flavour
))
3516 input_bfd
= p
->u
.indirect
.section
->owner
;
3517 if (! input_bfd
->output_has_begun
)
3519 if (! aout_link_input_bfd (&aout_info
, input_bfd
))
3521 input_bfd
->output_has_begun
= true;
3524 else if (p
->type
== bfd_section_reloc_link_order
3525 || p
->type
== bfd_symbol_reloc_link_order
)
3527 /* These are handled below. */
3528 have_link_order_relocs
= true;
3532 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3538 /* Write out any symbols that we have not already written out. */
3539 aout_link_hash_traverse (aout_hash_table (info
),
3540 aout_link_write_other_symbol
,
3543 /* Now handle any relocs we were asked to create by the linker.
3544 These did not come from any input file. We must do these after
3545 we have written out all the symbols, so that we know the symbol
3547 if (have_link_order_relocs
)
3549 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3551 for (p
= o
->link_order_head
;
3552 p
!= (struct bfd_link_order
*) NULL
;
3555 if (p
->type
== bfd_section_reloc_link_order
3556 || p
->type
== bfd_symbol_reloc_link_order
)
3558 if (! aout_link_reloc_link_order (&aout_info
, o
, p
))
3565 /* Update the header information. */
3566 abfd
->symcount
= obj_aout_external_sym_count (abfd
);
3567 exec_hdr (abfd
)->a_syms
= abfd
->symcount
* EXTERNAL_NLIST_SIZE
;
3568 obj_str_filepos (abfd
) = obj_sym_filepos (abfd
) + exec_hdr (abfd
)->a_syms
;
3569 obj_textsec (abfd
)->reloc_count
=
3570 exec_hdr (abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
);
3571 obj_datasec (abfd
)->reloc_count
=
3572 exec_hdr (abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
);
3574 /* Write out the string table. */
3575 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0)
3577 emit_strtab (abfd
, &aout_info
.strtab
);
3582 /* Link an a.out input BFD into the output file. */
3585 aout_link_input_bfd (finfo
, input_bfd
)
3586 struct aout_final_link_info
*finfo
;
3589 bfd_size_type sym_count
;
3590 int *symbol_map
= NULL
;
3592 BFD_ASSERT (bfd_get_format (input_bfd
) == bfd_object
);
3594 /* Get the symbols. We probably have them already, unless
3595 finfo->info->keep_memory is false. */
3596 if (! aout_link_get_symbols (input_bfd
))
3599 sym_count
= obj_aout_external_sym_count (input_bfd
);
3600 symbol_map
= (int *) malloc ((size_t) sym_count
* sizeof (int));
3601 if (symbol_map
== NULL
&& sym_count
!= 0)
3603 bfd_set_error (bfd_error_no_memory
);
3607 /* Write out the symbols and get a map of the new indices. */
3608 if (! aout_link_write_symbols (finfo
, input_bfd
, symbol_map
))
3611 /* Relocate and write out the sections. */
3612 if (! aout_link_input_section (finfo
, input_bfd
,
3613 obj_textsec (input_bfd
),
3615 exec_hdr (input_bfd
)->a_trsize
,
3617 || ! aout_link_input_section (finfo
, input_bfd
,
3618 obj_datasec (input_bfd
),
3620 exec_hdr (input_bfd
)->a_drsize
,
3624 /* If we are not keeping memory, we don't need the symbols any
3625 longer. We still need them if we are keeping memory, because the
3626 strings in the hash table point into them. */
3627 if (! finfo
->info
->keep_memory
)
3629 if (! aout_link_free_symbols (input_bfd
))
3633 if (symbol_map
!= NULL
)
3637 if (symbol_map
!= NULL
)
3642 /* Adjust and write out the symbols for an a.out file. Set the new
3643 symbol indices into a symbol_map. */
3646 aout_link_write_symbols (finfo
, input_bfd
, symbol_map
)
3647 struct aout_final_link_info
*finfo
;
3652 bfd_size_type sym_count
;
3654 enum bfd_link_strip strip
;
3655 enum bfd_link_discard discard
;
3656 struct external_nlist
*output_syms
= NULL
;
3657 struct external_nlist
*outsym
;
3658 register struct external_nlist
*sym
;
3659 struct external_nlist
*sym_end
;
3660 struct aout_link_hash_entry
**sym_hash
;
3662 boolean skip_indirect
;
3664 output_bfd
= finfo
->output_bfd
;
3665 sym_count
= obj_aout_external_sym_count (input_bfd
);
3666 strings
= obj_aout_external_strings (input_bfd
);
3667 strip
= finfo
->info
->strip
;
3668 discard
= finfo
->info
->discard
;
3669 output_syms
= ((struct external_nlist
*)
3670 malloc ((size_t) (sym_count
+ 1) * EXTERNAL_NLIST_SIZE
));
3671 if (output_syms
== NULL
)
3673 bfd_set_error (bfd_error_no_memory
);
3676 outsym
= output_syms
;
3678 /* First write out a symbol for this object file, unless we are
3679 discarding such symbols. */
3680 if (strip
!= strip_all
3681 && (strip
!= strip_some
3682 || bfd_hash_lookup (finfo
->info
->keep_hash
, input_bfd
->filename
,
3683 false, false) != NULL
)
3684 && discard
!= discard_all
)
3686 bfd_h_put_8 (output_bfd
, N_TEXT
, outsym
->e_type
);
3687 bfd_h_put_8 (output_bfd
, 0, outsym
->e_other
);
3688 bfd_h_put_16 (output_bfd
, (bfd_vma
) 0, outsym
->e_desc
);
3689 PUT_WORD (output_bfd
,
3690 add_to_stringtab (output_bfd
, input_bfd
->filename
,
3693 PUT_WORD (output_bfd
,
3694 (bfd_get_section_vma (output_bfd
,
3695 obj_textsec (input_bfd
)->output_section
)
3696 + obj_textsec (input_bfd
)->output_offset
),
3698 ++obj_aout_external_sym_count (output_bfd
);
3703 skip_indirect
= false;
3704 sym
= obj_aout_external_syms (input_bfd
);
3705 sym_end
= sym
+ sym_count
;
3706 sym_hash
= obj_aout_sym_hashes (input_bfd
);
3707 for (; sym
< sym_end
; sym
++, sym_hash
++, symbol_map
++)
3717 type
= bfd_h_get_8 (input_bfd
, sym
->e_type
);
3718 name
= strings
+ GET_WORD (input_bfd
, sym
->e_strx
);
3722 /* Pass this symbol through. It is the target of an
3723 indirect or warning symbol. */
3724 val
= GET_WORD (input_bfd
, sym
->e_value
);
3727 else if (skip_indirect
)
3729 /* Skip this symbol, which is the target of an indirect
3730 symbol that we have changed to no longer be an indirect
3732 skip_indirect
= false;
3737 struct aout_link_hash_entry
*h
;
3738 struct aout_link_hash_entry
*hresolve
;
3740 /* We have saved the hash table entry for this symbol, if
3741 there is one. Note that we could just look it up again
3742 in the hash table, provided we first check that it is an
3746 /* If this is an indirect or warning symbol, then change
3747 hresolve to the base symbol. We also change *sym_hash so
3748 that the relocation routines relocate against the real
3751 if (h
!= (struct aout_link_hash_entry
*) NULL
3752 && (h
->root
.type
== bfd_link_hash_indirect
3753 || h
->root
.type
== bfd_link_hash_warning
))
3755 hresolve
= (struct aout_link_hash_entry
*) h
->root
.u
.i
.link
;
3756 while (hresolve
->root
.type
== bfd_link_hash_indirect
)
3757 hresolve
= ((struct aout_link_hash_entry
*)
3758 hresolve
->root
.u
.i
.link
);
3759 *sym_hash
= hresolve
;
3762 /* If the symbol has already been written out, skip it. */
3763 if (h
!= (struct aout_link_hash_entry
*) NULL
3766 *symbol_map
= h
->indx
;
3770 /* See if we are stripping this symbol. */
3776 case strip_debugger
:
3777 if ((type
& N_STAB
) != 0)
3781 if (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
3791 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3792 h
->root
.written
= true;
3796 /* Get the value of the symbol. */
3797 if ((type
& N_TYPE
) == N_TEXT
)
3798 symsec
= obj_textsec (input_bfd
);
3799 else if ((type
& N_TYPE
) == N_DATA
)
3800 symsec
= obj_datasec (input_bfd
);
3801 else if ((type
& N_TYPE
) == N_BSS
)
3802 symsec
= obj_bsssec (input_bfd
);
3803 else if ((type
& N_TYPE
) == N_ABS
)
3804 symsec
= &bfd_abs_section
;
3805 else if (((type
& N_TYPE
) == N_INDR
3806 && (hresolve
== (struct aout_link_hash_entry
*) NULL
3807 || (hresolve
->root
.type
!= bfd_link_hash_defined
3808 && hresolve
->root
.type
!= bfd_link_hash_common
)))
3809 || type
== N_WARNING
)
3811 /* Pass the next symbol through unchanged. The
3812 condition above for indirect symbols is so that if
3813 the indirect symbol was defined, we output it with
3814 the correct definition so the debugger will
3817 val
= GET_WORD (input_bfd
, sym
->e_value
);
3820 else if ((type
& N_STAB
) != 0)
3822 val
= GET_WORD (input_bfd
, sym
->e_value
);
3827 /* If we get here with an indirect symbol, it means that
3828 we are outputting it with a real definition. In such
3829 a case we do not want to output the next symbol,
3830 which is the target of the indirection. */
3831 if ((type
& N_TYPE
) == N_INDR
)
3832 skip_indirect
= true;
3834 /* We need to get the value from the hash table. We use
3835 hresolve so that if we have defined an indirect
3836 symbol we output the final definition. */
3837 if (h
== (struct aout_link_hash_entry
*) NULL
)
3839 else if (hresolve
->root
.type
== bfd_link_hash_defined
)
3841 asection
*input_section
;
3842 asection
*output_section
;
3844 /* This case means a common symbol which was turned
3845 into a defined symbol. */
3846 input_section
= hresolve
->root
.u
.def
.section
;
3847 output_section
= input_section
->output_section
;
3848 BFD_ASSERT (output_section
== &bfd_abs_section
3849 || output_section
->owner
== output_bfd
);
3850 val
= (hresolve
->root
.u
.def
.value
3851 + bfd_get_section_vma (output_bfd
, output_section
)
3852 + input_section
->output_offset
);
3854 /* Get the correct type based on the section. If
3855 this is a constructed set, force it to be
3856 globally visible. */
3865 if (output_section
== obj_textsec (output_bfd
))
3867 else if (output_section
== obj_datasec (output_bfd
))
3869 else if (output_section
== obj_bsssec (output_bfd
))
3874 else if (hresolve
->root
.type
== bfd_link_hash_common
)
3875 val
= hresolve
->root
.u
.c
.size
;
3881 if (symsec
!= (asection
*) NULL
)
3882 val
= (symsec
->output_section
->vma
3883 + symsec
->output_offset
3884 + (GET_WORD (input_bfd
, sym
->e_value
)
3887 /* If this is a global symbol set the written flag, and if
3888 it is a local symbol see if we should discard it. */
3889 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3891 h
->root
.written
= true;
3892 h
->indx
= obj_aout_external_sym_count (output_bfd
);
3901 if (*name
== *finfo
->info
->lprefix
3902 && (finfo
->info
->lprefix_len
== 1
3903 || strncmp (name
, finfo
->info
->lprefix
,
3904 finfo
->info
->lprefix_len
) == 0))
3919 /* Copy this symbol into the list of symbols we are going to
3921 bfd_h_put_8 (output_bfd
, type
, outsym
->e_type
);
3922 bfd_h_put_8 (output_bfd
, bfd_h_get_8 (input_bfd
, sym
->e_other
),
3924 bfd_h_put_16 (output_bfd
, bfd_h_get_16 (input_bfd
, sym
->e_desc
),
3926 PUT_WORD (output_bfd
,
3927 add_to_stringtab (output_bfd
, name
, &finfo
->strtab
),
3929 PUT_WORD (output_bfd
, val
, outsym
->e_value
);
3930 *symbol_map
= obj_aout_external_sym_count (output_bfd
);
3931 ++obj_aout_external_sym_count (output_bfd
);
3935 /* Write out the output symbols we have just constructed. */
3936 if (outsym
> output_syms
)
3938 bfd_size_type outsym_count
;
3940 if (bfd_seek (output_bfd
, finfo
->symoff
, SEEK_SET
) != 0)
3942 outsym_count
= outsym
- output_syms
;
3943 if (bfd_write ((PTR
) output_syms
, (bfd_size_type
) EXTERNAL_NLIST_SIZE
,
3944 (bfd_size_type
) outsym_count
, output_bfd
)
3945 != outsym_count
* EXTERNAL_NLIST_SIZE
)
3947 finfo
->symoff
+= outsym_count
* EXTERNAL_NLIST_SIZE
;
3950 if (output_syms
!= NULL
)
3954 if (output_syms
!= NULL
)
3959 /* Write out a symbol that was not associated with an a.out input
3963 aout_link_write_other_symbol (h
, data
)
3964 struct aout_link_hash_entry
*h
;
3967 struct aout_final_link_info
*finfo
= (struct aout_final_link_info
*) data
;
3971 struct external_nlist outsym
;
3973 if (h
->root
.written
)
3976 h
->root
.written
= true;
3978 if (finfo
->info
->strip
== strip_all
3979 || (finfo
->info
->strip
== strip_some
3980 && bfd_hash_lookup (finfo
->info
->keep_hash
, h
->root
.root
.string
,
3981 false, false) == NULL
))
3984 output_bfd
= finfo
->output_bfd
;
3986 switch (h
->root
.type
)
3989 case bfd_link_hash_new
:
3991 /* Avoid variable not initialized warnings. */
3993 case bfd_link_hash_undefined
:
3994 type
= N_UNDF
| N_EXT
;
3997 case bfd_link_hash_defined
:
4001 sec
= h
->root
.u
.def
.section
;
4002 BFD_ASSERT (sec
== &bfd_abs_section
4003 || sec
->owner
== output_bfd
);
4004 if (sec
== obj_textsec (output_bfd
))
4005 type
= N_TEXT
| N_EXT
;
4006 else if (sec
== obj_datasec (output_bfd
))
4007 type
= N_DATA
| N_EXT
;
4008 else if (sec
== obj_bsssec (output_bfd
))
4009 type
= N_BSS
| N_EXT
;
4011 type
= N_ABS
| N_EXT
;
4012 val
= (h
->root
.u
.def
.value
4013 + sec
->output_section
->vma
4014 + sec
->output_offset
);
4017 case bfd_link_hash_common
:
4018 type
= N_UNDF
| N_EXT
;
4019 val
= h
->root
.u
.c
.size
;
4021 case bfd_link_hash_indirect
:
4022 case bfd_link_hash_warning
:
4023 /* FIXME: Ignore these for now. The circumstances under which
4024 they should be written out are not clear to me. */
4028 bfd_h_put_8 (output_bfd
, type
, outsym
.e_type
);
4029 bfd_h_put_8 (output_bfd
, 0, outsym
.e_other
);
4030 bfd_h_put_16 (output_bfd
, 0, outsym
.e_desc
);
4031 PUT_WORD (output_bfd
,
4032 add_to_stringtab (output_bfd
, h
->root
.root
.string
, &finfo
->strtab
),
4034 PUT_WORD (output_bfd
, val
, outsym
.e_value
);
4036 if (bfd_seek (output_bfd
, finfo
->symoff
, SEEK_SET
) != 0
4037 || bfd_write ((PTR
) &outsym
, (bfd_size_type
) EXTERNAL_NLIST_SIZE
,
4038 (bfd_size_type
) 1, output_bfd
) != EXTERNAL_NLIST_SIZE
)
4040 /* FIXME: No way to handle errors. */
4044 finfo
->symoff
+= EXTERNAL_NLIST_SIZE
;
4045 h
->indx
= obj_aout_external_sym_count (output_bfd
);
4046 ++obj_aout_external_sym_count (output_bfd
);
4051 /* Link an a.out section into the output file. */
4054 aout_link_input_section (finfo
, input_bfd
, input_section
, reloff_ptr
,
4055 rel_size
, symbol_map
)
4056 struct aout_final_link_info
*finfo
;
4058 asection
*input_section
;
4059 file_ptr
*reloff_ptr
;
4060 bfd_size_type rel_size
;
4063 bfd_size_type input_size
;
4064 bfd_byte
*contents
= NULL
;
4067 /* Get the section contents. */
4068 input_size
= bfd_section_size (input_bfd
, input_section
);
4069 contents
= (bfd_byte
*) malloc (input_size
);
4070 if (contents
== NULL
&& input_size
!= 0)
4072 bfd_set_error (bfd_error_no_memory
);
4075 if (! bfd_get_section_contents (input_bfd
, input_section
, (PTR
) contents
,
4076 (file_ptr
) 0, input_size
))
4079 /* Read in the relocs. */
4080 relocs
= (PTR
) malloc (rel_size
);
4081 if (relocs
== NULL
&& rel_size
!= 0)
4083 bfd_set_error (bfd_error_no_memory
);
4086 if (bfd_seek (input_bfd
, input_section
->rel_filepos
, SEEK_SET
) != 0
4087 || bfd_read (relocs
, 1, rel_size
, input_bfd
) != rel_size
)
4090 /* Relocate the section contents. */
4091 if (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
)
4093 if (! aout_link_input_section_std (finfo
, input_bfd
, input_section
,
4094 (struct reloc_std_external
*) relocs
,
4095 rel_size
, contents
, symbol_map
))
4100 if (! aout_link_input_section_ext (finfo
, input_bfd
, input_section
,
4101 (struct reloc_ext_external
*) relocs
,
4102 rel_size
, contents
, symbol_map
))
4106 /* Write out the section contents. */
4107 if (! bfd_set_section_contents (finfo
->output_bfd
,
4108 input_section
->output_section
,
4110 input_section
->output_offset
,
4114 /* If we are producing relocateable output, the relocs were
4115 modified, and we now write them out. */
4116 if (finfo
->info
->relocateable
)
4118 if (bfd_seek (finfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0)
4120 if (bfd_write (relocs
, (bfd_size_type
) 1, rel_size
, finfo
->output_bfd
)
4123 *reloff_ptr
+= rel_size
;
4125 /* Assert that the relocs have not run into the symbols, and
4126 that if these are the text relocs they have not run into the
4128 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (finfo
->output_bfd
)
4129 && (reloff_ptr
!= &finfo
->treloff
4131 <= obj_datasec (finfo
->output_bfd
)->rel_filepos
)));
4136 if (contents
!= NULL
)
4142 if (contents
!= NULL
)
4147 /* Get the section corresponding to a reloc index. */
4149 static INLINE asection
*
4150 aout_reloc_index_to_section (abfd
, indx
)
4154 switch (indx
& N_TYPE
)
4157 return obj_textsec (abfd
);
4159 return obj_datasec (abfd
);
4161 return obj_bsssec (abfd
);
4164 return &bfd_abs_section
;
4170 /* Relocate an a.out section using standard a.out relocs. */
4173 aout_link_input_section_std (finfo
, input_bfd
, input_section
, relocs
,
4174 rel_size
, contents
, symbol_map
)
4175 struct aout_final_link_info
*finfo
;
4177 asection
*input_section
;
4178 struct reloc_std_external
*relocs
;
4179 bfd_size_type rel_size
;
4184 boolean relocateable
;
4185 struct external_nlist
*syms
;
4187 struct aout_link_hash_entry
**sym_hashes
;
4188 bfd_size_type reloc_count
;
4189 register struct reloc_std_external
*rel
;
4190 struct reloc_std_external
*rel_end
;
4192 output_bfd
= finfo
->output_bfd
;
4194 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
);
4195 BFD_ASSERT (input_bfd
->xvec
->header_byteorder_big_p
4196 == output_bfd
->xvec
->header_byteorder_big_p
);
4198 relocateable
= finfo
->info
->relocateable
;
4199 syms
= obj_aout_external_syms (input_bfd
);
4200 strings
= obj_aout_external_strings (input_bfd
);
4201 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4203 reloc_count
= rel_size
/ RELOC_STD_SIZE
;
4205 rel_end
= rel
+ reloc_count
;
4206 for (; rel
< rel_end
; rel
++)
4218 bfd_reloc_status_type r
;
4220 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4222 if (input_bfd
->xvec
->header_byteorder_big_p
)
4224 r_index
= ((rel
->r_index
[0] << 16)
4225 | (rel
->r_index
[1] << 8)
4227 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
4228 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
4229 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
4230 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
4231 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
4232 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
4233 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
4237 r_index
= ((rel
->r_index
[2] << 16)
4238 | (rel
->r_index
[1] << 8)
4240 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
4241 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
4242 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
4243 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
4244 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
4245 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
4246 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
4249 howto_idx
= r_length
+ 4 * r_pcrel
+ 8 * r_baserel
;
4250 BFD_ASSERT (howto_idx
< TABLE_SIZE (howto_table_std
));
4251 BFD_ASSERT (r_jmptable
== 0);
4252 BFD_ASSERT (r_relative
== 0);
4256 /* We are generating a relocateable output file, and must
4257 modify the reloc accordingly. */
4260 struct aout_link_hash_entry
*h
;
4262 /* If we know the symbol this relocation is against,
4263 convert it into a relocation against a section. This
4264 is what the native linker does. */
4265 h
= sym_hashes
[r_index
];
4266 if (h
!= (struct aout_link_hash_entry
*) NULL
4267 && h
->root
.type
== bfd_link_hash_defined
)
4269 asection
*output_section
;
4271 /* Change the r_extern value. */
4272 if (output_bfd
->xvec
->header_byteorder_big_p
)
4273 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_BIG
;
4275 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE
;
4277 /* Compute a new r_index. */
4278 output_section
= h
->root
.u
.def
.section
->output_section
;
4279 if (output_section
== obj_textsec (output_bfd
))
4281 else if (output_section
== obj_datasec (output_bfd
))
4283 else if (output_section
== obj_bsssec (output_bfd
))
4288 /* Add the symbol value and the section VMA to the
4289 addend stored in the contents. */
4290 relocation
= (h
->root
.u
.def
.value
4291 + output_section
->vma
4292 + h
->root
.u
.def
.section
->output_offset
);
4296 /* We must change r_index according to the symbol
4298 r_index
= symbol_map
[r_index
];
4304 name
= strings
+ GET_WORD (input_bfd
,
4305 syms
[r_index
].e_strx
);
4306 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4307 (finfo
->info
, name
, input_bfd
, input_section
,
4316 /* Write out the new r_index value. */
4317 if (output_bfd
->xvec
->header_byteorder_big_p
)
4319 rel
->r_index
[0] = r_index
>> 16;
4320 rel
->r_index
[1] = r_index
>> 8;
4321 rel
->r_index
[2] = r_index
;
4325 rel
->r_index
[2] = r_index
>> 16;
4326 rel
->r_index
[1] = r_index
>> 8;
4327 rel
->r_index
[0] = r_index
;
4334 /* This is a relocation against a section. We must
4335 adjust by the amount that the section moved. */
4336 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4337 relocation
= (section
->output_section
->vma
4338 + section
->output_offset
4342 /* Change the address of the relocation. */
4343 PUT_WORD (output_bfd
,
4344 r_addr
+ input_section
->output_offset
,
4347 /* Adjust a PC relative relocation by removing the reference
4348 to the original address in the section and including the
4349 reference to the new address. */
4351 relocation
-= (input_section
->output_section
->vma
4352 + input_section
->output_offset
4353 - input_section
->vma
);
4355 if (relocation
== 0)
4358 r
= _bfd_relocate_contents (howto_table_std
+ howto_idx
,
4359 input_bfd
, relocation
,
4364 /* We are generating an executable, and must do a full
4368 struct aout_link_hash_entry
*h
;
4370 h
= sym_hashes
[r_index
];
4371 if (h
!= (struct aout_link_hash_entry
*) NULL
4372 && h
->root
.type
== bfd_link_hash_defined
)
4374 relocation
= (h
->root
.u
.def
.value
4375 + h
->root
.u
.def
.section
->output_section
->vma
4376 + h
->root
.u
.def
.section
->output_offset
);
4382 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4383 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4384 (finfo
->info
, name
, input_bfd
, input_section
,
4394 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4395 relocation
= (section
->output_section
->vma
4396 + section
->output_offset
4399 relocation
+= input_section
->vma
;
4402 r
= _bfd_final_link_relocate (howto_table_std
+ howto_idx
,
4403 input_bfd
, input_section
,
4404 contents
, r_addr
, relocation
,
4408 if (r
!= bfd_reloc_ok
)
4413 case bfd_reloc_outofrange
:
4415 case bfd_reloc_overflow
:
4420 name
= strings
+ GET_WORD (input_bfd
,
4421 syms
[r_index
].e_strx
);
4426 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4427 name
= bfd_section_name (input_bfd
, s
);
4429 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4430 (finfo
->info
, name
, howto_table_std
[howto_idx
].name
,
4431 (bfd_vma
) 0, input_bfd
, input_section
, r_addr
)))
4442 /* Relocate an a.out section using extended a.out relocs. */
4445 aout_link_input_section_ext (finfo
, input_bfd
, input_section
, relocs
,
4446 rel_size
, contents
, symbol_map
)
4447 struct aout_final_link_info
*finfo
;
4449 asection
*input_section
;
4450 struct reloc_ext_external
*relocs
;
4451 bfd_size_type rel_size
;
4456 boolean relocateable
;
4457 struct external_nlist
*syms
;
4459 struct aout_link_hash_entry
**sym_hashes
;
4460 bfd_size_type reloc_count
;
4461 register struct reloc_ext_external
*rel
;
4462 struct reloc_ext_external
*rel_end
;
4464 output_bfd
= finfo
->output_bfd
;
4466 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_EXT_SIZE
);
4467 BFD_ASSERT (input_bfd
->xvec
->header_byteorder_big_p
4468 == output_bfd
->xvec
->header_byteorder_big_p
);
4470 relocateable
= finfo
->info
->relocateable
;
4471 syms
= obj_aout_external_syms (input_bfd
);
4472 strings
= obj_aout_external_strings (input_bfd
);
4473 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4475 reloc_count
= rel_size
/ RELOC_EXT_SIZE
;
4477 rel_end
= rel
+ reloc_count
;
4478 for (; rel
< rel_end
; rel
++)
4487 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4489 if (input_bfd
->xvec
->header_byteorder_big_p
)
4491 r_index
= ((rel
->r_index
[0] << 16)
4492 | (rel
->r_index
[1] << 8)
4494 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
4495 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
4496 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
4500 r_index
= ((rel
->r_index
[2] << 16)
4501 | (rel
->r_index
[1] << 8)
4503 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
4504 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
4505 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
4508 r_addend
= GET_SWORD (input_bfd
, rel
->r_addend
);
4510 BFD_ASSERT (r_type
>= 0
4511 && r_type
< TABLE_SIZE (howto_table_ext
));
4515 /* We are generating a relocateable output file, and must
4516 modify the reloc accordingly. */
4519 struct aout_link_hash_entry
*h
;
4521 /* If we know the symbol this relocation is against,
4522 convert it into a relocation against a section. This
4523 is what the native linker does. */
4524 h
= sym_hashes
[r_index
];
4525 if (h
!= (struct aout_link_hash_entry
*) NULL
4526 && h
->root
.type
== bfd_link_hash_defined
)
4528 asection
*output_section
;
4530 /* Change the r_extern value. */
4531 if (output_bfd
->xvec
->header_byteorder_big_p
)
4532 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_BIG
;
4534 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE
;
4536 /* Compute a new r_index. */
4537 output_section
= h
->root
.u
.def
.section
->output_section
;
4538 if (output_section
== obj_textsec (output_bfd
))
4540 else if (output_section
== obj_datasec (output_bfd
))
4542 else if (output_section
== obj_bsssec (output_bfd
))
4547 /* Add the symbol value and the section VMA to the
4549 relocation
= (h
->root
.u
.def
.value
4550 + output_section
->vma
4551 + h
->root
.u
.def
.section
->output_offset
);
4553 /* Now RELOCATION is the VMA of the final
4554 destination. If this is a PC relative reloc,
4555 then ADDEND is the negative of the source VMA.
4556 We want to set ADDEND to the difference between
4557 the destination VMA and the source VMA, which
4558 means we must adjust RELOCATION by the change in
4559 the source VMA. This is done below. */
4563 /* We must change r_index according to the symbol
4565 r_index
= symbol_map
[r_index
];
4572 + GET_WORD (input_bfd
, syms
[r_index
].e_strx
));
4573 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4574 (finfo
->info
, name
, input_bfd
, input_section
,
4582 /* If this is a PC relative reloc, then the addend
4583 is the negative of the source VMA. We must
4584 adjust it by the change in the source VMA. This
4588 /* Write out the new r_index value. */
4589 if (output_bfd
->xvec
->header_byteorder_big_p
)
4591 rel
->r_index
[0] = r_index
>> 16;
4592 rel
->r_index
[1] = r_index
>> 8;
4593 rel
->r_index
[2] = r_index
;
4597 rel
->r_index
[2] = r_index
>> 16;
4598 rel
->r_index
[1] = r_index
>> 8;
4599 rel
->r_index
[0] = r_index
;
4606 /* This is a relocation against a section. We must
4607 adjust by the amount that the section moved. */
4608 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4609 relocation
= (section
->output_section
->vma
4610 + section
->output_offset
4613 /* If this is a PC relative reloc, then the addend is
4614 the difference in VMA between the destination and the
4615 source. We have just adjusted for the change in VMA
4616 of the destination, so we must also adjust by the
4617 change in VMA of the source. This is done below. */
4620 /* As described above, we must always adjust a PC relative
4621 reloc by the change in VMA of the source. */
4622 if (howto_table_ext
[r_type
].pc_relative
)
4623 relocation
-= (input_section
->output_section
->vma
4624 + input_section
->output_offset
4625 - input_section
->vma
);
4627 /* Change the addend if necessary. */
4628 if (relocation
!= 0)
4629 PUT_WORD (output_bfd
, r_addend
+ relocation
, rel
->r_addend
);
4631 /* Change the address of the relocation. */
4632 PUT_WORD (output_bfd
,
4633 r_addr
+ input_section
->output_offset
,
4638 bfd_reloc_status_type r
;
4640 /* We are generating an executable, and must do a full
4644 struct aout_link_hash_entry
*h
;
4646 h
= sym_hashes
[r_index
];
4647 if (h
!= (struct aout_link_hash_entry
*) NULL
4648 && h
->root
.type
== bfd_link_hash_defined
)
4650 relocation
= (h
->root
.u
.def
.value
4651 + h
->root
.u
.def
.section
->output_section
->vma
4652 + h
->root
.u
.def
.section
->output_offset
);
4658 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4659 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4660 (finfo
->info
, name
, input_bfd
, input_section
,
4670 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4672 /* If this is a PC relative reloc, then R_ADDEND is the
4673 difference between the two vmas, or
4674 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4676 old_dest_sec == section->vma
4678 old_src_sec == input_section->vma
4680 old_src_off == r_addr
4682 _bfd_final_link_relocate expects RELOCATION +
4683 R_ADDEND to be the VMA of the destination minus
4684 r_addr (the minus r_addr is because this relocation
4685 is not pcrel_offset, which is a bit confusing and
4686 should, perhaps, be changed), or
4689 new_dest_sec == output_section->vma + output_offset
4690 We arrange for this to happen by setting RELOCATION to
4691 new_dest_sec + old_src_sec - old_dest_sec
4693 If this is not a PC relative reloc, then R_ADDEND is
4694 simply the VMA of the destination, so we set
4695 RELOCATION to the change in the destination VMA, or
4696 new_dest_sec - old_dest_sec
4698 relocation
= (section
->output_section
->vma
4699 + section
->output_offset
4701 if (howto_table_ext
[r_type
].pc_relative
)
4702 relocation
+= input_section
->vma
;
4705 r
= _bfd_final_link_relocate (howto_table_ext
+ r_type
,
4706 input_bfd
, input_section
,
4707 contents
, r_addr
, relocation
,
4709 if (r
!= bfd_reloc_ok
)
4714 case bfd_reloc_outofrange
:
4716 case bfd_reloc_overflow
:
4721 name
= strings
+ GET_WORD (input_bfd
,
4722 syms
[r_index
].e_strx
);
4727 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4728 name
= bfd_section_name (input_bfd
, s
);
4730 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4731 (finfo
->info
, name
, howto_table_ext
[r_type
].name
,
4732 r_addend
, input_bfd
, input_section
, r_addr
)))
4744 /* Handle a link order which is supposed to generate a reloc. */
4747 aout_link_reloc_link_order (finfo
, o
, p
)
4748 struct aout_final_link_info
*finfo
;
4750 struct bfd_link_order
*p
;
4752 struct bfd_link_order_reloc
*pr
;
4755 const reloc_howto_type
*howto
;
4756 file_ptr
*reloff_ptr
;
4757 struct reloc_std_external srel
;
4758 struct reloc_ext_external erel
;
4763 if (p
->type
== bfd_section_reloc_link_order
)
4766 if (pr
->u
.section
== &bfd_abs_section
)
4767 r_index
= N_ABS
| N_EXT
;
4770 BFD_ASSERT (pr
->u
.section
->owner
== finfo
->output_bfd
);
4771 r_index
= pr
->u
.section
->target_index
;
4776 struct aout_link_hash_entry
*h
;
4778 BFD_ASSERT (p
->type
== bfd_symbol_reloc_link_order
);
4780 h
= aout_link_hash_lookup (aout_hash_table (finfo
->info
),
4781 pr
->u
.name
, false, false, true);
4782 if (h
!= (struct aout_link_hash_entry
*) NULL
4787 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4788 (finfo
->info
, pr
->u
.name
, (bfd
*) NULL
,
4789 (asection
*) NULL
, (bfd_vma
) 0)))
4795 howto
= bfd_reloc_type_lookup (finfo
->output_bfd
, pr
->reloc
);
4796 if (howto
== (const reloc_howto_type
*) NULL
)
4798 bfd_set_error (bfd_error_bad_value
);
4802 if (o
== obj_textsec (finfo
->output_bfd
))
4803 reloff_ptr
= &finfo
->treloff
;
4804 else if (o
== obj_datasec (finfo
->output_bfd
))
4805 reloff_ptr
= &finfo
->dreloff
;
4809 if (obj_reloc_entry_size (finfo
->output_bfd
) == RELOC_STD_SIZE
)
4817 r_pcrel
= howto
->pc_relative
;
4818 r_baserel
= (howto
->type
& 8) != 0;
4821 r_length
= howto
->size
;
4823 PUT_WORD (finfo
->output_bfd
, p
->offset
, srel
.r_address
);
4824 if (finfo
->output_bfd
->xvec
->header_byteorder_big_p
)
4826 srel
.r_index
[0] = r_index
>> 16;
4827 srel
.r_index
[1] = r_index
>> 8;
4828 srel
.r_index
[2] = r_index
;
4830 ((r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
4831 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
4832 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
4833 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
4834 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
4835 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
4839 srel
.r_index
[2] = r_index
>> 16;
4840 srel
.r_index
[1] = r_index
>> 8;
4841 srel
.r_index
[0] = r_index
;
4843 ((r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
4844 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
4845 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
4846 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
4847 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
4848 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
4851 rel_ptr
= (PTR
) &srel
;
4853 /* We have to write the addend into the object file, since
4854 standard a.out relocs are in place. It would be more
4855 reliable if we had the current contents of the file here,
4856 rather than assuming zeroes, but we can't read the file since
4857 it was opened using bfd_openw. */
4858 if (pr
->addend
!= 0)
4861 bfd_reloc_status_type r
;
4865 size
= bfd_get_reloc_size (howto
);
4866 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4867 if (buf
== (bfd_byte
*) NULL
)
4869 bfd_set_error (bfd_error_no_memory
);
4872 r
= _bfd_relocate_contents (howto
, finfo
->output_bfd
,
4879 case bfd_reloc_outofrange
:
4881 case bfd_reloc_overflow
:
4882 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4884 (p
->type
== bfd_section_reloc_link_order
4885 ? bfd_section_name (finfo
->output_bfd
,
4888 howto
->name
, pr
->addend
, (bfd
*) NULL
,
4889 (asection
*) NULL
, (bfd_vma
) 0)))
4896 ok
= bfd_set_section_contents (finfo
->output_bfd
, o
,
4898 (file_ptr
) p
->offset
,
4907 PUT_WORD (finfo
->output_bfd
, p
->offset
, erel
.r_address
);
4909 if (finfo
->output_bfd
->xvec
->header_byteorder_big_p
)
4911 erel
.r_index
[0] = r_index
>> 16;
4912 erel
.r_index
[1] = r_index
>> 8;
4913 erel
.r_index
[2] = r_index
;
4915 ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
4916 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
4920 erel
.r_index
[2] = r_index
>> 16;
4921 erel
.r_index
[1] = r_index
>> 8;
4922 erel
.r_index
[0] = r_index
;
4924 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
4925 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
4928 PUT_WORD (finfo
->output_bfd
, pr
->addend
, erel
.r_addend
);
4930 rel_ptr
= (PTR
) &erel
;
4933 if (bfd_seek (finfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0
4934 || (bfd_write (rel_ptr
, (bfd_size_type
) 1,
4935 obj_reloc_entry_size (finfo
->output_bfd
),
4937 != obj_reloc_entry_size (finfo
->output_bfd
)))
4940 *reloff_ptr
+= obj_reloc_entry_size (finfo
->output_bfd
);
4942 /* Assert that the relocs have not run into the symbols, and that n
4943 the text relocs have not run into the data relocs. */
4944 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (finfo
->output_bfd
)
4945 && (reloff_ptr
!= &finfo
->treloff
4947 <= obj_datasec (finfo
->output_bfd
)->rel_filepos
)));