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