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