bfd-in.h 18 KB

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