Use comp_unit_maybe_decode_line_info in more places
[binutils-gdb.git] / bfd / dwarf2.c
index 0f8257f6015e5eb07b114d26d7a6a74b66d57d8f..65bb1e4e32d2ae552435d9f9aef49bc8bbce9dd7 100644 (file)
@@ -1,5 +1,5 @@
 /* DWARF 2 support.
-   Copyright (C) 1994-2018 Free Software Foundation, Inc.
+   Copyright (C) 1994-2019 Free Software Foundation, Inc.
 
    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
    (gavin@cygnus.com).
@@ -169,6 +169,8 @@ struct dwarf2_debug
 
   /* Section VMAs at the time the stash was built.  */
   bfd_vma *sec_vma;
+  /* Number of sections in the SEC_VMA table.  */
+  unsigned int sec_vma_count;
 
   /* Number of sections whose VMA we must adjust.  */
   int adjusted_section_count;
@@ -345,7 +347,7 @@ const struct dwarf_debug_section dwarf_debug_sections[] =
   { NULL,                      NULL },
 };
 
-/* NB/ Numbers in this enum must match up with indicies
+/* NB/ Numbers in this enum must match up with indices
    into the dwarf_debug_sections[] array above.  */
 enum dwarf_debug_section_enum
 {
@@ -527,6 +529,7 @@ read_section (bfd *       abfd,
   asection *msec;
   const char *section_name = sec->uncompressed_name;
   bfd_byte *contents = *section_buffer;
+  bfd_size_type amt;
 
   /* The section may have already been read.  */
   if (contents == NULL)
@@ -549,7 +552,13 @@ read_section (bfd *              abfd,
       *section_size = msec->rawsize ? msec->rawsize : msec->size;
       /* Paranoia - alloc one extra so that we can make sure a string
         section is NUL terminated.  */
-      contents = (bfd_byte *) bfd_malloc (*section_size + 1);
+      amt = *section_size + 1;
+      if (amt == 0)
+       {
+         bfd_set_error (bfd_error_no_memory);
+         return FALSE;
+       }
+      contents = (bfd_byte *) bfd_malloc (amt);
       if (contents == NULL)
        return FALSE;
       if (syms
@@ -2795,6 +2804,9 @@ lookup_symbol_in_variable_table (struct comp_unit *unit,
   return FALSE;
 }
 
+static bfd_boolean comp_unit_maybe_decode_line_info (struct comp_unit *,
+                                                    struct dwarf2_debug *);
+
 static bfd_boolean
 find_abstract_instance (struct comp_unit *   unit,
                        bfd_byte *           orig_info_ptr,
@@ -2837,7 +2849,9 @@ find_abstract_instance (struct comp_unit *   unit,
       info_ptr = unit->stash->info_ptr_memory;
       info_ptr_end = unit->stash->info_ptr_end;
       total = info_ptr_end - info_ptr;
-      if (!die_ref || die_ref >= total)
+      if (!die_ref)
+       return TRUE;
+      else if (die_ref >= total)
        {
          _bfd_error_handler
            (_("DWARF error: invalid abstract instance DIE ref"));
@@ -2953,7 +2967,7 @@ find_abstract_instance (struct comp_unit *   unit,
                  break;
                case DW_AT_specification:
                  if (!find_abstract_instance (unit, info_ptr, &attr,
-                                              pname, is_linkage,
+                                              &name, is_linkage,
                                               filename_ptr, linenumber_ptr))
                    return FALSE;
                  break;
@@ -3593,33 +3607,9 @@ comp_unit_find_nearest_line (struct comp_unit *unit,
 {
   bfd_boolean func_p;
 
-  if (unit->error)
+  if (!comp_unit_maybe_decode_line_info (unit, stash))
     return FALSE;
 
-  if (! unit->line_table)
-    {
-      if (! unit->stmtlist)
-       {
-         unit->error = 1;
-         return FALSE;
-       }
-
-      unit->line_table = decode_line_info (unit, stash);
-
-      if (! unit->line_table)
-       {
-         unit->error = 1;
-         return FALSE;
-       }
-
-      if (unit->first_child_die_ptr < unit->end_ptr
-         && ! scan_unit_for_symbols (unit))
-       {
-         unit->error = 1;
-         return FALSE;
-       }
-    }
-
   *function_ptr = NULL;
   func_p = lookup_address_in_function_table (unit, addr, function_ptr);
   if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
@@ -4260,7 +4250,10 @@ save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
   stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
   if (stash->sec_vma == NULL)
     return FALSE;
-  for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
+  stash->sec_vma_count = abfd->section_count;
+  for (i = 0, s = abfd->sections;
+       s != NULL && i < abfd->section_count;
+       i++, s = s->next)
     {
       if (s->output_section != NULL)
        stash->sec_vma[i] = s->output_section->vma + s->output_offset;
@@ -4283,7 +4276,15 @@ section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
   asection *s;
   unsigned int i;
 
-  for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
+  /* PR 24334: If the number of sections in ABFD has changed between
+     when the stash was created and now, then we cannot trust the
+     stashed vma information.  */
+  if (abfd->section_count != stash->sec_vma_count)
+    return FALSE;
+
+  for (i = 0, s = abfd->sections;
+       s != NULL && i < abfd->section_count;
+       i++, s = s->next)
     {
       bfd_vma vma;
 
@@ -4463,20 +4464,14 @@ _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
 
   stash = (struct dwarf2_debug *) *pinfo;
 
-  if (stash == NULL)
+  if (stash == NULL || symbols == NULL)
     return 0;
 
   for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
     {
       struct funcinfo * func;
 
-      if (unit->function_table == NULL)
-       {
-         if (unit->line_table == NULL)
-           unit->line_table = decode_line_info (unit, stash);
-         if (unit->line_table != NULL)
-           scan_unit_for_symbols (unit);
-       }
+      comp_unit_maybe_decode_line_info (unit, stash);
 
       for (func = unit->function_table; func != NULL; func = func->prev_func)
        if (func->name && func->arange.low)
@@ -4509,7 +4504,6 @@ _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
    NULL the FUNCTIONNAME_PTR is also filled in.
    SYMBOLS contains the symbol table for ABFD.
    DEBUG_SECTIONS contains the name of the dwarf debug sections.
-   ADDR_SIZE is the number of bytes in the initial .debug_info length
    field and in the abbreviation offset, or zero to indicate that the
    default value should be used.  */
 
@@ -4524,7 +4518,6 @@ _bfd_dwarf2_find_nearest_line (bfd *abfd,
                               unsigned int *linenumber_ptr,
                               unsigned int *discriminator_ptr,
                               const struct dwarf_debug_section *debug_sections,
-                              unsigned int addr_size,
                               void **pinfo)
 {
   /* Read each compilation unit from the section .debug_info, and check
@@ -4705,18 +4698,11 @@ _bfd_dwarf2_find_nearest_line (bfd *abfd,
        }
     }
 
-  /* The DWARF2 spec says that the initial length field, and the
-     offset of the abbreviation table, should both be 4-byte values.
-     However, some compilers do things differently.  */
-  if (addr_size == 0)
-    addr_size = 4;
-  BFD_ASSERT (addr_size == 4 || addr_size == 8);
-
   /* Read each remaining comp. units checking each as they are read.  */
   while (stash->info_ptr < stash->info_ptr_end)
     {
       bfd_vma length;
-      unsigned int offset_size = addr_size;
+      unsigned int offset_size;
       bfd_byte *info_ptr_unit = stash->info_ptr;
 
       length = read_4_bytes (stash->bfd_ptr, stash->info_ptr, stash->info_ptr_end);
@@ -4746,13 +4732,11 @@ _bfd_dwarf2_find_nearest_line (bfd *abfd,
           b) if they do use 64-bit offsets but they are not using
              the size hints that are tested for above then they are
              not conforming to the DWARF3 standard anyway.  */
-      else if (addr_size == 8)
+      else
        {
          offset_size = 4;
          stash->info_ptr += 4;
        }
-      else
-       stash->info_ptr += 4;
 
       if (length > 0)
        {