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