Add startswith function and use it instead of CONST_STRNEQ.
[deliverable/binutils-gdb.git] / bfd / coffgen.c
index 96140e0ad278d9f7559eaa3e4432bbace5c92231..f493978815e0d91a94e5c45baeba2cacb37a9cc0 100644 (file)
@@ -1,5 +1,5 @@
 /* Support for the generic parts of COFF, for BFD.
-   Copyright (C) 1990-2020 Free Software Foundation, Inc.
+   Copyright (C) 1990-2021 Free Software Foundation, Inc.
    Written by Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -544,11 +544,8 @@ _bfd_coff_read_internal_relocs (bfd *abfd,
   for (; erel < erel_end; erel += relsz, irel++)
     bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
 
-  if (free_external != NULL)
-    {
-      free (free_external);
-      free_external = NULL;
-    }
+  free (free_external);
+  free_external = NULL;
 
   if (cache && free_internal != NULL)
     {
@@ -566,10 +563,8 @@ _bfd_coff_read_internal_relocs (bfd *abfd,
   return internal_relocs;
 
  error_return:
-  if (free_external != NULL)
-    free (free_external);
-  if (free_internal != NULL)
-    free (free_internal);
+  free (free_external);
+  free (free_internal);
   return NULL;
 }
 
@@ -1819,7 +1814,7 @@ coff_get_normalized_symtab (bfd *abfd)
       internal_ptr->is_sym = TRUE;
 
       /* PR 17512: Prevent buffer overrun.  */
-      if (symbol_ptr->u.syment.n_numaux > (raw_end - raw_src) / symesz)
+      if (symbol_ptr->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz)
        {
          bfd_release (abfd, internal);
          return NULL;
@@ -2440,11 +2435,15 @@ coff_find_nearest_line_with_names (bfd *abfd,
 
                  /* In XCOFF a debugging symbol can follow the
                     function symbol.  */
-                 if (s->u.syment.n_scnum == N_DEBUG)
+                 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
+                      < obj_raw_syment_count (abfd) * sizeof (*s))
+                     && s->u.syment.n_scnum == N_DEBUG)
                    s = s + 1 + s->u.syment.n_numaux;
 
                  /* S should now point to the .bf of the function.  */
-                 if (s->u.syment.n_numaux)
+                 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd))
+                      < obj_raw_syment_count (abfd) * sizeof (*s))
+                     && s->u.syment.n_numaux)
                    {
                      /* The linenumber is stored in the auxent.  */
                      union internal_auxent *a = &((s + 1)->u.auxent);
@@ -2636,7 +2635,7 @@ _bfd_coff_section_already_linked (bfd *abfd,
     key = s_comdat->name;
   else
     {
-      if (CONST_STRNEQ (name, ".gnu.linkonce.")
+      if (startswith (name, ".gnu.linkonce.")
          && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
        key++;
       else
@@ -2663,7 +2662,8 @@ _bfd_coff_section_already_linked (bfd *abfd,
         .gnu.linkonce.*.<key>.  */
       if (((s_comdat != NULL) == (l_comdat != NULL)
           && strcmp (name, l->sec->name) == 0)
-         || (l->sec->owner->flags & BFD_PLUGIN) != 0)
+         || (l->sec->owner->flags & BFD_PLUGIN) != 0
+         || (sec->owner->flags & BFD_PLUGIN) != 0)
        {
          /* The section has already been linked.  See if we should
             issue a warning.  */
@@ -2995,10 +2995,10 @@ coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
          if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
              || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
            o->gc_mark = 1;
-         else if (CONST_STRNEQ (o->name, ".idata")
-                  || CONST_STRNEQ (o->name, ".pdata")
-                  || CONST_STRNEQ (o->name, ".xdata")
-                  || CONST_STRNEQ (o->name, ".rsrc"))
+         else if (startswith (o->name, ".idata")
+                  || startswith (o->name, ".pdata")
+                  || startswith (o->name, ".xdata")
+                  || startswith (o->name, ".rsrc"))
            o->gc_mark = 1;
 
          if (o->gc_mark)
@@ -3109,9 +3109,9 @@ bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
       for (o = sub->sections; o != NULL; o = o->next)
        {
          if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
-              || CONST_STRNEQ (o->name, ".vectors")
-              || CONST_STRNEQ (o->name, ".ctors")
-              || CONST_STRNEQ (o->name, ".dtors"))
+              || startswith (o->name, ".vectors")
+              || startswith (o->name, ".ctors")
+              || startswith (o->name, ".dtors"))
              && !o->gc_mark)
            {
              if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
@@ -3141,16 +3141,22 @@ bfd_coff_group_name (bfd *abfd, const asection *sec)
 bfd_boolean
 _bfd_coff_close_and_cleanup (bfd *abfd)
 {
-  if (abfd->format == bfd_object
-      && bfd_family_coff (abfd)
-      && coff_data (abfd) != NULL)
+  struct coff_tdata *tdata = coff_data (abfd);
+
+  if (tdata != NULL)
     {
       /* PR 25447:
         Do not clear the keep_syms and keep_strings flags.
         These may have been set by pe_ILF_build_a_bfd() indicating
         that the syms and strings pointers are not to be freed.  */
-      if (!_bfd_coff_free_symbols (abfd))
+      if (bfd_get_format (abfd) == bfd_object
+         && bfd_family_coff (abfd)
+         && !_bfd_coff_free_symbols (abfd))
        return FALSE;
+
+      if (bfd_get_format (abfd) == bfd_object
+         || bfd_get_format (abfd) == bfd_core)
+       _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info);
     }
   return _bfd_generic_close_and_cleanup (abfd);
 }
This page took 0.028851 seconds and 4 git commands to generate.