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