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