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