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