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