* elf-bfd.h (struct elf_backend_data): Add gc_mark_dynamic_ref.
[deliverable/binutils-gdb.git] / bfd / dwarf2.c
CommitLineData
252b5132 1/* DWARF 2 support.
818a27ac 2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
f075ee0c 3 2004, 2005 Free Software Foundation, Inc.
252b5132
RH
4
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
7
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
15
e2f6d277 16 This file is part of BFD.
252b5132 17
e2f6d277
NC
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or (at
21 your option) any later version.
252b5132 22
e2f6d277
NC
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
252b5132 27
e2f6d277
NC
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
3e110533 30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132
RH
31
32#include "bfd.h"
33#include "sysdep.h"
34#include "libiberty.h"
35#include "libbfd.h"
36#include "elf-bfd.h"
37#include "elf/dwarf2.h"
38
39/* The data in the .debug_line statement prologue looks like this. */
a092b084 40
252b5132 41struct line_head
a092b084 42{
d03ba2a1 43 bfd_vma total_length;
a092b084 44 unsigned short version;
f46c2da6 45 bfd_vma prologue_length;
a092b084
NC
46 unsigned char minimum_instruction_length;
47 unsigned char default_is_stmt;
48 int line_base;
49 unsigned char line_range;
50 unsigned char opcode_base;
51 unsigned char *standard_opcode_lengths;
52};
53
54/* Attributes have a name and a value. */
55
252b5132 56struct attribute
a092b084
NC
57{
58 enum dwarf_attribute name;
59 enum dwarf_form form;
60 union
252b5132 61 {
a092b084
NC
62 char *str;
63 struct dwarf_block *blk;
8ce8c090
AM
64 bfd_uint64_t val;
65 bfd_int64_t sval;
a092b084
NC
66 }
67 u;
68};
69
98591c73 70/* Blocks are a bunch of untyped bytes. */
252b5132 71struct dwarf_block
a092b084
NC
72{
73 unsigned int size;
f075ee0c 74 bfd_byte *data;
a092b084 75};
252b5132 76
a092b084
NC
77struct dwarf2_debug
78{
79 /* A list of all previously read comp_units. */
f075ee0c 80 struct comp_unit *all_comp_units;
252b5132
RH
81
82 /* The next unread compilation unit within the .debug_info section.
83 Zero indicates that the .debug_info section has not been loaded
a092b084 84 into a buffer yet. */
f075ee0c 85 bfd_byte *info_ptr;
252b5132 86
a092b084 87 /* Pointer to the end of the .debug_info section memory buffer. */
f075ee0c 88 bfd_byte *info_ptr_end;
252b5132 89
f2363ce5
AO
90 /* Pointer to the section and address of the beginning of the
91 section. */
f075ee0c
AM
92 asection *sec;
93 bfd_byte *sec_info_ptr;
f2363ce5
AO
94
95 /* Pointer to the symbol table. */
f075ee0c 96 asymbol **syms;
f2363ce5 97
a092b084 98 /* Pointer to the .debug_abbrev section loaded into memory. */
f075ee0c 99 bfd_byte *dwarf_abbrev_buffer;
252b5132 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. */
f075ee0c 105 bfd_byte *dwarf_line_buffer;
ccdb16fc
JW
106
107 /* Length of the loaded .debug_line section. */
108 unsigned long dwarf_line_size;
d03ba2a1
JJ
109
110 /* Pointer to the .debug_str section loaded into memory. */
f075ee0c 111 bfd_byte *dwarf_str_buffer;
d03ba2a1
JJ
112
113 /* Length of the loaded .debug_str section. */
114 unsigned long dwarf_str_size;
a13afe8e
FF
115
116 /* Pointer to the .debug_ranges section loaded into memory. */
117 bfd_byte *dwarf_ranges_buffer;
118
119 /* Length of the loaded .debug_ranges section. */
120 unsigned long dwarf_ranges_size;
4ab527b0
FF
121
122 /* If the most recent call to bfd_find_nearest_line was given an
123 address in an inlined function, preserve a pointer into the
124 calling chain for subsequent calls to bfd_find_inliner_info to
125 use. */
126 struct funcinfo *inliner_chain;
252b5132
RH
127};
128
a092b084
NC
129struct arange
130{
f623be2b
RH
131 struct arange *next;
132 bfd_vma low;
133 bfd_vma high;
134};
252b5132 135
252b5132 136/* A minimal decoding of DWARF2 compilation units. We only decode
a092b084 137 what's needed to get to the line number information. */
252b5132 138
a092b084
NC
139struct comp_unit
140{
141 /* Chain the previously read compilation units. */
f075ee0c 142 struct comp_unit *next_unit;
252b5132 143
2ae727ad 144 /* Keep the bfd convenient (for memory allocation). */
f075ee0c 145 bfd *abfd;
252b5132 146
09a9d560 147 /* The lowest and highest addresses contained in this compilation
a092b084 148 unit as specified in the compilation unit header. */
f623be2b 149 struct arange arange;
252b5132 150
a092b084 151 /* The DW_AT_name attribute (for error messages). */
f075ee0c 152 char *name;
252b5132 153
a092b084 154 /* The abbrev hash table. */
f075ee0c 155 struct abbrev_info **abbrevs;
252b5132 156
a092b084 157 /* Note that an error was found by comp_unit_find_nearest_line. */
252b5132
RH
158 int error;
159
a092b084 160 /* The DW_AT_comp_dir attribute. */
f075ee0c 161 char *comp_dir;
252b5132 162
b34976b6 163 /* TRUE if there is a line number table associated with this comp. unit. */
252b5132 164 int stmtlist;
98591c73 165
c0c28ab8
L
166 /* Pointer to the current comp_unit so that we can find a given entry
167 by its reference. */
f075ee0c 168 bfd_byte *info_ptr_unit;
c0c28ab8 169
a092b084 170 /* The offset into .debug_line of the line number table. */
252b5132
RH
171 unsigned long line_offset;
172
a092b084 173 /* Pointer to the first child die for the comp unit. */
f075ee0c 174 bfd_byte *first_child_die_ptr;
252b5132 175
a092b084 176 /* The end of the comp unit. */
f075ee0c 177 bfd_byte *end_ptr;
252b5132 178
a092b084 179 /* The decoded line number, NULL if not yet decoded. */
f075ee0c 180 struct line_info_table *line_table;
252b5132 181
a092b084 182 /* A list of the functions found in this comp. unit. */
f075ee0c 183 struct funcinfo *function_table;
252b5132 184
5420f73d
L
185 /* A list of the variables found in this comp. unit. */
186 struct varinfo *variable_table;
187
d03ba2a1
JJ
188 /* Pointer to dwarf2_debug structure. */
189 struct dwarf2_debug *stash;
190
a092b084 191 /* Address size for this unit - from unit header. */
252b5132 192 unsigned char addr_size;
d03ba2a1
JJ
193
194 /* Offset size for this unit - from unit header. */
195 unsigned char offset_size;
a13afe8e
FF
196
197 /* Base address for this unit - from DW_AT_low_pc attribute of
198 DW_TAG_compile_unit DIE */
199 bfd_vma base_address;
252b5132
RH
200};
201
a7b97311
AM
202/* This data structure holds the information of an abbrev. */
203struct abbrev_info
204{
205 unsigned int number; /* Number identifying abbrev. */
206 enum dwarf_tag tag; /* DWARF tag. */
207 int has_children; /* Boolean. */
208 unsigned int num_attrs; /* Number of attributes. */
209 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
210 struct abbrev_info *next; /* Next in chain. */
211};
212
213struct attr_abbrev
214{
215 enum dwarf_attribute name;
216 enum dwarf_form form;
217};
218
219#ifndef ABBREV_HASH_SIZE
220#define ABBREV_HASH_SIZE 121
221#endif
222#ifndef ATTR_ALLOC_CHUNK
223#define ATTR_ALLOC_CHUNK 4
224#endif
225
98591c73
KH
226/* VERBATIM
227 The following function up to the END VERBATIM mark are
a092b084 228 copied directly from dwarf2read.c. */
252b5132 229
a092b084 230/* Read dwarf information from a buffer. */
252b5132
RH
231
232static unsigned int
f075ee0c 233read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 234{
818a27ac 235 return bfd_get_8 (abfd, buf);
252b5132
RH
236}
237
238static int
f075ee0c 239read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 240{
818a27ac 241 return bfd_get_signed_8 (abfd, buf);
252b5132
RH
242}
243
244static unsigned int
f075ee0c 245read_2_bytes (bfd *abfd, bfd_byte *buf)
252b5132 246{
818a27ac 247 return bfd_get_16 (abfd, buf);
252b5132
RH
248}
249
252b5132 250static unsigned int
f075ee0c 251read_4_bytes (bfd *abfd, bfd_byte *buf)
252b5132 252{
818a27ac 253 return bfd_get_32 (abfd, buf);
252b5132
RH
254}
255
8ce8c090 256static bfd_uint64_t
f075ee0c 257read_8_bytes (bfd *abfd, bfd_byte *buf)
252b5132 258{
818a27ac 259 return bfd_get_64 (abfd, buf);
252b5132
RH
260}
261
f075ee0c 262static bfd_byte *
818a27ac 263read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 264 bfd_byte *buf,
818a27ac 265 unsigned int size ATTRIBUTE_UNUSED)
252b5132
RH
266{
267 /* If the size of a host char is 8 bits, we can return a pointer
268 to the buffer, otherwise we have to copy the data to a buffer
269 allocated on the temporary obstack. */
270 return buf;
271}
272
273static char *
818a27ac 274read_string (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 275 bfd_byte *buf,
818a27ac 276 unsigned int *bytes_read_ptr)
252b5132 277{
d03ba2a1 278 /* Return a pointer to the embedded string. */
f075ee0c
AM
279 char *str = (char *) buf;
280 if (*str == '\0')
252b5132
RH
281 {
282 *bytes_read_ptr = 1;
283 return NULL;
284 }
98591c73 285
f075ee0c
AM
286 *bytes_read_ptr = strlen (str) + 1;
287 return str;
252b5132
RH
288}
289
d03ba2a1 290static char *
818a27ac 291read_indirect_string (struct comp_unit* unit,
f075ee0c 292 bfd_byte *buf,
818a27ac 293 unsigned int *bytes_read_ptr)
d03ba2a1 294{
8ce8c090 295 bfd_uint64_t offset;
d03ba2a1 296 struct dwarf2_debug *stash = unit->stash;
f075ee0c 297 char *str;
d03ba2a1
JJ
298
299 if (unit->offset_size == 4)
300 offset = read_4_bytes (unit->abfd, buf);
301 else
302 offset = read_8_bytes (unit->abfd, buf);
303 *bytes_read_ptr = unit->offset_size;
304
305 if (! stash->dwarf_str_buffer)
306 {
307 asection *msec;
308 bfd *abfd = unit->abfd;
eea6121a 309 bfd_size_type sz;
d03ba2a1
JJ
310
311 msec = bfd_get_section_by_name (abfd, ".debug_str");
312 if (! msec)
313 {
314 (*_bfd_error_handler)
315 (_("Dwarf Error: Can't find .debug_str section."));
316 bfd_set_error (bfd_error_bad_value);
317 return NULL;
318 }
319
eea6121a
AM
320 sz = msec->rawsize ? msec->rawsize : msec->size;
321 stash->dwarf_str_size = sz;
322 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
36868d45 323 if (! stash->dwarf_str_buffer)
d03ba2a1
JJ
324 return NULL;
325
9f632188 326 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
eea6121a 327 0, sz))
d03ba2a1
JJ
328 return NULL;
329 }
330
331 if (offset >= stash->dwarf_str_size)
332 {
f46c2da6
AM
333 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
334 (unsigned long) offset, stash->dwarf_str_size);
d03ba2a1
JJ
335 bfd_set_error (bfd_error_bad_value);
336 return NULL;
337 }
338
f075ee0c
AM
339 str = (char *) stash->dwarf_str_buffer + offset;
340 if (*str == '\0')
d03ba2a1 341 return NULL;
f075ee0c 342 return str;
d03ba2a1
JJ
343}
344
252b5132
RH
345/* END VERBATIM */
346
8ce8c090 347static bfd_uint64_t
f075ee0c 348read_address (struct comp_unit *unit, bfd_byte *buf)
252b5132 349{
0af4cd7c
PK
350 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
351
352 if (signed_vma)
353 {
354 switch (unit->addr_size)
355 {
356 case 8:
357 return bfd_get_signed_64 (unit->abfd, buf);
358 case 4:
359 return bfd_get_signed_32 (unit->abfd, buf);
360 case 2:
361 return bfd_get_signed_16 (unit->abfd, buf);
362 default:
363 abort ();
364 }
365 }
366 else
252b5132 367 {
0af4cd7c
PK
368 switch (unit->addr_size)
369 {
370 case 8:
371 return bfd_get_64 (unit->abfd, buf);
372 case 4:
373 return bfd_get_32 (unit->abfd, buf);
374 case 2:
375 return bfd_get_16 (unit->abfd, buf);
376 default:
377 abort ();
378 }
252b5132 379 }
252b5132
RH
380}
381
252b5132
RH
382/* Lookup an abbrev_info structure in the abbrev hash table. */
383
384static struct abbrev_info *
818a27ac 385lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
252b5132
RH
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 }
98591c73 400
252b5132
RH
401 return NULL;
402}
403
404/* In DWARF version 2, the description of the debugging information is
405 stored in a separate .debug_abbrev section. Before we read any
406 dies from a section we read in all abbreviations and install them
407 in a hash table. */
408
409static struct abbrev_info**
8ce8c090 410read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
252b5132
RH
411{
412 struct abbrev_info **abbrevs;
f075ee0c 413 bfd_byte *abbrev_ptr;
252b5132
RH
414 struct abbrev_info *cur_abbrev;
415 unsigned int abbrev_number, bytes_read, abbrev_name;
416 unsigned int abbrev_form, hash_number;
dc810e39 417 bfd_size_type amt;
252b5132
RH
418
419 if (! stash->dwarf_abbrev_buffer)
420 {
421 asection *msec;
422
423 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
424 if (! msec)
425 {
426 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
427 bfd_set_error (bfd_error_bad_value);
428 return 0;
429 }
98591c73 430
eea6121a 431 stash->dwarf_abbrev_size = msec->size;
6e84a906
DJ
432 stash->dwarf_abbrev_buffer
433 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
434 stash->syms);
252b5132
RH
435 if (! stash->dwarf_abbrev_buffer)
436 return 0;
252b5132
RH
437 }
438
f5198f61 439 if (offset >= stash->dwarf_abbrev_size)
252b5132 440 {
f46c2da6
AM
441 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
442 (unsigned long) offset, stash->dwarf_abbrev_size);
252b5132
RH
443 bfd_set_error (bfd_error_bad_value);
444 return 0;
445 }
446
dc810e39 447 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
818a27ac 448 abbrevs = bfd_zalloc (abfd, amt);
252b5132
RH
449
450 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
451 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
452 abbrev_ptr += bytes_read;
453
a092b084 454 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
455 while (abbrev_number)
456 {
dc810e39 457 amt = sizeof (struct abbrev_info);
818a27ac 458 cur_abbrev = bfd_zalloc (abfd, amt);
252b5132 459
a092b084 460 /* Read in abbrev header. */
252b5132 461 cur_abbrev->number = abbrev_number;
d45913a0
DA
462 cur_abbrev->tag = (enum dwarf_tag)
463 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
252b5132
RH
464 abbrev_ptr += bytes_read;
465 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
466 abbrev_ptr += 1;
467
a092b084 468 /* Now read in declarations. */
252b5132
RH
469 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
470 abbrev_ptr += bytes_read;
471 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
472 abbrev_ptr += bytes_read;
98591c73 473
252b5132
RH
474 while (abbrev_name)
475 {
476 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
477 {
35330cce
NC
478 struct attr_abbrev *tmp;
479
dc810e39
AM
480 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
481 amt *= sizeof (struct attr_abbrev);
35330cce
NC
482 tmp = bfd_realloc (cur_abbrev->attrs, amt);
483 if (tmp == NULL)
484 {
485 size_t i;
486
487 for (i = 0; i < ABBREV_HASH_SIZE; i++)
488 {
489 struct abbrev_info *abbrev = abbrevs[i];
490
491 while (abbrev)
492 {
493 free (abbrev->attrs);
494 abbrev = abbrev->next;
495 }
496 }
497 return NULL;
498 }
499 cur_abbrev->attrs = tmp;
252b5132 500 }
98591c73 501
d45913a0
DA
502 cur_abbrev->attrs[cur_abbrev->num_attrs].name
503 = (enum dwarf_attribute) abbrev_name;
504 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
505 = (enum dwarf_form) abbrev_form;
252b5132
RH
506 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
507 abbrev_ptr += bytes_read;
508 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
509 abbrev_ptr += bytes_read;
510 }
511
512 hash_number = abbrev_number % ABBREV_HASH_SIZE;
513 cur_abbrev->next = abbrevs[hash_number];
514 abbrevs[hash_number] = cur_abbrev;
515
516 /* Get next abbreviation.
e82ce529 517 Under Irix6 the abbreviations for a compilation unit are not
252b5132
RH
518 always properly terminated with an abbrev number of 0.
519 Exit loop if we encounter an abbreviation which we have
520 already read (which means we are about to read the abbreviations
521 for the next compile unit) or if the end of the abbreviation
522 table is reached. */
523 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
524 >= stash->dwarf_abbrev_size)
525 break;
526 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
527 abbrev_ptr += bytes_read;
528 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
529 break;
530 }
531
532 return abbrevs;
533}
534
cf716c56 535/* Read an attribute value described by an attribute form. */
252b5132 536
f075ee0c 537static bfd_byte *
818a27ac
AM
538read_attribute_value (struct attribute *attr,
539 unsigned form,
540 struct comp_unit *unit,
f075ee0c 541 bfd_byte *info_ptr)
252b5132
RH
542{
543 bfd *abfd = unit->abfd;
544 unsigned int bytes_read;
545 struct dwarf_block *blk;
dc810e39 546 bfd_size_type amt;
252b5132 547
d45913a0 548 attr->form = (enum dwarf_form) form;
98591c73 549
cf716c56 550 switch (form)
252b5132
RH
551 {
552 case DW_FORM_addr:
0cc1cf99 553 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
252b5132 554 case DW_FORM_ref_addr:
482e2e37 555 attr->u.val = read_address (unit, info_ptr);
252b5132
RH
556 info_ptr += unit->addr_size;
557 break;
558 case DW_FORM_block2:
dc810e39 559 amt = sizeof (struct dwarf_block);
818a27ac 560 blk = bfd_alloc (abfd, amt);
252b5132
RH
561 blk->size = read_2_bytes (abfd, info_ptr);
562 info_ptr += 2;
563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
564 info_ptr += blk->size;
482e2e37 565 attr->u.blk = blk;
252b5132
RH
566 break;
567 case DW_FORM_block4:
dc810e39 568 amt = sizeof (struct dwarf_block);
818a27ac 569 blk = bfd_alloc (abfd, amt);
252b5132
RH
570 blk->size = read_4_bytes (abfd, info_ptr);
571 info_ptr += 4;
572 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
573 info_ptr += blk->size;
482e2e37 574 attr->u.blk = blk;
252b5132
RH
575 break;
576 case DW_FORM_data2:
482e2e37 577 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
578 info_ptr += 2;
579 break;
580 case DW_FORM_data4:
482e2e37 581 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
582 info_ptr += 4;
583 break;
584 case DW_FORM_data8:
482e2e37 585 attr->u.val = read_8_bytes (abfd, info_ptr);
252b5132
RH
586 info_ptr += 8;
587 break;
588 case DW_FORM_string:
482e2e37 589 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
252b5132
RH
590 info_ptr += bytes_read;
591 break;
d03ba2a1 592 case DW_FORM_strp:
482e2e37 593 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
d03ba2a1
JJ
594 info_ptr += bytes_read;
595 break;
252b5132 596 case DW_FORM_block:
dc810e39 597 amt = sizeof (struct dwarf_block);
818a27ac 598 blk = bfd_alloc (abfd, amt);
252b5132
RH
599 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
600 info_ptr += bytes_read;
601 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
602 info_ptr += blk->size;
482e2e37 603 attr->u.blk = blk;
252b5132
RH
604 break;
605 case DW_FORM_block1:
dc810e39 606 amt = sizeof (struct dwarf_block);
818a27ac 607 blk = bfd_alloc (abfd, amt);
252b5132
RH
608 blk->size = read_1_byte (abfd, info_ptr);
609 info_ptr += 1;
610 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
611 info_ptr += blk->size;
482e2e37 612 attr->u.blk = blk;
252b5132
RH
613 break;
614 case DW_FORM_data1:
482e2e37 615 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
616 info_ptr += 1;
617 break;
618 case DW_FORM_flag:
482e2e37 619 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
620 info_ptr += 1;
621 break;
622 case DW_FORM_sdata:
482e2e37 623 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
624 info_ptr += bytes_read;
625 break;
626 case DW_FORM_udata:
482e2e37 627 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
628 info_ptr += bytes_read;
629 break;
630 case DW_FORM_ref1:
482e2e37 631 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
632 info_ptr += 1;
633 break;
634 case DW_FORM_ref2:
482e2e37 635 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
636 info_ptr += 2;
637 break;
638 case DW_FORM_ref4:
482e2e37 639 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
640 info_ptr += 4;
641 break;
81edd86d 642 case DW_FORM_ref8:
482e2e37 643 attr->u.val = read_8_bytes (abfd, info_ptr);
81edd86d
MM
644 info_ptr += 8;
645 break;
252b5132 646 case DW_FORM_ref_udata:
482e2e37 647 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
648 info_ptr += bytes_read;
649 break;
252b5132 650 case DW_FORM_indirect:
cf716c56
RH
651 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
652 info_ptr += bytes_read;
653 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
654 break;
252b5132 655 default:
f46c2da6 656 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
cf716c56 657 form);
252b5132
RH
658 bfd_set_error (bfd_error_bad_value);
659 }
660 return info_ptr;
661}
662
cf716c56
RH
663/* Read an attribute described by an abbreviated attribute. */
664
f075ee0c 665static bfd_byte *
818a27ac
AM
666read_attribute (struct attribute *attr,
667 struct attr_abbrev *abbrev,
668 struct comp_unit *unit,
f075ee0c 669 bfd_byte *info_ptr)
cf716c56
RH
670{
671 attr->name = abbrev->name;
672 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
673 return info_ptr;
674}
675
a092b084 676/* Source line information table routines. */
252b5132
RH
677
678#define FILE_ALLOC_CHUNK 5
679#define DIR_ALLOC_CHUNK 5
680
a092b084
NC
681struct line_info
682{
252b5132 683 struct line_info* prev_line;
252b5132 684 bfd_vma address;
f075ee0c 685 char *filename;
252b5132
RH
686 unsigned int line;
687 unsigned int column;
a092b084 688 int end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
689};
690
a092b084
NC
691struct fileinfo
692{
252b5132
RH
693 char *name;
694 unsigned int dir;
695 unsigned int time;
696 unsigned int size;
697};
698
a092b084
NC
699struct line_info_table
700{
252b5132 701 bfd* abfd;
252b5132
RH
702 unsigned int num_files;
703 unsigned int num_dirs;
f075ee0c
AM
704 char *comp_dir;
705 char **dirs;
252b5132 706 struct fileinfo* files;
e82ce529
AM
707 struct line_info* last_line; /* largest VMA */
708 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
252b5132
RH
709};
710
4ab527b0
FF
711/* Remember some information about each function. If the function is
712 inlined (DW_TAG_inlined_subroutine) it may have two additional
713 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
714 source code location where this function was inlined. */
715
1ee24f27
DJ
716struct funcinfo
717{
4ab527b0
FF
718 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
719 struct funcinfo *caller_func; /* Pointer to function one scope higher */
720 char *caller_file; /* Source location file name where caller_func inlines this func */
721 int caller_line; /* Source location line number where caller_func inlines this func */
5420f73d
L
722 char *file; /* Source location file name */
723 int line; /* Source location line number */
4ab527b0 724 int tag;
f075ee0c 725 char *name;
a13afe8e 726 struct arange arange;
5420f73d
L
727 asection *sec; /* Where the symbol is defined */
728};
729
730struct varinfo
731{
732 /* Pointer to previous variable in list of all variables */
733 struct varinfo *prev_var;
734 /* Source location file name */
735 char *file;
736 /* Source location line number */
737 int line;
738 int tag;
739 char *name;
5cf2e3f0 740 bfd_vma addr;
5420f73d
L
741 /* Where the symbol is defined */
742 asection *sec;
743 /* Is this a stack variable? */
744 unsigned int stack: 1;
1ee24f27
DJ
745};
746
af3ef9fe
NC
747/* Adds a new entry to the line_info list in the line_info_table, ensuring
748 that the list is sorted. Note that the line_info list is sorted from
749 highest to lowest VMA (with possible duplicates); that is,
750 line_info->prev_line always accesses an equal or smaller VMA. */
751
98591c73 752static void
818a27ac
AM
753add_line_info (struct line_info_table *table,
754 bfd_vma address,
755 char *filename,
756 unsigned int line,
757 unsigned int column,
758 int end_sequence)
252b5132 759{
dc810e39 760 bfd_size_type amt = sizeof (struct line_info);
818a27ac 761 struct line_info* info = bfd_alloc (table->abfd, amt);
252b5132 762
e82ce529
AM
763 /* Find the correct location for 'info'. Normally we will receive
764 new line_info data 1) in order and 2) with increasing VMAs.
765 However some compilers break the rules (cf. decode_line_info) and
766 so we include some heuristics for quickly finding the correct
767 location for 'info'. In particular, these heuristics optimize for
768 the common case in which the VMA sequence that we receive is a
769 list of locally sorted VMAs such as
770 p...z a...j (where a < j < p < z)
252b5132 771
e82ce529
AM
772 Note: table->lcl_head is used to head an *actual* or *possible*
773 sequence within the list (such as a...j) that is not directly
774 headed by table->last_line
775
776 Note: we may receive duplicate entries from 'decode_line_info'. */
777
778 while (1)
779 if (!table->last_line
780 || address >= table->last_line->address)
781 {
782 /* Normal case: add 'info' to the beginning of the list */
783 info->prev_line = table->last_line;
784 table->last_line = info;
785
786 /* lcl_head: initialize to head a *possible* sequence at the end. */
787 if (!table->lcl_head)
788 table->lcl_head = info;
789 break;
790 }
791 else if (!table->lcl_head->prev_line
792 && table->lcl_head->address > address)
793 {
794 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
795 list and 2) the head of 'info'. */
796 info->prev_line = NULL;
797 table->lcl_head->prev_line = info;
798 break;
799 }
800 else if (table->lcl_head->prev_line
801 && table->lcl_head->address > address
802 && address >= table->lcl_head->prev_line->address)
803 {
804 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
805 list and 2) the head of 'info'. */
806 info->prev_line = table->lcl_head->prev_line;
807 table->lcl_head->prev_line = info;
808 break;
809 }
810 else
811 {
812 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
813 heads for 'info'. Reset 'lcl_head' and repeat. */
814 struct line_info* li2 = table->last_line; /* always non-NULL */
815 struct line_info* li1 = li2->prev_line;
816
817 while (li1)
818 {
819 if (li2->address > address && address >= li1->address)
820 break;
821
822 li2 = li1; /* always non-NULL */
823 li1 = li1->prev_line;
824 }
825 table->lcl_head = li2;
826 }
827
828 /* Set member data of 'info'. */
252b5132 829 info->address = address;
252b5132
RH
830 info->line = line;
831 info->column = column;
159002ff 832 info->end_sequence = end_sequence;
d63fd5d1 833
eb61d2d6 834 if (filename && filename[0])
d63fd5d1 835 {
eb61d2d6 836 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
d63fd5d1
NC
837 if (info->filename)
838 strcpy (info->filename, filename);
839 }
840 else
841 info->filename = NULL;
252b5132
RH
842}
843
5ed6aba4 844/* Extract a fully qualified filename from a line info table.
af3ef9fe
NC
845 The returned string has been malloc'ed and it is the caller's
846 responsibility to free it. */
5ed6aba4 847
a092b084 848static char *
818a27ac 849concat_filename (struct line_info_table *table, unsigned int file)
252b5132 850{
f075ee0c 851 char *filename;
159002ff
RH
852
853 if (file - 1 >= table->num_files)
854 {
dcdea4f4
AM
855 (*_bfd_error_handler)
856 (_("Dwarf Error: mangled line number section (bad file number)."));
af3ef9fe 857 return strdup ("<unknown>");
159002ff
RH
858 }
859
860 filename = table->files[file - 1].name;
5ed6aba4 861
af3ef9fe 862 if (! IS_ABSOLUTE_PATH (filename))
252b5132 863 {
f075ee0c 864 char *dirname = (table->files[file - 1].dir
252b5132
RH
865 ? table->dirs[table->files[file - 1].dir - 1]
866 : table->comp_dir);
0dafd5f6 867
af3ef9fe
NC
868 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
869 The best we can do is return the filename part. */
870 if (dirname != NULL)
871 {
872 unsigned int len = strlen (dirname) + strlen (filename) + 2;
873 char * name;
874
875 name = bfd_malloc (len);
876 if (name)
877 sprintf (name, "%s/%s", dirname, filename);
878 return name;
879 }
252b5132 880 }
af3ef9fe
NC
881
882 return strdup (filename);
252b5132
RH
883}
884
f623be2b 885static void
a13afe8e 886arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
f623be2b
RH
887{
888 struct arange *arange;
889
a13afe8e
FF
890 /* If the first arange is empty, use it. */
891 if (first_arange->high == 0)
892 {
893 first_arange->low = low_pc;
894 first_arange->high = high_pc;
895 return;
896 }
98591c73 897
a13afe8e
FF
898 /* Next see if we can cheaply extend an existing range. */
899 arange = first_arange;
f623be2b
RH
900 do
901 {
902 if (low_pc == arange->high)
903 {
904 arange->high = high_pc;
905 return;
906 }
907 if (high_pc == arange->low)
908 {
909 arange->low = low_pc;
910 return;
911 }
912 arange = arange->next;
913 }
914 while (arange);
915
a13afe8e
FF
916 /* Need to allocate a new arange and insert it into the arange list.
917 Order isn't significant, so just insert after the first arange. */
918 arange = bfd_zalloc (abfd, sizeof (*arange));
f623be2b
RH
919 arange->low = low_pc;
920 arange->high = high_pc;
a13afe8e
FF
921 arange->next = first_arange->next;
922 first_arange->next = arange;
f623be2b
RH
923}
924
a092b084 925/* Decode the line number information for UNIT. */
252b5132
RH
926
927static struct line_info_table*
818a27ac 928decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
252b5132
RH
929{
930 bfd *abfd = unit->abfd;
252b5132 931 struct line_info_table* table;
f075ee0c
AM
932 bfd_byte *line_ptr;
933 bfd_byte *line_end;
252b5132 934 struct line_head lh;
d03ba2a1 935 unsigned int i, bytes_read, offset_size;
252b5132
RH
936 char *cur_file, *cur_dir;
937 unsigned char op_code, extended_op, adj_opcode;
dc810e39 938 bfd_size_type amt;
252b5132 939
69dd2e2d 940 if (! stash->dwarf_line_buffer)
252b5132
RH
941 {
942 asection *msec;
252b5132
RH
943
944 msec = bfd_get_section_by_name (abfd, ".debug_line");
945 if (! msec)
946 {
947 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
948 bfd_set_error (bfd_error_bad_value);
949 return 0;
950 }
98591c73 951
eea6121a 952 stash->dwarf_line_size = msec->size;
6e84a906
DJ
953 stash->dwarf_line_buffer
954 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
955 stash->syms);
f623be2b 956 if (! stash->dwarf_line_buffer)
252b5132 957 return 0;
252b5132
RH
958 }
959
6e84a906
DJ
960 /* It is possible to get a bad value for the line_offset. Validate
961 it here so that we won't get a segfault below. */
ccdb16fc
JW
962 if (unit->line_offset >= stash->dwarf_line_size)
963 {
f46c2da6 964 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
ccdb16fc
JW
965 unit->line_offset, stash->dwarf_line_size);
966 bfd_set_error (bfd_error_bad_value);
967 return 0;
968 }
969
dc810e39 970 amt = sizeof (struct line_info_table);
818a27ac 971 table = bfd_alloc (abfd, amt);
252b5132
RH
972 table->abfd = abfd;
973 table->comp_dir = unit->comp_dir;
974
975 table->num_files = 0;
976 table->files = NULL;
977
978 table->num_dirs = 0;
979 table->dirs = NULL;
980
159002ff
RH
981 table->files = NULL;
982 table->last_line = NULL;
e82ce529 983 table->lcl_head = NULL;
159002ff 984
69dd2e2d 985 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 986
a092b084 987 /* Read in the prologue. */
91a4d569
AM
988 lh.total_length = read_4_bytes (abfd, line_ptr);
989 line_ptr += 4;
990 offset_size = 4;
991 if (lh.total_length == 0xffffffff)
dae2dd0d 992 {
dae2dd0d
NC
993 lh.total_length = read_8_bytes (abfd, line_ptr);
994 line_ptr += 8;
995 offset_size = 8;
996 }
91a4d569 997 else if (lh.total_length == 0 && unit->addr_size == 8)
d03ba2a1 998 {
91a4d569
AM
999 /* Handle (non-standard) 64-bit DWARF2 formats. */
1000 lh.total_length = read_4_bytes (abfd, line_ptr);
1001 line_ptr += 4;
d03ba2a1
JJ
1002 offset_size = 8;
1003 }
252b5132
RH
1004 line_end = line_ptr + lh.total_length;
1005 lh.version = read_2_bytes (abfd, line_ptr);
1006 line_ptr += 2;
d03ba2a1
JJ
1007 if (offset_size == 4)
1008 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1009 else
1010 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1011 line_ptr += offset_size;
252b5132
RH
1012 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1013 line_ptr += 1;
1014 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1015 line_ptr += 1;
1016 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1017 line_ptr += 1;
1018 lh.line_range = read_1_byte (abfd, line_ptr);
1019 line_ptr += 1;
1020 lh.opcode_base = read_1_byte (abfd, line_ptr);
1021 line_ptr += 1;
dc810e39 1022 amt = lh.opcode_base * sizeof (unsigned char);
818a27ac 1023 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
252b5132
RH
1024
1025 lh.standard_opcode_lengths[0] = 1;
98591c73 1026
252b5132
RH
1027 for (i = 1; i < lh.opcode_base; ++i)
1028 {
1029 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1030 line_ptr += 1;
1031 }
1032
a092b084 1033 /* Read directory table. */
252b5132
RH
1034 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1035 {
1036 line_ptr += bytes_read;
98591c73 1037
252b5132
RH
1038 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1039 {
35330cce
NC
1040 char **tmp;
1041
dc810e39
AM
1042 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1043 amt *= sizeof (char *);
35330cce
NC
1044
1045 tmp = bfd_realloc (table->dirs, amt);
1046 if (tmp == NULL)
1047 {
1048 free (table->dirs);
1049 return NULL;
1050 }
1051 table->dirs = tmp;
252b5132 1052 }
98591c73 1053
252b5132
RH
1054 table->dirs[table->num_dirs++] = cur_dir;
1055 }
98591c73 1056
252b5132
RH
1057 line_ptr += bytes_read;
1058
a092b084 1059 /* Read file name table. */
252b5132
RH
1060 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1061 {
1062 line_ptr += bytes_read;
98591c73 1063
252b5132
RH
1064 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1065 {
35330cce
NC
1066 struct fileinfo *tmp;
1067
dc810e39
AM
1068 amt = table->num_files + FILE_ALLOC_CHUNK;
1069 amt *= sizeof (struct fileinfo);
35330cce
NC
1070
1071 tmp = bfd_realloc (table->files, amt);
1072 if (tmp == NULL)
1073 {
1074 free (table->files);
1075 free (table->dirs);
1076 return NULL;
1077 }
1078 table->files = tmp;
252b5132 1079 }
98591c73 1080
252b5132
RH
1081 table->files[table->num_files].name = cur_file;
1082 table->files[table->num_files].dir =
1083 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1084 line_ptr += bytes_read;
1085 table->files[table->num_files].time =
1086 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1087 line_ptr += bytes_read;
1088 table->files[table->num_files].size =
1089 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1090 line_ptr += bytes_read;
1091 table->num_files++;
1092 }
98591c73 1093
252b5132
RH
1094 line_ptr += bytes_read;
1095
1096 /* Read the statement sequences until there's nothing left. */
1097 while (line_ptr < line_end)
1098 {
a092b084 1099 /* State machine registers. */
252b5132 1100 bfd_vma address = 0;
8bfd78b3 1101 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
252b5132
RH
1102 unsigned int line = 1;
1103 unsigned int column = 0;
1104 int is_stmt = lh.default_is_stmt;
1105 int basic_block = 0;
e2f6d277
NC
1106 int end_sequence = 0;
1107 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
e82ce529
AM
1108 compilers generate address sequences that are wildly out of
1109 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1110 for ia64-Linux). Thus, to determine the low and high
1111 address, we must compare on every DW_LNS_copy, etc. */
75758e9d 1112 bfd_vma low_pc = (bfd_vma) -1;
e2f6d277 1113 bfd_vma high_pc = 0;
252b5132 1114
a092b084 1115 /* Decode the table. */
252b5132
RH
1116 while (! end_sequence)
1117 {
1118 op_code = read_1_byte (abfd, line_ptr);
1119 line_ptr += 1;
98591c73 1120
1a509dcc 1121 if (op_code >= lh.opcode_base)
e2f6d277
NC
1122 {
1123 /* Special operand. */
1a509dcc
GK
1124 adj_opcode = op_code - lh.opcode_base;
1125 address += (adj_opcode / lh.line_range)
1126 * lh.minimum_instruction_length;
1127 line += lh.line_base + (adj_opcode % lh.line_range);
1128 /* Append row to matrix using current values. */
1129 add_line_info (table, address, filename, line, column, 0);
1130 basic_block = 1;
75758e9d
AM
1131 if (address < low_pc)
1132 low_pc = address;
e2f6d277
NC
1133 if (address > high_pc)
1134 high_pc = address;
1a509dcc
GK
1135 }
1136 else switch (op_code)
252b5132
RH
1137 {
1138 case DW_LNS_extended_op:
e2f6d277
NC
1139 /* Ignore length. */
1140 line_ptr += 1;
252b5132
RH
1141 extended_op = read_1_byte (abfd, line_ptr);
1142 line_ptr += 1;
e2f6d277 1143
252b5132
RH
1144 switch (extended_op)
1145 {
1146 case DW_LNE_end_sequence:
1147 end_sequence = 1;
f623be2b
RH
1148 add_line_info (table, address, filename, line, column,
1149 end_sequence);
75758e9d
AM
1150 if (address < low_pc)
1151 low_pc = address;
e2f6d277
NC
1152 if (address > high_pc)
1153 high_pc = address;
a13afe8e 1154 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
252b5132
RH
1155 break;
1156 case DW_LNE_set_address:
1157 address = read_address (unit, line_ptr);
1158 line_ptr += unit->addr_size;
1159 break;
1160 case DW_LNE_define_file:
1161 cur_file = read_string (abfd, line_ptr, &bytes_read);
1162 line_ptr += bytes_read;
1163 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1164 {
35330cce
NC
1165 struct fileinfo *tmp;
1166
dc810e39
AM
1167 amt = table->num_files + FILE_ALLOC_CHUNK;
1168 amt *= sizeof (struct fileinfo);
35330cce
NC
1169 tmp = bfd_realloc (table->files, amt);
1170 if (tmp == NULL)
1171 {
1172 free (table->files);
1173 free (table->dirs);
1174 free (filename);
1175 return NULL;
1176 }
1177 table->files = tmp;
252b5132
RH
1178 }
1179 table->files[table->num_files].name = cur_file;
1180 table->files[table->num_files].dir =
1181 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1182 line_ptr += bytes_read;
1183 table->files[table->num_files].time =
1184 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1185 line_ptr += bytes_read;
1186 table->files[table->num_files].size =
1187 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1188 line_ptr += bytes_read;
1189 table->num_files++;
1190 break;
1191 default:
1192 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1193 bfd_set_error (bfd_error_bad_value);
35330cce
NC
1194 free (filename);
1195 free (table->files);
1196 free (table->dirs);
1197 return NULL;
252b5132
RH
1198 }
1199 break;
1200 case DW_LNS_copy:
159002ff 1201 add_line_info (table, address, filename, line, column, 0);
252b5132 1202 basic_block = 0;
75758e9d
AM
1203 if (address < low_pc)
1204 low_pc = address;
e2f6d277
NC
1205 if (address > high_pc)
1206 high_pc = address;
252b5132
RH
1207 break;
1208 case DW_LNS_advance_pc:
1209 address += lh.minimum_instruction_length
1210 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1211 line_ptr += bytes_read;
1212 break;
1213 case DW_LNS_advance_line:
1214 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1215 line_ptr += bytes_read;
1216 break;
1217 case DW_LNS_set_file:
1218 {
1219 unsigned int file;
1220
e2f6d277
NC
1221 /* The file and directory tables are 0
1222 based, the references are 1 based. */
252b5132
RH
1223 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1224 line_ptr += bytes_read;
af3ef9fe
NC
1225 if (filename)
1226 free (filename);
252b5132
RH
1227 filename = concat_filename (table, file);
1228 break;
1229 }
1230 case DW_LNS_set_column:
1231 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1232 line_ptr += bytes_read;
1233 break;
1234 case DW_LNS_negate_stmt:
1235 is_stmt = (!is_stmt);
1236 break;
1237 case DW_LNS_set_basic_block:
1238 basic_block = 1;
1239 break;
1240 case DW_LNS_const_add_pc:
159002ff
RH
1241 address += lh.minimum_instruction_length
1242 * ((255 - lh.opcode_base) / lh.line_range);
252b5132
RH
1243 break;
1244 case DW_LNS_fixed_advance_pc:
1245 address += read_2_bytes (abfd, line_ptr);
1246 line_ptr += 2;
1247 break;
1a509dcc 1248 default:
e2f6d277 1249 {
1a509dcc 1250 int i;
5ed6aba4 1251
e2f6d277 1252 /* Unknown standard opcode, ignore it. */
1a509dcc
GK
1253 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1254 {
1255 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1256 line_ptr += bytes_read;
1257 }
1258 }
252b5132
RH
1259 }
1260 }
5ed6aba4 1261
af3ef9fe
NC
1262 if (filename)
1263 free (filename);
252b5132
RH
1264 }
1265
1266 return table;
1267}
1268
b34976b6
AM
1269/* If ADDR is within TABLE set the output parameters and return TRUE,
1270 otherwise return FALSE. The output parameters, FILENAME_PTR and
a092b084 1271 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132 1272
b34976b6 1273static bfd_boolean
818a27ac
AM
1274lookup_address_in_line_info_table (struct line_info_table *table,
1275 bfd_vma addr,
1276 struct funcinfo *function,
1277 const char **filename_ptr,
1278 unsigned int *linenumber_ptr)
252b5132 1279{
e82ce529 1280 /* Note: table->last_line should be a descendingly sorted list. */
159002ff 1281 struct line_info* next_line = table->last_line;
e82ce529
AM
1282 struct line_info* each_line = NULL;
1283 *filename_ptr = NULL;
98591c73 1284
159002ff 1285 if (!next_line)
b34976b6 1286 return FALSE;
159002ff
RH
1287
1288 each_line = next_line->prev_line;
1289
e82ce529
AM
1290 /* Check for large addresses */
1291 if (addr > next_line->address)
1292 each_line = NULL; /* ensure we skip over the normal case */
1293
1294 /* Normal case: search the list; save */
159002ff 1295 while (each_line && next_line)
252b5132 1296 {
e82ce529
AM
1297 /* If we have an address match, save this info. This allows us
1298 to return as good as results as possible for strange debugging
1299 info. */
b34976b6 1300 bfd_boolean addr_match = FALSE;
a13afe8e 1301 if (each_line->address <= addr && addr < next_line->address)
252b5132 1302 {
b34976b6 1303 addr_match = TRUE;
e82ce529 1304
1ee24f27
DJ
1305 /* If this line appears to span functions, and addr is in the
1306 later function, return the first line of that function instead
1307 of the last line of the earlier one. This check is for GCC
1308 2.95, which emits the first line number for a function late. */
a13afe8e
FF
1309
1310 if (function != NULL)
1ee24f27 1311 {
a13afe8e
FF
1312 bfd_vma lowest_pc;
1313 struct arange *arange;
1314
1315 /* Find the lowest address in the function's range list */
1316 lowest_pc = function->arange.low;
1317 for (arange = &function->arange;
1318 arange;
1319 arange = arange->next)
1320 {
1321 if (function->arange.low < lowest_pc)
1322 lowest_pc = function->arange.low;
1323 }
1324 /* Check for spanning function and set outgoing line info */
1325 if (addr >= lowest_pc
1326 && each_line->address < lowest_pc
1327 && next_line->address > lowest_pc)
1328 {
1329 *filename_ptr = next_line->filename;
1330 *linenumber_ptr = next_line->line;
1331 }
1332 else
1333 {
1334 *filename_ptr = each_line->filename;
1335 *linenumber_ptr = each_line->line;
1336 }
1ee24f27 1337 }
6bd00c5d
L
1338 else
1339 {
1340 *filename_ptr = each_line->filename;
1341 *linenumber_ptr = each_line->line;
1342 }
252b5132 1343 }
e82ce529
AM
1344
1345 if (addr_match && !each_line->end_sequence)
b34976b6 1346 return TRUE; /* we have definitely found what we want */
e82ce529 1347
159002ff
RH
1348 next_line = each_line;
1349 each_line = each_line->prev_line;
252b5132 1350 }
98591c73 1351
e82ce529
AM
1352 /* At this point each_line is NULL but next_line is not. If we found
1353 a candidate end-of-sequence point in the loop above, we can return
1354 that (compatibility with a bug in the Intel compiler); otherwise,
1355 assuming that we found the containing function for this address in
1356 this compilation unit, return the first line we have a number for
1357 (compatibility with GCC 2.95). */
1358 if (*filename_ptr == NULL && function != NULL)
1ee24f27
DJ
1359 {
1360 *filename_ptr = next_line->filename;
1361 *linenumber_ptr = next_line->line;
b34976b6 1362 return TRUE;
1ee24f27
DJ
1363 }
1364
b34976b6 1365 return FALSE;
252b5132 1366}
98591c73 1367
a13afe8e
FF
1368/* Read in the .debug_ranges section for future reference */
1369
1370static bfd_boolean
1371read_debug_ranges (struct comp_unit *unit)
1372{
1373 struct dwarf2_debug *stash = unit->stash;
1374 if (! stash->dwarf_ranges_buffer)
1375 {
1376 bfd *abfd = unit->abfd;
1377 asection *msec;
1378
1379 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1380 if (! msec)
1381 {
1382 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1383 bfd_set_error (bfd_error_bad_value);
1384 return FALSE;
1385 }
1386
1387 stash->dwarf_ranges_size = msec->size;
1388 stash->dwarf_ranges_buffer
1389 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1390 stash->syms);
1391 if (! stash->dwarf_ranges_buffer)
1392 return FALSE;
1393 }
1394 return TRUE;
1395}
1396
a092b084 1397/* Function table functions. */
252b5132 1398
a13afe8e
FF
1399/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1400 Note that we need to find the function that has the smallest
1401 range that contains ADDR, to handle inlined functions without
1402 depending upon them being ordered in TABLE by increasing range. */
252b5132 1403
b34976b6 1404static bfd_boolean
4ab527b0 1405lookup_address_in_function_table (struct comp_unit *unit,
818a27ac
AM
1406 bfd_vma addr,
1407 struct funcinfo **function_ptr,
1408 const char **functionname_ptr)
252b5132
RH
1409{
1410 struct funcinfo* each_func;
a13afe8e
FF
1411 struct funcinfo* best_fit = NULL;
1412 struct arange *arange;
252b5132 1413
4ab527b0 1414 for (each_func = unit->function_table;
252b5132
RH
1415 each_func;
1416 each_func = each_func->prev_func)
1417 {
a13afe8e
FF
1418 for (arange = &each_func->arange;
1419 arange;
1420 arange = arange->next)
252b5132 1421 {
a13afe8e
FF
1422 if (addr >= arange->low && addr < arange->high)
1423 {
1424 if (!best_fit ||
1425 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1426 best_fit = each_func;
1427 }
252b5132
RH
1428 }
1429 }
98591c73 1430
a13afe8e
FF
1431 if (best_fit)
1432 {
1433 *functionname_ptr = best_fit->name;
1434 *function_ptr = best_fit;
1435 return TRUE;
1436 }
1437 else
1438 {
1439 return FALSE;
1440 }
252b5132
RH
1441}
1442
5420f73d
L
1443/* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1444 and LINENUMBER_PTR, and return TRUE. */
1445
1446static bfd_boolean
1447lookup_symbol_in_function_table (struct comp_unit *unit,
1448 asymbol *sym,
1449 bfd_vma addr,
1450 const char **filename_ptr,
1451 unsigned int *linenumber_ptr)
1452{
1453 struct funcinfo* each_func;
1454 struct funcinfo* best_fit = NULL;
1455 struct arange *arange;
1456 const char *name = bfd_asymbol_name (sym);
1457 asection *sec = bfd_get_section (sym);
1458
1459 for (each_func = unit->function_table;
1460 each_func;
1461 each_func = each_func->prev_func)
1462 {
1463 for (arange = &each_func->arange;
1464 arange;
1465 arange = arange->next)
1466 {
1467 if ((!each_func->sec || each_func->sec == sec)
1468 && addr >= arange->low
1469 && addr < arange->high
650f284e 1470 && each_func->name
5420f73d
L
1471 && strcmp (name, each_func->name) == 0
1472 && (!best_fit
1473 || ((arange->high - arange->low)
1474 < (best_fit->arange.high - best_fit->arange.low))))
1475 best_fit = each_func;
1476 }
1477 }
1478
1479 if (best_fit)
1480 {
1481 best_fit->sec = sec;
1482 *filename_ptr = best_fit->file;
1483 *linenumber_ptr = best_fit->line;
1484 return TRUE;
1485 }
1486 else
1487 return FALSE;
1488}
1489
1490/* Variable table functions. */
1491
1492/* If SYM is within variable table of UNIT, set FILENAME_PTR and
1493 LINENUMBER_PTR, and return TRUE. */
1494
1495static bfd_boolean
1496lookup_symbol_in_variable_table (struct comp_unit *unit,
1497 asymbol *sym,
5cf2e3f0 1498 bfd_vma addr,
5420f73d
L
1499 const char **filename_ptr,
1500 unsigned int *linenumber_ptr)
1501{
1502 const char *name = bfd_asymbol_name (sym);
1503 asection *sec = bfd_get_section (sym);
1504 struct varinfo* each;
1505
1506 for (each = unit->variable_table; each; each = each->prev_var)
1507 if (each->stack == 0
5cf2e3f0
L
1508 && each->file != NULL
1509 && each->name != NULL
1510 && each->addr == addr
5420f73d
L
1511 && (!each->sec || each->sec == sec)
1512 && strcmp (name, each->name) == 0)
1513 break;
1514
1515 if (each)
1516 {
1517 each->sec = sec;
1518 *filename_ptr = each->file;
1519 *linenumber_ptr = each->line;
1520 return TRUE;
1521 }
1522 else
1523 return FALSE;
1524}
1525
06f22d7e
FF
1526static char *
1527find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1528{
1529 bfd *abfd = unit->abfd;
f075ee0c 1530 bfd_byte *info_ptr;
06f22d7e
FF
1531 unsigned int abbrev_number, bytes_read, i;
1532 struct abbrev_info *abbrev;
1533 struct attribute attr;
1534 char *name = 0;
1535
c0c28ab8 1536 info_ptr = unit->info_ptr_unit + die_ref;
06f22d7e
FF
1537 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1538 info_ptr += bytes_read;
1539
1540 if (abbrev_number)
1541 {
1542 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1543 if (! abbrev)
1544 {
1545 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1546 abbrev_number);
1547 bfd_set_error (bfd_error_bad_value);
1548 }
1549 else
1550 {
d5cbaa15 1551 for (i = 0; i < abbrev->num_attrs; ++i)
06f22d7e
FF
1552 {
1553 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
26bf4e33
FF
1554 switch (attr.name)
1555 {
1556 case DW_AT_name:
d5cbaa15
JW
1557 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1558 if (name == NULL)
1559 name = attr.u.str;
26bf4e33
FF
1560 break;
1561 case DW_AT_specification:
1562 name = find_abstract_instance_name (unit, attr.u.val);
1563 break;
d5cbaa15
JW
1564 case DW_AT_MIPS_linkage_name:
1565 name = attr.u.str;
1566 break;
26bf4e33
FF
1567 default:
1568 break;
1569 }
06f22d7e
FF
1570 }
1571 }
1572 }
1573 return (name);
1574}
1575
a13afe8e
FF
1576static void
1577read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1578{
1579 bfd_byte *ranges_ptr;
1580 bfd_vma base_address = unit->base_address;
1581
1582 if (! unit->stash->dwarf_ranges_buffer)
1583 {
1584 if (! read_debug_ranges (unit))
1585 return;
1586 }
1587 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1588
1589 for (;;)
1590 {
1591 bfd_vma low_pc;
1592 bfd_vma high_pc;
1593
a29b7e88 1594 if (unit->addr_size == 4)
a13afe8e
FF
1595 {
1596 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1597 ranges_ptr += 4;
1598 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1599 ranges_ptr += 4;
1600 }
1601 else
1602 {
1603 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1604 ranges_ptr += 8;
1605 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1606 ranges_ptr += 8;
1607 }
1608 if (low_pc == 0 && high_pc == 0)
1609 break;
1610 if (low_pc == -1UL && high_pc != -1UL)
1611 base_address = high_pc;
1612 else
1613 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1614 }
1615}
1616
a092b084 1617/* DWARF2 Compilation unit functions. */
252b5132
RH
1618
1619/* Scan over each die in a comp. unit looking for functions to add
5420f73d 1620 to the function table and variables to the variable table. */
252b5132 1621
b34976b6 1622static bfd_boolean
5420f73d 1623scan_unit_for_symbols (struct comp_unit *unit)
252b5132
RH
1624{
1625 bfd *abfd = unit->abfd;
f075ee0c 1626 bfd_byte *info_ptr = unit->first_child_die_ptr;
252b5132 1627 int nesting_level = 1;
c955f9cd
JW
1628 struct funcinfo **nested_funcs;
1629 int nested_funcs_size;
1630
1631 /* Maintain a stack of in-scope functions and inlined functions, which we
1632 can use to set the caller_func field. */
1633 nested_funcs_size = 32;
1634 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1635 if (nested_funcs == NULL)
1636 return FALSE;
1637 nested_funcs[nesting_level] = 0;
252b5132
RH
1638
1639 while (nesting_level)
1640 {
1641 unsigned int abbrev_number, bytes_read, i;
1642 struct abbrev_info *abbrev;
1643 struct attribute attr;
1644 struct funcinfo *func;
5420f73d 1645 struct varinfo *var;
a13afe8e
FF
1646 bfd_vma low_pc = 0;
1647 bfd_vma high_pc = 0;
252b5132
RH
1648
1649 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1650 info_ptr += bytes_read;
1651
1652 if (! abbrev_number)
1653 {
1654 nesting_level--;
1655 continue;
1656 }
98591c73 1657
252b5132
RH
1658 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1659 if (! abbrev)
1660 {
f46c2da6 1661 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1662 abbrev_number);
1663 bfd_set_error (bfd_error_bad_value);
c955f9cd 1664 free (nested_funcs);
b34976b6 1665 return FALSE;
252b5132 1666 }
98591c73 1667
5420f73d 1668 var = NULL;
06f22d7e 1669 if (abbrev->tag == DW_TAG_subprogram
5420f73d 1670 || abbrev->tag == DW_TAG_entry_point
06f22d7e 1671 || abbrev->tag == DW_TAG_inlined_subroutine)
252b5132 1672 {
dc810e39 1673 bfd_size_type amt = sizeof (struct funcinfo);
818a27ac 1674 func = bfd_zalloc (abfd, amt);
4ab527b0 1675 func->tag = abbrev->tag;
252b5132
RH
1676 func->prev_func = unit->function_table;
1677 unit->function_table = func;
c955f9cd
JW
1678
1679 if (func->tag == DW_TAG_inlined_subroutine)
1680 for (i = nesting_level - 1; i >= 1; i--)
1681 if (nested_funcs[i])
1682 {
1683 func->caller_func = nested_funcs[i];
1684 break;
1685 }
1686 nested_funcs[nesting_level] = func;
252b5132
RH
1687 }
1688 else
5420f73d
L
1689 {
1690 func = NULL;
1691 if (abbrev->tag == DW_TAG_variable)
1692 {
1693 bfd_size_type amt = sizeof (struct varinfo);
1694 var = bfd_zalloc (abfd, amt);
1695 var->tag = abbrev->tag;
1696 var->stack = 1;
1697 var->prev_var = unit->variable_table;
1698 unit->variable_table = var;
1699 }
c955f9cd
JW
1700
1701 /* No inline function in scope at this nesting level. */
1702 nested_funcs[nesting_level] = 0;
5420f73d 1703 }
98591c73 1704
252b5132
RH
1705 for (i = 0; i < abbrev->num_attrs; ++i)
1706 {
1707 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
98591c73 1708
252b5132
RH
1709 if (func)
1710 {
1711 switch (attr.name)
1712 {
4ab527b0
FF
1713 case DW_AT_call_file:
1714 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1715 break;
1716
1717 case DW_AT_call_line:
1718 func->caller_line = attr.u.val;
1719 break;
1720
06f22d7e
FF
1721 case DW_AT_abstract_origin:
1722 func->name = find_abstract_instance_name (unit, attr.u.val);
1723 break;
1724
252b5132 1725 case DW_AT_name:
252b5132
RH
1726 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1727 if (func->name == NULL)
482e2e37 1728 func->name = attr.u.str;
252b5132 1729 break;
98591c73 1730
252b5132 1731 case DW_AT_MIPS_linkage_name:
482e2e37 1732 func->name = attr.u.str;
252b5132
RH
1733 break;
1734
1735 case DW_AT_low_pc:
a13afe8e 1736 low_pc = attr.u.val;
252b5132
RH
1737 break;
1738
1739 case DW_AT_high_pc:
a13afe8e
FF
1740 high_pc = attr.u.val;
1741 break;
1742
1743 case DW_AT_ranges:
1744 read_rangelist (unit, &func->arange, attr.u.val);
252b5132
RH
1745 break;
1746
5420f73d
L
1747 case DW_AT_decl_file:
1748 func->file = concat_filename (unit->line_table,
1749 attr.u.val);
1750 break;
1751
1752 case DW_AT_decl_line:
1753 func->line = attr.u.val;
1754 break;
1755
1756 default:
1757 break;
1758 }
1759 }
1760 else if (var)
1761 {
1762 switch (attr.name)
1763 {
1764 case DW_AT_name:
1765 var->name = attr.u.str;
1766 break;
1767
1768 case DW_AT_decl_file:
1769 var->file = concat_filename (unit->line_table,
1770 attr.u.val);
1771 break;
1772
1773 case DW_AT_decl_line:
1774 var->line = attr.u.val;
1775 break;
1776
1777 case DW_AT_external:
1778 if (attr.u.val != 0)
1779 var->stack = 0;
1780 break;
1781
1782 case DW_AT_location:
5cf2e3f0 1783 switch (attr.form)
5420f73d 1784 {
5cf2e3f0
L
1785 case DW_FORM_block:
1786 case DW_FORM_block1:
1787 case DW_FORM_block2:
1788 case DW_FORM_block4:
1789 if (*attr.u.blk->data == DW_OP_addr)
5420f73d 1790 {
5cf2e3f0 1791 var->stack = 0;
98b880f4
JW
1792
1793 /* Verify that DW_OP_addr is the only opcode in the
1794 location, in which case the block size will be 1
1795 plus the address size. */
1796 /* ??? For TLS variables, gcc can emit
1797 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1798 which we don't handle here yet. */
1799 if (attr.u.blk->size == unit->addr_size + 1U)
1800 var->addr = bfd_get (unit->addr_size * 8,
1801 unit->abfd,
1802 attr.u.blk->data + 1);
5420f73d 1803 }
5cf2e3f0
L
1804 break;
1805
1806 default:
1807 break;
5420f73d
L
1808 }
1809 break;
1810
252b5132
RH
1811 default:
1812 break;
1813 }
1814 }
1815 }
1816
a13afe8e
FF
1817 if (func && high_pc != 0)
1818 {
1819 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1820 }
1821
252b5132 1822 if (abbrev->has_children)
c955f9cd
JW
1823 {
1824 nesting_level++;
1825
1826 if (nesting_level >= nested_funcs_size)
1827 {
1828 struct funcinfo **tmp;
1829
1830 nested_funcs_size *= 2;
1831 tmp = bfd_realloc (nested_funcs,
1832 (nested_funcs_size
1833 * sizeof (struct funcinfo *)));
1834 if (tmp == NULL)
1835 {
1836 free (nested_funcs);
1837 return FALSE;
1838 }
1839 nested_funcs = tmp;
1840 }
1841 nested_funcs[nesting_level] = 0;
1842 }
252b5132
RH
1843 }
1844
c955f9cd 1845 free (nested_funcs);
b34976b6 1846 return TRUE;
252b5132
RH
1847}
1848
5e38c3b8
MM
1849/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1850 includes the compilation unit header that proceeds the DIE's, but
5c4491d3 1851 does not include the length field that precedes each compilation
5e38c3b8 1852 unit header. END_PTR points one past the end of this comp unit.
d03ba2a1 1853 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
252b5132
RH
1854
1855 This routine does not read the whole compilation unit; only enough
1856 to get to the line number information for the compilation unit. */
1857
1858static struct comp_unit *
818a27ac
AM
1859parse_comp_unit (bfd *abfd,
1860 struct dwarf2_debug *stash,
1861 bfd_vma unit_length,
f075ee0c 1862 bfd_byte *info_ptr_unit,
818a27ac 1863 unsigned int offset_size)
252b5132
RH
1864{
1865 struct comp_unit* unit;
f46c2da6 1866 unsigned int version;
8ce8c090 1867 bfd_uint64_t abbrev_offset = 0;
f46c2da6 1868 unsigned int addr_size;
252b5132 1869 struct abbrev_info** abbrevs;
252b5132
RH
1870 unsigned int abbrev_number, bytes_read, i;
1871 struct abbrev_info *abbrev;
1872 struct attribute attr;
f075ee0c
AM
1873 bfd_byte *info_ptr = stash->info_ptr;
1874 bfd_byte *end_ptr = info_ptr + unit_length;
dc810e39 1875 bfd_size_type amt;
a13afe8e
FF
1876 bfd_vma low_pc = 0;
1877 bfd_vma high_pc = 0;
3fde5a36 1878
252b5132
RH
1879 version = read_2_bytes (abfd, info_ptr);
1880 info_ptr += 2;
d03ba2a1
JJ
1881 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1882 if (offset_size == 4)
5e38c3b8 1883 abbrev_offset = read_4_bytes (abfd, info_ptr);
d03ba2a1 1884 else
5e38c3b8 1885 abbrev_offset = read_8_bytes (abfd, info_ptr);
d03ba2a1 1886 info_ptr += offset_size;
252b5132
RH
1887 addr_size = read_1_byte (abfd, info_ptr);
1888 info_ptr += 1;
1889
1890 if (version != 2)
1891 {
f46c2da6 1892 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
252b5132
RH
1893 bfd_set_error (bfd_error_bad_value);
1894 return 0;
1895 }
1896
1897 if (addr_size > sizeof (bfd_vma))
1898 {
1899 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1900 addr_size,
f46c2da6 1901 (unsigned int) sizeof (bfd_vma));
252b5132
RH
1902 bfd_set_error (bfd_error_bad_value);
1903 return 0;
1904 }
1905
ecb651f0 1906 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 1907 {
f5a3e38a 1908 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
252b5132
RH
1909 bfd_set_error (bfd_error_bad_value);
1910 return 0;
1911 }
1912
a092b084 1913 /* Read the abbrevs for this compilation unit into a table. */
51db3708 1914 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
1915 if (! abbrevs)
1916 return 0;
1917
1918 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1919 info_ptr += bytes_read;
1920 if (! abbrev_number)
1921 {
f46c2da6 1922 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
252b5132
RH
1923 abbrev_number);
1924 bfd_set_error (bfd_error_bad_value);
1925 return 0;
1926 }
1927
1928 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1929 if (! abbrev)
1930 {
f46c2da6 1931 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1932 abbrev_number);
1933 bfd_set_error (bfd_error_bad_value);
1934 return 0;
1935 }
98591c73 1936
dc810e39 1937 amt = sizeof (struct comp_unit);
818a27ac 1938 unit = bfd_zalloc (abfd, amt);
252b5132 1939 unit->abfd = abfd;
98591c73 1940 unit->addr_size = addr_size;
d03ba2a1 1941 unit->offset_size = offset_size;
252b5132
RH
1942 unit->abbrevs = abbrevs;
1943 unit->end_ptr = end_ptr;
d03ba2a1 1944 unit->stash = stash;
c0c28ab8 1945 unit->info_ptr_unit = info_ptr_unit;
252b5132
RH
1946
1947 for (i = 0; i < abbrev->num_attrs; ++i)
1948 {
1949 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1950
1951 /* Store the data if it is of an attribute we want to keep in a
1952 partial symbol table. */
1953 switch (attr.name)
1954 {
1955 case DW_AT_stmt_list:
1956 unit->stmtlist = 1;
482e2e37 1957 unit->line_offset = attr.u.val;
252b5132
RH
1958 break;
1959
1960 case DW_AT_name:
482e2e37 1961 unit->name = attr.u.str;
252b5132
RH
1962 break;
1963
1964 case DW_AT_low_pc:
a13afe8e
FF
1965 low_pc = attr.u.val;
1966 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1967 this is the base address to use when reading location
1968 lists or range lists. */
1969 unit->base_address = low_pc;
252b5132
RH
1970 break;
1971
1972 case DW_AT_high_pc:
a13afe8e
FF
1973 high_pc = attr.u.val;
1974 break;
1975
1976 case DW_AT_ranges:
1977 read_rangelist (unit, &unit->arange, attr.u.val);
252b5132
RH
1978 break;
1979
1980 case DW_AT_comp_dir:
1981 {
f075ee0c 1982 char *comp_dir = attr.u.str;
252b5132
RH
1983 if (comp_dir)
1984 {
1985 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1986 directory, get rid of it. */
818a27ac 1987 char *cp = strchr (comp_dir, ':');
252b5132
RH
1988
1989 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1990 comp_dir = cp + 1;
1991 }
1992 unit->comp_dir = comp_dir;
1993 break;
1994 }
1995
1996 default:
1997 break;
1998 }
1999 }
a13afe8e
FF
2000 if (high_pc != 0)
2001 {
2002 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2003 }
252b5132
RH
2004
2005 unit->first_child_die_ptr = info_ptr;
2006 return unit;
2007}
2008
b34976b6 2009/* Return TRUE if UNIT contains the address given by ADDR. */
252b5132 2010
b34976b6 2011static bfd_boolean
818a27ac 2012comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
252b5132 2013{
f623be2b
RH
2014 struct arange *arange;
2015
2016 if (unit->error)
b34976b6 2017 return FALSE;
f623be2b
RH
2018
2019 arange = &unit->arange;
2020 do
2021 {
2022 if (addr >= arange->low && addr < arange->high)
b34976b6 2023 return TRUE;
f623be2b
RH
2024 arange = arange->next;
2025 }
2026 while (arange);
98591c73 2027
b34976b6 2028 return FALSE;
252b5132
RH
2029}
2030
252b5132
RH
2031/* If UNIT contains ADDR, set the output parameters to the values for
2032 the line containing ADDR. The output parameters, FILENAME_PTR,
2033 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 2034 to be filled in.
252b5132 2035
ab3acfbe 2036 Return TRUE if UNIT contains ADDR, and no errors were encountered;
b34976b6 2037 FALSE otherwise. */
252b5132 2038
b34976b6 2039static bfd_boolean
818a27ac
AM
2040comp_unit_find_nearest_line (struct comp_unit *unit,
2041 bfd_vma addr,
2042 const char **filename_ptr,
2043 const char **functionname_ptr,
2044 unsigned int *linenumber_ptr,
2045 struct dwarf2_debug *stash)
252b5132 2046{
b34976b6
AM
2047 bfd_boolean line_p;
2048 bfd_boolean func_p;
1ee24f27 2049 struct funcinfo *function;
98591c73 2050
252b5132 2051 if (unit->error)
b34976b6 2052 return FALSE;
252b5132
RH
2053
2054 if (! unit->line_table)
2055 {
2056 if (! unit->stmtlist)
2057 {
2058 unit->error = 1;
b34976b6 2059 return FALSE;
252b5132 2060 }
98591c73 2061
51db3708 2062 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
2063
2064 if (! unit->line_table)
2065 {
2066 unit->error = 1;
b34976b6 2067 return FALSE;
252b5132 2068 }
98591c73 2069
3f5864e1 2070 if (unit->first_child_die_ptr < unit->end_ptr
5420f73d 2071 && ! scan_unit_for_symbols (unit))
252b5132
RH
2072 {
2073 unit->error = 1;
b34976b6 2074 return FALSE;
252b5132
RH
2075 }
2076 }
2077
1ee24f27 2078 function = NULL;
4ab527b0 2079 func_p = lookup_address_in_function_table (unit, addr,
e2f6d277 2080 &function, functionname_ptr);
4ab527b0
FF
2081 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2082 stash->inliner_chain = function;
e2f6d277
NC
2083 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2084 function, filename_ptr,
252b5132 2085 linenumber_ptr);
b34976b6 2086 return line_p || func_p;
252b5132
RH
2087}
2088
5420f73d
L
2089/* If UNIT contains SYM at ADDR, set the output parameters to the
2090 values for the line containing SYM. The output parameters,
2091 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2092 filled in.
2093
2094 Return TRUE if UNIT contains SYM, and no errors were encountered;
2095 FALSE otherwise. */
2096
2097static bfd_boolean
2098comp_unit_find_line (struct comp_unit *unit,
2099 asymbol *sym,
2100 bfd_vma addr,
2101 const char **filename_ptr,
2102 unsigned int *linenumber_ptr,
2103 struct dwarf2_debug *stash)
2104{
2105 if (unit->error)
2106 return FALSE;
2107
2108 if (! unit->line_table)
2109 {
2110 if (! unit->stmtlist)
2111 {
2112 unit->error = 1;
2113 return FALSE;
2114 }
2115
2116 unit->line_table = decode_line_info (unit, stash);
2117
2118 if (! unit->line_table)
2119 {
2120 unit->error = 1;
2121 return FALSE;
2122 }
2123
2124 if (unit->first_child_die_ptr < unit->end_ptr
2125 && ! scan_unit_for_symbols (unit))
2126 {
2127 unit->error = 1;
2128 return FALSE;
2129 }
2130 }
2131
2132 if (sym->flags & BSF_FUNCTION)
2133 return lookup_symbol_in_function_table (unit, sym, addr,
2134 filename_ptr,
2135 linenumber_ptr);
2136 else
5cf2e3f0
L
2137 return lookup_symbol_in_variable_table (unit, sym, addr,
2138 filename_ptr,
5420f73d
L
2139 linenumber_ptr);
2140}
2141
e2f6d277
NC
2142/* Locate a section in a BFD containing debugging info. The search starts
2143 from the section after AFTER_SEC, or from the first section in the BFD if
2144 AFTER_SEC is NULL. The search works by examining the names of the
2145 sections. There are two permissiable names. The first is .debug_info.
2146 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2147 This is a variation on the .debug_info section which has a checksum
2148 describing the contents appended onto the name. This allows the linker to
2149 identify and discard duplicate debugging sections for different
2150 compilation units. */
a092b084
NC
2151#define DWARF2_DEBUG_INFO ".debug_info"
2152#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2153
2154static asection *
818a27ac 2155find_debug_info (bfd *abfd, asection *after_sec)
a092b084
NC
2156{
2157 asection * msec;
2158
2159 if (after_sec)
2160 msec = after_sec->next;
2161 else
2162 msec = abfd->sections;
2163
2164 while (msec)
2165 {
2166 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2167 return msec;
2168
2169 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
2170 return msec;
2171
2172 msec = msec->next;
2173 }
2174
2175 return NULL;
2176}
2177
cd917290 2178/* The DWARF2 version of find_nearest_line. Return TRUE if the line
5e38c3b8
MM
2179 is found without error. ADDR_SIZE is the number of bytes in the
2180 initial .debug_info length field and in the abbreviation offset.
2181 You may use zero to indicate that the default value should be
2182 used. */
252b5132 2183
b34976b6 2184bfd_boolean
818a27ac
AM
2185_bfd_dwarf2_find_nearest_line (bfd *abfd,
2186 asection *section,
2187 asymbol **symbols,
2188 bfd_vma offset,
2189 const char **filename_ptr,
2190 const char **functionname_ptr,
2191 unsigned int *linenumber_ptr,
2192 unsigned int addr_size,
2193 void **pinfo)
252b5132
RH
2194{
2195 /* Read each compilation unit from the section .debug_info, and check
2196 to see if it contains the address we are searching for. If yes,
2197 lookup the address, and return the line number info. If no, go
98591c73 2198 on to the next compilation unit.
252b5132
RH
2199
2200 We keep a list of all the previously read compilation units, and
98591c73 2201 a pointer to the next un-read compilation unit. Check the
a092b084 2202 previously read units before reading more. */
1ba54ee0 2203 struct dwarf2_debug *stash;
252b5132 2204
a092b084 2205 /* What address are we looking for? */
1ba54ee0 2206 bfd_vma addr;
252b5132
RH
2207
2208 struct comp_unit* each;
98591c73 2209
1ba54ee0
AM
2210 stash = *pinfo;
2211 addr = offset;
2212 if (section->output_section)
bc79cded 2213 addr += section->output_section->lma + section->output_offset;
1ba54ee0 2214 else
bc79cded 2215 addr += section->lma;
252b5132
RH
2216 *filename_ptr = NULL;
2217 *functionname_ptr = NULL;
2218 *linenumber_ptr = 0;
2219
5e38c3b8
MM
2220 /* The DWARF2 spec says that the initial length field, and the
2221 offset of the abbreviation table, should both be 4-byte values.
2222 However, some compilers do things differently. */
2223 if (addr_size == 0)
2224 addr_size = 4;
1efe4b10 2225 BFD_ASSERT (addr_size == 4 || addr_size == 8);
98591c73 2226
252b5132
RH
2227 if (! stash)
2228 {
dc810e39 2229 bfd_size_type total_size;
252b5132 2230 asection *msec;
dc810e39 2231 bfd_size_type amt = sizeof (struct dwarf2_debug);
a092b084 2232
818a27ac 2233 stash = bfd_zalloc (abfd, amt);
252b5132 2234 if (! stash)
b34976b6 2235 return FALSE;
252b5132 2236
818a27ac 2237 *pinfo = stash;
3fde5a36 2238
a092b084
NC
2239 msec = find_debug_info (abfd, NULL);
2240 if (! msec)
2241 /* No dwarf2 info. Note that at this point the stash
2242 has been allocated, but contains zeros, this lets
2243 future calls to this function fail quicker. */
b34976b6 2244 return FALSE;
a092b084
NC
2245
2246 /* There can be more than one DWARF2 info section in a BFD these days.
e82ce529 2247 Read them all in and produce one large stash. We do this in two
a092b084
NC
2248 passes - in the first pass we just accumulate the section sizes.
2249 In the second pass we read in the section's contents. The allows
2250 us to avoid reallocing the data as we add sections to the stash. */
2251 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
eea6121a 2252 total_size += msec->size;
98591c73 2253
818a27ac 2254 stash->info_ptr = bfd_alloc (abfd, total_size);
a092b084 2255 if (stash->info_ptr == NULL)
b34976b6 2256 return FALSE;
252b5132 2257
a092b084
NC
2258 stash->info_ptr_end = stash->info_ptr;
2259
2260 for (msec = find_debug_info (abfd, NULL);
2261 msec;
2262 msec = find_debug_info (abfd, msec))
252b5132 2263 {
dc810e39
AM
2264 bfd_size_type size;
2265 bfd_size_type start;
a092b084 2266
eea6121a 2267 size = msec->size;
a092b084
NC
2268 if (size == 0)
2269 continue;
2270
2271 start = stash->info_ptr_end - stash->info_ptr;
2272
6e84a906
DJ
2273 if ((bfd_simple_get_relocated_section_contents
2274 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
a092b084
NC
2275 continue;
2276
2277 stash->info_ptr_end = stash->info_ptr + start + size;
252b5132
RH
2278 }
2279
f2363ce5
AO
2280 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2281
2282 stash->sec = find_debug_info (abfd, NULL);
2283 stash->sec_info_ptr = stash->info_ptr;
2284 stash->syms = symbols;
252b5132 2285 }
a092b084 2286
98591c73 2287 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 2288 (or that an error occured while setting up the stash). */
252b5132 2289 if (! stash->info_ptr)
b34976b6 2290 return FALSE;
252b5132 2291
4ab527b0
FF
2292 stash->inliner_chain = NULL;
2293
a092b084 2294 /* Check the previously read comp. units first. */
252b5132 2295 for (each = stash->all_comp_units; each; each = each->next_unit)
f623be2b 2296 if (comp_unit_contains_address (each, addr))
98591c73 2297 return comp_unit_find_nearest_line (each, addr, filename_ptr,
51db3708
NC
2298 functionname_ptr, linenumber_ptr,
2299 stash);
252b5132 2300
a092b084 2301 /* Read each remaining comp. units checking each as they are read. */
252b5132
RH
2302 while (stash->info_ptr < stash->info_ptr_end)
2303 {
5e38c3b8 2304 bfd_vma length;
b34976b6 2305 bfd_boolean found;
d03ba2a1 2306 unsigned int offset_size = addr_size;
f075ee0c 2307 bfd_byte *info_ptr_unit = stash->info_ptr;
252b5132 2308
a3805e4e
AO
2309 length = read_4_bytes (abfd, stash->info_ptr);
2310 /* A 0xffffff length is the DWARF3 way of indicating we use
2311 64-bit offsets, instead of 32-bit offsets. */
2312 if (length == 0xffffffff)
d03ba2a1 2313 {
a3805e4e
AO
2314 offset_size = 8;
2315 length = read_8_bytes (abfd, stash->info_ptr + 4);
2316 stash->info_ptr += 12;
2317 }
2318 /* A zero length is the IRIX way of indicating 64-bit offsets,
2319 mostly because the 64-bit length will generally fit in 32
2320 bits, and the endianness helps. */
2321 else if (length == 0)
2322 {
2323 offset_size = 8;
2324 length = read_4_bytes (abfd, stash->info_ptr + 4);
2325 stash->info_ptr += 8;
2326 }
2327 /* In the absence of the hints above, we assume addr_size-sized
2328 offsets, for backward-compatibility with pre-DWARF3 64-bit
2329 platforms. */
2330 else if (addr_size == 8)
2331 {
2332 length = read_8_bytes (abfd, stash->info_ptr);
060dc71d 2333 stash->info_ptr += 8;
d03ba2a1 2334 }
5e38c3b8 2335 else
a3805e4e 2336 stash->info_ptr += 4;
252b5132
RH
2337
2338 if (length > 0)
e82ce529 2339 {
c0c28ab8
L
2340 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2341 offset_size);
252b5132
RH
2342 stash->info_ptr += length;
2343
f2363ce5 2344 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
eea6121a 2345 == stash->sec->size)
f2363ce5
AO
2346 {
2347 stash->sec = find_debug_info (abfd, stash->sec);
2348 stash->sec_info_ptr = stash->info_ptr;
2349 }
2350
252b5132
RH
2351 if (each)
2352 {
2353 each->next_unit = stash->all_comp_units;
2354 stash->all_comp_units = each;
2355
159002ff
RH
2356 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2357 compilation units. If we don't have them (i.e.,
2358 unit->high == 0), we need to consult the line info
2359 table to see if a compilation unit contains the given
a092b084 2360 address. */
f623be2b 2361 if (each->arange.high > 0)
159002ff
RH
2362 {
2363 if (comp_unit_contains_address (each, addr))
2364 return comp_unit_find_nearest_line (each, addr,
6e84a906
DJ
2365 filename_ptr,
2366 functionname_ptr,
2367 linenumber_ptr,
2368 stash);
159002ff
RH
2369 }
2370 else
2371 {
2372 found = comp_unit_find_nearest_line (each, addr,
2373 filename_ptr,
2374 functionname_ptr,
51db3708
NC
2375 linenumber_ptr,
2376 stash);
159002ff 2377 if (found)
b34976b6 2378 return TRUE;
159002ff 2379 }
252b5132
RH
2380 }
2381 }
2382 }
2383
b34976b6 2384 return FALSE;
252b5132 2385}
35330cce 2386
5420f73d
L
2387/* The DWARF2 version of find_line. Return TRUE if the line is found
2388 without error. */
2389
2390bfd_boolean
2391_bfd_dwarf2_find_line (bfd *abfd,
2392 asymbol **symbols,
2393 asymbol *symbol,
2394 const char **filename_ptr,
2395 unsigned int *linenumber_ptr,
2396 unsigned int addr_size,
2397 void **pinfo)
2398{
2399 /* Read each compilation unit from the section .debug_info, and check
2400 to see if it contains the address we are searching for. If yes,
2401 lookup the address, and return the line number info. If no, go
2402 on to the next compilation unit.
2403
2404 We keep a list of all the previously read compilation units, and
2405 a pointer to the next un-read compilation unit. Check the
2406 previously read units before reading more. */
2407 struct dwarf2_debug *stash;
2408
2409 /* What address are we looking for? */
2410 bfd_vma addr;
2411
2412 struct comp_unit* each;
2413
2414 asection *section;
2415
2416 bfd_boolean found;
2417
2418 section = bfd_get_section (symbol);
2419
2420 addr = symbol->value;
2421 if (section->output_section)
bc79cded 2422 addr += section->output_section->lma + section->output_offset;
5420f73d 2423 else
bc79cded 2424 addr += section->lma;
5420f73d
L
2425
2426 *filename_ptr = NULL;
2427 stash = *pinfo;
2428 *filename_ptr = NULL;
2429 *linenumber_ptr = 0;
2430
2431 if (! stash)
2432 {
2433 bfd_size_type total_size;
2434 asection *msec;
2435 bfd_size_type amt = sizeof (struct dwarf2_debug);
2436
2437 stash = bfd_zalloc (abfd, amt);
2438 if (! stash)
2439 return FALSE;
2440
2441 *pinfo = stash;
2442
2443 msec = find_debug_info (abfd, NULL);
2444 if (! msec)
2445 /* No dwarf2 info. Note that at this point the stash
2446 has been allocated, but contains zeros, this lets
2447 future calls to this function fail quicker. */
2448 return FALSE;
2449
2450 /* There can be more than one DWARF2 info section in a BFD these days.
2451 Read them all in and produce one large stash. We do this in two
2452 passes - in the first pass we just accumulate the section sizes.
2453 In the second pass we read in the section's contents. The allows
2454 us to avoid reallocing the data as we add sections to the stash. */
2455 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2456 total_size += msec->size;
2457
2458 stash->info_ptr = bfd_alloc (abfd, total_size);
2459 if (stash->info_ptr == NULL)
2460 return FALSE;
2461
2462 stash->info_ptr_end = stash->info_ptr;
2463
2464 for (msec = find_debug_info (abfd, NULL);
2465 msec;
2466 msec = find_debug_info (abfd, msec))
2467 {
2468 bfd_size_type size;
2469 bfd_size_type start;
2470
2471 size = msec->size;
2472 if (size == 0)
2473 continue;
2474
2475 start = stash->info_ptr_end - stash->info_ptr;
2476
2477 if ((bfd_simple_get_relocated_section_contents
2478 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2479 continue;
2480
2481 stash->info_ptr_end = stash->info_ptr + start + size;
2482 }
2483
2484 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2485
2486 stash->sec = find_debug_info (abfd, NULL);
2487 stash->sec_info_ptr = stash->info_ptr;
2488 stash->syms = symbols;
2489 }
2490
2491 /* A null info_ptr indicates that there is no dwarf2 info
2492 (or that an error occured while setting up the stash). */
2493 if (! stash->info_ptr)
2494 return FALSE;
2495
2496 stash->inliner_chain = NULL;
2497
2498 /* Check the previously read comp. units first. */
2499 for (each = stash->all_comp_units; each; each = each->next_unit)
2500 if ((symbol->flags & BSF_FUNCTION) == 0
2501 || comp_unit_contains_address (each, addr))
2502 {
2503 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
2504 linenumber_ptr, stash);
2505 if (found)
2506 return found;
2507 }
2508
2509 /* The DWARF2 spec says that the initial length field, and the
2510 offset of the abbreviation table, should both be 4-byte values.
2511 However, some compilers do things differently. */
2512 if (addr_size == 0)
2513 addr_size = 4;
2514 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2515
2516 /* Read each remaining comp. units checking each as they are read. */
2517 while (stash->info_ptr < stash->info_ptr_end)
2518 {
2519 bfd_vma length;
2520 unsigned int offset_size = addr_size;
2521 bfd_byte *info_ptr_unit = stash->info_ptr;
2522
2523 length = read_4_bytes (abfd, stash->info_ptr);
2524 /* A 0xffffff length is the DWARF3 way of indicating we use
2525 64-bit offsets, instead of 32-bit offsets. */
2526 if (length == 0xffffffff)
2527 {
2528 offset_size = 8;
2529 length = read_8_bytes (abfd, stash->info_ptr + 4);
2530 stash->info_ptr += 12;
2531 }
2532 /* A zero length is the IRIX way of indicating 64-bit offsets,
2533 mostly because the 64-bit length will generally fit in 32
2534 bits, and the endianness helps. */
2535 else if (length == 0)
2536 {
2537 offset_size = 8;
2538 length = read_4_bytes (abfd, stash->info_ptr + 4);
2539 stash->info_ptr += 8;
2540 }
2541 /* In the absence of the hints above, we assume addr_size-sized
2542 offsets, for backward-compatibility with pre-DWARF3 64-bit
2543 platforms. */
2544 else if (addr_size == 8)
2545 {
2546 length = read_8_bytes (abfd, stash->info_ptr);
2547 stash->info_ptr += 8;
2548 }
2549 else
2550 stash->info_ptr += 4;
2551
2552 if (length > 0)
2553 {
2554 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2555 offset_size);
2556 stash->info_ptr += length;
2557
2558 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2559 == stash->sec->size)
2560 {
2561 stash->sec = find_debug_info (abfd, stash->sec);
2562 stash->sec_info_ptr = stash->info_ptr;
2563 }
2564
2565 if (each)
2566 {
2567 each->next_unit = stash->all_comp_units;
2568 stash->all_comp_units = each;
2569
2570 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2571 compilation units. If we don't have them (i.e.,
2572 unit->high == 0), we need to consult the line info
2573 table to see if a compilation unit contains the given
2574 address. */
2575 found = (((symbol->flags & BSF_FUNCTION) == 0
2576 || each->arange.high <= 0
2577 || comp_unit_contains_address (each, addr))
2578 && comp_unit_find_line (each, symbol, addr,
2579 filename_ptr,
2580 linenumber_ptr,
2581 stash));
2582 if (found)
2583 return TRUE;
2584 }
2585 }
2586 }
2587
2588 return FALSE;
2589}
2590
4ab527b0
FF
2591bfd_boolean
2592_bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2593 const char **filename_ptr,
2594 const char **functionname_ptr,
2595 unsigned int *linenumber_ptr,
2596 void **pinfo)
2597{
2598 struct dwarf2_debug *stash;
2599
2600 stash = *pinfo;
2601 if (stash)
2602 {
2603 struct funcinfo *func = stash->inliner_chain;
2604 if (func && func->caller_func)
2605 {
2606 *filename_ptr = func->caller_file;
2607 *functionname_ptr = func->caller_func->name;
2608 *linenumber_ptr = func->caller_line;
2609 stash->inliner_chain = func->caller_func;
2610 return (TRUE);
2611 }
2612 }
2613
2614 return (FALSE);
2615}
2616
35330cce
NC
2617void
2618_bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2619{
2620 struct comp_unit *each;
2621 struct dwarf2_debug *stash;
2622
2623 if (abfd == NULL || elf_tdata (abfd) == NULL)
2624 return;
2625
2626 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2627
2628 if (stash == NULL)
2629 return;
2630
2631 for (each = stash->all_comp_units; each; each = each->next_unit)
2632 {
2633 struct abbrev_info **abbrevs = each->abbrevs;
2634 size_t i;
2635
2636 for (i = 0; i < ABBREV_HASH_SIZE; i++)
2637 {
2638 struct abbrev_info *abbrev = abbrevs[i];
2639
2640 while (abbrev)
2641 {
2642 free (abbrev->attrs);
2643 abbrev = abbrev->next;
2644 }
2645 }
2646
2647 if (each->line_table)
2648 {
2649 free (each->line_table->dirs);
2650 free (each->line_table->files);
2651 }
2652 }
2653
2654 free (stash->dwarf_abbrev_buffer);
2655 free (stash->dwarf_line_buffer);
2656 free (stash->dwarf_ranges_buffer);
2657}
This page took 0.495315 seconds and 4 git commands to generate.