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