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