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