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