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