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