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