9 kx /* Main header file for the bfd library -- portable access to object files.
9 kx
9 kx Copyright (C) 1990-2023 Free Software Foundation, Inc.
9 kx
9 kx Contributed by Cygnus Support.
9 kx
9 kx This file is part of BFD, the Binary File Descriptor library.
9 kx
9 kx This program is free software; you can redistribute it and/or modify
9 kx it under the terms of the GNU General Public License as published by
9 kx the Free Software Foundation; either version 3 of the License, or
9 kx (at your option) any later version.
9 kx
9 kx This program is distributed in the hope that it will be useful,
9 kx but WITHOUT ANY WARRANTY; without even the implied warranty of
9 kx MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 kx GNU General Public License for more details.
9 kx
9 kx You should have received a copy of the GNU General Public License
9 kx along with this program; if not, write to the Free Software
9 kx Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
9 kx
9 kx #ifndef __BFD_H_SEEN__
9 kx #define __BFD_H_SEEN__
9 kx
9 kx #ifdef __cplusplus
9 kx extern "C" {
9 kx #endif
9 kx
9 kx #include "ansidecl.h"
9 kx #include "symcat.h"
9 kx #include <stdint.h>
9 kx #include <stdbool.h>
9 kx #include "diagnostics.h"
9 kx #include <stdarg.h>
9 kx #include <string.h>
9 kx #include <sys/stat.h>
9 kx
9 kx #if defined (__STDC__) || defined (ALMOST_STDC) || defined (HAVE_STRINGIZE)
9 kx #ifndef SABER
9 kx /* This hack is to avoid a problem with some strict ANSI C preprocessors.
9 kx The problem is, "32_" is not a valid preprocessing token, and we don't
9 kx want extra underscores (e.g., "nlm_32_"). The XCONCAT2 macro will
9 kx cause the inner CONCAT2 macros to be evaluated first, producing
9 kx still-valid pp-tokens. Then the final concatenation can be done. */
9 kx #undef CONCAT4
9 kx #define CONCAT4(a,b,c,d) XCONCAT2(CONCAT2(a,b),CONCAT2(c,d))
9 kx #endif
9 kx #endif
9 kx
9 kx /* This is a utility macro to handle the situation where the code
9 kx wants to place a constant string into the code, followed by a
9 kx comma and then the length of the string. Doing this by hand
9 kx is error prone, so using this macro is safer. */
9 kx #define STRING_COMMA_LEN(STR) (STR), (sizeof (STR) - 1)
9 kx
9 kx #define BFD_SUPPORTS_PLUGINS @supports_plugins@
9 kx
9 kx /* The word size used by BFD on the host. This may be 64 with a 32
9 kx bit target if the host is 64 bit, or if other 64 bit targets have
9 kx been selected with --enable-targets, or if --enable-64-bit-bfd. */
9 kx #define BFD_ARCH_SIZE @wordsize@
9 kx
9 kx /* The word size of the default bfd target. */
9 kx #define BFD_DEFAULT_TARGET_SIZE @bfd_default_target_size@
9 kx
9 kx #include <inttypes.h>
9 kx
9 kx #if BFD_ARCH_SIZE >= 64
9 kx #define BFD64
9 kx #endif
9 kx
9 kx /* Forward declaration. */
9 kx typedef struct bfd bfd;
9 kx
9 kx /* Boolean type used in bfd.
9 kx General rule: Functions which are bfd_boolean return TRUE on
9 kx success and FALSE on failure (unless they're a predicate). */
9 kx
9 kx #ifdef POISON_BFD_BOOLEAN
9 kx # pragma GCC poison bfd_boolean
9 kx #else
9 kx # define bfd_boolean bool
9 kx # undef FALSE
9 kx # undef TRUE
9 kx # define FALSE 0
9 kx # define TRUE 1
9 kx #endif
9 kx
9 kx /* Silence "applying zero offset to null pointer" UBSAN warnings. */
9 kx #define PTR_ADD(P,A) ((A) != 0 ? (P) + (A) : (P))
9 kx /* Also prevent non-zero offsets from being applied to a null pointer. */
9 kx #define NPTR_ADD(P,A) ((P) != NULL ? (P) + (A) : (P))
9 kx
9 kx #ifdef BFD64
9 kx
9 kx /* Represent a target address. Also used as a generic unsigned type
9 kx which is guaranteed to be big enough to hold any arithmetic types
9 kx we need to deal with. */
9 kx typedef uint64_t bfd_vma;
9 kx
9 kx /* A generic signed type which is guaranteed to be big enough to hold any
9 kx arithmetic types we need to deal with. Can be assumed to be compatible
9 kx with bfd_vma in the same way that signed and unsigned ints are compatible
9 kx (as parameters, in assignment, etc). */
9 kx typedef int64_t bfd_signed_vma;
9 kx
9 kx typedef uint64_t bfd_size_type;
9 kx typedef uint64_t symvalue;
9 kx
9 kx #else /* not BFD64 */
9 kx
9 kx typedef unsigned long bfd_vma;
9 kx typedef long bfd_signed_vma;
9 kx typedef unsigned long symvalue;
9 kx typedef unsigned long bfd_size_type;
9 kx
9 kx #endif /* not BFD64 */
9 kx
9 kx #define HALF_BFD_SIZE_TYPE \
9 kx (((bfd_size_type) 1) << (8 * sizeof (bfd_size_type) / 2))
9 kx
9 kx /* An offset into a file. BFD always uses the largest possible offset
9 kx based on the build time availability of fseek, fseeko, or fseeko64. */
9 kx typedef @bfd_file_ptr@ file_ptr;
9 kx typedef @bfd_ufile_ptr@ ufile_ptr;
9 kx
9 kx extern void bfd_sprintf_vma (bfd *, char *, bfd_vma);
9 kx extern void bfd_fprintf_vma (bfd *, void *, bfd_vma);
9 kx
9 kx #define bfd_printf_vma(abfd,x) bfd_fprintf_vma (abfd,stdout,x)
9 kx
9 kx typedef unsigned int flagword; /* 32 bits of flags */
9 kx typedef unsigned char bfd_byte;
9 kx
9 kx /* File formats. */
9 kx
9 kx typedef enum bfd_format
9 kx {
9 kx bfd_unknown = 0, /* File format is unknown. */
9 kx bfd_object, /* Linker/assembler/compiler output. */
9 kx bfd_archive, /* Object archive file. */
9 kx bfd_core, /* Core dump. */
9 kx bfd_type_end /* Marks the end; don't use it! */
9 kx }
9 kx bfd_format;
9 kx
9 kx /* Symbols and relocation. */
9 kx
9 kx /* A count of carsyms (canonical archive symbols). */
9 kx typedef unsigned long symindex;
9 kx
9 kx #define BFD_NO_MORE_SYMBOLS ((symindex) ~0)
9 kx
9 kx /* A canonical archive symbol. */
9 kx /* This is a type pun with struct ranlib on purpose! */
9 kx typedef struct carsym
9 kx {
9 kx const char *name;
9 kx file_ptr file_offset; /* Look here to find the file. */
9 kx }
9 kx carsym; /* To make these you call a carsymogen. */
9 kx
9 kx /* Used in generating armaps (archive tables of contents).
9 kx Perhaps just a forward definition would do? */
9 kx struct orl /* Output ranlib. */
9 kx {
9 kx char **name; /* Symbol name. */
9 kx union
9 kx {
9 kx file_ptr pos;
9 kx bfd *abfd;
9 kx } u; /* bfd* or file position. */
9 kx int namidx; /* Index into string table. */
9 kx };
9 kx
9 kx /* Linenumber stuff. */
9 kx typedef struct lineno_cache_entry
9 kx {
9 kx unsigned int line_number; /* Linenumber from start of function. */
9 kx union
9 kx {
9 kx struct bfd_symbol *sym; /* Function name. */
9 kx bfd_vma offset; /* Offset into section. */
9 kx } u;
9 kx }
9 kx alent;
9 kx
9 kx /* Object and core file sections. */
9 kx typedef struct bfd_section *sec_ptr;
9 kx
9 kx #define align_power(addr, align) \
9 kx (((addr) + ((bfd_vma) 1 << (align)) - 1) & (-((bfd_vma) 1 << (align))))
9 kx
9 kx /* Align an address upward to a boundary, expressed as a number of bytes.
9 kx E.g. align to an 8-byte boundary with argument of 8. Take care never
9 kx to wrap around if the address is within boundary-1 of the end of the
9 kx address space. */
9 kx #define BFD_ALIGN(this, boundary) \
9 kx ((((bfd_vma) (this) + (boundary) - 1) >= (bfd_vma) (this)) \
9 kx ? (((bfd_vma) (this) + ((boundary) - 1)) & ~ (bfd_vma) ((boundary)-1)) \
9 kx : ~ (bfd_vma) 0)
9 kx
9 kx typedef enum bfd_print_symbol
9 kx {
9 kx bfd_print_symbol_name,
9 kx bfd_print_symbol_more,
9 kx bfd_print_symbol_all
9 kx } bfd_print_symbol_type;
9 kx
9 kx /* Information about a symbol that nm needs. */
9 kx
9 kx typedef struct _symbol_info
9 kx {
9 kx symvalue value;
9 kx char type;
9 kx const char *name; /* Symbol name. */
9 kx unsigned char stab_type; /* Stab type. */
9 kx char stab_other; /* Stab other. */
9 kx short stab_desc; /* Stab desc. */
9 kx const char *stab_name; /* String for stab type. */
9 kx } symbol_info;
9 kx
9 kx /* Get the name of a stabs type code. */
9 kx
9 kx extern const char *bfd_get_stab_name (int);
9 kx
9 kx /* Hash table routines. There is no way to free up a hash table. */
9 kx
9 kx /* An element in the hash table. Most uses will actually use a larger
9 kx structure, and an instance of this will be the first field. */
9 kx
9 kx struct bfd_hash_entry
9 kx {
9 kx /* Next entry for this hash code. */
9 kx struct bfd_hash_entry *next;
9 kx /* String being hashed. */
9 kx const char *string;
9 kx /* Hash code. This is the full hash code, not the index into the
9 kx table. */
9 kx unsigned long hash;
9 kx };
9 kx
9 kx /* A hash table. */
9 kx
9 kx struct bfd_hash_table
9 kx {
9 kx /* The hash array. */
9 kx struct bfd_hash_entry **table;
9 kx /* A function used to create new elements in the hash table. The
9 kx first entry is itself a pointer to an element. When this
9 kx function is first invoked, this pointer will be NULL. However,
9 kx having the pointer permits a hierarchy of method functions to be
9 kx built each of which calls the function in the superclass. Thus
9 kx each function should be written to allocate a new block of memory
9 kx only if the argument is NULL. */
9 kx struct bfd_hash_entry *(*newfunc)
9 kx (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
9 kx /* An objalloc for this hash table. This is a struct objalloc *,
9 kx but we use void * to avoid requiring the inclusion of objalloc.h. */
9 kx void *memory;
9 kx /* The number of slots in the hash table. */
9 kx unsigned int size;
9 kx /* The number of entries in the hash table. */
9 kx unsigned int count;
9 kx /* The size of elements. */
9 kx unsigned int entsize;
9 kx /* If non-zero, don't grow the hash table. */
9 kx unsigned int frozen:1;
9 kx };
9 kx
9 kx /* Initialize a hash table. */
9 kx extern bool bfd_hash_table_init
9 kx (struct bfd_hash_table *,
9 kx struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
9 kx struct bfd_hash_table *,
9 kx const char *),
9 kx unsigned int);
9 kx
9 kx /* Initialize a hash table specifying a size. */
9 kx extern bool bfd_hash_table_init_n
9 kx (struct bfd_hash_table *,
9 kx struct bfd_hash_entry *(*) (struct bfd_hash_entry *,
9 kx struct bfd_hash_table *,
9 kx const char *),
9 kx unsigned int, unsigned int);
9 kx
9 kx /* Free up a hash table. */
9 kx extern void bfd_hash_table_free
9 kx (struct bfd_hash_table *);
9 kx
9 kx /* Look up a string in a hash table. If CREATE is TRUE, a new entry
9 kx will be created for this string if one does not already exist. The
9 kx COPY argument must be TRUE if this routine should copy the string
9 kx into newly allocated memory when adding an entry. */
9 kx extern struct bfd_hash_entry *bfd_hash_lookup
9 kx (struct bfd_hash_table *, const char *, bool create, bool copy);
9 kx
9 kx /* Insert an entry in a hash table. */
9 kx extern struct bfd_hash_entry *bfd_hash_insert
9 kx (struct bfd_hash_table *, const char *, unsigned long);
9 kx
9 kx /* Rename an entry in a hash table. */
9 kx extern void bfd_hash_rename
9 kx (struct bfd_hash_table *, const char *, struct bfd_hash_entry *);
9 kx
9 kx /* Replace an entry in a hash table. */
9 kx extern void bfd_hash_replace
9 kx (struct bfd_hash_table *, struct bfd_hash_entry *old,
9 kx struct bfd_hash_entry *nw);
9 kx
9 kx /* Base method for creating a hash table entry. */
9 kx extern struct bfd_hash_entry *bfd_hash_newfunc
9 kx (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
9 kx
9 kx /* Grab some space for a hash table entry. */
9 kx extern void *bfd_hash_allocate
9 kx (struct bfd_hash_table *, unsigned int);
9 kx
9 kx /* Traverse a hash table in a random order, calling a function on each
9 kx element. If the function returns FALSE, the traversal stops. The
9 kx INFO argument is passed to the function. */
9 kx extern void bfd_hash_traverse
9 kx (struct bfd_hash_table *,
9 kx bool (*) (struct bfd_hash_entry *, void *),
9 kx void *info);
9 kx
9 kx /* Allows the default size of a hash table to be configured. New hash
9 kx tables allocated using bfd_hash_table_init will be created with
9 kx this size. */
9 kx extern unsigned long bfd_hash_set_default_size (unsigned long);
9 kx
9 kx /* This structure is used to keep track of stabs in sections
9 kx information while linking. */
9 kx
9 kx struct stab_info
9 kx {
9 kx /* A hash table used to hold stabs strings. */
9 kx struct bfd_strtab_hash *strings;
9 kx /* The header file hash table. */
9 kx struct bfd_hash_table includes;
9 kx /* The first .stabstr section. */
9 kx struct bfd_section *stabstr;
9 kx };
9 kx
9 kx #define COFF_SWAP_TABLE (void *) &bfd_coff_std_swap_table
9 kx
9 kx /* User program access to BFD facilities. */
9 kx
9 kx /* Direct I/O routines, for programs which know more about the object
9 kx file than BFD does. Use higher level routines if possible. */
9 kx
9 kx extern bfd_size_type bfd_bread (void *, bfd_size_type, bfd *);
9 kx extern bfd_size_type bfd_bwrite (const void *, bfd_size_type, bfd *);
9 kx extern int bfd_seek (bfd *, file_ptr, int);
9 kx extern file_ptr bfd_tell (bfd *);
9 kx extern int bfd_flush (bfd *);
9 kx extern int bfd_stat (bfd *, struct stat *);
9 kx
9 kx /* Deprecated old routines. */
9 kx #if __GNUC__
9 kx #define bfd_read(BUF, ELTSIZE, NITEMS, ABFD) \
9 kx (_bfd_warn_deprecated ("bfd_read", __FILE__, __LINE__, __FUNCTION__), \
9 kx bfd_bread ((BUF), (ELTSIZE) * (NITEMS), (ABFD)))
9 kx #define bfd_write(BUF, ELTSIZE, NITEMS, ABFD) \
9 kx (_bfd_warn_deprecated ("bfd_write", __FILE__, __LINE__, __FUNCTION__), \
9 kx bfd_bwrite ((BUF), (ELTSIZE) * (NITEMS), (ABFD)))
9 kx #else
9 kx #define bfd_read(BUF, ELTSIZE, NITEMS, ABFD) \
9 kx (_bfd_warn_deprecated ("bfd_read", (const char *) 0, 0, (const char *) 0), \
9 kx bfd_bread ((BUF), (ELTSIZE) * (NITEMS), (ABFD)))
9 kx #define bfd_write(BUF, ELTSIZE, NITEMS, ABFD) \
9 kx (_bfd_warn_deprecated ("bfd_write", (const char *) 0, 0, (const char *) 0),\
9 kx bfd_bwrite ((BUF), (ELTSIZE) * (NITEMS), (ABFD)))
9 kx #endif
9 kx extern void _bfd_warn_deprecated (const char *, const char *, int, const char *);
9 kx
9 kx extern bool bfd_cache_close
9 kx (bfd *abfd);
9 kx /* NB: This declaration should match the autogenerated one in libbfd.h. */
9 kx
9 kx extern bool bfd_cache_close_all (void);
9 kx
9 kx extern bool bfd_record_phdr
9 kx (bfd *, unsigned long, bool, flagword, bool, bfd_vma,
9 kx bool, bool, unsigned int, struct bfd_section **);
9 kx
9 kx /* Byte swapping routines. */
9 kx
9 kx uint64_t bfd_getb64 (const void *);
9 kx uint64_t bfd_getl64 (const void *);
9 kx int64_t bfd_getb_signed_64 (const void *);
9 kx int64_t bfd_getl_signed_64 (const void *);
9 kx bfd_vma bfd_getb32 (const void *);
9 kx bfd_vma bfd_getl32 (const void *);
9 kx bfd_signed_vma bfd_getb_signed_32 (const void *);
9 kx bfd_signed_vma bfd_getl_signed_32 (const void *);
9 kx bfd_vma bfd_getb16 (const void *);
9 kx bfd_vma bfd_getl16 (const void *);
9 kx bfd_signed_vma bfd_getb_signed_16 (const void *);
9 kx bfd_signed_vma bfd_getl_signed_16 (const void *);
9 kx void bfd_putb64 (uint64_t, void *);
9 kx void bfd_putl64 (uint64_t, void *);
9 kx void bfd_putb32 (bfd_vma, void *);
9 kx void bfd_putl32 (bfd_vma, void *);
9 kx void bfd_putb24 (bfd_vma, void *);
9 kx void bfd_putl24 (bfd_vma, void *);
9 kx void bfd_putb16 (bfd_vma, void *);
9 kx void bfd_putl16 (bfd_vma, void *);
9 kx
9 kx /* Byte swapping routines which take size and endiannes as arguments. */
9 kx
9 kx uint64_t bfd_get_bits (const void *, int, bool);
9 kx void bfd_put_bits (uint64_t, void *, int, bool);
9 kx
9 kx
9 kx /* mmap hacks */
9 kx
9 kx struct _bfd_window_internal;
9 kx typedef struct _bfd_window_internal bfd_window_internal;
9 kx
9 kx typedef struct _bfd_window
9 kx {
9 kx /* What the user asked for. */
9 kx void *data;
9 kx bfd_size_type size;
9 kx /* The actual window used by BFD. Small user-requested read-only
9 kx regions sharing a page may share a single window into the object
9 kx file. Read-write versions shouldn't until I've fixed things to
9 kx keep track of which portions have been claimed by the
9 kx application; don't want to give the same region back when the
9 kx application wants two writable copies! */
9 kx struct _bfd_window_internal *i;
9 kx }
9 kx bfd_window;
9 kx
9 kx extern void bfd_init_window
9 kx (bfd_window *);
9 kx extern void bfd_free_window
9 kx (bfd_window *);
9 kx extern bool bfd_get_file_window
9 kx (bfd *, file_ptr, bfd_size_type, bfd_window *, bool);
9 kx
9 kx /* Externally visible ELF routines. */
9 kx
9 kx /* Create a new BFD as if by bfd_openr. Rather than opening a file,
9 kx reconstruct an ELF file by reading the segments out of remote
9 kx memory based on the ELF file header at EHDR_VMA and the ELF program
9 kx headers it points to. If non-zero, SIZE is the known extent of the
9 kx object. If not null, *LOADBASEP is filled in with the difference
9 kx between the VMAs from which the segments were read, and the VMAs
9 kx the file headers (and hence BFD's idea of each section's VMA) put
9 kx them at.
9 kx
9 kx The function TARGET_READ_MEMORY is called to copy LEN bytes from
9 kx the remote memory at target address VMA into the local buffer at
9 kx MYADDR; it should return zero on success or an `errno' code on
9 kx failure. TEMPL must be a BFD for a target with the word size and
9 kx byte order found in the remote memory. */
9 kx extern bfd *bfd_elf_bfd_from_remote_memory
9 kx (bfd *templ, bfd_vma ehdr_vma, bfd_size_type size, bfd_vma *loadbasep,
9 kx int (*target_read_memory) (bfd_vma vma, bfd_byte *myaddr,
9 kx bfd_size_type len));
9 kx
9 kx /* Forward declarations. */
9 kx struct ecoff_debug_info;
9 kx struct ecoff_debug_swap;
9 kx struct ecoff_extr;
9 kx struct bfd_link_info;
9 kx struct bfd_link_hash_entry;
9 kx
9 kx /* Return TRUE if the start of STR matches PREFIX, FALSE otherwise. */
9 kx
9 kx static inline bool
9 kx startswith (const char *str, const char *prefix)
9 kx {
9 kx return strncmp (str, prefix, strlen (prefix)) == 0;
9 kx }