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