1 /* Main header file for the bfd library -- portable access to object files.
2 
3    Copyright (C) 1990-2021 Free Software Foundation, Inc.
4 
5    Contributed by Cygnus Support.
6 
7    This file is part of BFD, the Binary File Descriptor library.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
22 
23 #ifndef __BFD_H_SEEN__
24 #define __BFD_H_SEEN__
25 
26 /* PR 14072: Ensure that config.h is included first.  */
27 #if !defined PACKAGE && !defined PACKAGE_VERSION
28 #error config.h must be included before this header
29 #endif
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #include "ansidecl.h"
36 #include "symcat.h"
37 #include <stdint.h>
38 #include <stdbool.h>
39 #include "diagnostics.h"
40 #include <stdarg.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 
44 #if defined (__STDC__) || defined (ALMOST_STDC) || defined (HAVE_STRINGIZE)
45 #ifndef SABER
46 /* This hack is to avoid a problem with some strict ANSI C preprocessors.
47    The problem is, "32_" is not a valid preprocessing token, and we don't
48    want extra underscores (e.g., "nlm_32_").  The XCONCAT2 macro will
49    cause the inner CONCAT2 macros to be evaluated first, producing
50    still-valid pp-tokens.  Then the final concatenation can be done.  */
51 #undef CONCAT4
52 #define CONCAT4(a,b,c,d) XCONCAT2(CONCAT2(a,b),CONCAT2(c,d))
53 #endif
54 #endif
55 
56 /* This is a utility macro to handle the situation where the code
57    wants to place a constant string into the code, followed by a
58    comma and then the length of the string.  Doing this by hand
59    is error prone, so using this macro is safer.  */
60 #define STRING_COMMA_LEN(STR) (STR), (sizeof (STR) - 1)
61 
62 #define BFD_SUPPORTS_PLUGINS @supports_plugins@
63 
64 /* The word size used by BFD on the host.  This may be 64 with a 32
65    bit target if the host is 64 bit, or if other 64 bit targets have
66    been selected with --enable-targets, or if --enable-64-bit-bfd.  */
67 #define BFD_ARCH_SIZE @wordsize@
68 
69 /* The word size of the default bfd target.  */
70 #define BFD_DEFAULT_TARGET_SIZE @bfd_default_target_size@
71 
72 #define BFD_HOST_64BIT_LONG @BFD_HOST_64BIT_LONG@
73 #define BFD_HOST_64BIT_LONG_LONG @BFD_HOST_64BIT_LONG_LONG@
74 #if @BFD_HOST_64_BIT_DEFINED@
75 #define BFD_HOST_64_BIT @BFD_HOST_64_BIT@
76 #define BFD_HOST_U_64_BIT @BFD_HOST_U_64_BIT@
77 typedef BFD_HOST_64_BIT bfd_int64_t;
78 typedef BFD_HOST_U_64_BIT bfd_uint64_t;
79 #endif
80 
81 #include <inttypes.h>
82 
83 #if BFD_ARCH_SIZE >= 64
84 #define BFD64
85 #endif
86 
87 #ifndef INLINE
88 #if __GNUC__ >= 2
89 #define INLINE __inline__
90 #else
91 #define INLINE
92 #endif
93 #endif
94 
95 /* Declaring a type wide enough to hold a host long and a host pointer.  */
96 #define BFD_HOSTPTR_T @BFD_HOSTPTR_T@
97 typedef BFD_HOSTPTR_T bfd_hostptr_t;
98 
99 /* Forward declaration.  */
100 typedef struct bfd bfd;
101 
102 /* Boolean type used in bfd.
103    General rule: Functions which are bfd_boolean return TRUE on
104    success and FALSE on failure (unless they're a predicate).  */
105 
106 #ifdef POISON_BFD_BOOLEAN
107 # pragma GCC poison bfd_boolean
108 #else
109 # define bfd_boolean bool
110 # undef FALSE
111 # undef TRUE
112 # define FALSE 0
113 # define TRUE 1
114 #endif
115 
116 #ifdef BFD64
117 
118 #ifndef BFD_HOST_64_BIT
119  #error No 64 bit integer type available
120 #endif /* ! defined (BFD_HOST_64_BIT) */
121 
122 typedef BFD_HOST_U_64_BIT bfd_vma;
123 typedef BFD_HOST_64_BIT bfd_signed_vma;
124 typedef BFD_HOST_U_64_BIT bfd_size_type;
125 typedef BFD_HOST_U_64_BIT symvalue;
126 
127 #if BFD_HOST_64BIT_LONG
128 #define BFD_VMA_FMT "l"
129 #elif defined (__MSVCRT__)
130 #define BFD_VMA_FMT "I64"
131 #else
132 #define BFD_VMA_FMT "ll"
133 #endif
134 
135 #ifndef fprintf_vma
136 #define sprintf_vma(s,x) sprintf (s, "%016" BFD_VMA_FMT "x", x)
137 #define fprintf_vma(f,x) fprintf (f, "%016" BFD_VMA_FMT "x", x)
138 #endif
139 
140 #else /* not BFD64  */
141 
142 /* Represent a target address.  Also used as a generic unsigned type
143    which is guaranteed to be big enough to hold any arithmetic types
144    we need to deal with.  */
145 typedef unsigned long bfd_vma;
146 
147 /* A generic signed type which is guaranteed to be big enough to hold any
148    arithmetic types we need to deal with.  Can be assumed to be compatible
149    with bfd_vma in the same way that signed and unsigned ints are compatible
150    (as parameters, in assignment, etc).  */
151 typedef long bfd_signed_vma;
152 
153 typedef unsigned long symvalue;
154 typedef unsigned long bfd_size_type;
155 
156 /* Print a bfd_vma x on stream s.  */
157 #define BFD_VMA_FMT "l"
158 #define fprintf_vma(s,x) fprintf (s, "%08" BFD_VMA_FMT "x", x)
159 #define sprintf_vma(s,x) sprintf (s, "%08" BFD_VMA_FMT "x", x)
160 
161 #endif /* not BFD64  */
162 
163 #define HALF_BFD_SIZE_TYPE \
164   (((bfd_size_type) 1) << (8 * sizeof (bfd_size_type) / 2))
165 
166 #ifndef BFD_HOST_64_BIT
167 /* Fall back on a 32 bit type.  The idea is to make these types always
168    available for function return types, but in the case that
169    BFD_HOST_64_BIT is undefined such a function should abort or
170    otherwise signal an error.  */
171 typedef bfd_signed_vma bfd_int64_t;
172 typedef bfd_vma bfd_uint64_t;
173 #endif
174 
175 /* An offset into a file.  BFD always uses the largest possible offset
176    based on the build time availability of fseek, fseeko, or fseeko64.  */
177 typedef @bfd_file_ptr@ file_ptr;
178 typedef unsigned @bfd_file_ptr@ ufile_ptr;
179 
180 extern void bfd_sprintf_vma (bfd *, char *, bfd_vma);
181 extern void bfd_fprintf_vma (bfd *, void *, bfd_vma);
182 
183 #define printf_vma(x) fprintf_vma(stdout,x)
184 #define bfd_printf_vma(abfd,x) bfd_fprintf_vma (abfd,stdout,x)
185 
186 typedef unsigned int flagword;	/* 32 bits of flags */
187 typedef unsigned char bfd_byte;
188 
189 /* File formats.  */
190 
191 typedef enum bfd_format
192 {
193   bfd_unknown = 0,	/* File format is unknown.  */
194   bfd_object,		/* Linker/assembler/compiler output.  */
195   bfd_archive,		/* Object archive file.  */
196   bfd_core,		/* Core dump.  */
197   bfd_type_end		/* Marks the end; don't use it!  */
198 }
199 bfd_format;
200 
201 /* Symbols and relocation.  */
202 
203 /* A count of carsyms (canonical archive symbols).  */
204 typedef unsigned long symindex;
205 
206 #define BFD_NO_MORE_SYMBOLS ((symindex) ~0)
207 
208 /* A canonical archive symbol.  */
209 /* This is a type pun with struct ranlib on purpose!  */
210 typedef struct carsym
211 {
212   const char *name;
213   file_ptr file_offset;	/* Look here to find the file.  */
214 }
215 carsym;			/* To make these you call a carsymogen.  */
216 
217 /* Used in generating armaps (archive tables of contents).
218    Perhaps just a forward definition would do?  */
219 struct orl		/* Output ranlib.  */
220 {
221   char **name;		/* Symbol name.  */
222   union
223   {
224     file_ptr pos;
225     bfd *abfd;
226   } u;			/* bfd* or file position.  */
227   int namidx;		/* Index into string table.  */
228 };
229 
230 /* Linenumber stuff.  */
231 typedef struct lineno_cache_entry
232 {
233   unsigned int line_number;	/* Linenumber from start of function.  */
234   union
235   {
236     struct bfd_symbol *sym;	/* Function name.  */
237     bfd_vma offset;		/* Offset into section.  */
238   } u;
239 }
240 alent;
241 
242 /* Object and core file sections.  */
243 typedef struct bfd_section *sec_ptr;
244 
245 #define	align_power(addr, align)	\
246   (((addr) + ((bfd_vma) 1 << (align)) - 1) & (-((bfd_vma) 1 << (align))))
247 
248 /* Align an address upward to a boundary, expressed as a number of bytes.
249    E.g. align to an 8-byte boundary with argument of 8.  Take care never
250    to wrap around if the address is within boundary-1 of the end of the
251    address space.  */
252 #define BFD_ALIGN(this, boundary)					  \
253   ((((bfd_vma) (this) + (boundary) - 1) >= (bfd_vma) (this))		  \
254    ? (((bfd_vma) (this) + ((boundary) - 1)) & ~ (bfd_vma) ((boundary)-1)) \
255    : ~ (bfd_vma) 0)
256 
257 typedef enum bfd_print_symbol
258 {
259   bfd_print_symbol_name,
260   bfd_print_symbol_more,
261   bfd_print_symbol_all
262 } bfd_print_symbol_type;
263 
264 /* Information about a symbol that nm needs.  */
265 
266 typedef struct _symbol_info
267 {
268   symvalue value;
269   char type;
270   const char *name;		/* Symbol name.  */
271   unsigned char stab_type;	/* Stab type.  */
272   char stab_other;		/* Stab other.  */
273   short stab_desc;		/* Stab desc.  */
274   const char *stab_name;	/* String for stab type.  */
275 } symbol_info;
276 
277 /* Get the name of a stabs type code.  */
278 
279 extern const char *bfd_get_stab_name (int);
280 
281 /* Hash table routines.  There is no way to free up a hash table.  */
282 
283 /* An element in the hash table.  Most uses will actually use a larger
284    structure, and an instance of this will be the first field.  */
285 
286 struct bfd_hash_entry
287 {
288   /* Next entry for this hash code.  */
289   struct bfd_hash_entry *next;
290   /* String being hashed.  */
291   const char *string;
292   /* Hash code.  This is the full hash code, not the index into the
293      table.  */
294   unsigned long hash;
295 };
296 
297 /* A hash table.  */
298 
299 struct bfd_hash_table
300 {
301   /* The hash array.  */
302   struct bfd_hash_entry **table;
303   /* A function used to create new elements in the hash table.  The
304      first entry is itself a pointer to an element.  When this
305      function is first invoked, this pointer will be NULL.  However,
306      having the pointer permits a hierarchy of method functions to be
307      built each of which calls the function in the superclass.  Thus
308      each function should be written to allocate a new block of memory
309      only if the argument is NULL.  */
310   struct bfd_hash_entry *(*newfunc)
311     (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
312   /* An objalloc for this hash table.  This is a struct objalloc *,
313      but we use void * to avoid requiring the inclusion of objalloc.h.  */
314   void *memory;
315   /* The number of slots in the hash table.  */
316   unsigned int size;
317   /* The number of entries in the hash table.  */
318   unsigned int count;
319   /* The size of elements.  */
320   unsigned int entsize;
321   /* If non-zero, don't grow the hash table.  */
322   unsigned int frozen:1;
323 };
324 
325 /* Initialize a hash table.  */
326 extern bool bfd_hash_table_init
327   (struct bfd_hash_table *,
328    struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
329 			       struct bfd_hash_table *,
330 			       const char *),
331    unsigned int);
332 
333 /* Initialize a hash table specifying a size.  */
334 extern bool bfd_hash_table_init_n
335   (struct bfd_hash_table *,
336    struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
337 			       struct bfd_hash_table *,
338 			       const char *),
339    unsigned int, unsigned int);
340 
341 /* Free up a hash table.  */
342 extern void bfd_hash_table_free
343   (struct bfd_hash_table *);
344 
345 /* Look up a string in a hash table.  If CREATE is TRUE, a new entry
346    will be created for this string if one does not already exist.  The
347    COPY argument must be TRUE if this routine should copy the string
348    into newly allocated memory when adding an entry.  */
349 extern struct bfd_hash_entry *bfd_hash_lookup
350   (struct bfd_hash_table *, const char *, bool create, bool copy);
351 
352 /* Insert an entry in a hash table.  */
353 extern struct bfd_hash_entry *bfd_hash_insert
354   (struct bfd_hash_table *, const char *, unsigned long);
355 
356 /* Rename an entry in a hash table.  */
357 extern void bfd_hash_rename
358   (struct bfd_hash_table *, const char *, struct bfd_hash_entry *);
359 
360 /* Replace an entry in a hash table.  */
361 extern void bfd_hash_replace
362   (struct bfd_hash_table *, struct bfd_hash_entry *old,
363    struct bfd_hash_entry *nw);
364 
365 /* Base method for creating a hash table entry.  */
366 extern struct bfd_hash_entry *bfd_hash_newfunc
367   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
368 
369 /* Grab some space for a hash table entry.  */
370 extern void *bfd_hash_allocate
371   (struct bfd_hash_table *, unsigned int);
372 
373 /* Traverse a hash table in a random order, calling a function on each
374    element.  If the function returns FALSE, the traversal stops.  The
375    INFO argument is passed to the function.  */
376 extern void bfd_hash_traverse
377   (struct bfd_hash_table *,
378    bool (*) (struct bfd_hash_entry *, void *),
379    void *info);
380 
381 /* Allows the default size of a hash table to be configured. New hash
382    tables allocated using bfd_hash_table_init will be created with
383    this size.  */
384 extern unsigned long bfd_hash_set_default_size (unsigned long);
385 
386 /* Types of compressed DWARF debug sections.  We currently support
387    zlib.  */
388 enum compressed_debug_section_type
389 {
390   COMPRESS_DEBUG_NONE = 0,
391   COMPRESS_DEBUG = 1 << 0,
392   COMPRESS_DEBUG_GNU_ZLIB = COMPRESS_DEBUG | 1 << 1,
393   COMPRESS_DEBUG_GABI_ZLIB = COMPRESS_DEBUG | 1 << 2
394 };
395 
396 /* This structure is used to keep track of stabs in sections
397    information while linking.  */
398 
399 struct stab_info
400 {
401   /* A hash table used to hold stabs strings.  */
402   struct bfd_strtab_hash *strings;
403   /* The header file hash table.  */
404   struct bfd_hash_table includes;
405   /* The first .stabstr section.  */
406   struct bfd_section *stabstr;
407 };
408 
409 #define COFF_SWAP_TABLE (void *) &bfd_coff_std_swap_table
410 
411 /* User program access to BFD facilities.  */
412 
413 /* Direct I/O routines, for programs which know more about the object
414    file than BFD does.  Use higher level routines if possible.  */
415 
416 extern bfd_size_type bfd_bread (void *, bfd_size_type, bfd *);
417 extern bfd_size_type bfd_bwrite (const void *, bfd_size_type, bfd *);
418 extern int bfd_seek (bfd *, file_ptr, int);
419 extern file_ptr bfd_tell (bfd *);
420 extern int bfd_flush (bfd *);
421 extern int bfd_stat (bfd *, struct stat *);
422 
423 /* Deprecated old routines.  */
424 #if __GNUC__
425 #define bfd_read(BUF, ELTSIZE, NITEMS, ABFD)				\
426   (_bfd_warn_deprecated ("bfd_read", __FILE__, __LINE__, __FUNCTION__),	\
427    bfd_bread ((BUF), (ELTSIZE) * (NITEMS), (ABFD)))
428 #define bfd_write(BUF, ELTSIZE, NITEMS, ABFD)				\
429   (_bfd_warn_deprecated ("bfd_write", __FILE__, __LINE__, __FUNCTION__), \
430    bfd_bwrite ((BUF), (ELTSIZE) * (NITEMS), (ABFD)))
431 #else
432 #define bfd_read(BUF, ELTSIZE, NITEMS, ABFD)				\
433   (_bfd_warn_deprecated ("bfd_read", (const char *) 0, 0, (const char *) 0), \
434    bfd_bread ((BUF), (ELTSIZE) * (NITEMS), (ABFD)))
435 #define bfd_write(BUF, ELTSIZE, NITEMS, ABFD)				\
436   (_bfd_warn_deprecated ("bfd_write", (const char *) 0, 0, (const char *) 0),\
437    bfd_bwrite ((BUF), (ELTSIZE) * (NITEMS), (ABFD)))
438 #endif
439 extern void _bfd_warn_deprecated (const char *, const char *, int, const char *);
440 
441 extern bool bfd_cache_close
442   (bfd *abfd);
443 /* NB: This declaration should match the autogenerated one in libbfd.h.  */
444 
445 extern bool bfd_cache_close_all (void);
446 
447 extern bool bfd_record_phdr
448   (bfd *, unsigned long, bool, flagword, bool, bfd_vma,
449    bool, bool, unsigned int, struct bfd_section **);
450 
451 /* Byte swapping routines.  */
452 
453 bfd_uint64_t bfd_getb64 (const void *);
454 bfd_uint64_t bfd_getl64 (const void *);
455 bfd_int64_t bfd_getb_signed_64 (const void *);
456 bfd_int64_t bfd_getl_signed_64 (const void *);
457 bfd_vma bfd_getb32 (const void *);
458 bfd_vma bfd_getl32 (const void *);
459 bfd_signed_vma bfd_getb_signed_32 (const void *);
460 bfd_signed_vma bfd_getl_signed_32 (const void *);
461 bfd_vma bfd_getb16 (const void *);
462 bfd_vma bfd_getl16 (const void *);
463 bfd_signed_vma bfd_getb_signed_16 (const void *);
464 bfd_signed_vma bfd_getl_signed_16 (const void *);
465 void bfd_putb64 (bfd_uint64_t, void *);
466 void bfd_putl64 (bfd_uint64_t, void *);
467 void bfd_putb32 (bfd_vma, void *);
468 void bfd_putl32 (bfd_vma, void *);
469 void bfd_putb24 (bfd_vma, void *);
470 void bfd_putl24 (bfd_vma, void *);
471 void bfd_putb16 (bfd_vma, void *);
472 void bfd_putl16 (bfd_vma, void *);
473 
474 /* Byte swapping routines which take size and endiannes as arguments.  */
475 
476 bfd_uint64_t bfd_get_bits (const void *, int, bool);
477 void bfd_put_bits (bfd_uint64_t, void *, int, bool);
478 
479 
480 /* mmap hacks */
481 
482 struct _bfd_window_internal;
483 typedef struct _bfd_window_internal bfd_window_internal;
484 
485 typedef struct _bfd_window
486 {
487   /* What the user asked for.  */
488   void *data;
489   bfd_size_type size;
490   /* The actual window used by BFD.  Small user-requested read-only
491      regions sharing a page may share a single window into the object
492      file.  Read-write versions shouldn't until I've fixed things to
493      keep track of which portions have been claimed by the
494      application; don't want to give the same region back when the
495      application wants two writable copies!  */
496   struct _bfd_window_internal *i;
497 }
498 bfd_window;
499 
500 extern void bfd_init_window
501   (bfd_window *);
502 extern void bfd_free_window
503   (bfd_window *);
504 extern bool bfd_get_file_window
505   (bfd *, file_ptr, bfd_size_type, bfd_window *, bool);
506 
507 /* Externally visible ELF routines.  */
508 
509 /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
510    reconstruct an ELF file by reading the segments out of remote
511    memory based on the ELF file header at EHDR_VMA and the ELF program
512    headers it points to.  If non-zero, SIZE is the known extent of the
513    object.  If not null, *LOADBASEP is filled in with the difference
514    between the VMAs from which the segments were read, and the VMAs
515    the file headers (and hence BFD's idea of each section's VMA) put
516    them at.
517 
518    The function TARGET_READ_MEMORY is called to copy LEN bytes from
519    the remote memory at target address VMA into the local buffer at
520    MYADDR; it should return zero on success or an `errno' code on
521    failure.  TEMPL must be a BFD for a target with the word size and
522    byte order found in the remote memory.  */
523 extern bfd *bfd_elf_bfd_from_remote_memory
524   (bfd *templ, bfd_vma ehdr_vma, bfd_size_type size, bfd_vma *loadbasep,
525    int (*target_read_memory) (bfd_vma vma, bfd_byte *myaddr,
526 			      bfd_size_type len));
527 
528 /* Forward declarations.  */
529 struct ecoff_debug_info;
530 struct ecoff_debug_swap;
531 struct ecoff_extr;
532 struct bfd_link_info;
533 struct bfd_link_hash_entry;
534 
535 /* Return TRUE if the start of STR matches PREFIX, FALSE otherwise.  */
536 
537 static inline bool
startswith(const char * str,const char * prefix)538 startswith (const char *str, const char *prefix)
539 {
540   return strncmp (str, prefix, strlen (prefix)) == 0;
541 }
542