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