/* 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).
#include "libbfd.h"
#include "elf-bfd.h"
#include "dwarf2.h"
+#include "hashtab.h"
/* The data in the .debug_line statement prologue looks like this. */
/* 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;
if (stash->alt_bfd_ptr == NULL)
{
- bfd * debug_bfd;
- char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
+ bfd *debug_bfd;
+ char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
if (debug_filename == NULL)
return NULL;
- if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
- || ! bfd_check_format (debug_bfd, bfd_object))
- {
- if (debug_bfd)
- bfd_close (debug_bfd);
+ debug_bfd = bfd_openr (debug_filename, NULL);
+ free (debug_filename);
+ if (debug_bfd == NULL)
+ /* FIXME: Should we report our failure to follow the debuglink ? */
+ return NULL;
- /* FIXME: Should we report our failure to follow the debuglink ? */
- free (debug_filename);
+ if (!bfd_check_format (debug_bfd, bfd_object))
+ {
+ bfd_close (debug_bfd);
return NULL;
}
stash->alt_bfd_ptr = debug_bfd;
if (stash->alt_bfd_ptr == NULL)
{
- bfd * debug_bfd;
- char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
+ bfd *debug_bfd;
+ char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
if (debug_filename == NULL)
return FALSE;
- if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
- || ! bfd_check_format (debug_bfd, bfd_object))
- {
- if (debug_bfd)
- bfd_close (debug_bfd);
+ debug_bfd = bfd_openr (debug_filename, NULL);
+ free (debug_filename);
+ if (debug_bfd == NULL)
+ /* FIXME: Should we report our failure to follow the debuglink ? */
+ return NULL;
- /* FIXME: Should we report our failure to follow the debuglink ? */
- free (debug_filename);
+ if (!bfd_check_format (debug_bfd, bfd_object))
+ {
+ bfd_close (debug_bfd);
return NULL;
}
stash->alt_bfd_ptr = debug_bfd;
return FALSE;
}
+static struct comp_unit *stash_comp_unit (struct dwarf2_debug *);
+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,
if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
break;
- if (u)
+ while (u == NULL)
+ {
+ u = stash_comp_unit (unit->stash);
+ if (u == NULL)
+ break;
+ if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
+ break;
+ u = NULL;
+ }
+
+ if (u == NULL)
{
- unit = u;
- info_ptr_end = unit->end_ptr;
+ _bfd_error_handler
+ (_("DWARF error: unable to locate abstract instance DIE ref %"
+ PRIu64), (uint64_t) die_ref);
+ bfd_set_error (bfd_error_bad_value);
+ return FALSE;
}
- /* else FIXME: What do we do now ? */
+ unit = u;
+ info_ptr_end = unit->end_ptr;
}
}
else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
}
break;
case DW_AT_decl_file:
+ if (!comp_unit_maybe_decode_line_info (unit, unit->stash))
+ return FALSE;
*filename_ptr = concat_filename (unit->line_table,
attr.u.val);
break;
{
bfd *abfd = unit->abfd;
bfd_byte *info_ptr = unit->first_child_die_ptr;
- bfd_byte *info_ptr_end = unit->stash->info_ptr_end;
+ bfd_byte *info_ptr_end = unit->end_ptr;
int nesting_level = 0;
struct nest_funcinfo {
struct funcinfo *func;
return FALSE;
}
-/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
+/* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
includes the compilation unit header that proceeds the DIE's, but
does not include the length field that precedes each compilation
unit header. END_PTR points one past the end of this comp 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)
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;
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;
fail more quickly. */
return FALSE;
+ debug_bfd = bfd_openr (debug_filename, NULL);
+ free (debug_filename);
+ if (debug_bfd == NULL)
+ /* FIXME: Should we report our failure to follow the debuglink ? */
+ return FALSE;
+
/* Set BFD_DECOMPRESS to decompress debug sections. */
- if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
- || !(debug_bfd->flags |= BFD_DECOMPRESS,
- bfd_check_format (debug_bfd, bfd_object))
+ debug_bfd->flags |= BFD_DECOMPRESS;
+ if (!bfd_check_format (debug_bfd, bfd_object)
|| (msec = find_debug_info (debug_bfd,
debug_sections, NULL)) == NULL
|| !bfd_generic_link_read_symbols (debug_bfd))
{
- if (debug_bfd)
- bfd_close (debug_bfd);
- /* FIXME: Should we report our failure to follow the debuglink ? */
- free (debug_filename);
+ bfd_close (debug_bfd);
return FALSE;
}
return TRUE;
}
+/* Parse the next DWARF2 compilation unit at STASH->INFO_PTR. */
+
+static struct comp_unit *
+stash_comp_unit (struct dwarf2_debug *stash)
+{
+ bfd_size_type length;
+ unsigned int offset_size;
+ bfd_byte *info_ptr_unit = stash->info_ptr;
+
+ if (stash->info_ptr >= stash->info_ptr_end)
+ return NULL;
+
+ length = read_4_bytes (stash->bfd_ptr, stash->info_ptr,
+ stash->info_ptr_end);
+ /* A 0xffffff length is the DWARF3 way of indicating
+ we use 64-bit offsets, instead of 32-bit offsets. */
+ if (length == 0xffffffff)
+ {
+ offset_size = 8;
+ length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4,
+ stash->info_ptr_end);
+ stash->info_ptr += 12;
+ }
+ /* A zero length is the IRIX way of indicating 64-bit offsets,
+ mostly because the 64-bit length will generally fit in 32
+ bits, and the endianness helps. */
+ else if (length == 0)
+ {
+ offset_size = 8;
+ length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4,
+ stash->info_ptr_end);
+ stash->info_ptr += 8;
+ }
+ /* In the absence of the hints above, we assume 32-bit DWARF2
+ offsets even for targets with 64-bit addresses, because:
+ a) most of the time these targets will not have generated
+ more than 2Gb of debug info and so will not need 64-bit
+ offsets,
+ and
+ 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
+ {
+ offset_size = 4;
+ stash->info_ptr += 4;
+ }
+
+ if (length != 0
+ && stash->info_ptr + length <= stash->info_ptr_end
+ && stash->info_ptr + length > stash->info_ptr)
+ {
+ struct comp_unit *each = parse_comp_unit (stash, length, info_ptr_unit,
+ offset_size);
+ if (each)
+ {
+ if (stash->all_comp_units)
+ stash->all_comp_units->prev_unit = each;
+ else
+ stash->last_comp_unit = each;
+
+ each->next_unit = stash->all_comp_units;
+ stash->all_comp_units = each;
+
+ stash->info_ptr += length;
+
+ if ((bfd_size_type) (stash->info_ptr - stash->sec_info_ptr)
+ == stash->sec->size)
+ {
+ stash->sec = find_debug_info (stash->bfd_ptr,
+ stash->debug_sections,
+ stash->sec);
+ stash->sec_info_ptr = stash->info_ptr;
+ }
+ return each;
+ }
+ }
+
+ /* Don't trust any of the DWARF info after a corrupted length or
+ parse error. */
+ stash->info_ptr = stash->info_ptr_end;
+ return NULL;
+}
+
+/* Hash function for an asymbol. */
+
+static hashval_t
+hash_asymbol (const void *sym)
+{
+ const asymbol *asym = sym;
+ return htab_hash_string (asym->name);
+}
+
+/* Equality function for asymbols. */
+
+static int
+eq_asymbol (const void *a, const void *b)
+{
+ const asymbol *sa = a;
+ const asymbol *sb = b;
+ return strcmp (sa->name, sb->name) == 0;
+}
+
/* Scan the debug information in PINFO looking for a DW_TAG_subprogram
abbrev with a DW_AT_low_pc attached to it. Then lookup that same
symbol in SYMBOLS and return the difference between the low_pc and
{
struct dwarf2_debug *stash;
struct comp_unit * unit;
+ htab_t sym_hash;
+ bfd_signed_vma result = 0;
+ asymbol ** psym;
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)
+ sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
+ NULL, xcalloc, free);
+ for (psym = symbols; * psym != NULL; psym++)
{
- struct funcinfo * func;
+ asymbol * sym = * psym;
- if (unit->function_table == NULL)
+ if (sym->flags & BSF_FUNCTION && sym->section != NULL)
{
- if (unit->line_table == NULL)
- unit->line_table = decode_line_info (unit, stash);
- if (unit->line_table != NULL)
- scan_unit_for_symbols (unit);
+ void **slot = htab_find_slot (sym_hash, sym, INSERT);
+ *slot = sym;
}
+ }
+
+ for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
+ {
+ struct funcinfo * func;
+
+ 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)
{
- asymbol ** psym;
+ asymbol search, *sym;
/* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
- for (psym = symbols; * psym != NULL; psym++)
+ search.name = func->name;
+ sym = htab_find (sym_hash, &search);
+ if (sym != NULL)
{
- asymbol * sym = * psym;
-
- if (sym->flags & BSF_FUNCTION
- && sym->section != NULL
- && strcmp (sym->name, func->name) == 0)
- return ((bfd_signed_vma) func->arange.low) -
- ((bfd_signed_vma) (sym->value + sym->section->vma));
+ result = ((bfd_signed_vma) func->arange.low) -
+ ((bfd_signed_vma) (sym->value + sym->section->vma));
+ goto done;
}
}
}
- return 0;
+ done:
+ htab_delete (sym_hash);
+ return result;
}
/* Find the source code location of SYMBOL. If SYMBOL is NULL
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. */
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
}
}
- /* 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)
+ while ((each = stash_comp_unit (stash)) != NULL)
{
- bfd_vma length;
- unsigned int offset_size = addr_size;
- bfd_byte *info_ptr_unit = stash->info_ptr;
-
- length = read_4_bytes (stash->bfd_ptr, stash->info_ptr, stash->info_ptr_end);
- /* A 0xffffff length is the DWARF3 way of indicating
- we use 64-bit offsets, instead of 32-bit offsets. */
- if (length == 0xffffffff)
- {
- offset_size = 8;
- length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
- stash->info_ptr += 12;
- }
- /* A zero length is the IRIX way of indicating 64-bit offsets,
- mostly because the 64-bit length will generally fit in 32
- bits, and the endianness helps. */
- else if (length == 0)
- {
- offset_size = 8;
- length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
- stash->info_ptr += 8;
- }
- /* In the absence of the hints above, we assume 32-bit DWARF2
- offsets even for targets with 64-bit addresses, because:
- a) most of the time these targets will not have generated
- more than 2Gb of debug info and so will not need 64-bit
- offsets,
- and
- 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)
- {
- offset_size = 4;
- stash->info_ptr += 4;
- }
+ /* DW_AT_low_pc and DW_AT_high_pc are optional for
+ compilation units. If we don't have them (i.e.,
+ unit->high == 0), we need to consult the line info table
+ to see if a compilation unit contains the given
+ address. */
+ if (do_line)
+ found = (((symbol->flags & BSF_FUNCTION) == 0
+ || each->arange.high == 0
+ || comp_unit_contains_address (each, addr))
+ && comp_unit_find_line (each, symbol, addr,
+ filename_ptr,
+ linenumber_ptr,
+ stash));
else
- stash->info_ptr += 4;
-
- if (length > 0)
- {
- bfd_byte * new_ptr;
-
- /* PR 21151 */
- if (stash->info_ptr + length > stash->info_ptr_end)
- return FALSE;
-
- each = parse_comp_unit (stash, length, info_ptr_unit,
- offset_size);
- if (!each)
- /* The dwarf information is damaged, don't trust it any
- more. */
- break;
-
- new_ptr = stash->info_ptr + length;
- /* PR 17512: file: 1500698c. */
- if (new_ptr < stash->info_ptr)
- {
- /* A corrupt length value - do not trust the info any more. */
- found = FALSE;
- break;
- }
- else
- stash->info_ptr = new_ptr;
-
- if (stash->all_comp_units)
- stash->all_comp_units->prev_unit = each;
- else
- stash->last_comp_unit = each;
-
- each->next_unit = stash->all_comp_units;
- stash->all_comp_units = each;
-
- /* DW_AT_low_pc and DW_AT_high_pc are optional for
- compilation units. If we don't have them (i.e.,
- unit->high == 0), we need to consult the line info table
- to see if a compilation unit contains the given
- address. */
- if (do_line)
- found = (((symbol->flags & BSF_FUNCTION) == 0
- || each->arange.high == 0
- || comp_unit_contains_address (each, addr))
- && comp_unit_find_line (each, symbol, addr,
- filename_ptr,
- linenumber_ptr,
- stash));
- else
- found = ((each->arange.high == 0
- || comp_unit_contains_address (each, addr))
- && comp_unit_find_nearest_line (each, addr,
- filename_ptr,
- &function,
- linenumber_ptr,
- discriminator_ptr,
- stash) != 0);
-
- if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
- == stash->sec->size)
- {
- stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
- stash->sec);
- stash->sec_info_ptr = stash->info_ptr;
- }
-
- if (found)
- goto done;
- }
+ found = ((each->arange.high == 0
+ || comp_unit_contains_address (each, addr))
+ && comp_unit_find_nearest_line (each, addr,
+ filename_ptr,
+ &function,
+ linenumber_ptr,
+ discriminator_ptr,
+ stash) != 0);
+
+ if (found)
+ break;
}
done: