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