(read_section): Take pointer to bfd_size_type instead
[binutils-gdb.git] / bfd / dwarf2.c
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
6 (gavin@cygnus.com).
7
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
15
16 This file is part of BFD.
17
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 3 of the License, or (at
21 your option) any later version.
22
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31 MA 02110-1301, USA. */
32
33 #include "sysdep.h"
34 #include "bfd.h"
35 #include "libiberty.h"
36 #include "libbfd.h"
37 #include "elf-bfd.h"
38 #include "elf/dwarf2.h"
39
40 /* The data in the .debug_line statement prologue looks like this. */
41
42 struct line_head
43 {
44 bfd_vma total_length;
45 unsigned short version;
46 bfd_vma prologue_length;
47 unsigned char minimum_instruction_length;
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 loadable_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 /* The next unread compilation unit within the .debug_info section.
93 Zero indicates that the .debug_info section has not been loaded
94 into a buffer yet. */
95 bfd_byte *info_ptr;
96
97 /* Pointer to the end of the .debug_info section memory buffer. */
98 bfd_byte *info_ptr_end;
99
100 /* Pointer to the bfd, section and address of the beginning of the
101 section. The bfd might be different than expected because of
102 gnu_debuglink sections. */
103 bfd * bfd;
104 asection *sec;
105 bfd_byte *sec_info_ptr;
106
107 /* Pointer to the symbol table. */
108 asymbol **syms;
109
110 /* Pointer to the .debug_abbrev section loaded into memory. */
111 bfd_byte *dwarf_abbrev_buffer;
112
113 /* Length of the loaded .debug_abbrev section. */
114 bfd_size_type dwarf_abbrev_size;
115
116 /* Buffer for decode_line_info. */
117 bfd_byte *dwarf_line_buffer;
118
119 /* Length of the loaded .debug_line section. */
120 bfd_size_type dwarf_line_size;
121
122 /* Pointer to the .debug_str section loaded into memory. */
123 bfd_byte *dwarf_str_buffer;
124
125 /* Length of the loaded .debug_str section. */
126 bfd_size_type dwarf_str_size;
127
128 /* Pointer to the .debug_ranges section loaded into memory. */
129 bfd_byte *dwarf_ranges_buffer;
130
131 /* Length of the loaded .debug_ranges section. */
132 bfd_size_type dwarf_ranges_size;
133
134 /* If the most recent call to bfd_find_nearest_line was given an
135 address in an inlined function, preserve a pointer into the
136 calling chain for subsequent calls to bfd_find_inliner_info to
137 use. */
138 struct funcinfo *inliner_chain;
139
140 /* Number of loadable sections. */
141 unsigned int loadable_section_count;
142
143 /* Array of loadable sections. */
144 struct loadable_section *loadable_sections;
145
146 /* Number of times find_line is called. This is used in
147 the heuristic for enabling the info hash tables. */
148 int info_hash_count;
149
150 #define STASH_INFO_HASH_TRIGGER 100
151
152 /* Hash table mapping symbol names to function infos. */
153 struct info_hash_table *funcinfo_hash_table;
154
155 /* Hash table mapping symbol names to variable infos. */
156 struct info_hash_table *varinfo_hash_table;
157
158 /* Head of comp_unit list in the last hash table update. */
159 struct comp_unit *hash_units_head;
160
161 /* Status of info hash. */
162 int info_hash_status;
163 #define STASH_INFO_HASH_OFF 0
164 #define STASH_INFO_HASH_ON 1
165 #define STASH_INFO_HASH_DISABLED 2
166 };
167
168 struct arange
169 {
170 struct arange *next;
171 bfd_vma low;
172 bfd_vma high;
173 };
174
175 /* A minimal decoding of DWARF2 compilation units. We only decode
176 what's needed to get to the line number information. */
177
178 struct comp_unit
179 {
180 /* Chain the previously read compilation units. */
181 struct comp_unit *next_unit;
182
183 /* Likewise, chain the compilation unit read after this one.
184 The comp units are stored in reversed reading order. */
185 struct comp_unit *prev_unit;
186
187 /* Keep the bfd convenient (for memory allocation). */
188 bfd *abfd;
189
190 /* The lowest and highest addresses contained in this compilation
191 unit as specified in the compilation unit header. */
192 struct arange arange;
193
194 /* The DW_AT_name attribute (for error messages). */
195 char *name;
196
197 /* The abbrev hash table. */
198 struct abbrev_info **abbrevs;
199
200 /* Note that an error was found by comp_unit_find_nearest_line. */
201 int error;
202
203 /* The DW_AT_comp_dir attribute. */
204 char *comp_dir;
205
206 /* TRUE if there is a line number table associated with this comp. unit. */
207 int stmtlist;
208
209 /* Pointer to the current comp_unit so that we can find a given entry
210 by its reference. */
211 bfd_byte *info_ptr_unit;
212
213 /* The offset into .debug_line of the line number table. */
214 unsigned long line_offset;
215
216 /* Pointer to the first child die for the comp unit. */
217 bfd_byte *first_child_die_ptr;
218
219 /* The end of the comp unit. */
220 bfd_byte *end_ptr;
221
222 /* The decoded line number, NULL if not yet decoded. */
223 struct line_info_table *line_table;
224
225 /* A list of the functions found in this comp. unit. */
226 struct funcinfo *function_table;
227
228 /* A list of the variables found in this comp. unit. */
229 struct varinfo *variable_table;
230
231 /* Pointer to dwarf2_debug structure. */
232 struct dwarf2_debug *stash;
233
234 /* Address size for this unit - from unit header. */
235 unsigned char addr_size;
236
237 /* Offset size for this unit - from unit header. */
238 unsigned char offset_size;
239
240 /* Base address for this unit - from DW_AT_low_pc attribute of
241 DW_TAG_compile_unit DIE */
242 bfd_vma base_address;
243
244 /* TRUE if symbols are cached in hash table for faster lookup by name. */
245 bfd_boolean cached;
246 };
247
248 /* This data structure holds the information of an abbrev. */
249 struct abbrev_info
250 {
251 unsigned int number; /* Number identifying abbrev. */
252 enum dwarf_tag tag; /* DWARF tag. */
253 int has_children; /* Boolean. */
254 unsigned int num_attrs; /* Number of attributes. */
255 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
256 struct abbrev_info *next; /* Next in chain. */
257 };
258
259 struct attr_abbrev
260 {
261 enum dwarf_attribute name;
262 enum dwarf_form form;
263 };
264
265 #ifndef ABBREV_HASH_SIZE
266 #define ABBREV_HASH_SIZE 121
267 #endif
268 #ifndef ATTR_ALLOC_CHUNK
269 #define ATTR_ALLOC_CHUNK 4
270 #endif
271
272 /* Variable and function hash tables. This is used to speed up look-up
273 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
274 In order to share code between variable and function infos, we use
275 a list of untyped pointer for all variable/function info associated with
276 a symbol. We waste a bit of memory for list with one node but that
277 simplifies the code. */
278
279 struct info_list_node
280 {
281 struct info_list_node *next;
282 void *info;
283 };
284
285 /* Info hash entry. */
286 struct info_hash_entry
287 {
288 struct bfd_hash_entry root;
289 struct info_list_node *head;
290 };
291
292 struct info_hash_table
293 {
294 struct bfd_hash_table base;
295 };
296
297 /* Function to create a new entry in info hash table. */
298
299 static struct bfd_hash_entry *
300 info_hash_table_newfunc (struct bfd_hash_entry *entry,
301 struct bfd_hash_table *table,
302 const char *string)
303 {
304 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
305
306 /* Allocate the structure if it has not already been allocated by a
307 derived class. */
308 if (ret == NULL)
309 {
310 ret = bfd_hash_allocate (table, sizeof (* ret));
311 if (ret == NULL)
312 return NULL;
313 }
314
315 /* Call the allocation method of the base class. */
316 ret = ((struct info_hash_entry *)
317 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
318
319 /* Initialize the local fields here. */
320 if (ret)
321 ret->head = NULL;
322
323 return (struct bfd_hash_entry *) ret;
324 }
325
326 /* Function to create a new info hash table. It returns a pointer to the
327 newly created table or NULL if there is any error. We need abfd
328 solely for memory allocation. */
329
330 static struct info_hash_table *
331 create_info_hash_table (bfd *abfd)
332 {
333 struct info_hash_table *hash_table;
334
335 hash_table = bfd_alloc (abfd, sizeof (struct info_hash_table));
336 if (!hash_table)
337 return hash_table;
338
339 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
340 sizeof (struct info_hash_entry)))
341 {
342 bfd_release (abfd, hash_table);
343 return NULL;
344 }
345
346 return hash_table;
347 }
348
349 /* Insert an info entry into an info hash table. We do not check of
350 duplicate entries. Also, the caller need to guarantee that the
351 right type of info in inserted as info is passed as a void* pointer.
352 This function returns true if there is no error. */
353
354 static bfd_boolean
355 insert_info_hash_table (struct info_hash_table *hash_table,
356 const char *key,
357 void *info,
358 bfd_boolean copy_p)
359 {
360 struct info_hash_entry *entry;
361 struct info_list_node *node;
362
363 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
364 key, TRUE, copy_p);
365 if (!entry)
366 return FALSE;
367
368 node = bfd_hash_allocate (&hash_table->base, sizeof (*node));
369 if (!node)
370 return FALSE;
371
372 node->info = info;
373 node->next = entry->head;
374 entry->head = node;
375
376 return TRUE;
377 }
378
379 /* Look up an info entry list from an info hash table. Return NULL
380 if there is none. */
381
382 static struct info_list_node *
383 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
384 {
385 struct info_hash_entry *entry;
386
387 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
388 FALSE, FALSE);
389 return entry ? entry->head : NULL;
390 }
391
392 /* Read a section into its appropriate place in the dwarf2_debug
393 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If syms is
394 not NULL, use bfd_simple_get_relocated_section_contents to read the
395 section contents, otherwise use bfd_get_section_contents. */
396
397 static bfd_boolean
398 read_section (bfd *abfd,
399 const char* section_name, const char* compressed_section_name,
400 asymbol** syms, bfd_uint64_t offset,
401 bfd_byte **section_buffer, bfd_size_type *section_size)
402 {
403 asection *msec;
404 bfd_boolean section_is_compressed = FALSE;
405
406 /* read_section is a noop if the section has already been read. */
407 if (*section_buffer)
408 return TRUE;
409
410 msec = bfd_get_section_by_name (abfd, section_name);
411 if (! msec && compressed_section_name)
412 {
413 msec = bfd_get_section_by_name (abfd, compressed_section_name);
414 section_is_compressed = TRUE;
415 }
416 if (! msec)
417 {
418 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
419 bfd_set_error (bfd_error_bad_value);
420 return FALSE;
421 }
422
423 if (syms)
424 {
425 *section_size = msec->size;
426 *section_buffer
427 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
428 if (! *section_buffer)
429 return FALSE;
430 }
431 else
432 {
433 *section_size = msec->rawsize ? msec->rawsize : msec->size;
434 *section_buffer = bfd_malloc (*section_size);
435 if (! *section_buffer)
436 return FALSE;
437 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
438 0, *section_size))
439 return FALSE;
440 }
441
442 if (section_is_compressed)
443 {
444 if (! bfd_uncompress_section_contents (section_buffer, section_size))
445 {
446 (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name);
447 bfd_set_error (bfd_error_bad_value);
448 return FALSE;
449 }
450 }
451
452 /* It is possible to get a bad value for the offset into the section
453 * that the client wants. Validate it here to avoid trouble later. */
454 if (offset != 0 && offset >= *section_size)
455 {
456 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
457 offset, section_name, *section_size);
458 bfd_set_error (bfd_error_bad_value);
459 return FALSE;
460 }
461
462 return TRUE;
463 }
464
465 /* VERBATIM
466 The following function up to the END VERBATIM mark are
467 copied directly from dwarf2read.c. */
468
469 /* Read dwarf information from a buffer. */
470
471 static unsigned int
472 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
473 {
474 return bfd_get_8 (abfd, buf);
475 }
476
477 static int
478 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
479 {
480 return bfd_get_signed_8 (abfd, buf);
481 }
482
483 static unsigned int
484 read_2_bytes (bfd *abfd, bfd_byte *buf)
485 {
486 return bfd_get_16 (abfd, buf);
487 }
488
489 static unsigned int
490 read_4_bytes (bfd *abfd, bfd_byte *buf)
491 {
492 return bfd_get_32 (abfd, buf);
493 }
494
495 static bfd_uint64_t
496 read_8_bytes (bfd *abfd, bfd_byte *buf)
497 {
498 return bfd_get_64 (abfd, buf);
499 }
500
501 static bfd_byte *
502 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
503 bfd_byte *buf,
504 unsigned int size ATTRIBUTE_UNUSED)
505 {
506 /* If the size of a host char is 8 bits, we can return a pointer
507 to the buffer, otherwise we have to copy the data to a buffer
508 allocated on the temporary obstack. */
509 return buf;
510 }
511
512 static char *
513 read_string (bfd *abfd ATTRIBUTE_UNUSED,
514 bfd_byte *buf,
515 unsigned int *bytes_read_ptr)
516 {
517 /* Return a pointer to the embedded string. */
518 char *str = (char *) buf;
519 if (*str == '\0')
520 {
521 *bytes_read_ptr = 1;
522 return NULL;
523 }
524
525 *bytes_read_ptr = strlen (str) + 1;
526 return str;
527 }
528
529 static char *
530 read_indirect_string (struct comp_unit* unit,
531 bfd_byte *buf,
532 unsigned int *bytes_read_ptr)
533 {
534 bfd_uint64_t offset;
535 struct dwarf2_debug *stash = unit->stash;
536 char *str;
537
538 if (unit->offset_size == 4)
539 offset = read_4_bytes (unit->abfd, buf);
540 else
541 offset = read_8_bytes (unit->abfd, buf);
542 *bytes_read_ptr = unit->offset_size;
543
544 if (! read_section (unit->abfd, ".debug_str", ".zdebug_str",
545 0, offset,
546 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
547 return 0;
548
549 str = (char *) stash->dwarf_str_buffer + offset;
550 if (*str == '\0')
551 return NULL;
552 return str;
553 }
554
555 /* END VERBATIM */
556
557 static bfd_uint64_t
558 read_address (struct comp_unit *unit, bfd_byte *buf)
559 {
560 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
561
562 if (signed_vma)
563 {
564 switch (unit->addr_size)
565 {
566 case 8:
567 return bfd_get_signed_64 (unit->abfd, buf);
568 case 4:
569 return bfd_get_signed_32 (unit->abfd, buf);
570 case 2:
571 return bfd_get_signed_16 (unit->abfd, buf);
572 default:
573 abort ();
574 }
575 }
576 else
577 {
578 switch (unit->addr_size)
579 {
580 case 8:
581 return bfd_get_64 (unit->abfd, buf);
582 case 4:
583 return bfd_get_32 (unit->abfd, buf);
584 case 2:
585 return bfd_get_16 (unit->abfd, buf);
586 default:
587 abort ();
588 }
589 }
590 }
591
592 /* Lookup an abbrev_info structure in the abbrev hash table. */
593
594 static struct abbrev_info *
595 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
596 {
597 unsigned int hash_number;
598 struct abbrev_info *abbrev;
599
600 hash_number = number % ABBREV_HASH_SIZE;
601 abbrev = abbrevs[hash_number];
602
603 while (abbrev)
604 {
605 if (abbrev->number == number)
606 return abbrev;
607 else
608 abbrev = abbrev->next;
609 }
610
611 return NULL;
612 }
613
614 /* In DWARF version 2, the description of the debugging information is
615 stored in a separate .debug_abbrev section. Before we read any
616 dies from a section we read in all abbreviations and install them
617 in a hash table. */
618
619 static struct abbrev_info**
620 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
621 {
622 struct abbrev_info **abbrevs;
623 bfd_byte *abbrev_ptr;
624 struct abbrev_info *cur_abbrev;
625 unsigned int abbrev_number, bytes_read, abbrev_name;
626 unsigned int abbrev_form, hash_number;
627 bfd_size_type amt;
628
629 if (! read_section (abfd, ".debug_abbrev", ".zdebug_abbrev",
630 stash->syms, offset,
631 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
632 return 0;
633
634 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
635 abbrevs = bfd_zalloc (abfd, amt);
636
637 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
638 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
639 abbrev_ptr += bytes_read;
640
641 /* Loop until we reach an abbrev number of 0. */
642 while (abbrev_number)
643 {
644 amt = sizeof (struct abbrev_info);
645 cur_abbrev = bfd_zalloc (abfd, amt);
646
647 /* Read in abbrev header. */
648 cur_abbrev->number = abbrev_number;
649 cur_abbrev->tag = (enum dwarf_tag)
650 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
651 abbrev_ptr += bytes_read;
652 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
653 abbrev_ptr += 1;
654
655 /* Now read in declarations. */
656 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
657 abbrev_ptr += bytes_read;
658 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
659 abbrev_ptr += bytes_read;
660
661 while (abbrev_name)
662 {
663 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
664 {
665 struct attr_abbrev *tmp;
666
667 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
668 amt *= sizeof (struct attr_abbrev);
669 tmp = bfd_realloc (cur_abbrev->attrs, amt);
670 if (tmp == NULL)
671 {
672 size_t i;
673
674 for (i = 0; i < ABBREV_HASH_SIZE; i++)
675 {
676 struct abbrev_info *abbrev = abbrevs[i];
677
678 while (abbrev)
679 {
680 free (abbrev->attrs);
681 abbrev = abbrev->next;
682 }
683 }
684 return NULL;
685 }
686 cur_abbrev->attrs = tmp;
687 }
688
689 cur_abbrev->attrs[cur_abbrev->num_attrs].name
690 = (enum dwarf_attribute) abbrev_name;
691 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
692 = (enum dwarf_form) abbrev_form;
693 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
694 abbrev_ptr += bytes_read;
695 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
696 abbrev_ptr += bytes_read;
697 }
698
699 hash_number = abbrev_number % ABBREV_HASH_SIZE;
700 cur_abbrev->next = abbrevs[hash_number];
701 abbrevs[hash_number] = cur_abbrev;
702
703 /* Get next abbreviation.
704 Under Irix6 the abbreviations for a compilation unit are not
705 always properly terminated with an abbrev number of 0.
706 Exit loop if we encounter an abbreviation which we have
707 already read (which means we are about to read the abbreviations
708 for the next compile unit) or if the end of the abbreviation
709 table is reached. */
710 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
711 >= stash->dwarf_abbrev_size)
712 break;
713 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
714 abbrev_ptr += bytes_read;
715 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
716 break;
717 }
718
719 return abbrevs;
720 }
721
722 /* Read an attribute value described by an attribute form. */
723
724 static bfd_byte *
725 read_attribute_value (struct attribute *attr,
726 unsigned form,
727 struct comp_unit *unit,
728 bfd_byte *info_ptr)
729 {
730 bfd *abfd = unit->abfd;
731 unsigned int bytes_read;
732 struct dwarf_block *blk;
733 bfd_size_type amt;
734
735 attr->form = (enum dwarf_form) form;
736
737 switch (form)
738 {
739 case DW_FORM_addr:
740 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
741 case DW_FORM_ref_addr:
742 attr->u.val = read_address (unit, info_ptr);
743 info_ptr += unit->addr_size;
744 break;
745 case DW_FORM_block2:
746 amt = sizeof (struct dwarf_block);
747 blk = bfd_alloc (abfd, amt);
748 blk->size = read_2_bytes (abfd, info_ptr);
749 info_ptr += 2;
750 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
751 info_ptr += blk->size;
752 attr->u.blk = blk;
753 break;
754 case DW_FORM_block4:
755 amt = sizeof (struct dwarf_block);
756 blk = bfd_alloc (abfd, amt);
757 blk->size = read_4_bytes (abfd, info_ptr);
758 info_ptr += 4;
759 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
760 info_ptr += blk->size;
761 attr->u.blk = blk;
762 break;
763 case DW_FORM_data2:
764 attr->u.val = read_2_bytes (abfd, info_ptr);
765 info_ptr += 2;
766 break;
767 case DW_FORM_data4:
768 attr->u.val = read_4_bytes (abfd, info_ptr);
769 info_ptr += 4;
770 break;
771 case DW_FORM_data8:
772 attr->u.val = read_8_bytes (abfd, info_ptr);
773 info_ptr += 8;
774 break;
775 case DW_FORM_string:
776 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
777 info_ptr += bytes_read;
778 break;
779 case DW_FORM_strp:
780 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
781 info_ptr += bytes_read;
782 break;
783 case DW_FORM_block:
784 amt = sizeof (struct dwarf_block);
785 blk = bfd_alloc (abfd, amt);
786 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
787 info_ptr += bytes_read;
788 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
789 info_ptr += blk->size;
790 attr->u.blk = blk;
791 break;
792 case DW_FORM_block1:
793 amt = sizeof (struct dwarf_block);
794 blk = bfd_alloc (abfd, amt);
795 blk->size = read_1_byte (abfd, info_ptr);
796 info_ptr += 1;
797 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
798 info_ptr += blk->size;
799 attr->u.blk = blk;
800 break;
801 case DW_FORM_data1:
802 attr->u.val = read_1_byte (abfd, info_ptr);
803 info_ptr += 1;
804 break;
805 case DW_FORM_flag:
806 attr->u.val = read_1_byte (abfd, info_ptr);
807 info_ptr += 1;
808 break;
809 case DW_FORM_sdata:
810 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
811 info_ptr += bytes_read;
812 break;
813 case DW_FORM_udata:
814 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
815 info_ptr += bytes_read;
816 break;
817 case DW_FORM_ref1:
818 attr->u.val = read_1_byte (abfd, info_ptr);
819 info_ptr += 1;
820 break;
821 case DW_FORM_ref2:
822 attr->u.val = read_2_bytes (abfd, info_ptr);
823 info_ptr += 2;
824 break;
825 case DW_FORM_ref4:
826 attr->u.val = read_4_bytes (abfd, info_ptr);
827 info_ptr += 4;
828 break;
829 case DW_FORM_ref8:
830 attr->u.val = read_8_bytes (abfd, info_ptr);
831 info_ptr += 8;
832 break;
833 case DW_FORM_ref_udata:
834 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
835 info_ptr += bytes_read;
836 break;
837 case DW_FORM_indirect:
838 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
839 info_ptr += bytes_read;
840 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
841 break;
842 default:
843 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
844 form);
845 bfd_set_error (bfd_error_bad_value);
846 }
847 return info_ptr;
848 }
849
850 /* Read an attribute described by an abbreviated attribute. */
851
852 static bfd_byte *
853 read_attribute (struct attribute *attr,
854 struct attr_abbrev *abbrev,
855 struct comp_unit *unit,
856 bfd_byte *info_ptr)
857 {
858 attr->name = abbrev->name;
859 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
860 return info_ptr;
861 }
862
863 /* Source line information table routines. */
864
865 #define FILE_ALLOC_CHUNK 5
866 #define DIR_ALLOC_CHUNK 5
867
868 struct line_info
869 {
870 struct line_info* prev_line;
871 bfd_vma address;
872 char *filename;
873 unsigned int line;
874 unsigned int column;
875 int end_sequence; /* End of (sequential) code sequence. */
876 };
877
878 struct fileinfo
879 {
880 char *name;
881 unsigned int dir;
882 unsigned int time;
883 unsigned int size;
884 };
885
886 struct line_info_table
887 {
888 bfd* abfd;
889 unsigned int num_files;
890 unsigned int num_dirs;
891 char *comp_dir;
892 char **dirs;
893 struct fileinfo* files;
894 struct line_info* last_line; /* largest VMA */
895 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
896 };
897
898 /* Remember some information about each function. If the function is
899 inlined (DW_TAG_inlined_subroutine) it may have two additional
900 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
901 source code location where this function was inlined. */
902
903 struct funcinfo
904 {
905 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
906 struct funcinfo *caller_func; /* Pointer to function one scope higher */
907 char *caller_file; /* Source location file name where caller_func inlines this func */
908 int caller_line; /* Source location line number where caller_func inlines this func */
909 char *file; /* Source location file name */
910 int line; /* Source location line number */
911 int tag;
912 char *name;
913 struct arange arange;
914 asection *sec; /* Where the symbol is defined */
915 };
916
917 struct varinfo
918 {
919 /* Pointer to previous variable in list of all variables */
920 struct varinfo *prev_var;
921 /* Source location file name */
922 char *file;
923 /* Source location line number */
924 int line;
925 int tag;
926 char *name;
927 bfd_vma addr;
928 /* Where the symbol is defined */
929 asection *sec;
930 /* Is this a stack variable? */
931 unsigned int stack: 1;
932 };
933
934 /* Return TRUE if NEW_LINE should sort after LINE. */
935
936 static inline bfd_boolean
937 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
938 {
939 return (new_line->address > line->address
940 || (new_line->address == line->address
941 && new_line->end_sequence < line->end_sequence));
942 }
943
944
945 /* Adds a new entry to the line_info list in the line_info_table, ensuring
946 that the list is sorted. Note that the line_info list is sorted from
947 highest to lowest VMA (with possible duplicates); that is,
948 line_info->prev_line always accesses an equal or smaller VMA. */
949
950 static void
951 add_line_info (struct line_info_table *table,
952 bfd_vma address,
953 char *filename,
954 unsigned int line,
955 unsigned int column,
956 int end_sequence)
957 {
958 bfd_size_type amt = sizeof (struct line_info);
959 struct line_info* info = bfd_alloc (table->abfd, amt);
960
961 /* Set member data of 'info'. */
962 info->address = address;
963 info->line = line;
964 info->column = column;
965 info->end_sequence = end_sequence;
966
967 if (filename && filename[0])
968 {
969 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
970 if (info->filename)
971 strcpy (info->filename, filename);
972 }
973 else
974 info->filename = NULL;
975
976 /* Find the correct location for 'info'. Normally we will receive
977 new line_info data 1) in order and 2) with increasing VMAs.
978 However some compilers break the rules (cf. decode_line_info) and
979 so we include some heuristics for quickly finding the correct
980 location for 'info'. In particular, these heuristics optimize for
981 the common case in which the VMA sequence that we receive is a
982 list of locally sorted VMAs such as
983 p...z a...j (where a < j < p < z)
984
985 Note: table->lcl_head is used to head an *actual* or *possible*
986 sequence within the list (such as a...j) that is not directly
987 headed by table->last_line
988
989 Note: we may receive duplicate entries from 'decode_line_info'. */
990
991 if (table->last_line
992 && table->last_line->address == address
993 && table->last_line->end_sequence == end_sequence)
994 {
995 /* We only keep the last entry with the same address and end
996 sequence. See PR ld/4986. */
997 if (table->lcl_head == table->last_line)
998 table->lcl_head = info;
999 info->prev_line = table->last_line->prev_line;
1000 table->last_line = info;
1001 }
1002 else if (!table->last_line
1003 || new_line_sorts_after (info, table->last_line))
1004 {
1005 /* Normal case: add 'info' to the beginning of the list */
1006 info->prev_line = table->last_line;
1007 table->last_line = info;
1008
1009 /* lcl_head: initialize to head a *possible* sequence at the end. */
1010 if (!table->lcl_head)
1011 table->lcl_head = info;
1012 }
1013 else if (!new_line_sorts_after (info, table->lcl_head)
1014 && (!table->lcl_head->prev_line
1015 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1016 {
1017 /* Abnormal but easy: lcl_head is the head of 'info'. */
1018 info->prev_line = table->lcl_head->prev_line;
1019 table->lcl_head->prev_line = info;
1020 }
1021 else
1022 {
1023 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1024 heads for 'info'. Reset 'lcl_head'. */
1025 struct line_info* li2 = table->last_line; /* always non-NULL */
1026 struct line_info* li1 = li2->prev_line;
1027
1028 while (li1)
1029 {
1030 if (!new_line_sorts_after (info, li2)
1031 && new_line_sorts_after (info, li1))
1032 break;
1033
1034 li2 = li1; /* always non-NULL */
1035 li1 = li1->prev_line;
1036 }
1037 table->lcl_head = li2;
1038 info->prev_line = table->lcl_head->prev_line;
1039 table->lcl_head->prev_line = info;
1040 }
1041 }
1042
1043 /* Extract a fully qualified filename from a line info table.
1044 The returned string has been malloc'ed and it is the caller's
1045 responsibility to free it. */
1046
1047 static char *
1048 concat_filename (struct line_info_table *table, unsigned int file)
1049 {
1050 char *filename;
1051
1052 if (file - 1 >= table->num_files)
1053 {
1054 /* FILE == 0 means unknown. */
1055 if (file)
1056 (*_bfd_error_handler)
1057 (_("Dwarf Error: mangled line number section (bad file number)."));
1058 return strdup ("<unknown>");
1059 }
1060
1061 filename = table->files[file - 1].name;
1062
1063 if (!IS_ABSOLUTE_PATH (filename))
1064 {
1065 char *dirname = NULL;
1066 char *subdirname = NULL;
1067 char *name;
1068 size_t len;
1069
1070 if (table->files[file - 1].dir)
1071 subdirname = table->dirs[table->files[file - 1].dir - 1];
1072
1073 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1074 dirname = table->comp_dir;
1075
1076 if (!dirname)
1077 {
1078 dirname = subdirname;
1079 subdirname = NULL;
1080 }
1081
1082 if (!dirname)
1083 return strdup (filename);
1084
1085 len = strlen (dirname) + strlen (filename) + 2;
1086
1087 if (subdirname)
1088 {
1089 len += strlen (subdirname) + 1;
1090 name = bfd_malloc (len);
1091 if (name)
1092 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1093 }
1094 else
1095 {
1096 name = bfd_malloc (len);
1097 if (name)
1098 sprintf (name, "%s/%s", dirname, filename);
1099 }
1100
1101 return name;
1102 }
1103
1104 return strdup (filename);
1105 }
1106
1107 static void
1108 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
1109 {
1110 struct arange *arange;
1111
1112 /* If the first arange is empty, use it. */
1113 if (first_arange->high == 0)
1114 {
1115 first_arange->low = low_pc;
1116 first_arange->high = high_pc;
1117 return;
1118 }
1119
1120 /* Next see if we can cheaply extend an existing range. */
1121 arange = first_arange;
1122 do
1123 {
1124 if (low_pc == arange->high)
1125 {
1126 arange->high = high_pc;
1127 return;
1128 }
1129 if (high_pc == arange->low)
1130 {
1131 arange->low = low_pc;
1132 return;
1133 }
1134 arange = arange->next;
1135 }
1136 while (arange);
1137
1138 /* Need to allocate a new arange and insert it into the arange list.
1139 Order isn't significant, so just insert after the first arange. */
1140 arange = bfd_zalloc (abfd, sizeof (*arange));
1141 arange->low = low_pc;
1142 arange->high = high_pc;
1143 arange->next = first_arange->next;
1144 first_arange->next = arange;
1145 }
1146
1147 /* Decode the line number information for UNIT. */
1148
1149 static struct line_info_table*
1150 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1151 {
1152 bfd *abfd = unit->abfd;
1153 struct line_info_table* table;
1154 bfd_byte *line_ptr;
1155 bfd_byte *line_end;
1156 struct line_head lh;
1157 unsigned int i, bytes_read, offset_size;
1158 char *cur_file, *cur_dir;
1159 unsigned char op_code, extended_op, adj_opcode;
1160 bfd_size_type amt;
1161
1162 if (! read_section (abfd, ".debug_line", ".zdebug_line",
1163 stash->syms, unit->line_offset,
1164 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1165 return 0;
1166
1167 amt = sizeof (struct line_info_table);
1168 table = bfd_alloc (abfd, amt);
1169 table->abfd = abfd;
1170 table->comp_dir = unit->comp_dir;
1171
1172 table->num_files = 0;
1173 table->files = NULL;
1174
1175 table->num_dirs = 0;
1176 table->dirs = NULL;
1177
1178 table->files = NULL;
1179 table->last_line = NULL;
1180 table->lcl_head = NULL;
1181
1182 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1183
1184 /* Read in the prologue. */
1185 lh.total_length = read_4_bytes (abfd, line_ptr);
1186 line_ptr += 4;
1187 offset_size = 4;
1188 if (lh.total_length == 0xffffffff)
1189 {
1190 lh.total_length = read_8_bytes (abfd, line_ptr);
1191 line_ptr += 8;
1192 offset_size = 8;
1193 }
1194 else if (lh.total_length == 0 && unit->addr_size == 8)
1195 {
1196 /* Handle (non-standard) 64-bit DWARF2 formats. */
1197 lh.total_length = read_4_bytes (abfd, line_ptr);
1198 line_ptr += 4;
1199 offset_size = 8;
1200 }
1201 line_end = line_ptr + lh.total_length;
1202 lh.version = read_2_bytes (abfd, line_ptr);
1203 line_ptr += 2;
1204 if (offset_size == 4)
1205 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1206 else
1207 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1208 line_ptr += offset_size;
1209 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1210 line_ptr += 1;
1211 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1212 line_ptr += 1;
1213 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1214 line_ptr += 1;
1215 lh.line_range = read_1_byte (abfd, line_ptr);
1216 line_ptr += 1;
1217 lh.opcode_base = read_1_byte (abfd, line_ptr);
1218 line_ptr += 1;
1219 amt = lh.opcode_base * sizeof (unsigned char);
1220 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1221
1222 lh.standard_opcode_lengths[0] = 1;
1223
1224 for (i = 1; i < lh.opcode_base; ++i)
1225 {
1226 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1227 line_ptr += 1;
1228 }
1229
1230 /* Read directory table. */
1231 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1232 {
1233 line_ptr += bytes_read;
1234
1235 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1236 {
1237 char **tmp;
1238
1239 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1240 amt *= sizeof (char *);
1241
1242 tmp = bfd_realloc (table->dirs, amt);
1243 if (tmp == NULL)
1244 {
1245 free (table->dirs);
1246 return NULL;
1247 }
1248 table->dirs = tmp;
1249 }
1250
1251 table->dirs[table->num_dirs++] = cur_dir;
1252 }
1253
1254 line_ptr += bytes_read;
1255
1256 /* Read file name table. */
1257 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1258 {
1259 line_ptr += bytes_read;
1260
1261 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1262 {
1263 struct fileinfo *tmp;
1264
1265 amt = table->num_files + FILE_ALLOC_CHUNK;
1266 amt *= sizeof (struct fileinfo);
1267
1268 tmp = bfd_realloc (table->files, amt);
1269 if (tmp == NULL)
1270 {
1271 free (table->files);
1272 free (table->dirs);
1273 return NULL;
1274 }
1275 table->files = tmp;
1276 }
1277
1278 table->files[table->num_files].name = cur_file;
1279 table->files[table->num_files].dir =
1280 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1281 line_ptr += bytes_read;
1282 table->files[table->num_files].time =
1283 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1284 line_ptr += bytes_read;
1285 table->files[table->num_files].size =
1286 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1287 line_ptr += bytes_read;
1288 table->num_files++;
1289 }
1290
1291 line_ptr += bytes_read;
1292
1293 /* Read the statement sequences until there's nothing left. */
1294 while (line_ptr < line_end)
1295 {
1296 /* State machine registers. */
1297 bfd_vma address = 0;
1298 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1299 unsigned int line = 1;
1300 unsigned int column = 0;
1301 int is_stmt = lh.default_is_stmt;
1302 int end_sequence = 0;
1303 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1304 compilers generate address sequences that are wildly out of
1305 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1306 for ia64-Linux). Thus, to determine the low and high
1307 address, we must compare on every DW_LNS_copy, etc. */
1308 bfd_vma low_pc = (bfd_vma) -1;
1309 bfd_vma high_pc = 0;
1310
1311 /* Decode the table. */
1312 while (! end_sequence)
1313 {
1314 op_code = read_1_byte (abfd, line_ptr);
1315 line_ptr += 1;
1316
1317 if (op_code >= lh.opcode_base)
1318 {
1319 /* Special operand. */
1320 adj_opcode = op_code - lh.opcode_base;
1321 address += (adj_opcode / lh.line_range)
1322 * lh.minimum_instruction_length;
1323 line += lh.line_base + (adj_opcode % lh.line_range);
1324 /* Append row to matrix using current values. */
1325 add_line_info (table, address, filename, line, column, 0);
1326 if (address < low_pc)
1327 low_pc = address;
1328 if (address > high_pc)
1329 high_pc = address;
1330 }
1331 else switch (op_code)
1332 {
1333 case DW_LNS_extended_op:
1334 /* Ignore length. */
1335 line_ptr += 1;
1336 extended_op = read_1_byte (abfd, line_ptr);
1337 line_ptr += 1;
1338
1339 switch (extended_op)
1340 {
1341 case DW_LNE_end_sequence:
1342 end_sequence = 1;
1343 add_line_info (table, address, filename, line, column,
1344 end_sequence);
1345 if (address < low_pc)
1346 low_pc = address;
1347 if (address > high_pc)
1348 high_pc = address;
1349 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1350 break;
1351 case DW_LNE_set_address:
1352 address = read_address (unit, line_ptr);
1353 line_ptr += unit->addr_size;
1354 break;
1355 case DW_LNE_define_file:
1356 cur_file = read_string (abfd, line_ptr, &bytes_read);
1357 line_ptr += bytes_read;
1358 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1359 {
1360 struct fileinfo *tmp;
1361
1362 amt = table->num_files + FILE_ALLOC_CHUNK;
1363 amt *= sizeof (struct fileinfo);
1364 tmp = bfd_realloc (table->files, amt);
1365 if (tmp == NULL)
1366 {
1367 free (table->files);
1368 free (table->dirs);
1369 free (filename);
1370 return NULL;
1371 }
1372 table->files = tmp;
1373 }
1374 table->files[table->num_files].name = cur_file;
1375 table->files[table->num_files].dir =
1376 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1377 line_ptr += bytes_read;
1378 table->files[table->num_files].time =
1379 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1380 line_ptr += bytes_read;
1381 table->files[table->num_files].size =
1382 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1383 line_ptr += bytes_read;
1384 table->num_files++;
1385 break;
1386 default:
1387 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1388 bfd_set_error (bfd_error_bad_value);
1389 free (filename);
1390 free (table->files);
1391 free (table->dirs);
1392 return NULL;
1393 }
1394 break;
1395 case DW_LNS_copy:
1396 add_line_info (table, address, filename, line, column, 0);
1397 if (address < low_pc)
1398 low_pc = address;
1399 if (address > high_pc)
1400 high_pc = address;
1401 break;
1402 case DW_LNS_advance_pc:
1403 address += lh.minimum_instruction_length
1404 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1405 line_ptr += bytes_read;
1406 break;
1407 case DW_LNS_advance_line:
1408 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1409 line_ptr += bytes_read;
1410 break;
1411 case DW_LNS_set_file:
1412 {
1413 unsigned int file;
1414
1415 /* The file and directory tables are 0
1416 based, the references are 1 based. */
1417 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1418 line_ptr += bytes_read;
1419 if (filename)
1420 free (filename);
1421 filename = concat_filename (table, file);
1422 break;
1423 }
1424 case DW_LNS_set_column:
1425 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1426 line_ptr += bytes_read;
1427 break;
1428 case DW_LNS_negate_stmt:
1429 is_stmt = (!is_stmt);
1430 break;
1431 case DW_LNS_set_basic_block:
1432 break;
1433 case DW_LNS_const_add_pc:
1434 address += lh.minimum_instruction_length
1435 * ((255 - lh.opcode_base) / lh.line_range);
1436 break;
1437 case DW_LNS_fixed_advance_pc:
1438 address += read_2_bytes (abfd, line_ptr);
1439 line_ptr += 2;
1440 break;
1441 default:
1442 {
1443 int i;
1444
1445 /* Unknown standard opcode, ignore it. */
1446 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1447 {
1448 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1449 line_ptr += bytes_read;
1450 }
1451 }
1452 }
1453 }
1454
1455 if (filename)
1456 free (filename);
1457 }
1458
1459 return table;
1460 }
1461
1462 /* If ADDR is within TABLE set the output parameters and return TRUE,
1463 otherwise return FALSE. The output parameters, FILENAME_PTR and
1464 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1465
1466 static bfd_boolean
1467 lookup_address_in_line_info_table (struct line_info_table *table,
1468 bfd_vma addr,
1469 struct funcinfo *function,
1470 const char **filename_ptr,
1471 unsigned int *linenumber_ptr)
1472 {
1473 /* Note: table->last_line should be a descendingly sorted list. */
1474 struct line_info* next_line = table->last_line;
1475 struct line_info* each_line = NULL;
1476 *filename_ptr = NULL;
1477
1478 if (!next_line)
1479 return FALSE;
1480
1481 each_line = next_line->prev_line;
1482
1483 /* Check for large addresses */
1484 if (addr > next_line->address)
1485 each_line = NULL; /* ensure we skip over the normal case */
1486
1487 /* Normal case: search the list; save */
1488 while (each_line && next_line)
1489 {
1490 /* If we have an address match, save this info. This allows us
1491 to return as good as results as possible for strange debugging
1492 info. */
1493 bfd_boolean addr_match = FALSE;
1494 if (each_line->address <= addr && addr < next_line->address)
1495 {
1496 addr_match = TRUE;
1497
1498 /* If this line appears to span functions, and addr is in the
1499 later function, return the first line of that function instead
1500 of the last line of the earlier one. This check is for GCC
1501 2.95, which emits the first line number for a function late. */
1502
1503 if (function != NULL)
1504 {
1505 bfd_vma lowest_pc;
1506 struct arange *arange;
1507
1508 /* Find the lowest address in the function's range list */
1509 lowest_pc = function->arange.low;
1510 for (arange = &function->arange;
1511 arange;
1512 arange = arange->next)
1513 {
1514 if (function->arange.low < lowest_pc)
1515 lowest_pc = function->arange.low;
1516 }
1517 /* Check for spanning function and set outgoing line info */
1518 if (addr >= lowest_pc
1519 && each_line->address < lowest_pc
1520 && next_line->address > lowest_pc)
1521 {
1522 *filename_ptr = next_line->filename;
1523 *linenumber_ptr = next_line->line;
1524 }
1525 else
1526 {
1527 *filename_ptr = each_line->filename;
1528 *linenumber_ptr = each_line->line;
1529 }
1530 }
1531 else
1532 {
1533 *filename_ptr = each_line->filename;
1534 *linenumber_ptr = each_line->line;
1535 }
1536 }
1537
1538 if (addr_match && !each_line->end_sequence)
1539 return TRUE; /* we have definitely found what we want */
1540
1541 next_line = each_line;
1542 each_line = each_line->prev_line;
1543 }
1544
1545 /* At this point each_line is NULL but next_line is not. If we found
1546 a candidate end-of-sequence point in the loop above, we can return
1547 that (compatibility with a bug in the Intel compiler); otherwise,
1548 assuming that we found the containing function for this address in
1549 this compilation unit, return the first line we have a number for
1550 (compatibility with GCC 2.95). */
1551 if (*filename_ptr == NULL && function != NULL)
1552 {
1553 *filename_ptr = next_line->filename;
1554 *linenumber_ptr = next_line->line;
1555 return TRUE;
1556 }
1557
1558 return FALSE;
1559 }
1560
1561 /* Read in the .debug_ranges section for future reference */
1562
1563 static bfd_boolean
1564 read_debug_ranges (struct comp_unit *unit)
1565 {
1566 struct dwarf2_debug *stash = unit->stash;
1567 return read_section (unit->abfd, ".debug_ranges", ".zdebug_ranges",
1568 stash->syms, 0,
1569 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1570 }
1571
1572 /* Function table functions. */
1573
1574 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1575 Note that we need to find the function that has the smallest
1576 range that contains ADDR, to handle inlined functions without
1577 depending upon them being ordered in TABLE by increasing range. */
1578
1579 static bfd_boolean
1580 lookup_address_in_function_table (struct comp_unit *unit,
1581 bfd_vma addr,
1582 struct funcinfo **function_ptr,
1583 const char **functionname_ptr)
1584 {
1585 struct funcinfo* each_func;
1586 struct funcinfo* best_fit = NULL;
1587 struct arange *arange;
1588
1589 for (each_func = unit->function_table;
1590 each_func;
1591 each_func = each_func->prev_func)
1592 {
1593 for (arange = &each_func->arange;
1594 arange;
1595 arange = arange->next)
1596 {
1597 if (addr >= arange->low && addr < arange->high)
1598 {
1599 if (!best_fit ||
1600 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1601 best_fit = each_func;
1602 }
1603 }
1604 }
1605
1606 if (best_fit)
1607 {
1608 *functionname_ptr = best_fit->name;
1609 *function_ptr = best_fit;
1610 return TRUE;
1611 }
1612 else
1613 {
1614 return FALSE;
1615 }
1616 }
1617
1618 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1619 and LINENUMBER_PTR, and return TRUE. */
1620
1621 static bfd_boolean
1622 lookup_symbol_in_function_table (struct comp_unit *unit,
1623 asymbol *sym,
1624 bfd_vma addr,
1625 const char **filename_ptr,
1626 unsigned int *linenumber_ptr)
1627 {
1628 struct funcinfo* each_func;
1629 struct funcinfo* best_fit = NULL;
1630 struct arange *arange;
1631 const char *name = bfd_asymbol_name (sym);
1632 asection *sec = bfd_get_section (sym);
1633
1634 for (each_func = unit->function_table;
1635 each_func;
1636 each_func = each_func->prev_func)
1637 {
1638 for (arange = &each_func->arange;
1639 arange;
1640 arange = arange->next)
1641 {
1642 if ((!each_func->sec || each_func->sec == sec)
1643 && addr >= arange->low
1644 && addr < arange->high
1645 && each_func->name
1646 && strcmp (name, each_func->name) == 0
1647 && (!best_fit
1648 || ((arange->high - arange->low)
1649 < (best_fit->arange.high - best_fit->arange.low))))
1650 best_fit = each_func;
1651 }
1652 }
1653
1654 if (best_fit)
1655 {
1656 best_fit->sec = sec;
1657 *filename_ptr = best_fit->file;
1658 *linenumber_ptr = best_fit->line;
1659 return TRUE;
1660 }
1661 else
1662 return FALSE;
1663 }
1664
1665 /* Variable table functions. */
1666
1667 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1668 LINENUMBER_PTR, and return TRUE. */
1669
1670 static bfd_boolean
1671 lookup_symbol_in_variable_table (struct comp_unit *unit,
1672 asymbol *sym,
1673 bfd_vma addr,
1674 const char **filename_ptr,
1675 unsigned int *linenumber_ptr)
1676 {
1677 const char *name = bfd_asymbol_name (sym);
1678 asection *sec = bfd_get_section (sym);
1679 struct varinfo* each;
1680
1681 for (each = unit->variable_table; each; each = each->prev_var)
1682 if (each->stack == 0
1683 && each->file != NULL
1684 && each->name != NULL
1685 && each->addr == addr
1686 && (!each->sec || each->sec == sec)
1687 && strcmp (name, each->name) == 0)
1688 break;
1689
1690 if (each)
1691 {
1692 each->sec = sec;
1693 *filename_ptr = each->file;
1694 *linenumber_ptr = each->line;
1695 return TRUE;
1696 }
1697 else
1698 return FALSE;
1699 }
1700
1701 static char *
1702 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1703 {
1704 bfd *abfd = unit->abfd;
1705 bfd_byte *info_ptr;
1706 unsigned int abbrev_number, bytes_read, i;
1707 struct abbrev_info *abbrev;
1708 struct attribute attr;
1709 char *name = 0;
1710
1711 info_ptr = unit->info_ptr_unit + die_ref;
1712 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1713 info_ptr += bytes_read;
1714
1715 if (abbrev_number)
1716 {
1717 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1718 if (! abbrev)
1719 {
1720 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1721 abbrev_number);
1722 bfd_set_error (bfd_error_bad_value);
1723 }
1724 else
1725 {
1726 for (i = 0; i < abbrev->num_attrs; ++i)
1727 {
1728 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1729 switch (attr.name)
1730 {
1731 case DW_AT_name:
1732 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1733 if (name == NULL)
1734 name = attr.u.str;
1735 break;
1736 case DW_AT_specification:
1737 name = find_abstract_instance_name (unit, attr.u.val);
1738 break;
1739 case DW_AT_MIPS_linkage_name:
1740 name = attr.u.str;
1741 break;
1742 default:
1743 break;
1744 }
1745 }
1746 }
1747 }
1748 return (name);
1749 }
1750
1751 static void
1752 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1753 {
1754 bfd_byte *ranges_ptr;
1755 bfd_vma base_address = unit->base_address;
1756
1757 if (! unit->stash->dwarf_ranges_buffer)
1758 {
1759 if (! read_debug_ranges (unit))
1760 return;
1761 }
1762 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1763
1764 for (;;)
1765 {
1766 bfd_vma low_pc;
1767 bfd_vma high_pc;
1768
1769 if (unit->addr_size == 4)
1770 {
1771 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1772 ranges_ptr += 4;
1773 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1774 ranges_ptr += 4;
1775 }
1776 else
1777 {
1778 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1779 ranges_ptr += 8;
1780 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1781 ranges_ptr += 8;
1782 }
1783 if (low_pc == 0 && high_pc == 0)
1784 break;
1785 if (low_pc == -1UL && high_pc != -1UL)
1786 base_address = high_pc;
1787 else
1788 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1789 }
1790 }
1791
1792 /* DWARF2 Compilation unit functions. */
1793
1794 /* Scan over each die in a comp. unit looking for functions to add
1795 to the function table and variables to the variable table. */
1796
1797 static bfd_boolean
1798 scan_unit_for_symbols (struct comp_unit *unit)
1799 {
1800 bfd *abfd = unit->abfd;
1801 bfd_byte *info_ptr = unit->first_child_die_ptr;
1802 int nesting_level = 1;
1803 struct funcinfo **nested_funcs;
1804 int nested_funcs_size;
1805
1806 /* Maintain a stack of in-scope functions and inlined functions, which we
1807 can use to set the caller_func field. */
1808 nested_funcs_size = 32;
1809 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1810 if (nested_funcs == NULL)
1811 return FALSE;
1812 nested_funcs[nesting_level] = 0;
1813
1814 while (nesting_level)
1815 {
1816 unsigned int abbrev_number, bytes_read, i;
1817 struct abbrev_info *abbrev;
1818 struct attribute attr;
1819 struct funcinfo *func;
1820 struct varinfo *var;
1821 bfd_vma low_pc = 0;
1822 bfd_vma high_pc = 0;
1823
1824 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1825 info_ptr += bytes_read;
1826
1827 if (! abbrev_number)
1828 {
1829 nesting_level--;
1830 continue;
1831 }
1832
1833 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1834 if (! abbrev)
1835 {
1836 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1837 abbrev_number);
1838 bfd_set_error (bfd_error_bad_value);
1839 free (nested_funcs);
1840 return FALSE;
1841 }
1842
1843 var = NULL;
1844 if (abbrev->tag == DW_TAG_subprogram
1845 || abbrev->tag == DW_TAG_entry_point
1846 || abbrev->tag == DW_TAG_inlined_subroutine)
1847 {
1848 bfd_size_type amt = sizeof (struct funcinfo);
1849 func = bfd_zalloc (abfd, amt);
1850 func->tag = abbrev->tag;
1851 func->prev_func = unit->function_table;
1852 unit->function_table = func;
1853 BFD_ASSERT (!unit->cached);
1854
1855 if (func->tag == DW_TAG_inlined_subroutine)
1856 for (i = nesting_level - 1; i >= 1; i--)
1857 if (nested_funcs[i])
1858 {
1859 func->caller_func = nested_funcs[i];
1860 break;
1861 }
1862 nested_funcs[nesting_level] = func;
1863 }
1864 else
1865 {
1866 func = NULL;
1867 if (abbrev->tag == DW_TAG_variable)
1868 {
1869 bfd_size_type amt = sizeof (struct varinfo);
1870 var = bfd_zalloc (abfd, amt);
1871 var->tag = abbrev->tag;
1872 var->stack = 1;
1873 var->prev_var = unit->variable_table;
1874 unit->variable_table = var;
1875 BFD_ASSERT (!unit->cached);
1876 }
1877
1878 /* No inline function in scope at this nesting level. */
1879 nested_funcs[nesting_level] = 0;
1880 }
1881
1882 for (i = 0; i < abbrev->num_attrs; ++i)
1883 {
1884 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1885
1886 if (func)
1887 {
1888 switch (attr.name)
1889 {
1890 case DW_AT_call_file:
1891 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1892 break;
1893
1894 case DW_AT_call_line:
1895 func->caller_line = attr.u.val;
1896 break;
1897
1898 case DW_AT_abstract_origin:
1899 func->name = find_abstract_instance_name (unit, attr.u.val);
1900 break;
1901
1902 case DW_AT_name:
1903 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1904 if (func->name == NULL)
1905 func->name = attr.u.str;
1906 break;
1907
1908 case DW_AT_MIPS_linkage_name:
1909 func->name = attr.u.str;
1910 break;
1911
1912 case DW_AT_low_pc:
1913 low_pc = attr.u.val;
1914 break;
1915
1916 case DW_AT_high_pc:
1917 high_pc = attr.u.val;
1918 break;
1919
1920 case DW_AT_ranges:
1921 read_rangelist (unit, &func->arange, attr.u.val);
1922 break;
1923
1924 case DW_AT_decl_file:
1925 func->file = concat_filename (unit->line_table,
1926 attr.u.val);
1927 break;
1928
1929 case DW_AT_decl_line:
1930 func->line = attr.u.val;
1931 break;
1932
1933 default:
1934 break;
1935 }
1936 }
1937 else if (var)
1938 {
1939 switch (attr.name)
1940 {
1941 case DW_AT_name:
1942 var->name = attr.u.str;
1943 break;
1944
1945 case DW_AT_decl_file:
1946 var->file = concat_filename (unit->line_table,
1947 attr.u.val);
1948 break;
1949
1950 case DW_AT_decl_line:
1951 var->line = attr.u.val;
1952 break;
1953
1954 case DW_AT_external:
1955 if (attr.u.val != 0)
1956 var->stack = 0;
1957 break;
1958
1959 case DW_AT_location:
1960 switch (attr.form)
1961 {
1962 case DW_FORM_block:
1963 case DW_FORM_block1:
1964 case DW_FORM_block2:
1965 case DW_FORM_block4:
1966 if (*attr.u.blk->data == DW_OP_addr)
1967 {
1968 var->stack = 0;
1969
1970 /* Verify that DW_OP_addr is the only opcode in the
1971 location, in which case the block size will be 1
1972 plus the address size. */
1973 /* ??? For TLS variables, gcc can emit
1974 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1975 which we don't handle here yet. */
1976 if (attr.u.blk->size == unit->addr_size + 1U)
1977 var->addr = bfd_get (unit->addr_size * 8,
1978 unit->abfd,
1979 attr.u.blk->data + 1);
1980 }
1981 break;
1982
1983 default:
1984 break;
1985 }
1986 break;
1987
1988 default:
1989 break;
1990 }
1991 }
1992 }
1993
1994 if (func && high_pc != 0)
1995 {
1996 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1997 }
1998
1999 if (abbrev->has_children)
2000 {
2001 nesting_level++;
2002
2003 if (nesting_level >= nested_funcs_size)
2004 {
2005 struct funcinfo **tmp;
2006
2007 nested_funcs_size *= 2;
2008 tmp = bfd_realloc (nested_funcs,
2009 (nested_funcs_size
2010 * sizeof (struct funcinfo *)));
2011 if (tmp == NULL)
2012 {
2013 free (nested_funcs);
2014 return FALSE;
2015 }
2016 nested_funcs = tmp;
2017 }
2018 nested_funcs[nesting_level] = 0;
2019 }
2020 }
2021
2022 free (nested_funcs);
2023 return TRUE;
2024 }
2025
2026 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2027 includes the compilation unit header that proceeds the DIE's, but
2028 does not include the length field that precedes each compilation
2029 unit header. END_PTR points one past the end of this comp unit.
2030 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2031
2032 This routine does not read the whole compilation unit; only enough
2033 to get to the line number information for the compilation unit. */
2034
2035 static struct comp_unit *
2036 parse_comp_unit (struct dwarf2_debug *stash,
2037 bfd_vma unit_length,
2038 bfd_byte *info_ptr_unit,
2039 unsigned int offset_size)
2040 {
2041 struct comp_unit* unit;
2042 unsigned int version;
2043 bfd_uint64_t abbrev_offset = 0;
2044 unsigned int addr_size;
2045 struct abbrev_info** abbrevs;
2046 unsigned int abbrev_number, bytes_read, i;
2047 struct abbrev_info *abbrev;
2048 struct attribute attr;
2049 bfd_byte *info_ptr = stash->info_ptr;
2050 bfd_byte *end_ptr = info_ptr + unit_length;
2051 bfd_size_type amt;
2052 bfd_vma low_pc = 0;
2053 bfd_vma high_pc = 0;
2054 bfd *abfd = stash->bfd;
2055
2056 version = read_2_bytes (abfd, info_ptr);
2057 info_ptr += 2;
2058 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2059 if (offset_size == 4)
2060 abbrev_offset = read_4_bytes (abfd, info_ptr);
2061 else
2062 abbrev_offset = read_8_bytes (abfd, info_ptr);
2063 info_ptr += offset_size;
2064 addr_size = read_1_byte (abfd, info_ptr);
2065 info_ptr += 1;
2066
2067 if (version != 2)
2068 {
2069 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
2070 bfd_set_error (bfd_error_bad_value);
2071 return 0;
2072 }
2073
2074 if (addr_size > sizeof (bfd_vma))
2075 {
2076 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2077 addr_size,
2078 (unsigned int) sizeof (bfd_vma));
2079 bfd_set_error (bfd_error_bad_value);
2080 return 0;
2081 }
2082
2083 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2084 {
2085 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2086 bfd_set_error (bfd_error_bad_value);
2087 return 0;
2088 }
2089
2090 /* Read the abbrevs for this compilation unit into a table. */
2091 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2092 if (! abbrevs)
2093 return 0;
2094
2095 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2096 info_ptr += bytes_read;
2097 if (! abbrev_number)
2098 {
2099 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2100 abbrev_number);
2101 bfd_set_error (bfd_error_bad_value);
2102 return 0;
2103 }
2104
2105 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2106 if (! abbrev)
2107 {
2108 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2109 abbrev_number);
2110 bfd_set_error (bfd_error_bad_value);
2111 return 0;
2112 }
2113
2114 amt = sizeof (struct comp_unit);
2115 unit = bfd_zalloc (abfd, amt);
2116 unit->abfd = abfd;
2117 unit->addr_size = addr_size;
2118 unit->offset_size = offset_size;
2119 unit->abbrevs = abbrevs;
2120 unit->end_ptr = end_ptr;
2121 unit->stash = stash;
2122 unit->info_ptr_unit = info_ptr_unit;
2123
2124 for (i = 0; i < abbrev->num_attrs; ++i)
2125 {
2126 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2127
2128 /* Store the data if it is of an attribute we want to keep in a
2129 partial symbol table. */
2130 switch (attr.name)
2131 {
2132 case DW_AT_stmt_list:
2133 unit->stmtlist = 1;
2134 unit->line_offset = attr.u.val;
2135 break;
2136
2137 case DW_AT_name:
2138 unit->name = attr.u.str;
2139 break;
2140
2141 case DW_AT_low_pc:
2142 low_pc = attr.u.val;
2143 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2144 this is the base address to use when reading location
2145 lists or range lists. */
2146 unit->base_address = low_pc;
2147 break;
2148
2149 case DW_AT_high_pc:
2150 high_pc = attr.u.val;
2151 break;
2152
2153 case DW_AT_ranges:
2154 read_rangelist (unit, &unit->arange, attr.u.val);
2155 break;
2156
2157 case DW_AT_comp_dir:
2158 {
2159 char *comp_dir = attr.u.str;
2160 if (comp_dir)
2161 {
2162 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2163 directory, get rid of it. */
2164 char *cp = strchr (comp_dir, ':');
2165
2166 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2167 comp_dir = cp + 1;
2168 }
2169 unit->comp_dir = comp_dir;
2170 break;
2171 }
2172
2173 default:
2174 break;
2175 }
2176 }
2177 if (high_pc != 0)
2178 {
2179 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2180 }
2181
2182 unit->first_child_die_ptr = info_ptr;
2183 return unit;
2184 }
2185
2186 /* Return TRUE if UNIT may contain the address given by ADDR. When
2187 there are functions written entirely with inline asm statements, the
2188 range info in the compilation unit header may not be correct. We
2189 need to consult the line info table to see if a compilation unit
2190 really contains the given address. */
2191
2192 static bfd_boolean
2193 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2194 {
2195 struct arange *arange;
2196
2197 if (unit->error)
2198 return FALSE;
2199
2200 arange = &unit->arange;
2201 do
2202 {
2203 if (addr >= arange->low && addr < arange->high)
2204 return TRUE;
2205 arange = arange->next;
2206 }
2207 while (arange);
2208
2209 return FALSE;
2210 }
2211
2212 /* If UNIT contains ADDR, set the output parameters to the values for
2213 the line containing ADDR. The output parameters, FILENAME_PTR,
2214 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2215 to be filled in.
2216
2217 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2218 FALSE otherwise. */
2219
2220 static bfd_boolean
2221 comp_unit_find_nearest_line (struct comp_unit *unit,
2222 bfd_vma addr,
2223 const char **filename_ptr,
2224 const char **functionname_ptr,
2225 unsigned int *linenumber_ptr,
2226 struct dwarf2_debug *stash)
2227 {
2228 bfd_boolean line_p;
2229 bfd_boolean func_p;
2230 struct funcinfo *function;
2231
2232 if (unit->error)
2233 return FALSE;
2234
2235 if (! unit->line_table)
2236 {
2237 if (! unit->stmtlist)
2238 {
2239 unit->error = 1;
2240 return FALSE;
2241 }
2242
2243 unit->line_table = decode_line_info (unit, stash);
2244
2245 if (! unit->line_table)
2246 {
2247 unit->error = 1;
2248 return FALSE;
2249 }
2250
2251 if (unit->first_child_die_ptr < unit->end_ptr
2252 && ! scan_unit_for_symbols (unit))
2253 {
2254 unit->error = 1;
2255 return FALSE;
2256 }
2257 }
2258
2259 function = NULL;
2260 func_p = lookup_address_in_function_table (unit, addr,
2261 &function, functionname_ptr);
2262 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2263 stash->inliner_chain = function;
2264 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2265 function, filename_ptr,
2266 linenumber_ptr);
2267 return line_p || func_p;
2268 }
2269
2270 /* Check to see if line info is already decoded in a comp_unit.
2271 If not, decode it. Returns TRUE if no errors were encountered;
2272 FALSE otherwise. */
2273
2274 static bfd_boolean
2275 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2276 struct dwarf2_debug *stash)
2277 {
2278 if (unit->error)
2279 return FALSE;
2280
2281 if (! unit->line_table)
2282 {
2283 if (! unit->stmtlist)
2284 {
2285 unit->error = 1;
2286 return FALSE;
2287 }
2288
2289 unit->line_table = decode_line_info (unit, stash);
2290
2291 if (! unit->line_table)
2292 {
2293 unit->error = 1;
2294 return FALSE;
2295 }
2296
2297 if (unit->first_child_die_ptr < unit->end_ptr
2298 && ! scan_unit_for_symbols (unit))
2299 {
2300 unit->error = 1;
2301 return FALSE;
2302 }
2303 }
2304
2305 return TRUE;
2306 }
2307
2308 /* If UNIT contains SYM at ADDR, set the output parameters to the
2309 values for the line containing SYM. The output parameters,
2310 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2311 filled in.
2312
2313 Return TRUE if UNIT contains SYM, and no errors were encountered;
2314 FALSE otherwise. */
2315
2316 static bfd_boolean
2317 comp_unit_find_line (struct comp_unit *unit,
2318 asymbol *sym,
2319 bfd_vma addr,
2320 const char **filename_ptr,
2321 unsigned int *linenumber_ptr,
2322 struct dwarf2_debug *stash)
2323 {
2324 if (!comp_unit_maybe_decode_line_info (unit, stash))
2325 return FALSE;
2326
2327 if (sym->flags & BSF_FUNCTION)
2328 return lookup_symbol_in_function_table (unit, sym, addr,
2329 filename_ptr,
2330 linenumber_ptr);
2331
2332 return lookup_symbol_in_variable_table (unit, sym, addr,
2333 filename_ptr,
2334 linenumber_ptr);
2335 }
2336
2337 static struct funcinfo *
2338 reverse_funcinfo_list (struct funcinfo *head)
2339 {
2340 struct funcinfo *rhead;
2341 struct funcinfo *temp;
2342
2343 for (rhead = NULL; head; head = temp)
2344 {
2345 temp = head->prev_func;
2346 head->prev_func = rhead;
2347 rhead = head;
2348 }
2349 return rhead;
2350 }
2351
2352 static struct varinfo *
2353 reverse_varinfo_list (struct varinfo *head)
2354 {
2355 struct varinfo *rhead;
2356 struct varinfo *temp;
2357
2358 for (rhead = NULL; head; head = temp)
2359 {
2360 temp = head->prev_var;
2361 head->prev_var = rhead;
2362 rhead = head;
2363 }
2364 return rhead;
2365 }
2366
2367 /* Extract all interesting funcinfos and varinfos of a compilation
2368 unit into hash tables for faster lookup. Returns TRUE if no
2369 errors were enountered; FALSE otherwise. */
2370
2371 static bfd_boolean
2372 comp_unit_hash_info (struct dwarf2_debug *stash,
2373 struct comp_unit *unit,
2374 struct info_hash_table *funcinfo_hash_table,
2375 struct info_hash_table *varinfo_hash_table)
2376 {
2377 struct funcinfo* each_func;
2378 struct varinfo* each_var;
2379 bfd_boolean okay = TRUE;
2380
2381 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2382
2383 if (!comp_unit_maybe_decode_line_info (unit, stash))
2384 return FALSE;
2385
2386 BFD_ASSERT (!unit->cached);
2387
2388 /* To preserve the original search order, we went to visit the function
2389 infos in the reversed order of the list. However, making the list
2390 bi-directional use quite a bit of extra memory. So we reverse
2391 the list first, traverse the list in the now reversed order and
2392 finally reverse the list again to get back the original order. */
2393 unit->function_table = reverse_funcinfo_list (unit->function_table);
2394 for (each_func = unit->function_table;
2395 each_func && okay;
2396 each_func = each_func->prev_func)
2397 {
2398 /* Skip nameless functions. */
2399 if (each_func->name)
2400 /* There is no need to copy name string into hash table as
2401 name string is either in the dwarf string buffer or
2402 info in the stash. */
2403 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2404 (void*) each_func, FALSE);
2405 }
2406 unit->function_table = reverse_funcinfo_list (unit->function_table);
2407 if (!okay)
2408 return FALSE;
2409
2410 /* We do the same for variable infos. */
2411 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2412 for (each_var = unit->variable_table;
2413 each_var && okay;
2414 each_var = each_var->prev_var)
2415 {
2416 /* Skip stack vars and vars with no files or names. */
2417 if (each_var->stack == 0
2418 && each_var->file != NULL
2419 && each_var->name != NULL)
2420 /* There is no need to copy name string into hash table as
2421 name string is either in the dwarf string buffer or
2422 info in the stash. */
2423 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2424 (void*) each_var, FALSE);
2425 }
2426
2427 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2428 unit->cached = TRUE;
2429 return okay;
2430 }
2431
2432 /* Locate a section in a BFD containing debugging info. The search starts
2433 from the section after AFTER_SEC, or from the first section in the BFD if
2434 AFTER_SEC is NULL. The search works by examining the names of the
2435 sections. There are two permissiable names. The first is .debug_info.
2436 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2437 This is a variation on the .debug_info section which has a checksum
2438 describing the contents appended onto the name. This allows the linker to
2439 identify and discard duplicate debugging sections for different
2440 compilation units. */
2441 #define DWARF2_DEBUG_INFO ".debug_info"
2442 #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2443 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2444
2445 static asection *
2446 find_debug_info (bfd *abfd, asection *after_sec)
2447 {
2448 asection * msec;
2449
2450 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2451
2452 while (msec)
2453 {
2454 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2455 return msec;
2456
2457 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2458 return msec;
2459
2460 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2461 return msec;
2462
2463 msec = msec->next;
2464 }
2465
2466 return NULL;
2467 }
2468
2469 /* Unset vmas for loadable sections in STASH. */
2470
2471 static void
2472 unset_sections (struct dwarf2_debug *stash)
2473 {
2474 unsigned int i;
2475 struct loadable_section *p;
2476
2477 i = stash->loadable_section_count;
2478 p = stash->loadable_sections;
2479 for (; i > 0; i--, p++)
2480 p->section->vma = 0;
2481 }
2482
2483 /* Set unique vmas for loadable sections in ABFD and save vmas in
2484 STASH for unset_sections. */
2485
2486 static bfd_boolean
2487 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2488 {
2489 struct loadable_section *p;
2490 unsigned int i;
2491
2492 if (stash->loadable_section_count != 0)
2493 {
2494 i = stash->loadable_section_count;
2495 p = stash->loadable_sections;
2496 for (; i > 0; i--, p++)
2497 p->section->vma = p->adj_vma;
2498 }
2499 else
2500 {
2501 asection *sect;
2502 bfd_vma last_vma = 0;
2503 bfd_size_type amt;
2504 struct loadable_section *p;
2505
2506 i = 0;
2507 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2508 {
2509 bfd_size_type sz;
2510
2511 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2512 continue;
2513
2514 sz = sect->rawsize ? sect->rawsize : sect->size;
2515 if (sz == 0)
2516 continue;
2517
2518 i++;
2519 }
2520
2521 amt = i * sizeof (struct loadable_section);
2522 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2523 if (! p)
2524 return FALSE;
2525
2526 stash->loadable_sections = p;
2527 stash->loadable_section_count = i;
2528
2529 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2530 {
2531 bfd_size_type sz;
2532
2533 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2534 continue;
2535
2536 sz = sect->rawsize ? sect->rawsize : sect->size;
2537 if (sz == 0)
2538 continue;
2539
2540 p->section = sect;
2541 if (last_vma != 0)
2542 {
2543 /* Align the new address to the current section
2544 alignment. */
2545 last_vma = ((last_vma
2546 + ~((bfd_vma) -1 << sect->alignment_power))
2547 & ((bfd_vma) -1 << sect->alignment_power));
2548 sect->vma = last_vma;
2549 }
2550 p->adj_vma = sect->vma;
2551 last_vma += sect->vma + sz;
2552
2553 p++;
2554 }
2555 }
2556
2557 return TRUE;
2558 }
2559
2560 /* Look up a funcinfo by name using the given info hash table. If found,
2561 also update the locations pointed to by filename_ptr and linenumber_ptr.
2562
2563 This function returns TRUE if a funcinfo that matches the given symbol
2564 and address is found with any error; otherwise it returns FALSE. */
2565
2566 static bfd_boolean
2567 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2568 asymbol *sym,
2569 bfd_vma addr,
2570 const char **filename_ptr,
2571 unsigned int *linenumber_ptr)
2572 {
2573 struct funcinfo* each_func;
2574 struct funcinfo* best_fit = NULL;
2575 struct info_list_node *node;
2576 struct arange *arange;
2577 const char *name = bfd_asymbol_name (sym);
2578 asection *sec = bfd_get_section (sym);
2579
2580 for (node = lookup_info_hash_table (hash_table, name);
2581 node;
2582 node = node->next)
2583 {
2584 each_func = node->info;
2585 for (arange = &each_func->arange;
2586 arange;
2587 arange = arange->next)
2588 {
2589 if ((!each_func->sec || each_func->sec == sec)
2590 && addr >= arange->low
2591 && addr < arange->high
2592 && (!best_fit
2593 || ((arange->high - arange->low)
2594 < (best_fit->arange.high - best_fit->arange.low))))
2595 best_fit = each_func;
2596 }
2597 }
2598
2599 if (best_fit)
2600 {
2601 best_fit->sec = sec;
2602 *filename_ptr = best_fit->file;
2603 *linenumber_ptr = best_fit->line;
2604 return TRUE;
2605 }
2606
2607 return FALSE;
2608 }
2609
2610 /* Look up a varinfo by name using the given info hash table. If found,
2611 also update the locations pointed to by filename_ptr and linenumber_ptr.
2612
2613 This function returns TRUE if a varinfo that matches the given symbol
2614 and address is found with any error; otherwise it returns FALSE. */
2615
2616 static bfd_boolean
2617 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2618 asymbol *sym,
2619 bfd_vma addr,
2620 const char **filename_ptr,
2621 unsigned int *linenumber_ptr)
2622 {
2623 const char *name = bfd_asymbol_name (sym);
2624 asection *sec = bfd_get_section (sym);
2625 struct varinfo* each;
2626 struct info_list_node *node;
2627
2628 for (node = lookup_info_hash_table (hash_table, name);
2629 node;
2630 node = node->next)
2631 {
2632 each = node->info;
2633 if (each->addr == addr
2634 && (!each->sec || each->sec == sec))
2635 {
2636 each->sec = sec;
2637 *filename_ptr = each->file;
2638 *linenumber_ptr = each->line;
2639 return TRUE;
2640 }
2641 }
2642
2643 return FALSE;
2644 }
2645
2646 /* Update the funcinfo and varinfo info hash tables if they are
2647 not up to date. Returns TRUE if there is no error; otherwise
2648 returns FALSE and disable the info hash tables. */
2649
2650 static bfd_boolean
2651 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2652 {
2653 struct comp_unit *each;
2654
2655 /* Exit if hash tables are up-to-date. */
2656 if (stash->all_comp_units == stash->hash_units_head)
2657 return TRUE;
2658
2659 if (stash->hash_units_head)
2660 each = stash->hash_units_head->prev_unit;
2661 else
2662 each = stash->last_comp_unit;
2663
2664 while (each)
2665 {
2666 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2667 stash->varinfo_hash_table))
2668 {
2669 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2670 return FALSE;
2671 }
2672 each = each->prev_unit;
2673 }
2674
2675 stash->hash_units_head = stash->all_comp_units;
2676 return TRUE;
2677 }
2678
2679 /* Check consistency of info hash tables. This is for debugging only. */
2680
2681 static void ATTRIBUTE_UNUSED
2682 stash_verify_info_hash_table (struct dwarf2_debug *stash)
2683 {
2684 struct comp_unit *each_unit;
2685 struct funcinfo *each_func;
2686 struct varinfo *each_var;
2687 struct info_list_node *node;
2688 bfd_boolean found;
2689
2690 for (each_unit = stash->all_comp_units;
2691 each_unit;
2692 each_unit = each_unit->next_unit)
2693 {
2694 for (each_func = each_unit->function_table;
2695 each_func;
2696 each_func = each_func->prev_func)
2697 {
2698 if (!each_func->name)
2699 continue;
2700 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2701 each_func->name);
2702 BFD_ASSERT (node);
2703 found = FALSE;
2704 while (node && !found)
2705 {
2706 found = node->info == each_func;
2707 node = node->next;
2708 }
2709 BFD_ASSERT (found);
2710 }
2711
2712 for (each_var = each_unit->variable_table;
2713 each_var;
2714 each_var = each_var->prev_var)
2715 {
2716 if (!each_var->name || !each_var->file || each_var->stack)
2717 continue;
2718 node = lookup_info_hash_table (stash->varinfo_hash_table,
2719 each_var->name);
2720 BFD_ASSERT (node);
2721 found = FALSE;
2722 while (node && !found)
2723 {
2724 found = node->info == each_var;
2725 node = node->next;
2726 }
2727 BFD_ASSERT (found);
2728 }
2729 }
2730 }
2731
2732 /* Check to see if we want to enable the info hash tables, which consume
2733 quite a bit of memory. Currently we only check the number times
2734 bfd_dwarf2_find_line is called. In the future, we may also want to
2735 take the number of symbols into account. */
2736
2737 static void
2738 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2739 {
2740 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2741
2742 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2743 return;
2744
2745 /* FIXME: Maybe we should check the reduce_memory_overheads
2746 and optimize fields in the bfd_link_info structure ? */
2747
2748 /* Create hash tables. */
2749 stash->funcinfo_hash_table = create_info_hash_table (abfd);
2750 stash->varinfo_hash_table = create_info_hash_table (abfd);
2751 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2752 {
2753 /* Turn off info hashes if any allocation above fails. */
2754 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2755 return;
2756 }
2757 /* We need a forced update so that the info hash tables will
2758 be created even though there is no compilation unit. That
2759 happens if STASH_INFO_HASH_TRIGGER is 0. */
2760 stash_maybe_update_info_hash_tables (stash);
2761 stash->info_hash_status = STASH_INFO_HASH_ON;
2762 }
2763
2764 /* Find the file and line associated with a symbol and address using the
2765 info hash tables of a stash. If there is a match, the function returns
2766 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2767 otherwise it returns FALSE. */
2768
2769 static bfd_boolean
2770 stash_find_line_fast (struct dwarf2_debug *stash,
2771 asymbol *sym,
2772 bfd_vma addr,
2773 const char **filename_ptr,
2774 unsigned int *linenumber_ptr)
2775 {
2776 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2777
2778 if (sym->flags & BSF_FUNCTION)
2779 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2780 filename_ptr, linenumber_ptr);
2781 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2782 filename_ptr, linenumber_ptr);
2783 }
2784
2785 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2786 then find the nearest source code location corresponding to
2787 the address SECTION + OFFSET.
2788 Returns TRUE if the line is found without error and fills in
2789 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2790 NULL the FUNCTIONNAME_PTR is also filled in.
2791 SYMBOLS contains the symbol table for ABFD.
2792 ADDR_SIZE is the number of bytes in the initial .debug_info length
2793 field and in the abbreviation offset, or zero to indicate that the
2794 default value should be used. */
2795
2796 static bfd_boolean
2797 find_line (bfd *abfd,
2798 asection *section,
2799 bfd_vma offset,
2800 asymbol *symbol,
2801 asymbol **symbols,
2802 const char **filename_ptr,
2803 const char **functionname_ptr,
2804 unsigned int *linenumber_ptr,
2805 unsigned int addr_size,
2806 void **pinfo)
2807 {
2808 /* Read each compilation unit from the section .debug_info, and check
2809 to see if it contains the address we are searching for. If yes,
2810 lookup the address, and return the line number info. If no, go
2811 on to the next compilation unit.
2812
2813 We keep a list of all the previously read compilation units, and
2814 a pointer to the next un-read compilation unit. Check the
2815 previously read units before reading more. */
2816 struct dwarf2_debug *stash;
2817 /* What address are we looking for? */
2818 bfd_vma addr;
2819 struct comp_unit* each;
2820 bfd_vma found = FALSE;
2821 bfd_boolean do_line;
2822
2823 stash = *pinfo;
2824
2825 if (! stash)
2826 {
2827 bfd_size_type amt = sizeof (struct dwarf2_debug);
2828
2829 stash = bfd_zalloc (abfd, amt);
2830 if (! stash)
2831 return FALSE;
2832 }
2833
2834 /* In a relocatable file, 2 functions may have the same address.
2835 We change the section vma so that they won't overlap. */
2836 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2837 {
2838 if (! place_sections (abfd, stash))
2839 return FALSE;
2840 }
2841
2842 do_line = (section == NULL
2843 && offset == 0
2844 && functionname_ptr == NULL
2845 && symbol != NULL);
2846 if (do_line)
2847 {
2848 addr = symbol->value;
2849 section = bfd_get_section (symbol);
2850 }
2851 else if (section != NULL
2852 && functionname_ptr != NULL
2853 && symbol == NULL)
2854 addr = offset;
2855 else
2856 abort ();
2857
2858 if (section->output_section)
2859 addr += section->output_section->vma + section->output_offset;
2860 else
2861 addr += section->vma;
2862 *filename_ptr = NULL;
2863 if (! do_line)
2864 *functionname_ptr = NULL;
2865 *linenumber_ptr = 0;
2866
2867 if (! *pinfo)
2868 {
2869 bfd *debug_bfd;
2870 bfd_size_type total_size;
2871 asection *msec;
2872
2873 *pinfo = stash;
2874
2875 msec = find_debug_info (abfd, NULL);
2876 if (msec == NULL)
2877 {
2878 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2879
2880 if (debug_filename == NULL)
2881 /* No dwarf2 info, and no gnu_debuglink to follow.
2882 Note that at this point the stash has been allocated, but
2883 contains zeros. This lets future calls to this function
2884 fail more quickly. */
2885 goto done;
2886
2887 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2888 || ! bfd_check_format (debug_bfd, bfd_object)
2889 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2890 {
2891 if (debug_bfd)
2892 bfd_close (debug_bfd);
2893 /* FIXME: Should we report our failure to follow the debuglink ? */
2894 free (debug_filename);
2895 goto done;
2896 }
2897 }
2898 else
2899 debug_bfd = abfd;
2900
2901 /* There can be more than one DWARF2 info section in a BFD these
2902 days. First handle the easy case when there's only one. If
2903 there's more than one, try case two: none of the sections is
2904 compressed. In that case, read them all in and produce one
2905 large stash. We do this in two passes - in the first pass we
2906 just accumulate the section sizes, and in the second pass we
2907 read in the section's contents. (The allows us to avoid
2908 reallocing the data as we add sections to the stash.) If
2909 some or all sections are compressed, then do things the slow
2910 way, with a bunch of reallocs. */
2911
2912 if (! find_debug_info (debug_bfd, msec))
2913 {
2914 /* Case 1: only one info section. */
2915 total_size = msec->size;
2916 if (! read_section (debug_bfd, ".debug_info", ".zdebug_info",
2917 symbols, 0,
2918 &stash->info_ptr, &total_size))
2919 goto done;
2920 stash->info_ptr_end = stash->info_ptr + total_size;
2921 }
2922 else
2923 {
2924 int all_uncompressed = 1;
2925 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
2926 {
2927 total_size += msec->size;
2928 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2929 all_uncompressed = 0;
2930 }
2931 if (all_uncompressed)
2932 {
2933 /* Case 2: multiple sections, but none is compressed. */
2934 stash->info_ptr = bfd_malloc (total_size);
2935 if (stash->info_ptr == NULL)
2936 goto done;
2937
2938 stash->info_ptr_end = stash->info_ptr;
2939
2940 for (msec = find_debug_info (debug_bfd, NULL);
2941 msec;
2942 msec = find_debug_info (debug_bfd, msec))
2943 {
2944 bfd_size_type size;
2945 bfd_size_type start;
2946
2947 size = msec->size;
2948 if (size == 0)
2949 continue;
2950
2951 start = stash->info_ptr_end - stash->info_ptr;
2952
2953 if ((bfd_simple_get_relocated_section_contents
2954 (debug_bfd, msec, stash->info_ptr + start, symbols))
2955 == NULL)
2956 continue;
2957
2958 stash->info_ptr_end = stash->info_ptr + start + size;
2959 }
2960
2961 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2962 }
2963 else
2964 {
2965 /* Case 3: multiple sections, some or all compressed. */
2966 stash->info_ptr = bfd_malloc (1);
2967 stash->info_ptr_end = stash->info_ptr;
2968 for (msec = find_debug_info (debug_bfd, NULL);
2969 msec;
2970 msec = find_debug_info (debug_bfd, msec))
2971 {
2972 bfd_size_type size = msec->size;
2973 bfd_byte* buffer
2974 = (bfd_simple_get_relocated_section_contents
2975 (debug_bfd, msec, NULL, symbols));
2976 if (! buffer)
2977 continue;
2978 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2979 {
2980 if (! bfd_uncompress_section_contents (&buffer, &size))
2981 continue;
2982 }
2983 stash->info_ptr = bfd_realloc (stash->info_ptr,
2984 stash->info_ptr_end
2985 - stash->info_ptr + size);
2986 memcpy (stash->info_ptr_end, buffer, size);
2987 free (buffer);
2988 stash->info_ptr_end += size;
2989 }
2990 }
2991 }
2992
2993 stash->sec = find_debug_info (debug_bfd, NULL);
2994 stash->sec_info_ptr = stash->info_ptr;
2995 stash->syms = symbols;
2996 stash->bfd = debug_bfd;
2997 }
2998
2999 /* A null info_ptr indicates that there is no dwarf2 info
3000 (or that an error occured while setting up the stash). */
3001 if (! stash->info_ptr)
3002 goto done;
3003
3004 stash->inliner_chain = NULL;
3005
3006 /* Check the previously read comp. units first. */
3007 if (do_line)
3008 {
3009 /* The info hash tables use quite a bit of memory. We may not want to
3010 always use them. We use some heuristics to decide if and when to
3011 turn it on. */
3012 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3013 stash_maybe_enable_info_hash_tables (abfd, stash);
3014
3015 /* Keep info hash table up to date if they are available. Note that we
3016 may disable the hash tables if there is any error duing update. */
3017 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3018 stash_maybe_update_info_hash_tables (stash);
3019
3020 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3021 {
3022 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3023 linenumber_ptr);
3024 if (found)
3025 goto done;
3026 }
3027 else
3028 {
3029 /* Check the previously read comp. units first. */
3030 for (each = stash->all_comp_units; each; each = each->next_unit)
3031 if ((symbol->flags & BSF_FUNCTION) == 0
3032 || comp_unit_contains_address (each, addr))
3033 {
3034 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3035 linenumber_ptr, stash);
3036 if (found)
3037 goto done;
3038 }
3039 }
3040 }
3041 else
3042 {
3043 for (each = stash->all_comp_units; each; each = each->next_unit)
3044 {
3045 found = (comp_unit_contains_address (each, addr)
3046 && comp_unit_find_nearest_line (each, addr,
3047 filename_ptr,
3048 functionname_ptr,
3049 linenumber_ptr,
3050 stash));
3051 if (found)
3052 goto done;
3053 }
3054 }
3055
3056 /* The DWARF2 spec says that the initial length field, and the
3057 offset of the abbreviation table, should both be 4-byte values.
3058 However, some compilers do things differently. */
3059 if (addr_size == 0)
3060 addr_size = 4;
3061 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3062
3063 /* Read each remaining comp. units checking each as they are read. */
3064 while (stash->info_ptr < stash->info_ptr_end)
3065 {
3066 bfd_vma length;
3067 unsigned int offset_size = addr_size;
3068 bfd_byte *info_ptr_unit = stash->info_ptr;
3069
3070 length = read_4_bytes (stash->bfd, stash->info_ptr);
3071 /* A 0xffffff length is the DWARF3 way of indicating
3072 we use 64-bit offsets, instead of 32-bit offsets. */
3073 if (length == 0xffffffff)
3074 {
3075 offset_size = 8;
3076 length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
3077 stash->info_ptr += 12;
3078 }
3079 /* A zero length is the IRIX way of indicating 64-bit offsets,
3080 mostly because the 64-bit length will generally fit in 32
3081 bits, and the endianness helps. */
3082 else if (length == 0)
3083 {
3084 offset_size = 8;
3085 length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
3086 stash->info_ptr += 8;
3087 }
3088 /* In the absence of the hints above, we assume 32-bit DWARF2
3089 offsets even for targets with 64-bit addresses, because:
3090 a) most of the time these targets will not have generated
3091 more than 2Gb of debug info and so will not need 64-bit
3092 offsets,
3093 and
3094 b) if they do use 64-bit offsets but they are not using
3095 the size hints that are tested for above then they are
3096 not conforming to the DWARF3 standard anyway. */
3097 else if (addr_size == 8)
3098 {
3099 offset_size = 4;
3100 stash->info_ptr += 4;
3101 }
3102 else
3103 stash->info_ptr += 4;
3104
3105 if (length > 0)
3106 {
3107 each = parse_comp_unit (stash, length, info_ptr_unit,
3108 offset_size);
3109 if (!each)
3110 /* The dwarf information is damaged, don't trust it any
3111 more. */
3112 break;
3113 stash->info_ptr += length;
3114
3115 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3116 == stash->sec->size)
3117 {
3118 stash->sec = find_debug_info (stash->bfd, stash->sec);
3119 stash->sec_info_ptr = stash->info_ptr;
3120 }
3121
3122 if (stash->all_comp_units)
3123 stash->all_comp_units->prev_unit = each;
3124 else
3125 stash->last_comp_unit = each;
3126
3127 each->next_unit = stash->all_comp_units;
3128 stash->all_comp_units = each;
3129
3130 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3131 compilation units. If we don't have them (i.e.,
3132 unit->high == 0), we need to consult the line info table
3133 to see if a compilation unit contains the given
3134 address. */
3135 if (do_line)
3136 found = (((symbol->flags & BSF_FUNCTION) == 0
3137 || each->arange.high == 0
3138 || comp_unit_contains_address (each, addr))
3139 && comp_unit_find_line (each, symbol, addr,
3140 filename_ptr,
3141 linenumber_ptr,
3142 stash));
3143 else
3144 found = ((each->arange.high == 0
3145 || comp_unit_contains_address (each, addr))
3146 && comp_unit_find_nearest_line (each, addr,
3147 filename_ptr,
3148 functionname_ptr,
3149 linenumber_ptr,
3150 stash));
3151 if (found)
3152 goto done;
3153 }
3154 }
3155
3156 done:
3157 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3158 unset_sections (stash);
3159
3160 return found;
3161 }
3162
3163 /* The DWARF2 version of find_nearest_line.
3164 Return TRUE if the line is found without error. */
3165
3166 bfd_boolean
3167 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3168 asection *section,
3169 asymbol **symbols,
3170 bfd_vma offset,
3171 const char **filename_ptr,
3172 const char **functionname_ptr,
3173 unsigned int *linenumber_ptr,
3174 unsigned int addr_size,
3175 void **pinfo)
3176 {
3177 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3178 functionname_ptr, linenumber_ptr, addr_size,
3179 pinfo);
3180 }
3181
3182 /* The DWARF2 version of find_line.
3183 Return TRUE if the line is found without error. */
3184
3185 bfd_boolean
3186 _bfd_dwarf2_find_line (bfd *abfd,
3187 asymbol **symbols,
3188 asymbol *symbol,
3189 const char **filename_ptr,
3190 unsigned int *linenumber_ptr,
3191 unsigned int addr_size,
3192 void **pinfo)
3193 {
3194 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3195 NULL, linenumber_ptr, addr_size,
3196 pinfo);
3197 }
3198
3199 bfd_boolean
3200 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3201 const char **filename_ptr,
3202 const char **functionname_ptr,
3203 unsigned int *linenumber_ptr,
3204 void **pinfo)
3205 {
3206 struct dwarf2_debug *stash;
3207
3208 stash = *pinfo;
3209 if (stash)
3210 {
3211 struct funcinfo *func = stash->inliner_chain;
3212
3213 if (func && func->caller_func)
3214 {
3215 *filename_ptr = func->caller_file;
3216 *functionname_ptr = func->caller_func->name;
3217 *linenumber_ptr = func->caller_line;
3218 stash->inliner_chain = func->caller_func;
3219 return TRUE;
3220 }
3221 }
3222
3223 return FALSE;
3224 }
3225
3226 void
3227 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3228 {
3229 struct comp_unit *each;
3230 struct dwarf2_debug *stash;
3231
3232 if (abfd == NULL || elf_tdata (abfd) == NULL)
3233 return;
3234
3235 stash = elf_tdata (abfd)->dwarf2_find_line_info;
3236
3237 if (stash == NULL)
3238 return;
3239
3240 for (each = stash->all_comp_units; each; each = each->next_unit)
3241 {
3242 struct abbrev_info **abbrevs = each->abbrevs;
3243 struct funcinfo *function_table = each->function_table;
3244 struct varinfo *variable_table = each->variable_table;
3245 size_t i;
3246
3247 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3248 {
3249 struct abbrev_info *abbrev = abbrevs[i];
3250
3251 while (abbrev)
3252 {
3253 free (abbrev->attrs);
3254 abbrev = abbrev->next;
3255 }
3256 }
3257
3258 if (each->line_table)
3259 {
3260 free (each->line_table->dirs);
3261 free (each->line_table->files);
3262 }
3263
3264 while (function_table)
3265 {
3266 if (function_table->file)
3267 {
3268 free (function_table->file);
3269 function_table->file = NULL;
3270 }
3271
3272 if (function_table->caller_file)
3273 {
3274 free (function_table->caller_file);
3275 function_table->caller_file = NULL;
3276 }
3277 function_table = function_table->prev_func;
3278 }
3279
3280 while (variable_table)
3281 {
3282 if (variable_table->file)
3283 {
3284 free (variable_table->file);
3285 variable_table->file = NULL;
3286 }
3287
3288 variable_table = variable_table->prev_var;
3289 }
3290 }
3291
3292 free (stash->dwarf_abbrev_buffer);
3293 free (stash->dwarf_line_buffer);
3294 free (stash->dwarf_ranges_buffer);
3295 free (stash->sec_info_ptr);
3296 }