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