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 aout_get_external_symbols
PARAMS ((bfd
*));
143 The file @file{aoutx.h} provides for both the @emph{standard}
144 and @emph{extended} forms of a.out relocation records.
146 The standard records contain only an
147 address, a symbol index, and a type field. The extended records
148 (used on 29ks and sparcs) also have a full integer for an
152 #define CTOR_TABLE_RELOC_IDX 2
154 #define howto_table_ext NAME(aout,ext_howto_table)
155 #define howto_table_std NAME(aout,std_howto_table)
157 reloc_howto_type howto_table_ext
[] =
159 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
160 HOWTO(RELOC_8
, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", false, 0,0x000000ff, false),
161 HOWTO(RELOC_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", false, 0,0x0000ffff, false),
162 HOWTO(RELOC_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", false, 0,0xffffffff, false),
163 HOWTO(RELOC_DISP8
, 0, 0, 8, true, 0, complain_overflow_signed
,0,"DISP8", false, 0,0x000000ff, false),
164 HOWTO(RELOC_DISP16
, 0, 1, 16, true, 0, complain_overflow_signed
,0,"DISP16", false, 0,0x0000ffff, false),
165 HOWTO(RELOC_DISP32
, 0, 2, 32, true, 0, complain_overflow_signed
,0,"DISP32", false, 0,0xffffffff, false),
166 HOWTO(RELOC_WDISP30
,2, 2, 30, true, 0, complain_overflow_signed
,0,"WDISP30", false, 0,0x3fffffff, false),
167 HOWTO(RELOC_WDISP22
,2, 2, 22, true, 0, complain_overflow_signed
,0,"WDISP22", false, 0,0x003fffff, false),
168 HOWTO(RELOC_HI22
, 10, 2, 22, false, 0, complain_overflow_bitfield
,0,"HI22", false, 0,0x003fffff, false),
169 HOWTO(RELOC_22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"22", false, 0,0x003fffff, false),
170 HOWTO(RELOC_13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"13", false, 0,0x00001fff, false),
171 HOWTO(RELOC_LO10
, 0, 2, 10, false, 0, complain_overflow_dont
,0,"LO10", false, 0,0x000003ff, false),
172 HOWTO(RELOC_SFA_BASE
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_BASE", false, 0,0xffffffff, false),
173 HOWTO(RELOC_SFA_OFF13
,0,2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_OFF13",false, 0,0xffffffff, false),
174 HOWTO(RELOC_BASE10
, 0, 2, 16, false, 0, complain_overflow_bitfield
,0,"BASE10", false, 0,0x0000ffff, false),
175 HOWTO(RELOC_BASE13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"BASE13", false, 0,0x00001fff, false),
176 HOWTO(RELOC_BASE22
, 0, 2, 0, false, 0, complain_overflow_bitfield
,0,"BASE22", false, 0,0x00000000, false),
177 HOWTO(RELOC_PC10
, 0, 2, 10, false, 0, complain_overflow_bitfield
,0,"PC10", false, 0,0x000003ff, false),
178 HOWTO(RELOC_PC22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"PC22", false, 0,0x003fffff, false),
179 HOWTO(RELOC_JMP_TBL
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"JMP_TBL", false, 0,0xffffffff, false),
180 HOWTO(RELOC_SEGOFF16
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"SEGOFF16", false, 0,0x00000000, false),
181 HOWTO(RELOC_GLOB_DAT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"GLOB_DAT", false, 0,0x00000000, false),
182 HOWTO(RELOC_JMP_SLOT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"JMP_SLOT", false, 0,0x00000000, false),
183 HOWTO(RELOC_RELATIVE
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"RELATIVE", false, 0,0x00000000, false),
186 /* Convert standard reloc records to "arelent" format (incl byte swap). */
188 reloc_howto_type howto_table_std
[] = {
189 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
190 HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", true, 0x000000ff,0x000000ff, false),
191 HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", true, 0x0000ffff,0x0000ffff, false),
192 HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", true, 0xffffffff,0xffffffff, false),
193 HOWTO( 3, 0, 4, 64, false, 0, complain_overflow_bitfield
,0,"64", true, 0xdeaddead,0xdeaddead, false),
194 HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed
, 0,"DISP8", true, 0x000000ff,0x000000ff, false),
195 HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed
, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
196 HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed
, 0,"DISP32", true, 0xffffffff,0xffffffff, false),
197 HOWTO( 7, 0, 4, 64, true, 0, complain_overflow_signed
, 0,"DISP64", true, 0xfeedface,0xfeedface, false),
199 HOWTO( 9, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"BASE16", false,0xffffffff,0xffffffff, false),
200 HOWTO(10, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"BASE32", false,0xffffffff,0xffffffff, false),
203 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
205 CONST
struct reloc_howto_struct
*
206 NAME(aout
,reloc_type_lookup
) (abfd
,code
)
208 bfd_reloc_code_real_type code
;
210 #define EXT(i,j) case i: return &howto_table_ext[j]
211 #define STD(i,j) case i: return &howto_table_std[j]
212 int ext
= obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
;
213 if (code
== BFD_RELOC_CTOR
)
214 switch (bfd_get_arch_info (abfd
)->bits_per_address
)
226 EXT (BFD_RELOC_32
, 2);
227 EXT (BFD_RELOC_HI22
, 8);
228 EXT (BFD_RELOC_LO10
, 11);
229 EXT (BFD_RELOC_32_PCREL_S2
, 6);
230 EXT (BFD_RELOC_SPARC_WDISP22
, 7);
231 EXT (BFD_RELOC_SPARC13
, 10);
232 EXT (BFD_RELOC_SPARC_BASE13
, 15);
233 default: return (CONST
struct reloc_howto_struct
*) 0;
239 STD (BFD_RELOC_16
, 1);
240 STD (BFD_RELOC_32
, 2);
241 STD (BFD_RELOC_8_PCREL
, 4);
242 STD (BFD_RELOC_16_PCREL
, 5);
243 STD (BFD_RELOC_32_PCREL
, 6);
244 STD (BFD_RELOC_16_BASEREL
, 9);
245 STD (BFD_RELOC_32_BASEREL
, 10);
246 default: return (CONST
struct reloc_howto_struct
*) 0;
252 Internal entry points
255 @file{aoutx.h} exports several routines for accessing the
256 contents of an a.out file, which are gathered and exported in
257 turn by various format specific files (eg sunos.c).
263 aout_@var{size}_swap_exec_header_in
266 void aout_@var{size}_swap_exec_header_in,
268 struct external_exec *raw_bytes,
269 struct internal_exec *execp);
272 Swap the information in an executable header @var{raw_bytes} taken
273 from a raw byte stream memory image into the internal exec header
274 structure @var{execp}.
277 #ifndef NAME_swap_exec_header_in
279 NAME(aout
,swap_exec_header_in
) (abfd
, raw_bytes
, execp
)
281 struct external_exec
*raw_bytes
;
282 struct internal_exec
*execp
;
284 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
286 /* The internal_exec structure has some fields that are unused in this
287 configuration (IE for i960), so ensure that all such uninitialized
288 fields are zero'd out. There are places where two of these structs
289 are memcmp'd, and thus the contents do matter. */
290 memset ((PTR
) execp
, 0, sizeof (struct internal_exec
));
291 /* Now fill in fields in the execp, from the bytes in the raw data. */
292 execp
->a_info
= bfd_h_get_32 (abfd
, bytes
->e_info
);
293 execp
->a_text
= GET_WORD (abfd
, bytes
->e_text
);
294 execp
->a_data
= GET_WORD (abfd
, bytes
->e_data
);
295 execp
->a_bss
= GET_WORD (abfd
, bytes
->e_bss
);
296 execp
->a_syms
= GET_WORD (abfd
, bytes
->e_syms
);
297 execp
->a_entry
= GET_WORD (abfd
, bytes
->e_entry
);
298 execp
->a_trsize
= GET_WORD (abfd
, bytes
->e_trsize
);
299 execp
->a_drsize
= GET_WORD (abfd
, bytes
->e_drsize
);
301 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
306 aout_@var{size}_swap_exec_header_out
309 void aout_@var{size}_swap_exec_header_out
311 struct internal_exec *execp,
312 struct external_exec *raw_bytes);
315 Swap the information in an internal exec header structure
316 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
319 NAME(aout
,swap_exec_header_out
) (abfd
, execp
, raw_bytes
)
321 struct internal_exec
*execp
;
322 struct external_exec
*raw_bytes
;
324 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
326 /* Now fill in fields in the raw data, from the fields in the exec struct. */
327 bfd_h_put_32 (abfd
, execp
->a_info
, bytes
->e_info
);
328 PUT_WORD (abfd
, execp
->a_text
, bytes
->e_text
);
329 PUT_WORD (abfd
, execp
->a_data
, bytes
->e_data
);
330 PUT_WORD (abfd
, execp
->a_bss
, bytes
->e_bss
);
331 PUT_WORD (abfd
, execp
->a_syms
, bytes
->e_syms
);
332 PUT_WORD (abfd
, execp
->a_entry
, bytes
->e_entry
);
333 PUT_WORD (abfd
, execp
->a_trsize
, bytes
->e_trsize
);
334 PUT_WORD (abfd
, execp
->a_drsize
, bytes
->e_drsize
);
337 /* Make all the section for an a.out file. */
340 NAME(aout
,make_sections
) (abfd
)
343 if (obj_textsec (abfd
) == (asection
*) NULL
344 && bfd_make_section (abfd
, ".text") == (asection
*) NULL
)
346 if (obj_datasec (abfd
) == (asection
*) NULL
347 && bfd_make_section (abfd
, ".data") == (asection
*) NULL
)
349 if (obj_bsssec (abfd
) == (asection
*) NULL
350 && bfd_make_section (abfd
, ".bss") == (asection
*) NULL
)
357 aout_@var{size}_some_aout_object_p
360 bfd_target *aout_@var{size}_some_aout_object_p
362 bfd_target *(*callback_to_real_object_p)());
365 Some a.out variant thinks that the file open in @var{abfd}
366 checking is an a.out file. Do some more checking, and set up
367 for access if it really is. Call back to the calling
368 environment's "finish up" function just before returning, to
369 handle any last-minute setup.
373 NAME(aout
,some_aout_object_p
) (abfd
, execp
, callback_to_real_object_p
)
375 struct internal_exec
*execp
;
376 bfd_target
*(*callback_to_real_object_p
) PARAMS ((bfd
*));
378 struct aout_data_struct
*rawptr
, *oldrawptr
;
381 rawptr
= (struct aout_data_struct
*) bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
382 if (rawptr
== NULL
) {
383 bfd_set_error (bfd_error_no_memory
);
387 oldrawptr
= abfd
->tdata
.aout_data
;
388 abfd
->tdata
.aout_data
= rawptr
;
390 /* Copy the contents of the old tdata struct.
391 In particular, we want the subformat, since for hpux it was set in
392 hp300hpux.c:swap_exec_header_in and will be used in
393 hp300hpux.c:callback. */
394 if (oldrawptr
!= NULL
)
395 *abfd
->tdata
.aout_data
= *oldrawptr
;
397 abfd
->tdata
.aout_data
->a
.hdr
= &rawptr
->e
;
398 *(abfd
->tdata
.aout_data
->a
.hdr
) = *execp
; /* Copy in the internal_exec struct */
399 execp
= abfd
->tdata
.aout_data
->a
.hdr
;
401 /* Set the file flags */
402 abfd
->flags
= NO_FLAGS
;
403 if (execp
->a_drsize
|| execp
->a_trsize
)
404 abfd
->flags
|= HAS_RELOC
;
405 /* Setting of EXEC_P has been deferred to the bottom of this function */
407 abfd
->flags
|= HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
;
408 if (N_DYNAMIC(*execp
))
409 abfd
->flags
|= DYNAMIC
;
411 if (N_MAGIC (*execp
) == ZMAGIC
)
413 abfd
->flags
|= D_PAGED
|WP_TEXT
;
414 adata(abfd
).magic
= z_magic
;
416 else if (N_MAGIC (*execp
) == NMAGIC
)
418 abfd
->flags
|= WP_TEXT
;
419 adata(abfd
).magic
= n_magic
;
422 adata(abfd
).magic
= o_magic
;
424 bfd_get_start_address (abfd
) = execp
->a_entry
;
426 obj_aout_symbols (abfd
) = (aout_symbol_type
*)NULL
;
427 bfd_get_symcount (abfd
) = execp
->a_syms
/ sizeof (struct external_nlist
);
429 /* The default relocation entry size is that of traditional V7 Unix. */
430 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
432 /* The default symbol entry size is that of traditional Unix. */
433 obj_symbol_entry_size (abfd
) = EXTERNAL_NLIST_SIZE
;
435 obj_aout_external_syms (abfd
) = NULL
;
436 obj_aout_external_strings (abfd
) = NULL
;
437 obj_aout_sym_hashes (abfd
) = NULL
;
439 if (! NAME(aout
,make_sections
) (abfd
))
442 obj_datasec (abfd
)->_raw_size
= execp
->a_data
;
443 obj_bsssec (abfd
)->_raw_size
= execp
->a_bss
;
445 /* If this object is dynamically linked, we assume that both
446 sections have relocs. This does no real harm, even though it may
448 obj_textsec (abfd
)->flags
=
449 (execp
->a_trsize
!= 0 || (abfd
->flags
& DYNAMIC
) != 0
450 ? (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
| SEC_RELOC
)
451 : (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
));
452 obj_datasec (abfd
)->flags
=
453 (execp
->a_drsize
!= 0 || (abfd
->flags
& DYNAMIC
) != 0
454 ? (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_RELOC
)
455 : (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
));
456 obj_bsssec (abfd
)->flags
= SEC_ALLOC
;
458 #ifdef THIS_IS_ONLY_DOCUMENTATION
459 /* The common code can't fill in these things because they depend
460 on either the start address of the text segment, the rounding
461 up of virtual addresses between segments, or the starting file
462 position of the text segment -- all of which varies among different
463 versions of a.out. */
465 /* Call back to the format-dependent code to fill in the rest of the
466 fields and do any further cleanup. Things that should be filled
467 in by the callback: */
469 struct exec
*execp
= exec_hdr (abfd
);
471 obj_textsec (abfd
)->size
= N_TXTSIZE(*execp
);
472 obj_textsec (abfd
)->raw_size
= N_TXTSIZE(*execp
);
473 /* data and bss are already filled in since they're so standard */
475 /* The virtual memory addresses of the sections */
476 obj_textsec (abfd
)->vma
= N_TXTADDR(*execp
);
477 obj_datasec (abfd
)->vma
= N_DATADDR(*execp
);
478 obj_bsssec (abfd
)->vma
= N_BSSADDR(*execp
);
480 /* The file offsets of the sections */
481 obj_textsec (abfd
)->filepos
= N_TXTOFF(*execp
);
482 obj_datasec (abfd
)->filepos
= N_DATOFF(*execp
);
484 /* The file offsets of the relocation info */
485 obj_textsec (abfd
)->rel_filepos
= N_TRELOFF(*execp
);
486 obj_datasec (abfd
)->rel_filepos
= N_DRELOFF(*execp
);
488 /* The file offsets of the string table and symbol table. */
489 obj_str_filepos (abfd
) = N_STROFF (*execp
);
490 obj_sym_filepos (abfd
) = N_SYMOFF (*execp
);
492 /* Determine the architecture and machine type of the object file. */
493 switch (N_MACHTYPE (*exec_hdr (abfd
))) {
495 abfd
->obj_arch
= bfd_arch_obscure
;
499 adata(abfd
)->page_size
= PAGE_SIZE
;
500 adata(abfd
)->segment_size
= SEGMENT_SIZE
;
501 adata(abfd
)->exec_bytes_size
= EXEC_BYTES_SIZE
;
505 /* The architecture is encoded in various ways in various a.out variants,
506 or is not encoded at all in some of them. The relocation size depends
507 on the architecture and the a.out variant. Finally, the return value
508 is the bfd_target vector in use. If an error occurs, return zero and
509 set bfd_error to the appropriate error code.
511 Formats such as b.out, which have additional fields in the a.out
512 header, should cope with them in this callback as well. */
513 #endif /* DOCUMENTATION */
515 result
= (*callback_to_real_object_p
)(abfd
);
517 /* Now that the segment addresses have been worked out, take a better
518 guess at whether the file is executable. If the entry point
519 is within the text segment, assume it is. (This makes files
520 executable even if their entry point address is 0, as long as
521 their text starts at zero.)
523 At some point we should probably break down and stat the file and
524 declare it executable if (one of) its 'x' bits are on... */
525 if ((execp
->a_entry
>= obj_textsec(abfd
)->vma
) &&
526 (execp
->a_entry
< obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
))
527 abfd
->flags
|= EXEC_P
;
530 #if 0 /* These should be set correctly anyways. */
531 abfd
->sections
= obj_textsec (abfd
);
532 obj_textsec (abfd
)->next
= obj_datasec (abfd
);
533 obj_datasec (abfd
)->next
= obj_bsssec (abfd
);
539 abfd
->tdata
.aout_data
= oldrawptr
;
546 aout_@var{size}_mkobject
549 boolean aout_@var{size}_mkobject, (bfd *abfd);
552 Initialize BFD @var{abfd} for use with a.out files.
556 NAME(aout
,mkobject
) (abfd
)
559 struct aout_data_struct
*rawptr
;
561 bfd_set_error (bfd_error_system_call
);
563 /* Use an intermediate variable for clarity */
564 rawptr
= (struct aout_data_struct
*)bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
566 if (rawptr
== NULL
) {
567 bfd_set_error (bfd_error_no_memory
);
571 abfd
->tdata
.aout_data
= rawptr
;
572 exec_hdr (abfd
) = &(rawptr
->e
);
574 obj_textsec (abfd
) = (asection
*)NULL
;
575 obj_datasec (abfd
) = (asection
*)NULL
;
576 obj_bsssec (abfd
) = (asection
*)NULL
;
584 aout_@var{size}_machine_type
587 enum machine_type aout_@var{size}_machine_type
588 (enum bfd_architecture arch,
589 unsigned long machine));
592 Keep track of machine architecture and machine type for
593 a.out's. Return the <<machine_type>> for a particular
594 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
595 and machine can't be represented in a.out format.
597 If the architecture is understood, machine type 0 (default)
598 is always understood.
602 NAME(aout
,machine_type
) (arch
, machine
)
603 enum bfd_architecture arch
;
604 unsigned long machine
;
606 enum machine_type arch_flags
;
608 arch_flags
= M_UNKNOWN
;
612 if (machine
== 0) arch_flags
= M_SPARC
;
617 case 0: arch_flags
= M_68010
; break;
618 case 68000: arch_flags
= M_UNKNOWN
; break;
619 case 68010: arch_flags
= M_68010
; break;
620 case 68020: arch_flags
= M_68020
; break;
621 default: arch_flags
= M_UNKNOWN
; break;
626 if (machine
== 0) arch_flags
= M_386
;
630 if (machine
== 0) arch_flags
= M_29K
;
637 case 3000: arch_flags
= M_MIPS1
; break;
640 case 6000: arch_flags
= M_MIPS2
; break;
641 default: arch_flags
= M_UNKNOWN
; break;
646 arch_flags
= M_UNKNOWN
;
654 aout_@var{size}_set_arch_mach
657 boolean aout_@var{size}_set_arch_mach,
659 enum bfd_architecture arch,
660 unsigned long machine));
663 Set the architecture and the machine of the BFD @var{abfd} to the
664 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
665 can support the architecture required.
669 NAME(aout
,set_arch_mach
) (abfd
, arch
, machine
)
671 enum bfd_architecture arch
;
672 unsigned long machine
;
674 if (! bfd_default_set_arch_mach (abfd
, arch
, machine
))
677 if (arch
!= bfd_arch_unknown
&&
678 NAME(aout
,machine_type
) (arch
, machine
) == M_UNKNOWN
)
679 return false; /* We can't represent this type */
681 /* Determine the size of a relocation entry */
686 obj_reloc_entry_size (abfd
) = RELOC_EXT_SIZE
;
689 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
693 return (*aout_backend_info(abfd
)->set_sizes
) (abfd
);
697 adjust_o_magic (abfd
, execp
)
699 struct internal_exec
*execp
;
701 file_ptr pos
= adata (abfd
).exec_bytes_size
;
706 obj_textsec(abfd
)->filepos
= pos
;
707 pos
+= obj_textsec(abfd
)->_raw_size
;
708 vma
+= obj_textsec(abfd
)->_raw_size
;
711 if (!obj_datasec(abfd
)->user_set_vma
)
713 #if 0 /* ?? Does alignment in the file image really matter? */
714 pad
= align_power (vma
, obj_datasec(abfd
)->alignment_power
) - vma
;
716 obj_textsec(abfd
)->_raw_size
+= pad
;
719 obj_datasec(abfd
)->vma
= vma
;
721 obj_datasec(abfd
)->filepos
= pos
;
722 pos
+= obj_datasec(abfd
)->_raw_size
;
723 vma
+= obj_datasec(abfd
)->_raw_size
;
726 if (!obj_bsssec(abfd
)->user_set_vma
)
729 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
731 obj_datasec(abfd
)->_raw_size
+= pad
;
734 obj_bsssec(abfd
)->vma
= vma
;
736 obj_bsssec(abfd
)->filepos
= pos
;
738 /* Fix up the exec header. */
739 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
740 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
741 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
742 N_SET_MAGIC (*execp
, OMAGIC
);
746 adjust_z_magic (abfd
, execp
)
748 struct internal_exec
*execp
;
750 bfd_size_type data_pad
, text_pad
;
752 CONST
struct aout_backend_data
*abdp
;
753 int ztih
; /* Nonzero if text includes exec header. */
755 abdp
= aout_backend_info (abfd
);
758 ztih
= abdp
&& abdp
->text_includes_header
;
759 obj_textsec(abfd
)->filepos
= (ztih
760 ? adata(abfd
).exec_bytes_size
761 : adata(abfd
).page_size
);
762 if (! obj_textsec(abfd
)->user_set_vma
)
763 /* ?? Do we really need to check for relocs here? */
764 obj_textsec(abfd
)->vma
= ((abfd
->flags
& HAS_RELOC
)
767 ? (abdp
->default_text_vma
768 + adata(abfd
).exec_bytes_size
)
769 : abdp
->default_text_vma
));
770 /* Could take strange alignment of text section into account here? */
772 /* Find start of data. */
773 text_end
= obj_textsec(abfd
)->filepos
+ obj_textsec(abfd
)->_raw_size
;
774 text_pad
= BFD_ALIGN (text_end
, adata(abfd
).page_size
) - text_end
;
775 obj_textsec(abfd
)->_raw_size
+= text_pad
;
776 text_end
+= text_pad
;
779 if (!obj_datasec(abfd
)->user_set_vma
)
782 vma
= obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
;
783 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
785 if (abdp
&& abdp
->zmagic_mapped_contiguous
)
787 text_pad
= (obj_datasec(abfd
)->vma
788 - obj_textsec(abfd
)->vma
789 - obj_textsec(abfd
)->_raw_size
);
790 obj_textsec(abfd
)->_raw_size
+= text_pad
;
792 obj_datasec(abfd
)->filepos
= (obj_textsec(abfd
)->filepos
793 + obj_textsec(abfd
)->_raw_size
);
795 /* Fix up exec header while we're at it. */
796 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
797 if (ztih
&& (!abdp
|| (abdp
&& !abdp
->exec_header_not_counted
)))
798 execp
->a_text
+= adata(abfd
).exec_bytes_size
;
799 N_SET_MAGIC (*execp
, ZMAGIC
);
801 /* Spec says data section should be rounded up to page boundary. */
802 obj_datasec(abfd
)->_raw_size
803 = align_power (obj_datasec(abfd
)->_raw_size
,
804 obj_bsssec(abfd
)->alignment_power
);
805 execp
->a_data
= BFD_ALIGN (obj_datasec(abfd
)->_raw_size
,
806 adata(abfd
).page_size
);
807 data_pad
= execp
->a_data
- obj_datasec(abfd
)->_raw_size
;
810 if (!obj_bsssec(abfd
)->user_set_vma
)
811 obj_bsssec(abfd
)->vma
= (obj_datasec(abfd
)->vma
812 + obj_datasec(abfd
)->_raw_size
);
813 /* If the BSS immediately follows the data section and extra space
814 in the page is left after the data section, fudge data
815 in the header so that the bss section looks smaller by that
816 amount. We'll start the bss section there, and lie to the OS.
817 (Note that a linker script, as well as the above assignment,
818 could have explicitly set the BSS vma to immediately follow
819 the data section.) */
820 if (align_power (obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->alignment_power
)
821 == obj_datasec(abfd
)->vma
+ obj_datasec(abfd
)->_raw_size
)
822 execp
->a_bss
= (data_pad
> obj_bsssec(abfd
)->_raw_size
) ? 0 :
823 obj_bsssec(abfd
)->_raw_size
- data_pad
;
825 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
829 adjust_n_magic (abfd
, execp
)
831 struct internal_exec
*execp
;
833 file_ptr pos
= adata(abfd
).exec_bytes_size
;
838 obj_textsec(abfd
)->filepos
= pos
;
839 if (!obj_textsec(abfd
)->user_set_vma
)
840 obj_textsec(abfd
)->vma
= vma
;
842 vma
= obj_textsec(abfd
)->vma
;
843 pos
+= obj_textsec(abfd
)->_raw_size
;
844 vma
+= obj_textsec(abfd
)->_raw_size
;
847 obj_datasec(abfd
)->filepos
= pos
;
848 if (!obj_datasec(abfd
)->user_set_vma
)
849 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
850 vma
= obj_datasec(abfd
)->vma
;
852 /* Since BSS follows data immediately, see if it needs alignment. */
853 vma
+= obj_datasec(abfd
)->_raw_size
;
854 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
855 obj_datasec(abfd
)->_raw_size
+= pad
;
856 pos
+= obj_datasec(abfd
)->_raw_size
;
859 if (!obj_bsssec(abfd
)->user_set_vma
)
860 obj_bsssec(abfd
)->vma
= vma
;
862 vma
= obj_bsssec(abfd
)->vma
;
864 /* Fix up exec header. */
865 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
866 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
867 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
868 N_SET_MAGIC (*execp
, NMAGIC
);
872 NAME(aout
,adjust_sizes_and_vmas
) (abfd
, text_size
, text_end
)
874 bfd_size_type
*text_size
;
877 struct internal_exec
*execp
= exec_hdr (abfd
);
879 if (! NAME(aout
,make_sections
) (abfd
))
882 if (adata(abfd
).magic
!= undecided_magic
) return true;
884 obj_textsec(abfd
)->_raw_size
=
885 align_power(obj_textsec(abfd
)->_raw_size
,
886 obj_textsec(abfd
)->alignment_power
);
888 *text_size
= obj_textsec (abfd
)->_raw_size
;
889 /* Rule (heuristic) for when to pad to a new page. Note that there
890 are (at least) two ways demand-paged (ZMAGIC) files have been
891 handled. Most Berkeley-based systems start the text segment at
892 (PAGE_SIZE). However, newer versions of SUNOS start the text
893 segment right after the exec header; the latter is counted in the
894 text segment size, and is paged in by the kernel with the rest of
897 /* This perhaps isn't the right way to do this, but made it simpler for me
898 to understand enough to implement it. Better would probably be to go
899 right from BFD flags to alignment/positioning characteristics. But the
900 old code was sloppy enough about handling the flags, and had enough
901 other magic, that it was a little hard for me to understand. I think
902 I understand it better now, but I haven't time to do the cleanup this
905 if (abfd
->flags
& D_PAGED
)
906 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
907 /* @@ What about QMAGIC? */
908 adata(abfd
).magic
= z_magic
;
909 else if (abfd
->flags
& WP_TEXT
)
910 adata(abfd
).magic
= n_magic
;
912 adata(abfd
).magic
= o_magic
;
914 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
916 fprintf (stderr
, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
918 switch (adata(abfd
).magic
) {
919 case n_magic
: str
= "NMAGIC"; break;
920 case o_magic
: str
= "OMAGIC"; break;
921 case z_magic
: str
= "ZMAGIC"; break;
926 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
,
927 obj_textsec(abfd
)->alignment_power
,
928 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
,
929 obj_datasec(abfd
)->alignment_power
,
930 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
,
931 obj_bsssec(abfd
)->alignment_power
);
935 switch (adata(abfd
).magic
)
938 adjust_o_magic (abfd
, execp
);
941 adjust_z_magic (abfd
, execp
);
944 adjust_n_magic (abfd
, execp
);
950 #ifdef BFD_AOUT_DEBUG
951 fprintf (stderr
, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
952 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
,
953 obj_textsec(abfd
)->filepos
,
954 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
,
955 obj_datasec(abfd
)->filepos
,
956 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
);
964 aout_@var{size}_new_section_hook
967 boolean aout_@var{size}_new_section_hook,
972 Called by the BFD in response to a @code{bfd_make_section}
976 NAME(aout
,new_section_hook
) (abfd
, newsect
)
980 /* align to double at least */
981 newsect
->alignment_power
= bfd_get_arch_info(abfd
)->section_align_power
;
984 if (bfd_get_format (abfd
) == bfd_object
)
986 if (obj_textsec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".text")) {
987 obj_textsec(abfd
)= newsect
;
988 newsect
->target_index
= N_TEXT
;
992 if (obj_datasec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".data")) {
993 obj_datasec(abfd
) = newsect
;
994 newsect
->target_index
= N_DATA
;
998 if (obj_bsssec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".bss")) {
999 obj_bsssec(abfd
) = newsect
;
1000 newsect
->target_index
= N_BSS
;
1006 /* We allow more than three sections internally */
1011 NAME(aout
,set_section_contents
) (abfd
, section
, location
, offset
, count
)
1016 bfd_size_type count
;
1019 bfd_size_type text_size
;
1021 if (abfd
->output_has_begun
== false)
1023 if (NAME(aout
,adjust_sizes_and_vmas
) (abfd
,
1025 &text_end
) == false)
1029 /* regardless, once we know what we're doing, we might as well get going */
1030 if (section
!= obj_bsssec(abfd
))
1032 if (bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
) != 0)
1036 return (bfd_write ((PTR
)location
, 1, count
, abfd
) == count
) ?
1044 /* Classify stabs symbols */
1046 #define sym_in_text_section(sym) \
1047 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
1049 #define sym_in_data_section(sym) \
1050 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
1052 #define sym_in_bss_section(sym) \
1053 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
1055 /* Symbol is undefined if type is N_UNDF|N_EXT and if it has
1056 zero in the "value" field. Nonzeroes there are fortrancommon
1058 #define sym_is_undefined(sym) \
1059 ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0)
1061 /* Symbol is a global definition if N_EXT is on and if it has
1062 a nonzero type field. */
1063 #define sym_is_global_defn(sym) \
1064 (((sym)->type & N_EXT) && (sym)->type & N_TYPE)
1066 /* Symbol is debugger info if any bits outside N_TYPE or N_EXT
1068 #define sym_is_debugger_info(sym) \
1069 (((sym)->type & ~(N_EXT | N_TYPE)) || (sym)->type == N_FN)
1071 #define sym_is_fortrancommon(sym) \
1072 (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0)
1074 /* Symbol is absolute if it has N_ABS set */
1075 #define sym_is_absolute(sym) \
1076 (((sym)->type & N_TYPE)== N_ABS)
1079 #define sym_is_indirect(sym) \
1080 (((sym)->type & N_ABS)== N_ABS)
1082 /* Read the external symbols from an a.out file. */
1085 aout_get_external_symbols (abfd
)
1088 if (obj_aout_external_syms (abfd
) == (struct external_nlist
*) NULL
)
1090 bfd_size_type count
;
1091 struct external_nlist
*syms
;
1093 count
= exec_hdr (abfd
)->a_syms
/ EXTERNAL_NLIST_SIZE
;
1095 /* We allocate using malloc to make the values easy to free
1096 later on. If we put them on the obstack it might not be
1097 possible to free them. */
1098 syms
= ((struct external_nlist
*)
1099 malloc ((size_t) count
* EXTERNAL_NLIST_SIZE
));
1100 if (syms
== (struct external_nlist
*) NULL
&& count
!= 0)
1102 bfd_set_error (bfd_error_no_memory
);
1106 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1107 || (bfd_read (syms
, 1, exec_hdr (abfd
)->a_syms
, abfd
)
1108 != exec_hdr (abfd
)->a_syms
))
1114 obj_aout_external_syms (abfd
) = syms
;
1115 obj_aout_external_sym_count (abfd
) = count
;
1118 if (obj_aout_external_strings (abfd
) == NULL
)
1120 unsigned char string_chars
[BYTES_IN_WORD
];
1121 bfd_size_type stringsize
;
1124 /* Get the size of the strings. */
1125 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0
1126 || (bfd_read ((PTR
) string_chars
, BYTES_IN_WORD
, 1, abfd
)
1129 stringsize
= GET_WORD (abfd
, string_chars
);
1131 strings
= (char *) malloc ((size_t) stringsize
+ 1);
1132 if (strings
== NULL
)
1134 bfd_set_error (bfd_error_no_memory
);
1138 /* Skip space for the string count in the buffer for convenience
1139 when using indexes. */
1140 if (bfd_read (strings
+ BYTES_IN_WORD
, 1, stringsize
- BYTES_IN_WORD
,
1142 != stringsize
- BYTES_IN_WORD
)
1148 /* Sanity preservation. */
1149 strings
[stringsize
] = '\0';
1151 obj_aout_external_strings (abfd
) = strings
;
1152 obj_aout_external_string_size (abfd
) = stringsize
;
1158 /* Only in their own functions for ease of debugging; when sym flags have
1159 stabilised these should be inlined into their (single) caller */
1162 translate_from_native_sym_flags (sym_pointer
, cache_ptr
, abfd
)
1163 struct external_nlist
*sym_pointer
;
1164 aout_symbol_type
* cache_ptr
;
1167 cache_ptr
->symbol
.section
= 0;
1168 switch (cache_ptr
->type
& N_TYPE
)
1170 case N_SETA
: case N_SETA
| N_EXT
:
1171 case N_SETT
: case N_SETT
| N_EXT
:
1172 case N_SETD
: case N_SETD
| N_EXT
:
1173 case N_SETB
: case N_SETB
| N_EXT
:
1175 char *copy
= bfd_alloc (abfd
, strlen (cache_ptr
->symbol
.name
) + 1);
1177 asection
*into_section
;
1178 arelent_chain
*reloc
= (arelent_chain
*) bfd_alloc (abfd
, sizeof (arelent_chain
));
1180 if (!copy
|| !reloc
)
1182 bfd_set_error (bfd_error_no_memory
);
1186 strcpy (copy
, cache_ptr
->symbol
.name
);
1188 /* Make sure that this bfd has a section with the right contructor
1190 section
= bfd_get_section_by_name (abfd
, copy
);
1192 section
= bfd_make_section (abfd
, copy
);
1194 /* Build a relocation entry for the constructor */
1195 switch ((cache_ptr
->type
& N_TYPE
))
1197 case N_SETA
: case N_SETA
| N_EXT
:
1198 into_section
= &bfd_abs_section
;
1199 cache_ptr
->type
= N_ABS
;
1201 case N_SETT
: case N_SETT
| N_EXT
:
1202 into_section
= (asection
*) obj_textsec (abfd
);
1203 cache_ptr
->type
= N_TEXT
;
1205 case N_SETD
: case N_SETD
| N_EXT
:
1206 into_section
= (asection
*) obj_datasec (abfd
);
1207 cache_ptr
->type
= N_DATA
;
1209 case N_SETB
: case N_SETB
| N_EXT
:
1210 into_section
= (asection
*) obj_bsssec (abfd
);
1211 cache_ptr
->type
= N_BSS
;
1214 bfd_set_error (bfd_error_bad_value
);
1218 /* Build a relocation pointing into the constuctor section
1219 pointing at the symbol in the set vector specified */
1221 reloc
->relent
.addend
= cache_ptr
->symbol
.value
;
1222 cache_ptr
->symbol
.section
= into_section
->symbol
->section
;
1223 reloc
->relent
.sym_ptr_ptr
= into_section
->symbol_ptr_ptr
;
1226 /* We modify the symbol to belong to a section depending upon the
1227 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
1228 really care, and add to the size of the section to contain a
1229 pointer to the symbol. Build a reloc entry to relocate to this
1230 symbol attached to this section. */
1232 section
->flags
= SEC_CONSTRUCTOR
| SEC_RELOC
;
1235 section
->reloc_count
++;
1236 section
->alignment_power
= 2;
1238 reloc
->next
= section
->constructor_chain
;
1239 section
->constructor_chain
= reloc
;
1240 reloc
->relent
.address
= section
->_raw_size
;
1241 section
->_raw_size
+= sizeof (int *);
1244 = (obj_reloc_entry_size(abfd
) == RELOC_EXT_SIZE
1245 ? howto_table_ext
: howto_table_std
)
1246 + CTOR_TABLE_RELOC_IDX
;
1247 cache_ptr
->symbol
.flags
|= BSF_CONSTRUCTOR
;
1251 if (cache_ptr
->type
== N_WARNING
)
1253 /* This symbol is the text of a warning message, the next symbol
1254 is the symbol to associate the warning with */
1255 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_WARNING
;
1257 /* @@ Stuffing pointers into integers is a no-no.
1258 We can usually get away with it if the integer is
1259 large enough though. */
1260 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1262 cache_ptr
->symbol
.value
= (bfd_vma
) ((cache_ptr
+ 1));
1264 /* We don't use a warning symbol's section, but we need
1265 it to be nonzero for the sanity check below, so
1266 pick one arbitrarily. */
1267 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1269 /* We furgle with the next symbol in place.
1270 We don't want it to be undefined, we'll trample the type */
1271 (sym_pointer
+ 1)->e_type
[0] = 0xff;
1274 if ((cache_ptr
->type
| N_EXT
) == (N_INDR
| N_EXT
))
1276 /* Two symbols in a row for an INDR message. The first symbol
1277 contains the name we will match, the second symbol contains
1278 the name the first name is translated into. It is supplied to
1279 us undefined. This is good, since we want to pull in any files
1281 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_INDIRECT
;
1283 /* @@ Stuffing pointers into integers is a no-no.
1284 We can usually get away with it if the integer is
1285 large enough though. */
1286 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1289 cache_ptr
->symbol
.value
= (bfd_vma
) ((cache_ptr
+ 1));
1290 cache_ptr
->symbol
.section
= &bfd_ind_section
;
1293 else if (sym_is_debugger_info (cache_ptr
))
1295 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
;
1296 /* Work out the section correct for this symbol */
1297 switch (cache_ptr
->type
& N_TYPE
)
1301 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1302 cache_ptr
->symbol
.value
-= obj_textsec (abfd
)->vma
;
1305 cache_ptr
->symbol
.value
-= obj_datasec (abfd
)->vma
;
1306 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1309 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1310 cache_ptr
->symbol
.value
-= obj_bsssec (abfd
)->vma
;
1314 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1321 if (sym_is_fortrancommon (cache_ptr
))
1323 cache_ptr
->symbol
.flags
= 0;
1324 cache_ptr
->symbol
.section
= &bfd_com_section
;
1332 /* In a.out, the value of a symbol is always relative to the
1333 * start of the file, if this is a data symbol we'll subtract
1334 * the size of the text section to get the section relative
1335 * value. If this is a bss symbol (which would be strange)
1336 * we'll subtract the size of the previous two sections
1337 * to find the section relative address.
1340 if (sym_in_text_section (cache_ptr
))
1342 cache_ptr
->symbol
.value
-= obj_textsec (abfd
)->vma
;
1343 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1345 else if (sym_in_data_section (cache_ptr
))
1347 cache_ptr
->symbol
.value
-= obj_datasec (abfd
)->vma
;
1348 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1350 else if (sym_in_bss_section (cache_ptr
))
1352 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1353 cache_ptr
->symbol
.value
-= obj_bsssec (abfd
)->vma
;
1355 else if (sym_is_undefined (cache_ptr
))
1357 cache_ptr
->symbol
.flags
= 0;
1358 cache_ptr
->symbol
.section
= &bfd_und_section
;
1360 else if (sym_is_absolute (cache_ptr
))
1362 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1365 if (sym_is_global_defn (cache_ptr
))
1367 cache_ptr
->symbol
.flags
= BSF_GLOBAL
| BSF_EXPORT
;
1369 else if (! sym_is_undefined (cache_ptr
))
1371 cache_ptr
->symbol
.flags
= BSF_LOCAL
;
1375 if (cache_ptr
->symbol
.section
== 0)
1382 translate_to_native_sym_flags (sym_pointer
, cache_ptr
, abfd
)
1383 struct external_nlist
*sym_pointer
;
1387 bfd_vma value
= cache_ptr
->value
;
1389 /* mask out any existing type bits in case copying from one section
1391 sym_pointer
->e_type
[0] &= ~N_TYPE
;
1393 /* We attempt to order these tests by decreasing frequency of success,
1394 according to tcov when linking the linker. */
1395 if (bfd_get_output_section(cache_ptr
) == &bfd_abs_section
) {
1396 sym_pointer
->e_type
[0] |= N_ABS
;
1398 else if (bfd_get_output_section(cache_ptr
) == obj_textsec (abfd
)) {
1399 sym_pointer
->e_type
[0] |= N_TEXT
;
1401 else if (bfd_get_output_section(cache_ptr
) == obj_datasec (abfd
)) {
1402 sym_pointer
->e_type
[0] |= N_DATA
;
1404 else if (bfd_get_output_section(cache_ptr
) == obj_bsssec (abfd
)) {
1405 sym_pointer
->e_type
[0] |= N_BSS
;
1407 else if (bfd_get_output_section(cache_ptr
) == &bfd_und_section
) {
1408 sym_pointer
->e_type
[0] = (N_UNDF
| N_EXT
);
1410 else if (bfd_get_output_section(cache_ptr
) == &bfd_ind_section
) {
1411 sym_pointer
->e_type
[0] = N_INDR
;
1413 else if (bfd_get_output_section(cache_ptr
) == NULL
) {
1414 /* Protect the bfd_is_com_section call.
1415 This case occurs, e.g., for the *DEBUG* section of a COFF file. */
1416 bfd_set_error (bfd_error_nonrepresentable_section
);
1419 else if (bfd_is_com_section (bfd_get_output_section (cache_ptr
))) {
1420 sym_pointer
->e_type
[0] = (N_UNDF
| N_EXT
);
1423 bfd_set_error (bfd_error_nonrepresentable_section
);
1427 /* Turn the symbol from section relative to absolute again */
1429 value
+= cache_ptr
->section
->output_section
->vma
+ cache_ptr
->section
->output_offset
;
1432 if (cache_ptr
->flags
& (BSF_WARNING
)) {
1433 sym_pointer
->e_type
[0] = N_WARNING
;
1434 (sym_pointer
+1)->e_type
[0] = 1;
1437 if (cache_ptr
->flags
& BSF_DEBUGGING
) {
1438 sym_pointer
->e_type
[0] = ((aout_symbol_type
*)cache_ptr
)->type
;
1440 else if (cache_ptr
->flags
& (BSF_GLOBAL
| BSF_EXPORT
)) {
1441 sym_pointer
->e_type
[0] |= N_EXT
;
1443 if (cache_ptr
->flags
& BSF_CONSTRUCTOR
) {
1444 int type
= ((aout_symbol_type
*)cache_ptr
)->type
;
1447 case N_ABS
: type
= N_SETA
; break;
1448 case N_TEXT
: type
= N_SETT
; break;
1449 case N_DATA
: type
= N_SETD
; break;
1450 case N_BSS
: type
= N_SETB
; break;
1452 sym_pointer
->e_type
[0] = type
;
1455 PUT_WORD(abfd
, value
, sym_pointer
->e_value
);
1460 /* Native-level interface to symbols. */
1464 NAME(aout
,make_empty_symbol
) (abfd
)
1467 aout_symbol_type
*new =
1468 (aout_symbol_type
*)bfd_zalloc (abfd
, sizeof (aout_symbol_type
));
1471 bfd_set_error (bfd_error_no_memory
);
1474 new->symbol
.the_bfd
= abfd
;
1476 return &new->symbol
;
1479 /* Translate a set of internal symbols into external symbols. */
1482 NAME(aout
,translate_symbol_table
) (abfd
, in
, ext
, count
, str
, strsize
, dynamic
)
1484 aout_symbol_type
*in
;
1485 struct external_nlist
*ext
;
1486 bfd_size_type count
;
1488 bfd_size_type strsize
;
1491 struct external_nlist
*ext_end
;
1493 ext_end
= ext
+ count
;
1494 for (; ext
< ext_end
; ext
++, in
++)
1498 x
= GET_WORD (abfd
, ext
->e_strx
);
1499 in
->symbol
.the_bfd
= abfd
;
1501 /* For the normal symbols, the zero index points at the number
1502 of bytes in the string table but is to be interpreted as the
1503 null string. For the dynamic symbols, the number of bytes in
1504 the string table is stored in the __DYNAMIC structure and the
1505 zero index points at an actual string. */
1506 if (x
== 0 && ! dynamic
)
1507 in
->symbol
.name
= "";
1508 else if (x
< strsize
)
1509 in
->symbol
.name
= str
+ x
;
1513 in
->symbol
.value
= GET_SWORD (abfd
, ext
->e_value
);
1514 in
->desc
= bfd_h_get_16 (abfd
, ext
->e_desc
);
1515 in
->other
= bfd_h_get_8 (abfd
, ext
->e_other
);
1516 in
->type
= bfd_h_get_8 (abfd
, ext
->e_type
);
1517 in
->symbol
.udata
= 0;
1519 if (!translate_from_native_sym_flags (ext
, in
, abfd
))
1523 in
->symbol
.flags
|= BSF_DYNAMIC
;
1529 /* We read the symbols into a buffer, which is discarded when this
1530 function exits. We read the strings into a buffer large enough to
1531 hold them all plus all the cached symbol entries. */
1534 NAME(aout
,slurp_symbol_table
) (abfd
)
1537 struct external_nlist
*old_external_syms
;
1538 aout_symbol_type
*cached
;
1541 /* If there's no work to be done, don't do any */
1542 if (obj_aout_symbols (abfd
) != (aout_symbol_type
*) NULL
)
1545 old_external_syms
= obj_aout_external_syms (abfd
);
1547 if (! aout_get_external_symbols (abfd
))
1550 if (obj_aout_external_sym_count (abfd
) == 0)
1552 bfd_set_error (bfd_error_no_symbols
);
1556 cached_size
= (obj_aout_external_sym_count (abfd
)
1557 * sizeof (aout_symbol_type
));
1558 cached
= (aout_symbol_type
*) malloc (cached_size
);
1559 memset (cached
, 0, cached_size
);
1563 bfd_set_error (bfd_error_no_memory
);
1567 /* Convert from external symbol information to internal. */
1568 if (! (NAME(aout
,translate_symbol_table
)
1570 obj_aout_external_syms (abfd
),
1571 obj_aout_external_sym_count (abfd
),
1572 obj_aout_external_strings (abfd
),
1573 obj_aout_external_string_size (abfd
),
1580 bfd_get_symcount (abfd
) = obj_aout_external_sym_count (abfd
);
1582 obj_aout_symbols (abfd
) = cached
;
1584 /* It is very likely that anybody who calls this function will not
1585 want the external symbol information, so if it was allocated
1586 because of our call to aout_get_external_symbols, we free it up
1587 right away to save space. */
1588 if (old_external_syms
== (struct external_nlist
*) NULL
1589 && obj_aout_external_syms (abfd
) != (struct external_nlist
*) NULL
)
1591 free (obj_aout_external_syms (abfd
));
1592 obj_aout_external_syms (abfd
) = NULL
;
1598 /* Possible improvements:
1599 + look for strings matching trailing substrings of other strings
1600 + better data structures? balanced trees?
1601 + smaller per-string or per-symbol data? re-use some of the symbol's
1603 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1604 construct the entire symbol table at once, we could get by with smaller
1605 amounts of VM? (What effect does that have on the string table
1607 + rip this out of here, put it into its own file in bfd or libiberty, so
1608 coff and elf can use it too. I'll work on this soon, but have more
1609 pressing tasks right now.
1611 A hash table might(?) be more efficient for handling exactly the cases that
1612 are handled now, but for trailing substring matches, I think we want to
1613 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1614 order, nor look only for exact-match or not-match. I don't think a hash
1615 table would be very useful for that, and I don't feel like fleshing out two
1616 completely different implementations. [raeburn:930419.0331EDT] */
1618 struct stringtab_entry
{
1619 /* Hash value for this string. Only useful so long as we aren't doing
1620 substring matches. */
1623 /* Next node to look at, depending on whether the hash value of the string
1624 being searched for is less than or greater than the hash value of the
1625 current node. For now, `equal to' is lumped in with `greater than', for
1626 space efficiency. It's not a common enough case to warrant another field
1627 to be used for all nodes. */
1628 struct stringtab_entry
*less
;
1629 struct stringtab_entry
*greater
;
1631 /* The string itself. */
1634 /* The index allocated for this string. */
1635 bfd_size_type index
;
1637 #ifdef GATHER_STATISTICS
1638 /* How many references have there been to this string? (Not currently used;
1639 could be dumped out for anaylsis, if anyone's interested.) */
1640 unsigned long count
;
1643 /* Next node in linked list, in suggested output order. */
1644 struct stringtab_entry
*next_to_output
;
1647 struct stringtab_data
{
1648 /* Tree of string table entries. */
1649 struct stringtab_entry
*strings
;
1651 /* Fudge factor used to center top node of tree. */
1654 /* Next index value to issue. */
1655 bfd_size_type index
;
1657 /* Index used for empty strings. Cached here because checking for them
1658 is really easy, and we can avoid searching the tree. */
1659 bfd_size_type empty_string_index
;
1661 /* These fields indicate the two ends of a singly-linked list that indicates
1662 the order strings should be written out in. Use this order, and no
1663 seeking will need to be done, so output efficiency should be maximized. */
1664 struct stringtab_entry
**end
;
1665 struct stringtab_entry
*output_order
;
1667 #ifdef GATHER_STATISTICS
1668 /* Number of strings which duplicate strings already in the table. */
1669 unsigned long duplicates
;
1671 /* Number of bytes saved by not having to write all the duplicate strings. */
1672 unsigned long bytes_saved
;
1674 /* Number of zero-length strings. Currently, these all turn into
1675 references to the null byte at the end of the first string. In some
1676 cases (possibly not all? explore this...), it should be possible to
1677 simply write out a zero index value. */
1678 unsigned long empty_strings
;
1680 /* Number of times the hash values matched but the strings were different.
1681 Note that this includes the number of times the other string(s) occurs, so
1682 there may only be two strings hashing to the same value, even if this
1683 number is very large. */
1684 unsigned long bad_hash_matches
;
1686 /* Null strings aren't counted in this one.
1687 This will probably only be nonzero if we've got an input file
1688 which was produced by `ld -r' (i.e., it's already been processed
1689 through this code). Under some operating systems, native tools
1690 may make all empty strings have the same index; but the pointer
1691 check won't catch those, because to get to that stage we'd already
1692 have to compute the checksum, which requires reading the string,
1693 so we short-circuit that case with empty_string_index above. */
1694 unsigned long pointer_matches
;
1696 /* Number of comparisons done. I figure with the algorithms in use below,
1697 the average number of comparisons done (per symbol) should be roughly
1698 log-base-2 of the number of unique strings. */
1699 unsigned long n_compares
;
1703 /* Some utility functions for the string table code. */
1705 /* For speed, only hash on the first this many bytes of strings.
1706 This number was chosen by profiling ld linking itself, with -g. */
1707 #define HASHMAXLEN 25
1709 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1711 static INLINE
unsigned int
1713 unsigned char *string
;
1714 register unsigned int len
;
1716 register unsigned int sum
= 0;
1718 if (len
> HASHMAXLEN
)
1726 HASH_CHAR (*string
++);
1732 stringtab_init (tab
)
1733 struct stringtab_data
*tab
;
1736 tab
->output_order
= 0;
1738 tab
->end
= &tab
->output_order
;
1740 /* Initial string table length includes size of length field. */
1741 tab
->index
= BYTES_IN_WORD
;
1742 tab
->empty_string_index
= -1;
1743 #ifdef GATHER_STATISTICS
1744 tab
->duplicates
= 0;
1745 tab
->empty_strings
= 0;
1746 tab
->bad_hash_matches
= 0;
1747 tab
->pointer_matches
= 0;
1748 tab
->bytes_saved
= 0;
1749 tab
->n_compares
= 0;
1754 compare (entry
, str
, hash
)
1755 struct stringtab_entry
*entry
;
1759 return hash
- entry
->hash
;
1762 #ifdef GATHER_STATISTICS
1763 /* Don't want to have to link in math library with all bfd applications... */
1764 static INLINE
double
1772 return ((d
> 1.41) ? 0.5 : 0) + n
;
1776 /* Main string table routines. */
1777 /* Returns index in string table. Whether or not this actually adds an
1778 entry into the string table should be irrelevant -- it just has to
1779 return a valid index. */
1780 static bfd_size_type
1781 add_to_stringtab (abfd
, str
, tab
)
1784 struct stringtab_data
*tab
;
1786 struct stringtab_entry
**ep
;
1787 register struct stringtab_entry
*entry
;
1788 unsigned int hashval
, len
;
1792 bfd_size_type index
;
1793 CONST bfd_size_type minus_one
= -1;
1795 #ifdef GATHER_STATISTICS
1796 tab
->empty_strings
++;
1798 index
= tab
->empty_string_index
;
1799 if (index
!= minus_one
)
1802 #ifdef GATHER_STATISTICS
1809 /* Need to find it. */
1810 entry
= tab
->strings
;
1813 index
= entry
->index
+ strlen (entry
->string
);
1814 tab
->empty_string_index
= index
;
1822 /* The hash_zero value is chosen such that the first symbol gets a value of
1823 zero. With a balanced tree, this wouldn't be very useful, but without it,
1824 we might get a more even split at the top level, instead of skewing it
1825 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1826 hashval
= hash (str
, len
) ^ tab
->hash_zero
;
1830 tab
->hash_zero
= hashval
;
1840 #ifdef GATHER_STATISTICS
1843 cmp
= compare (entry
, str
, hashval
);
1844 /* The not-equal cases are more frequent, so check them first. */
1846 ep
= &entry
->greater
;
1851 if (entry
->string
== str
)
1853 #ifdef GATHER_STATISTICS
1854 tab
->pointer_matches
++;
1858 /* Compare the first bytes to save a function call if they
1860 if (entry
->string
[0] == str
[0] && !strcmp (entry
->string
, str
))
1863 #ifdef GATHER_STATISTICS
1865 tab
->bytes_saved
+= len
+ 1;
1868 /* If we're in the linker, and the new string is from a new
1869 input file which might have already had these reductions
1870 run over it, we want to keep the new string pointer. I
1871 don't think we're likely to see any (or nearly as many,
1872 at least) cases where a later string is in the same location
1873 as an earlier one rather than this one. */
1874 entry
->string
= str
;
1875 return entry
->index
;
1877 #ifdef GATHER_STATISTICS
1878 tab
->bad_hash_matches
++;
1880 ep
= &entry
->greater
;
1884 /* If we get here, nothing that's in the table already matched.
1885 EP points to the `next' field at the end of the chain; stick a
1886 new entry on here. */
1888 entry
= (struct stringtab_entry
*)
1889 bfd_alloc_by_size_t (abfd
, sizeof (struct stringtab_entry
));
1892 bfd_set_error (bfd_error_no_memory
);
1893 abort(); /* FIXME */
1896 entry
->less
= entry
->greater
= 0;
1897 entry
->hash
= hashval
;
1898 entry
->index
= tab
->index
;
1899 entry
->string
= str
;
1900 entry
->next_to_output
= 0;
1901 #ifdef GATHER_STATISTICS
1905 assert (*tab
->end
== 0);
1906 *(tab
->end
) = entry
;
1907 tab
->end
= &entry
->next_to_output
;
1908 assert (*tab
->end
== 0);
1911 tab
->index
+= len
+ 1;
1913 tab
->empty_string_index
= entry
->index
;
1917 return entry
->index
;
1921 emit_strtab (abfd
, tab
)
1923 struct stringtab_data
*tab
;
1925 struct stringtab_entry
*entry
;
1926 #ifdef GATHER_STATISTICS
1930 /* Be sure to put string length into correct byte ordering before writing
1932 char buffer
[BYTES_IN_WORD
];
1934 PUT_WORD (abfd
, tab
->index
, (unsigned char *) buffer
);
1935 if (bfd_write ((PTR
) buffer
, 1, BYTES_IN_WORD
, abfd
) != BYTES_IN_WORD
)
1938 for (entry
= tab
->output_order
; entry
; entry
= entry
->next_to_output
)
1940 size_t len
= strlen (entry
->string
) + 1;
1942 if (bfd_write ((PTR
) entry
->string
, 1, len
, abfd
) != len
)
1945 #ifdef GATHER_STATISTICS
1950 #ifdef GATHER_STATISTICS
1951 /* Short form only, for now.
1952 To do: Specify output file. Conditionalize on environment? Detailed
1953 analysis if desired. */
1955 int n_syms
= bfd_get_symcount (abfd
);
1957 fprintf (stderr
, "String table data for output file:\n");
1958 fprintf (stderr
, " %8d symbols output\n", n_syms
);
1959 fprintf (stderr
, " %8d duplicate strings\n", tab
->duplicates
);
1960 fprintf (stderr
, " %8d empty strings\n", tab
->empty_strings
);
1961 fprintf (stderr
, " %8d unique strings output\n", count
);
1962 fprintf (stderr
, " %8d pointer matches\n", tab
->pointer_matches
);
1963 fprintf (stderr
, " %8d bytes saved\n", tab
->bytes_saved
);
1964 fprintf (stderr
, " %8d bad hash matches\n", tab
->bad_hash_matches
);
1965 fprintf (stderr
, " %8d hash-val comparisons\n", tab
->n_compares
);
1968 double n_compares
= tab
->n_compares
;
1969 double avg_compares
= n_compares
/ n_syms
;
1970 /* The second value here should usually be near one. */
1972 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
1973 avg_compares
, avg_compares
/ log2 (count
));
1980 generic = bfd_get_outsymbols(abfd);
1981 for (count = 0; count < bfd_get_symcount(abfd); count++)
1983 asymbol *g = *(generic++);
1987 size_t length = strlen(g->name)+1;
1988 bfd_write((PTR)g->name, 1, length, abfd);
1990 g->KEEPIT = (KEEPITTYPE) count;
1997 NAME(aout
,write_syms
) (abfd
)
2000 unsigned int count
;
2001 asymbol
**generic
= bfd_get_outsymbols (abfd
);
2002 struct stringtab_data strtab
;
2004 stringtab_init (&strtab
);
2006 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
2008 asymbol
*g
= generic
[count
];
2009 struct external_nlist nsp
;
2012 PUT_WORD (abfd
, add_to_stringtab (abfd
, g
->name
, &strtab
),
2013 (unsigned char *) nsp
.e_strx
);
2015 PUT_WORD (abfd
, 0, (unsigned char *)nsp
.e_strx
);
2017 if (bfd_asymbol_flavour(g
) == abfd
->xvec
->flavour
)
2019 bfd_h_put_16(abfd
, aout_symbol(g
)->desc
, nsp
.e_desc
);
2020 bfd_h_put_8(abfd
, aout_symbol(g
)->other
, nsp
.e_other
);
2021 bfd_h_put_8(abfd
, aout_symbol(g
)->type
, nsp
.e_type
);
2025 bfd_h_put_16(abfd
,0, nsp
.e_desc
);
2026 bfd_h_put_8(abfd
, 0, nsp
.e_other
);
2027 bfd_h_put_8(abfd
, 0, nsp
.e_type
);
2030 if (! translate_to_native_sym_flags (&nsp
, g
, abfd
))
2033 if (bfd_write((PTR
)&nsp
,1,EXTERNAL_NLIST_SIZE
, abfd
)
2034 != EXTERNAL_NLIST_SIZE
)
2037 /* NB: `KEEPIT' currently overlays `flags', so set this only
2038 here, at the end. */
2042 return emit_strtab (abfd
, &strtab
);
2047 NAME(aout
,get_symtab
) (abfd
, location
)
2051 unsigned int counter
= 0;
2052 aout_symbol_type
*symbase
;
2054 if (!NAME(aout
,slurp_symbol_table
)(abfd
))
2057 for (symbase
= obj_aout_symbols(abfd
); counter
++ < bfd_get_symcount (abfd
);)
2058 *(location
++) = (asymbol
*)( symbase
++);
2060 return bfd_get_symcount (abfd
);
2064 /* Standard reloc stuff */
2065 /* Output standard relocation information to a file in target byte order. */
2068 NAME(aout
,swap_std_reloc_out
) (abfd
, g
, natptr
)
2071 struct reloc_std_external
*natptr
;
2074 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2076 unsigned int r_length
;
2078 int r_baserel
, r_jmptable
, r_relative
;
2079 asection
*output_section
= sym
->section
->output_section
;
2081 PUT_WORD(abfd
, g
->address
, natptr
->r_address
);
2083 r_length
= g
->howto
->size
; /* Size as a power of two */
2084 r_pcrel
= (int) g
->howto
->pc_relative
; /* Relative to PC? */
2085 /* XXX This relies on relocs coming from a.out files. */
2086 r_baserel
= (g
->howto
->type
& 8) != 0;
2087 /* r_jmptable, r_relative??? FIXME-soon */
2092 /* For a standard reloc, the addend is in the object file. */
2093 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2096 /* name was clobbered by aout_write_syms to be symbol index */
2098 /* If this relocation is relative to a symbol then set the
2099 r_index to the symbols index, and the r_extern bit.
2101 Absolute symbols can come in in two ways, either as an offset
2102 from the abs section, or as a symbol which has an abs value.
2107 if (bfd_is_com_section (output_section
)
2108 || output_section
== &bfd_abs_section
2109 || output_section
== &bfd_und_section
)
2111 if (bfd_abs_section
.symbol
== sym
)
2113 /* Whoops, looked like an abs symbol, but is really an offset
2114 from the abs section */
2120 /* Fill in symbol */
2122 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
2128 /* Just an ordinary section */
2130 r_index
= output_section
->target_index
;
2133 /* now the fun stuff */
2134 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2135 natptr
->r_index
[0] = r_index
>> 16;
2136 natptr
->r_index
[1] = r_index
>> 8;
2137 natptr
->r_index
[2] = r_index
;
2139 (r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
2140 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
2141 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
2142 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
2143 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
2144 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
);
2146 natptr
->r_index
[2] = r_index
>> 16;
2147 natptr
->r_index
[1] = r_index
>> 8;
2148 natptr
->r_index
[0] = r_index
;
2150 (r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
2151 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
2152 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
2153 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
2154 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
2155 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
);
2160 /* Extended stuff */
2161 /* Output extended relocation information to a file in target byte order. */
2164 NAME(aout
,swap_ext_reloc_out
) (abfd
, g
, natptr
)
2167 register struct reloc_ext_external
*natptr
;
2171 unsigned int r_type
;
2172 unsigned int r_addend
;
2173 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2174 asection
*output_section
= sym
->section
->output_section
;
2176 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
2178 r_type
= (unsigned int) g
->howto
->type
;
2180 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2182 /* If this relocation is relative to a symbol then set the
2183 r_index to the symbols index, and the r_extern bit.
2185 Absolute symbols can come in in two ways, either as an offset
2186 from the abs section, or as a symbol which has an abs value.
2187 check for that here. */
2189 if (bfd_is_com_section (output_section
)
2190 || output_section
== &bfd_abs_section
2191 || output_section
== &bfd_und_section
)
2193 if (bfd_abs_section
.symbol
== sym
)
2195 /* Whoops, looked like an abs symbol, but is really an offset
2196 from the abs section */
2203 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
2208 /* Just an ordinary section */
2210 r_index
= output_section
->target_index
;
2213 /* now the fun stuff */
2214 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2215 natptr
->r_index
[0] = r_index
>> 16;
2216 natptr
->r_index
[1] = r_index
>> 8;
2217 natptr
->r_index
[2] = r_index
;
2219 ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
2220 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
2222 natptr
->r_index
[2] = r_index
>> 16;
2223 natptr
->r_index
[1] = r_index
>> 8;
2224 natptr
->r_index
[0] = r_index
;
2226 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
2227 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
2230 PUT_WORD (abfd
, r_addend
, natptr
->r_addend
);
2233 /* BFD deals internally with all things based from the section they're
2234 in. so, something in 10 bytes into a text section with a base of
2235 50 would have a symbol (.text+10) and know .text vma was 50.
2237 Aout keeps all it's symbols based from zero, so the symbol would
2238 contain 60. This macro subs the base of each section from the value
2239 to give the true offset from the section */
2242 #define MOVE_ADDRESS(ad) \
2244 /* undefined symbol */ \
2245 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2246 cache_ptr->addend = ad; \
2248 /* defined, section relative. replace symbol with pointer to \
2249 symbol which points to section */ \
2250 switch (r_index) { \
2252 case N_TEXT | N_EXT: \
2253 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2254 cache_ptr->addend = ad - su->textsec->vma; \
2257 case N_DATA | N_EXT: \
2258 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2259 cache_ptr->addend = ad - su->datasec->vma; \
2262 case N_BSS | N_EXT: \
2263 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2264 cache_ptr->addend = ad - su->bsssec->vma; \
2268 case N_ABS | N_EXT: \
2269 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2270 cache_ptr->addend = ad; \
2276 NAME(aout
,swap_ext_reloc_in
) (abfd
, bytes
, cache_ptr
, symbols
)
2278 struct reloc_ext_external
*bytes
;
2284 unsigned int r_type
;
2285 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2287 cache_ptr
->address
= (GET_SWORD (abfd
, bytes
->r_address
));
2289 /* now the fun stuff */
2290 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2291 r_index
= (bytes
->r_index
[0] << 16)
2292 | (bytes
->r_index
[1] << 8)
2293 | bytes
->r_index
[2];
2294 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
2295 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
2296 >> RELOC_EXT_BITS_TYPE_SH_BIG
;
2298 r_index
= (bytes
->r_index
[2] << 16)
2299 | (bytes
->r_index
[1] << 8)
2300 | bytes
->r_index
[0];
2301 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
2302 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
2303 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
;
2306 cache_ptr
->howto
= howto_table_ext
+ r_type
;
2307 MOVE_ADDRESS(GET_SWORD(abfd
, bytes
->r_addend
));
2311 NAME(aout
,swap_std_reloc_in
) (abfd
, bytes
, cache_ptr
, symbols
)
2313 struct reloc_std_external
*bytes
;
2319 unsigned int r_length
;
2321 int r_baserel
, r_jmptable
, r_relative
;
2322 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2325 cache_ptr
->address
= bfd_h_get_32 (abfd
, bytes
->r_address
);
2327 /* now the fun stuff */
2328 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2329 r_index
= (bytes
->r_index
[0] << 16)
2330 | (bytes
->r_index
[1] << 8)
2331 | bytes
->r_index
[2];
2332 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
2333 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
2334 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
2335 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
2336 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
2337 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
2338 >> RELOC_STD_BITS_LENGTH_SH_BIG
;
2340 r_index
= (bytes
->r_index
[2] << 16)
2341 | (bytes
->r_index
[1] << 8)
2342 | bytes
->r_index
[0];
2343 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
2344 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
2345 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
2346 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
2347 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
2348 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
2349 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
;
2352 howto_idx
= r_length
+ 4 * r_pcrel
+ 8 * r_baserel
;
2353 BFD_ASSERT (howto_idx
< TABLE_SIZE (howto_table_std
));
2354 cache_ptr
->howto
= howto_table_std
+ howto_idx
;
2355 BFD_ASSERT (cache_ptr
->howto
->type
!= -1);
2356 BFD_ASSERT (r_jmptable
== 0);
2357 BFD_ASSERT (r_relative
== 0);
2358 /* FIXME-soon: Roll jmptable, relative bits into howto setting */
2363 /* Read and swap the relocs for a section. */
2366 NAME(aout
,slurp_reloc_table
) (abfd
, asect
, symbols
)
2372 bfd_size_type reloc_size
;
2374 arelent
*reloc_cache
;
2376 unsigned int counter
= 0;
2379 if (asect
->relocation
)
2382 if (asect
->flags
& SEC_CONSTRUCTOR
)
2385 if (asect
== obj_datasec (abfd
))
2386 reloc_size
= exec_hdr(abfd
)->a_drsize
;
2387 else if (asect
== obj_textsec (abfd
))
2388 reloc_size
= exec_hdr(abfd
)->a_trsize
;
2391 bfd_set_error (bfd_error_invalid_operation
);
2395 if (bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
) != 0)
2398 each_size
= obj_reloc_entry_size (abfd
);
2400 count
= reloc_size
/ each_size
;
2402 reloc_cache
= (arelent
*) malloc ((size_t) (count
* sizeof (arelent
)));
2403 if (reloc_cache
== NULL
&& count
!= 0)
2405 bfd_set_error (bfd_error_no_memory
);
2408 memset (reloc_cache
, 0, count
* sizeof (arelent
));
2410 relocs
= malloc (reloc_size
);
2411 if (relocs
== NULL
&& reloc_size
!= 0)
2414 bfd_set_error (bfd_error_no_memory
);
2418 if (bfd_read (relocs
, 1, reloc_size
, abfd
) != reloc_size
)
2425 cache_ptr
= reloc_cache
;
2426 if (each_size
== RELOC_EXT_SIZE
)
2428 register struct reloc_ext_external
*rptr
=
2429 (struct reloc_ext_external
*) relocs
;
2431 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2432 NAME(aout
,swap_ext_reloc_in
) (abfd
, rptr
, cache_ptr
, symbols
);
2436 register struct reloc_std_external
*rptr
=
2437 (struct reloc_std_external
*) relocs
;
2439 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++)
2440 NAME(aout
,swap_std_reloc_in
) (abfd
, rptr
, cache_ptr
, symbols
);
2445 asect
->relocation
= reloc_cache
;
2446 asect
->reloc_count
= cache_ptr
- reloc_cache
;
2451 /* Write out a relocation section into an object file. */
2454 NAME(aout
,squirt_out_relocs
) (abfd
, section
)
2459 unsigned char *native
, *natptr
;
2462 unsigned int count
= section
->reloc_count
;
2465 if (count
== 0) return true;
2467 each_size
= obj_reloc_entry_size (abfd
);
2468 natsize
= each_size
* count
;
2469 native
= (unsigned char *) bfd_zalloc (abfd
, natsize
);
2471 bfd_set_error (bfd_error_no_memory
);
2475 generic
= section
->orelocation
;
2477 if (each_size
== RELOC_EXT_SIZE
)
2479 for (natptr
= native
;
2481 --count
, natptr
+= each_size
, ++generic
)
2482 NAME(aout
,swap_ext_reloc_out
) (abfd
, *generic
, (struct reloc_ext_external
*)natptr
);
2486 for (natptr
= native
;
2488 --count
, natptr
+= each_size
, ++generic
)
2489 NAME(aout
,swap_std_reloc_out
)(abfd
, *generic
, (struct reloc_std_external
*)natptr
);
2492 if ( bfd_write ((PTR
) native
, 1, natsize
, abfd
) != natsize
) {
2493 bfd_release(abfd
, native
);
2496 bfd_release (abfd
, native
);
2501 /* This is stupid. This function should be a boolean predicate */
2503 NAME(aout
,canonicalize_reloc
) (abfd
, section
, relptr
, symbols
)
2509 arelent
*tblptr
= section
->relocation
;
2512 if (!(tblptr
|| NAME(aout
,slurp_reloc_table
)(abfd
, section
, symbols
)))
2515 if (section
->flags
& SEC_CONSTRUCTOR
) {
2516 arelent_chain
*chain
= section
->constructor_chain
;
2517 for (count
= 0; count
< section
->reloc_count
; count
++) {
2518 *relptr
++ = &chain
->relent
;
2519 chain
= chain
->next
;
2523 tblptr
= section
->relocation
;
2525 for (count
= 0; count
++ < section
->reloc_count
;)
2527 *relptr
++ = tblptr
++;
2532 return section
->reloc_count
;
2536 NAME(aout
,get_reloc_upper_bound
) (abfd
, asect
)
2540 if (bfd_get_format (abfd
) != bfd_object
) {
2541 bfd_set_error (bfd_error_invalid_operation
);
2544 if (asect
->flags
& SEC_CONSTRUCTOR
) {
2545 return (sizeof (arelent
*) * (asect
->reloc_count
+1));
2548 if (asect
== obj_datasec (abfd
))
2549 return (sizeof (arelent
*)
2550 * ((exec_hdr(abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
))
2553 if (asect
== obj_textsec (abfd
))
2554 return (sizeof (arelent
*)
2555 * ((exec_hdr(abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
))
2558 bfd_set_error (bfd_error_invalid_operation
);
2564 NAME(aout
,get_symtab_upper_bound
) (abfd
)
2567 if (!NAME(aout
,slurp_symbol_table
)(abfd
))
2570 return (bfd_get_symcount (abfd
)+1) * (sizeof (aout_symbol_type
*));
2575 NAME(aout
,get_lineno
) (ignore_abfd
, ignore_symbol
)
2577 asymbol
*ignore_symbol
;
2579 return (alent
*)NULL
;
2584 NAME(aout
,get_symbol_info
) (ignore_abfd
, symbol
, ret
)
2589 bfd_symbol_info (symbol
, ret
);
2591 if (ret
->type
== '?')
2593 int type_code
= aout_symbol(symbol
)->type
& 0xff;
2594 CONST
char *stab_name
= aout_stab_name(type_code
);
2595 static char buf
[10];
2597 if (stab_name
== NULL
)
2599 sprintf(buf
, "(%d)", type_code
);
2603 ret
->stab_other
= (unsigned)(aout_symbol(symbol
)->other
& 0xff);
2604 ret
->stab_desc
= (unsigned)(aout_symbol(symbol
)->desc
& 0xffff);
2605 ret
->stab_name
= stab_name
;
2611 NAME(aout
,print_symbol
) (ignore_abfd
, afile
, symbol
, how
)
2615 bfd_print_symbol_type how
;
2617 FILE *file
= (FILE *)afile
;
2620 case bfd_print_symbol_name
:
2622 fprintf(file
,"%s", symbol
->name
);
2624 case bfd_print_symbol_more
:
2625 fprintf(file
,"%4x %2x %2x",(unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2626 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2627 (unsigned)(aout_symbol(symbol
)->type
));
2629 case bfd_print_symbol_all
:
2631 CONST
char *section_name
= symbol
->section
->name
;
2634 bfd_print_symbol_vandf((PTR
)file
,symbol
);
2636 fprintf(file
," %-5s %04x %02x %02x",
2638 (unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2639 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2640 (unsigned)(aout_symbol(symbol
)->type
& 0xff));
2642 fprintf(file
," %s", symbol
->name
);
2649 provided a BFD, a section and an offset into the section, calculate
2650 and return the name of the source file and the line nearest to the
2655 NAME(aout
,find_nearest_line
)
2656 (abfd
, section
, symbols
, offset
, filename_ptr
, functionname_ptr
, line_ptr
)
2661 CONST
char **filename_ptr
;
2662 CONST
char **functionname_ptr
;
2663 unsigned int *line_ptr
;
2665 /* Run down the file looking for the filename, function and linenumber */
2667 static char buffer
[100];
2668 static char filename_buffer
[200];
2669 CONST
char *directory_name
= NULL
;
2670 CONST
char *main_file_name
= NULL
;
2671 CONST
char *current_file_name
= NULL
;
2672 CONST
char *line_file_name
= NULL
; /* Value of current_file_name at line number. */
2673 bfd_vma high_line_vma
= ~0;
2674 bfd_vma low_func_vma
= 0;
2676 *filename_ptr
= abfd
->filename
;
2677 *functionname_ptr
= 0;
2679 if (symbols
!= (asymbol
**)NULL
) {
2680 for (p
= symbols
; *p
; p
++) {
2681 aout_symbol_type
*q
= (aout_symbol_type
*)(*p
);
2685 main_file_name
= current_file_name
= q
->symbol
.name
;
2686 /* Look ahead to next symbol to check if that too is an N_SO. */
2690 q
= (aout_symbol_type
*)(*p
);
2691 if (q
->type
!= (int)N_SO
)
2694 /* Found a second N_SO First is directory; second is filename. */
2695 directory_name
= current_file_name
;
2696 main_file_name
= current_file_name
= q
->symbol
.name
;
2697 if (obj_textsec(abfd
) != section
)
2701 current_file_name
= q
->symbol
.name
;
2708 /* We'll keep this if it resolves nearer than the one we have already */
2709 if (q
->symbol
.value
>= offset
&&
2710 q
->symbol
.value
< high_line_vma
) {
2711 *line_ptr
= q
->desc
;
2712 high_line_vma
= q
->symbol
.value
;
2713 line_file_name
= current_file_name
;
2718 /* We'll keep this if it is nearer than the one we have already */
2719 if (q
->symbol
.value
>= low_func_vma
&&
2720 q
->symbol
.value
<= offset
) {
2721 low_func_vma
= q
->symbol
.value
;
2722 func
= (asymbol
*)q
;
2724 if (*line_ptr
&& func
) {
2725 CONST
char *function
= func
->name
;
2728 /* The caller expects a symbol name. We actually have a
2729 function name, without the leading underscore. Put the
2730 underscore back in, so that the caller gets a symbol
2732 if (bfd_get_symbol_leading_char (abfd
) == '\0')
2733 strncpy (buffer
, function
, sizeof (buffer
) - 1);
2736 buffer
[0] = bfd_get_symbol_leading_char (abfd
);
2737 strncpy (buffer
+ 1, function
, sizeof (buffer
) - 2);
2739 buffer
[sizeof(buffer
)-1] = 0;
2740 /* Have to remove : stuff */
2741 p
= strchr(buffer
,':');
2742 if (p
!= NULL
) { *p
= '\0'; }
2743 *functionname_ptr
= buffer
;
2755 main_file_name
= line_file_name
;
2756 if (main_file_name
) {
2757 if (main_file_name
[0] == '/' || directory_name
== NULL
)
2758 *filename_ptr
= main_file_name
;
2760 sprintf(filename_buffer
, "%.140s%.50s",
2761 directory_name
, main_file_name
);
2762 *filename_ptr
= filename_buffer
;
2771 NAME(aout
,sizeof_headers
) (abfd
, execable
)
2775 return adata(abfd
).exec_bytes_size
;
2778 /* Free all information we have cached for this BFD. We can always
2779 read it again later if we need it. */
2782 NAME(aout
,bfd_free_cached_info
) (abfd
)
2787 if (bfd_get_format (abfd
) != bfd_object
)
2790 #define FREE(x) if (x != NULL) { free (x); x = NULL; }
2791 FREE (obj_aout_symbols (abfd
));
2792 FREE (obj_aout_external_syms (abfd
));
2793 FREE (obj_aout_external_strings (abfd
));
2794 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
2795 FREE (o
->relocation
);
2801 /* a.out link code. */
2803 /* a.out linker hash table entries. */
2805 struct aout_link_hash_entry
2807 struct bfd_link_hash_entry root
;
2808 /* Symbol index in output file. */
2812 /* a.out linker hash table. */
2814 struct aout_link_hash_table
2816 struct bfd_link_hash_table root
;
2819 static struct bfd_hash_entry
*aout_link_hash_newfunc
2820 PARAMS ((struct bfd_hash_entry
*entry
,
2821 struct bfd_hash_table
*table
,
2822 const char *string
));
2823 static boolean aout_link_add_object_symbols
2824 PARAMS ((bfd
*, struct bfd_link_info
*));
2825 static boolean aout_link_check_archive_element
2826 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
2827 static boolean aout_link_free_symbols
PARAMS ((bfd
*));
2828 static boolean aout_link_check_ar_symbols
2829 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*pneeded
));
2830 static boolean aout_link_add_symbols
2831 PARAMS ((bfd
*, struct bfd_link_info
*));
2833 /* Routine to create an entry in an a.out link hash table. */
2835 static struct bfd_hash_entry
*
2836 aout_link_hash_newfunc (entry
, table
, string
)
2837 struct bfd_hash_entry
*entry
;
2838 struct bfd_hash_table
*table
;
2841 struct aout_link_hash_entry
*ret
= (struct aout_link_hash_entry
*) entry
;
2843 /* Allocate the structure if it has not already been allocated by a
2845 if (ret
== (struct aout_link_hash_entry
*) NULL
)
2846 ret
= ((struct aout_link_hash_entry
*)
2847 bfd_hash_allocate (table
, sizeof (struct aout_link_hash_entry
)));
2848 if (ret
== (struct aout_link_hash_entry
*) NULL
)
2850 bfd_set_error (bfd_error_no_memory
);
2851 return (struct bfd_hash_entry
*) ret
;
2854 /* Call the allocation method of the superclass. */
2855 ret
= ((struct aout_link_hash_entry
*)
2856 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2859 /* Set local fields. */
2862 return (struct bfd_hash_entry
*) ret
;
2865 /* Create an a.out link hash table. */
2867 struct bfd_link_hash_table
*
2868 NAME(aout
,link_hash_table_create
) (abfd
)
2871 struct aout_link_hash_table
*ret
;
2873 ret
= ((struct aout_link_hash_table
*)
2874 malloc (sizeof (struct aout_link_hash_table
)));
2875 if (ret
== (struct aout_link_hash_table
*) NULL
)
2877 bfd_set_error (bfd_error_no_memory
);
2878 return (struct bfd_link_hash_table
*) NULL
;
2880 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
2881 aout_link_hash_newfunc
))
2884 return (struct bfd_link_hash_table
*) NULL
;
2889 /* Look up an entry in an a.out link hash table. */
2891 #define aout_link_hash_lookup(table, string, create, copy, follow) \
2892 ((struct aout_link_hash_entry *) \
2893 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
2895 /* Traverse an a.out link hash table. */
2897 #define aout_link_hash_traverse(table, func, info) \
2898 (bfd_link_hash_traverse \
2900 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
2903 /* Get the a.out link hash table from the info structure. This is
2906 #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash))
2908 /* Given an a.out BFD, add symbols to the global hash table as
2912 NAME(aout
,link_add_symbols
) (abfd
, info
)
2914 struct bfd_link_info
*info
;
2916 switch (bfd_get_format (abfd
))
2919 return aout_link_add_object_symbols (abfd
, info
);
2921 return _bfd_generic_link_add_archive_symbols
2922 (abfd
, info
, aout_link_check_archive_element
);
2924 bfd_set_error (bfd_error_wrong_format
);
2929 /* Add symbols from an a.out object file. */
2932 aout_link_add_object_symbols (abfd
, info
)
2934 struct bfd_link_info
*info
;
2936 if (! aout_get_external_symbols (abfd
))
2938 if (! aout_link_add_symbols (abfd
, info
))
2940 if (! info
->keep_memory
)
2942 if (! aout_link_free_symbols (abfd
))
2948 /* Check a single archive element to see if we need to include it in
2949 the link. *PNEEDED is set according to whether this element is
2950 needed in the link or not. This is called from
2951 _bfd_generic_link_add_archive_symbols. */
2954 aout_link_check_archive_element (abfd
, info
, pneeded
)
2956 struct bfd_link_info
*info
;
2959 if (! aout_get_external_symbols (abfd
))
2962 if (! aout_link_check_ar_symbols (abfd
, info
, pneeded
))
2967 if (! aout_link_add_symbols (abfd
, info
))
2971 /* We keep around the symbols even if we aren't going to use this
2972 object file, because we may want to reread it. This doesn't
2973 waste too much memory, because it isn't all that common to read
2974 an archive element but not need it. */
2975 if (! info
->keep_memory
)
2977 if (! aout_link_free_symbols (abfd
))
2984 /* Free up the internal symbols read from an a.out file. */
2987 aout_link_free_symbols (abfd
)
2990 if (obj_aout_external_syms (abfd
) != (struct external_nlist
*) NULL
)
2992 free ((PTR
) obj_aout_external_syms (abfd
));
2993 obj_aout_external_syms (abfd
) = (struct external_nlist
*) NULL
;
2995 if (obj_aout_external_strings (abfd
) != (char *) NULL
)
2997 free ((PTR
) obj_aout_external_strings (abfd
));
2998 obj_aout_external_strings (abfd
) = (char *) NULL
;
3003 /* Look through the internal symbols to see if this object file should
3004 be included in the link. We should include this object file if it
3005 defines any symbols which are currently undefined. If this object
3006 file defines a common symbol, then we may adjust the size of the
3007 known symbol but we do not include the object file in the link
3008 (unless there is some other reason to include it). */
3011 aout_link_check_ar_symbols (abfd
, info
, pneeded
)
3013 struct bfd_link_info
*info
;
3016 register struct external_nlist
*p
;
3017 struct external_nlist
*pend
;
3022 /* Look through all the symbols. */
3023 p
= obj_aout_external_syms (abfd
);
3024 pend
= p
+ obj_aout_external_sym_count (abfd
);
3025 strings
= obj_aout_external_strings (abfd
);
3026 for (; p
< pend
; p
++)
3028 int type
= bfd_h_get_8 (abfd
, p
->e_type
);
3030 struct bfd_link_hash_entry
*h
;
3032 /* Ignore symbols that are not externally visible. */
3033 if ((type
& N_EXT
) == 0)
3035 if (type
== N_WARNING
3041 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3042 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
3044 /* We are only interested in symbols that are currently
3045 undefined or common. */
3046 if (h
== (struct bfd_link_hash_entry
*) NULL
3047 || (h
->type
!= bfd_link_hash_undefined
3048 && h
->type
!= bfd_link_hash_common
))
3050 if (type
== (N_INDR
| N_EXT
))
3055 if (type
== (N_TEXT
| N_EXT
)
3056 || type
== (N_DATA
| N_EXT
)
3057 || type
== (N_BSS
| N_EXT
)
3058 || type
== (N_ABS
| N_EXT
)
3059 || type
== (N_INDR
| N_EXT
))
3061 /* This object file defines this symbol. We must link it
3062 in. This is true regardless of whether the current
3063 definition of the symbol is undefined or common. If the
3064 current definition is common, we have a case in which we
3065 have already seen an object file including
3067 and this object file from the archive includes
3069 In such a case we must include this object file. */
3070 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
3076 if (type
== (N_UNDF
| N_EXT
))
3080 value
= GET_WORD (abfd
, p
->e_value
);
3083 /* This symbol is common in the object from the archive
3085 if (h
->type
== bfd_link_hash_undefined
)
3089 symbfd
= h
->u
.undef
.abfd
;
3090 if (symbfd
== (bfd
*) NULL
)
3092 /* This symbol was created as undefined from
3093 outside BFD. We assume that we should link
3094 in the object file. This is done for the -u
3095 option in the linker. */
3096 if (! (*info
->callbacks
->add_archive_element
) (info
,
3103 /* Turn the current link symbol into a common
3104 symbol. It is already on the undefs list. */
3105 h
->type
= bfd_link_hash_common
;
3106 h
->u
.c
.size
= value
;
3107 h
->u
.c
.section
= bfd_make_section_old_way (symbfd
,
3112 /* Adjust the size of the common symbol if
3114 if (value
> h
->u
.c
.size
)
3115 h
->u
.c
.size
= value
;
3121 /* We do not need this object file. */
3125 /* Add all symbols from an object file to the hash table. */
3128 aout_link_add_symbols (abfd
, info
)
3130 struct bfd_link_info
*info
;
3132 bfd_size_type sym_count
;
3135 struct aout_link_hash_entry
**sym_hash
;
3136 register struct external_nlist
*p
;
3137 struct external_nlist
*pend
;
3139 sym_count
= obj_aout_external_sym_count (abfd
);
3140 strings
= obj_aout_external_strings (abfd
);
3141 if (info
->keep_memory
)
3146 /* We keep a list of the linker hash table entries that correspond
3147 to particular symbols. We could just look them up in the hash
3148 table, but keeping the list is more efficient. Perhaps this
3149 should be conditional on info->keep_memory. */
3150 sym_hash
= ((struct aout_link_hash_entry
**)
3153 * sizeof (struct aout_link_hash_entry
*))));
3156 bfd_set_error (bfd_error_no_memory
);
3159 obj_aout_sym_hashes (abfd
) = sym_hash
;
3161 p
= obj_aout_external_syms (abfd
);
3162 pend
= p
+ sym_count
;
3163 for (; p
< pend
; p
++, sym_hash
++)
3174 type
= bfd_h_get_8 (abfd
, p
->e_type
);
3176 /* Ignore debugging symbols. */
3177 if ((type
& N_STAB
) != 0)
3180 /* Ignore symbols that are not external. */
3181 if ((type
& N_EXT
) == 0
3182 && type
!= N_WARNING
3188 /* If this is an N_INDR symbol we must skip the next entry,
3189 which is the symbol to indirect to (actually, an N_INDR
3190 symbol without N_EXT set is pretty useless). */
3199 /* Ignore N_FN symbols (these appear to have N_EXT set). */
3203 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3204 value
= GET_WORD (abfd
, p
->e_value
);
3211 case N_UNDF
| N_EXT
:
3213 section
= &bfd_com_section
;
3215 section
= &bfd_und_section
;
3218 section
= &bfd_abs_section
;
3220 case N_TEXT
| N_EXT
:
3221 section
= obj_textsec (abfd
);
3222 value
-= bfd_get_section_vma (abfd
, section
);
3224 case N_DATA
| N_EXT
:
3225 section
= obj_datasec (abfd
);
3226 value
-= bfd_get_section_vma (abfd
, section
);
3229 section
= obj_bsssec (abfd
);
3230 value
-= bfd_get_section_vma (abfd
, section
);
3232 case N_INDR
| N_EXT
:
3233 /* An indirect symbol. The next symbol is the symbol
3234 which this one really is. */
3235 BFD_ASSERT (p
+ 1 < pend
);
3237 string
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3238 section
= &bfd_ind_section
;
3239 flags
|= BSF_INDIRECT
;
3241 case N_COMM
| N_EXT
:
3242 section
= &bfd_com_section
;
3244 case N_SETA
: case N_SETA
| N_EXT
:
3245 section
= &bfd_abs_section
;
3246 flags
|= BSF_CONSTRUCTOR
;
3248 case N_SETT
: case N_SETT
| N_EXT
:
3249 section
= obj_textsec (abfd
);
3250 flags
|= BSF_CONSTRUCTOR
;
3251 value
-= bfd_get_section_vma (abfd
, section
);
3253 case N_SETD
: case N_SETD
| N_EXT
:
3254 section
= obj_datasec (abfd
);
3255 flags
|= BSF_CONSTRUCTOR
;
3256 value
-= bfd_get_section_vma (abfd
, section
);
3258 case N_SETB
: case N_SETB
| N_EXT
:
3259 section
= obj_bsssec (abfd
);
3260 flags
|= BSF_CONSTRUCTOR
;
3261 value
-= bfd_get_section_vma (abfd
, section
);
3264 /* A warning symbol. The next symbol is the one to warn
3266 BFD_ASSERT (p
+ 1 < pend
);
3269 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3270 section
= &bfd_und_section
;
3271 flags
|= BSF_WARNING
;
3275 if (! (_bfd_generic_link_add_one_symbol
3276 (info
, abfd
, name
, flags
, section
, value
, string
, copy
, false,
3277 (struct bfd_link_hash_entry
**) sym_hash
)))
3280 if (type
== (N_INDR
| N_EXT
) || type
== N_WARNING
)
3287 /* During the final link step we need to pass around a bunch of
3288 information, so we do it in an instance of this structure. */
3290 struct aout_final_link_info
3292 /* General link information. */
3293 struct bfd_link_info
*info
;
3296 /* Reloc file positions. */
3297 file_ptr treloff
, dreloff
;
3298 /* File position of symbols. */
3301 struct stringtab_data strtab
;
3304 static boolean aout_link_input_bfd
3305 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
));
3306 static boolean aout_link_write_symbols
3307 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
, int *symbol_map
));
3308 static boolean aout_link_write_other_symbol
3309 PARAMS ((struct aout_link_hash_entry
*, PTR
));
3310 static boolean aout_link_input_section
3311 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3312 asection
*input_section
, file_ptr
*reloff_ptr
,
3313 bfd_size_type rel_size
, int *symbol_map
));
3314 static boolean aout_link_input_section_std
3315 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3316 asection
*input_section
, struct reloc_std_external
*,
3317 bfd_size_type rel_size
, bfd_byte
*contents
, int *symbol_map
));
3318 static boolean aout_link_input_section_ext
3319 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3320 asection
*input_section
, struct reloc_ext_external
*,
3321 bfd_size_type rel_size
, bfd_byte
*contents
, int *symbol_map
));
3322 static INLINE asection
*aout_reloc_index_to_section
3323 PARAMS ((bfd
*, int));
3324 static boolean aout_link_reloc_link_order
3325 PARAMS ((struct aout_final_link_info
*, asection
*,
3326 struct bfd_link_order
*));
3328 /* Do the final link step. This is called on the output BFD. The
3329 INFO structure should point to a list of BFDs linked through the
3330 link_next field which can be used to find each BFD which takes part
3331 in the output. Also, each section in ABFD should point to a list
3332 of bfd_link_order structures which list all the input sections for
3333 the output section. */
3336 NAME(aout
,final_link
) (abfd
, info
, callback
)
3338 struct bfd_link_info
*info
;
3339 void (*callback
) PARAMS ((bfd
*, file_ptr
*, file_ptr
*, file_ptr
*));
3341 struct aout_final_link_info aout_info
;
3343 bfd_size_type text_size
;
3345 register struct bfd_link_order
*p
;
3347 boolean have_link_order_relocs
;
3349 aout_info
.info
= info
;
3350 aout_info
.output_bfd
= abfd
;
3352 if (! info
->relocateable
)
3354 exec_hdr (abfd
)->a_trsize
= 0;
3355 exec_hdr (abfd
)->a_drsize
= 0;
3359 bfd_size_type trsize
, drsize
;
3361 /* Count up the relocation sizes. */
3364 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
3366 if (bfd_get_flavour (abfd
) == bfd_target_aout_flavour
)
3368 trsize
+= exec_hdr (sub
)->a_trsize
;
3369 drsize
+= exec_hdr (sub
)->a_drsize
;
3373 /* FIXME: We need to identify the .text and .data sections
3374 and call get_reloc_upper_bound and canonicalize_reloc to
3375 work out the number of relocs needed, and then multiply
3376 by the reloc size. */
3380 if (obj_textsec (abfd
) != (asection
*) NULL
)
3381 trsize
+= (_bfd_count_link_order_relocs (obj_textsec (abfd
)
3383 * obj_reloc_entry_size (abfd
));
3384 exec_hdr (abfd
)->a_trsize
= trsize
;
3385 if (obj_datasec (abfd
) != (asection
*) NULL
)
3386 drsize
+= (_bfd_count_link_order_relocs (obj_datasec (abfd
)
3388 * obj_reloc_entry_size (abfd
));
3389 exec_hdr (abfd
)->a_drsize
= drsize
;
3392 exec_hdr (abfd
)->a_entry
= bfd_get_start_address (abfd
);
3394 /* Adjust the section sizes and vmas according to the magic number.
3395 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3396 filepos for each section. */
3397 if (! NAME(aout
,adjust_sizes_and_vmas
) (abfd
, &text_size
, &text_end
))
3400 /* The relocation and symbol file positions differ among a.out
3401 targets. We are passed a callback routine from the backend
3402 specific code to handle this.
3403 FIXME: At this point we do not know how much space the symbol
3404 table will require. This will not work for any (nonstandard)
3405 a.out target that needs to know the symbol table size before it
3406 can compute the relocation file positions. This may or may not
3407 be the case for the hp300hpux target, for example. */
3408 (*callback
) (abfd
, &aout_info
.treloff
, &aout_info
.dreloff
,
3410 obj_textsec (abfd
)->rel_filepos
= aout_info
.treloff
;
3411 obj_datasec (abfd
)->rel_filepos
= aout_info
.dreloff
;
3412 obj_sym_filepos (abfd
) = aout_info
.symoff
;
3414 /* We keep a count of the symbols as we output them. */
3415 obj_aout_external_sym_count (abfd
) = 0;
3417 /* We accumulate the string table as we write out the symbols. */
3418 stringtab_init (&aout_info
.strtab
);
3420 /* The most time efficient way to do the link would be to read all
3421 the input object files into memory and then sort out the
3422 information into the output file. Unfortunately, that will
3423 probably use too much memory. Another method would be to step
3424 through everything that composes the text section and write it
3425 out, and then everything that composes the data section and write
3426 it out, and then write out the relocs, and then write out the
3427 symbols. Unfortunately, that requires reading stuff from each
3428 input file several times, and we will not be able to keep all the
3429 input files open simultaneously, and reopening them will be slow.
3431 What we do is basically process one input file at a time. We do
3432 everything we need to do with an input file once--copy over the
3433 section contents, handle the relocation information, and write
3434 out the symbols--and then we throw away the information we read
3435 from it. This approach requires a lot of lseeks of the output
3436 file, which is unfortunate but still faster than reopening a lot
3439 We use the output_has_begun field of the input BFDs to see
3440 whether we have already handled it. */
3441 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
3442 sub
->output_has_begun
= false;
3444 have_link_order_relocs
= false;
3445 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3447 for (p
= o
->link_order_head
;
3448 p
!= (struct bfd_link_order
*) NULL
;
3451 if (p
->type
== bfd_indirect_link_order
3452 && (bfd_get_flavour (p
->u
.indirect
.section
->owner
)
3453 == bfd_target_aout_flavour
))
3457 input_bfd
= p
->u
.indirect
.section
->owner
;
3458 if (! input_bfd
->output_has_begun
)
3460 if (! aout_link_input_bfd (&aout_info
, input_bfd
))
3462 input_bfd
->output_has_begun
= true;
3465 else if (p
->type
== bfd_section_reloc_link_order
3466 || p
->type
== bfd_symbol_reloc_link_order
)
3468 /* These are handled below. */
3469 have_link_order_relocs
= true;
3473 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3479 /* Write out any symbols that we have not already written out. */
3480 aout_link_hash_traverse (aout_hash_table (info
),
3481 aout_link_write_other_symbol
,
3484 /* Now handle any relocs we were asked to create by the linker.
3485 These did not come from any input file. We must do these after
3486 we have written out all the symbols, so that we know the symbol
3488 if (have_link_order_relocs
)
3490 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3492 for (p
= o
->link_order_head
;
3493 p
!= (struct bfd_link_order
*) NULL
;
3496 if (p
->type
== bfd_section_reloc_link_order
3497 || p
->type
== bfd_symbol_reloc_link_order
)
3499 if (! aout_link_reloc_link_order (&aout_info
, o
, p
))
3506 /* Update the header information. */
3507 abfd
->symcount
= obj_aout_external_sym_count (abfd
);
3508 exec_hdr (abfd
)->a_syms
= abfd
->symcount
* EXTERNAL_NLIST_SIZE
;
3509 obj_str_filepos (abfd
) = obj_sym_filepos (abfd
) + exec_hdr (abfd
)->a_syms
;
3510 obj_textsec (abfd
)->reloc_count
=
3511 exec_hdr (abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
);
3512 obj_datasec (abfd
)->reloc_count
=
3513 exec_hdr (abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
);
3515 /* Write out the string table. */
3516 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0)
3518 return emit_strtab (abfd
, &aout_info
.strtab
);
3521 /* Link an a.out input BFD into the output file. */
3524 aout_link_input_bfd (finfo
, input_bfd
)
3525 struct aout_final_link_info
*finfo
;
3528 bfd_size_type sym_count
;
3529 int *symbol_map
= NULL
;
3531 BFD_ASSERT (bfd_get_format (input_bfd
) == bfd_object
);
3533 /* Get the symbols. We probably have them already, unless
3534 finfo->info->keep_memory is false. */
3535 if (! aout_get_external_symbols (input_bfd
))
3538 sym_count
= obj_aout_external_sym_count (input_bfd
);
3539 symbol_map
= (int *) malloc ((size_t) sym_count
* sizeof (int));
3540 if (symbol_map
== NULL
&& sym_count
!= 0)
3542 bfd_set_error (bfd_error_no_memory
);
3546 /* Write out the symbols and get a map of the new indices. */
3547 if (! aout_link_write_symbols (finfo
, input_bfd
, symbol_map
))
3550 /* Relocate and write out the sections. */
3551 if (! aout_link_input_section (finfo
, input_bfd
,
3552 obj_textsec (input_bfd
),
3554 exec_hdr (input_bfd
)->a_trsize
,
3556 || ! aout_link_input_section (finfo
, input_bfd
,
3557 obj_datasec (input_bfd
),
3559 exec_hdr (input_bfd
)->a_drsize
,
3563 /* If we are not keeping memory, we don't need the symbols any
3564 longer. We still need them if we are keeping memory, because the
3565 strings in the hash table point into them. */
3566 if (! finfo
->info
->keep_memory
)
3568 if (! aout_link_free_symbols (input_bfd
))
3572 if (symbol_map
!= NULL
)
3576 if (symbol_map
!= NULL
)
3581 /* Adjust and write out the symbols for an a.out file. Set the new
3582 symbol indices into a symbol_map. */
3585 aout_link_write_symbols (finfo
, input_bfd
, symbol_map
)
3586 struct aout_final_link_info
*finfo
;
3591 bfd_size_type sym_count
;
3593 enum bfd_link_strip strip
;
3594 enum bfd_link_discard discard
;
3595 struct external_nlist
*output_syms
= NULL
;
3596 struct external_nlist
*outsym
;
3597 register struct external_nlist
*sym
;
3598 struct external_nlist
*sym_end
;
3599 struct aout_link_hash_entry
**sym_hash
;
3601 boolean skip_indirect
;
3603 output_bfd
= finfo
->output_bfd
;
3604 sym_count
= obj_aout_external_sym_count (input_bfd
);
3605 strings
= obj_aout_external_strings (input_bfd
);
3606 strip
= finfo
->info
->strip
;
3607 discard
= finfo
->info
->discard
;
3608 output_syms
= ((struct external_nlist
*)
3609 malloc ((size_t) (sym_count
+ 1) * EXTERNAL_NLIST_SIZE
));
3610 if (output_syms
== NULL
)
3612 bfd_set_error (bfd_error_no_memory
);
3615 outsym
= output_syms
;
3617 /* First write out a symbol for this object file, unless we are
3618 discarding such symbols. */
3619 if (strip
!= strip_all
3620 && (strip
!= strip_some
3621 || bfd_hash_lookup (finfo
->info
->keep_hash
, input_bfd
->filename
,
3622 false, false) != NULL
)
3623 && discard
!= discard_all
)
3625 bfd_h_put_8 (output_bfd
, N_TEXT
, outsym
->e_type
);
3626 bfd_h_put_8 (output_bfd
, 0, outsym
->e_other
);
3627 bfd_h_put_16 (output_bfd
, (bfd_vma
) 0, outsym
->e_desc
);
3628 PUT_WORD (output_bfd
,
3629 add_to_stringtab (output_bfd
, input_bfd
->filename
,
3632 PUT_WORD (output_bfd
,
3633 (bfd_get_section_vma (output_bfd
,
3634 obj_textsec (input_bfd
)->output_section
)
3635 + obj_textsec (input_bfd
)->output_offset
),
3637 ++obj_aout_external_sym_count (output_bfd
);
3642 skip_indirect
= false;
3643 sym
= obj_aout_external_syms (input_bfd
);
3644 sym_end
= sym
+ sym_count
;
3645 sym_hash
= obj_aout_sym_hashes (input_bfd
);
3646 for (; sym
< sym_end
; sym
++, sym_hash
++, symbol_map
++)
3650 struct aout_link_hash_entry
*h
;
3657 type
= bfd_h_get_8 (input_bfd
, sym
->e_type
);
3658 name
= strings
+ GET_WORD (input_bfd
, sym
->e_strx
);
3664 /* Pass this symbol through. It is the target of an
3665 indirect or warning symbol. */
3666 val
= GET_WORD (input_bfd
, sym
->e_value
);
3669 else if (skip_indirect
)
3671 /* Skip this symbol, which is the target of an indirect
3672 symbol that we have changed to no longer be an indirect
3674 skip_indirect
= false;
3679 struct aout_link_hash_entry
*hresolve
;
3681 /* We have saved the hash table entry for this symbol, if
3682 there is one. Note that we could just look it up again
3683 in the hash table, provided we first check that it is an
3687 /* If this is an indirect or warning symbol, then change
3688 hresolve to the base symbol. We also change *sym_hash so
3689 that the relocation routines relocate against the real
3692 if (h
!= (struct aout_link_hash_entry
*) NULL
3693 && (h
->root
.type
== bfd_link_hash_indirect
3694 || h
->root
.type
== bfd_link_hash_warning
))
3696 hresolve
= (struct aout_link_hash_entry
*) h
->root
.u
.i
.link
;
3697 while (hresolve
->root
.type
== bfd_link_hash_indirect
)
3698 hresolve
= ((struct aout_link_hash_entry
*)
3699 hresolve
->root
.u
.i
.link
);
3700 *sym_hash
= hresolve
;
3703 /* If the symbol has already been written out, skip it. */
3704 if (h
!= (struct aout_link_hash_entry
*) NULL
3707 *symbol_map
= h
->indx
;
3711 /* See if we are stripping this symbol. */
3717 case strip_debugger
:
3718 if ((type
& N_STAB
) != 0)
3722 if (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
3732 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3733 h
->root
.written
= true;
3737 /* Get the value of the symbol. */
3738 if ((type
& N_TYPE
) == N_TEXT
)
3739 symsec
= obj_textsec (input_bfd
);
3740 else if ((type
& N_TYPE
) == N_DATA
)
3741 symsec
= obj_datasec (input_bfd
);
3742 else if ((type
& N_TYPE
) == N_BSS
)
3743 symsec
= obj_bsssec (input_bfd
);
3744 else if ((type
& N_TYPE
) == N_ABS
)
3745 symsec
= &bfd_abs_section
;
3746 else if (((type
& N_TYPE
) == N_INDR
3747 && (hresolve
== (struct aout_link_hash_entry
*) NULL
3748 || (hresolve
->root
.type
!= bfd_link_hash_defined
3749 && hresolve
->root
.type
!= bfd_link_hash_common
)))
3750 || type
== N_WARNING
)
3752 /* Pass the next symbol through unchanged. The
3753 condition above for indirect symbols is so that if
3754 the indirect symbol was defined, we output it with
3755 the correct definition so the debugger will
3758 val
= GET_WORD (input_bfd
, sym
->e_value
);
3761 else if ((type
& N_STAB
) != 0)
3763 val
= GET_WORD (input_bfd
, sym
->e_value
);
3768 /* If we get here with an indirect symbol, it means that
3769 we are outputting it with a real definition. In such
3770 a case we do not want to output the next symbol,
3771 which is the target of the indirection. */
3772 if ((type
& N_TYPE
) == N_INDR
)
3773 skip_indirect
= true;
3775 /* We need to get the value from the hash table. We use
3776 hresolve so that if we have defined an indirect
3777 symbol we output the final definition. */
3778 if (h
== (struct aout_link_hash_entry
*) NULL
)
3780 else if (hresolve
->root
.type
== bfd_link_hash_defined
)
3782 asection
*input_section
;
3783 asection
*output_section
;
3785 /* This case means a common symbol which was turned
3786 into a defined symbol. */
3787 input_section
= hresolve
->root
.u
.def
.section
;
3788 output_section
= input_section
->output_section
;
3789 BFD_ASSERT (output_section
== &bfd_abs_section
3790 || output_section
->owner
== output_bfd
);
3791 val
= (hresolve
->root
.u
.def
.value
3792 + bfd_get_section_vma (output_bfd
, output_section
)
3793 + input_section
->output_offset
);
3795 /* Get the correct type based on the section. If
3796 this is a constructed set, force it to be
3797 globally visible. */
3806 if (output_section
== obj_textsec (output_bfd
))
3808 else if (output_section
== obj_datasec (output_bfd
))
3810 else if (output_section
== obj_bsssec (output_bfd
))
3815 else if (hresolve
->root
.type
== bfd_link_hash_common
)
3816 val
= hresolve
->root
.u
.c
.size
;
3822 if (symsec
!= (asection
*) NULL
)
3823 val
= (symsec
->output_section
->vma
3824 + symsec
->output_offset
3825 + (GET_WORD (input_bfd
, sym
->e_value
)
3828 /* If this is a global symbol set the written flag, and if
3829 it is a local symbol see if we should discard it. */
3830 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3832 h
->root
.written
= true;
3833 h
->indx
= obj_aout_external_sym_count (output_bfd
);
3842 if (*name
== *finfo
->info
->lprefix
3843 && (finfo
->info
->lprefix_len
== 1
3844 || strncmp (name
, finfo
->info
->lprefix
,
3845 finfo
->info
->lprefix_len
) == 0))
3860 /* Copy this symbol into the list of symbols we are going to
3862 bfd_h_put_8 (output_bfd
, type
, outsym
->e_type
);
3863 bfd_h_put_8 (output_bfd
, bfd_h_get_8 (input_bfd
, sym
->e_other
),
3865 bfd_h_put_16 (output_bfd
, bfd_h_get_16 (input_bfd
, sym
->e_desc
),
3867 if (! finfo
->info
->keep_memory
)
3869 /* name points into a string table which we are going to
3870 free. If there is a hash table entry, use that string.
3871 Otherwise, copy name into memory. */
3872 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3873 name
= (*sym_hash
)->root
.root
.string
;
3878 n
= bfd_alloc (output_bfd
, strlen (name
) + 1);
3883 PUT_WORD (output_bfd
,
3884 add_to_stringtab (output_bfd
, name
, &finfo
->strtab
),
3886 PUT_WORD (output_bfd
, val
, outsym
->e_value
);
3887 *symbol_map
= obj_aout_external_sym_count (output_bfd
);
3888 ++obj_aout_external_sym_count (output_bfd
);
3892 /* Write out the output symbols we have just constructed. */
3893 if (outsym
> output_syms
)
3895 bfd_size_type outsym_count
;
3897 if (bfd_seek (output_bfd
, finfo
->symoff
, SEEK_SET
) != 0)
3899 outsym_count
= outsym
- output_syms
;
3900 if (bfd_write ((PTR
) output_syms
, (bfd_size_type
) EXTERNAL_NLIST_SIZE
,
3901 (bfd_size_type
) outsym_count
, output_bfd
)
3902 != outsym_count
* EXTERNAL_NLIST_SIZE
)
3904 finfo
->symoff
+= outsym_count
* EXTERNAL_NLIST_SIZE
;
3907 if (output_syms
!= NULL
)
3911 if (output_syms
!= NULL
)
3916 /* Write out a symbol that was not associated with an a.out input
3920 aout_link_write_other_symbol (h
, data
)
3921 struct aout_link_hash_entry
*h
;
3924 struct aout_final_link_info
*finfo
= (struct aout_final_link_info
*) data
;
3928 struct external_nlist outsym
;
3930 if (h
->root
.written
)
3933 h
->root
.written
= true;
3935 if (finfo
->info
->strip
== strip_all
3936 || (finfo
->info
->strip
== strip_some
3937 && bfd_hash_lookup (finfo
->info
->keep_hash
, h
->root
.root
.string
,
3938 false, false) == NULL
))
3941 output_bfd
= finfo
->output_bfd
;
3943 switch (h
->root
.type
)
3946 case bfd_link_hash_new
:
3948 /* Avoid variable not initialized warnings. */
3950 case bfd_link_hash_undefined
:
3951 type
= N_UNDF
| N_EXT
;
3954 case bfd_link_hash_defined
:
3958 sec
= h
->root
.u
.def
.section
;
3959 BFD_ASSERT (sec
== &bfd_abs_section
3960 || sec
->owner
== output_bfd
);
3961 if (sec
== obj_textsec (output_bfd
))
3962 type
= N_TEXT
| N_EXT
;
3963 else if (sec
== obj_datasec (output_bfd
))
3964 type
= N_DATA
| N_EXT
;
3965 else if (sec
== obj_bsssec (output_bfd
))
3966 type
= N_BSS
| N_EXT
;
3968 type
= N_ABS
| N_EXT
;
3969 val
= (h
->root
.u
.def
.value
3970 + sec
->output_section
->vma
3971 + sec
->output_offset
);
3974 case bfd_link_hash_common
:
3975 type
= N_UNDF
| N_EXT
;
3976 val
= h
->root
.u
.c
.size
;
3978 case bfd_link_hash_indirect
:
3979 case bfd_link_hash_warning
:
3980 /* FIXME: Ignore these for now. The circumstances under which
3981 they should be written out are not clear to me. */
3985 bfd_h_put_8 (output_bfd
, type
, outsym
.e_type
);
3986 bfd_h_put_8 (output_bfd
, 0, outsym
.e_other
);
3987 bfd_h_put_16 (output_bfd
, 0, outsym
.e_desc
);
3988 PUT_WORD (output_bfd
,
3989 add_to_stringtab (output_bfd
, h
->root
.root
.string
, &finfo
->strtab
),
3991 PUT_WORD (output_bfd
, val
, outsym
.e_value
);
3993 if (bfd_seek (output_bfd
, finfo
->symoff
, SEEK_SET
) != 0
3994 || bfd_write ((PTR
) &outsym
, (bfd_size_type
) EXTERNAL_NLIST_SIZE
,
3995 (bfd_size_type
) 1, output_bfd
) != EXTERNAL_NLIST_SIZE
)
3997 /* FIXME: No way to handle errors. */
4001 finfo
->symoff
+= EXTERNAL_NLIST_SIZE
;
4002 h
->indx
= obj_aout_external_sym_count (output_bfd
);
4003 ++obj_aout_external_sym_count (output_bfd
);
4008 /* Link an a.out section into the output file. */
4011 aout_link_input_section (finfo
, input_bfd
, input_section
, reloff_ptr
,
4012 rel_size
, symbol_map
)
4013 struct aout_final_link_info
*finfo
;
4015 asection
*input_section
;
4016 file_ptr
*reloff_ptr
;
4017 bfd_size_type rel_size
;
4020 bfd_size_type input_size
;
4021 bfd_byte
*contents
= NULL
;
4024 /* Get the section contents. */
4025 input_size
= bfd_section_size (input_bfd
, input_section
);
4026 contents
= (bfd_byte
*) malloc (input_size
);
4027 if (contents
== NULL
&& input_size
!= 0)
4029 bfd_set_error (bfd_error_no_memory
);
4032 if (! bfd_get_section_contents (input_bfd
, input_section
, (PTR
) contents
,
4033 (file_ptr
) 0, input_size
))
4036 /* Read in the relocs. */
4037 relocs
= (PTR
) malloc (rel_size
);
4038 if (relocs
== NULL
&& rel_size
!= 0)
4040 bfd_set_error (bfd_error_no_memory
);
4043 if (bfd_seek (input_bfd
, input_section
->rel_filepos
, SEEK_SET
) != 0
4044 || bfd_read (relocs
, 1, rel_size
, input_bfd
) != rel_size
)
4047 /* Relocate the section contents. */
4048 if (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
)
4050 if (! aout_link_input_section_std (finfo
, input_bfd
, input_section
,
4051 (struct reloc_std_external
*) relocs
,
4052 rel_size
, contents
, symbol_map
))
4057 if (! aout_link_input_section_ext (finfo
, input_bfd
, input_section
,
4058 (struct reloc_ext_external
*) relocs
,
4059 rel_size
, contents
, symbol_map
))
4063 /* Write out the section contents. */
4064 if (! bfd_set_section_contents (finfo
->output_bfd
,
4065 input_section
->output_section
,
4067 input_section
->output_offset
,
4071 /* If we are producing relocateable output, the relocs were
4072 modified, and we now write them out. */
4073 if (finfo
->info
->relocateable
)
4075 if (bfd_seek (finfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0)
4077 if (bfd_write (relocs
, (bfd_size_type
) 1, rel_size
, finfo
->output_bfd
)
4080 *reloff_ptr
+= rel_size
;
4082 /* Assert that the relocs have not run into the symbols, and
4083 that if these are the text relocs they have not run into the
4085 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (finfo
->output_bfd
)
4086 && (reloff_ptr
!= &finfo
->treloff
4088 <= obj_datasec (finfo
->output_bfd
)->rel_filepos
)));
4093 if (contents
!= NULL
)
4099 if (contents
!= NULL
)
4104 /* Get the section corresponding to a reloc index. */
4106 static INLINE asection
*
4107 aout_reloc_index_to_section (abfd
, indx
)
4111 switch (indx
& N_TYPE
)
4114 return obj_textsec (abfd
);
4116 return obj_datasec (abfd
);
4118 return obj_bsssec (abfd
);
4121 return &bfd_abs_section
;
4127 /* Relocate an a.out section using standard a.out relocs. */
4130 aout_link_input_section_std (finfo
, input_bfd
, input_section
, relocs
,
4131 rel_size
, contents
, symbol_map
)
4132 struct aout_final_link_info
*finfo
;
4134 asection
*input_section
;
4135 struct reloc_std_external
*relocs
;
4136 bfd_size_type rel_size
;
4141 boolean relocateable
;
4142 struct external_nlist
*syms
;
4144 struct aout_link_hash_entry
**sym_hashes
;
4145 bfd_size_type reloc_count
;
4146 register struct reloc_std_external
*rel
;
4147 struct reloc_std_external
*rel_end
;
4149 output_bfd
= finfo
->output_bfd
;
4151 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
);
4152 BFD_ASSERT (input_bfd
->xvec
->header_byteorder_big_p
4153 == output_bfd
->xvec
->header_byteorder_big_p
);
4155 relocateable
= finfo
->info
->relocateable
;
4156 syms
= obj_aout_external_syms (input_bfd
);
4157 strings
= obj_aout_external_strings (input_bfd
);
4158 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4160 reloc_count
= rel_size
/ RELOC_STD_SIZE
;
4162 rel_end
= rel
+ reloc_count
;
4163 for (; rel
< rel_end
; rel
++)
4175 bfd_reloc_status_type r
;
4177 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4179 if (input_bfd
->xvec
->header_byteorder_big_p
)
4181 r_index
= ((rel
->r_index
[0] << 16)
4182 | (rel
->r_index
[1] << 8)
4184 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
4185 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
4186 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
4187 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
4188 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
4189 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
4190 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
4194 r_index
= ((rel
->r_index
[2] << 16)
4195 | (rel
->r_index
[1] << 8)
4197 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
4198 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
4199 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
4200 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
4201 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
4202 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
4203 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
4206 howto_idx
= r_length
+ 4 * r_pcrel
+ 8 * r_baserel
;
4207 BFD_ASSERT (howto_idx
< TABLE_SIZE (howto_table_std
));
4208 BFD_ASSERT (r_jmptable
== 0);
4209 BFD_ASSERT (r_relative
== 0);
4213 /* We are generating a relocateable output file, and must
4214 modify the reloc accordingly. */
4217 struct aout_link_hash_entry
*h
;
4219 /* If we know the symbol this relocation is against,
4220 convert it into a relocation against a section. This
4221 is what the native linker does. */
4222 h
= sym_hashes
[r_index
];
4223 if (h
!= (struct aout_link_hash_entry
*) NULL
4224 && h
->root
.type
== bfd_link_hash_defined
)
4226 asection
*output_section
;
4228 /* Change the r_extern value. */
4229 if (output_bfd
->xvec
->header_byteorder_big_p
)
4230 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_BIG
;
4232 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE
;
4234 /* Compute a new r_index. */
4235 output_section
= h
->root
.u
.def
.section
->output_section
;
4236 if (output_section
== obj_textsec (output_bfd
))
4238 else if (output_section
== obj_datasec (output_bfd
))
4240 else if (output_section
== obj_bsssec (output_bfd
))
4245 /* Add the symbol value and the section VMA to the
4246 addend stored in the contents. */
4247 relocation
= (h
->root
.u
.def
.value
4248 + output_section
->vma
4249 + h
->root
.u
.def
.section
->output_offset
);
4253 /* We must change r_index according to the symbol
4255 r_index
= symbol_map
[r_index
];
4261 name
= strings
+ GET_WORD (input_bfd
,
4262 syms
[r_index
].e_strx
);
4263 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4264 (finfo
->info
, name
, input_bfd
, input_section
,
4273 /* Write out the new r_index value. */
4274 if (output_bfd
->xvec
->header_byteorder_big_p
)
4276 rel
->r_index
[0] = r_index
>> 16;
4277 rel
->r_index
[1] = r_index
>> 8;
4278 rel
->r_index
[2] = r_index
;
4282 rel
->r_index
[2] = r_index
>> 16;
4283 rel
->r_index
[1] = r_index
>> 8;
4284 rel
->r_index
[0] = r_index
;
4291 /* This is a relocation against a section. We must
4292 adjust by the amount that the section moved. */
4293 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4294 relocation
= (section
->output_section
->vma
4295 + section
->output_offset
4299 /* Change the address of the relocation. */
4300 PUT_WORD (output_bfd
,
4301 r_addr
+ input_section
->output_offset
,
4304 /* Adjust a PC relative relocation by removing the reference
4305 to the original address in the section and including the
4306 reference to the new address. */
4308 relocation
-= (input_section
->output_section
->vma
4309 + input_section
->output_offset
4310 - input_section
->vma
);
4312 if (relocation
== 0)
4315 r
= _bfd_relocate_contents (howto_table_std
+ howto_idx
,
4316 input_bfd
, relocation
,
4321 /* We are generating an executable, and must do a full
4325 struct aout_link_hash_entry
*h
;
4327 h
= sym_hashes
[r_index
];
4328 if (h
!= (struct aout_link_hash_entry
*) NULL
4329 && h
->root
.type
== bfd_link_hash_defined
)
4331 relocation
= (h
->root
.u
.def
.value
4332 + h
->root
.u
.def
.section
->output_section
->vma
4333 + h
->root
.u
.def
.section
->output_offset
);
4339 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4340 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4341 (finfo
->info
, name
, input_bfd
, input_section
,
4351 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4352 relocation
= (section
->output_section
->vma
4353 + section
->output_offset
4356 relocation
+= input_section
->vma
;
4359 r
= _bfd_final_link_relocate (howto_table_std
+ howto_idx
,
4360 input_bfd
, input_section
,
4361 contents
, r_addr
, relocation
,
4365 if (r
!= bfd_reloc_ok
)
4370 case bfd_reloc_outofrange
:
4372 case bfd_reloc_overflow
:
4377 name
= strings
+ GET_WORD (input_bfd
,
4378 syms
[r_index
].e_strx
);
4383 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4384 name
= bfd_section_name (input_bfd
, s
);
4386 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4387 (finfo
->info
, name
, howto_table_std
[howto_idx
].name
,
4388 (bfd_vma
) 0, input_bfd
, input_section
, r_addr
)))
4399 /* Relocate an a.out section using extended a.out relocs. */
4402 aout_link_input_section_ext (finfo
, input_bfd
, input_section
, relocs
,
4403 rel_size
, contents
, symbol_map
)
4404 struct aout_final_link_info
*finfo
;
4406 asection
*input_section
;
4407 struct reloc_ext_external
*relocs
;
4408 bfd_size_type rel_size
;
4413 boolean relocateable
;
4414 struct external_nlist
*syms
;
4416 struct aout_link_hash_entry
**sym_hashes
;
4417 bfd_size_type reloc_count
;
4418 register struct reloc_ext_external
*rel
;
4419 struct reloc_ext_external
*rel_end
;
4421 output_bfd
= finfo
->output_bfd
;
4423 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_EXT_SIZE
);
4424 BFD_ASSERT (input_bfd
->xvec
->header_byteorder_big_p
4425 == output_bfd
->xvec
->header_byteorder_big_p
);
4427 relocateable
= finfo
->info
->relocateable
;
4428 syms
= obj_aout_external_syms (input_bfd
);
4429 strings
= obj_aout_external_strings (input_bfd
);
4430 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4432 reloc_count
= rel_size
/ RELOC_EXT_SIZE
;
4434 rel_end
= rel
+ reloc_count
;
4435 for (; rel
< rel_end
; rel
++)
4444 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4446 if (input_bfd
->xvec
->header_byteorder_big_p
)
4448 r_index
= ((rel
->r_index
[0] << 16)
4449 | (rel
->r_index
[1] << 8)
4451 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
4452 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
4453 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
4457 r_index
= ((rel
->r_index
[2] << 16)
4458 | (rel
->r_index
[1] << 8)
4460 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
4461 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
4462 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
4465 r_addend
= GET_SWORD (input_bfd
, rel
->r_addend
);
4467 BFD_ASSERT (r_type
>= 0
4468 && r_type
< TABLE_SIZE (howto_table_ext
));
4472 /* We are generating a relocateable output file, and must
4473 modify the reloc accordingly. */
4476 struct aout_link_hash_entry
*h
;
4478 /* If we know the symbol this relocation is against,
4479 convert it into a relocation against a section. This
4480 is what the native linker does. */
4481 h
= sym_hashes
[r_index
];
4482 if (h
!= (struct aout_link_hash_entry
*) NULL
4483 && h
->root
.type
== bfd_link_hash_defined
)
4485 asection
*output_section
;
4487 /* Change the r_extern value. */
4488 if (output_bfd
->xvec
->header_byteorder_big_p
)
4489 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_BIG
;
4491 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE
;
4493 /* Compute a new r_index. */
4494 output_section
= h
->root
.u
.def
.section
->output_section
;
4495 if (output_section
== obj_textsec (output_bfd
))
4497 else if (output_section
== obj_datasec (output_bfd
))
4499 else if (output_section
== obj_bsssec (output_bfd
))
4504 /* Add the symbol value and the section VMA to the
4506 relocation
= (h
->root
.u
.def
.value
4507 + output_section
->vma
4508 + h
->root
.u
.def
.section
->output_offset
);
4510 /* Now RELOCATION is the VMA of the final
4511 destination. If this is a PC relative reloc,
4512 then ADDEND is the negative of the source VMA.
4513 We want to set ADDEND to the difference between
4514 the destination VMA and the source VMA, which
4515 means we must adjust RELOCATION by the change in
4516 the source VMA. This is done below. */
4520 /* We must change r_index according to the symbol
4522 r_index
= symbol_map
[r_index
];
4529 + GET_WORD (input_bfd
, syms
[r_index
].e_strx
));
4530 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4531 (finfo
->info
, name
, input_bfd
, input_section
,
4539 /* If this is a PC relative reloc, then the addend
4540 is the negative of the source VMA. We must
4541 adjust it by the change in the source VMA. This
4545 /* Write out the new r_index value. */
4546 if (output_bfd
->xvec
->header_byteorder_big_p
)
4548 rel
->r_index
[0] = r_index
>> 16;
4549 rel
->r_index
[1] = r_index
>> 8;
4550 rel
->r_index
[2] = r_index
;
4554 rel
->r_index
[2] = r_index
>> 16;
4555 rel
->r_index
[1] = r_index
>> 8;
4556 rel
->r_index
[0] = r_index
;
4563 /* This is a relocation against a section. We must
4564 adjust by the amount that the section moved. */
4565 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4566 relocation
= (section
->output_section
->vma
4567 + section
->output_offset
4570 /* If this is a PC relative reloc, then the addend is
4571 the difference in VMA between the destination and the
4572 source. We have just adjusted for the change in VMA
4573 of the destination, so we must also adjust by the
4574 change in VMA of the source. This is done below. */
4577 /* As described above, we must always adjust a PC relative
4578 reloc by the change in VMA of the source. */
4579 if (howto_table_ext
[r_type
].pc_relative
)
4580 relocation
-= (input_section
->output_section
->vma
4581 + input_section
->output_offset
4582 - input_section
->vma
);
4584 /* Change the addend if necessary. */
4585 if (relocation
!= 0)
4586 PUT_WORD (output_bfd
, r_addend
+ relocation
, rel
->r_addend
);
4588 /* Change the address of the relocation. */
4589 PUT_WORD (output_bfd
,
4590 r_addr
+ input_section
->output_offset
,
4595 bfd_reloc_status_type r
;
4597 /* We are generating an executable, and must do a full
4601 struct aout_link_hash_entry
*h
;
4603 h
= sym_hashes
[r_index
];
4604 if (h
!= (struct aout_link_hash_entry
*) NULL
4605 && h
->root
.type
== bfd_link_hash_defined
)
4607 relocation
= (h
->root
.u
.def
.value
4608 + h
->root
.u
.def
.section
->output_section
->vma
4609 + h
->root
.u
.def
.section
->output_offset
);
4615 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4616 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4617 (finfo
->info
, name
, input_bfd
, input_section
,
4627 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4629 /* If this is a PC relative reloc, then R_ADDEND is the
4630 difference between the two vmas, or
4631 old_dest_sec + old_dest_off - (old_src_sec + old_src_off)
4633 old_dest_sec == section->vma
4635 old_src_sec == input_section->vma
4637 old_src_off == r_addr
4639 _bfd_final_link_relocate expects RELOCATION +
4640 R_ADDEND to be the VMA of the destination minus
4641 r_addr (the minus r_addr is because this relocation
4642 is not pcrel_offset, which is a bit confusing and
4643 should, perhaps, be changed), or
4646 new_dest_sec == output_section->vma + output_offset
4647 We arrange for this to happen by setting RELOCATION to
4648 new_dest_sec + old_src_sec - old_dest_sec
4650 If this is not a PC relative reloc, then R_ADDEND is
4651 simply the VMA of the destination, so we set
4652 RELOCATION to the change in the destination VMA, or
4653 new_dest_sec - old_dest_sec
4655 relocation
= (section
->output_section
->vma
4656 + section
->output_offset
4658 if (howto_table_ext
[r_type
].pc_relative
)
4659 relocation
+= input_section
->vma
;
4662 r
= _bfd_final_link_relocate (howto_table_ext
+ r_type
,
4663 input_bfd
, input_section
,
4664 contents
, r_addr
, relocation
,
4666 if (r
!= bfd_reloc_ok
)
4671 case bfd_reloc_outofrange
:
4673 case bfd_reloc_overflow
:
4678 name
= strings
+ GET_WORD (input_bfd
,
4679 syms
[r_index
].e_strx
);
4684 s
= aout_reloc_index_to_section (input_bfd
, r_index
);
4685 name
= bfd_section_name (input_bfd
, s
);
4687 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4688 (finfo
->info
, name
, howto_table_ext
[r_type
].name
,
4689 r_addend
, input_bfd
, input_section
, r_addr
)))
4701 /* Handle a link order which is supposed to generate a reloc. */
4704 aout_link_reloc_link_order (finfo
, o
, p
)
4705 struct aout_final_link_info
*finfo
;
4707 struct bfd_link_order
*p
;
4709 struct bfd_link_order_reloc
*pr
;
4712 const reloc_howto_type
*howto
;
4713 file_ptr
*reloff_ptr
;
4714 struct reloc_std_external srel
;
4715 struct reloc_ext_external erel
;
4720 if (p
->type
== bfd_section_reloc_link_order
)
4723 if (pr
->u
.section
== &bfd_abs_section
)
4724 r_index
= N_ABS
| N_EXT
;
4727 BFD_ASSERT (pr
->u
.section
->owner
== finfo
->output_bfd
);
4728 r_index
= pr
->u
.section
->target_index
;
4733 struct aout_link_hash_entry
*h
;
4735 BFD_ASSERT (p
->type
== bfd_symbol_reloc_link_order
);
4737 h
= aout_link_hash_lookup (aout_hash_table (finfo
->info
),
4738 pr
->u
.name
, false, false, true);
4739 if (h
!= (struct aout_link_hash_entry
*) NULL
4744 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4745 (finfo
->info
, pr
->u
.name
, (bfd
*) NULL
,
4746 (asection
*) NULL
, (bfd_vma
) 0)))
4752 howto
= bfd_reloc_type_lookup (finfo
->output_bfd
, pr
->reloc
);
4753 if (howto
== (const reloc_howto_type
*) NULL
)
4755 bfd_set_error (bfd_error_bad_value
);
4759 if (o
== obj_textsec (finfo
->output_bfd
))
4760 reloff_ptr
= &finfo
->treloff
;
4761 else if (o
== obj_datasec (finfo
->output_bfd
))
4762 reloff_ptr
= &finfo
->dreloff
;
4766 if (obj_reloc_entry_size (finfo
->output_bfd
) == RELOC_STD_SIZE
)
4774 r_pcrel
= howto
->pc_relative
;
4775 r_baserel
= (howto
->type
& 8) != 0;
4778 r_length
= howto
->size
;
4780 PUT_WORD (finfo
->output_bfd
, p
->offset
, srel
.r_address
);
4781 if (finfo
->output_bfd
->xvec
->header_byteorder_big_p
)
4783 srel
.r_index
[0] = r_index
>> 16;
4784 srel
.r_index
[1] = r_index
>> 8;
4785 srel
.r_index
[2] = r_index
;
4787 ((r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
4788 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
4789 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
4790 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
4791 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
4792 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
));
4796 srel
.r_index
[2] = r_index
>> 16;
4797 srel
.r_index
[1] = r_index
>> 8;
4798 srel
.r_index
[0] = r_index
;
4800 ((r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
4801 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
4802 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
4803 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
4804 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
4805 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
));
4808 rel_ptr
= (PTR
) &srel
;
4810 /* We have to write the addend into the object file, since
4811 standard a.out relocs are in place. It would be more
4812 reliable if we had the current contents of the file here,
4813 rather than assuming zeroes, but we can't read the file since
4814 it was opened using bfd_openw. */
4815 if (pr
->addend
!= 0)
4818 bfd_reloc_status_type r
;
4822 size
= bfd_get_reloc_size (howto
);
4823 buf
= (bfd_byte
*) bfd_zmalloc (size
);
4824 if (buf
== (bfd_byte
*) NULL
)
4826 bfd_set_error (bfd_error_no_memory
);
4829 r
= _bfd_relocate_contents (howto
, finfo
->output_bfd
,
4836 case bfd_reloc_outofrange
:
4838 case bfd_reloc_overflow
:
4839 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4841 (p
->type
== bfd_section_reloc_link_order
4842 ? bfd_section_name (finfo
->output_bfd
,
4845 howto
->name
, pr
->addend
, (bfd
*) NULL
,
4846 (asection
*) NULL
, (bfd_vma
) 0)))
4853 ok
= bfd_set_section_contents (finfo
->output_bfd
, o
,
4855 (file_ptr
) p
->offset
,
4864 PUT_WORD (finfo
->output_bfd
, p
->offset
, erel
.r_address
);
4866 if (finfo
->output_bfd
->xvec
->header_byteorder_big_p
)
4868 erel
.r_index
[0] = r_index
>> 16;
4869 erel
.r_index
[1] = r_index
>> 8;
4870 erel
.r_index
[2] = r_index
;
4872 ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
4873 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
4877 erel
.r_index
[2] = r_index
>> 16;
4878 erel
.r_index
[1] = r_index
>> 8;
4879 erel
.r_index
[0] = r_index
;
4881 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
4882 | (howto
->type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
4885 PUT_WORD (finfo
->output_bfd
, pr
->addend
, erel
.r_addend
);
4887 rel_ptr
= (PTR
) &erel
;
4890 if (bfd_seek (finfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0
4891 || (bfd_write (rel_ptr
, (bfd_size_type
) 1,
4892 obj_reloc_entry_size (finfo
->output_bfd
),
4894 != obj_reloc_entry_size (finfo
->output_bfd
)))
4897 *reloff_ptr
+= obj_reloc_entry_size (finfo
->output_bfd
);
4899 /* Assert that the relocs have not run into the symbols, and that n
4900 the text relocs have not run into the data relocs. */
4901 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (finfo
->output_bfd
)
4902 && (reloff_ptr
!= &finfo
->treloff
4904 <= obj_datasec (finfo
->output_bfd
)->rel_filepos
)));