import gdb-2000-01-17 snapshot
[external/binutils.git] / bfd / dwarf2.c
1 /* DWARF 2 support.
2    Copyright 1994, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
3
4    Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5    (gavin@cygnus.com).
6
7    From the dwarf2read.c header:
8    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9    Inc.  with support from Florida State University (under contract
10    with the Ada Joint Program Office), and Silicon Graphics, Inc.
11    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13    support in dwarfread.c
14
15 This file is part of BFD.
16
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or (at
20 your option) any later version.
21
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25 General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
30
31 #include "bfd.h"
32 #include "sysdep.h"
33 #include "libiberty.h"
34 #include "libbfd.h"
35 #include "elf-bfd.h"
36 #include "elf/dwarf2.h"
37
38 /* The data in the .debug_line statement prologue looks like this.  */
39 struct line_head
40   {
41     unsigned int total_length;
42     unsigned short version;
43     unsigned int prologue_length;
44     unsigned char minimum_instruction_length;
45     unsigned char default_is_stmt;
46     int line_base;
47     unsigned char line_range;
48     unsigned char opcode_base;
49     unsigned char *standard_opcode_lengths;
50   };
51
52 /* Attributes have a name and a value */
53 struct attribute
54   {
55     enum dwarf_attribute name;
56     enum dwarf_form form;
57     union
58       {
59         char *str;
60         struct dwarf_block *blk;
61         unsigned int unsnd;
62         int snd;
63         bfd_vma addr;
64       }
65     u;
66   };
67
68 /* Get at parts of an attribute structure */
69
70 #define DW_STRING(attr)    ((attr)->u.str)
71 #define DW_UNSND(attr)     ((attr)->u.unsnd)
72 #define DW_BLOCK(attr)     ((attr)->u.blk)
73 #define DW_SND(attr)       ((attr)->u.snd)
74 #define DW_ADDR(attr)      ((attr)->u.addr)
75
76 /* Blocks are a bunch of untyped bytes. */
77 struct dwarf_block
78   {
79     unsigned int size;
80     char *data;
81   };
82
83
84 struct dwarf2_debug {
85
86   /* A list of all previously read comp_units. */
87   struct comp_unit* all_comp_units;
88
89   /* The next unread compilation unit within the .debug_info section.
90      Zero indicates that the .debug_info section has not been loaded
91      into a buffer yet.*/
92   char* info_ptr;
93
94   /* Pointer to the end of the .debug_info section memory buffer. */
95   char* info_ptr_end;
96
97   /* Pointer to the .debug_abbrev section loaded into memory. */
98   char* dwarf_abbrev_buffer;
99
100   /* Length of the loaded .debug_abbrev section. */
101   unsigned long dwarf_abbrev_size;
102
103   /* Buffer for decode_line_info.  */
104   char *dwarf_line_buffer;
105 };
106
107 struct arange {
108   struct arange *next;
109   bfd_vma low;
110   bfd_vma high;
111 };
112
113
114 /* A minimal decoding of DWARF2 compilation units.  We only decode
115    what's needed to get to the line number information. */
116
117 struct comp_unit {
118
119   /* Chain the previously read compilation units. */
120   struct comp_unit* next_unit;
121
122   /* Keep the bdf convenient (for memory allocation). */
123   bfd* abfd;
124
125   /* The lowest and higest addresses contained in this compilation
126      unit as specified in the compilation unit header. */
127   struct arange arange;
128
129   /* The DW_AT_name attribute (for error messages). */
130   char* name;
131
132   /* The abbrev hash table. */
133   struct abbrev_info** abbrevs;
134
135   /* Note that an error was found by comp_unit_find_nearest_line. */
136   int error;
137
138   /* The DW_AT_comp_dir attribute */
139   char* comp_dir;
140
141   /* True if there is a line number table associated with this comp. unit. */
142   int stmtlist;
143   
144   /* The offset into .debug_line of the line number table. */
145   unsigned long line_offset;
146
147   /* Pointer to the first child die for the comp unit. */
148   char *first_child_die_ptr;
149
150   /* The end of the comp unit. */
151   char *end_ptr;
152
153   /* The decoded line number, NULL if not yet decoded. */
154   struct line_info_table* line_table;
155
156   /* A list of the functions found in this comp. unit. */
157   struct funcinfo* function_table; 
158
159   /* Address size for this unit - from unit header */
160   unsigned char addr_size;
161 };
162
163
164
165 /* VERBATIM 
166    The following function up to the END VERBATIM mark are 
167    copied directly from dwarf2read.c. */
168
169 /* read dwarf information from a buffer */
170
171 static unsigned int
172 read_1_byte (abfd, buf)
173      bfd *abfd ATTRIBUTE_UNUSED;
174      char *buf;
175 {
176   return bfd_get_8 (abfd, (bfd_byte *) buf);
177 }
178
179 static int
180 read_1_signed_byte (abfd, buf)
181      bfd *abfd ATTRIBUTE_UNUSED;
182      char *buf;
183 {
184   return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
185 }
186
187 static unsigned int
188 read_2_bytes (abfd, buf)
189      bfd *abfd;
190      char *buf;
191 {
192   return bfd_get_16 (abfd, (bfd_byte *) buf);
193 }
194
195 #if 0
196
197 /* This is not used.  */
198
199 static int
200 read_2_signed_bytes (abfd, buf)
201      bfd *abfd;
202      char *buf;
203 {
204   return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
205 }
206
207 #endif
208
209 static unsigned int
210 read_4_bytes (abfd, buf)
211      bfd *abfd;
212      char *buf;
213 {
214   return bfd_get_32 (abfd, (bfd_byte *) buf);
215 }
216
217 #if 0
218
219 /* This is not used.  */
220
221 static int
222 read_4_signed_bytes (abfd, buf)
223      bfd *abfd;
224      char *buf;
225 {
226   return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
227 }
228
229 #endif
230
231 static unsigned int
232 read_8_bytes (abfd, buf)
233      bfd *abfd;
234      char *buf;
235 {
236   return bfd_get_64 (abfd, (bfd_byte *) buf);
237 }
238
239 static char *
240 read_n_bytes (abfd, buf, size)
241      bfd *abfd ATTRIBUTE_UNUSED;
242      char *buf;
243      unsigned int size ATTRIBUTE_UNUSED;
244 {
245   /* If the size of a host char is 8 bits, we can return a pointer
246      to the buffer, otherwise we have to copy the data to a buffer
247      allocated on the temporary obstack.  */
248   return buf;
249 }
250
251 static char *
252 read_string (abfd, buf, bytes_read_ptr)
253      bfd *abfd ATTRIBUTE_UNUSED;
254      char *buf;
255      unsigned int *bytes_read_ptr;
256 {
257   /* If the size of a host char is 8 bits, we can return a pointer
258      to the string, otherwise we have to copy the string to a buffer
259      allocated on the temporary obstack.  */
260   if (*buf == '\0')
261     {
262       *bytes_read_ptr = 1;
263       return NULL;
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   bfd_vma retval = 0;
340
341   if (unit->addr_size == 4)
342     {
343       retval = bfd_get_32 (unit->abfd, (bfd_byte *) buf);
344     } else {
345       retval = bfd_get_64 (unit->abfd, (bfd_byte *) buf);
346     }
347   return retval;
348 }
349
350
351
352
353
354 /* This data structure holds the information of an abbrev. */
355 struct abbrev_info
356   {
357     unsigned int number;        /* number identifying abbrev */
358     enum dwarf_tag tag;         /* dwarf tag */
359     int has_children;           /* boolean */
360     unsigned int num_attrs;     /* number of attributes */
361     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
362     struct abbrev_info *next;   /* next in chain */
363   };
364
365 struct attr_abbrev
366   {
367     enum dwarf_attribute name;
368     enum dwarf_form form;
369   };
370
371 #ifndef ABBREV_HASH_SIZE
372 #define ABBREV_HASH_SIZE 121
373 #endif
374 #ifndef ATTR_ALLOC_CHUNK
375 #define ATTR_ALLOC_CHUNK 4
376 #endif
377
378 /* Lookup an abbrev_info structure in the abbrev hash table.  */
379
380 static struct abbrev_info *
381 lookup_abbrev (number,abbrevs)
382      unsigned int number;
383      struct abbrev_info **abbrevs;
384 {
385   unsigned int hash_number;
386   struct abbrev_info *abbrev;
387
388   hash_number = number % ABBREV_HASH_SIZE;
389   abbrev = abbrevs[hash_number];
390
391   while (abbrev)
392     {
393       if (abbrev->number == number)
394         return abbrev;
395       else
396         abbrev = abbrev->next;
397     }
398   return NULL;
399 }
400
401 /* In DWARF version 2, the description of the debugging information is
402    stored in a separate .debug_abbrev section.  Before we read any
403    dies from a section we read in all abbreviations and install them
404    in a hash table.  */
405
406 static struct abbrev_info**
407 read_abbrevs (abfd, offset)
408      bfd * abfd;
409      unsigned int offset;
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   struct dwarf2_debug *stash;
417
418   stash = elf_tdata(abfd)->dwarf2_find_line_info;
419
420   if (! stash->dwarf_abbrev_buffer)
421     {
422       asection *msec;
423
424       msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
425       if (! msec)
426         {
427           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
428           bfd_set_error (bfd_error_bad_value);
429           return 0;
430         }
431       
432       stash->dwarf_abbrev_size = bfd_get_section_size_before_reloc (msec);
433       stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
434       if (! stash->dwarf_abbrev_buffer)
435           return 0;
436       
437       if (! bfd_get_section_contents (abfd, msec, 
438                                       stash->dwarf_abbrev_buffer, 0,
439                                       stash->dwarf_abbrev_size))
440         return 0;
441     }
442
443   if (offset > stash->dwarf_abbrev_size)
444     {
445       (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."), 
446                              offset, stash->dwarf_abbrev_size );
447       bfd_set_error (bfd_error_bad_value);
448       return 0;
449     }
450
451   abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof(struct abbrev_info*) * ABBREV_HASH_SIZE);
452
453   abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
454   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
455   abbrev_ptr += bytes_read;
456
457   /* loop until we reach an abbrev number of 0 */
458   while (abbrev_number)
459     {
460       cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
461
462       /* read in abbrev header */
463       cur_abbrev->number = abbrev_number;
464       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
465       abbrev_ptr += bytes_read;
466       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
467       abbrev_ptr += 1;
468
469       /* now read in declarations */
470       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
471       abbrev_ptr += bytes_read;
472       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
473       abbrev_ptr += bytes_read;
474       while (abbrev_name)
475         {
476           if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
477             {
478               cur_abbrev->attrs = (struct attr_abbrev *)
479                 bfd_realloc (cur_abbrev->attrs,
480                              (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
481                              * sizeof (struct attr_abbrev));
482               if (! cur_abbrev->attrs)
483                 return 0;
484             }
485           cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
486           cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
487           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
488           abbrev_ptr += bytes_read;
489           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
490           abbrev_ptr += bytes_read;
491         }
492
493       hash_number = abbrev_number % ABBREV_HASH_SIZE;
494       cur_abbrev->next = abbrevs[hash_number];
495       abbrevs[hash_number] = cur_abbrev;
496
497       /* Get next abbreviation.
498          Under Irix6 the abbreviations for a compilation unit are not
499          always properly terminated with an abbrev number of 0.
500          Exit loop if we encounter an abbreviation which we have
501          already read (which means we are about to read the abbreviations
502          for the next compile unit) or if the end of the abbreviation
503          table is reached.  */
504       if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
505             >= stash->dwarf_abbrev_size)
506         break;
507       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
508       abbrev_ptr += bytes_read;
509       if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
510         break;
511     }
512
513   return abbrevs;
514 }
515
516 /* Read an attribute described by an abbreviated attribute.  */
517
518 static char *
519 read_attribute (attr, abbrev, unit, info_ptr)
520      struct attribute   *attr;
521      struct attr_abbrev *abbrev;
522      struct comp_unit   *unit;
523      char               *info_ptr;
524 {
525   bfd *abfd = unit->abfd;
526   unsigned int bytes_read;
527   struct dwarf_block *blk;
528
529   attr->name = abbrev->name;
530   attr->form = abbrev->form;
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
633 /* Source line information table routines. */
634
635 #define FILE_ALLOC_CHUNK 5
636 #define DIR_ALLOC_CHUNK 5
637
638 struct line_info {
639   struct line_info* prev_line;
640
641   bfd_vma address;
642   char* filename;
643   unsigned int line;
644   unsigned int column;
645   int end_sequence;             /* end of (sequential) code sequence */
646 };
647
648 struct fileinfo {
649   char *name;
650   unsigned int dir;
651   unsigned int time;
652   unsigned int size;
653 };
654
655 struct line_info_table {
656   bfd* abfd;
657
658   unsigned int num_files;
659   unsigned int num_dirs;
660
661   char* comp_dir;
662   char** dirs;
663   struct fileinfo* files;
664   struct line_info* last_line;
665 };
666
667 static void 
668 add_line_info (table, address, filename, line, column, end_sequence)
669      struct line_info_table* table;
670      bfd_vma address;
671      char* filename;
672      unsigned int line;
673      unsigned int column;
674      int end_sequence;
675 {
676   struct line_info* info = (struct line_info*)
677     bfd_alloc (table->abfd, sizeof (struct line_info));
678
679   info->prev_line = table->last_line;
680   table->last_line = info;
681
682   info->address = address;
683   info->filename = filename;
684   info->line = line;
685   info->column = column;
686   info->end_sequence = end_sequence;
687 }
688
689 static char* 
690 concat_filename (table, file)
691      struct line_info_table* table;
692      unsigned int file;
693 {
694   char* filename;
695
696   if (file - 1 >= table->num_files)
697     {
698       (*_bfd_error_handler) (_("Dwarf Error: mangled line number "
699                                "section (bad file number)."));
700       return "<unknown>";
701     }
702
703   filename = table->files[file - 1].name;
704   if (*filename == '/')
705     return filename;
706
707   else
708     {
709       char* dirname = (table->files[file - 1].dir
710                        ? table->dirs[table->files[file - 1].dir - 1]
711                        : table->comp_dir);
712       return (char*) concat (dirname, "/", filename, NULL);
713     }
714 }
715
716 static void
717 arange_add (unit, low_pc, high_pc)
718      struct comp_unit *unit;
719      bfd_vma low_pc;
720      bfd_vma high_pc;
721 {
722   struct arange *arange;
723
724   /* first see if we can cheaply extend an existing range: */
725   arange = &unit->arange;
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)
764      struct comp_unit *unit;
765 {
766   bfd *abfd = unit->abfd;
767
768   struct dwarf2_debug *stash;
769
770   struct line_info_table* table;
771
772   char *line_ptr;
773   char *line_end;
774   struct line_head lh;
775   unsigned int i, bytes_read;
776   char *cur_file, *cur_dir;
777   unsigned char op_code, extended_op, adj_opcode;
778
779   stash = elf_tdata (abfd)->dwarf2_find_line_info;
780
781   if (! stash->dwarf_line_buffer)
782     {
783       asection *msec;
784       unsigned long size;
785
786       msec = bfd_get_section_by_name (abfd, ".debug_line");
787       if (! msec)
788         {
789           (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
790           bfd_set_error (bfd_error_bad_value);
791           return 0;
792         }
793       
794       size = bfd_get_section_size_before_reloc (msec);
795       stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, size);
796       if (! stash->dwarf_line_buffer)
797         return 0;
798
799       if (! bfd_get_section_contents (abfd, msec, 
800                                       stash->dwarf_line_buffer, 0,
801                                       size))
802         return 0;
803
804       /* FIXME: We ought to apply the relocs against this section before
805          we process it.... */
806     }
807
808   table = (struct line_info_table*) bfd_alloc (abfd, 
809                                                sizeof (struct line_info_table));
810   table->abfd = abfd;
811   table->comp_dir = unit->comp_dir;
812
813   table->num_files = 0;
814   table->files = NULL;
815
816   table->num_dirs = 0;
817   table->dirs = NULL;
818
819   table->files = NULL;
820   table->last_line = NULL;
821
822   line_ptr = stash->dwarf_line_buffer + unit->line_offset;
823
824   /* read in the prologue */
825   lh.total_length = read_4_bytes (abfd, line_ptr);
826   line_ptr += 4;
827   line_end = line_ptr + lh.total_length;
828   lh.version = read_2_bytes (abfd, line_ptr);
829   line_ptr += 2;
830   lh.prologue_length = read_4_bytes (abfd, line_ptr);
831   line_ptr += 4;
832   lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
833   line_ptr += 1;
834   lh.default_is_stmt = read_1_byte (abfd, line_ptr);
835   line_ptr += 1;
836   lh.line_base = read_1_signed_byte (abfd, line_ptr);
837   line_ptr += 1;
838   lh.line_range = read_1_byte (abfd, line_ptr);
839   line_ptr += 1;
840   lh.opcode_base = read_1_byte (abfd, line_ptr);
841   line_ptr += 1;
842   lh.standard_opcode_lengths = (unsigned char *)
843     bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
844
845   lh.standard_opcode_lengths[0] = 1;
846   for (i = 1; i < lh.opcode_base; ++i)
847     {
848       lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
849       line_ptr += 1;
850     }
851
852   /* Read directory table  */
853   while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
854     {
855       line_ptr += bytes_read;
856       if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
857         {
858           table->dirs = (char **)
859             bfd_realloc (table->dirs,
860                          (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
861           if (! table->dirs)
862             return 0;
863         }
864       table->dirs[table->num_dirs++] = cur_dir;
865     }
866   line_ptr += bytes_read;
867
868   /* Read file name table */
869   while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
870     {
871       line_ptr += bytes_read;
872       if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
873         {
874           table->files = (struct fileinfo *)
875             bfd_realloc (table->files,
876                          (table->num_files + FILE_ALLOC_CHUNK)
877                          * sizeof (struct fileinfo));
878           if (! table->files)
879             return 0;
880         }
881       table->files[table->num_files].name = cur_file;
882       table->files[table->num_files].dir =
883         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
884       line_ptr += bytes_read;
885       table->files[table->num_files].time =
886         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
887       line_ptr += bytes_read;
888       table->files[table->num_files].size =
889         read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
890       line_ptr += bytes_read;
891       table->num_files++;
892     }
893   line_ptr += bytes_read;
894
895   /* Read the statement sequences until there's nothing left.  */
896   while (line_ptr < line_end)
897     {
898       /* state machine registers  */
899       bfd_vma address = 0;
900       char* filename = concat_filename (table, 1);
901       unsigned int line = 1;
902       unsigned int column = 0;
903       int is_stmt = lh.default_is_stmt;
904       int basic_block = 0;
905       int end_sequence = 0, need_low_pc = 1;
906       bfd_vma low_pc = 0;
907
908       /* Decode the table. */
909       while (! end_sequence)
910         {
911           op_code = read_1_byte (abfd, line_ptr);
912           line_ptr += 1;
913           switch (op_code)
914             {
915             case DW_LNS_extended_op:
916               line_ptr += 1;    /* ignore length */
917               extended_op = read_1_byte (abfd, line_ptr);
918               line_ptr += 1;
919               switch (extended_op)
920                 {
921                 case DW_LNE_end_sequence:
922                   end_sequence = 1;
923                   add_line_info (table, address, filename, line, column,
924                                  end_sequence);
925                   if (need_low_pc)
926                     {
927                       need_low_pc = 0;
928                       low_pc = address;
929                     }
930                   arange_add (unit, low_pc, address);
931                   break;
932                 case DW_LNE_set_address:
933                   address = read_address (unit, line_ptr);
934                   line_ptr += unit->addr_size;
935                   break;
936                 case DW_LNE_define_file:
937                   cur_file = read_string (abfd, line_ptr, &bytes_read);
938                   line_ptr += bytes_read;
939                   if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
940                     {
941                       table->files = (struct fileinfo *)
942                         bfd_realloc (table->files,
943                                      (table->num_files + FILE_ALLOC_CHUNK)
944                                      * sizeof (struct fileinfo));
945                       if (! table->files)
946                         return 0;
947                     }
948                   table->files[table->num_files].name = cur_file;
949                   table->files[table->num_files].dir =
950                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
951                   line_ptr += bytes_read;
952                   table->files[table->num_files].time =
953                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
954                   line_ptr += bytes_read;
955                   table->files[table->num_files].size =
956                     read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
957                   line_ptr += bytes_read;
958                   table->num_files++;
959                   break;
960                 default:
961                   (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
962                   bfd_set_error (bfd_error_bad_value);
963                   return 0;
964                 }
965               break;
966             case DW_LNS_copy:
967               add_line_info (table, address, filename, line, column, 0);
968               basic_block = 0;
969               if (need_low_pc)
970                 {
971                   need_low_pc = 0;
972                   low_pc = address;
973                 }
974               break;
975             case DW_LNS_advance_pc:
976               address += lh.minimum_instruction_length
977                 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
978               line_ptr += bytes_read;
979               break;
980             case DW_LNS_advance_line:
981               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
982               line_ptr += bytes_read;
983               break;
984             case DW_LNS_set_file:
985               {
986                 unsigned int file;
987
988                 /* The file and directory tables are 0 based, the references
989                    are 1 based.  */
990                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
991                 line_ptr += bytes_read;
992                 filename = concat_filename (table, file);
993                 break;
994               }
995             case DW_LNS_set_column:
996               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
997               line_ptr += bytes_read;
998               break;
999             case DW_LNS_negate_stmt:
1000               is_stmt = (!is_stmt);
1001               break;
1002             case DW_LNS_set_basic_block:
1003               basic_block = 1;
1004               break;
1005             case DW_LNS_const_add_pc:
1006               address += lh.minimum_instruction_length
1007                       * ((255 - lh.opcode_base) / lh.line_range);
1008               break;
1009             case DW_LNS_fixed_advance_pc:
1010               address += read_2_bytes (abfd, line_ptr);
1011               line_ptr += 2;
1012               break;
1013             default:            /* special operand */
1014               adj_opcode = op_code - lh.opcode_base;
1015               address += (adj_opcode / lh.line_range)
1016                 * lh.minimum_instruction_length;
1017               line += lh.line_base + (adj_opcode % lh.line_range);
1018               /* append row to matrix using current values */
1019               add_line_info (table, address, filename, line, column, 0);
1020               basic_block = 1;
1021               if (need_low_pc)
1022                 {
1023                   need_low_pc = 0;
1024                   low_pc = address;
1025                 }
1026             }
1027         }
1028     }
1029
1030   return table;
1031 }
1032
1033
1034 /* If ADDR is within TABLE set the output parameters and return true,
1035    otherwise return false.  The output parameters, FILENAME_PTR and
1036    LINENUMBER_PTR, are pointers to the objects to be filled in. */
1037
1038 static boolean
1039 lookup_address_in_line_info_table (table, 
1040                                    addr,
1041                                    filename_ptr, 
1042                                    linenumber_ptr)
1043      struct line_info_table* table;
1044      bfd_vma addr;
1045      const char **filename_ptr;
1046      unsigned int *linenumber_ptr;
1047 {
1048   struct line_info* next_line = table->last_line;
1049   struct line_info* each_line;
1050   
1051   if (!next_line)
1052     return false;
1053
1054   each_line = next_line->prev_line;
1055
1056   while (each_line && next_line)
1057     {
1058       if (!each_line->end_sequence
1059           && addr >= each_line->address && addr < next_line->address)
1060         {
1061           *filename_ptr = each_line->filename;
1062           *linenumber_ptr = each_line->line;
1063           return true;
1064         }
1065       next_line = each_line;
1066       each_line = each_line->prev_line;
1067     }
1068   
1069   return false;
1070 }
1071   
1072
1073
1074
1075 /* Function table functions. */
1076
1077 struct funcinfo {
1078   struct funcinfo *prev_func;
1079
1080   char* name;
1081   bfd_vma low;
1082   bfd_vma high;
1083 };
1084
1085
1086 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1087
1088 static boolean
1089 lookup_address_in_function_table (table, 
1090                                   addr,
1091                                   functionname_ptr)
1092      struct funcinfo* table;
1093      bfd_vma addr;
1094      const char **functionname_ptr;
1095 {
1096   struct funcinfo* each_func;
1097
1098   for (each_func = table;
1099        each_func;
1100        each_func = each_func->prev_func)
1101     {
1102       if (addr >= each_func->low && addr < each_func->high)
1103         {
1104           *functionname_ptr = each_func->name;
1105           return true;
1106         }
1107     }
1108   
1109   return false;
1110 }
1111
1112
1113
1114
1115 /* DWARF2 Compilation unit functions. */
1116
1117
1118 /* Scan over each die in a comp. unit looking for functions to add
1119    to the function table. */
1120
1121 static boolean
1122 scan_unit_for_functions (unit)
1123      struct comp_unit *unit;
1124 {
1125   bfd *abfd = unit->abfd;
1126   char *info_ptr = unit->first_child_die_ptr;
1127   int nesting_level = 1;
1128
1129   while (nesting_level)
1130     {
1131       unsigned int abbrev_number, bytes_read, i;
1132       struct abbrev_info *abbrev;
1133       struct attribute attr;
1134       struct funcinfo *func;
1135       char* name = 0;
1136
1137       abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1138       info_ptr += bytes_read;
1139
1140       if (! abbrev_number)
1141         {
1142           nesting_level--;
1143           continue;
1144         }
1145       
1146       abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1147       if (! abbrev)
1148         {
1149           (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."), 
1150                              abbrev_number);
1151           bfd_set_error (bfd_error_bad_value);
1152           return false;
1153         }
1154       
1155       if (abbrev->tag == DW_TAG_subprogram)
1156         {
1157           func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1158           func->prev_func = unit->function_table;
1159           unit->function_table = func;
1160         }
1161       else
1162         func = NULL;
1163   
1164       for (i = 0; i < abbrev->num_attrs; ++i)
1165         {
1166           info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1167           
1168           if (func)
1169             {
1170               switch (attr.name)
1171                 {
1172                 case DW_AT_name:
1173                   
1174                   name = DW_STRING (&attr);
1175
1176                   /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name.  */
1177                   if (func->name == NULL)
1178                     func->name = DW_STRING (&attr);
1179                   break;
1180                   
1181                 case DW_AT_MIPS_linkage_name:
1182                   func->name = DW_STRING (&attr);
1183                   break;
1184
1185                 case DW_AT_low_pc:
1186                   func->low = DW_ADDR (&attr);
1187                   break;
1188
1189                 case DW_AT_high_pc:
1190                   func->high = DW_ADDR (&attr);
1191                   break;
1192
1193                 default:
1194                   break;
1195                 }
1196             }
1197           else
1198             {
1199               switch (attr.name)
1200                 {
1201                 case DW_AT_name:
1202                   name = DW_STRING (&attr);
1203                   break;
1204                   
1205                 default:
1206                   break;
1207                 }
1208             }
1209         }
1210
1211       if (abbrev->has_children)
1212         nesting_level++;
1213     }
1214
1215   return true;
1216 }
1217
1218
1219
1220
1221
1222
1223 /* Parse a DWARF2 compilation unit starting at INFO_PTR.  This
1224    includes the compilation unit header that proceeds the DIE's, but
1225    does not include the length field that preceeds each compilation
1226    unit header.  END_PTR points one past the end of this comp unit.
1227    If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1228    is assumed to be four bytes.  Otherwise, it it is the size given.
1229
1230    This routine does not read the whole compilation unit; only enough
1231    to get to the line number information for the compilation unit.  */
1232
1233 static struct comp_unit *
1234 parse_comp_unit (abfd, info_ptr, end_ptr, abbrev_length)
1235      bfd* abfd;
1236      char* info_ptr;
1237      char* end_ptr;
1238      unsigned int abbrev_length;
1239 {
1240   struct comp_unit* unit;
1241
1242   unsigned short version;
1243   unsigned int abbrev_offset = 0;
1244   unsigned char addr_size;
1245   struct abbrev_info** abbrevs;
1246
1247   unsigned int abbrev_number, bytes_read, i;
1248   struct abbrev_info *abbrev;
1249   struct attribute attr;
1250
1251   version = read_2_bytes (abfd, info_ptr);
1252   info_ptr += 2;
1253   BFD_ASSERT (abbrev_length == 0
1254               || abbrev_length == 4
1255               || abbrev_length == 8);
1256   if (abbrev_length == 0 || abbrev_length == 4)
1257     abbrev_offset = read_4_bytes (abfd, info_ptr);
1258   else if (abbrev_length == 8)
1259     abbrev_offset = read_8_bytes (abfd, info_ptr);
1260   info_ptr += abbrev_length;
1261   addr_size = read_1_byte (abfd, info_ptr);
1262   info_ptr += 1;
1263
1264   if (version != 2)
1265     {
1266       (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1267       bfd_set_error (bfd_error_bad_value);
1268       return 0;
1269     }
1270
1271   if (addr_size > sizeof (bfd_vma))
1272     {
1273       (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1274                          addr_size,
1275                          sizeof (bfd_vma));
1276       bfd_set_error (bfd_error_bad_value);
1277       return 0;
1278     }
1279
1280   if (addr_size != 4 && addr_size != 8)
1281     {
1282       (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '4' and '8'.", addr_size );
1283       bfd_set_error (bfd_error_bad_value);
1284       return 0;
1285     }
1286
1287   /* Read the abbrevs for this compilation unit into a table */
1288   abbrevs = read_abbrevs (abfd, abbrev_offset);
1289   if (! abbrevs)
1290       return 0;
1291
1292   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1293   info_ptr += bytes_read;
1294   if (! abbrev_number)
1295     {
1296       (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1297                          abbrev_number);
1298       bfd_set_error (bfd_error_bad_value);
1299       return 0;
1300     }
1301
1302   abbrev = lookup_abbrev (abbrev_number, abbrevs);
1303   if (! abbrev)
1304     {
1305       (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1306                          abbrev_number);
1307       bfd_set_error (bfd_error_bad_value);
1308       return 0;
1309     }
1310   
1311   unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1312   unit->abfd = abfd;
1313   unit->addr_size = addr_size; 
1314   unit->abbrevs = abbrevs;
1315   unit->end_ptr = end_ptr;
1316
1317   for (i = 0; i < abbrev->num_attrs; ++i)
1318     {
1319       info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1320
1321       /* Store the data if it is of an attribute we want to keep in a
1322          partial symbol table.  */
1323       switch (attr.name)
1324         {
1325         case DW_AT_stmt_list:
1326           unit->stmtlist = 1;
1327           unit->line_offset = DW_UNSND (&attr);
1328           break;
1329
1330         case DW_AT_name:
1331           unit->name = DW_STRING (&attr);
1332           break;
1333
1334         case DW_AT_low_pc:
1335           unit->arange.low = DW_ADDR (&attr);
1336           break;
1337
1338         case DW_AT_high_pc:
1339           unit->arange.high = DW_ADDR (&attr);
1340           break;
1341
1342         case DW_AT_comp_dir:
1343           {
1344             char* comp_dir = DW_STRING (&attr);
1345             if (comp_dir)
1346               {
1347                 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1348                    directory, get rid of it.  */
1349                 char *cp = (char*) strchr (comp_dir, ':');
1350
1351                 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1352                   comp_dir = cp + 1;
1353               }
1354             unit->comp_dir = comp_dir;
1355             break;
1356           }
1357
1358         default:
1359           break;
1360         }
1361     }
1362
1363   unit->first_child_die_ptr = info_ptr;
1364   return unit;
1365 }
1366
1367
1368
1369
1370
1371 /* Return true if UNIT contains the address given by ADDR. */
1372
1373 static boolean
1374 comp_unit_contains_address (unit, addr)
1375      struct comp_unit* unit;
1376      bfd_vma addr;
1377 {
1378   struct arange *arange;
1379
1380   if (unit->error)
1381     return 0;
1382
1383   arange = &unit->arange;
1384   do
1385     {
1386       if (addr >= arange->low && addr < arange->high)
1387         return 1;
1388       arange = arange->next;
1389     }
1390   while (arange);
1391   return 0;
1392 }
1393
1394
1395 /* If UNIT contains ADDR, set the output parameters to the values for
1396    the line containing ADDR.  The output parameters, FILENAME_PTR,
1397    FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1398    to be filled in.  
1399
1400    Return true of UNIT contains ADDR, and no errors were encountered;
1401    false otherwise.  */
1402
1403 static boolean
1404 comp_unit_find_nearest_line (unit, addr,
1405                              filename_ptr, functionname_ptr, linenumber_ptr)
1406      struct comp_unit* unit;
1407      bfd_vma addr;
1408      const char **filename_ptr;
1409      const char **functionname_ptr;
1410      unsigned int *linenumber_ptr;
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);
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 /* The DWARF2 version of find_nearest line.  Return true if the line
1452    is found without error.  ADDR_SIZE is the number of bytes in the
1453    initial .debug_info length field and in the abbreviation offset.
1454    You may use zero to indicate that the default value should be
1455    used.  */
1456
1457 boolean
1458 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1459                                filename_ptr, functionname_ptr,
1460                                linenumber_ptr,
1461                                addr_size)
1462      bfd *abfd;
1463      asection *section;
1464      asymbol **symbols ATTRIBUTE_UNUSED;
1465      bfd_vma offset;
1466      const char **filename_ptr;
1467      const char **functionname_ptr;
1468      unsigned int *linenumber_ptr;
1469      unsigned int addr_size;
1470 {
1471   /* Read each compilation unit from the section .debug_info, and check
1472      to see if it contains the address we are searching for.  If yes,
1473      lookup the address, and return the line number info.  If no, go
1474      on to the next compilation unit.  
1475
1476      We keep a list of all the previously read compilation units, and
1477      a pointer to the next un-read compilation unit.  Check the 
1478      previously read units before reading more.
1479      */
1480
1481   struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info;
1482
1483   /* What address are we looking for? */
1484   bfd_vma addr = offset + section->vma;
1485
1486   struct comp_unit* each;
1487   
1488   *filename_ptr = NULL;
1489   *functionname_ptr = NULL;
1490   *linenumber_ptr = 0;
1491
1492   /* The DWARF2 spec says that the initial length field, and the
1493      offset of the abbreviation table, should both be 4-byte values.
1494      However, some compilers do things differently.  */
1495   if (addr_size == 0)
1496     addr_size = 4;
1497   BFD_ASSERT (addr_size == 4 || addr_size == 8);
1498     
1499   if (! stash)
1500     {
1501       asection *msec;
1502       unsigned long size;
1503       
1504       stash = elf_tdata (abfd)->dwarf2_find_line_info =
1505         (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
1506       
1507       if (! stash)
1508         return false;
1509       
1510       msec = bfd_get_section_by_name (abfd, ".debug_info");
1511       if (! msec)
1512         {
1513           /* No dwarf2 info.  Note that at this point the stash
1514              has been allocated, but contains zeros, this lets
1515              future calls to this function fail quicker. */
1516           return false;
1517         }
1518
1519       size = bfd_get_section_size_before_reloc (msec);
1520       if (size == 0)
1521         return false;
1522       
1523       stash->info_ptr = (char *) bfd_alloc (abfd, size);
1524       
1525       if (! stash->info_ptr)
1526         return false;
1527
1528       if (! bfd_get_section_contents (abfd, msec, stash->info_ptr, 0, size))
1529         {
1530           stash->info_ptr = 0;
1531           return false;
1532         }
1533
1534       stash->info_ptr_end = stash->info_ptr + size;
1535
1536       /* FIXME: There is a problem with the contents of the
1537          .debug_info section.  The 'low' and 'high' addresses of the
1538          comp_units are computed by relocs against symbols in the
1539          .text segment.  We need these addresses in order to determine
1540          the nearest line number, and so we have to resolve the
1541          relocs.  There is a similar problem when the .debug_line
1542          section is processed as well (e.g., there may be relocs
1543          against the operand of the DW_LNE_set_address operator).
1544          
1545          Unfortunately getting hold of the reloc information is hard...
1546
1547          For now, this means that disassembling object files (as
1548          opposed to fully executables) does not always work as well as
1549          we would like.  */
1550     }
1551   
1552   /* A null info_ptr indicates that there is no dwarf2 info 
1553      (or that an error occured while setting up the stash). */
1554
1555   if (! stash->info_ptr)
1556     return false;
1557
1558   /* Check the previously read comp. units first. */
1559
1560   for (each = stash->all_comp_units; each; each = each->next_unit)
1561     if (comp_unit_contains_address (each, addr))
1562       return comp_unit_find_nearest_line (each, addr, filename_ptr, 
1563                                           functionname_ptr, linenumber_ptr);
1564
1565   /* Read each remaining comp. units checking each as they are read. */
1566   while (stash->info_ptr < stash->info_ptr_end)
1567     {
1568       struct comp_unit* each;
1569       bfd_vma length;
1570       boolean found;
1571
1572       if (addr_size == 4)
1573         length = read_4_bytes (abfd, stash->info_ptr);
1574       else
1575         length = read_8_bytes (abfd, stash->info_ptr);
1576       stash->info_ptr += addr_size;
1577
1578       if (length > 0)
1579         {
1580           each = parse_comp_unit (abfd, stash->info_ptr, 
1581                                   stash->info_ptr + length,
1582                                   addr_size);
1583           stash->info_ptr += length;
1584
1585           if (each)
1586             {
1587               each->next_unit = stash->all_comp_units;
1588               stash->all_comp_units = each;
1589
1590               /* DW_AT_low_pc and DW_AT_high_pc are optional for
1591                  compilation units.  If we don't have them (i.e.,
1592                  unit->high == 0), we need to consult the line info
1593                  table to see if a compilation unit contains the given
1594                  address. */
1595               if (each->arange.high > 0)
1596                 {
1597                   if (comp_unit_contains_address (each, addr))
1598                     return comp_unit_find_nearest_line (each, addr,
1599                                                        filename_ptr,
1600                                                        functionname_ptr,
1601                                                        linenumber_ptr);
1602                 }
1603               else
1604                 {
1605                   found = comp_unit_find_nearest_line (each, addr,
1606                                                        filename_ptr,
1607                                                        functionname_ptr,
1608                                                        linenumber_ptr);
1609                   if (found)
1610                     return true;
1611                 }
1612             }
1613         }
1614     }
1615
1616   return false;
1617 }
1618
1619 /* end of file */