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