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