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