Update the address and phone number of the FSF organization in the GPL notices
[platform/upstream/binutils.git] / bfd / dwarf2.c
1 /* DWARF 2 support.
2    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005 Free Software Foundation, Inc.
4
5    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6    (gavin@cygnus.com).
7
8    From the dwarf2read.c header:
9    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10    Inc.  with support from Florida State University (under contract
11    with the Ada Joint Program Office), and Silicon Graphics, Inc.
12    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14    support in dwarfread.c
15
16    This file is part of BFD.
17
18    This program is free software; you can redistribute it and/or modify
19    it under the terms of the GNU General Public License as published by
20    the Free Software Foundation; either version 2 of the License, or (at
21    your option) any later version.
22
23    This program is distributed in the hope that it will be useful, but
24    WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public License
29    along with this program; if not, write to the Free Software
30    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
31
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "libiberty.h"
35 #include "libbfd.h"
36 #include "elf-bfd.h"
37 #include "elf/dwarf2.h"
38
39 /* The data in the .debug_line statement prologue looks like this.  */
40
41 struct line_head
42 {
43   bfd_vma total_length;
44   unsigned short version;
45   bfd_vma prologue_length;
46   unsigned char minimum_instruction_length;
47   unsigned char default_is_stmt;
48   int line_base;
49   unsigned char line_range;
50   unsigned char opcode_base;
51   unsigned char *standard_opcode_lengths;
52 };
53
54 /* Attributes have a name and a value.  */
55
56 struct attribute
57 {
58   enum dwarf_attribute name;
59   enum dwarf_form form;
60   union
61   {
62     char *str;
63     struct dwarf_block *blk;
64     bfd_uint64_t val;
65     bfd_int64_t sval;
66   }
67   u;
68 };
69
70 /* Blocks are a bunch of untyped bytes.  */
71 struct dwarf_block
72 {
73   unsigned int size;
74   bfd_byte *data;
75 };
76
77 struct dwarf2_debug
78 {
79   /* A list of all previously read comp_units.  */
80   struct comp_unit *all_comp_units;
81
82   /* The next unread compilation unit within the .debug_info section.
83      Zero indicates that the .debug_info section has not been loaded
84      into a buffer yet.  */
85   bfd_byte *info_ptr;
86
87   /* Pointer to the end of the .debug_info section memory buffer.  */
88   bfd_byte *info_ptr_end;
89
90   /* Pointer to the section and address of the beginning of the
91      section.  */
92   asection *sec;
93   bfd_byte *sec_info_ptr;
94
95   /* Pointer to the symbol table.  */
96   asymbol **syms;
97
98   /* Pointer to the .debug_abbrev section loaded into memory.  */
99   bfd_byte *dwarf_abbrev_buffer;
100
101   /* Length of the loaded .debug_abbrev section.  */
102   unsigned long dwarf_abbrev_size;
103
104   /* Buffer for decode_line_info.  */
105   bfd_byte *dwarf_line_buffer;
106
107   /* Length of the loaded .debug_line section.  */
108   unsigned long dwarf_line_size;
109
110   /* Pointer to the .debug_str section loaded into memory.  */
111   bfd_byte *dwarf_str_buffer;
112
113   /* Length of the loaded .debug_str section.  */
114   unsigned long dwarf_str_size;
115
116   /* Pointer to the .debug_ranges section loaded into memory. */
117   bfd_byte *dwarf_ranges_buffer;
118
119   /* Length of the loaded .debug_ranges section. */
120   unsigned long dwarf_ranges_size;
121 };
122
123 struct arange
124 {
125   struct arange *next;
126   bfd_vma low;
127   bfd_vma high;
128 };
129
130 /* A minimal decoding of DWARF2 compilation units.  We only decode
131    what's needed to get to the line number information.  */
132
133 struct comp_unit
134 {
135   /* Chain the previously read compilation units.  */
136   struct comp_unit *next_unit;
137
138   /* Keep the bdf convenient (for memory allocation).  */
139   bfd *abfd;
140
141   /* The lowest and highest addresses contained in this compilation
142      unit as specified in the compilation unit header.  */
143   struct arange arange;
144
145   /* The DW_AT_name attribute (for error messages).  */
146   char *name;
147
148   /* The abbrev hash table.  */
149   struct abbrev_info **abbrevs;
150
151   /* Note that an error was found by comp_unit_find_nearest_line.  */
152   int error;
153
154   /* The DW_AT_comp_dir attribute.  */
155   char *comp_dir;
156
157   /* TRUE if there is a line number table associated with this comp. unit.  */
158   int stmtlist;
159
160   /* Pointer to the current comp_unit so that we can find a given entry
161      by its reference.  */
162   bfd_byte *info_ptr_unit;
163
164   /* The offset into .debug_line of the line number table.  */
165   unsigned long line_offset;
166
167   /* Pointer to the first child die for the comp unit.  */
168   bfd_byte *first_child_die_ptr;
169
170   /* The end of the comp unit.  */
171   bfd_byte *end_ptr;
172
173   /* The decoded line number, NULL if not yet decoded.  */
174   struct line_info_table *line_table;
175
176   /* A list of the functions found in this comp. unit.  */
177   struct funcinfo *function_table;
178
179   /* Pointer to dwarf2_debug structure.  */
180   struct dwarf2_debug *stash;
181
182   /* Address size for this unit - from unit header.  */
183   unsigned char addr_size;
184
185   /* Offset size for this unit - from unit header.  */
186   unsigned char offset_size;
187
188   /* Base address for this unit - from DW_AT_low_pc attribute of
189      DW_TAG_compile_unit DIE */
190   bfd_vma base_address;
191 };
192
193 /* This data structure holds the information of an abbrev.  */
194 struct abbrev_info
195 {
196   unsigned int number;          /* Number identifying abbrev.  */
197   enum dwarf_tag tag;           /* DWARF tag.  */
198   int has_children;             /* Boolean.  */
199   unsigned int num_attrs;       /* Number of attributes.  */
200   struct attr_abbrev *attrs;    /* An array of attribute descriptions.  */
201   struct abbrev_info *next;     /* Next in chain.  */
202 };
203
204 struct attr_abbrev
205 {
206   enum dwarf_attribute name;
207   enum dwarf_form form;
208 };
209
210 #ifndef ABBREV_HASH_SIZE
211 #define ABBREV_HASH_SIZE 121
212 #endif
213 #ifndef ATTR_ALLOC_CHUNK
214 #define ATTR_ALLOC_CHUNK 4
215 #endif
216
217 /* VERBATIM
218    The following function up to the END VERBATIM mark are
219    copied directly from dwarf2read.c.  */
220
221 /* Read dwarf information from a buffer.  */
222
223 static unsigned int
224 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
225 {
226   return bfd_get_8 (abfd, buf);
227 }
228
229 static int
230 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
231 {
232   return bfd_get_signed_8 (abfd, buf);
233 }
234
235 static unsigned int
236 read_2_bytes (bfd *abfd, bfd_byte *buf)
237 {
238   return bfd_get_16 (abfd, buf);
239 }
240
241 static unsigned int
242 read_4_bytes (bfd *abfd, bfd_byte *buf)
243 {
244   return bfd_get_32 (abfd, buf);
245 }
246
247 static bfd_uint64_t
248 read_8_bytes (bfd *abfd, bfd_byte *buf)
249 {
250   return bfd_get_64 (abfd, buf);
251 }
252
253 static bfd_byte *
254 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
255               bfd_byte *buf,
256               unsigned int size ATTRIBUTE_UNUSED)
257 {
258   /* If the size of a host char is 8 bits, we can return a pointer
259      to the buffer, otherwise we have to copy the data to a buffer
260      allocated on the temporary obstack.  */
261   return buf;
262 }
263
264 static char *
265 read_string (bfd *abfd ATTRIBUTE_UNUSED,
266              bfd_byte *buf,
267              unsigned int *bytes_read_ptr)
268 {
269   /* Return a pointer to the embedded string.  */
270   char *str = (char *) buf;
271   if (*str == '\0')
272     {
273       *bytes_read_ptr = 1;
274       return NULL;
275     }
276
277   *bytes_read_ptr = strlen (str) + 1;
278   return str;
279 }
280
281 static char *
282 read_indirect_string (struct comp_unit* unit,
283                       bfd_byte *buf,
284                       unsigned int *bytes_read_ptr)
285 {
286   bfd_uint64_t offset;
287   struct dwarf2_debug *stash = unit->stash;
288   char *str;
289
290   if (unit->offset_size == 4)
291     offset = read_4_bytes (unit->abfd, buf);
292   else
293     offset = read_8_bytes (unit->abfd, buf);
294   *bytes_read_ptr = unit->offset_size;
295
296   if (! stash->dwarf_str_buffer)
297     {
298       asection *msec;
299       bfd *abfd = unit->abfd;
300       bfd_size_type sz;
301
302       msec = bfd_get_section_by_name (abfd, ".debug_str");
303       if (! msec)
304         {
305           (*_bfd_error_handler)
306             (_("Dwarf Error: Can't find .debug_str section."));
307           bfd_set_error (bfd_error_bad_value);
308           return NULL;
309         }
310
311       sz = msec->rawsize ? msec->rawsize : msec->size;
312       stash->dwarf_str_size = sz;
313       stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
314       if (! stash->dwarf_str_buffer)
315         return NULL;
316
317       if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
318                                       0, sz))
319         return NULL;
320     }
321
322   if (offset >= stash->dwarf_str_size)
323     {
324       (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
325                              (unsigned long) offset, stash->dwarf_str_size);
326       bfd_set_error (bfd_error_bad_value);
327       return NULL;
328     }
329
330   str = (char *) stash->dwarf_str_buffer + offset;
331   if (*str == '\0')
332     return NULL;
333   return str;
334 }
335
336 /* END VERBATIM */
337
338 static bfd_uint64_t
339 read_address (struct comp_unit *unit, bfd_byte *buf)
340 {
341   switch (unit->addr_size)
342     {
343     case 8:
344       return bfd_get_64 (unit->abfd, buf);
345     case 4:
346       return bfd_get_32 (unit->abfd, buf);
347     case 2:
348       return bfd_get_16 (unit->abfd, buf);
349     default:
350       abort ();
351     }
352 }
353
354 /* Lookup an abbrev_info structure in the abbrev hash table.  */
355
356 static struct abbrev_info *
357 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
358 {
359   unsigned int hash_number;
360   struct abbrev_info *abbrev;
361
362   hash_number = number % ABBREV_HASH_SIZE;
363   abbrev = abbrevs[hash_number];
364
365   while (abbrev)
366     {
367       if (abbrev->number == number)
368         return abbrev;
369       else
370         abbrev = abbrev->next;
371     }
372
373   return NULL;
374 }
375
376 /* In DWARF version 2, the description of the debugging information is
377    stored in a separate .debug_abbrev section.  Before we read any
378    dies from a section we read in all abbreviations and install them
379    in a hash table.  */
380
381 static struct abbrev_info**
382 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
383 {
384   struct abbrev_info **abbrevs;
385   bfd_byte *abbrev_ptr;
386   struct abbrev_info *cur_abbrev;
387   unsigned int abbrev_number, bytes_read, abbrev_name;
388   unsigned int abbrev_form, hash_number;
389   bfd_size_type amt;
390
391   if (! stash->dwarf_abbrev_buffer)
392     {
393       asection *msec;
394
395       msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
396       if (! msec)
397         {
398           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
399           bfd_set_error (bfd_error_bad_value);
400           return 0;
401         }
402
403       stash->dwarf_abbrev_size = msec->size;
404       stash->dwarf_abbrev_buffer
405         = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
406                                                      stash->syms);
407       if (! stash->dwarf_abbrev_buffer)
408           return 0;
409     }
410
411   if (offset >= stash->dwarf_abbrev_size)
412     {
413       (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
414                              (unsigned long) offset, stash->dwarf_abbrev_size);
415       bfd_set_error (bfd_error_bad_value);
416       return 0;
417     }
418
419   amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
420   abbrevs = bfd_zalloc (abfd, amt);
421
422   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
423   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
424   abbrev_ptr += bytes_read;
425
426   /* Loop until we reach an abbrev number of 0.  */
427   while (abbrev_number)
428     {
429       amt = sizeof (struct abbrev_info);
430       cur_abbrev = bfd_zalloc (abfd, amt);
431
432       /* Read in abbrev header.  */
433       cur_abbrev->number = abbrev_number;
434       cur_abbrev->tag = (enum dwarf_tag)
435         read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
436       abbrev_ptr += bytes_read;
437       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
438       abbrev_ptr += 1;
439
440       /* Now read in declarations.  */
441       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
442       abbrev_ptr += bytes_read;
443       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
444       abbrev_ptr += bytes_read;
445
446       while (abbrev_name)
447         {
448           if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
449             {
450               amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
451               amt *= sizeof (struct attr_abbrev);
452               cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
453               if (! cur_abbrev->attrs)
454                 return 0;
455             }
456
457           cur_abbrev->attrs[cur_abbrev->num_attrs].name
458             = (enum dwarf_attribute) abbrev_name;
459           cur_abbrev->attrs[cur_abbrev->num_attrs++].form
460             = (enum dwarf_form) abbrev_form;
461           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
462           abbrev_ptr += bytes_read;
463           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
464           abbrev_ptr += bytes_read;
465         }
466
467       hash_number = abbrev_number % ABBREV_HASH_SIZE;
468       cur_abbrev->next = abbrevs[hash_number];
469       abbrevs[hash_number] = cur_abbrev;
470
471       /* Get next abbreviation.
472          Under Irix6 the abbreviations for a compilation unit are not
473          always properly terminated with an abbrev number of 0.
474          Exit loop if we encounter an abbreviation which we have
475          already read (which means we are about to read the abbreviations
476          for the next compile unit) or if the end of the abbreviation
477          table is reached.  */
478       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
479             >= stash->dwarf_abbrev_size)
480         break;
481       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
482       abbrev_ptr += bytes_read;
483       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
484         break;
485     }
486
487   return abbrevs;
488 }
489
490 /* Read an attribute value described by an attribute form.  */
491
492 static bfd_byte *
493 read_attribute_value (struct attribute *attr,
494                       unsigned form,
495                       struct comp_unit *unit,
496                       bfd_byte *info_ptr)
497 {
498   bfd *abfd = unit->abfd;
499   unsigned int bytes_read;
500   struct dwarf_block *blk;
501   bfd_size_type amt;
502
503   attr->form = (enum dwarf_form) form;
504
505   switch (form)
506     {
507     case DW_FORM_addr:
508       /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size.  */
509     case DW_FORM_ref_addr:
510       attr->u.val = read_address (unit, info_ptr);
511       info_ptr += unit->addr_size;
512       break;
513     case DW_FORM_block2:
514       amt = sizeof (struct dwarf_block);
515       blk = bfd_alloc (abfd, amt);
516       blk->size = read_2_bytes (abfd, info_ptr);
517       info_ptr += 2;
518       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
519       info_ptr += blk->size;
520       attr->u.blk = blk;
521       break;
522     case DW_FORM_block4:
523       amt = sizeof (struct dwarf_block);
524       blk = bfd_alloc (abfd, amt);
525       blk->size = read_4_bytes (abfd, info_ptr);
526       info_ptr += 4;
527       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
528       info_ptr += blk->size;
529       attr->u.blk = blk;
530       break;
531     case DW_FORM_data2:
532       attr->u.val = read_2_bytes (abfd, info_ptr);
533       info_ptr += 2;
534       break;
535     case DW_FORM_data4:
536       attr->u.val = read_4_bytes (abfd, info_ptr);
537       info_ptr += 4;
538       break;
539     case DW_FORM_data8:
540       attr->u.val = read_8_bytes (abfd, info_ptr);
541       info_ptr += 8;
542       break;
543     case DW_FORM_string:
544       attr->u.str = read_string (abfd, info_ptr, &bytes_read);
545       info_ptr += bytes_read;
546       break;
547     case DW_FORM_strp:
548       attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
549       info_ptr += bytes_read;
550       break;
551     case DW_FORM_block:
552       amt = sizeof (struct dwarf_block);
553       blk = bfd_alloc (abfd, amt);
554       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
555       info_ptr += bytes_read;
556       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
557       info_ptr += blk->size;
558       attr->u.blk = blk;
559       break;
560     case DW_FORM_block1:
561       amt = sizeof (struct dwarf_block);
562       blk = bfd_alloc (abfd, amt);
563       blk->size = read_1_byte (abfd, info_ptr);
564       info_ptr += 1;
565       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
566       info_ptr += blk->size;
567       attr->u.blk = blk;
568       break;
569     case DW_FORM_data1:
570       attr->u.val = read_1_byte (abfd, info_ptr);
571       info_ptr += 1;
572       break;
573     case DW_FORM_flag:
574       attr->u.val = read_1_byte (abfd, info_ptr);
575       info_ptr += 1;
576       break;
577     case DW_FORM_sdata:
578       attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
579       info_ptr += bytes_read;
580       break;
581     case DW_FORM_udata:
582       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
583       info_ptr += bytes_read;
584       break;
585     case DW_FORM_ref1:
586       attr->u.val = read_1_byte (abfd, info_ptr);
587       info_ptr += 1;
588       break;
589     case DW_FORM_ref2:
590       attr->u.val = read_2_bytes (abfd, info_ptr);
591       info_ptr += 2;
592       break;
593     case DW_FORM_ref4:
594       attr->u.val = read_4_bytes (abfd, info_ptr);
595       info_ptr += 4;
596       break;
597     case DW_FORM_ref8:
598       attr->u.val = read_8_bytes (abfd, info_ptr);
599       info_ptr += 8;
600       break;
601     case DW_FORM_ref_udata:
602       attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
603       info_ptr += bytes_read;
604       break;
605     case DW_FORM_indirect:
606       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
607       info_ptr += bytes_read;
608       info_ptr = read_attribute_value (attr, form, unit, info_ptr);
609       break;
610     default:
611       (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
612                              form);
613       bfd_set_error (bfd_error_bad_value);
614     }
615   return info_ptr;
616 }
617
618 /* Read an attribute described by an abbreviated attribute.  */
619
620 static bfd_byte *
621 read_attribute (struct attribute *attr,
622                 struct attr_abbrev *abbrev,
623                 struct comp_unit *unit,
624                 bfd_byte *info_ptr)
625 {
626   attr->name = abbrev->name;
627   info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
628   return info_ptr;
629 }
630
631 /* Source line information table routines.  */
632
633 #define FILE_ALLOC_CHUNK 5
634 #define DIR_ALLOC_CHUNK 5
635
636 struct line_info
637 {
638   struct line_info* prev_line;
639   bfd_vma address;
640   char *filename;
641   unsigned int line;
642   unsigned int column;
643   int end_sequence;             /* End of (sequential) code sequence.  */
644 };
645
646 struct fileinfo
647 {
648   char *name;
649   unsigned int dir;
650   unsigned int time;
651   unsigned int size;
652 };
653
654 struct line_info_table
655 {
656   bfd* abfd;
657   unsigned int num_files;
658   unsigned int num_dirs;
659   char *comp_dir;
660   char **dirs;
661   struct fileinfo* files;
662   struct line_info* last_line;  /* largest VMA */
663   struct line_info* lcl_head;   /* local head; used in 'add_line_info' */
664 };
665
666 struct funcinfo
667 {
668   struct funcinfo *prev_func;
669   char *name;
670   struct arange arange;
671 };
672
673 /* Adds a new entry to the line_info list in the line_info_table, ensuring
674    that the list is sorted.  Note that the line_info list is sorted from
675    highest to lowest VMA (with possible duplicates); that is,
676    line_info->prev_line always accesses an equal or smaller VMA.  */
677
678 static void
679 add_line_info (struct line_info_table *table,
680                bfd_vma address,
681                char *filename,
682                unsigned int line,
683                unsigned int column,
684                int end_sequence)
685 {
686   bfd_size_type amt = sizeof (struct line_info);
687   struct line_info* info = bfd_alloc (table->abfd, amt);
688
689   /* Find the correct location for 'info'.  Normally we will receive
690      new line_info data 1) in order and 2) with increasing VMAs.
691      However some compilers break the rules (cf. decode_line_info) and
692      so we include some heuristics for quickly finding the correct
693      location for 'info'. In particular, these heuristics optimize for
694      the common case in which the VMA sequence that we receive is a
695      list of locally sorted VMAs such as
696        p...z a...j  (where a < j < p < z)
697
698      Note: table->lcl_head is used to head an *actual* or *possible*
699      sequence within the list (such as a...j) that is not directly
700      headed by table->last_line
701
702      Note: we may receive duplicate entries from 'decode_line_info'.  */
703
704   while (1)
705     if (!table->last_line
706         || address >= table->last_line->address)
707       {
708         /* Normal case: add 'info' to the beginning of the list */
709         info->prev_line = table->last_line;
710         table->last_line = info;
711
712         /* lcl_head: initialize to head a *possible* sequence at the end.  */
713         if (!table->lcl_head)
714           table->lcl_head = info;
715         break;
716       }
717     else if (!table->lcl_head->prev_line
718              && table->lcl_head->address > address)
719       {
720         /* Abnormal but easy: lcl_head is 1) at the *end* of the line
721            list and 2) the head of 'info'.  */
722         info->prev_line = NULL;
723         table->lcl_head->prev_line = info;
724         break;
725       }
726     else if (table->lcl_head->prev_line
727              && table->lcl_head->address > address
728              && address >= table->lcl_head->prev_line->address)
729       {
730         /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
731            list and 2) the head of 'info'.  */
732         info->prev_line = table->lcl_head->prev_line;
733         table->lcl_head->prev_line = info;
734         break;
735       }
736     else
737       {
738         /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
739            heads for 'info'.  Reset 'lcl_head' and repeat.  */
740         struct line_info* li2 = table->last_line; /* always non-NULL */
741         struct line_info* li1 = li2->prev_line;
742
743         while (li1)
744           {
745             if (li2->address > address && address >= li1->address)
746               break;
747
748             li2 = li1; /* always non-NULL */
749             li1 = li1->prev_line;
750           }
751         table->lcl_head = li2;
752       }
753
754   /* Set member data of 'info'.  */
755   info->address = address;
756   info->line = line;
757   info->column = column;
758   info->end_sequence = end_sequence;
759
760   if (filename && filename[0])
761     {
762       info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
763       if (info->filename)
764         strcpy (info->filename, filename);
765     }
766   else
767     info->filename = NULL;
768 }
769
770 /* Extract a fully qualified filename from a line info table.
771    The returned string has been malloc'ed and it is the caller's
772    responsibility to free it.  */
773
774 static char *
775 concat_filename (struct line_info_table *table, unsigned int file)
776 {
777   char *filename;
778
779   if (file - 1 >= table->num_files)
780     {
781       (*_bfd_error_handler)
782         (_("Dwarf Error: mangled line number section (bad file number)."));
783       return strdup ("<unknown>");
784     }
785
786   filename = table->files[file - 1].name;
787
788   if (! IS_ABSOLUTE_PATH (filename))
789     {
790       char *dirname = (table->files[file - 1].dir
791                        ? table->dirs[table->files[file - 1].dir - 1]
792                        : table->comp_dir);
793
794       /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
795          The best we can do is return the filename part.  */
796       if (dirname != NULL)
797         {
798           unsigned int len = strlen (dirname) + strlen (filename) + 2;
799           char * name;
800
801           name = bfd_malloc (len);
802           if (name)
803             sprintf (name, "%s/%s", dirname, filename);
804           return name;
805         }
806     }
807
808   return strdup (filename);
809 }
810
811 static void
812 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
813 {
814   struct arange *arange;
815
816   /* If the first arange is empty, use it. */
817   if (first_arange->high == 0)
818     {
819       first_arange->low = low_pc;
820       first_arange->high = high_pc;
821       return;
822     }
823
824   /* Next see if we can cheaply extend an existing range.  */
825   arange = first_arange;
826   do
827     {
828       if (low_pc == arange->high)
829         {
830           arange->high = high_pc;
831           return;
832         }
833       if (high_pc == arange->low)
834         {
835           arange->low = low_pc;
836           return;
837         }
838       arange = arange->next;
839     }
840   while (arange);
841
842   /* Need to allocate a new arange and insert it into the arange list.
843      Order isn't significant, so just insert after the first arange. */
844   arange = bfd_zalloc (abfd, sizeof (*arange));
845   arange->low = low_pc;
846   arange->high = high_pc;
847   arange->next = first_arange->next;
848   first_arange->next = arange;
849 }
850
851 /* Decode the line number information for UNIT.  */
852
853 static struct line_info_table*
854 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
855 {
856   bfd *abfd = unit->abfd;
857   struct line_info_table* table;
858   bfd_byte *line_ptr;
859   bfd_byte *line_end;
860   struct line_head lh;
861   unsigned int i, bytes_read, offset_size;
862   char *cur_file, *cur_dir;
863   unsigned char op_code, extended_op, adj_opcode;
864   bfd_size_type amt;
865
866   if (! stash->dwarf_line_buffer)
867     {
868       asection *msec;
869
870       msec = bfd_get_section_by_name (abfd, ".debug_line");
871       if (! msec)
872         {
873           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
874           bfd_set_error (bfd_error_bad_value);
875           return 0;
876         }
877
878       stash->dwarf_line_size = msec->size;
879       stash->dwarf_line_buffer
880         = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
881                                                      stash->syms);
882       if (! stash->dwarf_line_buffer)
883         return 0;
884     }
885
886   /* It is possible to get a bad value for the line_offset.  Validate
887      it here so that we won't get a segfault below.  */
888   if (unit->line_offset >= stash->dwarf_line_size)
889     {
890       (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
891                              unit->line_offset, stash->dwarf_line_size);
892       bfd_set_error (bfd_error_bad_value);
893       return 0;
894     }
895
896   amt = sizeof (struct line_info_table);
897   table = bfd_alloc (abfd, amt);
898   table->abfd = abfd;
899   table->comp_dir = unit->comp_dir;
900
901   table->num_files = 0;
902   table->files = NULL;
903
904   table->num_dirs = 0;
905   table->dirs = NULL;
906
907   table->files = NULL;
908   table->last_line = NULL;
909   table->lcl_head = NULL;
910
911   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
912
913   /* Read in the prologue.  */
914   lh.total_length = read_4_bytes (abfd, line_ptr);
915   line_ptr += 4;
916   offset_size = 4;
917   if (lh.total_length == 0xffffffff)
918     {
919       lh.total_length = read_8_bytes (abfd, line_ptr);
920       line_ptr += 8;
921       offset_size = 8;
922     }
923   else if (lh.total_length == 0 && unit->addr_size == 8)
924     {
925       /* Handle (non-standard) 64-bit DWARF2 formats.  */
926       lh.total_length = read_4_bytes (abfd, line_ptr);
927       line_ptr += 4;
928       offset_size = 8;
929     }
930   line_end = line_ptr + lh.total_length;
931   lh.version = read_2_bytes (abfd, line_ptr);
932   line_ptr += 2;
933   if (offset_size == 4)
934     lh.prologue_length = read_4_bytes (abfd, line_ptr);
935   else
936     lh.prologue_length = read_8_bytes (abfd, line_ptr);
937   line_ptr += offset_size;
938   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
939   line_ptr += 1;
940   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
941   line_ptr += 1;
942   lh.line_base = read_1_signed_byte (abfd, line_ptr);
943   line_ptr += 1;
944   lh.line_range = read_1_byte (abfd, line_ptr);
945   line_ptr += 1;
946   lh.opcode_base = read_1_byte (abfd, line_ptr);
947   line_ptr += 1;
948   amt = lh.opcode_base * sizeof (unsigned char);
949   lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
950
951   lh.standard_opcode_lengths[0] = 1;
952
953   for (i = 1; i < lh.opcode_base; ++i)
954     {
955       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
956       line_ptr += 1;
957     }
958
959   /* Read directory table.  */
960   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
961     {
962       line_ptr += bytes_read;
963
964       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
965         {
966           amt = table->num_dirs + DIR_ALLOC_CHUNK;
967           amt *= sizeof (char *);
968           table->dirs = bfd_realloc (table->dirs, amt);
969           if (! table->dirs)
970             return 0;
971         }
972
973       table->dirs[table->num_dirs++] = cur_dir;
974     }
975
976   line_ptr += bytes_read;
977
978   /* Read file name table.  */
979   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
980     {
981       line_ptr += bytes_read;
982
983       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
984         {
985           amt = table->num_files + FILE_ALLOC_CHUNK;
986           amt *= sizeof (struct fileinfo);
987           table->files = bfd_realloc (table->files, amt);
988           if (! table->files)
989             return 0;
990         }
991
992       table->files[table->num_files].name = cur_file;
993       table->files[table->num_files].dir =
994         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
995       line_ptr += bytes_read;
996       table->files[table->num_files].time =
997         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
998       line_ptr += bytes_read;
999       table->files[table->num_files].size =
1000         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1001       line_ptr += bytes_read;
1002       table->num_files++;
1003     }
1004
1005   line_ptr += bytes_read;
1006
1007   /* Read the statement sequences until there's nothing left.  */
1008   while (line_ptr < line_end)
1009     {
1010       /* State machine registers.  */
1011       bfd_vma address = 0;
1012       char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1013       unsigned int line = 1;
1014       unsigned int column = 0;
1015       int is_stmt = lh.default_is_stmt;
1016       int basic_block = 0;
1017       int end_sequence = 0;
1018       /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1019          compilers generate address sequences that are wildly out of
1020          order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1021          for ia64-Linux).  Thus, to determine the low and high
1022          address, we must compare on every DW_LNS_copy, etc.  */
1023       bfd_vma low_pc  = 0;
1024       bfd_vma high_pc = 0;
1025
1026       /* Decode the table.  */
1027       while (! end_sequence)
1028         {
1029           op_code = read_1_byte (abfd, line_ptr);
1030           line_ptr += 1;
1031
1032           if (op_code >= lh.opcode_base)
1033             {
1034               /* Special operand.  */
1035               adj_opcode = op_code - lh.opcode_base;
1036               address += (adj_opcode / lh.line_range)
1037                 * lh.minimum_instruction_length;
1038               line += lh.line_base + (adj_opcode % lh.line_range);
1039               /* Append row to matrix using current values.  */
1040               add_line_info (table, address, filename, line, column, 0);
1041               basic_block = 1;
1042               if (low_pc == 0 || address < low_pc)
1043                 low_pc = address;
1044               if (address > high_pc)
1045                 high_pc = address;
1046             }
1047           else switch (op_code)
1048             {
1049             case DW_LNS_extended_op:
1050               /* Ignore length.  */
1051               line_ptr += 1;
1052               extended_op = read_1_byte (abfd, line_ptr);
1053               line_ptr += 1;
1054
1055               switch (extended_op)
1056                 {
1057                 case DW_LNE_end_sequence:
1058                   end_sequence = 1;
1059                   add_line_info (table, address, filename, line, column,
1060                                  end_sequence);
1061                   if (low_pc == 0 || address < low_pc)
1062                     low_pc = address;
1063                   if (address > high_pc)
1064                     high_pc = address;
1065                   arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1066                   break;
1067                 case DW_LNE_set_address:
1068                   address = read_address (unit, line_ptr);
1069                   line_ptr += unit->addr_size;
1070                   break;
1071                 case DW_LNE_define_file:
1072                   cur_file = read_string (abfd, line_ptr, &bytes_read);
1073                   line_ptr += bytes_read;
1074                   if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1075                     {
1076                       amt = table->num_files + FILE_ALLOC_CHUNK;
1077                       amt *= sizeof (struct fileinfo);
1078                       table->files = bfd_realloc (table->files, amt);
1079                       if (! table->files)
1080                         return 0;
1081                     }
1082                   table->files[table->num_files].name = cur_file;
1083                   table->files[table->num_files].dir =
1084                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1085                   line_ptr += bytes_read;
1086                   table->files[table->num_files].time =
1087                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1088                   line_ptr += bytes_read;
1089                   table->files[table->num_files].size =
1090                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1091                   line_ptr += bytes_read;
1092                   table->num_files++;
1093                   break;
1094                 default:
1095                   (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1096                   bfd_set_error (bfd_error_bad_value);
1097                   return 0;
1098                 }
1099               break;
1100             case DW_LNS_copy:
1101               add_line_info (table, address, filename, line, column, 0);
1102               basic_block = 0;
1103               if (low_pc == 0 || address < low_pc)
1104                 low_pc = address;
1105               if (address > high_pc)
1106                 high_pc = address;
1107               break;
1108             case DW_LNS_advance_pc:
1109               address += lh.minimum_instruction_length
1110                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1111               line_ptr += bytes_read;
1112               break;
1113             case DW_LNS_advance_line:
1114               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1115               line_ptr += bytes_read;
1116               break;
1117             case DW_LNS_set_file:
1118               {
1119                 unsigned int file;
1120
1121                 /* The file and directory tables are 0
1122                    based, the references are 1 based.  */
1123                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1124                 line_ptr += bytes_read;
1125                 if (filename)
1126                   free (filename);
1127                 filename = concat_filename (table, file);
1128                 break;
1129               }
1130             case DW_LNS_set_column:
1131               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1132               line_ptr += bytes_read;
1133               break;
1134             case DW_LNS_negate_stmt:
1135               is_stmt = (!is_stmt);
1136               break;
1137             case DW_LNS_set_basic_block:
1138               basic_block = 1;
1139               break;
1140             case DW_LNS_const_add_pc:
1141               address += lh.minimum_instruction_length
1142                       * ((255 - lh.opcode_base) / lh.line_range);
1143               break;
1144             case DW_LNS_fixed_advance_pc:
1145               address += read_2_bytes (abfd, line_ptr);
1146               line_ptr += 2;
1147               break;
1148             default:
1149               {
1150                 int i;
1151
1152                 /* Unknown standard opcode, ignore it.  */
1153                 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1154                   {
1155                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1156                     line_ptr += bytes_read;
1157                   }
1158               }
1159             }
1160         }
1161
1162       if (filename)
1163         free (filename);
1164     }
1165
1166   return table;
1167 }
1168
1169 /* If ADDR is within TABLE set the output parameters and return TRUE,
1170    otherwise return FALSE.  The output parameters, FILENAME_PTR and
1171    LINENUMBER_PTR, are pointers to the objects to be filled in.  */
1172
1173 static bfd_boolean
1174 lookup_address_in_line_info_table (struct line_info_table *table,
1175                                    bfd_vma addr,
1176                                    struct funcinfo *function,
1177                                    const char **filename_ptr,
1178                                    unsigned int *linenumber_ptr)
1179 {
1180   /* Note: table->last_line should be a descendingly sorted list. */
1181   struct line_info* next_line = table->last_line;
1182   struct line_info* each_line = NULL;
1183   *filename_ptr = NULL;
1184
1185   if (!next_line)
1186     return FALSE;
1187
1188   each_line = next_line->prev_line;
1189
1190   /* Check for large addresses */
1191   if (addr > next_line->address)
1192     each_line = NULL; /* ensure we skip over the normal case */
1193
1194   /* Normal case: search the list; save  */
1195   while (each_line && next_line)
1196     {
1197       /* If we have an address match, save this info.  This allows us
1198          to return as good as results as possible for strange debugging
1199          info.  */
1200       bfd_boolean addr_match = FALSE;
1201       if (each_line->address <= addr && addr < next_line->address)
1202         {
1203           addr_match = TRUE;
1204
1205           /* If this line appears to span functions, and addr is in the
1206              later function, return the first line of that function instead
1207              of the last line of the earlier one.  This check is for GCC
1208              2.95, which emits the first line number for a function late.  */
1209
1210           if (function != NULL)
1211             {
1212               bfd_vma lowest_pc;
1213               struct arange *arange;
1214
1215               /* Find the lowest address in the function's range list */
1216               lowest_pc = function->arange.low;
1217               for (arange = &function->arange;
1218                    arange;
1219                    arange = arange->next)
1220                 {
1221                   if (function->arange.low < lowest_pc)
1222                     lowest_pc = function->arange.low;
1223                 }
1224               /* Check for spanning function and set outgoing line info */
1225               if (addr >= lowest_pc
1226                   && each_line->address < lowest_pc
1227                   && next_line->address > lowest_pc)
1228                 {
1229                   *filename_ptr = next_line->filename;
1230                   *linenumber_ptr = next_line->line;
1231                 }
1232               else
1233                 {
1234                   *filename_ptr = each_line->filename;
1235                   *linenumber_ptr = each_line->line;
1236                 }
1237             }
1238         }
1239
1240       if (addr_match && !each_line->end_sequence)
1241         return TRUE; /* we have definitely found what we want */
1242
1243       next_line = each_line;
1244       each_line = each_line->prev_line;
1245     }
1246
1247   /* At this point each_line is NULL but next_line is not.  If we found
1248      a candidate end-of-sequence point in the loop above, we can return
1249      that (compatibility with a bug in the Intel compiler); otherwise,
1250      assuming that we found the containing function for this address in
1251      this compilation unit, return the first line we have a number for
1252      (compatibility with GCC 2.95).  */
1253   if (*filename_ptr == NULL && function != NULL)
1254     {
1255       *filename_ptr = next_line->filename;
1256       *linenumber_ptr = next_line->line;
1257       return TRUE;
1258     }
1259
1260   return FALSE;
1261 }
1262
1263 /* Read in the .debug_ranges section for future reference */
1264
1265 static bfd_boolean
1266 read_debug_ranges (struct comp_unit *unit)
1267 {
1268   struct dwarf2_debug *stash = unit->stash;
1269   if (! stash->dwarf_ranges_buffer)
1270     {
1271       bfd *abfd = unit->abfd;
1272       asection *msec;
1273
1274       msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1275       if (! msec)
1276         {
1277           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1278           bfd_set_error (bfd_error_bad_value);
1279           return FALSE;
1280         }
1281
1282       stash->dwarf_ranges_size = msec->size;
1283       stash->dwarf_ranges_buffer
1284         = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1285                                                      stash->syms);
1286       if (! stash->dwarf_ranges_buffer)
1287         return FALSE;
1288     }
1289   return TRUE;
1290 }
1291
1292 /* Function table functions.  */
1293
1294 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1295    Note that we need to find the function that has the smallest
1296    range that contains ADDR, to handle inlined functions without
1297    depending upon them being ordered in TABLE by increasing range. */
1298
1299 static bfd_boolean
1300 lookup_address_in_function_table (struct funcinfo *table,
1301                                   bfd_vma addr,
1302                                   struct funcinfo **function_ptr,
1303                                   const char **functionname_ptr)
1304 {
1305   struct funcinfo* each_func;
1306   struct funcinfo* best_fit = NULL;
1307   struct arange *arange;
1308
1309   for (each_func = table;
1310        each_func;
1311        each_func = each_func->prev_func)
1312     {
1313       for (arange = &each_func->arange;
1314            arange;
1315            arange = arange->next)
1316         {
1317           if (addr >= arange->low && addr < arange->high)
1318             {
1319               if (!best_fit ||
1320                   ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1321                 best_fit = each_func;
1322             }
1323         }
1324     }
1325
1326   if (best_fit)
1327     {
1328       *functionname_ptr = best_fit->name;
1329       *function_ptr = best_fit;
1330       return TRUE;
1331     }
1332   else
1333     {
1334       return FALSE;
1335     }
1336 }
1337
1338 static char *
1339 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1340 {
1341   bfd *abfd = unit->abfd;
1342   bfd_byte *info_ptr;
1343   unsigned int abbrev_number, bytes_read, i;
1344   struct abbrev_info *abbrev;
1345   struct attribute attr;
1346   char *name = 0;
1347
1348   info_ptr = unit->info_ptr_unit + die_ref;
1349   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1350   info_ptr += bytes_read;
1351
1352   if (abbrev_number)
1353     {
1354       abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1355       if (! abbrev)
1356         {
1357           (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1358                                  abbrev_number);
1359           bfd_set_error (bfd_error_bad_value);
1360         }
1361       else
1362         {
1363           for (i = 0; i < abbrev->num_attrs && !name; ++i)
1364             {
1365               info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1366               switch (attr.name)
1367                 {
1368                 case DW_AT_name:
1369                   name = attr.u.str;
1370                   break;
1371                 case DW_AT_specification:
1372                   name = find_abstract_instance_name (unit, attr.u.val);
1373                   break;
1374                 default:
1375                   break;
1376                 }
1377             }
1378         }
1379     }
1380   return (name);
1381 }
1382
1383 static void
1384 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1385 {
1386   bfd_byte *ranges_ptr;
1387   bfd_vma base_address = unit->base_address;
1388
1389   if (! unit->stash->dwarf_ranges_buffer)
1390     {
1391       if (! read_debug_ranges (unit))
1392         return;
1393     }
1394   ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1395     
1396   for (;;)
1397     {
1398       bfd_vma low_pc;
1399       bfd_vma high_pc;
1400
1401       if (unit->offset_size == 4)
1402         {
1403           low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1404           ranges_ptr += 4;
1405           high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1406           ranges_ptr += 4;
1407         }
1408       else
1409         {
1410           low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1411           ranges_ptr += 8;
1412           high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1413           ranges_ptr += 8;
1414         }
1415       if (low_pc == 0 && high_pc == 0)
1416         break;
1417       if (low_pc == -1UL && high_pc != -1UL)
1418         base_address = high_pc;
1419       else
1420           arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1421     }
1422 }
1423
1424 /* DWARF2 Compilation unit functions.  */
1425
1426 /* Scan over each die in a comp. unit looking for functions to add
1427    to the function table.  */
1428
1429 static bfd_boolean
1430 scan_unit_for_functions (struct comp_unit *unit)
1431 {
1432   bfd *abfd = unit->abfd;
1433   bfd_byte *info_ptr = unit->first_child_die_ptr;
1434   int nesting_level = 1;
1435
1436   while (nesting_level)
1437     {
1438       unsigned int abbrev_number, bytes_read, i;
1439       struct abbrev_info *abbrev;
1440       struct attribute attr;
1441       struct funcinfo *func;
1442       bfd_vma low_pc = 0;
1443       bfd_vma high_pc = 0;
1444
1445       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1446       info_ptr += bytes_read;
1447
1448       if (! abbrev_number)
1449         {
1450           nesting_level--;
1451           continue;
1452         }
1453
1454       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1455       if (! abbrev)
1456         {
1457           (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1458                              abbrev_number);
1459           bfd_set_error (bfd_error_bad_value);
1460           return FALSE;
1461         }
1462
1463       if (abbrev->tag == DW_TAG_subprogram
1464           || abbrev->tag == DW_TAG_inlined_subroutine)
1465         {
1466           bfd_size_type amt = sizeof (struct funcinfo);
1467           func = bfd_zalloc (abfd, amt);
1468           func->prev_func = unit->function_table;
1469           unit->function_table = func;
1470         }
1471       else
1472         func = NULL;
1473
1474       for (i = 0; i < abbrev->num_attrs; ++i)
1475         {
1476           info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1477
1478           if (func)
1479             {
1480               switch (attr.name)
1481                 {
1482                 case DW_AT_abstract_origin:
1483                   func->name = find_abstract_instance_name (unit, attr.u.val);
1484                   break;
1485
1486                 case DW_AT_name:
1487                   /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1488                   if (func->name == NULL)
1489                     func->name = attr.u.str;
1490                   break;
1491
1492                 case DW_AT_MIPS_linkage_name:
1493                   func->name = attr.u.str;
1494                   break;
1495
1496                 case DW_AT_low_pc:
1497                   low_pc = attr.u.val;
1498                   break;
1499
1500                 case DW_AT_high_pc:
1501                   high_pc = attr.u.val;
1502                   break;
1503
1504                 case DW_AT_ranges:
1505                   read_rangelist (unit, &func->arange, attr.u.val);
1506                   break;
1507
1508                 default:
1509                   break;
1510                 }
1511             }
1512         }
1513
1514       if (func && high_pc != 0)
1515         {
1516           arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1517         }
1518
1519       if (abbrev->has_children)
1520         nesting_level++;
1521     }
1522
1523   return TRUE;
1524 }
1525
1526 /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
1527    includes the compilation unit header that proceeds the DIE's, but
1528    does not include the length field that precedes each compilation
1529    unit header.  END_PTR points one past the end of this comp unit.
1530    OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1531
1532    This routine does not read the whole compilation unit; only enough
1533    to get to the line number information for the compilation unit.  */
1534
1535 static struct comp_unit *
1536 parse_comp_unit (bfd *abfd,
1537                  struct dwarf2_debug *stash,
1538                  bfd_vma unit_length,
1539                  bfd_byte *info_ptr_unit,
1540                  unsigned int offset_size)
1541 {
1542   struct comp_unit* unit;
1543   unsigned int version;
1544   bfd_uint64_t abbrev_offset = 0;
1545   unsigned int addr_size;
1546   struct abbrev_info** abbrevs;
1547   unsigned int abbrev_number, bytes_read, i;
1548   struct abbrev_info *abbrev;
1549   struct attribute attr;
1550   bfd_byte *info_ptr = stash->info_ptr;
1551   bfd_byte *end_ptr = info_ptr + unit_length;
1552   bfd_size_type amt;
1553   bfd_vma low_pc = 0;
1554   bfd_vma high_pc = 0;
1555
1556   version = read_2_bytes (abfd, info_ptr);
1557   info_ptr += 2;
1558   BFD_ASSERT (offset_size == 4 || offset_size == 8);
1559   if (offset_size == 4)
1560     abbrev_offset = read_4_bytes (abfd, info_ptr);
1561   else
1562     abbrev_offset = read_8_bytes (abfd, info_ptr);
1563   info_ptr += offset_size;
1564   addr_size = read_1_byte (abfd, info_ptr);
1565   info_ptr += 1;
1566
1567   if (version != 2)
1568     {
1569       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1570       bfd_set_error (bfd_error_bad_value);
1571       return 0;
1572     }
1573
1574   if (addr_size > sizeof (bfd_vma))
1575     {
1576       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1577                          addr_size,
1578                          (unsigned int) sizeof (bfd_vma));
1579       bfd_set_error (bfd_error_bad_value);
1580       return 0;
1581     }
1582
1583   if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1584     {
1585       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1586       bfd_set_error (bfd_error_bad_value);
1587       return 0;
1588     }
1589
1590   /* Read the abbrevs for this compilation unit into a table.  */
1591   abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1592   if (! abbrevs)
1593       return 0;
1594
1595   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1596   info_ptr += bytes_read;
1597   if (! abbrev_number)
1598     {
1599       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1600                          abbrev_number);
1601       bfd_set_error (bfd_error_bad_value);
1602       return 0;
1603     }
1604
1605   abbrev = lookup_abbrev (abbrev_number, abbrevs);
1606   if (! abbrev)
1607     {
1608       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1609                          abbrev_number);
1610       bfd_set_error (bfd_error_bad_value);
1611       return 0;
1612     }
1613
1614   amt = sizeof (struct comp_unit);
1615   unit = bfd_zalloc (abfd, amt);
1616   unit->abfd = abfd;
1617   unit->addr_size = addr_size;
1618   unit->offset_size = offset_size;
1619   unit->abbrevs = abbrevs;
1620   unit->end_ptr = end_ptr;
1621   unit->stash = stash;
1622   unit->info_ptr_unit = info_ptr_unit;
1623
1624   for (i = 0; i < abbrev->num_attrs; ++i)
1625     {
1626       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1627
1628       /* Store the data if it is of an attribute we want to keep in a
1629          partial symbol table.  */
1630       switch (attr.name)
1631         {
1632         case DW_AT_stmt_list:
1633           unit->stmtlist = 1;
1634           unit->line_offset = attr.u.val;
1635           break;
1636
1637         case DW_AT_name:
1638           unit->name = attr.u.str;
1639           break;
1640
1641         case DW_AT_low_pc:
1642           low_pc = attr.u.val;
1643           /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1644              this is the base address to use when reading location
1645              lists or range lists. */
1646           unit->base_address = low_pc;
1647           break;
1648
1649         case DW_AT_high_pc:
1650           high_pc = attr.u.val;
1651           break;
1652
1653         case DW_AT_ranges:
1654           read_rangelist (unit, &unit->arange, attr.u.val);
1655           break;
1656
1657         case DW_AT_comp_dir:
1658           {
1659             char *comp_dir = attr.u.str;
1660             if (comp_dir)
1661               {
1662                 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1663                    directory, get rid of it.  */
1664                 char *cp = strchr (comp_dir, ':');
1665
1666                 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1667                   comp_dir = cp + 1;
1668               }
1669             unit->comp_dir = comp_dir;
1670             break;
1671           }
1672
1673         default:
1674           break;
1675         }
1676     }
1677   if (high_pc != 0)
1678     {
1679       arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1680     }
1681
1682   unit->first_child_die_ptr = info_ptr;
1683   return unit;
1684 }
1685
1686 /* Return TRUE if UNIT contains the address given by ADDR.  */
1687
1688 static bfd_boolean
1689 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1690 {
1691   struct arange *arange;
1692
1693   if (unit->error)
1694     return FALSE;
1695
1696   arange = &unit->arange;
1697   do
1698     {
1699       if (addr >= arange->low && addr < arange->high)
1700         return TRUE;
1701       arange = arange->next;
1702     }
1703   while (arange);
1704
1705   return FALSE;
1706 }
1707
1708 /* If UNIT contains ADDR, set the output parameters to the values for
1709    the line containing ADDR.  The output parameters, FILENAME_PTR,
1710    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1711    to be filled in.
1712
1713    Return TRUE if UNIT contains ADDR, and no errors were encountered;
1714    FALSE otherwise.  */
1715
1716 static bfd_boolean
1717 comp_unit_find_nearest_line (struct comp_unit *unit,
1718                              bfd_vma addr,
1719                              const char **filename_ptr,
1720                              const char **functionname_ptr,
1721                              unsigned int *linenumber_ptr,
1722                              struct dwarf2_debug *stash)
1723 {
1724   bfd_boolean line_p;
1725   bfd_boolean func_p;
1726   struct funcinfo *function;
1727
1728   if (unit->error)
1729     return FALSE;
1730
1731   if (! unit->line_table)
1732     {
1733       if (! unit->stmtlist)
1734         {
1735           unit->error = 1;
1736           return FALSE;
1737         }
1738
1739       unit->line_table = decode_line_info (unit, stash);
1740
1741       if (! unit->line_table)
1742         {
1743           unit->error = 1;
1744           return FALSE;
1745         }
1746
1747       if (unit->first_child_die_ptr < unit->end_ptr
1748           && ! scan_unit_for_functions (unit))
1749         {
1750           unit->error = 1;
1751           return FALSE;
1752         }
1753     }
1754
1755   function = NULL;
1756   func_p = lookup_address_in_function_table (unit->function_table, addr,
1757                                              &function, functionname_ptr);
1758   line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1759                                               function, filename_ptr,
1760                                               linenumber_ptr);
1761   return line_p || func_p;
1762 }
1763
1764 /* Locate a section in a BFD containing debugging info.  The search starts
1765    from the section after AFTER_SEC, or from the first section in the BFD if
1766    AFTER_SEC is NULL.  The search works by examining the names of the
1767    sections.  There are two permissiable names.  The first is .debug_info.
1768    This is the standard DWARF2 name.  The second is a prefix .gnu.linkonce.wi.
1769    This is a variation on the .debug_info section which has a checksum
1770    describing the contents appended onto the name.  This allows the linker to
1771    identify and discard duplicate debugging sections for different
1772    compilation units.  */
1773 #define DWARF2_DEBUG_INFO ".debug_info"
1774 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1775
1776 static asection *
1777 find_debug_info (bfd *abfd, asection *after_sec)
1778 {
1779   asection * msec;
1780
1781   if (after_sec)
1782     msec = after_sec->next;
1783   else
1784     msec = abfd->sections;
1785
1786   while (msec)
1787     {
1788       if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1789         return msec;
1790
1791       if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1792         return msec;
1793
1794       msec = msec->next;
1795     }
1796
1797   return NULL;
1798 }
1799
1800 /* The DWARF2 version of find_nearest_line.  Return TRUE if the line
1801    is found without error.  ADDR_SIZE is the number of bytes in the
1802    initial .debug_info length field and in the abbreviation offset.
1803    You may use zero to indicate that the default value should be
1804    used.  */
1805
1806 bfd_boolean
1807 _bfd_dwarf2_find_nearest_line (bfd *abfd,
1808                                asection *section,
1809                                asymbol **symbols,
1810                                bfd_vma offset,
1811                                const char **filename_ptr,
1812                                const char **functionname_ptr,
1813                                unsigned int *linenumber_ptr,
1814                                unsigned int addr_size,
1815                                void **pinfo)
1816 {
1817   /* Read each compilation unit from the section .debug_info, and check
1818      to see if it contains the address we are searching for.  If yes,
1819      lookup the address, and return the line number info.  If no, go
1820      on to the next compilation unit.
1821
1822      We keep a list of all the previously read compilation units, and
1823      a pointer to the next un-read compilation unit.  Check the
1824      previously read units before reading more.  */
1825   struct dwarf2_debug *stash;
1826
1827   /* What address are we looking for?  */
1828   bfd_vma addr;
1829
1830   struct comp_unit* each;
1831
1832   stash = *pinfo;
1833   addr = offset;
1834   if (section->output_section)
1835     addr += section->output_section->vma + section->output_offset;
1836   else
1837     addr += section->vma;
1838   *filename_ptr = NULL;
1839   *functionname_ptr = NULL;
1840   *linenumber_ptr = 0;
1841
1842   /* The DWARF2 spec says that the initial length field, and the
1843      offset of the abbreviation table, should both be 4-byte values.
1844      However, some compilers do things differently.  */
1845   if (addr_size == 0)
1846     addr_size = 4;
1847   BFD_ASSERT (addr_size == 4 || addr_size == 8);
1848
1849   if (! stash)
1850     {
1851       bfd_size_type total_size;
1852       asection *msec;
1853       bfd_size_type amt = sizeof (struct dwarf2_debug);
1854
1855       stash = bfd_zalloc (abfd, amt);
1856       if (! stash)
1857         return FALSE;
1858
1859       *pinfo = stash;
1860
1861       msec = find_debug_info (abfd, NULL);
1862       if (! msec)
1863         /* No dwarf2 info.  Note that at this point the stash
1864            has been allocated, but contains zeros, this lets
1865            future calls to this function fail quicker.  */
1866          return FALSE;
1867
1868       /* There can be more than one DWARF2 info section in a BFD these days.
1869          Read them all in and produce one large stash.  We do this in two
1870          passes - in the first pass we just accumulate the section sizes.
1871          In the second pass we read in the section's contents.  The allows
1872          us to avoid reallocing the data as we add sections to the stash.  */
1873       for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1874         total_size += msec->size;
1875
1876       stash->info_ptr = bfd_alloc (abfd, total_size);
1877       if (stash->info_ptr == NULL)
1878         return FALSE;
1879
1880       stash->info_ptr_end = stash->info_ptr;
1881
1882       for (msec = find_debug_info (abfd, NULL);
1883            msec;
1884            msec = find_debug_info (abfd, msec))
1885         {
1886           bfd_size_type size;
1887           bfd_size_type start;
1888
1889           size = msec->size;
1890           if (size == 0)
1891             continue;
1892
1893           start = stash->info_ptr_end - stash->info_ptr;
1894
1895           if ((bfd_simple_get_relocated_section_contents
1896                (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1897             continue;
1898
1899           stash->info_ptr_end = stash->info_ptr + start + size;
1900         }
1901
1902       BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1903
1904       stash->sec = find_debug_info (abfd, NULL);
1905       stash->sec_info_ptr = stash->info_ptr;
1906       stash->syms = symbols;
1907     }
1908
1909   /* A null info_ptr indicates that there is no dwarf2 info
1910      (or that an error occured while setting up the stash).  */
1911   if (! stash->info_ptr)
1912     return FALSE;
1913
1914   /* Check the previously read comp. units first.  */
1915   for (each = stash->all_comp_units; each; each = each->next_unit)
1916     if (comp_unit_contains_address (each, addr))
1917       return comp_unit_find_nearest_line (each, addr, filename_ptr,
1918                                           functionname_ptr, linenumber_ptr,
1919                                           stash);
1920
1921   /* Read each remaining comp. units checking each as they are read.  */
1922   while (stash->info_ptr < stash->info_ptr_end)
1923     {
1924       bfd_vma length;
1925       bfd_boolean found;
1926       unsigned int offset_size = addr_size;
1927       bfd_byte *info_ptr_unit = stash->info_ptr;
1928
1929       length = read_4_bytes (abfd, stash->info_ptr);
1930       /* A 0xffffff length is the DWARF3 way of indicating we use
1931          64-bit offsets, instead of 32-bit offsets.  */
1932       if (length == 0xffffffff)
1933         {
1934           offset_size = 8;
1935           length = read_8_bytes (abfd, stash->info_ptr + 4);
1936           stash->info_ptr += 12;
1937         }
1938       /* A zero length is the IRIX way of indicating 64-bit offsets,
1939          mostly because the 64-bit length will generally fit in 32
1940          bits, and the endianness helps.  */
1941       else if (length == 0)
1942         {
1943           offset_size = 8;
1944           length = read_4_bytes (abfd, stash->info_ptr + 4);
1945           stash->info_ptr += 8;
1946         }
1947       /* In the absence of the hints above, we assume addr_size-sized
1948          offsets, for backward-compatibility with pre-DWARF3 64-bit
1949          platforms.  */
1950       else if (addr_size == 8)
1951         {
1952           length = read_8_bytes (abfd, stash->info_ptr);
1953           stash->info_ptr += 8;
1954         }
1955       else
1956         stash->info_ptr += 4;
1957
1958       if (length > 0)
1959         {
1960           each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
1961                                   offset_size);
1962           stash->info_ptr += length;
1963
1964           if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1965               == stash->sec->size)
1966             {
1967               stash->sec = find_debug_info (abfd, stash->sec);
1968               stash->sec_info_ptr = stash->info_ptr;
1969             }
1970
1971           if (each)
1972             {
1973               each->next_unit = stash->all_comp_units;
1974               stash->all_comp_units = each;
1975
1976               /* DW_AT_low_pc and DW_AT_high_pc are optional for
1977                  compilation units.  If we don't have them (i.e.,
1978                  unit->high == 0), we need to consult the line info
1979                  table to see if a compilation unit contains the given
1980                  address.  */
1981               if (each->arange.high > 0)
1982                 {
1983                   if (comp_unit_contains_address (each, addr))
1984                     return comp_unit_find_nearest_line (each, addr,
1985                                                         filename_ptr,
1986                                                         functionname_ptr,
1987                                                         linenumber_ptr,
1988                                                         stash);
1989                 }
1990               else
1991                 {
1992                   found = comp_unit_find_nearest_line (each, addr,
1993                                                        filename_ptr,
1994                                                        functionname_ptr,
1995                                                        linenumber_ptr,
1996                                                        stash);
1997                   if (found)
1998                     return TRUE;
1999                 }
2000             }
2001         }
2002     }
2003
2004   return FALSE;
2005 }