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