38alloc_lzma (
void *opaque,
size_t nmemb,
size_t size)
46free_lzma (
void *opaque,
void *ptr)
54static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };
69 bfd_size_type data_start;
70 bfd_size_type data_end;
82lzma_open (
struct bfd *nbfd,
void *open_closure)
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];
90 uint64_t memlimit = UINT64_MAX;
91 struct gdb_lzma_stream *lstream;
94 size = bfd_section_size (section);
95 offset = section->filepos +
size - LZMA_STREAM_HEADER_SIZE;
96 if (
size < LZMA_STREAM_HEADER_SIZE
97 || bfd_seek (section->owner, offset,
SEEK_SET) != 0
98 || bfd_bread (footer, LZMA_STREAM_HEADER_SIZE, section->owner)
99 != LZMA_STREAM_HEADER_SIZE
100 || lzma_stream_footer_decode (&options, footer) != LZMA_OK
101 || offset < options.backward_size)
103 bfd_set_error (bfd_error_wrong_format);
107 offset -= options.backward_size;
108 indexdata = (gdb_byte *)
xmalloc (options.backward_size);
111 if (bfd_seek (section->owner, offset,
SEEK_SET) != 0
112 || bfd_bread (indexdata, options.backward_size, section->owner)
113 != options.backward_size
114 || lzma_index_buffer_decode (&index, &memlimit, &gdb_lzma_allocator,
115 indexdata, &pos, options.backward_size)
117 || lzma_index_size (index) != options.backward_size)
120 bfd_set_error (bfd_error_wrong_format);
125 lstream = XCNEW (
struct gdb_lzma_stream);
126 lstream->section = section;
127 lstream->index = index;
137lzma_pread (
struct bfd *nbfd,
void *stream,
void *buf, file_ptr nbytes,
140 struct gdb_lzma_stream *lstream = (
struct gdb_lzma_stream *) stream;
141 bfd_size_type chunk_size;
142 lzma_index_iter iter;
143 gdb_byte *compressed, *uncompressed;
144 file_ptr block_offset;
145 lzma_filter filters[LZMA_FILTERS_MAX + 1];
147 size_t compressed_pos, uncompressed_pos;
153 if (lstream->data == NULL
154 || lstream->data_start > offset || offset >= lstream->data_end)
156 asection *section = lstream->section;
158 lzma_index_iter_init (&iter, lstream->index);
159 if (lzma_index_iter_locate (&iter, offset))
162 compressed = (gdb_byte *)
xmalloc (iter.block.total_size);
163 block_offset = section->filepos + iter.block.compressed_file_offset;
164 if (bfd_seek (section->owner, block_offset,
SEEK_SET) != 0
165 || bfd_bread (compressed, iter.block.total_size, section->owner)
166 != iter.block.total_size)
172 uncompressed = (gdb_byte *)
xmalloc (iter.block.uncompressed_size);
175 block.filters = filters;
176 block.header_size = lzma_block_header_size_decode (compressed[0]);
177 if (lzma_block_header_decode (&
block, &gdb_lzma_allocator, compressed)
181 xfree (uncompressed);
185 compressed_pos =
block.header_size;
186 uncompressed_pos = 0;
187 if (lzma_block_buffer_decode (&
block, &gdb_lzma_allocator,
188 compressed, &compressed_pos,
189 iter.block.total_size,
190 uncompressed, &uncompressed_pos,
191 iter.block.uncompressed_size)
195 xfree (uncompressed);
201 xfree (lstream->data);
202 lstream->data = uncompressed;
203 lstream->data_start = iter.block.uncompressed_file_offset;
204 lstream->data_end = (iter.block.uncompressed_file_offset
205 + iter.block.uncompressed_size);
208 chunk_size = std::min (nbytes, (file_ptr) lstream->data_end - offset);
209 memcpy (buf, lstream->data + offset - lstream->data_start, chunk_size);
210 buf = (gdb_byte *) buf + chunk_size;
211 offset += chunk_size;
212 nbytes -= chunk_size;
224lzma_close (
struct bfd *nbfd,
227 struct gdb_lzma_stream *lstream = (
struct gdb_lzma_stream *) stream;
229 lzma_index_end (lstream->index, &gdb_lzma_allocator);
230 xfree (lstream->data);
242lzma_stat (
struct bfd *abfd,
246 struct gdb_lzma_stream *lstream = (
struct gdb_lzma_stream *) stream;
248 memset (sb, 0,
sizeof (
struct stat));
249 sb->st_size = lzma_index_uncompressed_size (lstream->index);
271 section = bfd_get_section_by_name (
objfile->
obfd.get (),
".gnu_debugdata");
277 if (shared !=
nullptr)
280 std::string filename = string_printf (_(
".gnu_debugdata for %s"),
284 section, lzma_pread, lzma_close, lzma_stat);
288 if (!bfd_check_format (abfd.get (), bfd_object))
290 warning (_(
"Cannot parse .gnu_debugdata section; not a BFD object"));
294 gnu_debug_key.emplace (
objfile->
obfd.get (), abfd);
297 warning (_(
"Cannot parse .gnu_debugdata section; LZMA support was "
298 "disabled at compile time"));
gdb_bfd_ref_ptr gdb_bfd_openr_iovec(const char *filename, const char *target, void *(*open_func)(struct bfd *nbfd, void *open_closure), void *open_closure, file_ptr(*pread_func)(struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes, file_ptr offset), int(*close_func)(struct bfd *nbfd, void *stream), int(*stat_func)(struct bfd *abfd, void *stream, struct stat *sb))
gdb::ref_ptr< struct bfd, gdb_bfd_ref_policy > gdb_bfd_ref_ptr
gdb_bfd_ref_ptr find_separate_debug_file_in_section(struct objfile *objfile)
const char * objfile_name(const struct objfile *objfile)