Commit | Line | Data |
---|---|---|
cbb099e8 TT |
1 | /* Definitions for BFD wrappers used by GDB. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2011-2020 Free Software Foundation, Inc. |
cbb099e8 TT |
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 | ||
e992eda4 | 23 | #include "registry.h" |
e0fc5c3f | 24 | #include "gdbsupport/byte-vector.h" |
268a13a5 | 25 | #include "gdbsupport/gdb_ref_ptr.h" |
e992eda4 TT |
26 | |
27 | DECLARE_REGISTRY (bfd); | |
28 | ||
f08e97fe GB |
29 | /* If supplied a path starting with this sequence, gdb_bfd_open will |
30 | open BFDs using target fileio operations. */ | |
31 | ||
32 | #define TARGET_SYSROOT_PREFIX "target:" | |
33 | ||
34 | /* Returns nonzero if NAME starts with TARGET_SYSROOT_PREFIX, zero | |
35 | otherwise. */ | |
36 | ||
37 | int is_target_filename (const char *name); | |
38 | ||
39 | /* Returns nonzero if the filename associated with ABFD starts with | |
40 | TARGET_SYSROOT_PREFIX, zero otherwise. */ | |
41 | ||
42 | int gdb_bfd_has_target_filename (struct bfd *abfd); | |
43 | ||
192b62ce TT |
44 | /* Increment the reference count of ABFD. It is fine for ABFD to be |
45 | NULL; in this case the function does nothing. */ | |
46 | ||
47 | void gdb_bfd_ref (struct bfd *abfd); | |
48 | ||
49 | /* Decrement the reference count of ABFD. If this is the last | |
50 | reference, ABFD will be freed. If ABFD is NULL, this function does | |
51 | nothing. */ | |
52 | ||
53 | void gdb_bfd_unref (struct bfd *abfd); | |
54 | ||
55 | /* A policy class for gdb::ref_ptr for BFD reference counting. */ | |
56 | struct gdb_bfd_ref_policy | |
57 | { | |
58 | static void incref (struct bfd *abfd) | |
59 | { | |
60 | gdb_bfd_ref (abfd); | |
61 | } | |
62 | ||
63 | static void decref (struct bfd *abfd) | |
64 | { | |
65 | gdb_bfd_unref (abfd); | |
66 | } | |
67 | }; | |
68 | ||
69 | /* A gdb::ref_ptr that has been specialized for BFD objects. */ | |
70 | typedef gdb::ref_ptr<struct bfd, gdb_bfd_ref_policy> gdb_bfd_ref_ptr; | |
71 | ||
6ec53d05 | 72 | /* Open a read-only (FOPEN_RB) BFD given arguments like bfd_fopen. |
f08e97fe GB |
73 | If NAME starts with TARGET_SYSROOT_PREFIX then the BFD will be |
74 | opened using target fileio operations if necessary. Returns NULL | |
1831a9f9 TT |
75 | on error. On success, returns a new reference to the BFD. BFDs |
76 | returned by this call are shared among all callers opening the same | |
77 | file. If FD is not -1, then after this call it is owned by BFD. | |
78 | If the BFD was not accessed using target fileio operations then the | |
79 | filename associated with the BFD and accessible with | |
80 | bfd_get_filename will not be exactly NAME but rather NAME with | |
98c59b52 PA |
81 | TARGET_SYSROOT_PREFIX stripped. If WARN_IF_SLOW is true, print a |
82 | warning message if the file is being accessed over a link that may | |
83 | be slow. */ | |
6ec53d05 | 84 | |
ad80db5b | 85 | gdb_bfd_ref_ptr gdb_bfd_open (const char *name, const char *target, |
98c59b52 | 86 | int fd = -1, bool warn_if_slow = true); |
cbb099e8 | 87 | |
0cd61f44 TT |
88 | /* Mark the CHILD BFD as being a member of PARENT. Also, increment |
89 | the reference count of CHILD. Calling this function ensures that | |
90 | as along as CHILD remains alive, PARENT will as well. Both CHILD | |
91 | and PARENT must be non-NULL. This can be called more than once | |
92 | with the same arguments; but it is not allowed to call it for a | |
93 | single CHILD with different values for PARENT. */ | |
94 | ||
95 | void gdb_bfd_mark_parent (bfd *child, bfd *parent); | |
96 | ||
13aaf454 DE |
97 | /* Mark INCLUDEE as being included by INCLUDER. |
98 | This is used to associate the life time of INCLUDEE with INCLUDER. | |
99 | For example, with Fission, one file can refer to debug info in another | |
100 | file, and internal tables we build for the main file (INCLUDER) may refer | |
101 | to data contained in INCLUDEE. Therefore we want to keep INCLUDEE around | |
102 | at least as long as INCLUDER exists. | |
103 | ||
104 | Note that this is different than gdb_bfd_mark_parent because in our case | |
105 | lifetime tracking is based on the "parent" whereas in gdb_bfd_mark_parent | |
106 | lifetime tracking is based on the "child". Plus in our case INCLUDEE could | |
107 | have multiple different "parents". */ | |
108 | ||
109 | void gdb_bfd_record_inclusion (bfd *includer, bfd *includee); | |
110 | ||
41667530 MG |
111 | /* Try to read or map the contents of the section SECT. If successful, the |
112 | section data is returned and *SIZE is set to the size of the section data; | |
fd361982 | 113 | this may not be the same as the size according to bfd_section_size if the |
41667530 MG |
114 | section was compressed. The returned section data is associated with the BFD |
115 | and will be destroyed when the BFD is destroyed. There is no other way to | |
116 | free it; for temporary uses of section data, see bfd_malloc_and_get_section. | |
117 | SECT may not have relocations. If there is an error reading the section, | |
118 | this issues a warning, sets *SIZE to 0, and returns NULL. */ | |
4bf44c1c TT |
119 | |
120 | const gdb_byte *gdb_bfd_map_section (asection *section, bfd_size_type *size); | |
121 | ||
dccee2de TT |
122 | /* Compute the CRC for ABFD. The CRC is used to find and verify |
123 | separate debug files. When successful, this fills in *CRC_OUT and | |
124 | returns 1. Otherwise, this issues a warning and returns 0. */ | |
125 | ||
126 | int gdb_bfd_crc (struct bfd *abfd, unsigned long *crc_out); | |
127 | ||
64c31149 TT |
128 | \f |
129 | ||
130 | /* A wrapper for bfd_fopen that initializes the gdb-specific reference | |
adcf2eed | 131 | count. */ |
64c31149 | 132 | |
192b62ce | 133 | gdb_bfd_ref_ptr gdb_bfd_fopen (const char *, const char *, const char *, int); |
64c31149 TT |
134 | |
135 | /* A wrapper for bfd_openr that initializes the gdb-specific reference | |
adcf2eed | 136 | count. */ |
64c31149 | 137 | |
192b62ce | 138 | gdb_bfd_ref_ptr gdb_bfd_openr (const char *, const char *); |
64c31149 TT |
139 | |
140 | /* A wrapper for bfd_openw that initializes the gdb-specific reference | |
adcf2eed | 141 | count. */ |
64c31149 | 142 | |
192b62ce | 143 | gdb_bfd_ref_ptr gdb_bfd_openw (const char *, const char *); |
64c31149 TT |
144 | |
145 | /* A wrapper for bfd_openr_iovec that initializes the gdb-specific | |
adcf2eed | 146 | reference count. */ |
64c31149 | 147 | |
192b62ce TT |
148 | gdb_bfd_ref_ptr gdb_bfd_openr_iovec (const char *filename, const char *target, |
149 | void *(*open_func) (struct bfd *nbfd, | |
150 | void *open_closure), | |
151 | void *open_closure, | |
152 | file_ptr (*pread_func) (struct bfd *nbfd, | |
153 | void *stream, | |
154 | void *buf, | |
155 | file_ptr nbytes, | |
156 | file_ptr offset), | |
157 | int (*close_func) (struct bfd *nbfd, | |
158 | void *stream), | |
159 | int (*stat_func) (struct bfd *abfd, | |
160 | void *stream, | |
161 | struct stat *sb)); | |
64c31149 TT |
162 | |
163 | /* A wrapper for bfd_openr_next_archived_file that initializes the | |
adcf2eed | 164 | gdb-specific reference count. */ |
64c31149 | 165 | |
192b62ce | 166 | gdb_bfd_ref_ptr gdb_bfd_openr_next_archived_file (bfd *archive, bfd *previous); |
64c31149 | 167 | |
64c31149 | 168 | |
65cf3563 TT |
169 | \f |
170 | ||
171 | /* Return the index of the BFD section SECTION. Ordinarily this is | |
172 | just the section's index, but for some special sections, like | |
173 | bfd_com_section_ptr, it will be a synthesized value. */ | |
174 | ||
175 | int gdb_bfd_section_index (bfd *abfd, asection *section); | |
176 | ||
177 | ||
178 | /* Like bfd_count_sections, but include any possible global sections, | |
179 | like bfd_com_section_ptr. */ | |
180 | ||
181 | int gdb_bfd_count_sections (bfd *abfd); | |
182 | ||
1da77581 TT |
183 | /* Return true if any section requires relocations, false |
184 | otherwise. */ | |
185 | ||
186 | int gdb_bfd_requires_relocations (bfd *abfd); | |
187 | ||
e0fc5c3f SM |
188 | /* Alternative to bfd_get_full_section_contents that returns the section |
189 | contents in *CONTENTS, instead of an allocated buffer. | |
190 | ||
191 | Return true on success, false otherwise. */ | |
192 | ||
193 | bool gdb_bfd_get_full_section_contents (bfd *abfd, asection *section, | |
194 | gdb::byte_vector *contents); | |
195 | ||
cbb099e8 | 196 | #endif /* GDB_BFD_H */ |