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