]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/dwarf2.c
bfd/
[thirdparty/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,
3db64b00 3 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
252b5132
RH
4
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
7
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
15
e2f6d277 16 This file is part of BFD.
252b5132 17
e2f6d277
NC
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or (at
21 your option) any later version.
252b5132 22
e2f6d277
NC
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
252b5132 27
e2f6d277
NC
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
3e110533 30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132 31
252b5132 32#include "sysdep.h"
3db64b00 33#include "bfd.h"
252b5132
RH
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
d4c32a81
L
77struct loadable_section
78{
79 asection *section;
80 bfd_vma adj_vma;
81};
82
a092b084
NC
83struct dwarf2_debug
84{
85 /* A list of all previously read comp_units. */
f075ee0c 86 struct comp_unit *all_comp_units;
252b5132
RH
87
88 /* The next unread compilation unit within the .debug_info section.
89 Zero indicates that the .debug_info section has not been loaded
a092b084 90 into a buffer yet. */
f075ee0c 91 bfd_byte *info_ptr;
252b5132 92
a092b084 93 /* Pointer to the end of the .debug_info section memory buffer. */
f075ee0c 94 bfd_byte *info_ptr_end;
252b5132 95
f2363ce5
AO
96 /* Pointer to the section and address of the beginning of the
97 section. */
f075ee0c
AM
98 asection *sec;
99 bfd_byte *sec_info_ptr;
f2363ce5
AO
100
101 /* Pointer to the symbol table. */
f075ee0c 102 asymbol **syms;
f2363ce5 103
a092b084 104 /* Pointer to the .debug_abbrev section loaded into memory. */
f075ee0c 105 bfd_byte *dwarf_abbrev_buffer;
252b5132 106
a092b084 107 /* Length of the loaded .debug_abbrev section. */
252b5132 108 unsigned long dwarf_abbrev_size;
69dd2e2d
RH
109
110 /* Buffer for decode_line_info. */
f075ee0c 111 bfd_byte *dwarf_line_buffer;
ccdb16fc
JW
112
113 /* Length of the loaded .debug_line section. */
114 unsigned long dwarf_line_size;
d03ba2a1
JJ
115
116 /* Pointer to the .debug_str section loaded into memory. */
f075ee0c 117 bfd_byte *dwarf_str_buffer;
d03ba2a1
JJ
118
119 /* Length of the loaded .debug_str section. */
120 unsigned long dwarf_str_size;
a13afe8e
FF
121
122 /* Pointer to the .debug_ranges section loaded into memory. */
123 bfd_byte *dwarf_ranges_buffer;
124
125 /* Length of the loaded .debug_ranges section. */
126 unsigned long dwarf_ranges_size;
4ab527b0
FF
127
128 /* If the most recent call to bfd_find_nearest_line was given an
129 address in an inlined function, preserve a pointer into the
130 calling chain for subsequent calls to bfd_find_inliner_info to
131 use. */
132 struct funcinfo *inliner_chain;
d4c32a81
L
133
134 /* Number of loadable sections. */
135 unsigned int loadable_section_count;
136
137 /* Array of loadable sections. */
138 struct loadable_section *loadable_sections;
252b5132
RH
139};
140
a092b084
NC
141struct arange
142{
f623be2b
RH
143 struct arange *next;
144 bfd_vma low;
145 bfd_vma high;
146};
252b5132 147
252b5132 148/* A minimal decoding of DWARF2 compilation units. We only decode
a092b084 149 what's needed to get to the line number information. */
252b5132 150
a092b084
NC
151struct comp_unit
152{
153 /* Chain the previously read compilation units. */
f075ee0c 154 struct comp_unit *next_unit;
252b5132 155
2ae727ad 156 /* Keep the bfd convenient (for memory allocation). */
f075ee0c 157 bfd *abfd;
252b5132 158
09a9d560 159 /* The lowest and highest addresses contained in this compilation
a092b084 160 unit as specified in the compilation unit header. */
f623be2b 161 struct arange arange;
252b5132 162
a092b084 163 /* The DW_AT_name attribute (for error messages). */
f075ee0c 164 char *name;
252b5132 165
a092b084 166 /* The abbrev hash table. */
f075ee0c 167 struct abbrev_info **abbrevs;
252b5132 168
a092b084 169 /* Note that an error was found by comp_unit_find_nearest_line. */
252b5132
RH
170 int error;
171
a092b084 172 /* The DW_AT_comp_dir attribute. */
f075ee0c 173 char *comp_dir;
252b5132 174
b34976b6 175 /* TRUE if there is a line number table associated with this comp. unit. */
252b5132 176 int stmtlist;
98591c73 177
c0c28ab8
L
178 /* Pointer to the current comp_unit so that we can find a given entry
179 by its reference. */
f075ee0c 180 bfd_byte *info_ptr_unit;
c0c28ab8 181
a092b084 182 /* The offset into .debug_line of the line number table. */
252b5132
RH
183 unsigned long line_offset;
184
a092b084 185 /* Pointer to the first child die for the comp unit. */
f075ee0c 186 bfd_byte *first_child_die_ptr;
252b5132 187
a092b084 188 /* The end of the comp unit. */
f075ee0c 189 bfd_byte *end_ptr;
252b5132 190
a092b084 191 /* The decoded line number, NULL if not yet decoded. */
f075ee0c 192 struct line_info_table *line_table;
252b5132 193
a092b084 194 /* A list of the functions found in this comp. unit. */
f075ee0c 195 struct funcinfo *function_table;
252b5132 196
5420f73d
L
197 /* A list of the variables found in this comp. unit. */
198 struct varinfo *variable_table;
199
d03ba2a1
JJ
200 /* Pointer to dwarf2_debug structure. */
201 struct dwarf2_debug *stash;
202
a092b084 203 /* Address size for this unit - from unit header. */
252b5132 204 unsigned char addr_size;
d03ba2a1
JJ
205
206 /* Offset size for this unit - from unit header. */
207 unsigned char offset_size;
a13afe8e
FF
208
209 /* Base address for this unit - from DW_AT_low_pc attribute of
210 DW_TAG_compile_unit DIE */
211 bfd_vma base_address;
252b5132
RH
212};
213
a7b97311
AM
214/* This data structure holds the information of an abbrev. */
215struct abbrev_info
216{
217 unsigned int number; /* Number identifying abbrev. */
218 enum dwarf_tag tag; /* DWARF tag. */
219 int has_children; /* Boolean. */
220 unsigned int num_attrs; /* Number of attributes. */
221 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
222 struct abbrev_info *next; /* Next in chain. */
223};
224
225struct attr_abbrev
226{
227 enum dwarf_attribute name;
228 enum dwarf_form form;
229};
230
231#ifndef ABBREV_HASH_SIZE
232#define ABBREV_HASH_SIZE 121
233#endif
234#ifndef ATTR_ALLOC_CHUNK
235#define ATTR_ALLOC_CHUNK 4
236#endif
237
98591c73
KH
238/* VERBATIM
239 The following function up to the END VERBATIM mark are
a092b084 240 copied directly from dwarf2read.c. */
252b5132 241
a092b084 242/* Read dwarf information from a buffer. */
252b5132
RH
243
244static unsigned int
f075ee0c 245read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 246{
818a27ac 247 return bfd_get_8 (abfd, buf);
252b5132
RH
248}
249
250static int
f075ee0c 251read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 252{
818a27ac 253 return bfd_get_signed_8 (abfd, buf);
252b5132
RH
254}
255
256static unsigned int
f075ee0c 257read_2_bytes (bfd *abfd, bfd_byte *buf)
252b5132 258{
818a27ac 259 return bfd_get_16 (abfd, buf);
252b5132
RH
260}
261
252b5132 262static unsigned int
f075ee0c 263read_4_bytes (bfd *abfd, bfd_byte *buf)
252b5132 264{
818a27ac 265 return bfd_get_32 (abfd, buf);
252b5132
RH
266}
267
8ce8c090 268static bfd_uint64_t
f075ee0c 269read_8_bytes (bfd *abfd, bfd_byte *buf)
252b5132 270{
818a27ac 271 return bfd_get_64 (abfd, buf);
252b5132
RH
272}
273
f075ee0c 274static bfd_byte *
818a27ac 275read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 276 bfd_byte *buf,
818a27ac 277 unsigned int size ATTRIBUTE_UNUSED)
252b5132
RH
278{
279 /* If the size of a host char is 8 bits, we can return a pointer
280 to the buffer, otherwise we have to copy the data to a buffer
281 allocated on the temporary obstack. */
282 return buf;
283}
284
285static char *
818a27ac 286read_string (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 287 bfd_byte *buf,
818a27ac 288 unsigned int *bytes_read_ptr)
252b5132 289{
d03ba2a1 290 /* Return a pointer to the embedded string. */
f075ee0c
AM
291 char *str = (char *) buf;
292 if (*str == '\0')
252b5132
RH
293 {
294 *bytes_read_ptr = 1;
295 return NULL;
296 }
98591c73 297
f075ee0c
AM
298 *bytes_read_ptr = strlen (str) + 1;
299 return str;
252b5132
RH
300}
301
d03ba2a1 302static char *
818a27ac 303read_indirect_string (struct comp_unit* unit,
f075ee0c 304 bfd_byte *buf,
818a27ac 305 unsigned int *bytes_read_ptr)
d03ba2a1 306{
8ce8c090 307 bfd_uint64_t offset;
d03ba2a1 308 struct dwarf2_debug *stash = unit->stash;
f075ee0c 309 char *str;
d03ba2a1
JJ
310
311 if (unit->offset_size == 4)
312 offset = read_4_bytes (unit->abfd, buf);
313 else
314 offset = read_8_bytes (unit->abfd, buf);
315 *bytes_read_ptr = unit->offset_size;
316
317 if (! stash->dwarf_str_buffer)
318 {
319 asection *msec;
320 bfd *abfd = unit->abfd;
eea6121a 321 bfd_size_type sz;
d03ba2a1
JJ
322
323 msec = bfd_get_section_by_name (abfd, ".debug_str");
324 if (! msec)
325 {
326 (*_bfd_error_handler)
327 (_("Dwarf Error: Can't find .debug_str section."));
328 bfd_set_error (bfd_error_bad_value);
329 return NULL;
330 }
331
eea6121a
AM
332 sz = msec->rawsize ? msec->rawsize : msec->size;
333 stash->dwarf_str_size = sz;
334 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
36868d45 335 if (! stash->dwarf_str_buffer)
d03ba2a1
JJ
336 return NULL;
337
9f632188 338 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
eea6121a 339 0, sz))
d03ba2a1
JJ
340 return NULL;
341 }
342
343 if (offset >= stash->dwarf_str_size)
344 {
f46c2da6
AM
345 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
346 (unsigned long) offset, stash->dwarf_str_size);
d03ba2a1
JJ
347 bfd_set_error (bfd_error_bad_value);
348 return NULL;
349 }
350
f075ee0c
AM
351 str = (char *) stash->dwarf_str_buffer + offset;
352 if (*str == '\0')
d03ba2a1 353 return NULL;
f075ee0c 354 return str;
d03ba2a1
JJ
355}
356
252b5132
RH
357/* END VERBATIM */
358
8ce8c090 359static bfd_uint64_t
f075ee0c 360read_address (struct comp_unit *unit, bfd_byte *buf)
252b5132 361{
0af4cd7c
PK
362 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
363
364 if (signed_vma)
365 {
366 switch (unit->addr_size)
367 {
368 case 8:
369 return bfd_get_signed_64 (unit->abfd, buf);
370 case 4:
371 return bfd_get_signed_32 (unit->abfd, buf);
372 case 2:
373 return bfd_get_signed_16 (unit->abfd, buf);
374 default:
375 abort ();
376 }
377 }
378 else
252b5132 379 {
0af4cd7c
PK
380 switch (unit->addr_size)
381 {
382 case 8:
383 return bfd_get_64 (unit->abfd, buf);
384 case 4:
385 return bfd_get_32 (unit->abfd, buf);
386 case 2:
387 return bfd_get_16 (unit->abfd, buf);
388 default:
389 abort ();
390 }
252b5132 391 }
252b5132
RH
392}
393
252b5132
RH
394/* Lookup an abbrev_info structure in the abbrev hash table. */
395
396static struct abbrev_info *
818a27ac 397lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
252b5132
RH
398{
399 unsigned int hash_number;
400 struct abbrev_info *abbrev;
401
402 hash_number = number % ABBREV_HASH_SIZE;
403 abbrev = abbrevs[hash_number];
404
405 while (abbrev)
406 {
407 if (abbrev->number == number)
408 return abbrev;
409 else
410 abbrev = abbrev->next;
411 }
98591c73 412
252b5132
RH
413 return NULL;
414}
415
416/* In DWARF version 2, the description of the debugging information is
417 stored in a separate .debug_abbrev section. Before we read any
418 dies from a section we read in all abbreviations and install them
419 in a hash table. */
420
421static struct abbrev_info**
8ce8c090 422read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
252b5132
RH
423{
424 struct abbrev_info **abbrevs;
f075ee0c 425 bfd_byte *abbrev_ptr;
252b5132
RH
426 struct abbrev_info *cur_abbrev;
427 unsigned int abbrev_number, bytes_read, abbrev_name;
428 unsigned int abbrev_form, hash_number;
dc810e39 429 bfd_size_type amt;
252b5132
RH
430
431 if (! stash->dwarf_abbrev_buffer)
432 {
433 asection *msec;
434
435 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
436 if (! msec)
437 {
438 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
439 bfd_set_error (bfd_error_bad_value);
440 return 0;
441 }
98591c73 442
eea6121a 443 stash->dwarf_abbrev_size = msec->size;
6e84a906
DJ
444 stash->dwarf_abbrev_buffer
445 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
446 stash->syms);
252b5132
RH
447 if (! stash->dwarf_abbrev_buffer)
448 return 0;
252b5132
RH
449 }
450
f5198f61 451 if (offset >= stash->dwarf_abbrev_size)
252b5132 452 {
f46c2da6
AM
453 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
454 (unsigned long) offset, stash->dwarf_abbrev_size);
252b5132
RH
455 bfd_set_error (bfd_error_bad_value);
456 return 0;
457 }
458
dc810e39 459 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
818a27ac 460 abbrevs = bfd_zalloc (abfd, amt);
252b5132
RH
461
462 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
463 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
464 abbrev_ptr += bytes_read;
465
a092b084 466 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
467 while (abbrev_number)
468 {
dc810e39 469 amt = sizeof (struct abbrev_info);
818a27ac 470 cur_abbrev = bfd_zalloc (abfd, amt);
252b5132 471
a092b084 472 /* Read in abbrev header. */
252b5132 473 cur_abbrev->number = abbrev_number;
d45913a0
DA
474 cur_abbrev->tag = (enum dwarf_tag)
475 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
252b5132
RH
476 abbrev_ptr += bytes_read;
477 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
478 abbrev_ptr += 1;
479
a092b084 480 /* Now read in declarations. */
252b5132
RH
481 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
482 abbrev_ptr += bytes_read;
483 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
484 abbrev_ptr += bytes_read;
98591c73 485
252b5132
RH
486 while (abbrev_name)
487 {
488 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
489 {
35330cce
NC
490 struct attr_abbrev *tmp;
491
dc810e39
AM
492 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
493 amt *= sizeof (struct attr_abbrev);
35330cce
NC
494 tmp = bfd_realloc (cur_abbrev->attrs, amt);
495 if (tmp == NULL)
d8d1c398
AM
496 {
497 size_t i;
498
499 for (i = 0; i < ABBREV_HASH_SIZE; i++)
500 {
501 struct abbrev_info *abbrev = abbrevs[i];
502
503 while (abbrev)
504 {
505 free (abbrev->attrs);
506 abbrev = abbrev->next;
507 }
508 }
509 return NULL;
510 }
35330cce 511 cur_abbrev->attrs = tmp;
252b5132 512 }
98591c73 513
d45913a0
DA
514 cur_abbrev->attrs[cur_abbrev->num_attrs].name
515 = (enum dwarf_attribute) abbrev_name;
516 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
517 = (enum dwarf_form) abbrev_form;
252b5132
RH
518 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
519 abbrev_ptr += bytes_read;
520 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
521 abbrev_ptr += bytes_read;
522 }
523
524 hash_number = abbrev_number % ABBREV_HASH_SIZE;
525 cur_abbrev->next = abbrevs[hash_number];
526 abbrevs[hash_number] = cur_abbrev;
527
528 /* Get next abbreviation.
e82ce529 529 Under Irix6 the abbreviations for a compilation unit are not
252b5132
RH
530 always properly terminated with an abbrev number of 0.
531 Exit loop if we encounter an abbreviation which we have
532 already read (which means we are about to read the abbreviations
533 for the next compile unit) or if the end of the abbreviation
534 table is reached. */
535 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
d8d1c398 536 >= stash->dwarf_abbrev_size)
252b5132
RH
537 break;
538 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
539 abbrev_ptr += bytes_read;
540 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
541 break;
542 }
543
544 return abbrevs;
545}
546
cf716c56 547/* Read an attribute value described by an attribute form. */
252b5132 548
f075ee0c 549static bfd_byte *
818a27ac
AM
550read_attribute_value (struct attribute *attr,
551 unsigned form,
552 struct comp_unit *unit,
f075ee0c 553 bfd_byte *info_ptr)
252b5132
RH
554{
555 bfd *abfd = unit->abfd;
556 unsigned int bytes_read;
557 struct dwarf_block *blk;
dc810e39 558 bfd_size_type amt;
252b5132 559
d45913a0 560 attr->form = (enum dwarf_form) form;
98591c73 561
cf716c56 562 switch (form)
252b5132
RH
563 {
564 case DW_FORM_addr:
0cc1cf99 565 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
252b5132 566 case DW_FORM_ref_addr:
482e2e37 567 attr->u.val = read_address (unit, info_ptr);
252b5132
RH
568 info_ptr += unit->addr_size;
569 break;
570 case DW_FORM_block2:
dc810e39 571 amt = sizeof (struct dwarf_block);
818a27ac 572 blk = bfd_alloc (abfd, amt);
252b5132
RH
573 blk->size = read_2_bytes (abfd, info_ptr);
574 info_ptr += 2;
575 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
576 info_ptr += blk->size;
482e2e37 577 attr->u.blk = blk;
252b5132
RH
578 break;
579 case DW_FORM_block4:
dc810e39 580 amt = sizeof (struct dwarf_block);
818a27ac 581 blk = bfd_alloc (abfd, amt);
252b5132
RH
582 blk->size = read_4_bytes (abfd, info_ptr);
583 info_ptr += 4;
584 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
585 info_ptr += blk->size;
482e2e37 586 attr->u.blk = blk;
252b5132
RH
587 break;
588 case DW_FORM_data2:
482e2e37 589 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
590 info_ptr += 2;
591 break;
592 case DW_FORM_data4:
482e2e37 593 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
594 info_ptr += 4;
595 break;
596 case DW_FORM_data8:
482e2e37 597 attr->u.val = read_8_bytes (abfd, info_ptr);
252b5132
RH
598 info_ptr += 8;
599 break;
600 case DW_FORM_string:
482e2e37 601 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
252b5132
RH
602 info_ptr += bytes_read;
603 break;
d03ba2a1 604 case DW_FORM_strp:
482e2e37 605 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
d03ba2a1
JJ
606 info_ptr += bytes_read;
607 break;
252b5132 608 case DW_FORM_block:
dc810e39 609 amt = sizeof (struct dwarf_block);
818a27ac 610 blk = bfd_alloc (abfd, amt);
252b5132
RH
611 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
612 info_ptr += bytes_read;
613 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
614 info_ptr += blk->size;
482e2e37 615 attr->u.blk = blk;
252b5132
RH
616 break;
617 case DW_FORM_block1:
dc810e39 618 amt = sizeof (struct dwarf_block);
818a27ac 619 blk = bfd_alloc (abfd, amt);
252b5132
RH
620 blk->size = read_1_byte (abfd, info_ptr);
621 info_ptr += 1;
622 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
623 info_ptr += blk->size;
482e2e37 624 attr->u.blk = blk;
252b5132
RH
625 break;
626 case DW_FORM_data1:
482e2e37 627 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
628 info_ptr += 1;
629 break;
630 case DW_FORM_flag:
482e2e37 631 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
632 info_ptr += 1;
633 break;
634 case DW_FORM_sdata:
482e2e37 635 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
636 info_ptr += bytes_read;
637 break;
638 case DW_FORM_udata:
482e2e37 639 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
640 info_ptr += bytes_read;
641 break;
642 case DW_FORM_ref1:
482e2e37 643 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
644 info_ptr += 1;
645 break;
646 case DW_FORM_ref2:
482e2e37 647 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
648 info_ptr += 2;
649 break;
650 case DW_FORM_ref4:
482e2e37 651 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
652 info_ptr += 4;
653 break;
81edd86d 654 case DW_FORM_ref8:
482e2e37 655 attr->u.val = read_8_bytes (abfd, info_ptr);
81edd86d
MM
656 info_ptr += 8;
657 break;
252b5132 658 case DW_FORM_ref_udata:
482e2e37 659 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
660 info_ptr += bytes_read;
661 break;
252b5132 662 case DW_FORM_indirect:
cf716c56
RH
663 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
664 info_ptr += bytes_read;
665 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
666 break;
252b5132 667 default:
f46c2da6 668 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
cf716c56 669 form);
252b5132
RH
670 bfd_set_error (bfd_error_bad_value);
671 }
672 return info_ptr;
673}
674
cf716c56
RH
675/* Read an attribute described by an abbreviated attribute. */
676
f075ee0c 677static bfd_byte *
818a27ac
AM
678read_attribute (struct attribute *attr,
679 struct attr_abbrev *abbrev,
680 struct comp_unit *unit,
f075ee0c 681 bfd_byte *info_ptr)
cf716c56
RH
682{
683 attr->name = abbrev->name;
684 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
685 return info_ptr;
686}
687
a092b084 688/* Source line information table routines. */
252b5132
RH
689
690#define FILE_ALLOC_CHUNK 5
691#define DIR_ALLOC_CHUNK 5
692
a092b084
NC
693struct line_info
694{
252b5132 695 struct line_info* prev_line;
252b5132 696 bfd_vma address;
f075ee0c 697 char *filename;
252b5132
RH
698 unsigned int line;
699 unsigned int column;
a092b084 700 int end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
701};
702
a092b084
NC
703struct fileinfo
704{
252b5132
RH
705 char *name;
706 unsigned int dir;
707 unsigned int time;
708 unsigned int size;
709};
710
a092b084
NC
711struct line_info_table
712{
252b5132 713 bfd* abfd;
252b5132
RH
714 unsigned int num_files;
715 unsigned int num_dirs;
f075ee0c
AM
716 char *comp_dir;
717 char **dirs;
252b5132 718 struct fileinfo* files;
e82ce529
AM
719 struct line_info* last_line; /* largest VMA */
720 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
252b5132
RH
721};
722
4ab527b0
FF
723/* Remember some information about each function. If the function is
724 inlined (DW_TAG_inlined_subroutine) it may have two additional
725 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
726 source code location where this function was inlined. */
727
1ee24f27
DJ
728struct funcinfo
729{
4ab527b0
FF
730 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
731 struct funcinfo *caller_func; /* Pointer to function one scope higher */
732 char *caller_file; /* Source location file name where caller_func inlines this func */
733 int caller_line; /* Source location line number where caller_func inlines this func */
5420f73d
L
734 char *file; /* Source location file name */
735 int line; /* Source location line number */
4ab527b0 736 int tag;
f075ee0c 737 char *name;
a13afe8e 738 struct arange arange;
5420f73d
L
739 asection *sec; /* Where the symbol is defined */
740};
741
742struct varinfo
743{
744 /* Pointer to previous variable in list of all variables */
745 struct varinfo *prev_var;
746 /* Source location file name */
747 char *file;
748 /* Source location line number */
749 int line;
750 int tag;
751 char *name;
5cf2e3f0 752 bfd_vma addr;
5420f73d
L
753 /* Where the symbol is defined */
754 asection *sec;
755 /* Is this a stack variable? */
756 unsigned int stack: 1;
1ee24f27
DJ
757};
758
d4c32a81
L
759/* Return TRUE if NEW_LINE should sort after LINE. */
760
761static inline bfd_boolean
762new_line_sorts_after (struct line_info *new_line, struct line_info *line)
763{
764 return (new_line->address > line->address
765 || (new_line->address == line->address
766 && new_line->end_sequence < line->end_sequence));
767}
768
769
af3ef9fe
NC
770/* Adds a new entry to the line_info list in the line_info_table, ensuring
771 that the list is sorted. Note that the line_info list is sorted from
772 highest to lowest VMA (with possible duplicates); that is,
773 line_info->prev_line always accesses an equal or smaller VMA. */
774
98591c73 775static void
818a27ac
AM
776add_line_info (struct line_info_table *table,
777 bfd_vma address,
778 char *filename,
779 unsigned int line,
780 unsigned int column,
781 int end_sequence)
252b5132 782{
dc810e39 783 bfd_size_type amt = sizeof (struct line_info);
818a27ac 784 struct line_info* info = bfd_alloc (table->abfd, amt);
252b5132 785
d4c32a81
L
786 /* Set member data of 'info'. */
787 info->address = address;
788 info->line = line;
789 info->column = column;
790 info->end_sequence = end_sequence;
791
792 if (filename && filename[0])
793 {
794 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
795 if (info->filename)
796 strcpy (info->filename, filename);
797 }
798 else
799 info->filename = NULL;
800
e82ce529
AM
801 /* Find the correct location for 'info'. Normally we will receive
802 new line_info data 1) in order and 2) with increasing VMAs.
803 However some compilers break the rules (cf. decode_line_info) and
804 so we include some heuristics for quickly finding the correct
805 location for 'info'. In particular, these heuristics optimize for
806 the common case in which the VMA sequence that we receive is a
807 list of locally sorted VMAs such as
808 p...z a...j (where a < j < p < z)
252b5132 809
e82ce529
AM
810 Note: table->lcl_head is used to head an *actual* or *possible*
811 sequence within the list (such as a...j) that is not directly
812 headed by table->last_line
813
814 Note: we may receive duplicate entries from 'decode_line_info'. */
815
d8d1c398
AM
816 if (!table->last_line
817 || new_line_sorts_after (info, table->last_line))
818 {
819 /* Normal case: add 'info' to the beginning of the list */
820 info->prev_line = table->last_line;
821 table->last_line = info;
e82ce529 822
d8d1c398
AM
823 /* lcl_head: initialize to head a *possible* sequence at the end. */
824 if (!table->lcl_head)
825 table->lcl_head = info;
826 }
827 else if (!new_line_sorts_after (info, table->lcl_head)
828 && (!table->lcl_head->prev_line
829 || new_line_sorts_after (info, table->lcl_head->prev_line)))
830 {
831 /* Abnormal but easy: lcl_head is the head of 'info'. */
832 info->prev_line = table->lcl_head->prev_line;
833 table->lcl_head->prev_line = info;
834 }
835 else
836 {
837 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
838 heads for 'info'. Reset 'lcl_head'. */
839 struct line_info* li2 = table->last_line; /* always non-NULL */
840 struct line_info* li1 = li2->prev_line;
e82ce529 841
d8d1c398
AM
842 while (li1)
843 {
844 if (!new_line_sorts_after (info, li2)
845 && new_line_sorts_after (info, li1))
846 break;
e82ce529 847
d8d1c398
AM
848 li2 = li1; /* always non-NULL */
849 li1 = li1->prev_line;
850 }
851 table->lcl_head = li2;
852 info->prev_line = table->lcl_head->prev_line;
853 table->lcl_head->prev_line = info;
854 }
252b5132
RH
855}
856
5ed6aba4 857/* Extract a fully qualified filename from a line info table.
af3ef9fe
NC
858 The returned string has been malloc'ed and it is the caller's
859 responsibility to free it. */
5ed6aba4 860
a092b084 861static char *
818a27ac 862concat_filename (struct line_info_table *table, unsigned int file)
252b5132 863{
f075ee0c 864 char *filename;
159002ff
RH
865
866 if (file - 1 >= table->num_files)
867 {
75a657ba
L
868 /* FILE == 0 means unknown. */
869 if (file)
870 (*_bfd_error_handler)
871 (_("Dwarf Error: mangled line number section (bad file number)."));
af3ef9fe 872 return strdup ("<unknown>");
159002ff
RH
873 }
874
875 filename = table->files[file - 1].name;
5ed6aba4 876
7421a730 877 if (!IS_ABSOLUTE_PATH (filename))
252b5132 878 {
7421a730
AM
879 char *dirname = NULL;
880 char *subdirname = NULL;
881 char *name;
882 size_t len;
0dafd5f6 883
7421a730
AM
884 if (table->files[file - 1].dir)
885 subdirname = table->dirs[table->files[file - 1].dir - 1];
886
887 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
888 dirname = table->comp_dir;
889
890 if (!dirname)
af3ef9fe 891 {
7421a730
AM
892 dirname = subdirname;
893 subdirname = NULL;
894 }
af3ef9fe 895
7421a730
AM
896 if (!dirname)
897 return strdup (filename);
898
899 len = strlen (dirname) + strlen (filename) + 2;
900
901 if (subdirname)
902 {
903 len += strlen (subdirname) + 1;
904 name = bfd_malloc (len);
905 if (name)
906 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
907 }
908 else
909 {
af3ef9fe
NC
910 name = bfd_malloc (len);
911 if (name)
912 sprintf (name, "%s/%s", dirname, filename);
af3ef9fe 913 }
7421a730
AM
914
915 return name;
252b5132 916 }
af3ef9fe
NC
917
918 return strdup (filename);
252b5132
RH
919}
920
f623be2b 921static void
a13afe8e 922arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
f623be2b
RH
923{
924 struct arange *arange;
925
a13afe8e
FF
926 /* If the first arange is empty, use it. */
927 if (first_arange->high == 0)
928 {
929 first_arange->low = low_pc;
930 first_arange->high = high_pc;
931 return;
932 }
98591c73 933
a13afe8e
FF
934 /* Next see if we can cheaply extend an existing range. */
935 arange = first_arange;
f623be2b
RH
936 do
937 {
938 if (low_pc == arange->high)
939 {
940 arange->high = high_pc;
941 return;
942 }
943 if (high_pc == arange->low)
944 {
945 arange->low = low_pc;
946 return;
947 }
948 arange = arange->next;
949 }
950 while (arange);
951
a13afe8e
FF
952 /* Need to allocate a new arange and insert it into the arange list.
953 Order isn't significant, so just insert after the first arange. */
954 arange = bfd_zalloc (abfd, sizeof (*arange));
f623be2b
RH
955 arange->low = low_pc;
956 arange->high = high_pc;
a13afe8e
FF
957 arange->next = first_arange->next;
958 first_arange->next = arange;
f623be2b
RH
959}
960
a092b084 961/* Decode the line number information for UNIT. */
252b5132
RH
962
963static struct line_info_table*
818a27ac 964decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
252b5132
RH
965{
966 bfd *abfd = unit->abfd;
252b5132 967 struct line_info_table* table;
f075ee0c
AM
968 bfd_byte *line_ptr;
969 bfd_byte *line_end;
252b5132 970 struct line_head lh;
d03ba2a1 971 unsigned int i, bytes_read, offset_size;
252b5132
RH
972 char *cur_file, *cur_dir;
973 unsigned char op_code, extended_op, adj_opcode;
dc810e39 974 bfd_size_type amt;
252b5132 975
69dd2e2d 976 if (! stash->dwarf_line_buffer)
252b5132
RH
977 {
978 asection *msec;
252b5132
RH
979
980 msec = bfd_get_section_by_name (abfd, ".debug_line");
981 if (! msec)
982 {
983 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
984 bfd_set_error (bfd_error_bad_value);
985 return 0;
986 }
98591c73 987
eea6121a 988 stash->dwarf_line_size = msec->size;
6e84a906
DJ
989 stash->dwarf_line_buffer
990 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
991 stash->syms);
f623be2b 992 if (! stash->dwarf_line_buffer)
252b5132 993 return 0;
252b5132
RH
994 }
995
6e84a906
DJ
996 /* It is possible to get a bad value for the line_offset. Validate
997 it here so that we won't get a segfault below. */
ccdb16fc
JW
998 if (unit->line_offset >= stash->dwarf_line_size)
999 {
f46c2da6 1000 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
ccdb16fc
JW
1001 unit->line_offset, stash->dwarf_line_size);
1002 bfd_set_error (bfd_error_bad_value);
1003 return 0;
1004 }
1005
dc810e39 1006 amt = sizeof (struct line_info_table);
818a27ac 1007 table = bfd_alloc (abfd, amt);
252b5132
RH
1008 table->abfd = abfd;
1009 table->comp_dir = unit->comp_dir;
1010
1011 table->num_files = 0;
1012 table->files = NULL;
1013
1014 table->num_dirs = 0;
1015 table->dirs = NULL;
1016
159002ff
RH
1017 table->files = NULL;
1018 table->last_line = NULL;
e82ce529 1019 table->lcl_head = NULL;
159002ff 1020
69dd2e2d 1021 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 1022
a092b084 1023 /* Read in the prologue. */
91a4d569
AM
1024 lh.total_length = read_4_bytes (abfd, line_ptr);
1025 line_ptr += 4;
1026 offset_size = 4;
1027 if (lh.total_length == 0xffffffff)
dae2dd0d 1028 {
dae2dd0d
NC
1029 lh.total_length = read_8_bytes (abfd, line_ptr);
1030 line_ptr += 8;
1031 offset_size = 8;
1032 }
91a4d569 1033 else if (lh.total_length == 0 && unit->addr_size == 8)
d03ba2a1 1034 {
91a4d569
AM
1035 /* Handle (non-standard) 64-bit DWARF2 formats. */
1036 lh.total_length = read_4_bytes (abfd, line_ptr);
1037 line_ptr += 4;
d03ba2a1
JJ
1038 offset_size = 8;
1039 }
252b5132
RH
1040 line_end = line_ptr + lh.total_length;
1041 lh.version = read_2_bytes (abfd, line_ptr);
1042 line_ptr += 2;
d03ba2a1
JJ
1043 if (offset_size == 4)
1044 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1045 else
1046 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1047 line_ptr += offset_size;
252b5132
RH
1048 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1049 line_ptr += 1;
1050 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1051 line_ptr += 1;
1052 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1053 line_ptr += 1;
1054 lh.line_range = read_1_byte (abfd, line_ptr);
1055 line_ptr += 1;
1056 lh.opcode_base = read_1_byte (abfd, line_ptr);
1057 line_ptr += 1;
dc810e39 1058 amt = lh.opcode_base * sizeof (unsigned char);
818a27ac 1059 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
252b5132
RH
1060
1061 lh.standard_opcode_lengths[0] = 1;
98591c73 1062
252b5132
RH
1063 for (i = 1; i < lh.opcode_base; ++i)
1064 {
1065 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1066 line_ptr += 1;
1067 }
1068
a092b084 1069 /* Read directory table. */
252b5132
RH
1070 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1071 {
1072 line_ptr += bytes_read;
98591c73 1073
252b5132
RH
1074 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1075 {
35330cce
NC
1076 char **tmp;
1077
dc810e39
AM
1078 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1079 amt *= sizeof (char *);
35330cce
NC
1080
1081 tmp = bfd_realloc (table->dirs, amt);
1082 if (tmp == NULL)
1083 {
1084 free (table->dirs);
1085 return NULL;
1086 }
1087 table->dirs = tmp;
252b5132 1088 }
98591c73 1089
252b5132
RH
1090 table->dirs[table->num_dirs++] = cur_dir;
1091 }
98591c73 1092
252b5132
RH
1093 line_ptr += bytes_read;
1094
a092b084 1095 /* Read file name table. */
252b5132
RH
1096 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1097 {
1098 line_ptr += bytes_read;
98591c73 1099
252b5132
RH
1100 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1101 {
35330cce
NC
1102 struct fileinfo *tmp;
1103
dc810e39
AM
1104 amt = table->num_files + FILE_ALLOC_CHUNK;
1105 amt *= sizeof (struct fileinfo);
35330cce
NC
1106
1107 tmp = bfd_realloc (table->files, amt);
1108 if (tmp == NULL)
1109 {
1110 free (table->files);
1111 free (table->dirs);
1112 return NULL;
1113 }
1114 table->files = tmp;
252b5132 1115 }
98591c73 1116
252b5132
RH
1117 table->files[table->num_files].name = cur_file;
1118 table->files[table->num_files].dir =
1119 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1120 line_ptr += bytes_read;
1121 table->files[table->num_files].time =
1122 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1123 line_ptr += bytes_read;
1124 table->files[table->num_files].size =
1125 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1126 line_ptr += bytes_read;
1127 table->num_files++;
1128 }
98591c73 1129
252b5132
RH
1130 line_ptr += bytes_read;
1131
1132 /* Read the statement sequences until there's nothing left. */
1133 while (line_ptr < line_end)
1134 {
a092b084 1135 /* State machine registers. */
252b5132 1136 bfd_vma address = 0;
8bfd78b3 1137 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
252b5132
RH
1138 unsigned int line = 1;
1139 unsigned int column = 0;
1140 int is_stmt = lh.default_is_stmt;
e2f6d277
NC
1141 int end_sequence = 0;
1142 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
e82ce529
AM
1143 compilers generate address sequences that are wildly out of
1144 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1145 for ia64-Linux). Thus, to determine the low and high
1146 address, we must compare on every DW_LNS_copy, etc. */
75758e9d 1147 bfd_vma low_pc = (bfd_vma) -1;
e2f6d277 1148 bfd_vma high_pc = 0;
252b5132 1149
a092b084 1150 /* Decode the table. */
252b5132
RH
1151 while (! end_sequence)
1152 {
1153 op_code = read_1_byte (abfd, line_ptr);
1154 line_ptr += 1;
98591c73 1155
1a509dcc 1156 if (op_code >= lh.opcode_base)
e2f6d277
NC
1157 {
1158 /* Special operand. */
1a509dcc
GK
1159 adj_opcode = op_code - lh.opcode_base;
1160 address += (adj_opcode / lh.line_range)
1161 * lh.minimum_instruction_length;
1162 line += lh.line_base + (adj_opcode % lh.line_range);
1163 /* Append row to matrix using current values. */
1164 add_line_info (table, address, filename, line, column, 0);
75758e9d
AM
1165 if (address < low_pc)
1166 low_pc = address;
e2f6d277
NC
1167 if (address > high_pc)
1168 high_pc = address;
1a509dcc
GK
1169 }
1170 else switch (op_code)
252b5132
RH
1171 {
1172 case DW_LNS_extended_op:
e2f6d277
NC
1173 /* Ignore length. */
1174 line_ptr += 1;
252b5132
RH
1175 extended_op = read_1_byte (abfd, line_ptr);
1176 line_ptr += 1;
e2f6d277 1177
252b5132
RH
1178 switch (extended_op)
1179 {
1180 case DW_LNE_end_sequence:
1181 end_sequence = 1;
f623be2b
RH
1182 add_line_info (table, address, filename, line, column,
1183 end_sequence);
75758e9d
AM
1184 if (address < low_pc)
1185 low_pc = address;
e2f6d277
NC
1186 if (address > high_pc)
1187 high_pc = address;
a13afe8e 1188 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
252b5132
RH
1189 break;
1190 case DW_LNE_set_address:
1191 address = read_address (unit, line_ptr);
1192 line_ptr += unit->addr_size;
1193 break;
1194 case DW_LNE_define_file:
1195 cur_file = read_string (abfd, line_ptr, &bytes_read);
1196 line_ptr += bytes_read;
1197 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1198 {
35330cce
NC
1199 struct fileinfo *tmp;
1200
dc810e39
AM
1201 amt = table->num_files + FILE_ALLOC_CHUNK;
1202 amt *= sizeof (struct fileinfo);
35330cce
NC
1203 tmp = bfd_realloc (table->files, amt);
1204 if (tmp == NULL)
d8d1c398 1205 {
35330cce
NC
1206 free (table->files);
1207 free (table->dirs);
1208 free (filename);
1209 return NULL;
d8d1c398 1210 }
35330cce 1211 table->files = tmp;
252b5132
RH
1212 }
1213 table->files[table->num_files].name = cur_file;
1214 table->files[table->num_files].dir =
1215 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1216 line_ptr += bytes_read;
1217 table->files[table->num_files].time =
1218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1219 line_ptr += bytes_read;
1220 table->files[table->num_files].size =
1221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1222 line_ptr += bytes_read;
1223 table->num_files++;
1224 break;
1225 default:
1226 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1227 bfd_set_error (bfd_error_bad_value);
35330cce
NC
1228 free (filename);
1229 free (table->files);
1230 free (table->dirs);
1231 return NULL;
252b5132
RH
1232 }
1233 break;
1234 case DW_LNS_copy:
159002ff 1235 add_line_info (table, address, filename, line, column, 0);
75758e9d
AM
1236 if (address < low_pc)
1237 low_pc = address;
e2f6d277
NC
1238 if (address > high_pc)
1239 high_pc = address;
252b5132
RH
1240 break;
1241 case DW_LNS_advance_pc:
1242 address += lh.minimum_instruction_length
1243 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1244 line_ptr += bytes_read;
1245 break;
1246 case DW_LNS_advance_line:
1247 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1248 line_ptr += bytes_read;
1249 break;
1250 case DW_LNS_set_file:
1251 {
1252 unsigned int file;
1253
e2f6d277
NC
1254 /* The file and directory tables are 0
1255 based, the references are 1 based. */
252b5132
RH
1256 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1257 line_ptr += bytes_read;
af3ef9fe
NC
1258 if (filename)
1259 free (filename);
252b5132
RH
1260 filename = concat_filename (table, file);
1261 break;
1262 }
1263 case DW_LNS_set_column:
1264 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1265 line_ptr += bytes_read;
1266 break;
1267 case DW_LNS_negate_stmt:
1268 is_stmt = (!is_stmt);
1269 break;
1270 case DW_LNS_set_basic_block:
252b5132
RH
1271 break;
1272 case DW_LNS_const_add_pc:
159002ff
RH
1273 address += lh.minimum_instruction_length
1274 * ((255 - lh.opcode_base) / lh.line_range);
252b5132
RH
1275 break;
1276 case DW_LNS_fixed_advance_pc:
1277 address += read_2_bytes (abfd, line_ptr);
1278 line_ptr += 2;
1279 break;
1a509dcc 1280 default:
e2f6d277 1281 {
1a509dcc 1282 int i;
5ed6aba4 1283
e2f6d277 1284 /* Unknown standard opcode, ignore it. */
1a509dcc
GK
1285 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1286 {
1287 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1288 line_ptr += bytes_read;
1289 }
1290 }
252b5132
RH
1291 }
1292 }
5ed6aba4 1293
af3ef9fe
NC
1294 if (filename)
1295 free (filename);
252b5132
RH
1296 }
1297
1298 return table;
1299}
1300
b34976b6
AM
1301/* If ADDR is within TABLE set the output parameters and return TRUE,
1302 otherwise return FALSE. The output parameters, FILENAME_PTR and
a092b084 1303 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132 1304
b34976b6 1305static bfd_boolean
818a27ac
AM
1306lookup_address_in_line_info_table (struct line_info_table *table,
1307 bfd_vma addr,
1308 struct funcinfo *function,
1309 const char **filename_ptr,
1310 unsigned int *linenumber_ptr)
252b5132 1311{
e82ce529 1312 /* Note: table->last_line should be a descendingly sorted list. */
159002ff 1313 struct line_info* next_line = table->last_line;
e82ce529
AM
1314 struct line_info* each_line = NULL;
1315 *filename_ptr = NULL;
98591c73 1316
159002ff 1317 if (!next_line)
b34976b6 1318 return FALSE;
159002ff
RH
1319
1320 each_line = next_line->prev_line;
1321
e82ce529
AM
1322 /* Check for large addresses */
1323 if (addr > next_line->address)
1324 each_line = NULL; /* ensure we skip over the normal case */
1325
1326 /* Normal case: search the list; save */
159002ff 1327 while (each_line && next_line)
252b5132 1328 {
e82ce529
AM
1329 /* If we have an address match, save this info. This allows us
1330 to return as good as results as possible for strange debugging
1331 info. */
b34976b6 1332 bfd_boolean addr_match = FALSE;
a13afe8e 1333 if (each_line->address <= addr && addr < next_line->address)
252b5132 1334 {
b34976b6 1335 addr_match = TRUE;
e82ce529 1336
1ee24f27
DJ
1337 /* If this line appears to span functions, and addr is in the
1338 later function, return the first line of that function instead
1339 of the last line of the earlier one. This check is for GCC
1340 2.95, which emits the first line number for a function late. */
a13afe8e
FF
1341
1342 if (function != NULL)
1ee24f27 1343 {
a13afe8e
FF
1344 bfd_vma lowest_pc;
1345 struct arange *arange;
1346
1347 /* Find the lowest address in the function's range list */
1348 lowest_pc = function->arange.low;
1349 for (arange = &function->arange;
1350 arange;
1351 arange = arange->next)
1352 {
1353 if (function->arange.low < lowest_pc)
1354 lowest_pc = function->arange.low;
1355 }
1356 /* Check for spanning function and set outgoing line info */
1357 if (addr >= lowest_pc
1358 && each_line->address < lowest_pc
1359 && next_line->address > lowest_pc)
1360 {
1361 *filename_ptr = next_line->filename;
1362 *linenumber_ptr = next_line->line;
1363 }
1364 else
1365 {
1366 *filename_ptr = each_line->filename;
1367 *linenumber_ptr = each_line->line;
1368 }
1ee24f27 1369 }
6bd00c5d
L
1370 else
1371 {
1372 *filename_ptr = each_line->filename;
1373 *linenumber_ptr = each_line->line;
1374 }
252b5132 1375 }
e82ce529
AM
1376
1377 if (addr_match && !each_line->end_sequence)
b34976b6 1378 return TRUE; /* we have definitely found what we want */
e82ce529 1379
159002ff
RH
1380 next_line = each_line;
1381 each_line = each_line->prev_line;
252b5132 1382 }
98591c73 1383
e82ce529
AM
1384 /* At this point each_line is NULL but next_line is not. If we found
1385 a candidate end-of-sequence point in the loop above, we can return
1386 that (compatibility with a bug in the Intel compiler); otherwise,
1387 assuming that we found the containing function for this address in
1388 this compilation unit, return the first line we have a number for
1389 (compatibility with GCC 2.95). */
1390 if (*filename_ptr == NULL && function != NULL)
1ee24f27
DJ
1391 {
1392 *filename_ptr = next_line->filename;
1393 *linenumber_ptr = next_line->line;
b34976b6 1394 return TRUE;
1ee24f27
DJ
1395 }
1396
b34976b6 1397 return FALSE;
252b5132 1398}
98591c73 1399
a13afe8e
FF
1400/* Read in the .debug_ranges section for future reference */
1401
1402static bfd_boolean
1403read_debug_ranges (struct comp_unit *unit)
1404{
1405 struct dwarf2_debug *stash = unit->stash;
1406 if (! stash->dwarf_ranges_buffer)
1407 {
1408 bfd *abfd = unit->abfd;
1409 asection *msec;
1410
1411 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1412 if (! msec)
1413 {
1414 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1415 bfd_set_error (bfd_error_bad_value);
1416 return FALSE;
1417 }
1418
1419 stash->dwarf_ranges_size = msec->size;
1420 stash->dwarf_ranges_buffer
1421 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1422 stash->syms);
1423 if (! stash->dwarf_ranges_buffer)
1424 return FALSE;
1425 }
1426 return TRUE;
1427}
1428
a092b084 1429/* Function table functions. */
252b5132 1430
a13afe8e
FF
1431/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1432 Note that we need to find the function that has the smallest
1433 range that contains ADDR, to handle inlined functions without
1434 depending upon them being ordered in TABLE by increasing range. */
252b5132 1435
b34976b6 1436static bfd_boolean
4ab527b0 1437lookup_address_in_function_table (struct comp_unit *unit,
818a27ac
AM
1438 bfd_vma addr,
1439 struct funcinfo **function_ptr,
1440 const char **functionname_ptr)
252b5132
RH
1441{
1442 struct funcinfo* each_func;
a13afe8e
FF
1443 struct funcinfo* best_fit = NULL;
1444 struct arange *arange;
252b5132 1445
4ab527b0 1446 for (each_func = unit->function_table;
252b5132
RH
1447 each_func;
1448 each_func = each_func->prev_func)
1449 {
a13afe8e
FF
1450 for (arange = &each_func->arange;
1451 arange;
1452 arange = arange->next)
252b5132 1453 {
a13afe8e
FF
1454 if (addr >= arange->low && addr < arange->high)
1455 {
1456 if (!best_fit ||
1457 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1458 best_fit = each_func;
1459 }
252b5132
RH
1460 }
1461 }
98591c73 1462
a13afe8e
FF
1463 if (best_fit)
1464 {
1465 *functionname_ptr = best_fit->name;
1466 *function_ptr = best_fit;
1467 return TRUE;
1468 }
1469 else
1470 {
1471 return FALSE;
1472 }
252b5132
RH
1473}
1474
5420f73d
L
1475/* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1476 and LINENUMBER_PTR, and return TRUE. */
1477
1478static bfd_boolean
1479lookup_symbol_in_function_table (struct comp_unit *unit,
1480 asymbol *sym,
1481 bfd_vma addr,
1482 const char **filename_ptr,
1483 unsigned int *linenumber_ptr)
1484{
1485 struct funcinfo* each_func;
1486 struct funcinfo* best_fit = NULL;
1487 struct arange *arange;
1488 const char *name = bfd_asymbol_name (sym);
1489 asection *sec = bfd_get_section (sym);
1490
1491 for (each_func = unit->function_table;
1492 each_func;
1493 each_func = each_func->prev_func)
1494 {
1495 for (arange = &each_func->arange;
1496 arange;
1497 arange = arange->next)
1498 {
1499 if ((!each_func->sec || each_func->sec == sec)
1500 && addr >= arange->low
1501 && addr < arange->high
650f284e 1502 && each_func->name
5420f73d
L
1503 && strcmp (name, each_func->name) == 0
1504 && (!best_fit
1505 || ((arange->high - arange->low)
1506 < (best_fit->arange.high - best_fit->arange.low))))
1507 best_fit = each_func;
1508 }
1509 }
1510
1511 if (best_fit)
1512 {
1513 best_fit->sec = sec;
1514 *filename_ptr = best_fit->file;
1515 *linenumber_ptr = best_fit->line;
1516 return TRUE;
1517 }
1518 else
1519 return FALSE;
1520}
1521
1522/* Variable table functions. */
1523
1524/* If SYM is within variable table of UNIT, set FILENAME_PTR and
1525 LINENUMBER_PTR, and return TRUE. */
1526
1527static bfd_boolean
1528lookup_symbol_in_variable_table (struct comp_unit *unit,
1529 asymbol *sym,
5cf2e3f0 1530 bfd_vma addr,
5420f73d
L
1531 const char **filename_ptr,
1532 unsigned int *linenumber_ptr)
1533{
1534 const char *name = bfd_asymbol_name (sym);
1535 asection *sec = bfd_get_section (sym);
1536 struct varinfo* each;
1537
1538 for (each = unit->variable_table; each; each = each->prev_var)
1539 if (each->stack == 0
5cf2e3f0
L
1540 && each->file != NULL
1541 && each->name != NULL
1542 && each->addr == addr
5420f73d
L
1543 && (!each->sec || each->sec == sec)
1544 && strcmp (name, each->name) == 0)
1545 break;
1546
1547 if (each)
1548 {
1549 each->sec = sec;
1550 *filename_ptr = each->file;
1551 *linenumber_ptr = each->line;
1552 return TRUE;
1553 }
1554 else
1555 return FALSE;
1556}
1557
06f22d7e
FF
1558static char *
1559find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1560{
1561 bfd *abfd = unit->abfd;
f075ee0c 1562 bfd_byte *info_ptr;
06f22d7e
FF
1563 unsigned int abbrev_number, bytes_read, i;
1564 struct abbrev_info *abbrev;
1565 struct attribute attr;
1566 char *name = 0;
1567
c0c28ab8 1568 info_ptr = unit->info_ptr_unit + die_ref;
06f22d7e
FF
1569 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1570 info_ptr += bytes_read;
1571
1572 if (abbrev_number)
1573 {
1574 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1575 if (! abbrev)
1576 {
1577 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1578 abbrev_number);
1579 bfd_set_error (bfd_error_bad_value);
1580 }
1581 else
1582 {
d5cbaa15 1583 for (i = 0; i < abbrev->num_attrs; ++i)
06f22d7e
FF
1584 {
1585 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
26bf4e33
FF
1586 switch (attr.name)
1587 {
1588 case DW_AT_name:
d5cbaa15
JW
1589 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1590 if (name == NULL)
1591 name = attr.u.str;
26bf4e33
FF
1592 break;
1593 case DW_AT_specification:
1594 name = find_abstract_instance_name (unit, attr.u.val);
1595 break;
d5cbaa15
JW
1596 case DW_AT_MIPS_linkage_name:
1597 name = attr.u.str;
1598 break;
26bf4e33
FF
1599 default:
1600 break;
1601 }
06f22d7e
FF
1602 }
1603 }
1604 }
1605 return (name);
1606}
1607
a13afe8e
FF
1608static void
1609read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1610{
1611 bfd_byte *ranges_ptr;
1612 bfd_vma base_address = unit->base_address;
1613
1614 if (! unit->stash->dwarf_ranges_buffer)
1615 {
1616 if (! read_debug_ranges (unit))
1617 return;
1618 }
1619 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
d8d1c398 1620
a13afe8e
FF
1621 for (;;)
1622 {
1623 bfd_vma low_pc;
1624 bfd_vma high_pc;
1625
a29b7e88 1626 if (unit->addr_size == 4)
a13afe8e
FF
1627 {
1628 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1629 ranges_ptr += 4;
1630 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1631 ranges_ptr += 4;
1632 }
1633 else
1634 {
1635 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1636 ranges_ptr += 8;
1637 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1638 ranges_ptr += 8;
1639 }
1640 if (low_pc == 0 && high_pc == 0)
1641 break;
1642 if (low_pc == -1UL && high_pc != -1UL)
1643 base_address = high_pc;
1644 else
e460dd0d 1645 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
a13afe8e
FF
1646 }
1647}
1648
a092b084 1649/* DWARF2 Compilation unit functions. */
252b5132
RH
1650
1651/* Scan over each die in a comp. unit looking for functions to add
5420f73d 1652 to the function table and variables to the variable table. */
252b5132 1653
b34976b6 1654static bfd_boolean
5420f73d 1655scan_unit_for_symbols (struct comp_unit *unit)
252b5132
RH
1656{
1657 bfd *abfd = unit->abfd;
f075ee0c 1658 bfd_byte *info_ptr = unit->first_child_die_ptr;
252b5132 1659 int nesting_level = 1;
c955f9cd
JW
1660 struct funcinfo **nested_funcs;
1661 int nested_funcs_size;
1662
1663 /* Maintain a stack of in-scope functions and inlined functions, which we
1664 can use to set the caller_func field. */
1665 nested_funcs_size = 32;
1666 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1667 if (nested_funcs == NULL)
1668 return FALSE;
1669 nested_funcs[nesting_level] = 0;
252b5132
RH
1670
1671 while (nesting_level)
1672 {
1673 unsigned int abbrev_number, bytes_read, i;
1674 struct abbrev_info *abbrev;
1675 struct attribute attr;
1676 struct funcinfo *func;
5420f73d 1677 struct varinfo *var;
a13afe8e
FF
1678 bfd_vma low_pc = 0;
1679 bfd_vma high_pc = 0;
252b5132
RH
1680
1681 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1682 info_ptr += bytes_read;
1683
1684 if (! abbrev_number)
1685 {
1686 nesting_level--;
1687 continue;
1688 }
98591c73 1689
252b5132
RH
1690 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1691 if (! abbrev)
1692 {
f46c2da6 1693 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1694 abbrev_number);
1695 bfd_set_error (bfd_error_bad_value);
c955f9cd 1696 free (nested_funcs);
b34976b6 1697 return FALSE;
252b5132 1698 }
98591c73 1699
5420f73d 1700 var = NULL;
06f22d7e 1701 if (abbrev->tag == DW_TAG_subprogram
5420f73d 1702 || abbrev->tag == DW_TAG_entry_point
06f22d7e 1703 || abbrev->tag == DW_TAG_inlined_subroutine)
252b5132 1704 {
dc810e39 1705 bfd_size_type amt = sizeof (struct funcinfo);
818a27ac 1706 func = bfd_zalloc (abfd, amt);
4ab527b0 1707 func->tag = abbrev->tag;
252b5132
RH
1708 func->prev_func = unit->function_table;
1709 unit->function_table = func;
c955f9cd
JW
1710
1711 if (func->tag == DW_TAG_inlined_subroutine)
1712 for (i = nesting_level - 1; i >= 1; i--)
1713 if (nested_funcs[i])
1714 {
1715 func->caller_func = nested_funcs[i];
1716 break;
1717 }
1718 nested_funcs[nesting_level] = func;
252b5132
RH
1719 }
1720 else
5420f73d
L
1721 {
1722 func = NULL;
1723 if (abbrev->tag == DW_TAG_variable)
1724 {
1725 bfd_size_type amt = sizeof (struct varinfo);
1726 var = bfd_zalloc (abfd, amt);
1727 var->tag = abbrev->tag;
1728 var->stack = 1;
1729 var->prev_var = unit->variable_table;
1730 unit->variable_table = var;
1731 }
c955f9cd
JW
1732
1733 /* No inline function in scope at this nesting level. */
1734 nested_funcs[nesting_level] = 0;
5420f73d 1735 }
98591c73 1736
252b5132
RH
1737 for (i = 0; i < abbrev->num_attrs; ++i)
1738 {
1739 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
98591c73 1740
252b5132
RH
1741 if (func)
1742 {
1743 switch (attr.name)
1744 {
4ab527b0
FF
1745 case DW_AT_call_file:
1746 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1747 break;
1748
1749 case DW_AT_call_line:
1750 func->caller_line = attr.u.val;
1751 break;
1752
06f22d7e
FF
1753 case DW_AT_abstract_origin:
1754 func->name = find_abstract_instance_name (unit, attr.u.val);
1755 break;
1756
252b5132 1757 case DW_AT_name:
252b5132
RH
1758 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1759 if (func->name == NULL)
482e2e37 1760 func->name = attr.u.str;
252b5132 1761 break;
98591c73 1762
252b5132 1763 case DW_AT_MIPS_linkage_name:
482e2e37 1764 func->name = attr.u.str;
252b5132
RH
1765 break;
1766
1767 case DW_AT_low_pc:
a13afe8e 1768 low_pc = attr.u.val;
252b5132
RH
1769 break;
1770
1771 case DW_AT_high_pc:
a13afe8e
FF
1772 high_pc = attr.u.val;
1773 break;
1774
1775 case DW_AT_ranges:
1776 read_rangelist (unit, &func->arange, attr.u.val);
252b5132
RH
1777 break;
1778
5420f73d
L
1779 case DW_AT_decl_file:
1780 func->file = concat_filename (unit->line_table,
1781 attr.u.val);
1782 break;
1783
1784 case DW_AT_decl_line:
1785 func->line = attr.u.val;
1786 break;
1787
1788 default:
1789 break;
1790 }
1791 }
1792 else if (var)
1793 {
1794 switch (attr.name)
1795 {
1796 case DW_AT_name:
1797 var->name = attr.u.str;
1798 break;
1799
1800 case DW_AT_decl_file:
1801 var->file = concat_filename (unit->line_table,
1802 attr.u.val);
1803 break;
1804
1805 case DW_AT_decl_line:
1806 var->line = attr.u.val;
1807 break;
1808
1809 case DW_AT_external:
1810 if (attr.u.val != 0)
1811 var->stack = 0;
1812 break;
1813
1814 case DW_AT_location:
5cf2e3f0 1815 switch (attr.form)
5420f73d 1816 {
5cf2e3f0
L
1817 case DW_FORM_block:
1818 case DW_FORM_block1:
1819 case DW_FORM_block2:
1820 case DW_FORM_block4:
1821 if (*attr.u.blk->data == DW_OP_addr)
5420f73d 1822 {
5cf2e3f0 1823 var->stack = 0;
98b880f4
JW
1824
1825 /* Verify that DW_OP_addr is the only opcode in the
1826 location, in which case the block size will be 1
1827 plus the address size. */
1828 /* ??? For TLS variables, gcc can emit
1829 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1830 which we don't handle here yet. */
1831 if (attr.u.blk->size == unit->addr_size + 1U)
1832 var->addr = bfd_get (unit->addr_size * 8,
1833 unit->abfd,
1834 attr.u.blk->data + 1);
5420f73d 1835 }
5cf2e3f0 1836 break;
d8d1c398 1837
5cf2e3f0
L
1838 default:
1839 break;
5420f73d
L
1840 }
1841 break;
1842
252b5132
RH
1843 default:
1844 break;
1845 }
1846 }
1847 }
1848
a13afe8e
FF
1849 if (func && high_pc != 0)
1850 {
1851 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1852 }
1853
252b5132 1854 if (abbrev->has_children)
c955f9cd
JW
1855 {
1856 nesting_level++;
1857
1858 if (nesting_level >= nested_funcs_size)
1859 {
1860 struct funcinfo **tmp;
1861
1862 nested_funcs_size *= 2;
1863 tmp = bfd_realloc (nested_funcs,
1864 (nested_funcs_size
1865 * sizeof (struct funcinfo *)));
1866 if (tmp == NULL)
1867 {
1868 free (nested_funcs);
1869 return FALSE;
1870 }
1871 nested_funcs = tmp;
1872 }
1873 nested_funcs[nesting_level] = 0;
1874 }
252b5132
RH
1875 }
1876
c955f9cd 1877 free (nested_funcs);
b34976b6 1878 return TRUE;
252b5132
RH
1879}
1880
5e38c3b8
MM
1881/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1882 includes the compilation unit header that proceeds the DIE's, but
5c4491d3 1883 does not include the length field that precedes each compilation
5e38c3b8 1884 unit header. END_PTR points one past the end of this comp unit.
d03ba2a1 1885 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
252b5132
RH
1886
1887 This routine does not read the whole compilation unit; only enough
1888 to get to the line number information for the compilation unit. */
1889
1890static struct comp_unit *
818a27ac
AM
1891parse_comp_unit (bfd *abfd,
1892 struct dwarf2_debug *stash,
1893 bfd_vma unit_length,
f075ee0c 1894 bfd_byte *info_ptr_unit,
818a27ac 1895 unsigned int offset_size)
252b5132
RH
1896{
1897 struct comp_unit* unit;
f46c2da6 1898 unsigned int version;
8ce8c090 1899 bfd_uint64_t abbrev_offset = 0;
f46c2da6 1900 unsigned int addr_size;
252b5132 1901 struct abbrev_info** abbrevs;
252b5132
RH
1902 unsigned int abbrev_number, bytes_read, i;
1903 struct abbrev_info *abbrev;
1904 struct attribute attr;
f075ee0c
AM
1905 bfd_byte *info_ptr = stash->info_ptr;
1906 bfd_byte *end_ptr = info_ptr + unit_length;
dc810e39 1907 bfd_size_type amt;
a13afe8e
FF
1908 bfd_vma low_pc = 0;
1909 bfd_vma high_pc = 0;
3fde5a36 1910
252b5132
RH
1911 version = read_2_bytes (abfd, info_ptr);
1912 info_ptr += 2;
d03ba2a1
JJ
1913 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1914 if (offset_size == 4)
5e38c3b8 1915 abbrev_offset = read_4_bytes (abfd, info_ptr);
d03ba2a1 1916 else
5e38c3b8 1917 abbrev_offset = read_8_bytes (abfd, info_ptr);
d03ba2a1 1918 info_ptr += offset_size;
252b5132
RH
1919 addr_size = read_1_byte (abfd, info_ptr);
1920 info_ptr += 1;
1921
1922 if (version != 2)
1923 {
f46c2da6 1924 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
252b5132
RH
1925 bfd_set_error (bfd_error_bad_value);
1926 return 0;
1927 }
1928
1929 if (addr_size > sizeof (bfd_vma))
1930 {
1931 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1932 addr_size,
f46c2da6 1933 (unsigned int) sizeof (bfd_vma));
252b5132
RH
1934 bfd_set_error (bfd_error_bad_value);
1935 return 0;
1936 }
1937
ecb651f0 1938 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 1939 {
f5a3e38a 1940 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
252b5132
RH
1941 bfd_set_error (bfd_error_bad_value);
1942 return 0;
1943 }
1944
a092b084 1945 /* Read the abbrevs for this compilation unit into a table. */
51db3708 1946 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
1947 if (! abbrevs)
1948 return 0;
1949
1950 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1951 info_ptr += bytes_read;
1952 if (! abbrev_number)
1953 {
f46c2da6 1954 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
252b5132
RH
1955 abbrev_number);
1956 bfd_set_error (bfd_error_bad_value);
1957 return 0;
1958 }
1959
1960 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1961 if (! abbrev)
1962 {
f46c2da6 1963 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
1964 abbrev_number);
1965 bfd_set_error (bfd_error_bad_value);
1966 return 0;
1967 }
98591c73 1968
dc810e39 1969 amt = sizeof (struct comp_unit);
818a27ac 1970 unit = bfd_zalloc (abfd, amt);
252b5132 1971 unit->abfd = abfd;
98591c73 1972 unit->addr_size = addr_size;
d03ba2a1 1973 unit->offset_size = offset_size;
252b5132
RH
1974 unit->abbrevs = abbrevs;
1975 unit->end_ptr = end_ptr;
d03ba2a1 1976 unit->stash = stash;
c0c28ab8 1977 unit->info_ptr_unit = info_ptr_unit;
252b5132
RH
1978
1979 for (i = 0; i < abbrev->num_attrs; ++i)
1980 {
1981 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1982
1983 /* Store the data if it is of an attribute we want to keep in a
1984 partial symbol table. */
1985 switch (attr.name)
1986 {
1987 case DW_AT_stmt_list:
1988 unit->stmtlist = 1;
482e2e37 1989 unit->line_offset = attr.u.val;
252b5132
RH
1990 break;
1991
1992 case DW_AT_name:
482e2e37 1993 unit->name = attr.u.str;
252b5132
RH
1994 break;
1995
1996 case DW_AT_low_pc:
a13afe8e
FF
1997 low_pc = attr.u.val;
1998 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1999 this is the base address to use when reading location
2000 lists or range lists. */
2001 unit->base_address = low_pc;
252b5132
RH
2002 break;
2003
2004 case DW_AT_high_pc:
a13afe8e
FF
2005 high_pc = attr.u.val;
2006 break;
2007
2008 case DW_AT_ranges:
2009 read_rangelist (unit, &unit->arange, attr.u.val);
252b5132
RH
2010 break;
2011
2012 case DW_AT_comp_dir:
2013 {
f075ee0c 2014 char *comp_dir = attr.u.str;
252b5132
RH
2015 if (comp_dir)
2016 {
2017 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2018 directory, get rid of it. */
818a27ac 2019 char *cp = strchr (comp_dir, ':');
252b5132
RH
2020
2021 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2022 comp_dir = cp + 1;
2023 }
2024 unit->comp_dir = comp_dir;
2025 break;
2026 }
2027
2028 default:
2029 break;
2030 }
2031 }
a13afe8e
FF
2032 if (high_pc != 0)
2033 {
2034 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2035 }
252b5132
RH
2036
2037 unit->first_child_die_ptr = info_ptr;
2038 return unit;
2039}
2040
6dd55cb7
L
2041/* Return TRUE if UNIT may contain the address given by ADDR. When
2042 there are functions written entirely with inline asm statements, the
2043 range info in the compilation unit header may not be correct. We
2044 need to consult the line info table to see if a compilation unit
2045 really contains the given address. */
252b5132 2046
b34976b6 2047static bfd_boolean
818a27ac 2048comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
252b5132 2049{
f623be2b
RH
2050 struct arange *arange;
2051
2052 if (unit->error)
b34976b6 2053 return FALSE;
f623be2b
RH
2054
2055 arange = &unit->arange;
2056 do
2057 {
2058 if (addr >= arange->low && addr < arange->high)
b34976b6 2059 return TRUE;
f623be2b
RH
2060 arange = arange->next;
2061 }
2062 while (arange);
98591c73 2063
b34976b6 2064 return FALSE;
252b5132
RH
2065}
2066
252b5132
RH
2067/* If UNIT contains ADDR, set the output parameters to the values for
2068 the line containing ADDR. The output parameters, FILENAME_PTR,
2069 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 2070 to be filled in.
252b5132 2071
ab3acfbe 2072 Return TRUE if UNIT contains ADDR, and no errors were encountered;
b34976b6 2073 FALSE otherwise. */
252b5132 2074
b34976b6 2075static bfd_boolean
818a27ac
AM
2076comp_unit_find_nearest_line (struct comp_unit *unit,
2077 bfd_vma addr,
2078 const char **filename_ptr,
2079 const char **functionname_ptr,
2080 unsigned int *linenumber_ptr,
2081 struct dwarf2_debug *stash)
252b5132 2082{
b34976b6
AM
2083 bfd_boolean line_p;
2084 bfd_boolean func_p;
1ee24f27 2085 struct funcinfo *function;
98591c73 2086
252b5132 2087 if (unit->error)
b34976b6 2088 return FALSE;
252b5132
RH
2089
2090 if (! unit->line_table)
2091 {
2092 if (! unit->stmtlist)
2093 {
2094 unit->error = 1;
b34976b6 2095 return FALSE;
252b5132 2096 }
98591c73 2097
51db3708 2098 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
2099
2100 if (! unit->line_table)
2101 {
2102 unit->error = 1;
b34976b6 2103 return FALSE;
252b5132 2104 }
98591c73 2105
3f5864e1 2106 if (unit->first_child_die_ptr < unit->end_ptr
5420f73d 2107 && ! scan_unit_for_symbols (unit))
252b5132
RH
2108 {
2109 unit->error = 1;
b34976b6 2110 return FALSE;
252b5132
RH
2111 }
2112 }
2113
1ee24f27 2114 function = NULL;
4ab527b0 2115 func_p = lookup_address_in_function_table (unit, addr,
e2f6d277 2116 &function, functionname_ptr);
4ab527b0
FF
2117 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2118 stash->inliner_chain = function;
e2f6d277
NC
2119 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2120 function, filename_ptr,
252b5132 2121 linenumber_ptr);
b34976b6 2122 return line_p || func_p;
252b5132
RH
2123}
2124
5420f73d
L
2125/* If UNIT contains SYM at ADDR, set the output parameters to the
2126 values for the line containing SYM. The output parameters,
2127 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2128 filled in.
2129
2130 Return TRUE if UNIT contains SYM, and no errors were encountered;
2131 FALSE otherwise. */
2132
2133static bfd_boolean
2134comp_unit_find_line (struct comp_unit *unit,
2135 asymbol *sym,
2136 bfd_vma addr,
2137 const char **filename_ptr,
2138 unsigned int *linenumber_ptr,
2139 struct dwarf2_debug *stash)
2140{
2141 if (unit->error)
2142 return FALSE;
2143
2144 if (! unit->line_table)
2145 {
2146 if (! unit->stmtlist)
2147 {
2148 unit->error = 1;
2149 return FALSE;
2150 }
2151
2152 unit->line_table = decode_line_info (unit, stash);
2153
2154 if (! unit->line_table)
2155 {
2156 unit->error = 1;
2157 return FALSE;
2158 }
2159
2160 if (unit->first_child_die_ptr < unit->end_ptr
2161 && ! scan_unit_for_symbols (unit))
2162 {
2163 unit->error = 1;
2164 return FALSE;
2165 }
2166 }
2167
2168 if (sym->flags & BSF_FUNCTION)
2169 return lookup_symbol_in_function_table (unit, sym, addr,
2170 filename_ptr,
2171 linenumber_ptr);
2172 else
5cf2e3f0
L
2173 return lookup_symbol_in_variable_table (unit, sym, addr,
2174 filename_ptr,
5420f73d
L
2175 linenumber_ptr);
2176}
2177
e2f6d277
NC
2178/* Locate a section in a BFD containing debugging info. The search starts
2179 from the section after AFTER_SEC, or from the first section in the BFD if
2180 AFTER_SEC is NULL. The search works by examining the names of the
2181 sections. There are two permissiable names. The first is .debug_info.
2182 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2183 This is a variation on the .debug_info section which has a checksum
2184 describing the contents appended onto the name. This allows the linker to
2185 identify and discard duplicate debugging sections for different
2186 compilation units. */
a092b084
NC
2187#define DWARF2_DEBUG_INFO ".debug_info"
2188#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2189
2190static asection *
818a27ac 2191find_debug_info (bfd *abfd, asection *after_sec)
a092b084
NC
2192{
2193 asection * msec;
2194
2195 if (after_sec)
2196 msec = after_sec->next;
2197 else
2198 msec = abfd->sections;
2199
2200 while (msec)
2201 {
2202 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2203 return msec;
2204
0112cd26 2205 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
a092b084
NC
2206 return msec;
2207
2208 msec = msec->next;
2209 }
2210
2211 return NULL;
2212}
2213
d4c32a81
L
2214/* Unset vmas for loadable sections in STASH. */
2215
2216static void
2217unset_sections (struct dwarf2_debug *stash)
2218{
2219 unsigned int i;
2220 struct loadable_section *p;
2221
2222 i = stash->loadable_section_count;
2223 p = stash->loadable_sections;
2224 for (; i > 0; i--, p++)
2225 p->section->vma = 0;
2226}
2227
2228/* Set unique vmas for loadable sections in ABFD and save vmas in
2229 STASH for unset_sections. */
35ccda9e
L
2230
2231static bfd_boolean
d4c32a81 2232place_sections (bfd *abfd, struct dwarf2_debug *stash)
35ccda9e 2233{
d4c32a81
L
2234 struct loadable_section *p;
2235 unsigned int i;
2236
2237 if (stash->loadable_section_count != 0)
35ccda9e 2238 {
d4c32a81
L
2239 i = stash->loadable_section_count;
2240 p = stash->loadable_sections;
2241 for (; i > 0; i--, p++)
2242 p->section->vma = p->adj_vma;
2243 }
2244 else
2245 {
2246 asection *sect;
2247 bfd_vma last_vma = 0;
2248 bfd_size_type amt;
2249 struct loadable_section *p;
35ccda9e 2250
d4c32a81
L
2251 i = 0;
2252 for (sect = abfd->sections; sect != NULL; sect = sect->next)
35ccda9e 2253 {
d4c32a81
L
2254 bfd_size_type sz;
2255
2256 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2257 continue;
2258
2259 sz = sect->rawsize ? sect->rawsize : sect->size;
2260 if (sz == 0)
2261 continue;
2262
2263 i++;
2264 }
2265
2266 amt = i * sizeof (struct loadable_section);
2267 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2268 if (! p)
2269 return FALSE;
2270
2271 stash->loadable_sections = p;
2272 stash->loadable_section_count = i;
2273
2274 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2275 {
2276 bfd_size_type sz;
2277
2278 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2279 continue;
2280
2281 sz = sect->rawsize ? sect->rawsize : sect->size;
2282 if (sz == 0)
2283 continue;
2284
2285 p->section = sect;
2286 if (last_vma != 0)
2287 {
2288 /* Align the new address to the current section
2289 alignment. */
2290 last_vma = ((last_vma
2291 + ~((bfd_vma) -1 << sect->alignment_power))
2292 & ((bfd_vma) -1 << sect->alignment_power));
2293 sect->vma = last_vma;
2294 }
2295 p->adj_vma = sect->vma;
2296 last_vma += sect->vma + sz;
2297
2298 p++;
35ccda9e
L
2299 }
2300 }
2301
2302 return TRUE;
2303}
2304
cd917290 2305/* The DWARF2 version of find_nearest_line. Return TRUE if the line
5e38c3b8
MM
2306 is found without error. ADDR_SIZE is the number of bytes in the
2307 initial .debug_info length field and in the abbreviation offset.
2308 You may use zero to indicate that the default value should be
2309 used. */
252b5132 2310
b34976b6 2311bfd_boolean
818a27ac
AM
2312_bfd_dwarf2_find_nearest_line (bfd *abfd,
2313 asection *section,
2314 asymbol **symbols,
2315 bfd_vma offset,
2316 const char **filename_ptr,
2317 const char **functionname_ptr,
2318 unsigned int *linenumber_ptr,
2319 unsigned int addr_size,
2320 void **pinfo)
252b5132
RH
2321{
2322 /* Read each compilation unit from the section .debug_info, and check
2323 to see if it contains the address we are searching for. If yes,
2324 lookup the address, and return the line number info. If no, go
98591c73 2325 on to the next compilation unit.
252b5132
RH
2326
2327 We keep a list of all the previously read compilation units, and
98591c73 2328 a pointer to the next un-read compilation unit. Check the
a092b084 2329 previously read units before reading more. */
1ba54ee0 2330 struct dwarf2_debug *stash;
252b5132 2331
a092b084 2332 /* What address are we looking for? */
1ba54ee0 2333 bfd_vma addr;
252b5132
RH
2334
2335 struct comp_unit* each;
98591c73 2336
d4c32a81
L
2337 bfd_vma found = FALSE;
2338
1ba54ee0 2339 stash = *pinfo;
d4c32a81
L
2340
2341 if (! stash)
2342 {
2343 bfd_size_type amt = sizeof (struct dwarf2_debug);
2344
2345 stash = bfd_zalloc (abfd, amt);
2346 if (! stash)
2347 return FALSE;
2348 }
2349
2350 /* In a relocatable file, 2 functions may have the same address.
2351 We change the section vma so that they won't overlap. */
2352 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2353 {
2354 if (! place_sections (abfd, stash))
2355 return FALSE;
2356 }
2357
1ba54ee0
AM
2358 addr = offset;
2359 if (section->output_section)
6dd55cb7 2360 addr += section->output_section->vma + section->output_offset;
1ba54ee0 2361 else
6dd55cb7 2362 addr += section->vma;
252b5132
RH
2363 *filename_ptr = NULL;
2364 *functionname_ptr = NULL;
2365 *linenumber_ptr = 0;
2366
5e38c3b8
MM
2367 /* The DWARF2 spec says that the initial length field, and the
2368 offset of the abbreviation table, should both be 4-byte values.
2369 However, some compilers do things differently. */
2370 if (addr_size == 0)
2371 addr_size = 4;
1efe4b10 2372 BFD_ASSERT (addr_size == 4 || addr_size == 8);
98591c73 2373
d4c32a81 2374 if (! *pinfo)
252b5132 2375 {
dc810e39 2376 bfd_size_type total_size;
252b5132 2377 asection *msec;
252b5132 2378
818a27ac 2379 *pinfo = stash;
3fde5a36 2380
a092b084
NC
2381 msec = find_debug_info (abfd, NULL);
2382 if (! msec)
2383 /* No dwarf2 info. Note that at this point the stash
2384 has been allocated, but contains zeros, this lets
2385 future calls to this function fail quicker. */
d4c32a81 2386 goto done;
a092b084
NC
2387
2388 /* There can be more than one DWARF2 info section in a BFD these days.
e82ce529 2389 Read them all in and produce one large stash. We do this in two
a092b084
NC
2390 passes - in the first pass we just accumulate the section sizes.
2391 In the second pass we read in the section's contents. The allows
2392 us to avoid reallocing the data as we add sections to the stash. */
2393 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
eea6121a 2394 total_size += msec->size;
98591c73 2395
818a27ac 2396 stash->info_ptr = bfd_alloc (abfd, total_size);
a092b084 2397 if (stash->info_ptr == NULL)
d4c32a81 2398 goto done;
252b5132 2399
a092b084
NC
2400 stash->info_ptr_end = stash->info_ptr;
2401
2402 for (msec = find_debug_info (abfd, NULL);
2403 msec;
2404 msec = find_debug_info (abfd, msec))
252b5132 2405 {
dc810e39
AM
2406 bfd_size_type size;
2407 bfd_size_type start;
a092b084 2408
eea6121a 2409 size = msec->size;
a092b084
NC
2410 if (size == 0)
2411 continue;
2412
2413 start = stash->info_ptr_end - stash->info_ptr;
2414
6e84a906
DJ
2415 if ((bfd_simple_get_relocated_section_contents
2416 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
a092b084
NC
2417 continue;
2418
2419 stash->info_ptr_end = stash->info_ptr + start + size;
252b5132
RH
2420 }
2421
f2363ce5
AO
2422 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2423
2424 stash->sec = find_debug_info (abfd, NULL);
2425 stash->sec_info_ptr = stash->info_ptr;
2426 stash->syms = symbols;
252b5132 2427 }
a092b084 2428
98591c73 2429 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 2430 (or that an error occured while setting up the stash). */
252b5132 2431 if (! stash->info_ptr)
d4c32a81 2432 goto done;
252b5132 2433
4ab527b0
FF
2434 stash->inliner_chain = NULL;
2435
a092b084 2436 /* Check the previously read comp. units first. */
252b5132 2437 for (each = stash->all_comp_units; each; each = each->next_unit)
6dd55cb7
L
2438 if (comp_unit_contains_address (each, addr)
2439 && comp_unit_find_nearest_line (each, addr, filename_ptr,
2440 functionname_ptr,
d4c32a81
L
2441 linenumber_ptr, stash))
2442 {
2443 found = TRUE;
2444 goto done;
2445 }
252b5132 2446
a092b084 2447 /* Read each remaining comp. units checking each as they are read. */
252b5132
RH
2448 while (stash->info_ptr < stash->info_ptr_end)
2449 {
5e38c3b8 2450 bfd_vma length;
d03ba2a1 2451 unsigned int offset_size = addr_size;
f075ee0c 2452 bfd_byte *info_ptr_unit = stash->info_ptr;
252b5132 2453
a3805e4e
AO
2454 length = read_4_bytes (abfd, stash->info_ptr);
2455 /* A 0xffffff length is the DWARF3 way of indicating we use
2456 64-bit offsets, instead of 32-bit offsets. */
2457 if (length == 0xffffffff)
d03ba2a1 2458 {
a3805e4e
AO
2459 offset_size = 8;
2460 length = read_8_bytes (abfd, stash->info_ptr + 4);
2461 stash->info_ptr += 12;
2462 }
2463 /* A zero length is the IRIX way of indicating 64-bit offsets,
2464 mostly because the 64-bit length will generally fit in 32
2465 bits, and the endianness helps. */
2466 else if (length == 0)
2467 {
2468 offset_size = 8;
2469 length = read_4_bytes (abfd, stash->info_ptr + 4);
2470 stash->info_ptr += 8;
2471 }
024b2372
CD
2472 /* In the absence of the hints above, we assume 32-bit DWARF2
2473 offsets even for targets with 64-bit addresses, because:
2474 a) most of the time these targets will not have generated
2475 more than 2Gb of debug info and so will not need 64-bit
2476 offsets,
2477 and
2478 b) if they do use 64-bit offsets but they are not using
2479 the size hints that are tested for above then they are
2480 not conforming to the DWARF3 standard anyway. */
a3805e4e
AO
2481 else if (addr_size == 8)
2482 {
024b2372
CD
2483 offset_size = 4;
2484 stash->info_ptr += 4;
d03ba2a1 2485 }
5e38c3b8 2486 else
a3805e4e 2487 stash->info_ptr += 4;
252b5132
RH
2488
2489 if (length > 0)
e82ce529 2490 {
c0c28ab8
L
2491 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2492 offset_size);
252b5132
RH
2493 stash->info_ptr += length;
2494
f2363ce5 2495 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
eea6121a 2496 == stash->sec->size)
f2363ce5
AO
2497 {
2498 stash->sec = find_debug_info (abfd, stash->sec);
2499 stash->sec_info_ptr = stash->info_ptr;
2500 }
2501
252b5132
RH
2502 if (each)
2503 {
2504 each->next_unit = stash->all_comp_units;
2505 stash->all_comp_units = each;
2506
159002ff
RH
2507 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2508 compilation units. If we don't have them (i.e.,
2509 unit->high == 0), we need to consult the line info
2510 table to see if a compilation unit contains the given
a092b084 2511 address. */
6dd55cb7
L
2512 if ((each->arange.high == 0
2513 || comp_unit_contains_address (each, addr))
2514 && comp_unit_find_nearest_line (each, addr,
2515 filename_ptr,
2516 functionname_ptr,
2517 linenumber_ptr,
d4c32a81
L
2518 stash))
2519 {
2520 found = TRUE;
2521 goto done;
2522 }
252b5132
RH
2523 }
2524 }
2525 }
2526
d4c32a81
L
2527done:
2528 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2529 unset_sections (stash);
2530
2531 return found;
252b5132 2532}
35330cce 2533
5420f73d
L
2534/* The DWARF2 version of find_line. Return TRUE if the line is found
2535 without error. */
2536
2537bfd_boolean
2538_bfd_dwarf2_find_line (bfd *abfd,
2539 asymbol **symbols,
2540 asymbol *symbol,
2541 const char **filename_ptr,
2542 unsigned int *linenumber_ptr,
2543 unsigned int addr_size,
2544 void **pinfo)
2545{
2546 /* Read each compilation unit from the section .debug_info, and check
2547 to see if it contains the address we are searching for. If yes,
2548 lookup the address, and return the line number info. If no, go
2549 on to the next compilation unit.
2550
2551 We keep a list of all the previously read compilation units, and
2552 a pointer to the next un-read compilation unit. Check the
2553 previously read units before reading more. */
2554 struct dwarf2_debug *stash;
2555
2556 /* What address are we looking for? */
2557 bfd_vma addr;
2558
2559 struct comp_unit* each;
2560
2561 asection *section;
2562
d4c32a81 2563 bfd_boolean found = FALSE;
5420f73d
L
2564
2565 section = bfd_get_section (symbol);
2566
d4c32a81
L
2567 stash = *pinfo;
2568
2569 if (! stash)
2570 {
2571 bfd_size_type amt = sizeof (struct dwarf2_debug);
2572
2573 stash = bfd_zalloc (abfd, amt);
2574 if (! stash)
2575 return FALSE;
2576 }
2577
2578 /* In a relocatable file, 2 functions may have the same address.
2579 We change the section vma so that they won't overlap. */
2580 if (!stash && (abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2581 {
2582 if (! place_sections (abfd, stash))
2583 return FALSE;
2584 }
2585
5420f73d
L
2586 addr = symbol->value;
2587 if (section->output_section)
6dd55cb7 2588 addr += section->output_section->vma + section->output_offset;
5420f73d 2589 else
6dd55cb7 2590 addr += section->vma;
5420f73d
L
2591
2592 *filename_ptr = NULL;
5420f73d
L
2593 *filename_ptr = NULL;
2594 *linenumber_ptr = 0;
2595
d4c32a81 2596 if (! *pinfo)
5420f73d
L
2597 {
2598 bfd_size_type total_size;
2599 asection *msec;
5420f73d
L
2600
2601 *pinfo = stash;
2602
2603 msec = find_debug_info (abfd, NULL);
2604 if (! msec)
2605 /* No dwarf2 info. Note that at this point the stash
2606 has been allocated, but contains zeros, this lets
2607 future calls to this function fail quicker. */
d4c32a81 2608 goto done;
5420f73d
L
2609
2610 /* There can be more than one DWARF2 info section in a BFD these days.
2611 Read them all in and produce one large stash. We do this in two
2612 passes - in the first pass we just accumulate the section sizes.
2613 In the second pass we read in the section's contents. The allows
2614 us to avoid reallocing the data as we add sections to the stash. */
2615 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2616 total_size += msec->size;
2617
2618 stash->info_ptr = bfd_alloc (abfd, total_size);
2619 if (stash->info_ptr == NULL)
d4c32a81 2620 goto done;
5420f73d
L
2621
2622 stash->info_ptr_end = stash->info_ptr;
2623
2624 for (msec = find_debug_info (abfd, NULL);
2625 msec;
2626 msec = find_debug_info (abfd, msec))
2627 {
2628 bfd_size_type size;
2629 bfd_size_type start;
2630
2631 size = msec->size;
2632 if (size == 0)
2633 continue;
2634
2635 start = stash->info_ptr_end - stash->info_ptr;
2636
2637 if ((bfd_simple_get_relocated_section_contents
2638 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2639 continue;
2640
2641 stash->info_ptr_end = stash->info_ptr + start + size;
2642 }
2643
2644 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2645
2646 stash->sec = find_debug_info (abfd, NULL);
2647 stash->sec_info_ptr = stash->info_ptr;
2648 stash->syms = symbols;
2649 }
2650
2651 /* A null info_ptr indicates that there is no dwarf2 info
2652 (or that an error occured while setting up the stash). */
2653 if (! stash->info_ptr)
d4c32a81 2654 goto done;
5420f73d
L
2655
2656 stash->inliner_chain = NULL;
2657
2658 /* Check the previously read comp. units first. */
2659 for (each = stash->all_comp_units; each; each = each->next_unit)
2660 if ((symbol->flags & BSF_FUNCTION) == 0
2661 || comp_unit_contains_address (each, addr))
2662 {
2663 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
2664 linenumber_ptr, stash);
2665 if (found)
d4c32a81 2666 goto done;
5420f73d
L
2667 }
2668
2669 /* The DWARF2 spec says that the initial length field, and the
2670 offset of the abbreviation table, should both be 4-byte values.
2671 However, some compilers do things differently. */
2672 if (addr_size == 0)
2673 addr_size = 4;
2674 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2675
2676 /* Read each remaining comp. units checking each as they are read. */
2677 while (stash->info_ptr < stash->info_ptr_end)
2678 {
2679 bfd_vma length;
2680 unsigned int offset_size = addr_size;
2681 bfd_byte *info_ptr_unit = stash->info_ptr;
2682
2683 length = read_4_bytes (abfd, stash->info_ptr);
2684 /* A 0xffffff length is the DWARF3 way of indicating we use
2685 64-bit offsets, instead of 32-bit offsets. */
2686 if (length == 0xffffffff)
2687 {
2688 offset_size = 8;
2689 length = read_8_bytes (abfd, stash->info_ptr + 4);
2690 stash->info_ptr += 12;
2691 }
2692 /* A zero length is the IRIX way of indicating 64-bit offsets,
2693 mostly because the 64-bit length will generally fit in 32
2694 bits, and the endianness helps. */
2695 else if (length == 0)
2696 {
2697 offset_size = 8;
2698 length = read_4_bytes (abfd, stash->info_ptr + 4);
2699 stash->info_ptr += 8;
2700 }
024b2372
CD
2701 /* In the absence of the hints above, we assume 32-bit DWARF2
2702 offsets even for targets with 64-bit addresses, because:
2703 a) most of the time these targets will not have generated
2704 more than 2Gb of debug info and so will not need 64-bit
2705 offsets,
2706 and
2707 b) if they do use 64-bit offsets but they are not using
2708 the size hints that are tested for above then they are
2709 not conforming to the DWARF3 standard anyway. */
5420f73d
L
2710 else if (addr_size == 8)
2711 {
024b2372
CD
2712 offset_size = 4;
2713 stash->info_ptr += 4;
5420f73d
L
2714 }
2715 else
2716 stash->info_ptr += 4;
2717
2718 if (length > 0)
2719 {
2720 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2721 offset_size);
2722 stash->info_ptr += length;
2723
2724 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2725 == stash->sec->size)
2726 {
2727 stash->sec = find_debug_info (abfd, stash->sec);
2728 stash->sec_info_ptr = stash->info_ptr;
2729 }
2730
2731 if (each)
2732 {
2733 each->next_unit = stash->all_comp_units;
2734 stash->all_comp_units = each;
2735
2736 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2737 compilation units. If we don't have them (i.e.,
2738 unit->high == 0), we need to consult the line info
2739 table to see if a compilation unit contains the given
2740 address. */
2741 found = (((symbol->flags & BSF_FUNCTION) == 0
2742 || each->arange.high <= 0
2743 || comp_unit_contains_address (each, addr))
2744 && comp_unit_find_line (each, symbol, addr,
2745 filename_ptr,
2746 linenumber_ptr,
2747 stash));
2748 if (found)
d4c32a81 2749 goto done;
5420f73d
L
2750 }
2751 }
2752 }
2753
d4c32a81
L
2754done:
2755 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2756 unset_sections (stash);
2757
2758 return found;
5420f73d
L
2759}
2760
4ab527b0
FF
2761bfd_boolean
2762_bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2763 const char **filename_ptr,
2764 const char **functionname_ptr,
2765 unsigned int *linenumber_ptr,
2766 void **pinfo)
2767{
2768 struct dwarf2_debug *stash;
2769
2770 stash = *pinfo;
2771 if (stash)
2772 {
2773 struct funcinfo *func = stash->inliner_chain;
2774 if (func && func->caller_func)
2775 {
2776 *filename_ptr = func->caller_file;
2777 *functionname_ptr = func->caller_func->name;
2778 *linenumber_ptr = func->caller_line;
2779 stash->inliner_chain = func->caller_func;
2780 return (TRUE);
2781 }
2782 }
2783
2784 return (FALSE);
2785}
2786
35330cce
NC
2787void
2788_bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2789{
2790 struct comp_unit *each;
2791 struct dwarf2_debug *stash;
2792
2793 if (abfd == NULL || elf_tdata (abfd) == NULL)
2794 return;
2795
2796 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2797
2798 if (stash == NULL)
2799 return;
2800
2801 for (each = stash->all_comp_units; each; each = each->next_unit)
2802 {
2803 struct abbrev_info **abbrevs = each->abbrevs;
2804 size_t i;
2805
2806 for (i = 0; i < ABBREV_HASH_SIZE; i++)
d8d1c398
AM
2807 {
2808 struct abbrev_info *abbrev = abbrevs[i];
35330cce 2809
d8d1c398
AM
2810 while (abbrev)
2811 {
2812 free (abbrev->attrs);
2813 abbrev = abbrev->next;
2814 }
2815 }
35330cce
NC
2816
2817 if (each->line_table)
d8d1c398
AM
2818 {
2819 free (each->line_table->dirs);
2820 free (each->line_table->files);
2821 }
35330cce
NC
2822 }
2823
2824 free (stash->dwarf_abbrev_buffer);
2825 free (stash->dwarf_line_buffer);
2826 free (stash->dwarf_ranges_buffer);
2827}