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 @code{aoutx.h}
34 and other files which derive functions from the base. One
35 derivation file is @code{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 @code{sunos.c} for sun3 and sun4,
42 @code{newsos3.c} for the Sony NEWS, and @code{demo64.c} for a
43 demonstration of a 64 bit a.out format.
45 The base file @code{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 @code{aout32.c} and @code{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
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 aout32.c, and produces the jump vector
77 The 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 <<../include/sys/h-XXX.h>> (for your host). These
97 values, plus the structures and macros defined in <<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 <<host-aout.c>., specify:
102 | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
103 | TDEPFILES= host-aout.o trad-core.o
105 in the <<config/mt-XXX>> file, and modify configure.in to use the
106 <<mt-XXX>> file (by setting "<<bfd_target=XXX>>") when your
107 configuration is selected.
112 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
113 Doesn't matter what the setting of WP_TEXT is on output, but it'll
115 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
116 * Any BFD with both flags clear is OMAGIC.
117 (Just want to make these explicit, so the conditions tested in this
118 file make sense if you're more familiar with a.out than with BFD.) */
121 #define KEEPITTYPE int
124 #include <string.h> /* For strchr and friends */
127 #include <ansidecl.h>
129 struct external_exec
;
132 #include "aout/aout64.h"
133 #include "aout/stab_gnu.h"
136 extern void (*bfd_error_trap
)();
143 The file @code{aoutx.h} caters for both the @emph{standard}
144 and @emph{extended} forms of a.out relocation records.
146 The standard records are characterised by containing only an
147 address, a symbol index and a type field. The extended records
148 (used on 29ks and sparcs) also have a full integer for an
152 #define CTOR_TABLE_RELOC_IDX 2
154 #define howto_table_ext NAME(aout,ext_howto_table)
155 #define howto_table_std NAME(aout,std_howto_table)
157 reloc_howto_type howto_table_ext
[] =
159 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
160 HOWTO(RELOC_8
, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", false, 0,0x000000ff, false),
161 HOWTO(RELOC_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", false, 0,0x0000ffff, false),
162 HOWTO(RELOC_32
, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", false, 0,0xffffffff, false),
163 HOWTO(RELOC_DISP8
, 0, 0, 8, true, 0, complain_overflow_signed
,0,"DISP8", false, 0,0x000000ff, false),
164 HOWTO(RELOC_DISP16
, 0, 1, 16, true, 0, complain_overflow_signed
,0,"DISP16", false, 0,0x0000ffff, false),
165 HOWTO(RELOC_DISP32
, 0, 2, 32, true, 0, complain_overflow_signed
,0,"DISP32", false, 0,0xffffffff, false),
166 HOWTO(RELOC_WDISP30
,2, 2, 30, true, 0, complain_overflow_signed
,0,"WDISP30", false, 0,0x3fffffff, false),
167 HOWTO(RELOC_WDISP22
,2, 2, 22, true, 0, complain_overflow_signed
,0,"WDISP22", false, 0,0x003fffff, false),
168 HOWTO(RELOC_HI22
, 10, 2, 22, false, 0, complain_overflow_bitfield
,0,"HI22", false, 0,0x003fffff, false),
169 HOWTO(RELOC_22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"22", false, 0,0x003fffff, false),
170 HOWTO(RELOC_13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"13", false, 0,0x00001fff, false),
171 HOWTO(RELOC_LO10
, 0, 2, 10, false, 0, complain_overflow_dont
,0,"LO10", false, 0,0x000003ff, false),
172 HOWTO(RELOC_SFA_BASE
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_BASE", false, 0,0xffffffff, false),
173 HOWTO(RELOC_SFA_OFF13
,0,2, 32, false, 0, complain_overflow_bitfield
,0,"SFA_OFF13",false, 0,0xffffffff, false),
174 HOWTO(RELOC_BASE10
, 0, 2, 16, false, 0, complain_overflow_bitfield
,0,"BASE10", false, 0,0x0000ffff, false),
175 HOWTO(RELOC_BASE13
, 0, 2, 13, false, 0, complain_overflow_bitfield
,0,"BASE13", false, 0,0x00001fff, false),
176 HOWTO(RELOC_BASE22
, 0, 2, 0, false, 0, complain_overflow_bitfield
,0,"BASE22", false, 0,0x00000000, false),
177 HOWTO(RELOC_PC10
, 0, 2, 10, false, 0, complain_overflow_bitfield
,0,"PC10", false, 0,0x000003ff, false),
178 HOWTO(RELOC_PC22
, 0, 2, 22, false, 0, complain_overflow_bitfield
,0,"PC22", false, 0,0x003fffff, false),
179 HOWTO(RELOC_JMP_TBL
,0, 2, 32, false, 0, complain_overflow_bitfield
,0,"JMP_TBL", false, 0,0xffffffff, false),
180 HOWTO(RELOC_SEGOFF16
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"SEGOFF16", false, 0,0x00000000, false),
181 HOWTO(RELOC_GLOB_DAT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"GLOB_DAT", false, 0,0x00000000, false),
182 HOWTO(RELOC_JMP_SLOT
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"JMP_SLOT", false, 0,0x00000000, false),
183 HOWTO(RELOC_RELATIVE
,0, 2, 0, false, 0, complain_overflow_bitfield
,0,"RELATIVE", false, 0,0x00000000, false),
186 /* Convert standard reloc records to "arelent" format (incl byte swap). */
188 reloc_howto_type howto_table_std
[] = {
189 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
190 HOWTO( 0, 0, 0, 8, false, 0, complain_overflow_bitfield
,0,"8", true, 0x000000ff,0x000000ff, false),
191 HOWTO( 1, 0, 1, 16, false, 0, complain_overflow_bitfield
,0,"16", true, 0x0000ffff,0x0000ffff, false),
192 HOWTO( 2, 0, 2, 32, false, 0, complain_overflow_bitfield
,0,"32", true, 0xffffffff,0xffffffff, false),
193 HOWTO( 3, 0, 3, 64, false, 0, complain_overflow_bitfield
,0,"64", true, 0xdeaddead,0xdeaddead, false),
194 HOWTO( 4, 0, 0, 8, true, 0, complain_overflow_signed
,0,"DISP8", true, 0x000000ff,0x000000ff, false),
195 HOWTO( 5, 0, 1, 16, true, 0, complain_overflow_signed
,0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
196 HOWTO( 6, 0, 2, 32, true, 0, complain_overflow_signed
,0,"DISP32", true, 0xffffffff,0xffffffff, false),
197 HOWTO( 7, 0, 3, 64, true, 0, complain_overflow_signed
,0,"DISP64", true, 0xfeedface,0xfeedface, false),
200 CONST
struct reloc_howto_struct
*
201 DEFUN(NAME(aout
,reloc_type_lookup
),(abfd
,code
),
203 bfd_reloc_code_real_type code
)
205 #define EXT(i,j) case i: return &howto_table_ext[j]
206 #define STD(i,j) case i: return &howto_table_std[j]
207 int ext
= obj_reloc_entry_size (abfd
) == RELOC_EXT_SIZE
;
208 if (code
== BFD_RELOC_CTOR
)
209 switch (bfd_get_arch_info (abfd
)->bits_per_address
)
218 EXT (BFD_RELOC_32
, 2);
219 EXT (BFD_RELOC_HI22
, 8);
220 EXT (BFD_RELOC_LO10
, 11);
221 EXT (BFD_RELOC_32_PCREL_S2
, 6);
222 default: return (CONST
struct reloc_howto_struct
*) 0;
228 STD (BFD_RELOC_16
, 1);
229 STD (BFD_RELOC_32
, 2);
230 STD (BFD_RELOC_8_PCREL
, 4);
231 STD (BFD_RELOC_16_PCREL
, 5);
232 STD (BFD_RELOC_32_PCREL
, 6);
233 default: return (CONST
struct reloc_howto_struct
*) 0;
237 extern bfd_error_vector_type bfd_error_vector
;
241 Internal Entry Points
244 @code{aoutx.h} exports several routines for accessing the
245 contents of an a.out file, which are gathered and exported in
246 turn by various format specific files (eg sunos.c).
252 aout_<size>_swap_exec_header_in
255 Swaps the information in an executable header taken from a raw
256 byte stream memory image, into the internal exec_header
260 void aout_<size>_swap_exec_header_in,
262 struct external_exec *raw_bytes,
263 struct internal_exec *execp);
266 #ifndef NAME_swap_exec_header_in
268 DEFUN(NAME(aout
,swap_exec_header_in
),(abfd
, raw_bytes
, execp
),
270 struct external_exec
*raw_bytes AND
271 struct internal_exec
*execp
)
273 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
275 /* The internal_exec structure has some fields that are unused in this
276 configuration (IE for i960), so ensure that all such uninitialized
277 fields are zero'd out. There are places where two of these structs
278 are memcmp'd, and thus the contents do matter. */
279 memset (execp
, 0, sizeof (struct internal_exec
));
280 /* Now fill in fields in the execp, from the bytes in the raw data. */
281 execp
->a_info
= bfd_h_get_32 (abfd
, bytes
->e_info
);
282 execp
->a_text
= GET_WORD (abfd
, bytes
->e_text
);
283 execp
->a_data
= GET_WORD (abfd
, bytes
->e_data
);
284 execp
->a_bss
= GET_WORD (abfd
, bytes
->e_bss
);
285 execp
->a_syms
= GET_WORD (abfd
, bytes
->e_syms
);
286 execp
->a_entry
= GET_WORD (abfd
, bytes
->e_entry
);
287 execp
->a_trsize
= GET_WORD (abfd
, bytes
->e_trsize
);
288 execp
->a_drsize
= GET_WORD (abfd
, bytes
->e_drsize
);
290 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
295 aout_<size>_swap_exec_header_out
298 Swaps the information in an internal exec header structure
299 into the supplied buffer ready for writing to disk.
302 void aout_<size>_swap_exec_header_out
304 struct internal_exec *execp,
305 struct external_exec *raw_bytes);
308 DEFUN(NAME(aout
,swap_exec_header_out
),(abfd
, execp
, raw_bytes
),
310 struct internal_exec
*execp AND
311 struct external_exec
*raw_bytes
)
313 struct external_exec
*bytes
= (struct external_exec
*)raw_bytes
;
315 /* Now fill in fields in the raw data, from the fields in the exec struct. */
316 bfd_h_put_32 (abfd
, execp
->a_info
, bytes
->e_info
);
317 PUT_WORD (abfd
, execp
->a_text
, bytes
->e_text
);
318 PUT_WORD (abfd
, execp
->a_data
, bytes
->e_data
);
319 PUT_WORD (abfd
, execp
->a_bss
, bytes
->e_bss
);
320 PUT_WORD (abfd
, execp
->a_syms
, bytes
->e_syms
);
321 PUT_WORD (abfd
, execp
->a_entry
, bytes
->e_entry
);
322 PUT_WORD (abfd
, execp
->a_trsize
, bytes
->e_trsize
);
323 PUT_WORD (abfd
, execp
->a_drsize
, bytes
->e_drsize
);
330 aout_<size>_some_aout_object_p
333 Some A.OUT variant thinks that the file whose format we're
334 checking is an a.out file. Do some more checking, and set up
335 for access if it really is. Call back to the calling
336 environments "finish up" function just before returning, to
337 handle any last-minute setup.
340 bfd_target *aout_<size>_some_aout_object_p
342 bfd_target *(*callback_to_real_object_p)());
346 DEFUN(NAME(aout
,some_aout_object_p
),(abfd
, execp
, callback_to_real_object_p
),
348 struct internal_exec
*execp AND
349 bfd_target
*(*callback_to_real_object_p
) PARAMS ((bfd
*)))
351 struct aout_data_struct
*rawptr
, *oldrawptr
;
354 rawptr
= (struct aout_data_struct
*) bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
355 if (rawptr
== NULL
) {
356 bfd_error
= no_memory
;
360 oldrawptr
= abfd
->tdata
.aout_data
;
361 abfd
->tdata
.aout_data
= rawptr
;
363 /* Copy the contents of the old tdata struct.
364 In particular, we want the subformat, since for hpux it was set in
365 hp300hpux.c:swap_exec_header_in and will be used in
366 hp300hpux.c:callback. */
367 if (oldrawptr
!= NULL
)
368 *abfd
->tdata
.aout_data
= *oldrawptr
;
370 abfd
->tdata
.aout_data
->a
.hdr
= &rawptr
->e
;
371 *(abfd
->tdata
.aout_data
->a
.hdr
) = *execp
; /* Copy in the internal_exec struct */
372 execp
= abfd
->tdata
.aout_data
->a
.hdr
;
374 /* Set the file flags */
375 abfd
->flags
= NO_FLAGS
;
376 if (execp
->a_drsize
|| execp
->a_trsize
)
377 abfd
->flags
|= HAS_RELOC
;
378 /* Setting of EXEC_P has been deferred to the bottom of this function */
380 abfd
->flags
|= HAS_LINENO
| HAS_DEBUG
| HAS_SYMS
| HAS_LOCALS
;
382 if (N_MAGIC (*execp
) == ZMAGIC
)
384 abfd
->flags
|= D_PAGED
|WP_TEXT
;
385 adata(abfd
).magic
= z_magic
;
387 else if (N_MAGIC (*execp
) == NMAGIC
)
389 abfd
->flags
|= WP_TEXT
;
390 adata(abfd
).magic
= n_magic
;
393 adata(abfd
).magic
= o_magic
;
395 bfd_get_start_address (abfd
) = execp
->a_entry
;
397 obj_aout_symbols (abfd
) = (aout_symbol_type
*)NULL
;
398 bfd_get_symcount (abfd
) = execp
->a_syms
/ sizeof (struct external_nlist
);
400 /* The default relocation entry size is that of traditional V7 Unix. */
401 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
403 /* The default symbol entry size is that of traditional Unix. */
404 obj_symbol_entry_size (abfd
) = EXTERNAL_NLIST_SIZE
;
406 /* Create the sections. This is raunchy, but bfd_close wants to reclaim
409 obj_textsec (abfd
) = bfd_make_section_old_way (abfd
, ".text");
410 obj_datasec (abfd
) = bfd_make_section_old_way (abfd
, ".data");
411 obj_bsssec (abfd
) = bfd_make_section_old_way (abfd
, ".bss");
414 (void)bfd_make_section (abfd
, ".text");
415 (void)bfd_make_section (abfd
, ".data");
416 (void)bfd_make_section (abfd
, ".bss");
419 obj_datasec (abfd
)->_raw_size
= execp
->a_data
;
420 obj_bsssec (abfd
)->_raw_size
= execp
->a_bss
;
422 obj_textsec (abfd
)->flags
= (execp
->a_trsize
!= 0 ?
423 (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
| SEC_RELOC
) :
424 (SEC_ALLOC
| SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
));
425 obj_datasec (abfd
)->flags
= (execp
->a_drsize
!= 0 ?
426 (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
| SEC_RELOC
) :
427 (SEC_ALLOC
| SEC_LOAD
| SEC_DATA
| SEC_HAS_CONTENTS
));
428 obj_bsssec (abfd
)->flags
= SEC_ALLOC
;
430 #ifdef THIS_IS_ONLY_DOCUMENTATION
431 /* The common code can't fill in these things because they depend
432 on either the start address of the text segment, the rounding
433 up of virtual addersses between segments, or the starting file
434 position of the text segment -- all of which varies among different
435 versions of a.out. */
437 /* Call back to the format-dependent code to fill in the rest of the
438 fields and do any further cleanup. Things that should be filled
439 in by the callback: */
441 struct exec
*execp
= exec_hdr (abfd
);
443 obj_textsec (abfd
)->size
= N_TXTSIZE(*execp
);
444 obj_textsec (abfd
)->raw_size
= N_TXTSIZE(*execp
);
445 /* data and bss are already filled in since they're so standard */
447 /* The virtual memory addresses of the sections */
448 obj_textsec (abfd
)->vma
= N_TXTADDR(*execp
);
449 obj_datasec (abfd
)->vma
= N_DATADDR(*execp
);
450 obj_bsssec (abfd
)->vma
= N_BSSADDR(*execp
);
452 /* The file offsets of the sections */
453 obj_textsec (abfd
)->filepos
= N_TXTOFF(*execp
);
454 obj_datasec (abfd
)->filepos
= N_DATOFF(*execp
);
456 /* The file offsets of the relocation info */
457 obj_textsec (abfd
)->rel_filepos
= N_TRELOFF(*execp
);
458 obj_datasec (abfd
)->rel_filepos
= N_DRELOFF(*execp
);
460 /* The file offsets of the string table and symbol table. */
461 obj_str_filepos (abfd
) = N_STROFF (*execp
);
462 obj_sym_filepos (abfd
) = N_SYMOFF (*execp
);
464 /* Determine the architecture and machine type of the object file. */
465 switch (N_MACHTYPE (*exec_hdr (abfd
))) {
467 abfd
->obj_arch
= bfd_arch_obscure
;
471 adata(abfd
)->page_size
= PAGE_SIZE
;
472 adata(abfd
)->segment_size
= SEGMENT_SIZE
;
473 adata(abfd
)->exec_bytes_size
= EXEC_BYTES_SIZE
;
477 /* The architecture is encoded in various ways in various a.out variants,
478 or is not encoded at all in some of them. The relocation size depends
479 on the architecture and the a.out variant. Finally, the return value
480 is the bfd_target vector in use. If an error occurs, return zero and
481 set bfd_error to the appropriate error code.
483 Formats such as b.out, which have additional fields in the a.out
484 header, should cope with them in this callback as well. */
485 #endif /* DOCUMENTATION */
487 result
= (*callback_to_real_object_p
)(abfd
);
489 /* Now that the segment addresses have been worked out, take a better
490 guess at whether the file is executable. If the entry point
491 is within the text segment, assume it is. (This makes files
492 executable even if their entry point address is 0, as long as
493 their text starts at zero.)
495 At some point we should probably break down and stat the file and
496 declare it executable if (one of) its 'x' bits are on... */
497 if ((execp
->a_entry
>= obj_textsec(abfd
)->vma
) &&
498 (execp
->a_entry
< obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
))
499 abfd
->flags
|= EXEC_P
;
502 #if 0 /* These should be set correctly anyways. */
503 abfd
->sections
= obj_textsec (abfd
);
504 obj_textsec (abfd
)->next
= obj_datasec (abfd
);
505 obj_datasec (abfd
)->next
= obj_bsssec (abfd
);
511 abfd
->tdata
.aout_data
= oldrawptr
;
521 This routine initializes a BFD for use with a.out files.
524 boolean aout_<size>_mkobject, (bfd *);
528 DEFUN(NAME(aout
,mkobject
),(abfd
),
531 struct aout_data_struct
*rawptr
;
533 bfd_error
= system_call_error
;
535 /* Use an intermediate variable for clarity */
536 rawptr
= (struct aout_data_struct
*)bfd_zalloc (abfd
, sizeof (struct aout_data_struct
));
538 if (rawptr
== NULL
) {
539 bfd_error
= no_memory
;
543 abfd
->tdata
.aout_data
= rawptr
;
544 exec_hdr (abfd
) = &(rawptr
->e
);
546 /* For simplicity's sake we just make all the sections right here. */
548 obj_textsec (abfd
) = (asection
*)NULL
;
549 obj_datasec (abfd
) = (asection
*)NULL
;
550 obj_bsssec (abfd
) = (asection
*)NULL
;
551 bfd_make_section (abfd
, ".text");
552 bfd_make_section (abfd
, ".data");
553 bfd_make_section (abfd
, ".bss");
554 bfd_make_section (abfd
, BFD_ABS_SECTION_NAME
);
555 bfd_make_section (abfd
, BFD_UND_SECTION_NAME
);
556 bfd_make_section (abfd
, BFD_COM_SECTION_NAME
);
564 aout_<size>_machine_type
567 Keep track of machine architecture and machine type for
568 a.out's. Return the machine_type for a particular
569 arch&machine, or M_UNKNOWN if that exact arch&machine can't be
570 represented in a.out format.
572 If the architecture is understood, machine type 0 (default)
573 should always be understood.
576 enum machine_type aout_<size>_machine_type
577 (enum bfd_architecture arch,
578 unsigned long machine));
582 DEFUN(NAME(aout
,machine_type
),(arch
, machine
),
583 enum bfd_architecture arch AND
584 unsigned long machine
)
586 enum machine_type arch_flags
;
588 arch_flags
= M_UNKNOWN
;
592 if (machine
== 0) arch_flags
= M_SPARC
;
597 case 0: arch_flags
= M_68010
; break;
598 case 68000: arch_flags
= M_UNKNOWN
; break;
599 case 68010: arch_flags
= M_68010
; break;
600 case 68020: arch_flags
= M_68020
; break;
601 default: arch_flags
= M_UNKNOWN
; break;
606 if (machine
== 0) arch_flags
= M_386
;
610 if (machine
== 0) arch_flags
= M_29K
;
617 case 3000: arch_flags
= M_MIPS1
; break;
620 case 6000: arch_flags
= M_MIPS2
; break;
621 default: arch_flags
= M_UNKNOWN
; break;
626 arch_flags
= M_UNKNOWN
;
634 aout_<size>_set_arch_mach
637 Sets the architecture and the machine of the BFD to those
638 values supplied. Verifies that the format can support the
639 architecture required.
642 boolean aout_<size>_set_arch_mach,
644 enum bfd_architecture,
645 unsigned long machine));
649 DEFUN(NAME(aout
,set_arch_mach
),(abfd
, arch
, machine
),
651 enum bfd_architecture arch AND
652 unsigned long machine
)
654 if (! bfd_default_set_arch_mach (abfd
, arch
, machine
))
657 if (arch
!= bfd_arch_unknown
&&
658 NAME(aout
,machine_type
) (arch
, machine
) == M_UNKNOWN
)
659 return false; /* We can't represent this type */
661 /* Determine the size of a relocation entry */
666 obj_reloc_entry_size (abfd
) = RELOC_EXT_SIZE
;
669 obj_reloc_entry_size (abfd
) = RELOC_STD_SIZE
;
673 return (*aout_backend_info(abfd
)->set_sizes
) (abfd
);
677 DEFUN (NAME (aout
,adjust_sizes_and_vmas
), (abfd
, text_size
, text_end
),
678 bfd
*abfd AND bfd_size_type
*text_size AND file_ptr
*text_end
)
680 struct internal_exec
*execp
= exec_hdr (abfd
);
681 if ((obj_textsec (abfd
) == NULL
) || (obj_datasec (abfd
) == NULL
))
683 bfd_error
= invalid_operation
;
686 if (adata(abfd
).magic
!= undecided_magic
) return true;
687 obj_textsec(abfd
)->_raw_size
=
688 align_power(obj_textsec(abfd
)->_raw_size
,
689 obj_textsec(abfd
)->alignment_power
);
691 *text_size
= obj_textsec (abfd
)->_raw_size
;
692 /* Rule (heuristic) for when to pad to a new page. Note that there
693 * are (at least) two ways demand-paged (ZMAGIC) files have been
694 * handled. Most Berkeley-based systems start the text segment at
695 * (PAGE_SIZE). However, newer versions of SUNOS start the text
696 * segment right after the exec header; the latter is counted in the
697 * text segment size, and is paged in by the kernel with the rest of
700 /* This perhaps isn't the right way to do this, but made it simpler for me
701 to understand enough to implement it. Better would probably be to go
702 right from BFD flags to alignment/positioning characteristics. But the
703 old code was sloppy enough about handling the flags, and had enough
704 other magic, that it was a little hard for me to understand. I think
705 I understand it better now, but I haven't time to do the cleanup this
707 if (adata(abfd
).magic
== undecided_magic
)
709 if (abfd
->flags
& D_PAGED
)
710 /* Whether or not WP_TEXT is set -- let D_PAGED override. */
711 /* @@ What about QMAGIC? */
712 adata(abfd
).magic
= z_magic
;
713 else if (abfd
->flags
& WP_TEXT
)
714 adata(abfd
).magic
= n_magic
;
716 adata(abfd
).magic
= o_magic
;
719 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
721 fprintf (stderr
, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
723 switch (adata(abfd
).magic
) {
724 case n_magic
: str
= "NMAGIC"; break;
725 case o_magic
: str
= "OMAGIC"; break;
726 case z_magic
: str
= "ZMAGIC"; break;
731 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
, obj_textsec(abfd
)->alignment_power
,
732 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
, obj_datasec(abfd
)->alignment_power
,
733 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
, obj_bsssec(abfd
)->alignment_power
);
737 switch (adata(abfd
).magic
)
741 file_ptr pos
= adata (abfd
).exec_bytes_size
;
745 obj_textsec(abfd
)->filepos
= pos
;
746 pos
+= obj_textsec(abfd
)->_raw_size
;
747 vma
+= obj_textsec(abfd
)->_raw_size
;
748 if (!obj_datasec(abfd
)->user_set_vma
)
750 #if 0 /* ?? Does alignment in the file image really matter? */
751 pad
= align_power (vma
, obj_datasec(abfd
)->alignment_power
) - vma
;
753 obj_textsec(abfd
)->_raw_size
+= pad
;
756 obj_datasec(abfd
)->vma
= vma
;
758 obj_datasec(abfd
)->filepos
= pos
;
759 pos
+= obj_datasec(abfd
)->_raw_size
;
760 vma
+= obj_datasec(abfd
)->_raw_size
;
761 if (!obj_bsssec(abfd
)->user_set_vma
)
764 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
766 obj_datasec(abfd
)->_raw_size
+= pad
;
769 obj_bsssec(abfd
)->vma
= vma
;
771 obj_bsssec(abfd
)->filepos
= pos
;
772 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
773 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
774 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
775 N_SET_MAGIC (*execp
, OMAGIC
);
780 bfd_size_type data_pad
, text_pad
;
782 CONST
struct aout_backend_data
*abdp
;
786 abdp
= aout_backend_info (abfd
);
787 ztih
= abdp
&& abdp
->text_includes_header
;
788 obj_textsec(abfd
)->filepos
= (ztih
789 ? adata(abfd
).exec_bytes_size
790 : adata(abfd
).page_size
);
791 if (! obj_textsec(abfd
)->user_set_vma
)
792 /* ?? Do we really need to check for relocs here? */
793 obj_textsec(abfd
)->vma
= ((abfd
->flags
& HAS_RELOC
)
796 ? (abdp
->default_text_vma
797 + adata(abfd
).exec_bytes_size
)
798 : abdp
->default_text_vma
));
799 /* Could take strange alignment of text section into account here? */
801 /* Find start of data. */
802 text_end
= obj_textsec(abfd
)->filepos
+ obj_textsec(abfd
)->_raw_size
;
803 text_pad
= BFD_ALIGN (text_end
, adata(abfd
).page_size
) - text_end
;
804 obj_textsec(abfd
)->_raw_size
+= text_pad
;
805 text_end
+= text_pad
;
807 if (!obj_datasec(abfd
)->user_set_vma
)
810 vma
= obj_textsec(abfd
)->vma
+ obj_textsec(abfd
)->_raw_size
;
811 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
813 data_vma
= obj_datasec(abfd
)->vma
;
814 if (abdp
&& abdp
->zmagic_mapped_contiguous
)
816 text_pad
= (obj_datasec(abfd
)->vma
817 - obj_textsec(abfd
)->vma
818 - obj_textsec(abfd
)->_raw_size
);
819 obj_textsec(abfd
)->_raw_size
+= text_pad
;
821 obj_datasec(abfd
)->filepos
= (obj_textsec(abfd
)->filepos
822 + obj_textsec(abfd
)->_raw_size
);
824 /* Fix up exec header while we're at it. */
825 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
826 if (ztih
&& (!abdp
|| (abdp
&& !abdp
->exec_header_not_counted
)))
827 execp
->a_text
+= adata(abfd
).exec_bytes_size
;
828 N_SET_MAGIC (*execp
, ZMAGIC
);
829 /* Spec says data section should be rounded up to page boundary. */
830 /* If extra space in page is left after data section, fudge data
831 in the header so that the bss section looks smaller by that
832 amount. We'll start the bss section there, and lie to the OS. */
833 obj_datasec(abfd
)->_raw_size
834 = align_power (obj_datasec(abfd
)->_raw_size
,
835 obj_bsssec(abfd
)->alignment_power
);
836 execp
->a_data
= BFD_ALIGN (obj_datasec(abfd
)->_raw_size
,
837 adata(abfd
).page_size
);
838 data_pad
= execp
->a_data
- obj_datasec(abfd
)->_raw_size
;
840 if (!obj_bsssec(abfd
)->user_set_vma
)
841 obj_bsssec(abfd
)->vma
= (obj_datasec(abfd
)->vma
842 + obj_datasec(abfd
)->_raw_size
);
843 if (data_pad
> obj_bsssec(abfd
)->_raw_size
)
846 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
- data_pad
;
851 file_ptr pos
= adata(abfd
).exec_bytes_size
;
855 obj_textsec(abfd
)->filepos
= pos
;
856 if (!obj_textsec(abfd
)->user_set_vma
)
857 obj_textsec(abfd
)->vma
= vma
;
859 vma
= obj_textsec(abfd
)->vma
;
860 pos
+= obj_textsec(abfd
)->_raw_size
;
861 vma
+= obj_textsec(abfd
)->_raw_size
;
862 obj_datasec(abfd
)->filepos
= pos
;
863 if (!obj_datasec(abfd
)->user_set_vma
)
864 obj_datasec(abfd
)->vma
= BFD_ALIGN (vma
, adata(abfd
).segment_size
);
865 vma
= obj_datasec(abfd
)->vma
;
867 /* Since BSS follows data immediately, see if it needs alignment. */
868 vma
+= obj_datasec(abfd
)->_raw_size
;
869 pad
= align_power (vma
, obj_bsssec(abfd
)->alignment_power
) - vma
;
870 obj_datasec(abfd
)->_raw_size
+= pad
;
871 pos
+= obj_datasec(abfd
)->_raw_size
;
873 if (!obj_bsssec(abfd
)->user_set_vma
)
874 obj_bsssec(abfd
)->vma
= vma
;
876 vma
= obj_bsssec(abfd
)->vma
;
878 execp
->a_text
= obj_textsec(abfd
)->_raw_size
;
879 execp
->a_data
= obj_datasec(abfd
)->_raw_size
;
880 execp
->a_bss
= obj_bsssec(abfd
)->_raw_size
;
881 N_SET_MAGIC (*execp
, NMAGIC
);
886 #ifdef BFD_AOUT_DEBUG
887 fprintf (stderr
, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
888 obj_textsec(abfd
)->vma
, obj_textsec(abfd
)->_raw_size
, obj_textsec(abfd
)->filepos
,
889 obj_datasec(abfd
)->vma
, obj_datasec(abfd
)->_raw_size
, obj_datasec(abfd
)->filepos
,
890 obj_bsssec(abfd
)->vma
, obj_bsssec(abfd
)->_raw_size
);
897 aout_<size>_new_section_hook
900 Called by the BFD in response to a @code{bfd_make_section}
904 boolean aout_<size>_new_section_hook,
909 DEFUN(NAME(aout
,new_section_hook
),(abfd
, newsect
),
913 /* align to double at least */
914 newsect
->alignment_power
= bfd_get_arch_info(abfd
)->section_align_power
;
917 if (bfd_get_format (abfd
) == bfd_object
)
919 if (obj_textsec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".text")) {
920 obj_textsec(abfd
)= newsect
;
921 newsect
->target_index
= N_TEXT
| N_EXT
;
925 if (obj_datasec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".data")) {
926 obj_datasec(abfd
) = newsect
;
927 newsect
->target_index
= N_DATA
| N_EXT
;
931 if (obj_bsssec(abfd
) == NULL
&& !strcmp(newsect
->name
, ".bss")) {
932 obj_bsssec(abfd
) = newsect
;
933 newsect
->target_index
= N_BSS
| N_EXT
;
939 /* We allow more than three sections internally */
944 DEFUN(NAME(aout
,set_section_contents
),(abfd
, section
, location
, offset
, count
),
952 bfd_size_type text_size
;
954 if (abfd
->output_has_begun
== false)
956 if (NAME(aout
,adjust_sizes_and_vmas
) (abfd
,
962 /* regardless, once we know what we're doing, we might as well get going */
963 if (section
!= obj_bsssec(abfd
))
965 bfd_seek (abfd
, section
->filepos
+ offset
, SEEK_SET
);
968 return (bfd_write ((PTR
)location
, 1, count
, abfd
) == count
) ?
976 /* Classify stabs symbols */
978 #define sym_in_text_section(sym) \
979 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
981 #define sym_in_data_section(sym) \
982 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
984 #define sym_in_bss_section(sym) \
985 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
987 /* Symbol is undefined if type is N_UNDF|N_EXT and if it has
988 zero in the "value" field. Nonzeroes there are fortrancommon
990 #define sym_is_undefined(sym) \
991 ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0)
993 /* Symbol is a global definition if N_EXT is on and if it has
994 a nonzero type field. */
995 #define sym_is_global_defn(sym) \
996 (((sym)->type & N_EXT) && (sym)->type & N_TYPE)
998 /* Symbol is debugger info if any bits outside N_TYPE or N_EXT
1000 #define sym_is_debugger_info(sym) \
1001 ((sym)->type & ~(N_EXT | N_TYPE))
1003 #define sym_is_fortrancommon(sym) \
1004 (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0)
1006 /* Symbol is absolute if it has N_ABS set */
1007 #define sym_is_absolute(sym) \
1008 (((sym)->type & N_TYPE)== N_ABS)
1011 #define sym_is_indirect(sym) \
1012 (((sym)->type & N_ABS)== N_ABS)
1014 /* Only in their own functions for ease of debugging; when sym flags have
1015 stabilised these should be inlined into their (single) caller */
1018 DEFUN (translate_from_native_sym_flags
, (sym_pointer
, cache_ptr
, abfd
),
1019 struct external_nlist
*sym_pointer AND
1020 aout_symbol_type
* cache_ptr AND
1023 cache_ptr
->symbol
.section
= 0;
1024 switch (cache_ptr
->type
& N_TYPE
)
1031 char *copy
= bfd_alloc (abfd
, strlen (cache_ptr
->symbol
.name
) + 1);
1033 asection
*into_section
;
1035 arelent_chain
*reloc
= (arelent_chain
*) bfd_alloc (abfd
, sizeof (arelent_chain
));
1036 strcpy (copy
, cache_ptr
->symbol
.name
);
1038 /* Make sure that this bfd has a section with the right contructor
1040 section
= bfd_get_section_by_name (abfd
, copy
);
1042 section
= bfd_make_section (abfd
, copy
);
1044 /* Build a relocation entry for the constructor */
1045 switch ((cache_ptr
->type
& N_TYPE
))
1048 into_section
= &bfd_abs_section
;
1049 cache_ptr
->type
= N_ABS
;
1052 into_section
= (asection
*) obj_textsec (abfd
);
1053 cache_ptr
->type
= N_TEXT
;
1056 into_section
= (asection
*) obj_datasec (abfd
);
1057 cache_ptr
->type
= N_DATA
;
1060 into_section
= (asection
*) obj_bsssec (abfd
);
1061 cache_ptr
->type
= N_BSS
;
1067 /* Build a relocation pointing into the constuctor section
1068 pointing at the symbol in the set vector specified */
1070 reloc
->relent
.addend
= cache_ptr
->symbol
.value
;
1071 cache_ptr
->symbol
.section
= into_section
->symbol
->section
;
1072 reloc
->relent
.sym_ptr_ptr
= into_section
->symbol_ptr_ptr
;
1075 /* We modify the symbol to belong to a section depending upon the
1076 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
1077 really care, and add to the size of the section to contain a
1078 pointer to the symbol. Build a reloc entry to relocate to this
1079 symbol attached to this section. */
1081 section
->flags
= SEC_CONSTRUCTOR
;
1084 section
->reloc_count
++;
1085 section
->alignment_power
= 2;
1087 reloc
->next
= section
->constructor_chain
;
1088 section
->constructor_chain
= reloc
;
1089 reloc
->relent
.address
= section
->_raw_size
;
1090 section
->_raw_size
+= sizeof (int *);
1093 = (obj_reloc_entry_size(abfd
) == RELOC_EXT_SIZE
1094 ? howto_table_ext
: howto_table_std
)
1095 + CTOR_TABLE_RELOC_IDX
;
1096 cache_ptr
->symbol
.flags
|= BSF_CONSTRUCTOR
;
1100 if (cache_ptr
->type
== N_WARNING
)
1102 /* This symbol is the text of a warning message, the next symbol
1103 is the symbol to associate the warning with */
1104 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_WARNING
;
1106 /* @@ Stuffing pointers into integers is a no-no.
1107 We can usually get away with it if the integer is
1108 large enough though. */
1109 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1111 cache_ptr
->symbol
.value
= (bfd_vma
) ((cache_ptr
+ 1));
1113 /* We furgle with the next symbol in place.
1114 We don't want it to be undefined, we'll trample the type */
1115 (sym_pointer
+ 1)->e_type
[0] = 0xff;
1118 if ((cache_ptr
->type
| N_EXT
) == (N_INDR
| N_EXT
))
1120 /* Two symbols in a row for an INDR message. The first symbol
1121 contains the name we will match, the second symbol contains
1122 the name the first name is translated into. It is supplied to
1123 us undefined. This is good, since we want to pull in any files
1125 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
| BSF_INDIRECT
;
1127 /* @@ Stuffing pointers into integers is a no-no.
1128 We can usually get away with it if the integer is
1129 large enough though. */
1130 if (sizeof (cache_ptr
+ 1) > sizeof (bfd_vma
))
1133 cache_ptr
->symbol
.value
= (bfd_vma
) ((cache_ptr
+ 1));
1134 cache_ptr
->symbol
.section
= &bfd_ind_section
;
1137 else if (sym_is_debugger_info (cache_ptr
))
1139 cache_ptr
->symbol
.flags
= BSF_DEBUGGING
;
1140 /* Work out the section correct for this symbol */
1141 switch (cache_ptr
->type
& N_TYPE
)
1145 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1146 cache_ptr
->symbol
.value
-= obj_textsec (abfd
)->vma
;
1149 cache_ptr
->symbol
.value
-= obj_datasec (abfd
)->vma
;
1150 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1153 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1154 cache_ptr
->symbol
.value
-= obj_bsssec (abfd
)->vma
;
1159 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1166 if (sym_is_fortrancommon (cache_ptr
))
1168 cache_ptr
->symbol
.flags
= 0;
1169 cache_ptr
->symbol
.section
= &bfd_com_section
;
1177 /* In a.out, the value of a symbol is always relative to the
1178 * start of the file, if this is a data symbol we'll subtract
1179 * the size of the text section to get the section relative
1180 * value. If this is a bss symbol (which would be strange)
1181 * we'll subtract the size of the previous two sections
1182 * to find the section relative address.
1185 if (sym_in_text_section (cache_ptr
))
1187 cache_ptr
->symbol
.value
-= obj_textsec (abfd
)->vma
;
1188 cache_ptr
->symbol
.section
= obj_textsec (abfd
);
1190 else if (sym_in_data_section (cache_ptr
))
1192 cache_ptr
->symbol
.value
-= obj_datasec (abfd
)->vma
;
1193 cache_ptr
->symbol
.section
= obj_datasec (abfd
);
1195 else if (sym_in_bss_section (cache_ptr
))
1197 cache_ptr
->symbol
.section
= obj_bsssec (abfd
);
1198 cache_ptr
->symbol
.value
-= obj_bsssec (abfd
)->vma
;
1200 else if (sym_is_undefined (cache_ptr
))
1202 cache_ptr
->symbol
.flags
= 0;
1203 cache_ptr
->symbol
.section
= &bfd_und_section
;
1205 else if (sym_is_absolute (cache_ptr
))
1207 cache_ptr
->symbol
.section
= &bfd_abs_section
;
1210 if (sym_is_global_defn (cache_ptr
))
1212 cache_ptr
->symbol
.flags
= BSF_GLOBAL
| BSF_EXPORT
;
1216 cache_ptr
->symbol
.flags
= BSF_LOCAL
;
1220 if (cache_ptr
->symbol
.section
== 0)
1227 DEFUN(translate_to_native_sym_flags
,(sym_pointer
, cache_ptr
, abfd
),
1228 struct external_nlist
*sym_pointer AND
1229 asymbol
*cache_ptr AND
1232 bfd_vma value
= cache_ptr
->value
;
1234 /* mask out any existing type bits in case copying from one section
1236 sym_pointer
->e_type
[0] &= ~N_TYPE
;
1239 /* We attempt to order these tests by decreasing frequency of success,
1240 according to tcov when linking the linker. */
1241 if (bfd_get_output_section(cache_ptr
) == &bfd_abs_section
) {
1242 sym_pointer
->e_type
[0] |= N_ABS
;
1244 else if (bfd_get_output_section(cache_ptr
) == obj_textsec (abfd
)) {
1245 sym_pointer
->e_type
[0] |= N_TEXT
;
1247 else if (bfd_get_output_section(cache_ptr
) == obj_datasec (abfd
)) {
1248 sym_pointer
->e_type
[0] |= N_DATA
;
1250 else if (bfd_get_output_section(cache_ptr
) == obj_bsssec (abfd
)) {
1251 sym_pointer
->e_type
[0] |= N_BSS
;
1253 else if (bfd_get_output_section(cache_ptr
) == &bfd_und_section
)
1255 sym_pointer
->e_type
[0] = (N_UNDF
| N_EXT
);
1257 else if (bfd_get_output_section(cache_ptr
) == &bfd_ind_section
)
1259 sym_pointer
->e_type
[0] = N_INDR
;
1261 else if (bfd_is_com_section (bfd_get_output_section (cache_ptr
))) {
1262 sym_pointer
->e_type
[0] = (N_UNDF
| N_EXT
);
1265 if (cache_ptr
->section
->output_section
)
1268 bfd_error_vector
.nonrepresentable_section(abfd
,
1269 bfd_get_output_section(cache_ptr
)->name
);
1273 bfd_error_vector
.nonrepresentable_section(abfd
,
1274 cache_ptr
->section
->name
);
1279 /* Turn the symbol from section relative to absolute again */
1281 value
+= cache_ptr
->section
->output_section
->vma
+ cache_ptr
->section
->output_offset
;
1284 if (cache_ptr
->flags
& (BSF_WARNING
)) {
1285 (sym_pointer
+1)->e_type
[0] = 1;
1288 if (cache_ptr
->flags
& BSF_DEBUGGING
) {
1289 sym_pointer
->e_type
[0] = ((aout_symbol_type
*)cache_ptr
)->type
;
1291 else if (cache_ptr
->flags
& (BSF_GLOBAL
| BSF_EXPORT
)) {
1292 sym_pointer
->e_type
[0] |= N_EXT
;
1294 if (cache_ptr
->flags
& BSF_CONSTRUCTOR
) {
1295 int type
= ((aout_symbol_type
*)cache_ptr
)->type
;
1298 case N_ABS
: type
= N_SETA
; break;
1299 case N_TEXT
: type
= N_SETT
; break;
1300 case N_DATA
: type
= N_SETD
; break;
1301 case N_BSS
: type
= N_SETB
; break;
1303 sym_pointer
->e_type
[0] = type
;
1306 PUT_WORD(abfd
, value
, sym_pointer
->e_value
);
1309 /* Native-level interface to symbols. */
1311 /* We read the symbols into a buffer, which is discarded when this
1312 function exits. We read the strings into a buffer large enough to
1313 hold them all plus all the cached symbol entries. */
1316 DEFUN(NAME(aout
,make_empty_symbol
),(abfd
),
1319 aout_symbol_type
*new =
1320 (aout_symbol_type
*)bfd_zalloc (abfd
, sizeof (aout_symbol_type
));
1321 new->symbol
.the_bfd
= abfd
;
1323 return &new->symbol
;
1327 DEFUN(NAME(aout
,slurp_symbol_table
),(abfd
),
1330 bfd_size_type symbol_size
;
1331 bfd_size_type string_size
;
1332 unsigned char string_chars
[BYTES_IN_WORD
];
1333 struct external_nlist
*syms
;
1335 aout_symbol_type
*cached
;
1337 /* If there's no work to be done, don't do any */
1338 if (obj_aout_symbols (abfd
) != (aout_symbol_type
*)NULL
) return true;
1339 symbol_size
= exec_hdr(abfd
)->a_syms
;
1340 if (symbol_size
== 0)
1342 bfd_error
= no_symbols
;
1346 bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
);
1347 if (bfd_read ((PTR
)string_chars
, BYTES_IN_WORD
, 1, abfd
) != BYTES_IN_WORD
)
1349 string_size
= GET_WORD (abfd
, string_chars
);
1351 strings
=(char *) bfd_alloc(abfd
, string_size
+ 1);
1352 cached
= (aout_symbol_type
*)
1353 bfd_zalloc(abfd
, (bfd_size_type
)(bfd_get_symcount (abfd
) * sizeof(aout_symbol_type
)));
1355 /* malloc this, so we can free it if simply. The symbol caching
1356 might want to allocate onto the bfd's obstack */
1357 syms
= (struct external_nlist
*) bfd_xmalloc(symbol_size
);
1358 bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
);
1359 if (bfd_read ((PTR
)syms
, 1, symbol_size
, abfd
) != symbol_size
)
1365 bfd_release (abfd
, cached
);
1367 bfd_release (abfd
, strings
);
1371 bfd_seek (abfd
, obj_str_filepos (abfd
), SEEK_SET
);
1372 if (bfd_read ((PTR
)strings
, 1, string_size
, abfd
) != string_size
)
1376 strings
[string_size
] = 0; /* Just in case. */
1378 /* OK, now walk the new symtable, cacheing symbol properties */
1380 register struct external_nlist
*sym_pointer
;
1381 register struct external_nlist
*sym_end
= syms
+ bfd_get_symcount (abfd
);
1382 register aout_symbol_type
*cache_ptr
= cached
;
1384 /* Run through table and copy values */
1385 for (sym_pointer
= syms
, cache_ptr
= cached
;
1386 sym_pointer
< sym_end
; sym_pointer
++, cache_ptr
++)
1388 long x
= GET_WORD(abfd
, sym_pointer
->e_strx
);
1389 cache_ptr
->symbol
.the_bfd
= abfd
;
1391 cache_ptr
->symbol
.name
= "";
1392 else if (x
>= 0 && x
< string_size
)
1393 cache_ptr
->symbol
.name
= x
+ strings
;
1397 cache_ptr
->symbol
.value
= GET_SWORD(abfd
, sym_pointer
->e_value
);
1398 cache_ptr
->desc
= bfd_h_get_16(abfd
, sym_pointer
->e_desc
);
1399 cache_ptr
->other
= bfd_h_get_8(abfd
, sym_pointer
->e_other
);
1400 cache_ptr
->type
= bfd_h_get_8(abfd
, sym_pointer
->e_type
);
1401 cache_ptr
->symbol
.udata
= 0;
1402 translate_from_native_sym_flags (sym_pointer
, cache_ptr
, abfd
);
1406 obj_aout_symbols (abfd
) = cached
;
1413 /* Possible improvements:
1414 + look for strings matching trailing substrings of other strings
1415 + better data structures? balanced trees?
1416 + smaller per-string or per-symbol data? re-use some of the symbol's
1418 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1419 construct the entire symbol table at once, we could get by with smaller
1420 amounts of VM? (What effect does that have on the string table
1422 + rip this out of here, put it into its own file in bfd or libiberty, so
1423 coff and elf can use it too. I'll work on this soon, but have more
1424 pressing tasks right now.
1426 A hash table might(?) be more efficient for handling exactly the cases that
1427 are handled now, but for trailing substring matches, I think we want to
1428 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1429 order, nor look only for exact-match or not-match. I don't think a hash
1430 table would be very useful for that, and I don't feel like fleshing out two
1431 completely different implementations. [raeburn:930419.0331EDT] */
1433 struct stringtab_entry
{
1434 /* Hash value for this string. Only useful so long as we aren't doing
1435 substring matches. */
1438 /* Next node to look at, depending on whether the hash value of the string
1439 being searched for is less than or greater than the hash value of the
1440 current node. For now, `equal to' is lumped in with `greater than', for
1441 space efficiency. It's not a common enough case to warrant another field
1442 to be used for all nodes. */
1443 struct stringtab_entry
*less
;
1444 struct stringtab_entry
*greater
;
1446 /* The string itself. */
1449 /* The index allocated for this string. */
1450 bfd_size_type index
;
1452 #ifdef GATHER_STATISTICS
1453 /* How many references have there been to this string? (Not currently used;
1454 could be dumped out for anaylsis, if anyone's interested.) */
1455 unsigned long count
;
1458 /* Next node in linked list, in suggested output order. */
1459 struct stringtab_entry
*next_to_output
;
1462 struct stringtab_data
{
1463 /* Tree of string table entries. */
1464 struct stringtab_entry
*strings
;
1466 /* Fudge factor used to center top node of tree. */
1469 /* Next index value to issue. */
1470 bfd_size_type index
;
1472 /* Index used for empty strings. Cached here because checking for them
1473 is really easy, and we can avoid searching the tree. */
1474 bfd_size_type empty_string_index
;
1476 /* These fields indicate the two ends of a singly-linked list that indicates
1477 the order strings should be written out in. Use this order, and no
1478 seeking will need to be done, so output efficiency should be maximized. */
1479 struct stringtab_entry
**end
;
1480 struct stringtab_entry
*output_order
;
1482 #ifdef GATHER_STATISTICS
1483 /* Number of strings which duplicate strings already in the table. */
1484 unsigned long duplicates
;
1486 /* Number of bytes saved by not having to write all the duplicate strings. */
1487 unsigned long bytes_saved
;
1489 /* Number of zero-length strings. Currently, these all turn into
1490 references to the null byte at the end of the first string. In some
1491 cases (possibly not all? explore this...), it should be possible to
1492 simply write out a zero index value. */
1493 unsigned long empty_strings
;
1495 /* Number of times the hash values matched but the strings were different.
1496 Note that this includes the number of times the other string(s) occurs, so
1497 there may only be two strings hashing to the same value, even if this
1498 number is very large. */
1499 unsigned long bad_hash_matches
;
1501 /* Null strings aren't counted in this one.
1502 This will probably only be nonzero if we've got an input file
1503 which was produced by `ld -r' (i.e., it's already been processed
1504 through this code). Under some operating systems, native tools
1505 may make all empty strings have the same index; but the pointer
1506 check won't catch those, because to get to that stage we'd already
1507 have to compute the checksum, which requires reading the string,
1508 so we short-circuit that case with empty_string_index above. */
1509 unsigned long pointer_matches
;
1511 /* Number of comparisons done. I figure with the algorithms in use below,
1512 the average number of comparisons done (per symbol) should be roughly
1513 log-base-2 of the number of unique strings. */
1514 unsigned long n_compares
;
1518 /* Some utility functions for the string table code. */
1520 /* For speed, only hash on the first this many bytes of strings.
1521 This number was chosen by profiling ld linking itself, with -g. */
1522 #define HASHMAXLEN 25
1524 #define HASH_CHAR(c) (sum ^= sum >> 20, sum ^= sum << 7, sum += (c))
1526 static INLINE
unsigned int
1528 unsigned char *string
;
1529 register unsigned int len
;
1531 register unsigned int sum
= 0;
1533 if (len
> HASHMAXLEN
)
1541 HASH_CHAR (*string
++);
1547 stringtab_init (tab
)
1548 struct stringtab_data
*tab
;
1551 tab
->output_order
= 0;
1552 tab
->end
= &tab
->output_order
;
1554 /* Initial string table length includes size of length field. */
1555 tab
->index
= BYTES_IN_WORD
;
1556 tab
->empty_string_index
= -1;
1557 #ifdef GATHER_STATISTICS
1558 tab
->duplicates
= 0;
1559 tab
->empty_strings
= 0;
1560 tab
->bad_hash_matches
= 0;
1561 tab
->pointer_matches
= 0;
1562 tab
->bytes_saved
= 0;
1563 tab
->n_compares
= 0;
1568 compare (entry
, str
, hash
)
1569 struct stringtab_entry
*entry
;
1573 return hash
- entry
->hash
;
1576 #ifdef GATHER_STATISTICS
1577 /* Don't want to have to link in math library with all bfd applications... */
1578 static INLINE
double
1586 return ((d
> 1.41) ? 0.5 : 0) + n
;
1590 /* Main string table routines. */
1591 /* Returns index in string table. Whether or not this actually adds an
1592 entry into the string table should be irrelevant -- it just has to
1593 return a valid index. */
1594 static bfd_size_type
1595 add_to_stringtab (abfd
, str
, tab
, check
)
1598 struct stringtab_data
*tab
;
1601 struct stringtab_entry
**ep
;
1602 register struct stringtab_entry
*entry
;
1603 unsigned int hashval
, len
;
1607 bfd_size_type index
;
1608 CONST bfd_size_type minus_one
= -1;
1610 #ifdef GATHER_STATISTICS
1611 tab
->empty_strings
++;
1613 index
= tab
->empty_string_index
;
1614 if (index
!= minus_one
)
1617 #ifdef GATHER_STATISTICS
1624 /* Need to find it. */
1625 entry
= tab
->strings
;
1628 index
= entry
->index
+ strlen (entry
->string
);
1629 tab
->empty_string_index
= index
;
1637 /* The hash_zero value is chosen such that the first symbol gets a value of
1638 zero. With a balanced tree, this wouldn't be very useful, but without it,
1639 we might get a more even split at the top level, instead of skewing it
1640 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1641 hashval
= hash (str
, len
) ^ tab
->hash_zero
;
1645 tab
->hash_zero
= hashval
;
1655 #ifdef GATHER_STATISTICS
1658 cmp
= compare (entry
, str
, hashval
);
1659 /* The not-equal cases are more frequent, so check them first. */
1661 ep
= &entry
->greater
;
1666 if (entry
->string
== str
)
1668 #ifdef GATHER_STATISTICS
1669 tab
->pointer_matches
++;
1673 /* Compare the first bytes to save a function call if they
1675 if (entry
->string
[0] == str
[0] && !strcmp (entry
->string
, str
))
1678 #ifdef GATHER_STATISTICS
1680 tab
->bytes_saved
+= len
+ 1;
1683 /* If we're in the linker, and the new string is from a new
1684 input file which might have already had these reductions
1685 run over it, we want to keep the new string pointer. I
1686 don't think we're likely to see any (or nearly as many,
1687 at least) cases where a later string is in the same location
1688 as an earlier one rather than this one. */
1689 entry
->string
= str
;
1690 return entry
->index
;
1692 #ifdef GATHER_STATISTICS
1693 tab
->bad_hash_matches
++;
1695 ep
= &entry
->greater
;
1699 /* If we get here, nothing that's in the table already matched.
1700 EP points to the `next' field at the end of the chain; stick a
1701 new entry on here. */
1703 entry
= (struct stringtab_entry
*)
1704 bfd_alloc_by_size_t (abfd
, sizeof (struct stringtab_entry
));
1706 entry
->less
= entry
->greater
= 0;
1707 entry
->hash
= hashval
;
1708 entry
->index
= tab
->index
;
1709 entry
->string
= str
;
1710 entry
->next_to_output
= 0;
1711 #ifdef GATHER_STATISTICS
1715 assert (*tab
->end
== 0);
1716 *(tab
->end
) = entry
;
1717 tab
->end
= &entry
->next_to_output
;
1718 assert (*tab
->end
== 0);
1721 tab
->index
+= len
+ 1;
1723 tab
->empty_string_index
= entry
->index
;
1727 return entry
->index
;
1731 emit_strtab (abfd
, tab
)
1733 struct stringtab_data
*tab
;
1735 struct stringtab_entry
*entry
;
1736 #ifdef GATHER_STATISTICS
1740 /* Be sure to put string length into correct byte ordering before writing
1742 char buffer
[BYTES_IN_WORD
];
1744 PUT_WORD (abfd
, tab
->index
, (unsigned char *) buffer
);
1745 bfd_write ((PTR
) buffer
, 1, BYTES_IN_WORD
, abfd
);
1747 for (entry
= tab
->output_order
; entry
; entry
= entry
->next_to_output
)
1749 bfd_write ((PTR
) entry
->string
, 1, strlen (entry
->string
) + 1, abfd
);
1750 #ifdef GATHER_STATISTICS
1755 #ifdef GATHER_STATISTICS
1756 /* Short form only, for now.
1757 To do: Specify output file. Conditionalize on environment? Detailed
1758 analysis if desired. */
1760 int n_syms
= bfd_get_symcount (abfd
);
1762 fprintf (stderr
, "String table data for output file:\n");
1763 fprintf (stderr
, " %8d symbols output\n", n_syms
);
1764 fprintf (stderr
, " %8d duplicate strings\n", tab
->duplicates
);
1765 fprintf (stderr
, " %8d empty strings\n", tab
->empty_strings
);
1766 fprintf (stderr
, " %8d unique strings output\n", count
);
1767 fprintf (stderr
, " %8d pointer matches\n", tab
->pointer_matches
);
1768 fprintf (stderr
, " %8d bytes saved\n", tab
->bytes_saved
);
1769 fprintf (stderr
, " %8d bad hash matches\n", tab
->bad_hash_matches
);
1770 fprintf (stderr
, " %8d hash-val comparisons\n", tab
->n_compares
);
1773 double n_compares
= tab
->n_compares
;
1774 double avg_compares
= n_compares
/ n_syms
;
1775 /* The second value here should usually be near one. */
1777 "\t average %f comparisons per symbol (%f * log2 nstrings)\n",
1778 avg_compares
, avg_compares
/ log2 (count
));
1785 generic = bfd_get_outsymbols(abfd);
1786 for (count = 0; count < bfd_get_symcount(abfd); count++)
1788 asymbol *g = *(generic++);
1792 size_t length = strlen(g->name)+1;
1793 bfd_write((PTR)g->name, 1, length, abfd);
1795 g->KEEPIT = (KEEPITTYPE) count;
1800 DEFUN(NAME(aout
,write_syms
),(abfd
),
1803 unsigned int count
;
1804 asymbol
**generic
= bfd_get_outsymbols (abfd
);
1805 struct stringtab_data strtab
;
1807 stringtab_init (&strtab
);
1809 for (count
= 0; count
< bfd_get_symcount (abfd
); count
++)
1811 asymbol
*g
= generic
[count
];
1812 struct external_nlist nsp
;
1815 PUT_WORD (abfd
, add_to_stringtab (abfd
, g
->name
, &strtab
),
1816 (unsigned char *) nsp
.e_strx
);
1818 PUT_WORD (abfd
, 0, (unsigned char *)nsp
.e_strx
);
1820 if (bfd_asymbol_flavour(g
) == abfd
->xvec
->flavour
)
1822 bfd_h_put_16(abfd
, aout_symbol(g
)->desc
, nsp
.e_desc
);
1823 bfd_h_put_8(abfd
, aout_symbol(g
)->other
, nsp
.e_other
);
1824 bfd_h_put_8(abfd
, aout_symbol(g
)->type
, nsp
.e_type
);
1828 bfd_h_put_16(abfd
,0, nsp
.e_desc
);
1829 bfd_h_put_8(abfd
, 0, nsp
.e_other
);
1830 bfd_h_put_8(abfd
, 0, nsp
.e_type
);
1833 translate_to_native_sym_flags (&nsp
, g
, abfd
);
1835 bfd_write((PTR
)&nsp
,1,EXTERNAL_NLIST_SIZE
, abfd
);
1837 /* NB: `KEEPIT' currently overlays `flags', so set this only
1838 here, at the end. */
1842 emit_strtab (abfd
, &strtab
);
1847 DEFUN(NAME(aout
,get_symtab
),(abfd
, location
),
1851 unsigned int counter
= 0;
1852 aout_symbol_type
*symbase
;
1854 if (!NAME(aout
,slurp_symbol_table
)(abfd
)) return 0;
1856 for (symbase
= obj_aout_symbols(abfd
); counter
++ < bfd_get_symcount (abfd
);)
1857 *(location
++) = (asymbol
*)( symbase
++);
1859 return bfd_get_symcount (abfd
);
1863 /* Standard reloc stuff */
1864 /* Output standard relocation information to a file in target byte order. */
1867 DEFUN(NAME(aout
,swap_std_reloc_out
),(abfd
, g
, natptr
),
1870 struct reloc_std_external
*natptr
)
1873 asymbol
*sym
= *(g
->sym_ptr_ptr
);
1875 unsigned int r_length
;
1877 int r_baserel
, r_jmptable
, r_relative
;
1878 unsigned int r_addend
;
1879 asection
*output_section
= sym
->section
->output_section
;
1881 PUT_WORD(abfd
, g
->address
, natptr
->r_address
);
1883 r_length
= g
->howto
->size
; /* Size as a power of two */
1884 r_pcrel
= (int) g
->howto
->pc_relative
; /* Relative to PC? */
1885 /* r_baserel, r_jmptable, r_relative??? FIXME-soon */
1890 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
1892 /* name was clobbered by aout_write_syms to be symbol index */
1894 /* If this relocation is relative to a symbol then set the
1895 r_index to the symbols index, and the r_extern bit.
1897 Absolute symbols can come in in two ways, either as an offset
1898 from the abs section, or as a symbol which has an abs value.
1903 if (bfd_is_com_section (output_section
)
1904 || output_section
== &bfd_abs_section
1905 || output_section
== &bfd_und_section
)
1907 if (bfd_abs_section
.symbol
== sym
)
1909 /* Whoops, looked like an abs symbol, but is really an offset
1910 from the abs section */
1916 /* Fill in symbol */
1918 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
1924 /* Just an ordinary section */
1926 r_index
= output_section
->target_index
;
1929 /* now the fun stuff */
1930 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
1931 natptr
->r_index
[0] = r_index
>> 16;
1932 natptr
->r_index
[1] = r_index
>> 8;
1933 natptr
->r_index
[2] = r_index
;
1935 (r_extern
? RELOC_STD_BITS_EXTERN_BIG
: 0)
1936 | (r_pcrel
? RELOC_STD_BITS_PCREL_BIG
: 0)
1937 | (r_baserel
? RELOC_STD_BITS_BASEREL_BIG
: 0)
1938 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_BIG
: 0)
1939 | (r_relative
? RELOC_STD_BITS_RELATIVE_BIG
: 0)
1940 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_BIG
);
1942 natptr
->r_index
[2] = r_index
>> 16;
1943 natptr
->r_index
[1] = r_index
>> 8;
1944 natptr
->r_index
[0] = r_index
;
1946 (r_extern
? RELOC_STD_BITS_EXTERN_LITTLE
: 0)
1947 | (r_pcrel
? RELOC_STD_BITS_PCREL_LITTLE
: 0)
1948 | (r_baserel
? RELOC_STD_BITS_BASEREL_LITTLE
: 0)
1949 | (r_jmptable
? RELOC_STD_BITS_JMPTABLE_LITTLE
: 0)
1950 | (r_relative
? RELOC_STD_BITS_RELATIVE_LITTLE
: 0)
1951 | (r_length
<< RELOC_STD_BITS_LENGTH_SH_LITTLE
);
1956 /* Extended stuff */
1957 /* Output extended relocation information to a file in target byte order. */
1960 DEFUN(NAME(aout
,swap_ext_reloc_out
),(abfd
, g
, natptr
),
1963 register struct reloc_ext_external
*natptr
)
1967 unsigned int r_type
;
1968 unsigned int r_addend
;
1969 asymbol
*sym
= *(g
->sym_ptr_ptr
);
1970 asection
*output_section
= sym
->section
->output_section
;
1972 PUT_WORD (abfd
, g
->address
, natptr
->r_address
);
1974 r_type
= (unsigned int) g
->howto
->type
;
1976 r_addend
= g
->addend
+ (*(g
->sym_ptr_ptr
))->section
->output_section
->vma
;
1979 /* If this relocation is relative to a symbol then set the
1980 r_index to the symbols index, and the r_extern bit.
1982 Absolute symbols can come in in two ways, either as an offset
1983 from the abs section, or as a symbol which has an abs value.
1987 if (bfd_is_com_section (output_section
)
1988 || output_section
== &bfd_abs_section
1989 || output_section
== &bfd_und_section
)
1991 if (bfd_abs_section
.symbol
== sym
)
1993 /* Whoops, looked like an abs symbol, but is really an offset
1994 from the abs section */
2001 r_index
= stoi((*(g
->sym_ptr_ptr
))->KEEPIT
);
2006 /* Just an ordinary section */
2008 r_index
= output_section
->target_index
;
2012 /* now the fun stuff */
2013 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2014 natptr
->r_index
[0] = r_index
>> 16;
2015 natptr
->r_index
[1] = r_index
>> 8;
2016 natptr
->r_index
[2] = r_index
;
2018 (r_extern
? RELOC_EXT_BITS_EXTERN_BIG
: 0)
2019 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_BIG
);
2021 natptr
->r_index
[2] = r_index
>> 16;
2022 natptr
->r_index
[1] = r_index
>> 8;
2023 natptr
->r_index
[0] = r_index
;
2025 (r_extern
? RELOC_EXT_BITS_EXTERN_LITTLE
: 0)
2026 | (r_type
<< RELOC_EXT_BITS_TYPE_SH_LITTLE
);
2029 PUT_WORD (abfd
, r_addend
, natptr
->r_addend
);
2032 /* BFD deals internally with all things based from the section they're
2033 in. so, something in 10 bytes into a text section with a base of
2034 50 would have a symbol (.text+10) and know .text vma was 50.
2036 Aout keeps all it's symbols based from zero, so the symbol would
2037 contain 60. This macro subs the base of each section from the value
2038 to give the true offset from the section */
2041 #define MOVE_ADDRESS(ad) \
2043 /* undefined symbol */ \
2044 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2045 cache_ptr->addend = ad; \
2047 /* defined, section relative. replace symbol with pointer to \
2048 symbol which points to section */ \
2049 switch (r_index) { \
2051 case N_TEXT | N_EXT: \
2052 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
2053 cache_ptr->addend = ad - su->textsec->vma; \
2056 case N_DATA | N_EXT: \
2057 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
2058 cache_ptr->addend = ad - su->datasec->vma; \
2061 case N_BSS | N_EXT: \
2062 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
2063 cache_ptr->addend = ad - su->bsssec->vma; \
2067 case N_ABS | N_EXT: \
2068 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2069 cache_ptr->addend = ad; \
2075 DEFUN(NAME(aout
,swap_ext_reloc_in
), (abfd
, bytes
, cache_ptr
, symbols
),
2077 struct reloc_ext_external
*bytes AND
2078 arelent
*cache_ptr AND
2083 unsigned int r_type
;
2084 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2086 cache_ptr
->address
= (GET_SWORD (abfd
, bytes
->r_address
));
2088 /* now the fun stuff */
2089 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2090 r_index
= (bytes
->r_index
[0] << 16)
2091 | (bytes
->r_index
[1] << 8)
2092 | bytes
->r_index
[2];
2093 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_BIG
));
2094 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_BIG
)
2095 >> RELOC_EXT_BITS_TYPE_SH_BIG
;
2097 r_index
= (bytes
->r_index
[2] << 16)
2098 | (bytes
->r_index
[1] << 8)
2099 | bytes
->r_index
[0];
2100 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_EXT_BITS_EXTERN_LITTLE
));
2101 r_type
= (bytes
->r_type
[0] & RELOC_EXT_BITS_TYPE_LITTLE
)
2102 >> RELOC_EXT_BITS_TYPE_SH_LITTLE
;
2105 cache_ptr
->howto
= howto_table_ext
+ r_type
;
2106 MOVE_ADDRESS(GET_SWORD(abfd
, bytes
->r_addend
));
2110 DEFUN(NAME(aout
,swap_std_reloc_in
), (abfd
, bytes
, cache_ptr
, symbols
),
2112 struct reloc_std_external
*bytes AND
2113 arelent
*cache_ptr AND
2118 unsigned int r_length
;
2120 int r_baserel
, r_jmptable
, r_relative
;
2121 struct aoutdata
*su
= &(abfd
->tdata
.aout_data
->a
);
2123 cache_ptr
->address
= bfd_h_get_32 (abfd
, bytes
->r_address
);
2125 /* now the fun stuff */
2126 if (abfd
->xvec
->header_byteorder_big_p
!= false) {
2127 r_index
= (bytes
->r_index
[0] << 16)
2128 | (bytes
->r_index
[1] << 8)
2129 | bytes
->r_index
[2];
2130 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_BIG
));
2131 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_BIG
));
2132 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_BIG
));
2133 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_BIG
));
2134 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_BIG
));
2135 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_BIG
)
2136 >> RELOC_STD_BITS_LENGTH_SH_BIG
;
2138 r_index
= (bytes
->r_index
[2] << 16)
2139 | (bytes
->r_index
[1] << 8)
2140 | bytes
->r_index
[0];
2141 r_extern
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_EXTERN_LITTLE
));
2142 r_pcrel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_PCREL_LITTLE
));
2143 r_baserel
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_BASEREL_LITTLE
));
2144 r_jmptable
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_JMPTABLE_LITTLE
));
2145 r_relative
= (0 != (bytes
->r_type
[0] & RELOC_STD_BITS_RELATIVE_LITTLE
));
2146 r_length
= (bytes
->r_type
[0] & RELOC_STD_BITS_LENGTH_LITTLE
)
2147 >> RELOC_STD_BITS_LENGTH_SH_LITTLE
;
2150 cache_ptr
->howto
= howto_table_std
+ r_length
+ 4 * r_pcrel
;
2151 /* FIXME-soon: Roll baserel, jmptable, relative bits into howto setting */
2159 DEFUN(NAME(aout
,slurp_reloc_table
),(abfd
, asect
, symbols
),
2165 bfd_size_type reloc_size
;
2167 arelent
*reloc_cache
;
2170 if (asect
->relocation
) return true;
2172 if (asect
->flags
& SEC_CONSTRUCTOR
) return true;
2174 if (asect
== obj_datasec (abfd
)) {
2175 reloc_size
= exec_hdr(abfd
)->a_drsize
;
2179 if (asect
== obj_textsec (abfd
)) {
2180 reloc_size
= exec_hdr(abfd
)->a_trsize
;
2184 bfd_error
= invalid_operation
;
2188 bfd_seek (abfd
, asect
->rel_filepos
, SEEK_SET
);
2189 each_size
= obj_reloc_entry_size (abfd
);
2191 count
= reloc_size
/ each_size
;
2194 reloc_cache
= (arelent
*) bfd_zalloc (abfd
, (size_t)(count
* sizeof
2198 bfd_error
= no_memory
;
2202 relocs
= (PTR
) bfd_alloc (abfd
, reloc_size
);
2204 bfd_release (abfd
, reloc_cache
);
2208 if (bfd_read (relocs
, 1, reloc_size
, abfd
) != reloc_size
) {
2209 bfd_release (abfd
, relocs
);
2210 bfd_release (abfd
, reloc_cache
);
2211 bfd_error
= system_call_error
;
2215 if (each_size
== RELOC_EXT_SIZE
) {
2216 register struct reloc_ext_external
*rptr
= (struct reloc_ext_external
*) relocs
;
2217 unsigned int counter
= 0;
2218 arelent
*cache_ptr
= reloc_cache
;
2220 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++) {
2221 NAME(aout
,swap_ext_reloc_in
)(abfd
, rptr
, cache_ptr
, symbols
);
2224 register struct reloc_std_external
*rptr
= (struct reloc_std_external
*) relocs
;
2225 unsigned int counter
= 0;
2226 arelent
*cache_ptr
= reloc_cache
;
2228 for (; counter
< count
; counter
++, rptr
++, cache_ptr
++) {
2229 NAME(aout
,swap_std_reloc_in
)(abfd
, rptr
, cache_ptr
, symbols
);
2234 bfd_release (abfd
,relocs
);
2235 asect
->relocation
= reloc_cache
;
2236 asect
->reloc_count
= count
;
2242 /* Write out a relocation section into an object file. */
2245 DEFUN(NAME(aout
,squirt_out_relocs
),(abfd
, section
),
2250 unsigned char *native
, *natptr
;
2253 unsigned int count
= section
->reloc_count
;
2256 if (count
== 0) return true;
2258 each_size
= obj_reloc_entry_size (abfd
);
2259 natsize
= each_size
* count
;
2260 native
= (unsigned char *) bfd_zalloc (abfd
, natsize
);
2262 bfd_error
= no_memory
;
2266 generic
= section
->orelocation
;
2268 if (each_size
== RELOC_EXT_SIZE
)
2270 for (natptr
= native
;
2272 --count
, natptr
+= each_size
, ++generic
)
2273 NAME(aout
,swap_ext_reloc_out
) (abfd
, *generic
, (struct reloc_ext_external
*)natptr
);
2277 for (natptr
= native
;
2279 --count
, natptr
+= each_size
, ++generic
)
2280 NAME(aout
,swap_std_reloc_out
)(abfd
, *generic
, (struct reloc_std_external
*)natptr
);
2283 if ( bfd_write ((PTR
) native
, 1, natsize
, abfd
) != natsize
) {
2284 bfd_release(abfd
, native
);
2287 bfd_release (abfd
, native
);
2292 /* This is stupid. This function should be a boolean predicate */
2294 DEFUN(NAME(aout
,canonicalize_reloc
),(abfd
, section
, relptr
, symbols
),
2297 arelent
**relptr AND
2300 arelent
*tblptr
= section
->relocation
;
2303 if (!(tblptr
|| NAME(aout
,slurp_reloc_table
)(abfd
, section
, symbols
)))
2306 if (section
->flags
& SEC_CONSTRUCTOR
) {
2307 arelent_chain
*chain
= section
->constructor_chain
;
2308 for (count
= 0; count
< section
->reloc_count
; count
++) {
2309 *relptr
++ = &chain
->relent
;
2310 chain
= chain
->next
;
2314 tblptr
= section
->relocation
;
2315 if (!tblptr
) return 0;
2317 for (count
= 0; count
++ < section
->reloc_count
;)
2319 *relptr
++ = tblptr
++;
2324 return section
->reloc_count
;
2328 DEFUN(NAME(aout
,get_reloc_upper_bound
),(abfd
, asect
),
2332 if (bfd_get_format (abfd
) != bfd_object
) {
2333 bfd_error
= invalid_operation
;
2336 if (asect
->flags
& SEC_CONSTRUCTOR
) {
2337 return (sizeof (arelent
*) * (asect
->reloc_count
+1));
2341 if (asect
== obj_datasec (abfd
))
2342 return (sizeof (arelent
*) *
2343 ((exec_hdr(abfd
)->a_drsize
/ obj_reloc_entry_size (abfd
))
2346 if (asect
== obj_textsec (abfd
))
2347 return (sizeof (arelent
*) *
2348 ((exec_hdr(abfd
)->a_trsize
/ obj_reloc_entry_size (abfd
))
2351 bfd_error
= invalid_operation
;
2357 DEFUN(NAME(aout
,get_symtab_upper_bound
),(abfd
),
2360 if (!NAME(aout
,slurp_symbol_table
)(abfd
)) return 0;
2362 return (bfd_get_symcount (abfd
)+1) * (sizeof (aout_symbol_type
*));
2365 DEFUN(NAME(aout
,get_lineno
),(ignore_abfd
, ignore_symbol
),
2366 bfd
*ignore_abfd AND
2367 asymbol
*ignore_symbol
)
2369 return (alent
*)NULL
;
2373 DEFUN(NAME(aout
,get_symbol_info
),(ignore_abfd
, symbol
, ret
),
2374 bfd
*ignore_abfd AND
2378 bfd_symbol_info (symbol
, ret
);
2380 if (ret
->type
== '?')
2382 int type_code
= aout_symbol(symbol
)->type
& 0xff;
2383 CONST
char *stab_name
= aout_stab_name(type_code
);
2384 static char buf
[10];
2386 if (stab_name
== NULL
)
2388 sprintf(buf
, "(%d)", type_code
);
2392 ret
->stab_other
= (unsigned)(aout_symbol(symbol
)->other
& 0xff);
2393 ret
->stab_desc
= (unsigned)(aout_symbol(symbol
)->desc
& 0xffff);
2394 ret
->stab_name
= stab_name
;
2399 DEFUN(NAME(aout
,print_symbol
),(ignore_abfd
, afile
, symbol
, how
),
2400 bfd
*ignore_abfd AND
2403 bfd_print_symbol_type how
)
2405 FILE *file
= (FILE *)afile
;
2408 case bfd_print_symbol_name
:
2410 fprintf(file
,"%s", symbol
->name
);
2412 case bfd_print_symbol_more
:
2413 fprintf(file
,"%4x %2x %2x",(unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2414 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2415 (unsigned)(aout_symbol(symbol
)->type
));
2417 case bfd_print_symbol_all
:
2419 CONST
char *section_name
= symbol
->section
->name
;
2422 bfd_print_symbol_vandf((PTR
)file
,symbol
);
2424 fprintf(file
," %-5s %04x %02x %02x",
2426 (unsigned)(aout_symbol(symbol
)->desc
& 0xffff),
2427 (unsigned)(aout_symbol(symbol
)->other
& 0xff),
2428 (unsigned)(aout_symbol(symbol
)->type
& 0xff));
2430 fprintf(file
," %s", symbol
->name
);
2437 provided a BFD, a section and an offset into the section, calculate
2438 and return the name of the source file and the line nearest to the
2443 DEFUN(NAME(aout
,find_nearest_line
),(abfd
,
2451 asection
*section AND
2452 asymbol
**symbols AND
2454 CONST
char **filename_ptr AND
2455 CONST
char **functionname_ptr AND
2456 unsigned int *line_ptr
)
2458 /* Run down the file looking for the filename, function and linenumber */
2460 static char buffer
[100];
2461 static char filename_buffer
[200];
2462 CONST
char *directory_name
= NULL
;
2463 CONST
char *main_file_name
= NULL
;
2464 CONST
char *current_file_name
= NULL
;
2465 CONST
char *line_file_name
= NULL
; /* Value of current_file_name at line number. */
2466 bfd_vma high_line_vma
= ~0;
2467 bfd_vma low_func_vma
= 0;
2469 *filename_ptr
= abfd
->filename
;
2470 *functionname_ptr
= 0;
2472 if (symbols
!= (asymbol
**)NULL
) {
2473 for (p
= symbols
; *p
; p
++) {
2474 aout_symbol_type
*q
= (aout_symbol_type
*)(*p
);
2478 main_file_name
= current_file_name
= q
->symbol
.name
;
2479 /* Look ahead to next symbol to check if that too is an N_SO. */
2483 q
= (aout_symbol_type
*)(*p
);
2484 if (q
->type
!= (int)N_SO
)
2487 /* Found a second N_SO First is directory; second is filename. */
2488 directory_name
= current_file_name
;
2489 main_file_name
= current_file_name
= q
->symbol
.name
;
2490 if (obj_textsec(abfd
) != section
)
2494 current_file_name
= q
->symbol
.name
;
2501 /* We'll keep this if it resolves nearer than the one we have already */
2502 if (q
->symbol
.value
>= offset
&&
2503 q
->symbol
.value
< high_line_vma
) {
2504 *line_ptr
= q
->desc
;
2505 high_line_vma
= q
->symbol
.value
;
2506 line_file_name
= current_file_name
;
2511 /* We'll keep this if it is nearer than the one we have already */
2512 if (q
->symbol
.value
>= low_func_vma
&&
2513 q
->symbol
.value
<= offset
) {
2514 low_func_vma
= q
->symbol
.value
;
2515 func
= (asymbol
*)q
;
2517 if (*line_ptr
&& func
) {
2518 CONST
char *function
= func
->name
;
2520 strncpy(buffer
, function
, sizeof(buffer
)-1);
2521 buffer
[sizeof(buffer
)-1] = 0;
2522 /* Have to remove : stuff */
2523 p
= strchr(buffer
,':');
2524 if (p
!= NULL
) { *p
= '\0'; }
2525 *functionname_ptr
= buffer
;
2537 main_file_name
= line_file_name
;
2538 if (main_file_name
) {
2539 if (main_file_name
[0] == '/' || directory_name
== NULL
)
2540 *filename_ptr
= main_file_name
;
2542 sprintf(filename_buffer
, "%.140s%.50s",
2543 directory_name
, main_file_name
);
2544 *filename_ptr
= filename_buffer
;
2552 DEFUN(NAME(aout
,sizeof_headers
),(abfd
, execable
),
2556 return adata(abfd
).exec_bytes_size
;