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