2000-12-03 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)
406 bfd * abfd;
407 unsigned int offset;
408 {
409 struct abbrev_info **abbrevs;
410 char *abbrev_ptr;
411 struct abbrev_info *cur_abbrev;
412 unsigned int abbrev_number, bytes_read, abbrev_name;
413 unsigned int abbrev_form, hash_number;
414 struct dwarf2_debug *stash;
415
416 stash = elf_tdata(abfd)->dwarf2_find_line_info;
417
418 if (! stash->dwarf_abbrev_buffer)
419 {
420 asection *msec;
421
422 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
423 if (! msec)
424 {
425 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
426 bfd_set_error (bfd_error_bad_value);
427 return 0;
428 }
429
430 stash->dwarf_abbrev_size = msec->_raw_size;
431 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
432 if (! stash->dwarf_abbrev_buffer)
433 return 0;
434
435 if (! bfd_get_section_contents (abfd, msec,
436 stash->dwarf_abbrev_buffer, 0,
437 stash->dwarf_abbrev_size))
438 return 0;
439 }
440
441 if (offset > stash->dwarf_abbrev_size)
442 {
443 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."),
444 offset, stash->dwarf_abbrev_size );
445 bfd_set_error (bfd_error_bad_value);
446 return 0;
447 }
448
449 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE);
450
451 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
452 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
453 abbrev_ptr += bytes_read;
454
455 /* Loop until we reach an abbrev number of 0. */
456 while (abbrev_number)
457 {
458 cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
459
460 /* Read in abbrev header. */
461 cur_abbrev->number = abbrev_number;
462 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
463 abbrev_ptr += bytes_read;
464 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
465 abbrev_ptr += 1;
466
467 /* Now read in declarations. */
468 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
469 abbrev_ptr += bytes_read;
470 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
471 abbrev_ptr += bytes_read;
472
473 while (abbrev_name)
474 {
475 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
476 {
477 cur_abbrev->attrs = (struct attr_abbrev *)
478 bfd_realloc (cur_abbrev->attrs,
479 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
480 * sizeof (struct attr_abbrev));
481 if (! cur_abbrev->attrs)
482 return 0;
483 }
484
485 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
486 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
487 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
488 abbrev_ptr += bytes_read;
489 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
490 abbrev_ptr += bytes_read;
491 }
492
493 hash_number = abbrev_number % ABBREV_HASH_SIZE;
494 cur_abbrev->next = abbrevs[hash_number];
495 abbrevs[hash_number] = cur_abbrev;
496
497 /* Get next abbreviation.
498 Under Irix6 the abbreviations for a compilation unit are not
499 always properly terminated with an abbrev number of 0.
500 Exit loop if we encounter an abbreviation which we have
501 already read (which means we are about to read the abbreviations
502 for the next compile unit) or if the end of the abbreviation
503 table is reached. */
504 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
505 >= stash->dwarf_abbrev_size)
506 break;
507 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
508 abbrev_ptr += bytes_read;
509 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
510 break;
511 }
512
513 return abbrevs;
514 }
515
516 /* Read an attribute described by an abbreviated attribute. */
517
518 static char *
519 read_attribute (attr, abbrev, unit, info_ptr)
520 struct attribute *attr;
521 struct attr_abbrev *abbrev;
522 struct comp_unit *unit;
523 char *info_ptr;
524 {
525 bfd *abfd = unit->abfd;
526 unsigned int bytes_read;
527 struct dwarf_block *blk;
528
529 attr->name = abbrev->name;
530 attr->form = abbrev->form;
531
532 switch (abbrev->form)
533 {
534 case DW_FORM_addr:
535 case DW_FORM_ref_addr:
536 DW_ADDR (attr) = read_address (unit, info_ptr);
537 info_ptr += unit->addr_size;
538 break;
539 case DW_FORM_block2:
540 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
541 blk->size = read_2_bytes (abfd, info_ptr);
542 info_ptr += 2;
543 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
544 info_ptr += blk->size;
545 DW_BLOCK (attr) = blk;
546 break;
547 case DW_FORM_block4:
548 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
549 blk->size = read_4_bytes (abfd, info_ptr);
550 info_ptr += 4;
551 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
552 info_ptr += blk->size;
553 DW_BLOCK (attr) = blk;
554 break;
555 case DW_FORM_data2:
556 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
557 info_ptr += 2;
558 break;
559 case DW_FORM_data4:
560 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
561 info_ptr += 4;
562 break;
563 case DW_FORM_data8:
564 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
565 info_ptr += 8;
566 break;
567 case DW_FORM_string:
568 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
569 info_ptr += bytes_read;
570 break;
571 case DW_FORM_block:
572 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
573 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
574 info_ptr += bytes_read;
575 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
576 info_ptr += blk->size;
577 DW_BLOCK (attr) = blk;
578 break;
579 case DW_FORM_block1:
580 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
581 blk->size = read_1_byte (abfd, info_ptr);
582 info_ptr += 1;
583 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
584 info_ptr += blk->size;
585 DW_BLOCK (attr) = blk;
586 break;
587 case DW_FORM_data1:
588 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
589 info_ptr += 1;
590 break;
591 case DW_FORM_flag:
592 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
593 info_ptr += 1;
594 break;
595 case DW_FORM_sdata:
596 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
597 info_ptr += bytes_read;
598 break;
599 case DW_FORM_udata:
600 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
601 info_ptr += bytes_read;
602 break;
603 case DW_FORM_ref1:
604 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
605 info_ptr += 1;
606 break;
607 case DW_FORM_ref2:
608 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
609 info_ptr += 2;
610 break;
611 case DW_FORM_ref4:
612 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
613 info_ptr += 4;
614 break;
615 case DW_FORM_ref8:
616 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
617 info_ptr += 8;
618 break;
619 case DW_FORM_ref_udata:
620 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
621 info_ptr += bytes_read;
622 break;
623 case DW_FORM_strp:
624 case DW_FORM_indirect:
625 default:
626 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
627 abbrev->form);
628 bfd_set_error (bfd_error_bad_value);
629 }
630 return info_ptr;
631 }
632
633 /* Source line information table routines. */
634
635 #define FILE_ALLOC_CHUNK 5
636 #define DIR_ALLOC_CHUNK 5
637
638 struct line_info
639 {
640 struct line_info* prev_line;
641 bfd_vma address;
642 char* filename;
643 unsigned int line;
644 unsigned int column;
645 int end_sequence; /* End of (sequential) code sequence. */
646 };
647
648 struct fileinfo
649 {
650 char *name;
651 unsigned int dir;
652 unsigned int time;
653 unsigned int size;
654 };
655
656 struct line_info_table
657 {
658 bfd* abfd;
659 unsigned int num_files;
660 unsigned int num_dirs;
661 char* comp_dir;
662 char** dirs;
663 struct fileinfo* files;
664 struct line_info* last_line;
665 };
666
667 static void
668 add_line_info (table, address, filename, line, column, end_sequence)
669 struct line_info_table* table;
670 bfd_vma address;
671 char* filename;
672 unsigned int line;
673 unsigned int column;
674 int end_sequence;
675 {
676 struct line_info* info = (struct line_info*)
677 bfd_alloc (table->abfd, sizeof (struct line_info));
678
679 info->prev_line = table->last_line;
680 table->last_line = info;
681
682 info->address = address;
683 info->filename = filename;
684 info->line = line;
685 info->column = column;
686 info->end_sequence = end_sequence;
687 }
688
689 static char *
690 concat_filename (table, file)
691 struct line_info_table* table;
692 unsigned int file;
693 {
694 char* filename;
695
696 if (file - 1 >= table->num_files)
697 {
698 (*_bfd_error_handler)
699 (_("Dwarf Error: mangled line number section (bad file number)."));
700 return "<unknown>";
701 }
702
703 filename = table->files[file - 1].name;
704 if (*filename == '/')
705 return filename;
706
707 else
708 {
709 char* dirname = (table->files[file - 1].dir
710 ? table->dirs[table->files[file - 1].dir - 1]
711 : table->comp_dir);
712 return (char*) concat (dirname, "/", filename, NULL);
713 }
714 }
715
716 static void
717 arange_add (unit, low_pc, high_pc)
718 struct comp_unit *unit;
719 bfd_vma low_pc;
720 bfd_vma high_pc;
721 {
722 struct arange *arange;
723
724 /* First see if we can cheaply extend an existing range. */
725 arange = &unit->arange;
726
727 do
728 {
729 if (low_pc == arange->high)
730 {
731 arange->high = high_pc;
732 return;
733 }
734 if (high_pc == arange->low)
735 {
736 arange->low = low_pc;
737 return;
738 }
739 arange = arange->next;
740 }
741 while (arange);
742
743 if (unit->arange.high == 0)
744 {
745 /* This is the first address range: store it in unit->arange. */
746 unit->arange.next = 0;
747 unit->arange.low = low_pc;
748 unit->arange.high = high_pc;
749 return;
750 }
751
752 /* Need to allocate a new arange and insert it into the arange list. */
753 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
754 arange->low = low_pc;
755 arange->high = high_pc;
756
757 arange->next = unit->arange.next;
758 unit->arange.next = arange;
759 }
760
761 /* Decode the line number information for UNIT. */
762
763 static struct line_info_table*
764 decode_line_info (unit)
765 struct comp_unit *unit;
766 {
767 bfd *abfd = unit->abfd;
768 struct dwarf2_debug *stash;
769 struct line_info_table* table;
770 char *line_ptr;
771 char *line_end;
772 struct line_head lh;
773 unsigned int i, bytes_read;
774 char *cur_file, *cur_dir;
775 unsigned char op_code, extended_op, adj_opcode;
776
777 stash = elf_tdata (abfd)->dwarf2_find_line_info;
778
779 if (! stash->dwarf_line_buffer)
780 {
781 asection *msec;
782
783 msec = bfd_get_section_by_name (abfd, ".debug_line");
784 if (! msec)
785 {
786 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
787 bfd_set_error (bfd_error_bad_value);
788 return 0;
789 }
790
791 stash->dwarf_line_size = msec->_raw_size;
792 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, stash->dwarf_line_size);
793 if (! stash->dwarf_line_buffer)
794 return 0;
795
796 if (! bfd_get_section_contents (abfd, msec,
797 stash->dwarf_line_buffer, 0,
798 stash->dwarf_line_size))
799 return 0;
800
801 /* FIXME: We ought to apply the relocs against this section before
802 we process it... */
803 }
804
805 /* Since we are using un-relocated data, it is possible to get a bad value
806 for the line_offset. Validate it here so that we won't get a segfault
807 below. */
808 if (unit->line_offset >= stash->dwarf_line_size)
809 {
810 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%u) bigger than line size (%u)."),
811 unit->line_offset, stash->dwarf_line_size);
812 bfd_set_error (bfd_error_bad_value);
813 return 0;
814 }
815
816 table = (struct line_info_table*) bfd_alloc (abfd,
817 sizeof (struct line_info_table));
818 table->abfd = abfd;
819 table->comp_dir = unit->comp_dir;
820
821 table->num_files = 0;
822 table->files = NULL;
823
824 table->num_dirs = 0;
825 table->dirs = NULL;
826
827 table->files = NULL;
828 table->last_line = NULL;
829
830 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
831
832 /* Read in the prologue. */
833 lh.total_length = read_4_bytes (abfd, line_ptr);
834 line_ptr += 4;
835 line_end = line_ptr + lh.total_length;
836 lh.version = read_2_bytes (abfd, line_ptr);
837 line_ptr += 2;
838 lh.prologue_length = read_4_bytes (abfd, line_ptr);
839 line_ptr += 4;
840 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
841 line_ptr += 1;
842 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
843 line_ptr += 1;
844 lh.line_base = read_1_signed_byte (abfd, line_ptr);
845 line_ptr += 1;
846 lh.line_range = read_1_byte (abfd, line_ptr);
847 line_ptr += 1;
848 lh.opcode_base = read_1_byte (abfd, line_ptr);
849 line_ptr += 1;
850 lh.standard_opcode_lengths = (unsigned char *)
851 bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
852
853 lh.standard_opcode_lengths[0] = 1;
854
855 for (i = 1; i < lh.opcode_base; ++i)
856 {
857 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
858 line_ptr += 1;
859 }
860
861 /* Read directory table. */
862 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
863 {
864 line_ptr += bytes_read;
865
866 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
867 {
868 table->dirs = (char **)
869 bfd_realloc (table->dirs,
870 (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
871 if (! table->dirs)
872 return 0;
873 }
874
875 table->dirs[table->num_dirs++] = cur_dir;
876 }
877
878 line_ptr += bytes_read;
879
880 /* Read file name table. */
881 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
882 {
883 line_ptr += bytes_read;
884
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
895 table->files[table->num_files].name = cur_file;
896 table->files[table->num_files].dir =
897 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
898 line_ptr += bytes_read;
899 table->files[table->num_files].time =
900 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
901 line_ptr += bytes_read;
902 table->files[table->num_files].size =
903 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
904 line_ptr += bytes_read;
905 table->num_files++;
906 }
907
908 line_ptr += bytes_read;
909
910 /* Read the statement sequences until there's nothing left. */
911 while (line_ptr < line_end)
912 {
913 /* State machine registers. */
914 bfd_vma address = 0;
915 char* filename = concat_filename (table, 1);
916 unsigned int line = 1;
917 unsigned int column = 0;
918 int is_stmt = lh.default_is_stmt;
919 int basic_block = 0;
920 int end_sequence = 0, need_low_pc = 1;
921 bfd_vma low_pc = 0;
922
923 /* Decode the table. */
924 while (! end_sequence)
925 {
926 op_code = read_1_byte (abfd, line_ptr);
927 line_ptr += 1;
928
929 switch (op_code)
930 {
931 case DW_LNS_extended_op:
932 line_ptr += 1; /* Ignore length. */
933 extended_op = read_1_byte (abfd, line_ptr);
934 line_ptr += 1;
935 switch (extended_op)
936 {
937 case DW_LNE_end_sequence:
938 end_sequence = 1;
939 add_line_info (table, address, filename, line, column,
940 end_sequence);
941 if (need_low_pc)
942 {
943 need_low_pc = 0;
944 low_pc = address;
945 }
946 arange_add (unit, low_pc, address);
947 break;
948 case DW_LNE_set_address:
949 address = read_address (unit, line_ptr);
950 line_ptr += unit->addr_size;
951 break;
952 case DW_LNE_define_file:
953 cur_file = read_string (abfd, line_ptr, &bytes_read);
954 line_ptr += bytes_read;
955 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
956 {
957 table->files = (struct fileinfo *)
958 bfd_realloc (table->files,
959 (table->num_files + FILE_ALLOC_CHUNK)
960 * sizeof (struct fileinfo));
961 if (! table->files)
962 return 0;
963 }
964 table->files[table->num_files].name = cur_file;
965 table->files[table->num_files].dir =
966 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
967 line_ptr += bytes_read;
968 table->files[table->num_files].time =
969 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
970 line_ptr += bytes_read;
971 table->files[table->num_files].size =
972 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
973 line_ptr += bytes_read;
974 table->num_files++;
975 break;
976 default:
977 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
978 bfd_set_error (bfd_error_bad_value);
979 return 0;
980 }
981 break;
982 case DW_LNS_copy:
983 add_line_info (table, address, filename, line, column, 0);
984 basic_block = 0;
985 if (need_low_pc)
986 {
987 need_low_pc = 0;
988 low_pc = address;
989 }
990 break;
991 case DW_LNS_advance_pc:
992 address += lh.minimum_instruction_length
993 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
994 line_ptr += bytes_read;
995 break;
996 case DW_LNS_advance_line:
997 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
998 line_ptr += bytes_read;
999 break;
1000 case DW_LNS_set_file:
1001 {
1002 unsigned int file;
1003
1004 /* The file and directory tables are 0 based, the references
1005 are 1 based. */
1006 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1007 line_ptr += bytes_read;
1008 filename = concat_filename (table, file);
1009 break;
1010 }
1011 case DW_LNS_set_column:
1012 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1013 line_ptr += bytes_read;
1014 break;
1015 case DW_LNS_negate_stmt:
1016 is_stmt = (!is_stmt);
1017 break;
1018 case DW_LNS_set_basic_block:
1019 basic_block = 1;
1020 break;
1021 case DW_LNS_const_add_pc:
1022 address += lh.minimum_instruction_length
1023 * ((255 - lh.opcode_base) / lh.line_range);
1024 break;
1025 case DW_LNS_fixed_advance_pc:
1026 address += read_2_bytes (abfd, line_ptr);
1027 line_ptr += 2;
1028 break;
1029 default: /* Special operand. */
1030 adj_opcode = op_code - lh.opcode_base;
1031 address += (adj_opcode / lh.line_range)
1032 * lh.minimum_instruction_length;
1033 line += lh.line_base + (adj_opcode % lh.line_range);
1034 /* Append row to matrix using current values. */
1035 add_line_info (table, address, filename, line, column, 0);
1036 basic_block = 1;
1037 if (need_low_pc)
1038 {
1039 need_low_pc = 0;
1040 low_pc = address;
1041 }
1042 }
1043 }
1044 }
1045
1046 return table;
1047 }
1048
1049 /* If ADDR is within TABLE set the output parameters and return true,
1050 otherwise return false. The output parameters, FILENAME_PTR and
1051 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1052
1053 static boolean
1054 lookup_address_in_line_info_table (table,
1055 addr,
1056 filename_ptr,
1057 linenumber_ptr)
1058 struct line_info_table* table;
1059 bfd_vma addr;
1060 const char **filename_ptr;
1061 unsigned int *linenumber_ptr;
1062 {
1063 struct line_info* next_line = table->last_line;
1064 struct line_info* each_line;
1065
1066 if (!next_line)
1067 return false;
1068
1069 each_line = next_line->prev_line;
1070
1071 while (each_line && next_line)
1072 {
1073 if (!each_line->end_sequence
1074 && addr >= each_line->address && addr < next_line->address)
1075 {
1076 *filename_ptr = each_line->filename;
1077 *linenumber_ptr = each_line->line;
1078 return true;
1079 }
1080 next_line = each_line;
1081 each_line = each_line->prev_line;
1082 }
1083
1084 return false;
1085 }
1086
1087 /* Function table functions. */
1088
1089 struct funcinfo
1090 {
1091 struct funcinfo *prev_func;
1092 char* name;
1093 bfd_vma low;
1094 bfd_vma high;
1095 };
1096
1097 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1098
1099 static boolean
1100 lookup_address_in_function_table (table,
1101 addr,
1102 functionname_ptr)
1103 struct funcinfo* table;
1104 bfd_vma addr;
1105 const char **functionname_ptr;
1106 {
1107 struct funcinfo* each_func;
1108
1109 for (each_func = table;
1110 each_func;
1111 each_func = each_func->prev_func)
1112 {
1113 if (addr >= each_func->low && addr < each_func->high)
1114 {
1115 *functionname_ptr = each_func->name;
1116 return true;
1117 }
1118 }
1119
1120 return false;
1121 }
1122
1123 /* DWARF2 Compilation unit functions. */
1124
1125 /* Scan over each die in a comp. unit looking for functions to add
1126 to the function table. */
1127
1128 static boolean
1129 scan_unit_for_functions (unit)
1130 struct comp_unit *unit;
1131 {
1132 bfd *abfd = unit->abfd;
1133 char *info_ptr = unit->first_child_die_ptr;
1134 int nesting_level = 1;
1135
1136 while (nesting_level)
1137 {
1138 unsigned int abbrev_number, bytes_read, i;
1139 struct abbrev_info *abbrev;
1140 struct attribute attr;
1141 struct funcinfo *func;
1142 char* name = 0;
1143
1144 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1145 info_ptr += bytes_read;
1146
1147 if (! abbrev_number)
1148 {
1149 nesting_level--;
1150 continue;
1151 }
1152
1153 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1154 if (! abbrev)
1155 {
1156 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1157 abbrev_number);
1158 bfd_set_error (bfd_error_bad_value);
1159 return false;
1160 }
1161
1162 if (abbrev->tag == DW_TAG_subprogram)
1163 {
1164 func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1165 func->prev_func = unit->function_table;
1166 unit->function_table = func;
1167 }
1168 else
1169 func = NULL;
1170
1171 for (i = 0; i < abbrev->num_attrs; ++i)
1172 {
1173 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1174
1175 if (func)
1176 {
1177 switch (attr.name)
1178 {
1179 case DW_AT_name:
1180
1181 name = DW_STRING (&attr);
1182
1183 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1184 if (func->name == NULL)
1185 func->name = DW_STRING (&attr);
1186 break;
1187
1188 case DW_AT_MIPS_linkage_name:
1189 func->name = DW_STRING (&attr);
1190 break;
1191
1192 case DW_AT_low_pc:
1193 func->low = DW_ADDR (&attr);
1194 break;
1195
1196 case DW_AT_high_pc:
1197 func->high = DW_ADDR (&attr);
1198 break;
1199
1200 default:
1201 break;
1202 }
1203 }
1204 else
1205 {
1206 switch (attr.name)
1207 {
1208 case DW_AT_name:
1209 name = DW_STRING (&attr);
1210 break;
1211
1212 default:
1213 break;
1214 }
1215 }
1216 }
1217
1218 if (abbrev->has_children)
1219 nesting_level++;
1220 }
1221
1222 return true;
1223 }
1224
1225 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1226 includes the compilation unit header that proceeds the DIE's, but
1227 does not include the length field that preceeds each compilation
1228 unit header. END_PTR points one past the end of this comp unit.
1229 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1230 is assumed to be four bytes. Otherwise, it it is the size given.
1231
1232 This routine does not read the whole compilation unit; only enough
1233 to get to the line number information for the compilation unit. */
1234
1235 static struct comp_unit *
1236 parse_comp_unit (abfd, info_ptr, end_ptr, abbrev_length)
1237 bfd* abfd;
1238 char* info_ptr;
1239 char* end_ptr;
1240 unsigned int abbrev_length;
1241 {
1242 struct comp_unit* unit;
1243
1244 unsigned short version;
1245 unsigned int abbrev_offset = 0;
1246 unsigned char addr_size;
1247 struct abbrev_info** abbrevs;
1248
1249 unsigned int abbrev_number, bytes_read, i;
1250 struct abbrev_info *abbrev;
1251 struct attribute attr;
1252
1253 version = read_2_bytes (abfd, info_ptr);
1254 info_ptr += 2;
1255 BFD_ASSERT (abbrev_length == 0
1256 || abbrev_length == 4
1257 || abbrev_length == 8);
1258 if (abbrev_length == 0 || abbrev_length == 4)
1259 abbrev_offset = read_4_bytes (abfd, info_ptr);
1260 else if (abbrev_length == 8)
1261 abbrev_offset = read_8_bytes (abfd, info_ptr);
1262 info_ptr += abbrev_length;
1263 addr_size = read_1_byte (abfd, info_ptr);
1264 info_ptr += 1;
1265
1266 if (version != 2)
1267 {
1268 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1269 bfd_set_error (bfd_error_bad_value);
1270 return 0;
1271 }
1272
1273 if (addr_size > sizeof (bfd_vma))
1274 {
1275 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1276 addr_size,
1277 sizeof (bfd_vma));
1278 bfd_set_error (bfd_error_bad_value);
1279 return 0;
1280 }
1281
1282 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1283 {
1284 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size );
1285 bfd_set_error (bfd_error_bad_value);
1286 return 0;
1287 }
1288
1289 /* Read the abbrevs for this compilation unit into a table. */
1290 abbrevs = read_abbrevs (abfd, abbrev_offset);
1291 if (! abbrevs)
1292 return 0;
1293
1294 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1295 info_ptr += bytes_read;
1296 if (! abbrev_number)
1297 {
1298 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1299 abbrev_number);
1300 bfd_set_error (bfd_error_bad_value);
1301 return 0;
1302 }
1303
1304 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1305 if (! abbrev)
1306 {
1307 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1308 abbrev_number);
1309 bfd_set_error (bfd_error_bad_value);
1310 return 0;
1311 }
1312
1313 unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1314 unit->abfd = abfd;
1315 unit->addr_size = addr_size;
1316 unit->abbrevs = abbrevs;
1317 unit->end_ptr = end_ptr;
1318
1319 for (i = 0; i < abbrev->num_attrs; ++i)
1320 {
1321 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1322
1323 /* Store the data if it is of an attribute we want to keep in a
1324 partial symbol table. */
1325 switch (attr.name)
1326 {
1327 case DW_AT_stmt_list:
1328 unit->stmtlist = 1;
1329 unit->line_offset = DW_UNSND (&attr);
1330 break;
1331
1332 case DW_AT_name:
1333 unit->name = DW_STRING (&attr);
1334 break;
1335
1336 case DW_AT_low_pc:
1337 unit->arange.low = DW_ADDR (&attr);
1338 break;
1339
1340 case DW_AT_high_pc:
1341 unit->arange.high = DW_ADDR (&attr);
1342 break;
1343
1344 case DW_AT_comp_dir:
1345 {
1346 char* comp_dir = DW_STRING (&attr);
1347 if (comp_dir)
1348 {
1349 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1350 directory, get rid of it. */
1351 char *cp = (char*) strchr (comp_dir, ':');
1352
1353 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1354 comp_dir = cp + 1;
1355 }
1356 unit->comp_dir = comp_dir;
1357 break;
1358 }
1359
1360 default:
1361 break;
1362 }
1363 }
1364
1365 unit->first_child_die_ptr = info_ptr;
1366 return unit;
1367 }
1368
1369 /* Return true if UNIT contains the address given by ADDR. */
1370
1371 static boolean
1372 comp_unit_contains_address (unit, addr)
1373 struct comp_unit* unit;
1374 bfd_vma addr;
1375 {
1376 struct arange *arange;
1377
1378 if (unit->error)
1379 return 0;
1380
1381 arange = &unit->arange;
1382 do
1383 {
1384 if (addr >= arange->low && addr < arange->high)
1385 return 1;
1386 arange = arange->next;
1387 }
1388 while (arange);
1389
1390 return 0;
1391 }
1392
1393 /* If UNIT contains ADDR, set the output parameters to the values for
1394 the line containing ADDR. The output parameters, FILENAME_PTR,
1395 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1396 to be filled in.
1397
1398 Return true of UNIT contains ADDR, and no errors were encountered;
1399 false otherwise. */
1400
1401 static boolean
1402 comp_unit_find_nearest_line (unit, addr,
1403 filename_ptr, functionname_ptr, linenumber_ptr)
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 {
1410 boolean line_p;
1411 boolean func_p;
1412
1413 if (unit->error)
1414 return false;
1415
1416 if (! unit->line_table)
1417 {
1418 if (! unit->stmtlist)
1419 {
1420 unit->error = 1;
1421 return false;
1422 }
1423
1424 unit->line_table = decode_line_info (unit);
1425
1426 if (! unit->line_table)
1427 {
1428 unit->error = 1;
1429 return false;
1430 }
1431
1432 if (! scan_unit_for_functions (unit))
1433 {
1434 unit->error = 1;
1435 return false;
1436 }
1437 }
1438
1439 line_p = lookup_address_in_line_info_table (unit->line_table,
1440 addr,
1441 filename_ptr,
1442 linenumber_ptr);
1443 func_p = lookup_address_in_function_table (unit->function_table,
1444 addr,
1445 functionname_ptr);
1446 return line_p || func_p;
1447 }
1448
1449 /* Locate a section in a BFD containing debugging info. The search starts from the
1450 section after AFTER_SEC, or from the first section in the BFD if AFTER_SEC is
1451 NULL. The search works by examining the names of the sections. There are two
1452 permissiable names. The first is .debug_info. This is the standard DWARF2 name.
1453 The second is a prefix .gnu.linkonce.wi. This is a variation on the .debug_info
1454 section which has a checksum describing the contents appended onto the name. This
1455 allows the linker to identify and discard duplicate debugging sections for
1456 different compilation units. */
1457 #define DWARF2_DEBUG_INFO ".debug_info"
1458 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1459
1460 static asection *
1461 find_debug_info (abfd, after_sec)
1462 bfd * abfd;
1463 asection * after_sec;
1464 {
1465 asection * msec;
1466
1467 if (after_sec)
1468 msec = after_sec->next;
1469 else
1470 msec = abfd->sections;
1471
1472 while (msec)
1473 {
1474 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1475 return msec;
1476
1477 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1478 return msec;
1479
1480 msec = msec->next;
1481 }
1482
1483 return NULL;
1484 }
1485
1486 /* The DWARF2 version of find_nearest line. Return true if the line
1487 is found without error. ADDR_SIZE is the number of bytes in the
1488 initial .debug_info length field and in the abbreviation offset.
1489 You may use zero to indicate that the default value should be
1490 used. */
1491
1492 boolean
1493 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1494 filename_ptr, functionname_ptr,
1495 linenumber_ptr,
1496 addr_size)
1497 bfd *abfd;
1498 asection *section;
1499 asymbol **symbols ATTRIBUTE_UNUSED;
1500 bfd_vma offset;
1501 const char **filename_ptr;
1502 const char **functionname_ptr;
1503 unsigned int *linenumber_ptr;
1504 unsigned int addr_size;
1505 {
1506 /* Read each compilation unit from the section .debug_info, and check
1507 to see if it contains the address we are searching for. If yes,
1508 lookup the address, and return the line number info. If no, go
1509 on to the next compilation unit.
1510
1511 We keep a list of all the previously read compilation units, and
1512 a pointer to the next un-read compilation unit. Check the
1513 previously read units before reading more. */
1514 struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info;
1515
1516 /* What address are we looking for? */
1517 bfd_vma addr = offset + section->vma;
1518
1519 struct comp_unit* each;
1520
1521 *filename_ptr = NULL;
1522 *functionname_ptr = NULL;
1523 *linenumber_ptr = 0;
1524
1525 /* The DWARF2 spec says that the initial length field, and the
1526 offset of the abbreviation table, should both be 4-byte values.
1527 However, some compilers do things differently. */
1528 if (addr_size == 0)
1529 addr_size = 4;
1530 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1531
1532 if (! stash)
1533 {
1534 unsigned long total_size;
1535 asection *msec;
1536
1537 stash = elf_tdata (abfd)->dwarf2_find_line_info =
1538 (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
1539 if (! stash)
1540 return false;
1541
1542 msec = find_debug_info (abfd, NULL);
1543 if (! msec)
1544 /* No dwarf2 info. Note that at this point the stash
1545 has been allocated, but contains zeros, this lets
1546 future calls to this function fail quicker. */
1547 return false;
1548
1549 /* There can be more than one DWARF2 info section in a BFD these days.
1550 Read them all in and produce one large stash. We do this in two
1551 passes - in the first pass we just accumulate the section sizes.
1552 In the second pass we read in the section's contents. The allows
1553 us to avoid reallocing the data as we add sections to the stash. */
1554 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1555 total_size += msec->_raw_size;
1556
1557 stash->info_ptr = (char *) bfd_alloc (abfd, total_size);
1558 if (stash->info_ptr == NULL)
1559 return false;
1560
1561 stash->info_ptr_end = stash->info_ptr;
1562
1563 for (msec = find_debug_info (abfd, NULL);
1564 msec;
1565 msec = find_debug_info (abfd, msec))
1566 {
1567 unsigned long size;
1568 unsigned long start;
1569
1570 size = msec->_raw_size;
1571 if (size == 0)
1572 continue;
1573
1574 start = stash->info_ptr_end - stash->info_ptr;
1575
1576 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr + start, 0, size))
1577 continue;
1578
1579 stash->info_ptr_end = stash->info_ptr + start + size;
1580 }
1581
1582 BFD_ASSERT (stash->info_ptr_end = stash->info_ptr + total_size);
1583 }
1584
1585 /* FIXME: There is a problem with the contents of the
1586 .debug_info section. The 'low' and 'high' addresses of the
1587 comp_units are computed by relocs against symbols in the
1588 .text segment. We need these addresses in order to determine
1589 the nearest line number, and so we have to resolve the
1590 relocs. There is a similar problem when the .debug_line
1591 section is processed as well (e.g., there may be relocs
1592 against the operand of the DW_LNE_set_address operator).
1593
1594 Unfortunately getting hold of the reloc information is hard...
1595
1596 For now, this means that disassembling object files (as
1597 opposed to fully executables) does not always work as well as
1598 we would like. */
1599
1600 /* A null info_ptr indicates that there is no dwarf2 info
1601 (or that an error occured while setting up the stash). */
1602 if (! stash->info_ptr)
1603 return false;
1604
1605 /* Check the previously read comp. units first. */
1606 for (each = stash->all_comp_units; each; each = each->next_unit)
1607 if (comp_unit_contains_address (each, addr))
1608 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1609 functionname_ptr, linenumber_ptr);
1610
1611 /* Read each remaining comp. units checking each as they are read. */
1612 while (stash->info_ptr < stash->info_ptr_end)
1613 {
1614 struct comp_unit* each;
1615 bfd_vma length;
1616 boolean found;
1617
1618 if (addr_size == 4)
1619 length = read_4_bytes (abfd, stash->info_ptr);
1620 else
1621 length = read_8_bytes (abfd, stash->info_ptr);
1622 stash->info_ptr += addr_size;
1623
1624 if (length > 0)
1625 {
1626 each = parse_comp_unit (abfd, stash->info_ptr,
1627 stash->info_ptr + length,
1628 addr_size);
1629 stash->info_ptr += length;
1630
1631 if (each)
1632 {
1633 each->next_unit = stash->all_comp_units;
1634 stash->all_comp_units = each;
1635
1636 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1637 compilation units. If we don't have them (i.e.,
1638 unit->high == 0), we need to consult the line info
1639 table to see if a compilation unit contains the given
1640 address. */
1641 if (each->arange.high > 0)
1642 {
1643 if (comp_unit_contains_address (each, addr))
1644 return comp_unit_find_nearest_line (each, addr,
1645 filename_ptr,
1646 functionname_ptr,
1647 linenumber_ptr);
1648 }
1649 else
1650 {
1651 found = comp_unit_find_nearest_line (each, addr,
1652 filename_ptr,
1653 functionname_ptr,
1654 linenumber_ptr);
1655 if (found)
1656 return true;
1657 }
1658 }
1659 }
1660 }
1661
1662 return false;
1663 }
This page took 0.063968 seconds and 4 git commands to generate.