gdb/mi: remove warning about mi1
[binutils-gdb.git] / gdb / minidebug.c
1 /* Read MiniDebugInfo data from an objfile.
2
3 Copyright (C) 2012-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 #include "defs.h"
21 #include "gdb_bfd.h"
22 #include "symfile.h"
23 #include "objfiles.h"
24 #include "gdbcore.h"
25 #include <algorithm>
26
27 #ifdef HAVE_LIBLZMA
28
29 /* We stash a reference to the .gnu_debugdata BFD on the enclosing
30 BFD. */
31 static const registry<bfd>::key<gdb_bfd_ref_ptr> gnu_debug_key;
32
33 #include <lzma.h>
34
35 /* Allocator function for LZMA. */
36
37 static void *
38 alloc_lzma (void *opaque, size_t nmemb, size_t size)
39 {
40 return xmalloc (nmemb * size);
41 }
42
43 /* Free function for LZMA. */
44
45 static void
46 free_lzma (void *opaque, void *ptr)
47 {
48 xfree (ptr);
49 }
50
51 /* The allocator object for LZMA. Note that 'gdb_lzma_allocator'
52 cannot be const due to the lzma library function prototypes. */
53
54 static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };
55
56 /* Custom bfd_openr_iovec implementation to read compressed data from
57 a section. This keeps only the last decompressed block in memory
58 to allow larger data without using to much memory. */
59
60 struct gdb_lzma_stream
61 {
62 /* Section of input BFD from which we are decoding data. */
63 asection *section = nullptr;
64
65 /* lzma library decompression state. */
66 lzma_index *index = nullptr;
67
68 /* Currently decoded block. */
69 bfd_size_type data_start = 0;
70 bfd_size_type data_end = 0;
71 gdb::byte_vector data;
72 };
73
74 /* bfd_openr_iovec OPEN_P implementation for
75 find_separate_debug_file_in_section. OPEN_CLOSURE is 'asection *'
76 of the section to decompress.
77
78 Return 'struct gdb_lzma_stream *' must be freed by caller by delete,
79 together with its INDEX lzma data. */
80
81 static void *
82 lzma_open (struct bfd *nbfd, void *open_closure)
83 {
84 asection *section = (asection *) open_closure;
85 bfd_size_type size, offset;
86 lzma_stream_flags options;
87 gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
88 lzma_index *index;
89 uint64_t memlimit = UINT64_MAX;
90 struct gdb_lzma_stream *lstream;
91 size_t pos;
92
93 size = bfd_section_size (section);
94 offset = section->filepos + size - LZMA_STREAM_HEADER_SIZE;
95 if (size < LZMA_STREAM_HEADER_SIZE
96 || bfd_seek (section->owner, offset, SEEK_SET) != 0
97 || bfd_read (footer, LZMA_STREAM_HEADER_SIZE, section->owner)
98 != LZMA_STREAM_HEADER_SIZE
99 || lzma_stream_footer_decode (&options, footer) != LZMA_OK
100 || offset < options.backward_size)
101 {
102 bfd_set_error (bfd_error_wrong_format);
103 return NULL;
104 }
105
106 offset -= options.backward_size;
107 gdb::byte_vector indexdata (options.backward_size);
108 index = NULL;
109 pos = 0;
110 if (bfd_seek (section->owner, offset, SEEK_SET) != 0
111 || bfd_read (indexdata.data (), options.backward_size, section->owner)
112 != options.backward_size
113 || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator,
114 indexdata.data (), &pos,
115 options.backward_size)
116 != LZMA_OK
117 || lzma_index_size (index) != options.backward_size)
118 {
119 bfd_set_error (bfd_error_wrong_format);
120 return NULL;
121 }
122
123 lstream = new struct gdb_lzma_stream;
124 lstream->section = section;
125 lstream->index = index;
126
127 return lstream;
128 }
129
130 /* bfd_openr_iovec PREAD_P implementation for
131 find_separate_debug_file_in_section. Passed STREAM
132 is 'struct gdb_lzma_stream *'. */
133
134 static file_ptr
135 lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
136 file_ptr offset)
137 {
138 struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
139 bfd_size_type chunk_size;
140 lzma_index_iter iter;
141 file_ptr block_offset;
142 lzma_filter filters[LZMA_FILTERS_MAX + 1];
143 lzma_block block;
144 size_t compressed_pos, uncompressed_pos;
145 file_ptr res;
146
147 res = 0;
148 while (nbytes > 0)
149 {
150 if (lstream->data.empty ()
151 || lstream->data_start > offset || offset >= lstream->data_end)
152 {
153 asection *section = lstream->section;
154
155 lzma_index_iter_init (&iter, lstream->index);
156 if (lzma_index_iter_locate (&iter, offset))
157 break;
158
159 gdb::byte_vector compressed (iter.block.total_size);
160 block_offset = section->filepos + iter.block.compressed_file_offset;
161 if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0
162 || bfd_read (compressed.data (), iter.block.total_size,
163 section->owner) != iter.block.total_size)
164 break;
165
166 gdb::byte_vector uncompressed (iter.block.uncompressed_size);
167
168 memset (&block, 0, sizeof (block));
169 block.filters = filters;
170 block.header_size = lzma_block_header_size_decode (compressed[0]);
171 if (lzma_block_header_decode (&block, &gdb_lzma_allocator,
172 compressed.data ())
173 != LZMA_OK)
174 break;
175
176 compressed_pos = block.header_size;
177 uncompressed_pos = 0;
178 if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator,
179 compressed.data (), &compressed_pos,
180 iter.block.total_size,
181 uncompressed.data (),
182 &uncompressed_pos,
183 iter.block.uncompressed_size)
184 != LZMA_OK)
185 break;
186
187 lstream->data = std::move (uncompressed);
188 lstream->data_start = iter.block.uncompressed_file_offset;
189 lstream->data_end = (iter.block.uncompressed_file_offset
190 + iter.block.uncompressed_size);
191 }
192
193 chunk_size = std::min (nbytes, (file_ptr) lstream->data_end - offset);
194 memcpy (buf, lstream->data.data () + offset - lstream->data_start,
195 chunk_size);
196 buf = (gdb_byte *) buf + chunk_size;
197 offset += chunk_size;
198 nbytes -= chunk_size;
199 res += chunk_size;
200 }
201
202 return res;
203 }
204
205 /* bfd_openr_iovec CLOSE_P implementation for
206 find_separate_debug_file_in_section. Passed STREAM
207 is 'struct gdb_lzma_stream *'. */
208
209 static int
210 lzma_close (struct bfd *nbfd,
211 void *stream)
212 {
213 struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
214
215 lzma_index_end (lstream->index, &gdb_lzma_allocator);
216 delete lstream;
217
218 /* Zero means success. */
219 return 0;
220 }
221
222 /* bfd_openr_iovec STAT_P implementation for
223 find_separate_debug_file_in_section. Passed STREAM
224 is 'struct gdb_lzma_stream *'. */
225
226 static int
227 lzma_stat (struct bfd *abfd,
228 void *stream,
229 struct stat *sb)
230 {
231 struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
232
233 memset (sb, 0, sizeof (struct stat));
234 sb->st_size = lzma_index_uncompressed_size (lstream->index);
235 return 0;
236 }
237
238 #endif /* HAVE_LIBLZMA */
239
240 /* This looks for a xz compressed separate debug info object file embedded
241 in a section called .gnu_debugdata. See
242 http://fedoraproject.org/wiki/Features/MiniDebugInfo
243 or the "Separate Debug Sections" of the manual for details.
244 If we find one we create a iovec based bfd that decompresses the
245 object data on demand. If we don't find one, return NULL. */
246
247 gdb_bfd_ref_ptr
248 find_separate_debug_file_in_section (struct objfile *objfile)
249 {
250 asection *section;
251 gdb_bfd_ref_ptr abfd;
252
253 if (objfile->obfd == NULL)
254 return NULL;
255
256 section = bfd_get_section_by_name (objfile->obfd.get (), ".gnu_debugdata");
257 if (section == NULL)
258 return NULL;
259
260 #ifdef HAVE_LIBLZMA
261 gdb_bfd_ref_ptr *shared = gnu_debug_key.get (objfile->obfd.get ());
262 if (shared != nullptr)
263 return *shared;
264
265 std::string filename = string_printf (_(".gnu_debugdata for %s"),
266 objfile_name (objfile));
267
268 abfd = gdb_bfd_openr_iovec (filename.c_str (), gnutarget, lzma_open,
269 section, lzma_pread, lzma_close, lzma_stat);
270 if (abfd == NULL)
271 return NULL;
272
273 if (!bfd_check_format (abfd.get (), bfd_object))
274 {
275 warning (_("Cannot parse .gnu_debugdata section; not a BFD object"));
276 return NULL;
277 }
278
279 gnu_debug_key.emplace (objfile->obfd.get (), abfd);
280
281 #else
282 warning (_("Cannot parse .gnu_debugdata section; LZMA support was "
283 "disabled at compile time"));
284 #endif /* !HAVE_LIBLZMA */
285
286 return abfd;
287 }