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