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