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