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