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