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