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