OSDN Git Service

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