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