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