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