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