1 /* BFD semi-generic back-end for a.out binaries.
2 Copyright 1990, 1991, 1992, 1993 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 */
128 #include <ansidecl.h>
133 #include "aout/aout64.h"
134 #include "aout/stab_gnu.h"
142 The file @file{aoutx.h} provides for both the @emph{standard}
143 and @emph{extended} forms of a.out relocation records.
145 The standard records contain only an
146 address, a symbol index, and a type field. The extended records
147 (used on 29ks and sparcs) also have a full integer for an
151 #define CTOR_TABLE_RELOC_IDX 2
153 #define howto_table_ext NAME(aout,ext_howto_table)
154 #define howto_table_std NAME(aout,std_howto_table)
156 reloc_howto_type howto_table_ext
[] =
158 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
159 HOWTO(RELOC_8
, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", false, 0,0x000000ff, false),
160 HOWTO(RELOC_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", false, 0,0x0000ffff, false),
161 HOWTO(RELOC_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", false, 0,0xffffffff, false),
162 HOWTO(RELOC_DISP8
, 0, 0, 8, true, 0, complain_overflow_signed
,0,"DISP8", false, 0,0x000000ff, false),
163 HOWTO(RELOC_DISP16
, 0, 1, 16, true, 0, complain_overflow_signed
,0,"DISP16", false, 0,0x0000ffff, false),
164 HOWTO(RELOC_DISP32
, 0, 2, 32, true, 0, complain_overflow_signed
,0,"DISP32", false, 0,0xffffffff, false),
165 HOWTO(RELOC_WDISP30
,2, 2, 30, true, 0, complain_overflow_signed
,0,"WDISP30", false, 0,0x3fffffff, false),
166 HOWTO(RELOC_WDISP22
,2, 2, 22, true, 0, complain_overflow_signed
,0,"WDISP22", false, 0,0x003fffff, false),
167 HOWTO(RELOC_HI22
, 10, 2, 22, false, 0, complain_overflow_bitfield
,0,"HI22", false, 0,0x003fffff, false),
168 HOWTO(RELOC_22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"22", false, 0,0x003fffff, false),
169 HOWTO(RELOC_13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"13", false, 0,0x00001fff, false),
170 HOWTO(RELOC_LO10
, 0, 2, 10, false, 0, complain_overflow_dont
,0,"LO10", false, 0,0x000003ff, false),
171 HOWTO(RELOC_SFA_BASE
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_BASE", false, 0,0xffffffff, false),
172 HOWTO(RELOC_SFA_OFF13
,0,2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_OFF13",false, 0,0xffffffff, false),
173 HOWTO(RELOC_BASE10
, 0, 2, 16, false, 0, complain_overflow_bitfield
,0,"BASE10", false, 0,0x0000ffff, false),
174 HOWTO(RELOC_BASE13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"BASE13", false, 0,0x00001fff, false),
175 HOWTO(RELOC_BASE22
, 0, 2, 0, false, 0, complain_overflow_bitfield
,0,"BASE22", false, 0,0x00000000, false),
176 HOWTO(RELOC_PC10
, 0, 2, 10, false, 0, complain_overflow_bitfield
,0,"PC10", false, 0,0x000003ff, false),
177 HOWTO(RELOC_PC22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"PC22", false, 0,0x003fffff, false),
178 HOWTO(RELOC_JMP_TBL
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"JMP_TBL", false, 0,0xffffffff, false),
179 HOWTO(RELOC_SEGOFF16
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"SEGOFF16", false, 0,0x00000000, false),
180 HOWTO(RELOC_GLOB_DAT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"GLOB_DAT", false, 0,0x00000000, false),
181 HOWTO(RELOC_JMP_SLOT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"JMP_SLOT", false, 0,0x00000000, false),
182 HOWTO(RELOC_RELATIVE
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"RELATIVE", false, 0,0x00000000, false),
185 /* Convert standard reloc records to "arelent" format (incl byte swap). */
187 reloc_howto_type howto_table_std
[] = {
188 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
189 HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", true, 0x000000ff,0x000000ff, false),
190 HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", true, 0x0000ffff,0x0000ffff, false),
191 HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", true, 0xffffffff,0xffffffff, false),
192 HOWTO( 3, 0, 4, 64, false, 0, complain_overflow_bitfield
,0,"64", true, 0xdeaddead,0xdeaddead, false),
193 HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed
, 0,"DISP8", true, 0x000000ff,0x000000ff, false),
194 HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed
, 0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
195 HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed
, 0,"DISP32", true, 0xffffffff,0xffffffff, false),
196 HOWTO( 7, 0, 4, 64, true, 0, complain_overflow_signed
, 0,"DISP64", true, 0xfeedface,0xfeedface, false),
198 HOWTO( 9, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"BASE16", false,0xffffffff,0xffffffff, false),
199 HOWTO(10, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"BASE32", false,0xffffffff,0xffffffff, false),
202 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
204 CONST
struct reloc_howto_struct
*
205 DEFUN(NAME(aout
,reloc_type_lookup
),(abfd
,code
),
207 bfd_reloc_code_real_type code
)
209 #define EXT(i,j) case i: return &howto_table_ext[j]
210 #define STD(i,j) case i: return &howto_table_std[j]
211 int ext
= obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
;
212 if (code
== BFD_RELOC_CTOR
)
213 switch (bfd_get_arch_info (abfd
)->bits_per_address
)
222 EXT (BFD_RELOC_32
, 2);
223 EXT (BFD_RELOC_HI22
, 8);
224 EXT (BFD_RELOC_LO10
, 11);
225 EXT (BFD_RELOC_32_PCREL_S2
, 6);
226 EXT (BFD_RELOC_SPARC_WDISP22
, 7);
227 default: return (CONST
struct reloc_howto_struct
*) 0;
233 STD (BFD_RELOC_16
, 1);
234 STD (BFD_RELOC_32
, 2);
235 STD (BFD_RELOC_8_PCREL
, 4);
236 STD (BFD_RELOC_16_PCREL
, 5);
237 STD (BFD_RELOC_32_PCREL
, 6);
238 STD (BFD_RELOC_16_BASEREL
, 9);
239 STD (BFD_RELOC_32_BASEREL
, 10);
240 default: return (CONST
struct reloc_howto_struct
*) 0;
246 Internal entry points
249 @file{aoutx.h} exports several routines for accessing the
250 contents of an a.out file, which are gathered and exported in
251 turn by various format specific files (eg sunos.c).
257 aout_@var{size}_swap_exec_header_in
260 void aout_@var{size}_swap_exec_header_in,
262 struct external_exec *raw_bytes,
263 struct internal_exec *execp);
266 Swap the information in an executable header @var{raw_bytes} taken
267 from a raw byte stream memory image into the internal exec header
268 structure @var{execp}.
271 #ifndef NAME_swap_exec_header_in
273 DEFUN(NAME(aout
,swap_exec_header_in
),(abfd
, raw_bytes
, execp
),
275 struct external_exec
*raw_bytes AND
276 struct internal_exec
*execp
)
278 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
280 /* The internal_exec structure has some fields that are unused in this
281 configuration (IE for i960), so ensure that all such uninitialized
282 fields are zero'd out. There are places where two of these structs
283 are memcmp'd, and thus the contents do matter. */
284 memset (execp
, 0, sizeof (struct internal_exec
));
285 /* Now fill in fields in the execp, from the bytes in the raw data. */
286 execp
->a_info
= bfd_h_get_32 (abfd
, bytes
->e_info
);
287 execp
->a_text
= GET_WORD (abfd
, bytes
->e_text
);
288 execp
->a_data
= GET_WORD (abfd
, bytes
->e_data
);
289 execp
->a_bss
= GET_WORD (abfd
, bytes
->e_bss
);
290 execp
->a_syms
= GET_WORD (abfd
, bytes
->e_syms
);
291 execp
->a_entry
= GET_WORD (abfd
, bytes
->e_entry
);
292 execp
->a_trsize
= GET_WORD (abfd
, bytes
->e_trsize
);
293 execp
->a_drsize
= GET_WORD (abfd
, bytes
->e_drsize
);
295 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
300 aout_@var{size}_swap_exec_header_out
303 void aout_@var{size}_swap_exec_header_out
305 struct internal_exec *execp,
306 struct external_exec *raw_bytes);
309 Swap the information in an internal exec header structure
310 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
313 DEFUN(NAME(aout
,swap_exec_header_out
),(abfd
, execp
, raw_bytes
),
315 struct internal_exec
*execp AND
316 struct external_exec
*raw_bytes
)
318 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
320 /* Now fill in fields in the raw data, from the fields in the exec struct. */
321 bfd_h_put_32 (abfd
, execp
->a_info
, bytes
->e_info
);
322 PUT_WORD (abfd
, execp
->a_text
, bytes
->e_text
);
323 PUT_WORD (abfd
, execp
->a_data
, bytes
->e_data
);
324 PUT_WORD (abfd
, execp
->a_bss
, bytes
->e_bss
);
325 PUT_WORD (abfd
, execp
->a_syms
, bytes
->e_syms
);
326 PUT_WORD (abfd
, execp
->a_entry
, bytes
->e_entry
);
327 PUT_WORD (abfd
, execp
->a_trsize
, bytes
->e_trsize
);
328 PUT_WORD (abfd
, execp
->a_drsize
, bytes
->e_drsize
);
335 aout_@var{size}_some_aout_object_p
338 bfd_target *aout_@var{size}_some_aout_object_p
340 bfd_target *(*callback_to_real_object_p)());
343 Some a.out variant thinks that the file open in @var{abfd}
344 checking is an a.out file. Do some more checking, and set up
345 for access if it really is. Call back to the calling
346 environment's "finish up" function just before returning, to
347 handle any last-minute setup.
351 DEFUN(NAME(aout
,some_aout_object_p
),(abfd
, execp
, callback_to_real_object_p
),
353 struct internal_exec
*execp AND
354 bfd_target
*(*callback_to_real_object_p
) PARAMS ((bfd
*)))
356 struct aout_data_struct
*rawptr
, *oldrawptr
;
359 rawptr
= (struct aout_data_struct
*) bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
360 if (rawptr
== NULL
) {
361 bfd_error
= no_memory
;
365 oldrawptr
= abfd
->tdata
.aout_data
;
366 abfd
->tdata
.aout_data
= rawptr
;
368 /* Copy the contents of the old tdata struct.
369 In particular, we want the subformat, since for hpux it was set in
370 hp300hpux.c:swap_exec_header_in and will be used in
371 hp300hpux.c:callback. */
372 if (oldrawptr
!= NULL
)
373 *abfd
->tdata
.aout_data
= *oldrawptr
;
375 abfd
->tdata
.aout_data
->a
.hdr
= &rawptr
->e
;
376 *(abfd
->tdata
.aout_data
->a
.hdr
) = *execp
; /* Copy in the internal_exec struct */
377 execp
= abfd
->tdata
.aout_data
->a
.hdr
;
379 /* Set the file flags */
380 abfd
->flags
= NO_FLAGS
;
381 if (execp
->a_drsize
|| execp
->a_trsize
)
382 abfd
->flags
|= HAS_RELOC
;
383 /* Setting of EXEC_P has been deferred to the bottom of this function */
385 abfd
->flags
|= HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
;
387 if (N_MAGIC (*execp
) == ZMAGIC
)
389 abfd
->flags
|= D_PAGED
|WP_TEXT
;
390 adata(abfd
).magic
= z_magic
;
392 else if (N_MAGIC (*execp
) == NMAGIC
)
394 abfd
->flags
|= WP_TEXT
;
395 adata(abfd
).magic
= n_magic
;
398 adata(abfd
).magic
= o_magic
;
400 bfd_get_start_address (abfd
) = execp
->a_entry
;
402 obj_aout_symbols (abfd
) = (aout_symbol_type
*)NULL
;
403 bfd_get_symcount (abfd
) = execp
->a_syms
/ sizeof (struct external_nlist
);
405 /* The default relocation entry size is that of traditional V7 Unix. */
406 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
408 /* The default symbol entry size is that of traditional Unix. */
409 obj_symbol_entry_size (abfd
) = EXTERNAL_NLIST_SIZE
;
411 obj_aout_external_syms (abfd
) = NULL
;
412 obj_aout_external_strings (abfd
) = NULL
;
413 obj_aout_sym_hashes (abfd
) = NULL
;
415 /* Create the sections. This is raunchy, but bfd_close wants to reclaim
418 obj_textsec (abfd
) = bfd_make_section_old_way (abfd
, ".text");
419 obj_datasec (abfd
) = bfd_make_section_old_way (abfd
, ".data");
420 obj_bsssec (abfd
) = bfd_make_section_old_way (abfd
, ".bss");
423 (void)bfd_make_section (abfd
, ".text");
424 (void)bfd_make_section (abfd
, ".data");
425 (void)bfd_make_section (abfd
, ".bss");
428 obj_datasec (abfd
)->_raw_size
= execp
->a_data
;
429 obj_bsssec (abfd
)->_raw_size
= execp
->a_bss
;
431 obj_textsec (abfd
)->flags
= (execp
->a_trsize
!= 0 ?
432 (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
| SEC_RELOC
) :
433 (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
));
434 obj_datasec (abfd
)->flags
= (execp
->a_drsize
!= 0 ?
435 (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_RELOC
) :
436 (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
));
437 obj_bsssec (abfd
)->flags
= SEC_ALLOC
;
439 #ifdef THIS_IS_ONLY_DOCUMENTATION
440 /* The common code can't fill in these things because they depend
441 on either the start address of the text segment, the rounding
442 up of virtual addersses between segments, or the starting file
443 position of the text segment -- all of which varies among different
444 versions of a.out. */
446 /* Call back to the format-dependent code to fill in the rest of the
447 fields and do any further cleanup. Things that should be filled
448 in by the callback: */
450 struct exec
*execp
= exec_hdr (abfd
);
452 obj_textsec (abfd
)->size
= N_TXTSIZE(*execp
);
453 obj_textsec (abfd
)->raw_size
= N_TXTSIZE(*execp
);
454 /* data and bss are already filled in since they're so standard */
456 /* The virtual memory addresses of the sections */
457 obj_textsec (abfd
)->vma
= N_TXTADDR(*execp
);
458 obj_datasec (abfd
)->vma
= N_DATADDR(*execp
);
459 obj_bsssec (abfd
)->vma
= N_BSSADDR(*execp
);
461 /* The file offsets of the sections */
462 obj_textsec (abfd
)->filepos
= N_TXTOFF(*execp
);
463 obj_datasec (abfd
)->filepos
= N_DATOFF(*execp
);
465 /* The file offsets of the relocation info */
466 obj_textsec (abfd
)->rel_filepos
= N_TRELOFF(*execp
);
467 obj_datasec (abfd
)->rel_filepos
= N_DRELOFF(*execp
);
469 /* The file offsets of the string table and symbol table. */
470 obj_str_filepos (abfd
) = N_STROFF (*execp
);
471 obj_sym_filepos (abfd
) = N_SYMOFF (*execp
);
473 /* Determine the architecture and machine type of the object file. */
474 switch (N_MACHTYPE (*exec_hdr (abfd
))) {
476 abfd
->obj_arch
= bfd_arch_obscure
;
480 adata(abfd
)->page_size
= PAGE_SIZE
;
481 adata(abfd
)->segment_size
= SEGMENT_SIZE
;
482 adata(abfd
)->exec_bytes_size
= EXEC_BYTES_SIZE
;
486 /* The architecture is encoded in various ways in various a.out variants,
487 or is not encoded at all in some of them. The relocation size depends
488 on the architecture and the a.out variant. Finally, the return value
489 is the bfd_target vector in use. If an error occurs, return zero and
490 set bfd_error to the appropriate error code.
492 Formats such as b.out, which have additional fields in the a.out
493 header, should cope with them in this callback as well. */
494 #endif /* DOCUMENTATION */
496 result
= (*callback_to_real_object_p
)(abfd
);
498 /* Now that the segment addresses have been worked out, take a better
499 guess at whether the file is executable. If the entry point
500 is within the text segment, assume it is. (This makes files
501 executable even if their entry point address is 0, as long as
502 their text starts at zero.)
504 At some point we should probably break down and stat the file and
505 declare it executable if (one of) its 'x' bits are on... */
506 if ((execp
->a_entry
>= obj_textsec(abfd
)->vma
) &&
507 (execp
->a_entry
< obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
))
508 abfd
->flags
|= EXEC_P
;
511 #if 0 /* These should be set correctly anyways. */
512 abfd
->sections
= obj_textsec (abfd
);
513 obj_textsec (abfd
)->next
= obj_datasec (abfd
);
514 obj_datasec (abfd
)->next
= obj_bsssec (abfd
);
520 abfd
->tdata
.aout_data
= oldrawptr
;
527 aout_@var{size}_mkobject
530 boolean aout_@var{size}_mkobject, (bfd *abfd);
533 Initialize BFD @var{abfd} for use with a.out files.
537 DEFUN(NAME(aout
,mkobject
),(abfd
),
540 struct aout_data_struct
*rawptr
;
542 bfd_error
= system_call_error
;
544 /* Use an intermediate variable for clarity */
545 rawptr
= (struct aout_data_struct
*)bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
547 if (rawptr
== NULL
) {
548 bfd_error
= no_memory
;
552 abfd
->tdata
.aout_data
= rawptr
;
553 exec_hdr (abfd
) = &(rawptr
->e
);
555 /* For simplicity's sake we just make all the sections right here. */
557 obj_textsec (abfd
) = (asection
*)NULL
;
558 obj_datasec (abfd
) = (asection
*)NULL
;
559 obj_bsssec (abfd
) = (asection
*)NULL
;
560 bfd_make_section (abfd
, ".text");
561 bfd_make_section (abfd
, ".data");
562 bfd_make_section (abfd
, ".bss");
563 bfd_make_section (abfd
, BFD_ABS_SECTION_NAME
);
564 bfd_make_section (abfd
, BFD_UND_SECTION_NAME
);
565 bfd_make_section (abfd
, BFD_COM_SECTION_NAME
);
573 aout_@var{size}_machine_type
576 enum machine_type aout_@var{size}_machine_type
577 (enum bfd_architecture arch,
578 unsigned long machine));
581 Keep track of machine architecture and machine type for
582 a.out's. Return the <<machine_type>> for a particular
583 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
584 and machine can't be represented in a.out format.
586 If the architecture is understood, machine type 0 (default)
587 is always understood.
591 DEFUN(NAME(aout
,machine_type
),(arch
, machine
),
592 enum bfd_architecture arch AND
593 unsigned long machine
)
595 enum machine_type arch_flags
;
597 arch_flags
= M_UNKNOWN
;
601 if (machine
== 0) arch_flags
= M_SPARC
;
606 case 0: arch_flags
= M_68010
; break;
607 case 68000: arch_flags
= M_UNKNOWN
; break;
608 case 68010: arch_flags
= M_68010
; break;
609 case 68020: arch_flags
= M_68020
; break;
610 default: arch_flags
= M_UNKNOWN
; break;
615 if (machine
== 0) arch_flags
= M_386
;
619 if (machine
== 0) arch_flags
= M_29K
;
626 case 3000: arch_flags
= M_MIPS1
; break;
629 case 6000: arch_flags
= M_MIPS2
; break;
630 default: arch_flags
= M_UNKNOWN
; break;
635 arch_flags
= M_UNKNOWN
;
643 aout_@var{size}_set_arch_mach
646 boolean aout_@var{size}_set_arch_mach,
648 enum bfd_architecture arch,
649 unsigned long machine));
652 Set the architecture and the machine of the BFD @var{abfd} to the
653 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
654 can support the architecture required.
658 DEFUN(NAME(aout
,set_arch_mach
),(abfd
, arch
, machine
),
660 enum bfd_architecture arch AND
661 unsigned long machine
)
663 if (! bfd_default_set_arch_mach (abfd
, arch
, machine
))
666 if (arch
!= bfd_arch_unknown
&&
667 NAME(aout
,machine_type
) (arch
, machine
) == M_UNKNOWN
)
668 return false; /* We can't represent this type */
670 /* Determine the size of a relocation entry */
675 obj_reloc_entry_size (abfd
) = RELOC_EXT_SIZE
;
678 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
682 return (*aout_backend_info(abfd
)->set_sizes
) (abfd
);
686 adjust_o_magic (abfd
, execp
)
688 struct internal_exec
*execp
;
690 file_ptr pos
= adata (abfd
).exec_bytes_size
;
695 obj_textsec(abfd
)->filepos
= pos
;
696 pos
+= obj_textsec(abfd
)->_raw_size
;
697 vma
+= obj_textsec(abfd
)->_raw_size
;
700 if (!obj_datasec(abfd
)->user_set_vma
)
702 #if 0 /* ?? Does alignment in the file image really matter? */
703 pad
= align_power (vma
, obj_datasec(abfd
)->alignment_power
) - vma
;
705 obj_textsec(abfd
)->_raw_size
+= pad
;
708 obj_datasec(abfd
)->vma
= vma
;
710 obj_datasec(abfd
)->filepos
= pos
;
711 pos
+= obj_datasec(abfd
)->_raw_size
;
712 vma
+= obj_datasec(abfd
)->_raw_size
;
715 if (!obj_bsssec(abfd
)->user_set_vma
)
718 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
720 obj_datasec(abfd
)->_raw_size
+= pad
;
723 obj_bsssec(abfd
)->vma
= vma
;
725 obj_bsssec(abfd
)->filepos
= pos
;
727 /* Fix up the exec header. */
728 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
729 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
730 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
731 N_SET_MAGIC (*execp
, OMAGIC
);
735 adjust_z_magic (abfd
, execp
)
737 struct internal_exec
*execp
;
739 bfd_size_type data_pad
, text_pad
;
741 CONST
struct aout_backend_data
*abdp
;
742 int ztih
; /* Nonzero if text includes exec header. */
744 abdp
= aout_backend_info (abfd
);
747 ztih
= abdp
&& abdp
->text_includes_header
;
748 obj_textsec(abfd
)->filepos
= (ztih
749 ? adata(abfd
).exec_bytes_size
750 : adata(abfd
).page_size
);
751 if (! obj_textsec(abfd
)->user_set_vma
)
752 /* ?? Do we really need to check for relocs here? */
753 obj_textsec(abfd
)->vma
= ((abfd
->flags
& HAS_RELOC
)
756 ? (abdp
->default_text_vma
757 + adata(abfd
).exec_bytes_size
)
758 : abdp
->default_text_vma
));
759 /* Could take strange alignment of text section into account here? */
761 /* Find start of data. */
762 text_end
= obj_textsec(abfd
)->filepos
+ obj_textsec(abfd
)->_raw_size
;
763 text_pad
= BFD_ALIGN (text_end
, adata(abfd
).page_size
) - text_end
;
764 obj_textsec(abfd
)->_raw_size
+= text_pad
;
765 text_end
+= text_pad
;
768 if (!obj_datasec(abfd
)->user_set_vma
)
771 vma
= obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
;
772 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
774 if (abdp
&& abdp
->zmagic_mapped_contiguous
)
776 text_pad
= (obj_datasec(abfd
)->vma
777 - obj_textsec(abfd
)->vma
778 - obj_textsec(abfd
)->_raw_size
);
779 obj_textsec(abfd
)->_raw_size
+= text_pad
;
781 obj_datasec(abfd
)->filepos
= (obj_textsec(abfd
)->filepos
782 + obj_textsec(abfd
)->_raw_size
);
784 /* Fix up exec header while we're at it. */
785 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
786 if (ztih
&& (!abdp
|| (abdp
&& !abdp
->exec_header_not_counted
)))
787 execp
->a_text
+= adata(abfd
).exec_bytes_size
;
788 N_SET_MAGIC (*execp
, ZMAGIC
);
789 /* Spec says data section should be rounded up to page boundary. */
790 /* If extra space in page is left after data section, fudge data
791 in the header so that the bss section looks smaller by that
792 amount. We'll start the bss section there, and lie to the OS. */
793 obj_datasec(abfd
)->_raw_size
794 = align_power (obj_datasec(abfd
)->_raw_size
,
795 obj_bsssec(abfd
)->alignment_power
);
796 execp
->a_data
= BFD_ALIGN (obj_datasec(abfd
)->_raw_size
,
797 adata(abfd
).page_size
);
798 data_pad
= execp
->a_data
- obj_datasec(abfd
)->_raw_size
;
801 if (!obj_bsssec(abfd
)->user_set_vma
)
802 obj_bsssec(abfd
)->vma
= (obj_datasec(abfd
)->vma
803 + obj_datasec(abfd
)->_raw_size
);
804 execp
->a_bss
= (data_pad
> obj_bsssec(abfd
)->_raw_size
) ? 0 :
805 obj_bsssec(abfd
)->_raw_size
- data_pad
;
809 adjust_n_magic (abfd
, execp
)
811 struct internal_exec
*execp
;
813 file_ptr pos
= adata(abfd
).exec_bytes_size
;
818 obj_textsec(abfd
)->filepos
= pos
;
819 if (!obj_textsec(abfd
)->user_set_vma
)
820 obj_textsec(abfd
)->vma
= vma
;
822 vma
= obj_textsec(abfd
)->vma
;
823 pos
+= obj_textsec(abfd
)->_raw_size
;
824 vma
+= obj_textsec(abfd
)->_raw_size
;
827 obj_datasec(abfd
)->filepos
= pos
;
828 if (!obj_datasec(abfd
)->user_set_vma
)
829 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
830 vma
= obj_datasec(abfd
)->vma
;
832 /* Since BSS follows data immediately, see if it needs alignment. */
833 vma
+= obj_datasec(abfd
)->_raw_size
;
834 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
835 obj_datasec(abfd
)->_raw_size
+= pad
;
836 pos
+= obj_datasec(abfd
)->_raw_size
;
839 if (!obj_bsssec(abfd
)->user_set_vma
)
840 obj_bsssec(abfd
)->vma
= vma
;
842 vma
= obj_bsssec(abfd
)->vma
;
844 /* Fix up exec header. */
845 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
846 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
847 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
848 N_SET_MAGIC (*execp
, NMAGIC
);
852 DEFUN (NAME(aout
,adjust_sizes_and_vmas
), (abfd
, text_size
, text_end
),
853 bfd
*abfd AND bfd_size_type
*text_size AND file_ptr
*text_end
)
855 struct internal_exec
*execp
= exec_hdr (abfd
);
857 if ((obj_textsec (abfd
) == NULL
) || (obj_datasec (abfd
) == NULL
))
859 bfd_error
= invalid_operation
;
862 if (adata(abfd
).magic
!= undecided_magic
) return true;
864 obj_textsec(abfd
)->_raw_size
=
865 align_power(obj_textsec(abfd
)->_raw_size
,
866 obj_textsec(abfd
)->alignment_power
);
868 *text_size
= obj_textsec (abfd
)->_raw_size
;
869 /* Rule (heuristic) for when to pad to a new page. Note that there
870 are (at least) two ways demand-paged (ZMAGIC) files have been
871 handled. Most Berkeley-based systems start the text segment at
872 (PAGE_SIZE). However, newer versions of SUNOS start the text
873 segment right after the exec header; the latter is counted in the
874 text segment size, and is paged in by the kernel with the rest of
877 /* This perhaps isn't the right way to do this, but made it simpler for me
878 to understand enough to implement it. Better would probably be to go
879 right from BFD flags to alignment/positioning characteristics. But the
880 old code was sloppy enough about handling the flags, and had enough
881 other magic, that it was a little hard for me to understand. I think
882 I understand it better now, but I haven't time to do the cleanup this
885 if (abfd
->flags
& D_PAGED
)
886 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
887 /* @@ What about QMAGIC? */
888 adata(abfd
).magic
= z_magic
;
889 else if (abfd
->flags
& WP_TEXT
)
890 adata(abfd
).magic
= n_magic
;
892 adata(abfd
).magic
= o_magic
;
894 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
896 fprintf (stderr
, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
898 switch (adata(abfd
).magic
) {
899 case n_magic
: str
= "NMAGIC"; break;
900 case o_magic
: str
= "OMAGIC"; break;
901 case z_magic
: str
= "ZMAGIC"; break;
906 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
,
907 obj_textsec(abfd
)->alignment_power
,
908 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
,
909 obj_datasec(abfd
)->alignment_power
,
910 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
,
911 obj_bsssec(abfd
)->alignment_power
);
915 switch (adata(abfd
).magic
)
918 adjust_o_magic (abfd
, execp
);
921 adjust_z_magic (abfd
, execp
);
924 adjust_n_magic (abfd
, execp
);
930 #ifdef BFD_AOUT_DEBUG
931 fprintf (stderr
, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
932 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
,
933 obj_textsec(abfd
)->filepos
,
934 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
,
935 obj_datasec(abfd
)->filepos
,
936 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
);
944 aout_@var{size}_new_section_hook
947 boolean aout_@var{size}_new_section_hook,
952 Called by the BFD in response to a @code{bfd_make_section}
956 DEFUN(NAME(aout
,new_section_hook
),(abfd
, newsect
),
960 /* align to double at least */
961 newsect
->alignment_power
= bfd_get_arch_info(abfd
)->section_align_power
;
964 if (bfd_get_format (abfd
) == bfd_object
)
966 if (obj_textsec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".text")) {
967 obj_textsec(abfd
)= newsect
;
968 newsect
->target_index
= N_TEXT
| N_EXT
;
972 if (obj_datasec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".data")) {
973 obj_datasec(abfd
) = newsect
;
974 newsect
->target_index
= N_DATA
| N_EXT
;
978 if (obj_bsssec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".bss")) {
979 obj_bsssec(abfd
) = newsect
;
980 newsect
->target_index
= N_BSS
| N_EXT
;
986 /* We allow more than three sections internally */
991 DEFUN(NAME(aout
,set_section_contents
),(abfd
, section
, location
, offset
, count
),
999 bfd_size_type text_size
;
1001 if (abfd
->output_has_begun
== false)
1003 if (NAME(aout
,adjust_sizes_and_vmas
) (abfd
,
1005 &text_end
) == false)
1009 /* regardless, once we know what we're doing, we might as well get going */
1010 if (section
!= obj_bsssec(abfd
))
1012 bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
);
1015 return (bfd_write ((PTR
)location
, 1, count
, abfd
) == count
) ?
1023 /* Classify stabs symbols */
1025 #define sym_in_text_section(sym) \
1026 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
1028 #define sym_in_data_section(sym) \
1029 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
1031 #define sym_in_bss_section(sym) \
1032 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
1034 /* Symbol is undefined if type is N_UNDF|N_EXT and if it has
1035 zero in the "value" field. Nonzeroes there are fortrancommon
1037 #define sym_is_undefined(sym) \
1038 ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0)
1040 /* Symbol is a global definition if N_EXT is on and if it has
1041 a nonzero type field. */
1042 #define sym_is_global_defn(sym) \
1043 (((sym)->type & N_EXT) && (sym)->type & N_TYPE)
1045 /* Symbol is debugger info if any bits outside N_TYPE or N_EXT
1047 #define sym_is_debugger_info(sym) \
1048 (((sym)->type & ~(N_EXT | N_TYPE)) || (sym)->type == N_FN)
1050 #define sym_is_fortrancommon(sym) \
1051 (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0)
1053 /* Symbol is absolute if it has N_ABS set */
1054 #define sym_is_absolute(sym) \
1055 (((sym)->type & N_TYPE)== N_ABS)
1058 #define sym_is_indirect(sym) \
1059 (((sym)->type & N_ABS)== N_ABS)
1061 /* Only in their own functions for ease of debugging; when sym flags have
1062 stabilised these should be inlined into their (single) caller */
1065 DEFUN (translate_from_native_sym_flags
, (sym_pointer
, cache_ptr
, abfd
),
1066 struct external_nlist
*sym_pointer AND
1067 aout_symbol_type
* cache_ptr AND
1070 cache_ptr
->symbol
.section
= 0;
1071 switch (cache_ptr
->type
& N_TYPE
)
1078 char *copy
= bfd_alloc (abfd
, strlen (cache_ptr
->symbol
.name
) + 1);
1080 asection
*into_section
;
1082 arelent_chain
*reloc
= (arelent_chain
*) bfd_alloc (abfd
, sizeof (arelent_chain
));
1083 strcpy (copy
, cache_ptr
->symbol
.name
);
1085 /* Make sure that this bfd has a section with the right contructor
1087 section
= bfd_get_section_by_name (abfd
, copy
);
1089 section
= bfd_make_section (abfd
, copy
);
1091 /* Build a relocation entry for the constructor */
1092 switch ((cache_ptr
->type
& N_TYPE
))
1095 into_section
= &bfd_abs_section
;
1096 cache_ptr
->type
= N_ABS
;
1099 into_section
= (asection
*) obj_textsec (abfd
);
1100 cache_ptr
->type
= N_TEXT
;
1103 into_section
= (asection
*) obj_datasec (abfd
);
1104 cache_ptr
->type
= N_DATA
;
1107 into_section
= (asection
*) obj_bsssec (abfd
);
1108 cache_ptr
->type
= N_BSS
;
1114 /* Build a relocation pointing into the constuctor section
1115 pointing at the symbol in the set vector specified */
1117 reloc
->relent
.addend
= cache_ptr
->symbol
.value
;
1118 cache_ptr
->symbol
.section
= into_section
->symbol
->section
;
1119 reloc
->relent
.sym_ptr_ptr
= into_section
->symbol_ptr_ptr
;
1122 /* We modify the symbol to belong to a section depending upon the
1123 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
1124 really care, and add to the size of the section to contain a
1125 pointer to the symbol. Build a reloc entry to relocate to this
1126 symbol attached to this section. */
1128 section
->flags
= SEC_CONSTRUCTOR
;
1131 section
->reloc_count
++;
1132 section
->alignment_power
= 2;
1134 reloc
->next
= section
->constructor_chain
;
1135 section
->constructor_chain
= reloc
;
1136 reloc
->relent
.address
= section
->_raw_size
;
1137 section
->_raw_size
+= sizeof (int *);
1140 = (obj_reloc_entry_size(abfd
) == RELOC_EXT_SIZE
1141 ? howto_table_ext
: howto_table_std
)
1142 + CTOR_TABLE_RELOC_IDX
;
1143 cache_ptr
->symbol
.flags
|= BSF_CONSTRUCTOR
;
1147 if (cache_ptr
->type
== N_WARNING
)
1149 /* This symbol is the text of a warning message, the next symbol
1150 is the symbol to associate the warning with */
1151 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_WARNING
;
1153 /* @@ Stuffing pointers into integers is a no-no.
1154 We can usually get away with it if the integer is
1155 large enough though. */
1156 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1158 cache_ptr
->symbol
.value
= (bfd_vma
) ((cache_ptr
+ 1));
1160 /* We furgle with the next symbol in place.
1161 We don't want it to be undefined, we'll trample the type */
1162 (sym_pointer
+ 1)->e_type
[0] = 0xff;
1165 if ((cache_ptr
->type
| N_EXT
) == (N_INDR
| N_EXT
))
1167 /* Two symbols in a row for an INDR message. The first symbol
1168 contains the name we will match, the second symbol contains
1169 the name the first name is translated into. It is supplied to
1170 us undefined. This is good, since we want to pull in any files
1172 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_INDIRECT
;
1174 /* @@ Stuffing pointers into integers is a no-no.
1175 We can usually get away with it if the integer is
1176 large enough though. */
1177 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1180 cache_ptr
->symbol
.value
= (bfd_vma
) ((cache_ptr
+ 1));
1181 cache_ptr
->symbol
.section
= &bfd_ind_section
;
1184 else if (sym_is_debugger_info (cache_ptr
))
1186 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
;
1187 /* Work out the section correct for this symbol */
1188 switch (cache_ptr
->type
& N_TYPE
)
1192 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1193 cache_ptr
->symbol
.value
-= obj_textsec (abfd
)->vma
;
1196 cache_ptr
->symbol
.value
-= obj_datasec (abfd
)->vma
;
1197 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1200 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1201 cache_ptr
->symbol
.value
-= obj_bsssec (abfd
)->vma
;
1205 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1212 if (sym_is_fortrancommon (cache_ptr
))
1214 cache_ptr
->symbol
.flags
= 0;
1215 cache_ptr
->symbol
.section
= &bfd_com_section
;
1223 /* In a.out, the value of a symbol is always relative to the
1224 * start of the file, if this is a data symbol we'll subtract
1225 * the size of the text section to get the section relative
1226 * value. If this is a bss symbol (which would be strange)
1227 * we'll subtract the size of the previous two sections
1228 * to find the section relative address.
1231 if (sym_in_text_section (cache_ptr
))
1233 cache_ptr
->symbol
.value
-= obj_textsec (abfd
)->vma
;
1234 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1236 else if (sym_in_data_section (cache_ptr
))
1238 cache_ptr
->symbol
.value
-= obj_datasec (abfd
)->vma
;
1239 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1241 else if (sym_in_bss_section (cache_ptr
))
1243 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1244 cache_ptr
->symbol
.value
-= obj_bsssec (abfd
)->vma
;
1246 else if (sym_is_undefined (cache_ptr
))
1248 cache_ptr
->symbol
.flags
= 0;
1249 cache_ptr
->symbol
.section
= &bfd_und_section
;
1251 else if (sym_is_absolute (cache_ptr
))
1253 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1256 if (sym_is_global_defn (cache_ptr
))
1258 cache_ptr
->symbol
.flags
= BSF_GLOBAL
| BSF_EXPORT
;
1262 cache_ptr
->symbol
.flags
= BSF_LOCAL
;
1266 if (cache_ptr
->symbol
.section
== 0)
1273 DEFUN(translate_to_native_sym_flags
,(sym_pointer
, cache_ptr
, abfd
),
1274 struct external_nlist
*sym_pointer AND
1275 asymbol
*cache_ptr AND
1278 bfd_vma value
= cache_ptr
->value
;
1280 /* mask out any existing type bits in case copying from one section
1282 sym_pointer
->e_type
[0] &= ~N_TYPE
;
1285 /* We attempt to order these tests by decreasing frequency of success,
1286 according to tcov when linking the linker. */
1287 if (bfd_get_output_section(cache_ptr
) == &bfd_abs_section
) {
1288 sym_pointer
->e_type
[0] |= N_ABS
;
1290 else if (bfd_get_output_section(cache_ptr
) == obj_textsec (abfd
)) {
1291 sym_pointer
->e_type
[0] |= N_TEXT
;
1293 else if (bfd_get_output_section(cache_ptr
) == obj_datasec (abfd
)) {
1294 sym_pointer
->e_type
[0] |= N_DATA
;
1296 else if (bfd_get_output_section(cache_ptr
) == obj_bsssec (abfd
)) {
1297 sym_pointer
->e_type
[0] |= N_BSS
;
1299 else if (bfd_get_output_section(cache_ptr
) == &bfd_und_section
)
1301 sym_pointer
->e_type
[0] = (N_UNDF
| N_EXT
);
1303 else if (bfd_get_output_section(cache_ptr
) == &bfd_ind_section
)
1305 sym_pointer
->e_type
[0] = N_INDR
;
1307 else if (bfd_is_com_section (bfd_get_output_section (cache_ptr
))) {
1308 sym_pointer
->e_type
[0] = (N_UNDF
| N_EXT
);
1311 bfd_error
= bfd_error_nonrepresentable_section
;
1314 /* Turn the symbol from section relative to absolute again */
1316 value
+= cache_ptr
->section
->output_section
->vma
+ cache_ptr
->section
->output_offset
;
1319 if (cache_ptr
->flags
& (BSF_WARNING
)) {
1320 (sym_pointer
+1)->e_type
[0] = 1;
1323 if (cache_ptr
->flags
& BSF_DEBUGGING
) {
1324 sym_pointer
->e_type
[0] = ((aout_symbol_type
*)cache_ptr
)->type
;
1326 else if (cache_ptr
->flags
& (BSF_GLOBAL
| BSF_EXPORT
)) {
1327 sym_pointer
->e_type
[0] |= N_EXT
;
1329 if (cache_ptr
->flags
& BSF_CONSTRUCTOR
) {
1330 int type
= ((aout_symbol_type
*)cache_ptr
)->type
;
1333 case N_ABS
: type
= N_SETA
; break;
1334 case N_TEXT
: type
= N_SETT
; break;
1335 case N_DATA
: type
= N_SETD
; break;
1336 case N_BSS
: type
= N_SETB
; break;
1338 sym_pointer
->e_type
[0] = type
;
1341 PUT_WORD(abfd
, value
, sym_pointer
->e_value
);
1346 /* Native-level interface to symbols. */
1348 /* We read the symbols into a buffer, which is discarded when this
1349 function exits. We read the strings into a buffer large enough to
1350 hold them all plus all the cached symbol entries. */
1353 DEFUN(NAME(aout
,make_empty_symbol
),(abfd
),
1356 aout_symbol_type
*new =
1357 (aout_symbol_type
*)bfd_zalloc (abfd
, sizeof (aout_symbol_type
));
1358 new->symbol
.the_bfd
= abfd
;
1360 return &new->symbol
;
1364 DEFUN(NAME(aout
,slurp_symbol_table
),(abfd
),
1367 bfd_size_type symbol_size
;
1368 bfd_size_type string_size
;
1369 unsigned char string_chars
[BYTES_IN_WORD
];
1370 struct external_nlist
*syms
;
1372 aout_symbol_type
*cached
;
1374 /* If there's no work to be done, don't do any */
1375 if (obj_aout_symbols (abfd
) != (aout_symbol_type
*)NULL
) return true;
1376 symbol_size
= exec_hdr(abfd
)->a_syms
;
1377 if (symbol_size
== 0)
1379 bfd_error
= no_symbols
;
1383 bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
);
1384 if (bfd_read ((PTR
)string_chars
, BYTES_IN_WORD
, 1, abfd
) != BYTES_IN_WORD
)
1386 string_size
= GET_WORD (abfd
, string_chars
);
1388 strings
=(char *) bfd_alloc(abfd
, string_size
+ 1);
1389 cached
= (aout_symbol_type
*)
1390 bfd_zalloc(abfd
, (bfd_size_type
)(bfd_get_symcount (abfd
) * sizeof(aout_symbol_type
)));
1392 /* malloc this, so we can free it if simply. The symbol caching
1393 might want to allocate onto the bfd's obstack */
1394 syms
= (struct external_nlist
*) bfd_xmalloc(symbol_size
);
1395 bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
);
1396 if (bfd_read ((PTR
)syms
, 1, symbol_size
, abfd
) != symbol_size
)
1402 bfd_release (abfd
, cached
);
1404 bfd_release (abfd
, strings
);
1408 bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
);
1409 if (bfd_read ((PTR
)strings
, 1, string_size
, abfd
) != string_size
)
1413 strings
[string_size
] = 0; /* Just in case. */
1415 /* OK, now walk the new symtable, cacheing symbol properties */
1417 register struct external_nlist
*sym_pointer
;
1418 register struct external_nlist
*sym_end
= syms
+ bfd_get_symcount (abfd
);
1419 register aout_symbol_type
*cache_ptr
= cached
;
1421 /* Run through table and copy values */
1422 for (sym_pointer
= syms
, cache_ptr
= cached
;
1423 sym_pointer
< sym_end
; sym_pointer
++, cache_ptr
++)
1425 long x
= GET_WORD(abfd
, sym_pointer
->e_strx
);
1426 cache_ptr
->symbol
.the_bfd
= abfd
;
1428 cache_ptr
->symbol
.name
= "";
1429 else if (x
>= 0 && x
< string_size
)
1430 cache_ptr
->symbol
.name
= x
+ strings
;
1434 cache_ptr
->symbol
.value
= GET_SWORD(abfd
, sym_pointer
->e_value
);
1435 cache_ptr
->desc
= bfd_h_get_16(abfd
, sym_pointer
->e_desc
);
1436 cache_ptr
->other
= bfd_h_get_8(abfd
, sym_pointer
->e_other
);
1437 cache_ptr
->type
= bfd_h_get_8(abfd
, sym_pointer
->e_type
);
1438 cache_ptr
->symbol
.udata
= 0;
1439 translate_from_native_sym_flags (sym_pointer
, cache_ptr
, abfd
);
1443 obj_aout_symbols (abfd
) = cached
;
1450 /* Possible improvements:
1451 + look for strings matching trailing substrings of other strings
1452 + better data structures? balanced trees?
1453 + smaller per-string or per-symbol data? re-use some of the symbol's
1455 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1456 construct the entire symbol table at once, we could get by with smaller
1457 amounts of VM? (What effect does that have on the string table
1459 + rip this out of here, put it into its own file in bfd or libiberty, so
1460 coff and elf can use it too. I'll work on this soon, but have more
1461 pressing tasks right now.
1463 A hash table might(?) be more efficient for handling exactly the cases that
1464 are handled now, but for trailing substring matches, I think we want to
1465 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1466 order, nor look only for exact-match or not-match. I don't think a hash
1467 table would be very useful for that, and I don't feel like fleshing out two
1468 completely different implementations. [raeburn:930419.0331EDT] */
1470 struct stringtab_entry
{
1471 /* Hash value for this string. Only useful so long as we aren't doing
1472 substring matches. */
1475 /* Next node to look at, depending on whether the hash value of the string
1476 being searched for is less than or greater than the hash value of the
1477 current node. For now, `equal to' is lumped in with `greater than', for
1478 space efficiency. It's not a common enough case to warrant another field
1479 to be used for all nodes. */
1480 struct stringtab_entry
*less
;
1481 struct stringtab_entry
*greater
;
1483 /* The string itself. */
1486 /* The index allocated for this string. */
1487 bfd_size_type index
;
1489 #ifdef GATHER_STATISTICS
1490 /* How many references have there been to this string? (Not currently used;
1491 could be dumped out for anaylsis, if anyone's interested.) */
1492 unsigned long count
;
1495 /* Next node in linked list, in suggested output order. */
1496 struct stringtab_entry
*next_to_output
;
1499 struct stringtab_data
{
1500 /* Tree of string table entries. */
1501 struct stringtab_entry
*strings
;
1503 /* Fudge factor used to center top node of tree. */
1506 /* Next index value to issue. */
1507 bfd_size_type index
;
1509 /* Index used for empty strings. Cached here because checking for them
1510 is really easy, and we can avoid searching the tree. */
1511 bfd_size_type empty_string_index
;
1513 /* These fields indicate the two ends of a singly-linked list that indicates
1514 the order strings should be written out in. Use this order, and no
1515 seeking will need to be done, so output efficiency should be maximized. */
1516 struct stringtab_entry
**end
;
1517 struct stringtab_entry
*output_order
;
1519 #ifdef GATHER_STATISTICS
1520 /* Number of strings which duplicate strings already in the table. */
1521 unsigned long duplicates
;
1523 /* Number of bytes saved by not having to write all the duplicate strings. */
1524 unsigned long bytes_saved
;
1526 /* Number of zero-length strings. Currently, these all turn into
1527 references to the null byte at the end of the first string. In some
1528 cases (possibly not all? explore this...), it should be possible to
1529 simply write out a zero index value. */
1530 unsigned long empty_strings
;
1532 /* Number of times the hash values matched but the strings were different.
1533 Note that this includes the number of times the other string(s) occurs, so
1534 there may only be two strings hashing to the same value, even if this
1535 number is very large. */
1536 unsigned long bad_hash_matches
;
1538 /* Null strings aren't counted in this one.
1539 This will probably only be nonzero if we've got an input file
1540 which was produced by `ld -r' (i.e., it's already been processed
1541 through this code). Under some operating systems, native tools
1542 may make all empty strings have the same index; but the pointer
1543 check won't catch those, because to get to that stage we'd already
1544 have to compute the checksum, which requires reading the string,
1545 so we short-circuit that case with empty_string_index above. */
1546 unsigned long pointer_matches
;
1548 /* Number of comparisons done. I figure with the algorithms in use below,
1549 the average number of comparisons done (per symbol) should be roughly
1550 log-base-2 of the number of unique strings. */
1551 unsigned long n_compares
;
1555 /* Some utility functions for the string table code. */
1557 /* For speed, only hash on the first this many bytes of strings.
1558 This number was chosen by profiling ld linking itself, with -g. */
1559 #define HASHMAXLEN 25
1561 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1563 static INLINE
unsigned int
1565 unsigned char *string
;
1566 register unsigned int len
;
1568 register unsigned int sum
= 0;
1570 if (len
> HASHMAXLEN
)
1578 HASH_CHAR (*string
++);
1584 stringtab_init (tab
)
1585 struct stringtab_data
*tab
;
1588 tab
->output_order
= 0;
1590 tab
->end
= &tab
->output_order
;
1592 /* Initial string table length includes size of length field. */
1593 tab
->index
= BYTES_IN_WORD
;
1594 tab
->empty_string_index
= -1;
1595 #ifdef GATHER_STATISTICS
1596 tab
->duplicates
= 0;
1597 tab
->empty_strings
= 0;
1598 tab
->bad_hash_matches
= 0;
1599 tab
->pointer_matches
= 0;
1600 tab
->bytes_saved
= 0;
1601 tab
->n_compares
= 0;
1606 compare (entry
, str
, hash
)
1607 struct stringtab_entry
*entry
;
1611 return hash
- entry
->hash
;
1614 #ifdef GATHER_STATISTICS
1615 /* Don't want to have to link in math library with all bfd applications... */
1616 static INLINE
double
1624 return ((d
> 1.41) ? 0.5 : 0) + n
;
1628 /* Main string table routines. */
1629 /* Returns index in string table. Whether or not this actually adds an
1630 entry into the string table should be irrelevant -- it just has to
1631 return a valid index. */
1632 static bfd_size_type
1633 add_to_stringtab (abfd
, str
, tab
)
1636 struct stringtab_data
*tab
;
1638 struct stringtab_entry
**ep
;
1639 register struct stringtab_entry
*entry
;
1640 unsigned int hashval
, len
;
1644 bfd_size_type index
;
1645 CONST bfd_size_type minus_one
= -1;
1647 #ifdef GATHER_STATISTICS
1648 tab
->empty_strings
++;
1650 index
= tab
->empty_string_index
;
1651 if (index
!= minus_one
)
1654 #ifdef GATHER_STATISTICS
1661 /* Need to find it. */
1662 entry
= tab
->strings
;
1665 index
= entry
->index
+ strlen (entry
->string
);
1666 tab
->empty_string_index
= index
;
1674 /* The hash_zero value is chosen such that the first symbol gets a value of
1675 zero. With a balanced tree, this wouldn't be very useful, but without it,
1676 we might get a more even split at the top level, instead of skewing it
1677 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1678 hashval
= hash (str
, len
) ^ tab
->hash_zero
;
1682 tab
->hash_zero
= hashval
;
1692 #ifdef GATHER_STATISTICS
1695 cmp
= compare (entry
, str
, hashval
);
1696 /* The not-equal cases are more frequent, so check them first. */
1698 ep
= &entry
->greater
;
1703 if (entry
->string
== str
)
1705 #ifdef GATHER_STATISTICS
1706 tab
->pointer_matches
++;
1710 /* Compare the first bytes to save a function call if they
1712 if (entry
->string
[0] == str
[0] && !strcmp (entry
->string
, str
))
1715 #ifdef GATHER_STATISTICS
1717 tab
->bytes_saved
+= len
+ 1;
1720 /* If we're in the linker, and the new string is from a new
1721 input file which might have already had these reductions
1722 run over it, we want to keep the new string pointer. I
1723 don't think we're likely to see any (or nearly as many,
1724 at least) cases where a later string is in the same location
1725 as an earlier one rather than this one. */
1726 entry
->string
= str
;
1727 return entry
->index
;
1729 #ifdef GATHER_STATISTICS
1730 tab
->bad_hash_matches
++;
1732 ep
= &entry
->greater
;
1736 /* If we get here, nothing that's in the table already matched.
1737 EP points to the `next' field at the end of the chain; stick a
1738 new entry on here. */
1740 entry
= (struct stringtab_entry
*)
1741 bfd_alloc_by_size_t (abfd
, sizeof (struct stringtab_entry
));
1743 entry
->less
= entry
->greater
= 0;
1744 entry
->hash
= hashval
;
1745 entry
->index
= tab
->index
;
1746 entry
->string
= str
;
1747 entry
->next_to_output
= 0;
1748 #ifdef GATHER_STATISTICS
1752 assert (*tab
->end
== 0);
1753 *(tab
->end
) = entry
;
1754 tab
->end
= &entry
->next_to_output
;
1755 assert (*tab
->end
== 0);
1758 tab
->index
+= len
+ 1;
1760 tab
->empty_string_index
= entry
->index
;
1764 return entry
->index
;
1768 emit_strtab (abfd
, tab
)
1770 struct stringtab_data
*tab
;
1772 struct stringtab_entry
*entry
;
1773 #ifdef GATHER_STATISTICS
1777 /* Be sure to put string length into correct byte ordering before writing
1779 char buffer
[BYTES_IN_WORD
];
1781 PUT_WORD (abfd
, tab
->index
, (unsigned char *) buffer
);
1782 bfd_write ((PTR
) buffer
, 1, BYTES_IN_WORD
, abfd
);
1784 for (entry
= tab
->output_order
; entry
; entry
= entry
->next_to_output
)
1786 bfd_write ((PTR
) entry
->string
, 1, strlen (entry
->string
) + 1, abfd
);
1787 #ifdef GATHER_STATISTICS
1792 #ifdef GATHER_STATISTICS
1793 /* Short form only, for now.
1794 To do: Specify output file. Conditionalize on environment? Detailed
1795 analysis if desired. */
1797 int n_syms
= bfd_get_symcount (abfd
);
1799 fprintf (stderr
, "String table data for output file:\n");
1800 fprintf (stderr
, " %8d symbols output\n", n_syms
);
1801 fprintf (stderr
, " %8d duplicate strings\n", tab
->duplicates
);
1802 fprintf (stderr
, " %8d empty strings\n", tab
->empty_strings
);
1803 fprintf (stderr
, " %8d unique strings output\n", count
);
1804 fprintf (stderr
, " %8d pointer matches\n", tab
->pointer_matches
);
1805 fprintf (stderr
, " %8d bytes saved\n", tab
->bytes_saved
);
1806 fprintf (stderr
, " %8d bad hash matches\n", tab
->bad_hash_matches
);
1807 fprintf (stderr
, " %8d hash-val comparisons\n", tab
->n_compares
);
1810 double n_compares
= tab
->n_compares
;
1811 double avg_compares
= n_compares
/ n_syms
;
1812 /* The second value here should usually be near one. */
1814 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
1815 avg_compares
, avg_compares
/ log2 (count
));
1822 generic = bfd_get_outsymbols(abfd);
1823 for (count = 0; count < bfd_get_symcount(abfd); count++)
1825 asymbol *g = *(generic++);
1829 size_t length = strlen(g->name)+1;
1830 bfd_write((PTR)g->name, 1, length, abfd);
1832 g->KEEPIT = (KEEPITTYPE) count;
1837 DEFUN(NAME(aout
,write_syms
),(abfd
),
1840 unsigned int count
;
1841 asymbol
**generic
= bfd_get_outsymbols (abfd
);
1842 struct stringtab_data strtab
;
1844 stringtab_init (&strtab
);
1846 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
1848 asymbol
*g
= generic
[count
];
1849 struct external_nlist nsp
;
1852 PUT_WORD (abfd
, add_to_stringtab (abfd
, g
->name
, &strtab
),
1853 (unsigned char *) nsp
.e_strx
);
1855 PUT_WORD (abfd
, 0, (unsigned char *)nsp
.e_strx
);
1857 if (bfd_asymbol_flavour(g
) == abfd
->xvec
->flavour
)
1859 bfd_h_put_16(abfd
, aout_symbol(g
)->desc
, nsp
.e_desc
);
1860 bfd_h_put_8(abfd
, aout_symbol(g
)->other
, nsp
.e_other
);
1861 bfd_h_put_8(abfd
, aout_symbol(g
)->type
, nsp
.e_type
);
1865 bfd_h_put_16(abfd
,0, nsp
.e_desc
);
1866 bfd_h_put_8(abfd
, 0, nsp
.e_other
);
1867 bfd_h_put_8(abfd
, 0, nsp
.e_type
);
1870 if (! translate_to_native_sym_flags (&nsp
, g
, abfd
))
1873 if (bfd_write((PTR
)&nsp
,1,EXTERNAL_NLIST_SIZE
, abfd
)
1874 != EXTERNAL_NLIST_SIZE
)
1877 /* NB: `KEEPIT' currently overlays `flags', so set this only
1878 here, at the end. */
1882 emit_strtab (abfd
, &strtab
);
1889 DEFUN(NAME(aout
,get_symtab
),(abfd
, location
),
1893 unsigned int counter
= 0;
1894 aout_symbol_type
*symbase
;
1896 if (!NAME(aout
,slurp_symbol_table
)(abfd
)) return 0;
1898 for (symbase
= obj_aout_symbols(abfd
); counter
++ < bfd_get_symcount (abfd
);)
1899 *(location
++) = (asymbol
*)( symbase
++);
1901 return bfd_get_symcount (abfd
);
1905 /* Standard reloc stuff */
1906 /* Output standard relocation information to a file in target byte order. */
1909 DEFUN(NAME(aout
,swap_std_reloc_out
),(abfd
, g
, natptr
),
1912 struct reloc_std_external
*natptr
)
1915 asymbol
*sym
= *(g
->sym_ptr_ptr
);
1917 unsigned int r_length
;
1919 int r_baserel
, r_jmptable
, r_relative
;
1920 asection
*output_section
= sym
->section
->output_section
;
1922 PUT_WORD(abfd
, g
->address
, natptr
->r_address
);
1924 r_length
= g
->howto
->size
; /* Size as a power of two */
1925 r_pcrel
= (int) g
->howto
->pc_relative
; /* Relative to PC? */
1926 /* XXX This relies on relocs coming from a.out files. */
1927 r_baserel
= (g
->howto
->type
& 8) != 0;
1928 /* r_jmptable, r_relative??? FIXME-soon */
1933 /* For a standard reloc, the addend is in the object file. */
1934 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
1937 /* name was clobbered by aout_write_syms to be symbol index */
1939 /* If this relocation is relative to a symbol then set the
1940 r_index to the symbols index, and the r_extern bit.
1942 Absolute symbols can come in in two ways, either as an offset
1943 from the abs section, or as a symbol which has an abs value.
1948 if (bfd_is_com_section (output_section
)
1949 || output_section
== &bfd_abs_section
1950 || output_section
== &bfd_und_section
)
1952 if (bfd_abs_section
.symbol
== sym
)
1954 /* Whoops, looked like an abs symbol, but is really an offset
1955 from the abs section */
1961 /* Fill in symbol */
1963 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
1969 /* Just an ordinary section */
1971 r_index
= output_section
->target_index
;
1974 /* now the fun stuff */
1975 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
1976 natptr
->r_index
[0] = r_index
>> 16;
1977 natptr
->r_index
[1] = r_index
>> 8;
1978 natptr
->r_index
[2] = r_index
;
1980 (r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
1981 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
1982 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
1983 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
1984 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
1985 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
);
1987 natptr
->r_index
[2] = r_index
>> 16;
1988 natptr
->r_index
[1] = r_index
>> 8;
1989 natptr
->r_index
[0] = r_index
;
1991 (r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
1992 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
1993 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
1994 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
1995 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
1996 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
);
2001 /* Extended stuff */
2002 /* Output extended relocation information to a file in target byte order. */
2005 DEFUN(NAME(aout
,swap_ext_reloc_out
),(abfd
, g
, natptr
),
2008 register struct reloc_ext_external
*natptr
)
2012 unsigned int r_type
;
2013 unsigned int r_addend
;
2014 asymbol
*sym
= *(g
->sym_ptr_ptr
);
2015 asection
*output_section
= sym
->section
->output_section
;
2017 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
2019 r_type
= (unsigned int) g
->howto
->type
;
2021 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
2023 /* If this relocation is relative to a symbol then set the
2024 r_index to the symbols index, and the r_extern bit.
2026 Absolute symbols can come in in two ways, either as an offset
2027 from the abs section, or as a symbol which has an abs value.
2028 check for that here. */
2030 if (bfd_is_com_section (output_section
)
2031 || output_section
== &bfd_abs_section
2032 || output_section
== &bfd_und_section
)
2034 if (bfd_abs_section
.symbol
== sym
)
2036 /* Whoops, looked like an abs symbol, but is really an offset
2037 from the abs section */
2044 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
2049 /* Just an ordinary section */
2051 r_index
= output_section
->target_index
;
2054 /* now the fun stuff */
2055 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2056 natptr
->r_index
[0] = r_index
>> 16;
2057 natptr
->r_index
[1] = r_index
>> 8;
2058 natptr
->r_index
[2] = r_index
;
2060 ((r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
2061 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_BIG
));
2063 natptr
->r_index
[2] = r_index
>> 16;
2064 natptr
->r_index
[1] = r_index
>> 8;
2065 natptr
->r_index
[0] = r_index
;
2067 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
2068 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
2071 PUT_WORD (abfd
, r_addend
, natptr
->r_addend
);
2074 /* BFD deals internally with all things based from the section they're
2075 in. so, something in 10 bytes into a text section with a base of
2076 50 would have a symbol (.text+10) and know .text vma was 50.
2078 Aout keeps all it's symbols based from zero, so the symbol would
2079 contain 60. This macro subs the base of each section from the value
2080 to give the true offset from the section */
2083 #define MOVE_ADDRESS(ad) \
2085 /* undefined symbol */ \
2086 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2087 cache_ptr->addend = ad; \
2089 /* defined, section relative. replace symbol with pointer to \
2090 symbol which points to section */ \
2091 switch (r_index) { \
2093 case N_TEXT | N_EXT: \
2094 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2095 cache_ptr->addend = ad - su->textsec->vma; \
2098 case N_DATA | N_EXT: \
2099 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2100 cache_ptr->addend = ad - su->datasec->vma; \
2103 case N_BSS | N_EXT: \
2104 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2105 cache_ptr->addend = ad - su->bsssec->vma; \
2109 case N_ABS | N_EXT: \
2110 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2111 cache_ptr->addend = ad; \
2117 DEFUN(NAME(aout
,swap_ext_reloc_in
), (abfd
, bytes
, cache_ptr
, symbols
),
2119 struct reloc_ext_external
*bytes AND
2120 arelent
*cache_ptr AND
2125 unsigned int r_type
;
2126 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2128 cache_ptr
->address
= (GET_SWORD (abfd
, bytes
->r_address
));
2130 /* now the fun stuff */
2131 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2132 r_index
= (bytes
->r_index
[0] << 16)
2133 | (bytes
->r_index
[1] << 8)
2134 | bytes
->r_index
[2];
2135 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
2136 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
2137 >> RELOC_EXT_BITS_TYPE_SH_BIG
;
2139 r_index
= (bytes
->r_index
[2] << 16)
2140 | (bytes
->r_index
[1] << 8)
2141 | bytes
->r_index
[0];
2142 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
2143 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
2144 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
;
2147 cache_ptr
->howto
= howto_table_ext
+ r_type
;
2148 MOVE_ADDRESS(GET_SWORD(abfd
, bytes
->r_addend
));
2152 DEFUN(NAME(aout
,swap_std_reloc_in
), (abfd
, bytes
, cache_ptr
, symbols
),
2154 struct reloc_std_external
*bytes AND
2155 arelent
*cache_ptr AND
2160 unsigned int r_length
;
2162 int r_baserel
, r_jmptable
, r_relative
;
2163 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2166 cache_ptr
->address
= bfd_h_get_32 (abfd
, bytes
->r_address
);
2168 /* now the fun stuff */
2169 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2170 r_index
= (bytes
->r_index
[0] << 16)
2171 | (bytes
->r_index
[1] << 8)
2172 | bytes
->r_index
[2];
2173 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
2174 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
2175 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
2176 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
2177 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
2178 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
2179 >> RELOC_STD_BITS_LENGTH_SH_BIG
;
2181 r_index
= (bytes
->r_index
[2] << 16)
2182 | (bytes
->r_index
[1] << 8)
2183 | bytes
->r_index
[0];
2184 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
2185 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
2186 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
2187 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
2188 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
2189 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
2190 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
;
2193 howto_idx
= r_length
+ 4 * r_pcrel
+ 8 * r_baserel
;
2194 BFD_ASSERT (howto_idx
< TABLE_SIZE (howto_table_std
));
2195 cache_ptr
->howto
= howto_table_std
+ howto_idx
;
2196 BFD_ASSERT (cache_ptr
->howto
->type
!= -1);
2197 BFD_ASSERT (r_jmptable
== 0);
2198 BFD_ASSERT (r_relative
== 0);
2199 /* FIXME-soon: Roll jmptable, relative bits into howto setting */
2207 DEFUN(NAME(aout
,slurp_reloc_table
),(abfd
, asect
, symbols
),
2213 bfd_size_type reloc_size
;
2215 arelent
*reloc_cache
;
2218 if (asect
->relocation
) return true;
2220 if (asect
->flags
& SEC_CONSTRUCTOR
) return true;
2222 if (asect
== obj_datasec (abfd
)) {
2223 reloc_size
= exec_hdr(abfd
)->a_drsize
;
2224 } else if (asect
== obj_textsec (abfd
)) {
2225 reloc_size
= exec_hdr(abfd
)->a_trsize
;
2227 bfd_error
= invalid_operation
;
2231 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2232 each_size
= obj_reloc_entry_size (abfd
);
2234 count
= reloc_size
/ each_size
;
2237 reloc_cache
= (arelent
*) bfd_zalloc (abfd
, (size_t)(count
* sizeof
2241 bfd_error
= no_memory
;
2245 relocs
= (PTR
) bfd_alloc (abfd
, reloc_size
);
2247 bfd_release (abfd
, reloc_cache
);
2251 if (bfd_read (relocs
, 1, reloc_size
, abfd
) != reloc_size
) {
2252 bfd_release (abfd
, relocs
);
2253 bfd_release (abfd
, reloc_cache
);
2254 bfd_error
= system_call_error
;
2258 if (each_size
== RELOC_EXT_SIZE
) {
2259 register struct reloc_ext_external
*rptr
= (struct reloc_ext_external
*) relocs
;
2260 unsigned int counter
= 0;
2261 arelent
*cache_ptr
= reloc_cache
;
2263 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++) {
2264 NAME(aout
,swap_ext_reloc_in
)(abfd
, rptr
, cache_ptr
, symbols
);
2267 register struct reloc_std_external
*rptr
= (struct reloc_std_external
*) relocs
;
2268 unsigned int counter
= 0;
2269 arelent
*cache_ptr
= reloc_cache
;
2271 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++) {
2272 NAME(aout
,swap_std_reloc_in
)(abfd
, rptr
, cache_ptr
, symbols
);
2277 bfd_release (abfd
,relocs
);
2278 asect
->relocation
= reloc_cache
;
2279 asect
->reloc_count
= count
;
2285 /* Write out a relocation section into an object file. */
2288 DEFUN(NAME(aout
,squirt_out_relocs
),(abfd
, section
),
2293 unsigned char *native
, *natptr
;
2296 unsigned int count
= section
->reloc_count
;
2299 if (count
== 0) return true;
2301 each_size
= obj_reloc_entry_size (abfd
);
2302 natsize
= each_size
* count
;
2303 native
= (unsigned char *) bfd_zalloc (abfd
, natsize
);
2305 bfd_error
= no_memory
;
2309 generic
= section
->orelocation
;
2311 if (each_size
== RELOC_EXT_SIZE
)
2313 for (natptr
= native
;
2315 --count
, natptr
+= each_size
, ++generic
)
2316 NAME(aout
,swap_ext_reloc_out
) (abfd
, *generic
, (struct reloc_ext_external
*)natptr
);
2320 for (natptr
= native
;
2322 --count
, natptr
+= each_size
, ++generic
)
2323 NAME(aout
,swap_std_reloc_out
)(abfd
, *generic
, (struct reloc_std_external
*)natptr
);
2326 if ( bfd_write ((PTR
) native
, 1, natsize
, abfd
) != natsize
) {
2327 bfd_release(abfd
, native
);
2330 bfd_release (abfd
, native
);
2335 /* This is stupid. This function should be a boolean predicate */
2337 DEFUN(NAME(aout
,canonicalize_reloc
),(abfd
, section
, relptr
, symbols
),
2340 arelent
**relptr AND
2343 arelent
*tblptr
= section
->relocation
;
2346 if (!(tblptr
|| NAME(aout
,slurp_reloc_table
)(abfd
, section
, symbols
)))
2349 if (section
->flags
& SEC_CONSTRUCTOR
) {
2350 arelent_chain
*chain
= section
->constructor_chain
;
2351 for (count
= 0; count
< section
->reloc_count
; count
++) {
2352 *relptr
++ = &chain
->relent
;
2353 chain
= chain
->next
;
2357 tblptr
= section
->relocation
;
2358 if (!tblptr
) return 0;
2360 for (count
= 0; count
++ < section
->reloc_count
;)
2362 *relptr
++ = tblptr
++;
2367 return section
->reloc_count
;
2371 DEFUN(NAME(aout
,get_reloc_upper_bound
),(abfd
, asect
),
2375 if (bfd_get_format (abfd
) != bfd_object
) {
2376 bfd_error
= invalid_operation
;
2379 if (asect
->flags
& SEC_CONSTRUCTOR
) {
2380 return (sizeof (arelent
*) * (asect
->reloc_count
+1));
2384 if (asect
== obj_datasec (abfd
))
2385 return (sizeof (arelent
*) *
2386 ((exec_hdr(abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
))
2389 if (asect
== obj_textsec (abfd
))
2390 return (sizeof (arelent
*) *
2391 ((exec_hdr(abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
))
2394 bfd_error
= invalid_operation
;
2400 DEFUN(NAME(aout
,get_symtab_upper_bound
),(abfd
),
2403 if (!NAME(aout
,slurp_symbol_table
)(abfd
)) return 0;
2405 return (bfd_get_symcount (abfd
)+1) * (sizeof (aout_symbol_type
*));
2410 DEFUN(NAME(aout
,get_lineno
),(ignore_abfd
, ignore_symbol
),
2411 bfd
*ignore_abfd AND
2412 asymbol
*ignore_symbol
)
2414 return (alent
*)NULL
;
2419 DEFUN(NAME(aout
,get_symbol_info
),(ignore_abfd
, symbol
, ret
),
2420 bfd
*ignore_abfd AND
2424 bfd_symbol_info (symbol
, ret
);
2426 if (ret
->type
== '?')
2428 int type_code
= aout_symbol(symbol
)->type
& 0xff;
2429 CONST
char *stab_name
= aout_stab_name(type_code
);
2430 static char buf
[10];
2432 if (stab_name
== NULL
)
2434 sprintf(buf
, "(%d)", type_code
);
2438 ret
->stab_other
= (unsigned)(aout_symbol(symbol
)->other
& 0xff);
2439 ret
->stab_desc
= (unsigned)(aout_symbol(symbol
)->desc
& 0xffff);
2440 ret
->stab_name
= stab_name
;
2446 DEFUN(NAME(aout
,print_symbol
),(ignore_abfd
, afile
, symbol
, how
),
2447 bfd
*ignore_abfd AND
2450 bfd_print_symbol_type how
)
2452 FILE *file
= (FILE *)afile
;
2455 case bfd_print_symbol_name
:
2457 fprintf(file
,"%s", symbol
->name
);
2459 case bfd_print_symbol_more
:
2460 fprintf(file
,"%4x %2x %2x",(unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2461 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2462 (unsigned)(aout_symbol(symbol
)->type
));
2464 case bfd_print_symbol_all
:
2466 CONST
char *section_name
= symbol
->section
->name
;
2469 bfd_print_symbol_vandf((PTR
)file
,symbol
);
2471 fprintf(file
," %-5s %04x %02x %02x",
2473 (unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2474 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2475 (unsigned)(aout_symbol(symbol
)->type
& 0xff));
2477 fprintf(file
," %s", symbol
->name
);
2484 provided a BFD, a section and an offset into the section, calculate
2485 and return the name of the source file and the line nearest to the
2490 DEFUN(NAME(aout
,find_nearest_line
),(abfd
,
2498 asection
*section AND
2499 asymbol
**symbols AND
2501 CONST
char **filename_ptr AND
2502 CONST
char **functionname_ptr AND
2503 unsigned int *line_ptr
)
2505 /* Run down the file looking for the filename, function and linenumber */
2507 static char buffer
[100];
2508 static char filename_buffer
[200];
2509 CONST
char *directory_name
= NULL
;
2510 CONST
char *main_file_name
= NULL
;
2511 CONST
char *current_file_name
= NULL
;
2512 CONST
char *line_file_name
= NULL
; /* Value of current_file_name at line number. */
2513 bfd_vma high_line_vma
= ~0;
2514 bfd_vma low_func_vma
= 0;
2516 *filename_ptr
= abfd
->filename
;
2517 *functionname_ptr
= 0;
2519 if (symbols
!= (asymbol
**)NULL
) {
2520 for (p
= symbols
; *p
; p
++) {
2521 aout_symbol_type
*q
= (aout_symbol_type
*)(*p
);
2525 main_file_name
= current_file_name
= q
->symbol
.name
;
2526 /* Look ahead to next symbol to check if that too is an N_SO. */
2530 q
= (aout_symbol_type
*)(*p
);
2531 if (q
->type
!= (int)N_SO
)
2534 /* Found a second N_SO First is directory; second is filename. */
2535 directory_name
= current_file_name
;
2536 main_file_name
= current_file_name
= q
->symbol
.name
;
2537 if (obj_textsec(abfd
) != section
)
2541 current_file_name
= q
->symbol
.name
;
2548 /* We'll keep this if it resolves nearer than the one we have already */
2549 if (q
->symbol
.value
>= offset
&&
2550 q
->symbol
.value
< high_line_vma
) {
2551 *line_ptr
= q
->desc
;
2552 high_line_vma
= q
->symbol
.value
;
2553 line_file_name
= current_file_name
;
2558 /* We'll keep this if it is nearer than the one we have already */
2559 if (q
->symbol
.value
>= low_func_vma
&&
2560 q
->symbol
.value
<= offset
) {
2561 low_func_vma
= q
->symbol
.value
;
2562 func
= (asymbol
*)q
;
2564 if (*line_ptr
&& func
) {
2565 CONST
char *function
= func
->name
;
2567 strncpy(buffer
, function
, sizeof(buffer
)-1);
2568 buffer
[sizeof(buffer
)-1] = 0;
2569 /* Have to remove : stuff */
2570 p
= strchr(buffer
,':');
2571 if (p
!= NULL
) { *p
= '\0'; }
2572 *functionname_ptr
= buffer
;
2584 main_file_name
= line_file_name
;
2585 if (main_file_name
) {
2586 if (main_file_name
[0] == '/' || directory_name
== NULL
)
2587 *filename_ptr
= main_file_name
;
2589 sprintf(filename_buffer
, "%.140s%.50s",
2590 directory_name
, main_file_name
);
2591 *filename_ptr
= filename_buffer
;
2600 DEFUN(NAME(aout
,sizeof_headers
),(abfd
, execable
),
2604 return adata(abfd
).exec_bytes_size
;
2607 /* a.out link code. */
2609 /* a.out linker hash table entries. */
2611 struct aout_link_hash_entry
2613 struct bfd_link_hash_entry root
;
2614 /* Symbol index in output file. */
2618 /* a.out linker hash table. */
2620 struct aout_link_hash_table
2622 struct bfd_link_hash_table root
;
2625 static struct bfd_hash_entry
*aout_link_hash_newfunc
2626 PARAMS ((struct bfd_hash_entry
*entry
,
2627 struct bfd_hash_table
*table
,
2628 const char *string
));
2629 static boolean aout_link_add_object_symbols
2630 PARAMS ((bfd
*, struct bfd_link_info
*));
2631 static boolean aout_link_check_archive_element
2632 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*));
2633 static boolean aout_link_get_symbols
PARAMS ((bfd
*));
2634 static boolean aout_link_free_symbols
PARAMS ((bfd
*));
2635 static boolean aout_link_check_ar_symbols
2636 PARAMS ((bfd
*, struct bfd_link_info
*, boolean
*pneeded
));
2637 static boolean aout_link_add_symbols
2638 PARAMS ((bfd
*, struct bfd_link_info
*));
2640 /* Routine to create an entry in an a.out link hash table. */
2642 static struct bfd_hash_entry
*
2643 aout_link_hash_newfunc (entry
, table
, string
)
2644 struct bfd_hash_entry
*entry
;
2645 struct bfd_hash_table
*table
;
2648 struct aout_link_hash_entry
*ret
= (struct aout_link_hash_entry
*) entry
;
2650 /* Allocate the structure if it has not already been allocated by a
2652 if (ret
== (struct aout_link_hash_entry
*) NULL
)
2653 ret
= ((struct aout_link_hash_entry
*)
2654 bfd_hash_allocate (table
, sizeof (struct aout_link_hash_entry
)));
2656 /* Call the allocation method of the superclass. */
2657 ret
= ((struct aout_link_hash_entry
*)
2658 _bfd_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2661 /* Set local fields. */
2664 return (struct bfd_hash_entry
*) ret
;
2667 /* Create an a.out link hash table. */
2669 struct bfd_link_hash_table
*
2670 NAME(aout
,link_hash_table_create
) (abfd
)
2673 struct aout_link_hash_table
*ret
;
2675 ret
= ((struct aout_link_hash_table
*)
2676 bfd_xmalloc (sizeof (struct aout_link_hash_table
)));
2677 if (! _bfd_link_hash_table_init (&ret
->root
, abfd
,
2678 aout_link_hash_newfunc
))
2681 return (struct bfd_link_hash_table
*) NULL
;
2686 /* Look up an entry in an a.out link hash table. */
2688 #define aout_link_hash_lookup(table, string, create, copy, follow) \
2689 ((struct aout_link_hash_entry *) \
2690 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
2692 /* Traverse an a.out link hash table. */
2694 #define aout_link_hash_traverse(table, func, info) \
2695 (bfd_link_hash_traverse \
2697 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
2700 /* Get the a.out link hash table from the info structure. This is
2703 #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash))
2705 /* Given an a.out BFD, add symbols to the global hash table as
2709 NAME(aout
,link_add_symbols
) (abfd
, info
)
2711 struct bfd_link_info
*info
;
2713 switch (bfd_get_format (abfd
))
2716 return aout_link_add_object_symbols (abfd
, info
);
2718 return _bfd_generic_link_add_archive_symbols
2719 (abfd
, info
, aout_link_check_archive_element
);
2721 bfd_error
= wrong_format
;
2726 /* Add symbols from an a.out object file. */
2729 aout_link_add_object_symbols (abfd
, info
)
2731 struct bfd_link_info
*info
;
2733 if (! aout_link_get_symbols (abfd
))
2735 if (! aout_link_add_symbols (abfd
, info
))
2737 if (! info
->keep_memory
)
2739 if (! aout_link_free_symbols (abfd
))
2745 /* Check a single archive element to see if we need to include it in
2746 the link. *PNEEDED is set according to whether this element is
2747 needed in the link or not. This is called from
2748 _bfd_generic_link_add_archive_symbols. */
2751 aout_link_check_archive_element (abfd
, info
, pneeded
)
2753 struct bfd_link_info
*info
;
2756 if (! aout_link_get_symbols (abfd
))
2759 if (! aout_link_check_ar_symbols (abfd
, info
, pneeded
))
2764 if (! aout_link_add_symbols (abfd
, info
))
2768 /* We keep around the symbols even if we aren't going to use this
2769 object file, because we may want to reread it. This doesn't
2770 waste too much memory, because it isn't all that common to read
2771 an archive element but not need it. */
2772 if (! info
->keep_memory
)
2774 if (! aout_link_free_symbols (abfd
))
2781 /* Read the internal symbols from an a.out file. */
2784 aout_link_get_symbols (abfd
)
2787 bfd_size_type count
;
2788 struct external_nlist
*syms
;
2789 unsigned char string_chars
[BYTES_IN_WORD
];
2790 bfd_size_type stringsize
;
2793 if (obj_aout_external_syms (abfd
) != (struct external_nlist
*) NULL
)
2795 /* We already have them. */
2799 count
= exec_hdr (abfd
)->a_syms
/ EXTERNAL_NLIST_SIZE
;
2801 /* We allocate using bfd_xmalloc to make the values easy to free
2802 later on. If we put them on the obstack it might not be possible
2804 syms
= ((struct external_nlist
*)
2805 bfd_xmalloc ((size_t) count
* EXTERNAL_NLIST_SIZE
));
2807 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
2808 || (bfd_read ((PTR
) syms
, 1, exec_hdr (abfd
)->a_syms
, abfd
)
2809 != exec_hdr (abfd
)->a_syms
))
2812 /* Get the size of the strings. */
2813 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0
2814 || (bfd_read ((PTR
) string_chars
, BYTES_IN_WORD
, 1, abfd
)
2817 stringsize
= GET_WORD (abfd
, string_chars
);
2818 strings
= (char *) bfd_xmalloc ((size_t) stringsize
);
2820 /* Skip space for the string count in the buffer for convenience
2821 when using indexes. */
2822 if (bfd_read (strings
+ BYTES_IN_WORD
, 1, stringsize
- BYTES_IN_WORD
, abfd
)
2823 != stringsize
- BYTES_IN_WORD
)
2826 /* Save the data. */
2827 obj_aout_external_syms (abfd
) = syms
;
2828 obj_aout_external_sym_count (abfd
) = count
;
2829 obj_aout_external_strings (abfd
) = strings
;
2834 /* Free up the internal symbols read from an a.out file. */
2837 aout_link_free_symbols (abfd
)
2840 if (obj_aout_external_syms (abfd
) != (struct external_nlist
*) NULL
)
2842 free ((PTR
) obj_aout_external_syms (abfd
));
2843 obj_aout_external_syms (abfd
) = (struct external_nlist
*) NULL
;
2845 if (obj_aout_external_strings (abfd
) != (char *) NULL
)
2847 free ((PTR
) obj_aout_external_strings (abfd
));
2848 obj_aout_external_strings (abfd
) = (char *) NULL
;
2853 /* Look through the internal symbols to see if this object file should
2854 be included in the link. We should include this object file if it
2855 defines any symbols which are currently undefined. If this object
2856 file defines a common symbol, then we may adjust the size of the
2857 known symbol but we do not include the object file in the link
2858 (unless there is some other reason to include it). */
2861 aout_link_check_ar_symbols (abfd
, info
, pneeded
)
2863 struct bfd_link_info
*info
;
2866 register struct external_nlist
*p
;
2867 struct external_nlist
*pend
;
2872 /* Look through all the symbols. */
2873 p
= obj_aout_external_syms (abfd
);
2874 pend
= p
+ obj_aout_external_sym_count (abfd
);
2875 strings
= obj_aout_external_strings (abfd
);
2876 for (; p
< pend
; p
++)
2878 int type
= bfd_h_get_8 (abfd
, p
->e_type
);
2880 struct bfd_link_hash_entry
*h
;
2882 /* Ignore symbols that are not externally visible. */
2883 if ((type
& N_EXT
) == 0)
2886 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
2887 h
= bfd_link_hash_lookup (info
->hash
, name
, false, false, true);
2889 /* We are only interested in symbols that are currently
2890 undefined or common. */
2891 if (h
== (struct bfd_link_hash_entry
*) NULL
2892 || (h
->type
!= bfd_link_hash_undefined
2893 && h
->type
!= bfd_link_hash_common
))
2896 if ((type
& (N_TEXT
| N_DATA
| N_BSS
)) != 0)
2898 /* This object file defines this symbol. We must link it
2899 in. This is true regardless of whether the current
2900 definition of the symbol is undefined or common. If the
2901 current definition is common, we have a case in which we
2902 have already seen an object file including
2904 and this object file from the archive includes
2906 In such a case we must include this object file. */
2907 if (! (*info
->callbacks
->add_archive_element
) (info
, abfd
, name
))
2913 if (type
== (N_EXT
| N_UNDF
))
2917 value
= GET_WORD (abfd
, p
->e_value
);
2920 /* This symbol is common in the object from the archive
2922 if (h
->type
== bfd_link_hash_undefined
)
2926 symbfd
= h
->u
.undef
.abfd
;
2927 if (symbfd
== (bfd
*) NULL
)
2929 /* This symbol was created as undefined from
2930 outside BFD. We assume that we should link
2931 in the object file. This is done for the -u
2932 option in the linker. */
2933 if (! (*info
->callbacks
->add_archive_element
) (info
,
2940 /* Turn the current link symbol into a common
2941 symbol. It is already on the undefs list. */
2942 h
->type
= bfd_link_hash_common
;
2943 h
->u
.c
.size
= value
;
2944 h
->u
.c
.section
= bfd_make_section_old_way (symbfd
,
2949 /* Adjust the size of the common symbol if
2951 if (value
> h
->u
.c
.size
)
2952 h
->u
.c
.size
= value
;
2958 /* We do not need this object file. */
2962 /* Add all symbols from an object file to the hash table. */
2965 aout_link_add_symbols (abfd
, info
)
2967 struct bfd_link_info
*info
;
2969 bfd_size_type sym_count
;
2972 struct aout_link_hash_entry
**sym_hash
;
2973 register struct external_nlist
*p
;
2974 struct external_nlist
*pend
;
2976 sym_count
= obj_aout_external_sym_count (abfd
);
2977 strings
= obj_aout_external_strings (abfd
);
2978 if (info
->keep_memory
)
2983 /* We keep a list of the linker hash table entries that correspond
2984 to particular symbols. We could just look them up in the hash
2985 table, but keeping the list is more efficient. Perhaps this
2986 should be conditional on info->keep_memory. */
2987 sym_hash
= ((struct aout_link_hash_entry
**)
2990 * sizeof (struct aout_link_hash_entry
*))));
2991 obj_aout_sym_hashes (abfd
) = sym_hash
;
2993 p
= obj_aout_external_syms (abfd
);
2994 pend
= p
+ sym_count
;
2995 for (; p
< pend
; p
++, sym_hash
++)
3006 type
= bfd_h_get_8 (abfd
, p
->e_type
);
3008 /* Ignore debugging symbols. */
3009 if ((type
& N_STAB
) != 0)
3012 /* Ignore symbols that are not external. */
3013 if ((type
& N_EXT
) == 0
3014 && type
!= N_WARNING
3020 /* If this is an N_INDR symbol we must skip the next entry,
3021 which is the symbol to indirect to (actually, an N_INDR
3022 symbol without N_EXT set is pretty useless). */
3028 /* Ignore N_FN symbols (these appear to have N_EXT set). */
3032 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3033 value
= GET_WORD (abfd
, p
->e_value
);
3040 case N_UNDF
| N_EXT
:
3042 section
= &bfd_com_section
;
3044 section
= &bfd_und_section
;
3047 section
= &bfd_abs_section
;
3049 case N_TEXT
| N_EXT
:
3050 section
= obj_textsec (abfd
);
3051 value
-= bfd_get_section_vma (abfd
, section
);
3053 case N_DATA
| N_EXT
:
3054 section
= obj_datasec (abfd
);
3055 value
-= bfd_get_section_vma (abfd
, section
);
3058 section
= obj_bsssec (abfd
);
3059 value
-= bfd_get_section_vma (abfd
, section
);
3061 case N_INDR
| N_EXT
:
3062 /* An indirect symbol. The next symbol is the symbol
3063 which this one really is. */
3064 BFD_ASSERT (p
+ 1 < pend
);
3066 string
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3067 section
= &bfd_ind_section
;
3068 flags
|= BSF_INDIRECT
;
3070 case N_COMM
| N_EXT
:
3071 section
= &bfd_com_section
;
3074 section
= &bfd_abs_section
;
3075 flags
|= BSF_CONSTRUCTOR
;
3078 section
= obj_textsec (abfd
);
3079 flags
|= BSF_CONSTRUCTOR
;
3080 value
-= bfd_get_section_vma (abfd
, section
);
3083 section
= obj_datasec (abfd
);
3084 flags
|= BSF_CONSTRUCTOR
;
3085 value
-= bfd_get_section_vma (abfd
, section
);
3088 section
= obj_bsssec (abfd
);
3089 flags
|= BSF_CONSTRUCTOR
;
3090 value
-= bfd_get_section_vma (abfd
, section
);
3093 /* A warning symbol. The next symbol is the one to warn
3095 BFD_ASSERT (p
+ 1 < pend
);
3098 name
= strings
+ GET_WORD (abfd
, p
->e_strx
);
3099 section
= &bfd_und_section
;
3100 flags
|= BSF_WARNING
;
3104 if (! (_bfd_generic_link_add_one_symbol
3105 (info
, abfd
, name
, flags
, section
, value
, string
, copy
,
3106 (struct bfd_link_hash_entry
**) sym_hash
)))
3113 /* During the final link step we need to pass around a bunch of
3114 information, so we do it in an instance of this structure. */
3116 struct aout_final_link_info
3118 /* General link information. */
3119 struct bfd_link_info
*info
;
3122 /* Reloc file positions. */
3123 file_ptr treloff
, dreloff
;
3124 /* File position of symbols. */
3127 struct stringtab_data strtab
;
3130 static boolean aout_link_input_bfd
3131 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
));
3132 static boolean aout_link_write_symbols
3133 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
, int *symbol_map
));
3134 static boolean aout_link_write_other_symbol
3135 PARAMS ((struct aout_link_hash_entry
*, PTR
));
3136 static boolean aout_link_input_section
3137 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3138 asection
*input_section
, file_ptr
*reloff_ptr
,
3139 bfd_size_type rel_size
, int *symbol_map
));
3140 static boolean aout_link_input_section_std
3141 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3142 asection
*input_section
, struct reloc_std_external
*,
3143 bfd_size_type rel_size
, bfd_byte
*contents
, int *symbol_map
));
3144 static boolean aout_link_input_section_ext
3145 PARAMS ((struct aout_final_link_info
*, bfd
*input_bfd
,
3146 asection
*input_section
, struct reloc_ext_external
*,
3147 bfd_size_type rel_size
, bfd_byte
*contents
, int *symbol_map
));
3148 static INLINE asection
*aout_reloc_index_to_section
3149 PARAMS ((bfd
*, int));
3151 /* Do the final link step. This is called on the output BFD. The
3152 INFO structure should point to a list of BFDs linked through the
3153 link_next field which can be used to find each BFD which takes part
3154 in the output. Also, each section in ABFD should point to a list
3155 of bfd_link_order structures which list all the input sections for
3156 the output section. */
3159 NAME(aout
,final_link
) (abfd
, info
, callback
)
3161 struct bfd_link_info
*info
;
3162 void (*callback
) PARAMS ((bfd
*, file_ptr
*, file_ptr
*, file_ptr
*));
3164 struct aout_final_link_info aout_info
;
3166 bfd_size_type text_size
;
3168 register struct bfd_link_order
*p
;
3171 aout_info
.info
= info
;
3172 aout_info
.output_bfd
= abfd
;
3174 if (! info
->relocateable
)
3176 exec_hdr (abfd
)->a_trsize
= 0;
3177 exec_hdr (abfd
)->a_drsize
= 0;
3181 bfd_size_type trsize
, drsize
;
3183 /* Count up the relocation sizes. */
3186 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
3188 if (bfd_get_flavour (abfd
) == bfd_target_aout_flavour
)
3190 trsize
+= exec_hdr (sub
)->a_trsize
;
3191 drsize
+= exec_hdr (sub
)->a_drsize
;
3195 /* FIXME: We need to identify the .text and .data sections
3196 and call get_reloc_upper_bound and canonicalize_reloc to
3197 work out the number of relocs needed, and then multiply
3198 by the reloc size. */
3202 exec_hdr (abfd
)->a_trsize
= trsize
;
3203 exec_hdr (abfd
)->a_drsize
= drsize
;
3206 /* Adjust the section sizes and vmas according to the magic number.
3207 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3208 filepos for each section. */
3209 if (! NAME(aout
,adjust_sizes_and_vmas
) (abfd
, &text_size
, &text_end
))
3212 /* The relocation and symbol file positions differ among a.out
3213 targets. We are passed a callback routine from the backend
3214 specific code to handle this.
3215 FIXME: At this point we do not know how much space the symbol
3216 table will require. This will not work for any (nonstandard)
3217 a.out target that needs to know the symbol table size before it
3218 can compute the relocation file positions. This may or may not
3219 be the case for the hp300hpux target, for example. */
3220 (*callback
) (abfd
, &aout_info
.treloff
, &aout_info
.dreloff
,
3222 obj_textsec (abfd
)->rel_filepos
= aout_info
.treloff
;
3223 obj_datasec (abfd
)->rel_filepos
= aout_info
.dreloff
;
3224 obj_sym_filepos (abfd
) = aout_info
.symoff
;
3226 /* We keep a count of the symbols as we output them. */
3227 obj_aout_external_sym_count (abfd
) = 0;
3229 /* We accumulate the string table as we write out the symbols. */
3230 stringtab_init (&aout_info
.strtab
);
3232 /* The most time efficient way to do the link would be to read all
3233 the input object files into memory and then sort out the
3234 information into the output file. Unfortunately, that will
3235 probably use too much memory. Another method would be to step
3236 through everything that composes the text section and write it
3237 out, and then everything that composes the data section and write
3238 it out, and then write out the relocs, and then write out the
3239 symbols. Unfortunately, that requires reading stuff from each
3240 input file several times, and we will not be able to keep all the
3241 input files open simultaneously, and reopening them will be slow.
3243 What we do is basically process one input file at a time. We do
3244 everything we need to do with an input file once--copy over the
3245 section contents, handle the relocation information, and write
3246 out the symbols--and then we throw away the information we read
3247 from it. This approach requires a lot of lseeks of the output
3248 file, which is unfortunate but still faster than reopening a lot
3251 We use the output_has_begun field of the input BFDs to see
3252 whether we have already handled it. */
3253 for (sub
= info
->input_bfds
; sub
!= (bfd
*) NULL
; sub
= sub
->link_next
)
3254 sub
->output_has_begun
= false;
3256 for (o
= abfd
->sections
; o
!= (asection
*) NULL
; o
= o
->next
)
3260 for (p
= o
->link_order_head
;
3261 p
!= (struct bfd_link_order
*) NULL
;
3264 /* If we might be using the C based alloca function, we need
3265 to dump the memory allocated by aout_link_input_bfd. */
3273 case bfd_indirect_link_order
:
3274 input_bfd
= p
->u
.indirect
.section
->owner
;
3275 if (bfd_get_flavour (input_bfd
) == bfd_target_aout_flavour
)
3277 if (! input_bfd
->output_has_begun
)
3279 if (! aout_link_input_bfd (&aout_info
, input_bfd
))
3281 input_bfd
->output_has_begun
= true;
3291 if (! _bfd_default_link_order (abfd
, info
, o
, p
))
3297 /* Write out any symbols that we have not already written out. */
3298 aout_link_hash_traverse (aout_hash_table (info
),
3299 aout_link_write_other_symbol
,
3302 /* Update the header information. */
3303 abfd
->symcount
= obj_aout_external_sym_count (abfd
);
3304 exec_hdr (abfd
)->a_syms
= abfd
->symcount
* EXTERNAL_NLIST_SIZE
;
3305 obj_str_filepos (abfd
) = obj_sym_filepos (abfd
) + exec_hdr (abfd
)->a_syms
;
3306 obj_textsec (abfd
)->reloc_count
=
3307 exec_hdr (abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
);
3308 obj_datasec (abfd
)->reloc_count
=
3309 exec_hdr (abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
);
3311 /* Write out the string table. */
3312 if (bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
) != 0)
3314 emit_strtab (abfd
, &aout_info
.strtab
);
3319 /* Link an a.out input BFD into the output file. */
3322 aout_link_input_bfd (finfo
, input_bfd
)
3323 struct aout_final_link_info
*finfo
;
3326 bfd_size_type sym_count
;
3329 BFD_ASSERT (bfd_get_format (input_bfd
) == bfd_object
);
3331 /* Get the symbols. We probably have them already, unless
3332 finfo->info->keep_memory is false. */
3333 if (! aout_link_get_symbols (input_bfd
))
3336 sym_count
= obj_aout_external_sym_count (input_bfd
);
3337 symbol_map
= (int *) alloca ((size_t) sym_count
* sizeof (int));
3339 /* Write out the symbols and get a map of the new indices. */
3340 if (! aout_link_write_symbols (finfo
, input_bfd
, symbol_map
))
3343 /* Relocate and write out the sections. */
3344 if (! aout_link_input_section (finfo
, input_bfd
,
3345 obj_textsec (input_bfd
),
3347 exec_hdr (input_bfd
)->a_trsize
,
3349 || ! aout_link_input_section (finfo
, input_bfd
,
3350 obj_datasec (input_bfd
),
3352 exec_hdr (input_bfd
)->a_drsize
,
3356 /* If we are not keeping memory, we don't need the symbols any
3357 longer. We still need them if we are keeping memory, because the
3358 strings in the hash table point into them. */
3359 if (! finfo
->info
->keep_memory
)
3361 if (! aout_link_free_symbols (input_bfd
))
3368 /* Adjust and write out the symbols for an a.out file. Set the new
3369 symbol indices into a symbol_map. */
3372 aout_link_write_symbols (finfo
, input_bfd
, symbol_map
)
3373 struct aout_final_link_info
*finfo
;
3378 bfd_size_type sym_count
;
3380 enum bfd_link_strip strip
;
3381 enum bfd_link_discard discard
;
3382 struct external_nlist
*output_syms
;
3383 struct external_nlist
*outsym
;
3384 register struct external_nlist
*sym
;
3385 struct external_nlist
*sym_end
;
3386 struct aout_link_hash_entry
**sym_hash
;
3389 output_bfd
= finfo
->output_bfd
;
3390 sym_count
= obj_aout_external_sym_count (input_bfd
);
3391 strings
= obj_aout_external_strings (input_bfd
);
3392 strip
= finfo
->info
->strip
;
3393 discard
= finfo
->info
->discard
;
3394 output_syms
= ((struct external_nlist
*)
3395 alloca ((size_t) (sym_count
+ 1) * EXTERNAL_NLIST_SIZE
));
3396 outsym
= output_syms
;
3398 /* First write out a symbol for this object file, unless we are
3399 discarding such symbols. */
3400 if (strip
!= strip_all
3401 && (strip
!= strip_some
3402 || bfd_hash_lookup (finfo
->info
->keep_hash
, input_bfd
->filename
,
3403 false, false) != NULL
)
3404 && discard
!= discard_all
)
3406 bfd_h_put_8 (output_bfd
, N_TEXT
, outsym
->e_type
);
3407 bfd_h_put_8 (output_bfd
, 0, outsym
->e_other
);
3408 bfd_h_put_16 (output_bfd
, (bfd_vma
) 0, outsym
->e_desc
);
3409 PUT_WORD (output_bfd
,
3410 add_to_stringtab (output_bfd
, input_bfd
->filename
,
3413 PUT_WORD (output_bfd
,
3414 bfd_get_section_vma (input_bfd
, obj_textsec (input_bfd
)),
3416 ++obj_aout_external_sym_count (output_bfd
);
3421 sym
= obj_aout_external_syms (input_bfd
);
3422 sym_end
= sym
+ sym_count
;
3423 sym_hash
= obj_aout_sym_hashes (input_bfd
);
3424 for (; sym
< sym_end
; sym
++, sym_hash
++, symbol_map
++)
3434 type
= bfd_h_get_8 (input_bfd
, sym
->e_type
);
3435 name
= strings
+ GET_WORD (input_bfd
, sym
->e_strx
);
3439 /* Pass this symbol through. */
3440 val
= GET_WORD (input_bfd
, sym
->e_value
);
3445 struct aout_link_hash_entry
*h
;
3447 /* We have saved the hash table entry for this symbol, if
3448 there is one. Note that we could just look it up again
3449 in the hash table, provided we first check that it is an
3453 /* If the symbol has already been written out, skip it. */
3454 if (h
!= (struct aout_link_hash_entry
*) NULL
3457 *symbol_map
= h
->indx
;
3461 /* See if we are stripping this symbol. */
3467 case strip_debugger
:
3468 if ((type
& N_STAB
) != 0)
3472 if (bfd_hash_lookup (finfo
->info
->keep_hash
, name
, false, false)
3482 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3483 h
->root
.written
= true;
3487 /* Get the value of the symbol. */
3488 if ((type
& N_TYPE
) == N_TEXT
)
3489 symsec
= obj_textsec (input_bfd
);
3490 else if ((type
& N_TYPE
) == N_DATA
)
3491 symsec
= obj_datasec (input_bfd
);
3492 else if ((type
& N_TYPE
) == N_BSS
)
3493 symsec
= obj_bsssec (input_bfd
);
3494 else if ((type
& N_TYPE
) == N_ABS
)
3495 symsec
= &bfd_abs_section
;
3496 else if ((type
& N_TYPE
) == N_INDR
3497 || type
== N_WARNING
)
3499 /* Pass the next symbol through unchanged. */
3501 val
= GET_WORD (input_bfd
, sym
->e_value
);
3504 else if ((type
& N_STAB
) != 0)
3506 val
= GET_WORD (input_bfd
, sym
->e_value
);
3511 if (h
== (struct aout_link_hash_entry
*) NULL
)
3513 else if (h
->root
.type
== bfd_link_hash_defined
)
3515 asection
*output_section
;
3517 /* This case means a common symbol which was turned
3518 into a defined symbol. */
3519 output_section
= h
->root
.u
.def
.section
->output_section
;
3520 BFD_ASSERT (output_section
== &bfd_abs_section
3521 || output_section
->owner
== output_bfd
);
3522 val
= (h
->root
.u
.def
.value
3523 + bfd_get_section_vma (output_bfd
, output_section
)
3524 + h
->root
.u
.def
.section
->output_offset
);
3526 /* Get the correct type based on the section. If
3527 this is a constructed set, force it to be
3528 globally visible. */
3537 if (output_section
== obj_textsec (output_bfd
))
3539 else if (output_section
== obj_datasec (output_bfd
))
3541 else if (output_section
== obj_bsssec (output_bfd
))
3546 else if (h
->root
.type
== bfd_link_hash_common
)
3547 val
= h
->root
.u
.c
.size
;
3553 if (symsec
!= (asection
*) NULL
)
3554 val
= (symsec
->output_section
->vma
3555 + symsec
->output_offset
3556 + (GET_WORD (input_bfd
, sym
->e_value
)
3559 /* If this is a global symbol set the written flag, and if
3560 it is a local symbol see if we should discard it. */
3561 if (h
!= (struct aout_link_hash_entry
*) NULL
)
3563 h
->root
.written
= true;
3564 h
->indx
= obj_aout_external_sym_count (output_bfd
);
3573 if (*name
== *finfo
->info
->lprefix
3574 && (finfo
->info
->lprefix_len
== 1
3575 || strncmp (name
, finfo
->info
->lprefix
,
3576 finfo
->info
->lprefix_len
) == 0))
3591 /* Copy this symbol into the list of symbols we are going to
3593 bfd_h_put_8 (output_bfd
, type
, outsym
->e_type
);
3594 bfd_h_put_8 (output_bfd
, bfd_h_get_8 (input_bfd
, sym
->e_other
),
3596 bfd_h_put_16 (output_bfd
, bfd_h_get_16 (input_bfd
, sym
->e_desc
),
3598 PUT_WORD (output_bfd
,
3599 add_to_stringtab (output_bfd
, name
, &finfo
->strtab
),
3601 PUT_WORD (output_bfd
, val
, outsym
->e_value
);
3602 *symbol_map
= obj_aout_external_sym_count (output_bfd
);
3603 ++obj_aout_external_sym_count (output_bfd
);
3607 /* Write out the output symbols we have just constructed. */
3608 if (outsym
> output_syms
)
3610 bfd_size_type outsym_count
;
3612 if (bfd_seek (output_bfd
, finfo
->symoff
, SEEK_SET
) != 0)
3614 outsym_count
= outsym
- output_syms
;
3615 if (bfd_write ((PTR
) output_syms
, (bfd_size_type
) EXTERNAL_NLIST_SIZE
,
3616 (bfd_size_type
) outsym_count
, output_bfd
)
3617 != outsym_count
* EXTERNAL_NLIST_SIZE
)
3619 finfo
->symoff
+= outsym_count
* EXTERNAL_NLIST_SIZE
;
3625 /* Write out a symbol that was not associated with an a.out input
3629 aout_link_write_other_symbol (h
, data
)
3630 struct aout_link_hash_entry
*h
;
3633 struct aout_final_link_info
*finfo
= (struct aout_final_link_info
*) data
;
3637 struct external_nlist outsym
;
3639 if (h
->root
.written
)
3642 output_bfd
= finfo
->output_bfd
;
3644 switch (h
->root
.type
)
3647 case bfd_link_hash_new
:
3649 /* Avoid variable not initialized warnings. */
3651 case bfd_link_hash_undefined
:
3652 type
= N_UNDF
| N_EXT
;
3655 case bfd_link_hash_defined
:
3659 sec
= h
->root
.u
.def
.section
;
3660 BFD_ASSERT (sec
== &bfd_abs_section
3661 || sec
->owner
== output_bfd
);
3662 if (sec
== obj_textsec (output_bfd
))
3663 type
= N_TEXT
| N_EXT
;
3664 else if (sec
== obj_datasec (output_bfd
))
3665 type
= N_DATA
| N_EXT
;
3666 else if (sec
== obj_bsssec (output_bfd
))
3667 type
= N_BSS
| N_EXT
;
3669 type
= N_ABS
| N_EXT
;
3670 val
= (h
->root
.u
.def
.value
3671 + sec
->output_section
->vma
3672 + sec
->output_offset
);
3675 case bfd_link_hash_common
:
3676 type
= N_UNDF
| N_EXT
;
3677 val
= h
->root
.u
.c
.size
;
3679 case bfd_link_hash_indirect
:
3680 case bfd_link_hash_warning
:
3681 /* FIXME: Ignore these for now. The circumstances under which
3682 they should be written out are not clear to me. */
3686 bfd_h_put_8 (output_bfd
, type
, outsym
.e_type
);
3687 bfd_h_put_8 (output_bfd
, 0, outsym
.e_other
);
3688 bfd_h_put_16 (output_bfd
, 0, outsym
.e_desc
);
3689 PUT_WORD (output_bfd
,
3690 add_to_stringtab (output_bfd
, h
->root
.root
.string
, &finfo
->strtab
),
3692 PUT_WORD (output_bfd
, val
, outsym
.e_value
);
3694 if (bfd_seek (output_bfd
, finfo
->symoff
, SEEK_SET
) != 0
3695 || bfd_write ((PTR
) &outsym
, (bfd_size_type
) EXTERNAL_NLIST_SIZE
,
3696 (bfd_size_type
) 1, output_bfd
) != EXTERNAL_NLIST_SIZE
)
3698 /* FIXME: No way to handle errors. */
3702 finfo
->symoff
+= EXTERNAL_NLIST_SIZE
;
3703 h
->indx
= obj_aout_external_sym_count (output_bfd
);
3704 ++obj_aout_external_sym_count (output_bfd
);
3709 /* Link an a.out section into the output file. */
3712 aout_link_input_section (finfo
, input_bfd
, input_section
, reloff_ptr
,
3713 rel_size
, symbol_map
)
3714 struct aout_final_link_info
*finfo
;
3716 asection
*input_section
;
3717 file_ptr
*reloff_ptr
;
3718 bfd_size_type rel_size
;
3721 bfd_size_type input_size
;
3725 /* Get the section contents. */
3726 input_size
= bfd_section_size (input_bfd
, input_section
);
3727 contents
= (bfd_byte
*) alloca (input_size
);
3728 if (! bfd_get_section_contents (input_bfd
, input_section
, (PTR
) contents
,
3729 (file_ptr
) 0, input_size
))
3732 /* Read in the relocs. */
3733 relocs
= (PTR
) alloca (rel_size
);
3734 if (bfd_seek (input_bfd
, input_section
->rel_filepos
, SEEK_SET
) != 0
3735 || bfd_read (relocs
, 1, rel_size
, input_bfd
) != rel_size
)
3738 /* Relocate the section contents. */
3739 if (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
)
3741 if (! aout_link_input_section_std (finfo
, input_bfd
, input_section
,
3742 (struct reloc_std_external
*) relocs
,
3743 rel_size
, contents
, symbol_map
))
3748 if (! aout_link_input_section_ext (finfo
, input_bfd
, input_section
,
3749 (struct reloc_ext_external
*) relocs
,
3750 rel_size
, contents
, symbol_map
))
3754 /* Write out the section contents. */
3755 if (! bfd_set_section_contents (finfo
->output_bfd
,
3756 input_section
->output_section
,
3758 input_section
->output_offset
,
3762 /* If we are producing relocateable output, the relocs were
3763 modified, and we now write them out. */
3764 if (finfo
->info
->relocateable
)
3766 if (bfd_seek (finfo
->output_bfd
, *reloff_ptr
, SEEK_SET
) != 0)
3768 if (bfd_write (relocs
, (bfd_size_type
) 1, rel_size
, finfo
->output_bfd
)
3771 *reloff_ptr
+= rel_size
;
3773 /* Assert that the relocs have not run into the symbols, and
3774 that if these are the text relocs they have not run into the
3776 BFD_ASSERT (*reloff_ptr
<= obj_sym_filepos (finfo
->output_bfd
)
3777 && (reloff_ptr
!= &finfo
->treloff
3779 <= obj_datasec (finfo
->output_bfd
)->rel_filepos
)));
3785 /* Get the section corresponding to a reloc index. */
3787 static INLINE asection
*
3788 aout_reloc_index_to_section (abfd
, indx
)
3792 switch (indx
& N_TYPE
)
3795 return obj_textsec (abfd
);
3797 return obj_datasec (abfd
);
3799 return obj_bsssec (abfd
);
3801 return &bfd_abs_section
;
3807 /* Relocate an a.out section using standard a.out relocs. */
3810 aout_link_input_section_std (finfo
, input_bfd
, input_section
, relocs
,
3811 rel_size
, contents
, symbol_map
)
3812 struct aout_final_link_info
*finfo
;
3814 asection
*input_section
;
3815 struct reloc_std_external
*relocs
;
3816 bfd_size_type rel_size
;
3821 boolean relocateable
;
3822 struct external_nlist
*syms
;
3824 struct aout_link_hash_entry
**sym_hashes
;
3825 bfd_size_type reloc_count
;
3826 register struct reloc_std_external
*rel
;
3827 struct reloc_std_external
*rel_end
;
3829 output_bfd
= finfo
->output_bfd
;
3831 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_STD_SIZE
);
3832 BFD_ASSERT (input_bfd
->xvec
->header_byteorder_big_p
3833 == output_bfd
->xvec
->header_byteorder_big_p
);
3835 relocateable
= finfo
->info
->relocateable
;
3836 syms
= obj_aout_external_syms (input_bfd
);
3837 strings
= obj_aout_external_strings (input_bfd
);
3838 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
3840 reloc_count
= rel_size
/ RELOC_STD_SIZE
;
3842 rel_end
= rel
+ reloc_count
;
3843 for (; rel
< rel_end
; rel
++)
3855 bfd_reloc_status_type r
;
3857 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
3859 if (input_bfd
->xvec
->header_byteorder_big_p
)
3861 r_index
= ((rel
->r_index
[0] << 16)
3862 | (rel
->r_index
[1] << 8)
3864 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
3865 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
3866 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
3867 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
3868 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
3869 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
3870 >> RELOC_STD_BITS_LENGTH_SH_BIG
);
3874 r_index
= ((rel
->r_index
[2] << 16)
3875 | (rel
->r_index
[1] << 8)
3877 r_extern
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
3878 r_pcrel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
3879 r_baserel
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
3880 r_jmptable
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
3881 r_relative
= (0 != (rel
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
3882 r_length
= ((rel
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
3883 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
);
3886 howto_idx
= r_length
+ 4 * r_pcrel
+ 8 * r_baserel
;
3887 BFD_ASSERT (howto_idx
< TABLE_SIZE (howto_table_std
));
3888 BFD_ASSERT (r_jmptable
== 0);
3889 BFD_ASSERT (r_relative
== 0);
3893 /* We are generating a relocateable output file, and must
3894 modify the reloc accordingly. */
3897 struct aout_link_hash_entry
*h
;
3899 /* If we know the symbol this relocation is against,
3900 convert it into a relocation against a section. This
3901 is what the native linker does. */
3902 h
= sym_hashes
[r_index
];
3903 if (h
!= (struct aout_link_hash_entry
*) NULL
3904 && h
->root
.type
== bfd_link_hash_defined
)
3906 asection
*output_section
;
3908 /* Change the r_extern value. */
3909 if (output_bfd
->xvec
->header_byteorder_big_p
)
3910 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_BIG
;
3912 rel
->r_type
[0] &=~ RELOC_STD_BITS_EXTERN_LITTLE
;
3914 /* Compute a new r_index. */
3915 output_section
= h
->root
.u
.def
.section
->output_section
;
3916 if (output_section
== obj_textsec (output_bfd
))
3918 else if (output_section
== obj_datasec (output_bfd
))
3920 else if (output_section
== obj_bsssec (output_bfd
))
3925 /* Add the symbol value and the section VMA to the
3926 addend stored in the contents. */
3927 relocation
= (h
->root
.u
.def
.value
3928 + output_section
->vma
3929 + h
->root
.u
.def
.section
->output_offset
);
3933 /* We must change r_index according to the symbol
3935 r_index
= symbol_map
[r_index
];
3941 name
= strings
+ GET_WORD (input_bfd
,
3942 syms
[r_index
].e_strx
);
3943 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
3944 (finfo
->info
, name
, input_bfd
, input_section
,
3953 /* Write out the new r_index value. */
3954 if (output_bfd
->xvec
->header_byteorder_big_p
)
3956 rel
->r_index
[0] = r_index
>> 16;
3957 rel
->r_index
[1] = r_index
>> 8;
3958 rel
->r_index
[2] = r_index
;
3962 rel
->r_index
[2] = r_index
>> 16;
3963 rel
->r_index
[1] = r_index
>> 8;
3964 rel
->r_index
[0] = r_index
;
3971 /* This is a relocation against a section. We must
3972 adjust by the amount that the section moved. */
3973 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
3974 relocation
= (section
->output_section
->vma
3975 + section
->output_offset
3979 /* Change the address of the relocation. */
3980 PUT_WORD (output_bfd
,
3981 r_addr
+ input_section
->output_offset
,
3984 /* Adjust a PC relative relocation by removing the reference
3985 to the original address in the section and then including
3986 the reference to the new address. */
3989 relocation
+= input_section
->vma
;
3990 relocation
-= (input_section
->output_section
->vma
3991 + input_section
->output_offset
);
3994 if (relocation
== 0)
3997 r
= _bfd_relocate_contents (howto_table_std
+ howto_idx
,
3998 input_bfd
, relocation
,
4003 /* We are generating an executable, and must do a full
4007 struct aout_link_hash_entry
*h
;
4009 h
= sym_hashes
[r_index
];
4010 if (h
!= (struct aout_link_hash_entry
*) NULL
4011 && h
->root
.type
== bfd_link_hash_defined
)
4013 relocation
= (h
->root
.u
.def
.value
4014 + h
->root
.u
.def
.section
->output_section
->vma
4015 + h
->root
.u
.def
.section
->output_offset
);
4021 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4022 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4023 (finfo
->info
, name
, input_bfd
, input_section
,
4033 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4034 relocation
= (section
->output_section
->vma
4035 + section
->output_offset
4040 r
= _bfd_final_link_relocate (howto_table_std
+ howto_idx
,
4041 input_bfd
, input_section
,
4042 contents
, r_addr
, relocation
,
4046 if (r
!= bfd_reloc_ok
)
4051 case bfd_reloc_outofrange
:
4053 case bfd_reloc_overflow
:
4054 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4055 (finfo
->info
, input_bfd
, input_section
, r_addr
)))
4065 /* Relocate an a.out section using extended a.out relocs. */
4068 aout_link_input_section_ext (finfo
, input_bfd
, input_section
, relocs
,
4069 rel_size
, contents
, symbol_map
)
4070 struct aout_final_link_info
*finfo
;
4072 asection
*input_section
;
4073 struct reloc_ext_external
*relocs
;
4074 bfd_size_type rel_size
;
4079 boolean relocateable
;
4080 struct external_nlist
*syms
;
4082 struct aout_link_hash_entry
**sym_hashes
;
4083 bfd_size_type reloc_count
;
4084 register struct reloc_ext_external
*rel
;
4085 struct reloc_ext_external
*rel_end
;
4087 output_bfd
= finfo
->output_bfd
;
4089 BFD_ASSERT (obj_reloc_entry_size (input_bfd
) == RELOC_EXT_SIZE
);
4090 BFD_ASSERT (input_bfd
->xvec
->header_byteorder_big_p
4091 == output_bfd
->xvec
->header_byteorder_big_p
);
4093 relocateable
= finfo
->info
->relocateable
;
4094 syms
= obj_aout_external_syms (input_bfd
);
4095 strings
= obj_aout_external_strings (input_bfd
);
4096 sym_hashes
= obj_aout_sym_hashes (input_bfd
);
4098 reloc_count
= rel_size
/ RELOC_EXT_SIZE
;
4100 rel_end
= rel
+ reloc_count
;
4101 for (; rel
< rel_end
; rel
++)
4110 r_addr
= GET_SWORD (input_bfd
, rel
->r_address
);
4112 if (input_bfd
->xvec
->header_byteorder_big_p
)
4114 r_index
= ((rel
->r_index
[0] << 16)
4115 | (rel
->r_index
[1] << 8)
4117 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
4118 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
4119 >> RELOC_EXT_BITS_TYPE_SH_BIG
);
4123 r_index
= ((rel
->r_index
[2] << 16)
4124 | (rel
->r_index
[1] << 8)
4126 r_extern
= (0 != (rel
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
4127 r_type
= ((rel
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
4128 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
);
4131 r_addend
= GET_SWORD (input_bfd
, rel
->r_addend
);
4135 /* We are generating a relocateable output file, and must
4136 modify the reloc accordingly. */
4139 struct aout_link_hash_entry
*h
;
4141 /* If we know the symbol this relocation is against,
4142 convert it into a relocation against a section. This
4143 is what the native linker does. */
4144 h
= sym_hashes
[r_index
];
4145 if (h
!= (struct aout_link_hash_entry
*) NULL
4146 && h
->root
.type
== bfd_link_hash_defined
)
4148 asection
*output_section
;
4150 /* Change the r_extern value. */
4151 if (output_bfd
->xvec
->header_byteorder_big_p
)
4152 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_BIG
;
4154 rel
->r_type
[0] &=~ RELOC_EXT_BITS_EXTERN_LITTLE
;
4156 /* Compute a new r_index. */
4157 output_section
= h
->root
.u
.def
.section
->output_section
;
4158 if (output_section
== obj_textsec (output_bfd
))
4160 else if (output_section
== obj_datasec (output_bfd
))
4162 else if (output_section
== obj_bsssec (output_bfd
))
4167 /* Add the symbol value and the section VMA to the
4169 relocation
= (h
->root
.u
.def
.value
4170 + output_section
->vma
4171 + h
->root
.u
.def
.section
->output_offset
);
4175 /* We must change r_index according to the symbol
4177 r_index
= symbol_map
[r_index
];
4184 + GET_WORD (input_bfd
, syms
[r_index
].e_strx
));
4185 if (! ((*finfo
->info
->callbacks
->unattached_reloc
)
4186 (finfo
->info
, name
, input_bfd
, input_section
,
4195 /* Write out the new r_index value. */
4196 if (output_bfd
->xvec
->header_byteorder_big_p
)
4198 rel
->r_index
[0] = r_index
>> 16;
4199 rel
->r_index
[1] = r_index
>> 8;
4200 rel
->r_index
[2] = r_index
;
4204 rel
->r_index
[2] = r_index
>> 16;
4205 rel
->r_index
[1] = r_index
>> 8;
4206 rel
->r_index
[0] = r_index
;
4213 /* This is a relocation against a section. We must
4214 adjust by the amount that the section moved. */
4215 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4216 relocation
= (section
->output_section
->vma
4217 + section
->output_offset
4221 /* Adjust a PC relative relocation by removing the reference
4222 to the original address in the section and then including
4223 the reference to the new address. */
4224 if (howto_table_ext
[r_type
].pc_relative
4225 && ! howto_table_ext
[r_type
].pcrel_offset
)
4227 relocation
+= input_section
->vma
;
4228 relocation
-= (input_section
->output_section
->vma
4229 + input_section
->output_offset
);
4232 /* Change the addend if necessary. */
4233 if (relocation
!= 0)
4234 PUT_WORD (output_bfd
, r_addend
+ relocation
, rel
->r_addend
);
4236 /* Change the address of the relocation. */
4237 PUT_WORD (output_bfd
,
4238 r_addr
+ input_section
->output_offset
,
4243 bfd_reloc_status_type r
;
4245 /* We are generating an executable, and must do a full
4249 struct aout_link_hash_entry
*h
;
4251 h
= sym_hashes
[r_index
];
4252 if (h
!= (struct aout_link_hash_entry
*) NULL
4253 && h
->root
.type
== bfd_link_hash_defined
)
4255 relocation
= (h
->root
.u
.def
.value
4256 + h
->root
.u
.def
.section
->output_section
->vma
4257 + h
->root
.u
.def
.section
->output_offset
);
4263 name
= strings
+ GET_WORD (input_bfd
, syms
[r_index
].e_strx
);
4264 if (! ((*finfo
->info
->callbacks
->undefined_symbol
)
4265 (finfo
->info
, name
, input_bfd
, input_section
,
4275 section
= aout_reloc_index_to_section (input_bfd
, r_index
);
4276 relocation
= (section
->output_section
->vma
4277 + section
->output_offset
4281 BFD_ASSERT (r_type
>= 0
4282 && r_type
< TABLE_SIZE (howto_table_ext
));
4284 r
= _bfd_final_link_relocate (howto_table_ext
+ r_type
,
4285 input_bfd
, input_section
,
4286 contents
, r_addr
, relocation
,
4288 if (r
!= bfd_reloc_ok
)
4293 case bfd_reloc_outofrange
:
4295 case bfd_reloc_overflow
:
4296 if (! ((*finfo
->info
->callbacks
->reloc_overflow
)
4297 (finfo
->info
, input_bfd
, input_section
, r_addr
)))