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