* elf64-alpha.c: Update all function definitions to ISO C. Remove
[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
a092b084 144 /* Keep the bdf 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
d03ba2a1
JJ
185 /* Pointer to dwarf2_debug structure. */
186 struct dwarf2_debug *stash;
187
a092b084 188 /* Address size for this unit - from unit header. */
252b5132 189 unsigned char addr_size;
d03ba2a1
JJ
190
191 /* Offset size for this unit - from unit header. */
192 unsigned char offset_size;
a13afe8e
FF
193
194 /* Base address for this unit - from DW_AT_low_pc attribute of
195 DW_TAG_compile_unit DIE */
196 bfd_vma base_address;
252b5132
RH
197};
198
a7b97311
AM
199/* This data structure holds the information of an abbrev. */
200struct abbrev_info
201{
202 unsigned int number; /* Number identifying abbrev. */
203 enum dwarf_tag tag; /* DWARF tag. */
204 int has_children; /* Boolean. */
205 unsigned int num_attrs; /* Number of attributes. */
206 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
207 struct abbrev_info *next; /* Next in chain. */
208};
209
210struct attr_abbrev
211{
212 enum dwarf_attribute name;
213 enum dwarf_form form;
214};
215
216#ifndef ABBREV_HASH_SIZE
217#define ABBREV_HASH_SIZE 121
218#endif
219#ifndef ATTR_ALLOC_CHUNK
220#define ATTR_ALLOC_CHUNK 4
221#endif
222
98591c73
KH
223/* VERBATIM
224 The following function up to the END VERBATIM mark are
a092b084 225 copied directly from dwarf2read.c. */
252b5132 226
a092b084 227/* Read dwarf information from a buffer. */
252b5132
RH
228
229static unsigned int
f075ee0c 230read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 231{
818a27ac 232 return bfd_get_8 (abfd, buf);
252b5132
RH
233}
234
235static int
f075ee0c 236read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 237{
818a27ac 238 return bfd_get_signed_8 (abfd, buf);
252b5132
RH
239}
240
241static unsigned int
f075ee0c 242read_2_bytes (bfd *abfd, bfd_byte *buf)
252b5132 243{
818a27ac 244 return bfd_get_16 (abfd, buf);
252b5132
RH
245}
246
252b5132 247static unsigned int
f075ee0c 248read_4_bytes (bfd *abfd, bfd_byte *buf)
252b5132 249{
818a27ac 250 return bfd_get_32 (abfd, buf);
252b5132
RH
251}
252
8ce8c090 253static bfd_uint64_t
f075ee0c 254read_8_bytes (bfd *abfd, bfd_byte *buf)
252b5132 255{
818a27ac 256 return bfd_get_64 (abfd, buf);
252b5132
RH
257}
258
f075ee0c 259static bfd_byte *
818a27ac 260read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 261 bfd_byte *buf,
818a27ac 262 unsigned int size ATTRIBUTE_UNUSED)
252b5132
RH
263{
264 /* If the size of a host char is 8 bits, we can return a pointer
265 to the buffer, otherwise we have to copy the data to a buffer
266 allocated on the temporary obstack. */
267 return buf;
268}
269
270static char *
818a27ac 271read_string (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 272 bfd_byte *buf,
818a27ac 273 unsigned int *bytes_read_ptr)
252b5132 274{
d03ba2a1 275 /* Return a pointer to the embedded string. */
f075ee0c
AM
276 char *str = (char *) buf;
277 if (*str == '\0')
252b5132
RH
278 {
279 *bytes_read_ptr = 1;
280 return NULL;
281 }
98591c73 282
f075ee0c
AM
283 *bytes_read_ptr = strlen (str) + 1;
284 return str;
252b5132
RH
285}
286
d03ba2a1 287static char *
818a27ac 288read_indirect_string (struct comp_unit* unit,
f075ee0c 289 bfd_byte *buf,
818a27ac 290 unsigned int *bytes_read_ptr)
d03ba2a1 291{
8ce8c090 292 bfd_uint64_t offset;
d03ba2a1 293 struct dwarf2_debug *stash = unit->stash;
f075ee0c 294 char *str;
d03ba2a1
JJ
295
296 if (unit->offset_size == 4)
297 offset = read_4_bytes (unit->abfd, buf);
298 else
299 offset = read_8_bytes (unit->abfd, buf);
300 *bytes_read_ptr = unit->offset_size;
301
302 if (! stash->dwarf_str_buffer)
303 {
304 asection *msec;
305 bfd *abfd = unit->abfd;
eea6121a 306 bfd_size_type sz;
d03ba2a1
JJ
307
308 msec = bfd_get_section_by_name (abfd, ".debug_str");
309 if (! msec)
310 {
311 (*_bfd_error_handler)
312 (_("Dwarf Error: Can't find .debug_str section."));
313 bfd_set_error (bfd_error_bad_value);
314 return NULL;
315 }
316
eea6121a
AM
317 sz = msec->rawsize ? msec->rawsize : msec->size;
318 stash->dwarf_str_size = sz;
319 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
36868d45 320 if (! stash->dwarf_str_buffer)
d03ba2a1
JJ
321 return NULL;
322
9f632188 323 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
eea6121a 324 0, sz))
d03ba2a1
JJ
325 return NULL;
326 }
327
328 if (offset >= stash->dwarf_str_size)
329 {
f46c2da6
AM
330 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
331 (unsigned long) offset, stash->dwarf_str_size);
d03ba2a1
JJ
332 bfd_set_error (bfd_error_bad_value);
333 return NULL;
334 }
335
f075ee0c
AM
336 str = (char *) stash->dwarf_str_buffer + offset;
337 if (*str == '\0')
d03ba2a1 338 return NULL;
f075ee0c 339 return str;
d03ba2a1
JJ
340}
341
252b5132
RH
342/* END VERBATIM */
343
8ce8c090 344static bfd_uint64_t
f075ee0c 345read_address (struct comp_unit *unit, bfd_byte *buf)
252b5132 346{
ecb651f0 347 switch (unit->addr_size)
252b5132 348 {
ecb651f0 349 case 8:
818a27ac 350 return bfd_get_64 (unit->abfd, buf);
ecb651f0 351 case 4:
818a27ac 352 return bfd_get_32 (unit->abfd, buf);
ecb651f0 353 case 2:
818a27ac 354 return bfd_get_16 (unit->abfd, buf);
ecb651f0
NC
355 default:
356 abort ();
252b5132 357 }
252b5132
RH
358}
359
252b5132
RH
360/* Lookup an abbrev_info structure in the abbrev hash table. */
361
362static struct abbrev_info *
818a27ac 363lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
252b5132
RH
364{
365 unsigned int hash_number;
366 struct abbrev_info *abbrev;
367
368 hash_number = number % ABBREV_HASH_SIZE;
369 abbrev = abbrevs[hash_number];
370
371 while (abbrev)
372 {
373 if (abbrev->number == number)
374 return abbrev;
375 else
376 abbrev = abbrev->next;
377 }
98591c73 378
252b5132
RH
379 return NULL;
380}
381
382/* In DWARF version 2, the description of the debugging information is
383 stored in a separate .debug_abbrev section. Before we read any
384 dies from a section we read in all abbreviations and install them
385 in a hash table. */
386
387static struct abbrev_info**
8ce8c090 388read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
252b5132
RH
389{
390 struct abbrev_info **abbrevs;
f075ee0c 391 bfd_byte *abbrev_ptr;
252b5132
RH
392 struct abbrev_info *cur_abbrev;
393 unsigned int abbrev_number, bytes_read, abbrev_name;
394 unsigned int abbrev_form, hash_number;
dc810e39 395 bfd_size_type amt;
252b5132
RH
396
397 if (! stash->dwarf_abbrev_buffer)
398 {
399 asection *msec;
400
401 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
402 if (! msec)
403 {
404 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
405 bfd_set_error (bfd_error_bad_value);
406 return 0;
407 }
98591c73 408
eea6121a 409 stash->dwarf_abbrev_size = msec->size;
6e84a906
DJ
410 stash->dwarf_abbrev_buffer
411 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
412 stash->syms);
252b5132
RH
413 if (! stash->dwarf_abbrev_buffer)
414 return 0;
252b5132
RH
415 }
416
f5198f61 417 if (offset >= stash->dwarf_abbrev_size)
252b5132 418 {
f46c2da6
AM
419 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
420 (unsigned long) offset, stash->dwarf_abbrev_size);
252b5132
RH
421 bfd_set_error (bfd_error_bad_value);
422 return 0;
423 }
424
dc810e39 425 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
818a27ac 426 abbrevs = bfd_zalloc (abfd, amt);
252b5132
RH
427
428 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
429 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
430 abbrev_ptr += bytes_read;
431
a092b084 432 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
433 while (abbrev_number)
434 {
dc810e39 435 amt = sizeof (struct abbrev_info);
818a27ac 436 cur_abbrev = bfd_zalloc (abfd, amt);
252b5132 437
a092b084 438 /* Read in abbrev header. */
252b5132 439 cur_abbrev->number = abbrev_number;
d45913a0
DA
440 cur_abbrev->tag = (enum dwarf_tag)
441 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
252b5132
RH
442 abbrev_ptr += bytes_read;
443 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
444 abbrev_ptr += 1;
445
a092b084 446 /* Now read in declarations. */
252b5132
RH
447 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
448 abbrev_ptr += bytes_read;
449 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
450 abbrev_ptr += bytes_read;
98591c73 451
252b5132
RH
452 while (abbrev_name)
453 {
454 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
455 {
35330cce
NC
456 struct attr_abbrev *tmp;
457
dc810e39
AM
458 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
459 amt *= sizeof (struct attr_abbrev);
35330cce
NC
460 tmp = bfd_realloc (cur_abbrev->attrs, amt);
461 if (tmp == NULL)
462 {
463 size_t i;
464
465 for (i = 0; i < ABBREV_HASH_SIZE; i++)
466 {
467 struct abbrev_info *abbrev = abbrevs[i];
468
469 while (abbrev)
470 {
471 free (abbrev->attrs);
472 abbrev = abbrev->next;
473 }
474 }
475 return NULL;
476 }
477 cur_abbrev->attrs = tmp;
252b5132 478 }
98591c73 479
d45913a0
DA
480 cur_abbrev->attrs[cur_abbrev->num_attrs].name
481 = (enum dwarf_attribute) abbrev_name;
482 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
483 = (enum dwarf_form) abbrev_form;
252b5132
RH
484 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
485 abbrev_ptr += bytes_read;
486 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
487 abbrev_ptr += bytes_read;
488 }
489
490 hash_number = abbrev_number % ABBREV_HASH_SIZE;
491 cur_abbrev->next = abbrevs[hash_number];
492 abbrevs[hash_number] = cur_abbrev;
493
494 /* Get next abbreviation.
e82ce529 495 Under Irix6 the abbreviations for a compilation unit are not
252b5132
RH
496 always properly terminated with an abbrev number of 0.
497 Exit loop if we encounter an abbreviation which we have
498 already read (which means we are about to read the abbreviations
499 for the next compile unit) or if the end of the abbreviation
500 table is reached. */
501 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
502 >= stash->dwarf_abbrev_size)
503 break;
504 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
505 abbrev_ptr += bytes_read;
506 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
507 break;
508 }
509
510 return abbrevs;
511}
512
cf716c56 513/* Read an attribute value described by an attribute form. */
252b5132 514
f075ee0c 515static bfd_byte *
818a27ac
AM
516read_attribute_value (struct attribute *attr,
517 unsigned form,
518 struct comp_unit *unit,
f075ee0c 519 bfd_byte *info_ptr)
252b5132
RH
520{
521 bfd *abfd = unit->abfd;
522 unsigned int bytes_read;
523 struct dwarf_block *blk;
dc810e39 524 bfd_size_type amt;
252b5132 525
d45913a0 526 attr->form = (enum dwarf_form) form;
98591c73 527
cf716c56 528 switch (form)
252b5132
RH
529 {
530 case DW_FORM_addr:
0cc1cf99 531 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
252b5132 532 case DW_FORM_ref_addr:
482e2e37 533 attr->u.val = read_address (unit, info_ptr);
252b5132
RH
534 info_ptr += unit->addr_size;
535 break;
536 case DW_FORM_block2:
dc810e39 537 amt = sizeof (struct dwarf_block);
818a27ac 538 blk = bfd_alloc (abfd, amt);
252b5132
RH
539 blk->size = read_2_bytes (abfd, info_ptr);
540 info_ptr += 2;
541 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
542 info_ptr += blk->size;
482e2e37 543 attr->u.blk = blk;
252b5132
RH
544 break;
545 case DW_FORM_block4:
dc810e39 546 amt = sizeof (struct dwarf_block);
818a27ac 547 blk = bfd_alloc (abfd, amt);
252b5132
RH
548 blk->size = read_4_bytes (abfd, info_ptr);
549 info_ptr += 4;
550 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
551 info_ptr += blk->size;
482e2e37 552 attr->u.blk = blk;
252b5132
RH
553 break;
554 case DW_FORM_data2:
482e2e37 555 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
556 info_ptr += 2;
557 break;
558 case DW_FORM_data4:
482e2e37 559 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
560 info_ptr += 4;
561 break;
562 case DW_FORM_data8:
482e2e37 563 attr->u.val = read_8_bytes (abfd, info_ptr);
252b5132
RH
564 info_ptr += 8;
565 break;
566 case DW_FORM_string:
482e2e37 567 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
252b5132
RH
568 info_ptr += bytes_read;
569 break;
d03ba2a1 570 case DW_FORM_strp:
482e2e37 571 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
d03ba2a1
JJ
572 info_ptr += bytes_read;
573 break;
252b5132 574 case DW_FORM_block:
dc810e39 575 amt = sizeof (struct dwarf_block);
818a27ac 576 blk = bfd_alloc (abfd, amt);
252b5132
RH
577 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
578 info_ptr += bytes_read;
579 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
580 info_ptr += blk->size;
482e2e37 581 attr->u.blk = blk;
252b5132
RH
582 break;
583 case DW_FORM_block1:
dc810e39 584 amt = sizeof (struct dwarf_block);
818a27ac 585 blk = bfd_alloc (abfd, amt);
252b5132
RH
586 blk->size = read_1_byte (abfd, info_ptr);
587 info_ptr += 1;
588 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
589 info_ptr += blk->size;
482e2e37 590 attr->u.blk = blk;
252b5132
RH
591 break;
592 case DW_FORM_data1:
482e2e37 593 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
594 info_ptr += 1;
595 break;
596 case DW_FORM_flag:
482e2e37 597 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
598 info_ptr += 1;
599 break;
600 case DW_FORM_sdata:
482e2e37 601 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
602 info_ptr += bytes_read;
603 break;
604 case DW_FORM_udata:
482e2e37 605 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
606 info_ptr += bytes_read;
607 break;
608 case DW_FORM_ref1:
482e2e37 609 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
610 info_ptr += 1;
611 break;
612 case DW_FORM_ref2:
482e2e37 613 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
614 info_ptr += 2;
615 break;
616 case DW_FORM_ref4:
482e2e37 617 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
618 info_ptr += 4;
619 break;
81edd86d 620 case DW_FORM_ref8:
482e2e37 621 attr->u.val = read_8_bytes (abfd, info_ptr);
81edd86d
MM
622 info_ptr += 8;
623 break;
252b5132 624 case DW_FORM_ref_udata:
482e2e37 625 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
626 info_ptr += bytes_read;
627 break;
252b5132 628 case DW_FORM_indirect:
cf716c56
RH
629 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
630 info_ptr += bytes_read;
631 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
632 break;
252b5132 633 default:
f46c2da6 634 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
cf716c56 635 form);
252b5132
RH
636 bfd_set_error (bfd_error_bad_value);
637 }
638 return info_ptr;
639}
640
cf716c56
RH
641/* Read an attribute described by an abbreviated attribute. */
642
f075ee0c 643static bfd_byte *
818a27ac
AM
644read_attribute (struct attribute *attr,
645 struct attr_abbrev *abbrev,
646 struct comp_unit *unit,
f075ee0c 647 bfd_byte *info_ptr)
cf716c56
RH
648{
649 attr->name = abbrev->name;
650 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
651 return info_ptr;
652}
653
a092b084 654/* Source line information table routines. */
252b5132
RH
655
656#define FILE_ALLOC_CHUNK 5
657#define DIR_ALLOC_CHUNK 5
658
a092b084
NC
659struct line_info
660{
252b5132 661 struct line_info* prev_line;
252b5132 662 bfd_vma address;
f075ee0c 663 char *filename;
252b5132
RH
664 unsigned int line;
665 unsigned int column;
a092b084 666 int end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
667};
668
a092b084
NC
669struct fileinfo
670{
252b5132
RH
671 char *name;
672 unsigned int dir;
673 unsigned int time;
674 unsigned int size;
675};
676
a092b084
NC
677struct line_info_table
678{
252b5132 679 bfd* abfd;
252b5132
RH
680 unsigned int num_files;
681 unsigned int num_dirs;
f075ee0c
AM
682 char *comp_dir;
683 char **dirs;
252b5132 684 struct fileinfo* files;
e82ce529
AM
685 struct line_info* last_line; /* largest VMA */
686 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
252b5132
RH
687};
688
4ab527b0
FF
689/* Remember some information about each function. If the function is
690 inlined (DW_TAG_inlined_subroutine) it may have two additional
691 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
692 source code location where this function was inlined. */
693
1ee24f27
DJ
694struct funcinfo
695{
4ab527b0
FF
696 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
697 struct funcinfo *caller_func; /* Pointer to function one scope higher */
698 char *caller_file; /* Source location file name where caller_func inlines this func */
699 int caller_line; /* Source location line number where caller_func inlines this func */
700 int tag;
701 int nesting_level;
f075ee0c 702 char *name;
a13afe8e 703 struct arange arange;
1ee24f27
DJ
704};
705
af3ef9fe
NC
706/* Adds a new entry to the line_info list in the line_info_table, ensuring
707 that the list is sorted. Note that the line_info list is sorted from
708 highest to lowest VMA (with possible duplicates); that is,
709 line_info->prev_line always accesses an equal or smaller VMA. */
710
98591c73 711static void
818a27ac
AM
712add_line_info (struct line_info_table *table,
713 bfd_vma address,
714 char *filename,
715 unsigned int line,
716 unsigned int column,
717 int end_sequence)
252b5132 718{
dc810e39 719 bfd_size_type amt = sizeof (struct line_info);
818a27ac 720 struct line_info* info = bfd_alloc (table->abfd, amt);
252b5132 721
e82ce529
AM
722 /* Find the correct location for 'info'. Normally we will receive
723 new line_info data 1) in order and 2) with increasing VMAs.
724 However some compilers break the rules (cf. decode_line_info) and
725 so we include some heuristics for quickly finding the correct
726 location for 'info'. In particular, these heuristics optimize for
727 the common case in which the VMA sequence that we receive is a
728 list of locally sorted VMAs such as
729 p...z a...j (where a < j < p < z)
252b5132 730
e82ce529
AM
731 Note: table->lcl_head is used to head an *actual* or *possible*
732 sequence within the list (such as a...j) that is not directly
733 headed by table->last_line
734
735 Note: we may receive duplicate entries from 'decode_line_info'. */
736
737 while (1)
738 if (!table->last_line
739 || address >= table->last_line->address)
740 {
741 /* Normal case: add 'info' to the beginning of the list */
742 info->prev_line = table->last_line;
743 table->last_line = info;
744
745 /* lcl_head: initialize to head a *possible* sequence at the end. */
746 if (!table->lcl_head)
747 table->lcl_head = info;
748 break;
749 }
750 else if (!table->lcl_head->prev_line
751 && table->lcl_head->address > address)
752 {
753 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
754 list and 2) the head of 'info'. */
755 info->prev_line = NULL;
756 table->lcl_head->prev_line = info;
757 break;
758 }
759 else if (table->lcl_head->prev_line
760 && table->lcl_head->address > address
761 && address >= table->lcl_head->prev_line->address)
762 {
763 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
764 list and 2) the head of 'info'. */
765 info->prev_line = table->lcl_head->prev_line;
766 table->lcl_head->prev_line = info;
767 break;
768 }
769 else
770 {
771 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
772 heads for 'info'. Reset 'lcl_head' and repeat. */
773 struct line_info* li2 = table->last_line; /* always non-NULL */
774 struct line_info* li1 = li2->prev_line;
775
776 while (li1)
777 {
778 if (li2->address > address && address >= li1->address)
779 break;
780
781 li2 = li1; /* always non-NULL */
782 li1 = li1->prev_line;
783 }
784 table->lcl_head = li2;
785 }
786
787 /* Set member data of 'info'. */
252b5132 788 info->address = address;
252b5132
RH
789 info->line = line;
790 info->column = column;
159002ff 791 info->end_sequence = end_sequence;
d63fd5d1 792
eb61d2d6 793 if (filename && filename[0])
d63fd5d1 794 {
eb61d2d6 795 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
d63fd5d1
NC
796 if (info->filename)
797 strcpy (info->filename, filename);
798 }
799 else
800 info->filename = NULL;
252b5132
RH
801}
802
5ed6aba4 803/* Extract a fully qualified filename from a line info table.
af3ef9fe
NC
804 The returned string has been malloc'ed and it is the caller's
805 responsibility to free it. */
5ed6aba4 806
a092b084 807static char *
818a27ac 808concat_filename (struct line_info_table *table, unsigned int file)
252b5132 809{
f075ee0c 810 char *filename;
159002ff
RH
811
812 if (file - 1 >= table->num_files)
813 {
dcdea4f4
AM
814 (*_bfd_error_handler)
815 (_("Dwarf Error: mangled line number section (bad file number)."));
af3ef9fe 816 return strdup ("<unknown>");
159002ff
RH
817 }
818
819 filename = table->files[file - 1].name;
5ed6aba4 820
af3ef9fe 821 if (! IS_ABSOLUTE_PATH (filename))
252b5132 822 {
f075ee0c 823 char *dirname = (table->files[file - 1].dir
252b5132
RH
824 ? table->dirs[table->files[file - 1].dir - 1]
825 : table->comp_dir);
0dafd5f6 826
af3ef9fe
NC
827 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
828 The best we can do is return the filename part. */
829 if (dirname != NULL)
830 {
831 unsigned int len = strlen (dirname) + strlen (filename) + 2;
832 char * name;
833
834 name = bfd_malloc (len);
835 if (name)
836 sprintf (name, "%s/%s", dirname, filename);
837 return name;
838 }
252b5132 839 }
af3ef9fe
NC
840
841 return strdup (filename);
252b5132
RH
842}
843
f623be2b 844static void
a13afe8e 845arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
f623be2b
RH
846{
847 struct arange *arange;
848
a13afe8e
FF
849 /* If the first arange is empty, use it. */
850 if (first_arange->high == 0)
851 {
852 first_arange->low = low_pc;
853 first_arange->high = high_pc;
854 return;
855 }
98591c73 856
a13afe8e
FF
857 /* Next see if we can cheaply extend an existing range. */
858 arange = first_arange;
f623be2b
RH
859 do
860 {
861 if (low_pc == arange->high)
862 {
863 arange->high = high_pc;
864 return;
865 }
866 if (high_pc == arange->low)
867 {
868 arange->low = low_pc;
869 return;
870 }
871 arange = arange->next;
872 }
873 while (arange);
874
a13afe8e
FF
875 /* Need to allocate a new arange and insert it into the arange list.
876 Order isn't significant, so just insert after the first arange. */
877 arange = bfd_zalloc (abfd, sizeof (*arange));
f623be2b
RH
878 arange->low = low_pc;
879 arange->high = high_pc;
a13afe8e
FF
880 arange->next = first_arange->next;
881 first_arange->next = arange;
f623be2b
RH
882}
883
a092b084 884/* Decode the line number information for UNIT. */
252b5132
RH
885
886static struct line_info_table*
818a27ac 887decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
252b5132
RH
888{
889 bfd *abfd = unit->abfd;
252b5132 890 struct line_info_table* table;
f075ee0c
AM
891 bfd_byte *line_ptr;
892 bfd_byte *line_end;
252b5132 893 struct line_head lh;
d03ba2a1 894 unsigned int i, bytes_read, offset_size;
252b5132
RH
895 char *cur_file, *cur_dir;
896 unsigned char op_code, extended_op, adj_opcode;
dc810e39 897 bfd_size_type amt;
252b5132 898
69dd2e2d 899 if (! stash->dwarf_line_buffer)
252b5132
RH
900 {
901 asection *msec;
252b5132
RH
902
903 msec = bfd_get_section_by_name (abfd, ".debug_line");
904 if (! msec)
905 {
906 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
907 bfd_set_error (bfd_error_bad_value);
908 return 0;
909 }
98591c73 910
eea6121a 911 stash->dwarf_line_size = msec->size;
6e84a906
DJ
912 stash->dwarf_line_buffer
913 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
914 stash->syms);
f623be2b 915 if (! stash->dwarf_line_buffer)
252b5132 916 return 0;
252b5132
RH
917 }
918
6e84a906
DJ
919 /* It is possible to get a bad value for the line_offset. Validate
920 it here so that we won't get a segfault below. */
ccdb16fc
JW
921 if (unit->line_offset >= stash->dwarf_line_size)
922 {
f46c2da6 923 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
ccdb16fc
JW
924 unit->line_offset, stash->dwarf_line_size);
925 bfd_set_error (bfd_error_bad_value);
926 return 0;
927 }
928
dc810e39 929 amt = sizeof (struct line_info_table);
818a27ac 930 table = bfd_alloc (abfd, amt);
252b5132
RH
931 table->abfd = abfd;
932 table->comp_dir = unit->comp_dir;
933
934 table->num_files = 0;
935 table->files = NULL;
936
937 table->num_dirs = 0;
938 table->dirs = NULL;
939
159002ff
RH
940 table->files = NULL;
941 table->last_line = NULL;
e82ce529 942 table->lcl_head = NULL;
159002ff 943
69dd2e2d 944 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 945
a092b084 946 /* Read in the prologue. */
91a4d569
AM
947 lh.total_length = read_4_bytes (abfd, line_ptr);
948 line_ptr += 4;
949 offset_size = 4;
950 if (lh.total_length == 0xffffffff)
dae2dd0d 951 {
dae2dd0d
NC
952 lh.total_length = read_8_bytes (abfd, line_ptr);
953 line_ptr += 8;
954 offset_size = 8;
955 }
91a4d569 956 else if (lh.total_length == 0 && unit->addr_size == 8)
d03ba2a1 957 {
91a4d569
AM
958 /* Handle (non-standard) 64-bit DWARF2 formats. */
959 lh.total_length = read_4_bytes (abfd, line_ptr);
960 line_ptr += 4;
d03ba2a1
JJ
961 offset_size = 8;
962 }
252b5132
RH
963 line_end = line_ptr + lh.total_length;
964 lh.version = read_2_bytes (abfd, line_ptr);
965 line_ptr += 2;
d03ba2a1
JJ
966 if (offset_size == 4)
967 lh.prologue_length = read_4_bytes (abfd, line_ptr);
968 else
969 lh.prologue_length = read_8_bytes (abfd, line_ptr);
970 line_ptr += offset_size;
252b5132
RH
971 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
972 line_ptr += 1;
973 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
974 line_ptr += 1;
975 lh.line_base = read_1_signed_byte (abfd, line_ptr);
976 line_ptr += 1;
977 lh.line_range = read_1_byte (abfd, line_ptr);
978 line_ptr += 1;
979 lh.opcode_base = read_1_byte (abfd, line_ptr);
980 line_ptr += 1;
dc810e39 981 amt = lh.opcode_base * sizeof (unsigned char);
818a27ac 982 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
252b5132
RH
983
984 lh.standard_opcode_lengths[0] = 1;
98591c73 985
252b5132
RH
986 for (i = 1; i < lh.opcode_base; ++i)
987 {
988 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
989 line_ptr += 1;
990 }
991
a092b084 992 /* Read directory table. */
252b5132
RH
993 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
994 {
995 line_ptr += bytes_read;
98591c73 996
252b5132
RH
997 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
998 {
35330cce
NC
999 char **tmp;
1000
dc810e39
AM
1001 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1002 amt *= sizeof (char *);
35330cce
NC
1003
1004 tmp = bfd_realloc (table->dirs, amt);
1005 if (tmp == NULL)
1006 {
1007 free (table->dirs);
1008 return NULL;
1009 }
1010 table->dirs = tmp;
252b5132 1011 }
98591c73 1012
252b5132
RH
1013 table->dirs[table->num_dirs++] = cur_dir;
1014 }
98591c73 1015
252b5132
RH
1016 line_ptr += bytes_read;
1017
a092b084 1018 /* Read file name table. */
252b5132
RH
1019 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1020 {
1021 line_ptr += bytes_read;
98591c73 1022
252b5132
RH
1023 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1024 {
35330cce
NC
1025 struct fileinfo *tmp;
1026
dc810e39
AM
1027 amt = table->num_files + FILE_ALLOC_CHUNK;
1028 amt *= sizeof (struct fileinfo);
35330cce
NC
1029
1030 tmp = bfd_realloc (table->files, amt);
1031 if (tmp == NULL)
1032 {
1033 free (table->files);
1034 free (table->dirs);
1035 return NULL;
1036 }
1037 table->files = tmp;
252b5132 1038 }
98591c73 1039
252b5132
RH
1040 table->files[table->num_files].name = cur_file;
1041 table->files[table->num_files].dir =
1042 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1043 line_ptr += bytes_read;
1044 table->files[table->num_files].time =
1045 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1046 line_ptr += bytes_read;
1047 table->files[table->num_files].size =
1048 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1049 line_ptr += bytes_read;
1050 table->num_files++;
1051 }
98591c73 1052
252b5132
RH
1053 line_ptr += bytes_read;
1054
1055 /* Read the statement sequences until there's nothing left. */
1056 while (line_ptr < line_end)
1057 {
a092b084 1058 /* State machine registers. */
252b5132 1059 bfd_vma address = 0;
8bfd78b3 1060 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
252b5132
RH
1061 unsigned int line = 1;
1062 unsigned int column = 0;
1063 int is_stmt = lh.default_is_stmt;
1064 int basic_block = 0;
e2f6d277
NC
1065 int end_sequence = 0;
1066 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
e82ce529
AM
1067 compilers generate address sequences that are wildly out of
1068 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1069 for ia64-Linux). Thus, to determine the low and high
1070 address, we must compare on every DW_LNS_copy, etc. */
e2f6d277
NC
1071 bfd_vma low_pc = 0;
1072 bfd_vma high_pc = 0;
252b5132 1073
a092b084 1074 /* Decode the table. */
252b5132
RH
1075 while (! end_sequence)
1076 {
1077 op_code = read_1_byte (abfd, line_ptr);
1078 line_ptr += 1;
98591c73 1079
1a509dcc 1080 if (op_code >= lh.opcode_base)
e2f6d277
NC
1081 {
1082 /* Special operand. */
1a509dcc
GK
1083 adj_opcode = op_code - lh.opcode_base;
1084 address += (adj_opcode / lh.line_range)
1085 * lh.minimum_instruction_length;
1086 line += lh.line_base + (adj_opcode % lh.line_range);
1087 /* Append row to matrix using current values. */
1088 add_line_info (table, address, filename, line, column, 0);
1089 basic_block = 1;
e2f6d277
NC
1090 if (low_pc == 0 || address < low_pc)
1091 low_pc = address;
1092 if (address > high_pc)
1093 high_pc = address;
1a509dcc
GK
1094 }
1095 else switch (op_code)
252b5132
RH
1096 {
1097 case DW_LNS_extended_op:
e2f6d277
NC
1098 /* Ignore length. */
1099 line_ptr += 1;
252b5132
RH
1100 extended_op = read_1_byte (abfd, line_ptr);
1101 line_ptr += 1;
e2f6d277 1102
252b5132
RH
1103 switch (extended_op)
1104 {
1105 case DW_LNE_end_sequence:
1106 end_sequence = 1;
f623be2b
RH
1107 add_line_info (table, address, filename, line, column,
1108 end_sequence);
e2f6d277
NC
1109 if (low_pc == 0 || address < low_pc)
1110 low_pc = address;
1111 if (address > high_pc)
1112 high_pc = address;
a13afe8e 1113 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
252b5132
RH
1114 break;
1115 case DW_LNE_set_address:
1116 address = read_address (unit, line_ptr);
1117 line_ptr += unit->addr_size;
1118 break;
1119 case DW_LNE_define_file:
1120 cur_file = read_string (abfd, line_ptr, &bytes_read);
1121 line_ptr += bytes_read;
1122 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1123 {
35330cce
NC
1124 struct fileinfo *tmp;
1125
dc810e39
AM
1126 amt = table->num_files + FILE_ALLOC_CHUNK;
1127 amt *= sizeof (struct fileinfo);
35330cce
NC
1128 tmp = bfd_realloc (table->files, amt);
1129 if (tmp == NULL)
1130 {
1131 free (table->files);
1132 free (table->dirs);
1133 free (filename);
1134 return NULL;
1135 }
1136 table->files = tmp;
252b5132
RH
1137 }
1138 table->files[table->num_files].name = cur_file;
1139 table->files[table->num_files].dir =
1140 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1141 line_ptr += bytes_read;
1142 table->files[table->num_files].time =
1143 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1144 line_ptr += bytes_read;
1145 table->files[table->num_files].size =
1146 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1147 line_ptr += bytes_read;
1148 table->num_files++;
1149 break;
1150 default:
1151 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1152 bfd_set_error (bfd_error_bad_value);
35330cce
NC
1153 free (filename);
1154 free (table->files);
1155 free (table->dirs);
1156 return NULL;
252b5132
RH
1157 }
1158 break;
1159 case DW_LNS_copy:
159002ff 1160 add_line_info (table, address, filename, line, column, 0);
252b5132 1161 basic_block = 0;
e2f6d277
NC
1162 if (low_pc == 0 || address < low_pc)
1163 low_pc = address;
1164 if (address > high_pc)
1165 high_pc = address;
252b5132
RH
1166 break;
1167 case DW_LNS_advance_pc:
1168 address += lh.minimum_instruction_length
1169 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1170 line_ptr += bytes_read;
1171 break;
1172 case DW_LNS_advance_line:
1173 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1174 line_ptr += bytes_read;
1175 break;
1176 case DW_LNS_set_file:
1177 {
1178 unsigned int file;
1179
e2f6d277
NC
1180 /* The file and directory tables are 0
1181 based, the references are 1 based. */
252b5132
RH
1182 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1183 line_ptr += bytes_read;
af3ef9fe
NC
1184 if (filename)
1185 free (filename);
252b5132
RH
1186 filename = concat_filename (table, file);
1187 break;
1188 }
1189 case DW_LNS_set_column:
1190 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1191 line_ptr += bytes_read;
1192 break;
1193 case DW_LNS_negate_stmt:
1194 is_stmt = (!is_stmt);
1195 break;
1196 case DW_LNS_set_basic_block:
1197 basic_block = 1;
1198 break;
1199 case DW_LNS_const_add_pc:
159002ff
RH
1200 address += lh.minimum_instruction_length
1201 * ((255 - lh.opcode_base) / lh.line_range);
252b5132
RH
1202 break;
1203 case DW_LNS_fixed_advance_pc:
1204 address += read_2_bytes (abfd, line_ptr);
1205 line_ptr += 2;
1206 break;
1a509dcc 1207 default:
e2f6d277 1208 {
1a509dcc 1209 int i;
5ed6aba4 1210
e2f6d277 1211 /* Unknown standard opcode, ignore it. */
1a509dcc
GK
1212 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1213 {
1214 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1215 line_ptr += bytes_read;
1216 }
1217 }
252b5132
RH
1218 }
1219 }
5ed6aba4 1220
af3ef9fe
NC
1221 if (filename)
1222 free (filename);
252b5132
RH
1223 }
1224
1225 return table;
1226}
1227
b34976b6
AM
1228/* If ADDR is within TABLE set the output parameters and return TRUE,
1229 otherwise return FALSE. The output parameters, FILENAME_PTR and
a092b084 1230 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132 1231
b34976b6 1232static bfd_boolean
818a27ac
AM
1233lookup_address_in_line_info_table (struct line_info_table *table,
1234 bfd_vma addr,
1235 struct funcinfo *function,
1236 const char **filename_ptr,
1237 unsigned int *linenumber_ptr)
252b5132 1238{
e82ce529 1239 /* Note: table->last_line should be a descendingly sorted list. */
159002ff 1240 struct line_info* next_line = table->last_line;
e82ce529
AM
1241 struct line_info* each_line = NULL;
1242 *filename_ptr = NULL;
98591c73 1243
159002ff 1244 if (!next_line)
b34976b6 1245 return FALSE;
159002ff
RH
1246
1247 each_line = next_line->prev_line;
1248
e82ce529
AM
1249 /* Check for large addresses */
1250 if (addr > next_line->address)
1251 each_line = NULL; /* ensure we skip over the normal case */
1252
1253 /* Normal case: search the list; save */
159002ff 1254 while (each_line && next_line)
252b5132 1255 {
e82ce529
AM
1256 /* If we have an address match, save this info. This allows us
1257 to return as good as results as possible for strange debugging
1258 info. */
b34976b6 1259 bfd_boolean addr_match = FALSE;
a13afe8e 1260 if (each_line->address <= addr && addr < next_line->address)
252b5132 1261 {
b34976b6 1262 addr_match = TRUE;
e82ce529 1263
1ee24f27
DJ
1264 /* If this line appears to span functions, and addr is in the
1265 later function, return the first line of that function instead
1266 of the last line of the earlier one. This check is for GCC
1267 2.95, which emits the first line number for a function late. */
a13afe8e
FF
1268
1269 if (function != NULL)
1ee24f27 1270 {
a13afe8e
FF
1271 bfd_vma lowest_pc;
1272 struct arange *arange;
1273
1274 /* Find the lowest address in the function's range list */
1275 lowest_pc = function->arange.low;
1276 for (arange = &function->arange;
1277 arange;
1278 arange = arange->next)
1279 {
1280 if (function->arange.low < lowest_pc)
1281 lowest_pc = function->arange.low;
1282 }
1283 /* Check for spanning function and set outgoing line info */
1284 if (addr >= lowest_pc
1285 && each_line->address < lowest_pc
1286 && next_line->address > lowest_pc)
1287 {
1288 *filename_ptr = next_line->filename;
1289 *linenumber_ptr = next_line->line;
1290 }
1291 else
1292 {
1293 *filename_ptr = each_line->filename;
1294 *linenumber_ptr = each_line->line;
1295 }
1ee24f27 1296 }
252b5132 1297 }
e82ce529
AM
1298
1299 if (addr_match && !each_line->end_sequence)
b34976b6 1300 return TRUE; /* we have definitely found what we want */
e82ce529 1301
159002ff
RH
1302 next_line = each_line;
1303 each_line = each_line->prev_line;
252b5132 1304 }
98591c73 1305
e82ce529
AM
1306 /* At this point each_line is NULL but next_line is not. If we found
1307 a candidate end-of-sequence point in the loop above, we can return
1308 that (compatibility with a bug in the Intel compiler); otherwise,
1309 assuming that we found the containing function for this address in
1310 this compilation unit, return the first line we have a number for
1311 (compatibility with GCC 2.95). */
1312 if (*filename_ptr == NULL && function != NULL)
1ee24f27
DJ
1313 {
1314 *filename_ptr = next_line->filename;
1315 *linenumber_ptr = next_line->line;
b34976b6 1316 return TRUE;
1ee24f27
DJ
1317 }
1318
b34976b6 1319 return FALSE;
252b5132 1320}
98591c73 1321
a13afe8e
FF
1322/* Read in the .debug_ranges section for future reference */
1323
1324static bfd_boolean
1325read_debug_ranges (struct comp_unit *unit)
1326{
1327 struct dwarf2_debug *stash = unit->stash;
1328 if (! stash->dwarf_ranges_buffer)
1329 {
1330 bfd *abfd = unit->abfd;
1331 asection *msec;
1332
1333 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1334 if (! msec)
1335 {
1336 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1337 bfd_set_error (bfd_error_bad_value);
1338 return FALSE;
1339 }
1340
1341 stash->dwarf_ranges_size = msec->size;
1342 stash->dwarf_ranges_buffer
1343 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1344 stash->syms);
1345 if (! stash->dwarf_ranges_buffer)
1346 return FALSE;
1347 }
1348 return TRUE;
1349}
1350
a092b084 1351/* Function table functions. */
252b5132 1352
a13afe8e
FF
1353/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1354 Note that we need to find the function that has the smallest
1355 range that contains ADDR, to handle inlined functions without
1356 depending upon them being ordered in TABLE by increasing range. */
252b5132 1357
b34976b6 1358static bfd_boolean
4ab527b0 1359lookup_address_in_function_table (struct comp_unit *unit,
818a27ac
AM
1360 bfd_vma addr,
1361 struct funcinfo **function_ptr,
1362 const char **functionname_ptr)
252b5132
RH
1363{
1364 struct funcinfo* each_func;
a13afe8e
FF
1365 struct funcinfo* best_fit = NULL;
1366 struct arange *arange;
252b5132 1367
4ab527b0 1368 for (each_func = unit->function_table;
252b5132
RH
1369 each_func;
1370 each_func = each_func->prev_func)
1371 {
a13afe8e
FF
1372 for (arange = &each_func->arange;
1373 arange;
1374 arange = arange->next)
252b5132 1375 {
a13afe8e
FF
1376 if (addr >= arange->low && addr < arange->high)
1377 {
1378 if (!best_fit ||
1379 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1380 best_fit = each_func;
1381 }
252b5132
RH
1382 }
1383 }
98591c73 1384
a13afe8e
FF
1385 if (best_fit)
1386 {
4ab527b0
FF
1387 struct funcinfo* curr_func = best_fit;
1388
a13afe8e
FF
1389 *functionname_ptr = best_fit->name;
1390 *function_ptr = best_fit;
4ab527b0
FF
1391
1392 /* If we found a match and it is a function that was inlined,
1393 traverse the function list looking for the function at the
1394 next higher scope and save a pointer to it for future use.
1395 Note that because of the way the DWARF info is generated, and
1396 the way we build the function list, the first function at the
1397 next higher level is the one we want. */
1398
1399 for (each_func = best_fit -> prev_func;
1400 each_func && (curr_func->tag == DW_TAG_inlined_subroutine);
1401 each_func = each_func->prev_func)
1402 {
1403 if (each_func->nesting_level < curr_func->nesting_level)
1404 {
1405 curr_func->caller_func = each_func;
1406 curr_func = each_func;
1407 }
1408 }
a13afe8e
FF
1409 return TRUE;
1410 }
1411 else
1412 {
1413 return FALSE;
1414 }
252b5132
RH
1415}
1416
06f22d7e
FF
1417static char *
1418find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1419{
1420 bfd *abfd = unit->abfd;
f075ee0c 1421 bfd_byte *info_ptr;
06f22d7e
FF
1422 unsigned int abbrev_number, bytes_read, i;
1423 struct abbrev_info *abbrev;
1424 struct attribute attr;
1425 char *name = 0;
1426
c0c28ab8 1427 info_ptr = unit->info_ptr_unit + die_ref;
06f22d7e
FF
1428 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1429 info_ptr += bytes_read;
1430
1431 if (abbrev_number)
1432 {
1433 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1434 if (! abbrev)
1435 {
1436 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1437 abbrev_number);
1438 bfd_set_error (bfd_error_bad_value);
1439 }
1440 else
1441 {
1442 for (i = 0; i < abbrev->num_attrs && !name; ++i)
1443 {
1444 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
26bf4e33
FF
1445 switch (attr.name)
1446 {
1447 case DW_AT_name:
1448 name = attr.u.str;
1449 break;
1450 case DW_AT_specification:
1451 name = find_abstract_instance_name (unit, attr.u.val);
1452 break;
1453 default:
1454 break;
1455 }
06f22d7e
FF
1456 }
1457 }
1458 }
1459 return (name);
1460}
1461
a13afe8e
FF
1462static void
1463read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1464{
1465 bfd_byte *ranges_ptr;
1466 bfd_vma base_address = unit->base_address;
1467
1468 if (! unit->stash->dwarf_ranges_buffer)
1469 {
1470 if (! read_debug_ranges (unit))
1471 return;
1472 }
1473 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1474
1475 for (;;)
1476 {
1477 bfd_vma low_pc;
1478 bfd_vma high_pc;
1479
1480 if (unit->offset_size == 4)
1481 {
1482 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1483 ranges_ptr += 4;
1484 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1485 ranges_ptr += 4;
1486 }
1487 else
1488 {
1489 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1490 ranges_ptr += 8;
1491 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1492 ranges_ptr += 8;
1493 }
1494 if (low_pc == 0 && high_pc == 0)
1495 break;
1496 if (low_pc == -1UL && high_pc != -1UL)
1497 base_address = high_pc;
1498 else
1499 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1500 }
1501}
1502
a092b084 1503/* DWARF2 Compilation unit functions. */
252b5132
RH
1504
1505/* Scan over each die in a comp. unit looking for functions to add
a092b084 1506 to the function table. */
252b5132 1507
b34976b6 1508static bfd_boolean
818a27ac 1509scan_unit_for_functions (struct comp_unit *unit)
252b5132
RH
1510{
1511 bfd *abfd = unit->abfd;
f075ee0c 1512 bfd_byte *info_ptr = unit->first_child_die_ptr;
252b5132
RH
1513 int nesting_level = 1;
1514
1515 while (nesting_level)
1516 {
1517 unsigned int abbrev_number, bytes_read, i;
1518 struct abbrev_info *abbrev;
1519 struct attribute attr;
1520 struct funcinfo *func;
a13afe8e
FF
1521 bfd_vma low_pc = 0;
1522 bfd_vma high_pc = 0;
252b5132
RH
1523
1524 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1525 info_ptr += bytes_read;
1526
1527 if (! abbrev_number)
1528 {
1529 nesting_level--;
1530 continue;
1531 }
98591c73 1532
252b5132
RH
1533 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1534 if (! abbrev)
1535 {
f46c2da6 1536 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1537 abbrev_number);
1538 bfd_set_error (bfd_error_bad_value);
b34976b6 1539 return FALSE;
252b5132 1540 }
98591c73 1541
06f22d7e
FF
1542 if (abbrev->tag == DW_TAG_subprogram
1543 || abbrev->tag == DW_TAG_inlined_subroutine)
252b5132 1544 {
dc810e39 1545 bfd_size_type amt = sizeof (struct funcinfo);
818a27ac 1546 func = bfd_zalloc (abfd, amt);
4ab527b0
FF
1547 func->tag = abbrev->tag;
1548 func->nesting_level = nesting_level;
252b5132
RH
1549 func->prev_func = unit->function_table;
1550 unit->function_table = func;
1551 }
1552 else
1553 func = NULL;
98591c73 1554
252b5132
RH
1555 for (i = 0; i < abbrev->num_attrs; ++i)
1556 {
1557 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
98591c73 1558
252b5132
RH
1559 if (func)
1560 {
1561 switch (attr.name)
1562 {
4ab527b0
FF
1563 case DW_AT_call_file:
1564 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1565 break;
1566
1567 case DW_AT_call_line:
1568 func->caller_line = attr.u.val;
1569 break;
1570
06f22d7e
FF
1571 case DW_AT_abstract_origin:
1572 func->name = find_abstract_instance_name (unit, attr.u.val);
1573 break;
1574
252b5132 1575 case DW_AT_name:
252b5132
RH
1576 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1577 if (func->name == NULL)
482e2e37 1578 func->name = attr.u.str;
252b5132 1579 break;
98591c73 1580
252b5132 1581 case DW_AT_MIPS_linkage_name:
482e2e37 1582 func->name = attr.u.str;
252b5132
RH
1583 break;
1584
1585 case DW_AT_low_pc:
a13afe8e 1586 low_pc = attr.u.val;
252b5132
RH
1587 break;
1588
1589 case DW_AT_high_pc:
a13afe8e
FF
1590 high_pc = attr.u.val;
1591 break;
1592
1593 case DW_AT_ranges:
1594 read_rangelist (unit, &func->arange, attr.u.val);
252b5132
RH
1595 break;
1596
252b5132
RH
1597 default:
1598 break;
1599 }
1600 }
1601 }
1602
a13afe8e
FF
1603 if (func && high_pc != 0)
1604 {
1605 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1606 }
1607
252b5132
RH
1608 if (abbrev->has_children)
1609 nesting_level++;
1610 }
1611
b34976b6 1612 return TRUE;
252b5132
RH
1613}
1614
5e38c3b8
MM
1615/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1616 includes the compilation unit header that proceeds the DIE's, but
5c4491d3 1617 does not include the length field that precedes each compilation
5e38c3b8 1618 unit header. END_PTR points one past the end of this comp unit.
d03ba2a1 1619 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
252b5132
RH
1620
1621 This routine does not read the whole compilation unit; only enough
1622 to get to the line number information for the compilation unit. */
1623
1624static struct comp_unit *
818a27ac
AM
1625parse_comp_unit (bfd *abfd,
1626 struct dwarf2_debug *stash,
1627 bfd_vma unit_length,
f075ee0c 1628 bfd_byte *info_ptr_unit,
818a27ac 1629 unsigned int offset_size)
252b5132
RH
1630{
1631 struct comp_unit* unit;
f46c2da6 1632 unsigned int version;
8ce8c090 1633 bfd_uint64_t abbrev_offset = 0;
f46c2da6 1634 unsigned int addr_size;
252b5132 1635 struct abbrev_info** abbrevs;
252b5132
RH
1636 unsigned int abbrev_number, bytes_read, i;
1637 struct abbrev_info *abbrev;
1638 struct attribute attr;
f075ee0c
AM
1639 bfd_byte *info_ptr = stash->info_ptr;
1640 bfd_byte *end_ptr = info_ptr + unit_length;
dc810e39 1641 bfd_size_type amt;
a13afe8e
FF
1642 bfd_vma low_pc = 0;
1643 bfd_vma high_pc = 0;
3fde5a36 1644
252b5132
RH
1645 version = read_2_bytes (abfd, info_ptr);
1646 info_ptr += 2;
d03ba2a1
JJ
1647 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1648 if (offset_size == 4)
5e38c3b8 1649 abbrev_offset = read_4_bytes (abfd, info_ptr);
d03ba2a1 1650 else
5e38c3b8 1651 abbrev_offset = read_8_bytes (abfd, info_ptr);
d03ba2a1 1652 info_ptr += offset_size;
252b5132
RH
1653 addr_size = read_1_byte (abfd, info_ptr);
1654 info_ptr += 1;
1655
1656 if (version != 2)
1657 {
f46c2da6 1658 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
252b5132
RH
1659 bfd_set_error (bfd_error_bad_value);
1660 return 0;
1661 }
1662
1663 if (addr_size > sizeof (bfd_vma))
1664 {
1665 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1666 addr_size,
f46c2da6 1667 (unsigned int) sizeof (bfd_vma));
252b5132
RH
1668 bfd_set_error (bfd_error_bad_value);
1669 return 0;
1670 }
1671
ecb651f0 1672 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 1673 {
f5a3e38a 1674 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
252b5132
RH
1675 bfd_set_error (bfd_error_bad_value);
1676 return 0;
1677 }
1678
a092b084 1679 /* Read the abbrevs for this compilation unit into a table. */
51db3708 1680 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
1681 if (! abbrevs)
1682 return 0;
1683
1684 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1685 info_ptr += bytes_read;
1686 if (! abbrev_number)
1687 {
f46c2da6 1688 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
252b5132
RH
1689 abbrev_number);
1690 bfd_set_error (bfd_error_bad_value);
1691 return 0;
1692 }
1693
1694 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1695 if (! abbrev)
1696 {
f46c2da6 1697 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1698 abbrev_number);
1699 bfd_set_error (bfd_error_bad_value);
1700 return 0;
1701 }
98591c73 1702
dc810e39 1703 amt = sizeof (struct comp_unit);
818a27ac 1704 unit = bfd_zalloc (abfd, amt);
252b5132 1705 unit->abfd = abfd;
98591c73 1706 unit->addr_size = addr_size;
d03ba2a1 1707 unit->offset_size = offset_size;
252b5132
RH
1708 unit->abbrevs = abbrevs;
1709 unit->end_ptr = end_ptr;
d03ba2a1 1710 unit->stash = stash;
c0c28ab8 1711 unit->info_ptr_unit = info_ptr_unit;
252b5132
RH
1712
1713 for (i = 0; i < abbrev->num_attrs; ++i)
1714 {
1715 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1716
1717 /* Store the data if it is of an attribute we want to keep in a
1718 partial symbol table. */
1719 switch (attr.name)
1720 {
1721 case DW_AT_stmt_list:
1722 unit->stmtlist = 1;
482e2e37 1723 unit->line_offset = attr.u.val;
252b5132
RH
1724 break;
1725
1726 case DW_AT_name:
482e2e37 1727 unit->name = attr.u.str;
252b5132
RH
1728 break;
1729
1730 case DW_AT_low_pc:
a13afe8e
FF
1731 low_pc = attr.u.val;
1732 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1733 this is the base address to use when reading location
1734 lists or range lists. */
1735 unit->base_address = low_pc;
252b5132
RH
1736 break;
1737
1738 case DW_AT_high_pc:
a13afe8e
FF
1739 high_pc = attr.u.val;
1740 break;
1741
1742 case DW_AT_ranges:
1743 read_rangelist (unit, &unit->arange, attr.u.val);
252b5132
RH
1744 break;
1745
1746 case DW_AT_comp_dir:
1747 {
f075ee0c 1748 char *comp_dir = attr.u.str;
252b5132
RH
1749 if (comp_dir)
1750 {
1751 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1752 directory, get rid of it. */
818a27ac 1753 char *cp = strchr (comp_dir, ':');
252b5132
RH
1754
1755 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1756 comp_dir = cp + 1;
1757 }
1758 unit->comp_dir = comp_dir;
1759 break;
1760 }
1761
1762 default:
1763 break;
1764 }
1765 }
a13afe8e
FF
1766 if (high_pc != 0)
1767 {
1768 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1769 }
252b5132
RH
1770
1771 unit->first_child_die_ptr = info_ptr;
1772 return unit;
1773}
1774
b34976b6 1775/* Return TRUE if UNIT contains the address given by ADDR. */
252b5132 1776
b34976b6 1777static bfd_boolean
818a27ac 1778comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
252b5132 1779{
f623be2b
RH
1780 struct arange *arange;
1781
1782 if (unit->error)
b34976b6 1783 return FALSE;
f623be2b
RH
1784
1785 arange = &unit->arange;
1786 do
1787 {
1788 if (addr >= arange->low && addr < arange->high)
b34976b6 1789 return TRUE;
f623be2b
RH
1790 arange = arange->next;
1791 }
1792 while (arange);
98591c73 1793
b34976b6 1794 return FALSE;
252b5132
RH
1795}
1796
252b5132
RH
1797/* If UNIT contains ADDR, set the output parameters to the values for
1798 the line containing ADDR. The output parameters, FILENAME_PTR,
1799 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 1800 to be filled in.
252b5132 1801
ab3acfbe 1802 Return TRUE if UNIT contains ADDR, and no errors were encountered;
b34976b6 1803 FALSE otherwise. */
252b5132 1804
b34976b6 1805static bfd_boolean
818a27ac
AM
1806comp_unit_find_nearest_line (struct comp_unit *unit,
1807 bfd_vma addr,
1808 const char **filename_ptr,
1809 const char **functionname_ptr,
1810 unsigned int *linenumber_ptr,
1811 struct dwarf2_debug *stash)
252b5132 1812{
b34976b6
AM
1813 bfd_boolean line_p;
1814 bfd_boolean func_p;
1ee24f27 1815 struct funcinfo *function;
98591c73 1816
252b5132 1817 if (unit->error)
b34976b6 1818 return FALSE;
252b5132
RH
1819
1820 if (! unit->line_table)
1821 {
1822 if (! unit->stmtlist)
1823 {
1824 unit->error = 1;
b34976b6 1825 return FALSE;
252b5132 1826 }
98591c73 1827
51db3708 1828 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
1829
1830 if (! unit->line_table)
1831 {
1832 unit->error = 1;
b34976b6 1833 return FALSE;
252b5132 1834 }
98591c73 1835
3f5864e1 1836 if (unit->first_child_die_ptr < unit->end_ptr
e82ce529 1837 && ! scan_unit_for_functions (unit))
252b5132
RH
1838 {
1839 unit->error = 1;
b34976b6 1840 return FALSE;
252b5132
RH
1841 }
1842 }
1843
1ee24f27 1844 function = NULL;
4ab527b0 1845 func_p = lookup_address_in_function_table (unit, addr,
e2f6d277 1846 &function, functionname_ptr);
4ab527b0
FF
1847 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
1848 stash->inliner_chain = function;
e2f6d277
NC
1849 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1850 function, filename_ptr,
252b5132 1851 linenumber_ptr);
b34976b6 1852 return line_p || func_p;
252b5132
RH
1853}
1854
e2f6d277
NC
1855/* Locate a section in a BFD containing debugging info. The search starts
1856 from the section after AFTER_SEC, or from the first section in the BFD if
1857 AFTER_SEC is NULL. The search works by examining the names of the
1858 sections. There are two permissiable names. The first is .debug_info.
1859 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1860 This is a variation on the .debug_info section which has a checksum
1861 describing the contents appended onto the name. This allows the linker to
1862 identify and discard duplicate debugging sections for different
1863 compilation units. */
a092b084
NC
1864#define DWARF2_DEBUG_INFO ".debug_info"
1865#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1866
1867static asection *
818a27ac 1868find_debug_info (bfd *abfd, asection *after_sec)
a092b084
NC
1869{
1870 asection * msec;
1871
1872 if (after_sec)
1873 msec = after_sec->next;
1874 else
1875 msec = abfd->sections;
1876
1877 while (msec)
1878 {
1879 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1880 return msec;
1881
1882 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1883 return msec;
1884
1885 msec = msec->next;
1886 }
1887
1888 return NULL;
1889}
1890
cd917290 1891/* The DWARF2 version of find_nearest_line. Return TRUE if the line
5e38c3b8
MM
1892 is found without error. ADDR_SIZE is the number of bytes in the
1893 initial .debug_info length field and in the abbreviation offset.
1894 You may use zero to indicate that the default value should be
1895 used. */
252b5132 1896
b34976b6 1897bfd_boolean
818a27ac
AM
1898_bfd_dwarf2_find_nearest_line (bfd *abfd,
1899 asection *section,
1900 asymbol **symbols,
1901 bfd_vma offset,
1902 const char **filename_ptr,
1903 const char **functionname_ptr,
1904 unsigned int *linenumber_ptr,
1905 unsigned int addr_size,
1906 void **pinfo)
252b5132
RH
1907{
1908 /* Read each compilation unit from the section .debug_info, and check
1909 to see if it contains the address we are searching for. If yes,
1910 lookup the address, and return the line number info. If no, go
98591c73 1911 on to the next compilation unit.
252b5132
RH
1912
1913 We keep a list of all the previously read compilation units, and
98591c73 1914 a pointer to the next un-read compilation unit. Check the
a092b084 1915 previously read units before reading more. */
1ba54ee0 1916 struct dwarf2_debug *stash;
252b5132 1917
a092b084 1918 /* What address are we looking for? */
1ba54ee0 1919 bfd_vma addr;
252b5132
RH
1920
1921 struct comp_unit* each;
98591c73 1922
1ba54ee0
AM
1923 stash = *pinfo;
1924 addr = offset;
1925 if (section->output_section)
1926 addr += section->output_section->vma + section->output_offset;
1927 else
1928 addr += section->vma;
252b5132
RH
1929 *filename_ptr = NULL;
1930 *functionname_ptr = NULL;
1931 *linenumber_ptr = 0;
1932
5e38c3b8
MM
1933 /* The DWARF2 spec says that the initial length field, and the
1934 offset of the abbreviation table, should both be 4-byte values.
1935 However, some compilers do things differently. */
1936 if (addr_size == 0)
1937 addr_size = 4;
1efe4b10 1938 BFD_ASSERT (addr_size == 4 || addr_size == 8);
98591c73 1939
252b5132
RH
1940 if (! stash)
1941 {
dc810e39 1942 bfd_size_type total_size;
252b5132 1943 asection *msec;
dc810e39 1944 bfd_size_type amt = sizeof (struct dwarf2_debug);
a092b084 1945
818a27ac 1946 stash = bfd_zalloc (abfd, amt);
252b5132 1947 if (! stash)
b34976b6 1948 return FALSE;
252b5132 1949
818a27ac 1950 *pinfo = stash;
3fde5a36 1951
a092b084
NC
1952 msec = find_debug_info (abfd, NULL);
1953 if (! msec)
1954 /* No dwarf2 info. Note that at this point the stash
1955 has been allocated, but contains zeros, this lets
1956 future calls to this function fail quicker. */
b34976b6 1957 return FALSE;
a092b084
NC
1958
1959 /* There can be more than one DWARF2 info section in a BFD these days.
e82ce529 1960 Read them all in and produce one large stash. We do this in two
a092b084
NC
1961 passes - in the first pass we just accumulate the section sizes.
1962 In the second pass we read in the section's contents. The allows
1963 us to avoid reallocing the data as we add sections to the stash. */
1964 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
eea6121a 1965 total_size += msec->size;
98591c73 1966
818a27ac 1967 stash->info_ptr = bfd_alloc (abfd, total_size);
a092b084 1968 if (stash->info_ptr == NULL)
b34976b6 1969 return FALSE;
252b5132 1970
a092b084
NC
1971 stash->info_ptr_end = stash->info_ptr;
1972
1973 for (msec = find_debug_info (abfd, NULL);
1974 msec;
1975 msec = find_debug_info (abfd, msec))
252b5132 1976 {
dc810e39
AM
1977 bfd_size_type size;
1978 bfd_size_type start;
a092b084 1979
eea6121a 1980 size = msec->size;
a092b084
NC
1981 if (size == 0)
1982 continue;
1983
1984 start = stash->info_ptr_end - stash->info_ptr;
1985
6e84a906
DJ
1986 if ((bfd_simple_get_relocated_section_contents
1987 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
a092b084
NC
1988 continue;
1989
1990 stash->info_ptr_end = stash->info_ptr + start + size;
252b5132
RH
1991 }
1992
f2363ce5
AO
1993 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1994
1995 stash->sec = find_debug_info (abfd, NULL);
1996 stash->sec_info_ptr = stash->info_ptr;
1997 stash->syms = symbols;
252b5132 1998 }
a092b084 1999
98591c73 2000 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 2001 (or that an error occured while setting up the stash). */
252b5132 2002 if (! stash->info_ptr)
b34976b6 2003 return FALSE;
252b5132 2004
4ab527b0
FF
2005 stash->inliner_chain = NULL;
2006
a092b084 2007 /* Check the previously read comp. units first. */
252b5132 2008 for (each = stash->all_comp_units; each; each = each->next_unit)
f623be2b 2009 if (comp_unit_contains_address (each, addr))
98591c73 2010 return comp_unit_find_nearest_line (each, addr, filename_ptr,
51db3708
NC
2011 functionname_ptr, linenumber_ptr,
2012 stash);
252b5132 2013
a092b084 2014 /* Read each remaining comp. units checking each as they are read. */
252b5132
RH
2015 while (stash->info_ptr < stash->info_ptr_end)
2016 {
5e38c3b8 2017 bfd_vma length;
b34976b6 2018 bfd_boolean found;
d03ba2a1 2019 unsigned int offset_size = addr_size;
f075ee0c 2020 bfd_byte *info_ptr_unit = stash->info_ptr;
252b5132 2021
a3805e4e
AO
2022 length = read_4_bytes (abfd, stash->info_ptr);
2023 /* A 0xffffff length is the DWARF3 way of indicating we use
2024 64-bit offsets, instead of 32-bit offsets. */
2025 if (length == 0xffffffff)
d03ba2a1 2026 {
a3805e4e
AO
2027 offset_size = 8;
2028 length = read_8_bytes (abfd, stash->info_ptr + 4);
2029 stash->info_ptr += 12;
2030 }
2031 /* A zero length is the IRIX way of indicating 64-bit offsets,
2032 mostly because the 64-bit length will generally fit in 32
2033 bits, and the endianness helps. */
2034 else if (length == 0)
2035 {
2036 offset_size = 8;
2037 length = read_4_bytes (abfd, stash->info_ptr + 4);
2038 stash->info_ptr += 8;
2039 }
2040 /* In the absence of the hints above, we assume addr_size-sized
2041 offsets, for backward-compatibility with pre-DWARF3 64-bit
2042 platforms. */
2043 else if (addr_size == 8)
2044 {
2045 length = read_8_bytes (abfd, stash->info_ptr);
060dc71d 2046 stash->info_ptr += 8;
d03ba2a1 2047 }
5e38c3b8 2048 else
a3805e4e 2049 stash->info_ptr += 4;
252b5132
RH
2050
2051 if (length > 0)
e82ce529 2052 {
c0c28ab8
L
2053 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2054 offset_size);
252b5132
RH
2055 stash->info_ptr += length;
2056
f2363ce5 2057 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
eea6121a 2058 == stash->sec->size)
f2363ce5
AO
2059 {
2060 stash->sec = find_debug_info (abfd, stash->sec);
2061 stash->sec_info_ptr = stash->info_ptr;
2062 }
2063
252b5132
RH
2064 if (each)
2065 {
2066 each->next_unit = stash->all_comp_units;
2067 stash->all_comp_units = each;
2068
159002ff
RH
2069 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2070 compilation units. If we don't have them (i.e.,
2071 unit->high == 0), we need to consult the line info
2072 table to see if a compilation unit contains the given
a092b084 2073 address. */
f623be2b 2074 if (each->arange.high > 0)
159002ff
RH
2075 {
2076 if (comp_unit_contains_address (each, addr))
2077 return comp_unit_find_nearest_line (each, addr,
6e84a906
DJ
2078 filename_ptr,
2079 functionname_ptr,
2080 linenumber_ptr,
2081 stash);
159002ff
RH
2082 }
2083 else
2084 {
2085 found = comp_unit_find_nearest_line (each, addr,
2086 filename_ptr,
2087 functionname_ptr,
51db3708
NC
2088 linenumber_ptr,
2089 stash);
159002ff 2090 if (found)
b34976b6 2091 return TRUE;
159002ff 2092 }
252b5132
RH
2093 }
2094 }
2095 }
2096
b34976b6 2097 return FALSE;
252b5132 2098}
35330cce 2099
4ab527b0
FF
2100bfd_boolean
2101_bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2102 const char **filename_ptr,
2103 const char **functionname_ptr,
2104 unsigned int *linenumber_ptr,
2105 void **pinfo)
2106{
2107 struct dwarf2_debug *stash;
2108
2109 stash = *pinfo;
2110 if (stash)
2111 {
2112 struct funcinfo *func = stash->inliner_chain;
2113 if (func && func->caller_func)
2114 {
2115 *filename_ptr = func->caller_file;
2116 *functionname_ptr = func->caller_func->name;
2117 *linenumber_ptr = func->caller_line;
2118 stash->inliner_chain = func->caller_func;
2119 return (TRUE);
2120 }
2121 }
2122
2123 return (FALSE);
2124}
2125
35330cce
NC
2126void
2127_bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2128{
2129 struct comp_unit *each;
2130 struct dwarf2_debug *stash;
2131
2132 if (abfd == NULL || elf_tdata (abfd) == NULL)
2133 return;
2134
2135 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2136
2137 if (stash == NULL)
2138 return;
2139
2140 for (each = stash->all_comp_units; each; each = each->next_unit)
2141 {
2142 struct abbrev_info **abbrevs = each->abbrevs;
2143 size_t i;
2144
2145 for (i = 0; i < ABBREV_HASH_SIZE; i++)
2146 {
2147 struct abbrev_info *abbrev = abbrevs[i];
2148
2149 while (abbrev)
2150 {
2151 free (abbrev->attrs);
2152 abbrev = abbrev->next;
2153 }
2154 }
2155
2156 if (each->line_table)
2157 {
2158 free (each->line_table->dirs);
2159 free (each->line_table->files);
2160 }
2161 }
2162
2163 free (stash->dwarf_abbrev_buffer);
2164 free (stash->dwarf_line_buffer);
2165 free (stash->dwarf_ranges_buffer);
2166}
This page took 0.401007 seconds and 4 git commands to generate.