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