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