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