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