*** empty log message ***
[external/binutils.git] / include / bfd.h
1
2           /* A -*- C -*- header file for the bfd library */
3
4
5 /*** bfd.h -- The only header file required by users of the bfd library */
6
7 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
8
9 This file is part of BFD, the Binary File Diddler.
10
11 BFD is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 1, or (at your option)
14 any later version.
15
16 BFD is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with BFD; see the file COPYING.  If not, write to
23 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
24
25 /* executable_object_p is gone -- you can check the bfd flags and call
26    access() yourself */
27
28 #ifndef __BFD_H_SEEN__
29 #define __BFD_H_SEEN__
30
31 #include "ansidecl.h"
32 #include "sysdep.h"
33 #include "obstack.h"
34
35 /* Make it easier to declare prototypes (puts conditional here) */
36 #ifndef PROTO
37 #       if __STDC__
38 #               define PROTO(type, name, arglist) type name arglist
39 #       else
40 #               define PROTO(type, name, arglist) type name ()
41 #       endif
42 #endif
43
44 #define BFD_VERSION "1.10"
45
46 /* forward declaration */
47 typedef struct _bfd bfd;
48
49 /* General rules: functions which are boolean return true on success
50    and false on failure (unless they're a predicate).   -- bfd.doc */
51 /* I'm sure this is going to break something and someone is going to
52    force me to change it. */
53 typedef enum boolean {false, true} boolean;
54
55 /* Try to avoid breaking stuff */
56 typedef  long int file_ptr;
57
58 /* Support for different sizes of target format ints */
59
60 #ifdef TARGET_64_BIT
61 /* 64 bit machines use these items */
62 typedef uint64_type rawdata_offset;
63 typedef uint64_type bfd_vma;
64 typedef uint64_type bfd_word;
65 typedef uint64_type bfd_offset;
66 typedef uint64_type bfd_size_type;
67 typedef uint64_type  symvalue;
68 #define printf_vma(x) printf("%08x%08x",uint64_typeHIGH(x),    uint64_typeLOW(x))
69 #define fprintf_vma(s,x) fprintf(s,"%08x%08x",uint64_typeHIGH(x), uint64_typeLOW(x))
70 #else
71 typedef unsigned long int rawdata_offset;
72 typedef unsigned long bfd_vma;
73 typedef unsigned long bfd_offset;
74 typedef unsigned long bfd_word;
75 typedef unsigned long bfd_size;
76 typedef unsigned long symvalue;
77 typedef unsigned long bfd_size_type;
78 #define printf_vma(x) printf("%08x",x)
79 #define fprintf_vma(s,x) fprintf(s,"%08x",x)
80 #endif
81
82 typedef unsigned int flagword;  /* 32 bits of flags */
83 \f
84 /** File formats */
85
86 typedef enum bfd_format {
87               bfd_unknown = 0,  /* file format is unknown */
88               bfd_object,       /* linker/assember/compiler output */
89               bfd_archive,      /* object archive file */
90               bfd_core,         /* core dump */
91               bfd_type_end}     /* marks the end; don't use it! */
92          bfd_format;
93
94 /* Object file flag values */
95 #define NO_FLAGS 0
96 #define HAS_RELOC   001
97 #define EXEC_P      002
98 #define HAS_LINENO  004
99 #define HAS_DEBUG   010
100 #define HAS_SYMS    020
101 #define HAS_LOCALS  040
102 #define DYNAMIC     0100
103 #define WP_TEXT     0200
104 #define D_PAGED     0400
105
106 /* This enum gives the object file's CPU architecture, in a global sense.
107    E.g. what processor family does it belong to?  There is another field,
108    which indicates what processor within the family is in use.  */
109 enum bfd_architecture {
110         bfd_arch_unknown,       /* File arch not known */
111         bfd_arch_obscure,       /* File arch known, not one of these */
112         bfd_arch_m68k,          /* Motorola 68xxx */
113         bfd_arch_vax,           /* DEC Vax */
114         bfd_arch_i960,          /* Intel 960 */
115                         
116                                 /* The order of the following is important.
117                                  * A lower number indicates a machine type
118                                  * that only accepts a subset of the
119                                  * instructions available to machines with
120                                  * higher numbers.
121                                  *
122                                  * The exception is the "ca", which is
123                                  * incompatible with all other machines except
124                                  * "core".
125                                  */
126 #define bfd_mach_i960_core      1
127 #define bfd_mach_i960_ka_sa     2
128 #define bfd_mach_i960_kb_sb     3
129 #define bfd_mach_i960_mc        4
130 #define bfd_mach_i960_xa        5
131 #define bfd_mach_i960_ca        6
132
133
134         bfd_arch_a29k,          /* AMD 29000 */
135         bfd_arch_sparc,         /* Sun (SPARC International) SPARC */
136         bfd_arch_mips,          /* MIPS Rxxxx */
137         bfd_arch_i386,          /* Intel 386 */
138         bfd_arch_ns32k,         /* National Semiconductor 32xxx */
139         bfd_arch_tahoe,         /* CCI/Harris Tahoe */
140         bfd_arch_i860,          /* Intel 860 */
141         bfd_arch_romp,          /* IBM ROMP RS/6000 */
142         bfd_arch_alliant,       /* Alliant */
143         bfd_arch_convex,        /* Convex */
144         bfd_arch_m88k,          /* Motorola 88xxx */
145         bfd_arch_pyramid,       /* Pyramid Technology */
146         bfd_arch_h8_300,        /* Hitachi H8/300 */
147         bfd_arch_last
148 };
149 \f
150 /* symbols and relocation */
151
152 typedef unsigned long symindex;
153
154 #define BFD_NO_MORE_SYMBOLS ((symindex) ~0)
155
156 typedef enum {bfd_symclass_unknown = 0,
157               bfd_symclass_fcommon, /* fortran common symbols */
158               bfd_symclass_global, /* global symbol, what a surprise */
159               bfd_symclass_debugger, /* some debugger symbol */
160               bfd_symclass_undefined /* none known */
161             } symclass;
162
163
164 typedef int symtype;            /* Who knows, yet? */
165
166 /* Symbol cache classifications: (Bfd-Symbol-Flag_FOOBAR) */
167 #define BSF_NO_FLAGS    0x00
168 #define BSF_LOCAL       0x01    /* bfd_symclass_unknown */
169 #define BSF_GLOBAL      0x02    /* bfd_symclass_global */
170 #define BSF_IMPORT      0x04
171 #define BSF_EXPORT      0x08
172 #define BSF_UNDEFINED   0x10    /* bfd_symclass_undefined */
173 #define BSF_FORT_COMM   0x20    /* bfd_symclass_fcommon */
174 #define BSF_DEBUGGING   0x40    /* bfd_symclass_debugger */
175 #define BSF_ABSOLUTE    0x80
176 #define BSF_KEEP        0x10000
177 #define BSF_WARNING     0x20000
178 #define BSF_KEEP_G      0x80000
179 #define BSF_WEAK        0x100000
180 #define BSF_CTOR        0x200000  /* Symbol is a con/destructor */
181 #define BSF_FAKE        0x400000  /* SYmbol doesn't really exist */
182 #define BSF_OLD_COMMON  0x800000  /* Symbol used to be common,
183                                     but now is allocated */
184 /* If symbol is fort_comm, then value is size, and this is the */
185 /* contents */
186 #define BFD_FORT_COMM_DEFAULT_VALUE 0
187
188 /* in some files the type of a symbol sometimes alters its location
189  * in an output file - ie in coff a ISFCN symbol which is also C_EXT 
190  * symbol appears where it was declared and not at the end of a section. 
191  * This bit is set by the target bfd part to convey this information.
192  */
193 #define BSF_NOT_AT_END    0x40000
194
195
196 /* general purpose part of a symbol
197  * target specific parts will be found in libcoff.h, liba.out.h etc
198  */
199 typedef struct symbol_cache_entry 
200 {
201   struct _bfd *the_bfd; /* Just a way to find out host type */
202   CONST char *name;
203   symvalue value;
204   flagword flags;
205   struct sec *section;
206   PTR udata;                    /* Target-specific stuff */
207 } asymbol;
208
209 #define bfd_get_section(x) ((x)->section)
210 #define bfd_get_output_section(x) ((x)->section->output_section)
211 #define bfd_set_section(x,y) ((x)->section) = (y)
212 #define bfd_asymbol_base(x) ((x)->section?((x)->section->vma):0)
213 #define bfd_asymbol_value(x) (bfd_asymbol_base(x) + x->value)
214 #define bfd_asymbol_name(x) ((x)->name)
215
216
217 /* This is a type pun with struct ranlib on purpose! */
218 typedef struct carsym {
219   char *name;
220   file_ptr file_offset;         /* look here to find the file */
221 } carsym;                       /* to make these you call a carsymogen */
222
223 /* Relocation stuff */
224
225 /* Either: sym will point to a symbol and isextern will be 0, *OR*
226    sym will be NULL and isextern will be a symbol type (eg N_TEXT)
227    which means the location should be relocated relative to the
228    segment origin.  This is because we won't necessarily have a symbol
229    which is guaranteed to point to the segment origin. */
230
231 typedef enum bfd_reloc_status {
232   bfd_reloc_ok,
233   bfd_reloc_overflow,
234   bfd_reloc_outofrange,
235   bfd_reloc_continue,
236   bfd_reloc_notsupported,
237   bfd_reloc_other,
238   bfd_reloc_undefined,
239   bfd_reloc_dangerous}
240  bfd_reloc_status_enum_type;
241
242 typedef CONST struct rint 
243
244   unsigned int type;
245   unsigned int rightshift;
246   unsigned int size;
247   unsigned int bitsize;
248   boolean pc_relative;
249   unsigned int bitpos;
250
251   boolean absolute;
252   boolean complain_on_overflow;
253   bfd_reloc_status_enum_type (*special_function)();
254   char *name;
255   boolean partial_inplace;
256   /* Two mask fields,
257      the src_mask is used to select what parts of the read in data are to
258      be used in the relocation sum. Eg, if this was an 8 bit bit of data
259      which we read and relocated, this would be 0x000000ff. When we have
260      relocs which have an addend, such as sun4 extended relocs, the value
261      in the offset part of a relocating field is garbage so we never use
262      it. In this case the mask would be 0x00000000.
263      
264      The dst_mask is what parts of the instruction are replaced into the
265      instruction. In most cases src_mask == dst_mask, except in the above
266      special case, where dst_mask would be 0x000000ff, and src_mask would
267      be 0x00000000.
268      */
269   bfd_word src_mask;            /* What things to take from the source */
270   bfd_word dst_mask;            /* What things to put into the dest */
271
272   /* Does a pc rel offset already have the offset of the jump from the
273      beginnining of the module in place - eg on the sun3, a pcrel
274      instruction always has a negative number in place, containing the
275      displacement from the beginning of the module. 88k bcs has a zero
276      in there, so you have to work out the offset yourself.  */ 
277   boolean pcrel_offset;
278 } reloc_howto_type;
279
280 #define HOWTO(CODE, RT,SIZE,BITSIZE, PCREL, BITPOS, ABS, OVERFLOW, SF, NAME, INPLACE, MASKSRC, MASKDST, PCRELDONE) \
281 {(unsigned)CODE,RT,SIZE,BITSIZE, PCREL,  BITPOS,ABS,OVERFLOW,SF,NAME,INPLACE,MASKSRC,MASKDST,PCRELDONE}
282
283 typedef unsigned char bfd_byte;
284
285 typedef struct reloc_cache_entry 
286 {
287   /* A pointer into the canonicalized table for the symbol */
288   asymbol **sym_ptr_ptr;
289
290   rawdata_offset address;       /* offset in section */
291   bfd_vma addend;               /* addend for relocation value */
292   struct sec *section;   /* if sym is null this is the section */
293   reloc_howto_type *howto;
294
295 } arelent;
296
297 typedef struct relent_chain {
298   arelent relent;
299   struct   relent_chain *next;
300 } arelent_chain;
301   
302 /* Used in generating armaps.  Perhaps just a forward definition would do? */
303 struct orl {                    /* output ranlib */
304   char **name;                  /* symbol name */ 
305   file_ptr pos;                 /* element number or file position */
306   int namidx;                   /* index into string table */
307 };
308
309 \f
310
311 /* Linenumber stuff */
312 typedef struct lineno_cache_entry {
313   unsigned int line_number;     /* Linenumber from start of function*/  
314   union {
315     asymbol *sym;               /* Function name */
316     unsigned long offset;       /* Offset into section */
317   } u;
318 } alent;
319 \f
320 /* object and core file sections */
321
322 /* Section flag definitions */
323 #define SEC_NO_FLAGS 000
324 #define SEC_ALLOC  001
325 #define SEC_LOAD   002
326 #define SEC_RELOC  004
327 #define SEC_BALIGN 010
328 #define SEC_READONLY 020
329 #define SEC_CODE   040
330 #define SEC_DATA   0100
331 #define SEC_ROM    0200
332 #define SEC_CONSTRUCTOR 0400
333 #define SEC_HAS_CONTENTS        (0x200)
334
335 typedef struct sec 
336 {
337   CONST char *name;
338   struct sec *next;
339   flagword flags;
340
341   bfd_vma vma;
342   bfd_size_type size;    
343
344   /* The output_offset is the indent into the output section of
345      this section. If this is the first section to go into
346      an output section, this value will be 0...
347      */
348   bfd_vma output_offset;
349   struct sec *output_section;
350   unsigned int alignment_power; /* eg 4 aligns to 2^4*/
351
352   arelent *relocation;          /* for input files */
353   arelent **orelocation;        /* for output files */
354
355   unsigned reloc_count;
356   file_ptr filepos;             /* File position of section data */
357   file_ptr rel_filepos;         /* File position of relocation info */
358   file_ptr line_filepos;
359   struct user_section *userdata;
360   struct lang_output_section *otheruserdata;
361   int index;                    /* Which section is it 0..nth */
362   alent *lineno; 
363   unsigned int lineno_count;
364
365   /* When a section is being output, this value changes as more
366      linenumbers are written out */
367   file_ptr moving_line_filepos;
368
369   /* what the section number is in the target world */
370   unsigned int target_index;
371
372   PTR used_by_bfd;
373
374   /* If this is a constructor section then here is a list of relents */
375   arelent_chain *constructor_chain;
376 } asection;
377
378 #define align_power(addr, align)        \
379         ( ((addr) + ((1<<(align))-1)) & (-1 << (align)))
380
381
382
383 typedef struct sec *sec_ptr;
384
385 #define bfd_section_name(bfd, ptr) ((ptr)->name)
386 #define bfd_section_size(bfd, ptr) ((ptr)->size)
387 #define bfd_section_vma(bfd, ptr) ((ptr)->vma)
388 #define bfd_section_alignment(bfd, ptr) ((ptr)->alignment_power)
389 #define bfd_get_section_flags(bfd, ptr) ((ptr)->flags)
390 #define bfd_get_section_userdata(bfd, ptr) ((ptr)->userdata)
391
392 #define bfd_set_section_vma(bfd, ptr, val) (((ptr)->vma = (val)), true)
393 #define bfd_set_section_alignment(bfd, ptr, val) (((ptr)->alignment_power = (val)),true)
394 #define bfd_set_section_userdata(bfd, ptr, val) (((ptr)->userdata = (val)),true)
395
396 typedef struct stat stat_type; 
397 \f
398 /** Error handling */
399
400 typedef enum {no_error = 0, system_call_error, invalid_target,
401               wrong_format, invalid_operation, no_memory,
402               no_symbols, no_relocation_info,
403               no_more_archived_files, malformed_archive,
404               symbol_not_found, file_not_recognized,
405               file_ambiguously_recognized, no_contents,
406                 bfd_error_nonrepresentable_section,
407               invalid_error_code} bfd_ec;
408
409 extern bfd_ec bfd_error;
410
411 typedef struct bfd_error_vector {
412   PROTO(void,(* nonrepresentable_section ),(CONST bfd  *CONST abfd,
413                                             CONST char *CONST name));
414 } bfd_error_vector_type;
415
416 PROTO (char *, bfd_errmsg, ());
417 PROTO (void, bfd_perror, (CONST char *message));
418 \f
419
420 typedef enum 
421
422   bfd_print_symbol_name_enum,
423   bfd_print_symbol_type_enum,
424   bfd_print_symbol_all_enum
425 }  bfd_print_symbol_enum_type;
426     
427 \f
428 /* The BFD target structure.
429
430    This structure is how to find out everything BFD knows about a target.
431    It includes things like its byte order, name, what routines to call
432    to do various operations, etc.   
433
434    Every BFD points to a target structure with its "xvec" member.  */
435
436 /* Shortcut for declaring fields which are prototyped function pointers,
437    while avoiding anguish on compilers that don't support protos. */
438
439 #define SDEF(ret, name, arglist) PROTO(ret,(*name),arglist)
440 #define SDEF_FMT(ret, name, arglist) PROTO(ret,(*name[bfd_type_end]),arglist)
441
442 /* These macros are used to dispatch to functions through the bfd_target
443    vector.  They are used in a number of macros further down in bfd.h,
444    and are also used when calling various routines by hand inside the
445    bfd implementation.  The "arglist" argument must be parenthesized;
446    it contains all the arguments to the called function.  */
447
448 #define BFD_SEND(bfd, message, arglist) ((*((bfd)->xvec->message)) arglist)
449 /* For operations which index on the bfd format */
450 #define BFD_SEND_FMT(bfd, message, arglist) \
451                  (((bfd)->xvec->message[(int)((bfd)->format)]) arglist)
452
453 /*  This is the struct which defines the type of BFD this is.  The
454     "xvec" member of the struct bfd itself points here.  Each module
455     that implements access to a different target under BFD, defines
456     one of these.  */
457
458 /* FIXME, these names should be rationalised with the names of the entry points
459    which call them.  Too bad we can't have one macro to define them both! */
460 typedef struct bfd_target
461 {
462   /* identifies the kind of target, eg SunOS4, Ultrix, etc */
463   char *name;
464
465   /* This is bogus.  Anything that can be told from the "flavour"
466      by some user program should be an independent attribute that can
467      be queried instead.  FIXME.  -- gnu@cygnus.com */
468   enum target_flavour_enum {
469     bfd_target_aout_flavour_enum,
470     bfd_target_coff_flavour_enum,
471     bfd_target_ieee_flavour_enum,
472     bfd_target_oasys_flavour_enum,
473     bfd_target_srec_flavour_enum} flavour;
474
475   boolean byteorder_big_p;      /* Order of bytes in data sections */
476   boolean header_byteorder_big_p; /* Order of bytes in header */
477
478   flagword object_flags;        /* these are the ones that may be set */
479   flagword section_flags;       /* ditto */
480
481   char ar_pad_char;             /* filenames in archives padded w/this char */
482   unsigned short ar_max_namelen; /* this could be a char too! */
483
484   /* Byte swapping for data */
485   /* Note that these don't take bfd as first arg.  Certain other handlers
486      could do the same. */
487   SDEF (uint64_type,bfd_getx64, (bfd_byte *));
488   SDEF (void, bfd_putx64, (uint64_type, bfd_byte *));
489   SDEF (uint32_type, bfd_getx32, (bfd_byte *));
490   SDEF (void, bfd_putx32, (unsigned long, bfd_byte *));
491   SDEF (uint16_type, bfd_getx16, (bfd_byte *));
492   SDEF (void, bfd_putx16, (int, bfd_byte *));
493
494   /* Byte swapping for headers */
495   SDEF (uint64_type, bfd_h_getx64, (bfd_byte *));
496   SDEF (void, bfd_h_putx64, (uint64_type, bfd_byte *));
497   SDEF (uint32_type, bfd_h_getx32, (bfd_byte *));
498   SDEF (void, bfd_h_putx32, (unsigned long, bfd_byte *));
499   SDEF (uint16_type, bfd_h_getx16, (bfd_byte *));
500   SDEF (void, bfd_h_putx16, (int, bfd_byte *));
501
502   /* Format-dependent */
503   SDEF_FMT (struct bfd_target *, _bfd_check_format, (bfd *));/* file fmt or 0 */
504   SDEF_FMT (boolean, _bfd_set_format, (bfd *)); /* make it an object file */
505   SDEF_FMT (boolean, _bfd_write_contents, (bfd *)); /* write it out at close */
506
507   /* All these are defined in JUMP_TABLE */
508   /* Core files */
509   SDEF (char *, _core_file_failing_command, (bfd *));
510   SDEF (int, _core_file_failing_signal, (bfd *));
511   SDEF (boolean, _core_file_matches_executable_p, (bfd *, bfd *));
512
513   /* Archives */
514   SDEF (boolean, _bfd_slurp_armap, (bfd *));
515   SDEF (boolean, _bfd_slurp_extended_name_table, (bfd *));
516   SDEF (void, _bfd_truncate_arname, (bfd *, CONST char *, char *));
517   SDEF (boolean, write_armap, (bfd *arch, unsigned int elength,
518                                struct orl *map, int orl_count, int
519                                stridx));
520
521   /* All the standard stuff */
522   SDEF (boolean, _close_and_cleanup, (bfd *)); /* free any allocated data */
523   SDEF (boolean, _bfd_set_section_contents, (bfd *, sec_ptr, PTR,
524                                             file_ptr, bfd_size_type));
525   SDEF (boolean, _bfd_get_section_contents, (bfd *, sec_ptr, PTR, 
526                                              file_ptr, bfd_size_type));
527   SDEF (boolean, _new_section_hook, (bfd *, sec_ptr));
528
529   /* Symbols and relocation */
530   SDEF (unsigned int, _get_symtab_upper_bound, (bfd *));
531   SDEF (unsigned int, _bfd_canonicalize_symtab, (bfd *, asymbol **));
532   SDEF (unsigned int, _get_reloc_upper_bound, (bfd *, sec_ptr));
533   SDEF (unsigned int, _bfd_canonicalize_reloc, (bfd *, sec_ptr, arelent **,
534                                                 asymbol**));
535
536
537   SDEF (asymbol *, _bfd_make_empty_symbol, (bfd *));
538   SDEF (void, _bfd_print_symbol, (bfd *, PTR, asymbol *,
539                                   bfd_print_symbol_enum_type));
540   SDEF (alent *, _get_lineno, (bfd *, asymbol *));
541
542   SDEF (boolean, _bfd_set_arch_mach, (bfd *, enum bfd_architecture,
543                                       unsigned long));
544
545   SDEF (bfd *, openr_next_archived_file, (bfd *arch, bfd *prev));
546   SDEF (boolean, _bfd_find_nearest_line,
547         (bfd *abfd, asection *section, asymbol **symbols,bfd_vma offset,
548          CONST char **file, CONST char **func, unsigned int *line));
549   SDEF (int, _bfd_stat_arch_elt, (bfd *, struct stat *));
550   SDEF (int, _bfd_sizeof_headers, (bfd *, boolean));
551 } bfd_target;
552 \f
553 /* The code that implements targets can initialize a jump table with this
554    macro.  It must name all its routines the same way (a prefix plus
555    the standard routine suffix), or it must #define the routines that
556    are not so named, before calling JUMP_TABLE in the initializer.  */
557
558 /* Semi-portable string concatenation in cpp */
559 #ifndef CAT
560 #ifdef __STDC__
561 #define CAT(a,b) a##b
562 #else
563 #define CAT(a,b) a/**/b
564 #endif
565 #endif
566
567 #define JUMP_TABLE(NAME)\
568 CAT(NAME,_core_file_failing_command),\
569 CAT(NAME,_core_file_failing_signal),\
570 CAT(NAME,_core_file_matches_executable_p),\
571 CAT(NAME,_slurp_armap),\
572 CAT(NAME,_slurp_extended_name_table),\
573 CAT(NAME,_truncate_arname),\
574 CAT(NAME,_write_armap),\
575 CAT(NAME,_close_and_cleanup),   \
576 CAT(NAME,_set_section_contents),\
577 CAT(NAME,_get_section_contents),\
578 CAT(NAME,_new_section_hook),\
579 CAT(NAME,_get_symtab_upper_bound),\
580 CAT(NAME,_get_symtab),\
581 CAT(NAME,_get_reloc_upper_bound),\
582 CAT(NAME,_canonicalize_reloc),\
583 CAT(NAME,_make_empty_symbol),\
584 CAT(NAME,_print_symbol),\
585 CAT(NAME,_get_lineno),\
586 CAT(NAME,_set_arch_mach),\
587 CAT(NAME,_openr_next_archived_file),\
588 CAT(NAME,_find_nearest_line),\
589 CAT(NAME,_generic_stat_arch_elt),\
590 CAT(NAME,_sizeof_headers)
591 \f
592 /* User program access to BFD facilities */
593
594 extern CONST short _bfd_host_big_endian;
595 #define HOST_BYTE_ORDER_BIG_P   (*(char *)&_bfd_host_big_endian)
596
597 /* The bfd itself */
598
599 /* Cast from const char * to char * so that caller can assign to
600    a char * without a warning.  */
601 #define bfd_get_filename(abfd) ((char *) (abfd)->filename)
602 #define bfd_get_format(abfd) ((abfd)->format)
603 #define bfd_get_target(abfd) ((abfd)->xvec->name)
604 #define bfd_get_file_flags(abfd) ((abfd)->flags)
605 #define bfd_applicable_file_flags(abfd) ((abfd)->xvec->object_flags)
606 #define bfd_applicable_section_flags(abfd) ((abfd)->xvec->section_flags)
607 #define bfd_my_archive(abfd) ((abfd)->my_archive);
608 #define bfd_has_map(abfd) ((abfd)->has_armap)
609 #define bfd_header_twiddle_required(abfd) \
610         ((((abfd)->xvec->header_byteorder_big_p)                \
611           != (boolean)HOST_BYTE_ORDER_BIG_P) ? true:false)
612
613 #define bfd_valid_reloc_types(abfd) ((abfd)->xvec->valid_reloc_types)
614 #define bfd_usrdata(abfd) ((abfd)->usrdata)
615
616 #define bfd_get_start_address(abfd) ((abfd)->start_address)
617 #define bfd_get_symcount(abfd) ((abfd)->symcount)
618 #define bfd_get_outsymbols(abfd) ((abfd)->outsymbols)
619 #define bfd_count_sections(abfd) ((abfd)->section_count)
620 #define bfd_get_architecture(abfd) ((abfd)->obj_arch)
621 #define bfd_get_machine(abfd) ((abfd)->obj_machine)
622
623 /* Finally!  The BFD struct itself.  This contains the major data about
624    the file, and contains pointers to the rest of the data.
625
626    To avoid dragging too many header files into every file that
627    includes bfd.h, IOSTREAM has been declared as a "char *", and MTIME
628    as a "long".  Their correct types, to which they are cast when used,
629    are "FILE *" and "time_t".  If these had been declared as structs
630    rather than typedefs, we wouldn't have this problem.  */
631
632 struct _bfd 
633 {
634
635   CONST char *filename;         /* could be null; filename user opened with */
636   bfd_target *xvec;             /* operation jump table */
637   char *iostream;               /* stdio FILE *, unless an archive element */
638
639   boolean cacheable;            /* iostream can be closed if desired */
640   struct _bfd *lru_prev;        /* Used for file caching */
641   struct _bfd *lru_next;        /* Used for file caching */
642   file_ptr where;               /* Where the file was when closed */
643   boolean opened_once;
644   boolean mtime_set;            /* Flag indicating mtime is available */
645   long mtime;                   /* File modified time */
646   int ifd;                      /* for output files, channel we locked. */
647   bfd_format format;
648   enum bfd_direction {no_direction = 0,
649                         read_direction = 1,
650                         write_direction = 2,
651                         both_direction = 3} direction;
652
653   flagword flags;               /* format_specific */
654   /* 
655     Currently my_archive is tested before adding origin to anything. I
656     believe that this can become always an add of origin, with origin set
657     to 0 for non archive files
658     */
659
660   file_ptr origin;              /* for archive contents */
661   boolean output_has_begun;     /* cf bfd_set_section_size */
662   asection *sections;           /* Pointer to linked list of sections */
663   unsigned int section_count;   /* The number of sections */
664
665   /* Some object file stuff */
666   bfd_vma start_address;        /* for object files only, of course */
667   unsigned int symcount;        /* used for input and output */
668   asymbol **outsymbols;         /* symtab for output bfd */
669   enum bfd_architecture obj_arch; /* Architecture of object machine, eg m68k */
670   unsigned long obj_machine;    /* Particular machine within arch, e.g. 68010 */
671
672   /* Archive stuff.  strictly speaking we don't need all three bfd* vars,
673      but doing so would allow recursive archives! */
674   PTR arelt_data;               /* needed if this came from an archive */
675   struct _bfd *my_archive;      /* if this is an archive element */
676   struct _bfd *next;            /* output chain pointer */
677   struct _bfd *archive_head;    /* for output archive */
678   boolean has_armap;            /* if an arch; has it an armap? */
679   
680   PTR tdata;                    /* target-specific storage */
681   PTR usrdata;                  /* application-specific storage */
682
683   /* Should probably be enabled here always, so that library may be changed
684      to switch this on and off, while user code may remain unchanged */
685 #ifdef BFD_LOCKS
686   struct flock *lock;
687   char *actual_name;            /* for output files, name given to open()  */
688 #endif
689
690   /* Where all the allocated stuff under this BFD goes */
691   struct obstack memory;
692 };
693 \f
694 /* The various callable routines */
695 PROTO(bfd_size_type, bfd_alloc_size,(bfd *abfd));
696 PROTO (char *, bfd_printable_arch_mach,(enum bfd_architecture, unsigned long));
697 PROTO (char *, bfd_format_string, (bfd_format format));
698
699 PROTO (char**, bfd_target_list, ());
700 PROTO (bfd *, bfd_openr, (CONST char *filename, CONST char *target));
701 PROTO (bfd *, bfd_fdopenr, (CONST char *filename, CONST char *target, int fd));
702 PROTO (bfd *, bfd_openw, (CONST char *filename, CONST char *target));
703 PROTO (bfd *, bfd_create, (CONST char *filename, CONST bfd *abfd));
704 PROTO (boolean, bfd_close, (bfd *abfd));
705 PROTO (long, bfd_get_mtime, (bfd *abfd));
706 PROTO (bfd *, bfd_openr_next_archived_file, (bfd *obfd, bfd *last_file));
707 PROTO (boolean, bfd_set_archive_head, (bfd *output_archive, bfd *new_head));
708 PROTO (boolean, bfd_check_format, (bfd *abfd, bfd_format format));
709 PROTO (boolean, bfd_set_format, (bfd *abfd, bfd_format format));
710 PROTO (char *, bfd_core_file_failing_command, (bfd *abfd));
711 PROTO (int, bfd_core_file_failing_signal, (bfd *abfd));
712 PROTO (boolean, core_file_matches_executable_p, (bfd *core_bfd, bfd *exec_bfd));
713 PROTO (sec_ptr, bfd_get_section_by_name, (bfd *abfd, CONST char *name));
714 PROTO (void, bfd_map_over_sections, (bfd *abfd, void (*operation)(),
715                                      PTR user_storage));
716 PROTO (sec_ptr, bfd_make_section, (bfd *abfd, CONST char *CONST name));
717 PROTO (boolean, bfd_set_section_flags, (bfd *abfd, sec_ptr section,
718                                         flagword flags));
719 PROTO (boolean, bfd_set_file_flags, (bfd *abfd, flagword flags));
720 PROTO (boolean, bfd_arch_compatible,  (bfd *abfd, bfd *bbfd,
721                                     enum bfd_architecture *res_arch,
722                                     unsigned long *res_machine));
723
724 PROTO (boolean, bfd_set_section_size, (bfd *abfd, sec_ptr ptr,
725                                        unsigned long val));
726 PROTO (boolean, bfd_get_section_contents, (bfd *abfd, sec_ptr section,
727                                            PTR location,
728                                            file_ptr offset, bfd_size_type count));
729 PROTO (boolean, bfd_set_section_contents, (bfd *abfd, sec_ptr section,
730                                            PTR location,
731                                            file_ptr offset, bfd_size_type count));
732
733 PROTO (unsigned long, bfd_get_next_mapent, (bfd *abfd, symindex prev, carsym **entry));
734 PROTO (bfd *, bfd_get_elt_at_index, (bfd *abfd, int index));
735 PROTO (boolean, bfd_set_symtab, (bfd *abfd, asymbol **location,
736                                  unsigned int symcount));
737 PROTO (unsigned int, get_reloc_upper_bound, (bfd *abfd, sec_ptr asect));
738 PROTO (unsigned int, bfd_canonicalize_reloc, (bfd *abfd, sec_ptr asect,
739                                               arelent **location,
740                                               asymbol **canon));
741 PROTO (void, bfd_set_reloc, (bfd *abfd, sec_ptr asect, arelent **location,
742                              unsigned int count));
743 PROTO (boolean, bfd_set_start_address, (bfd *,bfd_vma));
744
745 PROTO (void, bfd_print_symbol_vandf, (PTR, asymbol *));
746 PROTO (bfd_reloc_status_enum_type, bfd_perform_relocation,
747        (bfd *, arelent*, PTR, asection *, bfd*));
748
749 PROTO (bfd_vma, bfd_log2, (bfd_vma));
750 #define bfd_symbol_same_target(abfd, symbol) \
751 ( ( ((symbol)->the_bfd->xvec) == (abfd)->xvec) ? true:false)
752
753 PROTO(boolean, bfd_scan_arch_mach,(CONST char *, enum bfd_architecture *,
754                                    unsigned long *));
755
756 /* For speed we turn calls to these interface routines directly into
757    jumps through the transfer vector.  This makes error-checking somewhat
758    confusing; the user basically has to read the documentation since there
759    are no longer prototypes, only declarations in the xfer vector (which
760    should be enough for some compilers).
761
762    To bad the preprocessor is too dumb to allow us to clean this up with
763    a macro. */
764
765 #define bfd_set_arch_mach(abfd, arch, mach) \
766      BFD_SEND (abfd, _bfd_set_arch_mach, (abfd, arch, mach))
767
768 #define bfd_sizeof_headers(abfd, reloc) \
769      BFD_SEND (abfd, _bfd_sizeof_headers, (abfd, reloc))
770
771 #define bfd_symbol_value(abfd, idx) \
772      BFD_SEND (abfd, _bfd_symbol_value, (abfd, idx))
773
774 #define bfd_symbol_name(abfd, idx) \
775      BFD_SEND (abfd, _bfd_symbol_name, (abfd, idx))
776
777
778 #define bfd_get_first_symbol(abfd) \
779      BFD_SEND (abfd, _bfd_get_first_symbol, (abfd))
780
781 #define bfd_get_next_symbol(abfd, oidx) \
782      BFD_SEND (abfd, _bfd_get_next_symbol, (abfd, oidx))
783
784 #define bfd_classify_symbol(abfd, idx) \
785      BFD_SEND (abfd, _bfd_classify_symbol, (abfd, idx))
786
787 #define bfd_symbol_hasclass(abfd, idx, class) \
788      BFD_SEND (abfd, _bfd_symbol_hasclass, (abfd, idx, class))
789
790 #define get_symtab_upper_bound(abfd) \
791      BFD_SEND (abfd, _get_symtab_upper_bound, (abfd))
792
793 #define bfd_canonicalize_symtab(abfd, location) \
794      BFD_SEND (abfd, _bfd_canonicalize_symtab, (abfd, location))
795
796
797 #define bfd_make_empty_symbol(abfd) \
798      BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
799
800 #define bfd_print_symbol(abfd, file, symbol, how) \
801      BFD_SEND (abfd, _bfd_print_symbol, (abfd, file, symbol, how))
802
803 #define bfd_get_lineno(abfd, symbol) \
804      BFD_SEND (abfd, _get_lineno, (abfd, symbol))
805
806 #define bfd_stat_arch_elt(abfd, buf) \
807      BFD_SEND(abfd, _bfd_stat_arch_elt, (abfd, buf))
808
809 #define bfd_find_nearest_line(abfd, section, symbols, offset,filename_ptr, func, line_ptr) \
810 BFD_SEND (abfd, _bfd_find_nearest_line, (abfd, section,symbols, offset, filename_ptr, func, line_ptr))
811
812 \f
813 /* Some byte-swapping i/o operations */
814 #define LONGLONG_SIZE 8
815 #define LONG_SIZE 4
816 #define SHORT_SIZE 2
817 #define BYTE_SIZE 1
818 #define bfd_put_8(abfd, val, ptr)    (*((char *)ptr) = (char)val)
819 #define bfd_get_8(abfd, ptr)           (*((char *)ptr))
820
821 #define bfd_put_32(abfd, val, ptr)    BFD_SEND(abfd, bfd_putx32,   (val,ptr))
822 #define bfd_get_32(abfd, ptr)          BFD_SEND(abfd, bfd_getx32,   (ptr))
823
824 #define bfd_put_64(abfd, val, ptr) BFD_SEND(abfd, bfd_putx64,   (val,ptr))
825 #define bfd_get_64(abfd, ptr)   BFD_SEND(abfd, bfd_getx64,   (ptr))
826
827 #define bfd_put_16(abfd, val, ptr)   BFD_SEND(abfd, bfd_putx16,  (val,ptr))
828 #define bfd_get_16(abfd, ptr)          BFD_SEND(abfd, bfd_getx16,  (ptr))
829
830 #define bfd_h_put_8(abfd, val, ptr)     bfd_put_8 (abfd, val, ptr)
831 #define bfd_h_get_8(abfd, ptr)  bfd_get_8 (abfd, ptr)
832
833 #define bfd_h_put_32(abfd, val, ptr)  BFD_SEND(abfd, bfd_h_putx32, (val, (bfd_byte *) ptr))
834 #define bfd_h_get_32(abfd, ptr)       BFD_SEND(abfd, bfd_h_getx32, ((bfd_byte *) ptr))
835
836 #define bfd_h_put_64(abfd, val, ptr)  BFD_SEND(abfd, bfd_h_putx64, (val, (bfd_byte *) ptr))
837 #define bfd_h_get_64(abfd, ptr)       BFD_SEND(abfd, bfd_h_getx64, ((bfd_byte *) ptr))
838
839 #define bfd_h_put_16(abfd, val, ptr) BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
840 #define bfd_h_get_16(abfd, ptr)      BFD_SEND(abfd, bfd_h_getx16,(ptr))
841
842 /* General purpose one fits all.  The do { } while (0) makes a single 
843    statement out of it, for use in things like nested if-statements.
844    
845    The idea is to create your external ref as a byte array of the
846    right size eg:
847    char foo[4];
848    char bar[2];
849    then you may do things like:
850    bfd_h_put_x(abfd, 1, &foo);
851    and bfd_h_get_x(abfd,& bar);
852 */
853
854 #define bfd_h_put_x(abfd, val, ptr) \
855   do {  \
856        if (sizeof((ptr)) == LONGLONG_SIZE) \
857                 bfd_h_put_64  (abfd, val, (ptr));\
858        if (sizeof((ptr)) == LONG_SIZE) \
859                 bfd_h_put_32  (abfd, val, (ptr));\
860   else if (sizeof((ptr)) == SHORT_SIZE) \
861                 bfd_h_put_16 (abfd, val, (ptr));\
862   else if (sizeof((ptr)) == BYTE_SIZE) \
863                 bfd_h_put_8  (abfd, val, (ptr));\
864   else abort(); } while (0)
865
866 #define bfd_h_get_x(abfd, ptr) \
867   ((sizeof((ptr))==LONGLONG_SIZE) ? bfd_h_get_64 (abfd, &(ptr[0])):\
868   (sizeof((ptr))==LONG_SIZE) ?  bfd_h_get_32 (abfd, &(ptr[0])):\
869    (sizeof((ptr))==SHORT_SIZE) ? bfd_h_get_16(abfd, &(ptr[0])):\
870     bfd_h_get_8 (abfd, &(ptr[0])))
871 #ifdef GNU960
872
873 #define BFD_COFF_FORMAT bfd_target_coff_flavour_enum
874 #define BFD_BOUT_FORMAT bfd_target_aout_flavour_enum
875
876 /*
877  * Return nonzero iff specified bfd is for big-endian target
878  */
879 #define BFD_BIG_ENDIAN_FILE_P(abfd)     \
880                         ((abfd)->xvec->header_byteorder_big_p == true)
881
882 /*
883  * Return nonzero iff specified bfd is for coff target
884  */
885 #define BFD_COFF_FILE_P(abfd)   ((abfd)->xvec->flavour == BFD_COFF_FORMAT)
886
887 /*
888  * The names of the only targets the GNU/960 release cares about
889  */
890 #define BFD_BIG_COFF_TARG       "coff-Intel-big"
891 #define BFD_LITTLE_COFF_TARG    "coff-Intel-little"
892 #define BFD_BIG_BOUT_TARG       "b.out.big"
893 #define BFD_LITTLE_BOUT_TARG    "b.out.little"
894
895 extern PROTO (char *, bfd_make_targ_name,( enum target_flavour_enum format, int bigendian));
896
897 #endif /* GNU960 */
898
899
900
901 #endif /* __BFD_H_SEEN__ */