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