* configure, config.in: Rebuild.
[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,
94df17f2 3 2004, 2005, 2006, 2007, 2008, 2009, 2010 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"
fa8f86ff 38#include "dwarf2.h"
252b5132
RH
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 47 unsigned char minimum_instruction_length;
a233b20c 48 unsigned char maximum_ops_per_insn;
a092b084
NC
49 unsigned char default_is_stmt;
50 int line_base;
51 unsigned char line_range;
52 unsigned char opcode_base;
53 unsigned char *standard_opcode_lengths;
54};
55
56/* Attributes have a name and a value. */
57
252b5132 58struct attribute
a092b084
NC
59{
60 enum dwarf_attribute name;
61 enum dwarf_form form;
62 union
252b5132 63 {
a092b084
NC
64 char *str;
65 struct dwarf_block *blk;
8ce8c090
AM
66 bfd_uint64_t val;
67 bfd_int64_t sval;
a092b084
NC
68 }
69 u;
70};
71
98591c73 72/* Blocks are a bunch of untyped bytes. */
252b5132 73struct dwarf_block
a092b084
NC
74{
75 unsigned int size;
f075ee0c 76 bfd_byte *data;
a092b084 77};
252b5132 78
5609a71e 79struct adjusted_section
d4c32a81
L
80{
81 asection *section;
82 bfd_vma adj_vma;
83};
84
a092b084
NC
85struct dwarf2_debug
86{
87 /* A list of all previously read comp_units. */
f075ee0c 88 struct comp_unit *all_comp_units;
252b5132 89
bd210d54
NC
90 /* Last comp unit in list above. */
91 struct comp_unit *last_comp_unit;
92
252b5132
RH
93 /* The next unread compilation unit within the .debug_info section.
94 Zero indicates that the .debug_info section has not been loaded
a092b084 95 into a buffer yet. */
f075ee0c 96 bfd_byte *info_ptr;
252b5132 97
a092b084 98 /* Pointer to the end of the .debug_info section memory buffer. */
f075ee0c 99 bfd_byte *info_ptr_end;
252b5132 100
0d161102
NC
101 /* Pointer to the bfd, section and address of the beginning of the
102 section. The bfd might be different than expected because of
103 gnu_debuglink sections. */
a50b1753 104 bfd *bfd_ptr;
f075ee0c
AM
105 asection *sec;
106 bfd_byte *sec_info_ptr;
f2363ce5 107
aaf30c25
CS
108 /* A pointer to the memory block allocated for info_ptr. Neither
109 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
110 beginning of the malloc block. This is used only to free the
111 memory later. */
112 bfd_byte *info_ptr_memory;
113
f2363ce5 114 /* Pointer to the symbol table. */
f075ee0c 115 asymbol **syms;
f2363ce5 116
a092b084 117 /* Pointer to the .debug_abbrev section loaded into memory. */
f075ee0c 118 bfd_byte *dwarf_abbrev_buffer;
252b5132 119
a092b084 120 /* Length of the loaded .debug_abbrev section. */
3076cd1f 121 bfd_size_type dwarf_abbrev_size;
69dd2e2d
RH
122
123 /* Buffer for decode_line_info. */
f075ee0c 124 bfd_byte *dwarf_line_buffer;
ccdb16fc
JW
125
126 /* Length of the loaded .debug_line section. */
3076cd1f 127 bfd_size_type dwarf_line_size;
d03ba2a1
JJ
128
129 /* Pointer to the .debug_str section loaded into memory. */
f075ee0c 130 bfd_byte *dwarf_str_buffer;
d03ba2a1
JJ
131
132 /* Length of the loaded .debug_str section. */
3076cd1f 133 bfd_size_type dwarf_str_size;
a13afe8e
FF
134
135 /* Pointer to the .debug_ranges section loaded into memory. */
136 bfd_byte *dwarf_ranges_buffer;
137
138 /* Length of the loaded .debug_ranges section. */
3076cd1f 139 bfd_size_type dwarf_ranges_size;
4ab527b0
FF
140
141 /* If the most recent call to bfd_find_nearest_line was given an
142 address in an inlined function, preserve a pointer into the
143 calling chain for subsequent calls to bfd_find_inliner_info to
144 use. */
145 struct funcinfo *inliner_chain;
d4c32a81 146
5609a71e
DJ
147 /* Number of sections whose VMA we must adjust. */
148 unsigned int adjusted_section_count;
d4c32a81 149
5609a71e
DJ
150 /* Array of sections with adjusted VMA. */
151 struct adjusted_section *adjusted_sections;
bd210d54
NC
152
153 /* Number of times find_line is called. This is used in
154 the heuristic for enabling the info hash tables. */
155 int info_hash_count;
156
157#define STASH_INFO_HASH_TRIGGER 100
158
159 /* Hash table mapping symbol names to function infos. */
160 struct info_hash_table *funcinfo_hash_table;
161
162 /* Hash table mapping symbol names to variable infos. */
163 struct info_hash_table *varinfo_hash_table;
164
165 /* Head of comp_unit list in the last hash table update. */
166 struct comp_unit *hash_units_head;
167
168 /* Status of info hash. */
169 int info_hash_status;
170#define STASH_INFO_HASH_OFF 0
171#define STASH_INFO_HASH_ON 1
172#define STASH_INFO_HASH_DISABLED 2
252b5132
RH
173};
174
a092b084
NC
175struct arange
176{
f623be2b
RH
177 struct arange *next;
178 bfd_vma low;
179 bfd_vma high;
180};
252b5132 181
252b5132 182/* A minimal decoding of DWARF2 compilation units. We only decode
a092b084 183 what's needed to get to the line number information. */
252b5132 184
a092b084
NC
185struct comp_unit
186{
187 /* Chain the previously read compilation units. */
f075ee0c 188 struct comp_unit *next_unit;
252b5132 189
bd210d54
NC
190 /* Likewise, chain the compilation unit read after this one.
191 The comp units are stored in reversed reading order. */
192 struct comp_unit *prev_unit;
193
2ae727ad 194 /* Keep the bfd convenient (for memory allocation). */
f075ee0c 195 bfd *abfd;
252b5132 196
709d67f1
AM
197 /* The lowest and highest addresses contained in this compilation
198 unit as specified in the compilation unit header. */
199 struct arange arange;
252b5132 200
a092b084 201 /* The DW_AT_name attribute (for error messages). */
f075ee0c 202 char *name;
252b5132 203
a092b084 204 /* The abbrev hash table. */
f075ee0c 205 struct abbrev_info **abbrevs;
252b5132 206
a092b084 207 /* Note that an error was found by comp_unit_find_nearest_line. */
252b5132
RH
208 int error;
209
a092b084 210 /* The DW_AT_comp_dir attribute. */
f075ee0c 211 char *comp_dir;
252b5132 212
b34976b6 213 /* TRUE if there is a line number table associated with this comp. unit. */
252b5132 214 int stmtlist;
98591c73 215
c0c28ab8
L
216 /* Pointer to the current comp_unit so that we can find a given entry
217 by its reference. */
f075ee0c 218 bfd_byte *info_ptr_unit;
c0c28ab8 219
a358ecb8
AM
220 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
221 bfd_byte *sec_info_ptr;
222
a092b084 223 /* The offset into .debug_line of the line number table. */
252b5132
RH
224 unsigned long line_offset;
225
a092b084 226 /* Pointer to the first child die for the comp unit. */
f075ee0c 227 bfd_byte *first_child_die_ptr;
252b5132 228
a092b084 229 /* The end of the comp unit. */
f075ee0c 230 bfd_byte *end_ptr;
252b5132 231
a092b084 232 /* The decoded line number, NULL if not yet decoded. */
f075ee0c 233 struct line_info_table *line_table;
252b5132 234
a092b084 235 /* A list of the functions found in this comp. unit. */
f075ee0c 236 struct funcinfo *function_table;
252b5132 237
5420f73d
L
238 /* A list of the variables found in this comp. unit. */
239 struct varinfo *variable_table;
240
d03ba2a1
JJ
241 /* Pointer to dwarf2_debug structure. */
242 struct dwarf2_debug *stash;
243
5609a71e
DJ
244 /* DWARF format version for this unit - from unit header. */
245 int version;
246
a092b084 247 /* Address size for this unit - from unit header. */
252b5132 248 unsigned char addr_size;
d03ba2a1
JJ
249
250 /* Offset size for this unit - from unit header. */
251 unsigned char offset_size;
a13afe8e
FF
252
253 /* Base address for this unit - from DW_AT_low_pc attribute of
254 DW_TAG_compile_unit DIE */
255 bfd_vma base_address;
bd210d54
NC
256
257 /* TRUE if symbols are cached in hash table for faster lookup by name. */
258 bfd_boolean cached;
252b5132
RH
259};
260
a7b97311
AM
261/* This data structure holds the information of an abbrev. */
262struct abbrev_info
263{
264 unsigned int number; /* Number identifying abbrev. */
265 enum dwarf_tag tag; /* DWARF tag. */
266 int has_children; /* Boolean. */
267 unsigned int num_attrs; /* Number of attributes. */
268 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
269 struct abbrev_info *next; /* Next in chain. */
270};
271
272struct attr_abbrev
273{
274 enum dwarf_attribute name;
275 enum dwarf_form form;
276};
277
278#ifndef ABBREV_HASH_SIZE
279#define ABBREV_HASH_SIZE 121
280#endif
281#ifndef ATTR_ALLOC_CHUNK
282#define ATTR_ALLOC_CHUNK 4
283#endif
284
bd210d54
NC
285/* Variable and function hash tables. This is used to speed up look-up
286 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
287 In order to share code between variable and function infos, we use
288 a list of untyped pointer for all variable/function info associated with
289 a symbol. We waste a bit of memory for list with one node but that
290 simplifies the code. */
291
292struct info_list_node
293{
294 struct info_list_node *next;
295 void *info;
296};
297
298/* Info hash entry. */
299struct info_hash_entry
300{
301 struct bfd_hash_entry root;
302 struct info_list_node *head;
303};
304
305struct info_hash_table
306{
307 struct bfd_hash_table base;
308};
309
310/* Function to create a new entry in info hash table. */
311
312static struct bfd_hash_entry *
313info_hash_table_newfunc (struct bfd_hash_entry *entry,
314 struct bfd_hash_table *table,
315 const char *string)
316{
317 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
318
319 /* Allocate the structure if it has not already been allocated by a
320 derived class. */
321 if (ret == NULL)
322 {
a50b1753
NC
323 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
324 sizeof (* ret));
bd210d54
NC
325 if (ret == NULL)
326 return NULL;
327 }
328
329 /* Call the allocation method of the base class. */
330 ret = ((struct info_hash_entry *)
2d47a72c 331 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
bd210d54
NC
332
333 /* Initialize the local fields here. */
334 if (ret)
335 ret->head = NULL;
336
337 return (struct bfd_hash_entry *) ret;
338}
339
340/* Function to create a new info hash table. It returns a pointer to the
341 newly created table or NULL if there is any error. We need abfd
342 solely for memory allocation. */
343
344static struct info_hash_table *
345create_info_hash_table (bfd *abfd)
346{
347 struct info_hash_table *hash_table;
348
a50b1753
NC
349 hash_table = (struct info_hash_table *)
350 bfd_alloc (abfd, sizeof (struct info_hash_table));
bd210d54
NC
351 if (!hash_table)
352 return hash_table;
353
354 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
355 sizeof (struct info_hash_entry)))
356 {
357 bfd_release (abfd, hash_table);
358 return NULL;
359 }
360
361 return hash_table;
362}
363
364/* Insert an info entry into an info hash table. We do not check of
365 duplicate entries. Also, the caller need to guarantee that the
366 right type of info in inserted as info is passed as a void* pointer.
367 This function returns true if there is no error. */
368
369static bfd_boolean
370insert_info_hash_table (struct info_hash_table *hash_table,
371 const char *key,
372 void *info,
373 bfd_boolean copy_p)
374{
375 struct info_hash_entry *entry;
376 struct info_list_node *node;
377
378 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
379 key, TRUE, copy_p);
380 if (!entry)
381 return FALSE;
382
a50b1753
NC
383 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
384 sizeof (*node));
bd210d54
NC
385 if (!node)
386 return FALSE;
387
388 node->info = info;
389 node->next = entry->head;
390 entry->head = node;
391
392 return TRUE;
393}
394
395/* Look up an info entry list from an info hash table. Return NULL
396 if there is none. */
397
398static struct info_list_node *
399lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
400{
401 struct info_hash_entry *entry;
402
403 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
404 FALSE, FALSE);
405 return entry ? entry->head : NULL;
406}
407
0acf065b
CC
408/* Read a section, uncompress it if necessary, and relocate it. */
409
410static bfd_boolean
411read_and_uncompress_section (bfd * abfd,
412 asection * msec,
413 bfd_boolean section_is_compressed,
414 asymbol ** syms,
415 bfd_byte ** section_buffer,
416 bfd_size_type * section_size)
417{
418 /* Get the unrelocated contents of the section. */
419 *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
420 if (! *section_buffer)
421 return FALSE;
422 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
423 0, *section_size))
424 return FALSE;
425
426 if (section_is_compressed)
427 {
428 if (! bfd_uncompress_section_contents (section_buffer, section_size))
429 {
430 (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."),
431 bfd_get_section_name (abfd, msec));
432 bfd_set_error (bfd_error_bad_value);
433 return FALSE;
434 }
435 }
436
437 if (syms)
438 {
439 /* We want to relocate the data we've already read (and
440 decompressed), so we store a pointer to the data in
441 the bfd_section, and tell it that the contents are
442 already in memory. */
443 BFD_ASSERT (msec->contents == NULL && (msec->flags & SEC_IN_MEMORY) == 0);
444 msec->contents = *section_buffer;
445 msec->flags |= SEC_IN_MEMORY;
446 msec->size = *section_size;
447 *section_buffer
448 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
449 if (! *section_buffer)
450 return FALSE;
451 }
452
453 return TRUE;
454}
455
1b315056 456/* Read a section into its appropriate place in the dwarf2_debug
dc80fd5c 457 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
1b315056 458 not NULL, use bfd_simple_get_relocated_section_contents to read the
dc80fd5c
NC
459 section contents, otherwise use bfd_get_section_contents. Fail if
460 the located section does not contain at least OFFSET bytes. */
1b315056
CS
461
462static bfd_boolean
dc80fd5c
NC
463read_section (bfd * abfd,
464 const char * section_name,
465 const char * compressed_section_name,
466 asymbol ** syms,
467 bfd_uint64_t offset,
468 bfd_byte ** section_buffer,
469 bfd_size_type * section_size)
1b315056
CS
470{
471 asection *msec;
472 bfd_boolean section_is_compressed = FALSE;
473
474 /* read_section is a noop if the section has already been read. */
53638231 475 if (!*section_buffer)
1b315056 476 {
53638231
AS
477 msec = bfd_get_section_by_name (abfd, section_name);
478 if (! msec && compressed_section_name)
479 {
480 msec = bfd_get_section_by_name (abfd, compressed_section_name);
481 section_is_compressed = TRUE;
482 }
483 if (! msec)
2d47a72c 484 {
53638231 485 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
2d47a72c
DJ
486 bfd_set_error (bfd_error_bad_value);
487 return FALSE;
488 }
53638231 489
bc664799 490 *section_size = msec->rawsize ? msec->rawsize : msec->size;
53638231 491
0acf065b
CC
492 if (! read_and_uncompress_section (abfd, msec, section_is_compressed,
493 syms, section_buffer, section_size))
494 return FALSE;
1b315056
CS
495 }
496
497 /* It is possible to get a bad value for the offset into the section
dc80fd5c 498 that the client wants. Validate it here to avoid trouble later. */
1b315056
CS
499 if (offset != 0 && offset >= *section_size)
500 {
501 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
dc80fd5c 502 (long) offset, section_name, *section_size);
1b315056
CS
503 bfd_set_error (bfd_error_bad_value);
504 return FALSE;
505 }
506
507 return TRUE;
508}
509
98591c73
KH
510/* VERBATIM
511 The following function up to the END VERBATIM mark are
a092b084 512 copied directly from dwarf2read.c. */
252b5132 513
a092b084 514/* Read dwarf information from a buffer. */
252b5132
RH
515
516static unsigned int
f075ee0c 517read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 518{
818a27ac 519 return bfd_get_8 (abfd, buf);
252b5132
RH
520}
521
522static int
f075ee0c 523read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
252b5132 524{
818a27ac 525 return bfd_get_signed_8 (abfd, buf);
252b5132
RH
526}
527
528static unsigned int
f075ee0c 529read_2_bytes (bfd *abfd, bfd_byte *buf)
252b5132 530{
818a27ac 531 return bfd_get_16 (abfd, buf);
252b5132
RH
532}
533
252b5132 534static unsigned int
f075ee0c 535read_4_bytes (bfd *abfd, bfd_byte *buf)
252b5132 536{
818a27ac 537 return bfd_get_32 (abfd, buf);
252b5132
RH
538}
539
8ce8c090 540static bfd_uint64_t
f075ee0c 541read_8_bytes (bfd *abfd, bfd_byte *buf)
252b5132 542{
818a27ac 543 return bfd_get_64 (abfd, buf);
252b5132
RH
544}
545
f075ee0c 546static bfd_byte *
818a27ac 547read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 548 bfd_byte *buf,
818a27ac 549 unsigned int size ATTRIBUTE_UNUSED)
252b5132 550{
252b5132
RH
551 return buf;
552}
553
554static char *
818a27ac 555read_string (bfd *abfd ATTRIBUTE_UNUSED,
f075ee0c 556 bfd_byte *buf,
818a27ac 557 unsigned int *bytes_read_ptr)
252b5132 558{
d03ba2a1 559 /* Return a pointer to the embedded string. */
f075ee0c 560 char *str = (char *) buf;
dc80fd5c 561
f075ee0c 562 if (*str == '\0')
252b5132
RH
563 {
564 *bytes_read_ptr = 1;
565 return NULL;
566 }
98591c73 567
f075ee0c
AM
568 *bytes_read_ptr = strlen (str) + 1;
569 return str;
252b5132
RH
570}
571
dc80fd5c
NC
572/* END VERBATIM */
573
d03ba2a1 574static char *
dc80fd5c
NC
575read_indirect_string (struct comp_unit * unit,
576 bfd_byte * buf,
577 unsigned int * bytes_read_ptr)
d03ba2a1 578{
8ce8c090 579 bfd_uint64_t offset;
d03ba2a1 580 struct dwarf2_debug *stash = unit->stash;
f075ee0c 581 char *str;
d03ba2a1
JJ
582
583 if (unit->offset_size == 4)
584 offset = read_4_bytes (unit->abfd, buf);
585 else
586 offset = read_8_bytes (unit->abfd, buf);
dc80fd5c 587
d03ba2a1
JJ
588 *bytes_read_ptr = unit->offset_size;
589
1b315056 590 if (! read_section (unit->abfd, ".debug_str", ".zdebug_str",
dc80fd5c 591 stash->syms, offset,
9e32b19f 592 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
dc80fd5c 593 return NULL;
d03ba2a1 594
f075ee0c
AM
595 str = (char *) stash->dwarf_str_buffer + offset;
596 if (*str == '\0')
d03ba2a1 597 return NULL;
f075ee0c 598 return str;
d03ba2a1
JJ
599}
600
8ce8c090 601static bfd_uint64_t
f075ee0c 602read_address (struct comp_unit *unit, bfd_byte *buf)
252b5132 603{
0af4cd7c
PK
604 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
605
606 if (signed_vma)
607 {
608 switch (unit->addr_size)
609 {
610 case 8:
611 return bfd_get_signed_64 (unit->abfd, buf);
612 case 4:
613 return bfd_get_signed_32 (unit->abfd, buf);
614 case 2:
615 return bfd_get_signed_16 (unit->abfd, buf);
616 default:
617 abort ();
618 }
619 }
620 else
252b5132 621 {
0af4cd7c
PK
622 switch (unit->addr_size)
623 {
624 case 8:
625 return bfd_get_64 (unit->abfd, buf);
626 case 4:
627 return bfd_get_32 (unit->abfd, buf);
628 case 2:
629 return bfd_get_16 (unit->abfd, buf);
630 default:
631 abort ();
632 }
252b5132 633 }
252b5132
RH
634}
635
252b5132
RH
636/* Lookup an abbrev_info structure in the abbrev hash table. */
637
638static struct abbrev_info *
818a27ac 639lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
252b5132
RH
640{
641 unsigned int hash_number;
642 struct abbrev_info *abbrev;
643
644 hash_number = number % ABBREV_HASH_SIZE;
645 abbrev = abbrevs[hash_number];
646
647 while (abbrev)
648 {
649 if (abbrev->number == number)
650 return abbrev;
651 else
652 abbrev = abbrev->next;
653 }
98591c73 654
252b5132
RH
655 return NULL;
656}
657
658/* In DWARF version 2, the description of the debugging information is
659 stored in a separate .debug_abbrev section. Before we read any
660 dies from a section we read in all abbreviations and install them
661 in a hash table. */
662
663static struct abbrev_info**
8ce8c090 664read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
252b5132
RH
665{
666 struct abbrev_info **abbrevs;
f075ee0c 667 bfd_byte *abbrev_ptr;
252b5132
RH
668 struct abbrev_info *cur_abbrev;
669 unsigned int abbrev_number, bytes_read, abbrev_name;
670 unsigned int abbrev_form, hash_number;
dc810e39 671 bfd_size_type amt;
252b5132 672
1b315056 673 if (! read_section (abfd, ".debug_abbrev", ".zdebug_abbrev",
9e32b19f
DJ
674 stash->syms, offset,
675 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
8af6b354 676 return NULL;
252b5132 677
dc810e39 678 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
a50b1753 679 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
8af6b354
AM
680 if (abbrevs == NULL)
681 return NULL;
252b5132
RH
682
683 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
684 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
685 abbrev_ptr += bytes_read;
686
a092b084 687 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
688 while (abbrev_number)
689 {
dc810e39 690 amt = sizeof (struct abbrev_info);
a50b1753 691 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
8af6b354
AM
692 if (cur_abbrev == NULL)
693 return NULL;
252b5132 694
a092b084 695 /* Read in abbrev header. */
252b5132 696 cur_abbrev->number = abbrev_number;
d45913a0
DA
697 cur_abbrev->tag = (enum dwarf_tag)
698 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
252b5132
RH
699 abbrev_ptr += bytes_read;
700 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
701 abbrev_ptr += 1;
702
a092b084 703 /* Now read in declarations. */
252b5132
RH
704 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
705 abbrev_ptr += bytes_read;
706 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
707 abbrev_ptr += bytes_read;
98591c73 708
252b5132
RH
709 while (abbrev_name)
710 {
711 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
712 {
35330cce
NC
713 struct attr_abbrev *tmp;
714
dc810e39
AM
715 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
716 amt *= sizeof (struct attr_abbrev);
a50b1753 717 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
35330cce 718 if (tmp == NULL)
d8d1c398
AM
719 {
720 size_t i;
721
722 for (i = 0; i < ABBREV_HASH_SIZE; i++)
723 {
724 struct abbrev_info *abbrev = abbrevs[i];
725
726 while (abbrev)
727 {
34b5e0b2
NC
728 free (abbrev->attrs);
729 abbrev = abbrev->next;
d8d1c398
AM
730 }
731 }
732 return NULL;
733 }
35330cce 734 cur_abbrev->attrs = tmp;
252b5132 735 }
98591c73 736
d45913a0
DA
737 cur_abbrev->attrs[cur_abbrev->num_attrs].name
738 = (enum dwarf_attribute) abbrev_name;
739 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
740 = (enum dwarf_form) abbrev_form;
252b5132
RH
741 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
742 abbrev_ptr += bytes_read;
743 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
744 abbrev_ptr += bytes_read;
745 }
746
747 hash_number = abbrev_number % ABBREV_HASH_SIZE;
748 cur_abbrev->next = abbrevs[hash_number];
749 abbrevs[hash_number] = cur_abbrev;
750
751 /* Get next abbreviation.
e82ce529 752 Under Irix6 the abbreviations for a compilation unit are not
252b5132
RH
753 always properly terminated with an abbrev number of 0.
754 Exit loop if we encounter an abbreviation which we have
755 already read (which means we are about to read the abbreviations
756 for the next compile unit) or if the end of the abbreviation
757 table is reached. */
758 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
d8d1c398 759 >= stash->dwarf_abbrev_size)
252b5132
RH
760 break;
761 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
762 abbrev_ptr += bytes_read;
763 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
764 break;
765 }
766
767 return abbrevs;
768}
769
cf716c56 770/* Read an attribute value described by an attribute form. */
252b5132 771
f075ee0c 772static bfd_byte *
818a27ac
AM
773read_attribute_value (struct attribute *attr,
774 unsigned form,
775 struct comp_unit *unit,
f075ee0c 776 bfd_byte *info_ptr)
252b5132
RH
777{
778 bfd *abfd = unit->abfd;
779 unsigned int bytes_read;
780 struct dwarf_block *blk;
dc810e39 781 bfd_size_type amt;
252b5132 782
d45913a0 783 attr->form = (enum dwarf_form) form;
98591c73 784
cf716c56 785 switch (form)
252b5132 786 {
252b5132 787 case DW_FORM_ref_addr:
5609a71e
DJ
788 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
789 DWARF3. */
c07cbdd7 790 if (unit->version == 3 || unit->version == 4)
5609a71e
DJ
791 {
792 if (unit->offset_size == 4)
793 attr->u.val = read_4_bytes (unit->abfd, info_ptr);
794 else
795 attr->u.val = read_8_bytes (unit->abfd, info_ptr);
796 info_ptr += unit->offset_size;
797 break;
798 }
799 /* FALLTHROUGH */
800 case DW_FORM_addr:
482e2e37 801 attr->u.val = read_address (unit, info_ptr);
252b5132
RH
802 info_ptr += unit->addr_size;
803 break;
c07cbdd7
JJ
804 case DW_FORM_sec_offset:
805 if (unit->offset_size == 4)
806 attr->u.val = read_4_bytes (unit->abfd, info_ptr);
807 else
808 attr->u.val = read_8_bytes (unit->abfd, info_ptr);
809 info_ptr += unit->offset_size;
810 break;
252b5132 811 case DW_FORM_block2:
dc810e39 812 amt = sizeof (struct dwarf_block);
a50b1753 813 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
8af6b354
AM
814 if (blk == NULL)
815 return NULL;
252b5132
RH
816 blk->size = read_2_bytes (abfd, info_ptr);
817 info_ptr += 2;
818 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
819 info_ptr += blk->size;
482e2e37 820 attr->u.blk = blk;
252b5132
RH
821 break;
822 case DW_FORM_block4:
dc810e39 823 amt = sizeof (struct dwarf_block);
a50b1753 824 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
8af6b354
AM
825 if (blk == NULL)
826 return NULL;
252b5132
RH
827 blk->size = read_4_bytes (abfd, info_ptr);
828 info_ptr += 4;
829 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
830 info_ptr += blk->size;
482e2e37 831 attr->u.blk = blk;
252b5132
RH
832 break;
833 case DW_FORM_data2:
482e2e37 834 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
835 info_ptr += 2;
836 break;
837 case DW_FORM_data4:
482e2e37 838 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
839 info_ptr += 4;
840 break;
841 case DW_FORM_data8:
482e2e37 842 attr->u.val = read_8_bytes (abfd, info_ptr);
252b5132
RH
843 info_ptr += 8;
844 break;
845 case DW_FORM_string:
482e2e37 846 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
252b5132
RH
847 info_ptr += bytes_read;
848 break;
d03ba2a1 849 case DW_FORM_strp:
482e2e37 850 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
d03ba2a1
JJ
851 info_ptr += bytes_read;
852 break;
c07cbdd7 853 case DW_FORM_exprloc:
252b5132 854 case DW_FORM_block:
dc810e39 855 amt = sizeof (struct dwarf_block);
a50b1753 856 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
8af6b354
AM
857 if (blk == NULL)
858 return NULL;
252b5132
RH
859 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
860 info_ptr += bytes_read;
861 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
862 info_ptr += blk->size;
482e2e37 863 attr->u.blk = blk;
252b5132
RH
864 break;
865 case DW_FORM_block1:
dc810e39 866 amt = sizeof (struct dwarf_block);
a50b1753 867 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
8af6b354
AM
868 if (blk == NULL)
869 return NULL;
252b5132
RH
870 blk->size = read_1_byte (abfd, info_ptr);
871 info_ptr += 1;
872 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
873 info_ptr += blk->size;
482e2e37 874 attr->u.blk = blk;
252b5132
RH
875 break;
876 case DW_FORM_data1:
482e2e37 877 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
878 info_ptr += 1;
879 break;
880 case DW_FORM_flag:
482e2e37 881 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
882 info_ptr += 1;
883 break;
c07cbdd7
JJ
884 case DW_FORM_flag_present:
885 attr->u.val = 1;
886 break;
252b5132 887 case DW_FORM_sdata:
482e2e37 888 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
889 info_ptr += bytes_read;
890 break;
891 case DW_FORM_udata:
482e2e37 892 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
893 info_ptr += bytes_read;
894 break;
895 case DW_FORM_ref1:
482e2e37 896 attr->u.val = read_1_byte (abfd, info_ptr);
252b5132
RH
897 info_ptr += 1;
898 break;
899 case DW_FORM_ref2:
482e2e37 900 attr->u.val = read_2_bytes (abfd, info_ptr);
252b5132
RH
901 info_ptr += 2;
902 break;
903 case DW_FORM_ref4:
482e2e37 904 attr->u.val = read_4_bytes (abfd, info_ptr);
252b5132
RH
905 info_ptr += 4;
906 break;
81edd86d 907 case DW_FORM_ref8:
482e2e37 908 attr->u.val = read_8_bytes (abfd, info_ptr);
81edd86d
MM
909 info_ptr += 8;
910 break;
a37a68dd
CC
911 case DW_FORM_ref_sig8:
912 attr->u.val = read_8_bytes (abfd, info_ptr);
913 info_ptr += 8;
914 break;
252b5132 915 case DW_FORM_ref_udata:
482e2e37 916 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
252b5132
RH
917 info_ptr += bytes_read;
918 break;
252b5132 919 case DW_FORM_indirect:
cf716c56
RH
920 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
921 info_ptr += bytes_read;
922 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
923 break;
252b5132 924 default:
f46c2da6 925 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
cf716c56 926 form);
252b5132 927 bfd_set_error (bfd_error_bad_value);
c07cbdd7 928 return NULL;
252b5132
RH
929 }
930 return info_ptr;
931}
932
cf716c56
RH
933/* Read an attribute described by an abbreviated attribute. */
934
f075ee0c 935static bfd_byte *
818a27ac
AM
936read_attribute (struct attribute *attr,
937 struct attr_abbrev *abbrev,
938 struct comp_unit *unit,
f075ee0c 939 bfd_byte *info_ptr)
cf716c56
RH
940{
941 attr->name = abbrev->name;
942 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
943 return info_ptr;
944}
945
a092b084 946/* Source line information table routines. */
252b5132
RH
947
948#define FILE_ALLOC_CHUNK 5
949#define DIR_ALLOC_CHUNK 5
950
a092b084
NC
951struct line_info
952{
252b5132 953 struct line_info* prev_line;
252b5132 954 bfd_vma address;
f075ee0c 955 char *filename;
252b5132
RH
956 unsigned int line;
957 unsigned int column;
a233b20c
JJ
958 unsigned char op_index;
959 unsigned char end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
960};
961
a092b084
NC
962struct fileinfo
963{
252b5132
RH
964 char *name;
965 unsigned int dir;
966 unsigned int time;
967 unsigned int size;
968};
969
0ee19663
NC
970struct line_sequence
971{
972 bfd_vma low_pc;
973 struct line_sequence* prev_sequence;
974 struct line_info* last_line; /* Largest VMA. */
975};
976
a092b084
NC
977struct line_info_table
978{
0ee19663
NC
979 bfd* abfd;
980 unsigned int num_files;
981 unsigned int num_dirs;
982 unsigned int num_sequences;
983 char * comp_dir;
984 char ** dirs;
985 struct fileinfo* files;
986 struct line_sequence* sequences;
987 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
252b5132
RH
988};
989
4ab527b0
FF
990/* Remember some information about each function. If the function is
991 inlined (DW_TAG_inlined_subroutine) it may have two additional
992 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
993 source code location where this function was inlined. */
994
1ee24f27
DJ
995struct funcinfo
996{
709d67f1
AM
997 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
998 struct funcinfo *caller_func; /* Pointer to function one scope higher */
999 char *caller_file; /* Source location file name where caller_func inlines this func */
1000 int caller_line; /* Source location line number where caller_func inlines this func */
1001 char *file; /* Source location file name */
1002 int line; /* Source location line number */
4ab527b0 1003 int tag;
f075ee0c 1004 char *name;
a13afe8e 1005 struct arange arange;
709d67f1 1006 asection *sec; /* Where the symbol is defined */
5420f73d
L
1007};
1008
1009struct varinfo
1010{
709d67f1 1011 /* Pointer to previous variable in list of all variables */
5420f73d 1012 struct varinfo *prev_var;
709d67f1 1013 /* Source location file name */
5420f73d 1014 char *file;
709d67f1 1015 /* Source location line number */
5420f73d
L
1016 int line;
1017 int tag;
1018 char *name;
5cf2e3f0 1019 bfd_vma addr;
709d67f1 1020 /* Where the symbol is defined */
5420f73d 1021 asection *sec;
709d67f1 1022 /* Is this a stack variable? */
5420f73d 1023 unsigned int stack: 1;
1ee24f27
DJ
1024};
1025
d4c32a81
L
1026/* Return TRUE if NEW_LINE should sort after LINE. */
1027
1028static inline bfd_boolean
1029new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1030{
1031 return (new_line->address > line->address
1032 || (new_line->address == line->address
a233b20c
JJ
1033 && (new_line->op_index > line->op_index
1034 || (new_line->op_index == line->op_index
1035 && new_line->end_sequence < line->end_sequence))));
d4c32a81
L
1036}
1037
1038
af3ef9fe
NC
1039/* Adds a new entry to the line_info list in the line_info_table, ensuring
1040 that the list is sorted. Note that the line_info list is sorted from
1041 highest to lowest VMA (with possible duplicates); that is,
1042 line_info->prev_line always accesses an equal or smaller VMA. */
1043
8af6b354 1044static bfd_boolean
818a27ac
AM
1045add_line_info (struct line_info_table *table,
1046 bfd_vma address,
a233b20c 1047 unsigned char op_index,
818a27ac
AM
1048 char *filename,
1049 unsigned int line,
1050 unsigned int column,
1051 int end_sequence)
252b5132 1052{
dc810e39 1053 bfd_size_type amt = sizeof (struct line_info);
0ee19663 1054 struct line_sequence* seq = table->sequences;
a50b1753 1055 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
252b5132 1056
8af6b354
AM
1057 if (info == NULL)
1058 return FALSE;
1059
d4c32a81
L
1060 /* Set member data of 'info'. */
1061 info->address = address;
a233b20c 1062 info->op_index = op_index;
d4c32a81
L
1063 info->line = line;
1064 info->column = column;
1065 info->end_sequence = end_sequence;
1066
1067 if (filename && filename[0])
1068 {
a50b1753 1069 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
8af6b354
AM
1070 if (info->filename == NULL)
1071 return FALSE;
1072 strcpy (info->filename, filename);
d4c32a81
L
1073 }
1074 else
1075 info->filename = NULL;
1076
e82ce529
AM
1077 /* Find the correct location for 'info'. Normally we will receive
1078 new line_info data 1) in order and 2) with increasing VMAs.
1079 However some compilers break the rules (cf. decode_line_info) and
1080 so we include some heuristics for quickly finding the correct
1081 location for 'info'. In particular, these heuristics optimize for
1082 the common case in which the VMA sequence that we receive is a
1083 list of locally sorted VMAs such as
1084 p...z a...j (where a < j < p < z)
252b5132 1085
e82ce529 1086 Note: table->lcl_head is used to head an *actual* or *possible*
0ee19663 1087 sub-sequence within the list (such as a...j) that is not directly
e82ce529
AM
1088 headed by table->last_line
1089
1090 Note: we may receive duplicate entries from 'decode_line_info'. */
1091
0ee19663
NC
1092 if (seq
1093 && seq->last_line->address == address
a233b20c 1094 && seq->last_line->op_index == op_index
0ee19663 1095 && seq->last_line->end_sequence == end_sequence)
aff90a5f
L
1096 {
1097 /* We only keep the last entry with the same address and end
1098 sequence. See PR ld/4986. */
0ee19663 1099 if (table->lcl_head == seq->last_line)
aff90a5f 1100 table->lcl_head = info;
0ee19663
NC
1101 info->prev_line = seq->last_line->prev_line;
1102 seq->last_line = info;
aff90a5f 1103 }
0ee19663 1104 else if (!seq || seq->last_line->end_sequence)
d8d1c398 1105 {
0ee19663
NC
1106 /* Start a new line sequence. */
1107 amt = sizeof (struct line_sequence);
1108 seq = (struct line_sequence *) bfd_malloc (amt);
8af6b354
AM
1109 if (seq == NULL)
1110 return FALSE;
0ee19663
NC
1111 seq->low_pc = address;
1112 seq->prev_sequence = table->sequences;
1113 seq->last_line = info;
1114 table->lcl_head = info;
1115 table->sequences = seq;
1116 table->num_sequences++;
1117 }
1118 else if (new_line_sorts_after (info, seq->last_line))
1119 {
1120 /* Normal case: add 'info' to the beginning of the current sequence. */
1121 info->prev_line = seq->last_line;
1122 seq->last_line = info;
e82ce529 1123
d8d1c398
AM
1124 /* lcl_head: initialize to head a *possible* sequence at the end. */
1125 if (!table->lcl_head)
1126 table->lcl_head = info;
1127 }
1128 else if (!new_line_sorts_after (info, table->lcl_head)
1129 && (!table->lcl_head->prev_line
1130 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1131 {
1132 /* Abnormal but easy: lcl_head is the head of 'info'. */
1133 info->prev_line = table->lcl_head->prev_line;
1134 table->lcl_head->prev_line = info;
1135 }
1136 else
1137 {
0ee19663
NC
1138 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1139 are valid heads for 'info'. Reset 'lcl_head'. */
1140 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
d8d1c398 1141 struct line_info* li1 = li2->prev_line;
e82ce529 1142
d8d1c398
AM
1143 while (li1)
1144 {
1145 if (!new_line_sorts_after (info, li2)
1146 && new_line_sorts_after (info, li1))
1147 break;
e82ce529 1148
709d67f1 1149 li2 = li1; /* always non-NULL */
d8d1c398
AM
1150 li1 = li1->prev_line;
1151 }
1152 table->lcl_head = li2;
1153 info->prev_line = table->lcl_head->prev_line;
1154 table->lcl_head->prev_line = info;
0ee19663
NC
1155 if (address < seq->low_pc)
1156 seq->low_pc = address;
d8d1c398 1157 }
8af6b354 1158 return TRUE;
252b5132
RH
1159}
1160
5ed6aba4 1161/* Extract a fully qualified filename from a line info table.
af3ef9fe
NC
1162 The returned string has been malloc'ed and it is the caller's
1163 responsibility to free it. */
5ed6aba4 1164
a092b084 1165static char *
818a27ac 1166concat_filename (struct line_info_table *table, unsigned int file)
252b5132 1167{
f075ee0c 1168 char *filename;
159002ff
RH
1169
1170 if (file - 1 >= table->num_files)
1171 {
75a657ba
L
1172 /* FILE == 0 means unknown. */
1173 if (file)
1174 (*_bfd_error_handler)
1175 (_("Dwarf Error: mangled line number section (bad file number)."));
af3ef9fe 1176 return strdup ("<unknown>");
159002ff
RH
1177 }
1178
1179 filename = table->files[file - 1].name;
5ed6aba4 1180
7421a730 1181 if (!IS_ABSOLUTE_PATH (filename))
252b5132 1182 {
608fa8d3
JB
1183 char *dir_name = NULL;
1184 char *subdir_name = NULL;
7421a730
AM
1185 char *name;
1186 size_t len;
0dafd5f6 1187
7421a730 1188 if (table->files[file - 1].dir)
608fa8d3 1189 subdir_name = table->dirs[table->files[file - 1].dir - 1];
7421a730 1190
608fa8d3
JB
1191 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1192 dir_name = table->comp_dir;
7421a730 1193
608fa8d3 1194 if (!dir_name)
af3ef9fe 1195 {
608fa8d3
JB
1196 dir_name = subdir_name;
1197 subdir_name = NULL;
7421a730 1198 }
af3ef9fe 1199
608fa8d3 1200 if (!dir_name)
7421a730
AM
1201 return strdup (filename);
1202
608fa8d3 1203 len = strlen (dir_name) + strlen (filename) + 2;
7421a730 1204
608fa8d3 1205 if (subdir_name)
7421a730 1206 {
608fa8d3 1207 len += strlen (subdir_name) + 1;
a50b1753 1208 name = (char *) bfd_malloc (len);
7421a730 1209 if (name)
608fa8d3 1210 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
7421a730
AM
1211 }
1212 else
1213 {
a50b1753 1214 name = (char *) bfd_malloc (len);
af3ef9fe 1215 if (name)
608fa8d3 1216 sprintf (name, "%s/%s", dir_name, filename);
af3ef9fe 1217 }
7421a730
AM
1218
1219 return name;
252b5132 1220 }
af3ef9fe
NC
1221
1222 return strdup (filename);
252b5132
RH
1223}
1224
8af6b354
AM
1225static bfd_boolean
1226arange_add (bfd *abfd, struct arange *first_arange,
1227 bfd_vma low_pc, bfd_vma high_pc)
f623be2b
RH
1228{
1229 struct arange *arange;
1230
709d67f1 1231 /* If the first arange is empty, use it. */
a13afe8e
FF
1232 if (first_arange->high == 0)
1233 {
1234 first_arange->low = low_pc;
1235 first_arange->high = high_pc;
8af6b354 1236 return TRUE;
a13afe8e 1237 }
98591c73 1238
a13afe8e
FF
1239 /* Next see if we can cheaply extend an existing range. */
1240 arange = first_arange;
f623be2b
RH
1241 do
1242 {
1243 if (low_pc == arange->high)
1244 {
1245 arange->high = high_pc;
8af6b354 1246 return TRUE;
f623be2b
RH
1247 }
1248 if (high_pc == arange->low)
1249 {
1250 arange->low = low_pc;
8af6b354 1251 return TRUE;
f623be2b
RH
1252 }
1253 arange = arange->next;
1254 }
1255 while (arange);
1256
a13afe8e 1257 /* Need to allocate a new arange and insert it into the arange list.
709d67f1 1258 Order isn't significant, so just insert after the first arange. */
a50b1753 1259 arange = (struct arange *) bfd_zalloc (abfd, sizeof (*arange));
8af6b354
AM
1260 if (arange == NULL)
1261 return FALSE;
f623be2b
RH
1262 arange->low = low_pc;
1263 arange->high = high_pc;
a13afe8e
FF
1264 arange->next = first_arange->next;
1265 first_arange->next = arange;
8af6b354 1266 return TRUE;
f623be2b
RH
1267}
1268
0ee19663
NC
1269/* Compare function for line sequences. */
1270
1271static int
1272compare_sequences (const void* a, const void* b)
1273{
1274 const struct line_sequence* seq1 = a;
1275 const struct line_sequence* seq2 = b;
1276
1277 /* Sort by low_pc as the primary key. */
1278 if (seq1->low_pc < seq2->low_pc)
1279 return -1;
1280 if (seq1->low_pc > seq2->low_pc)
1281 return 1;
1282
1283 /* If low_pc values are equal, sort in reverse order of
1284 high_pc, so that the largest region comes first. */
1285 if (seq1->last_line->address < seq2->last_line->address)
1286 return 1;
1287 if (seq1->last_line->address > seq2->last_line->address)
1288 return -1;
1289
a233b20c
JJ
1290 if (seq1->last_line->op_index < seq2->last_line->op_index)
1291 return 1;
1292 if (seq1->last_line->op_index > seq2->last_line->op_index)
1293 return -1;
1294
0ee19663
NC
1295 return 0;
1296}
1297
1298/* Sort the line sequences for quick lookup. */
1299
8af6b354 1300static bfd_boolean
0ee19663
NC
1301sort_line_sequences (struct line_info_table* table)
1302{
1303 bfd_size_type amt;
1304 struct line_sequence* sequences;
1305 struct line_sequence* seq;
1306 unsigned int n = 0;
1307 unsigned int num_sequences = table->num_sequences;
1308 bfd_vma last_high_pc;
1309
1310 if (num_sequences == 0)
8af6b354 1311 return TRUE;
0ee19663
NC
1312
1313 /* Allocate space for an array of sequences. */
1314 amt = sizeof (struct line_sequence) * num_sequences;
1315 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
8af6b354
AM
1316 if (sequences == NULL)
1317 return FALSE;
0ee19663
NC
1318
1319 /* Copy the linked list into the array, freeing the original nodes. */
1320 seq = table->sequences;
1321 for (n = 0; n < num_sequences; n++)
1322 {
1323 struct line_sequence* last_seq = seq;
1324
1325 BFD_ASSERT (seq);
1326 sequences[n].low_pc = seq->low_pc;
1327 sequences[n].prev_sequence = NULL;
1328 sequences[n].last_line = seq->last_line;
1329 seq = seq->prev_sequence;
1330 free (last_seq);
1331 }
1332 BFD_ASSERT (seq == NULL);
1333
1334 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1335
1336 /* Make the list binary-searchable by trimming overlapping entries
1337 and removing nested entries. */
1338 num_sequences = 1;
1339 last_high_pc = sequences[0].last_line->address;
1340 for (n = 1; n < table->num_sequences; n++)
1341 {
1342 if (sequences[n].low_pc < last_high_pc)
1343 {
1344 if (sequences[n].last_line->address <= last_high_pc)
1345 /* Skip nested entries. */
1346 continue;
1347
1348 /* Trim overlapping entries. */
1349 sequences[n].low_pc = last_high_pc;
1350 }
1351 last_high_pc = sequences[n].last_line->address;
1352 if (n > num_sequences)
1353 {
1354 /* Close up the gap. */
1355 sequences[num_sequences].low_pc = sequences[n].low_pc;
1356 sequences[num_sequences].last_line = sequences[n].last_line;
1357 }
1358 num_sequences++;
1359 }
1360
1361 table->sequences = sequences;
1362 table->num_sequences = num_sequences;
8af6b354 1363 return TRUE;
0ee19663
NC
1364}
1365
34b5e0b2 1366/* Decode the line number information for UNIT. */
252b5132 1367
34b5e0b2 1368static struct line_info_table*
818a27ac 1369decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
252b5132
RH
1370{
1371 bfd *abfd = unit->abfd;
252b5132 1372 struct line_info_table* table;
f075ee0c
AM
1373 bfd_byte *line_ptr;
1374 bfd_byte *line_end;
252b5132 1375 struct line_head lh;
d03ba2a1 1376 unsigned int i, bytes_read, offset_size;
252b5132
RH
1377 char *cur_file, *cur_dir;
1378 unsigned char op_code, extended_op, adj_opcode;
dc810e39 1379 bfd_size_type amt;
252b5132 1380
1b315056 1381 if (! read_section (abfd, ".debug_line", ".zdebug_line",
9e32b19f
DJ
1382 stash->syms, unit->line_offset,
1383 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
8af6b354 1384 return NULL;
ccdb16fc 1385
dc810e39 1386 amt = sizeof (struct line_info_table);
a50b1753 1387 table = (struct line_info_table *) bfd_alloc (abfd, amt);
8af6b354
AM
1388 if (table == NULL)
1389 return NULL;
252b5132
RH
1390 table->abfd = abfd;
1391 table->comp_dir = unit->comp_dir;
1392
1393 table->num_files = 0;
1394 table->files = NULL;
1395
1396 table->num_dirs = 0;
1397 table->dirs = NULL;
1398
0ee19663
NC
1399 table->num_sequences = 0;
1400 table->sequences = NULL;
1401
e82ce529 1402 table->lcl_head = NULL;
159002ff 1403
69dd2e2d 1404 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
252b5132 1405
a092b084 1406 /* Read in the prologue. */
91a4d569
AM
1407 lh.total_length = read_4_bytes (abfd, line_ptr);
1408 line_ptr += 4;
1409 offset_size = 4;
1410 if (lh.total_length == 0xffffffff)
dae2dd0d 1411 {
dae2dd0d
NC
1412 lh.total_length = read_8_bytes (abfd, line_ptr);
1413 line_ptr += 8;
1414 offset_size = 8;
1415 }
91a4d569 1416 else if (lh.total_length == 0 && unit->addr_size == 8)
d03ba2a1 1417 {
91a4d569
AM
1418 /* Handle (non-standard) 64-bit DWARF2 formats. */
1419 lh.total_length = read_4_bytes (abfd, line_ptr);
1420 line_ptr += 4;
d03ba2a1
JJ
1421 offset_size = 8;
1422 }
252b5132
RH
1423 line_end = line_ptr + lh.total_length;
1424 lh.version = read_2_bytes (abfd, line_ptr);
a233b20c
JJ
1425 if (lh.version < 2 || lh.version > 4)
1426 {
1427 (*_bfd_error_handler)
1428 (_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
1429 bfd_set_error (bfd_error_bad_value);
1430 return NULL;
1431 }
252b5132 1432 line_ptr += 2;
d03ba2a1
JJ
1433 if (offset_size == 4)
1434 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1435 else
1436 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1437 line_ptr += offset_size;
252b5132
RH
1438 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1439 line_ptr += 1;
a233b20c
JJ
1440 if (lh.version >= 4)
1441 {
1442 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr);
1443 line_ptr += 1;
1444 }
1445 else
1446 lh.maximum_ops_per_insn = 1;
1447 if (lh.maximum_ops_per_insn == 0)
1448 {
1449 (*_bfd_error_handler)
1450 (_("Dwarf Error: Invalid maximum operations per instruction."));
1451 bfd_set_error (bfd_error_bad_value);
1452 return NULL;
1453 }
252b5132
RH
1454 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1455 line_ptr += 1;
1456 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1457 line_ptr += 1;
1458 lh.line_range = read_1_byte (abfd, line_ptr);
1459 line_ptr += 1;
1460 lh.opcode_base = read_1_byte (abfd, line_ptr);
1461 line_ptr += 1;
dc810e39 1462 amt = lh.opcode_base * sizeof (unsigned char);
a50b1753 1463 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
252b5132
RH
1464
1465 lh.standard_opcode_lengths[0] = 1;
98591c73 1466
252b5132
RH
1467 for (i = 1; i < lh.opcode_base; ++i)
1468 {
1469 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1470 line_ptr += 1;
1471 }
1472
a092b084 1473 /* Read directory table. */
252b5132
RH
1474 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1475 {
1476 line_ptr += bytes_read;
98591c73 1477
252b5132
RH
1478 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1479 {
35330cce
NC
1480 char **tmp;
1481
dc810e39
AM
1482 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1483 amt *= sizeof (char *);
35330cce 1484
a50b1753 1485 tmp = (char **) bfd_realloc (table->dirs, amt);
35330cce 1486 if (tmp == NULL)
8af6b354 1487 goto fail;
35330cce 1488 table->dirs = tmp;
252b5132 1489 }
98591c73 1490
252b5132
RH
1491 table->dirs[table->num_dirs++] = cur_dir;
1492 }
98591c73 1493
252b5132
RH
1494 line_ptr += bytes_read;
1495
a092b084 1496 /* Read file name table. */
252b5132
RH
1497 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1498 {
1499 line_ptr += bytes_read;
98591c73 1500
252b5132
RH
1501 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1502 {
35330cce
NC
1503 struct fileinfo *tmp;
1504
dc810e39
AM
1505 amt = table->num_files + FILE_ALLOC_CHUNK;
1506 amt *= sizeof (struct fileinfo);
35330cce 1507
a50b1753 1508 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
35330cce 1509 if (tmp == NULL)
8af6b354 1510 goto fail;
35330cce 1511 table->files = tmp;
252b5132 1512 }
98591c73 1513
252b5132
RH
1514 table->files[table->num_files].name = cur_file;
1515 table->files[table->num_files].dir =
1516 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1517 line_ptr += bytes_read;
1518 table->files[table->num_files].time =
1519 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1520 line_ptr += bytes_read;
1521 table->files[table->num_files].size =
1522 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1523 line_ptr += bytes_read;
1524 table->num_files++;
1525 }
98591c73 1526
252b5132
RH
1527 line_ptr += bytes_read;
1528
1529 /* Read the statement sequences until there's nothing left. */
1530 while (line_ptr < line_end)
1531 {
a092b084 1532 /* State machine registers. */
252b5132 1533 bfd_vma address = 0;
a233b20c 1534 unsigned char op_index = 0;
8bfd78b3 1535 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
252b5132
RH
1536 unsigned int line = 1;
1537 unsigned int column = 0;
1538 int is_stmt = lh.default_is_stmt;
e2f6d277
NC
1539 int end_sequence = 0;
1540 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
e82ce529
AM
1541 compilers generate address sequences that are wildly out of
1542 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1543 for ia64-Linux). Thus, to determine the low and high
1544 address, we must compare on every DW_LNS_copy, etc. */
75758e9d 1545 bfd_vma low_pc = (bfd_vma) -1;
e2f6d277 1546 bfd_vma high_pc = 0;
252b5132 1547
a092b084 1548 /* Decode the table. */
252b5132
RH
1549 while (! end_sequence)
1550 {
1551 op_code = read_1_byte (abfd, line_ptr);
1552 line_ptr += 1;
98591c73 1553
1a509dcc 1554 if (op_code >= lh.opcode_base)
e2f6d277
NC
1555 {
1556 /* Special operand. */
1a509dcc 1557 adj_opcode = op_code - lh.opcode_base;
a233b20c
JJ
1558 if (lh.maximum_ops_per_insn == 1)
1559 address += (adj_opcode / lh.line_range)
1560 * lh.minimum_instruction_length;
1561 else
1562 {
1563 address += ((op_index + (adj_opcode / lh.line_range))
1564 / lh.maximum_ops_per_insn)
1565 * lh.minimum_instruction_length;
1566 op_index = (op_index + (adj_opcode / lh.line_range))
1567 % lh.maximum_ops_per_insn;
1568 }
1a509dcc
GK
1569 line += lh.line_base + (adj_opcode % lh.line_range);
1570 /* Append row to matrix using current values. */
a233b20c
JJ
1571 if (!add_line_info (table, address, op_index, filename,
1572 line, column, 0))
8af6b354 1573 goto line_fail;
75758e9d
AM
1574 if (address < low_pc)
1575 low_pc = address;
e2f6d277
NC
1576 if (address > high_pc)
1577 high_pc = address;
1a509dcc
GK
1578 }
1579 else switch (op_code)
252b5132
RH
1580 {
1581 case DW_LNS_extended_op:
e2f6d277
NC
1582 /* Ignore length. */
1583 line_ptr += 1;
252b5132
RH
1584 extended_op = read_1_byte (abfd, line_ptr);
1585 line_ptr += 1;
e2f6d277 1586
252b5132
RH
1587 switch (extended_op)
1588 {
1589 case DW_LNE_end_sequence:
1590 end_sequence = 1;
a233b20c
JJ
1591 if (!add_line_info (table, address, op_index, filename,
1592 line, column, end_sequence))
8af6b354 1593 goto line_fail;
75758e9d
AM
1594 if (address < low_pc)
1595 low_pc = address;
e2f6d277
NC
1596 if (address > high_pc)
1597 high_pc = address;
8af6b354
AM
1598 if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc))
1599 goto line_fail;
252b5132
RH
1600 break;
1601 case DW_LNE_set_address:
1602 address = read_address (unit, line_ptr);
a233b20c 1603 op_index = 0;
252b5132
RH
1604 line_ptr += unit->addr_size;
1605 break;
1606 case DW_LNE_define_file:
1607 cur_file = read_string (abfd, line_ptr, &bytes_read);
1608 line_ptr += bytes_read;
1609 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1610 {
35330cce
NC
1611 struct fileinfo *tmp;
1612
dc810e39
AM
1613 amt = table->num_files + FILE_ALLOC_CHUNK;
1614 amt *= sizeof (struct fileinfo);
a50b1753 1615 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
35330cce 1616 if (tmp == NULL)
8af6b354 1617 goto line_fail;
35330cce 1618 table->files = tmp;
252b5132
RH
1619 }
1620 table->files[table->num_files].name = cur_file;
1621 table->files[table->num_files].dir =
1622 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1623 line_ptr += bytes_read;
1624 table->files[table->num_files].time =
1625 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1626 line_ptr += bytes_read;
1627 table->files[table->num_files].size =
1628 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1629 line_ptr += bytes_read;
1630 table->num_files++;
1631 break;
9e1f7c0e
DK
1632 case DW_LNE_set_discriminator:
1633 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1634 line_ptr += bytes_read;
1635 break;
252b5132
RH
1636 default:
1637 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1638 bfd_set_error (bfd_error_bad_value);
8af6b354
AM
1639 line_fail:
1640 if (filename != NULL)
1641 free (filename);
1642 goto fail;
252b5132
RH
1643 }
1644 break;
1645 case DW_LNS_copy:
a233b20c
JJ
1646 if (!add_line_info (table, address, op_index,
1647 filename, line, column, 0))
8af6b354 1648 goto line_fail;
75758e9d
AM
1649 if (address < low_pc)
1650 low_pc = address;
e2f6d277
NC
1651 if (address > high_pc)
1652 high_pc = address;
252b5132
RH
1653 break;
1654 case DW_LNS_advance_pc:
a233b20c
JJ
1655 if (lh.maximum_ops_per_insn == 1)
1656 address += lh.minimum_instruction_length
1657 * read_unsigned_leb128 (abfd, line_ptr,
1658 &bytes_read);
1659 else
1660 {
1661 bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr,
1662 &bytes_read);
1663 address = ((op_index + adjust) / lh.maximum_ops_per_insn)
1664 * lh.minimum_instruction_length;
1665 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1666 }
252b5132
RH
1667 line_ptr += bytes_read;
1668 break;
1669 case DW_LNS_advance_line:
1670 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1671 line_ptr += bytes_read;
1672 break;
1673 case DW_LNS_set_file:
1674 {
1675 unsigned int file;
1676
e2f6d277
NC
1677 /* The file and directory tables are 0
1678 based, the references are 1 based. */
252b5132
RH
1679 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1680 line_ptr += bytes_read;
af3ef9fe
NC
1681 if (filename)
1682 free (filename);
252b5132
RH
1683 filename = concat_filename (table, file);
1684 break;
1685 }
1686 case DW_LNS_set_column:
1687 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1688 line_ptr += bytes_read;
1689 break;
1690 case DW_LNS_negate_stmt:
1691 is_stmt = (!is_stmt);
1692 break;
1693 case DW_LNS_set_basic_block:
252b5132
RH
1694 break;
1695 case DW_LNS_const_add_pc:
a233b20c
JJ
1696 if (lh.maximum_ops_per_insn == 1)
1697 address += lh.minimum_instruction_length
1698 * ((255 - lh.opcode_base) / lh.line_range);
1699 else
1700 {
1701 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
1702 address += lh.minimum_instruction_length
1703 * ((op_index + adjust) / lh.maximum_ops_per_insn);
1704 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1705 }
252b5132
RH
1706 break;
1707 case DW_LNS_fixed_advance_pc:
1708 address += read_2_bytes (abfd, line_ptr);
a233b20c 1709 op_index = 0;
252b5132
RH
1710 line_ptr += 2;
1711 break;
1a509dcc 1712 default:
91d6fa6a
NC
1713 /* Unknown standard opcode, ignore it. */
1714 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1715 {
1716 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1717 line_ptr += bytes_read;
1718 }
1719 break;
252b5132
RH
1720 }
1721 }
5ed6aba4 1722
af3ef9fe
NC
1723 if (filename)
1724 free (filename);
252b5132
RH
1725 }
1726
8af6b354
AM
1727 if (sort_line_sequences (table))
1728 return table;
0ee19663 1729
8af6b354
AM
1730 fail:
1731 if (table->sequences != NULL)
1732 free (table->sequences);
1733 if (table->files != NULL)
1734 free (table->files);
1735 if (table->dirs != NULL)
1736 free (table->dirs);
1737 return NULL;
252b5132
RH
1738}
1739
b34976b6
AM
1740/* If ADDR is within TABLE set the output parameters and return TRUE,
1741 otherwise return FALSE. The output parameters, FILENAME_PTR and
a092b084 1742 LINENUMBER_PTR, are pointers to the objects to be filled in. */
252b5132 1743
b34976b6 1744static bfd_boolean
818a27ac
AM
1745lookup_address_in_line_info_table (struct line_info_table *table,
1746 bfd_vma addr,
818a27ac
AM
1747 const char **filename_ptr,
1748 unsigned int *linenumber_ptr)
252b5132 1749{
0ee19663 1750 struct line_sequence *seq = NULL;
107601c8 1751 struct line_info *each_line;
0ee19663 1752 int low, high, mid;
e82ce529 1753
0ee19663
NC
1754 /* Binary search the array of sequences. */
1755 low = 0;
1756 high = table->num_sequences;
1757 while (low < high)
1758 {
1759 mid = (low + high) / 2;
1760 seq = &table->sequences[mid];
1761 if (addr < seq->low_pc)
1762 high = mid;
1763 else if (addr >= seq->last_line->address)
1764 low = mid + 1;
1765 else
1766 break;
1767 }
98591c73 1768
0ee19663 1769 if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
1ee24f27 1770 {
0ee19663
NC
1771 /* Note: seq->last_line should be a descendingly sorted list. */
1772 for (each_line = seq->last_line;
1773 each_line;
1774 each_line = each_line->prev_line)
1775 if (addr >= each_line->address)
1776 break;
1777
1778 if (each_line
1779 && !(each_line->end_sequence || each_line == seq->last_line))
1780 {
1781 *filename_ptr = each_line->filename;
1782 *linenumber_ptr = each_line->line;
1783 return TRUE;
1784 }
1ee24f27
DJ
1785 }
1786
107601c8 1787 *filename_ptr = NULL;
b34976b6 1788 return FALSE;
252b5132 1789}
98591c73 1790
0ee19663 1791/* Read in the .debug_ranges section for future reference. */
a13afe8e
FF
1792
1793static bfd_boolean
1794read_debug_ranges (struct comp_unit *unit)
1795{
1796 struct dwarf2_debug *stash = unit->stash;
1b315056 1797 return read_section (unit->abfd, ".debug_ranges", ".zdebug_ranges",
9e32b19f
DJ
1798 stash->syms, 0,
1799 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
a13afe8e
FF
1800}
1801
a092b084 1802/* Function table functions. */
252b5132 1803
a13afe8e
FF
1804/* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1805 Note that we need to find the function that has the smallest
1806 range that contains ADDR, to handle inlined functions without
1807 depending upon them being ordered in TABLE by increasing range. */
252b5132 1808
b34976b6 1809static bfd_boolean
4ab527b0 1810lookup_address_in_function_table (struct comp_unit *unit,
818a27ac
AM
1811 bfd_vma addr,
1812 struct funcinfo **function_ptr,
1813 const char **functionname_ptr)
252b5132
RH
1814{
1815 struct funcinfo* each_func;
a13afe8e
FF
1816 struct funcinfo* best_fit = NULL;
1817 struct arange *arange;
252b5132 1818
4ab527b0 1819 for (each_func = unit->function_table;
252b5132
RH
1820 each_func;
1821 each_func = each_func->prev_func)
1822 {
a13afe8e
FF
1823 for (arange = &each_func->arange;
1824 arange;
1825 arange = arange->next)
252b5132 1826 {
a13afe8e
FF
1827 if (addr >= arange->low && addr < arange->high)
1828 {
1829 if (!best_fit ||
1830 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1831 best_fit = each_func;
1832 }
252b5132
RH
1833 }
1834 }
98591c73 1835
a13afe8e
FF
1836 if (best_fit)
1837 {
1838 *functionname_ptr = best_fit->name;
1839 *function_ptr = best_fit;
1840 return TRUE;
1841 }
1842 else
1843 {
1844 return FALSE;
1845 }
252b5132
RH
1846}
1847
5420f73d
L
1848/* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1849 and LINENUMBER_PTR, and return TRUE. */
1850
1851static bfd_boolean
1852lookup_symbol_in_function_table (struct comp_unit *unit,
1853 asymbol *sym,
1854 bfd_vma addr,
1855 const char **filename_ptr,
1856 unsigned int *linenumber_ptr)
1857{
1858 struct funcinfo* each_func;
1859 struct funcinfo* best_fit = NULL;
1860 struct arange *arange;
1861 const char *name = bfd_asymbol_name (sym);
1862 asection *sec = bfd_get_section (sym);
1863
1864 for (each_func = unit->function_table;
1865 each_func;
1866 each_func = each_func->prev_func)
1867 {
1868 for (arange = &each_func->arange;
1869 arange;
1870 arange = arange->next)
1871 {
1872 if ((!each_func->sec || each_func->sec == sec)
1873 && addr >= arange->low
1874 && addr < arange->high
650f284e 1875 && each_func->name
5420f73d
L
1876 && strcmp (name, each_func->name) == 0
1877 && (!best_fit
1878 || ((arange->high - arange->low)
1879 < (best_fit->arange.high - best_fit->arange.low))))
1880 best_fit = each_func;
1881 }
1882 }
1883
1884 if (best_fit)
1885 {
1886 best_fit->sec = sec;
1887 *filename_ptr = best_fit->file;
1888 *linenumber_ptr = best_fit->line;
1889 return TRUE;
1890 }
1891 else
1892 return FALSE;
1893}
1894
1895/* Variable table functions. */
1896
1897/* If SYM is within variable table of UNIT, set FILENAME_PTR and
1898 LINENUMBER_PTR, and return TRUE. */
1899
1900static bfd_boolean
1901lookup_symbol_in_variable_table (struct comp_unit *unit,
1902 asymbol *sym,
5cf2e3f0 1903 bfd_vma addr,
5420f73d
L
1904 const char **filename_ptr,
1905 unsigned int *linenumber_ptr)
1906{
1907 const char *name = bfd_asymbol_name (sym);
1908 asection *sec = bfd_get_section (sym);
1909 struct varinfo* each;
1910
1911 for (each = unit->variable_table; each; each = each->prev_var)
1912 if (each->stack == 0
5cf2e3f0
L
1913 && each->file != NULL
1914 && each->name != NULL
1915 && each->addr == addr
5420f73d
L
1916 && (!each->sec || each->sec == sec)
1917 && strcmp (name, each->name) == 0)
1918 break;
1919
1920 if (each)
1921 {
1922 each->sec = sec;
1923 *filename_ptr = each->file;
1924 *linenumber_ptr = each->line;
1925 return TRUE;
1926 }
1927 else
1928 return FALSE;
1929}
1930
06f22d7e 1931static char *
5609a71e
DJ
1932find_abstract_instance_name (struct comp_unit *unit,
1933 struct attribute *attr_ptr)
06f22d7e
FF
1934{
1935 bfd *abfd = unit->abfd;
f075ee0c 1936 bfd_byte *info_ptr;
06f22d7e
FF
1937 unsigned int abbrev_number, bytes_read, i;
1938 struct abbrev_info *abbrev;
5609a71e 1939 bfd_uint64_t die_ref = attr_ptr->u.val;
06f22d7e
FF
1940 struct attribute attr;
1941 char *name = 0;
1942
5609a71e
DJ
1943 /* DW_FORM_ref_addr can reference an entry in a different CU. It
1944 is an offset from the .debug_info section, not the current CU. */
1945 if (attr_ptr->form == DW_FORM_ref_addr)
1946 {
1947 /* We only support DW_FORM_ref_addr within the same file, so
1948 any relocations should be resolved already. */
1949 if (!die_ref)
1950 abort ();
1951
a358ecb8 1952 info_ptr = unit->sec_info_ptr + die_ref;
5609a71e
DJ
1953 }
1954 else
1955 info_ptr = unit->info_ptr_unit + die_ref;
06f22d7e
FF
1956 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1957 info_ptr += bytes_read;
1958
1959 if (abbrev_number)
1960 {
1961 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1962 if (! abbrev)
1963 {
1964 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1965 abbrev_number);
1966 bfd_set_error (bfd_error_bad_value);
1967 }
1968 else
1969 {
d5cbaa15 1970 for (i = 0; i < abbrev->num_attrs; ++i)
06f22d7e 1971 {
8af6b354
AM
1972 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
1973 info_ptr);
1974 if (info_ptr == NULL)
1975 break;
26bf4e33
FF
1976 switch (attr.name)
1977 {
1978 case DW_AT_name:
643be349
JJ
1979 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
1980 over DW_AT_name. */
d5cbaa15
JW
1981 if (name == NULL)
1982 name = attr.u.str;
26bf4e33
FF
1983 break;
1984 case DW_AT_specification:
5609a71e 1985 name = find_abstract_instance_name (unit, &attr);
26bf4e33 1986 break;
643be349 1987 case DW_AT_linkage_name:
d5cbaa15
JW
1988 case DW_AT_MIPS_linkage_name:
1989 name = attr.u.str;
1990 break;
26bf4e33
FF
1991 default:
1992 break;
1993 }
06f22d7e
FF
1994 }
1995 }
1996 }
8af6b354 1997 return name;
06f22d7e
FF
1998}
1999
8af6b354
AM
2000static bfd_boolean
2001read_rangelist (struct comp_unit *unit, struct arange *arange,
2002 bfd_uint64_t offset)
a13afe8e
FF
2003{
2004 bfd_byte *ranges_ptr;
2005 bfd_vma base_address = unit->base_address;
2006
2007 if (! unit->stash->dwarf_ranges_buffer)
2008 {
2009 if (! read_debug_ranges (unit))
8af6b354 2010 return FALSE;
a13afe8e
FF
2011 }
2012 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
d8d1c398 2013
a13afe8e
FF
2014 for (;;)
2015 {
2016 bfd_vma low_pc;
2017 bfd_vma high_pc;
2018
13d72a14
AN
2019 low_pc = read_address (unit, ranges_ptr);
2020 ranges_ptr += unit->addr_size;
2021 high_pc = read_address (unit, ranges_ptr);
2022 ranges_ptr += unit->addr_size;
2023
a13afe8e
FF
2024 if (low_pc == 0 && high_pc == 0)
2025 break;
2026 if (low_pc == -1UL && high_pc != -1UL)
2027 base_address = high_pc;
2028 else
8af6b354
AM
2029 {
2030 if (!arange_add (unit->abfd, arange,
2031 base_address + low_pc, base_address + high_pc))
2032 return FALSE;
2033 }
a13afe8e 2034 }
8af6b354 2035 return TRUE;
a13afe8e
FF
2036}
2037
a092b084 2038/* DWARF2 Compilation unit functions. */
252b5132
RH
2039
2040/* Scan over each die in a comp. unit looking for functions to add
34b5e0b2 2041 to the function table and variables to the variable table. */
252b5132 2042
b34976b6 2043static bfd_boolean
5420f73d 2044scan_unit_for_symbols (struct comp_unit *unit)
252b5132
RH
2045{
2046 bfd *abfd = unit->abfd;
f075ee0c 2047 bfd_byte *info_ptr = unit->first_child_die_ptr;
252b5132 2048 int nesting_level = 1;
c955f9cd
JW
2049 struct funcinfo **nested_funcs;
2050 int nested_funcs_size;
2051
2052 /* Maintain a stack of in-scope functions and inlined functions, which we
2053 can use to set the caller_func field. */
2054 nested_funcs_size = 32;
a50b1753
NC
2055 nested_funcs = (struct funcinfo **)
2056 bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
c955f9cd
JW
2057 if (nested_funcs == NULL)
2058 return FALSE;
34b5e0b2 2059 nested_funcs[nesting_level] = 0;
252b5132
RH
2060
2061 while (nesting_level)
2062 {
2063 unsigned int abbrev_number, bytes_read, i;
2064 struct abbrev_info *abbrev;
2065 struct attribute attr;
2066 struct funcinfo *func;
5420f73d 2067 struct varinfo *var;
a13afe8e
FF
2068 bfd_vma low_pc = 0;
2069 bfd_vma high_pc = 0;
252b5132
RH
2070
2071 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2072 info_ptr += bytes_read;
2073
2074 if (! abbrev_number)
2075 {
2076 nesting_level--;
2077 continue;
2078 }
98591c73 2079
252b5132
RH
2080 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
2081 if (! abbrev)
2082 {
8af6b354
AM
2083 (*_bfd_error_handler)
2084 (_("Dwarf Error: Could not find abbrev number %u."),
2085 abbrev_number);
252b5132 2086 bfd_set_error (bfd_error_bad_value);
8af6b354 2087 goto fail;
252b5132 2088 }
98591c73 2089
5420f73d 2090 var = NULL;
06f22d7e 2091 if (abbrev->tag == DW_TAG_subprogram
5420f73d 2092 || abbrev->tag == DW_TAG_entry_point
06f22d7e 2093 || abbrev->tag == DW_TAG_inlined_subroutine)
252b5132 2094 {
dc810e39 2095 bfd_size_type amt = sizeof (struct funcinfo);
a50b1753 2096 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
8af6b354
AM
2097 if (func == NULL)
2098 goto fail;
4ab527b0 2099 func->tag = abbrev->tag;
252b5132
RH
2100 func->prev_func = unit->function_table;
2101 unit->function_table = func;
bd210d54 2102 BFD_ASSERT (!unit->cached);
c955f9cd
JW
2103
2104 if (func->tag == DW_TAG_inlined_subroutine)
2105 for (i = nesting_level - 1; i >= 1; i--)
2106 if (nested_funcs[i])
2107 {
2108 func->caller_func = nested_funcs[i];
2109 break;
2110 }
2111 nested_funcs[nesting_level] = func;
252b5132
RH
2112 }
2113 else
5420f73d
L
2114 {
2115 func = NULL;
2116 if (abbrev->tag == DW_TAG_variable)
2117 {
2118 bfd_size_type amt = sizeof (struct varinfo);
a50b1753 2119 var = (struct varinfo *) bfd_zalloc (abfd, amt);
8af6b354
AM
2120 if (var == NULL)
2121 goto fail;
5420f73d
L
2122 var->tag = abbrev->tag;
2123 var->stack = 1;
2124 var->prev_var = unit->variable_table;
2125 unit->variable_table = var;
bd210d54 2126 BFD_ASSERT (!unit->cached);
5420f73d 2127 }
c955f9cd
JW
2128
2129 /* No inline function in scope at this nesting level. */
2130 nested_funcs[nesting_level] = 0;
5420f73d 2131 }
98591c73 2132
252b5132
RH
2133 for (i = 0; i < abbrev->num_attrs; ++i)
2134 {
2135 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
8af6b354
AM
2136 if (info_ptr == NULL)
2137 return FALSE;
98591c73 2138
252b5132
RH
2139 if (func)
2140 {
2141 switch (attr.name)
2142 {
4ab527b0 2143 case DW_AT_call_file:
8af6b354
AM
2144 func->caller_file = concat_filename (unit->line_table,
2145 attr.u.val);
4ab527b0
FF
2146 break;
2147
2148 case DW_AT_call_line:
2149 func->caller_line = attr.u.val;
2150 break;
2151
06f22d7e 2152 case DW_AT_abstract_origin:
5609a71e 2153 func->name = find_abstract_instance_name (unit, &attr);
06f22d7e
FF
2154 break;
2155
252b5132 2156 case DW_AT_name:
643be349
JJ
2157 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2158 over DW_AT_name. */
252b5132 2159 if (func->name == NULL)
482e2e37 2160 func->name = attr.u.str;
252b5132 2161 break;
98591c73 2162
643be349 2163 case DW_AT_linkage_name:
252b5132 2164 case DW_AT_MIPS_linkage_name:
482e2e37 2165 func->name = attr.u.str;
252b5132
RH
2166 break;
2167
2168 case DW_AT_low_pc:
a13afe8e 2169 low_pc = attr.u.val;
252b5132
RH
2170 break;
2171
2172 case DW_AT_high_pc:
a13afe8e
FF
2173 high_pc = attr.u.val;
2174 break;
2175
2176 case DW_AT_ranges:
8af6b354
AM
2177 if (!read_rangelist (unit, &func->arange, attr.u.val))
2178 goto fail;
252b5132
RH
2179 break;
2180
5420f73d
L
2181 case DW_AT_decl_file:
2182 func->file = concat_filename (unit->line_table,
2183 attr.u.val);
2184 break;
2185
2186 case DW_AT_decl_line:
2187 func->line = attr.u.val;
2188 break;
2189
2190 default:
2191 break;
2192 }
2193 }
2194 else if (var)
2195 {
2196 switch (attr.name)
2197 {
2198 case DW_AT_name:
2199 var->name = attr.u.str;
2200 break;
2201
2202 case DW_AT_decl_file:
2203 var->file = concat_filename (unit->line_table,
2204 attr.u.val);
2205 break;
2206
2207 case DW_AT_decl_line:
2208 var->line = attr.u.val;
2209 break;
2210
2211 case DW_AT_external:
2212 if (attr.u.val != 0)
2213 var->stack = 0;
2214 break;
2215
2216 case DW_AT_location:
5cf2e3f0 2217 switch (attr.form)
5420f73d 2218 {
5cf2e3f0
L
2219 case DW_FORM_block:
2220 case DW_FORM_block1:
2221 case DW_FORM_block2:
2222 case DW_FORM_block4:
c07cbdd7 2223 case DW_FORM_exprloc:
5cf2e3f0 2224 if (*attr.u.blk->data == DW_OP_addr)
5420f73d 2225 {
5cf2e3f0 2226 var->stack = 0;
98b880f4
JW
2227
2228 /* Verify that DW_OP_addr is the only opcode in the
2229 location, in which case the block size will be 1
2230 plus the address size. */
2231 /* ??? For TLS variables, gcc can emit
2232 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2233 which we don't handle here yet. */
2234 if (attr.u.blk->size == unit->addr_size + 1U)
2235 var->addr = bfd_get (unit->addr_size * 8,
2236 unit->abfd,
2237 attr.u.blk->data + 1);
5420f73d 2238 }
5cf2e3f0 2239 break;
d8d1c398 2240
5cf2e3f0
L
2241 default:
2242 break;
5420f73d
L
2243 }
2244 break;
2245
252b5132
RH
2246 default:
2247 break;
2248 }
2249 }
2250 }
2251
a13afe8e
FF
2252 if (func && high_pc != 0)
2253 {
8af6b354
AM
2254 if (!arange_add (unit->abfd, &func->arange, low_pc, high_pc))
2255 goto fail;
a13afe8e
FF
2256 }
2257
252b5132 2258 if (abbrev->has_children)
c955f9cd
JW
2259 {
2260 nesting_level++;
2261
2262 if (nesting_level >= nested_funcs_size)
2263 {
2264 struct funcinfo **tmp;
2265
2266 nested_funcs_size *= 2;
a50b1753
NC
2267 tmp = (struct funcinfo **)
2268 bfd_realloc (nested_funcs,
2269 (nested_funcs_size * sizeof (struct funcinfo *)));
c955f9cd 2270 if (tmp == NULL)
8af6b354 2271 goto fail;
c955f9cd
JW
2272 nested_funcs = tmp;
2273 }
2274 nested_funcs[nesting_level] = 0;
2275 }
252b5132
RH
2276 }
2277
c955f9cd 2278 free (nested_funcs);
b34976b6 2279 return TRUE;
8af6b354
AM
2280
2281 fail:
2282 free (nested_funcs);
2283 return FALSE;
252b5132
RH
2284}
2285
5e38c3b8
MM
2286/* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2287 includes the compilation unit header that proceeds the DIE's, but
5c4491d3 2288 does not include the length field that precedes each compilation
5e38c3b8 2289 unit header. END_PTR points one past the end of this comp unit.
d03ba2a1 2290 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
252b5132
RH
2291
2292 This routine does not read the whole compilation unit; only enough
2293 to get to the line number information for the compilation unit. */
2294
2295static struct comp_unit *
0d161102 2296parse_comp_unit (struct dwarf2_debug *stash,
818a27ac 2297 bfd_vma unit_length,
f075ee0c 2298 bfd_byte *info_ptr_unit,
818a27ac 2299 unsigned int offset_size)
252b5132
RH
2300{
2301 struct comp_unit* unit;
f46c2da6 2302 unsigned int version;
8ce8c090 2303 bfd_uint64_t abbrev_offset = 0;
f46c2da6 2304 unsigned int addr_size;
252b5132 2305 struct abbrev_info** abbrevs;
252b5132
RH
2306 unsigned int abbrev_number, bytes_read, i;
2307 struct abbrev_info *abbrev;
2308 struct attribute attr;
f075ee0c
AM
2309 bfd_byte *info_ptr = stash->info_ptr;
2310 bfd_byte *end_ptr = info_ptr + unit_length;
dc810e39 2311 bfd_size_type amt;
a13afe8e
FF
2312 bfd_vma low_pc = 0;
2313 bfd_vma high_pc = 0;
a50b1753 2314 bfd *abfd = stash->bfd_ptr;
3fde5a36 2315
252b5132
RH
2316 version = read_2_bytes (abfd, info_ptr);
2317 info_ptr += 2;
d03ba2a1
JJ
2318 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2319 if (offset_size == 4)
5e38c3b8 2320 abbrev_offset = read_4_bytes (abfd, info_ptr);
d03ba2a1 2321 else
5e38c3b8 2322 abbrev_offset = read_8_bytes (abfd, info_ptr);
d03ba2a1 2323 info_ptr += offset_size;
252b5132
RH
2324 addr_size = read_1_byte (abfd, info_ptr);
2325 info_ptr += 1;
2326
c07cbdd7 2327 if (version != 2 && version != 3 && version != 4)
252b5132 2328 {
c07cbdd7 2329 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2, 3 and 4 information."), version);
252b5132
RH
2330 bfd_set_error (bfd_error_bad_value);
2331 return 0;
2332 }
2333
2334 if (addr_size > sizeof (bfd_vma))
2335 {
2336 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2337 addr_size,
f46c2da6 2338 (unsigned int) sizeof (bfd_vma));
252b5132
RH
2339 bfd_set_error (bfd_error_bad_value);
2340 return 0;
2341 }
2342
ecb651f0 2343 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 2344 {
f5a3e38a 2345 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
252b5132
RH
2346 bfd_set_error (bfd_error_bad_value);
2347 return 0;
2348 }
2349
a092b084 2350 /* Read the abbrevs for this compilation unit into a table. */
51db3708 2351 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132
RH
2352 if (! abbrevs)
2353 return 0;
2354
2355 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2356 info_ptr += bytes_read;
2357 if (! abbrev_number)
2358 {
f46c2da6 2359 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
252b5132
RH
2360 abbrev_number);
2361 bfd_set_error (bfd_error_bad_value);
2362 return 0;
2363 }
2364
2365 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2366 if (! abbrev)
2367 {
f46c2da6 2368 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
252b5132
RH
2369 abbrev_number);
2370 bfd_set_error (bfd_error_bad_value);
2371 return 0;
2372 }
98591c73 2373
dc810e39 2374 amt = sizeof (struct comp_unit);
a50b1753 2375 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
8af6b354
AM
2376 if (unit == NULL)
2377 return NULL;
252b5132 2378 unit->abfd = abfd;
5609a71e 2379 unit->version = version;
98591c73 2380 unit->addr_size = addr_size;
d03ba2a1 2381 unit->offset_size = offset_size;
252b5132
RH
2382 unit->abbrevs = abbrevs;
2383 unit->end_ptr = end_ptr;
d03ba2a1 2384 unit->stash = stash;
c0c28ab8 2385 unit->info_ptr_unit = info_ptr_unit;
a358ecb8 2386 unit->sec_info_ptr = stash->sec_info_ptr;
252b5132
RH
2387
2388 for (i = 0; i < abbrev->num_attrs; ++i)
2389 {
2390 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
8af6b354
AM
2391 if (info_ptr == NULL)
2392 return NULL;
252b5132
RH
2393
2394 /* Store the data if it is of an attribute we want to keep in a
2395 partial symbol table. */
2396 switch (attr.name)
2397 {
2398 case DW_AT_stmt_list:
2399 unit->stmtlist = 1;
482e2e37 2400 unit->line_offset = attr.u.val;
252b5132
RH
2401 break;
2402
2403 case DW_AT_name:
482e2e37 2404 unit->name = attr.u.str;
252b5132
RH
2405 break;
2406
2407 case DW_AT_low_pc:
a13afe8e
FF
2408 low_pc = attr.u.val;
2409 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2410 this is the base address to use when reading location
2411 lists or range lists. */
2412 unit->base_address = low_pc;
252b5132
RH
2413 break;
2414
2415 case DW_AT_high_pc:
a13afe8e
FF
2416 high_pc = attr.u.val;
2417 break;
2418
2419 case DW_AT_ranges:
8af6b354
AM
2420 if (!read_rangelist (unit, &unit->arange, attr.u.val))
2421 return NULL;
252b5132
RH
2422 break;
2423
2424 case DW_AT_comp_dir:
2425 {
f075ee0c 2426 char *comp_dir = attr.u.str;
252b5132
RH
2427 if (comp_dir)
2428 {
2429 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2430 directory, get rid of it. */
818a27ac 2431 char *cp = strchr (comp_dir, ':');
252b5132
RH
2432
2433 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2434 comp_dir = cp + 1;
2435 }
2436 unit->comp_dir = comp_dir;
2437 break;
2438 }
2439
2440 default:
2441 break;
2442 }
2443 }
a13afe8e 2444 if (high_pc != 0)
709d67f1 2445 {
8af6b354
AM
2446 if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc))
2447 return NULL;
709d67f1 2448 }
252b5132
RH
2449
2450 unit->first_child_die_ptr = info_ptr;
2451 return unit;
2452}
2453
6dd55cb7
L
2454/* Return TRUE if UNIT may contain the address given by ADDR. When
2455 there are functions written entirely with inline asm statements, the
2456 range info in the compilation unit header may not be correct. We
2457 need to consult the line info table to see if a compilation unit
2458 really contains the given address. */
252b5132 2459
b34976b6 2460static bfd_boolean
818a27ac 2461comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
252b5132 2462{
709d67f1
AM
2463 struct arange *arange;
2464
2465 if (unit->error)
2466 return FALSE;
2467
2468 arange = &unit->arange;
2469 do
2470 {
2471 if (addr >= arange->low && addr < arange->high)
2472 return TRUE;
2473 arange = arange->next;
2474 }
2475 while (arange);
2476
2477 return FALSE;
252b5132
RH
2478}
2479
252b5132
RH
2480/* If UNIT contains ADDR, set the output parameters to the values for
2481 the line containing ADDR. The output parameters, FILENAME_PTR,
2482 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 2483 to be filled in.
252b5132 2484
ab3acfbe 2485 Return TRUE if UNIT contains ADDR, and no errors were encountered;
b34976b6 2486 FALSE otherwise. */
252b5132 2487
b34976b6 2488static bfd_boolean
818a27ac
AM
2489comp_unit_find_nearest_line (struct comp_unit *unit,
2490 bfd_vma addr,
2491 const char **filename_ptr,
2492 const char **functionname_ptr,
2493 unsigned int *linenumber_ptr,
2494 struct dwarf2_debug *stash)
252b5132 2495{
b34976b6
AM
2496 bfd_boolean line_p;
2497 bfd_boolean func_p;
1ee24f27 2498 struct funcinfo *function;
98591c73 2499
252b5132 2500 if (unit->error)
b34976b6 2501 return FALSE;
252b5132
RH
2502
2503 if (! unit->line_table)
2504 {
2505 if (! unit->stmtlist)
2506 {
2507 unit->error = 1;
b34976b6 2508 return FALSE;
252b5132 2509 }
98591c73 2510
51db3708 2511 unit->line_table = decode_line_info (unit, stash);
252b5132
RH
2512
2513 if (! unit->line_table)
2514 {
2515 unit->error = 1;
b34976b6 2516 return FALSE;
252b5132 2517 }
98591c73 2518
3f5864e1 2519 if (unit->first_child_die_ptr < unit->end_ptr
5420f73d 2520 && ! scan_unit_for_symbols (unit))
252b5132
RH
2521 {
2522 unit->error = 1;
b34976b6 2523 return FALSE;
252b5132
RH
2524 }
2525 }
2526
1ee24f27 2527 function = NULL;
4ab527b0 2528 func_p = lookup_address_in_function_table (unit, addr,
e2f6d277 2529 &function, functionname_ptr);
4ab527b0
FF
2530 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2531 stash->inliner_chain = function;
e2f6d277 2532 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
107601c8 2533 filename_ptr,
252b5132 2534 linenumber_ptr);
b34976b6 2535 return line_p || func_p;
252b5132
RH
2536}
2537
bd210d54
NC
2538/* Check to see if line info is already decoded in a comp_unit.
2539 If not, decode it. Returns TRUE if no errors were encountered;
5420f73d
L
2540 FALSE otherwise. */
2541
2542static bfd_boolean
bd210d54
NC
2543comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2544 struct dwarf2_debug *stash)
5420f73d
L
2545{
2546 if (unit->error)
2547 return FALSE;
2548
2549 if (! unit->line_table)
2550 {
2551 if (! unit->stmtlist)
2552 {
2553 unit->error = 1;
2554 return FALSE;
2555 }
2556
2557 unit->line_table = decode_line_info (unit, stash);
2558
2559 if (! unit->line_table)
2560 {
2561 unit->error = 1;
2562 return FALSE;
2563 }
2564
2565 if (unit->first_child_die_ptr < unit->end_ptr
2566 && ! scan_unit_for_symbols (unit))
2567 {
2568 unit->error = 1;
2569 return FALSE;
2570 }
2571 }
2572
bd210d54
NC
2573 return TRUE;
2574}
2575
2576/* If UNIT contains SYM at ADDR, set the output parameters to the
2577 values for the line containing SYM. The output parameters,
2578 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2579 filled in.
2580
2581 Return TRUE if UNIT contains SYM, and no errors were encountered;
2582 FALSE otherwise. */
2583
2584static bfd_boolean
2585comp_unit_find_line (struct comp_unit *unit,
2586 asymbol *sym,
2587 bfd_vma addr,
2588 const char **filename_ptr,
2589 unsigned int *linenumber_ptr,
2590 struct dwarf2_debug *stash)
2591{
2592 if (!comp_unit_maybe_decode_line_info (unit, stash))
2593 return FALSE;
2594
5420f73d
L
2595 if (sym->flags & BSF_FUNCTION)
2596 return lookup_symbol_in_function_table (unit, sym, addr,
2597 filename_ptr,
2598 linenumber_ptr);
bd210d54
NC
2599
2600 return lookup_symbol_in_variable_table (unit, sym, addr,
2601 filename_ptr,
2602 linenumber_ptr);
2603}
2604
2605static struct funcinfo *
2606reverse_funcinfo_list (struct funcinfo *head)
2607{
2608 struct funcinfo *rhead;
2609 struct funcinfo *temp;
2610
2611 for (rhead = NULL; head; head = temp)
2612 {
2613 temp = head->prev_func;
2614 head->prev_func = rhead;
2615 rhead = head;
2616 }
2617 return rhead;
2618}
2619
2620static struct varinfo *
2621reverse_varinfo_list (struct varinfo *head)
2622{
2623 struct varinfo *rhead;
2624 struct varinfo *temp;
2625
2626 for (rhead = NULL; head; head = temp)
2627 {
2628 temp = head->prev_var;
2629 head->prev_var = rhead;
2630 rhead = head;
2631 }
2632 return rhead;
2633}
2634
2635/* Extract all interesting funcinfos and varinfos of a compilation
2636 unit into hash tables for faster lookup. Returns TRUE if no
2637 errors were enountered; FALSE otherwise. */
2638
2639static bfd_boolean
2640comp_unit_hash_info (struct dwarf2_debug *stash,
2641 struct comp_unit *unit,
2642 struct info_hash_table *funcinfo_hash_table,
2643 struct info_hash_table *varinfo_hash_table)
2644{
2645 struct funcinfo* each_func;
2646 struct varinfo* each_var;
2647 bfd_boolean okay = TRUE;
2648
2649 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2650
2651 if (!comp_unit_maybe_decode_line_info (unit, stash))
2652 return FALSE;
2653
2654 BFD_ASSERT (!unit->cached);
2655
2656 /* To preserve the original search order, we went to visit the function
2657 infos in the reversed order of the list. However, making the list
2658 bi-directional use quite a bit of extra memory. So we reverse
2659 the list first, traverse the list in the now reversed order and
2660 finally reverse the list again to get back the original order. */
2661 unit->function_table = reverse_funcinfo_list (unit->function_table);
2662 for (each_func = unit->function_table;
2663 each_func && okay;
2664 each_func = each_func->prev_func)
2665 {
2666 /* Skip nameless functions. */
2667 if (each_func->name)
2668 /* There is no need to copy name string into hash table as
2669 name string is either in the dwarf string buffer or
2670 info in the stash. */
2671 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2672 (void*) each_func, FALSE);
2673 }
2674 unit->function_table = reverse_funcinfo_list (unit->function_table);
2675 if (!okay)
2676 return FALSE;
2677
2678 /* We do the same for variable infos. */
2679 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2680 for (each_var = unit->variable_table;
2681 each_var && okay;
2682 each_var = each_var->prev_var)
2683 {
2684 /* Skip stack vars and vars with no files or names. */
2685 if (each_var->stack == 0
2686 && each_var->file != NULL
2687 && each_var->name != NULL)
2688 /* There is no need to copy name string into hash table as
2689 name string is either in the dwarf string buffer or
2690 info in the stash. */
2691 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2692 (void*) each_var, FALSE);
2693 }
2694
2695 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2696 unit->cached = TRUE;
2697 return okay;
5420f73d
L
2698}
2699
e2f6d277
NC
2700/* Locate a section in a BFD containing debugging info. The search starts
2701 from the section after AFTER_SEC, or from the first section in the BFD if
2702 AFTER_SEC is NULL. The search works by examining the names of the
2703 sections. There are two permissiable names. The first is .debug_info.
2704 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2705 This is a variation on the .debug_info section which has a checksum
2706 describing the contents appended onto the name. This allows the linker to
2707 identify and discard duplicate debugging sections for different
2708 compilation units. */
a092b084 2709#define DWARF2_DEBUG_INFO ".debug_info"
1b315056 2710#define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
a092b084
NC
2711#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2712
2713static asection *
818a27ac 2714find_debug_info (bfd *abfd, asection *after_sec)
a092b084
NC
2715{
2716 asection * msec;
2717
0d161102 2718 msec = after_sec != NULL ? after_sec->next : abfd->sections;
a092b084
NC
2719
2720 while (msec)
2721 {
2722 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2723 return msec;
2724
1b315056
CS
2725 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2726 return msec;
2727
0112cd26 2728 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
a092b084
NC
2729 return msec;
2730
2731 msec = msec->next;
2732 }
2733
2734 return NULL;
2735}
2736
5609a71e 2737/* Unset vmas for adjusted sections in STASH. */
d4c32a81
L
2738
2739static void
2740unset_sections (struct dwarf2_debug *stash)
2741{
2742 unsigned int i;
5609a71e 2743 struct adjusted_section *p;
d4c32a81 2744
5609a71e
DJ
2745 i = stash->adjusted_section_count;
2746 p = stash->adjusted_sections;
d4c32a81
L
2747 for (; i > 0; i--, p++)
2748 p->section->vma = 0;
2749}
2750
5609a71e
DJ
2751/* Set unique VMAs for loadable and DWARF sections in ABFD and save
2752 VMAs in STASH for unset_sections. */
35ccda9e
L
2753
2754static bfd_boolean
d4c32a81 2755place_sections (bfd *abfd, struct dwarf2_debug *stash)
35ccda9e 2756{
5609a71e 2757 struct adjusted_section *p;
d4c32a81
L
2758 unsigned int i;
2759
5609a71e 2760 if (stash->adjusted_section_count != 0)
35ccda9e 2761 {
5609a71e
DJ
2762 i = stash->adjusted_section_count;
2763 p = stash->adjusted_sections;
d4c32a81
L
2764 for (; i > 0; i--, p++)
2765 p->section->vma = p->adj_vma;
2766 }
2767 else
2768 {
2769 asection *sect;
5609a71e 2770 bfd_vma last_vma = 0, last_dwarf = 0;
d4c32a81 2771 bfd_size_type amt;
35ccda9e 2772
d4c32a81
L
2773 i = 0;
2774 for (sect = abfd->sections; sect != NULL; sect = sect->next)
35ccda9e 2775 {
d4c32a81 2776 bfd_size_type sz;
5609a71e
DJ
2777 int is_debug_info;
2778
2779 if (sect->vma != 0)
2780 continue;
2781
2782 /* We need to adjust the VMAs of any .debug_info sections.
2783 Skip compressed ones, since no relocations could target
2784 them - they should not appear in object files anyway. */
2785 if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
2786 is_debug_info = 1;
2787 else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2788 is_debug_info = 1;
2789 else
2790 is_debug_info = 0;
d4c32a81 2791
5609a71e 2792 if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
d4c32a81
L
2793 continue;
2794
2795 sz = sect->rawsize ? sect->rawsize : sect->size;
2796 if (sz == 0)
2797 continue;
2798
2799 i++;
2800 }
2801
5609a71e
DJ
2802 amt = i * sizeof (struct adjusted_section);
2803 p = (struct adjusted_section *) bfd_zalloc (abfd, amt);
d4c32a81
L
2804 if (! p)
2805 return FALSE;
2806
5609a71e
DJ
2807 stash->adjusted_sections = p;
2808 stash->adjusted_section_count = i;
d4c32a81
L
2809
2810 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2811 {
2812 bfd_size_type sz;
5609a71e 2813 int is_debug_info;
d4c32a81 2814
5609a71e
DJ
2815 if (sect->vma != 0)
2816 continue;
2817
2818 /* We need to adjust the VMAs of any .debug_info sections.
2819 Skip compressed ones, since no relocations could target
2820 them - they should not appear in object files anyway. */
2821 if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
2822 is_debug_info = 1;
2823 else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2824 is_debug_info = 1;
2825 else
2826 is_debug_info = 0;
2827
2828 if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
d4c32a81
L
2829 continue;
2830
2831 sz = sect->rawsize ? sect->rawsize : sect->size;
2832 if (sz == 0)
2833 continue;
2834
2835 p->section = sect;
5609a71e
DJ
2836 if (is_debug_info)
2837 {
2838 BFD_ASSERT (sect->alignment_power == 0);
2839 sect->vma = last_dwarf;
2840 last_dwarf += sz;
2841 }
2842 else if (last_vma != 0)
d4c32a81
L
2843 {
2844 /* Align the new address to the current section
2845 alignment. */
2846 last_vma = ((last_vma
2847 + ~((bfd_vma) -1 << sect->alignment_power))
2848 & ((bfd_vma) -1 << sect->alignment_power));
2849 sect->vma = last_vma;
5609a71e 2850 last_vma += sect->vma + sz;
d4c32a81 2851 }
5609a71e
DJ
2852 else
2853 last_vma += sect->vma + sz;
2854
d4c32a81 2855 p->adj_vma = sect->vma;
d4c32a81
L
2856
2857 p++;
35ccda9e
L
2858 }
2859 }
2860
2861 return TRUE;
2862}
2863
bd210d54
NC
2864/* Look up a funcinfo by name using the given info hash table. If found,
2865 also update the locations pointed to by filename_ptr and linenumber_ptr.
2866
2867 This function returns TRUE if a funcinfo that matches the given symbol
2868 and address is found with any error; otherwise it returns FALSE. */
2869
2870static bfd_boolean
2871info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2872 asymbol *sym,
2873 bfd_vma addr,
2874 const char **filename_ptr,
2875 unsigned int *linenumber_ptr)
2876{
2877 struct funcinfo* each_func;
2878 struct funcinfo* best_fit = NULL;
2879 struct info_list_node *node;
2880 struct arange *arange;
2881 const char *name = bfd_asymbol_name (sym);
2882 asection *sec = bfd_get_section (sym);
2883
2884 for (node = lookup_info_hash_table (hash_table, name);
2885 node;
2886 node = node->next)
2887 {
a50b1753 2888 each_func = (struct funcinfo *) node->info;
bd210d54
NC
2889 for (arange = &each_func->arange;
2890 arange;
2891 arange = arange->next)
2892 {
2893 if ((!each_func->sec || each_func->sec == sec)
2894 && addr >= arange->low
2895 && addr < arange->high
2896 && (!best_fit
2897 || ((arange->high - arange->low)
2898 < (best_fit->arange.high - best_fit->arange.low))))
2899 best_fit = each_func;
2900 }
2901 }
2902
2903 if (best_fit)
2904 {
2905 best_fit->sec = sec;
2906 *filename_ptr = best_fit->file;
2907 *linenumber_ptr = best_fit->line;
2908 return TRUE;
2909 }
2910
2911 return FALSE;
2912}
2913
2914/* Look up a varinfo by name using the given info hash table. If found,
2915 also update the locations pointed to by filename_ptr and linenumber_ptr.
2916
2917 This function returns TRUE if a varinfo that matches the given symbol
2918 and address is found with any error; otherwise it returns FALSE. */
2919
2920static bfd_boolean
2921info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2922 asymbol *sym,
2923 bfd_vma addr,
2924 const char **filename_ptr,
2925 unsigned int *linenumber_ptr)
2926{
2927 const char *name = bfd_asymbol_name (sym);
2928 asection *sec = bfd_get_section (sym);
2929 struct varinfo* each;
2930 struct info_list_node *node;
2931
2932 for (node = lookup_info_hash_table (hash_table, name);
2933 node;
2934 node = node->next)
2935 {
a50b1753 2936 each = (struct varinfo *) node->info;
bd210d54
NC
2937 if (each->addr == addr
2938 && (!each->sec || each->sec == sec))
2939 {
2940 each->sec = sec;
2941 *filename_ptr = each->file;
2942 *linenumber_ptr = each->line;
2943 return TRUE;
2944 }
2945 }
2946
2947 return FALSE;
2948}
2949
2950/* Update the funcinfo and varinfo info hash tables if they are
2951 not up to date. Returns TRUE if there is no error; otherwise
2952 returns FALSE and disable the info hash tables. */
2953
2954static bfd_boolean
2955stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2956{
2957 struct comp_unit *each;
2958
2959 /* Exit if hash tables are up-to-date. */
2960 if (stash->all_comp_units == stash->hash_units_head)
2961 return TRUE;
2962
2963 if (stash->hash_units_head)
2964 each = stash->hash_units_head->prev_unit;
2965 else
2966 each = stash->last_comp_unit;
2967
2968 while (each)
2969 {
2970 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2971 stash->varinfo_hash_table))
2972 {
2973 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2974 return FALSE;
2975 }
2976 each = each->prev_unit;
2977 }
2978
2979 stash->hash_units_head = stash->all_comp_units;
2980 return TRUE;
2981}
2982
2983/* Check consistency of info hash tables. This is for debugging only. */
2984
2985static void ATTRIBUTE_UNUSED
2986stash_verify_info_hash_table (struct dwarf2_debug *stash)
2987{
2988 struct comp_unit *each_unit;
2989 struct funcinfo *each_func;
2990 struct varinfo *each_var;
2991 struct info_list_node *node;
2992 bfd_boolean found;
2993
2994 for (each_unit = stash->all_comp_units;
2995 each_unit;
2996 each_unit = each_unit->next_unit)
2997 {
2998 for (each_func = each_unit->function_table;
2999 each_func;
3000 each_func = each_func->prev_func)
3001 {
3002 if (!each_func->name)
3003 continue;
3004 node = lookup_info_hash_table (stash->funcinfo_hash_table,
3005 each_func->name);
3006 BFD_ASSERT (node);
3007 found = FALSE;
3008 while (node && !found)
3009 {
3010 found = node->info == each_func;
3011 node = node->next;
3012 }
3013 BFD_ASSERT (found);
3014 }
3015
3016 for (each_var = each_unit->variable_table;
3017 each_var;
3018 each_var = each_var->prev_var)
3019 {
3020 if (!each_var->name || !each_var->file || each_var->stack)
3021 continue;
3022 node = lookup_info_hash_table (stash->varinfo_hash_table,
3023 each_var->name);
3024 BFD_ASSERT (node);
3025 found = FALSE;
3026 while (node && !found)
3027 {
3028 found = node->info == each_var;
3029 node = node->next;
3030 }
3031 BFD_ASSERT (found);
3032 }
3033 }
3034}
3035
3036/* Check to see if we want to enable the info hash tables, which consume
3037 quite a bit of memory. Currently we only check the number times
3038 bfd_dwarf2_find_line is called. In the future, we may also want to
3039 take the number of symbols into account. */
3040
3041static void
3042stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
3043{
3044 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
3045
3046 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
3047 return;
3048
3049 /* FIXME: Maybe we should check the reduce_memory_overheads
3050 and optimize fields in the bfd_link_info structure ? */
3051
3052 /* Create hash tables. */
3053 stash->funcinfo_hash_table = create_info_hash_table (abfd);
3054 stash->varinfo_hash_table = create_info_hash_table (abfd);
3055 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
3056 {
3057 /* Turn off info hashes if any allocation above fails. */
3058 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3059 return;
3060 }
3061 /* We need a forced update so that the info hash tables will
3062 be created even though there is no compilation unit. That
3063 happens if STASH_INFO_HASH_TRIGGER is 0. */
3064 stash_maybe_update_info_hash_tables (stash);
3065 stash->info_hash_status = STASH_INFO_HASH_ON;
3066}
3067
3068/* Find the file and line associated with a symbol and address using the
3069 info hash tables of a stash. If there is a match, the function returns
3070 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
3071 otherwise it returns FALSE. */
3072
3073static bfd_boolean
3074stash_find_line_fast (struct dwarf2_debug *stash,
3075 asymbol *sym,
3076 bfd_vma addr,
3077 const char **filename_ptr,
3078 unsigned int *linenumber_ptr)
3079{
3080 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
3081
3082 if (sym->flags & BSF_FUNCTION)
3083 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
3084 filename_ptr, linenumber_ptr);
3085 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
3086 filename_ptr, linenumber_ptr);
3087}
3088
bec42b15
NC
3089/* Find the source code location of SYMBOL. If SYMBOL is NULL
3090 then find the nearest source code location corresponding to
3091 the address SECTION + OFFSET.
3092 Returns TRUE if the line is found without error and fills in
3093 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
3094 NULL the FUNCTIONNAME_PTR is also filled in.
3095 SYMBOLS contains the symbol table for ABFD.
3096 ADDR_SIZE is the number of bytes in the initial .debug_info length
3097 field and in the abbreviation offset, or zero to indicate that the
3098 default value should be used. */
252b5132 3099
bec42b15
NC
3100static bfd_boolean
3101find_line (bfd *abfd,
3102 asection *section,
3103 bfd_vma offset,
3104 asymbol *symbol,
3105 asymbol **symbols,
3106 const char **filename_ptr,
3107 const char **functionname_ptr,
3108 unsigned int *linenumber_ptr,
3109 unsigned int addr_size,
3110 void **pinfo)
252b5132
RH
3111{
3112 /* Read each compilation unit from the section .debug_info, and check
3113 to see if it contains the address we are searching for. If yes,
3114 lookup the address, and return the line number info. If no, go
98591c73 3115 on to the next compilation unit.
252b5132
RH
3116
3117 We keep a list of all the previously read compilation units, and
98591c73 3118 a pointer to the next un-read compilation unit. Check the
a092b084 3119 previously read units before reading more. */
1ba54ee0 3120 struct dwarf2_debug *stash;
a092b084 3121 /* What address are we looking for? */
1ba54ee0 3122 bfd_vma addr;
252b5132 3123 struct comp_unit* each;
d4c32a81 3124 bfd_vma found = FALSE;
bec42b15 3125 bfd_boolean do_line;
d4c32a81 3126
a50b1753 3127 stash = (struct dwarf2_debug *) *pinfo;
d4c32a81
L
3128
3129 if (! stash)
3130 {
3131 bfd_size_type amt = sizeof (struct dwarf2_debug);
3132
a50b1753 3133 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
d4c32a81
L
3134 if (! stash)
3135 return FALSE;
3136 }
3137
3138 /* In a relocatable file, 2 functions may have the same address.
3139 We change the section vma so that they won't overlap. */
3140 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3141 {
3142 if (! place_sections (abfd, stash))
3143 return FALSE;
3144 }
3145
bec42b15
NC
3146 do_line = (section == NULL
3147 && offset == 0
3148 && functionname_ptr == NULL
3149 && symbol != NULL);
3150 if (do_line)
3151 {
3152 addr = symbol->value;
3153 section = bfd_get_section (symbol);
3154 }
3155 else if (section != NULL
3156 && functionname_ptr != NULL
3157 && symbol == NULL)
3158 addr = offset;
3159 else
3160 abort ();
3161
1ba54ee0 3162 if (section->output_section)
6dd55cb7 3163 addr += section->output_section->vma + section->output_offset;
1ba54ee0 3164 else
6dd55cb7 3165 addr += section->vma;
252b5132 3166 *filename_ptr = NULL;
6b33789f
NC
3167 if (! do_line)
3168 *functionname_ptr = NULL;
252b5132
RH
3169 *linenumber_ptr = 0;
3170
d4c32a81 3171 if (! *pinfo)
252b5132 3172 {
0d161102 3173 bfd *debug_bfd;
dc810e39 3174 bfd_size_type total_size;
252b5132 3175 asection *msec;
252b5132 3176
818a27ac 3177 *pinfo = stash;
3fde5a36 3178
a092b084 3179 msec = find_debug_info (abfd, NULL);
0d161102
NC
3180 if (msec == NULL)
3181 {
0d4a1476 3182 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
0d161102
NC
3183
3184 if (debug_filename == NULL)
3185 /* No dwarf2 info, and no gnu_debuglink to follow.
3186 Note that at this point the stash has been allocated, but
3187 contains zeros. This lets future calls to this function
3188 fail more quickly. */
3189 goto done;
3190
3191 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
3192 || ! bfd_check_format (debug_bfd, bfd_object)
3193 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
3194 {
3195 if (debug_bfd)
3196 bfd_close (debug_bfd);
3197 /* FIXME: Should we report our failure to follow the debuglink ? */
3198 free (debug_filename);
3199 goto done;
3200 }
3201 }
3202 else
3203 debug_bfd = abfd;
a092b084 3204
1b315056 3205 /* There can be more than one DWARF2 info section in a BFD these
2d47a72c
DJ
3206 days. First handle the easy case when there's only one. If
3207 there's more than one, try case two: none of the sections is
3208 compressed. In that case, read them all in and produce one
3209 large stash. We do this in two passes - in the first pass we
3210 just accumulate the section sizes, and in the second pass we
3211 read in the section's contents. (The allows us to avoid
3212 reallocing the data as we add sections to the stash.) If
3213 some or all sections are compressed, then do things the slow
3214 way, with a bunch of reallocs. */
1b315056
CS
3215
3216 if (! find_debug_info (debug_bfd, msec))
2d47a72c
DJ
3217 {
3218 /* Case 1: only one info section. */
3219 total_size = msec->size;
3220 if (! read_section (debug_bfd, ".debug_info", ".zdebug_info",
9e32b19f
DJ
3221 symbols, 0,
3222 &stash->info_ptr_memory, &total_size))
2d47a72c 3223 goto done;
2d47a72c 3224 }
1b315056 3225 else
2d47a72c
DJ
3226 {
3227 int all_uncompressed = 1;
3228 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
3229 {
3230 total_size += msec->size;
3231 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
9e32b19f 3232 all_uncompressed = 0;
2d47a72c
DJ
3233 }
3234 if (all_uncompressed)
3235 {
3236 /* Case 2: multiple sections, but none is compressed. */
a50b1753 3237 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
2d47a72c
DJ
3238 if (stash->info_ptr_memory == NULL)
3239 goto done;
3240
5d0900eb 3241 total_size = 0;
2d47a72c
DJ
3242 for (msec = find_debug_info (debug_bfd, NULL);
3243 msec;
3244 msec = find_debug_info (debug_bfd, msec))
3245 {
3246 bfd_size_type size;
2d47a72c
DJ
3247
3248 size = msec->size;
3249 if (size == 0)
3250 continue;
3251
5d0900eb
AM
3252 if (!(bfd_simple_get_relocated_section_contents
3253 (debug_bfd, msec, stash->info_ptr_memory + total_size,
3254 symbols)))
3255 goto done;
2d47a72c 3256
5d0900eb 3257 total_size += size;
2d47a72c 3258 }
2d47a72c
DJ
3259 }
3260 else
3261 {
3262 /* Case 3: multiple sections, some or all compressed. */
5d0900eb
AM
3263 stash->info_ptr_memory = NULL;
3264 total_size = 0;
2d47a72c
DJ
3265 for (msec = find_debug_info (debug_bfd, NULL);
3266 msec;
3267 msec = find_debug_info (debug_bfd, msec))
3268 {
9e32b19f 3269 bfd_size_type size = msec->size;
8af6b354 3270 bfd_byte *buffer, *tmp;
0acf065b
CC
3271 bfd_boolean is_compressed =
3272 strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0;
5d0900eb
AM
3273
3274 if (size == 0)
9e32b19f 3275 continue;
5d0900eb 3276
0acf065b
CC
3277 if (! read_and_uncompress_section (debug_bfd, msec,
3278 is_compressed, symbols,
3279 &buffer, &size))
5d0900eb
AM
3280 goto done;
3281
8af6b354
AM
3282 tmp = (bfd_byte *) bfd_realloc (stash->info_ptr_memory,
3283 total_size + size);
3284 if (tmp == NULL)
3285 {
3286 free (buffer);
3287 goto done;
3288 }
3289 stash->info_ptr_memory = tmp;
5d0900eb 3290 memcpy (stash->info_ptr_memory + total_size, buffer, size);
9e32b19f 3291 free (buffer);
5d0900eb 3292 total_size += size;
9e32b19f 3293 }
2d47a72c
DJ
3294 }
3295 }
f2363ce5 3296
5d0900eb
AM
3297 stash->info_ptr = stash->info_ptr_memory;
3298 stash->info_ptr_end = stash->info_ptr + total_size;
0d161102 3299 stash->sec = find_debug_info (debug_bfd, NULL);
f2363ce5
AO
3300 stash->sec_info_ptr = stash->info_ptr;
3301 stash->syms = symbols;
a50b1753 3302 stash->bfd_ptr = debug_bfd;
252b5132 3303 }
a092b084 3304
98591c73 3305 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 3306 (or that an error occured while setting up the stash). */
252b5132 3307 if (! stash->info_ptr)
d4c32a81 3308 goto done;
252b5132 3309
4ab527b0
FF
3310 stash->inliner_chain = NULL;
3311
a092b084 3312 /* Check the previously read comp. units first. */
bd210d54
NC
3313 if (do_line)
3314 {
3315 /* The info hash tables use quite a bit of memory. We may not want to
3316 always use them. We use some heuristics to decide if and when to
3317 turn it on. */
3318 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3319 stash_maybe_enable_info_hash_tables (abfd, stash);
3320
3321 /* Keep info hash table up to date if they are available. Note that we
3322 may disable the hash tables if there is any error duing update. */
3323 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3324 stash_maybe_update_info_hash_tables (stash);
3325
3326 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3327 {
3328 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3329 linenumber_ptr);
3330 if (found)
3331 goto done;
3332 }
0d161102 3333 else
bd210d54
NC
3334 {
3335 /* Check the previously read comp. units first. */
3336 for (each = stash->all_comp_units; each; each = each->next_unit)
3337 if ((symbol->flags & BSF_FUNCTION) == 0
3338 || comp_unit_contains_address (each, addr))
3339 {
3340 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3341 linenumber_ptr, stash);
3342 if (found)
3343 goto done;
3344 }
3345 }
3346 }
3347 else
3348 {
709d67f1
AM
3349 for (each = stash->all_comp_units; each; each = each->next_unit)
3350 {
3351 found = (comp_unit_contains_address (each, addr)
3352 && comp_unit_find_nearest_line (each, addr,
3353 filename_ptr,
3354 functionname_ptr,
3355 linenumber_ptr,
3356 stash));
3357 if (found)
3358 goto done;
3359 }
5420f73d
L
3360 }
3361
5420f73d
L
3362 /* The DWARF2 spec says that the initial length field, and the
3363 offset of the abbreviation table, should both be 4-byte values.
3364 However, some compilers do things differently. */
3365 if (addr_size == 0)
3366 addr_size = 4;
3367 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3368
3369 /* Read each remaining comp. units checking each as they are read. */
3370 while (stash->info_ptr < stash->info_ptr_end)
3371 {
3372 bfd_vma length;
3373 unsigned int offset_size = addr_size;
3374 bfd_byte *info_ptr_unit = stash->info_ptr;
3375
a50b1753 3376 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
bec42b15
NC
3377 /* A 0xffffff length is the DWARF3 way of indicating
3378 we use 64-bit offsets, instead of 32-bit offsets. */
5420f73d
L
3379 if (length == 0xffffffff)
3380 {
3381 offset_size = 8;
a50b1753 3382 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
5420f73d
L
3383 stash->info_ptr += 12;
3384 }
3385 /* A zero length is the IRIX way of indicating 64-bit offsets,
3386 mostly because the 64-bit length will generally fit in 32
3387 bits, and the endianness helps. */
3388 else if (length == 0)
3389 {
3390 offset_size = 8;
a50b1753 3391 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
5420f73d
L
3392 stash->info_ptr += 8;
3393 }
024b2372
CD
3394 /* In the absence of the hints above, we assume 32-bit DWARF2
3395 offsets even for targets with 64-bit addresses, because:
3396 a) most of the time these targets will not have generated
3397 more than 2Gb of debug info and so will not need 64-bit
3398 offsets,
3399 and
3400 b) if they do use 64-bit offsets but they are not using
3401 the size hints that are tested for above then they are
3402 not conforming to the DWARF3 standard anyway. */
5420f73d
L
3403 else if (addr_size == 8)
3404 {
024b2372 3405 offset_size = 4;
2d47a72c 3406 stash->info_ptr += 4;
5420f73d
L
3407 }
3408 else
3409 stash->info_ptr += 4;
3410
3411 if (length > 0)
3412 {
0d161102 3413 each = parse_comp_unit (stash, length, info_ptr_unit,
5420f73d 3414 offset_size);
d74e4b29
NS
3415 if (!each)
3416 /* The dwarf information is damaged, don't trust it any
3417 more. */
3418 break;
5420f73d
L
3419 stash->info_ptr += length;
3420
d74e4b29
NS
3421 if (stash->all_comp_units)
3422 stash->all_comp_units->prev_unit = each;
3423 else
3424 stash->last_comp_unit = each;
3425
3426 each->next_unit = stash->all_comp_units;
3427 stash->all_comp_units = each;
3428
3429 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3430 compilation units. If we don't have them (i.e.,
3431 unit->high == 0), we need to consult the line info table
3432 to see if a compilation unit contains the given
3433 address. */
3434 if (do_line)
3435 found = (((symbol->flags & BSF_FUNCTION) == 0
3436 || each->arange.high == 0
3437 || comp_unit_contains_address (each, addr))
3438 && comp_unit_find_line (each, symbol, addr,
3439 filename_ptr,
3440 linenumber_ptr,
3441 stash));
3442 else
3443 found = ((each->arange.high == 0
3444 || comp_unit_contains_address (each, addr))
3445 && comp_unit_find_nearest_line (each, addr,
3446 filename_ptr,
3447 functionname_ptr,
3448 linenumber_ptr,
3449 stash));
be04437d
AM
3450
3451 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3452 == stash->sec->size)
3453 {
a50b1753 3454 stash->sec = find_debug_info (stash->bfd_ptr, stash->sec);
be04437d
AM
3455 stash->sec_info_ptr = stash->info_ptr;
3456 }
3457
d74e4b29
NS
3458 if (found)
3459 goto done;
5420f73d
L
3460 }
3461 }
3462
d4c32a81
L
3463done:
3464 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3465 unset_sections (stash);
3466
3467 return found;
5420f73d
L
3468}
3469
bec42b15
NC
3470/* The DWARF2 version of find_nearest_line.
3471 Return TRUE if the line is found without error. */
3472
3473bfd_boolean
3474_bfd_dwarf2_find_nearest_line (bfd *abfd,
3475 asection *section,
3476 asymbol **symbols,
3477 bfd_vma offset,
3478 const char **filename_ptr,
3479 const char **functionname_ptr,
3480 unsigned int *linenumber_ptr,
3481 unsigned int addr_size,
3482 void **pinfo)
3483{
3484 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3485 functionname_ptr, linenumber_ptr, addr_size,
3486 pinfo);
3487}
3488
3489/* The DWARF2 version of find_line.
3490 Return TRUE if the line is found without error. */
3491
3492bfd_boolean
3493_bfd_dwarf2_find_line (bfd *abfd,
3494 asymbol **symbols,
3495 asymbol *symbol,
3496 const char **filename_ptr,
3497 unsigned int *linenumber_ptr,
3498 unsigned int addr_size,
3499 void **pinfo)
3500{
3501 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3502 NULL, linenumber_ptr, addr_size,
3503 pinfo);
3504}
3505
4ab527b0
FF
3506bfd_boolean
3507_bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3508 const char **filename_ptr,
3509 const char **functionname_ptr,
3510 unsigned int *linenumber_ptr,
3511 void **pinfo)
3512{
3513 struct dwarf2_debug *stash;
3514
a50b1753 3515 stash = (struct dwarf2_debug *) *pinfo;
4ab527b0
FF
3516 if (stash)
3517 {
3518 struct funcinfo *func = stash->inliner_chain;
bec42b15 3519
4ab527b0
FF
3520 if (func && func->caller_func)
3521 {
3522 *filename_ptr = func->caller_file;
3523 *functionname_ptr = func->caller_func->name;
3524 *linenumber_ptr = func->caller_line;
3525 stash->inliner_chain = func->caller_func;
bec42b15 3526 return TRUE;
4ab527b0
FF
3527 }
3528 }
3529
bec42b15 3530 return FALSE;
4ab527b0
FF
3531}
3532
35330cce
NC
3533void
3534_bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3535{
3536 struct comp_unit *each;
3537 struct dwarf2_debug *stash;
3538
3539 if (abfd == NULL || elf_tdata (abfd) == NULL)
3540 return;
3541
a50b1753 3542 stash = (struct dwarf2_debug *) elf_tdata (abfd)->dwarf2_find_line_info;
35330cce
NC
3543
3544 if (stash == NULL)
3545 return;
3546
3547 for (each = stash->all_comp_units; each; each = each->next_unit)
3548 {
34b5e0b2 3549 struct abbrev_info **abbrevs = each->abbrevs;
90b5b1a5
NC
3550 struct funcinfo *function_table = each->function_table;
3551 struct varinfo *variable_table = each->variable_table;
34b5e0b2 3552 size_t i;
35330cce 3553
34b5e0b2 3554 for (i = 0; i < ABBREV_HASH_SIZE; i++)
d8d1c398 3555 {
34b5e0b2 3556 struct abbrev_info *abbrev = abbrevs[i];
35330cce 3557
34b5e0b2 3558 while (abbrev)
d8d1c398 3559 {
34b5e0b2
NC
3560 free (abbrev->attrs);
3561 abbrev = abbrev->next;
d8d1c398
AM
3562 }
3563 }
35330cce
NC
3564
3565 if (each->line_table)
d8d1c398 3566 {
34b5e0b2
NC
3567 free (each->line_table->dirs);
3568 free (each->line_table->files);
d8d1c398 3569 }
90b5b1a5
NC
3570
3571 while (function_table)
3572 {
3573 if (function_table->file)
3574 {
3575 free (function_table->file);
3576 function_table->file = NULL;
3577 }
3578
3579 if (function_table->caller_file)
3580 {
3581 free (function_table->caller_file);
3582 function_table->caller_file = NULL;
3583 }
3584 function_table = function_table->prev_func;
3585 }
3586
3587 while (variable_table)
3588 {
3589 if (variable_table->file)
3590 {
3591 free (variable_table->file);
3592 variable_table->file = NULL;
3593 }
3594
3595 variable_table = variable_table->prev_var;
3596 }
35330cce
NC
3597 }
3598
5d0900eb
AM
3599 if (stash->dwarf_abbrev_buffer)
3600 free (stash->dwarf_abbrev_buffer);
3601 if (stash->dwarf_line_buffer)
3602 free (stash->dwarf_line_buffer);
3603 if (stash->dwarf_str_buffer)
3604 free (stash->dwarf_str_buffer);
3605 if (stash->dwarf_ranges_buffer)
3606 free (stash->dwarf_ranges_buffer);
3607 if (stash->info_ptr_memory)
3608 free (stash->info_ptr_memory);
35330cce 3609}
This page took 0.953534 seconds and 4 git commands to generate.