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