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