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