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