2005-06-09 H.J. Lu <hongjiu.lu@intel.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 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 2 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, MA 02110-1301, USA.  */
31
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "libiberty.h"
35 #include "libbfd.h"
36 #include "elf-bfd.h"
37 #include "elf/dwarf2.h"
38
39 /* The data in the .debug_line statement prologue looks like this.  */
40
41 struct line_head
42 {
43   bfd_vma total_length;
44   unsigned short version;
45   bfd_vma prologue_length;
46   unsigned char minimum_instruction_length;
47   unsigned char default_is_stmt;
48   int line_base;
49   unsigned char line_range;
50   unsigned char opcode_base;
51   unsigned char *standard_opcode_lengths;
52 };
53
54 /* Attributes have a name and a value.  */
55
56 struct attribute
57 {
58   enum dwarf_attribute name;
59   enum dwarf_form form;
60   union
61   {
62     char *str;
63     struct dwarf_block *blk;
64     bfd_uint64_t val;
65     bfd_int64_t sval;
66   }
67   u;
68 };
69
70 /* Blocks are a bunch of untyped bytes.  */
71 struct dwarf_block
72 {
73   unsigned int size;
74   bfd_byte *data;
75 };
76
77 struct dwarf2_debug
78 {
79   /* A list of all previously read comp_units.  */
80   struct comp_unit *all_comp_units;
81
82   /* The next unread compilation unit within the .debug_info section.
83      Zero indicates that the .debug_info section has not been loaded
84      into a buffer yet.  */
85   bfd_byte *info_ptr;
86
87   /* Pointer to the end of the .debug_info section memory buffer.  */
88   bfd_byte *info_ptr_end;
89
90   /* Pointer to the section and address of the beginning of the
91      section.  */
92   asection *sec;
93   bfd_byte *sec_info_ptr;
94
95   /* Pointer to the symbol table.  */
96   asymbol **syms;
97
98   /* Pointer to the .debug_abbrev section loaded into memory.  */
99   bfd_byte *dwarf_abbrev_buffer;
100
101   /* Length of the loaded .debug_abbrev section.  */
102   unsigned long dwarf_abbrev_size;
103
104   /* Buffer for decode_line_info.  */
105   bfd_byte *dwarf_line_buffer;
106
107   /* Length of the loaded .debug_line section.  */
108   unsigned long dwarf_line_size;
109
110   /* Pointer to the .debug_str section loaded into memory.  */
111   bfd_byte *dwarf_str_buffer;
112
113   /* Length of the loaded .debug_str section.  */
114   unsigned long dwarf_str_size;
115
116   /* Pointer to the .debug_ranges section loaded into memory. */
117   bfd_byte *dwarf_ranges_buffer;
118
119   /* Length of the loaded .debug_ranges section. */
120   unsigned long dwarf_ranges_size;
121
122   /* If the most recent call to bfd_find_nearest_line was given an
123      address in an inlined function, preserve a pointer into the
124      calling chain for subsequent calls to bfd_find_inliner_info to
125      use. */
126   struct funcinfo *inliner_chain;
127 };
128
129 struct arange
130 {
131   struct arange *next;
132   bfd_vma low;
133   bfd_vma high;
134 };
135
136 /* A minimal decoding of DWARF2 compilation units.  We only decode
137    what's needed to get to the line number information.  */
138
139 struct comp_unit
140 {
141   /* Chain the previously read compilation units.  */
142   struct comp_unit *next_unit;
143
144   /* Keep the bfd convenient (for memory allocation).  */
145   bfd *abfd;
146
147   /* The lowest and highest addresses contained in this compilation
148      unit as specified in the compilation unit header.  */
149   struct arange arange;
150
151   /* The DW_AT_name attribute (for error messages).  */
152   char *name;
153
154   /* The abbrev hash table.  */
155   struct abbrev_info **abbrevs;
156
157   /* Note that an error was found by comp_unit_find_nearest_line.  */
158   int error;
159
160   /* The DW_AT_comp_dir attribute.  */
161   char *comp_dir;
162
163   /* TRUE if there is a line number table associated with this comp. unit.  */
164   int stmtlist;
165
166   /* Pointer to the current comp_unit so that we can find a given entry
167      by its reference.  */
168   bfd_byte *info_ptr_unit;
169
170   /* The offset into .debug_line of the line number table.  */
171   unsigned long line_offset;
172
173   /* Pointer to the first child die for the comp unit.  */
174   bfd_byte *first_child_die_ptr;
175
176   /* The end of the comp unit.  */
177   bfd_byte *end_ptr;
178
179   /* The decoded line number, NULL if not yet decoded.  */
180   struct line_info_table *line_table;
181
182   /* A list of the functions found in this comp. unit.  */
183   struct funcinfo *function_table;
184
185   /* A list of the variables found in this comp. unit.  */
186   struct varinfo *variable_table;
187
188   /* Pointer to dwarf2_debug structure.  */
189   struct dwarf2_debug *stash;
190
191   /* Address size for this unit - from unit header.  */
192   unsigned char addr_size;
193
194   /* Offset size for this unit - from unit header.  */
195   unsigned char offset_size;
196
197   /* Base address for this unit - from DW_AT_low_pc attribute of
198      DW_TAG_compile_unit DIE */
199   bfd_vma base_address;
200 };
201
202 /* This data structure holds the information of an abbrev.  */
203 struct abbrev_info
204 {
205   unsigned int number;          /* Number identifying abbrev.  */
206   enum dwarf_tag tag;           /* DWARF tag.  */
207   int has_children;             /* Boolean.  */
208   unsigned int num_attrs;       /* Number of attributes.  */
209   struct attr_abbrev *attrs;    /* An array of attribute descriptions.  */
210   struct abbrev_info *next;     /* Next in chain.  */
211 };
212
213 struct attr_abbrev
214 {
215   enum dwarf_attribute name;
216   enum dwarf_form form;
217 };
218
219 #ifndef ABBREV_HASH_SIZE
220 #define ABBREV_HASH_SIZE 121
221 #endif
222 #ifndef ATTR_ALLOC_CHUNK
223 #define ATTR_ALLOC_CHUNK 4
224 #endif
225
226 /* VERBATIM
227    The following function up to the END VERBATIM mark are
228    copied directly from dwarf2read.c.  */
229
230 /* Read dwarf information from a buffer.  */
231
232 static unsigned int
233 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
234 {
235   return bfd_get_8 (abfd, buf);
236 }
237
238 static int
239 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
240 {
241   return bfd_get_signed_8 (abfd, buf);
242 }
243
244 static unsigned int
245 read_2_bytes (bfd *abfd, bfd_byte *buf)
246 {
247   return bfd_get_16 (abfd, buf);
248 }
249
250 static unsigned int
251 read_4_bytes (bfd *abfd, bfd_byte *buf)
252 {
253   return bfd_get_32 (abfd, buf);
254 }
255
256 static bfd_uint64_t
257 read_8_bytes (bfd *abfd, bfd_byte *buf)
258 {
259   return bfd_get_64 (abfd, buf);
260 }
261
262 static bfd_byte *
263 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
264               bfd_byte *buf,
265               unsigned int size ATTRIBUTE_UNUSED)
266 {
267   /* If the size of a host char is 8 bits, we can return a pointer
268      to the buffer, otherwise we have to copy the data to a buffer
269      allocated on the temporary obstack.  */
270   return buf;
271 }
272
273 static char *
274 read_string (bfd *abfd ATTRIBUTE_UNUSED,
275              bfd_byte *buf,
276              unsigned int *bytes_read_ptr)
277 {
278   /* Return a pointer to the embedded string.  */
279   char *str = (char *) buf;
280   if (*str == '\0')
281     {
282       *bytes_read_ptr = 1;
283       return NULL;
284     }
285
286   *bytes_read_ptr = strlen (str) + 1;
287   return str;
288 }
289
290 static char *
291 read_indirect_string (struct comp_unit* unit,
292                       bfd_byte *buf,
293                       unsigned int *bytes_read_ptr)
294 {
295   bfd_uint64_t offset;
296   struct dwarf2_debug *stash = unit->stash;
297   char *str;
298
299   if (unit->offset_size == 4)
300     offset = read_4_bytes (unit->abfd, buf);
301   else
302     offset = read_8_bytes (unit->abfd, buf);
303   *bytes_read_ptr = unit->offset_size;
304
305   if (! stash->dwarf_str_buffer)
306     {
307       asection *msec;
308       bfd *abfd = unit->abfd;
309       bfd_size_type sz;
310
311       msec = bfd_get_section_by_name (abfd, ".debug_str");
312       if (! msec)
313         {
314           (*_bfd_error_handler)
315             (_("Dwarf Error: Can't find .debug_str section."));
316           bfd_set_error (bfd_error_bad_value);
317           return NULL;
318         }
319
320       sz = msec->rawsize ? msec->rawsize : msec->size;
321       stash->dwarf_str_size = sz;
322       stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
323       if (! stash->dwarf_str_buffer)
324         return NULL;
325
326       if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
327                                       0, sz))
328         return NULL;
329     }
330
331   if (offset >= stash->dwarf_str_size)
332     {
333       (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
334                              (unsigned long) offset, stash->dwarf_str_size);
335       bfd_set_error (bfd_error_bad_value);
336       return NULL;
337     }
338
339   str = (char *) stash->dwarf_str_buffer + offset;
340   if (*str == '\0')
341     return NULL;
342   return str;
343 }
344
345 /* END VERBATIM */
346
347 static bfd_uint64_t
348 read_address (struct comp_unit *unit, bfd_byte *buf)
349 {
350   switch (unit->addr_size)
351     {
352     case 8:
353       return bfd_get_64 (unit->abfd, buf);
354     case 4:
355       return bfd_get_32 (unit->abfd, buf);
356     case 2:
357       return bfd_get_16 (unit->abfd, buf);
358     default:
359       abort ();
360     }
361 }
362
363 /* Lookup an abbrev_info structure in the abbrev hash table.  */
364
365 static struct abbrev_info *
366 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
367 {
368   unsigned int hash_number;
369   struct abbrev_info *abbrev;
370
371   hash_number = number % ABBREV_HASH_SIZE;
372   abbrev = abbrevs[hash_number];
373
374   while (abbrev)
375     {
376       if (abbrev->number == number)
377         return abbrev;
378       else
379         abbrev = abbrev->next;
380     }
381
382   return NULL;
383 }
384
385 /* In DWARF version 2, the description of the debugging information is
386    stored in a separate .debug_abbrev section.  Before we read any
387    dies from a section we read in all abbreviations and install them
388    in a hash table.  */
389
390 static struct abbrev_info**
391 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
392 {
393   struct abbrev_info **abbrevs;
394   bfd_byte *abbrev_ptr;
395   struct abbrev_info *cur_abbrev;
396   unsigned int abbrev_number, bytes_read, abbrev_name;
397   unsigned int abbrev_form, hash_number;
398   bfd_size_type amt;
399
400   if (! stash->dwarf_abbrev_buffer)
401     {
402       asection *msec;
403
404       msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
405       if (! msec)
406         {
407           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
408           bfd_set_error (bfd_error_bad_value);
409           return 0;
410         }
411
412       stash->dwarf_abbrev_size = msec->size;
413       stash->dwarf_abbrev_buffer
414         = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
415                                                      stash->syms);
416       if (! stash->dwarf_abbrev_buffer)
417           return 0;
418     }
419
420   if (offset >= stash->dwarf_abbrev_size)
421     {
422       (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
423                              (unsigned long) offset, stash->dwarf_abbrev_size);
424       bfd_set_error (bfd_error_bad_value);
425       return 0;
426     }
427
428   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
429   abbrevs = bfd_zalloc (abfd, amt);
430
431   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
432   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
433   abbrev_ptr += bytes_read;
434
435   /* Loop until we reach an abbrev number of 0.  */
436   while (abbrev_number)
437     {
438       amt = sizeof (struct abbrev_info);
439       cur_abbrev = bfd_zalloc (abfd, amt);
440
441       /* Read in abbrev header.  */
442       cur_abbrev->number = abbrev_number;
443       cur_abbrev->tag = (enum dwarf_tag)
444         read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
445       abbrev_ptr += bytes_read;
446       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
447       abbrev_ptr += 1;
448
449       /* Now read in declarations.  */
450       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
451       abbrev_ptr += bytes_read;
452       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
453       abbrev_ptr += bytes_read;
454
455       while (abbrev_name)
456         {
457           if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
458             {
459               struct attr_abbrev *tmp;
460
461               amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
462               amt *= sizeof (struct attr_abbrev);
463               tmp = bfd_realloc (cur_abbrev->attrs, amt);
464               if (tmp == NULL)
465                 {
466                   size_t i;
467
468                   for (i = 0; i < ABBREV_HASH_SIZE; i++)
469                     {
470                     struct abbrev_info *abbrev = abbrevs[i];
471
472                     while (abbrev)
473                       {
474                         free (abbrev->attrs);
475                         abbrev = abbrev->next;
476                       }
477                     }
478                   return NULL;
479                 }
480               cur_abbrev->attrs = tmp;
481             }
482
483           cur_abbrev->attrs[cur_abbrev->num_attrs].name
484             = (enum dwarf_attribute) abbrev_name;
485           cur_abbrev->attrs[cur_abbrev->num_attrs++].form
486             = (enum dwarf_form) abbrev_form;
487           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
488           abbrev_ptr += bytes_read;
489           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
490           abbrev_ptr += bytes_read;
491         }
492
493       hash_number = abbrev_number % ABBREV_HASH_SIZE;
494       cur_abbrev->next = abbrevs[hash_number];
495       abbrevs[hash_number] = cur_abbrev;
496
497       /* Get next abbreviation.
498          Under Irix6 the abbreviations for a compilation unit are not
499          always properly terminated with an abbrev number of 0.
500          Exit loop if we encounter an abbreviation which we have
501          already read (which means we are about to read the abbreviations
502          for the next compile unit) or if the end of the abbreviation
503          table is reached.  */
504       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
505             >= stash->dwarf_abbrev_size)
506         break;
507       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
508       abbrev_ptr += bytes_read;
509       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
510         break;
511     }
512
513   return abbrevs;
514 }
515
516 /* Read an attribute value described by an attribute form.  */
517
518 static bfd_byte *
519 read_attribute_value (struct attribute *attr,
520                       unsigned form,
521                       struct comp_unit *unit,
522                       bfd_byte *info_ptr)
523 {
524   bfd *abfd = unit->abfd;
525   unsigned int bytes_read;
526   struct dwarf_block *blk;
527   bfd_size_type amt;
528
529   attr->form = (enum dwarf_form) form;
530
531   switch (form)
532     {
533     case DW_FORM_addr:
534       /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size.  */
535     case DW_FORM_ref_addr:
536       attr->u.val = read_address (unit, info_ptr);
537       info_ptr += unit->addr_size;
538       break;
539     case DW_FORM_block2:
540       amt = sizeof (struct dwarf_block);
541       blk = bfd_alloc (abfd, amt);
542       blk->size = read_2_bytes (abfd, info_ptr);
543       info_ptr += 2;
544       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
545       info_ptr += blk->size;
546       attr->u.blk = blk;
547       break;
548     case DW_FORM_block4:
549       amt = sizeof (struct dwarf_block);
550       blk = bfd_alloc (abfd, amt);
551       blk->size = read_4_bytes (abfd, info_ptr);
552       info_ptr += 4;
553       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
554       info_ptr += blk->size;
555       attr->u.blk = blk;
556       break;
557     case DW_FORM_data2:
558       attr->u.val = read_2_bytes (abfd, info_ptr);
559       info_ptr += 2;
560       break;
561     case DW_FORM_data4:
562       attr->u.val = read_4_bytes (abfd, info_ptr);
563       info_ptr += 4;
564       break;
565     case DW_FORM_data8:
566       attr->u.val = read_8_bytes (abfd, info_ptr);
567       info_ptr += 8;
568       break;
569     case DW_FORM_string:
570       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
571       info_ptr += bytes_read;
572       break;
573     case DW_FORM_strp:
574       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
575       info_ptr += bytes_read;
576       break;
577     case DW_FORM_block:
578       amt = sizeof (struct dwarf_block);
579       blk = bfd_alloc (abfd, amt);
580       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
581       info_ptr += bytes_read;
582       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
583       info_ptr += blk->size;
584       attr->u.blk = blk;
585       break;
586     case DW_FORM_block1:
587       amt = sizeof (struct dwarf_block);
588       blk = bfd_alloc (abfd, amt);
589       blk->size = read_1_byte (abfd, info_ptr);
590       info_ptr += 1;
591       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
592       info_ptr += blk->size;
593       attr->u.blk = blk;
594       break;
595     case DW_FORM_data1:
596       attr->u.val = read_1_byte (abfd, info_ptr);
597       info_ptr += 1;
598       break;
599     case DW_FORM_flag:
600       attr->u.val = read_1_byte (abfd, info_ptr);
601       info_ptr += 1;
602       break;
603     case DW_FORM_sdata:
604       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
605       info_ptr += bytes_read;
606       break;
607     case DW_FORM_udata:
608       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
609       info_ptr += bytes_read;
610       break;
611     case DW_FORM_ref1:
612       attr->u.val = read_1_byte (abfd, info_ptr);
613       info_ptr += 1;
614       break;
615     case DW_FORM_ref2:
616       attr->u.val = read_2_bytes (abfd, info_ptr);
617       info_ptr += 2;
618       break;
619     case DW_FORM_ref4:
620       attr->u.val = read_4_bytes (abfd, info_ptr);
621       info_ptr += 4;
622       break;
623     case DW_FORM_ref8:
624       attr->u.val = read_8_bytes (abfd, info_ptr);
625       info_ptr += 8;
626       break;
627     case DW_FORM_ref_udata:
628       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
629       info_ptr += bytes_read;
630       break;
631     case DW_FORM_indirect:
632       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
633       info_ptr += bytes_read;
634       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
635       break;
636     default:
637       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
638                              form);
639       bfd_set_error (bfd_error_bad_value);
640     }
641   return info_ptr;
642 }
643
644 /* Read an attribute described by an abbreviated attribute.  */
645
646 static bfd_byte *
647 read_attribute (struct attribute *attr,
648                 struct attr_abbrev *abbrev,
649                 struct comp_unit *unit,
650                 bfd_byte *info_ptr)
651 {
652   attr->name = abbrev->name;
653   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
654   return info_ptr;
655 }
656
657 /* Source line information table routines.  */
658
659 #define FILE_ALLOC_CHUNK 5
660 #define DIR_ALLOC_CHUNK 5
661
662 struct line_info
663 {
664   struct line_info* prev_line;
665   bfd_vma address;
666   char *filename;
667   unsigned int line;
668   unsigned int column;
669   int end_sequence;             /* End of (sequential) code sequence.  */
670 };
671
672 struct fileinfo
673 {
674   char *name;
675   unsigned int dir;
676   unsigned int time;
677   unsigned int size;
678 };
679
680 struct line_info_table
681 {
682   bfd* abfd;
683   unsigned int num_files;
684   unsigned int num_dirs;
685   char *comp_dir;
686   char **dirs;
687   struct fileinfo* files;
688   struct line_info* last_line;  /* largest VMA */
689   struct line_info* lcl_head;   /* local head; used in 'add_line_info' */
690 };
691
692 /* Remember some information about each function.  If the function is
693    inlined (DW_TAG_inlined_subroutine) it may have two additional
694    attributes, DW_AT_call_file and DW_AT_call_line, which specify the
695    source code location where this function was inlined. */
696
697 struct funcinfo
698 {
699   struct funcinfo *prev_func;           /* Pointer to previous function in list of all functions */
700   struct funcinfo *caller_func;         /* Pointer to function one scope higher */
701   char *caller_file;                    /* Source location file name where caller_func inlines this func */
702   int caller_line;                      /* Source location line number where caller_func inlines this func */
703   char *file;                           /* Source location file name */
704   int line;                             /* Source location line number */
705   int tag;
706   int nesting_level;
707   char *name;
708   struct arange arange;
709   asection *sec;                        /* Where the symbol is defined */
710 };
711
712 struct varinfo
713 {
714   /* Pointer to previous variable in list of all variables */
715   struct varinfo *prev_var;
716   /* Source location file name */
717   char *file;
718   /* Source location line number */
719   int line;
720   int tag;
721   char *name;
722   /* Where the symbol is defined */
723   asection *sec;
724   /* Is this a stack variable? */
725   unsigned int stack: 1;
726 };
727
728 /* Adds a new entry to the line_info list in the line_info_table, ensuring
729    that the list is sorted.  Note that the line_info list is sorted from
730    highest to lowest VMA (with possible duplicates); that is,
731    line_info->prev_line always accesses an equal or smaller VMA.  */
732
733 static void
734 add_line_info (struct line_info_table *table,
735                bfd_vma address,
736                char *filename,
737                unsigned int line,
738                unsigned int column,
739                int end_sequence)
740 {
741   bfd_size_type amt = sizeof (struct line_info);
742   struct line_info* info = bfd_alloc (table->abfd, amt);
743
744   /* Find the correct location for 'info'.  Normally we will receive
745      new line_info data 1) in order and 2) with increasing VMAs.
746      However some compilers break the rules (cf. decode_line_info) and
747      so we include some heuristics for quickly finding the correct
748      location for 'info'. In particular, these heuristics optimize for
749      the common case in which the VMA sequence that we receive is a
750      list of locally sorted VMAs such as
751        p...z a...j  (where a < j < p < z)
752
753      Note: table->lcl_head is used to head an *actual* or *possible*
754      sequence within the list (such as a...j) that is not directly
755      headed by table->last_line
756
757      Note: we may receive duplicate entries from 'decode_line_info'.  */
758
759   while (1)
760     if (!table->last_line
761         || address >= table->last_line->address)
762       {
763         /* Normal case: add 'info' to the beginning of the list */
764         info->prev_line = table->last_line;
765         table->last_line = info;
766
767         /* lcl_head: initialize to head a *possible* sequence at the end.  */
768         if (!table->lcl_head)
769           table->lcl_head = info;
770         break;
771       }
772     else if (!table->lcl_head->prev_line
773              && table->lcl_head->address > address)
774       {
775         /* Abnormal but easy: lcl_head is 1) at the *end* of the line
776            list and 2) the head of 'info'.  */
777         info->prev_line = NULL;
778         table->lcl_head->prev_line = info;
779         break;
780       }
781     else if (table->lcl_head->prev_line
782              && table->lcl_head->address > address
783              && address >= table->lcl_head->prev_line->address)
784       {
785         /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
786            list and 2) the head of 'info'.  */
787         info->prev_line = table->lcl_head->prev_line;
788         table->lcl_head->prev_line = info;
789         break;
790       }
791     else
792       {
793         /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
794            heads for 'info'.  Reset 'lcl_head' and repeat.  */
795         struct line_info* li2 = table->last_line; /* always non-NULL */
796         struct line_info* li1 = li2->prev_line;
797
798         while (li1)
799           {
800             if (li2->address > address && address >= li1->address)
801               break;
802
803             li2 = li1; /* always non-NULL */
804             li1 = li1->prev_line;
805           }
806         table->lcl_head = li2;
807       }
808
809   /* Set member data of 'info'.  */
810   info->address = address;
811   info->line = line;
812   info->column = column;
813   info->end_sequence = end_sequence;
814
815   if (filename && filename[0])
816     {
817       info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
818       if (info->filename)
819         strcpy (info->filename, filename);
820     }
821   else
822     info->filename = NULL;
823 }
824
825 /* Extract a fully qualified filename from a line info table.
826    The returned string has been malloc'ed and it is the caller's
827    responsibility to free it.  */
828
829 static char *
830 concat_filename (struct line_info_table *table, unsigned int file)
831 {
832   char *filename;
833
834   if (file - 1 >= table->num_files)
835     {
836       (*_bfd_error_handler)
837         (_("Dwarf Error: mangled line number section (bad file number)."));
838       return strdup ("<unknown>");
839     }
840
841   filename = table->files[file - 1].name;
842
843   if (! IS_ABSOLUTE_PATH (filename))
844     {
845       char *dirname = (table->files[file - 1].dir
846                        ? table->dirs[table->files[file - 1].dir - 1]
847                        : table->comp_dir);
848
849       /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
850          The best we can do is return the filename part.  */
851       if (dirname != NULL)
852         {
853           unsigned int len = strlen (dirname) + strlen (filename) + 2;
854           char * name;
855
856           name = bfd_malloc (len);
857           if (name)
858             sprintf (name, "%s/%s", dirname, filename);
859           return name;
860         }
861     }
862
863   return strdup (filename);
864 }
865
866 static void
867 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
868 {
869   struct arange *arange;
870
871   /* If the first arange is empty, use it. */
872   if (first_arange->high == 0)
873     {
874       first_arange->low = low_pc;
875       first_arange->high = high_pc;
876       return;
877     }
878
879   /* Next see if we can cheaply extend an existing range.  */
880   arange = first_arange;
881   do
882     {
883       if (low_pc == arange->high)
884         {
885           arange->high = high_pc;
886           return;
887         }
888       if (high_pc == arange->low)
889         {
890           arange->low = low_pc;
891           return;
892         }
893       arange = arange->next;
894     }
895   while (arange);
896
897   /* Need to allocate a new arange and insert it into the arange list.
898      Order isn't significant, so just insert after the first arange. */
899   arange = bfd_zalloc (abfd, sizeof (*arange));
900   arange->low = low_pc;
901   arange->high = high_pc;
902   arange->next = first_arange->next;
903   first_arange->next = arange;
904 }
905
906 /* Decode the line number information for UNIT.  */
907
908 static struct line_info_table*
909 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
910 {
911   bfd *abfd = unit->abfd;
912   struct line_info_table* table;
913   bfd_byte *line_ptr;
914   bfd_byte *line_end;
915   struct line_head lh;
916   unsigned int i, bytes_read, offset_size;
917   char *cur_file, *cur_dir;
918   unsigned char op_code, extended_op, adj_opcode;
919   bfd_size_type amt;
920
921   if (! stash->dwarf_line_buffer)
922     {
923       asection *msec;
924
925       msec = bfd_get_section_by_name (abfd, ".debug_line");
926       if (! msec)
927         {
928           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
929           bfd_set_error (bfd_error_bad_value);
930           return 0;
931         }
932
933       stash->dwarf_line_size = msec->size;
934       stash->dwarf_line_buffer
935         = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
936                                                      stash->syms);
937       if (! stash->dwarf_line_buffer)
938         return 0;
939     }
940
941   /* It is possible to get a bad value for the line_offset.  Validate
942      it here so that we won't get a segfault below.  */
943   if (unit->line_offset >= stash->dwarf_line_size)
944     {
945       (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
946                              unit->line_offset, stash->dwarf_line_size);
947       bfd_set_error (bfd_error_bad_value);
948       return 0;
949     }
950
951   amt = sizeof (struct line_info_table);
952   table = bfd_alloc (abfd, amt);
953   table->abfd = abfd;
954   table->comp_dir = unit->comp_dir;
955
956   table->num_files = 0;
957   table->files = NULL;
958
959   table->num_dirs = 0;
960   table->dirs = NULL;
961
962   table->files = NULL;
963   table->last_line = NULL;
964   table->lcl_head = NULL;
965
966   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
967
968   /* Read in the prologue.  */
969   lh.total_length = read_4_bytes (abfd, line_ptr);
970   line_ptr += 4;
971   offset_size = 4;
972   if (lh.total_length == 0xffffffff)
973     {
974       lh.total_length = read_8_bytes (abfd, line_ptr);
975       line_ptr += 8;
976       offset_size = 8;
977     }
978   else if (lh.total_length == 0 && unit->addr_size == 8)
979     {
980       /* Handle (non-standard) 64-bit DWARF2 formats.  */
981       lh.total_length = read_4_bytes (abfd, line_ptr);
982       line_ptr += 4;
983       offset_size = 8;
984     }
985   line_end = line_ptr + lh.total_length;
986   lh.version = read_2_bytes (abfd, line_ptr);
987   line_ptr += 2;
988   if (offset_size == 4)
989     lh.prologue_length = read_4_bytes (abfd, line_ptr);
990   else
991     lh.prologue_length = read_8_bytes (abfd, line_ptr);
992   line_ptr += offset_size;
993   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
994   line_ptr += 1;
995   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
996   line_ptr += 1;
997   lh.line_base = read_1_signed_byte (abfd, line_ptr);
998   line_ptr += 1;
999   lh.line_range = read_1_byte (abfd, line_ptr);
1000   line_ptr += 1;
1001   lh.opcode_base = read_1_byte (abfd, line_ptr);
1002   line_ptr += 1;
1003   amt = lh.opcode_base * sizeof (unsigned char);
1004   lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1005
1006   lh.standard_opcode_lengths[0] = 1;
1007
1008   for (i = 1; i < lh.opcode_base; ++i)
1009     {
1010       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1011       line_ptr += 1;
1012     }
1013
1014   /* Read directory table.  */
1015   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1016     {
1017       line_ptr += bytes_read;
1018
1019       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1020         {
1021           char **tmp;
1022
1023           amt = table->num_dirs + DIR_ALLOC_CHUNK;
1024           amt *= sizeof (char *);
1025
1026           tmp = bfd_realloc (table->dirs, amt);
1027           if (tmp == NULL)
1028             {
1029               free (table->dirs);
1030               return NULL;
1031             }
1032           table->dirs = tmp;
1033         }
1034
1035       table->dirs[table->num_dirs++] = cur_dir;
1036     }
1037
1038   line_ptr += bytes_read;
1039
1040   /* Read file name table.  */
1041   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1042     {
1043       line_ptr += bytes_read;
1044
1045       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1046         {
1047           struct fileinfo *tmp;
1048
1049           amt = table->num_files + FILE_ALLOC_CHUNK;
1050           amt *= sizeof (struct fileinfo);
1051
1052           tmp = bfd_realloc (table->files, amt);
1053           if (tmp == NULL)
1054             {
1055               free (table->files);
1056               free (table->dirs);
1057               return NULL;
1058             }
1059           table->files = tmp;
1060         }
1061
1062       table->files[table->num_files].name = cur_file;
1063       table->files[table->num_files].dir =
1064         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1065       line_ptr += bytes_read;
1066       table->files[table->num_files].time =
1067         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1068       line_ptr += bytes_read;
1069       table->files[table->num_files].size =
1070         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1071       line_ptr += bytes_read;
1072       table->num_files++;
1073     }
1074
1075   line_ptr += bytes_read;
1076
1077   /* Read the statement sequences until there's nothing left.  */
1078   while (line_ptr < line_end)
1079     {
1080       /* State machine registers.  */
1081       bfd_vma address = 0;
1082       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1083       unsigned int line = 1;
1084       unsigned int column = 0;
1085       int is_stmt = lh.default_is_stmt;
1086       int basic_block = 0;
1087       int end_sequence = 0;
1088       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1089          compilers generate address sequences that are wildly out of
1090          order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1091          for ia64-Linux).  Thus, to determine the low and high
1092          address, we must compare on every DW_LNS_copy, etc.  */
1093       bfd_vma low_pc  = 0;
1094       bfd_vma high_pc = 0;
1095       bfd_boolean low_pc_set = FALSE;
1096
1097       /* Decode the table.  */
1098       while (! end_sequence)
1099         {
1100           op_code = read_1_byte (abfd, line_ptr);
1101           line_ptr += 1;
1102
1103           if (op_code >= lh.opcode_base)
1104             {
1105               /* Special operand.  */
1106               adj_opcode = op_code - lh.opcode_base;
1107               address += (adj_opcode / lh.line_range)
1108                 * lh.minimum_instruction_length;
1109               line += lh.line_base + (adj_opcode % lh.line_range);
1110               /* Append row to matrix using current values.  */
1111               add_line_info (table, address, filename, line, column, 0);
1112               basic_block = 1;
1113               if (!low_pc_set || address < low_pc)
1114                 {
1115                   low_pc_set = TRUE;
1116                   low_pc = address;
1117                 }
1118               if (address > high_pc)
1119                 high_pc = address;
1120             }
1121           else switch (op_code)
1122             {
1123             case DW_LNS_extended_op:
1124               /* Ignore length.  */
1125               line_ptr += 1;
1126               extended_op = read_1_byte (abfd, line_ptr);
1127               line_ptr += 1;
1128
1129               switch (extended_op)
1130                 {
1131                 case DW_LNE_end_sequence:
1132                   end_sequence = 1;
1133                   add_line_info (table, address, filename, line, column,
1134                                  end_sequence);
1135                   if (!low_pc_set || address < low_pc)
1136                     {
1137                       low_pc_set = TRUE;
1138                       low_pc = address;
1139                     }
1140                   if (address > high_pc)
1141                     high_pc = address;
1142                   arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1143                   break;
1144                 case DW_LNE_set_address:
1145                   address = read_address (unit, line_ptr);
1146                   line_ptr += unit->addr_size;
1147                   break;
1148                 case DW_LNE_define_file:
1149                   cur_file = read_string (abfd, line_ptr, &bytes_read);
1150                   line_ptr += bytes_read;
1151                   if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1152                     {
1153                       struct fileinfo *tmp;
1154
1155                       amt = table->num_files + FILE_ALLOC_CHUNK;
1156                       amt *= sizeof (struct fileinfo);
1157                       tmp = bfd_realloc (table->files, amt);
1158                       if (tmp == NULL)
1159                         {
1160                           free (table->files);
1161                           free (table->dirs);
1162                           free (filename);
1163                           return NULL;
1164                         }
1165                       table->files = tmp;
1166                     }
1167                   table->files[table->num_files].name = cur_file;
1168                   table->files[table->num_files].dir =
1169                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1170                   line_ptr += bytes_read;
1171                   table->files[table->num_files].time =
1172                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1173                   line_ptr += bytes_read;
1174                   table->files[table->num_files].size =
1175                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1176                   line_ptr += bytes_read;
1177                   table->num_files++;
1178                   break;
1179                 default:
1180                   (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1181                   bfd_set_error (bfd_error_bad_value);
1182                   free (filename);
1183                   free (table->files);
1184                   free (table->dirs);
1185                   return NULL;
1186                 }
1187               break;
1188             case DW_LNS_copy:
1189               add_line_info (table, address, filename, line, column, 0);
1190               basic_block = 0;
1191               if (!low_pc_set || address < low_pc)
1192                 {
1193                   low_pc_set = TRUE;
1194                   low_pc = address;
1195                 }
1196               if (address > high_pc)
1197                 high_pc = address;
1198               break;
1199             case DW_LNS_advance_pc:
1200               address += lh.minimum_instruction_length
1201                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1202               line_ptr += bytes_read;
1203               break;
1204             case DW_LNS_advance_line:
1205               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1206               line_ptr += bytes_read;
1207               break;
1208             case DW_LNS_set_file:
1209               {
1210                 unsigned int file;
1211
1212                 /* The file and directory tables are 0
1213                    based, the references are 1 based.  */
1214                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1215                 line_ptr += bytes_read;
1216                 if (filename)
1217                   free (filename);
1218                 filename = concat_filename (table, file);
1219                 break;
1220               }
1221             case DW_LNS_set_column:
1222               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1223               line_ptr += bytes_read;
1224               break;
1225             case DW_LNS_negate_stmt:
1226               is_stmt = (!is_stmt);
1227               break;
1228             case DW_LNS_set_basic_block:
1229               basic_block = 1;
1230               break;
1231             case DW_LNS_const_add_pc:
1232               address += lh.minimum_instruction_length
1233                       * ((255 - lh.opcode_base) / lh.line_range);
1234               break;
1235             case DW_LNS_fixed_advance_pc:
1236               address += read_2_bytes (abfd, line_ptr);
1237               line_ptr += 2;
1238               break;
1239             default:
1240               {
1241                 int i;
1242
1243                 /* Unknown standard opcode, ignore it.  */
1244                 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1245                   {
1246                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1247                     line_ptr += bytes_read;
1248                   }
1249               }
1250             }
1251         }
1252
1253       if (filename)
1254         free (filename);
1255     }
1256
1257   return table;
1258 }
1259
1260 /* If ADDR is within TABLE set the output parameters and return TRUE,
1261    otherwise return FALSE.  The output parameters, FILENAME_PTR and
1262    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
1263
1264 static bfd_boolean
1265 lookup_address_in_line_info_table (struct line_info_table *table,
1266                                    bfd_vma addr,
1267                                    struct funcinfo *function,
1268                                    const char **filename_ptr,
1269                                    unsigned int *linenumber_ptr)
1270 {
1271   /* Note: table->last_line should be a descendingly sorted list. */
1272   struct line_info* next_line = table->last_line;
1273   struct line_info* each_line = NULL;
1274   *filename_ptr = NULL;
1275
1276   if (!next_line)
1277     return FALSE;
1278
1279   each_line = next_line->prev_line;
1280
1281   /* Check for large addresses */
1282   if (addr > next_line->address)
1283     each_line = NULL; /* ensure we skip over the normal case */
1284
1285   /* Normal case: search the list; save  */
1286   while (each_line && next_line)
1287     {
1288       /* If we have an address match, save this info.  This allows us
1289          to return as good as results as possible for strange debugging
1290          info.  */
1291       bfd_boolean addr_match = FALSE;
1292       if (each_line->address <= addr && addr < next_line->address)
1293         {
1294           addr_match = TRUE;
1295
1296           /* If this line appears to span functions, and addr is in the
1297              later function, return the first line of that function instead
1298              of the last line of the earlier one.  This check is for GCC
1299              2.95, which emits the first line number for a function late.  */
1300
1301           if (function != NULL)
1302             {
1303               bfd_vma lowest_pc;
1304               struct arange *arange;
1305
1306               /* Find the lowest address in the function's range list */
1307               lowest_pc = function->arange.low;
1308               for (arange = &function->arange;
1309                    arange;
1310                    arange = arange->next)
1311                 {
1312                   if (function->arange.low < lowest_pc)
1313                     lowest_pc = function->arange.low;
1314                 }
1315               /* Check for spanning function and set outgoing line info */
1316               if (addr >= lowest_pc
1317                   && each_line->address < lowest_pc
1318                   && next_line->address > lowest_pc)
1319                 {
1320                   *filename_ptr = next_line->filename;
1321                   *linenumber_ptr = next_line->line;
1322                 }
1323               else
1324                 {
1325                   *filename_ptr = each_line->filename;
1326                   *linenumber_ptr = each_line->line;
1327                 }
1328             }
1329           else
1330             {
1331               *filename_ptr = each_line->filename;
1332               *linenumber_ptr = each_line->line;
1333             }
1334         }
1335
1336       if (addr_match && !each_line->end_sequence)
1337         return TRUE; /* we have definitely found what we want */
1338
1339       next_line = each_line;
1340       each_line = each_line->prev_line;
1341     }
1342
1343   /* At this point each_line is NULL but next_line is not.  If we found
1344      a candidate end-of-sequence point in the loop above, we can return
1345      that (compatibility with a bug in the Intel compiler); otherwise,
1346      assuming that we found the containing function for this address in
1347      this compilation unit, return the first line we have a number for
1348      (compatibility with GCC 2.95).  */
1349   if (*filename_ptr == NULL && function != NULL)
1350     {
1351       *filename_ptr = next_line->filename;
1352       *linenumber_ptr = next_line->line;
1353       return TRUE;
1354     }
1355
1356   return FALSE;
1357 }
1358
1359 /* Read in the .debug_ranges section for future reference */
1360
1361 static bfd_boolean
1362 read_debug_ranges (struct comp_unit *unit)
1363 {
1364   struct dwarf2_debug *stash = unit->stash;
1365   if (! stash->dwarf_ranges_buffer)
1366     {
1367       bfd *abfd = unit->abfd;
1368       asection *msec;
1369
1370       msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1371       if (! msec)
1372         {
1373           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1374           bfd_set_error (bfd_error_bad_value);
1375           return FALSE;
1376         }
1377
1378       stash->dwarf_ranges_size = msec->size;
1379       stash->dwarf_ranges_buffer
1380         = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1381                                                      stash->syms);
1382       if (! stash->dwarf_ranges_buffer)
1383         return FALSE;
1384     }
1385   return TRUE;
1386 }
1387
1388 /* Function table functions.  */
1389
1390 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1391    Note that we need to find the function that has the smallest
1392    range that contains ADDR, to handle inlined functions without
1393    depending upon them being ordered in TABLE by increasing range. */
1394
1395 static bfd_boolean
1396 lookup_address_in_function_table (struct comp_unit *unit,
1397                                   bfd_vma addr,
1398                                   struct funcinfo **function_ptr,
1399                                   const char **functionname_ptr)
1400 {
1401   struct funcinfo* each_func;
1402   struct funcinfo* best_fit = NULL;
1403   struct arange *arange;
1404
1405   for (each_func = unit->function_table;
1406        each_func;
1407        each_func = each_func->prev_func)
1408     {
1409       for (arange = &each_func->arange;
1410            arange;
1411            arange = arange->next)
1412         {
1413           if (addr >= arange->low && addr < arange->high)
1414             {
1415               if (!best_fit ||
1416                   ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1417                 best_fit = each_func;
1418             }
1419         }
1420     }
1421
1422   if (best_fit)
1423     {
1424       struct funcinfo* curr_func = best_fit;
1425
1426       *functionname_ptr = best_fit->name;
1427       *function_ptr = best_fit;
1428
1429       /* If we found a match and it is a function that was inlined,
1430          traverse the function list looking for the function at the
1431          next higher scope and save a pointer to it for future use.
1432          Note that because of the way the DWARF info is generated, and
1433          the way we build the function list, the first function at the
1434          next higher level is the one we want. */
1435
1436       for (each_func = best_fit -> prev_func;
1437            each_func && (curr_func->tag == DW_TAG_inlined_subroutine);
1438            each_func = each_func->prev_func)
1439         {
1440           if (each_func->nesting_level < curr_func->nesting_level)
1441             {
1442               curr_func->caller_func = each_func;
1443               curr_func = each_func;
1444             }
1445         }
1446       return TRUE;
1447     }
1448   else
1449     {
1450       return FALSE;
1451     }
1452 }
1453
1454 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1455    and LINENUMBER_PTR, and return TRUE.  */
1456
1457 static bfd_boolean
1458 lookup_symbol_in_function_table (struct comp_unit *unit,
1459                                  asymbol *sym,
1460                                  bfd_vma addr,
1461                                  const char **filename_ptr,
1462                                  unsigned int *linenumber_ptr)
1463 {
1464   struct funcinfo* each_func;
1465   struct funcinfo* best_fit = NULL;
1466   struct arange *arange;
1467   const char *name = bfd_asymbol_name (sym);
1468   asection *sec = bfd_get_section (sym);
1469
1470   for (each_func = unit->function_table;
1471        each_func;
1472        each_func = each_func->prev_func)
1473     {
1474       for (arange = &each_func->arange;
1475            arange;
1476            arange = arange->next)
1477         {
1478           if ((!each_func->sec || each_func->sec == sec)
1479               && addr >= arange->low
1480               && addr < arange->high
1481               && strcmp (name, each_func->name) == 0
1482               && (!best_fit
1483                   || ((arange->high - arange->low)
1484                       < (best_fit->arange.high - best_fit->arange.low))))
1485             best_fit = each_func;
1486         }
1487     }
1488
1489   if (best_fit)
1490     {
1491       best_fit->sec = sec;
1492       *filename_ptr = best_fit->file;
1493       *linenumber_ptr = best_fit->line;
1494       return TRUE;
1495     }
1496   else
1497     return FALSE;
1498 }
1499
1500 /* Variable table functions.  */
1501
1502 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1503    LINENUMBER_PTR, and return TRUE.  */
1504
1505 static bfd_boolean
1506 lookup_symbol_in_variable_table (struct comp_unit *unit,
1507                                  asymbol *sym,
1508                                  const char **filename_ptr,
1509                                  unsigned int *linenumber_ptr)
1510 {
1511   const char *name = bfd_asymbol_name (sym);
1512   asection *sec = bfd_get_section (sym);
1513   struct varinfo* each;
1514
1515   for (each = unit->variable_table; each; each = each->prev_var)
1516     if (each->stack == 0
1517         && (!each->sec || each->sec == sec)
1518         && strcmp (name, each->name) == 0)
1519       break;
1520
1521   if (each)
1522     {
1523       each->sec = sec;
1524       *filename_ptr = each->file;
1525       *linenumber_ptr = each->line;
1526       return TRUE;
1527     }
1528   else
1529     return FALSE;
1530 }
1531
1532 static char *
1533 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1534 {
1535   bfd *abfd = unit->abfd;
1536   bfd_byte *info_ptr;
1537   unsigned int abbrev_number, bytes_read, i;
1538   struct abbrev_info *abbrev;
1539   struct attribute attr;
1540   char *name = 0;
1541
1542   info_ptr = unit->info_ptr_unit + die_ref;
1543   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1544   info_ptr += bytes_read;
1545
1546   if (abbrev_number)
1547     {
1548       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1549       if (! abbrev)
1550         {
1551           (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1552                                  abbrev_number);
1553           bfd_set_error (bfd_error_bad_value);
1554         }
1555       else
1556         {
1557           for (i = 0; i < abbrev->num_attrs && !name; ++i)
1558             {
1559               info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1560               switch (attr.name)
1561                 {
1562                 case DW_AT_name:
1563                   name = attr.u.str;
1564                   break;
1565                 case DW_AT_specification:
1566                   name = find_abstract_instance_name (unit, attr.u.val);
1567                   break;
1568                 default:
1569                   break;
1570                 }
1571             }
1572         }
1573     }
1574   return (name);
1575 }
1576
1577 static void
1578 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1579 {
1580   bfd_byte *ranges_ptr;
1581   bfd_vma base_address = unit->base_address;
1582
1583   if (! unit->stash->dwarf_ranges_buffer)
1584     {
1585       if (! read_debug_ranges (unit))
1586         return;
1587     }
1588   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1589     
1590   for (;;)
1591     {
1592       bfd_vma low_pc;
1593       bfd_vma high_pc;
1594
1595       if (unit->offset_size == 4)
1596         {
1597           low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1598           ranges_ptr += 4;
1599           high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1600           ranges_ptr += 4;
1601         }
1602       else
1603         {
1604           low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1605           ranges_ptr += 8;
1606           high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1607           ranges_ptr += 8;
1608         }
1609       if (low_pc == 0 && high_pc == 0)
1610         break;
1611       if (low_pc == -1UL && high_pc != -1UL)
1612         base_address = high_pc;
1613       else
1614           arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1615     }
1616 }
1617
1618 /* DWARF2 Compilation unit functions.  */
1619
1620 /* Scan over each die in a comp. unit looking for functions to add
1621    to the function table and variables to the variable table.  */
1622
1623 static bfd_boolean
1624 scan_unit_for_symbols (struct comp_unit *unit)
1625 {
1626   bfd *abfd = unit->abfd;
1627   bfd_byte *info_ptr = unit->first_child_die_ptr;
1628   int nesting_level = 1;
1629
1630   while (nesting_level)
1631     {
1632       unsigned int abbrev_number, bytes_read, i;
1633       struct abbrev_info *abbrev;
1634       struct attribute attr;
1635       struct funcinfo *func;
1636       struct varinfo *var;
1637       bfd_vma low_pc = 0;
1638       bfd_vma high_pc = 0;
1639
1640       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1641       info_ptr += bytes_read;
1642
1643       if (! abbrev_number)
1644         {
1645           nesting_level--;
1646           continue;
1647         }
1648
1649       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1650       if (! abbrev)
1651         {
1652           (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1653                              abbrev_number);
1654           bfd_set_error (bfd_error_bad_value);
1655           return FALSE;
1656         }
1657
1658       var = NULL;
1659       if (abbrev->tag == DW_TAG_subprogram
1660           || abbrev->tag == DW_TAG_entry_point
1661           || abbrev->tag == DW_TAG_inlined_subroutine)
1662         {
1663           bfd_size_type amt = sizeof (struct funcinfo);
1664           func = bfd_zalloc (abfd, amt);
1665           func->tag = abbrev->tag;
1666           func->nesting_level = nesting_level;
1667           func->prev_func = unit->function_table;
1668           unit->function_table = func;
1669         }
1670       else
1671         {
1672           func = NULL;
1673           if (abbrev->tag == DW_TAG_variable)
1674             {
1675               bfd_size_type amt = sizeof (struct varinfo);
1676               var = bfd_zalloc (abfd, amt);
1677               var->tag = abbrev->tag;
1678               var->stack = 1;
1679               var->prev_var = unit->variable_table;
1680               unit->variable_table = var;
1681             }
1682         }
1683
1684       for (i = 0; i < abbrev->num_attrs; ++i)
1685         {
1686           info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1687
1688           if (func)
1689             {
1690               switch (attr.name)
1691                 {
1692                 case DW_AT_call_file:
1693                   func->caller_file = concat_filename (unit->line_table, attr.u.val);
1694                   break;
1695
1696                 case DW_AT_call_line:
1697                   func->caller_line = attr.u.val;
1698                   break;
1699
1700                 case DW_AT_abstract_origin:
1701                   func->name = find_abstract_instance_name (unit, attr.u.val);
1702                   break;
1703
1704                 case DW_AT_name:
1705                   /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1706                   if (func->name == NULL)
1707                     func->name = attr.u.str;
1708                   break;
1709
1710                 case DW_AT_MIPS_linkage_name:
1711                   func->name = attr.u.str;
1712                   break;
1713
1714                 case DW_AT_low_pc:
1715                   low_pc = attr.u.val;
1716                   break;
1717
1718                 case DW_AT_high_pc:
1719                   high_pc = attr.u.val;
1720                   break;
1721
1722                 case DW_AT_ranges:
1723                   read_rangelist (unit, &func->arange, attr.u.val);
1724                   break;
1725
1726                 case DW_AT_decl_file:
1727                   func->file = concat_filename (unit->line_table,
1728                                                 attr.u.val);
1729                   break;
1730
1731                 case DW_AT_decl_line:
1732                   func->line = attr.u.val;
1733                   break;
1734
1735                 default:
1736                   break;
1737                 }
1738             }
1739           else if (var)
1740             {
1741               switch (attr.name)
1742                 {
1743                 case DW_AT_name:
1744                   var->name = attr.u.str;
1745                   break;
1746
1747                 case DW_AT_decl_file:
1748                   var->file = concat_filename (unit->line_table,
1749                                                attr.u.val);
1750                   break;
1751
1752                 case DW_AT_decl_line:
1753                   var->line = attr.u.val;
1754                   break;
1755
1756                 case DW_AT_external:
1757                   if (attr.u.val != 0)
1758                     var->stack = 0;
1759                   break;
1760
1761                 case DW_AT_location:
1762                   if (var->stack)
1763                     {
1764                       switch (attr.form)
1765                         {
1766                         case DW_FORM_block:
1767                         case DW_FORM_block1:
1768                         case DW_FORM_block2:
1769                         case DW_FORM_block4:
1770                           if (*attr.u.blk->data == DW_OP_addr)
1771                             var->stack = 0;
1772                           break;
1773
1774                         default:
1775                           break;
1776                         }
1777                     }
1778                   break;
1779
1780                 default:
1781                   break;
1782                 }
1783             }
1784         }
1785
1786       if (func && high_pc != 0)
1787         {
1788           arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1789         }
1790
1791       if (abbrev->has_children)
1792         nesting_level++;
1793     }
1794
1795   return TRUE;
1796 }
1797
1798 /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
1799    includes the compilation unit header that proceeds the DIE's, but
1800    does not include the length field that precedes each compilation
1801    unit header.  END_PTR points one past the end of this comp unit.
1802    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1803
1804    This routine does not read the whole compilation unit; only enough
1805    to get to the line number information for the compilation unit.  */
1806
1807 static struct comp_unit *
1808 parse_comp_unit (bfd *abfd,
1809                  struct dwarf2_debug *stash,
1810                  bfd_vma unit_length,
1811                  bfd_byte *info_ptr_unit,
1812                  unsigned int offset_size)
1813 {
1814   struct comp_unit* unit;
1815   unsigned int version;
1816   bfd_uint64_t abbrev_offset = 0;
1817   unsigned int addr_size;
1818   struct abbrev_info** abbrevs;
1819   unsigned int abbrev_number, bytes_read, i;
1820   struct abbrev_info *abbrev;
1821   struct attribute attr;
1822   bfd_byte *info_ptr = stash->info_ptr;
1823   bfd_byte *end_ptr = info_ptr + unit_length;
1824   bfd_size_type amt;
1825   bfd_vma low_pc = 0;
1826   bfd_vma high_pc = 0;
1827
1828   version = read_2_bytes (abfd, info_ptr);
1829   info_ptr += 2;
1830   BFD_ASSERT (offset_size == 4 || offset_size == 8);
1831   if (offset_size == 4)
1832     abbrev_offset = read_4_bytes (abfd, info_ptr);
1833   else
1834     abbrev_offset = read_8_bytes (abfd, info_ptr);
1835   info_ptr += offset_size;
1836   addr_size = read_1_byte (abfd, info_ptr);
1837   info_ptr += 1;
1838
1839   if (version != 2)
1840     {
1841       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1842       bfd_set_error (bfd_error_bad_value);
1843       return 0;
1844     }
1845
1846   if (addr_size > sizeof (bfd_vma))
1847     {
1848       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1849                          addr_size,
1850                          (unsigned int) sizeof (bfd_vma));
1851       bfd_set_error (bfd_error_bad_value);
1852       return 0;
1853     }
1854
1855   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1856     {
1857       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1858       bfd_set_error (bfd_error_bad_value);
1859       return 0;
1860     }
1861
1862   /* Read the abbrevs for this compilation unit into a table.  */
1863   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1864   if (! abbrevs)
1865       return 0;
1866
1867   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1868   info_ptr += bytes_read;
1869   if (! abbrev_number)
1870     {
1871       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1872                          abbrev_number);
1873       bfd_set_error (bfd_error_bad_value);
1874       return 0;
1875     }
1876
1877   abbrev = lookup_abbrev (abbrev_number, abbrevs);
1878   if (! abbrev)
1879     {
1880       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1881                          abbrev_number);
1882       bfd_set_error (bfd_error_bad_value);
1883       return 0;
1884     }
1885
1886   amt = sizeof (struct comp_unit);
1887   unit = bfd_zalloc (abfd, amt);
1888   unit->abfd = abfd;
1889   unit->addr_size = addr_size;
1890   unit->offset_size = offset_size;
1891   unit->abbrevs = abbrevs;
1892   unit->end_ptr = end_ptr;
1893   unit->stash = stash;
1894   unit->info_ptr_unit = info_ptr_unit;
1895
1896   for (i = 0; i < abbrev->num_attrs; ++i)
1897     {
1898       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1899
1900       /* Store the data if it is of an attribute we want to keep in a
1901          partial symbol table.  */
1902       switch (attr.name)
1903         {
1904         case DW_AT_stmt_list:
1905           unit->stmtlist = 1;
1906           unit->line_offset = attr.u.val;
1907           break;
1908
1909         case DW_AT_name:
1910           unit->name = attr.u.str;
1911           break;
1912
1913         case DW_AT_low_pc:
1914           low_pc = attr.u.val;
1915           /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1916              this is the base address to use when reading location
1917              lists or range lists. */
1918           unit->base_address = low_pc;
1919           break;
1920
1921         case DW_AT_high_pc:
1922           high_pc = attr.u.val;
1923           break;
1924
1925         case DW_AT_ranges:
1926           read_rangelist (unit, &unit->arange, attr.u.val);
1927           break;
1928
1929         case DW_AT_comp_dir:
1930           {
1931             char *comp_dir = attr.u.str;
1932             if (comp_dir)
1933               {
1934                 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1935                    directory, get rid of it.  */
1936                 char *cp = strchr (comp_dir, ':');
1937
1938                 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1939                   comp_dir = cp + 1;
1940               }
1941             unit->comp_dir = comp_dir;
1942             break;
1943           }
1944
1945         default:
1946           break;
1947         }
1948     }
1949   if (high_pc != 0)
1950     {
1951       arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1952     }
1953
1954   unit->first_child_die_ptr = info_ptr;
1955   return unit;
1956 }
1957
1958 /* Return TRUE if UNIT contains the address given by ADDR.  */
1959
1960 static bfd_boolean
1961 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1962 {
1963   struct arange *arange;
1964
1965   if (unit->error)
1966     return FALSE;
1967
1968   arange = &unit->arange;
1969   do
1970     {
1971       if (addr >= arange->low && addr < arange->high)
1972         return TRUE;
1973       arange = arange->next;
1974     }
1975   while (arange);
1976
1977   return FALSE;
1978 }
1979
1980 /* If UNIT contains ADDR, set the output parameters to the values for
1981    the line containing ADDR.  The output parameters, FILENAME_PTR,
1982    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1983    to be filled in.
1984
1985    Return TRUE if UNIT contains ADDR, and no errors were encountered;
1986    FALSE otherwise.  */
1987
1988 static bfd_boolean
1989 comp_unit_find_nearest_line (struct comp_unit *unit,
1990                              bfd_vma addr,
1991                              const char **filename_ptr,
1992                              const char **functionname_ptr,
1993                              unsigned int *linenumber_ptr,
1994                              struct dwarf2_debug *stash)
1995 {
1996   bfd_boolean line_p;
1997   bfd_boolean func_p;
1998   struct funcinfo *function;
1999
2000   if (unit->error)
2001     return FALSE;
2002
2003   if (! unit->line_table)
2004     {
2005       if (! unit->stmtlist)
2006         {
2007           unit->error = 1;
2008           return FALSE;
2009         }
2010
2011       unit->line_table = decode_line_info (unit, stash);
2012
2013       if (! unit->line_table)
2014         {
2015           unit->error = 1;
2016           return FALSE;
2017         }
2018
2019       if (unit->first_child_die_ptr < unit->end_ptr
2020           && ! scan_unit_for_symbols (unit))
2021         {
2022           unit->error = 1;
2023           return FALSE;
2024         }
2025     }
2026
2027   function = NULL;
2028   func_p = lookup_address_in_function_table (unit, addr,
2029                                              &function, functionname_ptr);
2030   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2031     stash->inliner_chain = function;
2032   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2033                                               function, filename_ptr,
2034                                               linenumber_ptr);
2035   return line_p || func_p;
2036 }
2037
2038 /* If UNIT contains SYM at ADDR, set the output parameters to the
2039    values for the line containing SYM.  The output parameters,
2040    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2041    filled in.
2042
2043    Return TRUE if UNIT contains SYM, and no errors were encountered;
2044    FALSE otherwise.  */
2045
2046 static bfd_boolean
2047 comp_unit_find_line (struct comp_unit *unit,
2048                      asymbol *sym,
2049                      bfd_vma addr,
2050                      const char **filename_ptr,
2051                      unsigned int *linenumber_ptr,
2052                      struct dwarf2_debug *stash)
2053 {
2054   if (unit->error)
2055     return FALSE;
2056
2057   if (! unit->line_table)
2058     {
2059       if (! unit->stmtlist)
2060         {
2061           unit->error = 1;
2062           return FALSE;
2063         }
2064
2065       unit->line_table = decode_line_info (unit, stash);
2066
2067       if (! unit->line_table)
2068         {
2069           unit->error = 1;
2070           return FALSE;
2071         }
2072
2073       if (unit->first_child_die_ptr < unit->end_ptr
2074           && ! scan_unit_for_symbols (unit))
2075         {
2076           unit->error = 1;
2077           return FALSE;
2078         }
2079     }
2080
2081   if (sym->flags & BSF_FUNCTION)
2082     return lookup_symbol_in_function_table (unit, sym, addr,
2083                                             filename_ptr,
2084                                             linenumber_ptr);
2085   else
2086     return lookup_symbol_in_variable_table (unit, sym, filename_ptr,
2087                                             linenumber_ptr);
2088 }
2089
2090 /* Locate a section in a BFD containing debugging info.  The search starts
2091    from the section after AFTER_SEC, or from the first section in the BFD if
2092    AFTER_SEC is NULL.  The search works by examining the names of the
2093    sections.  There are two permissiable names.  The first is .debug_info.
2094    This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
2095    This is a variation on the .debug_info section which has a checksum
2096    describing the contents appended onto the name.  This allows the linker to
2097    identify and discard duplicate debugging sections for different
2098    compilation units.  */
2099 #define DWARF2_DEBUG_INFO ".debug_info"
2100 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2101
2102 static asection *
2103 find_debug_info (bfd *abfd, asection *after_sec)
2104 {
2105   asection * msec;
2106
2107   if (after_sec)
2108     msec = after_sec->next;
2109   else
2110     msec = abfd->sections;
2111
2112   while (msec)
2113     {
2114       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2115         return msec;
2116
2117       if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
2118         return msec;
2119
2120       msec = msec->next;
2121     }
2122
2123   return NULL;
2124 }
2125
2126 /* The DWARF2 version of find_nearest_line.  Return TRUE if the line
2127    is found without error.  ADDR_SIZE is the number of bytes in the
2128    initial .debug_info length field and in the abbreviation offset.
2129    You may use zero to indicate that the default value should be
2130    used.  */
2131
2132 bfd_boolean
2133 _bfd_dwarf2_find_nearest_line (bfd *abfd,
2134                                asection *section,
2135                                asymbol **symbols,
2136                                bfd_vma offset,
2137                                const char **filename_ptr,
2138                                const char **functionname_ptr,
2139                                unsigned int *linenumber_ptr,
2140                                unsigned int addr_size,
2141                                void **pinfo)
2142 {
2143   /* Read each compilation unit from the section .debug_info, and check
2144      to see if it contains the address we are searching for.  If yes,
2145      lookup the address, and return the line number info.  If no, go
2146      on to the next compilation unit.
2147
2148      We keep a list of all the previously read compilation units, and
2149      a pointer to the next un-read compilation unit.  Check the
2150      previously read units before reading more.  */
2151   struct dwarf2_debug *stash;
2152
2153   /* What address are we looking for?  */
2154   bfd_vma addr;
2155
2156   struct comp_unit* each;
2157
2158   stash = *pinfo;
2159   addr = offset;
2160   if (section->output_section)
2161     addr += section->output_section->vma + section->output_offset;
2162   else
2163     addr += section->vma;
2164   *filename_ptr = NULL;
2165   *functionname_ptr = NULL;
2166   *linenumber_ptr = 0;
2167
2168   /* The DWARF2 spec says that the initial length field, and the
2169      offset of the abbreviation table, should both be 4-byte values.
2170      However, some compilers do things differently.  */
2171   if (addr_size == 0)
2172     addr_size = 4;
2173   BFD_ASSERT (addr_size == 4 || addr_size == 8);
2174
2175   if (! stash)
2176     {
2177       bfd_size_type total_size;
2178       asection *msec;
2179       bfd_size_type amt = sizeof (struct dwarf2_debug);
2180
2181       stash = bfd_zalloc (abfd, amt);
2182       if (! stash)
2183         return FALSE;
2184
2185       *pinfo = stash;
2186
2187       msec = find_debug_info (abfd, NULL);
2188       if (! msec)
2189         /* No dwarf2 info.  Note that at this point the stash
2190            has been allocated, but contains zeros, this lets
2191            future calls to this function fail quicker.  */
2192          return FALSE;
2193
2194       /* There can be more than one DWARF2 info section in a BFD these days.
2195          Read them all in and produce one large stash.  We do this in two
2196          passes - in the first pass we just accumulate the section sizes.
2197          In the second pass we read in the section's contents.  The allows
2198          us to avoid reallocing the data as we add sections to the stash.  */
2199       for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2200         total_size += msec->size;
2201
2202       stash->info_ptr = bfd_alloc (abfd, total_size);
2203       if (stash->info_ptr == NULL)
2204         return FALSE;
2205
2206       stash->info_ptr_end = stash->info_ptr;
2207
2208       for (msec = find_debug_info (abfd, NULL);
2209            msec;
2210            msec = find_debug_info (abfd, msec))
2211         {
2212           bfd_size_type size;
2213           bfd_size_type start;
2214
2215           size = msec->size;
2216           if (size == 0)
2217             continue;
2218
2219           start = stash->info_ptr_end - stash->info_ptr;
2220
2221           if ((bfd_simple_get_relocated_section_contents
2222                (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2223             continue;
2224
2225           stash->info_ptr_end = stash->info_ptr + start + size;
2226         }
2227
2228       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2229
2230       stash->sec = find_debug_info (abfd, NULL);
2231       stash->sec_info_ptr = stash->info_ptr;
2232       stash->syms = symbols;
2233     }
2234
2235   /* A null info_ptr indicates that there is no dwarf2 info
2236      (or that an error occured while setting up the stash).  */
2237   if (! stash->info_ptr)
2238     return FALSE;
2239
2240   stash->inliner_chain = NULL;
2241
2242   /* Check the previously read comp. units first.  */
2243   for (each = stash->all_comp_units; each; each = each->next_unit)
2244     if (comp_unit_contains_address (each, addr))
2245       return comp_unit_find_nearest_line (each, addr, filename_ptr,
2246                                           functionname_ptr, linenumber_ptr,
2247                                           stash);
2248
2249   /* Read each remaining comp. units checking each as they are read.  */
2250   while (stash->info_ptr < stash->info_ptr_end)
2251     {
2252       bfd_vma length;
2253       bfd_boolean found;
2254       unsigned int offset_size = addr_size;
2255       bfd_byte *info_ptr_unit = stash->info_ptr;
2256
2257       length = read_4_bytes (abfd, stash->info_ptr);
2258       /* A 0xffffff length is the DWARF3 way of indicating we use
2259          64-bit offsets, instead of 32-bit offsets.  */
2260       if (length == 0xffffffff)
2261         {
2262           offset_size = 8;
2263           length = read_8_bytes (abfd, stash->info_ptr + 4);
2264           stash->info_ptr += 12;
2265         }
2266       /* A zero length is the IRIX way of indicating 64-bit offsets,
2267          mostly because the 64-bit length will generally fit in 32
2268          bits, and the endianness helps.  */
2269       else if (length == 0)
2270         {
2271           offset_size = 8;
2272           length = read_4_bytes (abfd, stash->info_ptr + 4);
2273           stash->info_ptr += 8;
2274         }
2275       /* In the absence of the hints above, we assume addr_size-sized
2276          offsets, for backward-compatibility with pre-DWARF3 64-bit
2277          platforms.  */
2278       else if (addr_size == 8)
2279         {
2280           length = read_8_bytes (abfd, stash->info_ptr);
2281           stash->info_ptr += 8;
2282         }
2283       else
2284         stash->info_ptr += 4;
2285
2286       if (length > 0)
2287         {
2288           each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2289                                   offset_size);
2290           stash->info_ptr += length;
2291
2292           if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2293               == stash->sec->size)
2294             {
2295               stash->sec = find_debug_info (abfd, stash->sec);
2296               stash->sec_info_ptr = stash->info_ptr;
2297             }
2298
2299           if (each)
2300             {
2301               each->next_unit = stash->all_comp_units;
2302               stash->all_comp_units = each;
2303
2304               /* DW_AT_low_pc and DW_AT_high_pc are optional for
2305                  compilation units.  If we don't have them (i.e.,
2306                  unit->high == 0), we need to consult the line info
2307                  table to see if a compilation unit contains the given
2308                  address.  */
2309               if (each->arange.high > 0)
2310                 {
2311                   if (comp_unit_contains_address (each, addr))
2312                     return comp_unit_find_nearest_line (each, addr,
2313                                                         filename_ptr,
2314                                                         functionname_ptr,
2315                                                         linenumber_ptr,
2316                                                         stash);
2317                 }
2318               else
2319                 {
2320                   found = comp_unit_find_nearest_line (each, addr,
2321                                                        filename_ptr,
2322                                                        functionname_ptr,
2323                                                        linenumber_ptr,
2324                                                        stash);
2325                   if (found)
2326                     return TRUE;
2327                 }
2328             }
2329         }
2330     }
2331
2332   return FALSE;
2333 }
2334
2335 /* The DWARF2 version of find_line.  Return TRUE if the line is found
2336    without error.  */
2337
2338 bfd_boolean
2339 _bfd_dwarf2_find_line (bfd *abfd,
2340                        asymbol **symbols,
2341                        asymbol *symbol,
2342                        const char **filename_ptr,
2343                        unsigned int *linenumber_ptr,
2344                        unsigned int addr_size,
2345                        void **pinfo)
2346 {
2347   /* Read each compilation unit from the section .debug_info, and check
2348      to see if it contains the address we are searching for.  If yes,
2349      lookup the address, and return the line number info.  If no, go
2350      on to the next compilation unit.
2351
2352      We keep a list of all the previously read compilation units, and
2353      a pointer to the next un-read compilation unit.  Check the
2354      previously read units before reading more.  */
2355   struct dwarf2_debug *stash;
2356
2357   /* What address are we looking for?  */
2358   bfd_vma addr;
2359
2360   struct comp_unit* each;
2361
2362   asection *section;
2363
2364   bfd_boolean found;
2365
2366   section = bfd_get_section (symbol);
2367
2368   addr = symbol->value;
2369   if (section->output_section)
2370     addr += section->output_section->vma + section->output_offset;
2371   else
2372     addr += section->vma;
2373
2374   *filename_ptr = NULL;
2375   stash = *pinfo;
2376   *filename_ptr = NULL;
2377   *linenumber_ptr = 0;
2378
2379   if (! stash)
2380     {
2381       bfd_size_type total_size;
2382       asection *msec;
2383       bfd_size_type amt = sizeof (struct dwarf2_debug);
2384
2385       stash = bfd_zalloc (abfd, amt);
2386       if (! stash)
2387         return FALSE;
2388
2389       *pinfo = stash;
2390
2391       msec = find_debug_info (abfd, NULL);
2392       if (! msec)
2393         /* No dwarf2 info.  Note that at this point the stash
2394            has been allocated, but contains zeros, this lets
2395            future calls to this function fail quicker.  */
2396          return FALSE;
2397
2398       /* There can be more than one DWARF2 info section in a BFD these days.
2399          Read them all in and produce one large stash.  We do this in two
2400          passes - in the first pass we just accumulate the section sizes.
2401          In the second pass we read in the section's contents.  The allows
2402          us to avoid reallocing the data as we add sections to the stash.  */
2403       for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2404         total_size += msec->size;
2405
2406       stash->info_ptr = bfd_alloc (abfd, total_size);
2407       if (stash->info_ptr == NULL)
2408         return FALSE;
2409
2410       stash->info_ptr_end = stash->info_ptr;
2411
2412       for (msec = find_debug_info (abfd, NULL);
2413            msec;
2414            msec = find_debug_info (abfd, msec))
2415         {
2416           bfd_size_type size;
2417           bfd_size_type start;
2418
2419           size = msec->size;
2420           if (size == 0)
2421             continue;
2422
2423           start = stash->info_ptr_end - stash->info_ptr;
2424
2425           if ((bfd_simple_get_relocated_section_contents
2426                (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2427             continue;
2428
2429           stash->info_ptr_end = stash->info_ptr + start + size;
2430         }
2431
2432       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2433
2434       stash->sec = find_debug_info (abfd, NULL);
2435       stash->sec_info_ptr = stash->info_ptr;
2436       stash->syms = symbols;
2437     }
2438
2439   /* A null info_ptr indicates that there is no dwarf2 info
2440      (or that an error occured while setting up the stash).  */
2441   if (! stash->info_ptr)
2442     return FALSE;
2443
2444   stash->inliner_chain = NULL;
2445
2446   /* Check the previously read comp. units first.  */
2447   for (each = stash->all_comp_units; each; each = each->next_unit)
2448     if ((symbol->flags & BSF_FUNCTION) == 0
2449         || comp_unit_contains_address (each, addr))
2450       {
2451         found = comp_unit_find_line (each, symbol, addr, filename_ptr,
2452                                      linenumber_ptr, stash);
2453         if (found)
2454           return found;
2455       }
2456
2457   /* The DWARF2 spec says that the initial length field, and the
2458      offset of the abbreviation table, should both be 4-byte values.
2459      However, some compilers do things differently.  */
2460   if (addr_size == 0)
2461     addr_size = 4;
2462   BFD_ASSERT (addr_size == 4 || addr_size == 8);
2463
2464   /* Read each remaining comp. units checking each as they are read.  */
2465   while (stash->info_ptr < stash->info_ptr_end)
2466     {
2467       bfd_vma length;
2468       unsigned int offset_size = addr_size;
2469       bfd_byte *info_ptr_unit = stash->info_ptr;
2470
2471       length = read_4_bytes (abfd, stash->info_ptr);
2472       /* A 0xffffff length is the DWARF3 way of indicating we use
2473          64-bit offsets, instead of 32-bit offsets.  */
2474       if (length == 0xffffffff)
2475         {
2476           offset_size = 8;
2477           length = read_8_bytes (abfd, stash->info_ptr + 4);
2478           stash->info_ptr += 12;
2479         }
2480       /* A zero length is the IRIX way of indicating 64-bit offsets,
2481          mostly because the 64-bit length will generally fit in 32
2482          bits, and the endianness helps.  */
2483       else if (length == 0)
2484         {
2485           offset_size = 8;
2486           length = read_4_bytes (abfd, stash->info_ptr + 4);
2487           stash->info_ptr += 8;
2488         }
2489       /* In the absence of the hints above, we assume addr_size-sized
2490          offsets, for backward-compatibility with pre-DWARF3 64-bit
2491          platforms.  */
2492       else if (addr_size == 8)
2493         {
2494           length = read_8_bytes (abfd, stash->info_ptr);
2495           stash->info_ptr += 8;
2496         }
2497       else
2498         stash->info_ptr += 4;
2499
2500       if (length > 0)
2501         {
2502           each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2503                                   offset_size);
2504           stash->info_ptr += length;
2505
2506           if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2507               == stash->sec->size)
2508             {
2509               stash->sec = find_debug_info (abfd, stash->sec);
2510               stash->sec_info_ptr = stash->info_ptr;
2511             }
2512
2513           if (each)
2514             {
2515               each->next_unit = stash->all_comp_units;
2516               stash->all_comp_units = each;
2517
2518               /* DW_AT_low_pc and DW_AT_high_pc are optional for
2519                  compilation units.  If we don't have them (i.e.,
2520                  unit->high == 0), we need to consult the line info
2521                  table to see if a compilation unit contains the given
2522                  address.  */
2523               found = (((symbol->flags & BSF_FUNCTION) == 0
2524                         || each->arange.high <= 0
2525                         || comp_unit_contains_address (each, addr))
2526                        && comp_unit_find_line (each, symbol, addr,
2527                                                filename_ptr,
2528                                                linenumber_ptr,
2529                                                stash));
2530               if (found)
2531                 return TRUE;
2532             }
2533         }
2534     }
2535
2536   return FALSE;
2537 }
2538
2539 bfd_boolean
2540 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2541                                const char **filename_ptr,
2542                                const char **functionname_ptr,
2543                                unsigned int *linenumber_ptr,
2544                                void **pinfo)
2545 {
2546   struct dwarf2_debug *stash;
2547
2548   stash = *pinfo;
2549   if (stash)
2550     {
2551       struct funcinfo *func = stash->inliner_chain;
2552       if (func && func->caller_func)
2553         {
2554           *filename_ptr = func->caller_file;
2555           *functionname_ptr = func->caller_func->name;
2556           *linenumber_ptr = func->caller_line;
2557           stash->inliner_chain = func->caller_func;
2558           return (TRUE);
2559         }
2560     }
2561
2562   return (FALSE);
2563 }
2564
2565 void
2566 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2567 {
2568   struct comp_unit *each;
2569   struct dwarf2_debug *stash;
2570
2571   if (abfd == NULL || elf_tdata (abfd) == NULL)
2572     return;
2573
2574   stash = elf_tdata (abfd)->dwarf2_find_line_info;
2575
2576   if (stash == NULL)
2577     return;
2578
2579   for (each = stash->all_comp_units; each; each = each->next_unit)
2580     {
2581       struct abbrev_info **abbrevs = each->abbrevs;
2582       size_t i;
2583
2584       for (i = 0; i < ABBREV_HASH_SIZE; i++)
2585         {
2586           struct abbrev_info *abbrev = abbrevs[i];
2587
2588           while (abbrev)
2589             {
2590               free (abbrev->attrs);
2591               abbrev = abbrev->next;
2592             }
2593         }
2594
2595       if (each->line_table)
2596         {
2597           free (each->line_table->dirs);
2598           free (each->line_table->files);
2599         }
2600     }
2601
2602   free (stash->dwarf_abbrev_buffer);
2603   free (stash->dwarf_line_buffer);
2604   free (stash->dwarf_ranges_buffer);
2605 }