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