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