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