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