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