2005-07-06 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   bfd_vma addr;
723   /* Where the symbol is defined */
724   asection *sec;
725   /* Is this a stack variable? */
726   unsigned int stack: 1;
727 };
728
729 /* Adds a new entry to the line_info list in the line_info_table, ensuring
730    that the list is sorted.  Note that the line_info list is sorted from
731    highest to lowest VMA (with possible duplicates); that is,
732    line_info->prev_line always accesses an equal or smaller VMA.  */
733
734 static void
735 add_line_info (struct line_info_table *table,
736                bfd_vma address,
737                char *filename,
738                unsigned int line,
739                unsigned int column,
740                int end_sequence)
741 {
742   bfd_size_type amt = sizeof (struct line_info);
743   struct line_info* info = bfd_alloc (table->abfd, amt);
744
745   /* Find the correct location for 'info'.  Normally we will receive
746      new line_info data 1) in order and 2) with increasing VMAs.
747      However some compilers break the rules (cf. decode_line_info) and
748      so we include some heuristics for quickly finding the correct
749      location for 'info'. In particular, these heuristics optimize for
750      the common case in which the VMA sequence that we receive is a
751      list of locally sorted VMAs such as
752        p...z a...j  (where a < j < p < z)
753
754      Note: table->lcl_head is used to head an *actual* or *possible*
755      sequence within the list (such as a...j) that is not directly
756      headed by table->last_line
757
758      Note: we may receive duplicate entries from 'decode_line_info'.  */
759
760   while (1)
761     if (!table->last_line
762         || address >= table->last_line->address)
763       {
764         /* Normal case: add 'info' to the beginning of the list */
765         info->prev_line = table->last_line;
766         table->last_line = info;
767
768         /* lcl_head: initialize to head a *possible* sequence at the end.  */
769         if (!table->lcl_head)
770           table->lcl_head = info;
771         break;
772       }
773     else if (!table->lcl_head->prev_line
774              && table->lcl_head->address > address)
775       {
776         /* Abnormal but easy: lcl_head is 1) at the *end* of the line
777            list and 2) the head of 'info'.  */
778         info->prev_line = NULL;
779         table->lcl_head->prev_line = info;
780         break;
781       }
782     else if (table->lcl_head->prev_line
783              && table->lcl_head->address > address
784              && address >= table->lcl_head->prev_line->address)
785       {
786         /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
787            list and 2) the head of 'info'.  */
788         info->prev_line = table->lcl_head->prev_line;
789         table->lcl_head->prev_line = info;
790         break;
791       }
792     else
793       {
794         /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
795            heads for 'info'.  Reset 'lcl_head' and repeat.  */
796         struct line_info* li2 = table->last_line; /* always non-NULL */
797         struct line_info* li1 = li2->prev_line;
798
799         while (li1)
800           {
801             if (li2->address > address && address >= li1->address)
802               break;
803
804             li2 = li1; /* always non-NULL */
805             li1 = li1->prev_line;
806           }
807         table->lcl_head = li2;
808       }
809
810   /* Set member data of 'info'.  */
811   info->address = address;
812   info->line = line;
813   info->column = column;
814   info->end_sequence = end_sequence;
815
816   if (filename && filename[0])
817     {
818       info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
819       if (info->filename)
820         strcpy (info->filename, filename);
821     }
822   else
823     info->filename = NULL;
824 }
825
826 /* Extract a fully qualified filename from a line info table.
827    The returned string has been malloc'ed and it is the caller's
828    responsibility to free it.  */
829
830 static char *
831 concat_filename (struct line_info_table *table, unsigned int file)
832 {
833   char *filename;
834
835   if (file - 1 >= table->num_files)
836     {
837       (*_bfd_error_handler)
838         (_("Dwarf Error: mangled line number section (bad file number)."));
839       return strdup ("<unknown>");
840     }
841
842   filename = table->files[file - 1].name;
843
844   if (! IS_ABSOLUTE_PATH (filename))
845     {
846       char *dirname = (table->files[file - 1].dir
847                        ? table->dirs[table->files[file - 1].dir - 1]
848                        : table->comp_dir);
849
850       /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
851          The best we can do is return the filename part.  */
852       if (dirname != NULL)
853         {
854           unsigned int len = strlen (dirname) + strlen (filename) + 2;
855           char * name;
856
857           name = bfd_malloc (len);
858           if (name)
859             sprintf (name, "%s/%s", dirname, filename);
860           return name;
861         }
862     }
863
864   return strdup (filename);
865 }
866
867 static void
868 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
869 {
870   struct arange *arange;
871
872   /* If the first arange is empty, use it. */
873   if (first_arange->high == 0)
874     {
875       first_arange->low = low_pc;
876       first_arange->high = high_pc;
877       return;
878     }
879
880   /* Next see if we can cheaply extend an existing range.  */
881   arange = first_arange;
882   do
883     {
884       if (low_pc == arange->high)
885         {
886           arange->high = high_pc;
887           return;
888         }
889       if (high_pc == arange->low)
890         {
891           arange->low = low_pc;
892           return;
893         }
894       arange = arange->next;
895     }
896   while (arange);
897
898   /* Need to allocate a new arange and insert it into the arange list.
899      Order isn't significant, so just insert after the first arange. */
900   arange = bfd_zalloc (abfd, sizeof (*arange));
901   arange->low = low_pc;
902   arange->high = high_pc;
903   arange->next = first_arange->next;
904   first_arange->next = arange;
905 }
906
907 /* Decode the line number information for UNIT.  */
908
909 static struct line_info_table*
910 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
911 {
912   bfd *abfd = unit->abfd;
913   struct line_info_table* table;
914   bfd_byte *line_ptr;
915   bfd_byte *line_end;
916   struct line_head lh;
917   unsigned int i, bytes_read, offset_size;
918   char *cur_file, *cur_dir;
919   unsigned char op_code, extended_op, adj_opcode;
920   bfd_size_type amt;
921
922   if (! stash->dwarf_line_buffer)
923     {
924       asection *msec;
925
926       msec = bfd_get_section_by_name (abfd, ".debug_line");
927       if (! msec)
928         {
929           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
930           bfd_set_error (bfd_error_bad_value);
931           return 0;
932         }
933
934       stash->dwarf_line_size = msec->size;
935       stash->dwarf_line_buffer
936         = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
937                                                      stash->syms);
938       if (! stash->dwarf_line_buffer)
939         return 0;
940     }
941
942   /* It is possible to get a bad value for the line_offset.  Validate
943      it here so that we won't get a segfault below.  */
944   if (unit->line_offset >= stash->dwarf_line_size)
945     {
946       (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
947                              unit->line_offset, stash->dwarf_line_size);
948       bfd_set_error (bfd_error_bad_value);
949       return 0;
950     }
951
952   amt = sizeof (struct line_info_table);
953   table = bfd_alloc (abfd, amt);
954   table->abfd = abfd;
955   table->comp_dir = unit->comp_dir;
956
957   table->num_files = 0;
958   table->files = NULL;
959
960   table->num_dirs = 0;
961   table->dirs = NULL;
962
963   table->files = NULL;
964   table->last_line = NULL;
965   table->lcl_head = NULL;
966
967   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
968
969   /* Read in the prologue.  */
970   lh.total_length = read_4_bytes (abfd, line_ptr);
971   line_ptr += 4;
972   offset_size = 4;
973   if (lh.total_length == 0xffffffff)
974     {
975       lh.total_length = read_8_bytes (abfd, line_ptr);
976       line_ptr += 8;
977       offset_size = 8;
978     }
979   else if (lh.total_length == 0 && unit->addr_size == 8)
980     {
981       /* Handle (non-standard) 64-bit DWARF2 formats.  */
982       lh.total_length = read_4_bytes (abfd, line_ptr);
983       line_ptr += 4;
984       offset_size = 8;
985     }
986   line_end = line_ptr + lh.total_length;
987   lh.version = read_2_bytes (abfd, line_ptr);
988   line_ptr += 2;
989   if (offset_size == 4)
990     lh.prologue_length = read_4_bytes (abfd, line_ptr);
991   else
992     lh.prologue_length = read_8_bytes (abfd, line_ptr);
993   line_ptr += offset_size;
994   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
995   line_ptr += 1;
996   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
997   line_ptr += 1;
998   lh.line_base = read_1_signed_byte (abfd, line_ptr);
999   line_ptr += 1;
1000   lh.line_range = read_1_byte (abfd, line_ptr);
1001   line_ptr += 1;
1002   lh.opcode_base = read_1_byte (abfd, line_ptr);
1003   line_ptr += 1;
1004   amt = lh.opcode_base * sizeof (unsigned char);
1005   lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1006
1007   lh.standard_opcode_lengths[0] = 1;
1008
1009   for (i = 1; i < lh.opcode_base; ++i)
1010     {
1011       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1012       line_ptr += 1;
1013     }
1014
1015   /* Read directory table.  */
1016   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1017     {
1018       line_ptr += bytes_read;
1019
1020       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1021         {
1022           char **tmp;
1023
1024           amt = table->num_dirs + DIR_ALLOC_CHUNK;
1025           amt *= sizeof (char *);
1026
1027           tmp = bfd_realloc (table->dirs, amt);
1028           if (tmp == NULL)
1029             {
1030               free (table->dirs);
1031               return NULL;
1032             }
1033           table->dirs = tmp;
1034         }
1035
1036       table->dirs[table->num_dirs++] = cur_dir;
1037     }
1038
1039   line_ptr += bytes_read;
1040
1041   /* Read file name table.  */
1042   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1043     {
1044       line_ptr += bytes_read;
1045
1046       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1047         {
1048           struct fileinfo *tmp;
1049
1050           amt = table->num_files + FILE_ALLOC_CHUNK;
1051           amt *= sizeof (struct fileinfo);
1052
1053           tmp = bfd_realloc (table->files, amt);
1054           if (tmp == NULL)
1055             {
1056               free (table->files);
1057               free (table->dirs);
1058               return NULL;
1059             }
1060           table->files = tmp;
1061         }
1062
1063       table->files[table->num_files].name = cur_file;
1064       table->files[table->num_files].dir =
1065         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1066       line_ptr += bytes_read;
1067       table->files[table->num_files].time =
1068         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1069       line_ptr += bytes_read;
1070       table->files[table->num_files].size =
1071         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1072       line_ptr += bytes_read;
1073       table->num_files++;
1074     }
1075
1076   line_ptr += bytes_read;
1077
1078   /* Read the statement sequences until there's nothing left.  */
1079   while (line_ptr < line_end)
1080     {
1081       /* State machine registers.  */
1082       bfd_vma address = 0;
1083       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1084       unsigned int line = 1;
1085       unsigned int column = 0;
1086       int is_stmt = lh.default_is_stmt;
1087       int basic_block = 0;
1088       int end_sequence = 0;
1089       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1090          compilers generate address sequences that are wildly out of
1091          order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1092          for ia64-Linux).  Thus, to determine the low and high
1093          address, we must compare on every DW_LNS_copy, etc.  */
1094       bfd_vma low_pc  = (bfd_vma) -1;
1095       bfd_vma high_pc = 0;
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 (address < low_pc)
1114                 low_pc = address;
1115               if (address > high_pc)
1116                 high_pc = address;
1117             }
1118           else switch (op_code)
1119             {
1120             case DW_LNS_extended_op:
1121               /* Ignore length.  */
1122               line_ptr += 1;
1123               extended_op = read_1_byte (abfd, line_ptr);
1124               line_ptr += 1;
1125
1126               switch (extended_op)
1127                 {
1128                 case DW_LNE_end_sequence:
1129                   end_sequence = 1;
1130                   add_line_info (table, address, filename, line, column,
1131                                  end_sequence);
1132                   if (address < low_pc)
1133                     low_pc = address;
1134                   if (address > high_pc)
1135                     high_pc = address;
1136                   arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1137                   break;
1138                 case DW_LNE_set_address:
1139                   address = read_address (unit, line_ptr);
1140                   line_ptr += unit->addr_size;
1141                   break;
1142                 case DW_LNE_define_file:
1143                   cur_file = read_string (abfd, line_ptr, &bytes_read);
1144                   line_ptr += bytes_read;
1145                   if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1146                     {
1147                       struct fileinfo *tmp;
1148
1149                       amt = table->num_files + FILE_ALLOC_CHUNK;
1150                       amt *= sizeof (struct fileinfo);
1151                       tmp = bfd_realloc (table->files, amt);
1152                       if (tmp == NULL)
1153                         {
1154                           free (table->files);
1155                           free (table->dirs);
1156                           free (filename);
1157                           return NULL;
1158                         }
1159                       table->files = tmp;
1160                     }
1161                   table->files[table->num_files].name = cur_file;
1162                   table->files[table->num_files].dir =
1163                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1164                   line_ptr += bytes_read;
1165                   table->files[table->num_files].time =
1166                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1167                   line_ptr += bytes_read;
1168                   table->files[table->num_files].size =
1169                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1170                   line_ptr += bytes_read;
1171                   table->num_files++;
1172                   break;
1173                 default:
1174                   (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1175                   bfd_set_error (bfd_error_bad_value);
1176                   free (filename);
1177                   free (table->files);
1178                   free (table->dirs);
1179                   return NULL;
1180                 }
1181               break;
1182             case DW_LNS_copy:
1183               add_line_info (table, address, filename, line, column, 0);
1184               basic_block = 0;
1185               if (address < low_pc)
1186                 low_pc = address;
1187               if (address > high_pc)
1188                 high_pc = address;
1189               break;
1190             case DW_LNS_advance_pc:
1191               address += lh.minimum_instruction_length
1192                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1193               line_ptr += bytes_read;
1194               break;
1195             case DW_LNS_advance_line:
1196               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1197               line_ptr += bytes_read;
1198               break;
1199             case DW_LNS_set_file:
1200               {
1201                 unsigned int file;
1202
1203                 /* The file and directory tables are 0
1204                    based, the references are 1 based.  */
1205                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1206                 line_ptr += bytes_read;
1207                 if (filename)
1208                   free (filename);
1209                 filename = concat_filename (table, file);
1210                 break;
1211               }
1212             case DW_LNS_set_column:
1213               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1214               line_ptr += bytes_read;
1215               break;
1216             case DW_LNS_negate_stmt:
1217               is_stmt = (!is_stmt);
1218               break;
1219             case DW_LNS_set_basic_block:
1220               basic_block = 1;
1221               break;
1222             case DW_LNS_const_add_pc:
1223               address += lh.minimum_instruction_length
1224                       * ((255 - lh.opcode_base) / lh.line_range);
1225               break;
1226             case DW_LNS_fixed_advance_pc:
1227               address += read_2_bytes (abfd, line_ptr);
1228               line_ptr += 2;
1229               break;
1230             default:
1231               {
1232                 int i;
1233
1234                 /* Unknown standard opcode, ignore it.  */
1235                 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1236                   {
1237                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1238                     line_ptr += bytes_read;
1239                   }
1240               }
1241             }
1242         }
1243
1244       if (filename)
1245         free (filename);
1246     }
1247
1248   return table;
1249 }
1250
1251 /* If ADDR is within TABLE set the output parameters and return TRUE,
1252    otherwise return FALSE.  The output parameters, FILENAME_PTR and
1253    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
1254
1255 static bfd_boolean
1256 lookup_address_in_line_info_table (struct line_info_table *table,
1257                                    bfd_vma addr,
1258                                    struct funcinfo *function,
1259                                    const char **filename_ptr,
1260                                    unsigned int *linenumber_ptr)
1261 {
1262   /* Note: table->last_line should be a descendingly sorted list. */
1263   struct line_info* next_line = table->last_line;
1264   struct line_info* each_line = NULL;
1265   *filename_ptr = NULL;
1266
1267   if (!next_line)
1268     return FALSE;
1269
1270   each_line = next_line->prev_line;
1271
1272   /* Check for large addresses */
1273   if (addr > next_line->address)
1274     each_line = NULL; /* ensure we skip over the normal case */
1275
1276   /* Normal case: search the list; save  */
1277   while (each_line && next_line)
1278     {
1279       /* If we have an address match, save this info.  This allows us
1280          to return as good as results as possible for strange debugging
1281          info.  */
1282       bfd_boolean addr_match = FALSE;
1283       if (each_line->address <= addr && addr < next_line->address)
1284         {
1285           addr_match = TRUE;
1286
1287           /* If this line appears to span functions, and addr is in the
1288              later function, return the first line of that function instead
1289              of the last line of the earlier one.  This check is for GCC
1290              2.95, which emits the first line number for a function late.  */
1291
1292           if (function != NULL)
1293             {
1294               bfd_vma lowest_pc;
1295               struct arange *arange;
1296
1297               /* Find the lowest address in the function's range list */
1298               lowest_pc = function->arange.low;
1299               for (arange = &function->arange;
1300                    arange;
1301                    arange = arange->next)
1302                 {
1303                   if (function->arange.low < lowest_pc)
1304                     lowest_pc = function->arange.low;
1305                 }
1306               /* Check for spanning function and set outgoing line info */
1307               if (addr >= lowest_pc
1308                   && each_line->address < lowest_pc
1309                   && next_line->address > lowest_pc)
1310                 {
1311                   *filename_ptr = next_line->filename;
1312                   *linenumber_ptr = next_line->line;
1313                 }
1314               else
1315                 {
1316                   *filename_ptr = each_line->filename;
1317                   *linenumber_ptr = each_line->line;
1318                 }
1319             }
1320           else
1321             {
1322               *filename_ptr = each_line->filename;
1323               *linenumber_ptr = each_line->line;
1324             }
1325         }
1326
1327       if (addr_match && !each_line->end_sequence)
1328         return TRUE; /* we have definitely found what we want */
1329
1330       next_line = each_line;
1331       each_line = each_line->prev_line;
1332     }
1333
1334   /* At this point each_line is NULL but next_line is not.  If we found
1335      a candidate end-of-sequence point in the loop above, we can return
1336      that (compatibility with a bug in the Intel compiler); otherwise,
1337      assuming that we found the containing function for this address in
1338      this compilation unit, return the first line we have a number for
1339      (compatibility with GCC 2.95).  */
1340   if (*filename_ptr == NULL && function != NULL)
1341     {
1342       *filename_ptr = next_line->filename;
1343       *linenumber_ptr = next_line->line;
1344       return TRUE;
1345     }
1346
1347   return FALSE;
1348 }
1349
1350 /* Read in the .debug_ranges section for future reference */
1351
1352 static bfd_boolean
1353 read_debug_ranges (struct comp_unit *unit)
1354 {
1355   struct dwarf2_debug *stash = unit->stash;
1356   if (! stash->dwarf_ranges_buffer)
1357     {
1358       bfd *abfd = unit->abfd;
1359       asection *msec;
1360
1361       msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1362       if (! msec)
1363         {
1364           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1365           bfd_set_error (bfd_error_bad_value);
1366           return FALSE;
1367         }
1368
1369       stash->dwarf_ranges_size = msec->size;
1370       stash->dwarf_ranges_buffer
1371         = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1372                                                      stash->syms);
1373       if (! stash->dwarf_ranges_buffer)
1374         return FALSE;
1375     }
1376   return TRUE;
1377 }
1378
1379 /* Function table functions.  */
1380
1381 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1382    Note that we need to find the function that has the smallest
1383    range that contains ADDR, to handle inlined functions without
1384    depending upon them being ordered in TABLE by increasing range. */
1385
1386 static bfd_boolean
1387 lookup_address_in_function_table (struct comp_unit *unit,
1388                                   bfd_vma addr,
1389                                   struct funcinfo **function_ptr,
1390                                   const char **functionname_ptr)
1391 {
1392   struct funcinfo* each_func;
1393   struct funcinfo* best_fit = NULL;
1394   struct arange *arange;
1395
1396   for (each_func = unit->function_table;
1397        each_func;
1398        each_func = each_func->prev_func)
1399     {
1400       for (arange = &each_func->arange;
1401            arange;
1402            arange = arange->next)
1403         {
1404           if (addr >= arange->low && addr < arange->high)
1405             {
1406               if (!best_fit ||
1407                   ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1408                 best_fit = each_func;
1409             }
1410         }
1411     }
1412
1413   if (best_fit)
1414     {
1415       struct funcinfo* curr_func = best_fit;
1416
1417       *functionname_ptr = best_fit->name;
1418       *function_ptr = best_fit;
1419
1420       /* If we found a match and it is a function that was inlined,
1421          traverse the function list looking for the function at the
1422          next higher scope and save a pointer to it for future use.
1423          Note that because of the way the DWARF info is generated, and
1424          the way we build the function list, the first function at the
1425          next higher level is the one we want. */
1426
1427       for (each_func = best_fit -> prev_func;
1428            each_func && (curr_func->tag == DW_TAG_inlined_subroutine);
1429            each_func = each_func->prev_func)
1430         {
1431           if (each_func->nesting_level < curr_func->nesting_level)
1432             {
1433               curr_func->caller_func = each_func;
1434               curr_func = each_func;
1435             }
1436         }
1437       return TRUE;
1438     }
1439   else
1440     {
1441       return FALSE;
1442     }
1443 }
1444
1445 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1446    and LINENUMBER_PTR, and return TRUE.  */
1447
1448 static bfd_boolean
1449 lookup_symbol_in_function_table (struct comp_unit *unit,
1450                                  asymbol *sym,
1451                                  bfd_vma addr,
1452                                  const char **filename_ptr,
1453                                  unsigned int *linenumber_ptr)
1454 {
1455   struct funcinfo* each_func;
1456   struct funcinfo* best_fit = NULL;
1457   struct arange *arange;
1458   const char *name = bfd_asymbol_name (sym);
1459   asection *sec = bfd_get_section (sym);
1460
1461   for (each_func = unit->function_table;
1462        each_func;
1463        each_func = each_func->prev_func)
1464     {
1465       for (arange = &each_func->arange;
1466            arange;
1467            arange = arange->next)
1468         {
1469           if ((!each_func->sec || each_func->sec == sec)
1470               && addr >= arange->low
1471               && addr < arange->high
1472               && strcmp (name, each_func->name) == 0
1473               && (!best_fit
1474                   || ((arange->high - arange->low)
1475                       < (best_fit->arange.high - best_fit->arange.low))))
1476             best_fit = each_func;
1477         }
1478     }
1479
1480   if (best_fit)
1481     {
1482       best_fit->sec = sec;
1483       *filename_ptr = best_fit->file;
1484       *linenumber_ptr = best_fit->line;
1485       return TRUE;
1486     }
1487   else
1488     return FALSE;
1489 }
1490
1491 /* Variable table functions.  */
1492
1493 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1494    LINENUMBER_PTR, and return TRUE.  */
1495
1496 static bfd_boolean
1497 lookup_symbol_in_variable_table (struct comp_unit *unit,
1498                                  asymbol *sym,
1499                                  bfd_vma addr,
1500                                  const char **filename_ptr,
1501                                  unsigned int *linenumber_ptr)
1502 {
1503   const char *name = bfd_asymbol_name (sym);
1504   asection *sec = bfd_get_section (sym);
1505   struct varinfo* each;
1506
1507   for (each = unit->variable_table; each; each = each->prev_var)
1508     if (each->stack == 0
1509         && each->file != NULL
1510         && each->name != NULL
1511         && each->addr == addr
1512         && (!each->sec || each->sec == sec)
1513         && strcmp (name, each->name) == 0)
1514       break;
1515
1516   if (each)
1517     {
1518       each->sec = sec;
1519       *filename_ptr = each->file;
1520       *linenumber_ptr = each->line;
1521       return TRUE;
1522     }
1523   else
1524     return FALSE;
1525 }
1526
1527 static char *
1528 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1529 {
1530   bfd *abfd = unit->abfd;
1531   bfd_byte *info_ptr;
1532   unsigned int abbrev_number, bytes_read, i;
1533   struct abbrev_info *abbrev;
1534   struct attribute attr;
1535   char *name = 0;
1536
1537   info_ptr = unit->info_ptr_unit + die_ref;
1538   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1539   info_ptr += bytes_read;
1540
1541   if (abbrev_number)
1542     {
1543       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1544       if (! abbrev)
1545         {
1546           (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1547                                  abbrev_number);
1548           bfd_set_error (bfd_error_bad_value);
1549         }
1550       else
1551         {
1552           for (i = 0; i < abbrev->num_attrs && !name; ++i)
1553             {
1554               info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1555               switch (attr.name)
1556                 {
1557                 case DW_AT_name:
1558                   name = attr.u.str;
1559                   break;
1560                 case DW_AT_specification:
1561                   name = find_abstract_instance_name (unit, attr.u.val);
1562                   break;
1563                 default:
1564                   break;
1565                 }
1566             }
1567         }
1568     }
1569   return (name);
1570 }
1571
1572 static void
1573 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1574 {
1575   bfd_byte *ranges_ptr;
1576   bfd_vma base_address = unit->base_address;
1577
1578   if (! unit->stash->dwarf_ranges_buffer)
1579     {
1580       if (! read_debug_ranges (unit))
1581         return;
1582     }
1583   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1584     
1585   for (;;)
1586     {
1587       bfd_vma low_pc;
1588       bfd_vma high_pc;
1589
1590       if (unit->offset_size == 4)
1591         {
1592           low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1593           ranges_ptr += 4;
1594           high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1595           ranges_ptr += 4;
1596         }
1597       else
1598         {
1599           low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1600           ranges_ptr += 8;
1601           high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1602           ranges_ptr += 8;
1603         }
1604       if (low_pc == 0 && high_pc == 0)
1605         break;
1606       if (low_pc == -1UL && high_pc != -1UL)
1607         base_address = high_pc;
1608       else
1609           arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1610     }
1611 }
1612
1613 /* DWARF2 Compilation unit functions.  */
1614
1615 /* Scan over each die in a comp. unit looking for functions to add
1616    to the function table and variables to the variable table.  */
1617
1618 static bfd_boolean
1619 scan_unit_for_symbols (struct comp_unit *unit)
1620 {
1621   bfd *abfd = unit->abfd;
1622   bfd_byte *info_ptr = unit->first_child_die_ptr;
1623   int nesting_level = 1;
1624
1625   while (nesting_level)
1626     {
1627       unsigned int abbrev_number, bytes_read, i;
1628       struct abbrev_info *abbrev;
1629       struct attribute attr;
1630       struct funcinfo *func;
1631       struct varinfo *var;
1632       bfd_vma low_pc = 0;
1633       bfd_vma high_pc = 0;
1634
1635       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1636       info_ptr += bytes_read;
1637
1638       if (! abbrev_number)
1639         {
1640           nesting_level--;
1641           continue;
1642         }
1643
1644       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1645       if (! abbrev)
1646         {
1647           (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1648                              abbrev_number);
1649           bfd_set_error (bfd_error_bad_value);
1650           return FALSE;
1651         }
1652
1653       var = NULL;
1654       if (abbrev->tag == DW_TAG_subprogram
1655           || abbrev->tag == DW_TAG_entry_point
1656           || abbrev->tag == DW_TAG_inlined_subroutine)
1657         {
1658           bfd_size_type amt = sizeof (struct funcinfo);
1659           func = bfd_zalloc (abfd, amt);
1660           func->tag = abbrev->tag;
1661           func->nesting_level = nesting_level;
1662           func->prev_func = unit->function_table;
1663           unit->function_table = func;
1664         }
1665       else
1666         {
1667           func = NULL;
1668           if (abbrev->tag == DW_TAG_variable)
1669             {
1670               bfd_size_type amt = sizeof (struct varinfo);
1671               var = bfd_zalloc (abfd, amt);
1672               var->tag = abbrev->tag;
1673               var->stack = 1;
1674               var->prev_var = unit->variable_table;
1675               unit->variable_table = var;
1676             }
1677         }
1678
1679       for (i = 0; i < abbrev->num_attrs; ++i)
1680         {
1681           info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1682
1683           if (func)
1684             {
1685               switch (attr.name)
1686                 {
1687                 case DW_AT_call_file:
1688                   func->caller_file = concat_filename (unit->line_table, attr.u.val);
1689                   break;
1690
1691                 case DW_AT_call_line:
1692                   func->caller_line = attr.u.val;
1693                   break;
1694
1695                 case DW_AT_abstract_origin:
1696                   func->name = find_abstract_instance_name (unit, attr.u.val);
1697                   break;
1698
1699                 case DW_AT_name:
1700                   /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1701                   if (func->name == NULL)
1702                     func->name = attr.u.str;
1703                   break;
1704
1705                 case DW_AT_MIPS_linkage_name:
1706                   func->name = attr.u.str;
1707                   break;
1708
1709                 case DW_AT_low_pc:
1710                   low_pc = attr.u.val;
1711                   break;
1712
1713                 case DW_AT_high_pc:
1714                   high_pc = attr.u.val;
1715                   break;
1716
1717                 case DW_AT_ranges:
1718                   read_rangelist (unit, &func->arange, attr.u.val);
1719                   break;
1720
1721                 case DW_AT_decl_file:
1722                   func->file = concat_filename (unit->line_table,
1723                                                 attr.u.val);
1724                   break;
1725
1726                 case DW_AT_decl_line:
1727                   func->line = attr.u.val;
1728                   break;
1729
1730                 default:
1731                   break;
1732                 }
1733             }
1734           else if (var)
1735             {
1736               switch (attr.name)
1737                 {
1738                 case DW_AT_name:
1739                   var->name = attr.u.str;
1740                   break;
1741
1742                 case DW_AT_decl_file:
1743                   var->file = concat_filename (unit->line_table,
1744                                                attr.u.val);
1745                   break;
1746
1747                 case DW_AT_decl_line:
1748                   var->line = attr.u.val;
1749                   break;
1750
1751                 case DW_AT_external:
1752                   if (attr.u.val != 0)
1753                     var->stack = 0;
1754                   break;
1755
1756                 case DW_AT_location:
1757                   switch (attr.form)
1758                     {
1759                     case DW_FORM_block:
1760                     case DW_FORM_block1:
1761                     case DW_FORM_block2:
1762                     case DW_FORM_block4:
1763                       if (*attr.u.blk->data == DW_OP_addr)
1764                         {
1765                           var->stack = 0;
1766                           var->addr = bfd_get ((attr.u.blk->size - 1) * 8,
1767                                                unit->abfd,
1768                                                attr.u.blk->data + 1);
1769                         }
1770                       break;
1771                     
1772                     default:
1773                       break;
1774                     }
1775                   break;
1776
1777                 default:
1778                   break;
1779                 }
1780             }
1781         }
1782
1783       if (func && high_pc != 0)
1784         {
1785           arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1786         }
1787
1788       if (abbrev->has_children)
1789         nesting_level++;
1790     }
1791
1792   return TRUE;
1793 }
1794
1795 /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
1796    includes the compilation unit header that proceeds the DIE's, but
1797    does not include the length field that precedes each compilation
1798    unit header.  END_PTR points one past the end of this comp unit.
1799    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1800
1801    This routine does not read the whole compilation unit; only enough
1802    to get to the line number information for the compilation unit.  */
1803
1804 static struct comp_unit *
1805 parse_comp_unit (bfd *abfd,
1806                  struct dwarf2_debug *stash,
1807                  bfd_vma unit_length,
1808                  bfd_byte *info_ptr_unit,
1809                  unsigned int offset_size)
1810 {
1811   struct comp_unit* unit;
1812   unsigned int version;
1813   bfd_uint64_t abbrev_offset = 0;
1814   unsigned int addr_size;
1815   struct abbrev_info** abbrevs;
1816   unsigned int abbrev_number, bytes_read, i;
1817   struct abbrev_info *abbrev;
1818   struct attribute attr;
1819   bfd_byte *info_ptr = stash->info_ptr;
1820   bfd_byte *end_ptr = info_ptr + unit_length;
1821   bfd_size_type amt;
1822   bfd_vma low_pc = 0;
1823   bfd_vma high_pc = 0;
1824
1825   version = read_2_bytes (abfd, info_ptr);
1826   info_ptr += 2;
1827   BFD_ASSERT (offset_size == 4 || offset_size == 8);
1828   if (offset_size == 4)
1829     abbrev_offset = read_4_bytes (abfd, info_ptr);
1830   else
1831     abbrev_offset = read_8_bytes (abfd, info_ptr);
1832   info_ptr += offset_size;
1833   addr_size = read_1_byte (abfd, info_ptr);
1834   info_ptr += 1;
1835
1836   if (version != 2)
1837     {
1838       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1839       bfd_set_error (bfd_error_bad_value);
1840       return 0;
1841     }
1842
1843   if (addr_size > sizeof (bfd_vma))
1844     {
1845       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1846                          addr_size,
1847                          (unsigned int) sizeof (bfd_vma));
1848       bfd_set_error (bfd_error_bad_value);
1849       return 0;
1850     }
1851
1852   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1853     {
1854       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1855       bfd_set_error (bfd_error_bad_value);
1856       return 0;
1857     }
1858
1859   /* Read the abbrevs for this compilation unit into a table.  */
1860   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1861   if (! abbrevs)
1862       return 0;
1863
1864   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1865   info_ptr += bytes_read;
1866   if (! abbrev_number)
1867     {
1868       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1869                          abbrev_number);
1870       bfd_set_error (bfd_error_bad_value);
1871       return 0;
1872     }
1873
1874   abbrev = lookup_abbrev (abbrev_number, abbrevs);
1875   if (! abbrev)
1876     {
1877       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1878                          abbrev_number);
1879       bfd_set_error (bfd_error_bad_value);
1880       return 0;
1881     }
1882
1883   amt = sizeof (struct comp_unit);
1884   unit = bfd_zalloc (abfd, amt);
1885   unit->abfd = abfd;
1886   unit->addr_size = addr_size;
1887   unit->offset_size = offset_size;
1888   unit->abbrevs = abbrevs;
1889   unit->end_ptr = end_ptr;
1890   unit->stash = stash;
1891   unit->info_ptr_unit = info_ptr_unit;
1892
1893   for (i = 0; i < abbrev->num_attrs; ++i)
1894     {
1895       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1896
1897       /* Store the data if it is of an attribute we want to keep in a
1898          partial symbol table.  */
1899       switch (attr.name)
1900         {
1901         case DW_AT_stmt_list:
1902           unit->stmtlist = 1;
1903           unit->line_offset = attr.u.val;
1904           break;
1905
1906         case DW_AT_name:
1907           unit->name = attr.u.str;
1908           break;
1909
1910         case DW_AT_low_pc:
1911           low_pc = attr.u.val;
1912           /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1913              this is the base address to use when reading location
1914              lists or range lists. */
1915           unit->base_address = low_pc;
1916           break;
1917
1918         case DW_AT_high_pc:
1919           high_pc = attr.u.val;
1920           break;
1921
1922         case DW_AT_ranges:
1923           read_rangelist (unit, &unit->arange, attr.u.val);
1924           break;
1925
1926         case DW_AT_comp_dir:
1927           {
1928             char *comp_dir = attr.u.str;
1929             if (comp_dir)
1930               {
1931                 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1932                    directory, get rid of it.  */
1933                 char *cp = strchr (comp_dir, ':');
1934
1935                 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1936                   comp_dir = cp + 1;
1937               }
1938             unit->comp_dir = comp_dir;
1939             break;
1940           }
1941
1942         default:
1943           break;
1944         }
1945     }
1946   if (high_pc != 0)
1947     {
1948       arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1949     }
1950
1951   unit->first_child_die_ptr = info_ptr;
1952   return unit;
1953 }
1954
1955 /* Return TRUE if UNIT contains the address given by ADDR.  */
1956
1957 static bfd_boolean
1958 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1959 {
1960   struct arange *arange;
1961
1962   if (unit->error)
1963     return FALSE;
1964
1965   arange = &unit->arange;
1966   do
1967     {
1968       if (addr >= arange->low && addr < arange->high)
1969         return TRUE;
1970       arange = arange->next;
1971     }
1972   while (arange);
1973
1974   return FALSE;
1975 }
1976
1977 /* If UNIT contains ADDR, set the output parameters to the values for
1978    the line containing ADDR.  The output parameters, FILENAME_PTR,
1979    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1980    to be filled in.
1981
1982    Return TRUE if UNIT contains ADDR, and no errors were encountered;
1983    FALSE otherwise.  */
1984
1985 static bfd_boolean
1986 comp_unit_find_nearest_line (struct comp_unit *unit,
1987                              bfd_vma addr,
1988                              const char **filename_ptr,
1989                              const char **functionname_ptr,
1990                              unsigned int *linenumber_ptr,
1991                              struct dwarf2_debug *stash)
1992 {
1993   bfd_boolean line_p;
1994   bfd_boolean func_p;
1995   struct funcinfo *function;
1996
1997   if (unit->error)
1998     return FALSE;
1999
2000   if (! unit->line_table)
2001     {
2002       if (! unit->stmtlist)
2003         {
2004           unit->error = 1;
2005           return FALSE;
2006         }
2007
2008       unit->line_table = decode_line_info (unit, stash);
2009
2010       if (! unit->line_table)
2011         {
2012           unit->error = 1;
2013           return FALSE;
2014         }
2015
2016       if (unit->first_child_die_ptr < unit->end_ptr
2017           && ! scan_unit_for_symbols (unit))
2018         {
2019           unit->error = 1;
2020           return FALSE;
2021         }
2022     }
2023
2024   function = NULL;
2025   func_p = lookup_address_in_function_table (unit, addr,
2026                                              &function, functionname_ptr);
2027   if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2028     stash->inliner_chain = function;
2029   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2030                                               function, filename_ptr,
2031                                               linenumber_ptr);
2032   return line_p || func_p;
2033 }
2034
2035 /* If UNIT contains SYM at ADDR, set the output parameters to the
2036    values for the line containing SYM.  The output parameters,
2037    FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2038    filled in.
2039
2040    Return TRUE if UNIT contains SYM, and no errors were encountered;
2041    FALSE otherwise.  */
2042
2043 static bfd_boolean
2044 comp_unit_find_line (struct comp_unit *unit,
2045                      asymbol *sym,
2046                      bfd_vma addr,
2047                      const char **filename_ptr,
2048                      unsigned int *linenumber_ptr,
2049                      struct dwarf2_debug *stash)
2050 {
2051   if (unit->error)
2052     return FALSE;
2053
2054   if (! unit->line_table)
2055     {
2056       if (! unit->stmtlist)
2057         {
2058           unit->error = 1;
2059           return FALSE;
2060         }
2061
2062       unit->line_table = decode_line_info (unit, stash);
2063
2064       if (! unit->line_table)
2065         {
2066           unit->error = 1;
2067           return FALSE;
2068         }
2069
2070       if (unit->first_child_die_ptr < unit->end_ptr
2071           && ! scan_unit_for_symbols (unit))
2072         {
2073           unit->error = 1;
2074           return FALSE;
2075         }
2076     }
2077
2078   if (sym->flags & BSF_FUNCTION)
2079     return lookup_symbol_in_function_table (unit, sym, addr,
2080                                             filename_ptr,
2081                                             linenumber_ptr);
2082   else
2083     return lookup_symbol_in_variable_table (unit, sym, addr,
2084                                             filename_ptr,
2085                                             linenumber_ptr);
2086 }
2087
2088 /* Locate a section in a BFD containing debugging info.  The search starts
2089    from the section after AFTER_SEC, or from the first section in the BFD if
2090    AFTER_SEC is NULL.  The search works by examining the names of the
2091    sections.  There are two permissiable names.  The first is .debug_info.
2092    This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
2093    This is a variation on the .debug_info section which has a checksum
2094    describing the contents appended onto the name.  This allows the linker to
2095    identify and discard duplicate debugging sections for different
2096    compilation units.  */
2097 #define DWARF2_DEBUG_INFO ".debug_info"
2098 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2099
2100 static asection *
2101 find_debug_info (bfd *abfd, asection *after_sec)
2102 {
2103   asection * msec;
2104
2105   if (after_sec)
2106     msec = after_sec->next;
2107   else
2108     msec = abfd->sections;
2109
2110   while (msec)
2111     {
2112       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2113         return msec;
2114
2115       if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
2116         return msec;
2117
2118       msec = msec->next;
2119     }
2120
2121   return NULL;
2122 }
2123
2124 /* The DWARF2 version of find_nearest_line.  Return TRUE if the line
2125    is found without error.  ADDR_SIZE is the number of bytes in the
2126    initial .debug_info length field and in the abbreviation offset.
2127    You may use zero to indicate that the default value should be
2128    used.  */
2129
2130 bfd_boolean
2131 _bfd_dwarf2_find_nearest_line (bfd *abfd,
2132                                asection *section,
2133                                asymbol **symbols,
2134                                bfd_vma offset,
2135                                const char **filename_ptr,
2136                                const char **functionname_ptr,
2137                                unsigned int *linenumber_ptr,
2138                                unsigned int addr_size,
2139                                void **pinfo)
2140 {
2141   /* Read each compilation unit from the section .debug_info, and check
2142      to see if it contains the address we are searching for.  If yes,
2143      lookup the address, and return the line number info.  If no, go
2144      on to the next compilation unit.
2145
2146      We keep a list of all the previously read compilation units, and
2147      a pointer to the next un-read compilation unit.  Check the
2148      previously read units before reading more.  */
2149   struct dwarf2_debug *stash;
2150
2151   /* What address are we looking for?  */
2152   bfd_vma addr;
2153
2154   struct comp_unit* each;
2155
2156   stash = *pinfo;
2157   addr = offset;
2158   if (section->output_section)
2159     addr += section->output_section->vma + section->output_offset;
2160   else
2161     addr += section->vma;
2162   *filename_ptr = NULL;
2163   *functionname_ptr = NULL;
2164   *linenumber_ptr = 0;
2165
2166   /* The DWARF2 spec says that the initial length field, and the
2167      offset of the abbreviation table, should both be 4-byte values.
2168      However, some compilers do things differently.  */
2169   if (addr_size == 0)
2170     addr_size = 4;
2171   BFD_ASSERT (addr_size == 4 || addr_size == 8);
2172
2173   if (! stash)
2174     {
2175       bfd_size_type total_size;
2176       asection *msec;
2177       bfd_size_type amt = sizeof (struct dwarf2_debug);
2178
2179       stash = bfd_zalloc (abfd, amt);
2180       if (! stash)
2181         return FALSE;
2182
2183       *pinfo = stash;
2184
2185       msec = find_debug_info (abfd, NULL);
2186       if (! msec)
2187         /* No dwarf2 info.  Note that at this point the stash
2188            has been allocated, but contains zeros, this lets
2189            future calls to this function fail quicker.  */
2190          return FALSE;
2191
2192       /* There can be more than one DWARF2 info section in a BFD these days.
2193          Read them all in and produce one large stash.  We do this in two
2194          passes - in the first pass we just accumulate the section sizes.
2195          In the second pass we read in the section's contents.  The allows
2196          us to avoid reallocing the data as we add sections to the stash.  */
2197       for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2198         total_size += msec->size;
2199
2200       stash->info_ptr = bfd_alloc (abfd, total_size);
2201       if (stash->info_ptr == NULL)
2202         return FALSE;
2203
2204       stash->info_ptr_end = stash->info_ptr;
2205
2206       for (msec = find_debug_info (abfd, NULL);
2207            msec;
2208            msec = find_debug_info (abfd, msec))
2209         {
2210           bfd_size_type size;
2211           bfd_size_type start;
2212
2213           size = msec->size;
2214           if (size == 0)
2215             continue;
2216
2217           start = stash->info_ptr_end - stash->info_ptr;
2218
2219           if ((bfd_simple_get_relocated_section_contents
2220                (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2221             continue;
2222
2223           stash->info_ptr_end = stash->info_ptr + start + size;
2224         }
2225
2226       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2227
2228       stash->sec = find_debug_info (abfd, NULL);
2229       stash->sec_info_ptr = stash->info_ptr;
2230       stash->syms = symbols;
2231     }
2232
2233   /* A null info_ptr indicates that there is no dwarf2 info
2234      (or that an error occured while setting up the stash).  */
2235   if (! stash->info_ptr)
2236     return FALSE;
2237
2238   stash->inliner_chain = NULL;
2239
2240   /* Check the previously read comp. units first.  */
2241   for (each = stash->all_comp_units; each; each = each->next_unit)
2242     if (comp_unit_contains_address (each, addr))
2243       return comp_unit_find_nearest_line (each, addr, filename_ptr,
2244                                           functionname_ptr, linenumber_ptr,
2245                                           stash);
2246
2247   /* Read each remaining comp. units checking each as they are read.  */
2248   while (stash->info_ptr < stash->info_ptr_end)
2249     {
2250       bfd_vma length;
2251       bfd_boolean found;
2252       unsigned int offset_size = addr_size;
2253       bfd_byte *info_ptr_unit = stash->info_ptr;
2254
2255       length = read_4_bytes (abfd, stash->info_ptr);
2256       /* A 0xffffff length is the DWARF3 way of indicating we use
2257          64-bit offsets, instead of 32-bit offsets.  */
2258       if (length == 0xffffffff)
2259         {
2260           offset_size = 8;
2261           length = read_8_bytes (abfd, stash->info_ptr + 4);
2262           stash->info_ptr += 12;
2263         }
2264       /* A zero length is the IRIX way of indicating 64-bit offsets,
2265          mostly because the 64-bit length will generally fit in 32
2266          bits, and the endianness helps.  */
2267       else if (length == 0)
2268         {
2269           offset_size = 8;
2270           length = read_4_bytes (abfd, stash->info_ptr + 4);
2271           stash->info_ptr += 8;
2272         }
2273       /* In the absence of the hints above, we assume addr_size-sized
2274          offsets, for backward-compatibility with pre-DWARF3 64-bit
2275          platforms.  */
2276       else if (addr_size == 8)
2277         {
2278           length = read_8_bytes (abfd, stash->info_ptr);
2279           stash->info_ptr += 8;
2280         }
2281       else
2282         stash->info_ptr += 4;
2283
2284       if (length > 0)
2285         {
2286           each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2287                                   offset_size);
2288           stash->info_ptr += length;
2289
2290           if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2291               == stash->sec->size)
2292             {
2293               stash->sec = find_debug_info (abfd, stash->sec);
2294               stash->sec_info_ptr = stash->info_ptr;
2295             }
2296
2297           if (each)
2298             {
2299               each->next_unit = stash->all_comp_units;
2300               stash->all_comp_units = each;
2301
2302               /* DW_AT_low_pc and DW_AT_high_pc are optional for
2303                  compilation units.  If we don't have them (i.e.,
2304                  unit->high == 0), we need to consult the line info
2305                  table to see if a compilation unit contains the given
2306                  address.  */
2307               if (each->arange.high > 0)
2308                 {
2309                   if (comp_unit_contains_address (each, addr))
2310                     return comp_unit_find_nearest_line (each, addr,
2311                                                         filename_ptr,
2312                                                         functionname_ptr,
2313                                                         linenumber_ptr,
2314                                                         stash);
2315                 }
2316               else
2317                 {
2318                   found = comp_unit_find_nearest_line (each, addr,
2319                                                        filename_ptr,
2320                                                        functionname_ptr,
2321                                                        linenumber_ptr,
2322                                                        stash);
2323                   if (found)
2324                     return TRUE;
2325                 }
2326             }
2327         }
2328     }
2329
2330   return FALSE;
2331 }
2332
2333 /* The DWARF2 version of find_line.  Return TRUE if the line is found
2334    without error.  */
2335
2336 bfd_boolean
2337 _bfd_dwarf2_find_line (bfd *abfd,
2338                        asymbol **symbols,
2339                        asymbol *symbol,
2340                        const char **filename_ptr,
2341                        unsigned int *linenumber_ptr,
2342                        unsigned int addr_size,
2343                        void **pinfo)
2344 {
2345   /* Read each compilation unit from the section .debug_info, and check
2346      to see if it contains the address we are searching for.  If yes,
2347      lookup the address, and return the line number info.  If no, go
2348      on to the next compilation unit.
2349
2350      We keep a list of all the previously read compilation units, and
2351      a pointer to the next un-read compilation unit.  Check the
2352      previously read units before reading more.  */
2353   struct dwarf2_debug *stash;
2354
2355   /* What address are we looking for?  */
2356   bfd_vma addr;
2357
2358   struct comp_unit* each;
2359
2360   asection *section;
2361
2362   bfd_boolean found;
2363
2364   section = bfd_get_section (symbol);
2365
2366   addr = symbol->value;
2367   if (section->output_section)
2368     addr += section->output_section->vma + section->output_offset;
2369   else
2370     addr += section->vma;
2371
2372   *filename_ptr = NULL;
2373   stash = *pinfo;
2374   *filename_ptr = NULL;
2375   *linenumber_ptr = 0;
2376
2377   if (! stash)
2378     {
2379       bfd_size_type total_size;
2380       asection *msec;
2381       bfd_size_type amt = sizeof (struct dwarf2_debug);
2382
2383       stash = bfd_zalloc (abfd, amt);
2384       if (! stash)
2385         return FALSE;
2386
2387       *pinfo = stash;
2388
2389       msec = find_debug_info (abfd, NULL);
2390       if (! msec)
2391         /* No dwarf2 info.  Note that at this point the stash
2392            has been allocated, but contains zeros, this lets
2393            future calls to this function fail quicker.  */
2394          return FALSE;
2395
2396       /* There can be more than one DWARF2 info section in a BFD these days.
2397          Read them all in and produce one large stash.  We do this in two
2398          passes - in the first pass we just accumulate the section sizes.
2399          In the second pass we read in the section's contents.  The allows
2400          us to avoid reallocing the data as we add sections to the stash.  */
2401       for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2402         total_size += msec->size;
2403
2404       stash->info_ptr = bfd_alloc (abfd, total_size);
2405       if (stash->info_ptr == NULL)
2406         return FALSE;
2407
2408       stash->info_ptr_end = stash->info_ptr;
2409
2410       for (msec = find_debug_info (abfd, NULL);
2411            msec;
2412            msec = find_debug_info (abfd, msec))
2413         {
2414           bfd_size_type size;
2415           bfd_size_type start;
2416
2417           size = msec->size;
2418           if (size == 0)
2419             continue;
2420
2421           start = stash->info_ptr_end - stash->info_ptr;
2422
2423           if ((bfd_simple_get_relocated_section_contents
2424                (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2425             continue;
2426
2427           stash->info_ptr_end = stash->info_ptr + start + size;
2428         }
2429
2430       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2431
2432       stash->sec = find_debug_info (abfd, NULL);
2433       stash->sec_info_ptr = stash->info_ptr;
2434       stash->syms = symbols;
2435     }
2436
2437   /* A null info_ptr indicates that there is no dwarf2 info
2438      (or that an error occured while setting up the stash).  */
2439   if (! stash->info_ptr)
2440     return FALSE;
2441
2442   stash->inliner_chain = NULL;
2443
2444   /* Check the previously read comp. units first.  */
2445   for (each = stash->all_comp_units; each; each = each->next_unit)
2446     if ((symbol->flags & BSF_FUNCTION) == 0
2447         || comp_unit_contains_address (each, addr))
2448       {
2449         found = comp_unit_find_line (each, symbol, addr, filename_ptr,
2450                                      linenumber_ptr, stash);
2451         if (found)
2452           return found;
2453       }
2454
2455   /* The DWARF2 spec says that the initial length field, and the
2456      offset of the abbreviation table, should both be 4-byte values.
2457      However, some compilers do things differently.  */
2458   if (addr_size == 0)
2459     addr_size = 4;
2460   BFD_ASSERT (addr_size == 4 || addr_size == 8);
2461
2462   /* Read each remaining comp. units checking each as they are read.  */
2463   while (stash->info_ptr < stash->info_ptr_end)
2464     {
2465       bfd_vma length;
2466       unsigned int offset_size = addr_size;
2467       bfd_byte *info_ptr_unit = stash->info_ptr;
2468
2469       length = read_4_bytes (abfd, stash->info_ptr);
2470       /* A 0xffffff length is the DWARF3 way of indicating we use
2471          64-bit offsets, instead of 32-bit offsets.  */
2472       if (length == 0xffffffff)
2473         {
2474           offset_size = 8;
2475           length = read_8_bytes (abfd, stash->info_ptr + 4);
2476           stash->info_ptr += 12;
2477         }
2478       /* A zero length is the IRIX way of indicating 64-bit offsets,
2479          mostly because the 64-bit length will generally fit in 32
2480          bits, and the endianness helps.  */
2481       else if (length == 0)
2482         {
2483           offset_size = 8;
2484           length = read_4_bytes (abfd, stash->info_ptr + 4);
2485           stash->info_ptr += 8;
2486         }
2487       /* In the absence of the hints above, we assume addr_size-sized
2488          offsets, for backward-compatibility with pre-DWARF3 64-bit
2489          platforms.  */
2490       else if (addr_size == 8)
2491         {
2492           length = read_8_bytes (abfd, stash->info_ptr);
2493           stash->info_ptr += 8;
2494         }
2495       else
2496         stash->info_ptr += 4;
2497
2498       if (length > 0)
2499         {
2500           each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2501                                   offset_size);
2502           stash->info_ptr += length;
2503
2504           if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2505               == stash->sec->size)
2506             {
2507               stash->sec = find_debug_info (abfd, stash->sec);
2508               stash->sec_info_ptr = stash->info_ptr;
2509             }
2510
2511           if (each)
2512             {
2513               each->next_unit = stash->all_comp_units;
2514               stash->all_comp_units = each;
2515
2516               /* DW_AT_low_pc and DW_AT_high_pc are optional for
2517                  compilation units.  If we don't have them (i.e.,
2518                  unit->high == 0), we need to consult the line info
2519                  table to see if a compilation unit contains the given
2520                  address.  */
2521               found = (((symbol->flags & BSF_FUNCTION) == 0
2522                         || each->arange.high <= 0
2523                         || comp_unit_contains_address (each, addr))
2524                        && comp_unit_find_line (each, symbol, addr,
2525                                                filename_ptr,
2526                                                linenumber_ptr,
2527                                                stash));
2528               if (found)
2529                 return TRUE;
2530             }
2531         }
2532     }
2533
2534   return FALSE;
2535 }
2536
2537 bfd_boolean
2538 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2539                                const char **filename_ptr,
2540                                const char **functionname_ptr,
2541                                unsigned int *linenumber_ptr,
2542                                void **pinfo)
2543 {
2544   struct dwarf2_debug *stash;
2545
2546   stash = *pinfo;
2547   if (stash)
2548     {
2549       struct funcinfo *func = stash->inliner_chain;
2550       if (func && func->caller_func)
2551         {
2552           *filename_ptr = func->caller_file;
2553           *functionname_ptr = func->caller_func->name;
2554           *linenumber_ptr = func->caller_line;
2555           stash->inliner_chain = func->caller_func;
2556           return (TRUE);
2557         }
2558     }
2559
2560   return (FALSE);
2561 }
2562
2563 void
2564 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2565 {
2566   struct comp_unit *each;
2567   struct dwarf2_debug *stash;
2568
2569   if (abfd == NULL || elf_tdata (abfd) == NULL)
2570     return;
2571
2572   stash = elf_tdata (abfd)->dwarf2_find_line_info;
2573
2574   if (stash == NULL)
2575     return;
2576
2577   for (each = stash->all_comp_units; each; each = each->next_unit)
2578     {
2579       struct abbrev_info **abbrevs = each->abbrevs;
2580       size_t i;
2581
2582       for (i = 0; i < ABBREV_HASH_SIZE; i++)
2583         {
2584           struct abbrev_info *abbrev = abbrevs[i];
2585
2586           while (abbrev)
2587             {
2588               free (abbrev->attrs);
2589               abbrev = abbrev->next;
2590             }
2591         }
2592
2593       if (each->line_table)
2594         {
2595           free (each->line_table->dirs);
2596           free (each->line_table->files);
2597         }
2598     }
2599
2600   free (stash->dwarf_abbrev_buffer);
2601   free (stash->dwarf_line_buffer);
2602   free (stash->dwarf_ranges_buffer);
2603 }