2012-05-03 Tristan Gingold <gingold@adacore.com>
[platform/upstream/binutils.git] / bfd / dwarf2.c
1 /* DWARF 2 support.
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6    (gavin@cygnus.com).
7
8    From the dwarf2read.c header:
9    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10    Inc.  with support from Florida State University (under contract
11    with the Ada Joint Program Office), and Silicon Graphics, Inc.
12    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14    support in dwarfread.c
15
16    This file is part of BFD.
17
18    This program is free software; you can redistribute it and/or modify
19    it under the terms of the GNU General Public License as published by
20    the Free Software Foundation; either version 3 of the License, or (at
21    your option) any later version.
22
23    This program is distributed in the hope that it will be useful, but
24    WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public License
29    along with this program; if not, write to the Free Software
30    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31    MA 02110-1301, USA.  */
32
33 #include "sysdep.h"
34 #include "bfd.h"
35 #include "libiberty.h"
36 #include "libbfd.h"
37 #include "elf-bfd.h"
38 #include "dwarf2.h"
39
40 /* The data in the .debug_line statement prologue looks like this.  */
41
42 struct line_head
43 {
44   bfd_vma total_length;
45   unsigned short version;
46   bfd_vma prologue_length;
47   unsigned char minimum_instruction_length;
48   unsigned char maximum_ops_per_insn;
49   unsigned char default_is_stmt;
50   int line_base;
51   unsigned char line_range;
52   unsigned char opcode_base;
53   unsigned char *standard_opcode_lengths;
54 };
55
56 /* Attributes have a name and a value.  */
57
58 struct attribute
59 {
60   enum dwarf_attribute name;
61   enum dwarf_form form;
62   union
63   {
64     char *str;
65     struct dwarf_block *blk;
66     bfd_uint64_t val;
67     bfd_int64_t sval;
68   }
69   u;
70 };
71
72 /* Blocks are a bunch of untyped bytes.  */
73 struct dwarf_block
74 {
75   unsigned int size;
76   bfd_byte *data;
77 };
78
79 struct adjusted_section
80 {
81   asection *section;
82   bfd_vma adj_vma;
83 };
84
85 struct dwarf2_debug
86 {
87   /* A list of all previously read comp_units.  */
88   struct comp_unit *all_comp_units;
89
90   /* Last comp unit in list above.  */
91   struct comp_unit *last_comp_unit;
92
93   /* Names of the debug sections.  */
94   const struct dwarf_debug_section *debug_sections;
95
96   /* The next unread compilation unit within the .debug_info section.
97      Zero indicates that the .debug_info section has not been loaded
98      into a buffer yet.  */
99   bfd_byte *info_ptr;
100
101   /* Pointer to the end of the .debug_info section memory buffer.  */
102   bfd_byte *info_ptr_end;
103
104   /* Pointer to the bfd, section and address of the beginning of the
105      section.  The bfd might be different than expected because of
106      gnu_debuglink sections.  */
107   bfd *bfd_ptr;
108   asection *sec;
109   bfd_byte *sec_info_ptr;
110
111   /* A pointer to the memory block allocated for info_ptr.  Neither
112      info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
113      beginning of the malloc block.  This is used only to free the
114      memory later.  */
115   bfd_byte *info_ptr_memory;
116
117   /* Pointer to the symbol table.  */
118   asymbol **syms;
119
120   /* Pointer to the .debug_abbrev section loaded into memory.  */
121   bfd_byte *dwarf_abbrev_buffer;
122
123   /* Length of the loaded .debug_abbrev section.  */
124   bfd_size_type dwarf_abbrev_size;
125
126   /* Buffer for decode_line_info.  */
127   bfd_byte *dwarf_line_buffer;
128
129   /* Length of the loaded .debug_line section.  */
130   bfd_size_type dwarf_line_size;
131
132   /* Pointer to the .debug_str section loaded into memory.  */
133   bfd_byte *dwarf_str_buffer;
134
135   /* Length of the loaded .debug_str section.  */
136   bfd_size_type dwarf_str_size;
137
138   /* Pointer to the .debug_ranges section loaded into memory. */
139   bfd_byte *dwarf_ranges_buffer;
140
141   /* Length of the loaded .debug_ranges section. */
142   bfd_size_type dwarf_ranges_size;
143
144   /* If the most recent call to bfd_find_nearest_line was given an
145      address in an inlined function, preserve a pointer into the
146      calling chain for subsequent calls to bfd_find_inliner_info to
147      use. */
148   struct funcinfo *inliner_chain;
149
150   /* Number of sections whose VMA we must adjust.  */
151   unsigned int adjusted_section_count;
152
153   /* Array of sections with adjusted VMA.  */
154   struct adjusted_section *adjusted_sections;
155
156   /* Number of times find_line is called.  This is used in
157      the heuristic for enabling the info hash tables.  */
158   int info_hash_count;
159
160 #define STASH_INFO_HASH_TRIGGER    100
161
162   /* Hash table mapping symbol names to function infos.  */
163   struct info_hash_table *funcinfo_hash_table;
164
165   /* Hash table mapping symbol names to variable infos.  */
166   struct info_hash_table *varinfo_hash_table;
167
168   /* Head of comp_unit list in the last hash table update.  */
169   struct comp_unit *hash_units_head;
170
171   /* Status of info hash.  */
172   int info_hash_status;
173 #define STASH_INFO_HASH_OFF        0
174 #define STASH_INFO_HASH_ON         1
175 #define STASH_INFO_HASH_DISABLED   2
176 };
177
178 struct arange
179 {
180   struct arange *next;
181   bfd_vma low;
182   bfd_vma high;
183 };
184
185 /* A minimal decoding of DWARF2 compilation units.  We only decode
186    what's needed to get to the line number information.  */
187
188 struct comp_unit
189 {
190   /* Chain the previously read compilation units.  */
191   struct comp_unit *next_unit;
192
193   /* Likewise, chain the compilation unit read after this one.
194      The comp units are stored in reversed reading order.  */
195   struct comp_unit *prev_unit;
196
197   /* Keep the bfd convenient (for memory allocation).  */
198   bfd *abfd;
199
200   /* The lowest and highest addresses contained in this compilation
201      unit as specified in the compilation unit header.  */
202   struct arange arange;
203
204   /* The DW_AT_name attribute (for error messages).  */
205   char *name;
206
207   /* The abbrev hash table.  */
208   struct abbrev_info **abbrevs;
209
210   /* Note that an error was found by comp_unit_find_nearest_line.  */
211   int error;
212
213   /* The DW_AT_comp_dir attribute.  */
214   char *comp_dir;
215
216   /* TRUE if there is a line number table associated with this comp. unit.  */
217   int stmtlist;
218
219   /* Pointer to the current comp_unit so that we can find a given entry
220      by its reference.  */
221   bfd_byte *info_ptr_unit;
222
223   /* Pointer to the start of the debug section, for DW_FORM_ref_addr.  */
224   bfd_byte *sec_info_ptr;
225
226   /* The offset into .debug_line of the line number table.  */
227   unsigned long line_offset;
228
229   /* Pointer to the first child die for the comp unit.  */
230   bfd_byte *first_child_die_ptr;
231
232   /* The end of the comp unit.  */
233   bfd_byte *end_ptr;
234
235   /* The decoded line number, NULL if not yet decoded.  */
236   struct line_info_table *line_table;
237
238   /* A list of the functions found in this comp. unit.  */
239   struct funcinfo *function_table;
240
241   /* A list of the variables found in this comp. unit.  */
242   struct varinfo *variable_table;
243
244   /* Pointer to dwarf2_debug structure.  */
245   struct dwarf2_debug *stash;
246
247   /* DWARF format version for this unit - from unit header.  */
248   int version;
249
250   /* Address size for this unit - from unit header.  */
251   unsigned char addr_size;
252
253   /* Offset size for this unit - from unit header.  */
254   unsigned char offset_size;
255
256   /* Base address for this unit - from DW_AT_low_pc attribute of
257      DW_TAG_compile_unit DIE */
258   bfd_vma base_address;
259
260   /* TRUE if symbols are cached in hash table for faster lookup by name.  */
261   bfd_boolean cached;
262 };
263
264 /* This data structure holds the information of an abbrev.  */
265 struct abbrev_info
266 {
267   unsigned int number;          /* Number identifying abbrev.  */
268   enum dwarf_tag tag;           /* DWARF tag.  */
269   int has_children;             /* Boolean.  */
270   unsigned int num_attrs;       /* Number of attributes.  */
271   struct attr_abbrev *attrs;    /* An array of attribute descriptions.  */
272   struct abbrev_info *next;     /* Next in chain.  */
273 };
274
275 struct attr_abbrev
276 {
277   enum dwarf_attribute name;
278   enum dwarf_form form;
279 };
280
281 /* Map of uncompressed DWARF debug section name to compressed one.  It
282    is terminated by NULL uncompressed_name.  */
283
284 const struct dwarf_debug_section dwarf_debug_sections[] =
285 {
286   { ".debug_abbrev",            ".zdebug_abbrev" },
287   { ".debug_aranges",           ".zdebug_aranges" },
288   { ".debug_frame",             ".zdebug_frame" },
289   { ".debug_info",              ".zdebug_info" },
290   { ".debug_line",              ".zdebug_line" },
291   { ".debug_loc",               ".zdebug_loc" },
292   { ".debug_macinfo",           ".zdebug_macinfo" },
293   { ".debug_macro",             ".zdebug_macro" },
294   { ".debug_pubnames",          ".zdebug_pubnames" },
295   { ".debug_pubtypes",          ".zdebug_pubtypes" },
296   { ".debug_ranges",            ".zdebug_ranges" },
297   { ".debug_static_func",       ".zdebug_static_func" },
298   { ".debug_static_vars",       ".zdebug_static_vars" },
299   { ".debug_str",               ".zdebug_str", },
300   { ".debug_types",             ".zdebug_types" },
301   /* GNU DWARF 1 extensions */
302   { ".debug_sfnames",           ".zdebug_sfnames" },
303   { ".debug_srcinfo",           ".zebug_srcinfo" },
304   /* SGI/MIPS DWARF 2 extensions */
305   { ".debug_funcnames",         ".zdebug_funcnames" },
306   { ".debug_typenames",         ".zdebug_typenames" },
307   { ".debug_varnames",          ".zdebug_varnames" },
308   { ".debug_weaknames",         ".zdebug_weaknames" },
309   { NULL,                       NULL },
310 };
311
312 enum dwarf_debug_section_enum
313 {
314   debug_abbrev = 0,
315   debug_aranges,
316   debug_frame,
317   debug_info,
318   debug_line,
319   debug_loc,
320   debug_macinfo,
321   debug_macro,
322   debug_pubnames,
323   debug_pubtypes,
324   debug_ranges,
325   debug_static_func,
326   debug_static_vars,
327   debug_str,
328   debug_types,
329   debug_sfnames,
330   debug_srcinfo,
331   debug_funcnames,
332   debug_typenames,
333   debug_varnames,
334   debug_weaknames
335 };
336
337 #ifndef ABBREV_HASH_SIZE
338 #define ABBREV_HASH_SIZE 121
339 #endif
340 #ifndef ATTR_ALLOC_CHUNK
341 #define ATTR_ALLOC_CHUNK 4
342 #endif
343
344 /* Variable and function hash tables.  This is used to speed up look-up
345    in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
346    In order to share code between variable and function infos, we use
347    a list of untyped pointer for all variable/function info associated with
348    a symbol.  We waste a bit of memory for list with one node but that
349    simplifies the code.  */
350
351 struct info_list_node
352 {
353   struct info_list_node *next;
354   void *info;
355 };
356
357 /* Info hash entry.  */
358 struct info_hash_entry
359 {
360   struct bfd_hash_entry root;
361   struct info_list_node *head;
362 };
363
364 struct info_hash_table
365 {
366   struct bfd_hash_table base;
367 };
368
369 /* Function to create a new entry in info hash table. */
370
371 static struct bfd_hash_entry *
372 info_hash_table_newfunc (struct bfd_hash_entry *entry,
373                          struct bfd_hash_table *table,
374                          const char *string)
375 {
376   struct info_hash_entry *ret = (struct info_hash_entry *) entry;
377
378   /* Allocate the structure if it has not already been allocated by a
379      derived class.  */
380   if (ret == NULL)
381     {
382       ret = (struct info_hash_entry *) bfd_hash_allocate (table,
383                                                           sizeof (* ret));
384       if (ret == NULL)
385         return NULL;
386     }
387
388   /* Call the allocation method of the base class.  */
389   ret = ((struct info_hash_entry *)
390          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
391
392   /* Initialize the local fields here.  */
393   if (ret)
394     ret->head = NULL;
395
396   return (struct bfd_hash_entry *) ret;
397 }
398
399 /* Function to create a new info hash table.  It returns a pointer to the
400    newly created table or NULL if there is any error.  We need abfd
401    solely for memory allocation.  */
402
403 static struct info_hash_table *
404 create_info_hash_table (bfd *abfd)
405 {
406   struct info_hash_table *hash_table;
407
408   hash_table = (struct info_hash_table *)
409       bfd_alloc (abfd, sizeof (struct info_hash_table));
410   if (!hash_table)
411     return hash_table;
412
413   if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
414                             sizeof (struct info_hash_entry)))
415     {
416       bfd_release (abfd, hash_table);
417       return NULL;
418     }
419
420   return hash_table;
421 }
422
423 /* Insert an info entry into an info hash table.  We do not check of
424    duplicate entries.  Also, the caller need to guarantee that the
425    right type of info in inserted as info is passed as a void* pointer.
426    This function returns true if there is no error.  */
427
428 static bfd_boolean
429 insert_info_hash_table (struct info_hash_table *hash_table,
430                         const char *key,
431                         void *info,
432                         bfd_boolean copy_p)
433 {
434   struct info_hash_entry *entry;
435   struct info_list_node *node;
436
437   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
438                                                      key, TRUE, copy_p);
439   if (!entry)
440     return FALSE;
441
442   node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
443                                                       sizeof (*node));
444   if (!node)
445     return FALSE;
446
447   node->info = info;
448   node->next = entry->head;
449   entry->head = node;
450
451   return TRUE;
452 }
453
454 /* Look up an info entry list from an info hash table.  Return NULL
455    if there is none. */
456
457 static struct info_list_node *
458 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
459 {
460   struct info_hash_entry *entry;
461
462   entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
463                                                      FALSE, FALSE);
464   return entry ? entry->head : NULL;
465 }
466
467 /* Read a section into its appropriate place in the dwarf2_debug
468    struct (indicated by SECTION_BUFFER and SECTION_SIZE).  If SYMS is
469    not NULL, use bfd_simple_get_relocated_section_contents to read the
470    section contents, otherwise use bfd_get_section_contents.  Fail if
471    the located section does not contain at least OFFSET bytes.  */
472
473 static bfd_boolean
474 read_section (bfd *           abfd,
475               const struct dwarf_debug_section *sec,
476               asymbol **      syms,
477               bfd_uint64_t    offset,
478               bfd_byte **     section_buffer,
479               bfd_size_type * section_size)
480 {
481   asection *msec;
482   const char *section_name = sec->uncompressed_name;
483
484   /* read_section is a noop if the section has already been read.  */
485   if (!*section_buffer)
486     {
487       msec = bfd_get_section_by_name (abfd, section_name);
488       if (! msec)
489         {
490           section_name = sec->compressed_name;
491           if (section_name != NULL)
492             msec = bfd_get_section_by_name (abfd, section_name);
493         }
494       if (! msec)
495         {
496           (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."),
497                                  sec->uncompressed_name);
498           bfd_set_error (bfd_error_bad_value);
499           return FALSE;
500         }
501
502       *section_size = msec->rawsize ? msec->rawsize : msec->size;
503       if (syms)
504         {
505           *section_buffer
506               = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
507           if (! *section_buffer)
508             return FALSE;
509         }
510       else
511         {
512           *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
513           if (! *section_buffer)
514             return FALSE;
515           if (! bfd_get_section_contents (abfd, msec, *section_buffer,
516                                           0, *section_size))
517             return FALSE;
518         }
519     }
520
521   /* It is possible to get a bad value for the offset into the section
522      that the client wants.  Validate it here to avoid trouble later.  */
523   if (offset != 0 && offset >= *section_size)
524     {
525       (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
526                              (long) offset, section_name, *section_size);
527       bfd_set_error (bfd_error_bad_value);
528       return FALSE;
529     }
530
531   return TRUE;
532 }
533
534 /* VERBATIM
535    The following function up to the END VERBATIM mark are
536    copied directly from dwarf2read.c.  */
537
538 /* Read dwarf information from a buffer.  */
539
540 static unsigned int
541 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
542 {
543   return bfd_get_8 (abfd, buf);
544 }
545
546 static int
547 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
548 {
549   return bfd_get_signed_8 (abfd, buf);
550 }
551
552 static unsigned int
553 read_2_bytes (bfd *abfd, bfd_byte *buf)
554 {
555   return bfd_get_16 (abfd, buf);
556 }
557
558 static unsigned int
559 read_4_bytes (bfd *abfd, bfd_byte *buf)
560 {
561   return bfd_get_32 (abfd, buf);
562 }
563
564 static bfd_uint64_t
565 read_8_bytes (bfd *abfd, bfd_byte *buf)
566 {
567   return bfd_get_64 (abfd, buf);
568 }
569
570 static bfd_byte *
571 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
572               bfd_byte *buf,
573               unsigned int size ATTRIBUTE_UNUSED)
574 {
575   return buf;
576 }
577
578 static char *
579 read_string (bfd *abfd ATTRIBUTE_UNUSED,
580              bfd_byte *buf,
581              unsigned int *bytes_read_ptr)
582 {
583   /* Return a pointer to the embedded string.  */
584   char *str = (char *) buf;
585
586   if (*str == '\0')
587     {
588       *bytes_read_ptr = 1;
589       return NULL;
590     }
591
592   *bytes_read_ptr = strlen (str) + 1;
593   return str;
594 }
595
596 /* END VERBATIM */
597
598 static char *
599 read_indirect_string (struct comp_unit * unit,
600                       bfd_byte *         buf,
601                       unsigned int *     bytes_read_ptr)
602 {
603   bfd_uint64_t offset;
604   struct dwarf2_debug *stash = unit->stash;
605   char *str;
606
607   if (unit->offset_size == 4)
608     offset = read_4_bytes (unit->abfd, buf);
609   else
610     offset = read_8_bytes (unit->abfd, buf);
611
612   *bytes_read_ptr = unit->offset_size;
613
614   if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
615                       stash->syms, offset,
616                       &stash->dwarf_str_buffer, &stash->dwarf_str_size))
617     return NULL;
618
619   str = (char *) stash->dwarf_str_buffer + offset;
620   if (*str == '\0')
621     return NULL;
622   return str;
623 }
624
625 static bfd_uint64_t
626 read_address (struct comp_unit *unit, bfd_byte *buf)
627 {
628   int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
629
630   if (signed_vma)
631     {
632       switch (unit->addr_size)
633         {
634         case 8:
635           return bfd_get_signed_64 (unit->abfd, buf);
636         case 4:
637           return bfd_get_signed_32 (unit->abfd, buf);
638         case 2:
639           return bfd_get_signed_16 (unit->abfd, buf);
640         default:
641           abort ();
642         }
643     }
644   else
645     {
646       switch (unit->addr_size)
647         {
648         case 8:
649           return bfd_get_64 (unit->abfd, buf);
650         case 4:
651           return bfd_get_32 (unit->abfd, buf);
652         case 2:
653           return bfd_get_16 (unit->abfd, buf);
654         default:
655           abort ();
656         }
657     }
658 }
659
660 /* Lookup an abbrev_info structure in the abbrev hash table.  */
661
662 static struct abbrev_info *
663 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
664 {
665   unsigned int hash_number;
666   struct abbrev_info *abbrev;
667
668   hash_number = number % ABBREV_HASH_SIZE;
669   abbrev = abbrevs[hash_number];
670
671   while (abbrev)
672     {
673       if (abbrev->number == number)
674         return abbrev;
675       else
676         abbrev = abbrev->next;
677     }
678
679   return NULL;
680 }
681
682 /* In DWARF version 2, the description of the debugging information is
683    stored in a separate .debug_abbrev section.  Before we read any
684    dies from a section we read in all abbreviations and install them
685    in a hash table.  */
686
687 static struct abbrev_info**
688 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
689 {
690   struct abbrev_info **abbrevs;
691   bfd_byte *abbrev_ptr;
692   struct abbrev_info *cur_abbrev;
693   unsigned int abbrev_number, bytes_read, abbrev_name;
694   unsigned int abbrev_form, hash_number;
695   bfd_size_type amt;
696
697   if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
698                       stash->syms, offset,
699                       &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
700     return NULL;
701
702   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
703   abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
704   if (abbrevs == NULL)
705     return NULL;
706
707   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
708   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
709   abbrev_ptr += bytes_read;
710
711   /* Loop until we reach an abbrev number of 0.  */
712   while (abbrev_number)
713     {
714       amt = sizeof (struct abbrev_info);
715       cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
716       if (cur_abbrev == NULL)
717         return NULL;
718
719       /* Read in abbrev header.  */
720       cur_abbrev->number = abbrev_number;
721       cur_abbrev->tag = (enum dwarf_tag)
722         read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
723       abbrev_ptr += bytes_read;
724       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
725       abbrev_ptr += 1;
726
727       /* Now read in declarations.  */
728       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
729       abbrev_ptr += bytes_read;
730       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
731       abbrev_ptr += bytes_read;
732
733       while (abbrev_name)
734         {
735           if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
736             {
737               struct attr_abbrev *tmp;
738
739               amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
740               amt *= sizeof (struct attr_abbrev);
741               tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
742               if (tmp == NULL)
743                 {
744                   size_t i;
745
746                   for (i = 0; i < ABBREV_HASH_SIZE; i++)
747                     {
748                       struct abbrev_info *abbrev = abbrevs[i];
749
750                       while (abbrev)
751                         {
752                           free (abbrev->attrs);
753                           abbrev = abbrev->next;
754                         }
755                     }
756                   return NULL;
757                 }
758               cur_abbrev->attrs = tmp;
759             }
760
761           cur_abbrev->attrs[cur_abbrev->num_attrs].name
762             = (enum dwarf_attribute) abbrev_name;
763           cur_abbrev->attrs[cur_abbrev->num_attrs++].form
764             = (enum dwarf_form) abbrev_form;
765           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
766           abbrev_ptr += bytes_read;
767           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
768           abbrev_ptr += bytes_read;
769         }
770
771       hash_number = abbrev_number % ABBREV_HASH_SIZE;
772       cur_abbrev->next = abbrevs[hash_number];
773       abbrevs[hash_number] = cur_abbrev;
774
775       /* Get next abbreviation.
776          Under Irix6 the abbreviations for a compilation unit are not
777          always properly terminated with an abbrev number of 0.
778          Exit loop if we encounter an abbreviation which we have
779          already read (which means we are about to read the abbreviations
780          for the next compile unit) or if the end of the abbreviation
781          table is reached.  */
782       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
783           >= stash->dwarf_abbrev_size)
784         break;
785       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
786       abbrev_ptr += bytes_read;
787       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
788         break;
789     }
790
791   return abbrevs;
792 }
793
794 /* Read an attribute value described by an attribute form.  */
795
796 static bfd_byte *
797 read_attribute_value (struct attribute *attr,
798                       unsigned form,
799                       struct comp_unit *unit,
800                       bfd_byte *info_ptr)
801 {
802   bfd *abfd = unit->abfd;
803   unsigned int bytes_read;
804   struct dwarf_block *blk;
805   bfd_size_type amt;
806
807   attr->form = (enum dwarf_form) form;
808
809   switch (form)
810     {
811     case DW_FORM_ref_addr:
812       /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
813          DWARF3.  */
814       if (unit->version == 3 || unit->version == 4)
815         {
816           if (unit->offset_size == 4)
817             attr->u.val = read_4_bytes (unit->abfd, info_ptr);
818           else
819             attr->u.val = read_8_bytes (unit->abfd, info_ptr);
820           info_ptr += unit->offset_size;
821           break;
822         }
823       /* FALLTHROUGH */
824     case DW_FORM_addr:
825       attr->u.val = read_address (unit, info_ptr);
826       info_ptr += unit->addr_size;
827       break;
828     case DW_FORM_sec_offset:
829       if (unit->offset_size == 4)
830         attr->u.val = read_4_bytes (unit->abfd, info_ptr);
831       else
832         attr->u.val = read_8_bytes (unit->abfd, info_ptr);
833       info_ptr += unit->offset_size;
834       break;
835     case DW_FORM_block2:
836       amt = sizeof (struct dwarf_block);
837       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
838       if (blk == NULL)
839         return NULL;
840       blk->size = read_2_bytes (abfd, info_ptr);
841       info_ptr += 2;
842       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
843       info_ptr += blk->size;
844       attr->u.blk = blk;
845       break;
846     case DW_FORM_block4:
847       amt = sizeof (struct dwarf_block);
848       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
849       if (blk == NULL)
850         return NULL;
851       blk->size = read_4_bytes (abfd, info_ptr);
852       info_ptr += 4;
853       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
854       info_ptr += blk->size;
855       attr->u.blk = blk;
856       break;
857     case DW_FORM_data2:
858       attr->u.val = read_2_bytes (abfd, info_ptr);
859       info_ptr += 2;
860       break;
861     case DW_FORM_data4:
862       attr->u.val = read_4_bytes (abfd, info_ptr);
863       info_ptr += 4;
864       break;
865     case DW_FORM_data8:
866       attr->u.val = read_8_bytes (abfd, info_ptr);
867       info_ptr += 8;
868       break;
869     case DW_FORM_string:
870       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
871       info_ptr += bytes_read;
872       break;
873     case DW_FORM_strp:
874       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
875       info_ptr += bytes_read;
876       break;
877     case DW_FORM_exprloc:
878     case DW_FORM_block:
879       amt = sizeof (struct dwarf_block);
880       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
881       if (blk == NULL)
882         return NULL;
883       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
884       info_ptr += bytes_read;
885       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
886       info_ptr += blk->size;
887       attr->u.blk = blk;
888       break;
889     case DW_FORM_block1:
890       amt = sizeof (struct dwarf_block);
891       blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
892       if (blk == NULL)
893         return NULL;
894       blk->size = read_1_byte (abfd, info_ptr);
895       info_ptr += 1;
896       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
897       info_ptr += blk->size;
898       attr->u.blk = blk;
899       break;
900     case DW_FORM_data1:
901       attr->u.val = read_1_byte (abfd, info_ptr);
902       info_ptr += 1;
903       break;
904     case DW_FORM_flag:
905       attr->u.val = read_1_byte (abfd, info_ptr);
906       info_ptr += 1;
907       break;
908     case DW_FORM_flag_present:
909       attr->u.val = 1;
910       break;
911     case DW_FORM_sdata:
912       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
913       info_ptr += bytes_read;
914       break;
915     case DW_FORM_udata:
916       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
917       info_ptr += bytes_read;
918       break;
919     case DW_FORM_ref1:
920       attr->u.val = read_1_byte (abfd, info_ptr);
921       info_ptr += 1;
922       break;
923     case DW_FORM_ref2:
924       attr->u.val = read_2_bytes (abfd, info_ptr);
925       info_ptr += 2;
926       break;
927     case DW_FORM_ref4:
928       attr->u.val = read_4_bytes (abfd, info_ptr);
929       info_ptr += 4;
930       break;
931     case DW_FORM_ref8:
932       attr->u.val = read_8_bytes (abfd, info_ptr);
933       info_ptr += 8;
934       break;
935     case DW_FORM_ref_sig8:
936       attr->u.val = read_8_bytes (abfd, info_ptr);
937       info_ptr += 8;
938       break;
939     case DW_FORM_ref_udata:
940       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
941       info_ptr += bytes_read;
942       break;
943     case DW_FORM_indirect:
944       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
945       info_ptr += bytes_read;
946       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
947       break;
948     default:
949       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
950                              form);
951       bfd_set_error (bfd_error_bad_value);
952       return NULL;
953     }
954   return info_ptr;
955 }
956
957 /* Read an attribute described by an abbreviated attribute.  */
958
959 static bfd_byte *
960 read_attribute (struct attribute *attr,
961                 struct attr_abbrev *abbrev,
962                 struct comp_unit *unit,
963                 bfd_byte *info_ptr)
964 {
965   attr->name = abbrev->name;
966   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
967   return info_ptr;
968 }
969
970 /* Source line information table routines.  */
971
972 #define FILE_ALLOC_CHUNK 5
973 #define DIR_ALLOC_CHUNK 5
974
975 struct line_info
976 {
977   struct line_info* prev_line;
978   bfd_vma address;
979   char *filename;
980   unsigned int line;
981   unsigned int column;
982   unsigned char op_index;
983   unsigned char end_sequence;           /* End of (sequential) code sequence.  */
984 };
985
986 struct fileinfo
987 {
988   char *name;
989   unsigned int dir;
990   unsigned int time;
991   unsigned int size;
992 };
993
994 struct line_sequence
995 {
996   bfd_vma               low_pc;
997   struct line_sequence* prev_sequence;
998   struct line_info*     last_line;  /* Largest VMA.  */
999 };
1000
1001 struct line_info_table
1002 {
1003   bfd*                  abfd;
1004   unsigned int          num_files;
1005   unsigned int          num_dirs;
1006   unsigned int          num_sequences;
1007   char *                comp_dir;
1008   char **               dirs;
1009   struct fileinfo*      files;
1010   struct line_sequence* sequences;
1011   struct line_info*     lcl_head;   /* Local head; used in 'add_line_info'.  */
1012 };
1013
1014 /* Remember some information about each function.  If the function is
1015    inlined (DW_TAG_inlined_subroutine) it may have two additional
1016    attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1017    source code location where this function was inlined. */
1018
1019 struct funcinfo
1020 {
1021   struct funcinfo *prev_func;           /* Pointer to previous function in list of all functions */
1022   struct funcinfo *caller_func;         /* Pointer to function one scope higher */
1023   char *caller_file;                    /* Source location file name where caller_func inlines this func */
1024   int caller_line;                      /* Source location line number where caller_func inlines this func */
1025   char *file;                           /* Source location file name */
1026   int line;                             /* Source location line number */
1027   int tag;
1028   char *name;
1029   struct arange arange;
1030   asection *sec;                        /* Where the symbol is defined */
1031 };
1032
1033 struct varinfo
1034 {
1035   /* Pointer to previous variable in list of all variables */
1036   struct varinfo *prev_var;
1037   /* Source location file name */
1038   char *file;
1039   /* Source location line number */
1040   int line;
1041   int tag;
1042   char *name;
1043   bfd_vma addr;
1044   /* Where the symbol is defined */
1045   asection *sec;
1046   /* Is this a stack variable? */
1047   unsigned int stack: 1;
1048 };
1049
1050 /* Return TRUE if NEW_LINE should sort after LINE.  */
1051
1052 static inline bfd_boolean
1053 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1054 {
1055   return (new_line->address > line->address
1056           || (new_line->address == line->address
1057               && (new_line->op_index > line->op_index
1058                   || (new_line->op_index == line->op_index
1059                       && new_line->end_sequence < line->end_sequence))));
1060 }
1061
1062
1063 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1064    that the list is sorted.  Note that the line_info list is sorted from
1065    highest to lowest VMA (with possible duplicates); that is,
1066    line_info->prev_line always accesses an equal or smaller VMA.  */
1067
1068 static bfd_boolean
1069 add_line_info (struct line_info_table *table,
1070                bfd_vma address,
1071                unsigned char op_index,
1072                char *filename,
1073                unsigned int line,
1074                unsigned int column,
1075                int end_sequence)
1076 {
1077   bfd_size_type amt = sizeof (struct line_info);
1078   struct line_sequence* seq = table->sequences;
1079   struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1080
1081   if (info == NULL)
1082     return FALSE;
1083
1084   /* Set member data of 'info'.  */
1085   info->prev_line = NULL;
1086   info->address = address;
1087   info->op_index = op_index;
1088   info->line = line;
1089   info->column = column;
1090   info->end_sequence = end_sequence;
1091
1092   if (filename && filename[0])
1093     {
1094       info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1095       if (info->filename == NULL)
1096         return FALSE;
1097       strcpy (info->filename, filename);
1098     }
1099   else
1100     info->filename = NULL;
1101
1102   /* Find the correct location for 'info'.  Normally we will receive
1103      new line_info data 1) in order and 2) with increasing VMAs.
1104      However some compilers break the rules (cf. decode_line_info) and
1105      so we include some heuristics for quickly finding the correct
1106      location for 'info'. In particular, these heuristics optimize for
1107      the common case in which the VMA sequence that we receive is a
1108      list of locally sorted VMAs such as
1109        p...z a...j  (where a < j < p < z)
1110
1111      Note: table->lcl_head is used to head an *actual* or *possible*
1112      sub-sequence within the list (such as a...j) that is not directly
1113      headed by table->last_line
1114
1115      Note: we may receive duplicate entries from 'decode_line_info'.  */
1116
1117   if (seq
1118       && seq->last_line->address == address
1119       && seq->last_line->op_index == op_index
1120       && seq->last_line->end_sequence == end_sequence)
1121     {
1122       /* We only keep the last entry with the same address and end
1123          sequence.  See PR ld/4986.  */
1124       if (table->lcl_head == seq->last_line)
1125         table->lcl_head = info;
1126       info->prev_line = seq->last_line->prev_line;
1127       seq->last_line = info;
1128     }
1129   else if (!seq || seq->last_line->end_sequence)
1130     {
1131       /* Start a new line sequence.  */
1132       amt = sizeof (struct line_sequence);
1133       seq = (struct line_sequence *) bfd_malloc (amt);
1134       if (seq == NULL)
1135         return FALSE;
1136       seq->low_pc = address;
1137       seq->prev_sequence = table->sequences;
1138       seq->last_line = info;
1139       table->lcl_head = info;
1140       table->sequences = seq;
1141       table->num_sequences++;
1142     }
1143   else if (new_line_sorts_after (info, seq->last_line))
1144     {
1145       /* Normal case: add 'info' to the beginning of the current sequence.  */
1146       info->prev_line = seq->last_line;
1147       seq->last_line = info;
1148
1149       /* lcl_head: initialize to head a *possible* sequence at the end.  */
1150       if (!table->lcl_head)
1151         table->lcl_head = info;
1152     }
1153   else if (!new_line_sorts_after (info, table->lcl_head)
1154            && (!table->lcl_head->prev_line
1155                || new_line_sorts_after (info, table->lcl_head->prev_line)))
1156     {
1157       /* Abnormal but easy: lcl_head is the head of 'info'.  */
1158       info->prev_line = table->lcl_head->prev_line;
1159       table->lcl_head->prev_line = info;
1160     }
1161   else
1162     {
1163       /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1164          are valid heads for 'info'.  Reset 'lcl_head'.  */
1165       struct line_info* li2 = seq->last_line; /* Always non-NULL.  */
1166       struct line_info* li1 = li2->prev_line;
1167
1168       while (li1)
1169         {
1170           if (!new_line_sorts_after (info, li2)
1171               && new_line_sorts_after (info, li1))
1172             break;
1173
1174           li2 = li1; /* always non-NULL */
1175           li1 = li1->prev_line;
1176         }
1177       table->lcl_head = li2;
1178       info->prev_line = table->lcl_head->prev_line;
1179       table->lcl_head->prev_line = info;
1180       if (address < seq->low_pc)
1181         seq->low_pc = address;
1182     }
1183   return TRUE;
1184 }
1185
1186 /* Extract a fully qualified filename from a line info table.
1187    The returned string has been malloc'ed and it is the caller's
1188    responsibility to free it.  */
1189
1190 static char *
1191 concat_filename (struct line_info_table *table, unsigned int file)
1192 {
1193   char *filename;
1194
1195   if (file - 1 >= table->num_files)
1196     {
1197       /* FILE == 0 means unknown.  */
1198       if (file)
1199         (*_bfd_error_handler)
1200           (_("Dwarf Error: mangled line number section (bad file number)."));
1201       return strdup ("<unknown>");
1202     }
1203
1204   filename = table->files[file - 1].name;
1205
1206   if (!IS_ABSOLUTE_PATH (filename))
1207     {
1208       char *dir_name = NULL;
1209       char *subdir_name = NULL;
1210       char *name;
1211       size_t len;
1212
1213       if (table->files[file - 1].dir)
1214         subdir_name = table->dirs[table->files[file - 1].dir - 1];
1215
1216       if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1217         dir_name = table->comp_dir;
1218
1219       if (!dir_name)
1220         {
1221           dir_name = subdir_name;
1222           subdir_name = NULL;
1223         }
1224
1225       if (!dir_name)
1226         return strdup (filename);
1227
1228       len = strlen (dir_name) + strlen (filename) + 2;
1229
1230       if (subdir_name)
1231         {
1232           len += strlen (subdir_name) + 1;
1233           name = (char *) bfd_malloc (len);
1234           if (name)
1235             sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1236         }
1237       else
1238         {
1239           name = (char *) bfd_malloc (len);
1240           if (name)
1241             sprintf (name, "%s/%s", dir_name, filename);
1242         }
1243
1244       return name;
1245     }
1246
1247   return strdup (filename);
1248 }
1249
1250 static bfd_boolean
1251 arange_add (bfd *abfd, struct arange *first_arange,
1252             bfd_vma low_pc, bfd_vma high_pc)
1253 {
1254   struct arange *arange;
1255
1256   /* If the first arange is empty, use it. */
1257   if (first_arange->high == 0)
1258     {
1259       first_arange->low = low_pc;
1260       first_arange->high = high_pc;
1261       return TRUE;
1262     }
1263
1264   /* Next see if we can cheaply extend an existing range.  */
1265   arange = first_arange;
1266   do
1267     {
1268       if (low_pc == arange->high)
1269         {
1270           arange->high = high_pc;
1271           return TRUE;
1272         }
1273       if (high_pc == arange->low)
1274         {
1275           arange->low = low_pc;
1276           return TRUE;
1277         }
1278       arange = arange->next;
1279     }
1280   while (arange);
1281
1282   /* Need to allocate a new arange and insert it into the arange list.
1283      Order isn't significant, so just insert after the first arange. */
1284   arange = (struct arange *) bfd_zalloc (abfd, sizeof (*arange));
1285   if (arange == NULL)
1286     return FALSE;
1287   arange->low = low_pc;
1288   arange->high = high_pc;
1289   arange->next = first_arange->next;
1290   first_arange->next = arange;
1291   return TRUE;
1292 }
1293
1294 /* Compare function for line sequences.  */
1295
1296 static int
1297 compare_sequences (const void* a, const void* b)
1298 {
1299   const struct line_sequence* seq1 = a;
1300   const struct line_sequence* seq2 = b;
1301
1302   /* Sort by low_pc as the primary key.  */
1303   if (seq1->low_pc < seq2->low_pc)
1304     return -1;
1305   if (seq1->low_pc > seq2->low_pc)
1306     return 1;
1307
1308   /* If low_pc values are equal, sort in reverse order of
1309      high_pc, so that the largest region comes first.  */
1310   if (seq1->last_line->address < seq2->last_line->address)
1311     return 1;
1312   if (seq1->last_line->address > seq2->last_line->address)
1313     return -1;
1314
1315   if (seq1->last_line->op_index < seq2->last_line->op_index)
1316     return 1;
1317   if (seq1->last_line->op_index > seq2->last_line->op_index)
1318     return -1;
1319
1320   return 0;
1321 }
1322
1323 /* Sort the line sequences for quick lookup.  */
1324
1325 static bfd_boolean
1326 sort_line_sequences (struct line_info_table* table)
1327 {
1328   bfd_size_type amt;
1329   struct line_sequence* sequences;
1330   struct line_sequence* seq;
1331   unsigned int n = 0;
1332   unsigned int num_sequences = table->num_sequences;
1333   bfd_vma last_high_pc;
1334
1335   if (num_sequences == 0)
1336     return TRUE;
1337
1338   /* Allocate space for an array of sequences.  */
1339   amt = sizeof (struct line_sequence) * num_sequences;
1340   sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1341   if (sequences == NULL)
1342     return FALSE;
1343
1344   /* Copy the linked list into the array, freeing the original nodes.  */
1345   seq = table->sequences;
1346   for (n = 0; n < num_sequences; n++)
1347     {
1348       struct line_sequence* last_seq = seq;
1349
1350       BFD_ASSERT (seq);
1351       sequences[n].low_pc = seq->low_pc;
1352       sequences[n].prev_sequence = NULL;
1353       sequences[n].last_line = seq->last_line;
1354       seq = seq->prev_sequence;
1355       free (last_seq);
1356     }
1357   BFD_ASSERT (seq == NULL);
1358
1359   qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1360
1361   /* Make the list binary-searchable by trimming overlapping entries
1362      and removing nested entries.  */
1363   num_sequences = 1;
1364   last_high_pc = sequences[0].last_line->address;
1365   for (n = 1; n < table->num_sequences; n++)
1366     {
1367       if (sequences[n].low_pc < last_high_pc)
1368         {
1369           if (sequences[n].last_line->address <= last_high_pc)
1370             /* Skip nested entries.  */
1371             continue;
1372
1373           /* Trim overlapping entries.  */
1374           sequences[n].low_pc = last_high_pc;
1375         }
1376       last_high_pc = sequences[n].last_line->address;
1377       if (n > num_sequences)
1378         {
1379           /* Close up the gap.  */
1380           sequences[num_sequences].low_pc = sequences[n].low_pc;
1381           sequences[num_sequences].last_line = sequences[n].last_line;
1382         }
1383       num_sequences++;
1384     }
1385
1386   table->sequences = sequences;
1387   table->num_sequences = num_sequences;
1388   return TRUE;
1389 }
1390
1391 /* Decode the line number information for UNIT.  */
1392
1393 static struct line_info_table*
1394 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1395 {
1396   bfd *abfd = unit->abfd;
1397   struct line_info_table* table;
1398   bfd_byte *line_ptr;
1399   bfd_byte *line_end;
1400   struct line_head lh;
1401   unsigned int i, bytes_read, offset_size;
1402   char *cur_file, *cur_dir;
1403   unsigned char op_code, extended_op, adj_opcode;
1404   unsigned int exop_len;
1405   bfd_size_type amt;
1406
1407   if (! read_section (abfd, &stash->debug_sections[debug_line],
1408                       stash->syms, unit->line_offset,
1409                       &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1410     return NULL;
1411
1412   amt = sizeof (struct line_info_table);
1413   table = (struct line_info_table *) bfd_alloc (abfd, amt);
1414   if (table == NULL)
1415     return NULL;
1416   table->abfd = abfd;
1417   table->comp_dir = unit->comp_dir;
1418
1419   table->num_files = 0;
1420   table->files = NULL;
1421
1422   table->num_dirs = 0;
1423   table->dirs = NULL;
1424
1425   table->num_sequences = 0;
1426   table->sequences = NULL;
1427
1428   table->lcl_head = NULL;
1429
1430   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1431
1432   /* Read in the prologue.  */
1433   lh.total_length = read_4_bytes (abfd, line_ptr);
1434   line_ptr += 4;
1435   offset_size = 4;
1436   if (lh.total_length == 0xffffffff)
1437     {
1438       lh.total_length = read_8_bytes (abfd, line_ptr);
1439       line_ptr += 8;
1440       offset_size = 8;
1441     }
1442   else if (lh.total_length == 0 && unit->addr_size == 8)
1443     {
1444       /* Handle (non-standard) 64-bit DWARF2 formats.  */
1445       lh.total_length = read_4_bytes (abfd, line_ptr);
1446       line_ptr += 4;
1447       offset_size = 8;
1448     }
1449   line_end = line_ptr + lh.total_length;
1450   lh.version = read_2_bytes (abfd, line_ptr);
1451   if (lh.version < 2 || lh.version > 4)
1452     {
1453       (*_bfd_error_handler)
1454         (_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
1455       bfd_set_error (bfd_error_bad_value);
1456       return NULL;
1457     }
1458   line_ptr += 2;
1459   if (offset_size == 4)
1460     lh.prologue_length = read_4_bytes (abfd, line_ptr);
1461   else
1462     lh.prologue_length = read_8_bytes (abfd, line_ptr);
1463   line_ptr += offset_size;
1464   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1465   line_ptr += 1;
1466   if (lh.version >= 4)
1467     {
1468       lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr);
1469       line_ptr += 1;
1470     }
1471   else
1472     lh.maximum_ops_per_insn = 1;
1473   if (lh.maximum_ops_per_insn == 0)
1474     {
1475       (*_bfd_error_handler)
1476         (_("Dwarf Error: Invalid maximum operations per instruction."));
1477       bfd_set_error (bfd_error_bad_value);
1478       return NULL;
1479     }
1480   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1481   line_ptr += 1;
1482   lh.line_base = read_1_signed_byte (abfd, line_ptr);
1483   line_ptr += 1;
1484   lh.line_range = read_1_byte (abfd, line_ptr);
1485   line_ptr += 1;
1486   lh.opcode_base = read_1_byte (abfd, line_ptr);
1487   line_ptr += 1;
1488   amt = lh.opcode_base * sizeof (unsigned char);
1489   lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1490
1491   lh.standard_opcode_lengths[0] = 1;
1492
1493   for (i = 1; i < lh.opcode_base; ++i)
1494     {
1495       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1496       line_ptr += 1;
1497     }
1498
1499   /* Read directory table.  */
1500   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1501     {
1502       line_ptr += bytes_read;
1503
1504       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1505         {
1506           char **tmp;
1507
1508           amt = table->num_dirs + DIR_ALLOC_CHUNK;
1509           amt *= sizeof (char *);
1510
1511           tmp = (char **) bfd_realloc (table->dirs, amt);
1512           if (tmp == NULL)
1513             goto fail;
1514           table->dirs = tmp;
1515         }
1516
1517       table->dirs[table->num_dirs++] = cur_dir;
1518     }
1519
1520   line_ptr += bytes_read;
1521
1522   /* Read file name table.  */
1523   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1524     {
1525       line_ptr += bytes_read;
1526
1527       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1528         {
1529           struct fileinfo *tmp;
1530
1531           amt = table->num_files + FILE_ALLOC_CHUNK;
1532           amt *= sizeof (struct fileinfo);
1533
1534           tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1535           if (tmp == NULL)
1536             goto fail;
1537           table->files = tmp;
1538         }
1539
1540       table->files[table->num_files].name = cur_file;
1541       table->files[table->num_files].dir =
1542         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1543       line_ptr += bytes_read;
1544       table->files[table->num_files].time =
1545         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1546       line_ptr += bytes_read;
1547       table->files[table->num_files].size =
1548         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1549       line_ptr += bytes_read;
1550       table->num_files++;
1551     }
1552
1553   line_ptr += bytes_read;
1554
1555   /* Read the statement sequences until there's nothing left.  */
1556   while (line_ptr < line_end)
1557     {
1558       /* State machine registers.  */
1559       bfd_vma address = 0;
1560       unsigned char op_index = 0;
1561       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1562       unsigned int line = 1;
1563       unsigned int column = 0;
1564       int is_stmt = lh.default_is_stmt;
1565       int end_sequence = 0;
1566       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1567          compilers generate address sequences that are wildly out of
1568          order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1569          for ia64-Linux).  Thus, to determine the low and high
1570          address, we must compare on every DW_LNS_copy, etc.  */
1571       bfd_vma low_pc  = (bfd_vma) -1;
1572       bfd_vma high_pc = 0;
1573
1574       /* Decode the table.  */
1575       while (! end_sequence)
1576         {
1577           op_code = read_1_byte (abfd, line_ptr);
1578           line_ptr += 1;
1579
1580           if (op_code >= lh.opcode_base)
1581             {
1582               /* Special operand.  */
1583               adj_opcode = op_code - lh.opcode_base;
1584               if (lh.maximum_ops_per_insn == 1)
1585                 address += (adj_opcode / lh.line_range)
1586                            * lh.minimum_instruction_length;
1587               else
1588                 {
1589                   address += ((op_index + (adj_opcode / lh.line_range))
1590                               / lh.maximum_ops_per_insn)
1591                              * lh.minimum_instruction_length;
1592                   op_index = (op_index + (adj_opcode / lh.line_range))
1593                              % lh.maximum_ops_per_insn;
1594                 }
1595               line += lh.line_base + (adj_opcode % lh.line_range);
1596               /* Append row to matrix using current values.  */
1597               if (!add_line_info (table, address, op_index, filename,
1598                                   line, column, 0))
1599                 goto line_fail;
1600               if (address < low_pc)
1601                 low_pc = address;
1602               if (address > high_pc)
1603                 high_pc = address;
1604             }
1605           else switch (op_code)
1606             {
1607             case DW_LNS_extended_op:
1608               exop_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1609               line_ptr += bytes_read;
1610               extended_op = read_1_byte (abfd, line_ptr);
1611               line_ptr += 1;
1612
1613               switch (extended_op)
1614                 {
1615                 case DW_LNE_end_sequence:
1616                   end_sequence = 1;
1617                   if (!add_line_info (table, address, op_index, filename,
1618                                       line, column, end_sequence))
1619                     goto line_fail;
1620                   if (address < low_pc)
1621                     low_pc = address;
1622                   if (address > high_pc)
1623                     high_pc = address;
1624                   if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc))
1625                     goto line_fail;
1626                   break;
1627                 case DW_LNE_set_address:
1628                   address = read_address (unit, line_ptr);
1629                   op_index = 0;
1630                   line_ptr += unit->addr_size;
1631                   break;
1632                 case DW_LNE_define_file:
1633                   cur_file = read_string (abfd, line_ptr, &bytes_read);
1634                   line_ptr += bytes_read;
1635                   if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1636                     {
1637                       struct fileinfo *tmp;
1638
1639                       amt = table->num_files + FILE_ALLOC_CHUNK;
1640                       amt *= sizeof (struct fileinfo);
1641                       tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1642                       if (tmp == NULL)
1643                         goto line_fail;
1644                       table->files = tmp;
1645                     }
1646                   table->files[table->num_files].name = cur_file;
1647                   table->files[table->num_files].dir =
1648                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1649                   line_ptr += bytes_read;
1650                   table->files[table->num_files].time =
1651                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1652                   line_ptr += bytes_read;
1653                   table->files[table->num_files].size =
1654                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1655                   line_ptr += bytes_read;
1656                   table->num_files++;
1657                   break;
1658                 case DW_LNE_set_discriminator:
1659                   (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1660                   line_ptr += bytes_read;
1661                   break;
1662                 case DW_LNE_HP_source_file_correlation:
1663                   line_ptr += exop_len - 1;
1664                   break;
1665                 default:
1666                   (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1667                   bfd_set_error (bfd_error_bad_value);
1668                 line_fail:
1669                   if (filename != NULL)
1670                     free (filename);
1671                   goto fail;
1672                 }
1673               break;
1674             case DW_LNS_copy:
1675               if (!add_line_info (table, address, op_index,
1676                                   filename, line, column, 0))
1677                 goto line_fail;
1678               if (address < low_pc)
1679                 low_pc = address;
1680               if (address > high_pc)
1681                 high_pc = address;
1682               break;
1683             case DW_LNS_advance_pc:
1684               if (lh.maximum_ops_per_insn == 1)
1685                 address += lh.minimum_instruction_length
1686                            * read_unsigned_leb128 (abfd, line_ptr,
1687                                                    &bytes_read);
1688               else
1689                 {
1690                   bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr,
1691                                                          &bytes_read);
1692                   address = ((op_index + adjust) / lh.maximum_ops_per_insn)
1693                             * lh.minimum_instruction_length;
1694                   op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1695                 }
1696               line_ptr += bytes_read;
1697               break;
1698             case DW_LNS_advance_line:
1699               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1700               line_ptr += bytes_read;
1701               break;
1702             case DW_LNS_set_file:
1703               {
1704                 unsigned int file;
1705
1706                 /* The file and directory tables are 0
1707                    based, the references are 1 based.  */
1708                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1709                 line_ptr += bytes_read;
1710                 if (filename)
1711                   free (filename);
1712                 filename = concat_filename (table, file);
1713                 break;
1714               }
1715             case DW_LNS_set_column:
1716               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1717               line_ptr += bytes_read;
1718               break;
1719             case DW_LNS_negate_stmt:
1720               is_stmt = (!is_stmt);
1721               break;
1722             case DW_LNS_set_basic_block:
1723               break;
1724             case DW_LNS_const_add_pc:
1725               if (lh.maximum_ops_per_insn == 1)
1726                 address += lh.minimum_instruction_length
1727                            * ((255 - lh.opcode_base) / lh.line_range);
1728               else
1729                 {
1730                   bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
1731                   address += lh.minimum_instruction_length
1732                              * ((op_index + adjust) / lh.maximum_ops_per_insn);
1733                   op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1734                 }
1735               break;
1736             case DW_LNS_fixed_advance_pc:
1737               address += read_2_bytes (abfd, line_ptr);
1738               op_index = 0;
1739               line_ptr += 2;
1740               break;
1741             default:
1742               /* Unknown standard opcode, ignore it.  */
1743               for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1744                 {
1745                   (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1746                   line_ptr += bytes_read;
1747                 }
1748               break;
1749             }
1750         }
1751
1752       if (filename)
1753         free (filename);
1754     }
1755
1756   if (sort_line_sequences (table))
1757     return table;
1758
1759  fail:
1760   if (table->sequences != NULL)
1761     free (table->sequences);
1762   if (table->files != NULL)
1763     free (table->files);
1764   if (table->dirs != NULL)
1765     free (table->dirs);
1766   return NULL;
1767 }
1768
1769 /* If ADDR is within TABLE set the output parameters and return TRUE,
1770    otherwise return FALSE.  The output parameters, FILENAME_PTR and
1771    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
1772
1773 static bfd_boolean
1774 lookup_address_in_line_info_table (struct line_info_table *table,
1775                                    bfd_vma addr,
1776                                    const char **filename_ptr,
1777                                    unsigned int *linenumber_ptr)
1778 {
1779   struct line_sequence *seq = NULL;
1780   struct line_info *each_line;
1781   int low, high, mid;
1782
1783   /* Binary search the array of sequences.  */
1784   low = 0;
1785   high = table->num_sequences;
1786   while (low < high)
1787     {
1788       mid = (low + high) / 2;
1789       seq = &table->sequences[mid];
1790       if (addr < seq->low_pc)
1791         high = mid;
1792       else if (addr >= seq->last_line->address)
1793         low = mid + 1;
1794       else
1795         break;
1796     }
1797
1798   if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
1799     {
1800       /* Note: seq->last_line should be a descendingly sorted list.  */
1801       for (each_line = seq->last_line;
1802            each_line;
1803            each_line = each_line->prev_line)
1804         if (addr >= each_line->address)
1805           break;
1806
1807       if (each_line
1808           && !(each_line->end_sequence || each_line == seq->last_line))
1809         {
1810           *filename_ptr = each_line->filename;
1811           *linenumber_ptr = each_line->line;
1812           return TRUE;
1813         }
1814     }
1815
1816   *filename_ptr = NULL;
1817   return FALSE;
1818 }
1819
1820 /* Read in the .debug_ranges section for future reference.  */
1821
1822 static bfd_boolean
1823 read_debug_ranges (struct comp_unit *unit)
1824 {
1825   struct dwarf2_debug *stash = unit->stash;
1826   return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
1827                        stash->syms, 0,
1828                        &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1829 }
1830
1831 /* Function table functions.  */
1832
1833 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1834    Note that we need to find the function that has the smallest
1835    range that contains ADDR, to handle inlined functions without
1836    depending upon them being ordered in TABLE by increasing range. */
1837
1838 static bfd_boolean
1839 lookup_address_in_function_table (struct comp_unit *unit,
1840                                   bfd_vma addr,
1841                                   struct funcinfo **function_ptr,
1842                                   const char **functionname_ptr)
1843 {
1844   struct funcinfo* each_func;
1845   struct funcinfo* best_fit = NULL;
1846   struct arange *arange;
1847
1848   for (each_func = unit->function_table;
1849        each_func;
1850        each_func = each_func->prev_func)
1851     {
1852       for (arange = &each_func->arange;
1853            arange;
1854            arange = arange->next)
1855         {
1856           if (addr >= arange->low && addr < arange->high)
1857             {
1858               if (!best_fit ||
1859                   ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1860                 best_fit = each_func;
1861             }
1862         }
1863     }
1864
1865   if (best_fit)
1866     {
1867       *functionname_ptr = best_fit->name;
1868       *function_ptr = best_fit;
1869       return TRUE;
1870     }
1871   else
1872     {
1873       return FALSE;
1874     }
1875 }
1876
1877 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1878    and LINENUMBER_PTR, and return TRUE.  */
1879
1880 static bfd_boolean
1881 lookup_symbol_in_function_table (struct comp_unit *unit,
1882                                  asymbol *sym,
1883                                  bfd_vma addr,
1884                                  const char **filename_ptr,
1885                                  unsigned int *linenumber_ptr)
1886 {
1887   struct funcinfo* each_func;
1888   struct funcinfo* best_fit = NULL;
1889   struct arange *arange;
1890   const char *name = bfd_asymbol_name (sym);
1891   asection *sec = bfd_get_section (sym);
1892
1893   for (each_func = unit->function_table;
1894        each_func;
1895        each_func = each_func->prev_func)
1896     {
1897       for (arange = &each_func->arange;
1898            arange;
1899            arange = arange->next)
1900         {
1901           if ((!each_func->sec || each_func->sec == sec)
1902               && addr >= arange->low
1903               && addr < arange->high
1904               && each_func->name
1905               && strcmp (name, each_func->name) == 0
1906               && (!best_fit
1907                   || ((arange->high - arange->low)
1908                       < (best_fit->arange.high - best_fit->arange.low))))
1909             best_fit = each_func;
1910         }
1911     }
1912
1913   if (best_fit)
1914     {
1915       best_fit->sec = sec;
1916       *filename_ptr = best_fit->file;
1917       *linenumber_ptr = best_fit->line;
1918       return TRUE;
1919     }
1920   else
1921     return FALSE;
1922 }
1923
1924 /* Variable table functions.  */
1925
1926 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1927    LINENUMBER_PTR, and return TRUE.  */
1928
1929 static bfd_boolean
1930 lookup_symbol_in_variable_table (struct comp_unit *unit,
1931                                  asymbol *sym,
1932                                  bfd_vma addr,
1933                                  const char **filename_ptr,
1934                                  unsigned int *linenumber_ptr)
1935 {
1936   const char *name = bfd_asymbol_name (sym);
1937   asection *sec = bfd_get_section (sym);
1938   struct varinfo* each;
1939
1940   for (each = unit->variable_table; each; each = each->prev_var)
1941     if (each->stack == 0
1942         && each->file != NULL
1943         && each->name != NULL
1944         && each->addr == addr
1945         && (!each->sec || each->sec == sec)
1946         && strcmp (name, each->name) == 0)
1947       break;
1948
1949   if (each)
1950     {
1951       each->sec = sec;
1952       *filename_ptr = each->file;
1953       *linenumber_ptr = each->line;
1954       return TRUE;
1955     }
1956   else
1957     return FALSE;
1958 }
1959
1960 static char *
1961 find_abstract_instance_name (struct comp_unit *unit,
1962                              struct attribute *attr_ptr)
1963 {
1964   bfd *abfd = unit->abfd;
1965   bfd_byte *info_ptr;
1966   unsigned int abbrev_number, bytes_read, i;
1967   struct abbrev_info *abbrev;
1968   bfd_uint64_t die_ref = attr_ptr->u.val;
1969   struct attribute attr;
1970   char *name = 0;
1971
1972   /* DW_FORM_ref_addr can reference an entry in a different CU. It
1973      is an offset from the .debug_info section, not the current CU.  */
1974   if (attr_ptr->form == DW_FORM_ref_addr)
1975     {
1976       /* We only support DW_FORM_ref_addr within the same file, so
1977          any relocations should be resolved already.  */
1978       if (!die_ref)
1979         abort ();
1980
1981       info_ptr = unit->sec_info_ptr + die_ref;
1982     }
1983   else 
1984     info_ptr = unit->info_ptr_unit + die_ref;
1985   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1986   info_ptr += bytes_read;
1987
1988   if (abbrev_number)
1989     {
1990       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1991       if (! abbrev)
1992         {
1993           (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1994                                  abbrev_number);
1995           bfd_set_error (bfd_error_bad_value);
1996         }
1997       else
1998         {
1999           for (i = 0; i < abbrev->num_attrs; ++i)
2000             {
2001               info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
2002                                          info_ptr);
2003               if (info_ptr == NULL)
2004                 break;
2005               switch (attr.name)
2006                 {
2007                 case DW_AT_name:
2008                   /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2009                      over DW_AT_name.  */
2010                   if (name == NULL)
2011                     name = attr.u.str;
2012                   break;
2013                 case DW_AT_specification:
2014                   name = find_abstract_instance_name (unit, &attr);
2015                   break;
2016                 case DW_AT_linkage_name:
2017                 case DW_AT_MIPS_linkage_name:
2018                   name = attr.u.str;
2019                   break;
2020                 default:
2021                   break;
2022                 }
2023             }
2024         }
2025     }
2026   return name;
2027 }
2028
2029 static bfd_boolean
2030 read_rangelist (struct comp_unit *unit, struct arange *arange,
2031                 bfd_uint64_t offset)
2032 {
2033   bfd_byte *ranges_ptr;
2034   bfd_vma base_address = unit->base_address;
2035
2036   if (! unit->stash->dwarf_ranges_buffer)
2037     {
2038       if (! read_debug_ranges (unit))
2039         return FALSE;
2040     }
2041   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
2042
2043   for (;;)
2044     {
2045       bfd_vma low_pc;
2046       bfd_vma high_pc;
2047
2048       low_pc = read_address (unit, ranges_ptr);
2049       ranges_ptr += unit->addr_size;
2050       high_pc = read_address (unit, ranges_ptr);
2051       ranges_ptr += unit->addr_size;
2052
2053       if (low_pc == 0 && high_pc == 0)
2054         break;
2055       if (low_pc == -1UL && high_pc != -1UL)
2056         base_address = high_pc;
2057       else
2058         {
2059           if (!arange_add (unit->abfd, arange,
2060                            base_address + low_pc, base_address + high_pc))
2061             return FALSE;
2062         }
2063     }
2064   return TRUE;
2065 }
2066
2067 /* DWARF2 Compilation unit functions.  */
2068
2069 /* Scan over each die in a comp. unit looking for functions to add
2070    to the function table and variables to the variable table.  */
2071
2072 static bfd_boolean
2073 scan_unit_for_symbols (struct comp_unit *unit)
2074 {
2075   bfd *abfd = unit->abfd;
2076   bfd_byte *info_ptr = unit->first_child_die_ptr;
2077   int nesting_level = 1;
2078   struct funcinfo **nested_funcs;
2079   int nested_funcs_size;
2080
2081   /* Maintain a stack of in-scope functions and inlined functions, which we
2082      can use to set the caller_func field.  */
2083   nested_funcs_size = 32;
2084   nested_funcs = (struct funcinfo **)
2085       bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
2086   if (nested_funcs == NULL)
2087     return FALSE;
2088   nested_funcs[nesting_level] = 0;
2089
2090   while (nesting_level)
2091     {
2092       unsigned int abbrev_number, bytes_read, i;
2093       struct abbrev_info *abbrev;
2094       struct attribute attr;
2095       struct funcinfo *func;
2096       struct varinfo *var;
2097       bfd_vma low_pc = 0;
2098       bfd_vma high_pc = 0;
2099       bfd_boolean high_pc_relative = FALSE;
2100
2101       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2102       info_ptr += bytes_read;
2103
2104       if (! abbrev_number)
2105         {
2106           nesting_level--;
2107           continue;
2108         }
2109
2110       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
2111       if (! abbrev)
2112         {
2113           (*_bfd_error_handler)
2114             (_("Dwarf Error: Could not find abbrev number %u."),
2115              abbrev_number);
2116           bfd_set_error (bfd_error_bad_value);
2117           goto fail;
2118         }
2119
2120       var = NULL;
2121       if (abbrev->tag == DW_TAG_subprogram
2122           || abbrev->tag == DW_TAG_entry_point
2123           || abbrev->tag == DW_TAG_inlined_subroutine)
2124         {
2125           bfd_size_type amt = sizeof (struct funcinfo);
2126           func = (struct funcinfo *) bfd_zalloc (abfd, amt);
2127           if (func == NULL)
2128             goto fail;
2129           func->tag = abbrev->tag;
2130           func->prev_func = unit->function_table;
2131           unit->function_table = func;
2132           BFD_ASSERT (!unit->cached);
2133
2134           if (func->tag == DW_TAG_inlined_subroutine)
2135             for (i = nesting_level - 1; i >= 1; i--)
2136               if (nested_funcs[i])
2137                 {
2138                   func->caller_func = nested_funcs[i];
2139                   break;
2140                 }
2141           nested_funcs[nesting_level] = func;
2142         }
2143       else
2144         {
2145           func = NULL;
2146           if (abbrev->tag == DW_TAG_variable)
2147             {
2148               bfd_size_type amt = sizeof (struct varinfo);
2149               var = (struct varinfo *) bfd_zalloc (abfd, amt);
2150               if (var == NULL)
2151                 goto fail;
2152               var->tag = abbrev->tag;
2153               var->stack = 1;
2154               var->prev_var = unit->variable_table;
2155               unit->variable_table = var;
2156               BFD_ASSERT (!unit->cached);
2157             }
2158
2159           /* No inline function in scope at this nesting level.  */
2160           nested_funcs[nesting_level] = 0;
2161         }
2162
2163       for (i = 0; i < abbrev->num_attrs; ++i)
2164         {
2165           info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2166           if (info_ptr == NULL)
2167             goto fail;
2168
2169           if (func)
2170             {
2171               switch (attr.name)
2172                 {
2173                 case DW_AT_call_file:
2174                   func->caller_file = concat_filename (unit->line_table,
2175                                                        attr.u.val);
2176                   break;
2177
2178                 case DW_AT_call_line:
2179                   func->caller_line = attr.u.val;
2180                   break;
2181
2182                 case DW_AT_abstract_origin:
2183                 case DW_AT_specification:
2184                   func->name = find_abstract_instance_name (unit, &attr);
2185                   break;
2186
2187                 case DW_AT_name:
2188                   /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2189                      over DW_AT_name.  */
2190                   if (func->name == NULL)
2191                     func->name = attr.u.str;
2192                   break;
2193
2194                 case DW_AT_linkage_name:
2195                 case DW_AT_MIPS_linkage_name:
2196                   func->name = attr.u.str;
2197                   break;
2198
2199                 case DW_AT_low_pc:
2200                   low_pc = attr.u.val;
2201                   break;
2202
2203                 case DW_AT_high_pc:
2204                   high_pc = attr.u.val;
2205                   high_pc_relative = attr.form != DW_FORM_addr;
2206                   break;
2207
2208                 case DW_AT_ranges:
2209                   if (!read_rangelist (unit, &func->arange, attr.u.val))
2210                     goto fail;
2211                   break;
2212
2213                 case DW_AT_decl_file:
2214                   func->file = concat_filename (unit->line_table,
2215                                                 attr.u.val);
2216                   break;
2217
2218                 case DW_AT_decl_line:
2219                   func->line = attr.u.val;
2220                   break;
2221
2222                 default:
2223                   break;
2224                 }
2225             }
2226           else if (var)
2227             {
2228               switch (attr.name)
2229                 {
2230                 case DW_AT_name:
2231                   var->name = attr.u.str;
2232                   break;
2233
2234                 case DW_AT_decl_file:
2235                   var->file = concat_filename (unit->line_table,
2236                                                attr.u.val);
2237                   break;
2238
2239                 case DW_AT_decl_line:
2240                   var->line = attr.u.val;
2241                   break;
2242
2243                 case DW_AT_external:
2244                   if (attr.u.val != 0)
2245                     var->stack = 0;
2246                   break;
2247
2248                 case DW_AT_location:
2249                   switch (attr.form)
2250                     {
2251                     case DW_FORM_block:
2252                     case DW_FORM_block1:
2253                     case DW_FORM_block2:
2254                     case DW_FORM_block4:
2255                     case DW_FORM_exprloc:
2256                       if (*attr.u.blk->data == DW_OP_addr)
2257                         {
2258                           var->stack = 0;
2259
2260                           /* Verify that DW_OP_addr is the only opcode in the
2261                              location, in which case the block size will be 1
2262                              plus the address size.  */
2263                           /* ??? For TLS variables, gcc can emit
2264                              DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2265                              which we don't handle here yet.  */
2266                           if (attr.u.blk->size == unit->addr_size + 1U)
2267                             var->addr = bfd_get (unit->addr_size * 8,
2268                                                  unit->abfd,
2269                                                  attr.u.blk->data + 1);
2270                         }
2271                       break;
2272
2273                     default:
2274                       break;
2275                     }
2276                   break;
2277
2278                 default:
2279                   break;
2280                 }
2281             }
2282         }
2283
2284       if (high_pc_relative)
2285         high_pc += low_pc;
2286
2287       if (func && high_pc != 0)
2288         {
2289           if (!arange_add (unit->abfd, &func->arange, low_pc, high_pc))
2290             goto fail;
2291         }
2292
2293       if (abbrev->has_children)
2294         {
2295           nesting_level++;
2296
2297           if (nesting_level >= nested_funcs_size)
2298             {
2299               struct funcinfo **tmp;
2300
2301               nested_funcs_size *= 2;
2302               tmp = (struct funcinfo **)
2303                  bfd_realloc (nested_funcs,
2304                               (nested_funcs_size * sizeof (struct funcinfo *)));
2305               if (tmp == NULL)
2306                 goto fail;
2307               nested_funcs = tmp;
2308             }
2309           nested_funcs[nesting_level] = 0;
2310         }
2311     }
2312
2313   free (nested_funcs);
2314   return TRUE;
2315
2316  fail:
2317   free (nested_funcs);
2318   return FALSE;
2319 }
2320
2321 /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
2322    includes the compilation unit header that proceeds the DIE's, but
2323    does not include the length field that precedes each compilation
2324    unit header.  END_PTR points one past the end of this comp unit.
2325    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2326
2327    This routine does not read the whole compilation unit; only enough
2328    to get to the line number information for the compilation unit.  */
2329
2330 static struct comp_unit *
2331 parse_comp_unit (struct dwarf2_debug *stash,
2332                  bfd_vma unit_length,
2333                  bfd_byte *info_ptr_unit,
2334                  unsigned int offset_size)
2335 {
2336   struct comp_unit* unit;
2337   unsigned int version;
2338   bfd_uint64_t abbrev_offset = 0;
2339   unsigned int addr_size;
2340   struct abbrev_info** abbrevs;
2341   unsigned int abbrev_number, bytes_read, i;
2342   struct abbrev_info *abbrev;
2343   struct attribute attr;
2344   bfd_byte *info_ptr = stash->info_ptr;
2345   bfd_byte *end_ptr = info_ptr + unit_length;
2346   bfd_size_type amt;
2347   bfd_vma low_pc = 0;
2348   bfd_vma high_pc = 0;
2349   bfd *abfd = stash->bfd_ptr;
2350   bfd_boolean high_pc_relative = FALSE;
2351
2352   version = read_2_bytes (abfd, info_ptr);
2353   info_ptr += 2;
2354   BFD_ASSERT (offset_size == 4 || offset_size == 8);
2355   if (offset_size == 4)
2356     abbrev_offset = read_4_bytes (abfd, info_ptr);
2357   else
2358     abbrev_offset = read_8_bytes (abfd, info_ptr);
2359   info_ptr += offset_size;
2360   addr_size = read_1_byte (abfd, info_ptr);
2361   info_ptr += 1;
2362
2363   if (version != 2 && version != 3 && version != 4)
2364     {
2365       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2, 3 and 4 information."), version);
2366       bfd_set_error (bfd_error_bad_value);
2367       return 0;
2368     }
2369
2370   if (addr_size > sizeof (bfd_vma))
2371     {
2372       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2373                          addr_size,
2374                          (unsigned int) sizeof (bfd_vma));
2375       bfd_set_error (bfd_error_bad_value);
2376       return 0;
2377     }
2378
2379   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2380     {
2381       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2382       bfd_set_error (bfd_error_bad_value);
2383       return 0;
2384     }
2385
2386   /* Read the abbrevs for this compilation unit into a table.  */
2387   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2388   if (! abbrevs)
2389       return 0;
2390
2391   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2392   info_ptr += bytes_read;
2393   if (! abbrev_number)
2394     {
2395       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2396                          abbrev_number);
2397       bfd_set_error (bfd_error_bad_value);
2398       return 0;
2399     }
2400
2401   abbrev = lookup_abbrev (abbrev_number, abbrevs);
2402   if (! abbrev)
2403     {
2404       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2405                          abbrev_number);
2406       bfd_set_error (bfd_error_bad_value);
2407       return 0;
2408     }
2409
2410   amt = sizeof (struct comp_unit);
2411   unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2412   if (unit == NULL)
2413     return NULL;
2414   unit->abfd = abfd;
2415   unit->version = version;
2416   unit->addr_size = addr_size;
2417   unit->offset_size = offset_size;
2418   unit->abbrevs = abbrevs;
2419   unit->end_ptr = end_ptr;
2420   unit->stash = stash;
2421   unit->info_ptr_unit = info_ptr_unit;
2422   unit->sec_info_ptr = stash->sec_info_ptr;
2423
2424   for (i = 0; i < abbrev->num_attrs; ++i)
2425     {
2426       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2427       if (info_ptr == NULL)
2428         return NULL;
2429
2430       /* Store the data if it is of an attribute we want to keep in a
2431          partial symbol table.  */
2432       switch (attr.name)
2433         {
2434         case DW_AT_stmt_list:
2435           unit->stmtlist = 1;
2436           unit->line_offset = attr.u.val;
2437           break;
2438
2439         case DW_AT_name:
2440           unit->name = attr.u.str;
2441           break;
2442
2443         case DW_AT_low_pc:
2444           low_pc = attr.u.val;
2445           /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2446              this is the base address to use when reading location
2447              lists or range lists. */
2448           unit->base_address = low_pc;
2449           break;
2450
2451         case DW_AT_high_pc:
2452           high_pc = attr.u.val;
2453           high_pc_relative = attr.form != DW_FORM_addr;
2454           break;
2455
2456         case DW_AT_ranges:
2457           if (!read_rangelist (unit, &unit->arange, attr.u.val))
2458             return NULL;
2459           break;
2460
2461         case DW_AT_comp_dir:
2462           {
2463             char *comp_dir = attr.u.str;
2464             if (comp_dir)
2465               {
2466                 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2467                    directory, get rid of it.  */
2468                 char *cp = strchr (comp_dir, ':');
2469
2470                 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2471                   comp_dir = cp + 1;
2472               }
2473             unit->comp_dir = comp_dir;
2474             break;
2475           }
2476
2477         default:
2478           break;
2479         }
2480     }
2481   if (high_pc_relative)
2482     high_pc += low_pc;
2483   if (high_pc != 0)
2484     {
2485       if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc))
2486         return NULL;
2487     }
2488
2489   unit->first_child_die_ptr = info_ptr;
2490   return unit;
2491 }
2492
2493 /* Return TRUE if UNIT may contain the address given by ADDR.  When
2494    there are functions written entirely with inline asm statements, the
2495    range info in the compilation unit header may not be correct.  We
2496    need to consult the line info table to see if a compilation unit
2497    really contains the given address.  */
2498
2499 static bfd_boolean
2500 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2501 {
2502   struct arange *arange;
2503
2504   if (unit->error)
2505     return FALSE;
2506
2507   arange = &unit->arange;
2508   do
2509     {
2510       if (addr >= arange->low && addr < arange->high)
2511         return TRUE;
2512       arange = arange->next;
2513     }
2514   while (arange);
2515
2516   return FALSE;
2517 }
2518
2519 /* If UNIT contains ADDR, set the output parameters to the values for
2520    the line containing ADDR.  The output parameters, FILENAME_PTR,
2521    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2522    to be filled in.
2523
2524    Return TRUE if UNIT contains ADDR, and no errors were encountered;
2525    FALSE otherwise.  */
2526
2527 static bfd_boolean
2528 comp_unit_find_nearest_line (struct comp_unit *unit,
2529                              bfd_vma addr,
2530                              const char **filename_ptr,
2531                              const char **functionname_ptr,
2532                              unsigned int *linenumber_ptr,
2533                              struct dwarf2_debug *stash)
2534 {
2535   bfd_boolean line_p;
2536   bfd_boolean func_p;
2537   struct funcinfo *function;
2538
2539   if (unit->error)
2540     return FALSE;
2541
2542   if (! unit->line_table)
2543     {
2544       if (! unit->stmtlist)
2545         {
2546           unit->error = 1;
2547           return FALSE;
2548         }
2549
2550       unit->line_table = decode_line_info (unit, stash);
2551
2552       if (! unit->line_table)
2553         {
2554           unit->error = 1;
2555           return FALSE;
2556         }
2557
2558       if (unit->first_child_die_ptr < unit->end_ptr
2559           && ! scan_unit_for_symbols (unit))
2560         {
2561           unit->error = 1;
2562           return FALSE;
2563         }
2564     }
2565
2566   function = NULL;
2567   func_p = lookup_address_in_function_table (unit, addr,
2568                                              &function, functionname_ptr);
2569   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2570     stash->inliner_chain = function;
2571   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2572                                               filename_ptr,
2573                                               linenumber_ptr);
2574   return line_p || func_p;
2575 }
2576
2577 /* Check to see if line info is already decoded in a comp_unit.
2578    If not, decode it.  Returns TRUE if no errors were encountered;
2579    FALSE otherwise.  */
2580
2581 static bfd_boolean
2582 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2583                                   struct dwarf2_debug *stash)
2584 {
2585   if (unit->error)
2586     return FALSE;
2587
2588   if (! unit->line_table)
2589     {
2590       if (! unit->stmtlist)
2591         {
2592           unit->error = 1;
2593           return FALSE;
2594         }
2595
2596       unit->line_table = decode_line_info (unit, stash);
2597
2598       if (! unit->line_table)
2599         {
2600           unit->error = 1;
2601           return FALSE;
2602         }
2603
2604       if (unit->first_child_die_ptr < unit->end_ptr
2605           && ! scan_unit_for_symbols (unit))
2606         {
2607           unit->error = 1;
2608           return FALSE;
2609         }
2610     }
2611
2612   return TRUE;
2613 }
2614
2615 /* If UNIT contains SYM at ADDR, set the output parameters to the
2616    values for the line containing SYM.  The output parameters,
2617    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2618    filled in.
2619
2620    Return TRUE if UNIT contains SYM, and no errors were encountered;
2621    FALSE otherwise.  */
2622
2623 static bfd_boolean
2624 comp_unit_find_line (struct comp_unit *unit,
2625                      asymbol *sym,
2626                      bfd_vma addr,
2627                      const char **filename_ptr,
2628                      unsigned int *linenumber_ptr,
2629                      struct dwarf2_debug *stash)
2630 {
2631   if (!comp_unit_maybe_decode_line_info (unit, stash))
2632     return FALSE;
2633
2634   if (sym->flags & BSF_FUNCTION)
2635     return lookup_symbol_in_function_table (unit, sym, addr,
2636                                             filename_ptr,
2637                                             linenumber_ptr);
2638
2639   return lookup_symbol_in_variable_table (unit, sym, addr,
2640                                           filename_ptr,
2641                                           linenumber_ptr);
2642 }
2643
2644 static struct funcinfo *
2645 reverse_funcinfo_list (struct funcinfo *head)
2646 {
2647   struct funcinfo *rhead;
2648   struct funcinfo *temp;
2649
2650   for (rhead = NULL; head; head = temp)
2651     {
2652       temp = head->prev_func;
2653       head->prev_func = rhead;
2654       rhead = head;
2655     }
2656   return rhead;
2657 }
2658
2659 static struct varinfo *
2660 reverse_varinfo_list (struct varinfo *head)
2661 {
2662   struct varinfo *rhead;
2663   struct varinfo *temp;
2664
2665   for (rhead = NULL; head; head = temp)
2666     {
2667       temp = head->prev_var;
2668       head->prev_var = rhead;
2669       rhead = head;
2670     }
2671   return rhead;
2672 }
2673
2674 /* Extract all interesting funcinfos and varinfos of a compilation
2675    unit into hash tables for faster lookup.  Returns TRUE if no
2676    errors were enountered; FALSE otherwise.  */
2677
2678 static bfd_boolean
2679 comp_unit_hash_info (struct dwarf2_debug *stash,
2680                      struct comp_unit *unit,
2681                      struct info_hash_table *funcinfo_hash_table,
2682                      struct info_hash_table *varinfo_hash_table)
2683 {
2684   struct funcinfo* each_func;
2685   struct varinfo* each_var;
2686   bfd_boolean okay = TRUE;
2687
2688   BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2689
2690   if (!comp_unit_maybe_decode_line_info (unit, stash))
2691     return FALSE;
2692
2693   BFD_ASSERT (!unit->cached);
2694
2695   /* To preserve the original search order, we went to visit the function
2696      infos in the reversed order of the list.  However, making the list
2697      bi-directional use quite a bit of extra memory.  So we reverse
2698      the list first, traverse the list in the now reversed order and
2699      finally reverse the list again to get back the original order.  */
2700   unit->function_table = reverse_funcinfo_list (unit->function_table);
2701   for (each_func = unit->function_table;
2702        each_func && okay;
2703        each_func = each_func->prev_func)
2704     {
2705       /* Skip nameless functions. */
2706       if (each_func->name)
2707         /* There is no need to copy name string into hash table as
2708            name string is either in the dwarf string buffer or
2709            info in the stash.  */
2710         okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2711                                        (void*) each_func, FALSE);
2712     }
2713   unit->function_table = reverse_funcinfo_list (unit->function_table);
2714   if (!okay)
2715     return FALSE;
2716
2717   /* We do the same for variable infos.  */
2718   unit->variable_table = reverse_varinfo_list (unit->variable_table);
2719   for (each_var = unit->variable_table;
2720        each_var && okay;
2721        each_var = each_var->prev_var)
2722     {
2723       /* Skip stack vars and vars with no files or names.  */
2724       if (each_var->stack == 0
2725           && each_var->file != NULL
2726           && each_var->name != NULL)
2727         /* There is no need to copy name string into hash table as
2728            name string is either in the dwarf string buffer or
2729            info in the stash.  */
2730         okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2731                                        (void*) each_var, FALSE);
2732     }
2733
2734   unit->variable_table = reverse_varinfo_list (unit->variable_table);
2735   unit->cached = TRUE;
2736   return okay;
2737 }
2738
2739 /* Locate a section in a BFD containing debugging info.  The search starts
2740    from the section after AFTER_SEC, or from the first section in the BFD if
2741    AFTER_SEC is NULL.  The search works by examining the names of the
2742    sections.  There are three permissiable names.  The first two are given
2743    by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
2744    and .zdebug_info).  The third is a prefix .gnu.linkonce.wi.
2745    This is a variation on the .debug_info section which has a checksum
2746    describing the contents appended onto the name.  This allows the linker to
2747    identify and discard duplicate debugging sections for different
2748    compilation units.  */
2749 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2750
2751 static asection *
2752 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
2753                  asection *after_sec)
2754 {
2755   asection * msec;
2756
2757   msec = after_sec != NULL ? after_sec->next : abfd->sections;
2758
2759   while (msec)
2760     {
2761       if (strcmp (msec->name,
2762                   debug_sections[debug_info].uncompressed_name) == 0)
2763         return msec;
2764
2765       if (debug_sections[debug_info].compressed_name != NULL
2766           && strcmp (msec->name,
2767                      debug_sections[debug_info].compressed_name) == 0)
2768         return msec;
2769
2770       if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2771         return msec;
2772
2773       msec = msec->next;
2774     }
2775
2776   return NULL;
2777 }
2778
2779 /* Unset vmas for adjusted sections in STASH.  */
2780
2781 static void
2782 unset_sections (struct dwarf2_debug *stash)
2783 {
2784   unsigned int i;
2785   struct adjusted_section *p;
2786
2787   i = stash->adjusted_section_count;
2788   p = stash->adjusted_sections;
2789   for (; i > 0; i--, p++)
2790     p->section->vma = 0;
2791 }
2792
2793 /* Set unique VMAs for loadable and DWARF sections in ABFD and save
2794    VMAs in STASH for unset_sections.  */
2795
2796 static bfd_boolean
2797 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2798 {
2799   struct adjusted_section *p;
2800   unsigned int i;
2801
2802   if (stash->adjusted_section_count != 0)
2803     {
2804       i = stash->adjusted_section_count;
2805       p = stash->adjusted_sections;
2806       for (; i > 0; i--, p++)
2807         p->section->vma = p->adj_vma;
2808     }
2809   else
2810     {
2811       asection *sect;
2812       bfd_vma last_vma = 0, last_dwarf = 0;
2813       bfd_size_type amt;
2814       const char *debug_info_name;
2815
2816       debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
2817       i = 0;
2818       for (sect = abfd->sections; sect != NULL; sect = sect->next)
2819         {
2820           bfd_size_type sz;
2821           int is_debug_info;
2822
2823           if (sect->vma != 0)
2824             continue;
2825
2826           /* We need to adjust the VMAs of any .debug_info sections.
2827              Skip compressed ones, since no relocations could target
2828              them - they should not appear in object files anyway.  */
2829           if (strcmp (sect->name, debug_info_name) == 0)
2830             is_debug_info = 1;
2831           else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2832             is_debug_info = 1;
2833           else
2834             is_debug_info = 0;
2835
2836           if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2837             continue;
2838
2839           sz = sect->rawsize ? sect->rawsize : sect->size;
2840           if (sz == 0)
2841             continue;
2842
2843           i++;
2844         }
2845
2846       amt = i * sizeof (struct adjusted_section);
2847       p = (struct adjusted_section *) bfd_zalloc (abfd, amt);
2848       if (! p)
2849         return FALSE;
2850
2851       stash->adjusted_sections = p;
2852       stash->adjusted_section_count = i;
2853
2854       for (sect = abfd->sections; sect != NULL; sect = sect->next)
2855         {
2856           bfd_size_type sz;
2857           int is_debug_info;
2858
2859           if (sect->vma != 0)
2860             continue;
2861
2862           /* We need to adjust the VMAs of any .debug_info sections.
2863              Skip compressed ones, since no relocations could target
2864              them - they should not appear in object files anyway.  */
2865           if (strcmp (sect->name, debug_info_name) == 0)
2866             is_debug_info = 1;
2867           else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2868             is_debug_info = 1;
2869           else
2870             is_debug_info = 0;
2871
2872           if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2873             continue;
2874
2875           sz = sect->rawsize ? sect->rawsize : sect->size;
2876           if (sz == 0)
2877             continue;
2878
2879           p->section = sect;
2880           if (is_debug_info)
2881             {
2882               BFD_ASSERT (sect->alignment_power == 0);
2883               sect->vma = last_dwarf;
2884               last_dwarf += sz;
2885             }
2886           else if (last_vma != 0)
2887             {
2888               /* Align the new address to the current section
2889                  alignment.  */
2890               last_vma = ((last_vma
2891                            + ~((bfd_vma) -1 << sect->alignment_power))
2892                           & ((bfd_vma) -1 << sect->alignment_power));
2893               sect->vma = last_vma;
2894               last_vma += sect->vma + sz;
2895             }
2896           else
2897             last_vma += sect->vma + sz;
2898
2899           p->adj_vma = sect->vma;
2900
2901           p++;
2902         }
2903     }
2904
2905   return TRUE;
2906 }
2907
2908 /* Look up a funcinfo by name using the given info hash table.  If found,
2909    also update the locations pointed to by filename_ptr and linenumber_ptr.
2910
2911    This function returns TRUE if a funcinfo that matches the given symbol
2912    and address is found with any error; otherwise it returns FALSE.  */
2913
2914 static bfd_boolean
2915 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2916                            asymbol *sym,
2917                            bfd_vma addr,
2918                            const char **filename_ptr,
2919                            unsigned int *linenumber_ptr)
2920 {
2921   struct funcinfo* each_func;
2922   struct funcinfo* best_fit = NULL;
2923   struct info_list_node *node;
2924   struct arange *arange;
2925   const char *name = bfd_asymbol_name (sym);
2926   asection *sec = bfd_get_section (sym);
2927
2928   for (node = lookup_info_hash_table (hash_table, name);
2929        node;
2930        node = node->next)
2931     {
2932       each_func = (struct funcinfo *) node->info;
2933       for (arange = &each_func->arange;
2934            arange;
2935            arange = arange->next)
2936         {
2937           if ((!each_func->sec || each_func->sec == sec)
2938               && addr >= arange->low
2939               && addr < arange->high
2940               && (!best_fit
2941                   || ((arange->high - arange->low)
2942                       < (best_fit->arange.high - best_fit->arange.low))))
2943             best_fit = each_func;
2944         }
2945     }
2946
2947   if (best_fit)
2948     {
2949       best_fit->sec = sec;
2950       *filename_ptr = best_fit->file;
2951       *linenumber_ptr = best_fit->line;
2952       return TRUE;
2953     }
2954
2955   return FALSE;
2956 }
2957
2958 /* Look up a varinfo by name using the given info hash table.  If found,
2959    also update the locations pointed to by filename_ptr and linenumber_ptr.
2960
2961    This function returns TRUE if a varinfo that matches the given symbol
2962    and address is found with any error; otherwise it returns FALSE.  */
2963
2964 static bfd_boolean
2965 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2966                           asymbol *sym,
2967                           bfd_vma addr,
2968                           const char **filename_ptr,
2969                           unsigned int *linenumber_ptr)
2970 {
2971   const char *name = bfd_asymbol_name (sym);
2972   asection *sec = bfd_get_section (sym);
2973   struct varinfo* each;
2974   struct info_list_node *node;
2975
2976   for (node = lookup_info_hash_table (hash_table, name);
2977        node;
2978        node = node->next)
2979     {
2980       each = (struct varinfo *) node->info;
2981       if (each->addr == addr
2982           && (!each->sec || each->sec == sec))
2983         {
2984           each->sec = sec;
2985           *filename_ptr = each->file;
2986           *linenumber_ptr = each->line;
2987           return TRUE;
2988         }
2989     }
2990
2991   return FALSE;
2992 }
2993
2994 /* Update the funcinfo and varinfo info hash tables if they are
2995    not up to date.  Returns TRUE if there is no error; otherwise
2996    returns FALSE and disable the info hash tables.  */
2997
2998 static bfd_boolean
2999 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
3000 {
3001   struct comp_unit *each;
3002
3003   /* Exit if hash tables are up-to-date.  */
3004   if (stash->all_comp_units == stash->hash_units_head)
3005     return TRUE;
3006
3007   if (stash->hash_units_head)
3008     each = stash->hash_units_head->prev_unit;
3009   else
3010     each = stash->last_comp_unit;
3011
3012   while (each)
3013     {
3014       if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
3015                                 stash->varinfo_hash_table))
3016         {
3017           stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3018           return FALSE;
3019         }
3020       each = each->prev_unit;
3021     }
3022
3023   stash->hash_units_head = stash->all_comp_units;
3024   return TRUE;
3025 }
3026
3027 /* Check consistency of info hash tables.  This is for debugging only. */
3028
3029 static void ATTRIBUTE_UNUSED
3030 stash_verify_info_hash_table (struct dwarf2_debug *stash)
3031 {
3032   struct comp_unit *each_unit;
3033   struct funcinfo *each_func;
3034   struct varinfo *each_var;
3035   struct info_list_node *node;
3036   bfd_boolean found;
3037
3038   for (each_unit = stash->all_comp_units;
3039        each_unit;
3040        each_unit = each_unit->next_unit)
3041     {
3042       for (each_func = each_unit->function_table;
3043            each_func;
3044            each_func = each_func->prev_func)
3045         {
3046           if (!each_func->name)
3047             continue;
3048           node = lookup_info_hash_table (stash->funcinfo_hash_table,
3049                                          each_func->name);
3050           BFD_ASSERT (node);
3051           found = FALSE;
3052           while (node && !found)
3053             {
3054               found = node->info == each_func;
3055               node = node->next;
3056             }
3057           BFD_ASSERT (found);
3058         }
3059
3060       for (each_var = each_unit->variable_table;
3061            each_var;
3062            each_var = each_var->prev_var)
3063         {
3064           if (!each_var->name || !each_var->file || each_var->stack)
3065             continue;
3066           node = lookup_info_hash_table (stash->varinfo_hash_table,
3067                                          each_var->name);
3068           BFD_ASSERT (node);
3069           found = FALSE;
3070           while (node && !found)
3071             {
3072               found = node->info == each_var;
3073               node = node->next;
3074             }
3075           BFD_ASSERT (found);
3076         }
3077     }
3078 }
3079
3080 /* Check to see if we want to enable the info hash tables, which consume
3081    quite a bit of memory.  Currently we only check the number times
3082    bfd_dwarf2_find_line is called.  In the future, we may also want to
3083    take the number of symbols into account.  */
3084
3085 static void
3086 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
3087 {
3088   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
3089
3090   if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
3091     return;
3092
3093   /* FIXME: Maybe we should check the reduce_memory_overheads
3094      and optimize fields in the bfd_link_info structure ?  */
3095
3096   /* Create hash tables.  */
3097   stash->funcinfo_hash_table = create_info_hash_table (abfd);
3098   stash->varinfo_hash_table = create_info_hash_table (abfd);
3099   if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
3100     {
3101       /* Turn off info hashes if any allocation above fails.  */
3102       stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3103       return;
3104     }
3105   /* We need a forced update so that the info hash tables will
3106      be created even though there is no compilation unit.  That
3107      happens if STASH_INFO_HASH_TRIGGER is 0.  */
3108   stash_maybe_update_info_hash_tables (stash);
3109   stash->info_hash_status = STASH_INFO_HASH_ON;
3110 }
3111
3112 /* Find the file and line associated with a symbol and address using the
3113    info hash tables of a stash. If there is a match, the function returns
3114    TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
3115    otherwise it returns FALSE.  */
3116
3117 static bfd_boolean
3118 stash_find_line_fast (struct dwarf2_debug *stash,
3119                       asymbol *sym,
3120                       bfd_vma addr,
3121                       const char **filename_ptr,
3122                       unsigned int *linenumber_ptr)
3123 {
3124   BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
3125
3126   if (sym->flags & BSF_FUNCTION)
3127     return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
3128                                       filename_ptr, linenumber_ptr);
3129   return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
3130                                    filename_ptr, linenumber_ptr);
3131 }
3132
3133 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
3134    If DEBUG_BFD is not specified, we read debug information from ABFD
3135    or its gnu_debuglink. The results will be stored in PINFO.
3136    The function returns TRUE iff debug information is ready.  */
3137
3138 bfd_boolean
3139 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
3140                               const struct dwarf_debug_section *debug_sections,
3141                               asymbol **symbols,
3142                               void **pinfo)
3143 {
3144   bfd_size_type amt = sizeof (struct dwarf2_debug);
3145   bfd_size_type total_size;
3146   asection *msec;
3147   struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
3148
3149   if (stash != NULL)
3150     return TRUE;
3151
3152   stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
3153   if (! stash)
3154     return FALSE;
3155   stash->debug_sections = debug_sections;
3156
3157   *pinfo = stash;
3158
3159   if (debug_bfd == NULL)
3160     debug_bfd = abfd;
3161
3162   msec = find_debug_info (debug_bfd, debug_sections, NULL);
3163   if (msec == NULL && abfd == debug_bfd)
3164     {
3165       char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
3166
3167       if (debug_filename == NULL)
3168         /* No dwarf2 info, and no gnu_debuglink to follow.
3169            Note that at this point the stash has been allocated, but
3170            contains zeros.  This lets future calls to this function
3171            fail more quickly.  */
3172         return FALSE;
3173
3174       if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
3175           || ! bfd_check_format (debug_bfd, bfd_object)
3176           || (msec = find_debug_info (debug_bfd,
3177                                       debug_sections, NULL)) == NULL)
3178         {
3179           if (debug_bfd)
3180             bfd_close (debug_bfd);
3181           /* FIXME: Should we report our failure to follow the debuglink ?  */
3182           free (debug_filename);
3183           return FALSE;
3184         }
3185     }
3186
3187   /* There can be more than one DWARF2 info section in a BFD these
3188      days.  First handle the easy case when there's only one.  If
3189      there's more than one, try case two: none of the sections is
3190      compressed.  In that case, read them all in and produce one
3191      large stash.  We do this in two passes - in the first pass we
3192      just accumulate the section sizes, and in the second pass we
3193      read in the section's contents.  (The allows us to avoid
3194      reallocing the data as we add sections to the stash.)  If
3195      some or all sections are compressed, then do things the slow
3196      way, with a bunch of reallocs.  */
3197
3198   if (! find_debug_info (debug_bfd, debug_sections, msec))
3199     {
3200       /* Case 1: only one info section.  */
3201       total_size = msec->size;
3202       if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
3203                           symbols, 0,
3204                           &stash->info_ptr_memory, &total_size))
3205         return FALSE;
3206     }
3207   else
3208     {
3209       /* Case 2: multiple sections.  */
3210       for (total_size = 0;
3211            msec;
3212            msec = find_debug_info (debug_bfd, debug_sections, msec))
3213         total_size += msec->size;
3214
3215       stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
3216       if (stash->info_ptr_memory == NULL)
3217         return FALSE;
3218
3219       total_size = 0;
3220       for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
3221            msec;
3222            msec = find_debug_info (debug_bfd, debug_sections, msec))
3223         {
3224           bfd_size_type size;
3225
3226           size = msec->size;
3227           if (size == 0)
3228             continue;
3229
3230           if (!(bfd_simple_get_relocated_section_contents
3231                 (debug_bfd, msec, stash->info_ptr_memory + total_size,
3232                  symbols)))
3233             return FALSE;
3234
3235           total_size += size;
3236         }
3237     }
3238
3239   stash->info_ptr = stash->info_ptr_memory;
3240   stash->info_ptr_end = stash->info_ptr + total_size;
3241   stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
3242   stash->sec_info_ptr = stash->info_ptr;
3243   stash->syms = symbols;
3244   stash->bfd_ptr = debug_bfd;
3245
3246   return TRUE;
3247 }
3248
3249 /* Find the source code location of SYMBOL.  If SYMBOL is NULL
3250    then find the nearest source code location corresponding to
3251    the address SECTION + OFFSET.
3252    Returns TRUE if the line is found without error and fills in
3253    FILENAME_PTR and LINENUMBER_PTR.  In the case where SYMBOL was
3254    NULL the FUNCTIONNAME_PTR is also filled in.
3255    SYMBOLS contains the symbol table for ABFD.
3256    DEBUG_SECTIONS contains the name of the dwarf debug sections.
3257    ADDR_SIZE is the number of bytes in the initial .debug_info length
3258    field and in the abbreviation offset, or zero to indicate that the
3259    default value should be used.  */
3260
3261 static bfd_boolean
3262 find_line (bfd *abfd,
3263            const struct dwarf_debug_section *debug_sections,
3264            asection *section,
3265            bfd_vma offset,
3266            asymbol *symbol,
3267            asymbol **symbols,
3268            const char **filename_ptr,
3269            const char **functionname_ptr,
3270            unsigned int *linenumber_ptr,
3271            unsigned int addr_size,
3272            void **pinfo)
3273 {
3274   /* Read each compilation unit from the section .debug_info, and check
3275      to see if it contains the address we are searching for.  If yes,
3276      lookup the address, and return the line number info.  If no, go
3277      on to the next compilation unit.
3278
3279      We keep a list of all the previously read compilation units, and
3280      a pointer to the next un-read compilation unit.  Check the
3281      previously read units before reading more.  */
3282   struct dwarf2_debug *stash;
3283   /* What address are we looking for?  */
3284   bfd_vma addr;
3285   struct comp_unit* each;
3286   bfd_vma found = FALSE;
3287   bfd_boolean do_line;
3288
3289   *filename_ptr = NULL;
3290   if (functionname_ptr != NULL)
3291     *functionname_ptr = NULL;
3292   *linenumber_ptr = 0;
3293
3294   if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL,
3295                                       debug_sections, symbols, pinfo))
3296     return FALSE;
3297
3298   stash = (struct dwarf2_debug *) *pinfo;
3299
3300   /* In a relocatable file, 2 functions may have the same address.
3301      We change the section vma so that they won't overlap.  */
3302   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3303     {
3304       if (! place_sections (abfd, stash))
3305         return FALSE;
3306     }
3307
3308   do_line = (section == NULL
3309              && offset == 0
3310              && functionname_ptr == NULL
3311              && symbol != NULL);
3312   if (do_line)
3313     {
3314       addr = symbol->value;
3315       section = bfd_get_section (symbol);
3316     }
3317   else if (section != NULL
3318            && functionname_ptr != NULL
3319            && symbol == NULL)
3320     addr = offset;
3321   else
3322     abort ();
3323
3324   if (section->output_section)
3325     addr += section->output_section->vma + section->output_offset;
3326   else
3327     addr += section->vma;
3328
3329   /* A null info_ptr indicates that there is no dwarf2 info
3330      (or that an error occured while setting up the stash).  */
3331   if (! stash->info_ptr)
3332     return FALSE;
3333
3334   stash->inliner_chain = NULL;
3335
3336   /* Check the previously read comp. units first.  */
3337   if (do_line)
3338     {
3339       /* The info hash tables use quite a bit of memory.  We may not want to
3340          always use them.  We use some heuristics to decide if and when to
3341          turn it on.  */
3342       if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3343         stash_maybe_enable_info_hash_tables (abfd, stash);
3344
3345       /* Keep info hash table up to date if they are available.  Note that we
3346          may disable the hash tables if there is any error duing update. */
3347       if (stash->info_hash_status == STASH_INFO_HASH_ON)
3348         stash_maybe_update_info_hash_tables (stash);
3349
3350       if (stash->info_hash_status == STASH_INFO_HASH_ON)
3351         {
3352           found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3353                                         linenumber_ptr);
3354           if (found)
3355             goto done;
3356         }
3357       else
3358         {
3359           /* Check the previously read comp. units first.  */
3360           for (each = stash->all_comp_units; each; each = each->next_unit)
3361             if ((symbol->flags & BSF_FUNCTION) == 0
3362                 || comp_unit_contains_address (each, addr))
3363               {
3364                 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3365                                              linenumber_ptr, stash);
3366                 if (found)
3367                   goto done;
3368               }
3369         }
3370     }
3371   else
3372     {
3373       for (each = stash->all_comp_units; each; each = each->next_unit)
3374         {
3375           found = (comp_unit_contains_address (each, addr)
3376                    && comp_unit_find_nearest_line (each, addr,
3377                                                    filename_ptr,
3378                                                    functionname_ptr,
3379                                                    linenumber_ptr,
3380                                                    stash));
3381           if (found)
3382             goto done;
3383         }
3384     }
3385
3386   /* The DWARF2 spec says that the initial length field, and the
3387      offset of the abbreviation table, should both be 4-byte values.
3388      However, some compilers do things differently.  */
3389   if (addr_size == 0)
3390     addr_size = 4;
3391   BFD_ASSERT (addr_size == 4 || addr_size == 8);
3392
3393   /* Read each remaining comp. units checking each as they are read.  */
3394   while (stash->info_ptr < stash->info_ptr_end)
3395     {
3396       bfd_vma length;
3397       unsigned int offset_size = addr_size;
3398       bfd_byte *info_ptr_unit = stash->info_ptr;
3399
3400       length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
3401       /* A 0xffffff length is the DWARF3 way of indicating
3402          we use 64-bit offsets, instead of 32-bit offsets.  */
3403       if (length == 0xffffffff)
3404         {
3405           offset_size = 8;
3406           length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3407           stash->info_ptr += 12;
3408         }
3409       /* A zero length is the IRIX way of indicating 64-bit offsets,
3410          mostly because the 64-bit length will generally fit in 32
3411          bits, and the endianness helps.  */
3412       else if (length == 0)
3413         {
3414           offset_size = 8;
3415           length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3416           stash->info_ptr += 8;
3417         }
3418       /* In the absence of the hints above, we assume 32-bit DWARF2
3419          offsets even for targets with 64-bit addresses, because:
3420            a) most of the time these targets will not have generated
3421               more than 2Gb of debug info and so will not need 64-bit
3422               offsets,
3423          and
3424            b) if they do use 64-bit offsets but they are not using
3425               the size hints that are tested for above then they are
3426               not conforming to the DWARF3 standard anyway.  */
3427       else if (addr_size == 8)
3428         {
3429           offset_size = 4;
3430           stash->info_ptr += 4;
3431         }
3432       else
3433         stash->info_ptr += 4;
3434
3435       if (length > 0)
3436         {
3437           each = parse_comp_unit (stash, length, info_ptr_unit,
3438                                   offset_size);
3439           if (!each)
3440             /* The dwarf information is damaged, don't trust it any
3441                more.  */
3442             break;
3443           stash->info_ptr += length;
3444
3445           if (stash->all_comp_units)
3446             stash->all_comp_units->prev_unit = each;
3447           else
3448             stash->last_comp_unit = each;
3449           
3450           each->next_unit = stash->all_comp_units;
3451           stash->all_comp_units = each;
3452           
3453           /* DW_AT_low_pc and DW_AT_high_pc are optional for
3454              compilation units.  If we don't have them (i.e.,
3455              unit->high == 0), we need to consult the line info table
3456              to see if a compilation unit contains the given
3457              address.  */
3458           if (do_line)
3459             found = (((symbol->flags & BSF_FUNCTION) == 0
3460                       || each->arange.high == 0
3461                       || comp_unit_contains_address (each, addr))
3462                      && comp_unit_find_line (each, symbol, addr,
3463                                              filename_ptr,
3464                                              linenumber_ptr,
3465                                              stash));
3466           else
3467             found = ((each->arange.high == 0
3468                       || comp_unit_contains_address (each, addr))
3469                      && comp_unit_find_nearest_line (each, addr,
3470                                                      filename_ptr,
3471                                                      functionname_ptr,
3472                                                      linenumber_ptr,
3473                                                      stash));
3474
3475           if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3476               == stash->sec->size)
3477             {
3478               stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
3479                                             stash->sec);
3480               stash->sec_info_ptr = stash->info_ptr;
3481             }
3482
3483           if (found)
3484             goto done;
3485         }
3486     }
3487
3488 done:
3489   if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3490     unset_sections (stash);
3491
3492   return found;
3493 }
3494
3495 /* The DWARF2 version of find_nearest_line.
3496    Return TRUE if the line is found without error.  */
3497
3498 bfd_boolean
3499 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3500                                const struct dwarf_debug_section *debug_sections,
3501                                asection *section,
3502                                asymbol **symbols,
3503                                bfd_vma offset,
3504                                const char **filename_ptr,
3505                                const char **functionname_ptr,
3506                                unsigned int *linenumber_ptr,
3507                                unsigned int addr_size,
3508                                void **pinfo)
3509 {
3510   return find_line (abfd, debug_sections, section, offset, NULL, symbols,
3511                     filename_ptr, functionname_ptr, linenumber_ptr, addr_size,
3512                     pinfo);
3513 }
3514
3515 /* The DWARF2 version of find_line.
3516    Return TRUE if the line is found without error.  */
3517
3518 bfd_boolean
3519 _bfd_dwarf2_find_line (bfd *abfd,
3520                        asymbol **symbols,
3521                        asymbol *symbol,
3522                        const char **filename_ptr,
3523                        unsigned int *linenumber_ptr,
3524                        unsigned int addr_size,
3525                        void **pinfo)
3526 {
3527   return find_line (abfd, dwarf_debug_sections, NULL, 0, symbol, symbols,
3528                     filename_ptr, NULL, linenumber_ptr, addr_size, pinfo);
3529 }
3530
3531 bfd_boolean
3532 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3533                                const char **filename_ptr,
3534                                const char **functionname_ptr,
3535                                unsigned int *linenumber_ptr,
3536                                void **pinfo)
3537 {
3538   struct dwarf2_debug *stash;
3539
3540   stash = (struct dwarf2_debug *) *pinfo;
3541   if (stash)
3542     {
3543       struct funcinfo *func = stash->inliner_chain;
3544
3545       if (func && func->caller_func)
3546         {
3547           *filename_ptr = func->caller_file;
3548           *functionname_ptr = func->caller_func->name;
3549           *linenumber_ptr = func->caller_line;
3550           stash->inliner_chain = func->caller_func;
3551           return TRUE;
3552         }
3553     }
3554
3555   return FALSE;
3556 }
3557
3558 void
3559 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
3560 {
3561   struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;;
3562   struct comp_unit *each;
3563
3564   if (abfd == NULL || stash == NULL)
3565     return;
3566
3567   for (each = stash->all_comp_units; each; each = each->next_unit)
3568     {
3569       struct abbrev_info **abbrevs = each->abbrevs;
3570       struct funcinfo *function_table = each->function_table;
3571       struct varinfo *variable_table = each->variable_table;
3572       size_t i;
3573
3574       for (i = 0; i < ABBREV_HASH_SIZE; i++)
3575         {
3576           struct abbrev_info *abbrev = abbrevs[i];
3577
3578           while (abbrev)
3579             {
3580               free (abbrev->attrs);
3581               abbrev = abbrev->next;
3582             }
3583         }
3584
3585       if (each->line_table)
3586         {
3587           free (each->line_table->dirs);
3588           free (each->line_table->files);
3589         }
3590
3591       while (function_table)
3592         {
3593           if (function_table->file)
3594             {
3595               free (function_table->file);
3596               function_table->file = NULL;
3597             }
3598
3599           if (function_table->caller_file)
3600             {
3601               free (function_table->caller_file);
3602               function_table->caller_file = NULL;
3603             }
3604           function_table = function_table->prev_func;
3605         }
3606
3607       while (variable_table)
3608         {
3609           if (variable_table->file)
3610             {
3611               free (variable_table->file);
3612               variable_table->file = NULL;
3613             }
3614
3615           variable_table = variable_table->prev_var;
3616         }
3617     }
3618
3619   if (stash->dwarf_abbrev_buffer)
3620     free (stash->dwarf_abbrev_buffer);
3621   if (stash->dwarf_line_buffer)
3622     free (stash->dwarf_line_buffer);
3623   if (stash->dwarf_str_buffer)
3624     free (stash->dwarf_str_buffer);
3625   if (stash->dwarf_ranges_buffer)
3626     free (stash->dwarf_ranges_buffer);
3627   if (stash->info_ptr_memory)
3628     free (stash->info_ptr_memory);
3629 }