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