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