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 */
248 M_SPARCLITE_LE
= 243,
249 M_CRIS
= 255 /* Axis CRIS binary. */
252 #define N_DYNAMIC(exec) ((exec).a_info & 0x80000000)
255 # define N_MAGIC(exec) ((exec).a_info & 0xffff)
259 # define N_MACHTYPE(exec) ((enum machine_type)(((exec).a_info >> 16) & 0xff))
263 # define N_FLAGS(exec) (((exec).a_info >> 24) & 0xff)
267 # define N_SET_INFO(exec, magic, type, flags) \
268 ((exec).a_info = ((magic) & 0xffff) \
269 | (((int)(type) & 0xff) << 16) \
270 | (((flags) & 0xff) << 24))
273 #ifndef N_SET_DYNAMIC
274 # define N_SET_DYNAMIC(exec, dynamic) \
275 ((exec).a_info = (dynamic) ? ((exec).a_info | 0x80000000) : \
276 ((exec).a_info & 0x7fffffff))
280 # define N_SET_MAGIC(exec, magic) \
281 ((exec).a_info = (((exec).a_info & 0xffff0000) | ((magic) & 0xffff)))
284 #ifndef N_SET_MACHTYPE
285 # define N_SET_MACHTYPE(exec, machtype) \
287 ((exec).a_info&0xff00ffff) | ((((int)(machtype))&0xff) << 16))
291 # define N_SET_FLAGS(exec, flags) \
293 ((exec).a_info&0x00ffffff) | (((flags) & 0xff) << 24))
296 typedef struct aout_symbol
{
303 /* The `tdata' struct for all a.out-like object file formats.
304 Various things depend on this struct being around any time an a.out
305 file is being handled. An example is dbxread.c in GDB. */
308 struct internal_exec
*hdr
; /* exec file header */
309 aout_symbol_type
*symbols
; /* symtab for input bfd */
311 /* For ease, we do this */
316 /* We remember these offsets so that after check_file_format, we have
317 no dependencies on the particular format of the exec_hdr. */
318 file_ptr sym_filepos
;
319 file_ptr str_filepos
;
321 /* Size of a relocation entry in external form */
322 unsigned reloc_entry_size
;
324 /* Size of a symbol table entry in external form */
325 unsigned symbol_entry_size
;
327 /* Page size - needed for alignment of demand paged files. */
328 unsigned long page_size
;
330 /* Segment size - needed for alignment of demand paged files. */
331 unsigned long segment_size
;
333 /* Zmagic disk block size - need to align the start of the text
334 section in ZMAGIC binaries. Normally the same as page_size. */
335 unsigned long zmagic_disk_block_size
;
337 unsigned exec_bytes_size
;
338 unsigned vma_adjusted
: 1;
340 /* used when a bfd supports several highly similar formats */
344 /* Used on HP 9000/300 running HP/UX. See hp300hpux.c. */
346 /* Used on Linux, 386BSD, etc. See include/aout/aout64.h. */
358 /* A buffer for find_nearest_line. */
361 /* The external symbol information. */
362 struct external_nlist
*external_syms
;
363 bfd_size_type external_sym_count
;
364 bfd_window sym_window
;
365 char *external_strings
;
366 bfd_size_type external_string_size
;
367 bfd_window string_window
;
368 struct aout_link_hash_entry
**sym_hashes
;
370 /* A pointer for shared library information. */
373 /* A mapping from local symbols to offsets into the global offset
374 table, used when linking on SunOS. This is indexed by the symbol
376 bfd_vma
*local_got_offsets
;
379 struct aout_data_struct
{
381 struct internal_exec e
;
384 #define adata(bfd) ((bfd)->tdata.aout_data->a)
385 #define exec_hdr(bfd) (adata(bfd).hdr)
386 #define obj_aout_symbols(bfd) (adata(bfd).symbols)
387 #define obj_textsec(bfd) (adata(bfd).textsec)
388 #define obj_datasec(bfd) (adata(bfd).datasec)
389 #define obj_bsssec(bfd) (adata(bfd).bsssec)
390 #define obj_sym_filepos(bfd) (adata(bfd).sym_filepos)
391 #define obj_str_filepos(bfd) (adata(bfd).str_filepos)
392 #define obj_reloc_entry_size(bfd) (adata(bfd).reloc_entry_size)
393 #define obj_symbol_entry_size(bfd) (adata(bfd).symbol_entry_size)
394 #define obj_aout_subformat(bfd) (adata(bfd).subformat)
395 #define obj_aout_external_syms(bfd) (adata(bfd).external_syms)
396 #define obj_aout_external_sym_count(bfd) (adata(bfd).external_sym_count)
397 #define obj_aout_sym_window(bfd) (adata(bfd).sym_window)
398 #define obj_aout_external_strings(bfd) (adata(bfd).external_strings)
399 #define obj_aout_external_string_size(bfd) (adata(bfd).external_string_size)
400 #define obj_aout_string_window(bfd) (adata(bfd).string_window)
401 #define obj_aout_sym_hashes(bfd) (adata(bfd).sym_hashes)
402 #define obj_aout_dynamic_info(bfd) (adata(bfd).dynamic_info)
404 /* We take the address of the first element of an asymbol to ensure that the
405 macro is only ever applied to an asymbol */
406 #define aout_symbol(asymbol) ((aout_symbol_type *)(&(asymbol)->the_bfd))
408 /* Information we keep for each a.out section. This is currently only
409 used by the a.out backend linker. */
411 struct aout_section_data_struct
413 /* The unswapped relocation entries for this section. */
417 #define aout_section_data(s) \
418 ((struct aout_section_data_struct *) (s)->used_by_bfd)
420 #define set_aout_section_data(s,v) \
421 ((s)->used_by_bfd = (PTR)&(v)->relocs)
423 /* Prototype declarations for functions defined in aoutx.h */
426 NAME(aout
,squirt_out_relocs
) PARAMS ((bfd
*abfd
, asection
*section
));
429 NAME(aout
,make_sections
) PARAMS ((bfd
*));
432 NAME(aout
,some_aout_object_p
) PARAMS ((bfd
*abfd
,
433 struct internal_exec
*execp
,
434 const bfd_target
*(*callback
)(bfd
*)));
437 NAME(aout
,mkobject
) PARAMS ((bfd
*abfd
));
440 NAME(aout
,machine_type
) PARAMS ((enum bfd_architecture arch
,
441 unsigned long machine
,
445 NAME(aout
,set_arch_mach
) PARAMS ((bfd
*abfd
, enum bfd_architecture arch
,
446 unsigned long machine
));
449 NAME(aout
,new_section_hook
) PARAMS ((bfd
*abfd
, asection
*newsect
));
452 NAME(aout
,set_section_contents
) PARAMS ((bfd
*abfd
, sec_ptr section
,
453 PTR location
, file_ptr offset
, bfd_size_type count
));
456 NAME(aout
,make_empty_symbol
) PARAMS ((bfd
*abfd
));
459 NAME(aout
,translate_symbol_table
) PARAMS ((bfd
*, aout_symbol_type
*,
460 struct external_nlist
*,
461 bfd_size_type
, char *,
466 NAME(aout
,slurp_symbol_table
) PARAMS ((bfd
*abfd
));
469 NAME(aout
,write_syms
) PARAMS ((bfd
*abfd
));
472 NAME(aout
,reclaim_symbol_table
) PARAMS ((bfd
*abfd
));
475 NAME(aout
,get_symtab_upper_bound
) PARAMS ((bfd
*abfd
));
478 NAME(aout
,get_symtab
) PARAMS ((bfd
*abfd
, asymbol
**location
));
481 NAME(aout
,swap_ext_reloc_in
) PARAMS ((bfd
*, struct reloc_ext_external
*,
482 arelent
*, asymbol
**, bfd_size_type
));
484 NAME(aout
,swap_std_reloc_in
) PARAMS ((bfd
*, struct reloc_std_external
*,
485 arelent
*, asymbol
**, bfd_size_type
));
488 NAME(aout
,reloc_type_lookup
) PARAMS ((bfd
*abfd
,
489 bfd_reloc_code_real_type code
));
492 NAME(aout
,slurp_reloc_table
) PARAMS ((bfd
*abfd
, sec_ptr asect
,
496 NAME(aout
,canonicalize_reloc
) PARAMS ((bfd
*abfd
, sec_ptr section
,
497 arelent
**relptr
, asymbol
**symbols
));
500 NAME(aout
,get_reloc_upper_bound
) PARAMS ((bfd
*abfd
, sec_ptr asect
));
503 NAME(aout
,reclaim_reloc
) PARAMS ((bfd
*ignore_abfd
, sec_ptr ignore
));
506 NAME(aout
,get_lineno
) PARAMS ((bfd
*ignore_abfd
, asymbol
*ignore_symbol
));
509 NAME(aout
,print_symbol
) PARAMS ((bfd
*ignore_abfd
, PTR file
,
510 asymbol
*symbol
, bfd_print_symbol_type how
));
513 NAME(aout
,get_symbol_info
) PARAMS ((bfd
*ignore_abfd
,
514 asymbol
*symbol
, symbol_info
*ret
));
517 NAME(aout
,find_nearest_line
) PARAMS ((bfd
*abfd
, asection
*section
,
518 asymbol
**symbols
, bfd_vma offset
, CONST
char **filename_ptr
,
519 CONST
char **functionname_ptr
, unsigned int *line_ptr
));
522 NAME(aout
,read_minisymbols
) PARAMS ((bfd
*, boolean
, PTR
*, unsigned int *));
525 NAME(aout
,minisymbol_to_symbol
) PARAMS ((bfd
*, boolean
, const PTR
,
529 NAME(aout
,sizeof_headers
) PARAMS ((bfd
*abfd
, boolean exec
));
532 NAME(aout
,adjust_sizes_and_vmas
) PARAMS ((bfd
*abfd
,
533 bfd_size_type
*text_size
, file_ptr
*text_end
));
536 NAME(aout
,swap_exec_header_in
) PARAMS ((bfd
*abfd
,
537 struct external_exec
*raw_bytes
, struct internal_exec
*execp
));
540 NAME(aout
,swap_exec_header_out
) PARAMS ((bfd
*abfd
,
541 struct internal_exec
*execp
, struct external_exec
*raw_bytes
));
543 struct bfd_hash_entry
*
544 NAME(aout
,link_hash_newfunc
)
545 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
548 NAME(aout
,link_hash_table_init
)
549 PARAMS ((struct aout_link_hash_table
*, bfd
*,
550 struct bfd_hash_entry
*(*) (struct bfd_hash_entry
*,
551 struct bfd_hash_table
*,
554 struct bfd_link_hash_table
*
555 NAME(aout
,link_hash_table_create
) PARAMS ((bfd
*));
558 NAME(aout
,link_add_symbols
) PARAMS ((bfd
*, struct bfd_link_info
*));
561 NAME(aout
,final_link
) PARAMS ((bfd
*, struct bfd_link_info
*,
562 void (*) (bfd
*, file_ptr
*, file_ptr
*,
566 NAME(aout
,bfd_free_cached_info
) PARAMS ((bfd
*));
568 /* A.out uses the generic versions of these routines... */
570 #define aout_32_get_section_contents _bfd_generic_get_section_contents
572 #define aout_64_get_section_contents _bfd_generic_get_section_contents
573 #ifndef NO_WRITE_HEADER_KLUDGE
574 #define NO_WRITE_HEADER_KLUDGE 0
577 #ifndef aout_32_bfd_is_local_label_name
578 #define aout_32_bfd_is_local_label_name bfd_generic_is_local_label_name
581 #ifndef WRITE_HEADERS
582 #define WRITE_HEADERS(abfd, execp) \
584 bfd_size_type text_size; /* dummy vars */ \
586 if (adata(abfd).magic == undecided_magic) \
587 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end); \
589 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE; \
590 execp->a_entry = bfd_get_start_address (abfd); \
592 execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * \
593 obj_reloc_entry_size (abfd)); \
594 execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * \
595 obj_reloc_entry_size (abfd)); \
596 NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes); \
598 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) return false; \
599 if (bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd) \
600 != EXEC_BYTES_SIZE) \
602 /* Now write out reloc info, followed by syms and strings */ \
604 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL \
605 && bfd_get_symcount (abfd) != 0) \
607 if (bfd_seek (abfd, (file_ptr)(N_SYMOFF(*execp)), SEEK_SET) != 0) \
610 if (! NAME(aout,write_syms)(abfd)) return false; \
613 if (bfd_seek (abfd, (file_ptr)(N_TRELOFF(*execp)), SEEK_SET) != 0) \
615 if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd))) \
618 if (bfd_seek (abfd, (file_ptr)(N_DRELOFF(*execp)), SEEK_SET) != 0) \
620 if (!NAME(aout,squirt_out_relocs)(abfd, obj_datasec (abfd))) \
625 #endif /* ! defined (LIBAOUT_H) */