+static struct varinfo *
+reverse_varinfo_list (struct varinfo *head)
+{
+ struct varinfo *rhead;
+ struct varinfo *temp;
+
+ for (rhead = NULL; head; head = temp)
+ {
+ temp = head->prev_var;
+ head->prev_var = rhead;
+ rhead = head;
+ }
+ return rhead;
+}
+
+/* Extract all interesting funcinfos and varinfos of a compilation
+ unit into hash tables for faster lookup. Returns TRUE if no
+ errors were enountered; FALSE otherwise. */
+
+static bfd_boolean
+comp_unit_hash_info (struct dwarf2_debug *stash,
+ struct comp_unit *unit,
+ struct info_hash_table *funcinfo_hash_table,
+ struct info_hash_table *varinfo_hash_table)
+{
+ struct funcinfo* each_func;
+ struct varinfo* each_var;
+ bfd_boolean okay = TRUE;
+
+ BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
+
+ if (!comp_unit_maybe_decode_line_info (unit, stash))
+ return FALSE;
+
+ BFD_ASSERT (!unit->cached);
+
+ /* To preserve the original search order, we went to visit the function
+ infos in the reversed order of the list. However, making the list
+ bi-directional use quite a bit of extra memory. So we reverse
+ the list first, traverse the list in the now reversed order and
+ finally reverse the list again to get back the original order. */
+ unit->function_table = reverse_funcinfo_list (unit->function_table);
+ for (each_func = unit->function_table;
+ each_func && okay;
+ each_func = each_func->prev_func)
+ {
+ /* Skip nameless functions. */
+ if (each_func->name)
+ /* There is no need to copy name string into hash table as
+ name string is either in the dwarf string buffer or
+ info in the stash. */
+ okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
+ (void*) each_func, FALSE);
+ }
+ unit->function_table = reverse_funcinfo_list (unit->function_table);
+ if (!okay)
+ return FALSE;
+
+ /* We do the same for variable infos. */
+ unit->variable_table = reverse_varinfo_list (unit->variable_table);
+ for (each_var = unit->variable_table;
+ each_var && okay;
+ each_var = each_var->prev_var)
+ {
+ /* Skip stack vars and vars with no files or names. */
+ if (each_var->stack == 0
+ && each_var->file != NULL
+ && each_var->name != NULL)
+ /* There is no need to copy name string into hash table as
+ name string is either in the dwarf string buffer or
+ info in the stash. */
+ okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
+ (void*) each_var, FALSE);
+ }
+
+ unit->variable_table = reverse_varinfo_list (unit->variable_table);
+ unit->cached = TRUE;
+ return okay;
+}
+
+/* Locate a section in a BFD containing debugging info. The search starts
+ from the section after AFTER_SEC, or from the first section in the BFD if
+ AFTER_SEC is NULL. The search works by examining the names of the
+ sections. There are three permissiable names. The first two are given
+ by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
+ and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
+ This is a variation on the .debug_info section which has a checksum
+ describing the contents appended onto the name. This allows the linker to
+ identify and discard duplicate debugging sections for different
+ compilation units. */
+#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
+
+static asection *
+find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
+ asection *after_sec)
+{
+ asection *msec;
+ const char *look;
+
+ if (after_sec == NULL)
+ {
+ look = debug_sections[debug_info].uncompressed_name;
+ msec = bfd_get_section_by_name (abfd, look);
+ if (msec != NULL)
+ return msec;
+
+ look = debug_sections[debug_info].compressed_name;
+ if (look != NULL)
+ {
+ msec = bfd_get_section_by_name (abfd, look);
+ if (msec != NULL)
+ return msec;
+ }
+
+ for (msec = abfd->sections; msec != NULL; msec = msec->next)
+ if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
+ return msec;
+
+ return NULL;
+ }
+
+ for (msec = after_sec->next; msec != NULL; msec = msec->next)
+ {
+ look = debug_sections[debug_info].uncompressed_name;
+ if (strcmp (msec->name, look) == 0)
+ return msec;
+
+ look = debug_sections[debug_info].compressed_name;
+ if (look != NULL && strcmp (msec->name, look) == 0)
+ return msec;
+
+ if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
+ return msec;
+ }
+
+ return NULL;
+}
+
+/* Transfer VMAs from object file to separate debug file. */
+
+static void
+set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
+{
+ asection *s, *d;
+
+ for (s = orig_bfd->sections, d = debug_bfd->sections;
+ s != NULL && d != NULL;
+ s = s->next, d = d->next)
+ {
+ if ((d->flags & SEC_DEBUGGING) != 0)
+ break;
+ /* ??? Assumes 1-1 correspondence between sections in the
+ two files. */
+ if (strcmp (s->name, d->name) == 0)
+ {
+ d->output_section = s->output_section;
+ d->output_offset = s->output_offset;
+ d->vma = s->vma;
+ }
+ }
+}
+
+/* Unset vmas for adjusted sections in STASH. */
+
+static void
+unset_sections (struct dwarf2_debug *stash)
+{
+ int i;
+ struct adjusted_section *p;
+
+ i = stash->adjusted_section_count;
+ p = stash->adjusted_sections;
+ for (; i > 0; i--, p++)
+ p->section->vma = 0;
+}
+
+/* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
+ relocatable object file. VMAs are normally all zero in relocatable
+ object files, so if we want to distinguish locations in sections by
+ address we need to set VMAs so the sections do not overlap. We
+ also set VMA on .debug_info so that when we have multiple
+ .debug_info sections (or the linkonce variant) they also do not
+ overlap. The multiple .debug_info sections make up a single
+ logical section. ??? We should probably do the same for other
+ debug sections. */
+
+static bfd_boolean
+place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
+{
+ bfd *abfd;
+ struct adjusted_section *p;
+ int i;
+ const char *debug_info_name;
+
+ if (stash->adjusted_section_count != 0)
+ {
+ i = stash->adjusted_section_count;
+ p = stash->adjusted_sections;
+ for (; i > 0; i--, p++)
+ p->section->vma = p->adj_vma;
+ return TRUE;
+ }
+
+ debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
+ i = 0;
+ abfd = orig_bfd;
+ while (1)
+ {
+ asection *sect;
+
+ for (sect = abfd->sections; sect != NULL; sect = sect->next)
+ {
+ int is_debug_info;
+
+ if ((sect->output_section != NULL
+ && sect->output_section != sect
+ && (sect->flags & SEC_DEBUGGING) == 0)
+ || sect->vma != 0)
+ continue;
+
+ is_debug_info = (strcmp (sect->name, debug_info_name) == 0
+ || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
+
+ if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
+ && !is_debug_info)
+ continue;
+
+ i++;
+ }
+ if (abfd == stash->bfd_ptr)
+ break;
+ abfd = stash->bfd_ptr;
+ }
+
+ if (i <= 1)
+ stash->adjusted_section_count = -1;
+ else
+ {
+ bfd_vma last_vma = 0, last_dwarf = 0;
+ bfd_size_type amt = i * sizeof (struct adjusted_section);
+
+ p = (struct adjusted_section *) bfd_malloc (amt);
+ if (p == NULL)
+ return FALSE;
+
+ stash->adjusted_sections = p;
+ stash->adjusted_section_count = i;
+
+ abfd = orig_bfd;
+ while (1)
+ {
+ asection *sect;
+
+ for (sect = abfd->sections; sect != NULL; sect = sect->next)
+ {
+ bfd_size_type sz;
+ int is_debug_info;
+
+ if ((sect->output_section != NULL
+ && sect->output_section != sect
+ && (sect->flags & SEC_DEBUGGING) == 0)
+ || sect->vma != 0)
+ continue;
+
+ is_debug_info = (strcmp (sect->name, debug_info_name) == 0
+ || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
+
+ if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
+ && !is_debug_info)
+ continue;
+
+ sz = sect->rawsize ? sect->rawsize : sect->size;
+
+ if (is_debug_info)
+ {
+ BFD_ASSERT (sect->alignment_power == 0);
+ sect->vma = last_dwarf;
+ last_dwarf += sz;
+ }
+ else
+ {
+ /* Align the new address to the current section
+ alignment. */
+ last_vma = ((last_vma
+ + ~(-((bfd_vma) 1 << sect->alignment_power)))
+ & (-((bfd_vma) 1 << sect->alignment_power)));
+ sect->vma = last_vma;
+ last_vma += sz;
+ }
+
+ p->section = sect;
+ p->adj_vma = sect->vma;
+ p++;
+ }
+ if (abfd == stash->bfd_ptr)
+ break;
+ abfd = stash->bfd_ptr;
+ }
+ }
+
+ if (orig_bfd != stash->bfd_ptr)
+ set_debug_vma (orig_bfd, stash->bfd_ptr);
+
+ return TRUE;
+}
+
+/* Look up a funcinfo by name using the given info hash table. If found,
+ also update the locations pointed to by filename_ptr and linenumber_ptr.
+
+ This function returns TRUE if a funcinfo that matches the given symbol
+ and address is found with any error; otherwise it returns FALSE. */
+
+static bfd_boolean
+info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
+ asymbol *sym,
+ bfd_vma addr,
+ const char **filename_ptr,
+ unsigned int *linenumber_ptr)
+{
+ struct funcinfo* each_func;
+ struct funcinfo* best_fit = NULL;
+ bfd_vma best_fit_len = 0;
+ struct info_list_node *node;
+ struct arange *arange;
+ const char *name = bfd_asymbol_name (sym);
+ asection *sec = bfd_get_section (sym);
+
+ for (node = lookup_info_hash_table (hash_table, name);
+ node;
+ node = node->next)
+ {
+ each_func = (struct funcinfo *) node->info;
+ for (arange = &each_func->arange;
+ arange;
+ arange = arange->next)
+ {
+ if ((!each_func->sec || each_func->sec == sec)
+ && addr >= arange->low
+ && addr < arange->high
+ && (!best_fit
+ || arange->high - arange->low < best_fit_len))
+ {
+ best_fit = each_func;
+ best_fit_len = arange->high - arange->low;
+ }
+ }
+ }
+
+ if (best_fit)
+ {
+ best_fit->sec = sec;
+ *filename_ptr = best_fit->file;
+ *linenumber_ptr = best_fit->line;
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+/* Look up a varinfo by name using the given info hash table. If found,
+ also update the locations pointed to by filename_ptr and linenumber_ptr.
+
+ This function returns TRUE if a varinfo that matches the given symbol
+ and address is found with any error; otherwise it returns FALSE. */
+
+static bfd_boolean
+info_hash_lookup_varinfo (struct info_hash_table *hash_table,
+ asymbol *sym,
+ bfd_vma addr,
+ const char **filename_ptr,
+ unsigned int *linenumber_ptr)
+{
+ const char *name = bfd_asymbol_name (sym);
+ asection *sec = bfd_get_section (sym);
+ struct varinfo* each;
+ struct info_list_node *node;
+
+ for (node = lookup_info_hash_table (hash_table, name);
+ node;
+ node = node->next)
+ {
+ each = (struct varinfo *) node->info;
+ if (each->addr == addr
+ && (!each->sec || each->sec == sec))
+ {
+ each->sec = sec;
+ *filename_ptr = each->file;
+ *linenumber_ptr = each->line;
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+/* Update the funcinfo and varinfo info hash tables if they are
+ not up to date. Returns TRUE if there is no error; otherwise
+ returns FALSE and disable the info hash tables. */
+
+static bfd_boolean
+stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
+{
+ struct comp_unit *each;
+
+ /* Exit if hash tables are up-to-date. */
+ if (stash->all_comp_units == stash->hash_units_head)
+ return TRUE;
+
+ if (stash->hash_units_head)
+ each = stash->hash_units_head->prev_unit;
+ else
+ each = stash->last_comp_unit;
+
+ while (each)
+ {
+ if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
+ stash->varinfo_hash_table))
+ {
+ stash->info_hash_status = STASH_INFO_HASH_DISABLED;
+ return FALSE;
+ }
+ each = each->prev_unit;
+ }
+
+ stash->hash_units_head = stash->all_comp_units;
+ return TRUE;
+}
+
+/* Check consistency of info hash tables. This is for debugging only. */
+
+static void ATTRIBUTE_UNUSED
+stash_verify_info_hash_table (struct dwarf2_debug *stash)
+{
+ struct comp_unit *each_unit;
+ struct funcinfo *each_func;
+ struct varinfo *each_var;
+ struct info_list_node *node;
+ bfd_boolean found;
+
+ for (each_unit = stash->all_comp_units;
+ each_unit;
+ each_unit = each_unit->next_unit)
+ {
+ for (each_func = each_unit->function_table;
+ each_func;
+ each_func = each_func->prev_func)
+ {
+ if (!each_func->name)
+ continue;
+ node = lookup_info_hash_table (stash->funcinfo_hash_table,
+ each_func->name);
+ BFD_ASSERT (node);
+ found = FALSE;
+ while (node && !found)
+ {
+ found = node->info == each_func;
+ node = node->next;
+ }
+ BFD_ASSERT (found);
+ }
+
+ for (each_var = each_unit->variable_table;
+ each_var;
+ each_var = each_var->prev_var)
+ {
+ if (!each_var->name || !each_var->file || each_var->stack)
+ continue;
+ node = lookup_info_hash_table (stash->varinfo_hash_table,
+ each_var->name);
+ BFD_ASSERT (node);
+ found = FALSE;
+ while (node && !found)
+ {
+ found = node->info == each_var;
+ node = node->next;
+ }
+ BFD_ASSERT (found);
+ }
+ }
+}
+
+/* Check to see if we want to enable the info hash tables, which consume
+ quite a bit of memory. Currently we only check the number times
+ bfd_dwarf2_find_line is called. In the future, we may also want to
+ take the number of symbols into account. */
+
+static void
+stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
+{
+ BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
+
+ if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
+ return;
+
+ /* FIXME: Maybe we should check the reduce_memory_overheads
+ and optimize fields in the bfd_link_info structure ? */
+
+ /* Create hash tables. */
+ stash->funcinfo_hash_table = create_info_hash_table (abfd);
+ stash->varinfo_hash_table = create_info_hash_table (abfd);
+ if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
+ {
+ /* Turn off info hashes if any allocation above fails. */
+ stash->info_hash_status = STASH_INFO_HASH_DISABLED;
+ return;
+ }
+ /* We need a forced update so that the info hash tables will
+ be created even though there is no compilation unit. That
+ happens if STASH_INFO_HASH_TRIGGER is 0. */
+ stash_maybe_update_info_hash_tables (stash);
+ stash->info_hash_status = STASH_INFO_HASH_ON;
+}
+
+/* Find the file and line associated with a symbol and address using the
+ info hash tables of a stash. If there is a match, the function returns
+ TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
+ otherwise it returns FALSE. */
+
+static bfd_boolean
+stash_find_line_fast (struct dwarf2_debug *stash,
+ asymbol *sym,
+ bfd_vma addr,
+ const char **filename_ptr,
+ unsigned int *linenumber_ptr)
+{
+ BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
+
+ if (sym->flags & BSF_FUNCTION)
+ return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
+ filename_ptr, linenumber_ptr);
+ return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
+ filename_ptr, linenumber_ptr);
+}
+
+/* Save current section VMAs. */
+
+static bfd_boolean
+save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
+{
+ asection *s;
+ unsigned int i;