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