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