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