gdb_bfd.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Definitions for BFD wrappers used by GDB.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef GDB_BFD_H
  15. #define GDB_BFD_H
  16. #include "registry.h"
  17. #include "gdbsupport/byte-vector.h"
  18. #include "gdbsupport/gdb_ref_ptr.h"
  19. #include "gdbsupport/iterator-range.h"
  20. #include "gdbsupport/next-iterator.h"
  21. DECLARE_REGISTRY (bfd);
  22. /* If supplied a path starting with this sequence, gdb_bfd_open will
  23. open BFDs using target fileio operations. */
  24. #define TARGET_SYSROOT_PREFIX "target:"
  25. /* Returns nonzero if NAME starts with TARGET_SYSROOT_PREFIX, zero
  26. otherwise. */
  27. int is_target_filename (const char *name);
  28. /* Returns nonzero if the filename associated with ABFD starts with
  29. TARGET_SYSROOT_PREFIX, zero otherwise. */
  30. int gdb_bfd_has_target_filename (struct bfd *abfd);
  31. /* Increment the reference count of ABFD. It is fine for ABFD to be
  32. NULL; in this case the function does nothing. */
  33. void gdb_bfd_ref (struct bfd *abfd);
  34. /* Decrement the reference count of ABFD. If this is the last
  35. reference, ABFD will be freed. If ABFD is NULL, this function does
  36. nothing. */
  37. void gdb_bfd_unref (struct bfd *abfd);
  38. /* A policy class for gdb::ref_ptr for BFD reference counting. */
  39. struct gdb_bfd_ref_policy
  40. {
  41. static void incref (struct bfd *abfd)
  42. {
  43. gdb_bfd_ref (abfd);
  44. }
  45. static void decref (struct bfd *abfd)
  46. {
  47. gdb_bfd_unref (abfd);
  48. }
  49. };
  50. /* A gdb::ref_ptr that has been specialized for BFD objects. */
  51. typedef gdb::ref_ptr<struct bfd, gdb_bfd_ref_policy> gdb_bfd_ref_ptr;
  52. /* Open a read-only (FOPEN_RB) BFD given arguments like bfd_fopen.
  53. If NAME starts with TARGET_SYSROOT_PREFIX then the BFD will be
  54. opened using target fileio operations if necessary. Returns NULL
  55. on error. On success, returns a new reference to the BFD. BFDs
  56. returned by this call are shared among all callers opening the same
  57. file. If FD is not -1, then after this call it is owned by BFD.
  58. If the BFD was not accessed using target fileio operations then the
  59. filename associated with the BFD and accessible with
  60. bfd_get_filename will not be exactly NAME but rather NAME with
  61. TARGET_SYSROOT_PREFIX stripped. If WARN_IF_SLOW is true, print a
  62. warning message if the file is being accessed over a link that may
  63. be slow. */
  64. gdb_bfd_ref_ptr gdb_bfd_open (const char *name, const char *target,
  65. int fd = -1, bool warn_if_slow = true);
  66. /* Mark the CHILD BFD as being a member of PARENT. Also, increment
  67. the reference count of CHILD. Calling this function ensures that
  68. as along as CHILD remains alive, PARENT will as well. Both CHILD
  69. and PARENT must be non-NULL. This can be called more than once
  70. with the same arguments; but it is not allowed to call it for a
  71. single CHILD with different values for PARENT. */
  72. void gdb_bfd_mark_parent (bfd *child, bfd *parent);
  73. /* Mark INCLUDEE as being included by INCLUDER.
  74. This is used to associate the life time of INCLUDEE with INCLUDER.
  75. For example, with Fission, one file can refer to debug info in another
  76. file, and internal tables we build for the main file (INCLUDER) may refer
  77. to data contained in INCLUDEE. Therefore we want to keep INCLUDEE around
  78. at least as long as INCLUDER exists.
  79. Note that this is different than gdb_bfd_mark_parent because in our case
  80. lifetime tracking is based on the "parent" whereas in gdb_bfd_mark_parent
  81. lifetime tracking is based on the "child". Plus in our case INCLUDEE could
  82. have multiple different "parents". */
  83. void gdb_bfd_record_inclusion (bfd *includer, bfd *includee);
  84. /* Try to read or map the contents of the section SECT. If successful, the
  85. section data is returned and *SIZE is set to the size of the section data;
  86. this may not be the same as the size according to bfd_section_size if the
  87. section was compressed. The returned section data is associated with the BFD
  88. and will be destroyed when the BFD is destroyed. There is no other way to
  89. free it; for temporary uses of section data, see bfd_malloc_and_get_section.
  90. SECT may not have relocations. If there is an error reading the section,
  91. this issues a warning, sets *SIZE to 0, and returns NULL. */
  92. const gdb_byte *gdb_bfd_map_section (asection *section, bfd_size_type *size);
  93. /* Compute the CRC for ABFD. The CRC is used to find and verify
  94. separate debug files. When successful, this fills in *CRC_OUT and
  95. returns 1. Otherwise, this issues a warning and returns 0. */
  96. int gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out);
  97. /* A wrapper for bfd_fopen that initializes the gdb-specific reference
  98. count. */
  99. gdb_bfd_ref_ptr gdb_bfd_fopen (const char *, const char *, const char *, int);
  100. /* A wrapper for bfd_openr that initializes the gdb-specific reference
  101. count. */
  102. gdb_bfd_ref_ptr gdb_bfd_openr (const char *, const char *);
  103. /* A wrapper for bfd_openw that initializes the gdb-specific reference
  104. count. */
  105. gdb_bfd_ref_ptr gdb_bfd_openw (const char *, const char *);
  106. /* A wrapper for bfd_openr_iovec that initializes the gdb-specific
  107. reference count. */
  108. gdb_bfd_ref_ptr gdb_bfd_openr_iovec (const char *filename, const char *target,
  109. void *(*open_func) (struct bfd *nbfd,
  110. void *open_closure),
  111. void *open_closure,
  112. file_ptr (*pread_func) (struct bfd *nbfd,
  113. void *stream,
  114. void *buf,
  115. file_ptr nbytes,
  116. file_ptr offset),
  117. int (*close_func) (struct bfd *nbfd,
  118. void *stream),
  119. int (*stat_func) (struct bfd *abfd,
  120. void *stream,
  121. struct stat *sb));
  122. /* A wrapper for bfd_openr_next_archived_file that initializes the
  123. gdb-specific reference count. */
  124. gdb_bfd_ref_ptr gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous);
  125. /* Return the index of the BFD section SECTION. Ordinarily this is
  126. just the section's index, but for some special sections, like
  127. bfd_com_section_ptr, it will be a synthesized value. */
  128. int gdb_bfd_section_index (bfd *abfd, asection *section);
  129. /* Like bfd_count_sections, but include any possible global sections,
  130. like bfd_com_section_ptr. */
  131. int gdb_bfd_count_sections (bfd *abfd);
  132. /* Return true if any section requires relocations, false
  133. otherwise. */
  134. int gdb_bfd_requires_relocations (bfd *abfd);
  135. /* Alternative to bfd_get_full_section_contents that returns the section
  136. contents in *CONTENTS, instead of an allocated buffer.
  137. Return true on success, false otherwise. */
  138. bool gdb_bfd_get_full_section_contents (bfd *abfd, asection *section,
  139. gdb::byte_vector *contents);
  140. /* Create and initialize a BFD handle from a target in-memory range. */
  141. gdb_bfd_ref_ptr gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
  142. const char *target,
  143. const char *filename = nullptr);
  144. /* Range adapter for a BFD's sections.
  145. To be used as:
  146. for (asection *sect : gdb_bfd_all_sections (bfd))
  147. ... use SECT ...
  148. */
  149. using gdb_bfd_section_range = next_range<asection>;
  150. static inline gdb_bfd_section_range
  151. gdb_bfd_sections (bfd *abfd)
  152. {
  153. return gdb_bfd_section_range (abfd->sections);
  154. }
  155. static inline gdb_bfd_section_range
  156. gdb_bfd_sections (const gdb_bfd_ref_ptr &abfd)
  157. {
  158. return gdb_bfd_section_range (abfd->sections);
  159. };
  160. /* A wrapper for bfd_errmsg to produce a more helpful error message
  161. in the case of bfd_error_file_ambiguously recognized.
  162. MATCHING, if non-NULL, is the corresponding argument to
  163. bfd_check_format_matches, and will be freed. */
  164. extern std::string gdb_bfd_errmsg (bfd_error_type error_tag, char **matching);
  165. #endif /* GDB_BFD_H */