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