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