1 /* BFD back-end data structures for a.out (and similar) files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 /* We try to encapsulate the differences in the various a.out file
26 variants in a few routines, and otherwise share large masses of code.
27 This means we only have to fix bugs in one place, most of the time. */
31 /* Parameterize the a.out code based on whether it is being built
32 for a 32-bit architecture or a 64-bit architecture. */
34 #define GET_WORD bfd_h_get_64
35 #define GET_SWORD bfd_h_get_signed_64
36 #define PUT_WORD bfd_h_put_64
38 #define NAME(x,y) CAT3(x,_64_,y)
40 #define JNAME(x) CAT(x,_64)
41 #define BYTES_IN_WORD 8
42 #else /* ARCH_SIZE == 32 */
43 #define GET_WORD bfd_h_get_32
44 #define GET_SWORD bfd_h_get_signed_32
45 #define PUT_WORD bfd_h_put_32
47 #define NAME(x,y) CAT3(x,_32_,y)
49 #define JNAME(x) CAT(x,_32)
50 #define BYTES_IN_WORD 4
51 #endif /* ARCH_SIZE==32 */
53 /* Declare at file level, since used in parameter lists, which have
56 struct external_nlist
;
57 struct reloc_ext_external
;
58 struct reloc_std_external
;
60 /* a.out backend linker hash table entries. */
62 struct aout_link_hash_entry
64 struct bfd_link_hash_entry root
;
65 /* Whether this symbol has been written out. */
67 /* Symbol index in output file. */
71 /* a.out backend linker hash table. */
73 struct aout_link_hash_table
75 struct bfd_link_hash_table root
;
78 /* Look up an entry in an a.out link hash table. */
80 #define aout_link_hash_lookup(table, string, create, copy, follow) \
81 ((struct aout_link_hash_entry *) \
82 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
84 /* Traverse an a.out link hash table. */
86 #define aout_link_hash_traverse(table, func, info) \
87 (bfd_link_hash_traverse \
89 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
92 /* Get the a.out link hash table from the info structure. This is
95 #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash))
97 /* Back-end information for various a.out targets. */
98 struct aout_backend_data
100 /* Are ZMAGIC files mapped contiguously? If so, the text section may
101 need more padding, if the segment size (granularity for memory access
102 control) is larger than the page size. */
103 unsigned char zmagic_mapped_contiguous
;
104 /* If this flag is set, ZMAGIC/NMAGIC file headers get mapped in with the
105 text section, which starts immediately after the file header.
106 If not, the text section starts on the next page. */
107 unsigned char text_includes_header
;
109 /* If this flag is set, then if the entry address is not in the
110 first SEGMENT_SIZE bytes of the text section, it is taken to be
111 the address of the start of the text section. This can be useful
113 unsigned char entry_is_text_address
;
115 /* The value to pass to N_SET_FLAGS. */
116 unsigned char exec_hdr_flags
;
118 /* If the text section VMA isn't specified, and we need an absolute
119 address, use this as the default. If we're producing a relocatable
120 file, zero is always used. */
121 /* ?? Perhaps a callback would be a better choice? Will this do anything
122 reasonable for a format that handles multiple CPUs with different
123 load addresses for each? */
124 bfd_vma default_text_vma
;
126 /* Callback for setting the page and segment sizes, if they can't be
127 trivially determined from the architecture. */
128 boolean (*set_sizes
) PARAMS ((bfd
*));
130 /* zmagic files only. For go32, the length of the exec header contributes
131 to the size of the text section in the file for alignment purposes but
132 does *not* get counted in the length of the text section. */
133 unsigned char exec_header_not_counted
;
135 /* Callback from the add symbols phase of the linker code to handle
137 boolean (*add_dynamic_symbols
) PARAMS ((bfd
*, struct bfd_link_info
*,
138 struct external_nlist
**,
139 bfd_size_type
*, char **));
141 /* Callback from the add symbols phase of the linker code to handle
142 adding a single symbol to the global linker hash table. */
143 boolean (*add_one_symbol
) PARAMS ((struct bfd_link_info
*, bfd
*,
144 const char *, flagword
, asection
*,
145 bfd_vma
, const char *, boolean
,
147 struct bfd_link_hash_entry
**));
149 /* Called to handle linking a dynamic object. */
150 boolean (*link_dynamic_object
) PARAMS ((struct bfd_link_info
*, bfd
*));
152 /* Called for each global symbol being written out by the linker.
153 This should write out the dynamic symbol information. */
154 boolean (*write_dynamic_symbol
) PARAMS ((bfd
*, struct bfd_link_info
*,
155 struct aout_link_hash_entry
*));
157 /* If this callback is not NULL, the linker calls it for each reloc.
158 RELOC is a pointer to the unswapped reloc. If *SKIP is set to
159 true, the reloc will be skipped. *RELOCATION may be changed to
160 change the effects of the relocation. */
161 boolean (*check_dynamic_reloc
) PARAMS ((struct bfd_link_info
*info
,
163 asection
*input_section
,
164 struct aout_link_hash_entry
*h
,
165 PTR reloc
, bfd_byte
*contents
,
167 bfd_vma
*relocation
));
169 /* Called at the end of a link to finish up any dynamic linking
171 boolean (*finish_dynamic_link
) PARAMS ((bfd
*, struct bfd_link_info
*));
173 #define aout_backend_info(abfd) \
174 ((CONST struct aout_backend_data *)((abfd)->xvec->backend_data))
176 /* This is the layout in memory of a "struct exec" while we process it.
177 All 'lengths' are given as a number of bytes.
178 All 'alignments' are for relinkable files only; an alignment of
179 'n' indicates the corresponding segment must begin at an
180 address that is a multiple of (2**n). */
184 long a_info
; /* Magic number and flags, packed */
185 bfd_vma a_text
; /* length of text, in bytes */
186 bfd_vma a_data
; /* length of data, in bytes */
187 bfd_vma a_bss
; /* length of uninitialized data area in mem */
188 bfd_vma a_syms
; /* length of symbol table data in file */
189 bfd_vma a_entry
; /* start address */
190 bfd_vma a_trsize
; /* length of text's relocation info, in bytes */
191 bfd_vma a_drsize
; /* length of data's relocation info, in bytes */
193 bfd_vma a_tload
; /* Text runtime load address */
194 bfd_vma a_dload
; /* Data runtime load address */
195 unsigned char a_talign
; /* Alignment of text segment */
196 unsigned char a_dalign
; /* Alignment of data segment */
197 unsigned char a_balign
; /* Alignment of bss segment */
198 char a_relaxable
; /* Enough info for linker relax */
201 /* Magic number is written
203 3130292827262524232221201918171615141312111009080706050403020100
204 < FLAGS >< MACHINE TYPE >< MAGIC NUMBER >
206 /* Magic number for NetBSD is
208 3130292827262524232221201918171615141312111009080706050403020100
209 < FLAGS >< MACHINE TYPE >< MAGIC NUMBER >
217 /* skip a bunch so we don't run into any of suns numbers */
218 /* make these up for the ns32k*/
219 M_NS32032
= (64), /* ns32032 running ? */
220 M_NS32532
= (64 + 5), /* ns32532 running mach */
223 M_29K
= 101, /* AMD 29000 */
224 M_386_DYNIX
= 102, /* Sequent running dynix */
225 M_ARM
= 103, /* Advanced Risc Machines ARM */
226 M_SPARCLET
= 131, /* SPARClet = M_SPARC + 128 */
227 M_386_NETBSD
= 134, /* NetBSD/i386 binary */
228 M_68K_NETBSD
= 135, /* NetBSD/m68k binary */
229 M_68K4K_NETBSD
= 136, /* NetBSD/m68k4k binary */
230 M_532_NETBSD
= 137, /* NetBSD/ns32k binary */
231 M_SPARC_NETBSD
= 138, /* NetBSD/sparc binary */
232 M_PMAX_NETBSD
= 139, /* NetBSD/pmax (MIPS little-endian) binary */
233 M_VAX_NETBSD
= 140, /* NetBSD/vax binary */
234 M_ALPHA_NETBSD
= 141, /* NetBSD/alpha binary */
235 M_ARM6_NETBSD
= 143, /* NetBSD/arm32 binary */
236 M_SPARCLET_1
= 147, /* 0x93, reserved */
237 M_MIPS1
= 151, /* MIPS R2000/R3000 binary */
238 M_MIPS2
= 152, /* MIPS R4000/R6000 binary */
239 M_SPARCLET_2
= 163, /* 0xa3, reserved */
240 M_SPARCLET_3
= 179, /* 0xb3, reserved */
241 M_SPARCLET_4
= 195, /* 0xc3, reserved */
242 M_HP200
= 200, /* HP 200 (68010) BSD binary */
243 M_HP300
= (300 % 256), /* HP 300 (68020+68881) BSD binary */
244 M_HPUX
= (0x20c % 256), /* HP 200/300 HPUX binary */
245 M_SPARCLET_5
= 211, /* 0xd3, reserved */
246 M_SPARCLET_6
= 227, /* 0xe3, reserved */
247 /* M_SPARCLET_7 = 243 / * 0xf3, reserved */
251 #define N_DYNAMIC(exec) ((exec).a_info & 0x80000000)
254 # define N_MAGIC(exec) ((exec).a_info & 0xffff)
258 # define N_MACHTYPE(exec) ((enum machine_type)(((exec).a_info >> 16) & 0xff))
262 # define N_FLAGS(exec) (((exec).a_info >> 24) & 0xff)
266 # define N_SET_INFO(exec, magic, type, flags) \
267 ((exec).a_info = ((magic) & 0xffff) \
268 | (((int)(type) & 0xff) << 16) \
269 | (((flags) & 0xff) << 24))
272 #ifndef N_SET_DYNAMIC
273 # define N_SET_DYNAMIC(exec, dynamic) \
274 ((exec).a_info = (dynamic) ? ((exec).a_info | 0x80000000) : \
275 ((exec).a_info & 0x7fffffff))
279 # define N_SET_MAGIC(exec, magic) \
280 ((exec).a_info = (((exec).a_info & 0xffff0000) | ((magic) & 0xffff)))
283 #ifndef N_SET_MACHTYPE
284 # define N_SET_MACHTYPE(exec, machtype) \
286 ((exec).a_info&0xff00ffff) | ((((int)(machtype))&0xff) << 16))
290 # define N_SET_FLAGS(exec, flags) \
292 ((exec).a_info&0x00ffffff) | (((flags) & 0xff) << 24))
295 typedef struct aout_symbol
{
302 /* The `tdata' struct for all a.out-like object file formats.
303 Various things depend on this struct being around any time an a.out
304 file is being handled. An example is dbxread.c in GDB. */
307 struct internal_exec
*hdr
; /* exec file header */
308 aout_symbol_type
*symbols
; /* symtab for input bfd */
310 /* For ease, we do this */
315 /* We remember these offsets so that after check_file_format, we have
316 no dependencies on the particular format of the exec_hdr. */
317 file_ptr sym_filepos
;
318 file_ptr str_filepos
;
320 /* Size of a relocation entry in external form */
321 unsigned reloc_entry_size
;
323 /* Size of a symbol table entry in external form */
324 unsigned symbol_entry_size
;
326 /* Page size - needed for alignment of demand paged files. */
327 unsigned long page_size
;
329 /* Segment size - needed for alignment of demand paged files. */
330 unsigned long segment_size
;
332 /* Zmagic disk block size - need to align the start of the text
333 section in ZMAGIC binaries. Normally the same as page_size. */
334 unsigned long zmagic_disk_block_size
;
336 unsigned exec_bytes_size
;
337 unsigned vma_adjusted
: 1;
339 /* used when a bfd supports several highly similar formats */
343 /* Used on HP 9000/300 running HP/UX. See hp300hpux.c. */
345 /* Used on Linux, 386BSD, etc. See include/aout/aout64.h. */
357 /* A buffer for find_nearest_line. */
360 /* The external symbol information. */
361 struct external_nlist
*external_syms
;
362 bfd_size_type external_sym_count
;
363 bfd_window sym_window
;
364 char *external_strings
;
365 bfd_size_type external_string_size
;
366 bfd_window string_window
;
367 struct aout_link_hash_entry
**sym_hashes
;
369 /* A pointer for shared library information. */
372 /* A mapping from local symbols to offsets into the global offset
373 table, used when linking on SunOS. This is indexed by the symbol
375 bfd_vma
*local_got_offsets
;
378 struct aout_data_struct
{
380 struct internal_exec e
;
383 #define adata(bfd) ((bfd)->tdata.aout_data->a)
384 #define exec_hdr(bfd) (adata(bfd).hdr)
385 #define obj_aout_symbols(bfd) (adata(bfd).symbols)
386 #define obj_textsec(bfd) (adata(bfd).textsec)
387 #define obj_datasec(bfd) (adata(bfd).datasec)
388 #define obj_bsssec(bfd) (adata(bfd).bsssec)
389 #define obj_sym_filepos(bfd) (adata(bfd).sym_filepos)
390 #define obj_str_filepos(bfd) (adata(bfd).str_filepos)
391 #define obj_reloc_entry_size(bfd) (adata(bfd).reloc_entry_size)
392 #define obj_symbol_entry_size(bfd) (adata(bfd).symbol_entry_size)
393 #define obj_aout_subformat(bfd) (adata(bfd).subformat)
394 #define obj_aout_external_syms(bfd) (adata(bfd).external_syms)
395 #define obj_aout_external_sym_count(bfd) (adata(bfd).external_sym_count)
396 #define obj_aout_sym_window(bfd) (adata(bfd).sym_window)
397 #define obj_aout_external_strings(bfd) (adata(bfd).external_strings)
398 #define obj_aout_external_string_size(bfd) (adata(bfd).external_string_size)
399 #define obj_aout_string_window(bfd) (adata(bfd).string_window)
400 #define obj_aout_sym_hashes(bfd) (adata(bfd).sym_hashes)
401 #define obj_aout_dynamic_info(bfd) (adata(bfd).dynamic_info)
403 /* We take the address of the first element of an asymbol to ensure that the
404 macro is only ever applied to an asymbol */
405 #define aout_symbol(asymbol) ((aout_symbol_type *)(&(asymbol)->the_bfd))
407 /* Information we keep for each a.out section. This is currently only
408 used by the a.out backend linker. */
410 struct aout_section_data_struct
412 /* The unswapped relocation entries for this section. */
416 #define aout_section_data(s) \
417 ((struct aout_section_data_struct *) (s)->used_by_bfd)
419 #define set_aout_section_data(s,v) \
420 ((s)->used_by_bfd = (PTR)&(v)->relocs)
422 /* Prototype declarations for functions defined in aoutx.h */
425 NAME(aout
,squirt_out_relocs
) PARAMS ((bfd
*abfd
, asection
*section
));
428 NAME(aout
,make_sections
) PARAMS ((bfd
*));
431 NAME(aout
,some_aout_object_p
) PARAMS ((bfd
*abfd
,
432 struct internal_exec
*execp
,
433 const bfd_target
*(*callback
)(bfd
*)));
436 NAME(aout
,mkobject
) PARAMS ((bfd
*abfd
));
439 NAME(aout
,machine_type
) PARAMS ((enum bfd_architecture arch
,
440 unsigned long machine
,
444 NAME(aout
,set_arch_mach
) PARAMS ((bfd
*abfd
, enum bfd_architecture arch
,
445 unsigned long machine
));
448 NAME(aout
,new_section_hook
) PARAMS ((bfd
*abfd
, asection
*newsect
));
451 NAME(aout
,set_section_contents
) PARAMS ((bfd
*abfd
, sec_ptr section
,
452 PTR location
, file_ptr offset
, bfd_size_type count
));
455 NAME(aout
,make_empty_symbol
) PARAMS ((bfd
*abfd
));
458 NAME(aout
,translate_symbol_table
) PARAMS ((bfd
*, aout_symbol_type
*,
459 struct external_nlist
*,
460 bfd_size_type
, char *,
465 NAME(aout
,slurp_symbol_table
) PARAMS ((bfd
*abfd
));
468 NAME(aout
,write_syms
) PARAMS ((bfd
*abfd
));
471 NAME(aout
,reclaim_symbol_table
) PARAMS ((bfd
*abfd
));
474 NAME(aout
,get_symtab_upper_bound
) PARAMS ((bfd
*abfd
));
477 NAME(aout
,get_symtab
) PARAMS ((bfd
*abfd
, asymbol
**location
));
480 NAME(aout
,swap_ext_reloc_in
) PARAMS ((bfd
*, struct reloc_ext_external
*,
481 arelent
*, asymbol
**, bfd_size_type
));
483 NAME(aout
,swap_std_reloc_in
) PARAMS ((bfd
*, struct reloc_std_external
*,
484 arelent
*, asymbol
**, bfd_size_type
));
487 NAME(aout
,reloc_type_lookup
) PARAMS ((bfd
*abfd
,
488 bfd_reloc_code_real_type code
));
491 NAME(aout
,slurp_reloc_table
) PARAMS ((bfd
*abfd
, sec_ptr asect
,
495 NAME(aout
,canonicalize_reloc
) PARAMS ((bfd
*abfd
, sec_ptr section
,
496 arelent
**relptr
, asymbol
**symbols
));
499 NAME(aout
,get_reloc_upper_bound
) PARAMS ((bfd
*abfd
, sec_ptr asect
));
502 NAME(aout
,reclaim_reloc
) PARAMS ((bfd
*ignore_abfd
, sec_ptr ignore
));
505 NAME(aout
,get_lineno
) PARAMS ((bfd
*ignore_abfd
, asymbol
*ignore_symbol
));
508 NAME(aout
,print_symbol
) PARAMS ((bfd
*ignore_abfd
, PTR file
,
509 asymbol
*symbol
, bfd_print_symbol_type how
));
512 NAME(aout
,get_symbol_info
) PARAMS ((bfd
*ignore_abfd
,
513 asymbol
*symbol
, symbol_info
*ret
));
516 NAME(aout
,find_nearest_line
) PARAMS ((bfd
*abfd
, asection
*section
,
517 asymbol
**symbols
, bfd_vma offset
, CONST
char **filename_ptr
,
518 CONST
char **functionname_ptr
, unsigned int *line_ptr
));
521 NAME(aout
,read_minisymbols
) PARAMS ((bfd
*, boolean
, PTR
*, unsigned int *));
524 NAME(aout
,minisymbol_to_symbol
) PARAMS ((bfd
*, boolean
, const PTR
,
528 NAME(aout
,sizeof_headers
) PARAMS ((bfd
*abfd
, boolean exec
));
531 NAME(aout
,adjust_sizes_and_vmas
) PARAMS ((bfd
*abfd
,
532 bfd_size_type
*text_size
, file_ptr
*text_end
));
535 NAME(aout
,swap_exec_header_in
) PARAMS ((bfd
*abfd
,
536 struct external_exec
*raw_bytes
, struct internal_exec
*execp
));
539 NAME(aout
,swap_exec_header_out
) PARAMS ((bfd
*abfd
,
540 struct internal_exec
*execp
, struct external_exec
*raw_bytes
));
542 struct bfd_hash_entry
*
543 NAME(aout
,link_hash_newfunc
)
544 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
547 NAME(aout
,link_hash_table_init
)
548 PARAMS ((struct aout_link_hash_table
*, bfd
*,
549 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
550 struct bfd_hash_table
*,
553 struct bfd_link_hash_table
*
554 NAME(aout
,link_hash_table_create
) PARAMS ((bfd
*));
557 NAME(aout
,link_add_symbols
) PARAMS ((bfd
*, struct bfd_link_info
*));
560 NAME(aout
,final_link
) PARAMS ((bfd
*, struct bfd_link_info
*,
561 void (*) (bfd
*, file_ptr
*, file_ptr
*,
565 NAME(aout
,bfd_free_cached_info
) PARAMS ((bfd
*));
567 /* A.out uses the generic versions of these routines... */
569 #define aout_32_get_section_contents _bfd_generic_get_section_contents
571 #define aout_64_get_section_contents _bfd_generic_get_section_contents
572 #ifndef NO_WRITE_HEADER_KLUDGE
573 #define NO_WRITE_HEADER_KLUDGE 0
576 #ifndef aout_32_bfd_is_local_label_name
577 #define aout_32_bfd_is_local_label_name bfd_generic_is_local_label_name
580 #ifndef WRITE_HEADERS
581 #define WRITE_HEADERS(abfd, execp) \
583 bfd_size_type text_size; /* dummy vars */ \
585 if (adata(abfd).magic == undecided_magic) \
586 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end); \
588 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE; \
589 execp->a_entry = bfd_get_start_address (abfd); \
591 execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * \
592 obj_reloc_entry_size (abfd)); \
593 execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * \
594 obj_reloc_entry_size (abfd)); \
595 NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes); \
597 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) return false; \
598 if (bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd) \
599 != EXEC_BYTES_SIZE) \
601 /* Now write out reloc info, followed by syms and strings */ \
603 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL \
604 && bfd_get_symcount (abfd) != 0) \
606 if (bfd_seek (abfd, (file_ptr)(N_SYMOFF(*execp)), SEEK_SET) != 0) \
609 if (! NAME(aout,write_syms)(abfd)) return false; \
612 if (bfd_seek (abfd, (file_ptr)(N_TRELOFF(*execp)), SEEK_SET) != 0) \
614 if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd))) \
617 if (bfd_seek (abfd, (file_ptr)(N_DRELOFF(*execp)), SEEK_SET) != 0) \
619 if (!NAME(aout,squirt_out_relocs)(abfd, obj_datasec (abfd))) \
624 #endif /* ! defined (LIBAOUT_H) */