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