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