e8ebd6215fe8e0e1e6e42dd0e2df16281207a277
[deliverable/binutils-gdb.git] / bfd / dwarf2.c
1 /* DWARF 2 support.
2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31
32 #include "bfd.h"
33 #include "sysdep.h"
34 #include "libiberty.h"
35 #include "libbfd.h"
36 #include "elf-bfd.h"
37 #include "elf/dwarf2.h"
38
39 /* The data in the .debug_line statement prologue looks like this. */
40
41 struct line_head
42 {
43 bfd_vma total_length;
44 unsigned short version;
45 bfd_vma prologue_length;
46 unsigned char minimum_instruction_length;
47 unsigned char default_is_stmt;
48 int line_base;
49 unsigned char line_range;
50 unsigned char opcode_base;
51 unsigned char *standard_opcode_lengths;
52 };
53
54 /* Attributes have a name and a value. */
55
56 struct attribute
57 {
58 enum dwarf_attribute name;
59 enum dwarf_form form;
60 union
61 {
62 char *str;
63 struct dwarf_block *blk;
64 unsigned int unsnd;
65 int snd;
66 bfd_vma addr;
67 }
68 u;
69 };
70
71 /* Get at parts of an attribute structure. */
72
73 #define DW_STRING(attr) ((attr)->u.str)
74 #define DW_UNSND(attr) ((attr)->u.unsnd)
75 #define DW_BLOCK(attr) ((attr)->u.blk)
76 #define DW_SND(attr) ((attr)->u.snd)
77 #define DW_ADDR(attr) ((attr)->u.addr)
78
79 /* Blocks are a bunch of untyped bytes. */
80 struct dwarf_block
81 {
82 unsigned int size;
83 char *data;
84 };
85
86 struct dwarf2_debug
87 {
88 /* A list of all previously read comp_units. */
89 struct comp_unit* all_comp_units;
90
91 /* The next unread compilation unit within the .debug_info section.
92 Zero indicates that the .debug_info section has not been loaded
93 into a buffer yet. */
94 char* info_ptr;
95
96 /* Pointer to the end of the .debug_info section memory buffer. */
97 char* info_ptr_end;
98
99 /* Pointer to the section and address of the beginning of the
100 section. */
101 asection* sec;
102 char* sec_info_ptr;
103
104 /* Pointer to the symbol table. */
105 asymbol** syms;
106
107 /* Pointer to the .debug_abbrev section loaded into memory. */
108 char* dwarf_abbrev_buffer;
109
110 /* Length of the loaded .debug_abbrev section. */
111 unsigned long dwarf_abbrev_size;
112
113 /* Buffer for decode_line_info. */
114 char *dwarf_line_buffer;
115
116 /* Length of the loaded .debug_line section. */
117 unsigned long dwarf_line_size;
118
119 /* Pointer to the .debug_str section loaded into memory. */
120 char* dwarf_str_buffer;
121
122 /* Length of the loaded .debug_str section. */
123 unsigned long dwarf_str_size;
124 };
125
126 struct arange
127 {
128 struct arange *next;
129 bfd_vma low;
130 bfd_vma high;
131 };
132
133 /* A minimal decoding of DWARF2 compilation units. We only decode
134 what's needed to get to the line number information. */
135
136 struct comp_unit
137 {
138 /* Chain the previously read compilation units. */
139 struct comp_unit* next_unit;
140
141 /* Keep the bdf convenient (for memory allocation). */
142 bfd* abfd;
143
144 /* The lowest and higest addresses contained in this compilation
145 unit as specified in the compilation unit header. */
146 struct arange arange;
147
148 /* The DW_AT_name attribute (for error messages). */
149 char* name;
150
151 /* The abbrev hash table. */
152 struct abbrev_info** abbrevs;
153
154 /* Note that an error was found by comp_unit_find_nearest_line. */
155 int error;
156
157 /* The DW_AT_comp_dir attribute. */
158 char* comp_dir;
159
160 /* TRUE if there is a line number table associated with this comp. unit. */
161 int stmtlist;
162
163 /* The offset into .debug_line of the line number table. */
164 unsigned long line_offset;
165
166 /* Pointer to the first child die for the comp unit. */
167 char *first_child_die_ptr;
168
169 /* The end of the comp unit. */
170 char *end_ptr;
171
172 /* The decoded line number, NULL if not yet decoded. */
173 struct line_info_table* line_table;
174
175 /* A list of the functions found in this comp. unit. */
176 struct funcinfo* function_table;
177
178 /* Pointer to dwarf2_debug structure. */
179 struct dwarf2_debug *stash;
180
181 /* Address size for this unit - from unit header. */
182 unsigned char addr_size;
183
184 /* Offset size for this unit - from unit header. */
185 unsigned char offset_size;
186 };
187
188 /* This data structure holds the information of an abbrev. */
189 struct abbrev_info
190 {
191 unsigned int number; /* Number identifying abbrev. */
192 enum dwarf_tag tag; /* DWARF tag. */
193 int has_children; /* Boolean. */
194 unsigned int num_attrs; /* Number of attributes. */
195 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
196 struct abbrev_info *next; /* Next in chain. */
197 };
198
199 struct attr_abbrev
200 {
201 enum dwarf_attribute name;
202 enum dwarf_form form;
203 };
204
205 #ifndef ABBREV_HASH_SIZE
206 #define ABBREV_HASH_SIZE 121
207 #endif
208 #ifndef ATTR_ALLOC_CHUNK
209 #define ATTR_ALLOC_CHUNK 4
210 #endif
211
212 static unsigned int read_1_byte PARAMS ((bfd *, char *));
213 static int read_1_signed_byte PARAMS ((bfd *, char *));
214 static unsigned int read_2_bytes PARAMS ((bfd *, char *));
215 static unsigned int read_4_bytes PARAMS ((bfd *, char *));
216 static bfd_vma read_8_bytes PARAMS ((bfd *, char *));
217 static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int));
218 static char *read_string PARAMS ((bfd *, char *, unsigned int *));
219 static char *read_indirect_string PARAMS ((struct comp_unit *, char *, unsigned int *));
220 static unsigned int read_unsigned_leb128
221 PARAMS ((bfd *, char *, unsigned int *));
222 static int read_signed_leb128
223 PARAMS ((bfd *, char *, unsigned int *));
224 static bfd_vma read_address PARAMS ((struct comp_unit *, char *));
225 static struct abbrev_info *lookup_abbrev
226 PARAMS ((unsigned int, struct abbrev_info **));
227 static struct abbrev_info **read_abbrevs
228 PARAMS ((bfd *, bfd_vma, struct dwarf2_debug *));
229 static char *read_attribute
230 PARAMS ((struct attribute *, struct attr_abbrev *,
231 struct comp_unit *, char *));
232 static char *read_attribute_value
233 PARAMS ((struct attribute *, unsigned,
234 struct comp_unit *, char *));
235 static void add_line_info
236 PARAMS ((struct line_info_table *, bfd_vma, char *,
237 unsigned int, unsigned int, int));
238 static char *concat_filename PARAMS ((struct line_info_table *, unsigned int));
239 static void arange_add PARAMS ((struct comp_unit *, bfd_vma, bfd_vma));
240 static struct line_info_table *decode_line_info
241 PARAMS ((struct comp_unit *, struct dwarf2_debug *));
242 static bfd_boolean lookup_address_in_line_info_table
243 PARAMS ((struct line_info_table *, bfd_vma, struct funcinfo *,
244 const char **, unsigned int *));
245 static bfd_boolean lookup_address_in_function_table
246 PARAMS ((struct funcinfo *, bfd_vma, struct funcinfo **, const char **));
247 static bfd_boolean scan_unit_for_functions PARAMS ((struct comp_unit *));
248 static struct comp_unit *parse_comp_unit
249 PARAMS ((bfd *, struct dwarf2_debug *, bfd_vma, unsigned int));
250 static bfd_boolean comp_unit_contains_address
251 PARAMS ((struct comp_unit *, bfd_vma));
252 static bfd_boolean comp_unit_find_nearest_line
253 PARAMS ((struct comp_unit *, bfd_vma, const char **, const char **,
254 unsigned int *, struct dwarf2_debug *));
255 static asection *find_debug_info PARAMS ((bfd *, asection *));
256
257 /* VERBATIM
258 The following function up to the END VERBATIM mark are
259 copied directly from dwarf2read.c. */
260
261 /* Read dwarf information from a buffer. */
262
263 static unsigned int
264 read_1_byte (abfd, buf)
265 bfd *abfd ATTRIBUTE_UNUSED;
266 char *buf;
267 {
268 return bfd_get_8 (abfd, (bfd_byte *) buf);
269 }
270
271 static int
272 read_1_signed_byte (abfd, buf)
273 bfd *abfd ATTRIBUTE_UNUSED;
274 char *buf;
275 {
276 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
277 }
278
279 static unsigned int
280 read_2_bytes (abfd, buf)
281 bfd *abfd;
282 char *buf;
283 {
284 return bfd_get_16 (abfd, (bfd_byte *) buf);
285 }
286
287 #if 0 /* This is not used. */
288
289 static int
290 read_2_signed_bytes (abfd, buf)
291 bfd *abfd;
292 char *buf;
293 {
294 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
295 }
296
297 #endif
298
299 static unsigned int
300 read_4_bytes (abfd, buf)
301 bfd *abfd;
302 char *buf;
303 {
304 return bfd_get_32 (abfd, (bfd_byte *) buf);
305 }
306
307 #if 0 /* This is not used. */
308
309 static int
310 read_4_signed_bytes (abfd, buf)
311 bfd *abfd;
312 char *buf;
313 {
314 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
315 }
316
317 #endif
318
319 static bfd_vma
320 read_8_bytes (abfd, buf)
321 bfd *abfd;
322 char *buf;
323 {
324 return bfd_get_64 (abfd, (bfd_byte *) buf);
325 }
326
327 static char *
328 read_n_bytes (abfd, buf, size)
329 bfd *abfd ATTRIBUTE_UNUSED;
330 char *buf;
331 unsigned int size ATTRIBUTE_UNUSED;
332 {
333 /* If the size of a host char is 8 bits, we can return a pointer
334 to the buffer, otherwise we have to copy the data to a buffer
335 allocated on the temporary obstack. */
336 return buf;
337 }
338
339 static char *
340 read_string (abfd, buf, bytes_read_ptr)
341 bfd *abfd ATTRIBUTE_UNUSED;
342 char *buf;
343 unsigned int *bytes_read_ptr;
344 {
345 /* Return a pointer to the embedded string. */
346 if (*buf == '\0')
347 {
348 *bytes_read_ptr = 1;
349 return NULL;
350 }
351
352 *bytes_read_ptr = strlen (buf) + 1;
353 return buf;
354 }
355
356 static char *
357 read_indirect_string (unit, buf, bytes_read_ptr)
358 struct comp_unit* unit;
359 char *buf;
360 unsigned int *bytes_read_ptr;
361 {
362 bfd_vma offset;
363 struct dwarf2_debug *stash = unit->stash;
364
365 if (unit->offset_size == 4)
366 offset = read_4_bytes (unit->abfd, buf);
367 else
368 offset = read_8_bytes (unit->abfd, buf);
369 *bytes_read_ptr = unit->offset_size;
370
371 if (! stash->dwarf_str_buffer)
372 {
373 asection *msec;
374 bfd *abfd = unit->abfd;
375
376 msec = bfd_get_section_by_name (abfd, ".debug_str");
377 if (! msec)
378 {
379 (*_bfd_error_handler)
380 (_("Dwarf Error: Can't find .debug_str section."));
381 bfd_set_error (bfd_error_bad_value);
382 return NULL;
383 }
384
385 stash->dwarf_str_size = msec->_raw_size;
386 stash->dwarf_str_buffer = (char*) bfd_alloc (abfd, msec->_raw_size);
387 if (! stash->dwarf_abbrev_buffer)
388 return NULL;
389
390 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
391 (bfd_vma) 0, msec->_raw_size))
392 return NULL;
393 }
394
395 if (offset >= stash->dwarf_str_size)
396 {
397 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
398 (unsigned long) offset, stash->dwarf_str_size);
399 bfd_set_error (bfd_error_bad_value);
400 return NULL;
401 }
402
403 buf = stash->dwarf_str_buffer + offset;
404 if (*buf == '\0')
405 return NULL;
406 return buf;
407 }
408
409 static unsigned int
410 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
411 bfd *abfd ATTRIBUTE_UNUSED;
412 char *buf;
413 unsigned int *bytes_read_ptr;
414 {
415 unsigned int result;
416 unsigned int num_read;
417 int shift;
418 unsigned char byte;
419
420 result = 0;
421 shift = 0;
422 num_read = 0;
423
424 do
425 {
426 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
427 buf ++;
428 num_read ++;
429 result |= ((byte & 0x7f) << shift);
430 shift += 7;
431 }
432 while (byte & 0x80);
433
434 * bytes_read_ptr = num_read;
435
436 return result;
437 }
438
439 static int
440 read_signed_leb128 (abfd, buf, bytes_read_ptr)
441 bfd *abfd ATTRIBUTE_UNUSED;
442 char *buf;
443 unsigned int * bytes_read_ptr;
444 {
445 int result;
446 int shift;
447 int num_read;
448 unsigned char byte;
449
450 result = 0;
451 shift = 0;
452 num_read = 0;
453
454 do
455 {
456 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
457 buf ++;
458 num_read ++;
459 result |= ((byte & 0x7f) << shift);
460 shift += 7;
461 }
462 while (byte & 0x80);
463
464 if ((shift < 32) && (byte & 0x40))
465 result |= -(1 << shift);
466
467 * bytes_read_ptr = num_read;
468
469 return result;
470 }
471
472 /* END VERBATIM */
473
474 static bfd_vma
475 read_address (unit, buf)
476 struct comp_unit* unit;
477 char *buf;
478 {
479 switch (unit->addr_size)
480 {
481 case 8:
482 return bfd_get_64 (unit->abfd, (bfd_byte *) buf);
483 case 4:
484 return bfd_get_32 (unit->abfd, (bfd_byte *) buf);
485 case 2:
486 return bfd_get_16 (unit->abfd, (bfd_byte *) buf);
487 default:
488 abort ();
489 }
490 }
491
492 /* Lookup an abbrev_info structure in the abbrev hash table. */
493
494 static struct abbrev_info *
495 lookup_abbrev (number,abbrevs)
496 unsigned int number;
497 struct abbrev_info **abbrevs;
498 {
499 unsigned int hash_number;
500 struct abbrev_info *abbrev;
501
502 hash_number = number % ABBREV_HASH_SIZE;
503 abbrev = abbrevs[hash_number];
504
505 while (abbrev)
506 {
507 if (abbrev->number == number)
508 return abbrev;
509 else
510 abbrev = abbrev->next;
511 }
512
513 return NULL;
514 }
515
516 /* In DWARF version 2, the description of the debugging information is
517 stored in a separate .debug_abbrev section. Before we read any
518 dies from a section we read in all abbreviations and install them
519 in a hash table. */
520
521 static struct abbrev_info**
522 read_abbrevs (abfd, offset, stash)
523 bfd * abfd;
524 bfd_vma offset;
525 struct dwarf2_debug *stash;
526 {
527 struct abbrev_info **abbrevs;
528 char *abbrev_ptr;
529 struct abbrev_info *cur_abbrev;
530 unsigned int abbrev_number, bytes_read, abbrev_name;
531 unsigned int abbrev_form, hash_number;
532 bfd_size_type amt;
533
534 if (! stash->dwarf_abbrev_buffer)
535 {
536 asection *msec;
537
538 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
539 if (! msec)
540 {
541 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
542 bfd_set_error (bfd_error_bad_value);
543 return 0;
544 }
545
546 stash->dwarf_abbrev_size = msec->_raw_size;
547 stash->dwarf_abbrev_buffer
548 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
549 stash->syms);
550 if (! stash->dwarf_abbrev_buffer)
551 return 0;
552 }
553
554 if (offset >= stash->dwarf_abbrev_size)
555 {
556 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
557 (unsigned long) offset, stash->dwarf_abbrev_size);
558 bfd_set_error (bfd_error_bad_value);
559 return 0;
560 }
561
562 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
563 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, amt);
564
565 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
566 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
567 abbrev_ptr += bytes_read;
568
569 /* Loop until we reach an abbrev number of 0. */
570 while (abbrev_number)
571 {
572 amt = sizeof (struct abbrev_info);
573 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
574
575 /* Read in abbrev header. */
576 cur_abbrev->number = abbrev_number;
577 cur_abbrev->tag = (enum dwarf_tag)
578 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
579 abbrev_ptr += bytes_read;
580 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
581 abbrev_ptr += 1;
582
583 /* Now read in declarations. */
584 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
585 abbrev_ptr += bytes_read;
586 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
587 abbrev_ptr += bytes_read;
588
589 while (abbrev_name)
590 {
591 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
592 {
593 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
594 amt *= sizeof (struct attr_abbrev);
595 cur_abbrev->attrs = ((struct attr_abbrev *)
596 bfd_realloc (cur_abbrev->attrs, amt));
597 if (! cur_abbrev->attrs)
598 return 0;
599 }
600
601 cur_abbrev->attrs[cur_abbrev->num_attrs].name
602 = (enum dwarf_attribute) abbrev_name;
603 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
604 = (enum dwarf_form) abbrev_form;
605 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
606 abbrev_ptr += bytes_read;
607 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
608 abbrev_ptr += bytes_read;
609 }
610
611 hash_number = abbrev_number % ABBREV_HASH_SIZE;
612 cur_abbrev->next = abbrevs[hash_number];
613 abbrevs[hash_number] = cur_abbrev;
614
615 /* Get next abbreviation.
616 Under Irix6 the abbreviations for a compilation unit are not
617 always properly terminated with an abbrev number of 0.
618 Exit loop if we encounter an abbreviation which we have
619 already read (which means we are about to read the abbreviations
620 for the next compile unit) or if the end of the abbreviation
621 table is reached. */
622 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
623 >= stash->dwarf_abbrev_size)
624 break;
625 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
626 abbrev_ptr += bytes_read;
627 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
628 break;
629 }
630
631 return abbrevs;
632 }
633
634 /* Read an attribute value described by an attribute form. */
635
636 static char *
637 read_attribute_value (attr, form, unit, info_ptr)
638 struct attribute *attr;
639 unsigned form;
640 struct comp_unit *unit;
641 char *info_ptr;
642 {
643 bfd *abfd = unit->abfd;
644 unsigned int bytes_read;
645 struct dwarf_block *blk;
646 bfd_size_type amt;
647
648 attr->form = (enum dwarf_form) form;
649
650 switch (form)
651 {
652 case DW_FORM_addr:
653 /* FIXME: DWARF3 draft sais DW_FORM_ref_addr is offset_size. */
654 case DW_FORM_ref_addr:
655 DW_ADDR (attr) = read_address (unit, info_ptr);
656 info_ptr += unit->addr_size;
657 break;
658 case DW_FORM_block2:
659 amt = sizeof (struct dwarf_block);
660 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
661 blk->size = read_2_bytes (abfd, info_ptr);
662 info_ptr += 2;
663 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
664 info_ptr += blk->size;
665 DW_BLOCK (attr) = blk;
666 break;
667 case DW_FORM_block4:
668 amt = sizeof (struct dwarf_block);
669 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
670 blk->size = read_4_bytes (abfd, info_ptr);
671 info_ptr += 4;
672 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
673 info_ptr += blk->size;
674 DW_BLOCK (attr) = blk;
675 break;
676 case DW_FORM_data2:
677 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
678 info_ptr += 2;
679 break;
680 case DW_FORM_data4:
681 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
682 info_ptr += 4;
683 break;
684 case DW_FORM_data8:
685 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
686 info_ptr += 8;
687 break;
688 case DW_FORM_string:
689 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
690 info_ptr += bytes_read;
691 break;
692 case DW_FORM_strp:
693 DW_STRING (attr) = read_indirect_string (unit, info_ptr, &bytes_read);
694 info_ptr += bytes_read;
695 break;
696 case DW_FORM_block:
697 amt = sizeof (struct dwarf_block);
698 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
699 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
700 info_ptr += bytes_read;
701 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
702 info_ptr += blk->size;
703 DW_BLOCK (attr) = blk;
704 break;
705 case DW_FORM_block1:
706 amt = sizeof (struct dwarf_block);
707 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
708 blk->size = read_1_byte (abfd, info_ptr);
709 info_ptr += 1;
710 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
711 info_ptr += blk->size;
712 DW_BLOCK (attr) = blk;
713 break;
714 case DW_FORM_data1:
715 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
716 info_ptr += 1;
717 break;
718 case DW_FORM_flag:
719 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
720 info_ptr += 1;
721 break;
722 case DW_FORM_sdata:
723 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
724 info_ptr += bytes_read;
725 break;
726 case DW_FORM_udata:
727 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
728 info_ptr += bytes_read;
729 break;
730 case DW_FORM_ref1:
731 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
732 info_ptr += 1;
733 break;
734 case DW_FORM_ref2:
735 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
736 info_ptr += 2;
737 break;
738 case DW_FORM_ref4:
739 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
740 info_ptr += 4;
741 break;
742 case DW_FORM_ref8:
743 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
744 info_ptr += 8;
745 break;
746 case DW_FORM_ref_udata:
747 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
748 info_ptr += bytes_read;
749 break;
750 case DW_FORM_indirect:
751 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
752 info_ptr += bytes_read;
753 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
754 break;
755 default:
756 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
757 form);
758 bfd_set_error (bfd_error_bad_value);
759 }
760 return info_ptr;
761 }
762
763 /* Read an attribute described by an abbreviated attribute. */
764
765 static char *
766 read_attribute (attr, abbrev, unit, info_ptr)
767 struct attribute *attr;
768 struct attr_abbrev *abbrev;
769 struct comp_unit *unit;
770 char *info_ptr;
771 {
772 attr->name = abbrev->name;
773 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
774 return info_ptr;
775 }
776
777 /* Source line information table routines. */
778
779 #define FILE_ALLOC_CHUNK 5
780 #define DIR_ALLOC_CHUNK 5
781
782 struct line_info
783 {
784 struct line_info* prev_line;
785 bfd_vma address;
786 char* filename;
787 unsigned int line;
788 unsigned int column;
789 int end_sequence; /* End of (sequential) code sequence. */
790 };
791
792 struct fileinfo
793 {
794 char *name;
795 unsigned int dir;
796 unsigned int time;
797 unsigned int size;
798 };
799
800 struct line_info_table
801 {
802 bfd* abfd;
803 unsigned int num_files;
804 unsigned int num_dirs;
805 char* comp_dir;
806 char** dirs;
807 struct fileinfo* files;
808 struct line_info* last_line; /* largest VMA */
809 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
810 };
811
812 struct funcinfo
813 {
814 struct funcinfo *prev_func;
815 char* name;
816 bfd_vma low;
817 bfd_vma high;
818 };
819
820 /* Adds a new entry to the line_info list in the line_info_table, ensuring
821 that the list is sorted. Note that the line_info list is sorted from
822 highest to lowest VMA (with possible duplicates); that is,
823 line_info->prev_line always accesses an equal or smaller VMA. */
824
825 static void
826 add_line_info (table, address, filename, line, column, end_sequence)
827 struct line_info_table* table;
828 bfd_vma address;
829 char* filename;
830 unsigned int line;
831 unsigned int column;
832 int end_sequence;
833 {
834 bfd_size_type amt = sizeof (struct line_info);
835 struct line_info* info = (struct line_info*) bfd_alloc (table->abfd, amt);
836
837 /* Find the correct location for 'info'. Normally we will receive
838 new line_info data 1) in order and 2) with increasing VMAs.
839 However some compilers break the rules (cf. decode_line_info) and
840 so we include some heuristics for quickly finding the correct
841 location for 'info'. In particular, these heuristics optimize for
842 the common case in which the VMA sequence that we receive is a
843 list of locally sorted VMAs such as
844 p...z a...j (where a < j < p < z)
845
846 Note: table->lcl_head is used to head an *actual* or *possible*
847 sequence within the list (such as a...j) that is not directly
848 headed by table->last_line
849
850 Note: we may receive duplicate entries from 'decode_line_info'. */
851
852 while (1)
853 if (!table->last_line
854 || address >= table->last_line->address)
855 {
856 /* Normal case: add 'info' to the beginning of the list */
857 info->prev_line = table->last_line;
858 table->last_line = info;
859
860 /* lcl_head: initialize to head a *possible* sequence at the end. */
861 if (!table->lcl_head)
862 table->lcl_head = info;
863 break;
864 }
865 else if (!table->lcl_head->prev_line
866 && table->lcl_head->address > address)
867 {
868 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
869 list and 2) the head of 'info'. */
870 info->prev_line = NULL;
871 table->lcl_head->prev_line = info;
872 break;
873 }
874 else if (table->lcl_head->prev_line
875 && table->lcl_head->address > address
876 && address >= table->lcl_head->prev_line->address)
877 {
878 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
879 list and 2) the head of 'info'. */
880 info->prev_line = table->lcl_head->prev_line;
881 table->lcl_head->prev_line = info;
882 break;
883 }
884 else
885 {
886 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
887 heads for 'info'. Reset 'lcl_head' and repeat. */
888 struct line_info* li2 = table->last_line; /* always non-NULL */
889 struct line_info* li1 = li2->prev_line;
890
891 while (li1)
892 {
893 if (li2->address > address && address >= li1->address)
894 break;
895
896 li2 = li1; /* always non-NULL */
897 li1 = li1->prev_line;
898 }
899 table->lcl_head = li2;
900 }
901
902 /* Set member data of 'info'. */
903 info->address = address;
904 info->filename = filename;
905 info->line = line;
906 info->column = column;
907 info->end_sequence = end_sequence;
908 }
909
910 /* Extract a fully qualified filename from a line info table.
911 The returned string has been malloc'ed and it is the caller's
912 responsibility to free it. */
913
914 static char *
915 concat_filename (table, file)
916 struct line_info_table* table;
917 unsigned int file;
918 {
919 char* filename;
920
921 if (file - 1 >= table->num_files)
922 {
923 (*_bfd_error_handler)
924 (_("Dwarf Error: mangled line number section (bad file number)."));
925 return strdup ("<unknown>");
926 }
927
928 filename = table->files[file - 1].name;
929
930 if (! IS_ABSOLUTE_PATH (filename))
931 {
932 char* dirname = (table->files[file - 1].dir
933 ? table->dirs[table->files[file - 1].dir - 1]
934 : table->comp_dir);
935
936 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
937 The best we can do is return the filename part. */
938 if (dirname != NULL)
939 {
940 unsigned int len = strlen (dirname) + strlen (filename) + 2;
941 char * name;
942
943 name = bfd_malloc (len);
944 if (name)
945 sprintf (name, "%s/%s", dirname, filename);
946 return name;
947 }
948 }
949
950 return strdup (filename);
951 }
952
953 static void
954 arange_add (unit, low_pc, high_pc)
955 struct comp_unit *unit;
956 bfd_vma low_pc;
957 bfd_vma high_pc;
958 {
959 struct arange *arange;
960
961 /* First see if we can cheaply extend an existing range. */
962 arange = &unit->arange;
963
964 do
965 {
966 if (low_pc == arange->high)
967 {
968 arange->high = high_pc;
969 return;
970 }
971 if (high_pc == arange->low)
972 {
973 arange->low = low_pc;
974 return;
975 }
976 arange = arange->next;
977 }
978 while (arange);
979
980 if (unit->arange.high == 0)
981 {
982 /* This is the first address range: store it in unit->arange. */
983 unit->arange.next = 0;
984 unit->arange.low = low_pc;
985 unit->arange.high = high_pc;
986 return;
987 }
988
989 /* Need to allocate a new arange and insert it into the arange list. */
990 arange = (struct arange *)
991 bfd_zalloc (unit->abfd, (bfd_size_type) sizeof (*arange));
992 arange->low = low_pc;
993 arange->high = high_pc;
994
995 arange->next = unit->arange.next;
996 unit->arange.next = arange;
997 }
998
999 /* Decode the line number information for UNIT. */
1000
1001 static struct line_info_table*
1002 decode_line_info (unit, stash)
1003 struct comp_unit *unit;
1004 struct dwarf2_debug *stash;
1005 {
1006 bfd *abfd = unit->abfd;
1007 struct line_info_table* table;
1008 char *line_ptr;
1009 char *line_end;
1010 struct line_head lh;
1011 unsigned int i, bytes_read, offset_size;
1012 char *cur_file, *cur_dir;
1013 unsigned char op_code, extended_op, adj_opcode;
1014 bfd_size_type amt;
1015
1016 if (! stash->dwarf_line_buffer)
1017 {
1018 asection *msec;
1019
1020 msec = bfd_get_section_by_name (abfd, ".debug_line");
1021 if (! msec)
1022 {
1023 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
1024 bfd_set_error (bfd_error_bad_value);
1025 return 0;
1026 }
1027
1028 stash->dwarf_line_size = msec->_raw_size;
1029 stash->dwarf_line_buffer
1030 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1031 stash->syms);
1032 if (! stash->dwarf_line_buffer)
1033 return 0;
1034 }
1035
1036 /* It is possible to get a bad value for the line_offset. Validate
1037 it here so that we won't get a segfault below. */
1038 if (unit->line_offset >= stash->dwarf_line_size)
1039 {
1040 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
1041 unit->line_offset, stash->dwarf_line_size);
1042 bfd_set_error (bfd_error_bad_value);
1043 return 0;
1044 }
1045
1046 amt = sizeof (struct line_info_table);
1047 table = (struct line_info_table*) bfd_alloc (abfd, amt);
1048 table->abfd = abfd;
1049 table->comp_dir = unit->comp_dir;
1050
1051 table->num_files = 0;
1052 table->files = NULL;
1053
1054 table->num_dirs = 0;
1055 table->dirs = NULL;
1056
1057 table->files = NULL;
1058 table->last_line = NULL;
1059 table->lcl_head = NULL;
1060
1061 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1062
1063 /* Read in the prologue. */
1064 lh.total_length = read_4_bytes (abfd, line_ptr);
1065 line_ptr += 4;
1066 offset_size = 4;
1067 if (lh.total_length == 0xffffffff)
1068 {
1069 lh.total_length = read_8_bytes (abfd, line_ptr);
1070 line_ptr += 8;
1071 offset_size = 8;
1072 }
1073 else if (lh.total_length == 0 && unit->addr_size == 8)
1074 {
1075 /* Handle (non-standard) 64-bit DWARF2 formats. */
1076 lh.total_length = read_4_bytes (abfd, line_ptr);
1077 line_ptr += 4;
1078 offset_size = 8;
1079 }
1080 line_end = line_ptr + lh.total_length;
1081 lh.version = read_2_bytes (abfd, line_ptr);
1082 line_ptr += 2;
1083 if (offset_size == 4)
1084 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1085 else
1086 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1087 line_ptr += offset_size;
1088 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1089 line_ptr += 1;
1090 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1091 line_ptr += 1;
1092 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1093 line_ptr += 1;
1094 lh.line_range = read_1_byte (abfd, line_ptr);
1095 line_ptr += 1;
1096 lh.opcode_base = read_1_byte (abfd, line_ptr);
1097 line_ptr += 1;
1098 amt = lh.opcode_base * sizeof (unsigned char);
1099 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1100
1101 lh.standard_opcode_lengths[0] = 1;
1102
1103 for (i = 1; i < lh.opcode_base; ++i)
1104 {
1105 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1106 line_ptr += 1;
1107 }
1108
1109 /* Read directory table. */
1110 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1111 {
1112 line_ptr += bytes_read;
1113
1114 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1115 {
1116 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1117 amt *= sizeof (char *);
1118 table->dirs = (char **) bfd_realloc (table->dirs, amt);
1119 if (! table->dirs)
1120 return 0;
1121 }
1122
1123 table->dirs[table->num_dirs++] = cur_dir;
1124 }
1125
1126 line_ptr += bytes_read;
1127
1128 /* Read file name table. */
1129 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1130 {
1131 line_ptr += bytes_read;
1132
1133 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1134 {
1135 amt = table->num_files + FILE_ALLOC_CHUNK;
1136 amt *= sizeof (struct fileinfo);
1137 table->files = (struct fileinfo *) bfd_realloc (table->files, amt);
1138 if (! table->files)
1139 return 0;
1140 }
1141
1142 table->files[table->num_files].name = cur_file;
1143 table->files[table->num_files].dir =
1144 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1145 line_ptr += bytes_read;
1146 table->files[table->num_files].time =
1147 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1148 line_ptr += bytes_read;
1149 table->files[table->num_files].size =
1150 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1151 line_ptr += bytes_read;
1152 table->num_files++;
1153 }
1154
1155 line_ptr += bytes_read;
1156
1157 /* Read the statement sequences until there's nothing left. */
1158 while (line_ptr < line_end)
1159 {
1160 /* State machine registers. */
1161 bfd_vma address = 0;
1162 char * filename = concat_filename (table, 1);
1163 unsigned int line = 1;
1164 unsigned int column = 0;
1165 int is_stmt = lh.default_is_stmt;
1166 int basic_block = 0;
1167 int end_sequence = 0;
1168 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1169 compilers generate address sequences that are wildly out of
1170 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1171 for ia64-Linux). Thus, to determine the low and high
1172 address, we must compare on every DW_LNS_copy, etc. */
1173 bfd_vma low_pc = 0;
1174 bfd_vma high_pc = 0;
1175
1176 /* Decode the table. */
1177 while (! end_sequence)
1178 {
1179 op_code = read_1_byte (abfd, line_ptr);
1180 line_ptr += 1;
1181
1182 if (op_code >= lh.opcode_base)
1183 {
1184 /* Special operand. */
1185 adj_opcode = op_code - lh.opcode_base;
1186 address += (adj_opcode / lh.line_range)
1187 * lh.minimum_instruction_length;
1188 line += lh.line_base + (adj_opcode % lh.line_range);
1189 /* Append row to matrix using current values. */
1190 add_line_info (table, address, filename, line, column, 0);
1191 basic_block = 1;
1192 if (low_pc == 0 || address < low_pc)
1193 low_pc = address;
1194 if (address > high_pc)
1195 high_pc = address;
1196 }
1197 else switch (op_code)
1198 {
1199 case DW_LNS_extended_op:
1200 /* Ignore length. */
1201 line_ptr += 1;
1202 extended_op = read_1_byte (abfd, line_ptr);
1203 line_ptr += 1;
1204
1205 switch (extended_op)
1206 {
1207 case DW_LNE_end_sequence:
1208 end_sequence = 1;
1209 add_line_info (table, address, filename, line, column,
1210 end_sequence);
1211 if (low_pc == 0 || address < low_pc)
1212 low_pc = address;
1213 if (address > high_pc)
1214 high_pc = address;
1215 arange_add (unit, low_pc, high_pc);
1216 break;
1217 case DW_LNE_set_address:
1218 address = read_address (unit, line_ptr);
1219 line_ptr += unit->addr_size;
1220 break;
1221 case DW_LNE_define_file:
1222 cur_file = read_string (abfd, line_ptr, &bytes_read);
1223 line_ptr += bytes_read;
1224 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1225 {
1226 amt = table->num_files + FILE_ALLOC_CHUNK;
1227 amt *= sizeof (struct fileinfo);
1228 table->files =
1229 (struct fileinfo *) bfd_realloc (table->files, amt);
1230 if (! table->files)
1231 return 0;
1232 }
1233 table->files[table->num_files].name = cur_file;
1234 table->files[table->num_files].dir =
1235 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1236 line_ptr += bytes_read;
1237 table->files[table->num_files].time =
1238 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1239 line_ptr += bytes_read;
1240 table->files[table->num_files].size =
1241 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1242 line_ptr += bytes_read;
1243 table->num_files++;
1244 break;
1245 default:
1246 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1247 bfd_set_error (bfd_error_bad_value);
1248 return 0;
1249 }
1250 break;
1251 case DW_LNS_copy:
1252 add_line_info (table, address, filename, line, column, 0);
1253 basic_block = 0;
1254 if (low_pc == 0 || address < low_pc)
1255 low_pc = address;
1256 if (address > high_pc)
1257 high_pc = address;
1258 break;
1259 case DW_LNS_advance_pc:
1260 address += lh.minimum_instruction_length
1261 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1262 line_ptr += bytes_read;
1263 break;
1264 case DW_LNS_advance_line:
1265 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1266 line_ptr += bytes_read;
1267 break;
1268 case DW_LNS_set_file:
1269 {
1270 unsigned int file;
1271
1272 /* The file and directory tables are 0
1273 based, the references are 1 based. */
1274 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1275 line_ptr += bytes_read;
1276 if (filename)
1277 free (filename);
1278 filename = concat_filename (table, file);
1279 break;
1280 }
1281 case DW_LNS_set_column:
1282 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1283 line_ptr += bytes_read;
1284 break;
1285 case DW_LNS_negate_stmt:
1286 is_stmt = (!is_stmt);
1287 break;
1288 case DW_LNS_set_basic_block:
1289 basic_block = 1;
1290 break;
1291 case DW_LNS_const_add_pc:
1292 address += lh.minimum_instruction_length
1293 * ((255 - lh.opcode_base) / lh.line_range);
1294 break;
1295 case DW_LNS_fixed_advance_pc:
1296 address += read_2_bytes (abfd, line_ptr);
1297 line_ptr += 2;
1298 break;
1299 default:
1300 {
1301 int i;
1302
1303 /* Unknown standard opcode, ignore it. */
1304 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1305 {
1306 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1307 line_ptr += bytes_read;
1308 }
1309 }
1310 }
1311 }
1312
1313 if (filename)
1314 free (filename);
1315 }
1316
1317 return table;
1318 }
1319
1320 /* If ADDR is within TABLE set the output parameters and return TRUE,
1321 otherwise return FALSE. The output parameters, FILENAME_PTR and
1322 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1323
1324 static bfd_boolean
1325 lookup_address_in_line_info_table (table, addr, function, filename_ptr,
1326 linenumber_ptr)
1327 struct line_info_table* table;
1328 bfd_vma addr;
1329 struct funcinfo *function;
1330 const char **filename_ptr;
1331 unsigned int *linenumber_ptr;
1332 {
1333 /* Note: table->last_line should be a descendingly sorted list. */
1334 struct line_info* next_line = table->last_line;
1335 struct line_info* each_line = NULL;
1336 *filename_ptr = NULL;
1337
1338 if (!next_line)
1339 return FALSE;
1340
1341 each_line = next_line->prev_line;
1342
1343 /* Check for large addresses */
1344 if (addr > next_line->address)
1345 each_line = NULL; /* ensure we skip over the normal case */
1346
1347 /* Normal case: search the list; save */
1348 while (each_line && next_line)
1349 {
1350 /* If we have an address match, save this info. This allows us
1351 to return as good as results as possible for strange debugging
1352 info. */
1353 bfd_boolean addr_match = FALSE;
1354 if (each_line->address <= addr && addr <= next_line->address)
1355 {
1356 addr_match = TRUE;
1357
1358 /* If this line appears to span functions, and addr is in the
1359 later function, return the first line of that function instead
1360 of the last line of the earlier one. This check is for GCC
1361 2.95, which emits the first line number for a function late. */
1362 if (function != NULL
1363 && each_line->address < function->low
1364 && next_line->address > function->low)
1365 {
1366 *filename_ptr = next_line->filename;
1367 *linenumber_ptr = next_line->line;
1368 }
1369 else
1370 {
1371 *filename_ptr = each_line->filename;
1372 *linenumber_ptr = each_line->line;
1373 }
1374 }
1375
1376 if (addr_match && !each_line->end_sequence)
1377 return TRUE; /* we have definitely found what we want */
1378
1379 next_line = each_line;
1380 each_line = each_line->prev_line;
1381 }
1382
1383 /* At this point each_line is NULL but next_line is not. If we found
1384 a candidate end-of-sequence point in the loop above, we can return
1385 that (compatibility with a bug in the Intel compiler); otherwise,
1386 assuming that we found the containing function for this address in
1387 this compilation unit, return the first line we have a number for
1388 (compatibility with GCC 2.95). */
1389 if (*filename_ptr == NULL && function != NULL)
1390 {
1391 *filename_ptr = next_line->filename;
1392 *linenumber_ptr = next_line->line;
1393 return TRUE;
1394 }
1395
1396 return FALSE;
1397 }
1398
1399 /* Function table functions. */
1400
1401 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1402
1403 static bfd_boolean
1404 lookup_address_in_function_table (table, addr, function_ptr,
1405 functionname_ptr)
1406 struct funcinfo* table;
1407 bfd_vma addr;
1408 struct funcinfo** function_ptr;
1409 const char **functionname_ptr;
1410 {
1411 struct funcinfo* each_func;
1412
1413 for (each_func = table;
1414 each_func;
1415 each_func = each_func->prev_func)
1416 {
1417 if (addr >= each_func->low && addr < each_func->high)
1418 {
1419 *functionname_ptr = each_func->name;
1420 *function_ptr = each_func;
1421 return TRUE;
1422 }
1423 }
1424
1425 return FALSE;
1426 }
1427
1428 /* DWARF2 Compilation unit functions. */
1429
1430 /* Scan over each die in a comp. unit looking for functions to add
1431 to the function table. */
1432
1433 static bfd_boolean
1434 scan_unit_for_functions (unit)
1435 struct comp_unit *unit;
1436 {
1437 bfd *abfd = unit->abfd;
1438 char *info_ptr = unit->first_child_die_ptr;
1439 int nesting_level = 1;
1440
1441 while (nesting_level)
1442 {
1443 unsigned int abbrev_number, bytes_read, i;
1444 struct abbrev_info *abbrev;
1445 struct attribute attr;
1446 struct funcinfo *func;
1447 char* name = 0;
1448
1449 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1450 info_ptr += bytes_read;
1451
1452 if (! abbrev_number)
1453 {
1454 nesting_level--;
1455 continue;
1456 }
1457
1458 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1459 if (! abbrev)
1460 {
1461 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1462 abbrev_number);
1463 bfd_set_error (bfd_error_bad_value);
1464 return FALSE;
1465 }
1466
1467 if (abbrev->tag == DW_TAG_subprogram)
1468 {
1469 bfd_size_type amt = sizeof (struct funcinfo);
1470 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
1471 func->prev_func = unit->function_table;
1472 unit->function_table = func;
1473 }
1474 else
1475 func = NULL;
1476
1477 for (i = 0; i < abbrev->num_attrs; ++i)
1478 {
1479 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1480
1481 if (func)
1482 {
1483 switch (attr.name)
1484 {
1485 case DW_AT_name:
1486
1487 name = DW_STRING (&attr);
1488
1489 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1490 if (func->name == NULL)
1491 func->name = DW_STRING (&attr);
1492 break;
1493
1494 case DW_AT_MIPS_linkage_name:
1495 func->name = DW_STRING (&attr);
1496 break;
1497
1498 case DW_AT_low_pc:
1499 func->low = DW_ADDR (&attr);
1500 break;
1501
1502 case DW_AT_high_pc:
1503 func->high = DW_ADDR (&attr);
1504 break;
1505
1506 default:
1507 break;
1508 }
1509 }
1510 else
1511 {
1512 switch (attr.name)
1513 {
1514 case DW_AT_name:
1515 name = DW_STRING (&attr);
1516 break;
1517
1518 default:
1519 break;
1520 }
1521 }
1522 }
1523
1524 if (abbrev->has_children)
1525 nesting_level++;
1526 }
1527
1528 return TRUE;
1529 }
1530
1531 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1532 includes the compilation unit header that proceeds the DIE's, but
1533 does not include the length field that preceeds each compilation
1534 unit header. END_PTR points one past the end of this comp unit.
1535 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1536
1537 This routine does not read the whole compilation unit; only enough
1538 to get to the line number information for the compilation unit. */
1539
1540 static struct comp_unit *
1541 parse_comp_unit (abfd, stash, unit_length, offset_size)
1542 bfd* abfd;
1543 struct dwarf2_debug *stash;
1544 bfd_vma unit_length;
1545 unsigned int offset_size;
1546 {
1547 struct comp_unit* unit;
1548 unsigned int version;
1549 bfd_vma abbrev_offset = 0;
1550 unsigned int addr_size;
1551 struct abbrev_info** abbrevs;
1552 unsigned int abbrev_number, bytes_read, i;
1553 struct abbrev_info *abbrev;
1554 struct attribute attr;
1555 char *info_ptr = stash->info_ptr;
1556 char *end_ptr = info_ptr + unit_length;
1557 bfd_size_type amt;
1558
1559 version = read_2_bytes (abfd, info_ptr);
1560 info_ptr += 2;
1561 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1562 if (offset_size == 4)
1563 abbrev_offset = read_4_bytes (abfd, info_ptr);
1564 else
1565 abbrev_offset = read_8_bytes (abfd, info_ptr);
1566 info_ptr += offset_size;
1567 addr_size = read_1_byte (abfd, info_ptr);
1568 info_ptr += 1;
1569
1570 if (version != 2)
1571 {
1572 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1573 bfd_set_error (bfd_error_bad_value);
1574 return 0;
1575 }
1576
1577 if (addr_size > sizeof (bfd_vma))
1578 {
1579 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1580 addr_size,
1581 (unsigned int) sizeof (bfd_vma));
1582 bfd_set_error (bfd_error_bad_value);
1583 return 0;
1584 }
1585
1586 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1587 {
1588 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1589 bfd_set_error (bfd_error_bad_value);
1590 return 0;
1591 }
1592
1593 /* Read the abbrevs for this compilation unit into a table. */
1594 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1595 if (! abbrevs)
1596 return 0;
1597
1598 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1599 info_ptr += bytes_read;
1600 if (! abbrev_number)
1601 {
1602 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1603 abbrev_number);
1604 bfd_set_error (bfd_error_bad_value);
1605 return 0;
1606 }
1607
1608 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1609 if (! abbrev)
1610 {
1611 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1612 abbrev_number);
1613 bfd_set_error (bfd_error_bad_value);
1614 return 0;
1615 }
1616
1617 amt = sizeof (struct comp_unit);
1618 unit = (struct comp_unit*) bfd_zalloc (abfd, amt);
1619 unit->abfd = abfd;
1620 unit->addr_size = addr_size;
1621 unit->offset_size = offset_size;
1622 unit->abbrevs = abbrevs;
1623 unit->end_ptr = end_ptr;
1624 unit->stash = stash;
1625
1626 for (i = 0; i < abbrev->num_attrs; ++i)
1627 {
1628 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1629
1630 /* Store the data if it is of an attribute we want to keep in a
1631 partial symbol table. */
1632 switch (attr.name)
1633 {
1634 case DW_AT_stmt_list:
1635 unit->stmtlist = 1;
1636 unit->line_offset = DW_UNSND (&attr);
1637 break;
1638
1639 case DW_AT_name:
1640 unit->name = DW_STRING (&attr);
1641 break;
1642
1643 case DW_AT_low_pc:
1644 unit->arange.low = DW_ADDR (&attr);
1645 break;
1646
1647 case DW_AT_high_pc:
1648 unit->arange.high = DW_ADDR (&attr);
1649 break;
1650
1651 case DW_AT_comp_dir:
1652 {
1653 char* comp_dir = DW_STRING (&attr);
1654 if (comp_dir)
1655 {
1656 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1657 directory, get rid of it. */
1658 char *cp = (char*) strchr (comp_dir, ':');
1659
1660 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1661 comp_dir = cp + 1;
1662 }
1663 unit->comp_dir = comp_dir;
1664 break;
1665 }
1666
1667 default:
1668 break;
1669 }
1670 }
1671
1672 unit->first_child_die_ptr = info_ptr;
1673 return unit;
1674 }
1675
1676 /* Return TRUE if UNIT contains the address given by ADDR. */
1677
1678 static bfd_boolean
1679 comp_unit_contains_address (unit, addr)
1680 struct comp_unit* unit;
1681 bfd_vma addr;
1682 {
1683 struct arange *arange;
1684
1685 if (unit->error)
1686 return FALSE;
1687
1688 arange = &unit->arange;
1689 do
1690 {
1691 if (addr >= arange->low && addr < arange->high)
1692 return TRUE;
1693 arange = arange->next;
1694 }
1695 while (arange);
1696
1697 return FALSE;
1698 }
1699
1700 /* If UNIT contains ADDR, set the output parameters to the values for
1701 the line containing ADDR. The output parameters, FILENAME_PTR,
1702 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1703 to be filled in.
1704
1705 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1706 FALSE otherwise. */
1707
1708 static bfd_boolean
1709 comp_unit_find_nearest_line (unit, addr, filename_ptr, functionname_ptr,
1710 linenumber_ptr, stash)
1711 struct comp_unit* unit;
1712 bfd_vma addr;
1713 const char **filename_ptr;
1714 const char **functionname_ptr;
1715 unsigned int *linenumber_ptr;
1716 struct dwarf2_debug *stash;
1717 {
1718 bfd_boolean line_p;
1719 bfd_boolean func_p;
1720 struct funcinfo *function;
1721
1722 if (unit->error)
1723 return FALSE;
1724
1725 if (! unit->line_table)
1726 {
1727 if (! unit->stmtlist)
1728 {
1729 unit->error = 1;
1730 return FALSE;
1731 }
1732
1733 unit->line_table = decode_line_info (unit, stash);
1734
1735 if (! unit->line_table)
1736 {
1737 unit->error = 1;
1738 return FALSE;
1739 }
1740
1741 if (unit->first_child_die_ptr < unit->end_ptr
1742 && ! scan_unit_for_functions (unit))
1743 {
1744 unit->error = 1;
1745 return FALSE;
1746 }
1747 }
1748
1749 function = NULL;
1750 func_p = lookup_address_in_function_table (unit->function_table, addr,
1751 &function, functionname_ptr);
1752 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1753 function, filename_ptr,
1754 linenumber_ptr);
1755 return line_p || func_p;
1756 }
1757
1758 /* Locate a section in a BFD containing debugging info. The search starts
1759 from the section after AFTER_SEC, or from the first section in the BFD if
1760 AFTER_SEC is NULL. The search works by examining the names of the
1761 sections. There are two permissiable names. The first is .debug_info.
1762 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1763 This is a variation on the .debug_info section which has a checksum
1764 describing the contents appended onto the name. This allows the linker to
1765 identify and discard duplicate debugging sections for different
1766 compilation units. */
1767 #define DWARF2_DEBUG_INFO ".debug_info"
1768 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1769
1770 static asection *
1771 find_debug_info (abfd, after_sec)
1772 bfd * abfd;
1773 asection * after_sec;
1774 {
1775 asection * msec;
1776
1777 if (after_sec)
1778 msec = after_sec->next;
1779 else
1780 msec = abfd->sections;
1781
1782 while (msec)
1783 {
1784 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1785 return msec;
1786
1787 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1788 return msec;
1789
1790 msec = msec->next;
1791 }
1792
1793 return NULL;
1794 }
1795
1796 /* The DWARF2 version of find_nearest line. Return TRUE if the line
1797 is found without error. ADDR_SIZE is the number of bytes in the
1798 initial .debug_info length field and in the abbreviation offset.
1799 You may use zero to indicate that the default value should be
1800 used. */
1801
1802 bfd_boolean
1803 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1804 filename_ptr, functionname_ptr,
1805 linenumber_ptr, addr_size, pinfo)
1806 bfd *abfd;
1807 asection *section;
1808 asymbol **symbols;
1809 bfd_vma offset;
1810 const char **filename_ptr;
1811 const char **functionname_ptr;
1812 unsigned int *linenumber_ptr;
1813 unsigned int addr_size;
1814 PTR *pinfo;
1815 {
1816 /* Read each compilation unit from the section .debug_info, and check
1817 to see if it contains the address we are searching for. If yes,
1818 lookup the address, and return the line number info. If no, go
1819 on to the next compilation unit.
1820
1821 We keep a list of all the previously read compilation units, and
1822 a pointer to the next un-read compilation unit. Check the
1823 previously read units before reading more. */
1824 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
1825
1826 /* What address are we looking for? */
1827 bfd_vma addr = offset + section->vma;
1828
1829 struct comp_unit* each;
1830
1831 *filename_ptr = NULL;
1832 *functionname_ptr = NULL;
1833 *linenumber_ptr = 0;
1834
1835 /* The DWARF2 spec says that the initial length field, and the
1836 offset of the abbreviation table, should both be 4-byte values.
1837 However, some compilers do things differently. */
1838 if (addr_size == 0)
1839 addr_size = 4;
1840 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1841
1842 if (! stash)
1843 {
1844 bfd_size_type total_size;
1845 asection *msec;
1846 bfd_size_type amt = sizeof (struct dwarf2_debug);
1847
1848 stash = (struct dwarf2_debug*) bfd_zalloc (abfd, amt);
1849 if (! stash)
1850 return FALSE;
1851
1852 *pinfo = (PTR) stash;
1853
1854 msec = find_debug_info (abfd, NULL);
1855 if (! msec)
1856 /* No dwarf2 info. Note that at this point the stash
1857 has been allocated, but contains zeros, this lets
1858 future calls to this function fail quicker. */
1859 return FALSE;
1860
1861 /* There can be more than one DWARF2 info section in a BFD these days.
1862 Read them all in and produce one large stash. We do this in two
1863 passes - in the first pass we just accumulate the section sizes.
1864 In the second pass we read in the section's contents. The allows
1865 us to avoid reallocing the data as we add sections to the stash. */
1866 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1867 total_size += msec->_raw_size;
1868
1869 stash->info_ptr = (char *) bfd_alloc (abfd, total_size);
1870 if (stash->info_ptr == NULL)
1871 return FALSE;
1872
1873 stash->info_ptr_end = stash->info_ptr;
1874
1875 for (msec = find_debug_info (abfd, NULL);
1876 msec;
1877 msec = find_debug_info (abfd, msec))
1878 {
1879 bfd_size_type size;
1880 bfd_size_type start;
1881
1882 size = msec->_raw_size;
1883 if (size == 0)
1884 continue;
1885
1886 start = stash->info_ptr_end - stash->info_ptr;
1887
1888 if ((bfd_simple_get_relocated_section_contents
1889 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1890 continue;
1891
1892 stash->info_ptr_end = stash->info_ptr + start + size;
1893 }
1894
1895 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1896
1897 stash->sec = find_debug_info (abfd, NULL);
1898 stash->sec_info_ptr = stash->info_ptr;
1899 stash->syms = symbols;
1900 }
1901
1902 /* A null info_ptr indicates that there is no dwarf2 info
1903 (or that an error occured while setting up the stash). */
1904 if (! stash->info_ptr)
1905 return FALSE;
1906
1907 /* Check the previously read comp. units first. */
1908 for (each = stash->all_comp_units; each; each = each->next_unit)
1909 if (comp_unit_contains_address (each, addr))
1910 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1911 functionname_ptr, linenumber_ptr,
1912 stash);
1913
1914 /* Read each remaining comp. units checking each as they are read. */
1915 while (stash->info_ptr < stash->info_ptr_end)
1916 {
1917 bfd_vma length;
1918 bfd_boolean found;
1919 unsigned int offset_size = addr_size;
1920
1921 if (addr_size == 4)
1922 {
1923 length = read_4_bytes (abfd, stash->info_ptr);
1924 if (length == 0xffffffff)
1925 {
1926 offset_size = 8;
1927 length = read_8_bytes (abfd, stash->info_ptr + 4);
1928 stash->info_ptr += 8;
1929 }
1930 else if (length == 0)
1931 {
1932 /* Handle (non-standard) 64-bit DWARF2 formats. */
1933 offset_size = 8;
1934 length = read_4_bytes (abfd, stash->info_ptr + 4);
1935 stash->info_ptr += 4;
1936 }
1937 }
1938 else
1939 length = read_8_bytes (abfd, stash->info_ptr);
1940 stash->info_ptr += addr_size;
1941
1942 if (length > 0)
1943 {
1944 each = parse_comp_unit (abfd, stash, length, offset_size);
1945 stash->info_ptr += length;
1946
1947 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1948 == stash->sec->_raw_size)
1949 {
1950 stash->sec = find_debug_info (abfd, stash->sec);
1951 stash->sec_info_ptr = stash->info_ptr;
1952 }
1953
1954 if (each)
1955 {
1956 each->next_unit = stash->all_comp_units;
1957 stash->all_comp_units = each;
1958
1959 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1960 compilation units. If we don't have them (i.e.,
1961 unit->high == 0), we need to consult the line info
1962 table to see if a compilation unit contains the given
1963 address. */
1964 if (each->arange.high > 0)
1965 {
1966 if (comp_unit_contains_address (each, addr))
1967 return comp_unit_find_nearest_line (each, addr,
1968 filename_ptr,
1969 functionname_ptr,
1970 linenumber_ptr,
1971 stash);
1972 }
1973 else
1974 {
1975 found = comp_unit_find_nearest_line (each, addr,
1976 filename_ptr,
1977 functionname_ptr,
1978 linenumber_ptr,
1979 stash);
1980 if (found)
1981 return TRUE;
1982 }
1983 }
1984 }
1985 }
1986
1987 return FALSE;
1988 }
This page took 0.071927 seconds and 4 git commands to generate.