]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
stab nearest_line bfd_malloc_and_get_section
authorAlan Modra <amodra@gmail.com>
Sat, 24 Sep 2022 08:44:14 +0000 (18:14 +0930)
committerAlan Modra <amodra@gmail.com>
Mon, 26 Sep 2022 04:53:29 +0000 (14:23 +0930)
bfd_malloc_and_get_section performs some sanity checks on the section
size before allocating memory.  This patch avails the stab
nearest_line code of that sanity checking, and tidies up memory
afterward.

* coffgen.c (_bfd_coff_close_and_cleanup): Call _bfd_stab_cleanup.
* elf.c (_bfd_elf_close_and_cleanup): Likewise.
* syms.c (_bfd_stab_section_find_nearest_line): Set *pinfo earlier.
Use bfd_malloc_and_get_section.  Free malloc'd buffers on failure.
Malloc indextable.
(_bfd_stab_cleanup): New function.
* libbfd-in.h (_bfd_stab_cleanup): Declare.
* libbfd.h: Regnerate.

bfd/coffgen.c
bfd/elf.c
bfd/libbfd-in.h
bfd/libbfd.h
bfd/syms.c

index 8eb69af2ce2bc95ac38c6b5f7f3cbdfe9afcd4d1..3e0fbc6184f513c1b4a4ca31794bd1ccba3f7dd9 100644 (file)
@@ -3175,7 +3175,10 @@ _bfd_coff_close_and_cleanup (bfd *abfd)
 
       if (bfd_get_format (abfd) == bfd_object
          || bfd_get_format (abfd) == bfd_core)
-       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
+       {
+         _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
+         _bfd_stab_cleanup (abfd, &tdata->line_info);
+       }
     }
   return _bfd_generic_close_and_cleanup (abfd);
 }
index 16cea4f8aeb0e352d4d01181161b66da35c382b6..9a0bc1d27407bf5d6ca55d198424543b20806ae3 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -9649,6 +9649,7 @@ _bfd_elf_close_and_cleanup (bfd *abfd)
       if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL)
        _bfd_elf_strtab_free (elf_shstrtab (abfd));
       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
+      _bfd_stab_cleanup (abfd, &tdata->line_info);
     }
 
   return _bfd_generic_close_and_cleanup (abfd);
index 04f343b051b9e3341698973ffb0e9be16ea3e66f..6d9d15f34093732b452b7710b3a530807b49a9ff 100644 (file)
@@ -615,6 +615,9 @@ extern bool _bfd_dwarf2_slurp_debug_info
 extern void _bfd_dwarf2_cleanup_debug_info
   (bfd *, void **) ATTRIBUTE_HIDDEN;
 
+extern void _bfd_stab_cleanup
+  (bfd *, void **) ATTRIBUTE_HIDDEN;
+
 /* Create a new section entry.  */
 extern struct bfd_hash_entry *bfd_section_hash_newfunc
   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *)
index a17b98e8e30ecc01c6b3146b7aee7166652aa8ef..a67bded0d18419c4476395c13a509ed16a81036e 100644 (file)
@@ -620,6 +620,9 @@ extern bool _bfd_dwarf2_slurp_debug_info
 extern void _bfd_dwarf2_cleanup_debug_info
   (bfd *, void **) ATTRIBUTE_HIDDEN;
 
+extern void _bfd_stab_cleanup
+  (bfd *, void **) ATTRIBUTE_HIDDEN;
+
 /* Create a new section entry.  */
 extern struct bfd_hash_entry *bfd_section_hash_newfunc
   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *)
index 7a454c5d7e7cd9e431df6617b8a046fa5a4751ed..ec62f3457ca3451b60c7d8d7fe463a1a784a6678 100644 (file)
@@ -978,7 +978,7 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
     {
       if (info->stabsec == NULL || info->strsec == NULL)
        {
-         /* No stabs debugging information.  */
+         /* No usable stabs debugging information.  */
          return true;
        }
 
@@ -1000,6 +1000,7 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
       info = (struct stab_find_info *) bfd_zalloc (abfd, amt);
       if (info == NULL)
        return false;
+      *pinfo = info;
 
       /* FIXME: When using the linker --split-by-file or
         --split-by-reloc options, it is possible for the .stab and
@@ -1015,12 +1016,7 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
          info->strsec  = bfd_get_section_by_name (abfd, "$GDB_STRINGS$");
 
          if (info->stabsec == NULL || info->strsec == NULL)
-           {
-             /* No stabs debugging information.  Set *pinfo so that we
-                can return quickly in the info != NULL case above.  */
-             *pinfo = info;
-             return true;
-           }
+           return true;
        }
 
       stabsize = (info->stabsec->rawsize
@@ -1031,16 +1027,10 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
                 ? info->strsec->rawsize
                 : info->strsec->size);
 
