Convert minidebug to new type-safe gdb_bfd_openr_iovec

This converts the minidebug BFD iovec implementation to the new
type-safe gdb_bfd_openr_iovec.

Reviewed-By: Lancelot Six <lancelot.six@amd.com>
This commit is contained in:
Tom Tromey 2023-08-24 09:16:43 -06:00
parent dcc045272c
commit d1f50898e1

View File

@ -57,7 +57,7 @@ static lzma_allocator gdb_lzma_allocator = { alloc_lzma, free_lzma, NULL };
a section. This keeps only the last decompressed block in memory a section. This keeps only the last decompressed block in memory
to allow larger data without using to much memory. */ to allow larger data without using to much memory. */
struct gdb_lzma_stream struct gdb_lzma_stream : public gdb_bfd_iovec_base
{ {
/* Section of input BFD from which we are decoding data. */ /* Section of input BFD from which we are decoding data. */
asection *section = nullptr; asection *section = nullptr;
@ -69,19 +69,25 @@ struct gdb_lzma_stream
bfd_size_type data_start = 0; bfd_size_type data_start = 0;
bfd_size_type data_end = 0; bfd_size_type data_end = 0;
gdb::byte_vector data; gdb::byte_vector data;
~gdb_lzma_stream ()
{
lzma_index_end (index, &gdb_lzma_allocator);
}
file_ptr read (bfd *abfd, void *buffer, file_ptr nbytes,
file_ptr offset) override;
int stat (struct bfd *abfd, struct stat *sb) override;
}; };
/* bfd_openr_iovec OPEN_P implementation for /* bfd_openr_iovec implementation helper for
find_separate_debug_file_in_section. OPEN_CLOSURE is 'asection *' find_separate_debug_file_in_section. */
of the section to decompress.
Return 'struct gdb_lzma_stream *' must be freed by caller by delete, static gdb_lzma_stream *
together with its INDEX lzma data. */ lzma_open (struct bfd *nbfd, asection *section)
static void *
lzma_open (struct bfd *nbfd, void *open_closure)
{ {
asection *section = (asection *) open_closure;
bfd_size_type size, offset; bfd_size_type size, offset;
lzma_stream_flags options; lzma_stream_flags options;
gdb_byte footer[LZMA_STREAM_HEADER_SIZE]; gdb_byte footer[LZMA_STREAM_HEADER_SIZE];
@ -127,15 +133,13 @@ lzma_open (struct bfd *nbfd, void *open_closure)
return lstream; return lstream;
} }
/* bfd_openr_iovec PREAD_P implementation for /* bfd_openr_iovec read implementation for
find_separate_debug_file_in_section. Passed STREAM find_separate_debug_file_in_section. */
is 'struct gdb_lzma_stream *'. */
static file_ptr file_ptr
lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes, gdb_lzma_stream::read (struct bfd *nbfd, void *buf, file_ptr nbytes,
file_ptr offset) file_ptr offset)
{ {
struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
bfd_size_type chunk_size; bfd_size_type chunk_size;
lzma_index_iter iter; lzma_index_iter iter;
file_ptr block_offset; file_ptr block_offset;
@ -147,12 +151,9 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
res = 0; res = 0;
while (nbytes > 0) while (nbytes > 0)
{ {
if (lstream->data.empty () if (data.empty () || data_start > offset || offset >= data_end)
|| lstream->data_start > offset || offset >= lstream->data_end)
{ {
asection *section = lstream->section; lzma_index_iter_init (&iter, index);
lzma_index_iter_init (&iter, lstream->index);
if (lzma_index_iter_locate (&iter, offset)) if (lzma_index_iter_locate (&iter, offset))
break; break;
@ -184,15 +185,14 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
!= LZMA_OK) != LZMA_OK)
break; break;
lstream->data = std::move (uncompressed); data = std::move (uncompressed);
lstream->data_start = iter.block.uncompressed_file_offset; data_start = iter.block.uncompressed_file_offset;
lstream->data_end = (iter.block.uncompressed_file_offset data_end = (iter.block.uncompressed_file_offset
+ iter.block.uncompressed_size); + iter.block.uncompressed_size);
} }
chunk_size = std::min (nbytes, (file_ptr) lstream->data_end - offset); chunk_size = std::min (nbytes, (file_ptr) data_end - offset);
memcpy (buf, lstream->data.data () + offset - lstream->data_start, memcpy (buf, data.data () + offset - data_start, chunk_size);
chunk_size);
buf = (gdb_byte *) buf + chunk_size; buf = (gdb_byte *) buf + chunk_size;
offset += chunk_size; offset += chunk_size;
nbytes -= chunk_size; nbytes -= chunk_size;
@ -202,36 +202,14 @@ lzma_pread (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes,
return res; return res;
} }
/* bfd_openr_iovec CLOSE_P implementation for /* bfd_openr_iovec stat implementation for
find_separate_debug_file_in_section. Passed STREAM find_separate_debug_file_in_section. */
is 'struct gdb_lzma_stream *'. */
static int int
lzma_close (struct bfd *nbfd, gdb_lzma_stream::stat (struct bfd *abfd, struct stat *sb)
void *stream)
{ {
struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
lzma_index_end (lstream->index, &gdb_lzma_allocator);
delete lstream;
/* Zero means success. */
return 0;
}
/* bfd_openr_iovec STAT_P implementation for
find_separate_debug_file_in_section. Passed STREAM
is 'struct gdb_lzma_stream *'. */
static int
lzma_stat (struct bfd *abfd,
void *stream,
struct stat *sb)
{
struct gdb_lzma_stream *lstream = (struct gdb_lzma_stream *) stream;
memset (sb, 0, sizeof (struct stat)); memset (sb, 0, sizeof (struct stat));
sb->st_size = lzma_index_uncompressed_size (lstream->index); sb->st_size = lzma_index_uncompressed_size (index);
return 0; return 0;
} }
@ -265,8 +243,12 @@ find_separate_debug_file_in_section (struct objfile *objfile)
std::string filename = string_printf (_(".gnu_debugdata for %s"), std::string filename = string_printf (_(".gnu_debugdata for %s"),
objfile_name (objfile)); objfile_name (objfile));
abfd = gdb_bfd_openr_iovec (filename.c_str (), gnutarget, lzma_open, auto open = [&] (bfd *nbfd) -> gdb_lzma_stream *
section, lzma_pread, lzma_close, lzma_stat); {
return lzma_open (nbfd, section);
};
abfd = gdb_bfd_openr_iovec (filename.c_str (), gnutarget, open);
if (abfd == NULL) if (abfd == NULL)
return NULL; return NULL;