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