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