-      info->stabs = (bfd_byte *) bfd_alloc (abfd, stabsize);
-      info->strs = (bfd_byte *) bfd_alloc (abfd, strsize);
-      if (info->stabs == NULL || info->strs == NULL)
-       return false;
-
-      if (! bfd_get_section_contents (abfd, info->stabsec, info->stabs,
-                                     0, stabsize)
-         || ! bfd_get_section_contents (abfd, info->strsec, info->strs,
-                                        0, strsize))
-       return false;
+      if (!bfd_malloc_and_get_section (abfd, info->stabsec, &info->stabs))
+       goto out;
+      if (!bfd_malloc_and_get_section (abfd, info->strsec, &info->strs))
+       goto out1;
 
       /* Stab strings ought to be nul terminated.  Ensure the last one
         is, to prevent running off the end of the buffer.  */
@@ -1052,15 +1042,24 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
         this should be no big deal.  */
       reloc_size = bfd_get_reloc_upper_bound (abfd, info->stabsec);
       if (reloc_size < 0)
-       return false;
+       goto out2;
       reloc_vector = (arelent **) bfd_malloc (reloc_size);
       if (reloc_vector == NULL && reloc_size != 0)
-       return false;
+       goto out2;
       reloc_count = bfd_canonicalize_reloc (abfd, info->stabsec, reloc_vector,
                                            symbols);
       if (reloc_count < 0)
        {
+       out3:
          free (reloc_vector);
+       out2:
+         free (info->strs);
+         info->strs = NULL;
+       out1:
+         free (info->stabs);
+         info->stabs = NULL;
+       out:
+         info->stabsec = NULL;
          return false;
        }
       if (reloc_count > 0)
@@ -1091,8 +1090,7 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
                  _bfd_error_handler
                    (_("unsupported .stab relocation"));
                  bfd_set_error (bfd_error_invalid_operation);
-                 free (reloc_vector);
-                 return false;
+                 goto out3;
                }
 
              val = bfd_get_32 (abfd, info->stabs + octets);
@@ -1146,14 +1144,21 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
        ++info->indextablesize;
 
       if (info->indextablesize == 0)
-       return true;
+       {
+         free (info->strs);
+         info->strs = NULL;
+         free (info->stabs);
+         info->stabs = NULL;
+         info->stabsec = NULL;
+         return true;
+       }
       ++info->indextablesize;
 
       amt = info->indextablesize;
       amt *= sizeof (struct indexentry);
-      info->indextable = (struct indexentry *) bfd_alloc (abfd, amt);
+      info->indextable = (struct indexentry *) bfd_malloc (amt);
       if (info->indextable == NULL)
-       return false;
+       goto out3;
 
       file_name = NULL;
       directory_name = NULL;
@@ -1280,8 +1285,6 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
       info->indextablesize = i;
       qsort (info->indextable, (size_t) i, sizeof (struct indexentry),
             cmpindexentry);
-
-      *pinfo = info;
     }
 
   /* We are passed a section relative offset.  The offsets in the
@@ -1446,6 +1449,18 @@ _bfd_stab_section_find_nearest_line (bfd *abfd,
   return true;
 }
 
+void
+_bfd_stab_cleanup (bfd *abfd ATTRIBUTE_UNUSED, void **pinfo)
+{
+  struct stab_find_info *info = (struct stab_find_info *) *pinfo;
+  if (info == NULL)
+    return;
+
+  free (info->indextable);
+  free (info->strs);
+  free (info->stabs);
+}
+
 long
 _bfd_nosymbols_canonicalize_symtab (bfd *abfd ATTRIBUTE_UNUSED,
                                    asymbol **location ATTRIBUTE_UNUSED)