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