1 /* BFD back-end data structures for a.out (and similar) files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 /* We try to encapsulate the differences in the various a.out file
27 variants in a few routines, and otherwise share large masses of code.
28 This means we only have to fix bugs in one place, most of the time. */
32 /* Parameterize the a.out code based on whether it is being built
33 for a 32-bit architecture or a 64-bit architecture. */
35 #define GET_WORD bfd_h_get_64
36 #define GET_SWORD bfd_h_get_signed_64
37 #define GET_MAGIC bfd_h_get_32
38 #define PUT_WORD bfd_h_put_64
39 #define PUT_MAGIC bfd_h_put_32
41 #define NAME(x,y) CAT3(x,_64_,y)
43 #define JNAME(x) CAT(x,_64)
44 #define BYTES_IN_WORD 8
47 #define GET_WORD bfd_h_get_16
48 #define GET_SWORD bfd_h_get_signed_16
49 #define GET_MAGIC bfd_h_get_16
50 #define PUT_WORD bfd_h_put_16
51 #define PUT_MAGIC bfd_h_put_16
53 #define NAME(x,y) CAT3(x,_16_,y)
55 #define JNAME(x) CAT(x,_16)
56 #define BYTES_IN_WORD 2
57 #else /* ARCH_SIZE == 32 */
58 #define GET_WORD bfd_h_get_32
59 #define GET_SWORD bfd_h_get_signed_32
60 #define GET_MAGIC bfd_h_get_32
61 #define PUT_WORD bfd_h_put_32
62 #define PUT_MAGIC bfd_h_put_32
64 #define NAME(x,y) CAT3(x,_32_,y)
66 #define JNAME(x) CAT(x,_32)
67 #define BYTES_IN_WORD 4
68 #endif /* ARCH_SIZE==32 */
69 #endif /* ARCH_SIZE==64 */
71 /* Declare at file level, since used in parameter lists, which have
74 struct external_nlist
;
75 struct reloc_ext_external
;
76 struct reloc_std_external
;
78 /* a.out backend linker hash table entries. */
80 struct aout_link_hash_entry
82 struct bfd_link_hash_entry root
;
83 /* Whether this symbol has been written out. */
85 /* Symbol index in output file. */
89 /* a.out backend linker hash table. */
91 struct aout_link_hash_table
93 struct bfd_link_hash_table root
;
96 /* Look up an entry in an a.out link hash table. */
98 #define aout_link_hash_lookup(table, string, create, copy, follow) \
99 ((struct aout_link_hash_entry *) \
100 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
102 /* Traverse an a.out link hash table. */
104 #define aout_link_hash_traverse(table, func, info) \
105 (bfd_link_hash_traverse \
107 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
110 /* Get the a.out link hash table from the info structure. This is
113 #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash))
115 /* Back-end information for various a.out targets. */
116 struct aout_backend_data
118 /* Are ZMAGIC files mapped contiguously? If so, the text section may
119 need more padding, if the segment size (granularity for memory access
120 control) is larger than the page size. */
121 unsigned char zmagic_mapped_contiguous
;
122 /* If this flag is set, ZMAGIC/NMAGIC file headers get mapped in with the
123 text section, which starts immediately after the file header.
124 If not, the text section starts on the next page. */
125 unsigned char text_includes_header
;
127 /* If this flag is set, then if the entry address is not in the
128 first SEGMENT_SIZE bytes of the text section, it is taken to be
129 the address of the start of the text section. This can be useful
131 unsigned char entry_is_text_address
;
133 /* The value to pass to N_SET_FLAGS. */
134 unsigned char exec_hdr_flags
;
136 /* If the text section VMA isn't specified, and we need an absolute
137 address, use this as the default. If we're producing a relocatable
138 file, zero is always used. */
139 /* ?? Perhaps a callback would be a better choice? Will this do anything
140 reasonable for a format that handles multiple CPUs with different
141 load addresses for each? */
142 bfd_vma default_text_vma
;
144 /* Callback for setting the page and segment sizes, if they can't be
145 trivially determined from the architecture. */
146 boolean (*set_sizes
) PARAMS ((bfd
*));
148 /* zmagic files only. For go32, the length of the exec header contributes
149 to the size of the text section in the file for alignment purposes but
150 does *not* get counted in the length of the text section. */
151 unsigned char exec_header_not_counted
;
153 /* Callback from the add symbols phase of the linker code to handle
155 boolean (*add_dynamic_symbols
) PARAMS ((bfd
*, struct bfd_link_info
*,
156 struct external_nlist
**,
157 bfd_size_type
*, char **));
159 /* Callback from the add symbols phase of the linker code to handle
160 adding a single symbol to the global linker hash table. */
161 boolean (*add_one_symbol
) PARAMS ((struct bfd_link_info
*, bfd
*,
162 const char *, flagword
, asection
*,
163 bfd_vma
, const char *, boolean
,
165 struct bfd_link_hash_entry
**));
167 /* Called to handle linking a dynamic object. */
168 boolean (*link_dynamic_object
) PARAMS ((struct bfd_link_info
*, bfd
*));
170 /* Called for each global symbol being written out by the linker.
171 This should write out the dynamic symbol information. */
172 boolean (*write_dynamic_symbol
) PARAMS ((bfd
*, struct bfd_link_info
*,
173 struct aout_link_hash_entry
*));
175 /* If this callback is not NULL, the linker calls it for each reloc.
176 RELOC is a pointer to the unswapped reloc. If *SKIP is set to
177 true, the reloc will be skipped. *RELOCATION may be changed to
178 change the effects of the relocation. */
179 boolean (*check_dynamic_reloc
) PARAMS ((struct bfd_link_info
*info
,
181 asection
*input_section
,
182 struct aout_link_hash_entry
*h
,
183 PTR reloc
, bfd_byte
*contents
,
185 bfd_vma
*relocation
));
187 /* Called at the end of a link to finish up any dynamic linking
189 boolean (*finish_dynamic_link
) PARAMS ((bfd
*, struct bfd_link_info
*));
191 #define aout_backend_info(abfd) \
192 ((CONST struct aout_backend_data *)((abfd)->xvec->backend_data))
194 /* This is the layout in memory of a "struct exec" while we process it.
195 All 'lengths' are given as a number of bytes.
196 All 'alignments' are for relinkable files only; an alignment of
197 'n' indicates the corresponding segment must begin at an
198 address that is a multiple of (2**n). */
202 long a_info
; /* Magic number and flags, packed */
203 bfd_vma a_text
; /* length of text, in bytes */
204 bfd_vma a_data
; /* length of data, in bytes */
205 bfd_vma a_bss
; /* length of uninitialized data area in mem */
206 bfd_vma a_syms
; /* length of symbol table data in file */
207 bfd_vma a_entry
; /* start address */
208 bfd_vma a_trsize
; /* length of text's relocation info, in bytes */
209 bfd_vma a_drsize
; /* length of data's relocation info, in bytes */
211 bfd_vma a_tload
; /* Text runtime load address */
212 bfd_vma a_dload
; /* Data runtime load address */
213 unsigned char a_talign
; /* Alignment of text segment */
214 unsigned char a_dalign
; /* Alignment of data segment */
215 unsigned char a_balign
; /* Alignment of bss segment */
216 char a_relaxable
; /* Enough info for linker relax */
219 /* Magic number is written
221 3130292827262524232221201918171615141312111009080706050403020100
222 < FLAGS >< MACHINE TYPE >< MAGIC NUMBER >
224 /* Magic number for NetBSD is
226 3130292827262524232221201918171615141312111009080706050403020100
227 < FLAGS >< MACHINE TYPE >< MAGIC NUMBER >
235 /* skip a bunch so we don't run into any of suns numbers */
236 /* make these up for the ns32k*/
237 M_NS32032
= (64), /* ns32032 running ? */
238 M_NS32532
= (64 + 5), /* ns32532 running mach */
241 M_29K
= 101, /* AMD 29000 */
242 M_386_DYNIX
= 102, /* Sequent running dynix */
243 M_ARM
= 103, /* Advanced Risc Machines ARM */
244 M_SPARCLET
= 131, /* SPARClet = M_SPARC + 128 */
245 M_386_NETBSD
= 134, /* NetBSD/i386 binary */
246 M_68K_NETBSD
= 135, /* NetBSD/m68k binary */
247 M_68K4K_NETBSD
= 136, /* NetBSD/m68k4k binary */
248 M_532_NETBSD
= 137, /* NetBSD/ns32k binary */
249 M_SPARC_NETBSD
= 138, /* NetBSD/sparc binary */
250 M_PMAX_NETBSD
= 139, /* NetBSD/pmax (MIPS little-endian) binary */
251 M_VAX_NETBSD
= 140, /* NetBSD/vax binary */
252 M_ALPHA_NETBSD
= 141, /* NetBSD/alpha binary */
253 M_ARM6_NETBSD
= 143, /* NetBSD/arm32 binary */
254 M_SPARCLET_1
= 147, /* 0x93, reserved */
255 M_MIPS1
= 151, /* MIPS R2000/R3000 binary */
256 M_MIPS2
= 152, /* MIPS R4000/R6000 binary */
257 M_SPARCLET_2
= 163, /* 0xa3, reserved */
258 M_SPARCLET_3
= 179, /* 0xb3, reserved */
259 M_SPARCLET_4
= 195, /* 0xc3, reserved */
260 M_HP200
= 200, /* HP 200 (68010) BSD binary */
261 M_HP300
= (300 % 256), /* HP 300 (68020+68881) BSD binary */
262 M_HPUX
= (0x20c % 256), /* HP 200/300 HPUX binary */
263 M_SPARCLET_5
= 211, /* 0xd3, reserved */
264 M_SPARCLET_6
= 227, /* 0xe3, reserved */
265 /* M_SPARCLET_7 = 243 / * 0xf3, reserved */
266 M_SPARCLITE_LE
= 243,
267 M_CRIS
= 255 /* Axis CRIS binary. */
270 #define N_DYNAMIC(exec) ((exec).a_info & 0x80000000)
273 # define N_MAGIC(exec) ((exec).a_info & 0xffff)
277 # define N_MACHTYPE(exec) ((enum machine_type)(((exec).a_info >> 16) & 0xff))
281 # define N_FLAGS(exec) (((exec).a_info >> 24) & 0xff)
285 # define N_SET_INFO(exec, magic, type, flags) \
286 ((exec).a_info = ((magic) & 0xffff) \
287 | (((int)(type) & 0xff) << 16) \
288 | (((flags) & 0xff) << 24))
291 #ifndef N_SET_DYNAMIC
292 # define N_SET_DYNAMIC(exec, dynamic) \
293 ((exec).a_info = (dynamic) ? ((exec).a_info | 0x80000000) : \
294 ((exec).a_info & 0x7fffffff))
298 # define N_SET_MAGIC(exec, magic) \
299 ((exec).a_info = (((exec).a_info & 0xffff0000) | ((magic) & 0xffff)))
302 #ifndef N_SET_MACHTYPE
303 # define N_SET_MACHTYPE(exec, machtype) \
305 ((exec).a_info&0xff00ffff) | ((((int)(machtype))&0xff) << 16))
309 # define N_SET_FLAGS(exec, flags) \
311 ((exec).a_info&0x00ffffff) | (((flags) & 0xff) << 24))
314 typedef struct aout_symbol
{
321 /* The `tdata' struct for all a.out-like object file formats.
322 Various things depend on this struct being around any time an a.out
323 file is being handled. An example is dbxread.c in GDB. */
326 struct internal_exec
*hdr
; /* exec file header */
327 aout_symbol_type
*symbols
; /* symtab for input bfd */
329 /* For ease, we do this */
334 /* We remember these offsets so that after check_file_format, we have
335 no dependencies on the particular format of the exec_hdr. */
336 file_ptr sym_filepos
;
337 file_ptr str_filepos
;
339 /* Size of a relocation entry in external form */
340 unsigned reloc_entry_size
;
342 /* Size of a symbol table entry in external form */
343 unsigned symbol_entry_size
;
345 /* Page size - needed for alignment of demand paged files. */
346 unsigned long page_size
;
348 /* Segment size - needed for alignment of demand paged files. */
349 unsigned long segment_size
;
351 /* Zmagic disk block size - need to align the start of the text
352 section in ZMAGIC binaries. Normally the same as page_size. */
353 unsigned long zmagic_disk_block_size
;
355 unsigned exec_bytes_size
;
356 unsigned vma_adjusted
: 1;
358 /* used when a bfd supports several highly similar formats */
362 /* Used on HP 9000/300 running HP/UX. See hp300hpux.c. */
364 /* Used on Linux, 386BSD, etc. See include/aout/aout64.h. */
376 /* A buffer for find_nearest_line. */
379 /* The external symbol information. */
380 struct external_nlist
*external_syms
;
381 bfd_size_type external_sym_count
;
382 bfd_window sym_window
;
383 char *external_strings
;
384 bfd_size_type external_string_size
;
385 bfd_window string_window
;
386 struct aout_link_hash_entry
**sym_hashes
;
388 /* A pointer for shared library information. */
391 /* A mapping from local symbols to offsets into the global offset
392 table, used when linking on SunOS. This is indexed by the symbol
394 bfd_vma
*local_got_offsets
;
397 struct aout_data_struct
{
399 struct internal_exec e
;
402 #define adata(bfd) ((bfd)->tdata.aout_data->a)
403 #define exec_hdr(bfd) (adata(bfd).hdr)
404 #define obj_aout_symbols(bfd) (adata(bfd).symbols)
405 #define obj_textsec(bfd) (adata(bfd).textsec)
406 #define obj_datasec(bfd) (adata(bfd).datasec)
407 #define obj_bsssec(bfd) (adata(bfd).bsssec)
408 #define obj_sym_filepos(bfd) (adata(bfd).sym_filepos)
409 #define obj_str_filepos(bfd) (adata(bfd).str_filepos)
410 #define obj_reloc_entry_size(bfd) (adata(bfd).reloc_entry_size)
411 #define obj_symbol_entry_size(bfd) (adata(bfd).symbol_entry_size)
412 #define obj_aout_subformat(bfd) (adata(bfd).subformat)
413 #define obj_aout_external_syms(bfd) (adata(bfd).external_syms)
414 #define obj_aout_external_sym_count(bfd) (adata(bfd).external_sym_count)
415 #define obj_aout_sym_window(bfd) (adata(bfd).sym_window)
416 #define obj_aout_external_strings(bfd) (adata(bfd).external_strings)
417 #define obj_aout_external_string_size(bfd) (adata(bfd).external_string_size)
418 #define obj_aout_string_window(bfd) (adata(bfd).string_window)
419 #define obj_aout_sym_hashes(bfd) (adata(bfd).sym_hashes)
420 #define obj_aout_dynamic_info(bfd) (adata(bfd).dynamic_info)
422 /* We take the address of the first element of an asymbol to ensure that the
423 macro is only ever applied to an asymbol */
424 #define aout_symbol(asymbol) ((aout_symbol_type *)(&(asymbol)->the_bfd))
426 /* Information we keep for each a.out section. This is currently only
427 used by the a.out backend linker. */
429 struct aout_section_data_struct
431 /* The unswapped relocation entries for this section. */
435 #define aout_section_data(s) \
436 ((struct aout_section_data_struct *) (s)->used_by_bfd)
438 #define set_aout_section_data(s,v) \
439 ((s)->used_by_bfd = (PTR)&(v)->relocs)
441 /* Prototype declarations for functions defined in aoutx.h */
444 NAME(aout
,squirt_out_relocs
) PARAMS ((bfd
*abfd
, asection
*section
));
447 NAME(aout
,make_sections
) PARAMS ((bfd
*));
450 NAME(aout
,some_aout_object_p
) PARAMS ((bfd
*abfd
,
451 struct internal_exec
*execp
,
452 const bfd_target
*(*callback
)(bfd
*)));
455 NAME(aout
,mkobject
) PARAMS ((bfd
*abfd
));
458 NAME(aout
,machine_type
) PARAMS ((enum bfd_architecture arch
,
459 unsigned long machine
,
463 NAME(aout
,set_arch_mach
) PARAMS ((bfd
*abfd
, enum bfd_architecture arch
,
464 unsigned long machine
));
467 NAME(aout
,new_section_hook
) PARAMS ((bfd
*abfd
, asection
*newsect
));
470 NAME(aout
,set_section_contents
) PARAMS ((bfd
*abfd
, sec_ptr section
,
471 PTR location
, file_ptr offset
, bfd_size_type count
));
474 NAME(aout
,make_empty_symbol
) PARAMS ((bfd
*abfd
));
477 NAME(aout
,translate_symbol_table
) PARAMS ((bfd
*, aout_symbol_type
*,
478 struct external_nlist
*,
479 bfd_size_type
, char *,
484 NAME(aout
,slurp_symbol_table
) PARAMS ((bfd
*abfd
));
487 NAME(aout
,write_syms
) PARAMS ((bfd
*abfd
));
490 NAME(aout
,reclaim_symbol_table
) PARAMS ((bfd
*abfd
));
493 NAME(aout
,get_symtab_upper_bound
) PARAMS ((bfd
*abfd
));
496 NAME(aout
,get_symtab
) PARAMS ((bfd
*abfd
, asymbol
**location
));
499 NAME(aout
,swap_ext_reloc_in
) PARAMS ((bfd
*, struct reloc_ext_external
*,
500 arelent
*, asymbol
**, bfd_size_type
));
502 NAME(aout
,swap_std_reloc_in
) PARAMS ((bfd
*, struct reloc_std_external
*,
503 arelent
*, asymbol
**, bfd_size_type
));
506 NAME(aout
,reloc_type_lookup
) PARAMS ((bfd
*abfd
,
507 bfd_reloc_code_real_type code
));
510 NAME(aout
,slurp_reloc_table
) PARAMS ((bfd
*abfd
, sec_ptr asect
,
514 NAME(aout
,canonicalize_reloc
) PARAMS ((bfd
*abfd
, sec_ptr section
,
515 arelent
**relptr
, asymbol
**symbols
));
518 NAME(aout
,get_reloc_upper_bound
) PARAMS ((bfd
*abfd
, sec_ptr asect
));
521 NAME(aout
,reclaim_reloc
) PARAMS ((bfd
*ignore_abfd
, sec_ptr ignore
));
524 NAME(aout
,get_lineno
) PARAMS ((bfd
*ignore_abfd
, asymbol
*ignore_symbol
));
527 NAME(aout
,print_symbol
) PARAMS ((bfd
*ignore_abfd
, PTR file
,
528 asymbol
*symbol
, bfd_print_symbol_type how
));
531 NAME(aout
,get_symbol_info
) PARAMS ((bfd
*ignore_abfd
,
532 asymbol
*symbol
, symbol_info
*ret
));
535 NAME(aout
,find_nearest_line
) PARAMS ((bfd
*abfd
, asection
*section
,
536 asymbol
**symbols
, bfd_vma offset
, CONST
char **filename_ptr
,
537 CONST
char **functionname_ptr
, unsigned int *line_ptr
));
540 NAME(aout
,read_minisymbols
) PARAMS ((bfd
*, boolean
, PTR
*, unsigned int *));
543 NAME(aout
,minisymbol_to_symbol
) PARAMS ((bfd
*, boolean
, const PTR
,
547 NAME(aout
,sizeof_headers
) PARAMS ((bfd
*abfd
, boolean exec
));
550 NAME(aout
,adjust_sizes_and_vmas
) PARAMS ((bfd
*abfd
,
551 bfd_size_type
*text_size
, file_ptr
*text_end
));
554 NAME(aout
,swap_exec_header_in
) PARAMS ((bfd
*abfd
,
555 struct external_exec
*raw_bytes
, struct internal_exec
*execp
));
558 NAME(aout
,swap_exec_header_out
) PARAMS ((bfd
*abfd
,
559 struct internal_exec
*execp
, struct external_exec
*raw_bytes
));
561 struct bfd_hash_entry
*
562 NAME(aout
,link_hash_newfunc
)
563 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
566 NAME(aout
,link_hash_table_init
)
567 PARAMS ((struct aout_link_hash_table
*, bfd
*,
568 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
569 struct bfd_hash_table
*,
572 struct bfd_link_hash_table
*
573 NAME(aout
,link_hash_table_create
) PARAMS ((bfd
*));
576 NAME(aout
,link_add_symbols
) PARAMS ((bfd
*, struct bfd_link_info
*));
579 NAME(aout
,final_link
) PARAMS ((bfd
*, struct bfd_link_info
*,
580 void (*) (bfd
*, file_ptr
*, file_ptr
*,
584 NAME(aout
,bfd_free_cached_info
) PARAMS ((bfd
*));
586 /* A.out uses the generic versions of these routines... */
588 #define aout_16_get_section_contents _bfd_generic_get_section_contents
590 #define aout_32_get_section_contents _bfd_generic_get_section_contents
592 #define aout_64_get_section_contents _bfd_generic_get_section_contents
593 #ifndef NO_WRITE_HEADER_KLUDGE
594 #define NO_WRITE_HEADER_KLUDGE 0
597 #ifndef aout_32_bfd_is_local_label_name
598 #define aout_32_bfd_is_local_label_name bfd_generic_is_local_label_name
601 #ifndef WRITE_HEADERS
602 #define WRITE_HEADERS(abfd, execp) \
604 bfd_size_type text_size; /* dummy vars */ \
606 if (adata(abfd).magic == undecided_magic) \
607 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end); \
609 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE; \
610 execp->a_entry = bfd_get_start_address (abfd); \
612 execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * \
613 obj_reloc_entry_size (abfd)); \
614 execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * \
615 obj_reloc_entry_size (abfd)); \
616 NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes); \
618 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) return false; \
619 if (bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd) \
620 != EXEC_BYTES_SIZE) \
622 /* Now write out reloc info, followed by syms and strings */ \
624 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL \
625 && bfd_get_symcount (abfd) != 0) \
627 if (bfd_seek (abfd, (file_ptr)(N_SYMOFF(*execp)), SEEK_SET) != 0) \
630 if (! NAME(aout,write_syms)(abfd)) return false; \
633 if (bfd_seek (abfd, (file_ptr)(N_TRELOFF(*execp)), SEEK_SET) != 0) \
635 if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd))) \
638 if (bfd_seek (abfd, (file_ptr)(N_DRELOFF(*execp)), SEEK_SET) != 0) \
640 if (!NAME(aout,squirt_out_relocs)(abfd, obj_datasec (abfd))) \
645 #endif /* ! defined (LIBAOUT_H) */