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