elfcore_grok_freebsd_note: Remove checks of note->namesz.
[binutils-gdb.git] / bfd / elf-strtab.c
index 55e138a6319cb105a8a2482aa56bc72678201bf5..ea30a6e638d3b864f68e030db55b3b7edd0fdafc 100644 (file)
@@ -1,5 +1,5 @@
 /* ELF strtab with GC and suffix merging support.
-   Copyright (C) 2001-2019 Free Software Foundation, Inc.
+   Copyright (C) 2001-2022 Free Software Foundation, Inc.
    Written by Jakub Jelinek <jakub@redhat.com>.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -95,7 +95,7 @@ struct elf_strtab_hash *
 _bfd_elf_strtab_init (void)
 {
   struct elf_strtab_hash *table;
-  bfd_size_type amt = sizeof (struct elf_strtab_hash);
+  size_t amt = sizeof (struct elf_strtab_hash);
 
   table = (struct elf_strtab_hash *) bfd_malloc (amt);
   if (table == NULL)
@@ -141,7 +141,7 @@ _bfd_elf_strtab_free (struct elf_strtab_hash *tab)
 size_t
 _bfd_elf_strtab_add (struct elf_strtab_hash *tab,
                     const char *str,
-                    bfd_boolean copy)
+                    bool copy)
 {
   register struct elf_strtab_hash_entry *entry;
 
@@ -152,7 +152,7 @@ _bfd_elf_strtab_add (struct elf_strtab_hash *tab,
 
   BFD_ASSERT (tab->sec_size == 0);
   entry = (struct elf_strtab_hash_entry *)
-         bfd_hash_lookup (&tab->table, str, TRUE, copy);
+         bfd_hash_lookup (&tab->table, str, true, copy);
 
   if (entry == NULL)
     return (size_t) -1;
@@ -245,13 +245,16 @@ _bfd_elf_strtab_save (struct elf_strtab_hash *tab)
 void
 _bfd_elf_strtab_restore (struct elf_strtab_hash *tab, void *buf)
 {
-  size_t idx, curr_size = tab->size;
+  size_t idx, curr_size = tab->size, save_size;
   struct strtab_save *save = (struct strtab_save *) buf;
 
   BFD_ASSERT (tab->sec_size == 0);
-  BFD_ASSERT (save->size <= curr_size);
-  tab->size = save->size;
-  for (idx = 1; idx < save->size; ++idx)
+  save_size = 1;
+  if (save != NULL)
+    save_size = save->size;
+  BFD_ASSERT (save_size <= curr_size);
+  tab->size = save_size;
+  for (idx = 1; idx < save_size; ++idx)
     tab->array[idx]->refcount = save->refcount[idx];
 
   for (; idx < curr_size; ++idx)
@@ -296,22 +299,24 @@ _bfd_elf_strtab_str (struct elf_strtab_hash *tab, size_t idx,
                     bfd_size_type *offset)
 {
   if (idx == 0)
-    return 0;
+    return NULL;
   BFD_ASSERT (idx < tab->size);
   BFD_ASSERT (tab->sec_size);
+  if (tab->array[idx]->refcount == 0)
+    return NULL;
   if (offset)
     *offset = tab->array[idx]->u.index;
   return tab->array[idx]->root.string;
 }
 
-bfd_boolean
+bool
 _bfd_elf_strtab_emit (register bfd *abfd, struct elf_strtab_hash *tab)
 {
   bfd_size_type off = 1;
   size_t i;
 
   if (bfd_bwrite ("", 1, abfd) != 1)
-    return FALSE;
+    return false;
 
   for (i = 1; i < tab->size; ++i)
     {
@@ -325,13 +330,13 @@ _bfd_elf_strtab_emit (register bfd *abfd, struct elf_strtab_hash *tab)
 
       str = tab->array[i]->root.string;
       if (bfd_bwrite (str, len, abfd) != len)
-       return FALSE;
+       return false;
 
       off += len;
     }
 
   BFD_ASSERT (off == tab->sec_size);
-  return TRUE;
+  return true;
 }
 
 /* Compare two elf_strtab_hash_entry structures.  Called via qsort.
@@ -439,9 +444,8 @@ _bfd_elf_strtab_finalize (struct elf_strtab_hash *tab)
        }
     }
 
-alloc_failure:
-  if (array)
-    free (array);
+ alloc_failure:
+  free (array);
 
   /* Assign positions to the strings we want to keep.  */
   sec_size = 1;