OSDN Git Service

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