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