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