]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/dwarf2.c
Memory leak in dwarf2.c
[thirdparty/binutils-gdb.git] / bfd / dwarf2.c
CommitLineData
252b5132 1/* DWARF 2 support.
82704155 2 Copyright (C) 1994-2019 Free Software Foundation, Inc.
252b5132
RH
3
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5 (gavin@cygnus.com).
6
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
14
e2f6d277 15 This file is part of BFD.
252b5132 16
e2f6d277
NC
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
cd123cb7 19 the Free Software Foundation; either version 3 of the License, or (at
e2f6d277 20 your option) any later version.
252b5132 21
e2f6d277
NC
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
252b5132 26
e2f6d277
NC
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
cd123cb7
NC
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
252b5132 31
252b5132 32#include "sysdep.h"
3db64b00 33#include "bfd.h"
252b5132
RH
34#include "libiberty.h"
35#include "libbfd.h"
36#include "elf-bfd.h"
fa8f86ff 37#include "dwarf2.h"
3eb185c9 38#include "hashtab.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
90ed9b8b
AB
104 /* Pointer to the original bfd for which debug was loaded. This is what
105 we use to compare and so check that the cached debug data is still
106 valid - it saves having to possibly dereference the gnu_debuglink each
107 time. */
108 bfd *orig_bfd;
109
0d161102
NC
110 /* Pointer to the bfd, section and address of the beginning of the
111 section. The bfd might be different than expected because of
112 gnu_debuglink sections. */
a50b1753 113 bfd *bfd_ptr;
f075ee0c
AM
114 asection *sec;
115 bfd_byte *sec_info_ptr;
f2363ce5 116
95e34fb4
NC
117 /* Support for alternate debug info sections created by the DWZ utility:
118 This includes a pointer to an alternate bfd which contains *extra*,
119 possibly duplicate debug sections, and pointers to the loaded
120 .debug_str and .debug_info sections from this bfd. */
07d6d2b8
AM
121 bfd * alt_bfd_ptr;
122 bfd_byte * alt_dwarf_str_buffer;
123 bfd_size_type alt_dwarf_str_size;
124 bfd_byte * alt_dwarf_info_buffer;
125 bfd_size_type alt_dwarf_info_size;
95e34fb4 126
aaf30c25
CS
127 /* A pointer to the memory block allocated for info_ptr. Neither
128 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
1b86808a 129 beginning of the malloc block. */
aaf30c25
CS
130 bfd_byte *info_ptr_memory;
131
f2363ce5 132 /* Pointer to the symbol table. */
f075ee0c 133 asymbol **syms;
f2363ce5 134
a092b084 135 /* Pointer to the .debug_abbrev section loaded into memory. */
f075ee0c 136 bfd_byte *dwarf_abbrev_buffer;
252b5132 137
a092b084 138 /* Length of the loaded .debug_abbrev section. */
3076cd1f 139 bfd_size_type dwarf_abbrev_size;
69dd2e2d
RH
140
141 /* Buffer for decode_line_info. */
f075ee0c 142 bfd_byte *dwarf_line_buffer;
ccdb16fc
JW
143
144 /* Length of the loaded .debug_line section. */
3076cd1f 145 bfd_size_type dwarf_line_size;
d03ba2a1
JJ
146
147 /* Pointer to the .debug_str section loaded into memory. */
f075ee0c 148 bfd_byte *dwarf_str_buffer;
d03ba2a1
JJ
149
150 /* Length of the loaded .debug_str section. */
3076cd1f 151 bfd_size_type dwarf_str_size;
a13afe8e 152
0041f7df
JK
153 /* Pointer to the .debug_line_str section loaded into memory. */
154 bfd_byte *dwarf_line_str_buffer;
155
156 /* Length of the loaded .debug_line_str section. */
157 bfd_size_type dwarf_line_str_size;
158
089e3718 159 /* Pointer to the .debug_ranges section loaded into memory. */
a13afe8e
FF
160 bfd_byte *dwarf_ranges_buffer;
161
089e3718 162 /* Length of the loaded .debug_ranges section. */
3076cd1f 163 bfd_size_type dwarf_ranges_size;
4ab527b0
FF
164
165 /* If the most recent call to bfd_find_nearest_line was given an
166 address in an inlined function, preserve a pointer into the
167 calling chain for subsequent calls to bfd_find_inliner_info to
089e3718 168 use. */
4ab527b0 169 struct funcinfo *inliner_chain;
d4c32a81 170
cd0449ab
AM
171 /* Section VMAs at the time the stash was built. */
172 bfd_vma *sec_vma;
d7f848c3
NC
173 /* Number of sections in the SEC_VMA table. */
174 unsigned int sec_vma_count;
cd0449ab 175
5609a71e 176 /* Number of sections whose VMA we must adjust. */
93ee1e36 177 int adjusted_section_count;
d4c32a81 178
5609a71e
DJ
179 /* Array of sections with adjusted VMA. */
180 struct adjusted_section *adjusted_sections;
bd210d54
NC
181
182 /* Number of times find_line is called. This is used in
183 the heuristic for enabling the info hash tables. */
184 int info_hash_count;
185
186#define STASH_INFO_HASH_TRIGGER 100
187
188 /* Hash table mapping symbol names to function infos. */
189 struct info_hash_table *funcinfo_hash_table;
190
191 /* Hash table mapping symbol names to variable infos. */
192 struct info_hash_table *varinfo_hash_table;
193
194 /* Head of comp_unit list in the last hash table update. */
195 struct comp_unit *hash_units_head;
196
197 /* Status of info hash. */
198 int info_hash_status;
07d6d2b8
AM
199#define STASH_INFO_HASH_OFF 0
200#define STASH_INFO_HASH_ON 1
bd210d54 201#define STASH_INFO_HASH_DISABLED 2
1c37913d
AM
202
203 /* True if we opened bfd_ptr. */
204 bfd_boolean close_on_cleanup;
252b5132
RH
205};
206
a092b084
NC
207struct arange
208{
f623be2b
RH
209 struct arange *next;
210 bfd_vma low;
211 bfd_vma high;
212};
252b5132 213
252b5132 214/* A minimal decoding of DWARF2 compilation units. We only decode
a092b084 215 what's needed to get to the line number information. */
252b5132 216
a092b084
NC
217struct comp_unit
218{
219 /* Chain the previously read compilation units. */
f075ee0c 220 struct comp_unit *next_unit;
252b5132 221
bd210d54
NC
222 /* Likewise, chain the compilation unit read after this one.
223 The comp units are stored in reversed reading order. */
224 struct comp_unit *prev_unit;
225
2ae727ad 226 /* Keep the bfd convenient (for memory allocation). */
f075ee0c 227 bfd *abfd;
252b5132 228
709d67f1
AM
229 /* The lowest and highest addresses contained in this compilation
230 unit as specified in the compilation unit header. */
231 struct arange arange;
252b5132 232
a092b084 233 /* The DW_AT_name attribute (for error messages). */
f075ee0c 234 char *name;
252b5132 235
a092b084 236 /* The abbrev hash table. */
f075ee0c 237 struct abbrev_info **abbrevs;
252b5132 238
e00e8198
AM
239 /* DW_AT_language. */
240 int lang;
241
a092b084 242 /* Note that an error was found by comp_unit_find_nearest_line. */
252b5132
RH
243 int error;
244
a092b084 245 /* The DW_AT_comp_dir attribute. */
f075ee0c 246 char *comp_dir;
252b5132 247
b34976b6 248 /* TRUE if there is a line number table associated with this comp. unit. */
252b5132 249 int stmtlist;
98591c73 250
c0c28ab8
L
251 /* Pointer to the current comp_unit so that we can find a given entry
252 by its reference. */
f075ee0c 253 bfd_byte *info_ptr_unit;
c0c28ab8 254
a092b084 255 /* The offset into .debug_line of the line number table. */
252b5132
RH
256 unsigned long line_offset;
257
a092b084 258 /* Pointer to the first child die for the comp unit. */
f075ee0c 259 bfd_byte *first_child_die_ptr;
252b5132 260
a092b084 261 /* The end of the comp unit. */
f075ee0c 262 bfd_byte *end_ptr;
252b5132 263
a092b084 264 /* The decoded line number, NULL if not yet decoded. */
f075ee0c 265 struct line_info_table *line_table;
252b5132 266
a092b084 267 /* A list of the functions found in this comp. unit. */
f075ee0c 268 struct funcinfo *function_table;
252b5132 269
089e3718
IT
270 /* A table of function information references searchable by address. */
271 struct lookup_funcinfo *lookup_funcinfo_table;
272
273 /* Number of functions in the function_table and sorted_function_table. */
274 bfd_size_type number_of_functions;
275
5420f73d
L
276 /* A list of the variables found in this comp. unit. */
277 struct varinfo *variable_table;
278
d03ba2a1
JJ
279 /* Pointer to dwarf2_debug structure. */
280 struct dwarf2_debug *stash;
281
5609a71e
DJ
282 /* DWARF format version for this unit - from unit header. */
283 int version;
284
a092b084 285 /* Address size for this unit - from unit header. */
252b5132 286 unsigned char addr_size;
d03ba2a1
JJ
287
288 /* Offset size for this unit - from unit header. */
289 unsigned char offset_size;
a13afe8e
FF
290
291 /* Base address for this unit - from DW_AT_low_pc attribute of
292 DW_TAG_compile_unit DIE */
293 bfd_vma base_address;
bd210d54
NC
294
295 /* TRUE if symbols are cached in hash table for faster lookup by name. */
296 bfd_boolean cached;
252b5132
RH
297};
298
a7b97311
AM
299/* This data structure holds the information of an abbrev. */
300struct abbrev_info
301{
302 unsigned int number; /* Number identifying abbrev. */
303 enum dwarf_tag tag; /* DWARF tag. */
304 int has_children; /* Boolean. */
305 unsigned int num_attrs; /* Number of attributes. */
306 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
307 struct abbrev_info *next; /* Next in chain. */
308};
309
310struct attr_abbrev
311{
312 enum dwarf_attribute name;
313 enum dwarf_form form;
0041f7df 314 bfd_vma implicit_const;
a7b97311
AM
315};
316
4a114e3e
L
317/* Map of uncompressed DWARF debug section name to compressed one. It
318 is terminated by NULL uncompressed_name. */
319
e4c93b56 320const struct dwarf_debug_section dwarf_debug_sections[] =
4a114e3e
L
321{
322 { ".debug_abbrev", ".zdebug_abbrev" },
323 { ".debug_aranges", ".zdebug_aranges" },
324 { ".debug_frame", ".zdebug_frame" },
325 { ".debug_info", ".zdebug_info" },
95e34fb4 326 { ".debug_info", ".zdebug_info" },
4a114e3e
L
327 { ".debug_line", ".zdebug_line" },
328 { ".debug_loc", ".zdebug_loc" },
329 { ".debug_macinfo", ".zdebug_macinfo" },
4ccf1e31 330 { ".debug_macro", ".zdebug_macro" },
4a114e3e
L
331 { ".debug_pubnames", ".zdebug_pubnames" },
332 { ".debug_pubtypes", ".zdebug_pubtypes" },
333 { ".debug_ranges", ".zdebug_ranges" },
334 { ".debug_static_func", ".zdebug_static_func" },
335 { ".debug_static_vars", ".zdebug_static_vars" },
336 { ".debug_str", ".zdebug_str", },
95e34fb4 337 { ".debug_str", ".zdebug_str", },
0041f7df 338 { ".debug_line_str", ".zdebug_line_str", },
4a114e3e
L
339 { ".debug_types", ".zdebug_types" },
340 /* GNU DWARF 1 extensions */
341 { ".debug_sfnames", ".zdebug_sfnames" },
342 { ".debug_srcinfo", ".zebug_srcinfo" },
343 /* SGI/MIPS DWARF 2 extensions */
344 { ".debug_funcnames", ".zdebug_funcnames" },
345 { ".debug_typenames", ".zdebug_typenames" },
346 { ".debug_varnames", ".zdebug_varnames" },
347 { ".debug_weaknames", ".zdebug_weaknames" },
348 { NULL, NULL },
349};
350
67ce483b 351/* NB/ Numbers in this enum must match up with indices
95e34fb4 352 into the dwarf_debug_sections[] array above. */
4a114e3e
L
353enum dwarf_debug_section_enum
354{
355 debug_abbrev = 0,
356 debug_aranges,
357 debug_frame,
358 debug_info,
95e34fb4 359 debug_info_alt,
4a114e3e
L
360 debug_line,
361 debug_loc,
362 debug_macinfo,
4ccf1e31 363 debug_macro,
4a114e3e
L
364 debug_pubnames,
365 debug_pubtypes,
366 debug_ranges,
367 debug_static_func,
368 debug_static_vars,
369 debug_str,
95e34fb4 370 debug_str_alt,
0041f7df 371 debug_line_str,
4a114e3e
L
372 debug_types,
373 debug_sfnames,
374 debug_srcinfo,
375 debug_funcnames,
376 debug_typenames,
377 debug_varnames,
0041f7df
JK
378 debug_weaknames,
379 debug_max
4a114e3e
L
380};
381
0041f7df
JK
382/* A static assertion. */
383extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
384 == debug_max + 1 ? 1 : -1];
385
a7b97311
AM
386#ifndef ABBREV_HASH_SIZE
387#define ABBREV_HASH_SIZE 121
388#endif
389#ifndef ATTR_ALLOC_CHUNK
390#define ATTR_ALLOC_CHUNK 4
391#endif
392
bd210d54
NC
393/* Variable and function hash tables. This is used to speed up look-up
394 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
395 In order to share code between variable and function infos, we use
396 a list of untyped pointer for all variable/function info associated with
397 a symbol. We waste a bit of memory for list with one node but that
398 simplifies the code. */
399
400struct info_list_node
401{
402 struct info_list_node *next;
403 void *info;
404};
405
406/* Info hash entry. */
407struct info_hash_entry
408{
409 struct bfd_hash_entry root;
410 struct info_list_node *head;
411};
412
413struct info_hash_table
414{
415 struct bfd_hash_table base;
416};
417
089e3718 418/* Function to create a new entry in info hash table. */
bd210d54
NC
419
420static struct bfd_hash_entry *
421info_hash_table_newfunc (struct bfd_hash_entry *entry,
422 struct bfd_hash_table *table,
423 const char *string)
424{
425 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
426
427 /* Allocate the structure if it has not already been allocated by a
428 derived class. */
429 if (ret == NULL)
430 {
a50b1753 431 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
93ee1e36 432 sizeof (* ret));
bd210d54
NC
433 if (ret == NULL)
434 return NULL;
435 }
436
437 /* Call the allocation method of the base class. */
438 ret = ((struct info_hash_entry *)
2d47a72c 439 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
bd210d54
NC
440
441 /* Initialize the local fields here. */
442 if (ret)
443 ret->head = NULL;
444
445 return (struct bfd_hash_entry *) ret;
446}
447
448/* Function to create a new info hash table. It returns a pointer to the
449 newly created table or NULL if there is any error. We need abfd
450 solely for memory allocation. */
451
452static struct info_hash_table *
453create_info_hash_table (bfd *abfd)
454{
455 struct info_hash_table *hash_table;
456
a2a50954
AM
457 hash_table = ((struct info_hash_table *)
458 bfd_alloc (abfd, sizeof (struct info_hash_table)));
bd210d54
NC
459 if (!hash_table)
460 return hash_table;
461
462 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
463 sizeof (struct info_hash_entry)))
464 {
465 bfd_release (abfd, hash_table);
466 return NULL;
467 }
468
469 return hash_table;
470}
471
472/* Insert an info entry into an info hash table. We do not check of
473 duplicate entries. Also, the caller need to guarantee that the
474 right type of info in inserted as info is passed as a void* pointer.
475 This function returns true if there is no error. */
476
477static bfd_boolean
478insert_info_hash_table (struct info_hash_table *hash_table,
479 const char *key,
480 void *info,
481 bfd_boolean copy_p)
482{
483 struct info_hash_entry *entry;
484 struct info_list_node *node;
485
486 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
487 key, TRUE, copy_p);
488 if (!entry)
489 return FALSE;
490
a50b1753 491 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
93ee1e36 492 sizeof (*node));
bd210d54
NC
493 if (!node)
494 return FALSE;
495
496 node->info = info;
497 node->next = entry->head;
498 entry->head = node;
499
500 return TRUE;
501}
502
503/* Look up an info entry list from an info hash table. Return NULL
089e3718 504 if there is none. */
bd210d54
NC
505
506static struct info_list_node *
507lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
508{
509 struct info_hash_entry *entry;
510
511 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
512 FALSE, FALSE);
513 return entry ? entry->head : NULL;
514}
515
1b315056 516/* Read a section into its appropriate place in the dwarf2_debug
dc80fd5c 517 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
1b315056 518 not NULL, use bfd_simple_get_relocated_section_contents to read the
dc80fd5c
NC
519 section contents, otherwise use bfd_get_section_contents. Fail if
520 the located section does not contain at least OFFSET bytes. */
1b315056
CS
521
522static bfd_boolean
07d6d2b8 523read_section (bfd * abfd,
fc28f9aa 524 const struct dwarf_debug_section *sec,
dc80fd5c
NC
525 asymbol ** syms,
526 bfd_uint64_t offset,
527 bfd_byte ** section_buffer,
528 bfd_size_type * section_size)
1b315056
CS
529{
530 asection *msec;
fc28f9aa 531 const char *section_name = sec->uncompressed_name;
4b04bba2 532 bfd_byte *contents = *section_buffer;
30838132 533 bfd_size_type amt;
1b315056 534
95e34fb4 535 /* The section may have already been read. */
4b04bba2 536 if (contents == NULL)
1b315056 537 {
53638231 538 msec = bfd_get_section_by_name (abfd, section_name);
4a114e3e 539 if (! msec)
53638231 540 {
fc28f9aa 541 section_name = sec->compressed_name;
93ee1e36
AM
542 if (section_name != NULL)
543 msec = bfd_get_section_by_name (abfd, section_name);
53638231
AS
544 }
545 if (! msec)
2d47a72c 546 {
9793eb77 547 _bfd_error_handler (_("DWARF error: can't find %s section."),
4eca0228 548 sec->uncompressed_name);
2d47a72c
DJ
549 bfd_set_error (bfd_error_bad_value);
550 return FALSE;
551 }
53638231 552
bc664799 553 *section_size = msec->rawsize ? msec->rawsize : msec->size;
4b04bba2
AM
554 /* Paranoia - alloc one extra so that we can make sure a string
555 section is NUL terminated. */
30838132
AM
556 amt = *section_size + 1;
557 if (amt == 0)
558 {
559 bfd_set_error (bfd_error_no_memory);
560 return FALSE;
561 }
562 contents = (bfd_byte *) bfd_malloc (amt);
4b04bba2
AM
563 if (contents == NULL)
564 return FALSE;
565 if (syms
566 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
567 syms)
568 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
8c2ccebd 569 {
4b04bba2
AM
570 free (contents);
571 return FALSE;
e4f27230 572 }
4b04bba2
AM
573 contents[*section_size] = 0;
574 *section_buffer = contents;
1b315056
CS
575 }
576
577 /* It is possible to get a bad value for the offset into the section
dc80fd5c 578 that the client wants. Validate it here to avoid trouble later. */
1b315056
CS
579 if (offset != 0 && offset >= *section_size)
580 {
695344c0 581 /* xgettext: c-format */
9793eb77
AM
582 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
583 " greater than or equal to %s size (%" PRIu64 ")"),
8979927a 584 (uint64_t) offset, section_name,
2dcf00ce 585 (uint64_t) *section_size);
1b315056
CS
586 bfd_set_error (bfd_error_bad_value);
587 return FALSE;
588 }
589
590 return TRUE;
591}
592
a092b084 593/* Read dwarf information from a buffer. */
252b5132
RH
594
595static unsigned int
dbb3fbbb 596read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
252b5132 597{
dbb3fbbb
NC
598 if (buf + 1 > end)
599 return 0;
818a27ac 600 return bfd_get_8 (abfd, buf);
252b5132
RH
601}
602
603static int
dbb3fbbb 604read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
252b5132 605{
dbb3fbbb
NC
606 if (buf + 1 > end)
607 return 0;
818a27ac 608 return bfd_get_signed_8 (abfd, buf);
252b5132
RH
609}
610
611static unsigned int
dbb3fbbb 612read_2_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
252b5132 613{
dbb3fbbb
NC
614 if (buf + 2 > end)
615 return 0;
818a27ac 616 return bfd_get_16 (abfd, buf);
252b5132
RH
617}
618
252b5132 619static unsigned int
dbb3fbbb 620read_4_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
252b5132 621{
dbb3fbbb
NC
622 if (buf + 4 > end)
623 return 0;
818a27ac 624 return bfd_get_32 (abfd, buf);
252b5132
RH
625}
626
8ce8c090 627static bfd_uint64_t
dbb3fbbb 628read_8_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
252b5132 629{
dbb3fbbb
NC
630 if (buf + 8 > end)
631 return 0;
818a27ac 632 return bfd_get_64 (abfd, buf);
252b5132
RH
633}
634
f075ee0c 635static bfd_byte *
12c96342
NC
636read_n_bytes (bfd_byte * buf,
637 bfd_byte * end,
638 struct dwarf_block * block)
252b5132 639{
12c96342
NC
640 unsigned int size = block->size;
641 bfd_byte * block_end = buf + size;
642
643 if (block_end > end || block_end < buf)
644 {
645 block->data = NULL;
646 block->size = 0;
647 return end;
648 }
649 else
650 {
651 block->data = buf;
652 return block_end;
653 }
252b5132
RH
654}
655
dbb3fbbb
NC
656/* Scans a NUL terminated string starting at BUF, returning a pointer to it.
657 Returns the number of characters in the string, *including* the NUL byte,
658 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
659 at or beyond BUF_END will not be read. Returns NULL if there was a
660 problem, or if the string is empty. */
661
252b5132 662static char *
07d6d2b8
AM
663read_string (bfd * abfd ATTRIBUTE_UNUSED,
664 bfd_byte * buf,
665 bfd_byte * buf_end,
dbb3fbbb 666 unsigned int * bytes_read_ptr)
252b5132 667{
dbb3fbbb
NC
668 bfd_byte *str = buf;
669
670 if (buf >= buf_end)
671 {
672 * bytes_read_ptr = 0;
673 return NULL;
674 }
dc80fd5c 675
f075ee0c 676 if (*str == '\0')
252b5132 677 {
dbb3fbbb 678 * bytes_read_ptr = 1;
252b5132
RH
679 return NULL;
680 }
98591c73 681
dbb3fbbb
NC
682 while (buf < buf_end)
683 if (* buf ++ == 0)
684 {
685 * bytes_read_ptr = buf - str;
686 return (char *) str;
687 }
688
689 * bytes_read_ptr = buf - str;
690 return NULL;
252b5132
RH
691}
692
dbb3fbbb
NC
693/* Reads an offset from BUF and then locates the string at this offset
694 inside the debug string section. Returns a pointer to the string.
695 Returns the number of bytes read from BUF, *not* the length of the string,
696 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
697 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
698 a problem, or if the string is empty. Does not check for NUL termination
699 of the string. */
dc80fd5c 700
d03ba2a1 701static char *
dc80fd5c 702read_indirect_string (struct comp_unit * unit,
07d6d2b8
AM
703 bfd_byte * buf,
704 bfd_byte * buf_end,
705 unsigned int * bytes_read_ptr)
d03ba2a1 706{
8ce8c090 707 bfd_uint64_t offset;
d03ba2a1 708 struct dwarf2_debug *stash = unit->stash;
f075ee0c 709 char *str;
d03ba2a1 710
dbb3fbbb
NC
711 if (buf + unit->offset_size > buf_end)
712 {
713 * bytes_read_ptr = 0;
714 return NULL;
715 }
62f8d217 716
d03ba2a1 717 if (unit->offset_size == 4)
dbb3fbbb 718 offset = read_4_bytes (unit->abfd, buf, buf_end);
d03ba2a1 719 else
dbb3fbbb 720 offset = read_8_bytes (unit->abfd, buf, buf_end);
dc80fd5c 721
d03ba2a1
JJ
722 *bytes_read_ptr = unit->offset_size;
723
fc28f9aa 724 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
93ee1e36 725 stash->syms, offset,
9e32b19f 726 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
dc80fd5c 727 return NULL;
d03ba2a1 728
dbb3fbbb
NC
729 if (offset >= stash->dwarf_str_size)
730 return NULL;
f075ee0c
AM
731 str = (char *) stash->dwarf_str_buffer + offset;
732 if (*str == '\0')
d03ba2a1 733 return NULL;
f075ee0c 734 return str;
d03ba2a1
JJ
735}
736
0041f7df
JK
737/* Like read_indirect_string but from .debug_line_str section. */
738
739static char *
740read_indirect_line_string (struct comp_unit * unit,
07d6d2b8
AM
741 bfd_byte * buf,
742 bfd_byte * buf_end,
0041f7df
JK
743 unsigned int * bytes_read_ptr)
744{
745 bfd_uint64_t offset;
746 struct dwarf2_debug *stash = unit->stash;
747 char *str;
748
749 if (buf + unit->offset_size > buf_end)
750 {
751 * bytes_read_ptr = 0;
752 return NULL;
753 }
754
755 if (unit->offset_size == 4)
756 offset = read_4_bytes (unit->abfd, buf, buf_end);
757 else
758 offset = read_8_bytes (unit->abfd, buf, buf_end);
759
760 *bytes_read_ptr = unit->offset_size;
761
762 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
763 stash->syms, offset,
764 &stash->dwarf_line_str_buffer,
765 &stash->dwarf_line_str_size))
766 return NULL;
767
768 if (offset >= stash->dwarf_line_str_size)
769 return NULL;
770 str = (char *) stash->dwarf_line_str_buffer + offset;
771 if (*str == '\0')
772 return NULL;
773 return str;
774}
775
95e34fb4 776/* Like read_indirect_string but uses a .debug_str located in
93ee1e36 777 an alternate file pointed to by the .gnu_debugaltlink section.
95e34fb4
NC
778 Used to impement DW_FORM_GNU_strp_alt. */
779
780static char *
781read_alt_indirect_string (struct comp_unit * unit,
07d6d2b8
AM
782 bfd_byte * buf,
783 bfd_byte * buf_end,
95e34fb4
NC
784 unsigned int * bytes_read_ptr)
785{
786 bfd_uint64_t offset;
787 struct dwarf2_debug *stash = unit->stash;
788 char *str;
789
dbb3fbbb
NC
790 if (buf + unit->offset_size > buf_end)
791 {
792 * bytes_read_ptr = 0;
793 return NULL;
794 }
62f8d217 795
95e34fb4 796 if (unit->offset_size == 4)
dbb3fbbb 797 offset = read_4_bytes (unit->abfd, buf, buf_end);
95e34fb4 798 else
dbb3fbbb 799 offset = read_8_bytes (unit->abfd, buf, buf_end);
95e34fb4
NC
800
801 *bytes_read_ptr = unit->offset_size;
802
803 if (stash->alt_bfd_ptr == NULL)
804 {
805 bfd * debug_bfd;
806 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
807
808 if (debug_filename == NULL)
809 return NULL;
810
811 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
812 || ! bfd_check_format (debug_bfd, bfd_object))
813 {
814 if (debug_bfd)
815 bfd_close (debug_bfd);
816
817 /* FIXME: Should we report our failure to follow the debuglink ? */
818 free (debug_filename);
819 return NULL;
820 }
821 stash->alt_bfd_ptr = debug_bfd;
822 }
62f8d217 823
95e34fb4
NC
824 if (! read_section (unit->stash->alt_bfd_ptr,
825 stash->debug_sections + debug_str_alt,
826 NULL, /* FIXME: Do we need to load alternate symbols ? */
827 offset,
828 &stash->alt_dwarf_str_buffer,
829 &stash->alt_dwarf_str_size))
830 return NULL;
831
dbb3fbbb
NC
832 if (offset >= stash->alt_dwarf_str_size)
833 return NULL;
95e34fb4
NC
834 str = (char *) stash->alt_dwarf_str_buffer + offset;
835 if (*str == '\0')
836 return NULL;
837
838 return str;
839}
840
841/* Resolve an alternate reference from UNIT at OFFSET.
842 Returns a pointer into the loaded alternate CU upon success
843 or NULL upon failure. */
844
845static bfd_byte *
846read_alt_indirect_ref (struct comp_unit * unit,
847 bfd_uint64_t offset)
848{
849 struct dwarf2_debug *stash = unit->stash;
850
851 if (stash->alt_bfd_ptr == NULL)
852 {
853 bfd * debug_bfd;
854 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
855
856 if (debug_filename == NULL)
857 return FALSE;
858
859 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
860 || ! bfd_check_format (debug_bfd, bfd_object))
861 {
862 if (debug_bfd)
863 bfd_close (debug_bfd);
864
865 /* FIXME: Should we report our failure to follow the debuglink ? */
866 free (debug_filename);
867 return NULL;
868 }
869 stash->alt_bfd_ptr = debug_bfd;
870 }
62f8d217 871
95e34fb4
NC
872 if (! read_section (unit->stash->alt_bfd_ptr,
873 stash->debug_sections + debug_info_alt,
874 NULL, /* FIXME: Do we need to load alternate symbols ? */
875 offset,
876 &stash->alt_dwarf_info_buffer,
877 &stash->alt_dwarf_info_size))
878 return NULL;
879
dbb3fbbb
NC
880 if (offset >= stash->alt_dwarf_info_size)
881 return NULL;
95e34fb4
NC
882 return stash->alt_dwarf_info_buffer + offset;
883}
884
8ce8c090 885static bfd_uint64_t
dbb3fbbb 886read_address (struct comp_unit *unit, bfd_byte *buf, bfd_byte * buf_end)
252b5132 887{
fa15f18d
AM
888 int signed_vma = 0;
889
890 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
891 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
0af4cd7c 892
dbb3fbbb
NC
893 if (buf + unit->addr_size > buf_end)
894 return 0;
895
0af4cd7c
PK
896 if (signed_vma)
897 {
898 switch (unit->addr_size)
899 {
900 case 8:
901 return bfd_get_signed_64 (unit->abfd, buf);
902 case 4:
903 return bfd_get_signed_32 (unit->abfd, buf);
904 case 2:
905 return bfd_get_signed_16 (unit->abfd, buf);
906 default:
907 abort ();
908 }
909 }
910 else
252b5132 911 {
0af4cd7c
PK
912 switch (unit->addr_size)
913 {
914 case 8:
915 return bfd_get_64 (unit->abfd, buf);
916 case 4:
917 return bfd_get_32 (unit->abfd, buf);
918 case 2:
919 return bfd_get_16 (unit->abfd, buf);
920 default:
921 abort ();
922 }
252b5132 923 }
252b5132
RH
924}
925
252b5132
RH
926/* Lookup an abbrev_info structure in the abbrev hash table. */
927
928static struct abbrev_info *
818a27ac 929lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
252b5132
RH
930{
931 unsigned int hash_number;
932 struct abbrev_info *abbrev;
933
934 hash_number = number % ABBREV_HASH_SIZE;
935 abbrev = abbrevs[hash_number];
936
937 while (abbrev)
938 {
939 if (abbrev->number == number)
940 return abbrev;
941 else
942 abbrev = abbrev->next;
943 }
98591c73 944
252b5132
RH
945 return NULL;
946}
947
948/* In DWARF version 2, the description of the debugging information is
949 stored in a separate .debug_abbrev section. Before we read any
950 dies from a section we read in all abbreviations and install them
951 in a hash table. */
952
953static struct abbrev_info**
8ce8c090 954read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
252b5132
RH
955{
956 struct abbrev_info **abbrevs;
f075ee0c 957 bfd_byte *abbrev_ptr;
dbb3fbbb 958 bfd_byte *abbrev_end;
252b5132
RH
959 struct abbrev_info *cur_abbrev;
960 unsigned int abbrev_number, bytes_read, abbrev_name;
961 unsigned int abbrev_form, hash_number;
dc810e39 962 bfd_size_type amt;
252b5132 963
fc28f9aa 964 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
93ee1e36 965 stash->syms, offset,
9e32b19f 966 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
8af6b354 967 return NULL;
252b5132 968
dbb3fbbb
NC
969 if (offset >= stash->dwarf_abbrev_size)
970 return NULL;
971
dc810e39 972 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
a50b1753 973 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
8af6b354
AM
974 if (abbrevs == NULL)
975 return NULL;
252b5132
RH
976
977 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
dbb3fbbb 978 abbrev_end = stash->dwarf_abbrev_buffer + stash->dwarf_abbrev_size;
c7c3d11b
PA
979 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
980 FALSE, abbrev_end);
252b5132
RH
981 abbrev_ptr += bytes_read;
982
a092b084 983 /* Loop until we reach an abbrev number of 0. */
252b5132
RH
984 while (abbrev_number)
985 {
dc810e39 986 amt = sizeof (struct abbrev_info);
a50b1753 987 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
8af6b354
AM
988 if (cur_abbrev == NULL)
989 return NULL;
252b5132 990
a092b084 991 /* Read in abbrev header. */
252b5132 992 cur_abbrev->number = abbrev_number;
d45913a0 993 cur_abbrev->tag = (enum dwarf_tag)
4265548c
PA
994 _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
995 FALSE, abbrev_end);
252b5132 996 abbrev_ptr += bytes_read;
dbb3fbbb 997 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr, abbrev_end);
252b5132
RH
998 abbrev_ptr += 1;
999
a092b084 1000 /* Now read in declarations. */
0041f7df 1001 for (;;)
252b5132 1002 {
0041f7df
JK
1003 /* Initialize it just to avoid a GCC false warning. */
1004 bfd_vma implicit_const = -1;
1005
1006 abbrev_name = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1007 FALSE, abbrev_end);
1008 abbrev_ptr += bytes_read;
1009 abbrev_form = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1010 FALSE, abbrev_end);
1011 abbrev_ptr += bytes_read;
1012 if (abbrev_form == DW_FORM_implicit_const)
1013 {
1014 implicit_const = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1015 &bytes_read, TRUE,
1016 abbrev_end);
1017 abbrev_ptr += bytes_read;
1018 }
1019
1020 if (abbrev_name == 0)
1021 break;
1022
252b5132
RH
1023 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1024 {
35330cce
NC
1025 struct attr_abbrev *tmp;
1026
dc810e39
AM
1027 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1028 amt *= sizeof (struct attr_abbrev);
a50b1753 1029 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
35330cce 1030 if (tmp == NULL)
d8d1c398
AM
1031 {
1032 size_t i;
1033
1034 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1035 {
1036 struct abbrev_info *abbrev = abbrevs[i];
1037
1038 while (abbrev)
1039 {
34b5e0b2
NC
1040 free (abbrev->attrs);
1041 abbrev = abbrev->next;
d8d1c398
AM
1042 }
1043 }
1044 return NULL;
1045 }
35330cce 1046 cur_abbrev->attrs = tmp;
252b5132 1047 }
98591c73 1048
d45913a0
DA
1049 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1050 = (enum dwarf_attribute) abbrev_name;
0041f7df 1051 cur_abbrev->attrs[cur_abbrev->num_attrs].form
d45913a0 1052 = (enum dwarf_form) abbrev_form;
0041f7df
JK
1053 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1054 = implicit_const;
1055 ++cur_abbrev->num_attrs;
252b5132
RH
1056 }
1057
1058 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1059 cur_abbrev->next = abbrevs[hash_number];
1060 abbrevs[hash_number] = cur_abbrev;
1061
1062 /* Get next abbreviation.
e82ce529 1063 Under Irix6 the abbreviations for a compilation unit are not
252b5132
RH
1064 always properly terminated with an abbrev number of 0.
1065 Exit loop if we encounter an abbreviation which we have
1066 already read (which means we are about to read the abbreviations
1067 for the next compile unit) or if the end of the abbreviation
1068 table is reached. */
1069 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
d8d1c398 1070 >= stash->dwarf_abbrev_size)
252b5132 1071 break;
4265548c
PA
1072 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1073 &bytes_read, FALSE, abbrev_end);
252b5132 1074 abbrev_ptr += bytes_read;
dbb3fbbb 1075 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
252b5132
RH
1076 break;
1077 }
1078
1079 return abbrevs;
1080}
1081
60d77146
NC
1082/* Returns true if the form is one which has a string value. */
1083
1084static inline bfd_boolean
1085is_str_attr (enum dwarf_form form)
1086{
0041f7df
JK
1087 return (form == DW_FORM_string || form == DW_FORM_strp
1088 || form == DW_FORM_line_strp || form == DW_FORM_GNU_strp_alt);
60d77146
NC
1089}
1090
dbb3fbbb
NC
1091/* Read and fill in the value of attribute ATTR as described by FORM.
1092 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1093 Returns an updated INFO_PTR taking into account the amount of data read. */
252b5132 1094
f075ee0c 1095static bfd_byte *
dbb3fbbb 1096read_attribute_value (struct attribute * attr,
07d6d2b8
AM
1097 unsigned form,
1098 bfd_vma implicit_const,
dbb3fbbb 1099 struct comp_unit * unit,
07d6d2b8
AM
1100 bfd_byte * info_ptr,
1101 bfd_byte * info_ptr_end)
252b5132
RH
1102{
1103 bfd *abfd = unit->abfd;
1104 unsigned int bytes_read;
1105 struct dwarf_block *blk;
dc810e39 1106 bfd_size_type amt;
252b5132 1107
a97fbc7e 1108 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
dbb3fbbb 1109 {
9793eb77 1110 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
dbb3fbbb
NC
1111 bfd_set_error (bfd_error_bad_value);
1112 return info_ptr;
1113 }
1114
d45913a0 1115 attr->form = (enum dwarf_form) form;
98591c73 1116
cf716c56 1117 switch (form)
252b5132 1118 {
252b5132 1119 case DW_FORM_ref_addr:
5609a71e
DJ
1120 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1121 DWARF3. */
c07cbdd7 1122 if (unit->version == 3 || unit->version == 4)
5609a71e
DJ
1123 {
1124 if (unit->offset_size == 4)
dbb3fbbb 1125 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
5609a71e 1126 else
dbb3fbbb 1127 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
5609a71e
DJ
1128 info_ptr += unit->offset_size;
1129 break;
1130 }
1131 /* FALLTHROUGH */
1132 case DW_FORM_addr:
dbb3fbbb 1133 attr->u.val = read_address (unit, info_ptr, info_ptr_end);
252b5132
RH
1134 info_ptr += unit->addr_size;
1135 break;
95e34fb4 1136 case DW_FORM_GNU_ref_alt:
c07cbdd7
JJ
1137 case DW_FORM_sec_offset:
1138 if (unit->offset_size == 4)
dbb3fbbb 1139 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
c07cbdd7 1140 else
dbb3fbbb 1141 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
c07cbdd7
JJ
1142 info_ptr += unit->offset_size;
1143 break;
252b5132 1144 case DW_FORM_block2:
dc810e39 1145 amt = sizeof (struct dwarf_block);
a50b1753 1146 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
8af6b354
AM
1147 if (blk == NULL)
1148 return NULL;
dbb3fbbb 1149 blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end);
252b5132 1150 info_ptr += 2;
12c96342 1151 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
482e2e37 1152 attr->u.blk = blk;
252b5132
RH
1153 break;
1154 case DW_FORM_block4:
dc810e39 1155 amt = sizeof (struct dwarf_block);
a50b1753 1156 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
8af6b354
AM
1157 if (blk == NULL)
1158 return NULL;
dbb3fbbb 1159 blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end);
252b5132 1160 info_ptr += 4;
12c96342 1161 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
482e2e37 1162 attr->u.blk = blk;
252b5132
RH
1163 break;
1164 case DW_FORM_data2:
dbb3fbbb 1165 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
252b5132
RH
1166 info_ptr += 2;
1167 break;
1168 case DW_FORM_data4:
dbb3fbbb 1169 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
252b5132
RH
1170 info_ptr += 4;
1171 break;
1172 case DW_FORM_data8:
dbb3fbbb 1173 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
252b5132
RH
1174 info_ptr += 8;
1175 break;
1176 case DW_FORM_string:
dbb3fbbb 1177 attr->u.str = read_string (abfd, info_ptr, info_ptr_end, &bytes_read);
252b5132
RH
1178 info_ptr += bytes_read;
1179 break;
d03ba2a1 1180 case DW_FORM_strp:
dbb3fbbb 1181 attr->u.str = read_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
d03ba2a1
JJ
1182 info_ptr += bytes_read;
1183 break;
0041f7df
JK
1184 case DW_FORM_line_strp:
1185 attr->u.str = read_indirect_line_string (unit, info_ptr, info_ptr_end, &bytes_read);
1186 info_ptr += bytes_read;
1187 break;
95e34fb4 1188 case DW_FORM_GNU_strp_alt:
dbb3fbbb 1189 attr->u.str = read_alt_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
95e34fb4
NC
1190 info_ptr += bytes_read;
1191 break;
c07cbdd7 1192 case DW_FORM_exprloc:
252b5132 1193 case DW_FORM_block:
dc810e39 1194 amt = sizeof (struct dwarf_block);
a50b1753 1195 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
8af6b354
AM
1196 if (blk == NULL)
1197 return NULL;
4265548c
PA
1198 blk->size = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1199 FALSE, info_ptr_end);
252b5132 1200 info_ptr += bytes_read;
12c96342 1201 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
482e2e37 1202 attr->u.blk = blk;
252b5132
RH
1203 break;
1204 case DW_FORM_block1:
dc810e39 1205 amt = sizeof (struct dwarf_block);
a50b1753 1206 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
8af6b354
AM
1207 if (blk == NULL)
1208 return NULL;
dbb3fbbb 1209 blk->size = read_1_byte (abfd, info_ptr, info_ptr_end);
252b5132 1210 info_ptr += 1;
12c96342 1211 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
482e2e37 1212 attr->u.blk = blk;
252b5132
RH
1213 break;
1214 case DW_FORM_data1:
dbb3fbbb 1215 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
252b5132
RH
1216 info_ptr += 1;
1217 break;
1218 case DW_FORM_flag:
dbb3fbbb 1219 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
252b5132
RH
1220 info_ptr += 1;
1221 break;
c07cbdd7
JJ
1222 case DW_FORM_flag_present:
1223 attr->u.val = 1;
1224 break;
252b5132 1225 case DW_FORM_sdata:
4265548c
PA
1226 attr->u.sval = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1227 TRUE, info_ptr_end);
252b5132
RH
1228 info_ptr += bytes_read;
1229 break;
1230 case DW_FORM_udata:
4265548c
PA
1231 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1232 FALSE, info_ptr_end);
252b5132
RH
1233 info_ptr += bytes_read;
1234 break;
1235 case DW_FORM_ref1:
dbb3fbbb 1236 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
252b5132
RH
1237 info_ptr += 1;
1238 break;
1239 case DW_FORM_ref2:
dbb3fbbb 1240 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
252b5132
RH
1241 info_ptr += 2;
1242 break;
1243 case DW_FORM_ref4:
dbb3fbbb 1244 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
252b5132
RH
1245 info_ptr += 4;
1246 break;
81edd86d 1247 case DW_FORM_ref8:
dbb3fbbb 1248 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
81edd86d
MM
1249 info_ptr += 8;
1250 break;
a37a68dd 1251 case DW_FORM_ref_sig8:
dbb3fbbb 1252 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
a37a68dd
CC
1253 info_ptr += 8;
1254 break;
252b5132 1255 case DW_FORM_ref_udata:
4265548c
PA
1256 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1257 FALSE, info_ptr_end);
252b5132
RH
1258 info_ptr += bytes_read;
1259 break;
252b5132 1260 case DW_FORM_indirect:
4265548c
PA
1261 form = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1262 FALSE, info_ptr_end);
cf716c56 1263 info_ptr += bytes_read;
0041f7df
JK
1264 if (form == DW_FORM_implicit_const)
1265 {
1266 implicit_const = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1267 TRUE, info_ptr_end);
1268 info_ptr += bytes_read;
1269 }
1270 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1271 info_ptr, info_ptr_end);
1272 break;
1273 case DW_FORM_implicit_const:
1274 attr->form = DW_FORM_sdata;
1275 attr->u.sval = implicit_const;
cf716c56 1276 break;
252b5132 1277 default:
9793eb77 1278 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
4eca0228 1279 form);
252b5132 1280 bfd_set_error (bfd_error_bad_value);
c07cbdd7 1281 return NULL;
252b5132
RH
1282 }
1283 return info_ptr;
1284}
1285
cf716c56
RH
1286/* Read an attribute described by an abbreviated attribute. */
1287
f075ee0c 1288static bfd_byte *
dbb3fbbb
NC
1289read_attribute (struct attribute * attr,
1290 struct attr_abbrev * abbrev,
1291 struct comp_unit * unit,
07d6d2b8
AM
1292 bfd_byte * info_ptr,
1293 bfd_byte * info_ptr_end)
cf716c56
RH
1294{
1295 attr->name = abbrev->name;
0041f7df
JK
1296 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1297 unit, info_ptr, info_ptr_end);
cf716c56
RH
1298 return info_ptr;
1299}
1300
e00e8198
AM
1301/* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1302 for a function. */
1303
1304static bfd_boolean
1305non_mangled (int lang)
1306{
1307 switch (lang)
1308 {
1309 default:
1310 return FALSE;
1311
1312 case DW_LANG_C89:
1313 case DW_LANG_C:
1314 case DW_LANG_Ada83:
1315 case DW_LANG_Cobol74:
1316 case DW_LANG_Cobol85:
1317 case DW_LANG_Fortran77:
1318 case DW_LANG_Pascal83:
1319 case DW_LANG_C99:
1320 case DW_LANG_Ada95:
1321 case DW_LANG_PLI:
1322 case DW_LANG_UPC:
1323 case DW_LANG_C11:
1324 return TRUE;
1325 }
1326}
1327
a092b084 1328/* Source line information table routines. */
252b5132
RH
1329
1330#define FILE_ALLOC_CHUNK 5
1331#define DIR_ALLOC_CHUNK 5
1332
a092b084
NC
1333struct line_info
1334{
089e3718
IT
1335 struct line_info * prev_line;
1336 bfd_vma address;
1337 char * filename;
1338 unsigned int line;
1339 unsigned int column;
1340 unsigned int discriminator;
1341 unsigned char op_index;
1342 unsigned char end_sequence; /* End of (sequential) code sequence. */
252b5132
RH
1343};
1344
a092b084
NC
1345struct fileinfo
1346{
089e3718
IT
1347 char * name;
1348 unsigned int dir;
1349 unsigned int time;
1350 unsigned int size;
252b5132
RH
1351};
1352
0ee19663
NC
1353struct line_sequence
1354{
07d6d2b8 1355 bfd_vma low_pc;
0ee19663 1356 struct line_sequence* prev_sequence;
07d6d2b8
AM
1357 struct line_info* last_line; /* Largest VMA. */
1358 struct line_info** line_info_lookup;
089e3718 1359 bfd_size_type num_lines;
0ee19663
NC
1360};
1361
a092b084
NC
1362struct line_info_table
1363{
07d6d2b8
AM
1364 bfd * abfd;
1365 unsigned int num_files;
1366 unsigned int num_dirs;
1367 unsigned int num_sequences;
1368 char * comp_dir;
1369 char ** dirs;
1370 struct fileinfo* files;
0ee19663 1371 struct line_sequence* sequences;
07d6d2b8 1372 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
252b5132
RH
1373};
1374
4ab527b0
FF
1375/* Remember some information about each function. If the function is
1376 inlined (DW_TAG_inlined_subroutine) it may have two additional
1377 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
a2a50954 1378 source code location where this function was inlined. */
4ab527b0 1379
1ee24f27
DJ
1380struct funcinfo
1381{
a2a50954 1382 /* Pointer to previous function in list of all functions. */
089e3718 1383 struct funcinfo * prev_func;
a2a50954 1384 /* Pointer to function one scope higher. */
089e3718 1385 struct funcinfo * caller_func;
a2a50954 1386 /* Source location file name where caller_func inlines this func. */
089e3718 1387 char * caller_file;
a2a50954 1388 /* Source location file name. */
089e3718 1389 char * file;
e00e8198 1390 /* Source location line number where caller_func inlines this func. */
089e3718 1391 int caller_line;
a2a50954 1392 /* Source location line number. */
089e3718
IT
1393 int line;
1394 int tag;
3b0d929d 1395 bfd_boolean is_linkage;
089e3718
IT
1396 const char * name;
1397 struct arange arange;
a2a50954 1398 /* Where the symbol is defined. */
089e3718
IT
1399 asection * sec;
1400};
1401
1402struct lookup_funcinfo
1403{
1404 /* Function information corresponding to this lookup table entry. */
1405 struct funcinfo * funcinfo;
1406
1407 /* The lowest address for this specific function. */
07d6d2b8 1408 bfd_vma low_addr;
089e3718
IT
1409
1410 /* The highest address of this function before the lookup table is sorted.
1411 The highest address of all prior functions after the lookup table is
1412 sorted, which is used for binary search. */
07d6d2b8 1413 bfd_vma high_addr;
5420f73d
L
1414};
1415
1416struct varinfo
1417{
709d67f1 1418 /* Pointer to previous variable in list of all variables */
5420f73d 1419 struct varinfo *prev_var;
709d67f1 1420 /* Source location file name */
5420f73d 1421 char *file;
709d67f1 1422 /* Source location line number */
5420f73d
L
1423 int line;
1424 int tag;
1425 char *name;
5cf2e3f0 1426 bfd_vma addr;
709d67f1 1427 /* Where the symbol is defined */
5420f73d 1428 asection *sec;
709d67f1 1429 /* Is this a stack variable? */
5420f73d 1430 unsigned int stack: 1;
1ee24f27
DJ
1431};
1432
d4c32a81
L
1433/* Return TRUE if NEW_LINE should sort after LINE. */
1434
1435static inline bfd_boolean
1436new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1437{
1438 return (new_line->address > line->address
1439 || (new_line->address == line->address
20230942 1440 && new_line->op_index > line->op_index));
d4c32a81
L
1441}
1442
1443
af3ef9fe
NC
1444/* Adds a new entry to the line_info list in the line_info_table, ensuring
1445 that the list is sorted. Note that the line_info list is sorted from
1446 highest to lowest VMA (with possible duplicates); that is,
1447 line_info->prev_line always accesses an equal or smaller VMA. */
1448
8af6b354 1449static bfd_boolean
818a27ac
AM
1450add_line_info (struct line_info_table *table,
1451 bfd_vma address,
a233b20c 1452 unsigned char op_index,
818a27ac
AM
1453 char *filename,
1454 unsigned int line,
1455 unsigned int column,
9b8d1a36 1456 unsigned int discriminator,
818a27ac 1457 int end_sequence)
252b5132 1458{
dc810e39 1459 bfd_size_type amt = sizeof (struct line_info);
0ee19663 1460 struct line_sequence* seq = table->sequences;
a50b1753 1461 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
252b5132 1462
8af6b354
AM
1463 if (info == NULL)
1464 return FALSE;
1465
d4c32a81 1466 /* Set member data of 'info'. */
f5296ddc 1467 info->prev_line = NULL;
d4c32a81 1468 info->address = address;
a233b20c 1469 info->op_index = op_index;
d4c32a81
L
1470 info->line = line;
1471 info->column = column;
9b8d1a36 1472 info->discriminator = discriminator;
d4c32a81
L
1473 info->end_sequence = end_sequence;
1474
1475 if (filename && filename[0])
1476 {
a50b1753 1477 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
8af6b354
AM
1478 if (info->filename == NULL)
1479 return FALSE;
1480 strcpy (info->filename, filename);
d4c32a81
L
1481 }
1482 else
1483 info->filename = NULL;
1484
e82ce529
AM
1485 /* Find the correct location for 'info'. Normally we will receive
1486 new line_info data 1) in order and 2) with increasing VMAs.
1487 However some compilers break the rules (cf. decode_line_info) and
1488 so we include some heuristics for quickly finding the correct
1489 location for 'info'. In particular, these heuristics optimize for
1490 the common case in which the VMA sequence that we receive is a
1491 list of locally sorted VMAs such as
1492 p...z a...j (where a < j < p < z)
252b5132 1493
e82ce529 1494 Note: table->lcl_head is used to head an *actual* or *possible*
0ee19663 1495 sub-sequence within the list (such as a...j) that is not directly
e82ce529
AM
1496 headed by table->last_line
1497
1498 Note: we may receive duplicate entries from 'decode_line_info'. */
1499
0ee19663
NC
1500 if (seq
1501 && seq->last_line->address == address
a233b20c 1502 && seq->last_line->op_index == op_index
0ee19663 1503 && seq->last_line->end_sequence == end_sequence)
aff90a5f
L
1504 {
1505 /* We only keep the last entry with the same address and end
1506 sequence. See PR ld/4986. */
0ee19663 1507 if (table->lcl_head == seq->last_line)
aff90a5f 1508 table->lcl_head = info;
0ee19663
NC
1509 info->prev_line = seq->last_line->prev_line;
1510 seq->last_line = info;
aff90a5f 1511 }
0ee19663 1512 else if (!seq || seq->last_line->end_sequence)
d8d1c398 1513 {
0ee19663
NC
1514 /* Start a new line sequence. */
1515 amt = sizeof (struct line_sequence);
1516 seq = (struct line_sequence *) bfd_malloc (amt);
8af6b354
AM
1517 if (seq == NULL)
1518 return FALSE;
0ee19663
NC
1519 seq->low_pc = address;
1520 seq->prev_sequence = table->sequences;
1521 seq->last_line = info;
1522 table->lcl_head = info;
1523 table->sequences = seq;
1524 table->num_sequences++;
1525 }
20230942
AM
1526 else if (info->end_sequence
1527 || new_line_sorts_after (info, seq->last_line))
0ee19663
NC
1528 {
1529 /* Normal case: add 'info' to the beginning of the current sequence. */
1530 info->prev_line = seq->last_line;
1531 seq->last_line = info;
e82ce529 1532
d8d1c398
AM
1533 /* lcl_head: initialize to head a *possible* sequence at the end. */
1534 if (!table->lcl_head)
1535 table->lcl_head = info;
1536 }
1537 else if (!new_line_sorts_after (info, table->lcl_head)
1538 && (!table->lcl_head->prev_line
1539 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1540 {
1541 /* Abnormal but easy: lcl_head is the head of 'info'. */
1542 info->prev_line = table->lcl_head->prev_line;
1543 table->lcl_head->prev_line = info;
1544 }
1545 else
1546 {
0ee19663
NC
1547 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1548 are valid heads for 'info'. Reset 'lcl_head'. */
1549 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
d8d1c398 1550 struct line_info* li1 = li2->prev_line;
e82ce529 1551
d8d1c398
AM
1552 while (li1)
1553 {
1554 if (!new_line_sorts_after (info, li2)
1555 && new_line_sorts_after (info, li1))
1556 break;
e82ce529 1557
709d67f1 1558 li2 = li1; /* always non-NULL */
d8d1c398
AM
1559 li1 = li1->prev_line;
1560 }
1561 table->lcl_head = li2;
1562 info->prev_line = table->lcl_head->prev_line;
1563 table->lcl_head->prev_line = info;
0ee19663 1564 if (address < seq->low_pc)
93ee1e36 1565 seq->low_pc = address;
d8d1c398 1566 }
8af6b354 1567 return TRUE;
252b5132
RH
1568}
1569
5ed6aba4 1570/* Extract a fully qualified filename from a line info table.
af3ef9fe
NC
1571 The returned string has been malloc'ed and it is the caller's
1572 responsibility to free it. */
5ed6aba4 1573
a092b084 1574static char *
818a27ac 1575concat_filename (struct line_info_table *table, unsigned int file)
252b5132 1576{
f075ee0c 1577 char *filename;
159002ff 1578
6327533b 1579 if (table == NULL || file - 1 >= table->num_files)
159002ff 1580 {
75a657ba
L
1581 /* FILE == 0 means unknown. */
1582 if (file)
4eca0228 1583 _bfd_error_handler
9793eb77 1584 (_("DWARF error: mangled line number section (bad file number)"));
af3ef9fe 1585 return strdup ("<unknown>");
159002ff
RH
1586 }
1587
1588 filename = table->files[file - 1].name;
a54018b7
AM
1589 if (filename == NULL)
1590 return strdup ("<unknown>");
5ed6aba4 1591
7421a730 1592 if (!IS_ABSOLUTE_PATH (filename))
252b5132 1593 {
608fa8d3
JB
1594 char *dir_name = NULL;
1595 char *subdir_name = NULL;
7421a730
AM
1596 char *name;
1597 size_t len;
0dafd5f6 1598
877a8638 1599 if (table->files[file - 1].dir
dbb3fbbb
NC
1600 /* PR 17512: file: 0317e960. */
1601 && table->files[file - 1].dir <= table->num_dirs
877a8638
NC
1602 /* PR 17512: file: 7f3d2e4b. */
1603 && table->dirs != NULL)
608fa8d3 1604 subdir_name = table->dirs[table->files[file - 1].dir - 1];
7421a730 1605
608fa8d3
JB
1606 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1607 dir_name = table->comp_dir;
7421a730 1608
608fa8d3 1609 if (!dir_name)
af3ef9fe 1610 {
608fa8d3
JB
1611 dir_name = subdir_name;
1612 subdir_name = NULL;
7421a730 1613 }
af3ef9fe 1614
608fa8d3 1615 if (!dir_name)
7421a730
AM
1616 return strdup (filename);
1617
608fa8d3 1618 len = strlen (dir_name) + strlen (filename) + 2;
7421a730 1619
608fa8d3 1620 if (subdir_name)
7421a730 1621 {
608fa8d3 1622 len += strlen (subdir_name) + 1;
a50b1753 1623 name = (char *) bfd_malloc (len);
7421a730 1624 if (name)
608fa8d3 1625 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
7421a730
AM
1626 }
1627 else
1628 {
a50b1753 1629 name = (char *) bfd_malloc (len);
af3ef9fe 1630 if (name)
608fa8d3 1631 sprintf (name, "%s/%s", dir_name, filename);
af3ef9fe 1632 }
7421a730
AM
1633
1634 return name;
252b5132 1635 }
af3ef9fe
NC
1636
1637 return strdup (filename);
252b5132
RH
1638}
1639
8af6b354 1640static bfd_boolean
a2a50954 1641arange_add (const struct comp_unit *unit, struct arange *first_arange,
8af6b354 1642 bfd_vma low_pc, bfd_vma high_pc)
f623be2b
RH
1643{
1644 struct arange *arange;
1645
a2a50954
AM
1646 /* Ignore empty ranges. */
1647 if (low_pc == high_pc)
1648 return TRUE;
1649
1650 /* If the first arange is empty, use it. */
a13afe8e
FF
1651 if (first_arange->high == 0)
1652 {
1653 first_arange->low = low_pc;
1654 first_arange->high = high_pc;
8af6b354 1655 return TRUE;
a13afe8e 1656 }
98591c73 1657
a13afe8e
FF
1658 /* Next see if we can cheaply extend an existing range. */
1659 arange = first_arange;
f623be2b
RH
1660 do
1661 {
1662 if (low_pc == arange->high)
1663 {
1664 arange->high = high_pc;
8af6b354 1665 return TRUE;
f623be2b
RH
1666 }
1667 if (high_pc == arange->low)
1668 {
1669 arange->low = low_pc;
8af6b354 1670 return TRUE;
f623be2b
RH
1671 }
1672 arange = arange->next;
1673 }
1674 while (arange);
1675
a13afe8e 1676 /* Need to allocate a new arange and insert it into the arange list.
089e3718 1677 Order isn't significant, so just insert after the first arange. */
a2a50954 1678 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
8af6b354
AM
1679 if (arange == NULL)
1680 return FALSE;
f623be2b
RH
1681 arange->low = low_pc;
1682 arange->high = high_pc;
a13afe8e
FF
1683 arange->next = first_arange->next;
1684 first_arange->next = arange;
8af6b354 1685 return TRUE;
f623be2b
RH
1686}
1687
0ee19663
NC
1688/* Compare function for line sequences. */
1689
1690static int
1691compare_sequences (const void* a, const void* b)
1692{
1693 const struct line_sequence* seq1 = a;
1694 const struct line_sequence* seq2 = b;
1695
1696 /* Sort by low_pc as the primary key. */
1697 if (seq1->low_pc < seq2->low_pc)
1698 return -1;
1699 if (seq1->low_pc > seq2->low_pc)
1700 return 1;
1701
1702 /* If low_pc values are equal, sort in reverse order of
1703 high_pc, so that the largest region comes first. */
1704 if (seq1->last_line->address < seq2->last_line->address)
1705 return 1;
1706 if (seq1->last_line->address > seq2->last_line->address)
1707 return -1;
1708
a233b20c
JJ
1709 if (seq1->last_line->op_index < seq2->last_line->op_index)
1710 return 1;
1711 if (seq1->last_line->op_index > seq2->last_line->op_index)
1712 return -1;
1713
0ee19663
NC
1714 return 0;
1715}
1716
089e3718
IT
1717/* Construct the line information table for quick lookup. */
1718
1719static bfd_boolean
1720build_line_info_table (struct line_info_table * table,
1721 struct line_sequence * seq)
1722{
1723 bfd_size_type amt;
1724 struct line_info** line_info_lookup;
1725 struct line_info* each_line;
1726 unsigned int num_lines;
b6ddcd85 1727 unsigned int line_index;
089e3718
IT
1728
1729 if (seq->line_info_lookup != NULL)
1730 return TRUE;
1731
1732 /* Count the number of line information entries. We could do this while
1733 scanning the debug information, but some entries may be added via
1734 lcl_head without having a sequence handy to increment the number of
1735 lines. */
1736 num_lines = 0;
1737 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1738 num_lines++;
1739
1740 if (num_lines == 0)
1741 return TRUE;
1742
1743 /* Allocate space for the line information lookup table. */
1744 amt = sizeof (struct line_info*) * num_lines;
1745 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
1746 if (line_info_lookup == NULL)
1747 return FALSE;
1748
1749 /* Create the line information lookup table. */
b6ddcd85 1750 line_index = num_lines;
089e3718 1751 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
b6ddcd85 1752 line_info_lookup[--line_index] = each_line;
089e3718 1753
b6ddcd85 1754 BFD_ASSERT (line_index == 0);
089e3718
IT
1755
1756 seq->num_lines = num_lines;
1757 seq->line_info_lookup = line_info_lookup;
1758
1759 return TRUE;
1760}
1761
0ee19663
NC
1762/* Sort the line sequences for quick lookup. */
1763
8af6b354 1764static bfd_boolean
0ee19663
NC
1765sort_line_sequences (struct line_info_table* table)
1766{
07d6d2b8
AM
1767 bfd_size_type amt;
1768 struct line_sequence* sequences;
1769 struct line_sequence* seq;
1770 unsigned int n = 0;
1771 unsigned int num_sequences = table->num_sequences;
1772 bfd_vma last_high_pc;
0ee19663
NC
1773
1774 if (num_sequences == 0)
8af6b354 1775 return TRUE;
0ee19663
NC
1776
1777 /* Allocate space for an array of sequences. */
1778 amt = sizeof (struct line_sequence) * num_sequences;
1779 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
8af6b354
AM
1780 if (sequences == NULL)
1781 return FALSE;
0ee19663
NC
1782
1783 /* Copy the linked list into the array, freeing the original nodes. */
1784 seq = table->sequences;
1785 for (n = 0; n < num_sequences; n++)
1786 {
1787 struct line_sequence* last_seq = seq;
1788
1789 BFD_ASSERT (seq);
1790 sequences[n].low_pc = seq->low_pc;
1791 sequences[n].prev_sequence = NULL;
1792 sequences[n].last_line = seq->last_line;
089e3718
IT
1793 sequences[n].line_info_lookup = NULL;
1794 sequences[n].num_lines = 0;
0ee19663
NC
1795 seq = seq->prev_sequence;
1796 free (last_seq);
1797 }
1798 BFD_ASSERT (seq == NULL);
1799
1800 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1801
1802 /* Make the list binary-searchable by trimming overlapping entries
1803 and removing nested entries. */
1804 num_sequences = 1;
1805 last_high_pc = sequences[0].last_line->address;
1806 for (n = 1; n < table->num_sequences; n++)
1807 {
1808 if (sequences[n].low_pc < last_high_pc)
93ee1e36 1809 {
0ee19663
NC
1810 if (sequences[n].last_line->address <= last_high_pc)
1811 /* Skip nested entries. */
1812 continue;
1813
1814 /* Trim overlapping entries. */
1815 sequences[n].low_pc = last_high_pc;
93ee1e36 1816 }
0ee19663
NC
1817 last_high_pc = sequences[n].last_line->address;
1818 if (n > num_sequences)
93ee1e36
AM
1819 {
1820 /* Close up the gap. */
1821 sequences[num_sequences].low_pc = sequences[n].low_pc;
1822 sequences[num_sequences].last_line = sequences[n].last_line;
1823 }
0ee19663
NC
1824 num_sequences++;
1825 }
1826
1827 table->sequences = sequences;
1828 table->num_sequences = num_sequences;
8af6b354 1829 return TRUE;
0ee19663
NC
1830}
1831
0041f7df
JK
1832/* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1833
1834static bfd_boolean
1835line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
1836{
1837 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1838 {
1839 char **tmp;
1840 bfd_size_type amt;
1841
1842 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1843 amt *= sizeof (char *);
1844
1845 tmp = (char **) bfd_realloc (table->dirs, amt);
1846 if (tmp == NULL)
1847 return FALSE;
1848 table->dirs = tmp;
1849 }
1850
1851 table->dirs[table->num_dirs++] = cur_dir;
1852 return TRUE;
1853}
1854
1855static bfd_boolean
1856line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
1857 unsigned int dir ATTRIBUTE_UNUSED,
1d827a72 1858 unsigned int xtime ATTRIBUTE_UNUSED,
0041f7df
JK
1859 unsigned int size ATTRIBUTE_UNUSED)
1860{
1861 return line_info_add_include_dir (table, cur_dir);
1862}
1863
1864/* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1865
1866static bfd_boolean
1867line_info_add_file_name (struct line_info_table *table, char *cur_file,
1d827a72
L
1868 unsigned int dir, unsigned int xtime,
1869 unsigned int size)
0041f7df
JK
1870{
1871 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1872 {
1873 struct fileinfo *tmp;
1874 bfd_size_type amt;
1875
1876 amt = table->num_files + FILE_ALLOC_CHUNK;
1877 amt *= sizeof (struct fileinfo);
1878
1879 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1880 if (tmp == NULL)
1881 return FALSE;
1882 table->files = tmp;
1883 }
1884
1885 table->files[table->num_files].name = cur_file;
1886 table->files[table->num_files].dir = dir;
1d827a72 1887 table->files[table->num_files].time = xtime;
0041f7df
JK
1888 table->files[table->num_files].size = size;
1889 table->num_files++;
1890 return TRUE;
1891}
1892
1893/* Read directory or file name entry format, starting with byte of
1894 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1895 entries count and the entries themselves in the described entry
1896 format. */
1897
1898static bfd_boolean
1899read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
1900 bfd_byte *buf_end, struct line_info_table *table,
1901 bfd_boolean (*callback) (struct line_info_table *table,
1902 char *cur_file,
1903 unsigned int dir,
1904 unsigned int time,
1905 unsigned int size))
1906{
1907 bfd *abfd = unit->abfd;
1908 bfd_byte format_count, formati;
1909 bfd_vma data_count, datai;
1910 bfd_byte *buf = *bufp;
1911 bfd_byte *format_header_data;
1912 unsigned int bytes_read;
1913
1914 format_count = read_1_byte (abfd, buf, buf_end);
1915 buf += 1;
1916 format_header_data = buf;
1917 for (formati = 0; formati < format_count; formati++)
1918 {
1919 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1920 buf += bytes_read;
1921 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1922 buf += bytes_read;
1923 }
1924
1925 data_count = _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1926 buf += bytes_read;
c361faae
AM
1927 if (format_count == 0 && data_count != 0)
1928 {
9793eb77 1929 _bfd_error_handler (_("DWARF error: zero format count"));
c361faae
AM
1930 bfd_set_error (bfd_error_bad_value);
1931 return FALSE;
1932 }
1933
30d0157a
NC
1934 /* PR 22210. Paranoia check. Don't bother running the loop
1935 if we know that we are going to run out of buffer. */
1936 if (data_count > (bfd_vma) (buf_end - buf))
1937 {
2dcf00ce 1938 _bfd_error_handler
9793eb77 1939 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2dcf00ce 1940 (uint64_t) data_count);
30d0157a
NC
1941 bfd_set_error (bfd_error_bad_value);
1942 return FALSE;
1943 }
1944
0041f7df
JK
1945 for (datai = 0; datai < data_count; datai++)
1946 {
1947 bfd_byte *format = format_header_data;
1948 struct fileinfo fe;
1949
a54018b7 1950 memset (&fe, 0, sizeof fe);
0041f7df
JK
1951 for (formati = 0; formati < format_count; formati++)
1952 {
1953 bfd_vma content_type, form;
1954 char *string_trash;
1955 char **stringp = &string_trash;
1956 unsigned int uint_trash, *uintp = &uint_trash;
33e0a9a0 1957 struct attribute attr;
0041f7df
JK
1958
1959 content_type = _bfd_safe_read_leb128 (abfd, format, &bytes_read,
1960 FALSE, buf_end);
1961 format += bytes_read;
1962 switch (content_type)
1963 {
1964 case DW_LNCT_path:
1965 stringp = &fe.name;
1966 break;
1967 case DW_LNCT_directory_index:
1968 uintp = &fe.dir;
1969 break;
1970 case DW_LNCT_timestamp:
1971 uintp = &fe.time;
1972 break;
1973 case DW_LNCT_size:
1974 uintp = &fe.size;
1975 break;
1976 case DW_LNCT_MD5:
1977 break;
1978 default:
1979 _bfd_error_handler
9793eb77 1980 (_("DWARF error: unknown format content type %" PRIu64),
2dcf00ce 1981 (uint64_t) content_type);
0041f7df
JK
1982 bfd_set_error (bfd_error_bad_value);
1983 return FALSE;
1984 }
1985
1986 form = _bfd_safe_read_leb128 (abfd, format, &bytes_read, FALSE,
1987 buf_end);
1988 format += bytes_read;
33e0a9a0
AM
1989
1990 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
1991 if (buf == NULL)
1992 return FALSE;
0041f7df
JK
1993 switch (form)
1994 {
1995 case DW_FORM_string:
0041f7df 1996 case DW_FORM_line_strp:
33e0a9a0 1997 *stringp = attr.u.str;
0041f7df
JK
1998 break;
1999
2000 case DW_FORM_data1:
0041f7df 2001 case DW_FORM_data2:
0041f7df 2002 case DW_FORM_data4:
0041f7df 2003 case DW_FORM_data8:
0041f7df 2004 case DW_FORM_udata:
33e0a9a0 2005 *uintp = attr.u.val;
0041f7df
JK
2006 break;
2007 }
2008 }
2009
2010 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2011 return FALSE;
2012 }
2013
2014 *bufp = buf;
2015 return TRUE;
2016}
2017
34b5e0b2 2018/* Decode the line number information for UNIT. */
252b5132 2019
34b5e0b2 2020static struct line_info_table*
818a27ac 2021decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
252b5132
RH
2022{
2023 bfd *abfd = unit->abfd;
252b5132 2024 struct line_info_table* table;
f075ee0c
AM
2025 bfd_byte *line_ptr;
2026 bfd_byte *line_end;
252b5132 2027 struct line_head lh;
d03ba2a1 2028 unsigned int i, bytes_read, offset_size;
252b5132
RH
2029 char *cur_file, *cur_dir;
2030 unsigned char op_code, extended_op, adj_opcode;
fec16237 2031 unsigned int exop_len;
dc810e39 2032 bfd_size_type amt;
252b5132 2033
fc28f9aa 2034 if (! read_section (abfd, &stash->debug_sections[debug_line],
93ee1e36 2035 stash->syms, unit->line_offset,
9e32b19f 2036 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
8af6b354 2037 return NULL;
ccdb16fc 2038
dc810e39 2039 amt = sizeof (struct line_info_table);
a50b1753 2040 table = (struct line_info_table *) bfd_alloc (abfd, amt);
8af6b354
AM
2041 if (table == NULL)
2042 return NULL;
252b5132
RH
2043 table->abfd = abfd;
2044 table->comp_dir = unit->comp_dir;
2045
2046 table->num_files = 0;
2047 table->files = NULL;
2048
2049 table->num_dirs = 0;
2050 table->dirs = NULL;
2051
0ee19663
NC
2052 table->num_sequences = 0;
2053 table->sequences = NULL;
2054
e82ce529 2055 table->lcl_head = NULL;
159002ff 2056
dbb3fbbb
NC
2057 if (stash->dwarf_line_size < 16)
2058 {
4eca0228 2059 _bfd_error_handler
9793eb77 2060 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2dcf00ce 2061 (int64_t) stash->dwarf_line_size);
dbb3fbbb
NC
2062 bfd_set_error (bfd_error_bad_value);
2063 return NULL;
2064 }
69dd2e2d 2065 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
dbb3fbbb 2066 line_end = stash->dwarf_line_buffer + stash->dwarf_line_size;
252b5132 2067
a092b084 2068 /* Read in the prologue. */
dbb3fbbb 2069 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
91a4d569
AM
2070 line_ptr += 4;
2071 offset_size = 4;
2072 if (lh.total_length == 0xffffffff)
dae2dd0d 2073 {
dbb3fbbb 2074 lh.total_length = read_8_bytes (abfd, line_ptr, line_end);
dae2dd0d
NC
2075 line_ptr += 8;
2076 offset_size = 8;
2077 }
91a4d569 2078 else if (lh.total_length == 0 && unit->addr_size == 8)
d03ba2a1 2079 {
91a4d569 2080 /* Handle (non-standard) 64-bit DWARF2 formats. */
dbb3fbbb 2081 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
91a4d569 2082 line_ptr += 4;
d03ba2a1
JJ
2083 offset_size = 8;
2084 }
dbb3fbbb 2085
515f23e6 2086 if (lh.total_length > (size_t) (line_end - line_ptr))
dbb3fbbb 2087 {
4eca0228 2088 _bfd_error_handler
695344c0 2089 /* xgettext: c-format */
9793eb77 2090 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
515f23e6 2091 " than the space remaining in the section (%#lx)"),
2dcf00ce 2092 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
dbb3fbbb
NC
2093 bfd_set_error (bfd_error_bad_value);
2094 return NULL;
2095 }
62f8d217 2096
252b5132 2097 line_end = line_ptr + lh.total_length;
62f8d217 2098
dbb3fbbb 2099 lh.version = read_2_bytes (abfd, line_ptr, line_end);
0041f7df 2100 if (lh.version < 2 || lh.version > 5)
a233b20c 2101 {
4eca0228 2102 _bfd_error_handler
9793eb77 2103 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
a233b20c
JJ
2104 bfd_set_error (bfd_error_bad_value);
2105 return NULL;
2106 }
252b5132 2107 line_ptr += 2;
dbb3fbbb 2108
0041f7df
JK
2109 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2110 >= line_end)
dbb3fbbb 2111 {
4eca0228 2112 _bfd_error_handler
9793eb77 2113 (_("DWARF error: ran out of room reading prologue"));
dbb3fbbb
NC
2114 bfd_set_error (bfd_error_bad_value);
2115 return NULL;
2116 }
2117
0041f7df
JK
2118 if (lh.version >= 5)
2119 {
2120 unsigned int segment_selector_size;
2121
2122 /* Skip address size. */
2123 read_1_byte (abfd, line_ptr, line_end);
2124 line_ptr += 1;
2125
2126 segment_selector_size = read_1_byte (abfd, line_ptr, line_end);
2127 line_ptr += 1;
2128 if (segment_selector_size != 0)
2129 {
2130 _bfd_error_handler
9793eb77 2131 (_("DWARF error: line info unsupported segment selector size %u"),
0041f7df
JK
2132 segment_selector_size);
2133 bfd_set_error (bfd_error_bad_value);
2134 return NULL;
2135 }
2136 }
2137
d03ba2a1 2138 if (offset_size == 4)
dbb3fbbb 2139 lh.prologue_length = read_4_bytes (abfd, line_ptr, line_end);
d03ba2a1 2140 else
dbb3fbbb 2141 lh.prologue_length = read_8_bytes (abfd, line_ptr, line_end);
d03ba2a1 2142 line_ptr += offset_size;
dbb3fbbb
NC
2143
2144 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr, line_end);
252b5132 2145 line_ptr += 1;
dbb3fbbb 2146
a233b20c
JJ
2147 if (lh.version >= 4)
2148 {
dbb3fbbb 2149 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr, line_end);
a233b20c
JJ
2150 line_ptr += 1;
2151 }
2152 else
2153 lh.maximum_ops_per_insn = 1;
dbb3fbbb 2154
a233b20c
JJ
2155 if (lh.maximum_ops_per_insn == 0)
2156 {
4eca0228 2157 _bfd_error_handler
9793eb77 2158 (_("DWARF error: invalid maximum operations per instruction"));
a233b20c
JJ
2159 bfd_set_error (bfd_error_bad_value);
2160 return NULL;
2161 }
dbb3fbbb
NC
2162
2163 lh.default_is_stmt = read_1_byte (abfd, line_ptr, line_end);
252b5132 2164 line_ptr += 1;
dbb3fbbb
NC
2165
2166 lh.line_base = read_1_signed_byte (abfd, line_ptr, line_end);
252b5132 2167 line_ptr += 1;
dbb3fbbb
NC
2168
2169 lh.line_range = read_1_byte (abfd, line_ptr, line_end);
252b5132 2170 line_ptr += 1;
dbb3fbbb
NC
2171
2172 lh.opcode_base = read_1_byte (abfd, line_ptr, line_end);
252b5132 2173 line_ptr += 1;
dbb3fbbb
NC
2174
2175 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2176 {
9793eb77 2177 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
dbb3fbbb
NC
2178 bfd_set_error (bfd_error_bad_value);
2179 return NULL;
2180 }
62f8d217 2181
dc810e39 2182 amt = lh.opcode_base * sizeof (unsigned char);
a50b1753 2183 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
252b5132
RH
2184
2185 lh.standard_opcode_lengths[0] = 1;
98591c73 2186
252b5132
RH
2187 for (i = 1; i < lh.opcode_base; ++i)
2188 {
dbb3fbbb 2189 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr, line_end);
252b5132
RH
2190 line_ptr += 1;
2191 }
2192
0041f7df 2193 if (lh.version >= 5)
252b5132 2194 {
0041f7df
JK
2195 /* Read directory table. */
2196 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2197 line_info_add_include_dir_stub))
2198 goto fail;
98591c73 2199
0041f7df
JK
2200 /* Read file name table. */
2201 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2202 line_info_add_file_name))
2203 goto fail;
2204 }
2205 else
2206 {
2207 /* Read directory table. */
2208 while ((cur_dir = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
252b5132 2209 {
0041f7df 2210 line_ptr += bytes_read;
35330cce 2211
0041f7df 2212 if (!line_info_add_include_dir (table, cur_dir))
8af6b354 2213 goto fail;
252b5132 2214 }
98591c73 2215
252b5132 2216 line_ptr += bytes_read;
98591c73 2217
0041f7df
JK
2218 /* Read file name table. */
2219 while ((cur_file = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
252b5132 2220 {
1d827a72 2221 unsigned int dir, xtime, size;
0041f7df
JK
2222
2223 line_ptr += bytes_read;
35330cce 2224
0041f7df
JK
2225 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2226 line_ptr += bytes_read;
1d827a72 2227 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
0041f7df
JK
2228 line_ptr += bytes_read;
2229 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2230 line_ptr += bytes_read;
35330cce 2231
1d827a72 2232 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
8af6b354 2233 goto fail;
252b5132 2234 }
98591c73 2235
252b5132 2236 line_ptr += bytes_read;
252b5132 2237 }
98591c73 2238
252b5132
RH
2239 /* Read the statement sequences until there's nothing left. */
2240 while (line_ptr < line_end)
2241 {
a092b084 2242 /* State machine registers. */
252b5132 2243 bfd_vma address = 0;
a233b20c 2244 unsigned char op_index = 0;
8bfd78b3 2245 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
252b5132
RH
2246 unsigned int line = 1;
2247 unsigned int column = 0;
9b8d1a36 2248 unsigned int discriminator = 0;
252b5132 2249 int is_stmt = lh.default_is_stmt;
e2f6d277 2250 int end_sequence = 0;
a54018b7 2251 unsigned int dir, xtime, size;
e2f6d277 2252 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
e82ce529
AM
2253 compilers generate address sequences that are wildly out of
2254 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2255 for ia64-Linux). Thus, to determine the low and high
2256 address, we must compare on every DW_LNS_copy, etc. */
75758e9d 2257 bfd_vma low_pc = (bfd_vma) -1;
e2f6d277 2258 bfd_vma high_pc = 0;
252b5132 2259
a092b084 2260 /* Decode the table. */
e338894d 2261 while (!end_sequence && line_ptr < line_end)
252b5132 2262 {
dbb3fbbb 2263 op_code = read_1_byte (abfd, line_ptr, line_end);
252b5132 2264 line_ptr += 1;
98591c73 2265
1a509dcc 2266 if (op_code >= lh.opcode_base)
e2f6d277
NC
2267 {
2268 /* Special operand. */
1a509dcc 2269 adj_opcode = op_code - lh.opcode_base;
dbb3fbbb
NC
2270 if (lh.line_range == 0)
2271 goto line_fail;
a233b20c 2272 if (lh.maximum_ops_per_insn == 1)
a2a50954
AM
2273 address += (adj_opcode / lh.line_range
2274 * lh.minimum_instruction_length);
a233b20c
JJ
2275 else
2276 {
a2a50954
AM
2277 address += ((op_index + adj_opcode / lh.line_range)
2278 / lh.maximum_ops_per_insn
2279 * lh.minimum_instruction_length);
2280 op_index = ((op_index + adj_opcode / lh.line_range)
2281 % lh.maximum_ops_per_insn);
a233b20c 2282 }
1a509dcc
GK
2283 line += lh.line_base + (adj_opcode % lh.line_range);
2284 /* Append row to matrix using current values. */
a233b20c 2285 if (!add_line_info (table, address, op_index, filename,
9b8d1a36 2286 line, column, discriminator, 0))
8af6b354 2287 goto line_fail;
93ee1e36 2288 discriminator = 0;
75758e9d
AM
2289 if (address < low_pc)
2290 low_pc = address;
e2f6d277
NC
2291 if (address > high_pc)
2292 high_pc = address;
1a509dcc
GK
2293 }
2294 else switch (op_code)
252b5132
RH
2295 {
2296 case DW_LNS_extended_op:
4265548c
PA
2297 exop_len = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2298 FALSE, line_end);
fec16237 2299 line_ptr += bytes_read;
dbb3fbbb 2300 extended_op = read_1_byte (abfd, line_ptr, line_end);
252b5132 2301 line_ptr += 1;
e2f6d277 2302
252b5132
RH
2303 switch (extended_op)
2304 {
2305 case DW_LNE_end_sequence:
2306 end_sequence = 1;
9b8d1a36
CC
2307 if (!add_line_info (table, address, op_index, filename, line,
2308 column, discriminator, end_sequence))
8af6b354 2309 goto line_fail;
93ee1e36 2310 discriminator = 0;
75758e9d
AM
2311 if (address < low_pc)
2312 low_pc = address;
e2f6d277
NC
2313 if (address > high_pc)
2314 high_pc = address;
a2a50954 2315 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
8af6b354 2316 goto line_fail;
252b5132
RH
2317 break;
2318 case DW_LNE_set_address:
dbb3fbbb 2319 address = read_address (unit, line_ptr, line_end);
a233b20c 2320 op_index = 0;
252b5132
RH
2321 line_ptr += unit->addr_size;
2322 break;
2323 case DW_LNE_define_file:
dbb3fbbb 2324 cur_file = read_string (abfd, line_ptr, line_end, &bytes_read);
252b5132 2325 line_ptr += bytes_read;
a54018b7
AM
2326 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2327 FALSE, line_end);
252b5132 2328 line_ptr += bytes_read;
a54018b7
AM
2329 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2330 FALSE, line_end);
252b5132 2331 line_ptr += bytes_read;
a54018b7
AM
2332 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2333 FALSE, line_end);
252b5132 2334 line_ptr += bytes_read;
a54018b7
AM
2335 if (!line_info_add_file_name (table, cur_file, dir,
2336 xtime, size))
2337 goto line_fail;
252b5132 2338 break;
9e1f7c0e 2339 case DW_LNE_set_discriminator:
9b8d1a36 2340 discriminator =
4265548c
PA
2341 _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2342 FALSE, line_end);
9e1f7c0e
DK
2343 line_ptr += bytes_read;
2344 break;
a2a50954
AM
2345 case DW_LNE_HP_source_file_correlation:
2346 line_ptr += exop_len - 1;
2347 break;
252b5132 2348 default:
4eca0228 2349 _bfd_error_handler
9793eb77 2350 (_("DWARF error: mangled line number section"));
252b5132 2351 bfd_set_error (bfd_error_bad_value);
8af6b354
AM
2352 line_fail:
2353 if (filename != NULL)
2354 free (filename);
2355 goto fail;
252b5132
RH
2356 }
2357 break;
2358 case DW_LNS_copy:
a233b20c 2359 if (!add_line_info (table, address, op_index,
9b8d1a36 2360 filename, line, column, discriminator, 0))
8af6b354 2361 goto line_fail;
93ee1e36 2362 discriminator = 0;
75758e9d
AM
2363 if (address < low_pc)
2364 low_pc = address;
e2f6d277
NC
2365 if (address > high_pc)
2366 high_pc = address;
252b5132
RH
2367 break;
2368 case DW_LNS_advance_pc:
a233b20c 2369 if (lh.maximum_ops_per_insn == 1)
a2a50954 2370 address += (lh.minimum_instruction_length
4265548c
PA
2371 * _bfd_safe_read_leb128 (abfd, line_ptr,
2372 &bytes_read,
2373 FALSE, line_end));
a233b20c
JJ
2374 else
2375 {
4265548c
PA
2376 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, line_ptr,
2377 &bytes_read,
2378 FALSE, line_end);
a2a50954
AM
2379 address = ((op_index + adjust) / lh.maximum_ops_per_insn
2380 * lh.minimum_instruction_length);
a233b20c
JJ
2381 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2382 }
252b5132
RH
2383 line_ptr += bytes_read;
2384 break;
2385 case DW_LNS_advance_line:
4265548c
PA
2386 line += _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2387 TRUE, line_end);
252b5132
RH
2388 line_ptr += bytes_read;
2389 break;
2390 case DW_LNS_set_file:
2391 {
2392 unsigned int file;
2393
e2f6d277
NC
2394 /* The file and directory tables are 0
2395 based, the references are 1 based. */
4265548c
PA
2396 file = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2397 FALSE, line_end);
252b5132 2398 line_ptr += bytes_read;
af3ef9fe
NC
2399 if (filename)
2400 free (filename);
252b5132
RH
2401 filename = concat_filename (table, file);
2402 break;
2403 }
2404 case DW_LNS_set_column:
4265548c
PA
2405 column = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2406 FALSE, line_end);
252b5132
RH
2407 line_ptr += bytes_read;
2408 break;
2409 case DW_LNS_negate_stmt:
2410 is_stmt = (!is_stmt);
2411 break;
2412 case DW_LNS_set_basic_block:
252b5132
RH
2413 break;
2414 case DW_LNS_const_add_pc:
d8010d3e
AM
2415 if (lh.line_range == 0)
2416 goto line_fail;
a233b20c 2417 if (lh.maximum_ops_per_insn == 1)
a2a50954
AM
2418 address += (lh.minimum_instruction_length
2419 * ((255 - lh.opcode_base) / lh.line_range));
a233b20c
JJ
2420 else
2421 {
2422 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
a2a50954
AM
2423 address += (lh.minimum_instruction_length
2424 * ((op_index + adjust)
2425 / lh.maximum_ops_per_insn));
a233b20c
JJ
2426 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2427 }
252b5132
RH
2428 break;
2429 case DW_LNS_fixed_advance_pc:
dbb3fbbb 2430 address += read_2_bytes (abfd, line_ptr, line_end);
a233b20c 2431 op_index = 0;
252b5132
RH
2432 line_ptr += 2;
2433 break;
1a509dcc 2434 default:
91d6fa6a
NC
2435 /* Unknown standard opcode, ignore it. */
2436 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
2437 {
4265548c
PA
2438 (void) _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2439 FALSE, line_end);
91d6fa6a
NC
2440 line_ptr += bytes_read;
2441 }
2442 break;
252b5132
RH
2443 }
2444 }
5ed6aba4 2445
af3ef9fe
NC
2446 if (filename)
2447 free (filename);
252b5132
RH
2448 }
2449
8af6b354
AM
2450 if (sort_line_sequences (table))
2451 return table;
0ee19663 2452
8af6b354 2453 fail:
a26a013f
AM
2454 while (table->sequences != NULL)
2455 {
2456 struct line_sequence* seq = table->sequences;
2457 table->sequences = table->sequences->prev_sequence;
2458 free (seq);
2459 }
8af6b354
AM
2460 if (table->files != NULL)
2461 free (table->files);
2462 if (table->dirs != NULL)
2463 free (table->dirs);
2464 return NULL;
252b5132
RH
2465}
2466
240d6706
NC
2467/* If ADDR is within TABLE set the output parameters and return the
2468 range of addresses covered by the entry used to fill them out.
2469 Otherwise set * FILENAME_PTR to NULL and return 0.
2470 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2471 are pointers to the objects to be filled in. */
252b5132 2472
240d6706 2473static bfd_vma
818a27ac
AM
2474lookup_address_in_line_info_table (struct line_info_table *table,
2475 bfd_vma addr,
818a27ac 2476 const char **filename_ptr,
9b8d1a36
CC
2477 unsigned int *linenumber_ptr,
2478 unsigned int *discriminator_ptr)
252b5132 2479{
0ee19663 2480 struct line_sequence *seq = NULL;
089e3718 2481 struct line_info *info;
0ee19663 2482 int low, high, mid;
e82ce529 2483
0ee19663
NC
2484 /* Binary search the array of sequences. */
2485 low = 0;
2486 high = table->num_sequences;
2487 while (low < high)
2488 {
2489 mid = (low + high) / 2;
2490 seq = &table->sequences[mid];
2491 if (addr < seq->low_pc)
2492 high = mid;
2493 else if (addr >= seq->last_line->address)
2494 low = mid + 1;
2495 else
2496 break;
2497 }
98591c73 2498
089e3718
IT
2499 /* Check for a valid sequence. */
2500 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
2501 goto fail;
2502
2503 if (!build_line_info_table (table, seq))
2504 goto fail;
2505
2506 /* Binary search the array of line information. */
2507 low = 0;
2508 high = seq->num_lines;
2509 info = NULL;
2510 while (low < high)
1ee24f27 2511 {
089e3718
IT
2512 mid = (low + high) / 2;
2513 info = seq->line_info_lookup[mid];
2514 if (addr < info->address)
2515 high = mid;
2516 else if (addr >= seq->line_info_lookup[mid + 1]->address)
2517 low = mid + 1;
2518 else
2519 break;
2520 }
0ee19663 2521
089e3718
IT
2522 /* Check for a valid line information entry. */
2523 if (info
2524 && addr >= info->address
2525 && addr < seq->line_info_lookup[mid + 1]->address
2526 && !(info->end_sequence || info == seq->last_line))
2527 {
2528 *filename_ptr = info->filename;
2529 *linenumber_ptr = info->line;
2530 if (discriminator_ptr)
2531 *discriminator_ptr = info->discriminator;
2532 return seq->last_line->address - seq->low_pc;
1ee24f27
DJ
2533 }
2534
089e3718 2535fail:
107601c8 2536 *filename_ptr = NULL;
240d6706 2537 return 0;
252b5132 2538}
98591c73 2539
0ee19663 2540/* Read in the .debug_ranges section for future reference. */
a13afe8e
FF
2541
2542static bfd_boolean
089e3718 2543read_debug_ranges (struct comp_unit * unit)
a13afe8e 2544{
089e3718
IT
2545 struct dwarf2_debug * stash = unit->stash;
2546
fc28f9aa 2547 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
93ee1e36 2548 stash->syms, 0,
089e3718
IT
2549 &stash->dwarf_ranges_buffer,
2550 &stash->dwarf_ranges_size);
a13afe8e
FF
2551}
2552
a092b084 2553/* Function table functions. */
252b5132 2554
089e3718
IT
2555static int
2556compare_lookup_funcinfos (const void * a, const void * b)
2557{
2558 const struct lookup_funcinfo * lookup1 = a;
2559 const struct lookup_funcinfo * lookup2 = b;
2560
2561 if (lookup1->low_addr < lookup2->low_addr)
2562 return -1;
2563 if (lookup1->low_addr > lookup2->low_addr)
2564 return 1;
2565 if (lookup1->high_addr < lookup2->high_addr)
2566 return -1;
2567 if (lookup1->high_addr > lookup2->high_addr)
2568 return 1;
2569
2570 return 0;
2571}
2572
2573static bfd_boolean
2574build_lookup_funcinfo_table (struct comp_unit * unit)
2575{
2576 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
2577 unsigned int number_of_functions = unit->number_of_functions;
2578 struct funcinfo *each;
2579 struct lookup_funcinfo *entry;
b6ddcd85 2580 size_t func_index;
089e3718
IT
2581 struct arange *range;
2582 bfd_vma low_addr, high_addr;
2583
2584 if (lookup_funcinfo_table || number_of_functions == 0)
2585 return TRUE;
2586
2587 /* Create the function info lookup table. */
2588 lookup_funcinfo_table = (struct lookup_funcinfo *)
2589 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
2590 if (lookup_funcinfo_table == NULL)
2591 return FALSE;
2592
2593 /* Populate the function info lookup table. */
b6ddcd85 2594 func_index = number_of_functions;
089e3718
IT
2595 for (each = unit->function_table; each; each = each->prev_func)
2596 {
b6ddcd85 2597 entry = &lookup_funcinfo_table[--func_index];
089e3718
IT
2598 entry->funcinfo = each;
2599
2600 /* Calculate the lowest and highest address for this function entry. */
2601 low_addr = entry->funcinfo->arange.low;
2602 high_addr = entry->funcinfo->arange.high;
2603
2604 for (range = entry->funcinfo->arange.next; range; range = range->next)
2605 {
2606 if (range->low < low_addr)
2607 low_addr = range->low;
2608 if (range->high > high_addr)
2609 high_addr = range->high;
2610 }
2611
2612 entry->low_addr = low_addr;
2613 entry->high_addr = high_addr;
2614 }
2615
b6ddcd85 2616 BFD_ASSERT (func_index == 0);
089e3718
IT
2617
2618 /* Sort the function by address. */
2619 qsort (lookup_funcinfo_table,
2620 number_of_functions,
2621 sizeof (struct lookup_funcinfo),
2622 compare_lookup_funcinfos);
2623
2624 /* Calculate the high watermark for each function in the lookup table. */
2625 high_addr = lookup_funcinfo_table[0].high_addr;
b6ddcd85 2626 for (func_index = 1; func_index < number_of_functions; func_index++)
089e3718 2627 {
b6ddcd85 2628 entry = &lookup_funcinfo_table[func_index];
089e3718
IT
2629 if (entry->high_addr > high_addr)
2630 high_addr = entry->high_addr;
2631 else
2632 entry->high_addr = high_addr;
2633 }
2634
2635 unit->lookup_funcinfo_table = lookup_funcinfo_table;
2636 return TRUE;
2637}
2638
e00e8198 2639/* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
240d6706
NC
2640 TRUE. Note that we need to find the function that has the smallest range
2641 that contains ADDR, to handle inlined functions without depending upon
2642 them being ordered in TABLE by increasing range. */
252b5132 2643
b34976b6 2644static bfd_boolean
4ab527b0 2645lookup_address_in_function_table (struct comp_unit *unit,
818a27ac 2646 bfd_vma addr,
e00e8198 2647 struct funcinfo **function_ptr)
252b5132 2648{
089e3718
IT
2649 unsigned int number_of_functions = unit->number_of_functions;
2650 struct lookup_funcinfo* lookup_funcinfo = NULL;
2651 struct funcinfo* funcinfo = NULL;
a13afe8e 2652 struct funcinfo* best_fit = NULL;
4ba3b326 2653 bfd_vma best_fit_len = 0;
089e3718 2654 bfd_size_type low, high, mid, first;
a13afe8e 2655 struct arange *arange;
252b5132 2656
cd6581da
NC
2657 if (number_of_functions == 0)
2658 return FALSE;
2659
089e3718
IT
2660 if (!build_lookup_funcinfo_table (unit))
2661 return FALSE;
2662
cd6581da
NC
2663 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
2664 return FALSE;
07d6d2b8 2665
089e3718
IT
2666 /* Find the first function in the lookup table which may contain the
2667 specified address. */
2668 low = 0;
2669 high = number_of_functions;
2670 first = high;
2671 while (low < high)
252b5132 2672 {
089e3718
IT
2673 mid = (low + high) / 2;
2674 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
2675 if (addr < lookup_funcinfo->low_addr)
2676 high = mid;
2677 else if (addr >= lookup_funcinfo->high_addr)
2678 low = mid + 1;
2679 else
2680 high = first = mid;
2681 }
2682
2683 /* Find the 'best' match for the address. The prior algorithm defined the
2684 best match as the function with the smallest address range containing
2685 the specified address. This definition should probably be changed to the
2686 innermost inline routine containing the address, but right now we want
2687 to get the same results we did before. */
2688 while (first < number_of_functions)
2689 {
2690 if (addr < unit->lookup_funcinfo_table[first].low_addr)
2691 break;
2692 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
2693
2694 for (arange = &funcinfo->arange; arange; arange = arange->next)
252b5132 2695 {
089e3718
IT
2696 if (addr < arange->low || addr >= arange->high)
2697 continue;
2698
2699 if (!best_fit
2700 || arange->high - arange->low < best_fit_len
2701 /* The following comparison is designed to return the same
2702 match as the previous algorithm for routines which have the
2703 same best fit length. */
2704 || (arange->high - arange->low == best_fit_len
2705 && funcinfo > best_fit))
a13afe8e 2706 {
089e3718
IT
2707 best_fit = funcinfo;
2708 best_fit_len = arange->high - arange->low;
a13afe8e 2709 }
252b5132 2710 }
98591c73 2711
089e3718 2712 first++;
a13afe8e 2713 }
089e3718
IT
2714
2715 if (!best_fit)
2716 return FALSE;
2717
2718 *function_ptr = best_fit;
2719 return TRUE;
252b5132
RH
2720}
2721
5420f73d
L
2722/* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2723 and LINENUMBER_PTR, and return TRUE. */
2724
2725static bfd_boolean
2726lookup_symbol_in_function_table (struct comp_unit *unit,
2727 asymbol *sym,
2728 bfd_vma addr,
2729 const char **filename_ptr,
2730 unsigned int *linenumber_ptr)
2731{
2732 struct funcinfo* each_func;
2733 struct funcinfo* best_fit = NULL;
4ba3b326 2734 bfd_vma best_fit_len = 0;
5420f73d
L
2735 struct arange *arange;
2736 const char *name = bfd_asymbol_name (sym);
2737 asection *sec = bfd_get_section (sym);
2738
2739 for (each_func = unit->function_table;
2740 each_func;
2741 each_func = each_func->prev_func)
2742 {
2743 for (arange = &each_func->arange;
2744 arange;
2745 arange = arange->next)
2746 {
2747 if ((!each_func->sec || each_func->sec == sec)
2748 && addr >= arange->low
2749 && addr < arange->high
650f284e 2750 && each_func->name
5420f73d
L
2751 && strcmp (name, each_func->name) == 0
2752 && (!best_fit
4ba3b326
TG
2753 || arange->high - arange->low < best_fit_len))
2754 {
2755 best_fit = each_func;
2756 best_fit_len = arange->high - arange->low;
2757 }
5420f73d
L
2758 }
2759 }
2760
2761 if (best_fit)
2762 {
2763 best_fit->sec = sec;
2764 *filename_ptr = best_fit->file;
2765 *linenumber_ptr = best_fit->line;
2766 return TRUE;
2767 }
2768 else
2769 return FALSE;
2770}
2771
2772/* Variable table functions. */
2773
2774/* If SYM is within variable table of UNIT, set FILENAME_PTR and
2775 LINENUMBER_PTR, and return TRUE. */
2776
2777static bfd_boolean
2778lookup_symbol_in_variable_table (struct comp_unit *unit,
2779 asymbol *sym,
5cf2e3f0 2780 bfd_vma addr,
5420f73d
L
2781 const char **filename_ptr,
2782 unsigned int *linenumber_ptr)
2783{
2784 const char *name = bfd_asymbol_name (sym);
2785 asection *sec = bfd_get_section (sym);
2786 struct varinfo* each;
2787
2788 for (each = unit->variable_table; each; each = each->prev_var)
2789 if (each->stack == 0
5cf2e3f0
L
2790 && each->file != NULL
2791 && each->name != NULL
2792 && each->addr == addr
5420f73d
L
2793 && (!each->sec || each->sec == sec)
2794 && strcmp (name, each->name) == 0)
2795 break;
2796
2797 if (each)
2798 {
2799 each->sec = sec;
2800 *filename_ptr = each->file;
2801 *linenumber_ptr = each->line;
2802 return TRUE;
2803 }
089e3718
IT
2804
2805 return FALSE;
5420f73d
L
2806}
2807
dfc19da6 2808static struct comp_unit *stash_comp_unit (struct dwarf2_debug *);
c327a44f
AM
2809static bfd_boolean comp_unit_maybe_decode_line_info (struct comp_unit *,
2810 struct dwarf2_debug *);
2811
52a93b95 2812static bfd_boolean
422f3d3d
PC
2813find_abstract_instance (struct comp_unit * unit,
2814 bfd_byte * orig_info_ptr,
2815 struct attribute * attr_ptr,
2816 const char ** pname,
2817 bfd_boolean * is_linkage,
2818 char ** filename_ptr,
2819 int * linenumber_ptr)
06f22d7e
FF
2820{
2821 bfd *abfd = unit->abfd;
f075ee0c 2822 bfd_byte *info_ptr;
dbb3fbbb 2823 bfd_byte *info_ptr_end;
06f22d7e
FF
2824 unsigned int abbrev_number, bytes_read, i;
2825 struct abbrev_info *abbrev;
5609a71e 2826 bfd_uint64_t die_ref = attr_ptr->u.val;
06f22d7e 2827 struct attribute attr;
52a93b95 2828 const char *name = NULL;
06f22d7e 2829
5609a71e
DJ
2830 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2831 is an offset from the .debug_info section, not the current CU. */
2832 if (attr_ptr->form == DW_FORM_ref_addr)
2833 {
2834 /* We only support DW_FORM_ref_addr within the same file, so
1b86808a
AM
2835 any relocations should be resolved already. Check this by
2836 testing for a zero die_ref; There can't be a valid reference
2837 to the header of a .debug_info section.
2838 DW_FORM_ref_addr is an offset relative to .debug_info.
2839 Normally when using the GNU linker this is accomplished by
2840 emitting a symbolic reference to a label, because .debug_info
2841 sections are linked at zero. When there are multiple section
2842 groups containing .debug_info, as there might be in a
2843 relocatable object file, it would be reasonable to assume that
2844 a symbolic reference to a label in any .debug_info section
2845 might be used. Since we lay out multiple .debug_info
2846 sections at non-zero VMAs (see place_sections), and read
2847 them contiguously into stash->info_ptr_memory, that means
2848 the reference is relative to stash->info_ptr_memory. */
2849 size_t total;
2850
2851 info_ptr = unit->stash->info_ptr_memory;
2852 info_ptr_end = unit->stash->info_ptr_end;
2853 total = info_ptr_end - info_ptr;
a4cd947a
AM
2854 if (!die_ref)
2855 return TRUE;
2856 else if (die_ref >= total)
52a93b95
AM
2857 {
2858 _bfd_error_handler
9793eb77 2859 (_("DWARF error: invalid abstract instance DIE ref"));
52a93b95
AM
2860 bfd_set_error (bfd_error_bad_value);
2861 return FALSE;
2862 }
1b86808a 2863 info_ptr += die_ref;
0a9c7b2b
NC
2864
2865 /* Now find the CU containing this pointer. */
2866 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
1b86808a 2867 info_ptr_end = unit->end_ptr;
0a9c7b2b
NC
2868 else
2869 {
2870 /* Check other CUs to see if they contain the abbrev. */
2871 struct comp_unit * u;
2872
2873 for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
2874 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2875 break;
2876
2877 if (u == NULL)
2878 for (u = unit->next_unit; u != NULL; u = u->next_unit)
2879 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2880 break;
2881
dfc19da6 2882 while (u == NULL)
1b86808a 2883 {
dfc19da6
AM
2884 u = stash_comp_unit (unit->stash);
2885 if (u == NULL)
2886 break;
2887 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2888 break;
2889 u = NULL;
1b86808a 2890 }
dfc19da6
AM
2891
2892 if (u == NULL)
2893 {
2894 _bfd_error_handler
2895 (_("DWARF error: unable to locate abstract instance DIE ref %"
2896 PRIu64), (uint64_t) die_ref);
2897 bfd_set_error (bfd_error_bad_value);
2898 return FALSE;
2899 }
2900 unit = u;
2901 info_ptr_end = unit->end_ptr;
0a9c7b2b 2902 }
5609a71e 2903 }
95e34fb4
NC
2904 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
2905 {
2906 info_ptr = read_alt_indirect_ref (unit, die_ref);
2907 if (info_ptr == NULL)
2908 {
4eca0228 2909 _bfd_error_handler
9793eb77 2910 (_("DWARF error: unable to read alt ref %" PRIu64),
8979927a 2911 (uint64_t) die_ref);
95e34fb4 2912 bfd_set_error (bfd_error_bad_value);
52a93b95 2913 return FALSE;
95e34fb4 2914 }
52a93b95
AM
2915 info_ptr_end = (unit->stash->alt_dwarf_info_buffer
2916 + unit->stash->alt_dwarf_info_size);
dbb3fbbb 2917
0a9c7b2b
NC
2918 /* FIXME: Do we need to locate the correct CU, in a similar
2919 fashion to the code in the DW_FORM_ref_addr case above ? */
95e34fb4 2920 }
68ffbac6 2921 else
dbb3fbbb 2922 {
1b86808a
AM
2923 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
2924 DW_FORM_ref_udata. These are all references relative to the
2925 start of the current CU. */
2926 size_t total;
2927
2928 info_ptr = unit->info_ptr_unit;
dbb3fbbb 2929 info_ptr_end = unit->end_ptr;
1b86808a
AM
2930 total = info_ptr_end - info_ptr;
2931 if (!die_ref || die_ref >= total)
2932 {
2933 _bfd_error_handler
9793eb77 2934 (_("DWARF error: invalid abstract instance DIE ref"));
1b86808a
AM
2935 bfd_set_error (bfd_error_bad_value);
2936 return FALSE;
2937 }
2938 info_ptr += die_ref;
dbb3fbbb 2939 }
95e34fb4 2940
4265548c
PA
2941 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
2942 FALSE, info_ptr_end);
06f22d7e
FF
2943 info_ptr += bytes_read;
2944
2945 if (abbrev_number)
2946 {
2947 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
2948 if (! abbrev)
2949 {
4eca0228 2950 _bfd_error_handler
9793eb77 2951 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
06f22d7e 2952 bfd_set_error (bfd_error_bad_value);
52a93b95 2953 return FALSE;
06f22d7e
FF
2954 }
2955 else
2956 {
d5cbaa15 2957 for (i = 0; i < abbrev->num_attrs; ++i)
06f22d7e 2958 {
8af6b354 2959 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
dbb3fbbb 2960 info_ptr, info_ptr_end);
8af6b354
AM
2961 if (info_ptr == NULL)
2962 break;
52a93b95
AM
2963 /* It doesn't ever make sense for DW_AT_specification to
2964 refer to the same DIE. Stop simple recursion. */
2965 if (info_ptr == orig_info_ptr)
2966 {
2967 _bfd_error_handler
9793eb77 2968 (_("DWARF error: abstract instance recursion detected"));
52a93b95
AM
2969 bfd_set_error (bfd_error_bad_value);
2970 return FALSE;
2971 }
26bf4e33
FF
2972 switch (attr.name)
2973 {
2974 case DW_AT_name:
643be349
JJ
2975 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2976 over DW_AT_name. */
60d77146 2977 if (name == NULL && is_str_attr (attr.form))
e00e8198
AM
2978 {
2979 name = attr.u.str;
2980 if (non_mangled (unit->lang))
2981 *is_linkage = TRUE;
2982 }
26bf4e33
FF
2983 break;
2984 case DW_AT_specification:
422f3d3d 2985 if (!find_abstract_instance (unit, info_ptr, &attr,
c8d3f932 2986 &name, is_linkage,
422f3d3d 2987 filename_ptr, linenumber_ptr))
52a93b95 2988 return FALSE;
26bf4e33 2989 break;
643be349 2990 case DW_AT_linkage_name:
d5cbaa15 2991 case DW_AT_MIPS_linkage_name:
60d77146
NC
2992 /* PR 16949: Corrupt debug info can place
2993 non-string forms into these attributes. */
6d74e8a1 2994 if (is_str_attr (attr.form))
e00e8198
AM
2995 {
2996 name = attr.u.str;
2997 *is_linkage = TRUE;
2998 }
d5cbaa15 2999 break;
422f3d3d 3000 case DW_AT_decl_file:
dfc19da6
AM
3001 if (!comp_unit_maybe_decode_line_info (unit, unit->stash))
3002 return FALSE;
422f3d3d
PC
3003 *filename_ptr = concat_filename (unit->line_table,
3004 attr.u.val);
3005 break;
3006 case DW_AT_decl_line:
3007 *linenumber_ptr = attr.u.val;
3008 break;
26bf4e33
FF
3009 default:
3010 break;
3011 }
06f22d7e
FF
3012 }
3013 }
3014 }
52a93b95
AM
3015 *pname = name;
3016 return TRUE;
06f22d7e
FF
3017}
3018
8af6b354
AM
3019static bfd_boolean
3020read_rangelist (struct comp_unit *unit, struct arange *arange,
3021 bfd_uint64_t offset)
a13afe8e
FF
3022{
3023 bfd_byte *ranges_ptr;
dbb3fbbb 3024 bfd_byte *ranges_end;
a13afe8e
FF
3025 bfd_vma base_address = unit->base_address;
3026
3027 if (! unit->stash->dwarf_ranges_buffer)
3028 {
3029 if (! read_debug_ranges (unit))
8af6b354 3030 return FALSE;
a13afe8e 3031 }
d8d1c398 3032
dbb3fbbb
NC
3033 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
3034 if (ranges_ptr < unit->stash->dwarf_ranges_buffer)
3035 return FALSE;
3036 ranges_end = unit->stash->dwarf_ranges_buffer + unit->stash->dwarf_ranges_size;
62f8d217 3037
a13afe8e
FF
3038 for (;;)
3039 {
3040 bfd_vma low_pc;
3041 bfd_vma high_pc;
3042
dbb3fbbb 3043 /* PR 17512: file: 62cada7d. */
62f8d217 3044 if (ranges_ptr + 2 * unit->addr_size > ranges_end)
dbb3fbbb
NC
3045 return FALSE;
3046
3047 low_pc = read_address (unit, ranges_ptr, ranges_end);
13d72a14 3048 ranges_ptr += unit->addr_size;
dbb3fbbb 3049 high_pc = read_address (unit, ranges_ptr, ranges_end);
13d72a14
AN
3050 ranges_ptr += unit->addr_size;
3051
a13afe8e
FF
3052 if (low_pc == 0 && high_pc == 0)
3053 break;
3054 if (low_pc == -1UL && high_pc != -1UL)
3055 base_address = high_pc;
3056 else
8af6b354 3057 {
a2a50954 3058 if (!arange_add (unit, arange,
8af6b354
AM
3059 base_address + low_pc, base_address + high_pc))
3060 return FALSE;
3061 }
a13afe8e 3062 }
8af6b354 3063 return TRUE;
a13afe8e
FF
3064}
3065
a092b084 3066/* DWARF2 Compilation unit functions. */
252b5132
RH
3067
3068/* Scan over each die in a comp. unit looking for functions to add
34b5e0b2 3069 to the function table and variables to the variable table. */
252b5132 3070
b34976b6 3071static bfd_boolean
5420f73d 3072scan_unit_for_symbols (struct comp_unit *unit)
252b5132
RH
3073{
3074 bfd *abfd = unit->abfd;
f075ee0c 3075 bfd_byte *info_ptr = unit->first_child_die_ptr;
05192282 3076 bfd_byte *info_ptr_end = unit->end_ptr;
52a93b95
AM
3077 int nesting_level = 0;
3078 struct nest_funcinfo {
3079 struct funcinfo *func;
3080 } *nested_funcs;
c955f9cd
JW
3081 int nested_funcs_size;
3082
3083 /* Maintain a stack of in-scope functions and inlined functions, which we
3084 can use to set the caller_func field. */
3085 nested_funcs_size = 32;
52a93b95
AM
3086 nested_funcs = (struct nest_funcinfo *)
3087 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
c955f9cd
JW
3088 if (nested_funcs == NULL)
3089 return FALSE;
52a93b95 3090 nested_funcs[nesting_level].func = 0;
252b5132 3091
52a93b95 3092 while (nesting_level >= 0)
252b5132
RH
3093 {
3094 unsigned int abbrev_number, bytes_read, i;
3095 struct abbrev_info *abbrev;
3096 struct attribute attr;
3097 struct funcinfo *func;
5420f73d 3098 struct varinfo *var;
a13afe8e
FF
3099 bfd_vma low_pc = 0;
3100 bfd_vma high_pc = 0;
c49ead2f 3101 bfd_boolean high_pc_relative = FALSE;
252b5132 3102
877a8638 3103 /* PR 17512: file: 9f405d9d. */
dbb3fbbb 3104 if (info_ptr >= info_ptr_end)
877a8638 3105 goto fail;
62f8d217 3106
4265548c
PA
3107 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3108 FALSE, info_ptr_end);
252b5132
RH
3109 info_ptr += bytes_read;
3110
3111 if (! abbrev_number)
3112 {
3113 nesting_level--;
3114 continue;
3115 }
98591c73 3116
e643cb45 3117 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
252b5132
RH
3118 if (! abbrev)
3119 {
e643cb45
NC
3120 static unsigned int previous_failed_abbrev = -1U;
3121
3122 /* Avoid multiple reports of the same missing abbrev. */
3123 if (abbrev_number != previous_failed_abbrev)
3124 {
3125 _bfd_error_handler
9793eb77 3126 (_("DWARF error: could not find abbrev number %u"),
e643cb45
NC
3127 abbrev_number);
3128 previous_failed_abbrev = abbrev_number;
3129 }
252b5132 3130 bfd_set_error (bfd_error_bad_value);
8af6b354 3131 goto fail;
252b5132 3132 }
98591c73 3133
5420f73d 3134 var = NULL;
06f22d7e 3135 if (abbrev->tag == DW_TAG_subprogram
5420f73d 3136 || abbrev->tag == DW_TAG_entry_point
06f22d7e 3137 || abbrev->tag == DW_TAG_inlined_subroutine)
252b5132 3138 {
dc810e39 3139 bfd_size_type amt = sizeof (struct funcinfo);
a50b1753 3140 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
8af6b354
AM
3141 if (func == NULL)
3142 goto fail;
4ab527b0 3143 func->tag = abbrev->tag;
252b5132
RH
3144 func->prev_func = unit->function_table;
3145 unit->function_table = func;
e643cb45 3146 unit->number_of_functions++;
bd210d54 3147 BFD_ASSERT (!unit->cached);
c955f9cd
JW
3148
3149 if (func->tag == DW_TAG_inlined_subroutine)
52a93b95
AM
3150 for (i = nesting_level; i-- != 0; )
3151 if (nested_funcs[i].func)
c955f9cd 3152 {
52a93b95 3153 func->caller_func = nested_funcs[i].func;
c955f9cd
JW
3154 break;
3155 }
52a93b95 3156 nested_funcs[nesting_level].func = func;
252b5132
RH
3157 }
3158 else
5420f73d
L
3159 {
3160 func = NULL;
3161 if (abbrev->tag == DW_TAG_variable)
3162 {
3163 bfd_size_type amt = sizeof (struct varinfo);
a50b1753 3164 var = (struct varinfo *) bfd_zalloc (abfd, amt);
8af6b354
AM
3165 if (var == NULL)
3166 goto fail;
5420f73d
L
3167 var->tag = abbrev->tag;
3168 var->stack = 1;
3169 var->prev_var = unit->variable_table;
3170 unit->variable_table = var;
e643cb45
NC
3171 /* PR 18205: Missing debug information can cause this
3172 var to be attached to an already cached unit. */
5420f73d 3173 }
c955f9cd
JW
3174
3175 /* No inline function in scope at this nesting level. */
52a93b95 3176 nested_funcs[nesting_level].func = 0;
5420f73d 3177 }
98591c73 3178
252b5132
RH
3179 for (i = 0; i < abbrev->num_attrs; ++i)
3180 {
52a93b95
AM
3181 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3182 unit, info_ptr, info_ptr_end);
8af6b354 3183 if (info_ptr == NULL)
8ecc1f20 3184 goto fail;
98591c73 3185
252b5132
RH
3186 if (func)
3187 {
3188 switch (attr.name)
3189 {
4ab527b0 3190 case DW_AT_call_file:
8af6b354
AM
3191 func->caller_file = concat_filename (unit->line_table,
3192 attr.u.val);
4ab527b0
FF
3193 break;
3194
3195 case DW_AT_call_line:
3196 func->caller_line = attr.u.val;
3197 break;
3198
06f22d7e 3199 case DW_AT_abstract_origin:
5d8e6b4d 3200 case DW_AT_specification:
422f3d3d
PC
3201 if (!find_abstract_instance (unit, info_ptr, &attr,
3202 &func->name,
3203 &func->is_linkage,
3204 &func->file,
3205 &func->line))
52a93b95 3206 goto fail;
06f22d7e
FF
3207 break;
3208
252b5132 3209 case DW_AT_name:
643be349
JJ
3210 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3211 over DW_AT_name. */
60d77146 3212 if (func->name == NULL && is_str_attr (attr.form))
e00e8198
AM
3213 {
3214 func->name = attr.u.str;
3215 if (non_mangled (unit->lang))
3216 func->is_linkage = TRUE;
3217 }
252b5132 3218 break;
98591c73 3219
643be349 3220 case DW_AT_linkage_name:
252b5132 3221 case DW_AT_MIPS_linkage_name:
60d77146
NC
3222 /* PR 16949: Corrupt debug info can place
3223 non-string forms into these attributes. */
3224 if (is_str_attr (attr.form))
e00e8198
AM
3225 {
3226 func->name = attr.u.str;
3227 func->is_linkage = TRUE;
3228 }
252b5132
RH
3229 break;
3230
3231 case DW_AT_low_pc:
a13afe8e 3232 low_pc = attr.u.val;
252b5132
RH
3233 break;
3234
3235 case DW_AT_high_pc:
a13afe8e 3236 high_pc = attr.u.val;
c49ead2f 3237 high_pc_relative = attr.form != DW_FORM_addr;
a13afe8e
FF
3238 break;
3239
3240 case DW_AT_ranges:
8af6b354
AM
3241 if (!read_rangelist (unit, &func->arange, attr.u.val))
3242 goto fail;
252b5132
RH
3243 break;
3244
5420f73d
L
3245 case DW_AT_decl_file:
3246 func->file = concat_filename (unit->line_table,
3247 attr.u.val);
3248 break;
3249
3250 case DW_AT_decl_line:
3251 func->line = attr.u.val;
3252 break;
3253
3254 default:
3255 break;
3256 }
3257 }
3258 else if (var)
3259 {
3260 switch (attr.name)
3261 {
3262 case DW_AT_name:
11855d8a
AM
3263 if (is_str_attr (attr.form))
3264 var->name = attr.u.str;
5420f73d
L
3265 break;
3266
3267 case DW_AT_decl_file:
3268 var->file = concat_filename (unit->line_table,
3269 attr.u.val);
3270 break;
3271
3272 case DW_AT_decl_line:
3273 var->line = attr.u.val;
3274 break;
3275
3276 case DW_AT_external:
3277 if (attr.u.val != 0)
3278 var->stack = 0;
3279 break;
3280
3281 case DW_AT_location:
5cf2e3f0 3282 switch (attr.form)
5420f73d 3283 {
5cf2e3f0
L
3284 case DW_FORM_block:
3285 case DW_FORM_block1:
3286 case DW_FORM_block2:
3287 case DW_FORM_block4:
c07cbdd7 3288 case DW_FORM_exprloc:
0d76029f
AM
3289 if (attr.u.blk->data != NULL
3290 && *attr.u.blk->data == DW_OP_addr)
5420f73d 3291 {
5cf2e3f0 3292 var->stack = 0;
98b880f4
JW
3293
3294 /* Verify that DW_OP_addr is the only opcode in the
3295 location, in which case the block size will be 1
3296 plus the address size. */
3297 /* ??? For TLS variables, gcc can emit
3298 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3299 which we don't handle here yet. */
3300 if (attr.u.blk->size == unit->addr_size + 1U)
3301 var->addr = bfd_get (unit->addr_size * 8,
3302 unit->abfd,
3303 attr.u.blk->data + 1);
5420f73d 3304 }
5cf2e3f0 3305 break;
d8d1c398 3306
5cf2e3f0
L
3307 default:
3308 break;
5420f73d
L
3309 }
3310 break;
3311
252b5132
RH
3312 default:
3313 break;
3314 }
3315 }
3316 }
3317
c49ead2f
MW
3318 if (high_pc_relative)
3319 high_pc += low_pc;
3320
a13afe8e
FF
3321 if (func && high_pc != 0)
3322 {
a2a50954 3323 if (!arange_add (unit, &func->arange, low_pc, high_pc))
8af6b354 3324 goto fail;
a13afe8e
FF
3325 }
3326
252b5132 3327 if (abbrev->has_children)
c955f9cd
JW
3328 {
3329 nesting_level++;
3330
3331 if (nesting_level >= nested_funcs_size)
3332 {
52a93b95 3333 struct nest_funcinfo *tmp;
c955f9cd
JW
3334
3335 nested_funcs_size *= 2;
52a93b95 3336 tmp = (struct nest_funcinfo *)
a2a50954 3337 bfd_realloc (nested_funcs,
52a93b95 3338 nested_funcs_size * sizeof (*nested_funcs));
c955f9cd 3339 if (tmp == NULL)
8af6b354 3340 goto fail;
c955f9cd
JW
3341 nested_funcs = tmp;
3342 }
52a93b95 3343 nested_funcs[nesting_level].func = 0;
c955f9cd 3344 }
252b5132
RH
3345 }
3346
c955f9cd 3347 free (nested_funcs);
b34976b6 3348 return TRUE;
8af6b354
AM
3349
3350 fail:
3351 free (nested_funcs);
3352 return FALSE;
252b5132
RH
3353}
3354
dfc19da6 3355/* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
5e38c3b8 3356 includes the compilation unit header that proceeds the DIE's, but
5c4491d3 3357 does not include the length field that precedes each compilation
5e38c3b8 3358 unit header. END_PTR points one past the end of this comp unit.
d03ba2a1 3359 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
252b5132
RH
3360
3361 This routine does not read the whole compilation unit; only enough
3362 to get to the line number information for the compilation unit. */
3363
3364static struct comp_unit *
0d161102 3365parse_comp_unit (struct dwarf2_debug *stash,
818a27ac 3366 bfd_vma unit_length,
f075ee0c 3367 bfd_byte *info_ptr_unit,
818a27ac 3368 unsigned int offset_size)
252b5132
RH
3369{
3370 struct comp_unit* unit;
f46c2da6 3371 unsigned int version;
8ce8c090 3372 bfd_uint64_t abbrev_offset = 0;
0041f7df
JK
3373 /* Initialize it just to avoid a GCC false warning. */
3374 unsigned int addr_size = -1;
252b5132 3375 struct abbrev_info** abbrevs;
252b5132
RH
3376 unsigned int abbrev_number, bytes_read, i;
3377 struct abbrev_info *abbrev;
3378 struct attribute attr;
f075ee0c
AM
3379 bfd_byte *info_ptr = stash->info_ptr;
3380 bfd_byte *end_ptr = info_ptr + unit_length;
dc810e39 3381 bfd_size_type amt;
a13afe8e
FF
3382 bfd_vma low_pc = 0;
3383 bfd_vma high_pc = 0;
a50b1753 3384 bfd *abfd = stash->bfd_ptr;
c49ead2f 3385 bfd_boolean high_pc_relative = FALSE;
0041f7df 3386 enum dwarf_unit_type unit_type;
3fde5a36 3387
dbb3fbbb 3388 version = read_2_bytes (abfd, info_ptr, end_ptr);
252b5132 3389 info_ptr += 2;
0041f7df 3390 if (version < 2 || version > 5)
252b5132 3391 {
67f101ee
NC
3392 /* PR 19872: A version number of 0 probably means that there is padding
3393 at the end of the .debug_info section. Gold puts it there when
3394 performing an incremental link, for example. So do not generate
3395 an error, just return a NULL. */
3396 if (version)
3397 {
4eca0228 3398 _bfd_error_handler
9793eb77
AM
3399 (_("DWARF error: found dwarf version '%u', this reader"
3400 " only handles version 2, 3, 4 and 5 information"), version);
67f101ee
NC
3401 bfd_set_error (bfd_error_bad_value);
3402 }
3403 return NULL;
252b5132
RH
3404 }
3405
0041f7df
JK
3406 if (version < 5)
3407 unit_type = DW_UT_compile;
3408 else
3409 {
3410 unit_type = read_1_byte (abfd, info_ptr, end_ptr);
3411 info_ptr += 1;
3412
3413 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3414 info_ptr += 1;
3415 }
3416
3417 BFD_ASSERT (offset_size == 4 || offset_size == 8);
3418 if (offset_size == 4)
3419 abbrev_offset = read_4_bytes (abfd, info_ptr, end_ptr);
3420 else
3421 abbrev_offset = read_8_bytes (abfd, info_ptr, end_ptr);
3422 info_ptr += offset_size;
3423
3424 if (version < 5)
3425 {
3426 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3427 info_ptr += 1;
3428 }
3429
3430 if (unit_type == DW_UT_type)
3431 {
3432 /* Skip type signature. */
3433 info_ptr += 8;
3434
3435 /* Skip type offset. */
3436 info_ptr += offset_size;
3437 }
3438
252b5132
RH
3439 if (addr_size > sizeof (bfd_vma))
3440 {
4eca0228 3441 _bfd_error_handler
695344c0 3442 /* xgettext: c-format */
9793eb77
AM
3443 (_("DWARF error: found address size '%u', this reader"
3444 " can not handle sizes greater than '%u'"),
a2a50954
AM
3445 addr_size,
3446 (unsigned int) sizeof (bfd_vma));
252b5132 3447 bfd_set_error (bfd_error_bad_value);
67f101ee 3448 return NULL;
252b5132
RH
3449 }
3450
ecb651f0 3451 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
252b5132 3452 {
4eca0228 3453 _bfd_error_handler
9793eb77
AM
3454 ("DWARF error: found address size '%u', this reader"
3455 " can only handle address sizes '2', '4' and '8'", addr_size);
252b5132 3456 bfd_set_error (bfd_error_bad_value);
67f101ee 3457 return NULL;
252b5132
RH
3458 }
3459
a092b084 3460 /* Read the abbrevs for this compilation unit into a table. */
51db3708 3461 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
252b5132 3462 if (! abbrevs)
67f101ee 3463 return NULL;
252b5132 3464
4265548c
PA
3465 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3466 FALSE, end_ptr);
252b5132
RH
3467 info_ptr += bytes_read;
3468 if (! abbrev_number)
3469 {
67f101ee
NC
3470 /* PR 19872: An abbrev number of 0 probably means that there is padding
3471 at the end of the .debug_abbrev section. Gold puts it there when
3472 performing an incremental link, for example. So do not generate
3473 an error, just return a NULL. */
3474 return NULL;
252b5132
RH
3475 }
3476
3477 abbrev = lookup_abbrev (abbrev_number, abbrevs);
3478 if (! abbrev)
3479 {
9793eb77 3480 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
4eca0228 3481 abbrev_number);
252b5132 3482 bfd_set_error (bfd_error_bad_value);
67f101ee 3483 return NULL;
252b5132 3484 }
98591c73 3485
dc810e39 3486 amt = sizeof (struct comp_unit);
a50b1753 3487 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
8af6b354
AM
3488 if (unit == NULL)
3489 return NULL;
252b5132 3490 unit->abfd = abfd;
5609a71e 3491 unit->version = version;
98591c73 3492 unit->addr_size = addr_size;
d03ba2a1 3493 unit->offset_size = offset_size;
252b5132
RH
3494 unit->abbrevs = abbrevs;
3495 unit->end_ptr = end_ptr;
d03ba2a1 3496 unit->stash = stash;
c0c28ab8 3497 unit->info_ptr_unit = info_ptr_unit;
252b5132
RH
3498
3499 for (i = 0; i < abbrev->num_attrs; ++i)
3500 {
dbb3fbbb 3501 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
8af6b354
AM
3502 if (info_ptr == NULL)
3503 return NULL;
252b5132
RH
3504
3505 /* Store the data if it is of an attribute we want to keep in a
3506 partial symbol table. */
3507 switch (attr.name)
3508 {
3509 case DW_AT_stmt_list:
3510 unit->stmtlist = 1;
482e2e37 3511 unit->line_offset = attr.u.val;
252b5132
RH
3512 break;
3513
3514 case DW_AT_name:
11855d8a
AM
3515 if (is_str_attr (attr.form))
3516 unit->name = attr.u.str;
252b5132
RH
3517 break;
3518
3519 case DW_AT_low_pc:
a13afe8e
FF
3520 low_pc = attr.u.val;
3521 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3522 this is the base address to use when reading location
089e3718 3523 lists or range lists. */
a2a50954
AM
3524 if (abbrev->tag == DW_TAG_compile_unit)
3525 unit->base_address = low_pc;
252b5132
RH
3526 break;
3527
3528 case DW_AT_high_pc:
a13afe8e 3529 high_pc = attr.u.val;
c49ead2f 3530 high_pc_relative = attr.form != DW_FORM_addr;
a13afe8e
FF
3531 break;
3532
3533 case DW_AT_ranges:
8af6b354
AM
3534 if (!read_rangelist (unit, &unit->arange, attr.u.val))
3535 return NULL;
252b5132
RH
3536 break;
3537
3538 case DW_AT_comp_dir:
3539 {
f075ee0c 3540 char *comp_dir = attr.u.str;
877a8638
NC
3541
3542 /* PR 17512: file: 1fe726be. */
3543 if (! is_str_attr (attr.form))
3544 {
4eca0228 3545 _bfd_error_handler
9793eb77 3546 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
877a8638
NC
3547 comp_dir = NULL;
3548 }
3549
252b5132
RH
3550 if (comp_dir)
3551 {
3552 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3553 directory, get rid of it. */
818a27ac 3554 char *cp = strchr (comp_dir, ':');
252b5132
RH
3555
3556 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3557 comp_dir = cp + 1;
3558 }
3559 unit->comp_dir = comp_dir;
3560 break;
3561 }
3562
e00e8198
AM
3563 case DW_AT_language:
3564 unit->lang = attr.u.val;
3565 break;
3566
252b5132
RH
3567 default:
3568 break;
3569 }
3570 }
c49ead2f
MW
3571 if (high_pc_relative)
3572 high_pc += low_pc;
a13afe8e 3573 if (high_pc != 0)
709d67f1 3574 {
a2a50954 3575 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
8af6b354 3576 return NULL;
709d67f1 3577 }
252b5132
RH
3578
3579 unit->first_child_die_ptr = info_ptr;
3580 return unit;
3581}
3582
6dd55cb7
L
3583/* Return TRUE if UNIT may contain the address given by ADDR. When
3584 there are functions written entirely with inline asm statements, the
3585 range info in the compilation unit header may not be correct. We
3586 need to consult the line info table to see if a compilation unit
3587 really contains the given address. */
252b5132 3588
b34976b6 3589static bfd_boolean
818a27ac 3590comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
252b5132 3591{
709d67f1
AM
3592 struct arange *arange;
3593
3594 if (unit->error)
3595 return FALSE;
3596
3597 arange = &unit->arange;
3598 do
3599 {
3600 if (addr >= arange->low && addr < arange->high)
3601 return TRUE;
3602 arange = arange->next;
3603 }
3604 while (arange);
3605
3606 return FALSE;
252b5132
RH
3607}
3608
252b5132
RH
3609/* If UNIT contains ADDR, set the output parameters to the values for
3610 the line containing ADDR. The output parameters, FILENAME_PTR,
e00e8198 3611 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
98591c73 3612 to be filled in.
252b5132 3613
240d6706
NC
3614 Returns the range of addresses covered by the entry that was used
3615 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
252b5132 3616
240d6706 3617static bfd_vma
818a27ac
AM
3618comp_unit_find_nearest_line (struct comp_unit *unit,
3619 bfd_vma addr,
3620 const char **filename_ptr,
e00e8198 3621 struct funcinfo **function_ptr,
818a27ac 3622 unsigned int *linenumber_ptr,
9b8d1a36 3623 unsigned int *discriminator_ptr,
818a27ac 3624 struct dwarf2_debug *stash)
252b5132 3625{
b34976b6 3626 bfd_boolean func_p;
98591c73 3627
c327a44f 3628 if (!comp_unit_maybe_decode_line_info (unit, stash))
b34976b6 3629 return FALSE;
252b5132 3630
e00e8198
AM
3631 *function_ptr = NULL;
3632 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
3633 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
3634 stash->inliner_chain = *function_ptr;
240d6706
NC
3635
3636 return lookup_address_in_line_info_table (unit->line_table, addr,
3637 filename_ptr,
3638 linenumber_ptr,
3639 discriminator_ptr);
252b5132
RH
3640}
3641
bd210d54
NC
3642/* Check to see if line info is already decoded in a comp_unit.
3643 If not, decode it. Returns TRUE if no errors were encountered;
5420f73d
L
3644 FALSE otherwise. */
3645
3646static bfd_boolean
bd210d54
NC
3647comp_unit_maybe_decode_line_info (struct comp_unit *unit,
3648 struct dwarf2_debug *stash)
5420f73d
L
3649{
3650 if (unit->error)
3651 return FALSE;
3652
3653 if (! unit->line_table)
3654 {
3655 if (! unit->stmtlist)
3656 {
3657 unit->error = 1;
3658 return FALSE;
3659 }
3660
3661 unit->line_table = decode_line_info (unit, stash);
3662
3663 if (! unit->line_table)
3664 {
3665 unit->error = 1;
3666 return FALSE;
3667 }
3668
3669 if (unit->first_child_die_ptr < unit->end_ptr
3670 && ! scan_unit_for_symbols (unit))
3671 {
3672 unit->error = 1;
3673 return FALSE;
3674 }
3675 }
3676
bd210d54
NC
3677 return TRUE;
3678}
3679
3680/* If UNIT contains SYM at ADDR, set the output parameters to the
3681 values for the line containing SYM. The output parameters,
3682 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3683 filled in.
3684
3685 Return TRUE if UNIT contains SYM, and no errors were encountered;
3686 FALSE otherwise. */
3687
3688static bfd_boolean
3689comp_unit_find_line (struct comp_unit *unit,
3690 asymbol *sym,
3691 bfd_vma addr,
3692 const char **filename_ptr,
3693 unsigned int *linenumber_ptr,
3694 struct dwarf2_debug *stash)
3695{
3696 if (!comp_unit_maybe_decode_line_info (unit, stash))
3697 return FALSE;
3698
5420f73d
L
3699 if (sym->flags & BSF_FUNCTION)
3700 return lookup_symbol_in_function_table (unit, sym, addr,
3701 filename_ptr,
3702 linenumber_ptr);
bd210d54
NC
3703
3704 return lookup_symbol_in_variable_table (unit, sym, addr,
3705 filename_ptr,
3706 linenumber_ptr);
3707}
3708
3709static struct funcinfo *
3710reverse_funcinfo_list (struct funcinfo *head)
3711{
3712 struct funcinfo *rhead;
3713 struct funcinfo *temp;
3714
3715 for (rhead = NULL; head; head = temp)
3716 {
3717 temp = head->prev_func;
3718 head->prev_func = rhead;
3719 rhead = head;
3720 }
3721 return rhead;
3722}
3723
3724static struct varinfo *
3725reverse_varinfo_list (struct varinfo *head)
3726{
3727 struct varinfo *rhead;
3728 struct varinfo *temp;
3729
3730 for (rhead = NULL; head; head = temp)
3731 {
3732 temp = head->prev_var;
3733 head->prev_var = rhead;
3734 rhead = head;
3735 }
3736 return rhead;
3737}
3738
3739/* Extract all interesting funcinfos and varinfos of a compilation
3740 unit into hash tables for faster lookup. Returns TRUE if no
3741 errors were enountered; FALSE otherwise. */
3742
3743static bfd_boolean
3744comp_unit_hash_info (struct dwarf2_debug *stash,
3745 struct comp_unit *unit,
3746 struct info_hash_table *funcinfo_hash_table,
3747 struct info_hash_table *varinfo_hash_table)
3748{
3749 struct funcinfo* each_func;
3750 struct varinfo* each_var;
3751 bfd_boolean okay = TRUE;
3752
3753 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
3754
3755 if (!comp_unit_maybe_decode_line_info (unit, stash))
3756 return FALSE;
3757
3758 BFD_ASSERT (!unit->cached);
3759
3760 /* To preserve the original search order, we went to visit the function
3761 infos in the reversed order of the list. However, making the list
3762 bi-directional use quite a bit of extra memory. So we reverse
3763 the list first, traverse the list in the now reversed order and
3764 finally reverse the list again to get back the original order. */
3765 unit->function_table = reverse_funcinfo_list (unit->function_table);
3766 for (each_func = unit->function_table;
3767 each_func && okay;
3768 each_func = each_func->prev_func)
3769 {
089e3718 3770 /* Skip nameless functions. */
bd210d54
NC
3771 if (each_func->name)
3772 /* There is no need to copy name string into hash table as
3773 name string is either in the dwarf string buffer or
3774 info in the stash. */
3775 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
3776 (void*) each_func, FALSE);
3777 }
3778 unit->function_table = reverse_funcinfo_list (unit->function_table);
3779 if (!okay)
3780 return FALSE;
3781
3782 /* We do the same for variable infos. */
3783 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3784 for (each_var = unit->variable_table;
3785 each_var && okay;
3786 each_var = each_var->prev_var)
3787 {
3788 /* Skip stack vars and vars with no files or names. */
3789 if (each_var->stack == 0
3790 && each_var->file != NULL
3791 && each_var->name != NULL)
3792 /* There is no need to copy name string into hash table as
3793 name string is either in the dwarf string buffer or
3794 info in the stash. */
3795 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
3796 (void*) each_var, FALSE);
3797 }
3798
3799 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3800 unit->cached = TRUE;
3801 return okay;
5420f73d
L
3802}
3803
e2f6d277
NC
3804/* Locate a section in a BFD containing debugging info. The search starts
3805 from the section after AFTER_SEC, or from the first section in the BFD if
3806 AFTER_SEC is NULL. The search works by examining the names of the
fc28f9aa
TG
3807 sections. There are three permissiable names. The first two are given
3808 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3809 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
e2f6d277
NC
3810 This is a variation on the .debug_info section which has a checksum
3811 describing the contents appended onto the name. This allows the linker to
3812 identify and discard duplicate debugging sections for different
3813 compilation units. */
a092b084
NC
3814#define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3815
3816static asection *
fc28f9aa 3817find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
93ee1e36 3818 asection *after_sec)
a092b084 3819{
a2a50954
AM
3820 asection *msec;
3821 const char *look;
3822
3823 if (after_sec == NULL)
3824 {
3825 look = debug_sections[debug_info].uncompressed_name;
3826 msec = bfd_get_section_by_name (abfd, look);
3827 if (msec != NULL)
3828 return msec;
a092b084 3829
a2a50954
AM
3830 look = debug_sections[debug_info].compressed_name;
3831 if (look != NULL)
3832 {
3833 msec = bfd_get_section_by_name (abfd, look);
3834 if (msec != NULL)
3835 return msec;
3836 }
a092b084 3837
a2a50954
AM
3838 for (msec = abfd->sections; msec != NULL; msec = msec->next)
3839 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3840 return msec;
3841
3842 return NULL;
3843 }
3844
3845 for (msec = after_sec->next; msec != NULL; msec = msec->next)
a092b084 3846 {
a2a50954
AM
3847 look = debug_sections[debug_info].uncompressed_name;
3848 if (strcmp (msec->name, look) == 0)
a092b084
NC
3849 return msec;
3850
a2a50954
AM
3851 look = debug_sections[debug_info].compressed_name;
3852 if (look != NULL && strcmp (msec->name, look) == 0)
1b315056
CS
3853 return msec;
3854
0112cd26 3855 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
a092b084 3856 return msec;
a092b084
NC
3857 }
3858
3859 return NULL;
3860}
3861
93ee1e36
AM
3862/* Transfer VMAs from object file to separate debug file. */
3863
3864static void
3865set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
3866{
3867 asection *s, *d;
3868
3869 for (s = orig_bfd->sections, d = debug_bfd->sections;
3870 s != NULL && d != NULL;
3871 s = s->next, d = d->next)
3872 {
3873 if ((d->flags & SEC_DEBUGGING) != 0)
3874 break;
3875 /* ??? Assumes 1-1 correspondence between sections in the
3876 two files. */
3877 if (strcmp (s->name, d->name) == 0)
3878 {
3879 d->output_section = s->output_section;
3880 d->output_offset = s->output_offset;
3881 d->vma = s->vma;
3882 }
3883 }
3884}
3885
5609a71e 3886/* Unset vmas for adjusted sections in STASH. */
d4c32a81
L
3887
3888static void
3889unset_sections (struct dwarf2_debug *stash)
3890{
93ee1e36 3891 int i;
5609a71e 3892 struct adjusted_section *p;
d4c32a81 3893
5609a71e
DJ
3894 i = stash->adjusted_section_count;
3895 p = stash->adjusted_sections;
d4c32a81
L
3896 for (; i > 0; i--, p++)
3897 p->section->vma = 0;
3898}
3899
93ee1e36
AM
3900/* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3901 relocatable object file. VMAs are normally all zero in relocatable
3902 object files, so if we want to distinguish locations in sections by
3903 address we need to set VMAs so the sections do not overlap. We
3904 also set VMA on .debug_info so that when we have multiple
3905 .debug_info sections (or the linkonce variant) they also do not
3906 overlap. The multiple .debug_info sections make up a single
3907 logical section. ??? We should probably do the same for other
3908 debug sections. */
35ccda9e
L
3909
3910static bfd_boolean
93ee1e36 3911place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
35ccda9e 3912{
93ee1e36 3913 bfd *abfd;
5609a71e 3914 struct adjusted_section *p;
93ee1e36
AM
3915 int i;
3916 const char *debug_info_name;
d4c32a81 3917
5609a71e 3918 if (stash->adjusted_section_count != 0)
35ccda9e 3919 {
5609a71e
DJ
3920 i = stash->adjusted_section_count;
3921 p = stash->adjusted_sections;
d4c32a81
L
3922 for (; i > 0; i--, p++)
3923 p->section->vma = p->adj_vma;
93ee1e36 3924 return TRUE;
d4c32a81 3925 }
93ee1e36
AM
3926
3927 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
3928 i = 0;
3929 abfd = orig_bfd;
3930 while (1)
d4c32a81
L
3931 {
3932 asection *sect;
35ccda9e 3933
d4c32a81 3934 for (sect = abfd->sections; sect != NULL; sect = sect->next)
35ccda9e 3935 {
5609a71e
DJ
3936 int is_debug_info;
3937
cd0449ab 3938 if ((sect->output_section != NULL
93ee1e36
AM
3939 && sect->output_section != sect
3940 && (sect->flags & SEC_DEBUGGING) == 0)
cd0449ab 3941 || sect->vma != 0)
5609a71e
DJ
3942 continue;
3943
93ee1e36
AM
3944 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3945 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
d4c32a81 3946
93ee1e36
AM
3947 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3948 && !is_debug_info)
d4c32a81
L
3949 continue;
3950
3951 i++;
3952 }
93ee1e36
AM
3953 if (abfd == stash->bfd_ptr)
3954 break;
3955 abfd = stash->bfd_ptr;
3956 }
3957
3958 if (i <= 1)
3959 stash->adjusted_section_count = -1;
3960 else
3961 {
3962 bfd_vma last_vma = 0, last_dwarf = 0;
3963 bfd_size_type amt = i * sizeof (struct adjusted_section);
d4c32a81 3964
93ee1e36
AM
3965 p = (struct adjusted_section *) bfd_malloc (amt);
3966 if (p == NULL)
d4c32a81
L
3967 return FALSE;
3968
5609a71e
DJ
3969 stash->adjusted_sections = p;
3970 stash->adjusted_section_count = i;
d4c32a81 3971
93ee1e36
AM
3972 abfd = orig_bfd;
3973 while (1)
d4c32a81 3974 {
93ee1e36 3975 asection *sect;
d4c32a81 3976
93ee1e36
AM
3977 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3978 {
3979 bfd_size_type sz;
3980 int is_debug_info;
5609a71e 3981
93ee1e36
AM
3982 if ((sect->output_section != NULL
3983 && sect->output_section != sect
3984 && (sect->flags & SEC_DEBUGGING) == 0)
3985 || sect->vma != 0)
3986 continue;
5609a71e 3987
93ee1e36
AM
3988 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3989 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
d4c32a81 3990
93ee1e36
AM
3991 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3992 && !is_debug_info)
3993 continue;
d4c32a81 3994
93ee1e36 3995 sz = sect->rawsize ? sect->rawsize : sect->size;
5609a71e 3996
93ee1e36
AM
3997 if (is_debug_info)
3998 {
3999 BFD_ASSERT (sect->alignment_power == 0);
4000 sect->vma = last_dwarf;
4001 last_dwarf += sz;
4002 }
4003 else
4004 {
4005 /* Align the new address to the current section
4006 alignment. */
4007 last_vma = ((last_vma
29f628db
DV
4008 + ~(-((bfd_vma) 1 << sect->alignment_power)))
4009 & (-((bfd_vma) 1 << sect->alignment_power)));
93ee1e36
AM
4010 sect->vma = last_vma;
4011 last_vma += sz;
4012 }
d4c32a81 4013
93ee1e36
AM
4014 p->section = sect;
4015 p->adj_vma = sect->vma;
4016 p++;
4017 }
4018 if (abfd == stash->bfd_ptr)
4019 break;
4020 abfd = stash->bfd_ptr;
35ccda9e
L
4021 }
4022 }
4023
93ee1e36
AM
4024 if (orig_bfd != stash->bfd_ptr)
4025 set_debug_vma (orig_bfd, stash->bfd_ptr);
4026
35ccda9e
L
4027 return TRUE;
4028}
4029
bd210d54
NC
4030/* Look up a funcinfo by name using the given info hash table. If found,
4031 also update the locations pointed to by filename_ptr and linenumber_ptr.
4032
4033 This function returns TRUE if a funcinfo that matches the given symbol
4034 and address is found with any error; otherwise it returns FALSE. */
4035
4036static bfd_boolean
4037info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
4038 asymbol *sym,
4039 bfd_vma addr,
4040 const char **filename_ptr,
4041 unsigned int *linenumber_ptr)
4042{
4043 struct funcinfo* each_func;
4044 struct funcinfo* best_fit = NULL;
4ba3b326 4045 bfd_vma best_fit_len = 0;
bd210d54
NC
4046 struct info_list_node *node;
4047 struct arange *arange;
4048 const char *name = bfd_asymbol_name (sym);
4049 asection *sec = bfd_get_section (sym);
4050
4051 for (node = lookup_info_hash_table (hash_table, name);
4052 node;
4053 node = node->next)
4054 {
a50b1753 4055 each_func = (struct funcinfo *) node->info;
bd210d54
NC
4056 for (arange = &each_func->arange;
4057 arange;
4058 arange = arange->next)
4059 {
4060 if ((!each_func->sec || each_func->sec == sec)
4061 && addr >= arange->low
4062 && addr < arange->high
4063 && (!best_fit
4ba3b326
TG
4064 || arange->high - arange->low < best_fit_len))
4065 {
4066 best_fit = each_func;
4067 best_fit_len = arange->high - arange->low;
4068 }
bd210d54
NC
4069 }
4070 }
4071
4072 if (best_fit)
4073 {
4074 best_fit->sec = sec;
4075 *filename_ptr = best_fit->file;
4076 *linenumber_ptr = best_fit->line;
4077 return TRUE;
4078 }
4079
4080 return FALSE;
4081}
4082
4083/* Look up a varinfo by name using the given info hash table. If found,
4084 also update the locations pointed to by filename_ptr and linenumber_ptr.
4085
4086 This function returns TRUE if a varinfo that matches the given symbol
4087 and address is found with any error; otherwise it returns FALSE. */
4088
4089static bfd_boolean
4090info_hash_lookup_varinfo (struct info_hash_table *hash_table,
4091 asymbol *sym,
4092 bfd_vma addr,
4093 const char **filename_ptr,
4094 unsigned int *linenumber_ptr)
4095{
4096 const char *name = bfd_asymbol_name (sym);
4097 asection *sec = bfd_get_section (sym);
4098 struct varinfo* each;
4099 struct info_list_node *node;
4100
4101 for (node = lookup_info_hash_table (hash_table, name);
4102 node;
4103 node = node->next)
4104 {
a50b1753 4105 each = (struct varinfo *) node->info;
bd210d54
NC
4106 if (each->addr == addr
4107 && (!each->sec || each->sec == sec))
4108 {
4109 each->sec = sec;
4110 *filename_ptr = each->file;
4111 *linenumber_ptr = each->line;
4112 return TRUE;
4113 }
4114 }
4115
4116 return FALSE;
4117}
4118
4119/* Update the funcinfo and varinfo info hash tables if they are
4120 not up to date. Returns TRUE if there is no error; otherwise
4121 returns FALSE and disable the info hash tables. */
4122
4123static bfd_boolean
4124stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
4125{
4126 struct comp_unit *each;
4127
4128 /* Exit if hash tables are up-to-date. */
4129 if (stash->all_comp_units == stash->hash_units_head)
4130 return TRUE;
4131
4132 if (stash->hash_units_head)
4133 each = stash->hash_units_head->prev_unit;
4134 else
4135 each = stash->last_comp_unit;
4136
4137 while (each)
4138 {
4139 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
4140 stash->varinfo_hash_table))
4141 {
4142 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4143 return FALSE;
4144 }
4145 each = each->prev_unit;
4146 }
4147
4148 stash->hash_units_head = stash->all_comp_units;
4149 return TRUE;
4150}
4151
089e3718 4152/* Check consistency of info hash tables. This is for debugging only. */
bd210d54
NC
4153
4154static void ATTRIBUTE_UNUSED
4155stash_verify_info_hash_table (struct dwarf2_debug *stash)
4156{
4157 struct comp_unit *each_unit;
4158 struct funcinfo *each_func;
4159 struct varinfo *each_var;
4160 struct info_list_node *node;
4161 bfd_boolean found;
4162
4163 for (each_unit = stash->all_comp_units;
4164 each_unit;
4165 each_unit = each_unit->next_unit)
4166 {
4167 for (each_func = each_unit->function_table;
4168 each_func;
4169 each_func = each_func->prev_func)
4170 {
4171 if (!each_func->name)
4172 continue;
4173 node = lookup_info_hash_table (stash->funcinfo_hash_table,
4174 each_func->name);
4175 BFD_ASSERT (node);
4176 found = FALSE;
4177 while (node && !found)
4178 {
4179 found = node->info == each_func;
4180 node = node->next;
4181 }
4182 BFD_ASSERT (found);
4183 }
4184
4185 for (each_var = each_unit->variable_table;
4186 each_var;
4187 each_var = each_var->prev_var)
4188 {
4189 if (!each_var->name || !each_var->file || each_var->stack)
4190 continue;
4191 node = lookup_info_hash_table (stash->varinfo_hash_table,
4192 each_var->name);
4193 BFD_ASSERT (node);
4194 found = FALSE;
4195 while (node && !found)
4196 {
4197 found = node->info == each_var;
4198 node = node->next;
4199 }
4200 BFD_ASSERT (found);
4201 }
4202 }
4203}
4204
4205/* Check to see if we want to enable the info hash tables, which consume
4206 quite a bit of memory. Currently we only check the number times
4207 bfd_dwarf2_find_line is called. In the future, we may also want to
4208 take the number of symbols into account. */
4209
4210static void
4211stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
4212{
4213 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
4214
4215 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
4216 return;
4217
4218 /* FIXME: Maybe we should check the reduce_memory_overheads
4219 and optimize fields in the bfd_link_info structure ? */
4220
4221 /* Create hash tables. */
4222 stash->funcinfo_hash_table = create_info_hash_table (abfd);
4223 stash->varinfo_hash_table = create_info_hash_table (abfd);
4224 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
4225 {
4226 /* Turn off info hashes if any allocation above fails. */
4227 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4228 return;
4229 }
4230 /* We need a forced update so that the info hash tables will
4231 be created even though there is no compilation unit. That
4232 happens if STASH_INFO_HASH_TRIGGER is 0. */
4233 stash_maybe_update_info_hash_tables (stash);
4234 stash->info_hash_status = STASH_INFO_HASH_ON;
4235}
4236
4237/* Find the file and line associated with a symbol and address using the
4238 info hash tables of a stash. If there is a match, the function returns
4239 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4240 otherwise it returns FALSE. */
4241
4242static bfd_boolean
4243stash_find_line_fast (struct dwarf2_debug *stash,
4244 asymbol *sym,
4245 bfd_vma addr,
4246 const char **filename_ptr,
4247 unsigned int *linenumber_ptr)
4248{
4249 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
4250
4251 if (sym->flags & BSF_FUNCTION)
4252 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
4253 filename_ptr, linenumber_ptr);
4254 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
4255 filename_ptr, linenumber_ptr);
4256}
4257
cd0449ab
AM
4258/* Save current section VMAs. */
4259
4260static bfd_boolean
4261save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
4262{
4263 asection *s;
4264 unsigned int i;
4265
4266 if (abfd->section_count == 0)
4267 return TRUE;
4268 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
4269 if (stash->sec_vma == NULL)
4270 return FALSE;
d7f848c3 4271 stash->sec_vma_count = abfd->section_count;
0eb32b6e
AM
4272 for (i = 0, s = abfd->sections;
4273 s != NULL && i < abfd->section_count;
4274 i++, s = s->next)
cd0449ab
AM
4275 {
4276 if (s->output_section != NULL)
4277 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
4278 else
4279 stash->sec_vma[i] = s->vma;
4280 }
4281 return TRUE;
4282}
4283
4284/* Compare current section VMAs against those at the time the stash
4285 was created. If find_nearest_line is used in linker warnings or
4286 errors early in the link process, the debug info stash will be
4287 invalid for later calls. This is because we relocate debug info
4288 sections, so the stashed section contents depend on symbol values,
4289 which in turn depend on section VMAs. */
4290
4291static bfd_boolean
4292section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
4293{
4294 asection *s;
4295 unsigned int i;
4296
d7f848c3
NC
4297 /* PR 24334: If the number of sections in ABFD has changed between
4298 when the stash was created and now, then we cannot trust the
4299 stashed vma information. */
4300 if (abfd->section_count != stash->sec_vma_count)
4301 return FALSE;
4b24dd1a 4302
0eb32b6e
AM
4303 for (i = 0, s = abfd->sections;
4304 s != NULL && i < abfd->section_count;
4305 i++, s = s->next)
cd0449ab
AM
4306 {
4307 bfd_vma vma;
4308
4309 if (s->output_section != NULL)
4310 vma = s->output_section->vma + s->output_offset;
4311 else
4312 vma = s->vma;
4313 if (vma != stash->sec_vma[i])
4314 return FALSE;
4315 }
4316 return TRUE;
4317}
4318
2ca7691a
TG
4319/* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4320 If DEBUG_BFD is not specified, we read debug information from ABFD
4321 or its gnu_debuglink. The results will be stored in PINFO.
4322 The function returns TRUE iff debug information is ready. */
4323
4324bfd_boolean
4325_bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
93ee1e36
AM
4326 const struct dwarf_debug_section *debug_sections,
4327 asymbol **symbols,
4328 void **pinfo,
4329 bfd_boolean do_place)
2ca7691a
TG
4330{
4331 bfd_size_type amt = sizeof (struct dwarf2_debug);
4332 bfd_size_type total_size;
4333 asection *msec;
4334 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4335
4336 if (stash != NULL)
cd0449ab 4337 {
90ed9b8b 4338 if (stash->orig_bfd == abfd
07d6d2b8
AM
4339 && section_vma_same (abfd, stash))
4340 {
4341 /* Check that we did previously find some debug information
4342 before attempting to make use of it. */
4343 if (stash->bfd_ptr != NULL)
4344 {
4345 if (do_place && !place_sections (abfd, stash))
4346 return FALSE;
4347 return TRUE;
4348 }
4349
4350 return FALSE;
4351 }
cd0449ab
AM
4352 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
4353 memset (stash, 0, amt);
4354 }
4355 else
4356 {
4357 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
4358 if (! stash)
4359 return FALSE;
4360 }
90ed9b8b 4361 stash->orig_bfd = abfd;
2ca7691a 4362 stash->debug_sections = debug_sections;
1c37913d 4363 stash->syms = symbols;
cd0449ab
AM
4364 if (!save_section_vma (abfd, stash))
4365 return FALSE;
2ca7691a
TG
4366
4367 *pinfo = stash;
4368
4369 if (debug_bfd == NULL)
4370 debug_bfd = abfd;
4371
4372 msec = find_debug_info (debug_bfd, debug_sections, NULL);
4373 if (msec == NULL && abfd == debug_bfd)
4374 {
2425a30e
NC
4375 char * debug_filename;
4376
4377 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
4378 if (debug_filename == NULL)
4379 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2ca7691a
TG
4380
4381 if (debug_filename == NULL)
4382 /* No dwarf2 info, and no gnu_debuglink to follow.
4383 Note that at this point the stash has been allocated, but
4384 contains zeros. This lets future calls to this function
4385 fail more quickly. */
4386 return FALSE;
4387
22b31fea
AM
4388 debug_bfd = bfd_openr (debug_filename, NULL);
4389 free (debug_filename);
4390 if (debug_bfd == NULL)
4391 /* FIXME: Should we report our failure to follow the debuglink ? */
4392 return FALSE;
4393
bf150a0b 4394 /* Set BFD_DECOMPRESS to decompress debug sections. */
22b31fea
AM
4395 debug_bfd->flags |= BFD_DECOMPRESS;
4396 if (!bfd_check_format (debug_bfd, bfd_object)
2ca7691a 4397 || (msec = find_debug_info (debug_bfd,
93ee1e36
AM
4398 debug_sections, NULL)) == NULL
4399 || !bfd_generic_link_read_symbols (debug_bfd))
2ca7691a 4400 {
22b31fea 4401 bfd_close (debug_bfd);
2ca7691a
TG
4402 return FALSE;
4403 }
93ee1e36
AM
4404
4405 symbols = bfd_get_outsymbols (debug_bfd);
4406 stash->syms = symbols;
1c37913d 4407 stash->close_on_cleanup = TRUE;
2ca7691a 4408 }
1c37913d 4409 stash->bfd_ptr = debug_bfd;
2ca7691a 4410
93ee1e36
AM
4411 if (do_place
4412 && !place_sections (abfd, stash))
4413 return FALSE;
4414
2ca7691a
TG
4415 /* There can be more than one DWARF2 info section in a BFD these
4416 days. First handle the easy case when there's only one. If
4417 there's more than one, try case two: none of the sections is
4418 compressed. In that case, read them all in and produce one
4419 large stash. We do this in two passes - in the first pass we
4420 just accumulate the section sizes, and in the second pass we
4421 read in the section's contents. (The allows us to avoid
4422 reallocing the data as we add sections to the stash.) If
4423 some or all sections are compressed, then do things the slow
4424 way, with a bunch of reallocs. */
4425
4426 if (! find_debug_info (debug_bfd, debug_sections, msec))
4427 {
4428 /* Case 1: only one info section. */
4429 total_size = msec->size;
4430 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
4431 symbols, 0,
4432 &stash->info_ptr_memory, &total_size))
4433 return FALSE;
4434 }
4435 else
4436 {
4437 /* Case 2: multiple sections. */
4438 for (total_size = 0;
4439 msec;
4440 msec = find_debug_info (debug_bfd, debug_sections, msec))
4441 total_size += msec->size;
4442
4443 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
4444 if (stash->info_ptr_memory == NULL)
4445 return FALSE;
4446
4447 total_size = 0;
4448 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
4449 msec;
4450 msec = find_debug_info (debug_bfd, debug_sections, msec))
4451 {
4452 bfd_size_type size;
4453
4454 size = msec->size;
4455 if (size == 0)
4456 continue;
4457
4458 if (!(bfd_simple_get_relocated_section_contents
4459 (debug_bfd, msec, stash->info_ptr_memory + total_size,
4460 symbols)))
4461 return FALSE;
4462
4463 total_size += size;
4464 }
4465 }
4466
4467 stash->info_ptr = stash->info_ptr_memory;
4468 stash->info_ptr_end = stash->info_ptr + total_size;
4469 stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
4470 stash->sec_info_ptr = stash->info_ptr;
2ca7691a
TG
4471 return TRUE;
4472}
4473
dfc19da6
AM
4474/* Parse the next DWARF2 compilation unit at STASH->INFO_PTR. */
4475
4476static struct comp_unit *
4477stash_comp_unit (struct dwarf2_debug *stash)
4478{
4479 bfd_size_type length;
4480 unsigned int offset_size;
4481 bfd_byte *info_ptr_unit = stash->info_ptr;
4482
4483 if (stash->info_ptr >= stash->info_ptr_end)
4484 return NULL;
4485
4486 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr,
4487 stash->info_ptr_end);
4488 /* A 0xffffff length is the DWARF3 way of indicating
4489 we use 64-bit offsets, instead of 32-bit offsets. */
4490 if (length == 0xffffffff)
4491 {
4492 offset_size = 8;
4493 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4,
4494 stash->info_ptr_end);
4495 stash->info_ptr += 12;
4496 }
4497 /* A zero length is the IRIX way of indicating 64-bit offsets,
4498 mostly because the 64-bit length will generally fit in 32
4499 bits, and the endianness helps. */
4500 else if (length == 0)
4501 {
4502 offset_size = 8;
4503 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4,
4504 stash->info_ptr_end);
4505 stash->info_ptr += 8;
4506 }
4507 /* In the absence of the hints above, we assume 32-bit DWARF2
4508 offsets even for targets with 64-bit addresses, because:
4509 a) most of the time these targets will not have generated
4510 more than 2Gb of debug info and so will not need 64-bit
4511 offsets,
4512 and
4513 b) if they do use 64-bit offsets but they are not using
4514 the size hints that are tested for above then they are
4515 not conforming to the DWARF3 standard anyway. */
4516 else
4517 {
4518 offset_size = 4;
4519 stash->info_ptr += 4;
4520 }
4521
4522 if (length != 0
4523 && stash->info_ptr + length <= stash->info_ptr_end
4524 && stash->info_ptr + length > stash->info_ptr)
4525 {
4526 struct comp_unit *each = parse_comp_unit (stash, length, info_ptr_unit,
4527 offset_size);
4528 if (each)
4529 {
4530 if (stash->all_comp_units)
4531 stash->all_comp_units->prev_unit = each;
4532 else
4533 stash->last_comp_unit = each;
4534
4535 each->next_unit = stash->all_comp_units;
4536 stash->all_comp_units = each;
4537
4538 stash->info_ptr += length;
4539
4540 if ((bfd_size_type) (stash->info_ptr - stash->sec_info_ptr)
4541 == stash->sec->size)
4542 {
4543 stash->sec = find_debug_info (stash->bfd_ptr,
4544 stash->debug_sections,
4545 stash->sec);
4546 stash->sec_info_ptr = stash->info_ptr;
4547 }
4548 return each;
4549 }
4550 }
4551
4552 /* Don't trust any of the DWARF info after a corrupted length or
4553 parse error. */
4554 stash->info_ptr = stash->info_ptr_end;
4555 return NULL;
4556}
4557
3eb185c9
TT
4558/* Hash function for an asymbol. */
4559
4560static hashval_t
4561hash_asymbol (const void *sym)
4562{
4563 const asymbol *asym = sym;
4564 return htab_hash_string (asym->name);
4565}
4566
4567/* Equality function for asymbols. */
4568
4569static int
4570eq_asymbol (const void *a, const void *b)
4571{
4572 const asymbol *sa = a;
4573 const asymbol *sb = b;
4574 return strcmp (sa->name, sb->name) == 0;
4575}
4576
425bd9e1
NC
4577/* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4578 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4579 symbol in SYMBOLS and return the difference between the low_pc and
4580 the symbol's address. Returns 0 if no suitable symbol could be found. */
4581
4582bfd_signed_vma
4583_bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
4584{
4585 struct dwarf2_debug *stash;
4586 struct comp_unit * unit;
3eb185c9
TT
4587 htab_t sym_hash;
4588 bfd_signed_vma result = 0;
4589 asymbol ** psym;
425bd9e1
NC
4590
4591 stash = (struct dwarf2_debug *) *pinfo;
4592
219d6836 4593 if (stash == NULL || symbols == NULL)
425bd9e1
NC
4594 return 0;
4595
3eb185c9
TT
4596 sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
4597 NULL, xcalloc, free);
4598 for (psym = symbols; * psym != NULL; psym++)
4599 {
4600 asymbol * sym = * psym;
4601
4602 if (sym->flags & BSF_FUNCTION && sym->section != NULL)
4603 {
4604 void **slot = htab_find_slot (sym_hash, sym, INSERT);
4605 *slot = sym;
4606 }
4607 }
4608
425bd9e1
NC
4609 for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
4610 {
4611 struct funcinfo * func;
4612
c327a44f 4613 comp_unit_maybe_decode_line_info (unit, stash);
425bd9e1
NC
4614
4615 for (func = unit->function_table; func != NULL; func = func->prev_func)
4616 if (func->name && func->arange.low)
4617 {
3eb185c9 4618 asymbol search, *sym;
425bd9e1
NC
4619
4620 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4621
3eb185c9
TT
4622 search.name = func->name;
4623 sym = htab_find (sym_hash, &search);
4624 if (sym != NULL)
425bd9e1 4625 {
3eb185c9
TT
4626 result = ((bfd_signed_vma) func->arange.low) -
4627 ((bfd_signed_vma) (sym->value + sym->section->vma));
4628 goto done;
425bd9e1
NC
4629 }
4630 }
4631 }
4632
3eb185c9
TT
4633 done:
4634 htab_delete (sym_hash);
4635 return result;
425bd9e1
NC
4636}
4637
bec42b15
NC
4638/* Find the source code location of SYMBOL. If SYMBOL is NULL
4639 then find the nearest source code location corresponding to
4640 the address SECTION + OFFSET.
4641 Returns TRUE if the line is found without error and fills in
4642 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4643 NULL the FUNCTIONNAME_PTR is also filled in.
4644 SYMBOLS contains the symbol table for ABFD.
fc28f9aa 4645 DEBUG_SECTIONS contains the name of the dwarf debug sections.
bec42b15
NC
4646 field and in the abbreviation offset, or zero to indicate that the
4647 default value should be used. */
252b5132 4648
fb167eb2
AM
4649bfd_boolean
4650_bfd_dwarf2_find_nearest_line (bfd *abfd,
4651 asymbol **symbols,
4652 asymbol *symbol,
4653 asection *section,
4654 bfd_vma offset,
4655 const char **filename_ptr,
4656 const char **functionname_ptr,
4657 unsigned int *linenumber_ptr,
4658 unsigned int *discriminator_ptr,
4659 const struct dwarf_debug_section *debug_sections,
fb167eb2 4660 void **pinfo)
252b5132
RH
4661{
4662 /* Read each compilation unit from the section .debug_info, and check
4663 to see if it contains the address we are searching for. If yes,
4664 lookup the address, and return the line number info. If no, go
98591c73 4665 on to the next compilation unit.
252b5132
RH
4666
4667 We keep a list of all the previously read compilation units, and
98591c73 4668 a pointer to the next un-read compilation unit. Check the
a092b084 4669 previously read units before reading more. */
1ba54ee0 4670 struct dwarf2_debug *stash;
a092b084 4671 /* What address are we looking for? */
1ba54ee0 4672 bfd_vma addr;
252b5132 4673 struct comp_unit* each;
e00e8198 4674 struct funcinfo *function = NULL;
240d6706 4675 bfd_boolean found = FALSE;
bec42b15 4676 bfd_boolean do_line;
d4c32a81 4677
2ca7691a
TG
4678 *filename_ptr = NULL;
4679 if (functionname_ptr != NULL)
4680 *functionname_ptr = NULL;
4681 *linenumber_ptr = 0;
f725daa8
CC
4682 if (discriminator_ptr)
4683 *discriminator_ptr = 0;
d4c32a81 4684
93ee1e36
AM
4685 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
4686 symbols, pinfo,
4687 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
2ca7691a 4688 return FALSE;
d4c32a81 4689
2ca7691a 4690 stash = (struct dwarf2_debug *) *pinfo;
d4c32a81 4691
fb167eb2 4692 do_line = symbol != NULL;
bec42b15
NC
4693 if (do_line)
4694 {
fb167eb2 4695 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
bec42b15 4696 section = bfd_get_section (symbol);
fb167eb2 4697 addr = symbol->value;
bec42b15 4698 }
bec42b15 4699 else
fb167eb2
AM
4700 {
4701 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
4702 addr = offset;
3239a423
AB
4703
4704 /* If we have no SYMBOL but the section we're looking at is not a
07d6d2b8
AM
4705 code section, then take a look through the list of symbols to see
4706 if we have a symbol at the address we're looking for. If we do
4707 then use this to look up line information. This will allow us to
4708 give file and line results for data symbols. We exclude code
4709 symbols here, if we look up a function symbol and then look up the
4710 line information we'll actually return the line number for the
4711 opening '{' rather than the function definition line. This is
4712 because looking up by symbol uses the line table, in which the
4713 first line for a function is usually the opening '{', while
4714 looking up the function by section + offset uses the
4715 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
4716 which will be the line of the function name. */
97e83a10 4717 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
3239a423
AB
4718 {
4719 asymbol **tmp;
4720
4721 for (tmp = symbols; (*tmp) != NULL; ++tmp)
4722 if ((*tmp)->the_bfd == abfd
4723 && (*tmp)->section == section
4724 && (*tmp)->value == offset
4725 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
4726 {
4727 symbol = *tmp;
4728 do_line = TRUE;
07d6d2b8
AM
4729 /* For local symbols, keep going in the hope we find a
4730 global. */
4731 if ((symbol->flags & BSF_GLOBAL) != 0)
4732 break;
3239a423
AB
4733 }
4734 }
fb167eb2 4735 }
bec42b15 4736
1ba54ee0 4737 if (section->output_section)
6dd55cb7 4738 addr += section->output_section->vma + section->output_offset;
1ba54ee0 4739 else
6dd55cb7 4740 addr += section->vma;
a092b084 4741
98591c73 4742 /* A null info_ptr indicates that there is no dwarf2 info
a092b084 4743 (or that an error occured while setting up the stash). */
252b5132 4744 if (! stash->info_ptr)
2ca7691a 4745 return FALSE;
252b5132 4746
4ab527b0
FF
4747 stash->inliner_chain = NULL;
4748
a092b084 4749 /* Check the previously read comp. units first. */
bd210d54
NC
4750 if (do_line)
4751 {
4752 /* The info hash tables use quite a bit of memory. We may not want to
4753 always use them. We use some heuristics to decide if and when to
4754 turn it on. */
4755 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
4756 stash_maybe_enable_info_hash_tables (abfd, stash);
4757
4758 /* Keep info hash table up to date if they are available. Note that we
089e3718 4759 may disable the hash tables if there is any error duing update. */
bd210d54
NC
4760 if (stash->info_hash_status == STASH_INFO_HASH_ON)
4761 stash_maybe_update_info_hash_tables (stash);
4762
4763 if (stash->info_hash_status == STASH_INFO_HASH_ON)
4764 {
4765 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
4766 linenumber_ptr);
4767 if (found)
4768 goto done;
4769 }
0d161102 4770 else
bd210d54
NC
4771 {
4772 /* Check the previously read comp. units first. */
4773 for (each = stash->all_comp_units; each; each = each->next_unit)
4774 if ((symbol->flags & BSF_FUNCTION) == 0
a2a50954 4775 || each->arange.high == 0
bd210d54
NC
4776 || comp_unit_contains_address (each, addr))
4777 {
4778 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
4779 linenumber_ptr, stash);
4780 if (found)
4781 goto done;
4782 }
4783 }
4784 }
4785 else
4786 {
240d6706
NC
4787 bfd_vma min_range = (bfd_vma) -1;
4788 const char * local_filename = NULL;
e00e8198 4789 struct funcinfo *local_function = NULL;
240d6706
NC
4790 unsigned int local_linenumber = 0;
4791 unsigned int local_discriminator = 0;
96691246 4792
709d67f1
AM
4793 for (each = stash->all_comp_units; each; each = each->next_unit)
4794 {
240d6706
NC
4795 bfd_vma range = (bfd_vma) -1;
4796
a2a50954
AM
4797 found = ((each->arange.high == 0
4798 || comp_unit_contains_address (each, addr))
240d6706
NC
4799 && (range = comp_unit_find_nearest_line (each, addr,
4800 & local_filename,
e00e8198 4801 & local_function,
240d6706
NC
4802 & local_linenumber,
4803 & local_discriminator,
4804 stash)) != 0);
709d67f1 4805 if (found)
240d6706
NC
4806 {
4807 /* PRs 15935 15994: Bogus debug information may have provided us
4808 with an erroneous match. We attempt to counter this by
4809 selecting the match that has the smallest address range
4810 associated with it. (We are assuming that corrupt debug info
4811 will tend to result in extra large address ranges rather than
4812 extra small ranges).
4813
4814 This does mean that we scan through all of the CUs associated
4815 with the bfd each time this function is called. But this does
4816 have the benefit of producing consistent results every time the
4817 function is called. */
4818 if (range <= min_range)
4819 {
4820 if (filename_ptr && local_filename)
4821 * filename_ptr = local_filename;
e00e8198
AM
4822 if (local_function)
4823 function = local_function;
240d6706
NC
4824 if (discriminator_ptr && local_discriminator)
4825 * discriminator_ptr = local_discriminator;
4826 if (local_linenumber)
4827 * linenumber_ptr = local_linenumber;
4828 min_range = range;
4829 }
4830 }
4831 }
4832
4833 if (* linenumber_ptr)
4834 {
4835 found = TRUE;
4836 goto done;
709d67f1 4837 }
5420f73d
L
4838 }
4839
5420f73d 4840 /* Read each remaining comp. units checking each as they are read. */
dfc19da6 4841 while ((each = stash_comp_unit (stash)) != NULL)
5420f73d 4842 {
dfc19da6
AM
4843 /* DW_AT_low_pc and DW_AT_high_pc are optional for
4844 compilation units. If we don't have them (i.e.,
4845 unit->high == 0), we need to consult the line info table
4846 to see if a compilation unit contains the given
4847 address. */
4848 if (do_line)
4849 found = (((symbol->flags & BSF_FUNCTION) == 0
4850 || each->arange.high == 0
4851 || comp_unit_contains_address (each, addr))
4852 && comp_unit_find_line (each, symbol, addr,
4853 filename_ptr,
4854 linenumber_ptr,
4855 stash));
9defd221 4856 else
dfc19da6
AM
4857 found = ((each->arange.high == 0
4858 || comp_unit_contains_address (each, addr))
4859 && comp_unit_find_nearest_line (each, addr,
4860 filename_ptr,
4861 &function,
4862 linenumber_ptr,
4863 discriminator_ptr,
4864 stash) != 0);
4865
4866 if (found)
4867 break;
5420f73d
L
4868 }
4869
a2a50954 4870 done:
e00e8198
AM
4871 if (function)
4872 {
923b198a 4873 if (!function->is_linkage)
e00e8198 4874 {
923b198a
AM
4875 asymbol *fun;
4876 bfd_vma sec_vma;
4877
4878 fun = _bfd_elf_find_function (abfd, symbols, section, offset,
4879 *filename_ptr ? NULL : filename_ptr,
4880 functionname_ptr);
4881 sec_vma = section->vma;
4882 if (section->output_section != NULL)
4883 sec_vma = section->output_section->vma + section->output_offset;
4884 if (fun != NULL
4885 && fun->value + sec_vma == function->arange.low)
4886 function->name = *functionname_ptr;
4887 /* Even if we didn't find a linkage name, say that we have
4888 to stop a repeated search of symbols. */
e00e8198
AM
4889 function->is_linkage = TRUE;
4890 }
923b198a 4891 *functionname_ptr = function->name;
e00e8198 4892 }
d4c32a81
L
4893 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
4894 unset_sections (stash);
4895
4896 return found;
5420f73d
L
4897}
4898
4ab527b0
FF
4899bfd_boolean
4900_bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
4901 const char **filename_ptr,
4902 const char **functionname_ptr,
4903 unsigned int *linenumber_ptr,
4904 void **pinfo)
4905{
4906 struct dwarf2_debug *stash;
4907
a50b1753 4908 stash = (struct dwarf2_debug *) *pinfo;
4ab527b0
FF
4909 if (stash)
4910 {
4911 struct funcinfo *func = stash->inliner_chain;
bec42b15 4912
4ab527b0
FF
4913 if (func && func->caller_func)
4914 {
4915 *filename_ptr = func->caller_file;
4916 *functionname_ptr = func->caller_func->name;
4917 *linenumber_ptr = func->caller_line;
4918 stash->inliner_chain = func->caller_func;
bec42b15 4919 return TRUE;
4ab527b0
FF
4920 }
4921 }
4922
bec42b15 4923 return FALSE;
4ab527b0
FF
4924}
4925
35330cce 4926void
d9071b0c 4927_bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
35330cce 4928{
5bb3703f 4929 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
35330cce 4930 struct comp_unit *each;
35330cce 4931
d9071b0c 4932 if (abfd == NULL || stash == NULL)
35330cce
NC
4933 return;
4934
4935 for (each = stash->all_comp_units; each; each = each->next_unit)
4936 {
34b5e0b2 4937 struct abbrev_info **abbrevs = each->abbrevs;
90b5b1a5
NC
4938 struct funcinfo *function_table = each->function_table;
4939 struct varinfo *variable_table = each->variable_table;
34b5e0b2 4940 size_t i;
35330cce 4941
34b5e0b2 4942 for (i = 0; i < ABBREV_HASH_SIZE; i++)
d8d1c398 4943 {
34b5e0b2 4944 struct abbrev_info *abbrev = abbrevs[i];
35330cce 4945
34b5e0b2 4946 while (abbrev)
d8d1c398 4947 {
34b5e0b2
NC
4948 free (abbrev->attrs);
4949 abbrev = abbrev->next;
d8d1c398
AM
4950 }
4951 }
35330cce
NC
4952
4953 if (each->line_table)
d8d1c398 4954 {
34b5e0b2
NC
4955 free (each->line_table->dirs);
4956 free (each->line_table->files);
d8d1c398 4957 }
90b5b1a5
NC
4958
4959 while (function_table)
4960 {
4961 if (function_table->file)
4962 {
4963 free (function_table->file);
4964 function_table->file = NULL;
4965 }
4966
4967 if (function_table->caller_file)
4968 {
4969 free (function_table->caller_file);
4970 function_table->caller_file = NULL;
4971 }
4972 function_table = function_table->prev_func;
4973 }
4974
089e3718
IT
4975 if (each->lookup_funcinfo_table)
4976 {
4977 free (each->lookup_funcinfo_table);
4978 each->lookup_funcinfo_table = NULL;
4979 }
4980
90b5b1a5
NC
4981 while (variable_table)
4982 {
4983 if (variable_table->file)
4984 {
4985 free (variable_table->file);
4986 variable_table->file = NULL;
4987 }
4988
4989 variable_table = variable_table->prev_var;
4990 }
35330cce
NC
4991 }
4992
b55ec8b6
AM
4993 if (stash->funcinfo_hash_table)
4994 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
4995 if (stash->varinfo_hash_table)
4996 bfd_hash_table_free (&stash->varinfo_hash_table->base);
5d0900eb
AM
4997 if (stash->dwarf_abbrev_buffer)
4998 free (stash->dwarf_abbrev_buffer);
4999 if (stash->dwarf_line_buffer)
5000 free (stash->dwarf_line_buffer);
5001 if (stash->dwarf_str_buffer)
5002 free (stash->dwarf_str_buffer);
0041f7df
JK
5003 if (stash->dwarf_line_str_buffer)
5004 free (stash->dwarf_line_str_buffer);
5d0900eb
AM
5005 if (stash->dwarf_ranges_buffer)
5006 free (stash->dwarf_ranges_buffer);
5007 if (stash->info_ptr_memory)
5008 free (stash->info_ptr_memory);
1c37913d
AM
5009 if (stash->close_on_cleanup)
5010 bfd_close (stash->bfd_ptr);
95e34fb4
NC
5011 if (stash->alt_dwarf_str_buffer)
5012 free (stash->alt_dwarf_str_buffer);
5013 if (stash->alt_dwarf_info_buffer)
5014 free (stash->alt_dwarf_info_buffer);
cd0449ab
AM
5015 if (stash->sec_vma)
5016 free (stash->sec_vma);
93ee1e36
AM
5017 if (stash->adjusted_sections)
5018 free (stash->adjusted_sections);
95e34fb4
NC
5019 if (stash->alt_bfd_ptr)
5020 bfd_close (stash->alt_bfd_ptr);
35330cce 5021}
e00e8198
AM
5022
5023/* Find the function to a particular section and offset,
5024 for error reporting. */
5025
923b198a 5026asymbol *
e00e8198
AM
5027_bfd_elf_find_function (bfd *abfd,
5028 asymbol **symbols,
5029 asection *section,
5030 bfd_vma offset,
5031 const char **filename_ptr,
5032 const char **functionname_ptr)
5033{
5034 struct elf_find_function_cache
5035 {
5036 asection *last_section;
5037 asymbol *func;
5038 const char *filename;
5039 bfd_size_type func_size;
5040 } *cache;
5041
5042 if (symbols == NULL)
923b198a 5043 return NULL;
e00e8198
AM
5044
5045 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
923b198a 5046 return NULL;
e00e8198
AM
5047
5048 cache = elf_tdata (abfd)->elf_find_function_cache;
5049 if (cache == NULL)
5050 {
5051 cache = bfd_zalloc (abfd, sizeof (*cache));
5052 elf_tdata (abfd)->elf_find_function_cache = cache;
5053 if (cache == NULL)
923b198a 5054 return NULL;
e00e8198
AM
5055 }
5056 if (cache->last_section != section
5057 || cache->func == NULL
5058 || offset < cache->func->value
5059 || offset >= cache->func->value + cache->func_size)
5060 {
5061 asymbol *file;
5062 bfd_vma low_func;
5063 asymbol **p;
5064 /* ??? Given multiple file symbols, it is impossible to reliably
5065 choose the right file name for global symbols. File symbols are
5066 local symbols, and thus all file symbols must sort before any
5067 global symbols. The ELF spec may be interpreted to say that a
5068 file symbol must sort before other local symbols, but currently
5069 ld -r doesn't do this. So, for ld -r output, it is possible to
5070 make a better choice of file name for local symbols by ignoring
5071 file symbols appearing after a given local symbol. */
5072 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
5073 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5074
5075 file = NULL;
5076 low_func = 0;
5077 state = nothing_seen;
5078 cache->filename = NULL;
5079 cache->func = NULL;
5080 cache->func_size = 0;
5081 cache->last_section = section;
5082
5083 for (p = symbols; *p != NULL; p++)
5084 {
5085 asymbol *sym = *p;
5086 bfd_vma code_off;
5087 bfd_size_type size;
5088
5089 if ((sym->flags & BSF_FILE) != 0)
5090 {
5091 file = sym;
5092 if (state == symbol_seen)
5093 state = file_after_symbol_seen;
5094 continue;
5095 }
5096
5097 size = bed->maybe_function_sym (sym, section, &code_off);
5098 if (size != 0
5099 && code_off <= offset
5100 && (code_off > low_func
5101 || (code_off == low_func
5102 && size > cache->func_size)))
5103 {
5104 cache->func = sym;
5105 cache->func_size = size;
5106 cache->filename = NULL;
5107 low_func = code_off;
5108 if (file != NULL
5109 && ((sym->flags & BSF_LOCAL) != 0
5110 || state != file_after_symbol_seen))
5111 cache->filename = bfd_asymbol_name (file);
5112 }
5113 if (state == nothing_seen)
5114 state = symbol_seen;
5115 }
5116 }
5117
5118 if (cache->func == NULL)
923b198a 5119 return NULL;
e00e8198
AM
5120
5121 if (filename_ptr)
5122 *filename_ptr = cache->filename;
5123 if (functionname_ptr)
5124 *functionname_ptr = bfd_asymbol_name (cache->func);
5125
923b198a 5126 return cache->func;
e00e8198 5127}