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