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