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