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