diff options
author | Alan Modra <amodra@gmail.com> | 2023-08-09 09:58:36 +0930 |
---|---|---|
committer | Alan Modra <amodra@gmail.com> | 2023-08-11 22:16:32 +0930 |
commit | 2db20b97f1dc3e5dce3d6ed74a8a62f0dede8c80 (patch) | |
tree | 255302b03f963939d9de59310a2e9c6dbe2ee86a /gdb/coff-pe-read.c | |
parent | 19c5c1bb327dbb5c88cd52571896c56296f5a308 (diff) | |
download | gdb-2db20b97f1dc3e5dce3d6ed74a8a62f0dede8c80.zip gdb-2db20b97f1dc3e5dce3d6ed74a8a62f0dede8c80.tar.gz gdb-2db20b97f1dc3e5dce3d6ed74a8a62f0dede8c80.tar.bz2 |
gdb: warn unused result for bfd IO functions
This fixes the compilation warnings introduced by my bfdio.c patch.
The removed bfd_seeks in coff_symfile_read date back to 1994, commit
7f4c859520, prior to which the file used stdio rather than bfd to read
symbols. Since it now uses bfd to read the file there should be no
need to synchronise to bfd's idea of the file position. I also fixed
a potential uninitialised memory access.
Approved-By: Andrew Burgess <aburgess@redhat.com>
Diffstat (limited to 'gdb/coff-pe-read.c')
-rw-r--r-- | gdb/coff-pe-read.c | 114 |
1 files changed, 73 insertions, 41 deletions
diff --git a/gdb/coff-pe-read.c b/gdb/coff-pe-read.c index b82b43c..0d76ebd 100644 --- a/gdb/coff-pe-read.c +++ b/gdb/coff-pe-read.c @@ -254,23 +254,31 @@ read_pe_truncate_name (char *dll_name) /* Low-level support functions, direct from the ld module pe-dll.c. */ static unsigned int -pe_get16 (bfd *abfd, int where) +pe_get16 (bfd *abfd, int where, bool *fail) { unsigned char b[2]; - bfd_seek (abfd, (file_ptr) where, SEEK_SET); - bfd_read (b, (bfd_size_type) 2, abfd); + if (bfd_seek (abfd, where, SEEK_SET) != 0 + || bfd_read (b, 2, abfd) != 2) + { + *fail = true; + return 0; + } return b[0] + (b[1] << 8); } static unsigned int -pe_get32 (bfd *abfd, int where) +pe_get32 (bfd *abfd, int where, bool *fail) { unsigned char b[4]; - bfd_seek (abfd, (file_ptr) where, SEEK_SET); - bfd_read (b, (bfd_size_type) 4, abfd); - return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24); + if (bfd_seek (abfd, where, SEEK_SET) != 0 + || bfd_read (b, 4, abfd) != 4) + { + *fail = true; + return 0; + } + return b[0] + (b[1] << 8) + (b[2] << 16) + ((unsigned) b[3] << 24); } static unsigned int @@ -286,7 +294,7 @@ pe_as32 (void *ptr) { unsigned char *b = (unsigned char *) ptr; - return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24); + return b[0] + (b[1] << 8) + (b[2] << 16) + ((unsigned) b[3] << 24); } /* Read the (non-debug) export symbol table from a portable @@ -335,37 +343,50 @@ read_pe_exported_syms (minimal_symbol_reader &reader, || strcmp (target, "pei-i386") == 0 || strcmp (target, "pe-arm-wince-little") == 0 || strcmp (target, "pei-arm-wince-little") == 0); + + /* Possibly print a debug message about DLL not having a valid format. */ + auto maybe_print_debug_msg = [&] () -> void { + if (debug_coff_pe_read) + gdb_printf (gdb_stdlog, _("%s doesn't appear to be a DLL\n"), + bfd_get_filename (dll)); + }; + if (!is_pe32 && !is_pe64) - { - /* This is not a recognized PE format file. Abort now, because - the code is untested on anything else. *FIXME* test on - further architectures and loosen or remove this test. */ - return; - } + return maybe_print_debug_msg (); /* Get pe_header, optional header and numbers of export entries. */ - pe_header_offset = pe_get32 (dll, 0x3c); + bool fail = false; + pe_header_offset = pe_get32 (dll, 0x3c, &fail); + if (fail) + return maybe_print_debug_msg (); opthdr_ofs = pe_header_offset + 4 + 20; if (is_pe64) - num_entries = pe_get32 (dll, opthdr_ofs + 108); + num_entries = pe_get32 (dll, opthdr_ofs + 108, &fail); else - num_entries = pe_get32 (dll, opthdr_ofs + 92); + num_entries = pe_get32 (dll, opthdr_ofs + 92, &fail); + if (fail) + return maybe_print_debug_msg (); if (num_entries < 1) /* No exports. */ return; if (is_pe64) { - export_opthdrrva = pe_get32 (dll, opthdr_ofs + 112); - export_opthdrsize = pe_get32 (dll, opthdr_ofs + 116); + export_opthdrrva = pe_get32 (dll, opthdr_ofs + 112, &fail); + export_opthdrsize = pe_get32 (dll, opthdr_ofs + 116, &fail); } else { - export_opthdrrva = pe_get32 (dll, opthdr_ofs + 96); - export_opthdrsize = pe_get32 (dll, opthdr_ofs + 100); + export_opthdrrva = pe_get32 (dll, opthdr_ofs + 96, &fail); + export_opthdrsize = pe_get32 (dll, opthdr_ofs + 100, &fail); } - nsections = pe_get16 (dll, pe_header_offset + 4 + 2); + if (fail) + return maybe_print_debug_msg (); + + nsections = pe_get16 (dll, pe_header_offset + 4 + 2, &fail); secptr = (pe_header_offset + 4 + 20 + - pe_get16 (dll, pe_header_offset + 4 + 16)); + pe_get16 (dll, pe_header_offset + 4 + 16, &fail)); + if (fail) + return maybe_print_debug_msg (); expptr = 0; export_size = 0; @@ -374,12 +395,13 @@ read_pe_exported_syms (minimal_symbol_reader &reader, { char sname[8]; unsigned long secptr1 = secptr + 40 * i; - unsigned long vaddr = pe_get32 (dll, secptr1 + 12); - unsigned long vsize = pe_get32 (dll, secptr1 + 16); - unsigned long fptr = pe_get32 (dll, secptr1 + 20); + unsigned long vaddr = pe_get32 (dll, secptr1 + 12, &fail); + unsigned long vsize = pe_get32 (dll, secptr1 + 16, &fail); + unsigned long fptr = pe_get32 (dll, secptr1 + 20, &fail); - bfd_seek (dll, (file_ptr) secptr1, SEEK_SET); - bfd_read (sname, (bfd_size_type) sizeof (sname), dll); + if (fail + || bfd_seek (dll, secptr1, SEEK_SET) != 0 + || bfd_read (sname, sizeof (sname), dll) != sizeof (sname)) if ((strcmp (sname, ".edata") == 0) || (vaddr <= export_opthdrrva && export_opthdrrva < vaddr + vsize)) @@ -420,16 +442,18 @@ read_pe_exported_syms (minimal_symbol_reader &reader, for (i = 0; i < nsections; i++) { unsigned long secptr1 = secptr + 40 * i; - unsigned long vsize = pe_get32 (dll, secptr1 + 8); - unsigned long vaddr = pe_get32 (dll, secptr1 + 12); - unsigned long characteristics = pe_get32 (dll, secptr1 + 36); + unsigned long vsize = pe_get32 (dll, secptr1 + 8, &fail); + unsigned long vaddr = pe_get32 (dll, secptr1 + 12, &fail); + unsigned long characteristics = pe_get32 (dll, secptr1 + 36, &fail); char sec_name[SCNNMLEN + 1]; int sectix; unsigned int bfd_section_index; asection *section; - bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET); - bfd_read (sec_name, (bfd_size_type) SCNNMLEN, dll); + if (fail + || bfd_seek (dll, secptr1 + 0, SEEK_SET) != 0 + || bfd_read (sec_name, SCNNMLEN, dll) != SCNNMLEN) + return maybe_print_debug_msg (); sec_name[SCNNMLEN] = '\0'; sectix = read_pe_section_index (sec_name); @@ -468,8 +492,9 @@ read_pe_exported_syms (minimal_symbol_reader &reader, gdb::def_vector<unsigned char> expdata_storage (export_size); expdata = expdata_storage.data (); - bfd_seek (dll, (file_ptr) expptr, SEEK_SET); - bfd_read (expdata, (bfd_size_type) export_size, dll); + if (bfd_seek (dll, expptr, SEEK_SET) != 0 + || bfd_read (expdata, export_size, dll) != export_size) + return maybe_print_debug_msg (); erva = expdata - export_rva; nexp = pe_as32 (expdata + 24); @@ -626,20 +651,27 @@ pe_text_section_offset (struct bfd *abfd) } /* Get pe_header, optional header and numbers of sections. */ - pe_header_offset = pe_get32 (abfd, 0x3c); - nsections = pe_get16 (abfd, pe_header_offset + 4 + 2); + bool fail = false; + pe_header_offset = pe_get32 (abfd, 0x3c, &fail); + if (fail) + return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET; + nsections = pe_get16 (abfd, pe_header_offset + 4 + 2, &fail); secptr = (pe_header_offset + 4 + 20 + - pe_get16 (abfd, pe_header_offset + 4 + 16)); + pe_get16 (abfd, pe_header_offset + 4 + 16, &fail)); + if (fail) + return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET; /* Get the rva and size of the export section. */ for (i = 0; i < nsections; i++) { char sname[SCNNMLEN + 1]; unsigned long secptr1 = secptr + 40 * i; - unsigned long vaddr = pe_get32 (abfd, secptr1 + 12); + unsigned long vaddr = pe_get32 (abfd, secptr1 + 12, &fail); - bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET); - bfd_read (sname, (bfd_size_type) SCNNMLEN, abfd); + if (fail + || bfd_seek (abfd, secptr1, SEEK_SET) != 0 + || bfd_read (sname, SCNNMLEN, abfd) != SCNNMLEN) + return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET; sname[SCNNMLEN] = '\0'; if (strcmp (sname, ".text") == 0) return vaddr; |