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