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