]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/minidebug.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / minidebug.c
CommitLineData
b181bbd0
TT
1/* Read MiniDebugInfo data from an objfile.
2
1d506c26 3 Copyright (C) 2012-2024 Free Software Foundation, Inc.
b181bbd0
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#include "defs.h"
21#include "gdb_bfd.h"
b181bbd0
TT
22#include "symfile.h"
23#include "objfiles.h"
24#include "gdbcore.h"
325fac50 25#include <algorithm>
b181bbd0
TT
26
27#ifdef HAVE_LIBLZMA
28
a4a38eb4
TT
29/* We stash a reference to the .gnu_debugdata BFD on the enclosing
30 BFD. */
08b8a139 31static const registry<bfd>::key<gdb_bfd_ref_ptr> gnu_debug_key;
a4a38eb4 32
b181bbd0
TT
33#include <lzma.h>
34
35/* Allocator function for LZMA. */
36
37static void *
38alloc_lzma (void *opaque, size_t nmemb, size_t size)
39{
40 return xmalloc (nmemb * size);
41}
42
43/* Free function for LZMA. */
44
45static void
46free_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
54static 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
d1f50898 60struct gdb_lzma_stream : public gdb_bfd_iovec_base
b181bbd0
TT
61{
62 /* Section of input BFD from which we are decoding data. */
e76d241a 63 asection *section = nullptr;
b181bbd0
TT
64
65 /* lzma library decompression state. */
e76d241a 66 lzma_index *index = nullptr;
b181bbd0
TT
67
68 /* Currently decoded block. */
e76d241a
TT
69 bfd_size_type data_start = 0;
70 bfd_size_type data_end = 0;
71 gdb::byte_vector data;
b181bbd0 72
b181bbd0 73
d1f50898
TT
74 ~gdb_lzma_stream ()
75 {
76 lzma_index_end (index, &gdb_lzma_allocator);
77 }
b181bbd0 78
d1f50898
TT
79 file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes,
80 file_ptr offset) override;
81
82 int stat (struct bfd *abfd, struct stat *sb) override;
83};
84
85/* bfd_openr_iovec implementation helper for
86 find_separate_debug_file_in_section. */
87
88static gdb_lzma_stream *
89lzma_open (struct bfd *nbfd, asection *section)
b181bbd0 90{
b181bbd0
TT
91 bfd_size_type size, offset;
92 lzma_stream_flags options;
93 gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
b181bbd0 94 lzma_index *index;
b181bbd0 95 uint64_t memlimit = UINT64_MAX;
1424c16e 96 struct gdb_lzma_stream *lstream;
b181bbd0
TT
97 size_t pos;
98
fd361982 99 size = bfd_section_size (section);
b181bbd0
TT
100 offset = section->filepos + size - LZMA_STREAM_HEADER_SIZE;
101 if (size < LZMA_STREAM_HEADER_SIZE
102 || bfd_seek (section->owner, offset, SEEK_SET) != 0
226f9f4f 103 || bfd_read (footer, LZMA_STREAM_HEADER_SIZE, section->owner)
dda83cd7 104 != LZMA_STREAM_HEADER_SIZE
b181bbd0
TT
105 || lzma_stream_footer_decode (&options, footer) != LZMA_OK
106 || offset < options.backward_size)
107 {
108 bfd_set_error (bfd_error_wrong_format);
109 return NULL;
110 }
111
112 offset -= options.backward_size;
e76d241a 113 gdb::byte_vector indexdata (options.backward_size);
b181bbd0
TT
114 index = NULL;
115 pos = 0;
116 if (bfd_seek (section->owner, offset, SEEK_SET) != 0
e76d241a 117 || bfd_read (indexdata.data (), options.backward_size, section->owner)
dda83cd7 118 != options.backward_size
b181bbd0 119 || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator,
e76d241a
TT
120 indexdata.data (), &pos,
121 options.backward_size)
dda83cd7 122 != LZMA_OK
b181bbd0
TT
123 || lzma_index_size (index) != options.backward_size)
124 {
b181bbd0
TT
125 bfd_set_error (bfd_error_wrong_format);
126 return NULL;
127 }
b181bbd0 128
e76d241a 129 lstream = new struct gdb_lzma_stream;
b181bbd0
TT
130 lstream->section = section;
131 lstream->index = index;
132
133 return lstream;
134}
135
d1f50898
TT
136/* bfd_openr_iovec read implementation for
137 find_separate_debug_file_in_section. */
b181bbd0 138
d1f50898
TT
139file_ptr
140gdb_lzma_stream::read (struct bfd *nbfd, void *buf, file_ptr nbytes,
141 file_ptr offset)
b181bbd0 142{
b181bbd0
TT
143 bfd_size_type chunk_size;
144 lzma_index_iter iter;
b181bbd0
TT
145 file_ptr block_offset;
146 lzma_filter filters[LZMA_FILTERS_MAX + 1];
147 lzma_block block;
148 size_t compressed_pos, uncompressed_pos;
149 file_ptr res;
150
151 res = 0;
152 while (nbytes > 0)
153 {
d1f50898 154 if (data.empty () || data_start > offset || offset >= data_end)
b181bbd0 155 {
d1f50898 156 lzma_index_iter_init (&iter, index);
b181bbd0
TT
157 if (lzma_index_iter_locate (&iter, offset))
158 break;
159
e76d241a 160 gdb::byte_vector compressed (iter.block.total_size);
b181bbd0
TT
161 block_offset = section->filepos + iter.block.compressed_file_offset;
162 if (bfd_seek (section->owner, block_offset, SEEK_SET) != 0
e76d241a
TT
163 || bfd_read (compressed.data (), iter.block.total_size,
164 section->owner) != iter.block.total_size)
165 break;
b181bbd0 166
e76d241a 167 gdb::byte_vector uncompressed (iter.block.uncompressed_size);
b181bbd0
TT
168
169 memset (&block, 0, sizeof (block));
170 block.filters = filters;
171 block.header_size = lzma_block_header_size_decode (compressed[0]);
e76d241a
TT
172 if (lzma_block_header_decode (&block, &gdb_lzma_allocator,
173 compressed.data ())
b181bbd0 174 != LZMA_OK)
e76d241a 175 break;
b181bbd0
TT
176
177 compressed_pos = block.header_size;
178 uncompressed_pos = 0;
179 if (lzma_block_buffer_decode (&block, &gdb_lzma_allocator,
e76d241a 180 compressed.data (), &compressed_pos,
b181bbd0 181 iter.block.total_size,
e76d241a
TT
182 uncompressed.data (),
183 &uncompressed_pos,
b181bbd0
TT
184 iter.block.uncompressed_size)
185 != LZMA_OK)
e76d241a 186 break;
b181bbd0 187
d1f50898
TT
188 data = std::move (uncompressed);
189 data_start = iter.block.uncompressed_file_offset;
190 data_end = (iter.block.uncompressed_file_offset
191 + iter.block.uncompressed_size);
b181bbd0
TT
192 }
193
d1f50898
TT
194 chunk_size = std::min (nbytes, (file_ptr) data_end - offset);
195 memcpy (buf, data.data () + offset - data_start, chunk_size);
b181bbd0
TT
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
d1f50898
TT
205/* bfd_openr_iovec stat implementation for
206 find_separate_debug_file_in_section. */
b181bbd0 207
d1f50898
TT
208int
209gdb_lzma_stream::stat (struct bfd *abfd, struct stat *sb)
b181bbd0 210{
326a5c7e 211 memset (sb, 0, sizeof (struct stat));
d1f50898 212 sb->st_size = lzma_index_uncompressed_size (index);
b181bbd0
TT
213 return 0;
214}
215
216#endif /* HAVE_LIBLZMA */
217
218/* This looks for a xz compressed separate debug info object file embedded
219 in a section called .gnu_debugdata. See
220 http://fedoraproject.org/wiki/Features/MiniDebugInfo
221 or the "Separate Debug Sections" of the manual for details.
222 If we find one we create a iovec based bfd that decompresses the
223 object data on demand. If we don't find one, return NULL. */
224
192b62ce 225gdb_bfd_ref_ptr
b181bbd0
TT
226find_separate_debug_file_in_section (struct objfile *objfile)
227{
228 asection *section;
192b62ce 229 gdb_bfd_ref_ptr abfd;
b181bbd0
TT
230
231 if (objfile->obfd == NULL)
232 return NULL;
233
98badbfd 234 section = bfd_get_section_by_name (objfile->obfd.get (), ".gnu_debugdata");
b181bbd0
TT
235 if (section == NULL)
236 return NULL;
237
238#ifdef HAVE_LIBLZMA
98badbfd 239 gdb_bfd_ref_ptr *shared = gnu_debug_key.get (objfile->obfd.get ());
a4a38eb4
TT
240 if (shared != nullptr)
241 return *shared;
242
921222e2
TT
243 std::string filename = string_printf (_(".gnu_debugdata for %s"),
244 objfile_name (objfile));
245
d1f50898
TT
246 auto open = [&] (bfd *nbfd) -> gdb_lzma_stream *
247 {
248 return lzma_open (nbfd, section);
249 };
250
251 abfd = gdb_bfd_openr_iovec (filename.c_str (), gnutarget, open);
b181bbd0
TT
252 if (abfd == NULL)
253 return NULL;
254
192b62ce 255 if (!bfd_check_format (abfd.get (), bfd_object))
b181bbd0
TT
256 {
257 warning (_("Cannot parse .gnu_debugdata section; not a BFD object"));
b181bbd0
TT
258 return NULL;
259 }
a4a38eb4 260
98badbfd 261 gnu_debug_key.emplace (objfile->obfd.get (), abfd);
a4a38eb4 262
b181bbd0
TT
263#else
264 warning (_("Cannot parse .gnu_debugdata section; LZMA support was "
265 "disabled at compile time"));
b181bbd0
TT
266#endif /* !HAVE_LIBLZMA */
267
268 return abfd;
269}