* libaout.h (struct aoutdata): Add q_magic_format to subformat
[external/binutils.git] / bfd / libaout.h
1 /* BFD back-end data structures for a.out (and similar) files.
2    Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3    Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
20
21 #ifndef LIBAOUT_H
22 #define LIBAOUT_H
23
24 /* We try to encapsulate the differences in the various a.out file
25    variants in a few routines, and otherwise share large masses of code.
26    This means we only have to fix bugs in one place, most of the time.  */
27
28 #include "bfdlink.h"
29
30 /* Parameterize the a.out code based on whether it is being built
31    for a 32-bit architecture or a 64-bit architecture.  */
32 #if ARCH_SIZE==64
33 #define GET_WORD bfd_h_get_64
34 #define GET_SWORD bfd_h_get_signed_64
35 #define PUT_WORD bfd_h_put_64
36 #ifndef NAME
37 #define NAME(x,y) CAT3(x,_64_,y)
38 #endif
39 #define JNAME(x) CAT(x,_64)
40 #define BYTES_IN_WORD 8
41 #else /* ARCH_SIZE == 32 */
42 #define GET_WORD bfd_h_get_32
43 #define GET_SWORD bfd_h_get_signed_32
44 #define PUT_WORD bfd_h_put_32
45 #ifndef NAME
46 #define NAME(x,y) CAT3(x,_32_,y)
47 #endif
48 #define JNAME(x) CAT(x,_32)
49 #define BYTES_IN_WORD 4
50 #endif /* ARCH_SIZE==32 */
51
52 /* Declare at file level, since used in parameter lists, which have
53    weird scope.  */
54 struct external_exec;
55 struct external_nlist;
56 struct reloc_ext_external;
57 struct reloc_std_external;
58 \f
59 /* a.out backend linker hash table entries.  */
60
61 struct aout_link_hash_entry
62 {
63   struct bfd_link_hash_entry root;
64   /* Whether this symbol has been written out.  */
65   boolean written;
66   /* Symbol index in output file.  */
67   int indx;
68 };
69
70 /* a.out backend linker hash table.  */
71
72 struct aout_link_hash_table
73 {
74   struct bfd_link_hash_table root;
75 };
76
77 /* Look up an entry in an a.out link hash table.  */
78
79 #define aout_link_hash_lookup(table, string, create, copy, follow) \
80   ((struct aout_link_hash_entry *) \
81    bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
82
83 /* Traverse an a.out link hash table.  */
84
85 #define aout_link_hash_traverse(table, func, info)                      \
86   (bfd_link_hash_traverse                                               \
87    (&(table)->root,                                                     \
88     (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func),  \
89     (info)))
90
91 /* Get the a.out link hash table from the info structure.  This is
92    just a cast.  */
93
94 #define aout_hash_table(p) ((struct aout_link_hash_table *) ((p)->hash))
95 \f
96 /* Back-end information for various a.out targets.  */
97 struct aout_backend_data
98 {
99   /* Are ZMAGIC files mapped contiguously?  If so, the text section may
100      need more padding, if the segment size (granularity for memory access
101      control) is larger than the page size.  */
102   unsigned char zmagic_mapped_contiguous;
103   /* If this flag is set, ZMAGIC/NMAGIC file headers get mapped in with the
104      text section, which starts immediately after the file header.
105      If not, the text section starts on the next page.  */
106   unsigned char text_includes_header;
107
108   /* The value to pass to N_SET_FLAGS.  */
109   unsigned char exec_hdr_flags;
110
111   /* If the text section VMA isn't specified, and we need an absolute
112      address, use this as the default.  If we're producing a relocatable
113      file, zero is always used.  */
114   /* ?? Perhaps a callback would be a better choice?  Will this do anything
115      reasonable for a format that handles multiple CPUs with different
116      load addresses for each?  */
117   bfd_vma default_text_vma;
118
119   /* Callback for setting the page and segment sizes, if they can't be
120      trivially determined from the architecture.  */
121   boolean (*set_sizes) PARAMS ((bfd *));
122
123   /* zmagic files only. For go32, the length of the exec header contributes
124      to the size of the text section in the file for alignment purposes but
125      does *not* get counted in the length of the text section. */
126   unsigned char exec_header_not_counted;
127
128   /* Callback from the add symbols phase of the linker code to handle
129      a dynamic object.  */
130   boolean (*add_dynamic_symbols) PARAMS ((bfd *, struct bfd_link_info *));
131
132   /* Callback from the add symbols phase of the linker code to handle
133      adding a single symbol to the global linker hash table.  */
134   boolean (*add_one_symbol) PARAMS ((struct bfd_link_info *, bfd *,
135                                      const char *, flagword, asection *,
136                                      bfd_vma, const char *, boolean,
137                                      boolean,
138                                      struct bfd_link_hash_entry **));
139
140   /* Called to handle linking a dynamic object.  */
141   boolean (*link_dynamic_object) PARAMS ((struct bfd_link_info *, bfd *));
142
143   /* Called for each global symbol being written out by the linker.
144      This should write out the dynamic symbol information.  */
145   boolean (*write_dynamic_symbol) PARAMS ((bfd *, struct bfd_link_info *,
146                                            struct aout_link_hash_entry *));
147
148   /* This callback is called by the linker for each reloc against an
149      external symbol.  RELOC is a pointer to the unswapped reloc.  If
150      *SKIP is set to true, the reloc will be skipped.  */
151   boolean (*check_dynamic_reloc) PARAMS ((struct bfd_link_info *info,
152                                           bfd *input_bfd,
153                                           asection *input_section,
154                                           struct aout_link_hash_entry *h,
155                                           PTR reloc, boolean *skip));
156
157   /* Called at the end of a link to finish up any dynamic linking
158      information.  */
159   boolean (*finish_dynamic_link) PARAMS ((bfd *, struct bfd_link_info *));
160 };
161 #define aout_backend_info(abfd) \
162         ((CONST struct aout_backend_data *)((abfd)->xvec->backend_data))
163
164 /* This is the layout in memory of a "struct exec" while we process it.
165    All 'lengths' are given as a number of bytes.
166    All 'alignments' are for relinkable files only;  an alignment of
167         'n' indicates the corresponding segment must begin at an
168         address that is a multiple of (2**n).  */
169
170 struct internal_exec
171 {
172     long a_info;                /* Magic number and flags, packed */
173     bfd_vma a_text;             /* length of text, in bytes  */
174     bfd_vma a_data;             /* length of data, in bytes  */
175     bfd_vma a_bss;              /* length of uninitialized data area in mem */
176     bfd_vma a_syms;             /* length of symbol table data in file */
177     bfd_vma a_entry;            /* start address */
178     bfd_vma a_trsize;           /* length of text's relocation info, in bytes */
179     bfd_vma a_drsize;           /* length of data's relocation info, in bytes */
180     /* Added for i960 */
181     bfd_vma a_tload;            /* Text runtime load address */
182     bfd_vma a_dload;            /* Data runtime load address */
183     unsigned char a_talign;     /* Alignment of text segment */
184     unsigned char a_dalign;     /* Alignment of data segment */
185     unsigned char a_balign;     /* Alignment of bss segment */
186     char a_relaxable;           /* Enough info for linker relax */
187 };
188
189 /* Magic number is written 
190 < MSB        >
191 3130292827262524232221201918171615141312111009080706050403020100
192 < FLAGS      >< MACHINE TYPE ><  MAGIC NUMBER                  >
193 */
194 /* Magic number for NetBSD is
195 <MSB         >
196 3130292827262524232221201918171615141312111009080706050403020100
197 < FLAGS    ><                  ><  MAGIC NUMBER                >
198 */
199
200 enum machine_type {
201   M_UNKNOWN = 0,
202   M_68010 = 1,
203   M_68020 = 2,
204   M_SPARC = 3,
205   /* skip a bunch so we don't run into any of suns numbers */
206   M_386 = 100,
207   M_29K = 101,          /* AMD 29000 */
208   M_386_DYNIX = 102,    /* Sequent running dynix */
209   M_386_NETBSD = 134,           /* NetBSD/386 binary */
210   M_MIPS1 = 151,        /* MIPS R2000/R3000 binary */
211   M_MIPS2 = 152,        /* MIPS R4000/R6000 binary */
212   M_HP200 = 200,        /* HP 200 (68010) BSD binary */
213   M_HP300 = (300 % 256), /* HP 300 (68020+68881) BSD binary */
214   M_HPUX = (0x20c % 256)/* HP 200/300 HPUX binary */
215 };
216
217 #define N_DYNAMIC(exec) ((exec).a_info & 0x80000000)
218
219 #ifndef N_MAGIC
220 # define N_MAGIC(exec) ((exec).a_info & 0xffff)
221 #endif
222
223 #ifndef N_MACHTYPE
224 # define N_MACHTYPE(exec) ((enum machine_type)(((exec).a_info >> 16) & 0xff))
225 #endif
226
227 #ifndef N_FLAGS
228 # define N_FLAGS(exec) (((exec).a_info >> 24) & 0xff)
229 #endif
230
231 #ifndef N_SET_INFO
232 # define N_SET_INFO(exec, magic, type, flags) \
233 ((exec).a_info = ((magic) & 0xffff) \
234  | (((int)(type) & 0xff) << 16) \
235  | (((flags) & 0xff) << 24))
236 #endif
237
238 #ifndef N_SET_DYNAMIC
239 # define N_SET_DYNAMIC(exec, dynamic) \
240 ((exec).a_info = (dynamic) ? ((exec).a_info | 0x80000000) : \
241 ((exec).a_info & 0x7fffffff))
242 #endif
243
244 #ifndef N_SET_MAGIC
245 # define N_SET_MAGIC(exec, magic) \
246 ((exec).a_info = (((exec).a_info & 0xffff0000) | ((magic) & 0xffff)))
247 #endif
248
249 #ifndef N_SET_MACHTYPE
250 # define N_SET_MACHTYPE(exec, machtype) \
251 ((exec).a_info = \
252  ((exec).a_info&0xff00ffff) | ((((int)(machtype))&0xff) << 16))
253 #endif
254
255 #ifndef N_SET_FLAGS
256 # define N_SET_FLAGS(exec, flags) \
257 ((exec).a_info = \
258  ((exec).a_info&0x00ffffff) | (((flags) & 0xff) << 24))
259 #endif
260
261 typedef struct aout_symbol {
262   asymbol symbol;
263   short desc;
264   char other;
265   unsigned char type;
266 } aout_symbol_type;
267
268 /* The `tdata' struct for all a.out-like object file formats.
269    Various things depend on this struct being around any time an a.out
270    file is being handled.  An example is dbxread.c in GDB.  */
271
272 struct aoutdata {
273   struct internal_exec *hdr;            /* exec file header */
274   aout_symbol_type *symbols;            /* symtab for input bfd */
275   
276   /* For ease, we do this */
277   asection *textsec;
278   asection *datasec;
279   asection *bsssec;
280
281   /* We remember these offsets so that after check_file_format, we have
282      no dependencies on the particular format of the exec_hdr.  */
283   file_ptr sym_filepos;
284   file_ptr str_filepos;
285
286   /* Size of a relocation entry in external form */
287   unsigned reloc_entry_size;
288
289   /* Size of a symbol table entry in external form */
290   unsigned symbol_entry_size;
291
292   /* Page size - needed for alignment of demand paged files. */
293   unsigned long page_size;
294
295   /* Segment size - needed for alignment of demand paged files. */
296   unsigned long segment_size;
297
298   unsigned exec_bytes_size;
299   unsigned vma_adjusted : 1;
300
301   /* used when a bfd supports several highly similar formats */
302   enum
303     {
304       default_format = 0,
305       /* Used on HP 9000/300 running HP/UX.  See hp300hpux.c.  */
306       gnu_encap_format,
307       /* Used on Linux, 386BSD, etc.  See include/aout/aout64.h.  */
308       q_magic_format
309     } subformat;
310
311   enum
312     {
313       undecided_magic = 0,
314       z_magic,
315       o_magic,
316       n_magic
317     } magic;
318
319   /* The external symbol information.  */
320   struct external_nlist *external_syms;
321   bfd_size_type external_sym_count;
322   char *external_strings;
323   bfd_size_type external_string_size;
324   struct aout_link_hash_entry **sym_hashes;
325
326   /* A pointer for shared library information.  */
327   PTR dynamic_info;
328 };
329
330 struct  aout_data_struct {
331     struct aoutdata a;
332     struct internal_exec e;
333 };
334
335 #define adata(bfd)              ((bfd)->tdata.aout_data->a)
336 #define exec_hdr(bfd)           (adata(bfd).hdr)
337 #define obj_aout_symbols(bfd)   (adata(bfd).symbols)
338 #define obj_textsec(bfd)        (adata(bfd).textsec)
339 #define obj_datasec(bfd)        (adata(bfd).datasec)
340 #define obj_bsssec(bfd)         (adata(bfd).bsssec)
341 #define obj_sym_filepos(bfd)    (adata(bfd).sym_filepos)
342 #define obj_str_filepos(bfd)    (adata(bfd).str_filepos)
343 #define obj_reloc_entry_size(bfd) (adata(bfd).reloc_entry_size)
344 #define obj_symbol_entry_size(bfd) (adata(bfd).symbol_entry_size)
345 #define obj_aout_subformat(bfd) (adata(bfd).subformat)
346 #define obj_aout_external_syms(bfd) (adata(bfd).external_syms)
347 #define obj_aout_external_sym_count(bfd) (adata(bfd).external_sym_count)
348 #define obj_aout_external_strings(bfd) (adata(bfd).external_strings)
349 #define obj_aout_external_string_size(bfd) (adata(bfd).external_string_size)
350 #define obj_aout_sym_hashes(bfd) (adata(bfd).sym_hashes)
351 #define obj_aout_dynamic_info(bfd) (adata(bfd).dynamic_info)
352
353 /* We take the address of the first element of an asymbol to ensure that the
354    macro is only ever applied to an asymbol */
355 #define aout_symbol(asymbol) ((aout_symbol_type *)(&(asymbol)->the_bfd))
356
357 /* Information we keep for each a.out section.  This is currently only
358    used by the a.out backend linker.  */
359
360 struct aout_section_data_struct
361 {
362   /* The unswapped relocation entries for this section.  */
363   PTR relocs;
364 };
365
366 #define aout_section_data(s) \
367   ((struct aout_section_data_struct *) (s)->used_by_bfd)
368
369 /* Prototype declarations for functions defined in aoutx.h  */
370
371 boolean
372 NAME(aout,squirt_out_relocs) PARAMS ((bfd *abfd, asection *section));
373
374 boolean
375 NAME(aout,make_sections) PARAMS ((bfd *));
376
377 bfd_target *
378 NAME(aout,some_aout_object_p) PARAMS ((bfd *abfd,
379                         struct internal_exec *execp,
380                         bfd_target * (*callback)(bfd *)));
381
382 boolean
383 NAME(aout,mkobject) PARAMS ((bfd *abfd));
384
385 enum machine_type
386 NAME(aout,machine_type) PARAMS ((enum bfd_architecture arch,
387                                  unsigned long machine,
388                                  boolean *unknown));
389
390 boolean
391 NAME(aout,set_arch_mach) PARAMS ((bfd *abfd, enum bfd_architecture arch,
392                                   unsigned long machine));
393
394 boolean
395 NAME(aout,new_section_hook) PARAMS ((bfd *abfd, asection *newsect));
396
397 boolean
398 NAME(aout,set_section_contents) PARAMS ((bfd *abfd, sec_ptr section,
399                          PTR location, file_ptr offset, bfd_size_type count));
400
401 asymbol *
402 NAME(aout,make_empty_symbol) PARAMS ((bfd *abfd));
403
404 boolean
405 NAME(aout,translate_symbol_table) PARAMS ((bfd *, aout_symbol_type *,
406                                            struct external_nlist *,
407                                            bfd_size_type, char *,
408                                            bfd_size_type,
409                                            boolean dynamic));
410
411 boolean
412 NAME(aout,slurp_symbol_table) PARAMS ((bfd *abfd));
413
414 boolean
415 NAME(aout,write_syms) PARAMS ((bfd *abfd));
416
417 void
418 NAME(aout,reclaim_symbol_table) PARAMS ((bfd *abfd));
419
420 long
421 NAME(aout,get_symtab_upper_bound) PARAMS ((bfd *abfd));
422
423 long
424 NAME(aout,get_symtab) PARAMS ((bfd *abfd, asymbol **location));
425
426 void
427 NAME(aout,swap_ext_reloc_in) PARAMS ((bfd *, struct reloc_ext_external *,
428                                       arelent *, asymbol **));
429 void
430 NAME(aout,swap_std_reloc_in) PARAMS ((bfd *, struct reloc_std_external *,
431                                       arelent *, asymbol **));
432
433 boolean
434 NAME(aout,slurp_reloc_table) PARAMS ((bfd *abfd, sec_ptr asect,
435                                       asymbol **symbols));
436
437 long
438 NAME(aout,canonicalize_reloc) PARAMS ((bfd *abfd, sec_ptr section,
439                                        arelent **relptr, asymbol **symbols));
440
441 long
442 NAME(aout,get_reloc_upper_bound) PARAMS ((bfd *abfd, sec_ptr asect));
443
444 void
445 NAME(aout,reclaim_reloc) PARAMS ((bfd *ignore_abfd, sec_ptr ignore));
446
447 alent *
448 NAME(aout,get_lineno) PARAMS ((bfd *ignore_abfd, asymbol *ignore_symbol));
449
450 void
451 NAME(aout,print_symbol) PARAMS ((bfd *ignore_abfd, PTR file,
452                             asymbol *symbol, bfd_print_symbol_type how));
453
454 void
455 NAME(aout,get_symbol_info) PARAMS ((bfd *ignore_abfd,
456                            asymbol *symbol, symbol_info *ret));
457
458 boolean
459 NAME(aout,find_nearest_line) PARAMS ((bfd *abfd, asection *section,
460       asymbol **symbols, bfd_vma offset, CONST char **filename_ptr,
461       CONST char **functionname_ptr, unsigned int *line_ptr));
462
463 int
464 NAME(aout,sizeof_headers) PARAMS ((bfd *abfd, boolean exec));
465
466 boolean
467 NAME(aout,adjust_sizes_and_vmas) PARAMS ((bfd *abfd,
468        bfd_size_type *text_size, file_ptr *text_end));
469
470 void
471 NAME(aout,swap_exec_header_in) PARAMS ((bfd *abfd,
472        struct external_exec *raw_bytes, struct internal_exec *execp));
473
474 void
475 NAME(aout,swap_exec_header_out) PARAMS ((bfd *abfd,
476        struct internal_exec *execp, struct external_exec *raw_bytes));
477
478 struct bfd_hash_entry *
479 NAME(aout,link_hash_newfunc)
480   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
481
482 boolean
483 NAME(aout,link_hash_table_init)
484      PARAMS ((struct aout_link_hash_table *, bfd *,
485               struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
486                                           struct bfd_hash_table *,
487                                           const char *)));
488
489 struct bfd_link_hash_table *
490 NAME(aout,link_hash_table_create) PARAMS ((bfd *));
491
492 boolean
493 NAME(aout,link_add_symbols) PARAMS ((bfd *, struct bfd_link_info *));
494
495 boolean
496 NAME(aout,final_link) PARAMS ((bfd *, struct bfd_link_info *,
497                                void (*) (bfd *, file_ptr *, file_ptr *,
498                                          file_ptr *)));
499
500 boolean
501 NAME(aout,bfd_free_cached_info) PARAMS ((bfd *));
502
503 /* Prototypes for functions in stab-syms.c. */
504
505 CONST char *
506 aout_stab_name PARAMS ((int code));
507
508 /* A.out uses the generic versions of these routines... */
509
510 #define aout_32_get_section_contents    _bfd_generic_get_section_contents
511
512 #define aout_64_get_section_contents    _bfd_generic_get_section_contents
513 #ifndef NO_WRITE_HEADER_KLUDGE
514 #define NO_WRITE_HEADER_KLUDGE 0
515 #endif
516
517 #ifndef aout_32_bfd_is_local_label
518 #define aout_32_bfd_is_local_label bfd_generic_is_local_label
519 #endif
520
521 #ifndef WRITE_HEADERS
522 #define WRITE_HEADERS(abfd, execp)                                            \
523       {                                                                       \
524         bfd_size_type text_size; /* dummy vars */                             \
525         file_ptr text_end;                                                    \
526         if (adata(abfd).magic == undecided_magic)                             \
527           NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end);     \
528                                                                               \
529         execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;        \
530         execp->a_entry = bfd_get_start_address (abfd);                        \
531                                                                               \
532         execp->a_trsize = ((obj_textsec (abfd)->reloc_count) *                \
533                            obj_reloc_entry_size (abfd));                      \
534         execp->a_drsize = ((obj_datasec (abfd)->reloc_count) *                \
535                            obj_reloc_entry_size (abfd));                      \
536         NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes);           \
537                                                                               \
538         if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) return false;       \
539         if (bfd_write ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)           \
540             != EXEC_BYTES_SIZE)                                               \
541           return false;                                                       \
542         /* Now write out reloc info, followed by syms and strings */          \
543                                                                               \
544         if (bfd_get_outsymbols (abfd) != (asymbol **) NULL                    \
545             && bfd_get_symcount (abfd) != 0)                                  \
546             {                                                                 \
547               if (bfd_seek (abfd, (file_ptr)(N_SYMOFF(*execp)), SEEK_SET)     \
548                   != 0)                                                       \
549                 return false;                                                 \
550                                                                               \
551               if (! NAME(aout,write_syms)(abfd)) return false;                \
552                                                                               \
553               if (bfd_seek (abfd, (file_ptr)(N_TRELOFF(*execp)), SEEK_SET)    \
554                   != 0)                                                       \
555                 return false;                                                 \
556                                                                               \
557               if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd)))   \
558                 return false;                                                 \
559               if (bfd_seek (abfd, (file_ptr)(N_DRELOFF(*execp)), SEEK_SET)    \
560                   != 0)                                                       \
561                 return false;                                                 \
562                                                                               \
563               if (!NAME(aout,squirt_out_relocs)(abfd, obj_datasec (abfd)))    \
564                 return false;                                                 \
565             }                                                                 \
566       }                                                                       
567 #endif
568
569 #endif /* ! defined (LIBAOUT_H) */