Bump version for general release.
[binutils.git] / binutils / dwarf.c
blobcdc8d12b66178cda4cddd25fdb861a728d8dd04f
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2 Copyright 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "libiberty.h"
24 #include "bfd.h"
25 #include "bucomm.h"
26 #include "elf/common.h"
27 #include "elf/dwarf2.h"
28 #include "dwarf.h"
30 static int have_frame_base;
31 static int need_base_address;
33 static unsigned int last_pointer_size = 0;
34 static int warned_about_missing_comp_units = FALSE;
36 static unsigned int num_debug_info_entries = 0;
37 static debug_info *debug_information = NULL;
38 /* Special value for num_debug_info_entries to indicate
39 that the .debug_info section could not be loaded/parsed. */
40 #define DEBUG_INFO_UNAVAILABLE (unsigned int) -1
42 int eh_addr_size;
44 int do_debug_info;
45 int do_debug_abbrevs;
46 int do_debug_lines;
47 int do_debug_pubnames;
48 int do_debug_aranges;
49 int do_debug_ranges;
50 int do_debug_frames;
51 int do_debug_frames_interp;
52 int do_debug_macinfo;
53 int do_debug_str;
54 int do_debug_loc;
56 dwarf_vma (*byte_get) (unsigned char *, int);
58 dwarf_vma
59 byte_get_little_endian (unsigned char *field, int size)
61 switch (size)
63 case 1:
64 return *field;
66 case 2:
67 return ((unsigned int) (field[0]))
68 | (((unsigned int) (field[1])) << 8);
70 case 4:
71 return ((unsigned long) (field[0]))
72 | (((unsigned long) (field[1])) << 8)
73 | (((unsigned long) (field[2])) << 16)
74 | (((unsigned long) (field[3])) << 24);
76 case 8:
77 if (sizeof (dwarf_vma) == 8)
78 return ((dwarf_vma) (field[0]))
79 | (((dwarf_vma) (field[1])) << 8)
80 | (((dwarf_vma) (field[2])) << 16)
81 | (((dwarf_vma) (field[3])) << 24)
82 | (((dwarf_vma) (field[4])) << 32)
83 | (((dwarf_vma) (field[5])) << 40)
84 | (((dwarf_vma) (field[6])) << 48)
85 | (((dwarf_vma) (field[7])) << 56);
86 else if (sizeof (dwarf_vma) == 4)
87 /* We want to extract data from an 8 byte wide field and
88 place it into a 4 byte wide field. Since this is a little
89 endian source we can just use the 4 byte extraction code. */
90 return ((unsigned long) (field[0]))
91 | (((unsigned long) (field[1])) << 8)
92 | (((unsigned long) (field[2])) << 16)
93 | (((unsigned long) (field[3])) << 24);
95 default:
96 error (_("Unhandled data length: %d\n"), size);
97 abort ();
101 dwarf_vma
102 byte_get_big_endian (unsigned char *field, int size)
104 switch (size)
106 case 1:
107 return *field;
109 case 2:
110 return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
112 case 4:
113 return ((unsigned long) (field[3]))
114 | (((unsigned long) (field[2])) << 8)
115 | (((unsigned long) (field[1])) << 16)
116 | (((unsigned long) (field[0])) << 24);
118 case 8:
119 if (sizeof (dwarf_vma) == 8)
120 return ((dwarf_vma) (field[7]))
121 | (((dwarf_vma) (field[6])) << 8)
122 | (((dwarf_vma) (field[5])) << 16)
123 | (((dwarf_vma) (field[4])) << 24)
124 | (((dwarf_vma) (field[3])) << 32)
125 | (((dwarf_vma) (field[2])) << 40)
126 | (((dwarf_vma) (field[1])) << 48)
127 | (((dwarf_vma) (field[0])) << 56);
128 else if (sizeof (dwarf_vma) == 4)
130 /* Although we are extracing data from an 8 byte wide field,
131 we are returning only 4 bytes of data. */
132 field += 4;
133 return ((unsigned long) (field[3]))
134 | (((unsigned long) (field[2])) << 8)
135 | (((unsigned long) (field[1])) << 16)
136 | (((unsigned long) (field[0])) << 24);
139 default:
140 error (_("Unhandled data length: %d\n"), size);
141 abort ();
145 static dwarf_vma
146 byte_get_signed (unsigned char *field, int size)
148 dwarf_vma x = byte_get (field, size);
150 switch (size)
152 case 1:
153 return (x ^ 0x80) - 0x80;
154 case 2:
155 return (x ^ 0x8000) - 0x8000;
156 case 4:
157 return (x ^ 0x80000000) - 0x80000000;
158 case 8:
159 return x;
160 default:
161 abort ();
165 /* Print a dwarf_vma value (typically an address, offset or length) in
166 hexadecimal format, followed by a space. The length of the value (and
167 hence the precision displayed) is determined by the byte_size parameter. */
169 static void
170 print_dwarf_vma (dwarf_vma val, unsigned byte_size)
172 static char buff[18];
174 /* Printf does not have a way of specifiying a maximum field width for an
175 integer value, so we print the full value into a buffer and then select
176 the precision we need. */
177 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
178 #ifndef __MSVCRT__
179 snprintf (buff, sizeof (buff), "%16.16llx ", val);
180 #else
181 snprintf (buff, sizeof (buff), "%016I64x ", val);
182 #endif
183 #else
184 snprintf (buff, sizeof (buff), "%16.16lx ", val);
185 #endif
187 printf (buff + (byte_size == 4 ? 8 : 0));
190 static unsigned long int
191 read_leb128 (unsigned char *data, unsigned int *length_return, int sign)
193 unsigned long int result = 0;
194 unsigned int num_read = 0;
195 unsigned int shift = 0;
196 unsigned char byte;
200 byte = *data++;
201 num_read++;
203 result |= ((unsigned long int) (byte & 0x7f)) << shift;
205 shift += 7;
208 while (byte & 0x80);
210 if (length_return != NULL)
211 *length_return = num_read;
213 if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
214 result |= -1L << shift;
216 return result;
219 typedef struct State_Machine_Registers
221 unsigned long address;
222 unsigned int file;
223 unsigned int line;
224 unsigned int column;
225 int is_stmt;
226 int basic_block;
227 int end_sequence;
228 /* This variable hold the number of the last entry seen
229 in the File Table. */
230 unsigned int last_file_entry;
231 } SMR;
233 static SMR state_machine_regs;
235 static void
236 reset_state_machine (int is_stmt)
238 state_machine_regs.address = 0;
239 state_machine_regs.file = 1;
240 state_machine_regs.line = 1;
241 state_machine_regs.column = 0;
242 state_machine_regs.is_stmt = is_stmt;
243 state_machine_regs.basic_block = 0;
244 state_machine_regs.end_sequence = 0;
245 state_machine_regs.last_file_entry = 0;
248 /* Handled an extend line op.
249 Returns the number of bytes read. */
251 static int
252 process_extended_line_op (unsigned char *data, int is_stmt)
254 unsigned char op_code;
255 unsigned int bytes_read;
256 unsigned int len;
257 unsigned char *name;
258 unsigned long adr;
260 len = read_leb128 (data, & bytes_read, 0);
261 data += bytes_read;
263 if (len == 0)
265 warn (_("badly formed extended line op encountered!\n"));
266 return bytes_read;
269 len += bytes_read;
270 op_code = *data++;
272 printf (_(" Extended opcode %d: "), op_code);
274 switch (op_code)
276 case DW_LNE_end_sequence:
277 printf (_("End of Sequence\n\n"));
278 reset_state_machine (is_stmt);
279 break;
281 case DW_LNE_set_address:
282 adr = byte_get (data, len - bytes_read - 1);
283 printf (_("set Address to 0x%lx\n"), adr);
284 state_machine_regs.address = adr;
285 break;
287 case DW_LNE_define_file:
288 printf (_(" define new File Table entry\n"));
289 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
291 printf (_(" %d\t"), ++state_machine_regs.last_file_entry);
292 name = data;
293 data += strlen ((char *) data) + 1;
294 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
295 data += bytes_read;
296 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
297 data += bytes_read;
298 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
299 printf (_("%s\n\n"), name);
300 break;
302 /* HP extensions. */
303 case DW_LNE_HP_negate_is_UV_update:
304 printf ("DW_LNE_HP_negate_is_UV_update");
305 break;
306 case DW_LNE_HP_push_context:
307 printf ("DW_LNE_HP_push_context");
308 break;
309 case DW_LNE_HP_pop_context:
310 printf ("DW_LNE_HP_pop_context");
311 break;
312 case DW_LNE_HP_set_file_line_column:
313 printf ("DW_LNE_HP_set_file_line_column");
314 break;
315 case DW_LNE_HP_set_routine_name:
316 printf ("DW_LNE_HP_set_routine_name");
317 break;
318 case DW_LNE_HP_set_sequence:
319 printf ("DW_LNE_HP_set_sequence");
320 break;
321 case DW_LNE_HP_negate_post_semantics:
322 printf ("DW_LNE_HP_negate_post_semantics");
323 break;
324 case DW_LNE_HP_negate_function_exit:
325 printf ("DW_LNE_HP_negate_function_exit");
326 break;
327 case DW_LNE_HP_negate_front_end_logical:
328 printf ("DW_LNE_HP_negate_front_end_logical");
329 break;
330 case DW_LNE_HP_define_proc:
331 printf ("DW_LNE_HP_define_proc");
332 break;
334 default:
335 if (op_code >= DW_LNE_lo_user
336 /* The test against DW_LNW_hi_user is redundant due to
337 the limited range of the unsigned char data type used
338 for op_code. */
339 /*&& op_code <= DW_LNE_hi_user*/)
340 printf (_("user defined: length %d\n"), len - bytes_read);
341 else
342 printf (_("UNKNOWN: length %d\n"), len - bytes_read);
343 break;
346 return len;
349 static const char *
350 fetch_indirect_string (unsigned long offset)
352 struct dwarf_section *section = &debug_displays [str].section;
354 if (section->start == NULL)
355 return _("<no .debug_str section>");
357 /* DWARF sections under Mach-O have non-zero addresses. */
358 offset -= section->address;
359 if (offset > section->size)
361 warn (_("DW_FORM_strp offset too big: %lx\n"), offset);
362 return _("<offset is too big>");
365 return (const char *) section->start + offset;
368 /* FIXME: There are better and more efficient ways to handle
369 these structures. For now though, I just want something that
370 is simple to implement. */
371 typedef struct abbrev_attr
373 unsigned long attribute;
374 unsigned long form;
375 struct abbrev_attr *next;
377 abbrev_attr;
379 typedef struct abbrev_entry
381 unsigned long entry;
382 unsigned long tag;
383 int children;
384 struct abbrev_attr *first_attr;
385 struct abbrev_attr *last_attr;
386 struct abbrev_entry *next;
388 abbrev_entry;
390 static abbrev_entry *first_abbrev = NULL;
391 static abbrev_entry *last_abbrev = NULL;
393 static void
394 free_abbrevs (void)
396 abbrev_entry *abbrev;
398 for (abbrev = first_abbrev; abbrev;)
400 abbrev_entry *next = abbrev->next;
401 abbrev_attr *attr;
403 for (attr = abbrev->first_attr; attr;)
405 abbrev_attr *next = attr->next;
407 free (attr);
408 attr = next;
411 free (abbrev);
412 abbrev = next;
415 last_abbrev = first_abbrev = NULL;
418 static void
419 add_abbrev (unsigned long number, unsigned long tag, int children)
421 abbrev_entry *entry;
423 entry = malloc (sizeof (*entry));
425 if (entry == NULL)
426 /* ugg */
427 return;
429 entry->entry = number;
430 entry->tag = tag;
431 entry->children = children;
432 entry->first_attr = NULL;
433 entry->last_attr = NULL;
434 entry->next = NULL;
436 if (first_abbrev == NULL)
437 first_abbrev = entry;
438 else
439 last_abbrev->next = entry;
441 last_abbrev = entry;
444 static void
445 add_abbrev_attr (unsigned long attribute, unsigned long form)
447 abbrev_attr *attr;
449 attr = malloc (sizeof (*attr));
451 if (attr == NULL)
452 /* ugg */
453 return;
455 attr->attribute = attribute;
456 attr->form = form;
457 attr->next = NULL;
459 if (last_abbrev->first_attr == NULL)
460 last_abbrev->first_attr = attr;
461 else
462 last_abbrev->last_attr->next = attr;
464 last_abbrev->last_attr = attr;
467 /* Processes the (partial) contents of a .debug_abbrev section.
468 Returns NULL if the end of the section was encountered.
469 Returns the address after the last byte read if the end of
470 an abbreviation set was found. */
472 static unsigned char *
473 process_abbrev_section (unsigned char *start, unsigned char *end)
475 if (first_abbrev != NULL)
476 return NULL;
478 while (start < end)
480 unsigned int bytes_read;
481 unsigned long entry;
482 unsigned long tag;
483 unsigned long attribute;
484 int children;
486 entry = read_leb128 (start, & bytes_read, 0);
487 start += bytes_read;
489 /* A single zero is supposed to end the section according
490 to the standard. If there's more, then signal that to
491 the caller. */
492 if (entry == 0)
493 return start == end ? NULL : start;
495 tag = read_leb128 (start, & bytes_read, 0);
496 start += bytes_read;
498 children = *start++;
500 add_abbrev (entry, tag, children);
504 unsigned long form;
506 attribute = read_leb128 (start, & bytes_read, 0);
507 start += bytes_read;
509 form = read_leb128 (start, & bytes_read, 0);
510 start += bytes_read;
512 if (attribute != 0)
513 add_abbrev_attr (attribute, form);
515 while (attribute != 0);
518 return NULL;
521 static char *
522 get_TAG_name (unsigned long tag)
524 switch (tag)
526 case DW_TAG_padding: return "DW_TAG_padding";
527 case DW_TAG_array_type: return "DW_TAG_array_type";
528 case DW_TAG_class_type: return "DW_TAG_class_type";
529 case DW_TAG_entry_point: return "DW_TAG_entry_point";
530 case DW_TAG_enumeration_type: return "DW_TAG_enumeration_type";
531 case DW_TAG_formal_parameter: return "DW_TAG_formal_parameter";
532 case DW_TAG_imported_declaration: return "DW_TAG_imported_declaration";
533 case DW_TAG_label: return "DW_TAG_label";
534 case DW_TAG_lexical_block: return "DW_TAG_lexical_block";
535 case DW_TAG_member: return "DW_TAG_member";
536 case DW_TAG_pointer_type: return "DW_TAG_pointer_type";
537 case DW_TAG_reference_type: return "DW_TAG_reference_type";
538 case DW_TAG_compile_unit: return "DW_TAG_compile_unit";
539 case DW_TAG_string_type: return "DW_TAG_string_type";
540 case DW_TAG_structure_type: return "DW_TAG_structure_type";
541 case DW_TAG_subroutine_type: return "DW_TAG_subroutine_type";
542 case DW_TAG_typedef: return "DW_TAG_typedef";
543 case DW_TAG_union_type: return "DW_TAG_union_type";
544 case DW_TAG_unspecified_parameters: return "DW_TAG_unspecified_parameters";
545 case DW_TAG_variant: return "DW_TAG_variant";
546 case DW_TAG_common_block: return "DW_TAG_common_block";
547 case DW_TAG_common_inclusion: return "DW_TAG_common_inclusion";
548 case DW_TAG_inheritance: return "DW_TAG_inheritance";
549 case DW_TAG_inlined_subroutine: return "DW_TAG_inlined_subroutine";
550 case DW_TAG_module: return "DW_TAG_module";
551 case DW_TAG_ptr_to_member_type: return "DW_TAG_ptr_to_member_type";
552 case DW_TAG_set_type: return "DW_TAG_set_type";
553 case DW_TAG_subrange_type: return "DW_TAG_subrange_type";
554 case DW_TAG_with_stmt: return "DW_TAG_with_stmt";
555 case DW_TAG_access_declaration: return "DW_TAG_access_declaration";
556 case DW_TAG_base_type: return "DW_TAG_base_type";
557 case DW_TAG_catch_block: return "DW_TAG_catch_block";
558 case DW_TAG_const_type: return "DW_TAG_const_type";
559 case DW_TAG_constant: return "DW_TAG_constant";
560 case DW_TAG_enumerator: return "DW_TAG_enumerator";
561 case DW_TAG_file_type: return "DW_TAG_file_type";
562 case DW_TAG_friend: return "DW_TAG_friend";
563 case DW_TAG_namelist: return "DW_TAG_namelist";
564 case DW_TAG_namelist_item: return "DW_TAG_namelist_item";
565 case DW_TAG_packed_type: return "DW_TAG_packed_type";
566 case DW_TAG_subprogram: return "DW_TAG_subprogram";
567 case DW_TAG_template_type_param: return "DW_TAG_template_type_param";
568 case DW_TAG_template_value_param: return "DW_TAG_template_value_param";
569 case DW_TAG_thrown_type: return "DW_TAG_thrown_type";
570 case DW_TAG_try_block: return "DW_TAG_try_block";
571 case DW_TAG_variant_part: return "DW_TAG_variant_part";
572 case DW_TAG_variable: return "DW_TAG_variable";
573 case DW_TAG_volatile_type: return "DW_TAG_volatile_type";
574 case DW_TAG_MIPS_loop: return "DW_TAG_MIPS_loop";
575 case DW_TAG_format_label: return "DW_TAG_format_label";
576 case DW_TAG_function_template: return "DW_TAG_function_template";
577 case DW_TAG_class_template: return "DW_TAG_class_template";
578 /* DWARF 2.1 values. */
579 case DW_TAG_dwarf_procedure: return "DW_TAG_dwarf_procedure";
580 case DW_TAG_restrict_type: return "DW_TAG_restrict_type";
581 case DW_TAG_interface_type: return "DW_TAG_interface_type";
582 case DW_TAG_namespace: return "DW_TAG_namespace";
583 case DW_TAG_imported_module: return "DW_TAG_imported_module";
584 case DW_TAG_unspecified_type: return "DW_TAG_unspecified_type";
585 case DW_TAG_partial_unit: return "DW_TAG_partial_unit";
586 case DW_TAG_imported_unit: return "DW_TAG_imported_unit";
587 /* UPC values. */
588 case DW_TAG_upc_shared_type: return "DW_TAG_upc_shared_type";
589 case DW_TAG_upc_strict_type: return "DW_TAG_upc_strict_type";
590 case DW_TAG_upc_relaxed_type: return "DW_TAG_upc_relaxed_type";
591 default:
593 static char buffer[100];
595 snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
596 return buffer;
601 static char *
602 get_FORM_name (unsigned long form)
604 switch (form)
606 case DW_FORM_addr: return "DW_FORM_addr";
607 case DW_FORM_block2: return "DW_FORM_block2";
608 case DW_FORM_block4: return "DW_FORM_block4";
609 case DW_FORM_data2: return "DW_FORM_data2";
610 case DW_FORM_data4: return "DW_FORM_data4";
611 case DW_FORM_data8: return "DW_FORM_data8";
612 case DW_FORM_string: return "DW_FORM_string";
613 case DW_FORM_block: return "DW_FORM_block";
614 case DW_FORM_block1: return "DW_FORM_block1";
615 case DW_FORM_data1: return "DW_FORM_data1";
616 case DW_FORM_flag: return "DW_FORM_flag";
617 case DW_FORM_sdata: return "DW_FORM_sdata";
618 case DW_FORM_strp: return "DW_FORM_strp";
619 case DW_FORM_udata: return "DW_FORM_udata";
620 case DW_FORM_ref_addr: return "DW_FORM_ref_addr";
621 case DW_FORM_ref1: return "DW_FORM_ref1";
622 case DW_FORM_ref2: return "DW_FORM_ref2";
623 case DW_FORM_ref4: return "DW_FORM_ref4";
624 case DW_FORM_ref8: return "DW_FORM_ref8";
625 case DW_FORM_ref_udata: return "DW_FORM_ref_udata";
626 case DW_FORM_indirect: return "DW_FORM_indirect";
627 default:
629 static char buffer[100];
631 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
632 return buffer;
637 static unsigned char *
638 display_block (unsigned char *data, unsigned long length)
640 printf (_(" %lu byte block: "), length);
642 while (length --)
643 printf ("%lx ", (unsigned long) byte_get (data++, 1));
645 return data;
648 static int
649 decode_location_expression (unsigned char * data,
650 unsigned int pointer_size,
651 unsigned long length,
652 unsigned long cu_offset)
654 unsigned op;
655 unsigned int bytes_read;
656 unsigned long uvalue;
657 unsigned char *end = data + length;
658 int need_frame_base = 0;
660 while (data < end)
662 op = *data++;
664 switch (op)
666 case DW_OP_addr:
667 printf ("DW_OP_addr: %lx",
668 (unsigned long) byte_get (data, pointer_size));
669 data += pointer_size;
670 break;
671 case DW_OP_deref:
672 printf ("DW_OP_deref");
673 break;
674 case DW_OP_const1u:
675 printf ("DW_OP_const1u: %lu", (unsigned long) byte_get (data++, 1));
676 break;
677 case DW_OP_const1s:
678 printf ("DW_OP_const1s: %ld", (long) byte_get_signed (data++, 1));
679 break;
680 case DW_OP_const2u:
681 printf ("DW_OP_const2u: %lu", (unsigned long) byte_get (data, 2));
682 data += 2;
683 break;
684 case DW_OP_const2s:
685 printf ("DW_OP_const2s: %ld", (long) byte_get_signed (data, 2));
686 data += 2;
687 break;
688 case DW_OP_const4u:
689 printf ("DW_OP_const4u: %lu", (unsigned long) byte_get (data, 4));
690 data += 4;
691 break;
692 case DW_OP_const4s:
693 printf ("DW_OP_const4s: %ld", (long) byte_get_signed (data, 4));
694 data += 4;
695 break;
696 case DW_OP_const8u:
697 printf ("DW_OP_const8u: %lu %lu", (unsigned long) byte_get (data, 4),
698 (unsigned long) byte_get (data + 4, 4));
699 data += 8;
700 break;
701 case DW_OP_const8s:
702 printf ("DW_OP_const8s: %ld %ld", (long) byte_get (data, 4),
703 (long) byte_get (data + 4, 4));
704 data += 8;
705 break;
706 case DW_OP_constu:
707 printf ("DW_OP_constu: %lu", read_leb128 (data, &bytes_read, 0));
708 data += bytes_read;
709 break;
710 case DW_OP_consts:
711 printf ("DW_OP_consts: %ld", read_leb128 (data, &bytes_read, 1));
712 data += bytes_read;
713 break;
714 case DW_OP_dup:
715 printf ("DW_OP_dup");
716 break;
717 case DW_OP_drop:
718 printf ("DW_OP_drop");
719 break;
720 case DW_OP_over:
721 printf ("DW_OP_over");
722 break;
723 case DW_OP_pick:
724 printf ("DW_OP_pick: %ld", (unsigned long) byte_get (data++, 1));
725 break;
726 case DW_OP_swap:
727 printf ("DW_OP_swap");
728 break;
729 case DW_OP_rot:
730 printf ("DW_OP_rot");
731 break;
732 case DW_OP_xderef:
733 printf ("DW_OP_xderef");
734 break;
735 case DW_OP_abs:
736 printf ("DW_OP_abs");
737 break;
738 case DW_OP_and:
739 printf ("DW_OP_and");
740 break;
741 case DW_OP_div:
742 printf ("DW_OP_div");
743 break;
744 case DW_OP_minus:
745 printf ("DW_OP_minus");
746 break;
747 case DW_OP_mod:
748 printf ("DW_OP_mod");
749 break;
750 case DW_OP_mul:
751 printf ("DW_OP_mul");
752 break;
753 case DW_OP_neg:
754 printf ("DW_OP_neg");
755 break;
756 case DW_OP_not:
757 printf ("DW_OP_not");
758 break;
759 case DW_OP_or:
760 printf ("DW_OP_or");
761 break;
762 case DW_OP_plus:
763 printf ("DW_OP_plus");
764 break;
765 case DW_OP_plus_uconst:
766 printf ("DW_OP_plus_uconst: %lu",
767 read_leb128 (data, &bytes_read, 0));
768 data += bytes_read;
769 break;
770 case DW_OP_shl:
771 printf ("DW_OP_shl");
772 break;
773 case DW_OP_shr:
774 printf ("DW_OP_shr");
775 break;
776 case DW_OP_shra:
777 printf ("DW_OP_shra");
778 break;
779 case DW_OP_xor:
780 printf ("DW_OP_xor");
781 break;
782 case DW_OP_bra:
783 printf ("DW_OP_bra: %ld", (long) byte_get_signed (data, 2));
784 data += 2;
785 break;
786 case DW_OP_eq:
787 printf ("DW_OP_eq");
788 break;
789 case DW_OP_ge:
790 printf ("DW_OP_ge");
791 break;
792 case DW_OP_gt:
793 printf ("DW_OP_gt");
794 break;
795 case DW_OP_le:
796 printf ("DW_OP_le");
797 break;
798 case DW_OP_lt:
799 printf ("DW_OP_lt");
800 break;
801 case DW_OP_ne:
802 printf ("DW_OP_ne");
803 break;
804 case DW_OP_skip:
805 printf ("DW_OP_skip: %ld", (long) byte_get_signed (data, 2));
806 data += 2;
807 break;
809 case DW_OP_lit0:
810 case DW_OP_lit1:
811 case DW_OP_lit2:
812 case DW_OP_lit3:
813 case DW_OP_lit4:
814 case DW_OP_lit5:
815 case DW_OP_lit6:
816 case DW_OP_lit7:
817 case DW_OP_lit8:
818 case DW_OP_lit9:
819 case DW_OP_lit10:
820 case DW_OP_lit11:
821 case DW_OP_lit12:
822 case DW_OP_lit13:
823 case DW_OP_lit14:
824 case DW_OP_lit15:
825 case DW_OP_lit16:
826 case DW_OP_lit17:
827 case DW_OP_lit18:
828 case DW_OP_lit19:
829 case DW_OP_lit20:
830 case DW_OP_lit21:
831 case DW_OP_lit22:
832 case DW_OP_lit23:
833 case DW_OP_lit24:
834 case DW_OP_lit25:
835 case DW_OP_lit26:
836 case DW_OP_lit27:
837 case DW_OP_lit28:
838 case DW_OP_lit29:
839 case DW_OP_lit30:
840 case DW_OP_lit31:
841 printf ("DW_OP_lit%d", op - DW_OP_lit0);
842 break;
844 case DW_OP_reg0:
845 case DW_OP_reg1:
846 case DW_OP_reg2:
847 case DW_OP_reg3:
848 case DW_OP_reg4:
849 case DW_OP_reg5:
850 case DW_OP_reg6:
851 case DW_OP_reg7:
852 case DW_OP_reg8:
853 case DW_OP_reg9:
854 case DW_OP_reg10:
855 case DW_OP_reg11:
856 case DW_OP_reg12:
857 case DW_OP_reg13:
858 case DW_OP_reg14:
859 case DW_OP_reg15:
860 case DW_OP_reg16:
861 case DW_OP_reg17:
862 case DW_OP_reg18:
863 case DW_OP_reg19:
864 case DW_OP_reg20:
865 case DW_OP_reg21:
866 case DW_OP_reg22:
867 case DW_OP_reg23:
868 case DW_OP_reg24:
869 case DW_OP_reg25:
870 case DW_OP_reg26:
871 case DW_OP_reg27:
872 case DW_OP_reg28:
873 case DW_OP_reg29:
874 case DW_OP_reg30:
875 case DW_OP_reg31:
876 printf ("DW_OP_reg%d", op - DW_OP_reg0);
877 break;
879 case DW_OP_breg0:
880 case DW_OP_breg1:
881 case DW_OP_breg2:
882 case DW_OP_breg3:
883 case DW_OP_breg4:
884 case DW_OP_breg5:
885 case DW_OP_breg6:
886 case DW_OP_breg7:
887 case DW_OP_breg8:
888 case DW_OP_breg9:
889 case DW_OP_breg10:
890 case DW_OP_breg11:
891 case DW_OP_breg12:
892 case DW_OP_breg13:
893 case DW_OP_breg14:
894 case DW_OP_breg15:
895 case DW_OP_breg16:
896 case DW_OP_breg17:
897 case DW_OP_breg18:
898 case DW_OP_breg19:
899 case DW_OP_breg20:
900 case DW_OP_breg21:
901 case DW_OP_breg22:
902 case DW_OP_breg23:
903 case DW_OP_breg24:
904 case DW_OP_breg25:
905 case DW_OP_breg26:
906 case DW_OP_breg27:
907 case DW_OP_breg28:
908 case DW_OP_breg29:
909 case DW_OP_breg30:
910 case DW_OP_breg31:
911 printf ("DW_OP_breg%d: %ld", op - DW_OP_breg0,
912 read_leb128 (data, &bytes_read, 1));
913 data += bytes_read;
914 break;
916 case DW_OP_regx:
917 printf ("DW_OP_regx: %lu", read_leb128 (data, &bytes_read, 0));
918 data += bytes_read;
919 break;
920 case DW_OP_fbreg:
921 need_frame_base = 1;
922 printf ("DW_OP_fbreg: %ld", read_leb128 (data, &bytes_read, 1));
923 data += bytes_read;
924 break;
925 case DW_OP_bregx:
926 uvalue = read_leb128 (data, &bytes_read, 0);
927 data += bytes_read;
928 printf ("DW_OP_bregx: %lu %ld", uvalue,
929 read_leb128 (data, &bytes_read, 1));
930 data += bytes_read;
931 break;
932 case DW_OP_piece:
933 printf ("DW_OP_piece: %lu", read_leb128 (data, &bytes_read, 0));
934 data += bytes_read;
935 break;
936 case DW_OP_deref_size:
937 printf ("DW_OP_deref_size: %ld", (long) byte_get (data++, 1));
938 break;
939 case DW_OP_xderef_size:
940 printf ("DW_OP_xderef_size: %ld", (long) byte_get (data++, 1));
941 break;
942 case DW_OP_nop:
943 printf ("DW_OP_nop");
944 break;
946 /* DWARF 3 extensions. */
947 case DW_OP_push_object_address:
948 printf ("DW_OP_push_object_address");
949 break;
950 case DW_OP_call2:
951 /* XXX: Strictly speaking for 64-bit DWARF3 files
952 this ought to be an 8-byte wide computation. */
953 printf ("DW_OP_call2: <%lx>", (long) byte_get (data, 2) + cu_offset);
954 data += 2;
955 break;
956 case DW_OP_call4:
957 /* XXX: Strictly speaking for 64-bit DWARF3 files
958 this ought to be an 8-byte wide computation. */
959 printf ("DW_OP_call4: <%lx>", (long) byte_get (data, 4) + cu_offset);
960 data += 4;
961 break;
962 case DW_OP_call_ref:
963 /* XXX: Strictly speaking for 64-bit DWARF3 files
964 this ought to be an 8-byte wide computation. */
965 printf ("DW_OP_call_ref: <%lx>", (long) byte_get (data, 4) + cu_offset);
966 data += 4;
967 break;
968 case DW_OP_form_tls_address:
969 printf ("DW_OP_form_tls_address");
970 break;
971 case DW_OP_call_frame_cfa:
972 printf ("DW_OP_call_frame_cfa");
973 break;
974 case DW_OP_bit_piece:
975 printf ("DW_OP_bit_piece: ");
976 printf ("size: %lu ", read_leb128 (data, &bytes_read, 0));
977 data += bytes_read;
978 printf ("offset: %lu ", read_leb128 (data, &bytes_read, 0));
979 data += bytes_read;
980 break;
982 /* GNU extensions. */
983 case DW_OP_GNU_push_tls_address:
984 printf ("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown");
985 break;
986 case DW_OP_GNU_uninit:
987 printf ("DW_OP_GNU_uninit");
988 /* FIXME: Is there data associated with this OP ? */
989 break;
991 /* HP extensions. */
992 case DW_OP_HP_is_value:
993 printf ("DW_OP_HP_is_value");
994 /* FIXME: Is there data associated with this OP ? */
995 break;
996 case DW_OP_HP_fltconst4:
997 printf ("DW_OP_HP_fltconst4");
998 /* FIXME: Is there data associated with this OP ? */
999 break;
1000 case DW_OP_HP_fltconst8:
1001 printf ("DW_OP_HP_fltconst8");
1002 /* FIXME: Is there data associated with this OP ? */
1003 break;
1004 case DW_OP_HP_mod_range:
1005 printf ("DW_OP_HP_mod_range");
1006 /* FIXME: Is there data associated with this OP ? */
1007 break;
1008 case DW_OP_HP_unmod_range:
1009 printf ("DW_OP_HP_unmod_range");
1010 /* FIXME: Is there data associated with this OP ? */
1011 break;
1012 case DW_OP_HP_tls:
1013 printf ("DW_OP_HP_tls");
1014 /* FIXME: Is there data associated with this OP ? */
1015 break;
1017 /* PGI (STMicroelectronics) extensions. */
1018 case DW_OP_PGI_omp_thread_num:
1019 /* Pushes the thread number for the current thread as it would be
1020 returned by the standard OpenMP library function:
1021 omp_get_thread_num(). The "current thread" is the thread for
1022 which the expression is being evaluated. */
1023 printf ("DW_OP_PGI_omp_thread_num");
1024 break;
1026 default:
1027 if (op >= DW_OP_lo_user
1028 && op <= DW_OP_hi_user)
1029 printf (_("(User defined location op)"));
1030 else
1031 printf (_("(Unknown location op)"));
1032 /* No way to tell where the next op is, so just bail. */
1033 return need_frame_base;
1036 /* Separate the ops. */
1037 if (data < end)
1038 printf ("; ");
1041 return need_frame_base;
1044 static unsigned char *
1045 read_and_display_attr_value (unsigned long attribute,
1046 unsigned long form,
1047 unsigned char * data,
1048 unsigned long cu_offset,
1049 unsigned long pointer_size,
1050 unsigned long offset_size,
1051 int dwarf_version,
1052 debug_info * debug_info_p,
1053 int do_loc,
1054 struct dwarf_section * section)
1056 unsigned long uvalue = 0;
1057 unsigned char *block_start = NULL;
1058 unsigned char * orig_data = data;
1059 unsigned int bytes_read;
1061 switch (form)
1063 default:
1064 break;
1066 case DW_FORM_ref_addr:
1067 if (dwarf_version == 2)
1069 uvalue = byte_get (data, pointer_size);
1070 data += pointer_size;
1072 else if (dwarf_version == 3)
1074 uvalue = byte_get (data, offset_size);
1075 data += offset_size;
1077 else
1079 error (_("Internal error: DWARF version is not 2 or 3.\n"));
1081 break;
1083 case DW_FORM_addr:
1084 uvalue = byte_get (data, pointer_size);
1085 data += pointer_size;
1086 break;
1088 case DW_FORM_strp:
1089 uvalue = byte_get (data, offset_size);
1090 data += offset_size;
1091 break;
1093 case DW_FORM_ref1:
1094 case DW_FORM_flag:
1095 case DW_FORM_data1:
1096 uvalue = byte_get (data++, 1);
1097 break;
1099 case DW_FORM_ref2:
1100 case DW_FORM_data2:
1101 uvalue = byte_get (data, 2);
1102 data += 2;
1103 break;
1105 case DW_FORM_ref4:
1106 case DW_FORM_data4:
1107 uvalue = byte_get (data, 4);
1108 data += 4;
1109 break;
1111 case DW_FORM_sdata:
1112 uvalue = read_leb128 (data, & bytes_read, 1);
1113 data += bytes_read;
1114 break;
1116 case DW_FORM_ref_udata:
1117 case DW_FORM_udata:
1118 uvalue = read_leb128 (data, & bytes_read, 0);
1119 data += bytes_read;
1120 break;
1122 case DW_FORM_indirect:
1123 form = read_leb128 (data, & bytes_read, 0);
1124 data += bytes_read;
1125 if (!do_loc)
1126 printf (" %s", get_FORM_name (form));
1127 return read_and_display_attr_value (attribute, form, data,
1128 cu_offset, pointer_size,
1129 offset_size, dwarf_version,
1130 debug_info_p, do_loc,
1131 section);
1134 switch (form)
1136 case DW_FORM_ref_addr:
1137 if (!do_loc)
1138 printf (" <0x%lx>", uvalue);
1139 break;
1141 case DW_FORM_ref1:
1142 case DW_FORM_ref2:
1143 case DW_FORM_ref4:
1144 case DW_FORM_ref_udata:
1145 if (!do_loc)
1146 printf (" <0x%lx>", uvalue + cu_offset);
1147 break;
1149 case DW_FORM_data4:
1150 case DW_FORM_addr:
1151 if (!do_loc)
1152 printf (" 0x%lx", uvalue);
1153 break;
1155 case DW_FORM_flag:
1156 case DW_FORM_data1:
1157 case DW_FORM_data2:
1158 case DW_FORM_sdata:
1159 case DW_FORM_udata:
1160 if (!do_loc)
1161 printf (" %ld", uvalue);
1162 break;
1164 case DW_FORM_ref8:
1165 case DW_FORM_data8:
1166 if (!do_loc)
1168 uvalue = byte_get (data, 4);
1169 printf (" 0x%lx", uvalue);
1170 printf (" 0x%lx", (unsigned long) byte_get (data + 4, 4));
1172 if ((do_loc || do_debug_loc || do_debug_ranges)
1173 && num_debug_info_entries == 0)
1175 if (sizeof (uvalue) == 8)
1176 uvalue = byte_get (data, 8);
1177 else
1178 error (_("DW_FORM_data8 is unsupported when sizeof (unsigned long) != 8\n"));
1180 data += 8;
1181 break;
1183 case DW_FORM_string:
1184 if (!do_loc)
1185 printf (" %s", data);
1186 data += strlen ((char *) data) + 1;
1187 break;
1189 case DW_FORM_block:
1190 uvalue = read_leb128 (data, & bytes_read, 0);
1191 block_start = data + bytes_read;
1192 if (do_loc)
1193 data = block_start + uvalue;
1194 else
1195 data = display_block (block_start, uvalue);
1196 break;
1198 case DW_FORM_block1:
1199 uvalue = byte_get (data, 1);
1200 block_start = data + 1;
1201 if (do_loc)
1202 data = block_start + uvalue;
1203 else
1204 data = display_block (block_start, uvalue);
1205 break;
1207 case DW_FORM_block2:
1208 uvalue = byte_get (data, 2);
1209 block_start = data + 2;
1210 if (do_loc)
1211 data = block_start + uvalue;
1212 else
1213 data = display_block (block_start, uvalue);
1214 break;
1216 case DW_FORM_block4:
1217 uvalue = byte_get (data, 4);
1218 block_start = data + 4;
1219 if (do_loc)
1220 data = block_start + uvalue;
1221 else
1222 data = display_block (block_start, uvalue);
1223 break;
1225 case DW_FORM_strp:
1226 if (!do_loc)
1227 printf (_(" (indirect string, offset: 0x%lx): %s"),
1228 uvalue, fetch_indirect_string (uvalue));
1229 break;
1231 case DW_FORM_indirect:
1232 /* Handled above. */
1233 break;
1235 default:
1236 warn (_("Unrecognized form: %lu\n"), form);
1237 break;
1240 if ((do_loc || do_debug_loc || do_debug_ranges)
1241 && num_debug_info_entries == 0)
1243 switch (attribute)
1245 case DW_AT_frame_base:
1246 have_frame_base = 1;
1247 case DW_AT_location:
1248 case DW_AT_string_length:
1249 case DW_AT_return_addr:
1250 case DW_AT_data_member_location:
1251 case DW_AT_vtable_elem_location:
1252 case DW_AT_segment:
1253 case DW_AT_static_link:
1254 case DW_AT_use_location:
1255 if (form == DW_FORM_data4 || form == DW_FORM_data8)
1257 /* Process location list. */
1258 unsigned int max = debug_info_p->max_loc_offsets;
1259 unsigned int num = debug_info_p->num_loc_offsets;
1261 if (max == 0 || num >= max)
1263 max += 1024;
1264 debug_info_p->loc_offsets
1265 = xcrealloc (debug_info_p->loc_offsets,
1266 max, sizeof (*debug_info_p->loc_offsets));
1267 debug_info_p->have_frame_base
1268 = xcrealloc (debug_info_p->have_frame_base,
1269 max, sizeof (*debug_info_p->have_frame_base));
1270 debug_info_p->max_loc_offsets = max;
1272 debug_info_p->loc_offsets [num] = uvalue;
1273 debug_info_p->have_frame_base [num] = have_frame_base;
1274 debug_info_p->num_loc_offsets++;
1276 break;
1278 case DW_AT_low_pc:
1279 if (need_base_address)
1280 debug_info_p->base_address = uvalue;
1281 break;
1283 case DW_AT_ranges:
1284 if (form == DW_FORM_data4 || form == DW_FORM_data8)
1286 /* Process range list. */
1287 unsigned int max = debug_info_p->max_range_lists;
1288 unsigned int num = debug_info_p->num_range_lists;
1290 if (max == 0 || num >= max)
1292 max += 1024;
1293 debug_info_p->range_lists
1294 = xcrealloc (debug_info_p->range_lists,
1295 max, sizeof (*debug_info_p->range_lists));
1296 debug_info_p->max_range_lists = max;
1298 debug_info_p->range_lists [num] = uvalue;
1299 debug_info_p->num_range_lists++;
1301 break;
1303 default:
1304 break;
1308 if (do_loc)
1309 return data;
1311 /* For some attributes we can display further information. */
1312 printf ("\t");
1314 switch (attribute)
1316 case DW_AT_inline:
1317 switch (uvalue)
1319 case DW_INL_not_inlined:
1320 printf (_("(not inlined)"));
1321 break;
1322 case DW_INL_inlined:
1323 printf (_("(inlined)"));
1324 break;
1325 case DW_INL_declared_not_inlined:
1326 printf (_("(declared as inline but ignored)"));
1327 break;
1328 case DW_INL_declared_inlined:
1329 printf (_("(declared as inline and inlined)"));
1330 break;
1331 default:
1332 printf (_(" (Unknown inline attribute value: %lx)"), uvalue);
1333 break;
1335 break;
1337 case DW_AT_language:
1338 switch (uvalue)
1340 /* Ordered by the numeric value of these constants. */
1341 case DW_LANG_C89: printf ("(ANSI C)"); break;
1342 case DW_LANG_C: printf ("(non-ANSI C)"); break;
1343 case DW_LANG_Ada83: printf ("(Ada)"); break;
1344 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
1345 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
1346 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
1347 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
1348 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
1349 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
1350 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
1351 /* DWARF 2.1 values. */
1352 case DW_LANG_Java: printf ("(Java)"); break;
1353 case DW_LANG_C99: printf ("(ANSI C99)"); break;
1354 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
1355 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
1356 /* DWARF 3 values. */
1357 case DW_LANG_PLI: printf ("(PLI)"); break;
1358 case DW_LANG_ObjC: printf ("(Objective C)"); break;
1359 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
1360 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
1361 case DW_LANG_D: printf ("(D)"); break;
1362 /* MIPS extension. */
1363 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
1364 /* UPC extension. */
1365 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
1366 default:
1367 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
1368 printf ("(implementation defined: %lx)", uvalue);
1369 else
1370 printf ("(Unknown: %lx)", uvalue);
1371 break;
1373 break;
1375 case DW_AT_encoding:
1376 switch (uvalue)
1378 case DW_ATE_void: printf ("(void)"); break;
1379 case DW_ATE_address: printf ("(machine address)"); break;
1380 case DW_ATE_boolean: printf ("(boolean)"); break;
1381 case DW_ATE_complex_float: printf ("(complex float)"); break;
1382 case DW_ATE_float: printf ("(float)"); break;
1383 case DW_ATE_signed: printf ("(signed)"); break;
1384 case DW_ATE_signed_char: printf ("(signed char)"); break;
1385 case DW_ATE_unsigned: printf ("(unsigned)"); break;
1386 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
1387 /* DWARF 2.1 values: */
1388 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
1389 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
1390 /* DWARF 3 values: */
1391 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
1392 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
1393 case DW_ATE_edited: printf ("(edited)"); break;
1394 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
1395 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
1396 /* HP extensions: */
1397 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
1398 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
1399 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
1400 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
1401 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
1402 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
1403 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
1405 default:
1406 if (uvalue >= DW_ATE_lo_user
1407 && uvalue <= DW_ATE_hi_user)
1408 printf ("(user defined type)");
1409 else
1410 printf ("(unknown type)");
1411 break;
1413 break;
1415 case DW_AT_accessibility:
1416 switch (uvalue)
1418 case DW_ACCESS_public: printf ("(public)"); break;
1419 case DW_ACCESS_protected: printf ("(protected)"); break;
1420 case DW_ACCESS_private: printf ("(private)"); break;
1421 default:
1422 printf ("(unknown accessibility)");
1423 break;
1425 break;
1427 case DW_AT_visibility:
1428 switch (uvalue)
1430 case DW_VIS_local: printf ("(local)"); break;
1431 case DW_VIS_exported: printf ("(exported)"); break;
1432 case DW_VIS_qualified: printf ("(qualified)"); break;
1433 default: printf ("(unknown visibility)"); break;
1435 break;
1437 case DW_AT_virtuality:
1438 switch (uvalue)
1440 case DW_VIRTUALITY_none: printf ("(none)"); break;
1441 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
1442 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
1443 default: printf ("(unknown virtuality)"); break;
1445 break;
1447 case DW_AT_identifier_case:
1448 switch (uvalue)
1450 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
1451 case DW_ID_up_case: printf ("(up_case)"); break;
1452 case DW_ID_down_case: printf ("(down_case)"); break;
1453 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
1454 default: printf ("(unknown case)"); break;
1456 break;
1458 case DW_AT_calling_convention:
1459 switch (uvalue)
1461 case DW_CC_normal: printf ("(normal)"); break;
1462 case DW_CC_program: printf ("(program)"); break;
1463 case DW_CC_nocall: printf ("(nocall)"); break;
1464 default:
1465 if (uvalue >= DW_CC_lo_user
1466 && uvalue <= DW_CC_hi_user)
1467 printf ("(user defined)");
1468 else
1469 printf ("(unknown convention)");
1471 break;
1473 case DW_AT_ordering:
1474 switch (uvalue)
1476 case -1: printf ("(undefined)"); break;
1477 case 0: printf ("(row major)"); break;
1478 case 1: printf ("(column major)"); break;
1480 break;
1482 case DW_AT_frame_base:
1483 have_frame_base = 1;
1484 case DW_AT_location:
1485 case DW_AT_string_length:
1486 case DW_AT_return_addr:
1487 case DW_AT_data_member_location:
1488 case DW_AT_vtable_elem_location:
1489 case DW_AT_segment:
1490 case DW_AT_static_link:
1491 case DW_AT_use_location:
1492 if (form == DW_FORM_data4 || form == DW_FORM_data8)
1493 printf (_("(location list)"));
1494 /* Fall through. */
1495 case DW_AT_allocated:
1496 case DW_AT_associated:
1497 case DW_AT_data_location:
1498 case DW_AT_stride:
1499 case DW_AT_upper_bound:
1500 case DW_AT_lower_bound:
1501 if (block_start)
1503 int need_frame_base;
1505 printf ("(");
1506 need_frame_base = decode_location_expression (block_start,
1507 pointer_size,
1508 uvalue,
1509 cu_offset);
1510 printf (")");
1511 if (need_frame_base && !have_frame_base)
1512 printf (_(" [without DW_AT_frame_base]"));
1514 break;
1516 case DW_AT_import:
1518 if (form == DW_FORM_ref1
1519 || form == DW_FORM_ref2
1520 || form == DW_FORM_ref4)
1521 uvalue += cu_offset;
1523 if (uvalue >= section->size)
1524 warn (_("Offset %lx used as value for DW_AT_import attribute of DIE at offset %lx is too big.\n"),
1525 uvalue, (long int)(orig_data - section->start));
1526 else
1528 unsigned long abbrev_number;
1529 abbrev_entry * entry;
1531 abbrev_number = read_leb128 (section->start + uvalue, NULL, 0);
1533 printf ("[Abbrev Number: %ld", abbrev_number);
1534 for (entry = first_abbrev; entry != NULL; entry = entry->next)
1535 if (entry->entry == abbrev_number)
1536 break;
1537 if (entry != NULL)
1538 printf (" (%s)", get_TAG_name (entry->tag));
1539 printf ("]");
1542 break;
1544 default:
1545 break;
1548 return data;
1551 static char *
1552 get_AT_name (unsigned long attribute)
1554 switch (attribute)
1556 case DW_AT_sibling: return "DW_AT_sibling";
1557 case DW_AT_location: return "DW_AT_location";
1558 case DW_AT_name: return "DW_AT_name";
1559 case DW_AT_ordering: return "DW_AT_ordering";
1560 case DW_AT_subscr_data: return "DW_AT_subscr_data";
1561 case DW_AT_byte_size: return "DW_AT_byte_size";
1562 case DW_AT_bit_offset: return "DW_AT_bit_offset";
1563 case DW_AT_bit_size: return "DW_AT_bit_size";
1564 case DW_AT_element_list: return "DW_AT_element_list";
1565 case DW_AT_stmt_list: return "DW_AT_stmt_list";
1566 case DW_AT_low_pc: return "DW_AT_low_pc";
1567 case DW_AT_high_pc: return "DW_AT_high_pc";
1568 case DW_AT_language: return "DW_AT_language";
1569 case DW_AT_member: return "DW_AT_member";
1570 case DW_AT_discr: return "DW_AT_discr";
1571 case DW_AT_discr_value: return "DW_AT_discr_value";
1572 case DW_AT_visibility: return "DW_AT_visibility";
1573 case DW_AT_import: return "DW_AT_import";
1574 case DW_AT_string_length: return "DW_AT_string_length";
1575 case DW_AT_common_reference: return "DW_AT_common_reference";
1576 case DW_AT_comp_dir: return "DW_AT_comp_dir";
1577 case DW_AT_const_value: return "DW_AT_const_value";
1578 case DW_AT_containing_type: return "DW_AT_containing_type";
1579 case DW_AT_default_value: return "DW_AT_default_value";
1580 case DW_AT_inline: return "DW_AT_inline";
1581 case DW_AT_is_optional: return "DW_AT_is_optional";
1582 case DW_AT_lower_bound: return "DW_AT_lower_bound";
1583 case DW_AT_producer: return "DW_AT_producer";
1584 case DW_AT_prototyped: return "DW_AT_prototyped";
1585 case DW_AT_return_addr: return "DW_AT_return_addr";
1586 case DW_AT_start_scope: return "DW_AT_start_scope";
1587 case DW_AT_stride_size: return "DW_AT_stride_size";
1588 case DW_AT_upper_bound: return "DW_AT_upper_bound";
1589 case DW_AT_abstract_origin: return "DW_AT_abstract_origin";
1590 case DW_AT_accessibility: return "DW_AT_accessibility";
1591 case DW_AT_address_class: return "DW_AT_address_class";
1592 case DW_AT_artificial: return "DW_AT_artificial";
1593 case DW_AT_base_types: return "DW_AT_base_types";
1594 case DW_AT_calling_convention: return "DW_AT_calling_convention";
1595 case DW_AT_count: return "DW_AT_count";
1596 case DW_AT_data_member_location: return "DW_AT_data_member_location";
1597 case DW_AT_decl_column: return "DW_AT_decl_column";
1598 case DW_AT_decl_file: return "DW_AT_decl_file";
1599 case DW_AT_decl_line: return "DW_AT_decl_line";
1600 case DW_AT_declaration: return "DW_AT_declaration";
1601 case DW_AT_discr_list: return "DW_AT_discr_list";
1602 case DW_AT_encoding: return "DW_AT_encoding";
1603 case DW_AT_external: return "DW_AT_external";
1604 case DW_AT_frame_base: return "DW_AT_frame_base";
1605 case DW_AT_friend: return "DW_AT_friend";
1606 case DW_AT_identifier_case: return "DW_AT_identifier_case";
1607 case DW_AT_macro_info: return "DW_AT_macro_info";
1608 case DW_AT_namelist_items: return "DW_AT_namelist_items";
1609 case DW_AT_priority: return "DW_AT_priority";
1610 case DW_AT_segment: return "DW_AT_segment";
1611 case DW_AT_specification: return "DW_AT_specification";
1612 case DW_AT_static_link: return "DW_AT_static_link";
1613 case DW_AT_type: return "DW_AT_type";
1614 case DW_AT_use_location: return "DW_AT_use_location";
1615 case DW_AT_variable_parameter: return "DW_AT_variable_parameter";
1616 case DW_AT_virtuality: return "DW_AT_virtuality";
1617 case DW_AT_vtable_elem_location: return "DW_AT_vtable_elem_location";
1618 /* DWARF 2.1 values. */
1619 case DW_AT_allocated: return "DW_AT_allocated";
1620 case DW_AT_associated: return "DW_AT_associated";
1621 case DW_AT_data_location: return "DW_AT_data_location";
1622 case DW_AT_stride: return "DW_AT_stride";
1623 case DW_AT_entry_pc: return "DW_AT_entry_pc";
1624 case DW_AT_use_UTF8: return "DW_AT_use_UTF8";
1625 case DW_AT_extension: return "DW_AT_extension";
1626 case DW_AT_ranges: return "DW_AT_ranges";
1627 case DW_AT_trampoline: return "DW_AT_trampoline";
1628 case DW_AT_call_column: return "DW_AT_call_column";
1629 case DW_AT_call_file: return "DW_AT_call_file";
1630 case DW_AT_call_line: return "DW_AT_call_line";
1631 case DW_AT_description: return "DW_AT_description";
1632 case DW_AT_binary_scale: return "DW_AT_binary_scale";
1633 case DW_AT_decimal_scale: return "DW_AT_decimal_scale";
1634 case DW_AT_small: return "DW_AT_small";
1635 case DW_AT_decimal_sign: return "DW_AT_decimal_sign";
1636 case DW_AT_digit_count: return "DW_AT_digit_count";
1637 case DW_AT_picture_string: return "DW_AT_picture_string";
1638 case DW_AT_mutable: return "DW_AT_mutable";
1639 case DW_AT_threads_scaled: return "DW_AT_threads_scaled";
1640 case DW_AT_explicit: return "DW_AT_explicit";
1641 case DW_AT_object_pointer: return "DW_AT_object_pointer";
1642 case DW_AT_endianity: return "DW_AT_endianity";
1643 case DW_AT_elemental: return "DW_AT_elemental";
1644 case DW_AT_pure: return "DW_AT_pure";
1645 case DW_AT_recursive: return "DW_AT_recursive";
1647 /* HP and SGI/MIPS extensions. */
1648 case DW_AT_MIPS_loop_begin: return "DW_AT_MIPS_loop_begin";
1649 case DW_AT_MIPS_tail_loop_begin: return "DW_AT_MIPS_tail_loop_begin";
1650 case DW_AT_MIPS_epilog_begin: return "DW_AT_MIPS_epilog_begin";
1651 case DW_AT_MIPS_loop_unroll_factor: return "DW_AT_MIPS_loop_unroll_factor";
1652 case DW_AT_MIPS_software_pipeline_depth: return "DW_AT_MIPS_software_pipeline_depth";
1653 case DW_AT_MIPS_linkage_name: return "DW_AT_MIPS_linkage_name";
1654 case DW_AT_MIPS_stride: return "DW_AT_MIPS_stride";
1655 case DW_AT_MIPS_abstract_name: return "DW_AT_MIPS_abstract_name";
1656 case DW_AT_MIPS_clone_origin: return "DW_AT_MIPS_clone_origin";
1657 case DW_AT_MIPS_has_inlines: return "DW_AT_MIPS_has_inlines";
1659 /* HP Extensions. */
1660 case DW_AT_HP_block_index: return "DW_AT_HP_block_index";
1661 case DW_AT_HP_actuals_stmt_list: return "DW_AT_HP_actuals_stmt_list";
1662 case DW_AT_HP_proc_per_section: return "DW_AT_HP_proc_per_section";
1663 case DW_AT_HP_raw_data_ptr: return "DW_AT_HP_raw_data_ptr";
1664 case DW_AT_HP_pass_by_reference: return "DW_AT_HP_pass_by_reference";
1665 case DW_AT_HP_opt_level: return "DW_AT_HP_opt_level";
1666 case DW_AT_HP_prof_version_id: return "DW_AT_HP_prof_version_id";
1667 case DW_AT_HP_opt_flags: return "DW_AT_HP_opt_flags";
1668 case DW_AT_HP_cold_region_low_pc: return "DW_AT_HP_cold_region_low_pc";
1669 case DW_AT_HP_cold_region_high_pc: return "DW_AT_HP_cold_region_high_pc";
1670 case DW_AT_HP_all_variables_modifiable: return "DW_AT_HP_all_variables_modifiable";
1671 case DW_AT_HP_linkage_name: return "DW_AT_HP_linkage_name";
1672 case DW_AT_HP_prof_flags: return "DW_AT_HP_prof_flags";
1674 /* One value is shared by the MIPS and HP extensions: */
1675 case DW_AT_MIPS_fde: return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1677 /* GNU extensions. */
1678 case DW_AT_sf_names: return "DW_AT_sf_names";
1679 case DW_AT_src_info: return "DW_AT_src_info";
1680 case DW_AT_mac_info: return "DW_AT_mac_info";
1681 case DW_AT_src_coords: return "DW_AT_src_coords";
1682 case DW_AT_body_begin: return "DW_AT_body_begin";
1683 case DW_AT_body_end: return "DW_AT_body_end";
1684 case DW_AT_GNU_vector: return "DW_AT_GNU_vector";
1686 /* UPC extension. */
1687 case DW_AT_upc_threads_scaled: return "DW_AT_upc_threads_scaled";
1689 /* PGI (STMicroelectronics) extensions. */
1690 case DW_AT_PGI_lbase: return "DW_AT_PGI_lbase";
1691 case DW_AT_PGI_soffset: return "DW_AT_PGI_soffset";
1692 case DW_AT_PGI_lstride: return "DW_AT_PGI_lstride";
1694 default:
1696 static char buffer[100];
1698 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1699 attribute);
1700 return buffer;
1705 static unsigned char *
1706 read_and_display_attr (unsigned long attribute,
1707 unsigned long form,
1708 unsigned char * data,
1709 unsigned long cu_offset,
1710 unsigned long pointer_size,
1711 unsigned long offset_size,
1712 int dwarf_version,
1713 debug_info * debug_info_p,
1714 int do_loc,
1715 struct dwarf_section * section)
1717 if (!do_loc)
1718 printf (" %-18s:", get_AT_name (attribute));
1719 data = read_and_display_attr_value (attribute, form, data, cu_offset,
1720 pointer_size, offset_size,
1721 dwarf_version, debug_info_p,
1722 do_loc, section);
1723 if (!do_loc)
1724 printf ("\n");
1725 return data;
1729 /* Process the contents of a .debug_info section. If do_loc is non-zero
1730 then we are scanning for location lists and we do not want to display
1731 anything to the user. */
1733 static int
1734 process_debug_info (struct dwarf_section *section,
1735 void *file,
1736 int do_loc)
1738 unsigned char *start = section->start;
1739 unsigned char *end = start + section->size;
1740 unsigned char *section_begin;
1741 unsigned int unit;
1742 unsigned int num_units = 0;
1744 if ((do_loc || do_debug_loc || do_debug_ranges)
1745 && num_debug_info_entries == 0)
1747 unsigned long length;
1749 /* First scan the section to get the number of comp units. */
1750 for (section_begin = start, num_units = 0; section_begin < end;
1751 num_units ++)
1753 /* Read the first 4 bytes. For a 32-bit DWARF section, this
1754 will be the length. For a 64-bit DWARF section, it'll be
1755 the escape code 0xffffffff followed by an 8 byte length. */
1756 length = byte_get (section_begin, 4);
1758 if (length == 0xffffffff)
1760 length = byte_get (section_begin + 4, 8);
1761 section_begin += length + 12;
1763 else if (length >= 0xfffffff0 && length < 0xffffffff)
1765 warn (_("Reserved length value (%lx) found in section %s\n"), length, section->name);
1766 return 0;
1768 else
1769 section_begin += length + 4;
1771 /* Negative values are illegal, they may even cause infinite
1772 looping. This can happen if we can't accurately apply
1773 relocations to an object file. */
1774 if ((signed long) length <= 0)
1776 warn (_("Corrupt unit length (%lx) found in section %s\n"), length, section->name);
1777 return 0;
1781 if (num_units == 0)
1783 error (_("No comp units in %s section ?"), section->name);
1784 return 0;
1787 /* Then allocate an array to hold the information. */
1788 debug_information = cmalloc (num_units,
1789 sizeof (* debug_information));
1790 if (debug_information == NULL)
1792 error (_("Not enough memory for a debug info array of %u entries"),
1793 num_units);
1794 return 0;
1798 if (!do_loc)
1800 printf (_("The section %s contains:\n\n"), section->name);
1802 load_debug_section (str, file);
1805 load_debug_section (abbrev, file);
1806 if (debug_displays [abbrev].section.start == NULL)
1808 warn (_("Unable to locate %s section!\n"),
1809 debug_displays [abbrev].section.name);
1810 return 0;
1813 for (section_begin = start, unit = 0; start < end; unit++)
1815 DWARF2_Internal_CompUnit compunit;
1816 unsigned char *hdrptr;
1817 unsigned char *cu_abbrev_offset_ptr;
1818 unsigned char *tags;
1819 int level;
1820 unsigned long cu_offset;
1821 int offset_size;
1822 int initial_length_size;
1824 hdrptr = start;
1826 compunit.cu_length = byte_get (hdrptr, 4);
1827 hdrptr += 4;
1829 if (compunit.cu_length == 0xffffffff)
1831 compunit.cu_length = byte_get (hdrptr, 8);
1832 hdrptr += 8;
1833 offset_size = 8;
1834 initial_length_size = 12;
1836 else
1838 offset_size = 4;
1839 initial_length_size = 4;
1842 compunit.cu_version = byte_get (hdrptr, 2);
1843 hdrptr += 2;
1845 cu_offset = start - section_begin;
1847 cu_abbrev_offset_ptr = hdrptr;
1848 compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
1849 hdrptr += offset_size;
1851 compunit.cu_pointer_size = byte_get (hdrptr, 1);
1852 hdrptr += 1;
1853 if ((do_loc || do_debug_loc || do_debug_ranges)
1854 && num_debug_info_entries == 0)
1856 debug_information [unit].cu_offset = cu_offset;
1857 debug_information [unit].pointer_size
1858 = compunit.cu_pointer_size;
1859 debug_information [unit].base_address = 0;
1860 debug_information [unit].loc_offsets = NULL;
1861 debug_information [unit].have_frame_base = NULL;
1862 debug_information [unit].max_loc_offsets = 0;
1863 debug_information [unit].num_loc_offsets = 0;
1864 debug_information [unit].range_lists = NULL;
1865 debug_information [unit].max_range_lists= 0;
1866 debug_information [unit].num_range_lists = 0;
1869 if (!do_loc)
1871 printf (_(" Compilation Unit @ offset 0x%lx:\n"), cu_offset);
1872 printf (_(" Length: 0x%lx (%s)\n"), compunit.cu_length,
1873 initial_length_size == 8 ? "64-bit" : "32-bit");
1874 printf (_(" Version: %d\n"), compunit.cu_version);
1875 printf (_(" Abbrev Offset: %ld\n"), compunit.cu_abbrev_offset);
1876 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
1879 if (cu_offset + compunit.cu_length + initial_length_size
1880 > section->size)
1882 warn (_("Debug info is corrupted, length of CU at %lx extends beyond end of section (length = %lx)\n"),
1883 cu_offset, compunit.cu_length);
1884 break;
1886 tags = hdrptr;
1887 start += compunit.cu_length + initial_length_size;
1889 if (compunit.cu_version != 2 && compunit.cu_version != 3)
1891 warn (_("CU at offset %lx contains corrupt or unsupported version number: %d.\n"),
1892 cu_offset, compunit.cu_version);
1893 continue;
1896 free_abbrevs ();
1898 /* Process the abbrevs used by this compilation unit. DWARF
1899 sections under Mach-O have non-zero addresses. */
1900 if (compunit.cu_abbrev_offset >= debug_displays [abbrev].section.size)
1901 warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
1902 (unsigned long) compunit.cu_abbrev_offset,
1903 (unsigned long) debug_displays [abbrev].section.size);
1904 else
1905 process_abbrev_section
1906 ((unsigned char *) debug_displays [abbrev].section.start
1907 + compunit.cu_abbrev_offset - debug_displays [abbrev].section.address,
1908 (unsigned char *) debug_displays [abbrev].section.start
1909 + debug_displays [abbrev].section.size);
1911 level = 0;
1912 while (tags < start)
1914 unsigned int bytes_read;
1915 unsigned long abbrev_number;
1916 unsigned long die_offset;
1917 abbrev_entry *entry;
1918 abbrev_attr *attr;
1920 die_offset = tags - section_begin;
1922 abbrev_number = read_leb128 (tags, & bytes_read, 0);
1923 tags += bytes_read;
1925 /* A null DIE marks the end of a list of siblings. */
1926 if (abbrev_number == 0)
1928 --level;
1929 if (level < 0)
1931 static unsigned num_bogus_warns = 0;
1933 if (num_bogus_warns < 3)
1935 warn (_("Bogus end-of-siblings marker detected at offset %lx in .debug_info section\n"),
1936 die_offset);
1937 num_bogus_warns ++;
1938 if (num_bogus_warns == 3)
1939 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
1942 continue;
1945 if (!do_loc)
1946 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
1947 level, die_offset, abbrev_number);
1949 /* Scan through the abbreviation list until we reach the
1950 correct entry. */
1951 for (entry = first_abbrev;
1952 entry && entry->entry != abbrev_number;
1953 entry = entry->next)
1954 continue;
1956 if (entry == NULL)
1958 if (!do_loc)
1960 printf ("\n");
1961 fflush (stdout);
1963 warn (_("DIE at offset %lx refers to abbreviation number %lu which does not exist\n"),
1964 die_offset, abbrev_number);
1965 return 0;
1968 if (!do_loc)
1969 printf (_(" (%s)\n"), get_TAG_name (entry->tag));
1971 switch (entry->tag)
1973 default:
1974 need_base_address = 0;
1975 break;
1976 case DW_TAG_compile_unit:
1977 need_base_address = 1;
1978 break;
1979 case DW_TAG_entry_point:
1980 case DW_TAG_subprogram:
1981 need_base_address = 0;
1982 /* Assuming that there is no DW_AT_frame_base. */
1983 have_frame_base = 0;
1984 break;
1987 for (attr = entry->first_attr; attr; attr = attr->next)
1989 if (! do_loc)
1990 /* Show the offset from where the tag was extracted. */
1991 printf (" <%2lx>", (unsigned long)(tags - section_begin));
1993 tags = read_and_display_attr (attr->attribute,
1994 attr->form,
1995 tags, cu_offset,
1996 compunit.cu_pointer_size,
1997 offset_size,
1998 compunit.cu_version,
1999 debug_information + unit,
2000 do_loc, section);
2003 if (entry->children)
2004 ++level;
2008 /* Set num_debug_info_entries here so that it can be used to check if
2009 we need to process .debug_loc and .debug_ranges sections. */
2010 if ((do_loc || do_debug_loc || do_debug_ranges)
2011 && num_debug_info_entries == 0)
2012 num_debug_info_entries = num_units;
2014 if (!do_loc)
2016 printf ("\n");
2019 return 1;
2022 /* Locate and scan the .debug_info section in the file and record the pointer
2023 sizes and offsets for the compilation units in it. Usually an executable
2024 will have just one pointer size, but this is not guaranteed, and so we try
2025 not to make any assumptions. Returns zero upon failure, or the number of
2026 compilation units upon success. */
2028 static unsigned int
2029 load_debug_info (void * file)
2031 /* Reset the last pointer size so that we can issue correct error
2032 messages if we are displaying the contents of more than one section. */
2033 last_pointer_size = 0;
2034 warned_about_missing_comp_units = FALSE;
2036 /* If we have already tried and failed to load the .debug_info
2037 section then do not bother to repear the task. */
2038 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2039 return 0;
2041 /* If we already have the information there is nothing else to do. */
2042 if (num_debug_info_entries > 0)
2043 return num_debug_info_entries;
2045 if (load_debug_section (info, file)
2046 && process_debug_info (&debug_displays [info].section, file, 1))
2047 return num_debug_info_entries;
2049 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2050 return 0;
2053 static int
2054 display_debug_lines (struct dwarf_section *section, void *file)
2056 unsigned char *start = section->start;
2057 unsigned char *data = start;
2058 unsigned char *end = start + section->size;
2060 printf (_("\nDump of debug contents of section %s:\n\n"),
2061 section->name);
2063 if (load_debug_info (file) == 0)
2065 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
2066 section->name);
2067 return 0;
2070 while (data < end)
2072 DWARF2_Internal_LineInfo info;
2073 unsigned char *standard_opcodes;
2074 unsigned char *end_of_sequence;
2075 unsigned char *hdrptr;
2076 unsigned long hdroff;
2077 int initial_length_size;
2078 int offset_size;
2079 int i;
2081 hdrptr = data;
2082 hdroff = hdrptr - start;
2084 /* Check the length of the block. */
2085 info.li_length = byte_get (hdrptr, 4);
2086 hdrptr += 4;
2088 if (info.li_length == 0xffffffff)
2090 /* This section is 64-bit DWARF 3. */
2091 info.li_length = byte_get (hdrptr, 8);
2092 hdrptr += 8;
2093 offset_size = 8;
2094 initial_length_size = 12;
2096 else
2098 offset_size = 4;
2099 initial_length_size = 4;
2102 if (info.li_length + initial_length_size > section->size)
2104 warn
2105 (_("The line info appears to be corrupt - the section is too small\n"));
2106 return 0;
2109 /* Check its version number. */
2110 info.li_version = byte_get (hdrptr, 2);
2111 hdrptr += 2;
2112 if (info.li_version != 2 && info.li_version != 3)
2114 warn (_("Only DWARF version 2 and 3 line info is currently supported.\n"));
2115 return 0;
2118 info.li_prologue_length = byte_get (hdrptr, offset_size);
2119 hdrptr += offset_size;
2120 info.li_min_insn_length = byte_get (hdrptr, 1);
2121 hdrptr++;
2122 info.li_default_is_stmt = byte_get (hdrptr, 1);
2123 hdrptr++;
2124 info.li_line_base = byte_get (hdrptr, 1);
2125 hdrptr++;
2126 info.li_line_range = byte_get (hdrptr, 1);
2127 hdrptr++;
2128 info.li_opcode_base = byte_get (hdrptr, 1);
2129 hdrptr++;
2131 /* Sign extend the line base field. */
2132 info.li_line_base <<= 24;
2133 info.li_line_base >>= 24;
2135 printf (_(" Offset: 0x%lx\n"), hdroff);
2136 printf (_(" Length: %ld\n"), info.li_length);
2137 printf (_(" DWARF Version: %d\n"), info.li_version);
2138 printf (_(" Prologue Length: %d\n"), info.li_prologue_length);
2139 printf (_(" Minimum Instruction Length: %d\n"), info.li_min_insn_length);
2140 printf (_(" Initial value of 'is_stmt': %d\n"), info.li_default_is_stmt);
2141 printf (_(" Line Base: %d\n"), info.li_line_base);
2142 printf (_(" Line Range: %d\n"), info.li_line_range);
2143 printf (_(" Opcode Base: %d\n"), info.li_opcode_base);
2145 end_of_sequence = data + info.li_length + initial_length_size;
2147 reset_state_machine (info.li_default_is_stmt);
2149 /* Display the contents of the Opcodes table. */
2150 standard_opcodes = hdrptr;
2152 printf (_("\n Opcodes:\n"));
2154 for (i = 1; i < info.li_opcode_base; i++)
2155 printf (_(" Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
2157 /* Display the contents of the Directory table. */
2158 data = standard_opcodes + info.li_opcode_base - 1;
2160 if (*data == 0)
2161 printf (_("\n The Directory Table is empty.\n"));
2162 else
2164 printf (_("\n The Directory Table:\n"));
2166 while (*data != 0)
2168 printf (_(" %s\n"), data);
2170 data += strlen ((char *) data) + 1;
2174 /* Skip the NUL at the end of the table. */
2175 data++;
2177 /* Display the contents of the File Name table. */
2178 if (*data == 0)
2179 printf (_("\n The File Name Table is empty.\n"));
2180 else
2182 printf (_("\n The File Name Table:\n"));
2183 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
2185 while (*data != 0)
2187 unsigned char *name;
2188 unsigned int bytes_read;
2190 printf (_(" %d\t"), ++state_machine_regs.last_file_entry);
2191 name = data;
2193 data += strlen ((char *) data) + 1;
2195 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
2196 data += bytes_read;
2197 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
2198 data += bytes_read;
2199 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
2200 data += bytes_read;
2201 printf (_("%s\n"), name);
2205 /* Skip the NUL at the end of the table. */
2206 data++;
2208 /* Now display the statements. */
2209 printf (_("\n Line Number Statements:\n"));
2211 while (data < end_of_sequence)
2213 unsigned char op_code;
2214 int adv;
2215 unsigned long int uladv;
2216 unsigned int bytes_read;
2218 op_code = *data++;
2220 if (op_code >= info.li_opcode_base)
2222 op_code -= info.li_opcode_base;
2223 uladv = (op_code / info.li_line_range) * info.li_min_insn_length;
2224 state_machine_regs.address += uladv;
2225 printf (_(" Special opcode %d: advance Address by %lu to 0x%lx"),
2226 op_code, uladv, state_machine_regs.address);
2227 adv = (op_code % info.li_line_range) + info.li_line_base;
2228 state_machine_regs.line += adv;
2229 printf (_(" and Line by %d to %d\n"),
2230 adv, state_machine_regs.line);
2232 else switch (op_code)
2234 case DW_LNS_extended_op:
2235 data += process_extended_line_op (data, info.li_default_is_stmt);
2236 break;
2238 case DW_LNS_copy:
2239 printf (_(" Copy\n"));
2240 break;
2242 case DW_LNS_advance_pc:
2243 uladv = read_leb128 (data, & bytes_read, 0);
2244 uladv *= info.li_min_insn_length;
2245 data += bytes_read;
2246 state_machine_regs.address += uladv;
2247 printf (_(" Advance PC by %lu to 0x%lx\n"), uladv,
2248 state_machine_regs.address);
2249 break;
2251 case DW_LNS_advance_line:
2252 adv = read_leb128 (data, & bytes_read, 1);
2253 data += bytes_read;
2254 state_machine_regs.line += adv;
2255 printf (_(" Advance Line by %d to %d\n"), adv,
2256 state_machine_regs.line);
2257 break;
2259 case DW_LNS_set_file:
2260 adv = read_leb128 (data, & bytes_read, 0);
2261 data += bytes_read;
2262 printf (_(" Set File Name to entry %d in the File Name Table\n"),
2263 adv);
2264 state_machine_regs.file = adv;
2265 break;
2267 case DW_LNS_set_column:
2268 uladv = read_leb128 (data, & bytes_read, 0);
2269 data += bytes_read;
2270 printf (_(" Set column to %lu\n"), uladv);
2271 state_machine_regs.column = uladv;
2272 break;
2274 case DW_LNS_negate_stmt:
2275 adv = state_machine_regs.is_stmt;
2276 adv = ! adv;
2277 printf (_(" Set is_stmt to %d\n"), adv);
2278 state_machine_regs.is_stmt = adv;
2279 break;
2281 case DW_LNS_set_basic_block:
2282 printf (_(" Set basic block\n"));
2283 state_machine_regs.basic_block = 1;
2284 break;
2286 case DW_LNS_const_add_pc:
2287 uladv = (((255 - info.li_opcode_base) / info.li_line_range)
2288 * info.li_min_insn_length);
2289 state_machine_regs.address += uladv;
2290 printf (_(" Advance PC by constant %lu to 0x%lx\n"), uladv,
2291 state_machine_regs.address);
2292 break;
2294 case DW_LNS_fixed_advance_pc:
2295 uladv = byte_get (data, 2);
2296 data += 2;
2297 state_machine_regs.address += uladv;
2298 printf (_(" Advance PC by fixed size amount %lu to 0x%lx\n"),
2299 uladv, state_machine_regs.address);
2300 break;
2302 case DW_LNS_set_prologue_end:
2303 printf (_(" Set prologue_end to true\n"));
2304 break;
2306 case DW_LNS_set_epilogue_begin:
2307 printf (_(" Set epilogue_begin to true\n"));
2308 break;
2310 case DW_LNS_set_isa:
2311 uladv = read_leb128 (data, & bytes_read, 0);
2312 data += bytes_read;
2313 printf (_(" Set ISA to %lu\n"), uladv);
2314 break;
2316 default:
2317 printf (_(" Unknown opcode %d with operands: "), op_code);
2319 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
2321 printf ("0x%lx%s", read_leb128 (data, &bytes_read, 0),
2322 i == 1 ? "" : ", ");
2323 data += bytes_read;
2325 putchar ('\n');
2326 break;
2329 putchar ('\n');
2332 return 1;
2335 static debug_info *
2336 find_debug_info_for_offset (unsigned long offset)
2338 unsigned int i;
2340 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2341 return NULL;
2343 for (i = 0; i < num_debug_info_entries; i++)
2344 if (debug_information[i].cu_offset == offset)
2345 return debug_information + i;
2347 return NULL;
2350 static int
2351 display_debug_pubnames (struct dwarf_section *section,
2352 void *file ATTRIBUTE_UNUSED)
2354 DWARF2_Internal_PubNames pubnames;
2355 unsigned char *start = section->start;
2356 unsigned char *end = start + section->size;
2358 /* It does not matter if this load fails,
2359 we test for that later on. */
2360 load_debug_info (file);
2362 printf (_("Contents of the %s section:\n\n"), section->name);
2364 while (start < end)
2366 unsigned char *data;
2367 unsigned long offset;
2368 int offset_size, initial_length_size;
2370 data = start;
2372 pubnames.pn_length = byte_get (data, 4);
2373 data += 4;
2374 if (pubnames.pn_length == 0xffffffff)
2376 pubnames.pn_length = byte_get (data, 8);
2377 data += 8;
2378 offset_size = 8;
2379 initial_length_size = 12;
2381 else
2383 offset_size = 4;
2384 initial_length_size = 4;
2387 pubnames.pn_version = byte_get (data, 2);
2388 data += 2;
2390 pubnames.pn_offset = byte_get (data, offset_size);
2391 data += offset_size;
2393 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
2394 && num_debug_info_entries > 0
2395 && find_debug_info_for_offset (pubnames.pn_offset) == NULL)
2396 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
2397 pubnames.pn_offset, section->name);
2399 pubnames.pn_size = byte_get (data, offset_size);
2400 data += offset_size;
2402 start += pubnames.pn_length + initial_length_size;
2404 if (pubnames.pn_version != 2 && pubnames.pn_version != 3)
2406 static int warned = 0;
2408 if (! warned)
2410 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
2411 warned = 1;
2414 continue;
2417 printf (_(" Length: %ld\n"),
2418 pubnames.pn_length);
2419 printf (_(" Version: %d\n"),
2420 pubnames.pn_version);
2421 printf (_(" Offset into .debug_info section: 0x%lx\n"),
2422 pubnames.pn_offset);
2423 printf (_(" Size of area in .debug_info section: %ld\n"),
2424 pubnames.pn_size);
2426 printf (_("\n Offset\tName\n"));
2430 offset = byte_get (data, offset_size);
2432 if (offset != 0)
2434 data += offset_size;
2435 printf (" %-6ld\t\t%s\n", offset, data);
2436 data += strlen ((char *) data) + 1;
2439 while (offset != 0);
2442 printf ("\n");
2443 return 1;
2446 static int
2447 display_debug_macinfo (struct dwarf_section *section,
2448 void *file ATTRIBUTE_UNUSED)
2450 unsigned char *start = section->start;
2451 unsigned char *end = start + section->size;
2452 unsigned char *curr = start;
2453 unsigned int bytes_read;
2454 enum dwarf_macinfo_record_type op;
2456 printf (_("Contents of the %s section:\n\n"), section->name);
2458 while (curr < end)
2460 unsigned int lineno;
2461 const char *string;
2463 op = *curr;
2464 curr++;
2466 switch (op)
2468 case DW_MACINFO_start_file:
2470 unsigned int filenum;
2472 lineno = read_leb128 (curr, & bytes_read, 0);
2473 curr += bytes_read;
2474 filenum = read_leb128 (curr, & bytes_read, 0);
2475 curr += bytes_read;
2477 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
2478 lineno, filenum);
2480 break;
2482 case DW_MACINFO_end_file:
2483 printf (_(" DW_MACINFO_end_file\n"));
2484 break;
2486 case DW_MACINFO_define:
2487 lineno = read_leb128 (curr, & bytes_read, 0);
2488 curr += bytes_read;
2489 string = (char *) curr;
2490 curr += strlen (string) + 1;
2491 printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
2492 lineno, string);
2493 break;
2495 case DW_MACINFO_undef:
2496 lineno = read_leb128 (curr, & bytes_read, 0);
2497 curr += bytes_read;
2498 string = (char *) curr;
2499 curr += strlen (string) + 1;
2500 printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
2501 lineno, string);
2502 break;
2504 case DW_MACINFO_vendor_ext:
2506 unsigned int constant;
2508 constant = read_leb128 (curr, & bytes_read, 0);
2509 curr += bytes_read;
2510 string = (char *) curr;
2511 curr += strlen (string) + 1;
2512 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
2513 constant, string);
2515 break;
2519 return 1;
2522 static int
2523 display_debug_abbrev (struct dwarf_section *section,
2524 void *file ATTRIBUTE_UNUSED)
2526 abbrev_entry *entry;
2527 unsigned char *start = section->start;
2528 unsigned char *end = start + section->size;
2530 printf (_("Contents of the %s section:\n\n"), section->name);
2534 free_abbrevs ();
2536 start = process_abbrev_section (start, end);
2538 if (first_abbrev == NULL)
2539 continue;
2541 printf (_(" Number TAG\n"));
2543 for (entry = first_abbrev; entry; entry = entry->next)
2545 abbrev_attr *attr;
2547 printf (_(" %ld %s [%s]\n"),
2548 entry->entry,
2549 get_TAG_name (entry->tag),
2550 entry->children ? _("has children") : _("no children"));
2552 for (attr = entry->first_attr; attr; attr = attr->next)
2553 printf (_(" %-18s %s\n"),
2554 get_AT_name (attr->attribute),
2555 get_FORM_name (attr->form));
2558 while (start);
2560 printf ("\n");
2562 return 1;
2565 static int
2566 display_debug_loc (struct dwarf_section *section, void *file)
2568 unsigned char *start = section->start;
2569 unsigned char *section_end;
2570 unsigned long bytes;
2571 unsigned char *section_begin = start;
2572 unsigned int num_loc_list = 0;
2573 unsigned long last_offset = 0;
2574 unsigned int first = 0;
2575 unsigned int i;
2576 unsigned int j;
2577 int seen_first_offset = 0;
2578 int use_debug_info = 1;
2579 unsigned char *next;
2581 bytes = section->size;
2582 section_end = start + bytes;
2584 if (bytes == 0)
2586 printf (_("\nThe %s section is empty.\n"), section->name);
2587 return 0;
2590 if (load_debug_info (file) == 0)
2592 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
2593 section->name);
2594 return 0;
2597 /* Check the order of location list in .debug_info section. If
2598 offsets of location lists are in the ascending order, we can
2599 use `debug_information' directly. */
2600 for (i = 0; i < num_debug_info_entries; i++)
2602 unsigned int num;
2604 num = debug_information [i].num_loc_offsets;
2605 num_loc_list += num;
2607 /* Check if we can use `debug_information' directly. */
2608 if (use_debug_info && num != 0)
2610 if (!seen_first_offset)
2612 /* This is the first location list. */
2613 last_offset = debug_information [i].loc_offsets [0];
2614 first = i;
2615 seen_first_offset = 1;
2616 j = 1;
2618 else
2619 j = 0;
2621 for (; j < num; j++)
2623 if (last_offset >
2624 debug_information [i].loc_offsets [j])
2626 use_debug_info = 0;
2627 break;
2629 last_offset = debug_information [i].loc_offsets [j];
2634 if (!use_debug_info)
2635 /* FIXME: Should we handle this case? */
2636 error (_("Location lists in .debug_info section aren't in ascending order!\n"));
2638 if (!seen_first_offset)
2639 error (_("No location lists in .debug_info section!\n"));
2641 /* DWARF sections under Mach-O have non-zero addresses. */
2642 if (debug_information [first].num_loc_offsets > 0
2643 && debug_information [first].loc_offsets [0] != section->address)
2644 warn (_("Location lists in %s section start at 0x%lx\n"),
2645 section->name, debug_information [first].loc_offsets [0]);
2647 printf (_("Contents of the %s section:\n\n"), section->name);
2648 printf (_(" Offset Begin End Expression\n"));
2650 seen_first_offset = 0;
2651 for (i = first; i < num_debug_info_entries; i++)
2653 dwarf_vma begin;
2654 dwarf_vma end;
2655 unsigned short length;
2656 unsigned long offset;
2657 unsigned int pointer_size;
2658 unsigned long cu_offset;
2659 unsigned long base_address;
2660 int need_frame_base;
2661 int has_frame_base;
2663 pointer_size = debug_information [i].pointer_size;
2664 cu_offset = debug_information [i].cu_offset;
2666 for (j = 0; j < debug_information [i].num_loc_offsets; j++)
2668 has_frame_base = debug_information [i].have_frame_base [j];
2669 /* DWARF sections under Mach-O have non-zero addresses. */
2670 offset = debug_information [i].loc_offsets [j] - section->address;
2671 next = section_begin + offset;
2672 base_address = debug_information [i].base_address;
2674 if (!seen_first_offset)
2675 seen_first_offset = 1;
2676 else
2678 if (start < next)
2679 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
2680 (long)(start - section_begin), (long)(next - section_begin));
2681 else if (start > next)
2682 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
2683 (long)(start - section_begin), (long)(next - section_begin));
2685 start = next;
2687 if (offset >= bytes)
2689 warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
2690 offset);
2691 continue;
2694 while (1)
2696 if (start + 2 * pointer_size > section_end)
2698 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2699 offset);
2700 break;
2703 /* Note: we use sign extension here in order to be sure that
2704 we can detect the -1 escape value. Sign extension into the
2705 top 32 bits of a 32-bit address will not affect the values
2706 that we display since we always show hex values, and always
2707 the bottom 32-bits. */
2708 begin = byte_get_signed (start, pointer_size);
2709 start += pointer_size;
2710 end = byte_get_signed (start, pointer_size);
2711 start += pointer_size;
2713 printf (" %8.8lx ", offset);
2715 if (begin == 0 && end == 0)
2717 printf (_("<End of list>\n"));
2718 break;
2721 /* Check base address specifiers. */
2722 if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
2724 base_address = end;
2725 print_dwarf_vma (begin, pointer_size);
2726 print_dwarf_vma (end, pointer_size);
2727 printf (_("(base address)\n"));
2728 continue;
2731 if (start + 2 > section_end)
2733 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2734 offset);
2735 break;
2738 length = byte_get (start, 2);
2739 start += 2;
2741 if (start + length > section_end)
2743 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
2744 offset);
2745 break;
2748 print_dwarf_vma (begin + base_address, pointer_size);
2749 print_dwarf_vma (end + base_address, pointer_size);
2751 putchar ('(');
2752 need_frame_base = decode_location_expression (start,
2753 pointer_size,
2754 length,
2755 cu_offset);
2756 putchar (')');
2758 if (need_frame_base && !has_frame_base)
2759 printf (_(" [without DW_AT_frame_base]"));
2761 if (begin == end)
2762 fputs (_(" (start == end)"), stdout);
2763 else if (begin > end)
2764 fputs (_(" (start > end)"), stdout);
2766 putchar ('\n');
2768 start += length;
2773 if (start < section_end)
2774 warn (_("There are %ld unused bytes at the end of section %s\n"),
2775 (long) (section_end - start), section->name);
2776 return 1;
2779 static int
2780 display_debug_str (struct dwarf_section *section,
2781 void *file ATTRIBUTE_UNUSED)
2783 unsigned char *start = section->start;
2784 unsigned long bytes = section->size;
2785 dwarf_vma addr = section->address;
2787 if (bytes == 0)
2789 printf (_("\nThe %s section is empty.\n"), section->name);
2790 return 0;
2793 printf (_("Contents of the %s section:\n\n"), section->name);
2795 while (bytes)
2797 int j;
2798 int k;
2799 int lbytes;
2801 lbytes = (bytes > 16 ? 16 : bytes);
2803 printf (" 0x%8.8lx ", (unsigned long) addr);
2805 for (j = 0; j < 16; j++)
2807 if (j < lbytes)
2808 printf ("%2.2x", start[j]);
2809 else
2810 printf (" ");
2812 if ((j & 3) == 3)
2813 printf (" ");
2816 for (j = 0; j < lbytes; j++)
2818 k = start[j];
2819 if (k >= ' ' && k < 0x80)
2820 printf ("%c", k);
2821 else
2822 printf (".");
2825 putchar ('\n');
2827 start += lbytes;
2828 addr += lbytes;
2829 bytes -= lbytes;
2832 putchar ('\n');
2834 return 1;
2837 static int
2838 display_debug_info (struct dwarf_section *section, void *file)
2840 return process_debug_info (section, file, 0);
2844 static int
2845 display_debug_aranges (struct dwarf_section *section,
2846 void *file ATTRIBUTE_UNUSED)
2848 unsigned char *start = section->start;
2849 unsigned char *end = start + section->size;
2851 printf (_("The section %s contains:\n\n"), section->name);
2853 /* It does not matter if this load fails,
2854 we test for that later on. */
2855 load_debug_info (file);
2857 while (start < end)
2859 unsigned char *hdrptr;
2860 DWARF2_Internal_ARange arange;
2861 unsigned char *ranges;
2862 dwarf_vma length;
2863 dwarf_vma address;
2864 unsigned char address_size;
2865 int excess;
2866 int offset_size;
2867 int initial_length_size;
2869 hdrptr = start;
2871 arange.ar_length = byte_get (hdrptr, 4);
2872 hdrptr += 4;
2874 if (arange.ar_length == 0xffffffff)
2876 arange.ar_length = byte_get (hdrptr, 8);
2877 hdrptr += 8;
2878 offset_size = 8;
2879 initial_length_size = 12;
2881 else
2883 offset_size = 4;
2884 initial_length_size = 4;
2887 arange.ar_version = byte_get (hdrptr, 2);
2888 hdrptr += 2;
2890 arange.ar_info_offset = byte_get (hdrptr, offset_size);
2891 hdrptr += offset_size;
2893 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
2894 && num_debug_info_entries > 0
2895 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
2896 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
2897 arange.ar_info_offset, section->name);
2899 arange.ar_pointer_size = byte_get (hdrptr, 1);
2900 hdrptr += 1;
2902 arange.ar_segment_size = byte_get (hdrptr, 1);
2903 hdrptr += 1;
2905 if (arange.ar_version != 2 && arange.ar_version != 3)
2907 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
2908 break;
2911 printf (_(" Length: %ld\n"), arange.ar_length);
2912 printf (_(" Version: %d\n"), arange.ar_version);
2913 printf (_(" Offset into .debug_info: 0x%lx\n"), arange.ar_info_offset);
2914 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
2915 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
2917 address_size = arange.ar_pointer_size + arange.ar_segment_size;
2919 /* The DWARF spec does not require that the address size be a power
2920 of two, but we do. This will have to change if we ever encounter
2921 an uneven architecture. */
2922 if ((address_size & (address_size - 1)) != 0)
2924 warn (_("Pointer size + Segment size is not a power of two.\n"));
2925 break;
2928 if (address_size > 4)
2929 printf (_("\n Address Length\n"));
2930 else
2931 printf (_("\n Address Length\n"));
2933 ranges = hdrptr;
2935 /* Must pad to an alignment boundary that is twice the address size. */
2936 excess = (hdrptr - start) % (2 * address_size);
2937 if (excess)
2938 ranges += (2 * address_size) - excess;
2940 start += arange.ar_length + initial_length_size;
2942 while (ranges + 2 * address_size <= start)
2944 address = byte_get (ranges, address_size);
2946 ranges += address_size;
2948 length = byte_get (ranges, address_size);
2950 ranges += address_size;
2952 print_dwarf_vma (address, address_size);
2953 print_dwarf_vma (length, address_size);
2954 putchar ('\n');
2958 printf ("\n");
2960 return 1;
2963 static int
2964 display_debug_ranges (struct dwarf_section *section,
2965 void *file ATTRIBUTE_UNUSED)
2967 unsigned char *start = section->start;
2968 unsigned char *section_end;
2969 unsigned long bytes;
2970 unsigned char *section_begin = start;
2971 unsigned int num_range_list = 0;
2972 unsigned long last_offset = 0;
2973 unsigned int first = 0;
2974 unsigned int i;
2975 unsigned int j;
2976 int seen_first_offset = 0;
2977 int use_debug_info = 1;
2978 unsigned char *next;
2980 bytes = section->size;
2981 section_end = start + bytes;
2983 if (bytes == 0)
2985 printf (_("\nThe %s section is empty.\n"), section->name);
2986 return 0;
2989 if (load_debug_info (file) == 0)
2991 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
2992 section->name);
2993 return 0;
2996 /* Check the order of range list in .debug_info section. If
2997 offsets of range lists are in the ascending order, we can
2998 use `debug_information' directly. */
2999 for (i = 0; i < num_debug_info_entries; i++)
3001 unsigned int num;
3003 num = debug_information [i].num_range_lists;
3004 num_range_list += num;
3006 /* Check if we can use `debug_information' directly. */
3007 if (use_debug_info && num != 0)
3009 if (!seen_first_offset)
3011 /* This is the first range list. */
3012 last_offset = debug_information [i].range_lists [0];
3013 first = i;
3014 seen_first_offset = 1;
3015 j = 1;
3017 else
3018 j = 0;
3020 for (; j < num; j++)
3022 if (last_offset >
3023 debug_information [i].range_lists [j])
3025 use_debug_info = 0;
3026 break;
3028 last_offset = debug_information [i].range_lists [j];
3033 if (!use_debug_info)
3034 /* FIXME: Should we handle this case? */
3035 error (_("Range lists in .debug_info section aren't in ascending order!\n"));
3037 if (!seen_first_offset)
3038 error (_("No range lists in .debug_info section!\n"));
3040 /* DWARF sections under Mach-O have non-zero addresses. */
3041 if (debug_information [first].num_range_lists > 0
3042 && debug_information [first].range_lists [0] != section->address)
3043 warn (_("Range lists in %s section start at 0x%lx\n"),
3044 section->name, debug_information [first].range_lists [0]);
3046 printf (_("Contents of the %s section:\n\n"), section->name);
3047 printf (_(" Offset Begin End\n"));
3049 seen_first_offset = 0;
3050 for (i = first; i < num_debug_info_entries; i++)
3052 dwarf_vma begin;
3053 dwarf_vma end;
3054 unsigned long offset;
3055 unsigned int pointer_size;
3056 unsigned long base_address;
3058 pointer_size = debug_information [i].pointer_size;
3060 for (j = 0; j < debug_information [i].num_range_lists; j++)
3062 /* DWARF sections under Mach-O have non-zero addresses. */
3063 offset = debug_information [i].range_lists [j] - section->address;
3064 next = section_begin + offset;
3065 base_address = debug_information [i].base_address;
3067 if (!seen_first_offset)
3068 seen_first_offset = 1;
3069 else
3071 if (start < next)
3072 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
3073 (long)(start - section_begin),
3074 (long)(next - section_begin), section->name);
3075 else if (start > next)
3076 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
3077 (long)(start - section_begin),
3078 (long)(next - section_begin), section->name);
3080 start = next;
3082 while (1)
3084 /* Note: we use sign extension here in order to be sure that
3085 we can detect the -1 escape value. Sign extension into the
3086 top 32 bits of a 32-bit address will not affect the values
3087 that we display since we always show hex values, and always
3088 the bottom 32-bits. */
3089 begin = byte_get_signed (start, pointer_size);
3090 start += pointer_size;
3091 end = byte_get_signed (start, pointer_size);
3092 start += pointer_size;
3094 printf (" %8.8lx ", offset);
3096 if (begin == 0 && end == 0)
3098 printf (_("<End of list>\n"));
3099 break;
3102 print_dwarf_vma (begin, pointer_size);
3103 print_dwarf_vma (end, pointer_size);
3105 /* Check base address specifiers. */
3106 if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
3108 base_address = end;
3109 printf ("(base address)\n");
3110 continue;
3113 if (begin == end)
3114 fputs (_("(start == end)"), stdout);
3115 else if (begin > end)
3116 fputs (_("(start > end)"), stdout);
3118 putchar ('\n');
3122 putchar ('\n');
3123 return 1;
3126 typedef struct Frame_Chunk
3128 struct Frame_Chunk *next;
3129 unsigned char *chunk_start;
3130 int ncols;
3131 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
3132 short int *col_type;
3133 int *col_offset;
3134 char *augmentation;
3135 unsigned int code_factor;
3136 int data_factor;
3137 unsigned long pc_begin;
3138 unsigned long pc_range;
3139 int cfa_reg;
3140 int cfa_offset;
3141 int ra;
3142 unsigned char fde_encoding;
3143 unsigned char cfa_exp;
3145 Frame_Chunk;
3147 /* A marker for a col_type that means this column was never referenced
3148 in the frame info. */
3149 #define DW_CFA_unreferenced (-1)
3151 static void
3152 frame_need_space (Frame_Chunk *fc, int reg)
3154 int prev = fc->ncols;
3156 if (reg < fc->ncols)
3157 return;
3159 fc->ncols = reg + 1;
3160 fc->col_type = xcrealloc (fc->col_type, fc->ncols, sizeof (short int));
3161 fc->col_offset = xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
3163 while (prev < fc->ncols)
3165 fc->col_type[prev] = DW_CFA_unreferenced;
3166 fc->col_offset[prev] = 0;
3167 prev++;
3171 static const char *const dwarf_regnames_i386[] =
3173 "eax", "ecx", "edx", "ebx",
3174 "esp", "ebp", "esi", "edi",
3175 "eip", "eflags", NULL,
3176 "st0", "st1", "st2", "st3",
3177 "st4", "st5", "st6", "st7",
3178 NULL, NULL,
3179 "xmm0", "xmm1", "xmm2", "xmm3",
3180 "xmm4", "xmm5", "xmm6", "xmm7",
3181 "mm0", "mm1", "mm2", "mm3",
3182 "mm4", "mm5", "mm6", "mm7",
3183 "fcw", "fsw", "mxcsr",
3184 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
3185 "tr", "ldtr"
3188 static const char *const dwarf_regnames_x86_64[] =
3190 "rax", "rdx", "rcx", "rbx",
3191 "rsi", "rdi", "rbp", "rsp",
3192 "r8", "r9", "r10", "r11",
3193 "r12", "r13", "r14", "r15",
3194 "rip",
3195 "xmm0", "xmm1", "xmm2", "xmm3",
3196 "xmm4", "xmm5", "xmm6", "xmm7",
3197 "xmm8", "xmm9", "xmm10", "xmm11",
3198 "xmm12", "xmm13", "xmm14", "xmm15",
3199 "st0", "st1", "st2", "st3",
3200 "st4", "st5", "st6", "st7",
3201 "mm0", "mm1", "mm2", "mm3",
3202 "mm4", "mm5", "mm6", "mm7",
3203 "rflags",
3204 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
3205 "fs.base", "gs.base", NULL, NULL,
3206 "tr", "ldtr",
3207 "mxcsr", "fcw", "fsw"
3210 static const char *const *dwarf_regnames;
3211 static unsigned int dwarf_regnames_count;
3213 void
3214 init_dwarf_regnames (unsigned int e_machine)
3216 switch (e_machine)
3218 case EM_386:
3219 case EM_486:
3220 dwarf_regnames = dwarf_regnames_i386;
3221 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
3222 break;
3224 case EM_X86_64:
3225 dwarf_regnames = dwarf_regnames_x86_64;
3226 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
3227 break;
3229 default:
3230 break;
3234 static const char *
3235 regname (unsigned int regno, int row)
3237 static char reg[64];
3238 if (dwarf_regnames
3239 && regno < dwarf_regnames_count
3240 && dwarf_regnames [regno] != NULL)
3242 if (row)
3243 return dwarf_regnames [regno];
3244 snprintf (reg, sizeof (reg), "r%d (%s)", regno,
3245 dwarf_regnames [regno]);
3247 else
3248 snprintf (reg, sizeof (reg), "r%d", regno);
3249 return reg;
3252 static void
3253 frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
3255 int r;
3256 char tmp[100];
3258 if (*max_regs < fc->ncols)
3259 *max_regs = fc->ncols;
3261 if (*need_col_headers)
3263 static const char *loc = " LOC";
3265 *need_col_headers = 0;
3267 printf ("%-*s CFA ", eh_addr_size * 2, loc);
3269 for (r = 0; r < *max_regs; r++)
3270 if (fc->col_type[r] != DW_CFA_unreferenced)
3272 if (r == fc->ra)
3273 printf ("ra ");
3274 else
3275 printf ("%-5s ", regname (r, 1));
3278 printf ("\n");
3281 printf ("%0*lx ", eh_addr_size * 2, fc->pc_begin);
3282 if (fc->cfa_exp)
3283 strcpy (tmp, "exp");
3284 else
3285 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), fc->cfa_offset);
3286 printf ("%-8s ", tmp);
3288 for (r = 0; r < fc->ncols; r++)
3290 if (fc->col_type[r] != DW_CFA_unreferenced)
3292 switch (fc->col_type[r])
3294 case DW_CFA_undefined:
3295 strcpy (tmp, "u");
3296 break;
3297 case DW_CFA_same_value:
3298 strcpy (tmp, "s");
3299 break;
3300 case DW_CFA_offset:
3301 sprintf (tmp, "c%+d", fc->col_offset[r]);
3302 break;
3303 case DW_CFA_val_offset:
3304 sprintf (tmp, "v%+d", fc->col_offset[r]);
3305 break;
3306 case DW_CFA_register:
3307 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
3308 break;
3309 case DW_CFA_expression:
3310 strcpy (tmp, "exp");
3311 break;
3312 case DW_CFA_val_expression:
3313 strcpy (tmp, "vexp");
3314 break;
3315 default:
3316 strcpy (tmp, "n/a");
3317 break;
3319 printf ("%-5s ", tmp);
3322 printf ("\n");
3325 static int
3326 size_of_encoded_value (int encoding)
3328 switch (encoding & 0x7)
3330 default: /* ??? */
3331 case 0: return eh_addr_size;
3332 case 2: return 2;
3333 case 3: return 4;
3334 case 4: return 8;
3338 static dwarf_vma
3339 get_encoded_value (unsigned char *data, int encoding)
3341 int size = size_of_encoded_value (encoding);
3343 if (encoding & DW_EH_PE_signed)
3344 return byte_get_signed (data, size);
3345 else
3346 return byte_get (data, size);
3349 #define GET(N) byte_get (start, N); start += N
3350 #define LEB() read_leb128 (start, & length_return, 0); start += length_return
3351 #define SLEB() read_leb128 (start, & length_return, 1); start += length_return
3353 static int
3354 display_debug_frames (struct dwarf_section *section,
3355 void *file ATTRIBUTE_UNUSED)
3357 unsigned char *start = section->start;
3358 unsigned char *end = start + section->size;
3359 unsigned char *section_start = start;
3360 Frame_Chunk *chunks = 0;
3361 Frame_Chunk *remembered_state = 0;
3362 Frame_Chunk *rs;
3363 int is_eh = strcmp (section->name, ".eh_frame") == 0;
3364 unsigned int length_return;
3365 int max_regs = 0;
3367 printf (_("The section %s contains:\n"), section->name);
3369 while (start < end)
3371 unsigned char *saved_start;
3372 unsigned char *block_end;
3373 unsigned long length;
3374 unsigned long cie_id;
3375 Frame_Chunk *fc;
3376 Frame_Chunk *cie;
3377 int need_col_headers = 1;
3378 unsigned char *augmentation_data = NULL;
3379 unsigned long augmentation_data_len = 0;
3380 int encoded_ptr_size = eh_addr_size;
3381 int offset_size;
3382 int initial_length_size;
3384 saved_start = start;
3385 length = byte_get (start, 4); start += 4;
3387 if (length == 0)
3389 printf ("\n%08lx ZERO terminator\n\n",
3390 (unsigned long)(saved_start - section_start));
3391 continue;
3394 if (length == 0xffffffff)
3396 length = byte_get (start, 8);
3397 start += 8;
3398 offset_size = 8;
3399 initial_length_size = 12;
3401 else
3403 offset_size = 4;
3404 initial_length_size = 4;
3407 block_end = saved_start + length + initial_length_size;
3408 if (block_end > end)
3410 warn ("Invalid length %#08lx in FDE at %#08lx\n",
3411 length, (unsigned long)(saved_start - section_start));
3412 block_end = end;
3414 cie_id = byte_get (start, offset_size); start += offset_size;
3416 if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
3418 int version;
3420 fc = xmalloc (sizeof (Frame_Chunk));
3421 memset (fc, 0, sizeof (Frame_Chunk));
3423 fc->next = chunks;
3424 chunks = fc;
3425 fc->chunk_start = saved_start;
3426 fc->ncols = 0;
3427 fc->col_type = xmalloc (sizeof (short int));
3428 fc->col_offset = xmalloc (sizeof (int));
3429 frame_need_space (fc, max_regs - 1);
3431 version = *start++;
3433 fc->augmentation = (char *) start;
3434 start = (unsigned char *) strchr ((char *) start, '\0') + 1;
3436 if (fc->augmentation[0] == 'z')
3438 fc->code_factor = LEB ();
3439 fc->data_factor = SLEB ();
3440 if (version == 1)
3442 fc->ra = GET (1);
3444 else
3446 fc->ra = LEB ();
3448 augmentation_data_len = LEB ();
3449 augmentation_data = start;
3450 start += augmentation_data_len;
3452 else if (strcmp (fc->augmentation, "eh") == 0)
3454 start += eh_addr_size;
3455 fc->code_factor = LEB ();
3456 fc->data_factor = SLEB ();
3457 if (version == 1)
3459 fc->ra = GET (1);
3461 else
3463 fc->ra = LEB ();
3466 else
3468 fc->code_factor = LEB ();
3469 fc->data_factor = SLEB ();
3470 if (version == 1)
3472 fc->ra = GET (1);
3474 else
3476 fc->ra = LEB ();
3479 cie = fc;
3481 if (do_debug_frames_interp)
3482 printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
3483 (unsigned long)(saved_start - section_start), length, cie_id,
3484 fc->augmentation, fc->code_factor, fc->data_factor,
3485 fc->ra);
3486 else
3488 printf ("\n%08lx %08lx %08lx CIE\n",
3489 (unsigned long)(saved_start - section_start), length, cie_id);
3490 printf (" Version: %d\n", version);
3491 printf (" Augmentation: \"%s\"\n", fc->augmentation);
3492 printf (" Code alignment factor: %u\n", fc->code_factor);
3493 printf (" Data alignment factor: %d\n", fc->data_factor);
3494 printf (" Return address column: %d\n", fc->ra);
3496 if (augmentation_data_len)
3498 unsigned long i;
3499 printf (" Augmentation data: ");
3500 for (i = 0; i < augmentation_data_len; ++i)
3501 printf (" %02x", augmentation_data[i]);
3502 putchar ('\n');
3504 putchar ('\n');
3507 if (augmentation_data_len)
3509 unsigned char *p, *q;
3510 p = (unsigned char *) fc->augmentation + 1;
3511 q = augmentation_data;
3513 while (1)
3515 if (*p == 'L')
3516 q++;
3517 else if (*p == 'P')
3518 q += 1 + size_of_encoded_value (*q);
3519 else if (*p == 'R')
3520 fc->fde_encoding = *q++;
3521 else
3522 break;
3523 p++;
3526 if (fc->fde_encoding)
3527 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
3530 frame_need_space (fc, fc->ra);
3532 else
3534 unsigned char *look_for;
3535 static Frame_Chunk fde_fc;
3537 fc = & fde_fc;
3538 memset (fc, 0, sizeof (Frame_Chunk));
3540 look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
3542 for (cie = chunks; cie ; cie = cie->next)
3543 if (cie->chunk_start == look_for)
3544 break;
3546 if (!cie)
3548 warn ("Invalid CIE pointer %#08lx in FDE at %#08lx\n",
3549 cie_id, (unsigned long)(saved_start - section_start));
3550 fc->ncols = 0;
3551 fc->col_type = xmalloc (sizeof (short int));
3552 fc->col_offset = xmalloc (sizeof (int));
3553 frame_need_space (fc, max_regs - 1);
3554 cie = fc;
3555 fc->augmentation = "";
3556 fc->fde_encoding = 0;
3558 else
3560 fc->ncols = cie->ncols;
3561 fc->col_type = xcmalloc (fc->ncols, sizeof (short int));
3562 fc->col_offset = xcmalloc (fc->ncols, sizeof (int));
3563 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
3564 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
3565 fc->augmentation = cie->augmentation;
3566 fc->code_factor = cie->code_factor;
3567 fc->data_factor = cie->data_factor;
3568 fc->cfa_reg = cie->cfa_reg;
3569 fc->cfa_offset = cie->cfa_offset;
3570 fc->ra = cie->ra;
3571 frame_need_space (fc, max_regs - 1);
3572 fc->fde_encoding = cie->fde_encoding;
3575 if (fc->fde_encoding)
3576 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
3578 fc->pc_begin = get_encoded_value (start, fc->fde_encoding);
3579 if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
3580 fc->pc_begin += section->address + (start - section_start);
3581 start += encoded_ptr_size;
3582 fc->pc_range = byte_get (start, encoded_ptr_size);
3583 start += encoded_ptr_size;
3585 if (cie->augmentation[0] == 'z')
3587 augmentation_data_len = LEB ();
3588 augmentation_data = start;
3589 start += augmentation_data_len;
3592 printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
3593 (unsigned long)(saved_start - section_start), length, cie_id,
3594 (unsigned long)(cie->chunk_start - section_start),
3595 fc->pc_begin, fc->pc_begin + fc->pc_range);
3596 if (! do_debug_frames_interp && augmentation_data_len)
3598 unsigned long i;
3600 printf (" Augmentation data: ");
3601 for (i = 0; i < augmentation_data_len; ++i)
3602 printf (" %02x", augmentation_data[i]);
3603 putchar ('\n');
3604 putchar ('\n');
3608 /* At this point, fc is the current chunk, cie (if any) is set, and
3609 we're about to interpret instructions for the chunk. */
3610 /* ??? At present we need to do this always, since this sizes the
3611 fc->col_type and fc->col_offset arrays, which we write into always.
3612 We should probably split the interpreted and non-interpreted bits
3613 into two different routines, since there's so much that doesn't
3614 really overlap between them. */
3615 if (1 || do_debug_frames_interp)
3617 /* Start by making a pass over the chunk, allocating storage
3618 and taking note of what registers are used. */
3619 unsigned char *tmp = start;
3621 while (start < block_end)
3623 unsigned op, opa;
3624 unsigned long reg, tmp;
3626 op = *start++;
3627 opa = op & 0x3f;
3628 if (op & 0xc0)
3629 op &= 0xc0;
3631 /* Warning: if you add any more cases to this switch, be
3632 sure to add them to the corresponding switch below. */
3633 switch (op)
3635 case DW_CFA_advance_loc:
3636 break;
3637 case DW_CFA_offset:
3638 LEB ();
3639 frame_need_space (fc, opa);
3640 fc->col_type[opa] = DW_CFA_undefined;
3641 break;
3642 case DW_CFA_restore:
3643 frame_need_space (fc, opa);
3644 fc->col_type[opa] = DW_CFA_undefined;
3645 break;
3646 case DW_CFA_set_loc:
3647 start += encoded_ptr_size;
3648 break;
3649 case DW_CFA_advance_loc1:
3650 start += 1;
3651 break;
3652 case DW_CFA_advance_loc2:
3653 start += 2;
3654 break;
3655 case DW_CFA_advance_loc4:
3656 start += 4;
3657 break;
3658 case DW_CFA_offset_extended:
3659 case DW_CFA_val_offset:
3660 reg = LEB (); LEB ();
3661 frame_need_space (fc, reg);
3662 fc->col_type[reg] = DW_CFA_undefined;
3663 break;
3664 case DW_CFA_restore_extended:
3665 reg = LEB ();
3666 frame_need_space (fc, reg);
3667 fc->col_type[reg] = DW_CFA_undefined;
3668 break;
3669 case DW_CFA_undefined:
3670 reg = LEB ();
3671 frame_need_space (fc, reg);
3672 fc->col_type[reg] = DW_CFA_undefined;
3673 break;
3674 case DW_CFA_same_value:
3675 reg = LEB ();
3676 frame_need_space (fc, reg);
3677 fc->col_type[reg] = DW_CFA_undefined;
3678 break;
3679 case DW_CFA_register:
3680 reg = LEB (); LEB ();
3681 frame_need_space (fc, reg);
3682 fc->col_type[reg] = DW_CFA_undefined;
3683 break;
3684 case DW_CFA_def_cfa:
3685 LEB (); LEB ();
3686 break;
3687 case DW_CFA_def_cfa_register:
3688 LEB ();
3689 break;
3690 case DW_CFA_def_cfa_offset:
3691 LEB ();
3692 break;
3693 case DW_CFA_def_cfa_expression:
3694 tmp = LEB ();
3695 start += tmp;
3696 break;
3697 case DW_CFA_expression:
3698 case DW_CFA_val_expression:
3699 reg = LEB ();
3700 tmp = LEB ();
3701 start += tmp;
3702 frame_need_space (fc, reg);
3703 fc->col_type[reg] = DW_CFA_undefined;
3704 break;
3705 case DW_CFA_offset_extended_sf:
3706 case DW_CFA_val_offset_sf:
3707 reg = LEB (); SLEB ();
3708 frame_need_space (fc, reg);
3709 fc->col_type[reg] = DW_CFA_undefined;
3710 break;
3711 case DW_CFA_def_cfa_sf:
3712 LEB (); SLEB ();
3713 break;
3714 case DW_CFA_def_cfa_offset_sf:
3715 SLEB ();
3716 break;
3717 case DW_CFA_MIPS_advance_loc8:
3718 start += 8;
3719 break;
3720 case DW_CFA_GNU_args_size:
3721 LEB ();
3722 break;
3723 case DW_CFA_GNU_negative_offset_extended:
3724 reg = LEB (); LEB ();
3725 frame_need_space (fc, reg);
3726 fc->col_type[reg] = DW_CFA_undefined;
3728 default:
3729 break;
3732 start = tmp;
3735 /* Now we know what registers are used, make a second pass over
3736 the chunk, this time actually printing out the info. */
3738 while (start < block_end)
3740 unsigned op, opa;
3741 unsigned long ul, reg, roffs;
3742 long l, ofs;
3743 dwarf_vma vma;
3745 op = *start++;
3746 opa = op & 0x3f;
3747 if (op & 0xc0)
3748 op &= 0xc0;
3750 /* Warning: if you add any more cases to this switch, be
3751 sure to add them to the corresponding switch above. */
3752 switch (op)
3754 case DW_CFA_advance_loc:
3755 if (do_debug_frames_interp)
3756 frame_display_row (fc, &need_col_headers, &max_regs);
3757 else
3758 printf (" DW_CFA_advance_loc: %d to %08lx\n",
3759 opa * fc->code_factor,
3760 fc->pc_begin + opa * fc->code_factor);
3761 fc->pc_begin += opa * fc->code_factor;
3762 break;
3764 case DW_CFA_offset:
3765 roffs = LEB ();
3766 if (! do_debug_frames_interp)
3767 printf (" DW_CFA_offset: %s at cfa%+ld\n",
3768 regname (opa, 0), roffs * fc->data_factor);
3769 fc->col_type[opa] = DW_CFA_offset;
3770 fc->col_offset[opa] = roffs * fc->data_factor;
3771 break;
3773 case DW_CFA_restore:
3774 if (! do_debug_frames_interp)
3775 printf (" DW_CFA_restore: %s\n", regname (opa, 0));
3776 fc->col_type[opa] = cie->col_type[opa];
3777 fc->col_offset[opa] = cie->col_offset[opa];
3778 break;
3780 case DW_CFA_set_loc:
3781 vma = get_encoded_value (start, fc->fde_encoding);
3782 if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
3783 vma += section->address + (start - section_start);
3784 start += encoded_ptr_size;
3785 if (do_debug_frames_interp)
3786 frame_display_row (fc, &need_col_headers, &max_regs);
3787 else
3788 printf (" DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
3789 fc->pc_begin = vma;
3790 break;
3792 case DW_CFA_advance_loc1:
3793 ofs = byte_get (start, 1); start += 1;
3794 if (do_debug_frames_interp)
3795 frame_display_row (fc, &need_col_headers, &max_regs);
3796 else
3797 printf (" DW_CFA_advance_loc1: %ld to %08lx\n",
3798 ofs * fc->code_factor,
3799 fc->pc_begin + ofs * fc->code_factor);
3800 fc->pc_begin += ofs * fc->code_factor;
3801 break;
3803 case DW_CFA_advance_loc2:
3804 ofs = byte_get (start, 2); start += 2;
3805 if (do_debug_frames_interp)
3806 frame_display_row (fc, &need_col_headers, &max_regs);
3807 else
3808 printf (" DW_CFA_advance_loc2: %ld to %08lx\n",
3809 ofs * fc->code_factor,
3810 fc->pc_begin + ofs * fc->code_factor);
3811 fc->pc_begin += ofs * fc->code_factor;
3812 break;
3814 case DW_CFA_advance_loc4:
3815 ofs = byte_get (start, 4); start += 4;
3816 if (do_debug_frames_interp)
3817 frame_display_row (fc, &need_col_headers, &max_regs);
3818 else
3819 printf (" DW_CFA_advance_loc4: %ld to %08lx\n",
3820 ofs * fc->code_factor,
3821 fc->pc_begin + ofs * fc->code_factor);
3822 fc->pc_begin += ofs * fc->code_factor;
3823 break;
3825 case DW_CFA_offset_extended:
3826 reg = LEB ();
3827 roffs = LEB ();
3828 if (! do_debug_frames_interp)
3829 printf (" DW_CFA_offset_extended: %s at cfa%+ld\n",
3830 regname (reg, 0), roffs * fc->data_factor);
3831 fc->col_type[reg] = DW_CFA_offset;
3832 fc->col_offset[reg] = roffs * fc->data_factor;
3833 break;
3835 case DW_CFA_val_offset:
3836 reg = LEB ();
3837 roffs = LEB ();
3838 if (! do_debug_frames_interp)
3839 printf (" DW_CFA_val_offset: %s at cfa%+ld\n",
3840 regname (reg, 0), roffs * fc->data_factor);
3841 fc->col_type[reg] = DW_CFA_val_offset;
3842 fc->col_offset[reg] = roffs * fc->data_factor;
3843 break;
3845 case DW_CFA_restore_extended:
3846 reg = LEB ();
3847 if (! do_debug_frames_interp)
3848 printf (" DW_CFA_restore_extended: %s\n",
3849 regname (reg, 0));
3850 fc->col_type[reg] = cie->col_type[reg];
3851 fc->col_offset[reg] = cie->col_offset[reg];
3852 break;
3854 case DW_CFA_undefined:
3855 reg = LEB ();
3856 if (! do_debug_frames_interp)
3857 printf (" DW_CFA_undefined: %s\n", regname (reg, 0));
3858 fc->col_type[reg] = DW_CFA_undefined;
3859 fc->col_offset[reg] = 0;
3860 break;
3862 case DW_CFA_same_value:
3863 reg = LEB ();
3864 if (! do_debug_frames_interp)
3865 printf (" DW_CFA_same_value: %s\n", regname (reg, 0));
3866 fc->col_type[reg] = DW_CFA_same_value;
3867 fc->col_offset[reg] = 0;
3868 break;
3870 case DW_CFA_register:
3871 reg = LEB ();
3872 roffs = LEB ();
3873 if (! do_debug_frames_interp)
3875 printf (" DW_CFA_register: %s in ",
3876 regname (reg, 0));
3877 puts (regname (roffs, 0));
3879 fc->col_type[reg] = DW_CFA_register;
3880 fc->col_offset[reg] = roffs;
3881 break;
3883 case DW_CFA_remember_state:
3884 if (! do_debug_frames_interp)
3885 printf (" DW_CFA_remember_state\n");
3886 rs = xmalloc (sizeof (Frame_Chunk));
3887 rs->ncols = fc->ncols;
3888 rs->col_type = xcmalloc (rs->ncols, sizeof (short int));
3889 rs->col_offset = xcmalloc (rs->ncols, sizeof (int));
3890 memcpy (rs->col_type, fc->col_type, rs->ncols);
3891 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
3892 rs->next = remembered_state;
3893 remembered_state = rs;
3894 break;
3896 case DW_CFA_restore_state:
3897 if (! do_debug_frames_interp)
3898 printf (" DW_CFA_restore_state\n");
3899 rs = remembered_state;
3900 if (rs)
3902 remembered_state = rs->next;
3903 frame_need_space (fc, rs->ncols - 1);
3904 memcpy (fc->col_type, rs->col_type, rs->ncols);
3905 memcpy (fc->col_offset, rs->col_offset,
3906 rs->ncols * sizeof (int));
3907 free (rs->col_type);
3908 free (rs->col_offset);
3909 free (rs);
3911 else if (do_debug_frames_interp)
3912 printf ("Mismatched DW_CFA_restore_state\n");
3913 break;
3915 case DW_CFA_def_cfa:
3916 fc->cfa_reg = LEB ();
3917 fc->cfa_offset = LEB ();
3918 fc->cfa_exp = 0;
3919 if (! do_debug_frames_interp)
3920 printf (" DW_CFA_def_cfa: %s ofs %d\n",
3921 regname (fc->cfa_reg, 0), fc->cfa_offset);
3922 break;
3924 case DW_CFA_def_cfa_register:
3925 fc->cfa_reg = LEB ();
3926 fc->cfa_exp = 0;
3927 if (! do_debug_frames_interp)
3928 printf (" DW_CFA_def_cfa_register: %s\n",
3929 regname (fc->cfa_reg, 0));
3930 break;
3932 case DW_CFA_def_cfa_offset:
3933 fc->cfa_offset = LEB ();
3934 if (! do_debug_frames_interp)
3935 printf (" DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
3936 break;
3938 case DW_CFA_nop:
3939 if (! do_debug_frames_interp)
3940 printf (" DW_CFA_nop\n");
3941 break;
3943 case DW_CFA_def_cfa_expression:
3944 ul = LEB ();
3945 if (! do_debug_frames_interp)
3947 printf (" DW_CFA_def_cfa_expression (");
3948 decode_location_expression (start, eh_addr_size, ul, 0);
3949 printf (")\n");
3951 fc->cfa_exp = 1;
3952 start += ul;
3953 break;
3955 case DW_CFA_expression:
3956 reg = LEB ();
3957 ul = LEB ();
3958 if (! do_debug_frames_interp)
3960 printf (" DW_CFA_expression: %s (",
3961 regname (reg, 0));
3962 decode_location_expression (start, eh_addr_size,
3963 ul, 0);
3964 printf (")\n");
3966 fc->col_type[reg] = DW_CFA_expression;
3967 start += ul;
3968 break;
3970 case DW_CFA_val_expression:
3971 reg = LEB ();
3972 ul = LEB ();
3973 if (! do_debug_frames_interp)
3975 printf (" DW_CFA_val_expression: %s (",
3976 regname (reg, 0));
3977 decode_location_expression (start, eh_addr_size, ul, 0);
3978 printf (")\n");
3980 fc->col_type[reg] = DW_CFA_val_expression;
3981 start += ul;
3982 break;
3984 case DW_CFA_offset_extended_sf:
3985 reg = LEB ();
3986 l = SLEB ();
3987 frame_need_space (fc, reg);
3988 if (! do_debug_frames_interp)
3989 printf (" DW_CFA_offset_extended_sf: %s at cfa%+ld\n",
3990 regname (reg, 0), l * fc->data_factor);
3991 fc->col_type[reg] = DW_CFA_offset;
3992 fc->col_offset[reg] = l * fc->data_factor;
3993 break;
3995 case DW_CFA_val_offset_sf:
3996 reg = LEB ();
3997 l = SLEB ();
3998 frame_need_space (fc, reg);
3999 if (! do_debug_frames_interp)
4000 printf (" DW_CFA_val_offset_sf: %s at cfa%+ld\n",
4001 regname (reg, 0), l * fc->data_factor);
4002 fc->col_type[reg] = DW_CFA_val_offset;
4003 fc->col_offset[reg] = l * fc->data_factor;
4004 break;
4006 case DW_CFA_def_cfa_sf:
4007 fc->cfa_reg = LEB ();
4008 fc->cfa_offset = SLEB ();
4009 fc->cfa_offset = fc->cfa_offset * fc->data_factor;
4010 fc->cfa_exp = 0;
4011 if (! do_debug_frames_interp)
4012 printf (" DW_CFA_def_cfa_sf: %s ofs %d\n",
4013 regname (fc->cfa_reg, 0), fc->cfa_offset);
4014 break;
4016 case DW_CFA_def_cfa_offset_sf:
4017 fc->cfa_offset = SLEB ();
4018 fc->cfa_offset = fc->cfa_offset * fc->data_factor;
4019 if (! do_debug_frames_interp)
4020 printf (" DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
4021 break;
4023 case DW_CFA_MIPS_advance_loc8:
4024 ofs = byte_get (start, 8); start += 8;
4025 if (do_debug_frames_interp)
4026 frame_display_row (fc, &need_col_headers, &max_regs);
4027 else
4028 printf (" DW_CFA_MIPS_advance_loc8: %ld to %08lx\n",
4029 ofs * fc->code_factor,
4030 fc->pc_begin + ofs * fc->code_factor);
4031 fc->pc_begin += ofs * fc->code_factor;
4032 break;
4034 case DW_CFA_GNU_window_save:
4035 if (! do_debug_frames_interp)
4036 printf (" DW_CFA_GNU_window_save\n");
4037 break;
4039 case DW_CFA_GNU_args_size:
4040 ul = LEB ();
4041 if (! do_debug_frames_interp)
4042 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
4043 break;
4045 case DW_CFA_GNU_negative_offset_extended:
4046 reg = LEB ();
4047 l = - LEB ();
4048 frame_need_space (fc, reg);
4049 if (! do_debug_frames_interp)
4050 printf (" DW_CFA_GNU_negative_offset_extended: %s at cfa%+ld\n",
4051 regname (reg, 0), l * fc->data_factor);
4052 fc->col_type[reg] = DW_CFA_offset;
4053 fc->col_offset[reg] = l * fc->data_factor;
4054 break;
4056 default:
4057 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
4058 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
4059 else
4060 warn (_("unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
4061 start = block_end;
4065 if (do_debug_frames_interp)
4066 frame_display_row (fc, &need_col_headers, &max_regs);
4068 start = block_end;
4071 printf ("\n");
4073 return 1;
4076 #undef GET
4077 #undef LEB
4078 #undef SLEB
4080 static int
4081 display_debug_not_supported (struct dwarf_section *section,
4082 void *file ATTRIBUTE_UNUSED)
4084 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
4085 section->name);
4087 return 1;
4090 void *
4091 cmalloc (size_t nmemb, size_t size)
4093 /* Check for overflow. */
4094 if (nmemb >= ~(size_t) 0 / size)
4095 return NULL;
4096 else
4097 return malloc (nmemb * size);
4100 void *
4101 xcmalloc (size_t nmemb, size_t size)
4103 /* Check for overflow. */
4104 if (nmemb >= ~(size_t) 0 / size)
4105 return NULL;
4106 else
4107 return xmalloc (nmemb * size);
4110 void *
4111 xcrealloc (void *ptr, size_t nmemb, size_t size)
4113 /* Check for overflow. */
4114 if (nmemb >= ~(size_t) 0 / size)
4115 return NULL;
4116 else
4117 return xrealloc (ptr, nmemb * size);
4120 void
4121 error (const char *message, ...)
4123 va_list args;
4125 va_start (args, message);
4126 fprintf (stderr, _("%s: Error: "), program_name);
4127 vfprintf (stderr, message, args);
4128 va_end (args);
4131 void
4132 warn (const char *message, ...)
4134 va_list args;
4136 va_start (args, message);
4137 fprintf (stderr, _("%s: Warning: "), program_name);
4138 vfprintf (stderr, message, args);
4139 va_end (args);
4142 void
4143 free_debug_memory (void)
4145 enum dwarf_section_display_enum i;
4147 free_abbrevs ();
4149 for (i = 0; i < max; i++)
4150 free_debug_section (i);
4152 if (debug_information != NULL)
4154 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
4156 for (i = 0; i < num_debug_info_entries; i++)
4158 if (!debug_information [i].max_loc_offsets)
4160 free (debug_information [i].loc_offsets);
4161 free (debug_information [i].have_frame_base);
4163 if (!debug_information [i].max_range_lists)
4164 free (debug_information [i].range_lists);
4168 free (debug_information);
4169 debug_information = NULL;
4170 num_debug_info_entries = 0;
4174 struct dwarf_section_display debug_displays[] =
4176 { { ".debug_abbrev", NULL, 0, 0 },
4177 display_debug_abbrev, 0, 0 },
4178 { { ".debug_aranges", NULL, 0, 0 },
4179 display_debug_aranges, 0, 0 },
4180 { { ".debug_frame", NULL, 0, 0 },
4181 display_debug_frames, 1, 0 },
4182 { { ".debug_info", NULL, 0, 0 },
4183 display_debug_info, 1, 0 },
4184 { { ".debug_line", NULL, 0, 0 },
4185 display_debug_lines, 0, 0 },
4186 { { ".debug_pubnames", NULL, 0, 0 },
4187 display_debug_pubnames, 0, 0 },
4188 { { ".eh_frame", NULL, 0, 0 },
4189 display_debug_frames, 1, 1 },
4190 { { ".debug_macinfo", NULL, 0, 0 },
4191 display_debug_macinfo, 0, 0 },
4192 { { ".debug_str", NULL, 0, 0 },
4193 display_debug_str, 0, 0 },
4194 { { ".debug_loc", NULL, 0, 0 },
4195 display_debug_loc, 0, 0 },
4196 { { ".debug_pubtypes", NULL, 0, 0 },
4197 display_debug_pubnames, 0, 0 },
4198 { { ".debug_ranges", NULL, 0, 0 },
4199 display_debug_ranges, 0, 0 },
4200 { { ".debug_static_func", NULL, 0, 0 },
4201 display_debug_not_supported, 0, 0 },
4202 { { ".debug_static_vars", NULL, 0, 0 },
4203 display_debug_not_supported, 0, 0 },
4204 { { ".debug_types", NULL, 0, 0 },
4205 display_debug_not_supported, 0, 0 },
4206 { { ".debug_weaknames", NULL, 0, 0 },
4207 display_debug_not_supported, 0, 0 }