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