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