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