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