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