2012-09-06 Cary Coutant <ccoutant@google.com>
[deliverable/binutils-gdb.git] / binutils / dwarf.c
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2 Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Binutils.
6
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.
11
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.
16
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. */
21
22 #include "sysdep.h"
23 #include "libiberty.h"
24 #include "bfd.h"
25 #include "bfd_stdint.h"
26 #include "bucomm.h"
27 #include "elfcomm.h"
28 #include "elf/common.h"
29 #include "dwarf2.h"
30 #include "dwarf.h"
31 #include "gdb/gdb-index.h"
32
33 static const char *regname (unsigned int regno, int row);
34
35 static int have_frame_base;
36 static int need_base_address;
37
38 static unsigned int last_pointer_size = 0;
39 static int warned_about_missing_comp_units = FALSE;
40
41 static unsigned int num_debug_info_entries = 0;
42 static debug_info *debug_information = NULL;
43 /* Special value for num_debug_info_entries to indicate
44 that the .debug_info section could not be loaded/parsed. */
45 #define DEBUG_INFO_UNAVAILABLE (unsigned int) -1
46
47 int eh_addr_size;
48
49 int do_debug_info;
50 int do_debug_abbrevs;
51 int do_debug_lines;
52 int do_debug_pubnames;
53 int do_debug_pubtypes;
54 int do_debug_aranges;
55 int do_debug_ranges;
56 int do_debug_frames;
57 int do_debug_frames_interp;
58 int do_debug_macinfo;
59 int do_debug_str;
60 int do_debug_loc;
61 int do_gdb_index;
62 int do_trace_info;
63 int do_trace_abbrevs;
64 int do_trace_aranges;
65 int do_wide;
66
67 int dwarf_cutoff_level = -1;
68 unsigned long dwarf_start_die;
69
70 int dwarf_check = 0;
71
72 /* Values for do_debug_lines. */
73 #define FLAG_DEBUG_LINES_RAW 1
74 #define FLAG_DEBUG_LINES_DECODED 2
75
76 static int
77 size_of_encoded_value (int encoding)
78 {
79 switch (encoding & 0x7)
80 {
81 default: /* ??? */
82 case 0: return eh_addr_size;
83 case 2: return 2;
84 case 3: return 4;
85 case 4: return 8;
86 }
87 }
88
89 static dwarf_vma
90 get_encoded_value (unsigned char *data,
91 int encoding,
92 struct dwarf_section *section)
93 {
94 int size = size_of_encoded_value (encoding);
95 dwarf_vma val;
96
97 if (encoding & DW_EH_PE_signed)
98 val = byte_get_signed (data, size);
99 else
100 val = byte_get (data, size);
101
102 if ((encoding & 0x70) == DW_EH_PE_pcrel)
103 val += section->address + (data - section->start);
104 return val;
105 }
106
107 /* Print a dwarf_vma value (typically an address, offset or length) in
108 hexadecimal format, followed by a space. The length of the value (and
109 hence the precision displayed) is determined by the byte_size parameter. */
110
111 static void
112 print_dwarf_vma (dwarf_vma val, unsigned byte_size)
113 {
114 static char buff[18];
115 int offset = 0;
116
117 /* Printf does not have a way of specifiying a maximum field width for an
118 integer value, so we print the full value into a buffer and then select
119 the precision we need. */
120 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
121 #ifndef __MINGW32__
122 snprintf (buff, sizeof (buff), "%16.16llx ", val);
123 #else
124 snprintf (buff, sizeof (buff), "%016I64x ", val);
125 #endif
126 #else
127 snprintf (buff, sizeof (buff), "%16.16lx ", val);
128 #endif
129
130 if (byte_size != 0)
131 {
132 if (byte_size > 0 && byte_size <= 8)
133 offset = 16 - 2 * byte_size;
134 else
135 error (_("Wrong size in print_dwarf_vma"));
136 }
137
138 fputs (buff + offset, stdout);
139 }
140
141 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
142 #ifndef __MINGW32__
143 #define DWARF_VMA_FMT "ll"
144 #else
145 #define DWARF_VMA_FMT "I64"
146 #endif
147 #else
148 #define DWARF_VMA_FMT "l"
149 #endif
150
151 static const char *
152 dwarf_vmatoa (const char *fmtch, dwarf_vma value)
153 {
154 /* As dwarf_vmatoa is used more then once in a printf call
155 for output, we are cycling through an fixed array of pointers
156 for return address. */
157 static int buf_pos = 0;
158 static struct dwarf_vmatoa_buf
159 {
160 char place[64];
161 } buf[16];
162 char fmt[32];
163 char *ret;
164
165 sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
166
167 ret = buf[buf_pos++].place;
168 buf_pos %= ARRAY_SIZE (buf);
169
170 snprintf (ret, sizeof (buf[0].place), fmt, value);
171
172 return ret;
173 }
174
175 /* Format a 64-bit value, given as two 32-bit values, in hex.
176 For reentrancy, this uses a buffer provided by the caller. */
177
178 static const char *
179 dwarf_vmatoa64 (dwarf_vma hvalue, dwarf_vma lvalue, char *buf,
180 unsigned int buf_len)
181 {
182 int len = 0;
183
184 if (hvalue == 0)
185 snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", lvalue);
186 else
187 {
188 len = snprintf (buf, buf_len, "%" DWARF_VMA_FMT "x", hvalue);
189 snprintf (buf + len, buf_len - len,
190 "%08" DWARF_VMA_FMT "x", lvalue);
191 }
192
193 return buf;
194 }
195
196 dwarf_vma
197 read_leb128 (unsigned char *data, unsigned int *length_return, int sign)
198 {
199 dwarf_vma result = 0;
200 unsigned int num_read = 0;
201 unsigned int shift = 0;
202 unsigned char byte;
203
204 do
205 {
206 byte = *data++;
207 num_read++;
208
209 result |= ((dwarf_vma) (byte & 0x7f)) << shift;
210
211 shift += 7;
212
213 }
214 while (byte & 0x80);
215
216 if (length_return != NULL)
217 *length_return = num_read;
218
219 if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40))
220 result |= -1L << shift;
221
222 return result;
223 }
224
225 /* Create a signed version to avoid painful typecasts. */
226 static dwarf_signed_vma
227 read_sleb128 (unsigned char *data, unsigned int *length_return)
228 {
229 return (dwarf_signed_vma) read_leb128 (data, length_return, 1);
230 }
231
232 typedef struct State_Machine_Registers
233 {
234 dwarf_vma address;
235 unsigned int file;
236 unsigned int line;
237 unsigned int column;
238 int is_stmt;
239 int basic_block;
240 unsigned char op_index;
241 unsigned char end_sequence;
242 /* This variable hold the number of the last entry seen
243 in the File Table. */
244 unsigned int last_file_entry;
245 } SMR;
246
247 static SMR state_machine_regs;
248
249 static void
250 reset_state_machine (int is_stmt)
251 {
252 state_machine_regs.address = 0;
253 state_machine_regs.op_index = 0;
254 state_machine_regs.file = 1;
255 state_machine_regs.line = 1;
256 state_machine_regs.column = 0;
257 state_machine_regs.is_stmt = is_stmt;
258 state_machine_regs.basic_block = 0;
259 state_machine_regs.end_sequence = 0;
260 state_machine_regs.last_file_entry = 0;
261 }
262
263 /* Handled an extend line op.
264 Returns the number of bytes read. */
265
266 static int
267 process_extended_line_op (unsigned char *data, int is_stmt)
268 {
269 unsigned char op_code;
270 unsigned int bytes_read;
271 unsigned int len;
272 unsigned char *name;
273 dwarf_vma adr;
274 unsigned char *orig_data = data;
275
276 len = read_leb128 (data, & bytes_read, 0);
277 data += bytes_read;
278
279 if (len == 0)
280 {
281 warn (_("badly formed extended line op encountered!\n"));
282 return bytes_read;
283 }
284
285 len += bytes_read;
286 op_code = *data++;
287
288 printf (_(" Extended opcode %d: "), op_code);
289
290 switch (op_code)
291 {
292 case DW_LNE_end_sequence:
293 printf (_("End of Sequence\n\n"));
294 reset_state_machine (is_stmt);
295 break;
296
297 case DW_LNE_set_address:
298 adr = byte_get (data, len - bytes_read - 1);
299 printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
300 state_machine_regs.address = adr;
301 state_machine_regs.op_index = 0;
302 break;
303
304 case DW_LNE_define_file:
305 printf (_("define new File Table entry\n"));
306 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
307
308 printf (" %d\t", ++state_machine_regs.last_file_entry);
309 name = data;
310 data += strlen ((char *) data) + 1;
311 printf ("%s\t", dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
312 data += bytes_read;
313 printf ("%s\t", dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
314 data += bytes_read;
315 printf ("%s\t", dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
316 data += bytes_read;
317 printf ("%s", name);
318 if ((unsigned int) (data - orig_data) != len)
319 printf (_(" [Bad opcode length]"));
320 printf ("\n\n");
321 break;
322
323 case DW_LNE_set_discriminator:
324 printf (_("set Discriminator to %s\n"),
325 dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
326 break;
327
328 /* HP extensions. */
329 case DW_LNE_HP_negate_is_UV_update:
330 printf ("DW_LNE_HP_negate_is_UV_update\n");
331 break;
332 case DW_LNE_HP_push_context:
333 printf ("DW_LNE_HP_push_context\n");
334 break;
335 case DW_LNE_HP_pop_context:
336 printf ("DW_LNE_HP_pop_context\n");
337 break;
338 case DW_LNE_HP_set_file_line_column:
339 printf ("DW_LNE_HP_set_file_line_column\n");
340 break;
341 case DW_LNE_HP_set_routine_name:
342 printf ("DW_LNE_HP_set_routine_name\n");
343 break;
344 case DW_LNE_HP_set_sequence:
345 printf ("DW_LNE_HP_set_sequence\n");
346 break;
347 case DW_LNE_HP_negate_post_semantics:
348 printf ("DW_LNE_HP_negate_post_semantics\n");
349 break;
350 case DW_LNE_HP_negate_function_exit:
351 printf ("DW_LNE_HP_negate_function_exit\n");
352 break;
353 case DW_LNE_HP_negate_front_end_logical:
354 printf ("DW_LNE_HP_negate_front_end_logical\n");
355 break;
356 case DW_LNE_HP_define_proc:
357 printf ("DW_LNE_HP_define_proc\n");
358 break;
359 case DW_LNE_HP_source_file_correlation:
360 {
361 unsigned char *edata = data + len - bytes_read - 1;
362
363 printf ("DW_LNE_HP_source_file_correlation\n");
364
365 while (data < edata)
366 {
367 unsigned int opc;
368
369 opc = read_leb128 (data, & bytes_read, 0);
370 data += bytes_read;
371
372 switch (opc)
373 {
374 case DW_LNE_HP_SFC_formfeed:
375 printf (" DW_LNE_HP_SFC_formfeed\n");
376 break;
377 case DW_LNE_HP_SFC_set_listing_line:
378 printf (" DW_LNE_HP_SFC_set_listing_line (%s)\n",
379 dwarf_vmatoa ("u",
380 read_leb128 (data, & bytes_read, 0)));
381 data += bytes_read;
382 break;
383 case DW_LNE_HP_SFC_associate:
384 printf (" DW_LNE_HP_SFC_associate ");
385 printf ("(%s",
386 dwarf_vmatoa ("u",
387 read_leb128 (data, & bytes_read, 0)));
388 data += bytes_read;
389 printf (",%s",
390 dwarf_vmatoa ("u",
391 read_leb128 (data, & bytes_read, 0)));
392 data += bytes_read;
393 printf (",%s)\n",
394 dwarf_vmatoa ("u",
395 read_leb128 (data, & bytes_read, 0)));
396 data += bytes_read;
397 break;
398 default:
399 printf (_(" UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
400 data = edata;
401 break;
402 }
403 }
404 }
405 break;
406
407 default:
408 {
409 unsigned int rlen = len - bytes_read - 1;
410
411 if (op_code >= DW_LNE_lo_user
412 /* The test against DW_LNW_hi_user is redundant due to
413 the limited range of the unsigned char data type used
414 for op_code. */
415 /*&& op_code <= DW_LNE_hi_user*/)
416 printf (_("user defined: "));
417 else
418 printf (_("UNKNOWN: "));
419 printf (_("length %d ["), rlen);
420 for (; rlen; rlen--)
421 printf (" %02x", *data++);
422 printf ("]\n");
423 }
424 break;
425 }
426
427 return len;
428 }
429
430 static const char *
431 fetch_indirect_string (dwarf_vma offset)
432 {
433 struct dwarf_section *section = &debug_displays [str].section;
434
435 if (section->start == NULL)
436 return _("<no .debug_str section>");
437
438 /* DWARF sections under Mach-O have non-zero addresses. */
439 offset -= section->address;
440 if (offset > section->size)
441 {
442 warn (_("DW_FORM_strp offset too big: %s\n"),
443 dwarf_vmatoa ("x", offset));
444 return _("<offset is too big>");
445 }
446
447 return (const char *) section->start + offset;
448 }
449
450 static const char *
451 fetch_indexed_string (dwarf_vma idx, dwarf_vma offset_size, int dwo)
452 {
453 enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
454 enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
455 struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
456 struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
457 dwarf_vma index_offset = idx * offset_size;
458 dwarf_vma str_offset;
459
460 if (index_section->start == NULL)
461 return (dwo ? _("<no .debug_str_offsets.dwo section>")
462 : _("<no .debug_str_offsets section>"));
463
464 /* DWARF sections under Mach-O have non-zero addresses. */
465 index_offset -= index_section->address;
466 if (index_offset > index_section->size)
467 {
468 warn (_("DW_FORM_GNU_str_index offset too big: %s\n"),
469 dwarf_vmatoa ("x", index_offset));
470 return _("<index offset is too big>");
471 }
472
473 if (str_section->start == NULL)
474 return (dwo ? _("<no .debug_str.dwo section>")
475 : _("<no .debug_str section>"));
476
477 str_offset = byte_get (index_section->start + index_offset, offset_size);
478 str_offset -= str_section->address;
479 if (str_offset > str_section->size)
480 {
481 warn (_("DW_FORM_GNU_str_index indirect offset too big: %s\n"),
482 dwarf_vmatoa ("x", str_offset));
483 return _("<indirect index offset is too big>");
484 }
485
486 return (const char *) str_section->start + str_offset;
487 }
488
489 static const char *
490 fetch_indexed_value (dwarf_vma offset, dwarf_vma bytes)
491 {
492 struct dwarf_section *section = &debug_displays [debug_addr].section;
493
494 if (section->start == NULL)
495 return (_("<no .debug_addr section>"));
496
497 if (offset + bytes > section->size)
498 {
499 warn (_("Offset into section %s too big: %s\n"),
500 section->name, dwarf_vmatoa ("x", offset));
501 return "<offset too big>";
502 }
503
504 return dwarf_vmatoa ("x", byte_get (section->start + offset, bytes));
505 }
506
507
508 /* FIXME: There are better and more efficient ways to handle
509 these structures. For now though, I just want something that
510 is simple to implement. */
511 typedef struct abbrev_attr
512 {
513 unsigned long attribute;
514 unsigned long form;
515 struct abbrev_attr *next;
516 }
517 abbrev_attr;
518
519 typedef struct abbrev_entry
520 {
521 unsigned long entry;
522 unsigned long tag;
523 int children;
524 struct abbrev_attr *first_attr;
525 struct abbrev_attr *last_attr;
526 struct abbrev_entry *next;
527 }
528 abbrev_entry;
529
530 static abbrev_entry *first_abbrev = NULL;
531 static abbrev_entry *last_abbrev = NULL;
532
533 static void
534 free_abbrevs (void)
535 {
536 abbrev_entry *abbrv;
537
538 for (abbrv = first_abbrev; abbrv;)
539 {
540 abbrev_entry *next_abbrev = abbrv->next;
541 abbrev_attr *attr;
542
543 for (attr = abbrv->first_attr; attr;)
544 {
545 abbrev_attr *next_attr = attr->next;
546
547 free (attr);
548 attr = next_attr;
549 }
550
551 free (abbrv);
552 abbrv = next_abbrev;
553 }
554
555 last_abbrev = first_abbrev = NULL;
556 }
557
558 static void
559 add_abbrev (unsigned long number, unsigned long tag, int children)
560 {
561 abbrev_entry *entry;
562
563 entry = (abbrev_entry *) malloc (sizeof (*entry));
564 if (entry == NULL)
565 /* ugg */
566 return;
567
568 entry->entry = number;
569 entry->tag = tag;
570 entry->children = children;
571 entry->first_attr = NULL;
572 entry->last_attr = NULL;
573 entry->next = NULL;
574
575 if (first_abbrev == NULL)
576 first_abbrev = entry;
577 else
578 last_abbrev->next = entry;
579
580 last_abbrev = entry;
581 }
582
583 static void
584 add_abbrev_attr (unsigned long attribute, unsigned long form)
585 {
586 abbrev_attr *attr;
587
588 attr = (abbrev_attr *) malloc (sizeof (*attr));
589 if (attr == NULL)
590 /* ugg */
591 return;
592
593 attr->attribute = attribute;
594 attr->form = form;
595 attr->next = NULL;
596
597 if (last_abbrev->first_attr == NULL)
598 last_abbrev->first_attr = attr;
599 else
600 last_abbrev->last_attr->next = attr;
601
602 last_abbrev->last_attr = attr;
603 }
604
605 /* Processes the (partial) contents of a .debug_abbrev section.
606 Returns NULL if the end of the section was encountered.
607 Returns the address after the last byte read if the end of
608 an abbreviation set was found. */
609
610 static unsigned char *
611 process_abbrev_section (unsigned char *start, unsigned char *end)
612 {
613 if (first_abbrev != NULL)
614 return NULL;
615
616 while (start < end)
617 {
618 unsigned int bytes_read;
619 unsigned long entry;
620 unsigned long tag;
621 unsigned long attribute;
622 int children;
623
624 entry = read_leb128 (start, & bytes_read, 0);
625 start += bytes_read;
626
627 /* A single zero is supposed to end the section according
628 to the standard. If there's more, then signal that to
629 the caller. */
630 if (entry == 0)
631 return start == end ? NULL : start;
632
633 tag = read_leb128 (start, & bytes_read, 0);
634 start += bytes_read;
635
636 children = *start++;
637
638 add_abbrev (entry, tag, children);
639
640 do
641 {
642 unsigned long form;
643
644 attribute = read_leb128 (start, & bytes_read, 0);
645 start += bytes_read;
646
647 form = read_leb128 (start, & bytes_read, 0);
648 start += bytes_read;
649
650 add_abbrev_attr (attribute, form);
651 }
652 while (attribute != 0);
653 }
654
655 /* Report the missing single zero which ends the section. */
656 error (_(".debug_abbrev section not zero terminated\n"));
657
658 return NULL;
659 }
660
661 static const char *
662 get_TAG_name (unsigned long tag)
663 {
664 const char *name = get_DW_TAG_name ((unsigned int)tag);
665
666 if (name == NULL)
667 {
668 static char buffer[100];
669
670 snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %lx"), tag);
671 return buffer;
672 }
673
674 return name;
675 }
676
677 static const char *
678 get_FORM_name (unsigned long form)
679 {
680 const char *name;
681
682 if (form == 0)
683 return "DW_FORM value: 0";
684
685 name = get_DW_FORM_name (form);
686 if (name == NULL)
687 {
688 static char buffer[100];
689
690 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
691 return buffer;
692 }
693
694 return name;
695 }
696
697 static unsigned char *
698 display_block (unsigned char *data, dwarf_vma length)
699 {
700 printf (_(" %s byte block: "), dwarf_vmatoa ("u", length));
701
702 while (length --)
703 printf ("%lx ", (unsigned long) byte_get (data++, 1));
704
705 return data;
706 }
707
708 static int
709 decode_location_expression (unsigned char * data,
710 unsigned int pointer_size,
711 unsigned int offset_size,
712 int dwarf_version,
713 dwarf_vma length,
714 dwarf_vma cu_offset,
715 struct dwarf_section * section)
716 {
717 unsigned op;
718 unsigned int bytes_read;
719 dwarf_vma uvalue;
720 unsigned char *end = data + length;
721 int need_frame_base = 0;
722
723 while (data < end)
724 {
725 op = *data++;
726
727 switch (op)
728 {
729 case DW_OP_addr:
730 printf ("DW_OP_addr: %s",
731 dwarf_vmatoa ("x", byte_get (data, pointer_size)));
732 data += pointer_size;
733 break;
734 case DW_OP_deref:
735 printf ("DW_OP_deref");
736 break;
737 case DW_OP_const1u:
738 printf ("DW_OP_const1u: %lu", (unsigned long) byte_get (data++, 1));
739 break;
740 case DW_OP_const1s:
741 printf ("DW_OP_const1s: %ld", (long) byte_get_signed (data++, 1));
742 break;
743 case DW_OP_const2u:
744 printf ("DW_OP_const2u: %lu", (unsigned long) byte_get (data, 2));
745 data += 2;
746 break;
747 case DW_OP_const2s:
748 printf ("DW_OP_const2s: %ld", (long) byte_get_signed (data, 2));
749 data += 2;
750 break;
751 case DW_OP_const4u:
752 printf ("DW_OP_const4u: %lu", (unsigned long) byte_get (data, 4));
753 data += 4;
754 break;
755 case DW_OP_const4s:
756 printf ("DW_OP_const4s: %ld", (long) byte_get_signed (data, 4));
757 data += 4;
758 break;
759 case DW_OP_const8u:
760 printf ("DW_OP_const8u: %lu %lu", (unsigned long) byte_get (data, 4),
761 (unsigned long) byte_get (data + 4, 4));
762 data += 8;
763 break;
764 case DW_OP_const8s:
765 printf ("DW_OP_const8s: %ld %ld", (long) byte_get (data, 4),
766 (long) byte_get (data + 4, 4));
767 data += 8;
768 break;
769 case DW_OP_constu:
770 printf ("DW_OP_constu: %s",
771 dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
772 data += bytes_read;
773 break;
774 case DW_OP_consts:
775 printf ("DW_OP_consts: %s",
776 dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read)));
777 data += bytes_read;
778 break;
779 case DW_OP_dup:
780 printf ("DW_OP_dup");
781 break;
782 case DW_OP_drop:
783 printf ("DW_OP_drop");
784 break;
785 case DW_OP_over:
786 printf ("DW_OP_over");
787 break;
788 case DW_OP_pick:
789 printf ("DW_OP_pick: %ld", (unsigned long) byte_get (data++, 1));
790 break;
791 case DW_OP_swap:
792 printf ("DW_OP_swap");
793 break;
794 case DW_OP_rot:
795 printf ("DW_OP_rot");
796 break;
797 case DW_OP_xderef:
798 printf ("DW_OP_xderef");
799 break;
800 case DW_OP_abs:
801 printf ("DW_OP_abs");
802 break;
803 case DW_OP_and:
804 printf ("DW_OP_and");
805 break;
806 case DW_OP_div:
807 printf ("DW_OP_div");
808 break;
809 case DW_OP_minus:
810 printf ("DW_OP_minus");
811 break;
812 case DW_OP_mod:
813 printf ("DW_OP_mod");
814 break;
815 case DW_OP_mul:
816 printf ("DW_OP_mul");
817 break;
818 case DW_OP_neg:
819 printf ("DW_OP_neg");
820 break;
821 case DW_OP_not:
822 printf ("DW_OP_not");
823 break;
824 case DW_OP_or:
825 printf ("DW_OP_or");
826 break;
827 case DW_OP_plus:
828 printf ("DW_OP_plus");
829 break;
830 case DW_OP_plus_uconst:
831 printf ("DW_OP_plus_uconst: %s",
832 dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
833 data += bytes_read;
834 break;
835 case DW_OP_shl:
836 printf ("DW_OP_shl");
837 break;
838 case DW_OP_shr:
839 printf ("DW_OP_shr");
840 break;
841 case DW_OP_shra:
842 printf ("DW_OP_shra");
843 break;
844 case DW_OP_xor:
845 printf ("DW_OP_xor");
846 break;
847 case DW_OP_bra:
848 printf ("DW_OP_bra: %ld", (long) byte_get_signed (data, 2));
849 data += 2;
850 break;
851 case DW_OP_eq:
852 printf ("DW_OP_eq");
853 break;
854 case DW_OP_ge:
855 printf ("DW_OP_ge");
856 break;
857 case DW_OP_gt:
858 printf ("DW_OP_gt");
859 break;
860 case DW_OP_le:
861 printf ("DW_OP_le");
862 break;
863 case DW_OP_lt:
864 printf ("DW_OP_lt");
865 break;
866 case DW_OP_ne:
867 printf ("DW_OP_ne");
868 break;
869 case DW_OP_skip:
870 printf ("DW_OP_skip: %ld", (long) byte_get_signed (data, 2));
871 data += 2;
872 break;
873
874 case DW_OP_lit0:
875 case DW_OP_lit1:
876 case DW_OP_lit2:
877 case DW_OP_lit3:
878 case DW_OP_lit4:
879 case DW_OP_lit5:
880 case DW_OP_lit6:
881 case DW_OP_lit7:
882 case DW_OP_lit8:
883 case DW_OP_lit9:
884 case DW_OP_lit10:
885 case DW_OP_lit11:
886 case DW_OP_lit12:
887 case DW_OP_lit13:
888 case DW_OP_lit14:
889 case DW_OP_lit15:
890 case DW_OP_lit16:
891 case DW_OP_lit17:
892 case DW_OP_lit18:
893 case DW_OP_lit19:
894 case DW_OP_lit20:
895 case DW_OP_lit21:
896 case DW_OP_lit22:
897 case DW_OP_lit23:
898 case DW_OP_lit24:
899 case DW_OP_lit25:
900 case DW_OP_lit26:
901 case DW_OP_lit27:
902 case DW_OP_lit28:
903 case DW_OP_lit29:
904 case DW_OP_lit30:
905 case DW_OP_lit31:
906 printf ("DW_OP_lit%d", op - DW_OP_lit0);
907 break;
908
909 case DW_OP_reg0:
910 case DW_OP_reg1:
911 case DW_OP_reg2:
912 case DW_OP_reg3:
913 case DW_OP_reg4:
914 case DW_OP_reg5:
915 case DW_OP_reg6:
916 case DW_OP_reg7:
917 case DW_OP_reg8:
918 case DW_OP_reg9:
919 case DW_OP_reg10:
920 case DW_OP_reg11:
921 case DW_OP_reg12:
922 case DW_OP_reg13:
923 case DW_OP_reg14:
924 case DW_OP_reg15:
925 case DW_OP_reg16:
926 case DW_OP_reg17:
927 case DW_OP_reg18:
928 case DW_OP_reg19:
929 case DW_OP_reg20:
930 case DW_OP_reg21:
931 case DW_OP_reg22:
932 case DW_OP_reg23:
933 case DW_OP_reg24:
934 case DW_OP_reg25:
935 case DW_OP_reg26:
936 case DW_OP_reg27:
937 case DW_OP_reg28:
938 case DW_OP_reg29:
939 case DW_OP_reg30:
940 case DW_OP_reg31:
941 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
942 regname (op - DW_OP_reg0, 1));
943 break;
944
945 case DW_OP_breg0:
946 case DW_OP_breg1:
947 case DW_OP_breg2:
948 case DW_OP_breg3:
949 case DW_OP_breg4:
950 case DW_OP_breg5:
951 case DW_OP_breg6:
952 case DW_OP_breg7:
953 case DW_OP_breg8:
954 case DW_OP_breg9:
955 case DW_OP_breg10:
956 case DW_OP_breg11:
957 case DW_OP_breg12:
958 case DW_OP_breg13:
959 case DW_OP_breg14:
960 case DW_OP_breg15:
961 case DW_OP_breg16:
962 case DW_OP_breg17:
963 case DW_OP_breg18:
964 case DW_OP_breg19:
965 case DW_OP_breg20:
966 case DW_OP_breg21:
967 case DW_OP_breg22:
968 case DW_OP_breg23:
969 case DW_OP_breg24:
970 case DW_OP_breg25:
971 case DW_OP_breg26:
972 case DW_OP_breg27:
973 case DW_OP_breg28:
974 case DW_OP_breg29:
975 case DW_OP_breg30:
976 case DW_OP_breg31:
977 printf ("DW_OP_breg%d (%s): %s",
978 op - DW_OP_breg0,
979 regname (op - DW_OP_breg0, 1),
980 dwarf_vmatoa ("d", (dwarf_signed_vma)
981 read_leb128 (data, &bytes_read, 1)));
982 data += bytes_read;
983 break;
984
985 case DW_OP_regx:
986 uvalue = read_leb128 (data, &bytes_read, 0);
987 data += bytes_read;
988 printf ("DW_OP_regx: %s (%s)",
989 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
990 break;
991 case DW_OP_fbreg:
992 need_frame_base = 1;
993 printf ("DW_OP_fbreg: %s",
994 dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read)));
995 data += bytes_read;
996 break;
997 case DW_OP_bregx:
998 uvalue = read_leb128 (data, &bytes_read, 0);
999 data += bytes_read;
1000 printf ("DW_OP_bregx: %s (%s) %s",
1001 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
1002 dwarf_vmatoa ("d", read_sleb128 (data, &bytes_read)));
1003 data += bytes_read;
1004 break;
1005 case DW_OP_piece:
1006 printf ("DW_OP_piece: %s",
1007 dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
1008 data += bytes_read;
1009 break;
1010 case DW_OP_deref_size:
1011 printf ("DW_OP_deref_size: %ld", (long) byte_get (data++, 1));
1012 break;
1013 case DW_OP_xderef_size:
1014 printf ("DW_OP_xderef_size: %ld", (long) byte_get (data++, 1));
1015 break;
1016 case DW_OP_nop:
1017 printf ("DW_OP_nop");
1018 break;
1019
1020 /* DWARF 3 extensions. */
1021 case DW_OP_push_object_address:
1022 printf ("DW_OP_push_object_address");
1023 break;
1024 case DW_OP_call2:
1025 /* XXX: Strictly speaking for 64-bit DWARF3 files
1026 this ought to be an 8-byte wide computation. */
1027 printf ("DW_OP_call2: <0x%s>",
1028 dwarf_vmatoa ("x", (dwarf_signed_vma) byte_get (data, 2)
1029 + cu_offset));
1030 data += 2;
1031 break;
1032 case DW_OP_call4:
1033 /* XXX: Strictly speaking for 64-bit DWARF3 files
1034 this ought to be an 8-byte wide computation. */
1035 printf ("DW_OP_call4: <0x%s>",
1036 dwarf_vmatoa ("x", (dwarf_signed_vma) byte_get (data, 4)
1037 + cu_offset));
1038 data += 4;
1039 break;
1040 case DW_OP_call_ref:
1041 /* XXX: Strictly speaking for 64-bit DWARF3 files
1042 this ought to be an 8-byte wide computation. */
1043 if (dwarf_version == -1)
1044 {
1045 printf (_("(DW_OP_call_ref in frame info)"));
1046 /* No way to tell where the next op is, so just bail. */
1047 return need_frame_base;
1048 }
1049 if (dwarf_version == 2)
1050 {
1051 printf ("DW_OP_call_ref: <0x%s>",
1052 dwarf_vmatoa ("x", byte_get (data, pointer_size)));
1053 data += pointer_size;
1054 }
1055 else
1056 {
1057 printf ("DW_OP_call_ref: <0x%s>",
1058 dwarf_vmatoa ("x", byte_get (data, offset_size)));
1059 data += offset_size;
1060 }
1061 break;
1062 case DW_OP_form_tls_address:
1063 printf ("DW_OP_form_tls_address");
1064 break;
1065 case DW_OP_call_frame_cfa:
1066 printf ("DW_OP_call_frame_cfa");
1067 break;
1068 case DW_OP_bit_piece:
1069 printf ("DW_OP_bit_piece: ");
1070 printf (_("size: %s "),
1071 dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
1072 data += bytes_read;
1073 printf (_("offset: %s "),
1074 dwarf_vmatoa ("u", read_leb128 (data, &bytes_read, 0)));
1075 data += bytes_read;
1076 break;
1077
1078 /* DWARF 4 extensions. */
1079 case DW_OP_stack_value:
1080 printf ("DW_OP_stack_value");
1081 break;
1082
1083 case DW_OP_implicit_value:
1084 printf ("DW_OP_implicit_value");
1085 uvalue = read_leb128 (data, &bytes_read, 0);
1086 data += bytes_read;
1087 display_block (data, uvalue);
1088 data += uvalue;
1089 break;
1090
1091 /* GNU extensions. */
1092 case DW_OP_GNU_push_tls_address:
1093 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1094 break;
1095 case DW_OP_GNU_uninit:
1096 printf ("DW_OP_GNU_uninit");
1097 /* FIXME: Is there data associated with this OP ? */
1098 break;
1099 case DW_OP_GNU_encoded_addr:
1100 {
1101 int encoding;
1102 dwarf_vma addr;
1103
1104 encoding = *data++;
1105 addr = get_encoded_value (data, encoding, section);
1106 data += size_of_encoded_value (encoding);
1107
1108 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1109 print_dwarf_vma (addr, pointer_size);
1110 }
1111 break;
1112 case DW_OP_GNU_implicit_pointer:
1113 /* XXX: Strictly speaking for 64-bit DWARF3 files
1114 this ought to be an 8-byte wide computation. */
1115 if (dwarf_version == -1)
1116 {
1117 printf (_("(DW_OP_GNU_implicit_pointer in frame info)"));
1118 /* No way to tell where the next op is, so just bail. */
1119 return need_frame_base;
1120 }
1121 if (dwarf_version == 2)
1122 {
1123 printf ("DW_OP_GNU_implicit_pointer: <0x%s> %s",
1124 dwarf_vmatoa ("x", byte_get (data, pointer_size)),
1125 dwarf_vmatoa ("d", read_sleb128 (data + pointer_size,
1126 &bytes_read)));
1127 data += pointer_size + bytes_read;
1128 }
1129 else
1130 {
1131 printf ("DW_OP_GNU_implicit_pointer: <0x%s> %s",
1132 dwarf_vmatoa ("x", byte_get (data, offset_size)),
1133 dwarf_vmatoa ("d", read_sleb128 (data + offset_size,
1134 &bytes_read)));
1135 data += offset_size + bytes_read;
1136 }
1137 break;
1138 case DW_OP_GNU_entry_value:
1139 uvalue = read_leb128 (data, &bytes_read, 0);
1140 data += bytes_read;
1141 printf ("DW_OP_GNU_entry_value: (");
1142 if (decode_location_expression (data, pointer_size, offset_size,
1143 dwarf_version, uvalue,
1144 cu_offset, section))
1145 need_frame_base = 1;
1146 putchar (')');
1147 data += uvalue;
1148 break;
1149 case DW_OP_GNU_const_type:
1150 uvalue = read_leb128 (data, &bytes_read, 0);
1151 data += bytes_read;
1152 printf ("DW_OP_GNU_const_type: <0x%s> ",
1153 dwarf_vmatoa ("x", cu_offset + uvalue));
1154 uvalue = byte_get (data++, 1);
1155 display_block (data, uvalue);
1156 data += uvalue;
1157 break;
1158 case DW_OP_GNU_regval_type:
1159 uvalue = read_leb128 (data, &bytes_read, 0);
1160 data += bytes_read;
1161 printf ("DW_OP_GNU_regval_type: %s (%s)",
1162 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1163 uvalue = read_leb128 (data, &bytes_read, 0);
1164 data += bytes_read;
1165 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1166 break;
1167 case DW_OP_GNU_deref_type:
1168 printf ("DW_OP_GNU_deref_type: %ld", (long) byte_get (data++, 1));
1169 uvalue = read_leb128 (data, &bytes_read, 0);
1170 data += bytes_read;
1171 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1172 break;
1173 case DW_OP_GNU_convert:
1174 uvalue = read_leb128 (data, &bytes_read, 0);
1175 data += bytes_read;
1176 printf ("DW_OP_GNU_convert <0x%s>",
1177 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1178 break;
1179 case DW_OP_GNU_reinterpret:
1180 uvalue = read_leb128 (data, &bytes_read, 0);
1181 data += bytes_read;
1182 printf ("DW_OP_GNU_reinterpret <0x%s>",
1183 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1184 break;
1185 case DW_OP_GNU_parameter_ref:
1186 printf ("DW_OP_GNU_parameter_ref: <0x%s>",
1187 dwarf_vmatoa ("x", cu_offset + byte_get (data, 4)));
1188 data += 4;
1189 break;
1190 case DW_OP_GNU_addr_index:
1191 uvalue = read_leb128 (data, &bytes_read, 0);
1192 data += bytes_read;
1193 printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1194 break;
1195 case DW_OP_GNU_const_index:
1196 uvalue = read_leb128 (data, &bytes_read, 0);
1197 data += bytes_read;
1198 printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1199 break;
1200
1201 /* HP extensions. */
1202 case DW_OP_HP_is_value:
1203 printf ("DW_OP_HP_is_value");
1204 /* FIXME: Is there data associated with this OP ? */
1205 break;
1206 case DW_OP_HP_fltconst4:
1207 printf ("DW_OP_HP_fltconst4");
1208 /* FIXME: Is there data associated with this OP ? */
1209 break;
1210 case DW_OP_HP_fltconst8:
1211 printf ("DW_OP_HP_fltconst8");
1212 /* FIXME: Is there data associated with this OP ? */
1213 break;
1214 case DW_OP_HP_mod_range:
1215 printf ("DW_OP_HP_mod_range");
1216 /* FIXME: Is there data associated with this OP ? */
1217 break;
1218 case DW_OP_HP_unmod_range:
1219 printf ("DW_OP_HP_unmod_range");
1220 /* FIXME: Is there data associated with this OP ? */
1221 break;
1222 case DW_OP_HP_tls:
1223 printf ("DW_OP_HP_tls");
1224 /* FIXME: Is there data associated with this OP ? */
1225 break;
1226
1227 /* PGI (STMicroelectronics) extensions. */
1228 case DW_OP_PGI_omp_thread_num:
1229 /* Pushes the thread number for the current thread as it would be
1230 returned by the standard OpenMP library function:
1231 omp_get_thread_num(). The "current thread" is the thread for
1232 which the expression is being evaluated. */
1233 printf ("DW_OP_PGI_omp_thread_num");
1234 break;
1235
1236 default:
1237 if (op >= DW_OP_lo_user
1238 && op <= DW_OP_hi_user)
1239 printf (_("(User defined location op)"));
1240 else
1241 printf (_("(Unknown location op)"));
1242 /* No way to tell where the next op is, so just bail. */
1243 return need_frame_base;
1244 }
1245
1246 /* Separate the ops. */
1247 if (data < end)
1248 printf ("; ");
1249 }
1250
1251 return need_frame_base;
1252 }
1253
1254 static unsigned char *
1255 read_and_display_attr_value (unsigned long attribute,
1256 unsigned long form,
1257 unsigned char * data,
1258 dwarf_vma cu_offset,
1259 dwarf_vma pointer_size,
1260 dwarf_vma offset_size,
1261 int dwarf_version,
1262 debug_info * debug_info_p,
1263 int do_loc,
1264 struct dwarf_section * section)
1265 {
1266 dwarf_vma uvalue = 0;
1267 unsigned char *block_start = NULL;
1268 unsigned char * orig_data = data;
1269 unsigned int bytes_read;
1270
1271 switch (form)
1272 {
1273 default:
1274 break;
1275
1276 case DW_FORM_ref_addr:
1277 if (dwarf_version == 2)
1278 {
1279 uvalue = byte_get (data, pointer_size);
1280 data += pointer_size;
1281 }
1282 else if (dwarf_version == 3 || dwarf_version == 4)
1283 {
1284 uvalue = byte_get (data, offset_size);
1285 data += offset_size;
1286 }
1287 else
1288 error (_("Internal error: DWARF version is not 2, 3 or 4.\n"));
1289
1290 break;
1291
1292 case DW_FORM_addr:
1293 uvalue = byte_get (data, pointer_size);
1294 data += pointer_size;
1295 break;
1296
1297 case DW_FORM_strp:
1298 case DW_FORM_sec_offset:
1299 case DW_FORM_GNU_ref_alt:
1300 case DW_FORM_GNU_strp_alt:
1301 uvalue = byte_get (data, offset_size);
1302 data += offset_size;
1303 break;
1304
1305 case DW_FORM_flag_present:
1306 uvalue = 1;
1307 break;
1308
1309 case DW_FORM_ref1:
1310 case DW_FORM_flag:
1311 case DW_FORM_data1:
1312 uvalue = byte_get (data++, 1);
1313 break;
1314
1315 case DW_FORM_ref2:
1316 case DW_FORM_data2:
1317 uvalue = byte_get (data, 2);
1318 data += 2;
1319 break;
1320
1321 case DW_FORM_ref4:
1322 case DW_FORM_data4:
1323 uvalue = byte_get (data, 4);
1324 data += 4;
1325 break;
1326
1327 case DW_FORM_sdata:
1328 uvalue = read_leb128 (data, & bytes_read, 1);
1329 data += bytes_read;
1330 break;
1331
1332 case DW_FORM_GNU_str_index:
1333 uvalue = read_leb128 (data, & bytes_read, 0);
1334 data += bytes_read;
1335 break;
1336
1337 case DW_FORM_ref_udata:
1338 case DW_FORM_udata:
1339 uvalue = read_leb128 (data, & bytes_read, 0);
1340 data += bytes_read;
1341 break;
1342
1343 case DW_FORM_indirect:
1344 form = read_leb128 (data, & bytes_read, 0);
1345 data += bytes_read;
1346 if (!do_loc)
1347 printf (" %s", get_FORM_name (form));
1348 return read_and_display_attr_value (attribute, form, data,
1349 cu_offset, pointer_size,
1350 offset_size, dwarf_version,
1351 debug_info_p, do_loc,
1352 section);
1353 case DW_FORM_GNU_addr_index:
1354 uvalue = read_leb128 (data, & bytes_read, 0);
1355 data += bytes_read;
1356 break;
1357 }
1358
1359 switch (form)
1360 {
1361 case DW_FORM_ref_addr:
1362 if (!do_loc)
1363 printf (" <0x%s>", dwarf_vmatoa ("x",uvalue));
1364 break;
1365
1366 case DW_FORM_GNU_ref_alt:
1367 if (!do_loc)
1368 printf (" <alt 0x%s>", dwarf_vmatoa ("x",uvalue));
1369 break;
1370
1371 case DW_FORM_ref1:
1372 case DW_FORM_ref2:
1373 case DW_FORM_ref4:
1374 case DW_FORM_ref_udata:
1375 if (!do_loc)
1376 printf (" <0x%s>", dwarf_vmatoa ("x", uvalue + cu_offset));
1377 break;
1378
1379 case DW_FORM_data4:
1380 case DW_FORM_addr:
1381 case DW_FORM_sec_offset:
1382 if (!do_loc)
1383 printf (" 0x%s", dwarf_vmatoa ("x", uvalue));
1384 break;
1385
1386 case DW_FORM_flag_present:
1387 case DW_FORM_flag:
1388 case DW_FORM_data1:
1389 case DW_FORM_data2:
1390 case DW_FORM_sdata:
1391 case DW_FORM_udata:
1392 if (!do_loc)
1393 printf (" %s", dwarf_vmatoa ("d", uvalue));
1394 break;
1395
1396 case DW_FORM_ref8:
1397 case DW_FORM_data8:
1398 if (!do_loc)
1399 {
1400 dwarf_vma high_bits;
1401 char buf[64];
1402
1403 byte_get_64 (data, &high_bits, &uvalue);
1404 printf (" 0x%s",
1405 dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
1406 }
1407 if ((do_loc || do_debug_loc || do_debug_ranges)
1408 && num_debug_info_entries == 0)
1409 {
1410 if (sizeof (uvalue) == 8)
1411 uvalue = byte_get (data, 8);
1412 else
1413 error (_("DW_FORM_data8 is unsupported when sizeof (dwarf_vma) != 8\n"));
1414 }
1415 data += 8;
1416 break;
1417
1418 case DW_FORM_string:
1419 if (!do_loc)
1420 printf (" %s", data);
1421 data += strlen ((char *) data) + 1;
1422 break;
1423
1424 case DW_FORM_block:
1425 case DW_FORM_exprloc:
1426 uvalue = read_leb128 (data, & bytes_read, 0);
1427 block_start = data + bytes_read;
1428 if (do_loc)
1429 data = block_start + uvalue;
1430 else
1431 data = display_block (block_start, uvalue);
1432 break;
1433
1434 case DW_FORM_block1:
1435 uvalue = byte_get (data, 1);
1436 block_start = data + 1;
1437 if (do_loc)
1438 data = block_start + uvalue;
1439 else
1440 data = display_block (block_start, uvalue);
1441 break;
1442
1443 case DW_FORM_block2:
1444 uvalue = byte_get (data, 2);
1445 block_start = data + 2;
1446 if (do_loc)
1447 data = block_start + uvalue;
1448 else
1449 data = display_block (block_start, uvalue);
1450 break;
1451
1452 case DW_FORM_block4:
1453 uvalue = byte_get (data, 4);
1454 block_start = data + 4;
1455 if (do_loc)
1456 data = block_start + uvalue;
1457 else
1458 data = display_block (block_start, uvalue);
1459 break;
1460
1461 case DW_FORM_strp:
1462 if (!do_loc)
1463 printf (_(" (indirect string, offset: 0x%s): %s"),
1464 dwarf_vmatoa ("x", uvalue),
1465 fetch_indirect_string (uvalue));
1466 break;
1467
1468 case DW_FORM_GNU_str_index:
1469 if (!do_loc)
1470 {
1471 const char *suffix = strrchr (section->name, '.');
1472 int dwo = (suffix && strcmp (suffix, ".dwo") == 0) ? 1 : 0;
1473
1474 printf (_(" (indexed string: 0x%s): %s"),
1475 dwarf_vmatoa ("x", uvalue),
1476 fetch_indexed_string (uvalue, offset_size, dwo));
1477 }
1478 break;
1479
1480 case DW_FORM_GNU_strp_alt:
1481 if (!do_loc)
1482 printf (_(" (alt indirect string, offset: 0x%s)"),
1483 dwarf_vmatoa ("x", uvalue));
1484 break;
1485
1486 case DW_FORM_indirect:
1487 /* Handled above. */
1488 break;
1489
1490 case DW_FORM_ref_sig8:
1491 if (!do_loc)
1492 {
1493 dwarf_vma high_bits;
1494 char buf[64];
1495
1496 byte_get_64 (data, &high_bits, &uvalue);
1497 printf (" signature: 0x%s",
1498 dwarf_vmatoa64 (high_bits, uvalue, buf, sizeof (buf)));
1499 }
1500 data += 8;
1501 break;
1502
1503 case DW_FORM_GNU_addr_index:
1504 if (!do_loc)
1505 printf (_(" (addr_index: 0x%s): %s"),
1506 dwarf_vmatoa ("x", uvalue),
1507 fetch_indexed_value (uvalue * pointer_size, pointer_size));
1508 break;
1509
1510 default:
1511 warn (_("Unrecognized form: %lu\n"), form);
1512 break;
1513 }
1514
1515 if ((do_loc || do_debug_loc || do_debug_ranges)
1516 && num_debug_info_entries == 0
1517 && debug_info_p != NULL)
1518 {
1519 switch (attribute)
1520 {
1521 case DW_AT_frame_base:
1522 have_frame_base = 1;
1523 case DW_AT_location:
1524 case DW_AT_string_length:
1525 case DW_AT_return_addr:
1526 case DW_AT_data_member_location:
1527 case DW_AT_vtable_elem_location:
1528 case DW_AT_segment:
1529 case DW_AT_static_link:
1530 case DW_AT_use_location:
1531 case DW_AT_GNU_call_site_value:
1532 case DW_AT_GNU_call_site_data_value:
1533 case DW_AT_GNU_call_site_target:
1534 case DW_AT_GNU_call_site_target_clobbered:
1535 if ((dwarf_version < 4
1536 && (form == DW_FORM_data4 || form == DW_FORM_data8))
1537 || form == DW_FORM_sec_offset)
1538 {
1539 /* Process location list. */
1540 unsigned int lmax = debug_info_p->max_loc_offsets;
1541 unsigned int num = debug_info_p->num_loc_offsets;
1542
1543 if (lmax == 0 || num >= lmax)
1544 {
1545 lmax += 1024;
1546 debug_info_p->loc_offsets = (dwarf_vma *)
1547 xcrealloc (debug_info_p->loc_offsets,
1548 lmax, sizeof (*debug_info_p->loc_offsets));
1549 debug_info_p->have_frame_base = (int *)
1550 xcrealloc (debug_info_p->have_frame_base,
1551 lmax, sizeof (*debug_info_p->have_frame_base));
1552 debug_info_p->max_loc_offsets = lmax;
1553 }
1554 debug_info_p->loc_offsets [num] = uvalue;
1555 debug_info_p->have_frame_base [num] = have_frame_base;
1556 debug_info_p->num_loc_offsets++;
1557 }
1558 break;
1559
1560 case DW_AT_low_pc:
1561 if (need_base_address)
1562 debug_info_p->base_address = uvalue;
1563 break;
1564
1565 case DW_AT_GNU_addr_base:
1566 debug_info_p->addr_base = uvalue;
1567 break;
1568
1569 case DW_AT_GNU_ranges_base:
1570 debug_info_p->ranges_base = uvalue;
1571 break;
1572
1573 case DW_AT_ranges:
1574 if ((dwarf_version < 4
1575 && (form == DW_FORM_data4 || form == DW_FORM_data8))
1576 || form == DW_FORM_sec_offset)
1577 {
1578 /* Process range list. */
1579 unsigned int lmax = debug_info_p->max_range_lists;
1580 unsigned int num = debug_info_p->num_range_lists;
1581
1582 if (lmax == 0 || num >= lmax)
1583 {
1584 lmax += 1024;
1585 debug_info_p->range_lists = (dwarf_vma *)
1586 xcrealloc (debug_info_p->range_lists,
1587 lmax, sizeof (*debug_info_p->range_lists));
1588 debug_info_p->max_range_lists = lmax;
1589 }
1590 debug_info_p->range_lists [num] = uvalue;
1591 debug_info_p->num_range_lists++;
1592 }
1593 break;
1594
1595 default:
1596 break;
1597 }
1598 }
1599
1600 if (do_loc || attribute == 0)
1601 return data;
1602
1603 /* For some attributes we can display further information. */
1604 printf ("\t");
1605
1606 switch (attribute)
1607 {
1608 case DW_AT_inline:
1609 switch (uvalue)
1610 {
1611 case DW_INL_not_inlined:
1612 printf (_("(not inlined)"));
1613 break;
1614 case DW_INL_inlined:
1615 printf (_("(inlined)"));
1616 break;
1617 case DW_INL_declared_not_inlined:
1618 printf (_("(declared as inline but ignored)"));
1619 break;
1620 case DW_INL_declared_inlined:
1621 printf (_("(declared as inline and inlined)"));
1622 break;
1623 default:
1624 printf (_(" (Unknown inline attribute value: %s)"),
1625 dwarf_vmatoa ("x", uvalue));
1626 break;
1627 }
1628 break;
1629
1630 case DW_AT_language:
1631 switch (uvalue)
1632 {
1633 /* Ordered by the numeric value of these constants. */
1634 case DW_LANG_C89: printf ("(ANSI C)"); break;
1635 case DW_LANG_C: printf ("(non-ANSI C)"); break;
1636 case DW_LANG_Ada83: printf ("(Ada)"); break;
1637 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
1638 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
1639 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
1640 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
1641 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
1642 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
1643 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
1644 /* DWARF 2.1 values. */
1645 case DW_LANG_Java: printf ("(Java)"); break;
1646 case DW_LANG_C99: printf ("(ANSI C99)"); break;
1647 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
1648 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
1649 /* DWARF 3 values. */
1650 case DW_LANG_PLI: printf ("(PLI)"); break;
1651 case DW_LANG_ObjC: printf ("(Objective C)"); break;
1652 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
1653 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
1654 case DW_LANG_D: printf ("(D)"); break;
1655 /* DWARF 4 values. */
1656 case DW_LANG_Python: printf ("(Python)"); break;
1657 /* DWARF 5 values. */
1658 case DW_LANG_Go: printf ("(Go)"); break;
1659 /* MIPS extension. */
1660 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
1661 /* UPC extension. */
1662 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
1663 default:
1664 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
1665 printf (_("(implementation defined: %s)"),
1666 dwarf_vmatoa ("x", uvalue));
1667 else
1668 printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
1669 break;
1670 }
1671 break;
1672
1673 case DW_AT_encoding:
1674 switch (uvalue)
1675 {
1676 case DW_ATE_void: printf ("(void)"); break;
1677 case DW_ATE_address: printf ("(machine address)"); break;
1678 case DW_ATE_boolean: printf ("(boolean)"); break;
1679 case DW_ATE_complex_float: printf ("(complex float)"); break;
1680 case DW_ATE_float: printf ("(float)"); break;
1681 case DW_ATE_signed: printf ("(signed)"); break;
1682 case DW_ATE_signed_char: printf ("(signed char)"); break;
1683 case DW_ATE_unsigned: printf ("(unsigned)"); break;
1684 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
1685 /* DWARF 2.1 values: */
1686 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
1687 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
1688 /* DWARF 3 values: */
1689 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
1690 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
1691 case DW_ATE_edited: printf ("(edited)"); break;
1692 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
1693 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
1694 /* HP extensions: */
1695 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
1696 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
1697 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
1698 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
1699 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
1700 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
1701 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
1702
1703 default:
1704 if (uvalue >= DW_ATE_lo_user
1705 && uvalue <= DW_ATE_hi_user)
1706 printf (_("(user defined type)"));
1707 else
1708 printf (_("(unknown type)"));
1709 break;
1710 }
1711 break;
1712
1713 case DW_AT_accessibility:
1714 switch (uvalue)
1715 {
1716 case DW_ACCESS_public: printf ("(public)"); break;
1717 case DW_ACCESS_protected: printf ("(protected)"); break;
1718 case DW_ACCESS_private: printf ("(private)"); break;
1719 default:
1720 printf (_("(unknown accessibility)"));
1721 break;
1722 }
1723 break;
1724
1725 case DW_AT_visibility:
1726 switch (uvalue)
1727 {
1728 case DW_VIS_local: printf ("(local)"); break;
1729 case DW_VIS_exported: printf ("(exported)"); break;
1730 case DW_VIS_qualified: printf ("(qualified)"); break;
1731 default: printf (_("(unknown visibility)")); break;
1732 }
1733 break;
1734
1735 case DW_AT_virtuality:
1736 switch (uvalue)
1737 {
1738 case DW_VIRTUALITY_none: printf ("(none)"); break;
1739 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
1740 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
1741 default: printf (_("(unknown virtuality)")); break;
1742 }
1743 break;
1744
1745 case DW_AT_identifier_case:
1746 switch (uvalue)
1747 {
1748 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
1749 case DW_ID_up_case: printf ("(up_case)"); break;
1750 case DW_ID_down_case: printf ("(down_case)"); break;
1751 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
1752 default: printf (_("(unknown case)")); break;
1753 }
1754 break;
1755
1756 case DW_AT_calling_convention:
1757 switch (uvalue)
1758 {
1759 case DW_CC_normal: printf ("(normal)"); break;
1760 case DW_CC_program: printf ("(program)"); break;
1761 case DW_CC_nocall: printf ("(nocall)"); break;
1762 default:
1763 if (uvalue >= DW_CC_lo_user
1764 && uvalue <= DW_CC_hi_user)
1765 printf (_("(user defined)"));
1766 else
1767 printf (_("(unknown convention)"));
1768 }
1769 break;
1770
1771 case DW_AT_ordering:
1772 switch (uvalue)
1773 {
1774 case -1: printf (_("(undefined)")); break;
1775 case 0: printf ("(row major)"); break;
1776 case 1: printf ("(column major)"); break;
1777 }
1778 break;
1779
1780 case DW_AT_frame_base:
1781 have_frame_base = 1;
1782 case DW_AT_location:
1783 case DW_AT_string_length:
1784 case DW_AT_return_addr:
1785 case DW_AT_data_member_location:
1786 case DW_AT_vtable_elem_location:
1787 case DW_AT_segment:
1788 case DW_AT_static_link:
1789 case DW_AT_use_location:
1790 case DW_AT_GNU_call_site_value:
1791 case DW_AT_GNU_call_site_data_value:
1792 case DW_AT_GNU_call_site_target:
1793 case DW_AT_GNU_call_site_target_clobbered:
1794 if ((dwarf_version < 4
1795 && (form == DW_FORM_data4 || form == DW_FORM_data8))
1796 || form == DW_FORM_sec_offset)
1797 printf (_("(location list)"));
1798 /* Fall through. */
1799 case DW_AT_allocated:
1800 case DW_AT_associated:
1801 case DW_AT_data_location:
1802 case DW_AT_stride:
1803 case DW_AT_upper_bound:
1804 case DW_AT_lower_bound:
1805 if (block_start)
1806 {
1807 int need_frame_base;
1808
1809 printf ("(");
1810 need_frame_base = decode_location_expression (block_start,
1811 pointer_size,
1812 offset_size,
1813 dwarf_version,
1814 uvalue,
1815 cu_offset, section);
1816 printf (")");
1817 if (need_frame_base && !have_frame_base)
1818 printf (_(" [without DW_AT_frame_base]"));
1819 }
1820 break;
1821
1822 case DW_AT_import:
1823 {
1824 if (form == DW_FORM_ref_sig8
1825 || form == DW_FORM_GNU_ref_alt)
1826 break;
1827
1828 if (form == DW_FORM_ref1
1829 || form == DW_FORM_ref2
1830 || form == DW_FORM_ref4
1831 || form == DW_FORM_ref_udata)
1832 uvalue += cu_offset;
1833
1834 if (uvalue >= section->size)
1835 warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset %lx is too big.\n"),
1836 dwarf_vmatoa ("x", uvalue),
1837 (unsigned long) (orig_data - section->start));
1838 else
1839 {
1840 unsigned long abbrev_number;
1841 abbrev_entry * entry;
1842
1843 abbrev_number = read_leb128 (section->start + uvalue, NULL, 0);
1844
1845 printf (_("[Abbrev Number: %ld"), abbrev_number);
1846 /* Don't look up abbrev for DW_FORM_ref_addr, as it very often will
1847 use different abbrev table, and we don't track .debug_info chunks
1848 yet. */
1849 if (form != DW_FORM_ref_addr)
1850 {
1851 for (entry = first_abbrev; entry != NULL; entry = entry->next)
1852 if (entry->entry == abbrev_number)
1853 break;
1854 if (entry != NULL)
1855 printf (" (%s)", get_TAG_name (entry->tag));
1856 }
1857 printf ("]");
1858 }
1859 }
1860 break;
1861
1862 default:
1863 break;
1864 }
1865
1866 return data;
1867 }
1868
1869 static const char *
1870 get_AT_name (unsigned long attribute)
1871 {
1872 const char *name;
1873
1874 if (attribute == 0)
1875 return "DW_AT value: 0";
1876
1877 /* One value is shared by the MIPS and HP extensions: */
1878 if (attribute == DW_AT_MIPS_fde)
1879 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1880
1881 name = get_DW_AT_name (attribute);
1882
1883 if (name == NULL)
1884 {
1885 static char buffer[100];
1886
1887 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1888 attribute);
1889 return buffer;
1890 }
1891
1892 return name;
1893 }
1894
1895 static unsigned char *
1896 read_and_display_attr (unsigned long attribute,
1897 unsigned long form,
1898 unsigned char * data,
1899 dwarf_vma cu_offset,
1900 dwarf_vma pointer_size,
1901 dwarf_vma offset_size,
1902 int dwarf_version,
1903 debug_info * debug_info_p,
1904 int do_loc,
1905 struct dwarf_section * section)
1906 {
1907 if (!do_loc)
1908 printf (" %-18s:", get_AT_name (attribute));
1909 data = read_and_display_attr_value (attribute, form, data, cu_offset,
1910 pointer_size, offset_size,
1911 dwarf_version, debug_info_p,
1912 do_loc, section);
1913 if (!do_loc)
1914 printf ("\n");
1915 return data;
1916 }
1917
1918
1919 /* Process the contents of a .debug_info section. If do_loc is non-zero
1920 then we are scanning for location lists and we do not want to display
1921 anything to the user. If do_types is non-zero, we are processing
1922 a .debug_types section instead of a .debug_info section. */
1923
1924 static int
1925 process_debug_info (struct dwarf_section *section,
1926 void *file,
1927 enum dwarf_section_display_enum abbrev_sec,
1928 int do_loc,
1929 int do_types)
1930 {
1931 unsigned char *start = section->start;
1932 unsigned char *end = start + section->size;
1933 unsigned char *section_begin;
1934 unsigned int unit;
1935 unsigned int num_units = 0;
1936
1937 if ((do_loc || do_debug_loc || do_debug_ranges)
1938 && num_debug_info_entries == 0
1939 && ! do_types)
1940 {
1941 dwarf_vma length;
1942
1943 /* First scan the section to get the number of comp units. */
1944 for (section_begin = start, num_units = 0; section_begin < end;
1945 num_units ++)
1946 {
1947 /* Read the first 4 bytes. For a 32-bit DWARF section, this
1948 will be the length. For a 64-bit DWARF section, it'll be
1949 the escape code 0xffffffff followed by an 8 byte length. */
1950 length = byte_get (section_begin, 4);
1951
1952 if (length == 0xffffffff)
1953 {
1954 length = byte_get (section_begin + 4, 8);
1955 section_begin += length + 12;
1956 }
1957 else if (length >= 0xfffffff0 && length < 0xffffffff)
1958 {
1959 warn (_("Reserved length value (0x%s) found in section %s\n"),
1960 dwarf_vmatoa ("x", length), section->name);
1961 return 0;
1962 }
1963 else
1964 section_begin += length + 4;
1965
1966 /* Negative values are illegal, they may even cause infinite
1967 looping. This can happen if we can't accurately apply
1968 relocations to an object file. */
1969 if ((signed long) length <= 0)
1970 {
1971 warn (_("Corrupt unit length (0x%s) found in section %s\n"),
1972 dwarf_vmatoa ("x", length), section->name);
1973 return 0;
1974 }
1975 }
1976
1977 if (num_units == 0)
1978 {
1979 error (_("No comp units in %s section ?"), section->name);
1980 return 0;
1981 }
1982
1983 /* Then allocate an array to hold the information. */
1984 debug_information = (debug_info *) cmalloc (num_units,
1985 sizeof (* debug_information));
1986 if (debug_information == NULL)
1987 {
1988 error (_("Not enough memory for a debug info array of %u entries"),
1989 num_units);
1990 return 0;
1991 }
1992 }
1993
1994 if (!do_loc)
1995 {
1996 if (dwarf_start_die == 0)
1997 printf (_("Contents of the %s section:\n\n"), section->name);
1998
1999 load_debug_section (str, file);
2000 load_debug_section (str_dwo, file);
2001 load_debug_section (str_index, file);
2002 load_debug_section (str_index_dwo, file);
2003 load_debug_section (debug_addr, file);
2004 }
2005
2006 load_debug_section (abbrev_sec, file);
2007 if (debug_displays [abbrev_sec].section.start == NULL)
2008 {
2009 warn (_("Unable to locate %s section!\n"),
2010 debug_displays [abbrev_sec].section.name);
2011 return 0;
2012 }
2013
2014 for (section_begin = start, unit = 0; start < end; unit++)
2015 {
2016 DWARF2_Internal_CompUnit compunit;
2017 unsigned char *hdrptr;
2018 unsigned char *tags;
2019 int level, last_level, saved_level;
2020 dwarf_vma cu_offset;
2021 int offset_size;
2022 int initial_length_size;
2023 dwarf_vma signature_high = 0;
2024 dwarf_vma signature_low = 0;
2025 dwarf_vma type_offset = 0;
2026
2027 hdrptr = start;
2028
2029 compunit.cu_length = byte_get (hdrptr, 4);
2030 hdrptr += 4;
2031
2032 if (compunit.cu_length == 0xffffffff)
2033 {
2034 compunit.cu_length = byte_get (hdrptr, 8);
2035 hdrptr += 8;
2036 offset_size = 8;
2037 initial_length_size = 12;
2038 }
2039 else
2040 {
2041 offset_size = 4;
2042 initial_length_size = 4;
2043 }
2044
2045 compunit.cu_version = byte_get (hdrptr, 2);
2046 hdrptr += 2;
2047
2048 cu_offset = start - section_begin;
2049
2050 compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
2051 hdrptr += offset_size;
2052
2053 compunit.cu_pointer_size = byte_get (hdrptr, 1);
2054 hdrptr += 1;
2055
2056 if (do_types)
2057 {
2058 byte_get_64 (hdrptr, &signature_high, &signature_low);
2059 hdrptr += 8;
2060 type_offset = byte_get (hdrptr, offset_size);
2061 hdrptr += offset_size;
2062 }
2063
2064 if ((do_loc || do_debug_loc || do_debug_ranges)
2065 && num_debug_info_entries == 0
2066 && ! do_types)
2067 {
2068 debug_information [unit].cu_offset = cu_offset;
2069 debug_information [unit].pointer_size
2070 = compunit.cu_pointer_size;
2071 debug_information [unit].offset_size = offset_size;
2072 debug_information [unit].dwarf_version = compunit.cu_version;
2073 debug_information [unit].base_address = 0;
2074 debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
2075 debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
2076 debug_information [unit].loc_offsets = NULL;
2077 debug_information [unit].have_frame_base = NULL;
2078 debug_information [unit].max_loc_offsets = 0;
2079 debug_information [unit].num_loc_offsets = 0;
2080 debug_information [unit].range_lists = NULL;
2081 debug_information [unit].max_range_lists= 0;
2082 debug_information [unit].num_range_lists = 0;
2083 }
2084
2085 if (!do_loc && dwarf_start_die == 0)
2086 {
2087 printf (_(" Compilation Unit @ offset 0x%s:\n"),
2088 dwarf_vmatoa ("x", cu_offset));
2089 printf (_(" Length: 0x%s (%s)\n"),
2090 dwarf_vmatoa ("x", compunit.cu_length),
2091 offset_size == 8 ? "64-bit" : "32-bit");
2092 printf (_(" Version: %d\n"), compunit.cu_version);
2093 printf (_(" Abbrev Offset: 0x%s\n"),
2094 dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
2095 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
2096 if (do_types)
2097 {
2098 char buf[64];
2099
2100 printf (_(" Signature: 0x%s\n"),
2101 dwarf_vmatoa64 (signature_high, signature_low,
2102 buf, sizeof (buf)));
2103 printf (_(" Type Offset: 0x%s\n"),
2104 dwarf_vmatoa ("x", type_offset));
2105 }
2106 }
2107
2108 if (cu_offset + compunit.cu_length + initial_length_size
2109 > section->size)
2110 {
2111 warn (_("Debug info is corrupted, length of CU at %s"
2112 " extends beyond end of section (length = %s)\n"),
2113 dwarf_vmatoa ("x", cu_offset),
2114 dwarf_vmatoa ("x", compunit.cu_length));
2115 break;
2116 }
2117 tags = hdrptr;
2118 start += compunit.cu_length + initial_length_size;
2119
2120 if (compunit.cu_version != 2
2121 && compunit.cu_version != 3
2122 && compunit.cu_version != 4)
2123 {
2124 warn (_("CU at offset %s contains corrupt or "
2125 "unsupported version number: %d.\n"),
2126 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
2127 continue;
2128 }
2129
2130 free_abbrevs ();
2131
2132 /* Process the abbrevs used by this compilation unit. DWARF
2133 sections under Mach-O have non-zero addresses. */
2134 if (compunit.cu_abbrev_offset >= debug_displays [abbrev_sec].section.size)
2135 warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than abbrev section size (%lx)\n"),
2136 (unsigned long) compunit.cu_abbrev_offset,
2137 (unsigned long) debug_displays [abbrev_sec].section.size);
2138 else
2139 process_abbrev_section
2140 ((unsigned char *) debug_displays [abbrev_sec].section.start
2141 + compunit.cu_abbrev_offset,
2142 (unsigned char *) debug_displays [abbrev_sec].section.start
2143 + debug_displays [abbrev_sec].section.size);
2144
2145 level = 0;
2146 last_level = level;
2147 saved_level = -1;
2148 while (tags < start)
2149 {
2150 unsigned int bytes_read;
2151 unsigned long abbrev_number;
2152 unsigned long die_offset;
2153 abbrev_entry *entry;
2154 abbrev_attr *attr;
2155 int do_printing = 1;
2156
2157 die_offset = tags - section_begin;
2158
2159 abbrev_number = read_leb128 (tags, & bytes_read, 0);
2160 tags += bytes_read;
2161
2162 /* A null DIE marks the end of a list of siblings or it may also be
2163 a section padding. */
2164 if (abbrev_number == 0)
2165 {
2166 /* Check if it can be a section padding for the last CU. */
2167 if (level == 0 && start == end)
2168 {
2169 unsigned char *chk;
2170
2171 for (chk = tags; chk < start; chk++)
2172 if (*chk != 0)
2173 break;
2174 if (chk == start)
2175 break;
2176 }
2177
2178 if (!do_loc && die_offset >= dwarf_start_die)
2179 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
2180 level, die_offset);
2181
2182 --level;
2183 if (level < 0)
2184 {
2185 static unsigned num_bogus_warns = 0;
2186
2187 if (num_bogus_warns < 3)
2188 {
2189 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
2190 die_offset, section->name);
2191 num_bogus_warns ++;
2192 if (num_bogus_warns == 3)
2193 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
2194 }
2195 }
2196 if (dwarf_start_die != 0 && level < saved_level)
2197 return 1;
2198 continue;
2199 }
2200
2201 if (!do_loc)
2202 {
2203 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
2204 do_printing = 0;
2205 else
2206 {
2207 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
2208 saved_level = level;
2209 do_printing = (dwarf_cutoff_level == -1
2210 || level < dwarf_cutoff_level);
2211 if (do_printing)
2212 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
2213 level, die_offset, abbrev_number);
2214 else if (dwarf_cutoff_level == -1
2215 || last_level < dwarf_cutoff_level)
2216 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
2217 last_level = level;
2218 }
2219 }
2220
2221 /* Scan through the abbreviation list until we reach the
2222 correct entry. */
2223 for (entry = first_abbrev;
2224 entry && entry->entry != abbrev_number;
2225 entry = entry->next)
2226 continue;
2227
2228 if (entry == NULL)
2229 {
2230 if (!do_loc && do_printing)
2231 {
2232 printf ("\n");
2233 fflush (stdout);
2234 }
2235 warn (_("DIE at offset %lx refers to abbreviation number %lu which does not exist\n"),
2236 die_offset, abbrev_number);
2237 return 0;
2238 }
2239
2240 if (!do_loc && do_printing)
2241 printf (" (%s)\n", get_TAG_name (entry->tag));
2242
2243 switch (entry->tag)
2244 {
2245 default:
2246 need_base_address = 0;
2247 break;
2248 case DW_TAG_compile_unit:
2249 need_base_address = 1;
2250 break;
2251 case DW_TAG_entry_point:
2252 case DW_TAG_subprogram:
2253 need_base_address = 0;
2254 /* Assuming that there is no DW_AT_frame_base. */
2255 have_frame_base = 0;
2256 break;
2257 }
2258
2259 for (attr = entry->first_attr;
2260 attr && attr->attribute;
2261 attr = attr->next)
2262 {
2263 debug_info *arg;
2264
2265 if (! do_loc && do_printing)
2266 /* Show the offset from where the tag was extracted. */
2267 printf (" <%lx>", (unsigned long)(tags - section_begin));
2268
2269 arg = debug_information;
2270 if (debug_information)
2271 arg += unit;
2272
2273 tags = read_and_display_attr (attr->attribute,
2274 attr->form,
2275 tags, cu_offset,
2276 compunit.cu_pointer_size,
2277 offset_size,
2278 compunit.cu_version,
2279 arg,
2280 do_loc || ! do_printing, section);
2281 }
2282
2283 if (entry->children)
2284 ++level;
2285 }
2286 }
2287
2288 /* Set num_debug_info_entries here so that it can be used to check if
2289 we need to process .debug_loc and .debug_ranges sections. */
2290 if ((do_loc || do_debug_loc || do_debug_ranges)
2291 && num_debug_info_entries == 0
2292 && ! do_types)
2293 num_debug_info_entries = num_units;
2294
2295 if (!do_loc)
2296 printf ("\n");
2297
2298 return 1;
2299 }
2300
2301 /* Locate and scan the .debug_info section in the file and record the pointer
2302 sizes and offsets for the compilation units in it. Usually an executable
2303 will have just one pointer size, but this is not guaranteed, and so we try
2304 not to make any assumptions. Returns zero upon failure, or the number of
2305 compilation units upon success. */
2306
2307 static unsigned int
2308 load_debug_info (void * file)
2309 {
2310 /* Reset the last pointer size so that we can issue correct error
2311 messages if we are displaying the contents of more than one section. */
2312 last_pointer_size = 0;
2313 warned_about_missing_comp_units = FALSE;
2314
2315 /* If we have already tried and failed to load the .debug_info
2316 section then do not bother to repear the task. */
2317 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
2318 return 0;
2319
2320 /* If we already have the information there is nothing else to do. */
2321 if (num_debug_info_entries > 0)
2322 return num_debug_info_entries;
2323
2324 if (load_debug_section (info, file)
2325 && process_debug_info (&debug_displays [info].section, file, abbrev, 1, 0))
2326 return num_debug_info_entries;
2327 else if (load_debug_section (info_dwo, file)
2328 && process_debug_info (&debug_displays [info_dwo].section, file,
2329 abbrev_dwo, 1, 0))
2330 return num_debug_info_entries;
2331
2332 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
2333 return 0;
2334 }
2335
2336 static int
2337 display_debug_lines_raw (struct dwarf_section *section,
2338 unsigned char *data,
2339 unsigned char *end)
2340 {
2341 unsigned char *start = section->start;
2342
2343 printf (_("Raw dump of debug contents of section %s:\n\n"),
2344 section->name);
2345
2346 while (data < end)
2347 {
2348 DWARF2_Internal_LineInfo linfo;
2349 unsigned char *standard_opcodes;
2350 unsigned char *end_of_sequence;
2351 unsigned char *hdrptr;
2352 unsigned long hdroff;
2353 int initial_length_size;
2354 int offset_size;
2355 int i;
2356
2357 hdrptr = data;
2358 hdroff = hdrptr - start;
2359
2360 /* Check the length of the block. */
2361 linfo.li_length = byte_get (hdrptr, 4);
2362 hdrptr += 4;
2363
2364 if (linfo.li_length == 0xffffffff)
2365 {
2366 /* This section is 64-bit DWARF 3. */
2367 linfo.li_length = byte_get (hdrptr, 8);
2368 hdrptr += 8;
2369 offset_size = 8;
2370 initial_length_size = 12;
2371 }
2372 else
2373 {
2374 offset_size = 4;
2375 initial_length_size = 4;
2376 }
2377
2378 if (linfo.li_length + initial_length_size > section->size)
2379 {
2380 warn
2381 (_("The information in section %s appears to be corrupt - the section is too small\n"),
2382 section->name);
2383 return 0;
2384 }
2385
2386 /* Check its version number. */
2387 linfo.li_version = byte_get (hdrptr, 2);
2388 hdrptr += 2;
2389 if (linfo.li_version != 2
2390 && linfo.li_version != 3
2391 && linfo.li_version != 4)
2392 {
2393 warn (_("Only DWARF version 2, 3 and 4 line info is currently supported.\n"));
2394 return 0;
2395 }
2396
2397 linfo.li_prologue_length = byte_get (hdrptr, offset_size);
2398 hdrptr += offset_size;
2399 linfo.li_min_insn_length = byte_get (hdrptr, 1);
2400 hdrptr++;
2401 if (linfo.li_version >= 4)
2402 {
2403 linfo.li_max_ops_per_insn = byte_get (hdrptr, 1);
2404 hdrptr++;
2405 if (linfo.li_max_ops_per_insn == 0)
2406 {
2407 warn (_("Invalid maximum operations per insn.\n"));
2408 return 0;
2409 }
2410 }
2411 else
2412 linfo.li_max_ops_per_insn = 1;
2413 linfo.li_default_is_stmt = byte_get (hdrptr, 1);
2414 hdrptr++;
2415 linfo.li_line_base = byte_get (hdrptr, 1);
2416 hdrptr++;
2417 linfo.li_line_range = byte_get (hdrptr, 1);
2418 hdrptr++;
2419 linfo.li_opcode_base = byte_get (hdrptr, 1);
2420 hdrptr++;
2421
2422 /* Sign extend the line base field. */
2423 linfo.li_line_base <<= 24;
2424 linfo.li_line_base >>= 24;
2425
2426 printf (_(" Offset: 0x%lx\n"), hdroff);
2427 printf (_(" Length: %ld\n"), (long) linfo.li_length);
2428 printf (_(" DWARF Version: %d\n"), linfo.li_version);
2429 printf (_(" Prologue Length: %d\n"), linfo.li_prologue_length);
2430 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
2431 if (linfo.li_version >= 4)
2432 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
2433 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
2434 printf (_(" Line Base: %d\n"), linfo.li_line_base);
2435 printf (_(" Line Range: %d\n"), linfo.li_line_range);
2436 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
2437
2438 end_of_sequence = data + linfo.li_length + initial_length_size;
2439
2440 reset_state_machine (linfo.li_default_is_stmt);
2441
2442 /* Display the contents of the Opcodes table. */
2443 standard_opcodes = hdrptr;
2444
2445 printf (_("\n Opcodes:\n"));
2446
2447 for (i = 1; i < linfo.li_opcode_base; i++)
2448 printf (_(" Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
2449
2450 /* Display the contents of the Directory table. */
2451 data = standard_opcodes + linfo.li_opcode_base - 1;
2452
2453 if (*data == 0)
2454 printf (_("\n The Directory Table is empty.\n"));
2455 else
2456 {
2457 printf (_("\n The Directory Table:\n"));
2458
2459 while (*data != 0)
2460 {
2461 printf (" %s\n", data);
2462
2463 data += strlen ((char *) data) + 1;
2464 }
2465 }
2466
2467 /* Skip the NUL at the end of the table. */
2468 data++;
2469
2470 /* Display the contents of the File Name table. */
2471 if (*data == 0)
2472 printf (_("\n The File Name Table is empty.\n"));
2473 else
2474 {
2475 printf (_("\n The File Name Table:\n"));
2476 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
2477
2478 while (*data != 0)
2479 {
2480 unsigned char *name;
2481 unsigned int bytes_read;
2482
2483 printf (" %d\t", ++state_machine_regs.last_file_entry);
2484 name = data;
2485
2486 data += strlen ((char *) data) + 1;
2487
2488 printf ("%s\t",
2489 dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2490 data += bytes_read;
2491 printf ("%s\t",
2492 dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2493 data += bytes_read;
2494 printf ("%s\t",
2495 dwarf_vmatoa ("u", read_leb128 (data, & bytes_read, 0)));
2496 data += bytes_read;
2497 printf ("%s\n", name);
2498 }
2499 }
2500
2501 /* Skip the NUL at the end of the table. */
2502 data++;
2503
2504 /* Now display the statements. */
2505 printf (_("\n Line Number Statements:\n"));
2506
2507 while (data < end_of_sequence)
2508 {
2509 unsigned char op_code;
2510 dwarf_signed_vma adv;
2511 dwarf_vma uladv;
2512 unsigned int bytes_read;
2513
2514 op_code = *data++;
2515
2516 if (op_code >= linfo.li_opcode_base)
2517 {
2518 op_code -= linfo.li_opcode_base;
2519 uladv = (op_code / linfo.li_line_range);
2520 if (linfo.li_max_ops_per_insn == 1)
2521 {
2522 uladv *= linfo.li_min_insn_length;
2523 state_machine_regs.address += uladv;
2524 printf (_(" Special opcode %d: "
2525 "advance Address by %s to 0x%s"),
2526 op_code, dwarf_vmatoa ("u", uladv),
2527 dwarf_vmatoa ("x", state_machine_regs.address));
2528 }
2529 else
2530 {
2531 state_machine_regs.address
2532 += ((state_machine_regs.op_index + uladv)
2533 / linfo.li_max_ops_per_insn)
2534 * linfo.li_min_insn_length;
2535 state_machine_regs.op_index
2536 = (state_machine_regs.op_index + uladv)
2537 % linfo.li_max_ops_per_insn;
2538 printf (_(" Special opcode %d: "
2539 "advance Address by %s to 0x%s[%d]"),
2540 op_code, dwarf_vmatoa ("u", uladv),
2541 dwarf_vmatoa ("x", state_machine_regs.address),
2542 state_machine_regs.op_index);
2543 }
2544 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
2545 state_machine_regs.line += adv;
2546 printf (_(" and Line by %s to %d\n"),
2547 dwarf_vmatoa ("d", adv), state_machine_regs.line);
2548 }
2549 else switch (op_code)
2550 {
2551 case DW_LNS_extended_op:
2552 data += process_extended_line_op (data, linfo.li_default_is_stmt);
2553 break;
2554
2555 case DW_LNS_copy:
2556 printf (_(" Copy\n"));
2557 break;
2558
2559 case DW_LNS_advance_pc:
2560 uladv = read_leb128 (data, & bytes_read, 0);
2561 data += bytes_read;
2562 if (linfo.li_max_ops_per_insn == 1)
2563 {
2564 uladv *= linfo.li_min_insn_length;
2565 state_machine_regs.address += uladv;
2566 printf (_(" Advance PC by %s to 0x%s\n"),
2567 dwarf_vmatoa ("u", uladv),
2568 dwarf_vmatoa ("x", state_machine_regs.address));
2569 }
2570 else
2571 {
2572 state_machine_regs.address
2573 += ((state_machine_regs.op_index + uladv)
2574 / linfo.li_max_ops_per_insn)
2575 * linfo.li_min_insn_length;
2576 state_machine_regs.op_index
2577 = (state_machine_regs.op_index + uladv)
2578 % linfo.li_max_ops_per_insn;
2579 printf (_(" Advance PC by %s to 0x%s[%d]\n"),
2580 dwarf_vmatoa ("u", uladv),
2581 dwarf_vmatoa ("x", state_machine_regs.address),
2582 state_machine_regs.op_index);
2583 }
2584 break;
2585
2586 case DW_LNS_advance_line:
2587 adv = read_sleb128 (data, & bytes_read);
2588 data += bytes_read;
2589 state_machine_regs.line += adv;
2590 printf (_(" Advance Line by %s to %d\n"),
2591 dwarf_vmatoa ("d", adv),
2592 state_machine_regs.line);
2593 break;
2594
2595 case DW_LNS_set_file:
2596 adv = read_leb128 (data, & bytes_read, 0);
2597 data += bytes_read;
2598 printf (_(" Set File Name to entry %s in the File Name Table\n"),
2599 dwarf_vmatoa ("d", adv));
2600 state_machine_regs.file = adv;
2601 break;
2602
2603 case DW_LNS_set_column:
2604 uladv = read_leb128 (data, & bytes_read, 0);
2605 data += bytes_read;
2606 printf (_(" Set column to %s\n"),
2607 dwarf_vmatoa ("u", uladv));
2608 state_machine_regs.column = uladv;
2609 break;
2610
2611 case DW_LNS_negate_stmt:
2612 adv = state_machine_regs.is_stmt;
2613 adv = ! adv;
2614 printf (_(" Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
2615 state_machine_regs.is_stmt = adv;
2616 break;
2617
2618 case DW_LNS_set_basic_block:
2619 printf (_(" Set basic block\n"));
2620 state_machine_regs.basic_block = 1;
2621 break;
2622
2623 case DW_LNS_const_add_pc:
2624 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
2625 if (linfo.li_max_ops_per_insn)
2626 {
2627 uladv *= linfo.li_min_insn_length;
2628 state_machine_regs.address += uladv;
2629 printf (_(" Advance PC by constant %s to 0x%s\n"),
2630 dwarf_vmatoa ("u", uladv),
2631 dwarf_vmatoa ("x", state_machine_regs.address));
2632 }
2633 else
2634 {
2635 state_machine_regs.address
2636 += ((state_machine_regs.op_index + uladv)
2637 / linfo.li_max_ops_per_insn)
2638 * linfo.li_min_insn_length;
2639 state_machine_regs.op_index
2640 = (state_machine_regs.op_index + uladv)
2641 % linfo.li_max_ops_per_insn;
2642 printf (_(" Advance PC by constant %s to 0x%s[%d]\n"),
2643 dwarf_vmatoa ("u", uladv),
2644 dwarf_vmatoa ("x", state_machine_regs.address),
2645 state_machine_regs.op_index);
2646 }
2647 break;
2648
2649 case DW_LNS_fixed_advance_pc:
2650 uladv = byte_get (data, 2);
2651 data += 2;
2652 state_machine_regs.address += uladv;
2653 state_machine_regs.op_index = 0;
2654 printf (_(" Advance PC by fixed size amount %s to 0x%s\n"),
2655 dwarf_vmatoa ("u", uladv),
2656 dwarf_vmatoa ("x", state_machine_regs.address));
2657 break;
2658
2659 case DW_LNS_set_prologue_end:
2660 printf (_(" Set prologue_end to true\n"));
2661 break;
2662
2663 case DW_LNS_set_epilogue_begin:
2664 printf (_(" Set epilogue_begin to true\n"));
2665 break;
2666
2667 case DW_LNS_set_isa:
2668 uladv = read_leb128 (data, & bytes_read, 0);
2669 data += bytes_read;
2670 printf (_(" Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
2671 break;
2672
2673 default:
2674 printf (_(" Unknown opcode %d with operands: "), op_code);
2675
2676 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
2677 {
2678 printf ("0x%s%s", dwarf_vmatoa ("x", read_leb128 (data,
2679 &bytes_read, 0)),
2680 i == 1 ? "" : ", ");
2681 data += bytes_read;
2682 }
2683 putchar ('\n');
2684 break;
2685 }
2686 }
2687 putchar ('\n');
2688 }
2689
2690 return 1;
2691 }
2692
2693 typedef struct
2694 {
2695 unsigned char *name;
2696 unsigned int directory_index;
2697 unsigned int modification_date;
2698 unsigned int length;
2699 } File_Entry;
2700
2701 /* Output a decoded representation of the .debug_line section. */
2702
2703 static int
2704 display_debug_lines_decoded (struct dwarf_section *section,
2705 unsigned char *data,
2706 unsigned char *end)
2707 {
2708 printf (_("Decoded dump of debug contents of section %s:\n\n"),
2709 section->name);
2710
2711 while (data < end)
2712 {
2713 /* This loop amounts to one iteration per compilation unit. */
2714 DWARF2_Internal_LineInfo linfo;
2715 unsigned char *standard_opcodes;
2716 unsigned char *end_of_sequence;
2717 unsigned char *hdrptr;
2718 int initial_length_size;
2719 int offset_size;
2720 int i;
2721 File_Entry *file_table = NULL;
2722 unsigned int n_files = 0;
2723 unsigned char **directory_table = NULL;
2724 unsigned int n_directories = 0;
2725
2726 hdrptr = data;
2727
2728 /* Extract information from the Line Number Program Header.
2729 (section 6.2.4 in the Dwarf3 doc). */
2730
2731 /* Get the length of this CU's line number information block. */
2732 linfo.li_length = byte_get (hdrptr, 4);
2733 hdrptr += 4;
2734
2735 if (linfo.li_length == 0xffffffff)
2736 {
2737 /* This section is 64-bit DWARF 3. */
2738 linfo.li_length = byte_get (hdrptr, 8);
2739 hdrptr += 8;
2740 offset_size = 8;
2741 initial_length_size = 12;
2742 }
2743 else
2744 {
2745 offset_size = 4;
2746 initial_length_size = 4;
2747 }
2748
2749 if (linfo.li_length + initial_length_size > section->size)
2750 {
2751 warn (_("The line info appears to be corrupt - "
2752 "the section is too small\n"));
2753 return 0;
2754 }
2755
2756 /* Get this CU's Line Number Block version number. */
2757 linfo.li_version = byte_get (hdrptr, 2);
2758 hdrptr += 2;
2759 if (linfo.li_version != 2
2760 && linfo.li_version != 3
2761 && linfo.li_version != 4)
2762 {
2763 warn (_("Only DWARF version 2, 3 and 4 line info is currently "
2764 "supported.\n"));
2765 return 0;
2766 }
2767
2768 linfo.li_prologue_length = byte_get (hdrptr, offset_size);
2769 hdrptr += offset_size;
2770 linfo.li_min_insn_length = byte_get (hdrptr, 1);
2771 hdrptr++;
2772 if (linfo.li_version >= 4)
2773 {
2774 linfo.li_max_ops_per_insn = byte_get (hdrptr, 1);
2775 hdrptr++;
2776 if (linfo.li_max_ops_per_insn == 0)
2777 {
2778 warn (_("Invalid maximum operations per insn.\n"));
2779 return 0;
2780 }
2781 }
2782 else
2783 linfo.li_max_ops_per_insn = 1;
2784 linfo.li_default_is_stmt = byte_get (hdrptr, 1);
2785 hdrptr++;
2786 linfo.li_line_base = byte_get (hdrptr, 1);
2787 hdrptr++;
2788 linfo.li_line_range = byte_get (hdrptr, 1);
2789 hdrptr++;
2790 linfo.li_opcode_base = byte_get (hdrptr, 1);
2791 hdrptr++;
2792
2793 /* Sign extend the line base field. */
2794 linfo.li_line_base <<= 24;
2795 linfo.li_line_base >>= 24;
2796
2797 /* Find the end of this CU's Line Number Information Block. */
2798 end_of_sequence = data + linfo.li_length + initial_length_size;
2799
2800 reset_state_machine (linfo.li_default_is_stmt);
2801
2802 /* Save a pointer to the contents of the Opcodes table. */
2803 standard_opcodes = hdrptr;
2804
2805 /* Traverse the Directory table just to count entries. */
2806 data = standard_opcodes + linfo.li_opcode_base - 1;
2807 if (*data != 0)
2808 {
2809 unsigned char *ptr_directory_table = data;
2810
2811 while (*data != 0)
2812 {
2813 data += strlen ((char *) data) + 1;
2814 n_directories++;
2815 }
2816
2817 /* Go through the directory table again to save the directories. */
2818 directory_table = (unsigned char **)
2819 xmalloc (n_directories * sizeof (unsigned char *));
2820
2821 i = 0;
2822 while (*ptr_directory_table != 0)
2823 {
2824 directory_table[i] = ptr_directory_table;
2825 ptr_directory_table += strlen ((char *) ptr_directory_table) + 1;
2826 i++;
2827 }
2828 }
2829 /* Skip the NUL at the end of the table. */
2830 data++;
2831
2832 /* Traverse the File Name table just to count the entries. */
2833 if (*data != 0)
2834 {
2835 unsigned char *ptr_file_name_table = data;
2836
2837 while (*data != 0)
2838 {
2839 unsigned int bytes_read;
2840
2841 /* Skip Name, directory index, last modification time and length
2842 of file. */
2843 data += strlen ((char *) data) + 1;
2844 read_leb128 (data, & bytes_read, 0);
2845 data += bytes_read;
2846 read_leb128 (data, & bytes_read, 0);
2847 data += bytes_read;
2848 read_leb128 (data, & bytes_read, 0);
2849 data += bytes_read;
2850
2851 n_files++;
2852 }
2853
2854 /* Go through the file table again to save the strings. */
2855 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
2856
2857 i = 0;
2858 while (*ptr_file_name_table != 0)
2859 {
2860 unsigned int bytes_read;
2861
2862 file_table[i].name = ptr_file_name_table;
2863 ptr_file_name_table += strlen ((char *) ptr_file_name_table) + 1;
2864
2865 /* We are not interested in directory, time or size. */
2866 file_table[i].directory_index = read_leb128 (ptr_file_name_table,
2867 & bytes_read, 0);
2868 ptr_file_name_table += bytes_read;
2869 file_table[i].modification_date = read_leb128 (ptr_file_name_table,
2870 & bytes_read, 0);
2871 ptr_file_name_table += bytes_read;
2872 file_table[i].length = read_leb128 (ptr_file_name_table, & bytes_read, 0);
2873 ptr_file_name_table += bytes_read;
2874 i++;
2875 }
2876 i = 0;
2877
2878 /* Print the Compilation Unit's name and a header. */
2879 if (directory_table == NULL)
2880 {
2881 printf (_("CU: %s:\n"), file_table[0].name);
2882 printf (_("File name Line number Starting address\n"));
2883 }
2884 else
2885 {
2886 unsigned int ix = file_table[0].directory_index;
2887 const char *directory = ix ? (char *)directory_table[ix - 1] : ".";
2888 if (do_wide || strlen (directory) < 76)
2889 printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
2890 else
2891 printf ("%s:\n", file_table[0].name);
2892
2893 printf (_("File name Line number Starting address\n"));
2894 }
2895 }
2896
2897 /* Skip the NUL at the end of the table. */
2898 data++;
2899
2900 /* This loop iterates through the Dwarf Line Number Program. */
2901 while (data < end_of_sequence)
2902 {
2903 unsigned char op_code;
2904 int adv;
2905 unsigned long int uladv;
2906 unsigned int bytes_read;
2907 int is_special_opcode = 0;
2908
2909 op_code = *data++;
2910
2911 if (op_code >= linfo.li_opcode_base)
2912 {
2913 op_code -= linfo.li_opcode_base;
2914 uladv = (op_code / linfo.li_line_range);
2915 if (linfo.li_max_ops_per_insn == 1)
2916 {
2917 uladv *= linfo.li_min_insn_length;
2918 state_machine_regs.address += uladv;
2919 }
2920 else
2921 {
2922 state_machine_regs.address
2923 += ((state_machine_regs.op_index + uladv)
2924 / linfo.li_max_ops_per_insn)
2925 * linfo.li_min_insn_length;
2926 state_machine_regs.op_index
2927 = (state_machine_regs.op_index + uladv)
2928 % linfo.li_max_ops_per_insn;
2929 }
2930
2931 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
2932 state_machine_regs.line += adv;
2933 is_special_opcode = 1;
2934 }
2935 else switch (op_code)
2936 {
2937 case DW_LNS_extended_op:
2938 {
2939 unsigned int ext_op_code_len;
2940 unsigned char ext_op_code;
2941 unsigned char *op_code_data = data;
2942
2943 ext_op_code_len = read_leb128 (op_code_data, &bytes_read, 0);
2944 op_code_data += bytes_read;
2945
2946 if (ext_op_code_len == 0)
2947 {
2948 warn (_("badly formed extended line op encountered!\n"));
2949 break;
2950 }
2951 ext_op_code_len += bytes_read;
2952 ext_op_code = *op_code_data++;
2953
2954 switch (ext_op_code)
2955 {
2956 case DW_LNE_end_sequence:
2957 reset_state_machine (linfo.li_default_is_stmt);
2958 break;
2959 case DW_LNE_set_address:
2960 state_machine_regs.address =
2961 byte_get (op_code_data, ext_op_code_len - bytes_read - 1);
2962 state_machine_regs.op_index = 0;
2963 break;
2964 case DW_LNE_define_file:
2965 {
2966 file_table = (File_Entry *) xrealloc
2967 (file_table, (n_files + 1) * sizeof (File_Entry));
2968
2969 ++state_machine_regs.last_file_entry;
2970 /* Source file name. */
2971 file_table[n_files].name = op_code_data;
2972 op_code_data += strlen ((char *) op_code_data) + 1;
2973 /* Directory index. */
2974 file_table[n_files].directory_index =
2975 read_leb128 (op_code_data, & bytes_read, 0);
2976 op_code_data += bytes_read;
2977 /* Last modification time. */
2978 file_table[n_files].modification_date =
2979 read_leb128 (op_code_data, & bytes_read, 0);
2980 op_code_data += bytes_read;
2981 /* File length. */
2982 file_table[n_files].length =
2983 read_leb128 (op_code_data, & bytes_read, 0);
2984
2985 n_files++;
2986 break;
2987 }
2988 case DW_LNE_set_discriminator:
2989 case DW_LNE_HP_set_sequence:
2990 /* Simply ignored. */
2991 break;
2992
2993 default:
2994 printf (_("UNKNOWN (%u): length %d\n"),
2995 ext_op_code, ext_op_code_len - bytes_read);
2996 break;
2997 }
2998 data += ext_op_code_len;
2999 break;
3000 }
3001 case DW_LNS_copy:
3002 break;
3003
3004 case DW_LNS_advance_pc:
3005 uladv = read_leb128 (data, & bytes_read, 0);
3006 data += bytes_read;
3007 if (linfo.li_max_ops_per_insn == 1)
3008 {
3009 uladv *= linfo.li_min_insn_length;
3010 state_machine_regs.address += uladv;
3011 }
3012 else
3013 {
3014 state_machine_regs.address
3015 += ((state_machine_regs.op_index + uladv)
3016 / linfo.li_max_ops_per_insn)
3017 * linfo.li_min_insn_length;
3018 state_machine_regs.op_index
3019 = (state_machine_regs.op_index + uladv)
3020 % linfo.li_max_ops_per_insn;
3021 }
3022 break;
3023
3024 case DW_LNS_advance_line:
3025 adv = read_sleb128 (data, & bytes_read);
3026 data += bytes_read;
3027 state_machine_regs.line += adv;
3028 break;
3029
3030 case DW_LNS_set_file:
3031 adv = read_leb128 (data, & bytes_read, 0);
3032 data += bytes_read;
3033 state_machine_regs.file = adv;
3034 if (file_table[state_machine_regs.file - 1].directory_index == 0)
3035 {
3036 /* If directory index is 0, that means current directory. */
3037 printf ("\n./%s:[++]\n",
3038 file_table[state_machine_regs.file - 1].name);
3039 }
3040 else
3041 {
3042 /* The directory index starts counting at 1. */
3043 printf ("\n%s/%s:\n",
3044 directory_table[file_table[state_machine_regs.file - 1].directory_index - 1],
3045 file_table[state_machine_regs.file - 1].name);
3046 }
3047 break;
3048
3049 case DW_LNS_set_column:
3050 uladv = read_leb128 (data, & bytes_read, 0);
3051 data += bytes_read;
3052 state_machine_regs.column = uladv;
3053 break;
3054
3055 case DW_LNS_negate_stmt:
3056 adv = state_machine_regs.is_stmt;
3057 adv = ! adv;
3058 state_machine_regs.is_stmt = adv;
3059 break;
3060
3061 case DW_LNS_set_basic_block:
3062 state_machine_regs.basic_block = 1;
3063 break;
3064
3065 case DW_LNS_const_add_pc:
3066 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
3067 if (linfo.li_max_ops_per_insn == 1)
3068 {
3069 uladv *= linfo.li_min_insn_length;
3070 state_machine_regs.address += uladv;
3071 }
3072 else
3073 {
3074 state_machine_regs.address
3075 += ((state_machine_regs.op_index + uladv)
3076 / linfo.li_max_ops_per_insn)
3077 * linfo.li_min_insn_length;
3078 state_machine_regs.op_index
3079 = (state_machine_regs.op_index + uladv)
3080 % linfo.li_max_ops_per_insn;
3081 }
3082 break;
3083
3084 case DW_LNS_fixed_advance_pc:
3085 uladv = byte_get (data, 2);
3086 data += 2;
3087 state_machine_regs.address += uladv;
3088 state_machine_regs.op_index = 0;
3089 break;
3090
3091 case DW_LNS_set_prologue_end:
3092 break;
3093
3094 case DW_LNS_set_epilogue_begin:
3095 break;
3096
3097 case DW_LNS_set_isa:
3098 uladv = read_leb128 (data, & bytes_read, 0);
3099 data += bytes_read;
3100 printf (_(" Set ISA to %lu\n"), uladv);
3101 break;
3102
3103 default:
3104 printf (_(" Unknown opcode %d with operands: "), op_code);
3105
3106 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
3107 {
3108 printf ("0x%s%s", dwarf_vmatoa ("x", read_leb128 (data,
3109 &bytes_read, 0)),
3110 i == 1 ? "" : ", ");
3111 data += bytes_read;
3112 }
3113 putchar ('\n');
3114 break;
3115 }
3116
3117 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
3118 to the DWARF address/line matrix. */
3119 if ((is_special_opcode) || (op_code == DW_LNE_end_sequence)
3120 || (op_code == DW_LNS_copy))
3121 {
3122 const unsigned int MAX_FILENAME_LENGTH = 35;
3123 char *fileName = (char *)file_table[state_machine_regs.file - 1].name;
3124 char *newFileName = NULL;
3125 size_t fileNameLength = strlen (fileName);
3126
3127 if ((fileNameLength > MAX_FILENAME_LENGTH) && (!do_wide))
3128 {
3129 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
3130 /* Truncate file name */
3131 strncpy (newFileName,
3132 fileName + fileNameLength - MAX_FILENAME_LENGTH,
3133 MAX_FILENAME_LENGTH + 1);
3134 }
3135 else
3136 {
3137 newFileName = (char *) xmalloc (fileNameLength + 1);
3138 strncpy (newFileName, fileName, fileNameLength + 1);
3139 }
3140
3141 if (!do_wide || (fileNameLength <= MAX_FILENAME_LENGTH))
3142 {
3143 if (linfo.li_max_ops_per_insn == 1)
3144 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x\n",
3145 newFileName, state_machine_regs.line,
3146 state_machine_regs.address);
3147 else
3148 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x[%d]\n",
3149 newFileName, state_machine_regs.line,
3150 state_machine_regs.address,
3151 state_machine_regs.op_index);
3152 }
3153 else
3154 {
3155 if (linfo.li_max_ops_per_insn == 1)
3156 printf ("%s %11d %#18" DWARF_VMA_FMT "x\n",
3157 newFileName, state_machine_regs.line,
3158 state_machine_regs.address);
3159 else
3160 printf ("%s %11d %#18" DWARF_VMA_FMT "x[%d]\n",
3161 newFileName, state_machine_regs.line,
3162 state_machine_regs.address,
3163 state_machine_regs.op_index);
3164 }
3165
3166 if (op_code == DW_LNE_end_sequence)
3167 printf ("\n");
3168
3169 free (newFileName);
3170 }
3171 }
3172 free (file_table);
3173 file_table = NULL;
3174 free (directory_table);
3175 directory_table = NULL;
3176 putchar ('\n');
3177 }
3178
3179 return 1;
3180 }
3181
3182 static int
3183 display_debug_lines (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
3184 {
3185 unsigned char *data = section->start;
3186 unsigned char *end = data + section->size;
3187 int retValRaw = 1;
3188 int retValDecoded = 1;
3189
3190 if (do_debug_lines == 0)
3191 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
3192
3193 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
3194 retValRaw = display_debug_lines_raw (section, data, end);
3195
3196 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
3197 retValDecoded = display_debug_lines_decoded (section, data, end);
3198
3199 if (!retValRaw || !retValDecoded)
3200 return 0;
3201
3202 return 1;
3203 }
3204
3205 static debug_info *
3206 find_debug_info_for_offset (unsigned long offset)
3207 {
3208 unsigned int i;
3209
3210 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
3211 return NULL;
3212
3213 for (i = 0; i < num_debug_info_entries; i++)
3214 if (debug_information[i].cu_offset == offset)
3215 return debug_information + i;
3216
3217 return NULL;
3218 }
3219
3220 static int
3221 display_debug_pubnames (struct dwarf_section *section,
3222 void *file ATTRIBUTE_UNUSED)
3223 {
3224 DWARF2_Internal_PubNames names;
3225 unsigned char *start = section->start;
3226 unsigned char *end = start + section->size;
3227
3228 /* It does not matter if this load fails,
3229 we test for that later on. */
3230 load_debug_info (file);
3231
3232 printf (_("Contents of the %s section:\n\n"), section->name);
3233
3234 while (start < end)
3235 {
3236 unsigned char *data;
3237 unsigned long offset;
3238 int offset_size, initial_length_size;
3239
3240 data = start;
3241
3242 names.pn_length = byte_get (data, 4);
3243 data += 4;
3244 if (names.pn_length == 0xffffffff)
3245 {
3246 names.pn_length = byte_get (data, 8);
3247 data += 8;
3248 offset_size = 8;
3249 initial_length_size = 12;
3250 }
3251 else
3252 {
3253 offset_size = 4;
3254 initial_length_size = 4;
3255 }
3256
3257 names.pn_version = byte_get (data, 2);
3258 data += 2;
3259
3260 names.pn_offset = byte_get (data, offset_size);
3261 data += offset_size;
3262
3263 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
3264 && num_debug_info_entries > 0
3265 && find_debug_info_for_offset (names.pn_offset) == NULL)
3266 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
3267 (unsigned long) names.pn_offset, section->name);
3268
3269 names.pn_size = byte_get (data, offset_size);
3270 data += offset_size;
3271
3272 start += names.pn_length + initial_length_size;
3273
3274 if (names.pn_version != 2 && names.pn_version != 3)
3275 {
3276 static int warned = 0;
3277
3278 if (! warned)
3279 {
3280 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
3281 warned = 1;
3282 }
3283
3284 continue;
3285 }
3286
3287 printf (_(" Length: %ld\n"),
3288 (long) names.pn_length);
3289 printf (_(" Version: %d\n"),
3290 names.pn_version);
3291 printf (_(" Offset into .debug_info section: 0x%lx\n"),
3292 (unsigned long) names.pn_offset);
3293 printf (_(" Size of area in .debug_info section: %ld\n"),
3294 (long) names.pn_size);
3295
3296 printf (_("\n Offset\tName\n"));
3297
3298 do
3299 {
3300 offset = byte_get (data, offset_size);
3301
3302 if (offset != 0)
3303 {
3304 data += offset_size;
3305 printf (" %-6lx\t%s\n", offset, data);
3306 data += strlen ((char *) data) + 1;
3307 }
3308 }
3309 while (offset != 0);
3310 }
3311
3312 printf ("\n");
3313 return 1;
3314 }
3315
3316 static int
3317 display_debug_macinfo (struct dwarf_section *section,
3318 void *file ATTRIBUTE_UNUSED)
3319 {
3320 unsigned char *start = section->start;
3321 unsigned char *end = start + section->size;
3322 unsigned char *curr = start;
3323 unsigned int bytes_read;
3324 enum dwarf_macinfo_record_type op;
3325
3326 printf (_("Contents of the %s section:\n\n"), section->name);
3327
3328 while (curr < end)
3329 {
3330 unsigned int lineno;
3331 const char *string;
3332
3333 op = (enum dwarf_macinfo_record_type) *curr;
3334 curr++;
3335
3336 switch (op)
3337 {
3338 case DW_MACINFO_start_file:
3339 {
3340 unsigned int filenum;
3341
3342 lineno = read_leb128 (curr, & bytes_read, 0);
3343 curr += bytes_read;
3344 filenum = read_leb128 (curr, & bytes_read, 0);
3345 curr += bytes_read;
3346
3347 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
3348 lineno, filenum);
3349 }
3350 break;
3351
3352 case DW_MACINFO_end_file:
3353 printf (_(" DW_MACINFO_end_file\n"));
3354 break;
3355
3356 case DW_MACINFO_define:
3357 lineno = read_leb128 (curr, & bytes_read, 0);
3358 curr += bytes_read;
3359 string = (char *) curr;
3360 curr += strlen (string) + 1;
3361 printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"),
3362 lineno, string);
3363 break;
3364
3365 case DW_MACINFO_undef:
3366 lineno = read_leb128 (curr, & bytes_read, 0);
3367 curr += bytes_read;
3368 string = (char *) curr;
3369 curr += strlen (string) + 1;
3370 printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"),
3371 lineno, string);
3372 break;
3373
3374 case DW_MACINFO_vendor_ext:
3375 {
3376 unsigned int constant;
3377
3378 constant = read_leb128 (curr, & bytes_read, 0);
3379 curr += bytes_read;
3380 string = (char *) curr;
3381 curr += strlen (string) + 1;
3382 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"),
3383 constant, string);
3384 }
3385 break;
3386 }
3387 }
3388
3389 return 1;
3390 }
3391
3392 /* Given LINE_OFFSET into the .debug_line section, attempt to return
3393 filename and dirname corresponding to file name table entry with index
3394 FILEIDX. Return NULL on failure. */
3395
3396 static unsigned char *
3397 get_line_filename_and_dirname (dwarf_vma line_offset, dwarf_vma fileidx,
3398 unsigned char **dir_name)
3399 {
3400 struct dwarf_section *section = &debug_displays [line].section;
3401 unsigned char *hdrptr, *dirtable, *file_name;
3402 unsigned int offset_size, initial_length_size;
3403 unsigned int version, opcode_base, bytes_read;
3404 dwarf_vma length, diridx;
3405
3406 *dir_name = NULL;
3407 if (section->start == NULL
3408 || line_offset >= section->size
3409 || fileidx == 0)
3410 return NULL;
3411
3412 hdrptr = section->start + line_offset;
3413 length = byte_get (hdrptr, 4);
3414 hdrptr += 4;
3415 if (length == 0xffffffff)
3416 {
3417 /* This section is 64-bit DWARF 3. */
3418 length = byte_get (hdrptr, 8);
3419 hdrptr += 8;
3420 offset_size = 8;
3421 initial_length_size = 12;
3422 }
3423 else
3424 {
3425 offset_size = 4;
3426 initial_length_size = 4;
3427 }
3428 if (length + initial_length_size > section->size)
3429 return NULL;
3430 version = byte_get (hdrptr, 2);
3431 hdrptr += 2;
3432 if (version != 2 && version != 3 && version != 4)
3433 return NULL;
3434 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
3435 if (version >= 4)
3436 hdrptr++; /* Skip max_ops_per_insn. */
3437 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
3438 opcode_base = byte_get (hdrptr, 1);
3439 if (opcode_base == 0)
3440 return NULL;
3441 hdrptr++;
3442 hdrptr += opcode_base - 1;
3443 dirtable = hdrptr;
3444 /* Skip over dirname table. */
3445 while (*hdrptr != '\0')
3446 hdrptr += strlen ((char *) hdrptr) + 1;
3447 hdrptr++; /* Skip the NUL at the end of the table. */
3448 /* Now skip over preceding filename table entries. */
3449 for (; *hdrptr != '\0' && fileidx > 1; fileidx--)
3450 {
3451 hdrptr += strlen ((char *) hdrptr) + 1;
3452 read_leb128 (hdrptr, &bytes_read, 0);
3453 hdrptr += bytes_read;
3454 read_leb128 (hdrptr, &bytes_read, 0);
3455 hdrptr += bytes_read;
3456 read_leb128 (hdrptr, &bytes_read, 0);
3457 hdrptr += bytes_read;
3458 }
3459 if (*hdrptr == '\0')
3460 return NULL;
3461 file_name = hdrptr;
3462 hdrptr += strlen ((char *) hdrptr) + 1;
3463 diridx = read_leb128 (hdrptr, &bytes_read, 0);
3464 if (diridx == 0)
3465 return file_name;
3466 for (; *dirtable != '\0' && diridx > 1; diridx--)
3467 dirtable += strlen ((char *) dirtable) + 1;
3468 if (*dirtable == '\0')
3469 return NULL;
3470 *dir_name = dirtable;
3471 return file_name;
3472 }
3473
3474 static int
3475 display_debug_macro (struct dwarf_section *section,
3476 void *file)
3477 {
3478 unsigned char *start = section->start;
3479 unsigned char *end = start + section->size;
3480 unsigned char *curr = start;
3481 unsigned char *extended_op_buf[256];
3482 unsigned int bytes_read;
3483
3484 load_debug_section (str, file);
3485 load_debug_section (line, file);
3486
3487 printf (_("Contents of the %s section:\n\n"), section->name);
3488
3489 while (curr < end)
3490 {
3491 unsigned int lineno, version, flags;
3492 unsigned int offset_size = 4;
3493 const char *string;
3494 dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
3495 unsigned char **extended_ops = NULL;
3496
3497 version = byte_get (curr, 2);
3498 curr += 2;
3499
3500 if (version != 4)
3501 {
3502 error (_("Only GNU extension to DWARF 4 of %s is currently supported.\n"),
3503 section->name);
3504 return 0;
3505 }
3506
3507 flags = byte_get (curr++, 1);
3508 if (flags & 1)
3509 offset_size = 8;
3510 printf (_(" Offset: 0x%lx\n"),
3511 (unsigned long) sec_offset);
3512 printf (_(" Version: %d\n"), version);
3513 printf (_(" Offset size: %d\n"), offset_size);
3514 if (flags & 2)
3515 {
3516 line_offset = byte_get (curr, offset_size);
3517 curr += offset_size;
3518 printf (_(" Offset into .debug_line: 0x%lx\n"),
3519 (unsigned long) line_offset);
3520 }
3521 if (flags & 4)
3522 {
3523 unsigned int i, count = byte_get (curr++, 1), op;
3524 dwarf_vma nargs, n;
3525 memset (extended_op_buf, 0, sizeof (extended_op_buf));
3526 extended_ops = extended_op_buf;
3527 if (count)
3528 {
3529 printf (_(" Extension opcode arguments:\n"));
3530 for (i = 0; i < count; i++)
3531 {
3532 op = byte_get (curr++, 1);
3533 extended_ops[op] = curr;
3534 nargs = read_leb128 (curr, &bytes_read, 0);
3535 curr += bytes_read;
3536 if (nargs == 0)
3537 printf (_(" DW_MACRO_GNU_%02x has no arguments\n"), op);
3538 else
3539 {
3540 printf (_(" DW_MACRO_GNU_%02x arguments: "), op);
3541 for (n = 0; n < nargs; n++)
3542 {
3543 unsigned int form = byte_get (curr++, 1);
3544 printf ("%s%s", get_FORM_name (form),
3545 n == nargs - 1 ? "\n" : ", ");
3546 switch (form)
3547 {
3548 case DW_FORM_data1:
3549 case DW_FORM_data2:
3550 case DW_FORM_data4:
3551 case DW_FORM_data8:
3552 case DW_FORM_sdata:
3553 case DW_FORM_udata:
3554 case DW_FORM_block:
3555 case DW_FORM_block1:
3556 case DW_FORM_block2:
3557 case DW_FORM_block4:
3558 case DW_FORM_flag:
3559 case DW_FORM_string:
3560 case DW_FORM_strp:
3561 case DW_FORM_sec_offset:
3562 break;
3563 default:
3564 error (_("Invalid extension opcode form %s\n"),
3565 get_FORM_name (form));
3566 return 0;
3567 }
3568 }
3569 }
3570 }
3571 }
3572 }
3573 printf ("\n");
3574
3575 while (1)
3576 {
3577 unsigned int op;
3578
3579 if (curr >= end)
3580 {
3581 error (_(".debug_macro section not zero terminated\n"));
3582 return 0;
3583 }
3584
3585 op = byte_get (curr++, 1);
3586 if (op == 0)
3587 break;
3588
3589 switch (op)
3590 {
3591 case DW_MACRO_GNU_start_file:
3592 {
3593 unsigned int filenum;
3594 unsigned char *file_name = NULL, *dir_name = NULL;
3595
3596 lineno = read_leb128 (curr, &bytes_read, 0);
3597 curr += bytes_read;
3598 filenum = read_leb128 (curr, &bytes_read, 0);
3599 curr += bytes_read;
3600
3601 if ((flags & 2) == 0)
3602 error (_("DW_MACRO_GNU_start_file used, but no .debug_line offset provided.\n"));
3603 else
3604 file_name
3605 = get_line_filename_and_dirname (line_offset, filenum,
3606 &dir_name);
3607 if (file_name == NULL)
3608 printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d\n"),
3609 lineno, filenum);
3610 else
3611 printf (_(" DW_MACRO_GNU_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
3612 lineno, filenum,
3613 dir_name != NULL ? (const char *) dir_name : "",
3614 dir_name != NULL ? "/" : "", file_name);
3615 }
3616 break;
3617
3618 case DW_MACRO_GNU_end_file:
3619 printf (_(" DW_MACRO_GNU_end_file\n"));
3620 break;
3621
3622 case DW_MACRO_GNU_define:
3623 lineno = read_leb128 (curr, &bytes_read, 0);
3624 curr += bytes_read;
3625 string = (char *) curr;
3626 curr += strlen (string) + 1;
3627 printf (_(" DW_MACRO_GNU_define - lineno : %d macro : %s\n"),
3628 lineno, string);
3629 break;
3630
3631 case DW_MACRO_GNU_undef:
3632 lineno = read_leb128 (curr, &bytes_read, 0);
3633 curr += bytes_read;
3634 string = (char *) curr;
3635 curr += strlen (string) + 1;
3636 printf (_(" DW_MACRO_GNU_undef - lineno : %d macro : %s\n"),
3637 lineno, string);
3638 break;
3639
3640 case DW_MACRO_GNU_define_indirect:
3641 lineno = read_leb128 (curr, &bytes_read, 0);
3642 curr += bytes_read;
3643 offset = byte_get (curr, offset_size);
3644 curr += offset_size;
3645 string = fetch_indirect_string (offset);
3646 printf (_(" DW_MACRO_GNU_define_indirect - lineno : %d macro : %s\n"),
3647 lineno, string);
3648 break;
3649
3650 case DW_MACRO_GNU_undef_indirect:
3651 lineno = read_leb128 (curr, &bytes_read, 0);
3652 curr += bytes_read;
3653 offset = byte_get (curr, offset_size);
3654 curr += offset_size;
3655 string = fetch_indirect_string (offset);
3656 printf (_(" DW_MACRO_GNU_undef_indirect - lineno : %d macro : %s\n"),
3657 lineno, string);
3658 break;
3659
3660 case DW_MACRO_GNU_transparent_include:
3661 offset = byte_get (curr, offset_size);
3662 curr += offset_size;
3663 printf (_(" DW_MACRO_GNU_transparent_include - offset : 0x%lx\n"),
3664 (unsigned long) offset);
3665 break;
3666
3667 case DW_MACRO_GNU_define_indirect_alt:
3668 lineno = read_leb128 (curr, &bytes_read, 0);
3669 curr += bytes_read;
3670 offset = byte_get (curr, offset_size);
3671 curr += offset_size;
3672 printf (_(" DW_MACRO_GNU_define_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
3673 lineno, (unsigned long) offset);
3674 break;
3675
3676 case DW_MACRO_GNU_undef_indirect_alt:
3677 lineno = read_leb128 (curr, &bytes_read, 0);
3678 curr += bytes_read;
3679 offset = byte_get (curr, offset_size);
3680 curr += offset_size;
3681 printf (_(" DW_MACRO_GNU_undef_indirect_alt - lineno : %d macro offset : 0x%lx\n"),
3682 lineno, (unsigned long) offset);
3683 break;
3684
3685 case DW_MACRO_GNU_transparent_include_alt:
3686 offset = byte_get (curr, offset_size);
3687 curr += offset_size;
3688 printf (_(" DW_MACRO_GNU_transparent_include_alt - offset : 0x%lx\n"),
3689 (unsigned long) offset);
3690 break;
3691
3692 default:
3693 if (extended_ops == NULL || extended_ops[op] == NULL)
3694 {
3695 error (_(" Unknown macro opcode %02x seen\n"), op);
3696 return 0;
3697 }
3698 else
3699 {
3700 /* Skip over unhandled opcodes. */
3701 dwarf_vma nargs, n;
3702 unsigned char *desc = extended_ops[op];
3703 nargs = read_leb128 (desc, &bytes_read, 0);
3704 desc += bytes_read;
3705 if (nargs == 0)
3706 {
3707 printf (_(" DW_MACRO_GNU_%02x\n"), op);
3708 break;
3709 }
3710 printf (_(" DW_MACRO_GNU_%02x -"), op);
3711 for (n = 0; n < nargs; n++)
3712 {
3713 curr
3714 = read_and_display_attr_value (0, byte_get (desc++, 1),
3715 curr, 0, 0, offset_size,
3716 version, NULL, 0, NULL);
3717 if (n != nargs - 1)
3718 printf (",");
3719 }
3720 printf ("\n");
3721 }
3722 break;
3723 }
3724 }
3725
3726 printf ("\n");
3727 }
3728
3729 return 1;
3730 }
3731
3732 static int
3733 display_debug_abbrev (struct dwarf_section *section,
3734 void *file ATTRIBUTE_UNUSED)
3735 {
3736 abbrev_entry *entry;
3737 unsigned char *start = section->start;
3738 unsigned char *end = start + section->size;
3739
3740 printf (_("Contents of the %s section:\n\n"), section->name);
3741
3742 do
3743 {
3744 unsigned char *last;
3745
3746 free_abbrevs ();
3747
3748 last = start;
3749 start = process_abbrev_section (start, end);
3750
3751 if (first_abbrev == NULL)
3752 continue;
3753
3754 printf (_(" Number TAG (0x%lx)\n"), (long) (last - section->start));
3755
3756 for (entry = first_abbrev; entry; entry = entry->next)
3757 {
3758 abbrev_attr *attr;
3759
3760 printf (" %ld %s [%s]\n",
3761 entry->entry,
3762 get_TAG_name (entry->tag),
3763 entry->children ? _("has children") : _("no children"));
3764
3765 for (attr = entry->first_attr; attr; attr = attr->next)
3766 printf (" %-18s %s\n",
3767 get_AT_name (attr->attribute),
3768 get_FORM_name (attr->form));
3769 }
3770 }
3771 while (start);
3772
3773 printf ("\n");
3774
3775 return 1;
3776 }
3777
3778 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
3779
3780 static void
3781 display_loc_list (struct dwarf_section *section,
3782 unsigned char **start_ptr,
3783 int debug_info_entry,
3784 unsigned long offset,
3785 unsigned long base_address,
3786 int has_frame_base)
3787 {
3788 unsigned char *start = *start_ptr;
3789 unsigned char *section_end = section->start + section->size;
3790 unsigned long cu_offset = debug_information [debug_info_entry].cu_offset;
3791 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
3792 unsigned int offset_size = debug_information [debug_info_entry].offset_size;
3793 int dwarf_version = debug_information [debug_info_entry].dwarf_version;
3794
3795 dwarf_vma begin;
3796 dwarf_vma end;
3797 unsigned short length;
3798 int need_frame_base;
3799
3800 while (1)
3801 {
3802 if (start + 2 * pointer_size > section_end)
3803 {
3804 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3805 offset);
3806 break;
3807 }
3808
3809 /* Note: we use sign extension here in order to be sure that we can detect
3810 the -1 escape value. Sign extension into the top 32 bits of a 32-bit
3811 address will not affect the values that we display since we always show
3812 hex values, and always the bottom 32-bits. */
3813 begin = byte_get_signed (start, pointer_size);
3814 start += pointer_size;
3815 end = byte_get_signed (start, pointer_size);
3816 start += pointer_size;
3817
3818 printf (" %8.8lx ", offset);
3819
3820 if (begin == 0 && end == 0)
3821 {
3822 printf (_("<End of list>\n"));
3823 break;
3824 }
3825
3826 /* Check base address specifiers. */
3827 if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
3828 {
3829 base_address = end;
3830 print_dwarf_vma (begin, pointer_size);
3831 print_dwarf_vma (end, pointer_size);
3832 printf (_("(base address)\n"));
3833 continue;
3834 }
3835
3836 if (start + 2 > section_end)
3837 {
3838 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3839 offset);
3840 break;
3841 }
3842
3843 length = byte_get (start, 2);
3844 start += 2;
3845
3846 if (start + length > section_end)
3847 {
3848 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3849 offset);
3850 break;
3851 }
3852
3853 print_dwarf_vma (begin + base_address, pointer_size);
3854 print_dwarf_vma (end + base_address, pointer_size);
3855
3856 putchar ('(');
3857 need_frame_base = decode_location_expression (start,
3858 pointer_size,
3859 offset_size,
3860 dwarf_version,
3861 length,
3862 cu_offset, section);
3863 putchar (')');
3864
3865 if (need_frame_base && !has_frame_base)
3866 printf (_(" [without DW_AT_frame_base]"));
3867
3868 if (begin == end)
3869 fputs (_(" (start == end)"), stdout);
3870 else if (begin > end)
3871 fputs (_(" (start > end)"), stdout);
3872
3873 putchar ('\n');
3874
3875 start += length;
3876 }
3877
3878 *start_ptr = start;
3879 }
3880
3881 /* Display a location list from a .dwo section. It uses address indexes rather
3882 than embedded addresses. This code closely follows display_loc_list, but the
3883 two are sufficiently different that combining things is very ugly. */
3884
3885 static void
3886 display_loc_list_dwo (struct dwarf_section *section,
3887 unsigned char **start_ptr,
3888 int debug_info_entry,
3889 unsigned long offset,
3890 int has_frame_base)
3891 {
3892 unsigned char *start = *start_ptr;
3893 unsigned char *section_end = section->start + section->size;
3894 unsigned long cu_offset = debug_information [debug_info_entry].cu_offset;
3895 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
3896 unsigned int offset_size = debug_information [debug_info_entry].offset_size;
3897 int dwarf_version = debug_information [debug_info_entry].dwarf_version;
3898 int entry_type;
3899 unsigned short length;
3900 int need_frame_base;
3901 dwarf_vma idx;
3902 unsigned int bytes_read;
3903
3904 while (1)
3905 {
3906 printf (" %8.8lx ", offset);
3907
3908 if (start + 2 > section_end)
3909 {
3910 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3911 offset);
3912 break;
3913 }
3914
3915 entry_type = byte_get (start, 1);
3916 start++;
3917 switch (entry_type)
3918 {
3919 case 0: /* A terminating entry. */
3920 idx = byte_get (start, 1);
3921 start++;
3922 *start_ptr = start;
3923 if (idx == 0)
3924 printf (_("<End of list>\n"));
3925 else
3926 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3927 offset);
3928 return;
3929 case 1: /* A base-address entry. */
3930 idx = read_leb128 (start, &bytes_read, 0);
3931 start += bytes_read;
3932 print_dwarf_vma (idx, pointer_size);
3933 printf (_("(base address index)\n"));
3934 continue;
3935 case 2: /* A normal entry. */
3936 idx = read_leb128 (start, &bytes_read, 0);
3937 start += bytes_read;
3938 print_dwarf_vma (idx, pointer_size);
3939 idx = read_leb128 (start, &bytes_read, 0);
3940 start += bytes_read;
3941 print_dwarf_vma (idx, pointer_size);
3942 break;
3943 default:
3944 warn (_("Unknown location-list type 0x%x.\n"), entry_type);
3945 *start_ptr = start;
3946 return;
3947 }
3948
3949 if (start + 2 > section_end)
3950 {
3951 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3952 offset);
3953 break;
3954 }
3955
3956 length = byte_get (start, 2);
3957 start += 2;
3958
3959 if (start + length > section_end)
3960 {
3961 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
3962 offset);
3963 break;
3964 }
3965
3966 putchar ('(');
3967 need_frame_base = decode_location_expression (start,
3968 pointer_size,
3969 offset_size,
3970 dwarf_version,
3971 length,
3972 cu_offset, section);
3973 putchar (')');
3974
3975 if (need_frame_base && !has_frame_base)
3976 printf (_(" [without DW_AT_frame_base]"));
3977
3978 putchar ('\n');
3979
3980 start += length;
3981 }
3982
3983 *start_ptr = start;
3984 }
3985
3986 /* Sort array of indexes in ascending order of loc_offsets[idx]. */
3987
3988 static dwarf_vma *loc_offsets;
3989
3990 static int
3991 loc_offsets_compar (const void *ap, const void *bp)
3992 {
3993 dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
3994 dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
3995
3996 return (a > b) - (b > a);
3997 }
3998
3999 static int
4000 display_debug_loc (struct dwarf_section *section, void *file)
4001 {
4002 unsigned char *start = section->start;
4003 unsigned long bytes;
4004 unsigned char *section_begin = start;
4005 unsigned int num_loc_list = 0;
4006 unsigned long last_offset = 0;
4007 unsigned int first = 0;
4008 unsigned int i;
4009 unsigned int j;
4010 unsigned int k;
4011 int seen_first_offset = 0;
4012 int locs_sorted = 1;
4013 unsigned char *next;
4014 unsigned int *array = NULL;
4015 const char *suffix = strrchr (section->name, '.');
4016 int is_dwo = 0;
4017
4018 if (suffix && strcmp (suffix, ".dwo") == 0)
4019 is_dwo = 1;
4020
4021 bytes = section->size;
4022
4023 if (bytes == 0)
4024 {
4025 printf (_("\nThe %s section is empty.\n"), section->name);
4026 return 0;
4027 }
4028
4029 if (load_debug_info (file) == 0)
4030 {
4031 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4032 section->name);
4033 return 0;
4034 }
4035
4036 /* Check the order of location list in .debug_info section. If
4037 offsets of location lists are in the ascending order, we can
4038 use `debug_information' directly. */
4039 for (i = 0; i < num_debug_info_entries; i++)
4040 {
4041 unsigned int num;
4042
4043 num = debug_information [i].num_loc_offsets;
4044 if (num > num_loc_list)
4045 num_loc_list = num;
4046
4047 /* Check if we can use `debug_information' directly. */
4048 if (locs_sorted && num != 0)
4049 {
4050 if (!seen_first_offset)
4051 {
4052 /* This is the first location list. */
4053 last_offset = debug_information [i].loc_offsets [0];
4054 first = i;
4055 seen_first_offset = 1;
4056 j = 1;
4057 }
4058 else
4059 j = 0;
4060
4061 for (; j < num; j++)
4062 {
4063 if (last_offset >
4064 debug_information [i].loc_offsets [j])
4065 {
4066 locs_sorted = 0;
4067 break;
4068 }
4069 last_offset = debug_information [i].loc_offsets [j];
4070 }
4071 }
4072 }
4073
4074 if (!seen_first_offset)
4075 error (_("No location lists in .debug_info section!\n"));
4076
4077 /* DWARF sections under Mach-O have non-zero addresses. */
4078 if (debug_information [first].num_loc_offsets > 0
4079 && debug_information [first].loc_offsets [0] != section->address)
4080 warn (_("Location lists in %s section start at 0x%s\n"),
4081 section->name,
4082 dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
4083
4084 if (!locs_sorted)
4085 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
4086 printf (_("Contents of the %s section:\n\n"), section->name);
4087 if (!is_dwo)
4088 printf (_(" Offset Begin End Expression\n"));
4089 else
4090 printf (_(" Offset Begin idx End idx Expression\n"));
4091
4092 seen_first_offset = 0;
4093 for (i = first; i < num_debug_info_entries; i++)
4094 {
4095 unsigned long offset;
4096 unsigned long base_address;
4097 int has_frame_base;
4098
4099 if (!locs_sorted)
4100 {
4101 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
4102 array[k] = k;
4103 loc_offsets = debug_information [i].loc_offsets;
4104 qsort (array, debug_information [i].num_loc_offsets,
4105 sizeof (*array), loc_offsets_compar);
4106 }
4107
4108 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
4109 {
4110 j = locs_sorted ? k : array[k];
4111 if (k
4112 && debug_information [i].loc_offsets [locs_sorted
4113 ? k - 1 : array [k - 1]]
4114 == debug_information [i].loc_offsets [j])
4115 continue;
4116 has_frame_base = debug_information [i].have_frame_base [j];
4117 /* DWARF sections under Mach-O have non-zero addresses. */
4118 offset = debug_information [i].loc_offsets [j] - section->address;
4119 next = section_begin + offset;
4120 base_address = debug_information [i].base_address;
4121
4122 if (!seen_first_offset)
4123 seen_first_offset = 1;
4124 else
4125 {
4126 if (start < next)
4127 warn (_("There is a hole [0x%lx - 0x%lx] in .debug_loc section.\n"),
4128 (unsigned long) (start - section_begin),
4129 (unsigned long) (next - section_begin));
4130 else if (start > next)
4131 warn (_("There is an overlap [0x%lx - 0x%lx] in .debug_loc section.\n"),
4132 (unsigned long) (start - section_begin),
4133 (unsigned long) (next - section_begin));
4134 }
4135 start = next;
4136
4137 if (offset >= bytes)
4138 {
4139 warn (_("Offset 0x%lx is bigger than .debug_loc section size.\n"),
4140 offset);
4141 continue;
4142 }
4143
4144 if (is_dwo)
4145 display_loc_list_dwo (section, &start, i, offset, has_frame_base);
4146 else
4147 display_loc_list (section, &start, i, offset, base_address,
4148 has_frame_base);
4149 }
4150 }
4151
4152 if (start < section->start + section->size)
4153 warn (_("There are %ld unused bytes at the end of section %s\n"),
4154 (long) (section->start + section->size - start), section->name);
4155 putchar ('\n');
4156 free (array);
4157 return 1;
4158 }
4159
4160 static int
4161 display_debug_str (struct dwarf_section *section,
4162 void *file ATTRIBUTE_UNUSED)
4163 {
4164 unsigned char *start = section->start;
4165 unsigned long bytes = section->size;
4166 dwarf_vma addr = section->address;
4167
4168 if (bytes == 0)
4169 {
4170 printf (_("\nThe %s section is empty.\n"), section->name);
4171 return 0;
4172 }
4173
4174 printf (_("Contents of the %s section:\n\n"), section->name);
4175
4176 while (bytes)
4177 {
4178 int j;
4179 int k;
4180 int lbytes;
4181
4182 lbytes = (bytes > 16 ? 16 : bytes);
4183
4184 printf (" 0x%8.8lx ", (unsigned long) addr);
4185
4186 for (j = 0; j < 16; j++)
4187 {
4188 if (j < lbytes)
4189 printf ("%2.2x", start[j]);
4190 else
4191 printf (" ");
4192
4193 if ((j & 3) == 3)
4194 printf (" ");
4195 }
4196
4197 for (j = 0; j < lbytes; j++)
4198 {
4199 k = start[j];
4200 if (k >= ' ' && k < 0x80)
4201 printf ("%c", k);
4202 else
4203 printf (".");
4204 }
4205
4206 putchar ('\n');
4207
4208 start += lbytes;
4209 addr += lbytes;
4210 bytes -= lbytes;
4211 }
4212
4213 putchar ('\n');
4214
4215 return 1;
4216 }
4217
4218 static int
4219 display_debug_info (struct dwarf_section *section, void *file)
4220 {
4221 return process_debug_info (section, file, section->abbrev_sec, 0, 0);
4222 }
4223
4224 static int
4225 display_debug_types (struct dwarf_section *section, void *file)
4226 {
4227 return process_debug_info (section, file, section->abbrev_sec, 0, 1);
4228 }
4229
4230 static int
4231 display_trace_info (struct dwarf_section *section, void *file)
4232 {
4233 return process_debug_info (section, file, section->abbrev_sec, 0, 0);
4234 }
4235
4236 static int
4237 display_debug_aranges (struct dwarf_section *section,
4238 void *file ATTRIBUTE_UNUSED)
4239 {
4240 unsigned char *start = section->start;
4241 unsigned char *end = start + section->size;
4242
4243 printf (_("Contents of the %s section:\n\n"), section->name);
4244
4245 /* It does not matter if this load fails,
4246 we test for that later on. */
4247 load_debug_info (file);
4248
4249 while (start < end)
4250 {
4251 unsigned char *hdrptr;
4252 DWARF2_Internal_ARange arange;
4253 unsigned char *addr_ranges;
4254 dwarf_vma length;
4255 dwarf_vma address;
4256 unsigned char address_size;
4257 int excess;
4258 int offset_size;
4259 int initial_length_size;
4260
4261 hdrptr = start;
4262
4263 arange.ar_length = byte_get (hdrptr, 4);
4264 hdrptr += 4;
4265
4266 if (arange.ar_length == 0xffffffff)
4267 {
4268 arange.ar_length = byte_get (hdrptr, 8);
4269 hdrptr += 8;
4270 offset_size = 8;
4271 initial_length_size = 12;
4272 }
4273 else
4274 {
4275 offset_size = 4;
4276 initial_length_size = 4;
4277 }
4278
4279 arange.ar_version = byte_get (hdrptr, 2);
4280 hdrptr += 2;
4281
4282 arange.ar_info_offset = byte_get (hdrptr, offset_size);
4283 hdrptr += offset_size;
4284
4285 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
4286 && num_debug_info_entries > 0
4287 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
4288 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
4289 (unsigned long) arange.ar_info_offset, section->name);
4290
4291 arange.ar_pointer_size = byte_get (hdrptr, 1);
4292 hdrptr += 1;
4293
4294 arange.ar_segment_size = byte_get (hdrptr, 1);
4295 hdrptr += 1;
4296
4297 if (arange.ar_version != 2 && arange.ar_version != 3)
4298 {
4299 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
4300 break;
4301 }
4302
4303 printf (_(" Length: %ld\n"),
4304 (long) arange.ar_length);
4305 printf (_(" Version: %d\n"), arange.ar_version);
4306 printf (_(" Offset into .debug_info: 0x%lx\n"),
4307 (unsigned long) arange.ar_info_offset);
4308 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
4309 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
4310
4311 address_size = arange.ar_pointer_size + arange.ar_segment_size;
4312
4313 if (address_size == 0)
4314 {
4315 error (_("Invalid address size in %s section!\n"),
4316 section->name);
4317 break;
4318 }
4319
4320 /* The DWARF spec does not require that the address size be a power
4321 of two, but we do. This will have to change if we ever encounter
4322 an uneven architecture. */
4323 if ((address_size & (address_size - 1)) != 0)
4324 {
4325 warn (_("Pointer size + Segment size is not a power of two.\n"));
4326 break;
4327 }
4328
4329 if (address_size > 4)
4330 printf (_("\n Address Length\n"));
4331 else
4332 printf (_("\n Address Length\n"));
4333
4334 addr_ranges = hdrptr;
4335
4336 /* Must pad to an alignment boundary that is twice the address size. */
4337 excess = (hdrptr - start) % (2 * address_size);
4338 if (excess)
4339 addr_ranges += (2 * address_size) - excess;
4340
4341 start += arange.ar_length + initial_length_size;
4342
4343 while (addr_ranges + 2 * address_size <= start)
4344 {
4345 address = byte_get (addr_ranges, address_size);
4346
4347 addr_ranges += address_size;
4348
4349 length = byte_get (addr_ranges, address_size);
4350
4351 addr_ranges += address_size;
4352
4353 printf (" ");
4354 print_dwarf_vma (address, address_size);
4355 print_dwarf_vma (length, address_size);
4356 putchar ('\n');
4357 }
4358 }
4359
4360 printf ("\n");
4361
4362 return 1;
4363 }
4364
4365 /* Comparison function for qsort. */
4366 static int
4367 comp_addr_base (const void * v0, const void * v1)
4368 {
4369 debug_info * info0 = (debug_info *) v0;
4370 debug_info * info1 = (debug_info *) v1;
4371 return info0->addr_base - info1->addr_base;
4372 }
4373
4374 /* Display the debug_addr section. */
4375 static int
4376 display_debug_addr (struct dwarf_section *section,
4377 void *file)
4378 {
4379 debug_info **debug_addr_info;
4380 unsigned char *entry;
4381 unsigned char *end;
4382 unsigned int i;
4383 unsigned int count;
4384
4385 if (section->size == 0)
4386 {
4387 printf (_("\nThe %s section is empty.\n"), section->name);
4388 return 0;
4389 }
4390
4391 if (load_debug_info (file) == 0)
4392 {
4393 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4394 section->name);
4395 return 0;
4396 }
4397
4398 printf (_("Contents of the %s section:\n\n"), section->name);
4399
4400 debug_addr_info = (debug_info **) xmalloc (num_debug_info_entries + 1
4401 * sizeof (debug_info *));
4402
4403 count = 0;
4404 for (i = 0; i < num_debug_info_entries; i++)
4405 {
4406 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
4407 debug_addr_info [count++] = &debug_information [i];
4408 }
4409
4410 /* Add a sentinel to make iteration convenient. */
4411 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
4412 debug_addr_info [count]->addr_base = section->size;
4413
4414 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
4415 for (i = 0; i < count; i++)
4416 {
4417 unsigned int idx;
4418
4419 printf (_(" For compilation unit at offset 0x%s:\n"),
4420 dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
4421
4422 printf (_("\tIndex\tOffset\n"));
4423 entry = section->start + debug_addr_info [i]->addr_base;
4424 end = section->start + debug_addr_info [i + 1]->addr_base;
4425 idx = 0;
4426 while (entry < end)
4427 {
4428 dwarf_vma base = byte_get (entry, debug_addr_info [i]->pointer_size);
4429 printf (_("\t%d:\t%s\n"), idx, dwarf_vmatoa ("x", base));
4430 entry += debug_addr_info [i]->pointer_size;
4431 idx++;
4432 }
4433 }
4434 printf ("\n");
4435
4436 free (debug_addr_info);
4437 return 1;
4438 }
4439
4440 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
4441 static int
4442 display_debug_str_offsets (struct dwarf_section *section,
4443 void *file ATTRIBUTE_UNUSED)
4444 {
4445 if (section->size == 0)
4446 {
4447 printf (_("\nThe %s section is empty.\n"), section->name);
4448 return 0;
4449 }
4450 /* TODO: Dump the contents. This is made somewhat difficult by not knowing
4451 what the offset size is for this section. */
4452 return 1;
4453 }
4454
4455 /* Each debug_information[x].range_lists[y] gets this representation for
4456 sorting purposes. */
4457
4458 struct range_entry
4459 {
4460 /* The debug_information[x].range_lists[y] value. */
4461 unsigned long ranges_offset;
4462
4463 /* Original debug_information to find parameters of the data. */
4464 debug_info *debug_info_p;
4465 };
4466
4467 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
4468
4469 static int
4470 range_entry_compar (const void *ap, const void *bp)
4471 {
4472 const struct range_entry *a_re = (const struct range_entry *) ap;
4473 const struct range_entry *b_re = (const struct range_entry *) bp;
4474 const unsigned long a = a_re->ranges_offset;
4475 const unsigned long b = b_re->ranges_offset;
4476
4477 return (a > b) - (b > a);
4478 }
4479
4480 static int
4481 display_debug_ranges (struct dwarf_section *section,
4482 void *file ATTRIBUTE_UNUSED)
4483 {
4484 unsigned char *start = section->start;
4485 unsigned char *last_start = start;
4486 unsigned long bytes;
4487 unsigned char *section_begin = start;
4488 unsigned int num_range_list, i;
4489 struct range_entry *range_entries, *range_entry_fill;
4490
4491 bytes = section->size;
4492
4493 if (bytes == 0)
4494 {
4495 printf (_("\nThe %s section is empty.\n"), section->name);
4496 return 0;
4497 }
4498
4499 if (load_debug_info (file) == 0)
4500 {
4501 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
4502 section->name);
4503 return 0;
4504 }
4505
4506 num_range_list = 0;
4507 for (i = 0; i < num_debug_info_entries; i++)
4508 num_range_list += debug_information [i].num_range_lists;
4509
4510 if (num_range_list == 0)
4511 {
4512 /* This can happen when the file was compiled with -gsplit-debug
4513 which removes references to range lists from the primary .o file. */
4514 printf (_("No range lists in .debug_info section.\n"));
4515 return 1;
4516 }
4517
4518 range_entries = (struct range_entry *)
4519 xmalloc (sizeof (*range_entries) * num_range_list);
4520 range_entry_fill = range_entries;
4521
4522 for (i = 0; i < num_debug_info_entries; i++)
4523 {
4524 debug_info *debug_info_p = &debug_information[i];
4525 unsigned int j;
4526
4527 for (j = 0; j < debug_info_p->num_range_lists; j++)
4528 {
4529 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
4530 range_entry_fill->debug_info_p = debug_info_p;
4531 range_entry_fill++;
4532 }
4533 }
4534
4535 qsort (range_entries, num_range_list, sizeof (*range_entries),
4536 range_entry_compar);
4537
4538 /* DWARF sections under Mach-O have non-zero addresses. */
4539 if (dwarf_check != 0 && range_entries[0].ranges_offset != section->address)
4540 warn (_("Range lists in %s section start at 0x%lx\n"),
4541 section->name, range_entries[0].ranges_offset);
4542
4543 printf (_("Contents of the %s section:\n\n"), section->name);
4544 printf (_(" Offset Begin End\n"));
4545
4546 for (i = 0; i < num_range_list; i++)
4547 {
4548 struct range_entry *range_entry = &range_entries[i];
4549 debug_info *debug_info_p = range_entry->debug_info_p;
4550 unsigned int pointer_size;
4551 unsigned long offset;
4552 unsigned char *next;
4553 unsigned long base_address;
4554
4555 pointer_size = debug_info_p->pointer_size;
4556
4557 /* DWARF sections under Mach-O have non-zero addresses. */
4558 offset = range_entry->ranges_offset - section->address;
4559 next = section_begin + offset;
4560 base_address = debug_info_p->base_address;
4561
4562 if (dwarf_check != 0 && i > 0)
4563 {
4564 if (start < next)
4565 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
4566 (unsigned long) (start - section_begin),
4567 (unsigned long) (next - section_begin), section->name);
4568 else if (start > next)
4569 {
4570 if (next == last_start)
4571 continue;
4572 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
4573 (unsigned long) (start - section_begin),
4574 (unsigned long) (next - section_begin), section->name);
4575 }
4576 }
4577 start = next;
4578 last_start = next;
4579
4580 while (1)
4581 {
4582 dwarf_vma begin;
4583 dwarf_vma end;
4584
4585 /* Note: we use sign extension here in order to be sure that
4586 we can detect the -1 escape value. Sign extension into the
4587 top 32 bits of a 32-bit address will not affect the values
4588 that we display since we always show hex values, and always
4589 the bottom 32-bits. */
4590 begin = byte_get_signed (start, pointer_size);
4591 start += pointer_size;
4592 end = byte_get_signed (start, pointer_size);
4593 start += pointer_size;
4594
4595 printf (" %8.8lx ", offset);
4596
4597 if (begin == 0 && end == 0)
4598 {
4599 printf (_("<End of list>\n"));
4600 break;
4601 }
4602
4603 /* Check base address specifiers. */
4604 if (begin == (dwarf_vma) -1 && end != (dwarf_vma) -1)
4605 {
4606 base_address = end;
4607 print_dwarf_vma (begin, pointer_size);
4608 print_dwarf_vma (end, pointer_size);
4609 printf ("(base address)\n");
4610 continue;
4611 }
4612
4613 print_dwarf_vma (begin + base_address, pointer_size);
4614 print_dwarf_vma (end + base_address, pointer_size);
4615
4616 if (begin == end)
4617 fputs (_("(start == end)"), stdout);
4618 else if (begin > end)
4619 fputs (_("(start > end)"), stdout);
4620
4621 putchar ('\n');
4622 }
4623 }
4624 putchar ('\n');
4625
4626 free (range_entries);
4627
4628 return 1;
4629 }
4630
4631 typedef struct Frame_Chunk
4632 {
4633 struct Frame_Chunk *next;
4634 unsigned char *chunk_start;
4635 int ncols;
4636 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
4637 short int *col_type;
4638 int *col_offset;
4639 char *augmentation;
4640 unsigned int code_factor;
4641 int data_factor;
4642 unsigned long pc_begin;
4643 unsigned long pc_range;
4644 int cfa_reg;
4645 int cfa_offset;
4646 int ra;
4647 unsigned char fde_encoding;
4648 unsigned char cfa_exp;
4649 unsigned char ptr_size;
4650 unsigned char segment_size;
4651 }
4652 Frame_Chunk;
4653
4654 static const char *const *dwarf_regnames;
4655 static unsigned int dwarf_regnames_count;
4656
4657 /* A marker for a col_type that means this column was never referenced
4658 in the frame info. */
4659 #define DW_CFA_unreferenced (-1)
4660
4661 /* Return 0 if not more space is needed, 1 if more space is needed,
4662 -1 for invalid reg. */
4663
4664 static int
4665 frame_need_space (Frame_Chunk *fc, unsigned int reg)
4666 {
4667 int prev = fc->ncols;
4668
4669 if (reg < (unsigned int) fc->ncols)
4670 return 0;
4671
4672 if (dwarf_regnames_count
4673 && reg > dwarf_regnames_count)
4674 return -1;
4675
4676 fc->ncols = reg + 1;
4677 fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
4678 sizeof (short int));
4679 fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
4680
4681 while (prev < fc->ncols)
4682 {
4683 fc->col_type[prev] = DW_CFA_unreferenced;
4684 fc->col_offset[prev] = 0;
4685 prev++;
4686 }
4687 return 1;
4688 }
4689
4690 static const char *const dwarf_regnames_i386[] =
4691 {
4692 "eax", "ecx", "edx", "ebx",
4693 "esp", "ebp", "esi", "edi",
4694 "eip", "eflags", NULL,
4695 "st0", "st1", "st2", "st3",
4696 "st4", "st5", "st6", "st7",
4697 NULL, NULL,
4698 "xmm0", "xmm1", "xmm2", "xmm3",
4699 "xmm4", "xmm5", "xmm6", "xmm7",
4700 "mm0", "mm1", "mm2", "mm3",
4701 "mm4", "mm5", "mm6", "mm7",
4702 "fcw", "fsw", "mxcsr",
4703 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
4704 "tr", "ldtr"
4705 };
4706
4707 void
4708 init_dwarf_regnames_i386 (void)
4709 {
4710 dwarf_regnames = dwarf_regnames_i386;
4711 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
4712 }
4713
4714 static const char *const dwarf_regnames_x86_64[] =
4715 {
4716 "rax", "rdx", "rcx", "rbx",
4717 "rsi", "rdi", "rbp", "rsp",
4718 "r8", "r9", "r10", "r11",
4719 "r12", "r13", "r14", "r15",
4720 "rip",
4721 "xmm0", "xmm1", "xmm2", "xmm3",
4722 "xmm4", "xmm5", "xmm6", "xmm7",
4723 "xmm8", "xmm9", "xmm10", "xmm11",
4724 "xmm12", "xmm13", "xmm14", "xmm15",
4725 "st0", "st1", "st2", "st3",
4726 "st4", "st5", "st6", "st7",
4727 "mm0", "mm1", "mm2", "mm3",
4728 "mm4", "mm5", "mm6", "mm7",
4729 "rflags",
4730 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
4731 "fs.base", "gs.base", NULL, NULL,
4732 "tr", "ldtr",
4733 "mxcsr", "fcw", "fsw"
4734 };
4735
4736 void
4737 init_dwarf_regnames_x86_64 (void)
4738 {
4739 dwarf_regnames = dwarf_regnames_x86_64;
4740 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
4741 }
4742
4743 void
4744 init_dwarf_regnames (unsigned int e_machine)
4745 {
4746 switch (e_machine)
4747 {
4748 case EM_386:
4749 case EM_486:
4750 init_dwarf_regnames_i386 ();
4751 break;
4752
4753 case EM_X86_64:
4754 case EM_L1OM:
4755 case EM_K1OM:
4756 init_dwarf_regnames_x86_64 ();
4757 break;
4758
4759 default:
4760 break;
4761 }
4762 }
4763
4764 static const char *
4765 regname (unsigned int regno, int row)
4766 {
4767 static char reg[64];
4768 if (dwarf_regnames
4769 && regno < dwarf_regnames_count
4770 && dwarf_regnames [regno] != NULL)
4771 {
4772 if (row)
4773 return dwarf_regnames [regno];
4774 snprintf (reg, sizeof (reg), "r%d (%s)", regno,
4775 dwarf_regnames [regno]);
4776 }
4777 else
4778 snprintf (reg, sizeof (reg), "r%d", regno);
4779 return reg;
4780 }
4781
4782 static void
4783 frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
4784 {
4785 int r;
4786 char tmp[100];
4787
4788 if (*max_regs < fc->ncols)
4789 *max_regs = fc->ncols;
4790
4791 if (*need_col_headers)
4792 {
4793 static const char *sloc = " LOC";
4794
4795 *need_col_headers = 0;
4796
4797 printf ("%-*s CFA ", eh_addr_size * 2, sloc);
4798
4799 for (r = 0; r < *max_regs; r++)
4800 if (fc->col_type[r] != DW_CFA_unreferenced)
4801 {
4802 if (r == fc->ra)
4803 printf ("ra ");
4804 else
4805 printf ("%-5s ", regname (r, 1));
4806 }
4807
4808 printf ("\n");
4809 }
4810
4811 printf ("%0*lx ", eh_addr_size * 2, fc->pc_begin);
4812 if (fc->cfa_exp)
4813 strcpy (tmp, "exp");
4814 else
4815 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), fc->cfa_offset);
4816 printf ("%-8s ", tmp);
4817
4818 for (r = 0; r < fc->ncols; r++)
4819 {
4820 if (fc->col_type[r] != DW_CFA_unreferenced)
4821 {
4822 switch (fc->col_type[r])
4823 {
4824 case DW_CFA_undefined:
4825 strcpy (tmp, "u");
4826 break;
4827 case DW_CFA_same_value:
4828 strcpy (tmp, "s");
4829 break;
4830 case DW_CFA_offset:
4831 sprintf (tmp, "c%+d", fc->col_offset[r]);
4832 break;
4833 case DW_CFA_val_offset:
4834 sprintf (tmp, "v%+d", fc->col_offset[r]);
4835 break;
4836 case DW_CFA_register:
4837 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
4838 break;
4839 case DW_CFA_expression:
4840 strcpy (tmp, "exp");
4841 break;
4842 case DW_CFA_val_expression:
4843 strcpy (tmp, "vexp");
4844 break;
4845 default:
4846 strcpy (tmp, "n/a");
4847 break;
4848 }
4849 printf ("%-5s ", tmp);
4850 }
4851 }
4852 printf ("\n");
4853 }
4854
4855 #define GET(N) byte_get (start, N); start += N
4856 #define LEB() read_leb128 (start, & length_return, 0); start += length_return
4857 #define SLEB() read_sleb128 (start, & length_return); start += length_return
4858
4859 static int
4860 display_debug_frames (struct dwarf_section *section,
4861 void *file ATTRIBUTE_UNUSED)
4862 {
4863 unsigned char *start = section->start;
4864 unsigned char *end = start + section->size;
4865 unsigned char *section_start = start;
4866 Frame_Chunk *chunks = 0;
4867 Frame_Chunk *remembered_state = 0;
4868 Frame_Chunk *rs;
4869 int is_eh = strcmp (section->name, ".eh_frame") == 0;
4870 unsigned int length_return;
4871 int max_regs = 0;
4872 const char *bad_reg = _("bad register: ");
4873 int saved_eh_addr_size = eh_addr_size;
4874
4875 printf (_("Contents of the %s section:\n"), section->name);
4876
4877 while (start < end)
4878 {
4879 unsigned char *saved_start;
4880 unsigned char *block_end;
4881 unsigned long length;
4882 unsigned long cie_id;
4883 Frame_Chunk *fc;
4884 Frame_Chunk *cie;
4885 int need_col_headers = 1;
4886 unsigned char *augmentation_data = NULL;
4887 unsigned long augmentation_data_len = 0;
4888 int encoded_ptr_size = saved_eh_addr_size;
4889 int offset_size;
4890 int initial_length_size;
4891
4892 saved_start = start;
4893 length = byte_get (start, 4); start += 4;
4894
4895 if (length == 0)
4896 {
4897 printf ("\n%08lx ZERO terminator\n\n",
4898 (unsigned long)(saved_start - section_start));
4899 continue;
4900 }
4901
4902 if (length == 0xffffffff)
4903 {
4904 length = byte_get (start, 8);
4905 start += 8;
4906 offset_size = 8;
4907 initial_length_size = 12;
4908 }
4909 else
4910 {
4911 offset_size = 4;
4912 initial_length_size = 4;
4913 }
4914
4915 block_end = saved_start + length + initial_length_size;
4916 if (block_end > end)
4917 {
4918 warn ("Invalid length %#08lx in FDE at %#08lx\n",
4919 length, (unsigned long)(saved_start - section_start));
4920 block_end = end;
4921 }
4922 cie_id = byte_get (start, offset_size); start += offset_size;
4923
4924 if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
4925 {
4926 int version;
4927
4928 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
4929 memset (fc, 0, sizeof (Frame_Chunk));
4930
4931 fc->next = chunks;
4932 chunks = fc;
4933 fc->chunk_start = saved_start;
4934 fc->ncols = 0;
4935 fc->col_type = (short int *) xmalloc (sizeof (short int));
4936 fc->col_offset = (int *) xmalloc (sizeof (int));
4937 frame_need_space (fc, max_regs - 1);
4938
4939 version = *start++;
4940
4941 fc->augmentation = (char *) start;
4942 start = (unsigned char *) strchr ((char *) start, '\0') + 1;
4943
4944 if (strcmp (fc->augmentation, "eh") == 0)
4945 start += eh_addr_size;
4946
4947 if (version >= 4)
4948 {
4949 fc->ptr_size = GET (1);
4950 fc->segment_size = GET (1);
4951 eh_addr_size = fc->ptr_size;
4952 }
4953 else
4954 {
4955 fc->ptr_size = eh_addr_size;
4956 fc->segment_size = 0;
4957 }
4958 fc->code_factor = LEB ();
4959 fc->data_factor = SLEB ();
4960 if (version == 1)
4961 {
4962 fc->ra = GET (1);
4963 }
4964 else
4965 {
4966 fc->ra = LEB ();
4967 }
4968
4969 if (fc->augmentation[0] == 'z')
4970 {
4971 augmentation_data_len = LEB ();
4972 augmentation_data = start;
4973 start += augmentation_data_len;
4974 }
4975 cie = fc;
4976
4977 if (do_debug_frames_interp)
4978 printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
4979 (unsigned long)(saved_start - section_start), length, cie_id,
4980 fc->augmentation, fc->code_factor, fc->data_factor,
4981 fc->ra);
4982 else
4983 {
4984 printf ("\n%08lx %08lx %08lx CIE\n",
4985 (unsigned long)(saved_start - section_start), length, cie_id);
4986 printf (" Version: %d\n", version);
4987 printf (" Augmentation: \"%s\"\n", fc->augmentation);
4988 if (version >= 4)
4989 {
4990 printf (" Pointer Size: %u\n", fc->ptr_size);
4991 printf (" Segment Size: %u\n", fc->segment_size);
4992 }
4993 printf (" Code alignment factor: %u\n", fc->code_factor);
4994 printf (" Data alignment factor: %d\n", fc->data_factor);
4995 printf (" Return address column: %d\n", fc->ra);
4996
4997 if (augmentation_data_len)
4998 {
4999 unsigned long i;
5000 printf (" Augmentation data: ");
5001 for (i = 0; i < augmentation_data_len; ++i)
5002 printf (" %02x", augmentation_data[i]);
5003 putchar ('\n');
5004 }
5005 putchar ('\n');
5006 }
5007
5008 if (augmentation_data_len)
5009 {
5010 unsigned char *p, *q;
5011 p = (unsigned char *) fc->augmentation + 1;
5012 q = augmentation_data;
5013
5014 while (1)
5015 {
5016 if (*p == 'L')
5017 q++;
5018 else if (*p == 'P')
5019 q += 1 + size_of_encoded_value (*q);
5020 else if (*p == 'R')
5021 fc->fde_encoding = *q++;
5022 else if (*p == 'S')
5023 ;
5024 else
5025 break;
5026 p++;
5027 }
5028
5029 if (fc->fde_encoding)
5030 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
5031 }
5032
5033 frame_need_space (fc, fc->ra);
5034 }
5035 else
5036 {
5037 unsigned char *look_for;
5038 static Frame_Chunk fde_fc;
5039 unsigned long segment_selector;
5040
5041 fc = & fde_fc;
5042 memset (fc, 0, sizeof (Frame_Chunk));
5043
5044 look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
5045
5046 for (cie = chunks; cie ; cie = cie->next)
5047 if (cie->chunk_start == look_for)
5048 break;
5049
5050 if (!cie)
5051 {
5052 warn ("Invalid CIE pointer %#08lx in FDE at %#08lx\n",
5053 cie_id, (unsigned long)(saved_start - section_start));
5054 fc->ncols = 0;
5055 fc->col_type = (short int *) xmalloc (sizeof (short int));
5056 fc->col_offset = (int *) xmalloc (sizeof (int));
5057 frame_need_space (fc, max_regs - 1);
5058 cie = fc;
5059 fc->augmentation = "";
5060 fc->fde_encoding = 0;
5061 fc->ptr_size = eh_addr_size;
5062 fc->segment_size = 0;
5063 }
5064 else
5065 {
5066 fc->ncols = cie->ncols;
5067 fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
5068 fc->col_offset = (int *) xcmalloc (fc->ncols, sizeof (int));
5069 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
5070 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
5071 fc->augmentation = cie->augmentation;
5072 fc->ptr_size = cie->ptr_size;
5073 eh_addr_size = cie->ptr_size;
5074 fc->segment_size = cie->segment_size;
5075 fc->code_factor = cie->code_factor;
5076 fc->data_factor = cie->data_factor;
5077 fc->cfa_reg = cie->cfa_reg;
5078 fc->cfa_offset = cie->cfa_offset;
5079 fc->ra = cie->ra;
5080 frame_need_space (fc, max_regs - 1);
5081 fc->fde_encoding = cie->fde_encoding;
5082 }
5083
5084 if (fc->fde_encoding)
5085 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
5086
5087 segment_selector = 0;
5088 if (fc->segment_size)
5089 {
5090 segment_selector = byte_get (start, fc->segment_size);
5091 start += fc->segment_size;
5092 }
5093 fc->pc_begin = get_encoded_value (start, fc->fde_encoding, section);
5094 start += encoded_ptr_size;
5095 fc->pc_range = byte_get (start, encoded_ptr_size);
5096 start += encoded_ptr_size;
5097
5098 if (cie->augmentation[0] == 'z')
5099 {
5100 augmentation_data_len = LEB ();
5101 augmentation_data = start;
5102 start += augmentation_data_len;
5103 }
5104
5105 printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=",
5106 (unsigned long)(saved_start - section_start), length, cie_id,
5107 (unsigned long)(cie->chunk_start - section_start));
5108 if (fc->segment_size)
5109 printf ("%04lx:", segment_selector);
5110 printf ("%08lx..%08lx\n", fc->pc_begin, fc->pc_begin + fc->pc_range);
5111 if (! do_debug_frames_interp && augmentation_data_len)
5112 {
5113 unsigned long i;
5114
5115 printf (" Augmentation data: ");
5116 for (i = 0; i < augmentation_data_len; ++i)
5117 printf (" %02x", augmentation_data[i]);
5118 putchar ('\n');
5119 putchar ('\n');
5120 }
5121 }
5122
5123 /* At this point, fc is the current chunk, cie (if any) is set, and
5124 we're about to interpret instructions for the chunk. */
5125 /* ??? At present we need to do this always, since this sizes the
5126 fc->col_type and fc->col_offset arrays, which we write into always.
5127 We should probably split the interpreted and non-interpreted bits
5128 into two different routines, since there's so much that doesn't
5129 really overlap between them. */
5130 if (1 || do_debug_frames_interp)
5131 {
5132 /* Start by making a pass over the chunk, allocating storage
5133 and taking note of what registers are used. */
5134 unsigned char *tmp = start;
5135
5136 while (start < block_end)
5137 {
5138 unsigned op, opa;
5139 unsigned long reg, temp;
5140
5141 op = *start++;
5142 opa = op & 0x3f;
5143 if (op & 0xc0)
5144 op &= 0xc0;
5145
5146 /* Warning: if you add any more cases to this switch, be
5147 sure to add them to the corresponding switch below. */
5148 switch (op)
5149 {
5150 case DW_CFA_advance_loc:
5151 break;
5152 case DW_CFA_offset:
5153 LEB ();
5154 if (frame_need_space (fc, opa) >= 0)
5155 fc->col_type[opa] = DW_CFA_undefined;
5156 break;
5157 case DW_CFA_restore:
5158 if (frame_need_space (fc, opa) >= 0)
5159 fc->col_type[opa] = DW_CFA_undefined;
5160 break;
5161 case DW_CFA_set_loc:
5162 start += encoded_ptr_size;
5163 break;
5164 case DW_CFA_advance_loc1:
5165 start += 1;
5166 break;
5167 case DW_CFA_advance_loc2:
5168 start += 2;
5169 break;
5170 case DW_CFA_advance_loc4:
5171 start += 4;
5172 break;
5173 case DW_CFA_offset_extended:
5174 case DW_CFA_val_offset:
5175 reg = LEB (); LEB ();
5176 if (frame_need_space (fc, reg) >= 0)
5177 fc->col_type[reg] = DW_CFA_undefined;
5178 break;
5179 case DW_CFA_restore_extended:
5180 reg = LEB ();
5181 frame_need_space (fc, reg);
5182 if (frame_need_space (fc, reg) >= 0)
5183 fc->col_type[reg] = DW_CFA_undefined;
5184 break;
5185 case DW_CFA_undefined:
5186 reg = LEB ();
5187 if (frame_need_space (fc, reg) >= 0)
5188 fc->col_type[reg] = DW_CFA_undefined;
5189 break;
5190 case DW_CFA_same_value:
5191 reg = LEB ();
5192 if (frame_need_space (fc, reg) >= 0)
5193 fc->col_type[reg] = DW_CFA_undefined;
5194 break;
5195 case DW_CFA_register:
5196 reg = LEB (); LEB ();
5197 if (frame_need_space (fc, reg) >= 0)
5198 fc->col_type[reg] = DW_CFA_undefined;
5199 break;
5200 case DW_CFA_def_cfa:
5201 LEB (); LEB ();
5202 break;
5203 case DW_CFA_def_cfa_register:
5204 LEB ();
5205 break;
5206 case DW_CFA_def_cfa_offset:
5207 LEB ();
5208 break;
5209 case DW_CFA_def_cfa_expression:
5210 temp = LEB ();
5211 start += temp;
5212 break;
5213 case DW_CFA_expression:
5214 case DW_CFA_val_expression:
5215 reg = LEB ();
5216 temp = LEB ();
5217 start += temp;
5218 if (frame_need_space (fc, reg) >= 0)
5219 fc->col_type[reg] = DW_CFA_undefined;
5220 break;
5221 case DW_CFA_offset_extended_sf:
5222 case DW_CFA_val_offset_sf:
5223 reg = LEB (); SLEB ();
5224 if (frame_need_space (fc, reg) >= 0)
5225 fc->col_type[reg] = DW_CFA_undefined;
5226 break;
5227 case DW_CFA_def_cfa_sf:
5228 LEB (); SLEB ();
5229 break;
5230 case DW_CFA_def_cfa_offset_sf:
5231 SLEB ();
5232 break;
5233 case DW_CFA_MIPS_advance_loc8:
5234 start += 8;
5235 break;
5236 case DW_CFA_GNU_args_size:
5237 LEB ();
5238 break;
5239 case DW_CFA_GNU_negative_offset_extended:
5240 reg = LEB (); LEB ();
5241 if (frame_need_space (fc, reg) >= 0)
5242 fc->col_type[reg] = DW_CFA_undefined;
5243 break;
5244 default:
5245 break;
5246 }
5247 }
5248 start = tmp;
5249 }
5250
5251 /* Now we know what registers are used, make a second pass over
5252 the chunk, this time actually printing out the info. */
5253
5254 while (start < block_end)
5255 {
5256 unsigned op, opa;
5257 unsigned long ul, reg, roffs;
5258 long l, ofs;
5259 dwarf_vma vma;
5260 const char *reg_prefix = "";
5261
5262 op = *start++;
5263 opa = op & 0x3f;
5264 if (op & 0xc0)
5265 op &= 0xc0;
5266
5267 /* Warning: if you add any more cases to this switch, be
5268 sure to add them to the corresponding switch above. */
5269 switch (op)
5270 {
5271 case DW_CFA_advance_loc:
5272 if (do_debug_frames_interp)
5273 frame_display_row (fc, &need_col_headers, &max_regs);
5274 else
5275 printf (" DW_CFA_advance_loc: %d to %08lx\n",
5276 opa * fc->code_factor,
5277 fc->pc_begin + opa * fc->code_factor);
5278 fc->pc_begin += opa * fc->code_factor;
5279 break;
5280
5281 case DW_CFA_offset:
5282 roffs = LEB ();
5283 if (opa >= (unsigned int) fc->ncols)
5284 reg_prefix = bad_reg;
5285 if (! do_debug_frames_interp || *reg_prefix != '\0')
5286 printf (" DW_CFA_offset: %s%s at cfa%+ld\n",
5287 reg_prefix, regname (opa, 0),
5288 roffs * fc->data_factor);
5289 if (*reg_prefix == '\0')
5290 {
5291 fc->col_type[opa] = DW_CFA_offset;
5292 fc->col_offset[opa] = roffs * fc->data_factor;
5293 }
5294 break;
5295
5296 case DW_CFA_restore:
5297 if (opa >= (unsigned int) cie->ncols
5298 || opa >= (unsigned int) fc->ncols)
5299 reg_prefix = bad_reg;
5300 if (! do_debug_frames_interp || *reg_prefix != '\0')
5301 printf (" DW_CFA_restore: %s%s\n",
5302 reg_prefix, regname (opa, 0));
5303 if (*reg_prefix == '\0')
5304 {
5305 fc->col_type[opa] = cie->col_type[opa];
5306 fc->col_offset[opa] = cie->col_offset[opa];
5307 if (do_debug_frames_interp
5308 && fc->col_type[opa] == DW_CFA_unreferenced)
5309 fc->col_type[opa] = DW_CFA_undefined;
5310 }
5311 break;
5312
5313 case DW_CFA_set_loc:
5314 vma = get_encoded_value (start, fc->fde_encoding, section);
5315 start += encoded_ptr_size;
5316 if (do_debug_frames_interp)
5317 frame_display_row (fc, &need_col_headers, &max_regs);
5318 else
5319 printf (" DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
5320 fc->pc_begin = vma;
5321 break;
5322
5323 case DW_CFA_advance_loc1:
5324 ofs = byte_get (start, 1); start += 1;
5325 if (do_debug_frames_interp)
5326 frame_display_row (fc, &need_col_headers, &max_regs);
5327 else
5328 printf (" DW_CFA_advance_loc1: %ld to %08lx\n",
5329 ofs * fc->code_factor,
5330 fc->pc_begin + ofs * fc->code_factor);
5331 fc->pc_begin += ofs * fc->code_factor;
5332 break;
5333
5334 case DW_CFA_advance_loc2:
5335 ofs = byte_get (start, 2); start += 2;
5336 if (do_debug_frames_interp)
5337 frame_display_row (fc, &need_col_headers, &max_regs);
5338 else
5339 printf (" DW_CFA_advance_loc2: %ld to %08lx\n",
5340 ofs * fc->code_factor,
5341 fc->pc_begin + ofs * fc->code_factor);
5342 fc->pc_begin += ofs * fc->code_factor;
5343 break;
5344
5345 case DW_CFA_advance_loc4:
5346 ofs = byte_get (start, 4); start += 4;
5347 if (do_debug_frames_interp)
5348 frame_display_row (fc, &need_col_headers, &max_regs);
5349 else
5350 printf (" DW_CFA_advance_loc4: %ld to %08lx\n",
5351 ofs * fc->code_factor,
5352 fc->pc_begin + ofs * fc->code_factor);
5353 fc->pc_begin += ofs * fc->code_factor;
5354 break;
5355
5356 case DW_CFA_offset_extended:
5357 reg = LEB ();
5358 roffs = LEB ();
5359 if (reg >= (unsigned int) fc->ncols)
5360 reg_prefix = bad_reg;
5361 if (! do_debug_frames_interp || *reg_prefix != '\0')
5362 printf (" DW_CFA_offset_extended: %s%s at cfa%+ld\n",
5363 reg_prefix, regname (reg, 0),
5364 roffs * fc->data_factor);
5365 if (*reg_prefix == '\0')
5366 {
5367 fc->col_type[reg] = DW_CFA_offset;
5368 fc->col_offset[reg] = roffs * fc->data_factor;
5369 }
5370 break;
5371
5372 case DW_CFA_val_offset:
5373 reg = LEB ();
5374 roffs = LEB ();
5375 if (reg >= (unsigned int) fc->ncols)
5376 reg_prefix = bad_reg;
5377 if (! do_debug_frames_interp || *reg_prefix != '\0')
5378 printf (" DW_CFA_val_offset: %s%s at cfa%+ld\n",
5379 reg_prefix, regname (reg, 0),
5380 roffs * fc->data_factor);
5381 if (*reg_prefix == '\0')
5382 {
5383 fc->col_type[reg] = DW_CFA_val_offset;
5384 fc->col_offset[reg] = roffs * fc->data_factor;
5385 }
5386 break;
5387
5388 case DW_CFA_restore_extended:
5389 reg = LEB ();
5390 if (reg >= (unsigned int) cie->ncols
5391 || reg >= (unsigned int) fc->ncols)
5392 reg_prefix = bad_reg;
5393 if (! do_debug_frames_interp || *reg_prefix != '\0')
5394 printf (" DW_CFA_restore_extended: %s%s\n",
5395 reg_prefix, regname (reg, 0));
5396 if (*reg_prefix == '\0')
5397 {
5398 fc->col_type[reg] = cie->col_type[reg];
5399 fc->col_offset[reg] = cie->col_offset[reg];
5400 }
5401 break;
5402
5403 case DW_CFA_undefined:
5404 reg = LEB ();
5405 if (reg >= (unsigned int) fc->ncols)
5406 reg_prefix = bad_reg;
5407 if (! do_debug_frames_interp || *reg_prefix != '\0')
5408 printf (" DW_CFA_undefined: %s%s\n",
5409 reg_prefix, regname (reg, 0));
5410 if (*reg_prefix == '\0')
5411 {
5412 fc->col_type[reg] = DW_CFA_undefined;
5413 fc->col_offset[reg] = 0;
5414 }
5415 break;
5416
5417 case DW_CFA_same_value:
5418 reg = LEB ();
5419 if (reg >= (unsigned int) fc->ncols)
5420 reg_prefix = bad_reg;
5421 if (! do_debug_frames_interp || *reg_prefix != '\0')
5422 printf (" DW_CFA_same_value: %s%s\n",
5423 reg_prefix, regname (reg, 0));
5424 if (*reg_prefix == '\0')
5425 {
5426 fc->col_type[reg] = DW_CFA_same_value;
5427 fc->col_offset[reg] = 0;
5428 }
5429 break;
5430
5431 case DW_CFA_register:
5432 reg = LEB ();
5433 roffs = LEB ();
5434 if (reg >= (unsigned int) fc->ncols)
5435 reg_prefix = bad_reg;
5436 if (! do_debug_frames_interp || *reg_prefix != '\0')
5437 {
5438 printf (" DW_CFA_register: %s%s in ",
5439 reg_prefix, regname (reg, 0));
5440 puts (regname (roffs, 0));
5441 }
5442 if (*reg_prefix == '\0')
5443 {
5444 fc->col_type[reg] = DW_CFA_register;
5445 fc->col_offset[reg] = roffs;
5446 }
5447 break;
5448
5449 case DW_CFA_remember_state:
5450 if (! do_debug_frames_interp)
5451 printf (" DW_CFA_remember_state\n");
5452 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
5453 rs->ncols = fc->ncols;
5454 rs->col_type = (short int *) xcmalloc (rs->ncols,
5455 sizeof (short int));
5456 rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (int));
5457 memcpy (rs->col_type, fc->col_type, rs->ncols);
5458 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
5459 rs->next = remembered_state;
5460 remembered_state = rs;
5461 break;
5462
5463 case DW_CFA_restore_state:
5464 if (! do_debug_frames_interp)
5465 printf (" DW_CFA_restore_state\n");
5466 rs = remembered_state;
5467 if (rs)
5468 {
5469 remembered_state = rs->next;
5470 frame_need_space (fc, rs->ncols - 1);
5471 memcpy (fc->col_type, rs->col_type, rs->ncols);
5472 memcpy (fc->col_offset, rs->col_offset,
5473 rs->ncols * sizeof (int));
5474 free (rs->col_type);
5475 free (rs->col_offset);
5476 free (rs);
5477 }
5478 else if (do_debug_frames_interp)
5479 printf ("Mismatched DW_CFA_restore_state\n");
5480 break;
5481
5482 case DW_CFA_def_cfa:
5483 fc->cfa_reg = LEB ();
5484 fc->cfa_offset = LEB ();
5485 fc->cfa_exp = 0;
5486 if (! do_debug_frames_interp)
5487 printf (" DW_CFA_def_cfa: %s ofs %d\n",
5488 regname (fc->cfa_reg, 0), fc->cfa_offset);
5489 break;
5490
5491 case DW_CFA_def_cfa_register:
5492 fc->cfa_reg = LEB ();
5493 fc->cfa_exp = 0;
5494 if (! do_debug_frames_interp)
5495 printf (" DW_CFA_def_cfa_register: %s\n",
5496 regname (fc->cfa_reg, 0));
5497 break;
5498
5499 case DW_CFA_def_cfa_offset:
5500 fc->cfa_offset = LEB ();
5501 if (! do_debug_frames_interp)
5502 printf (" DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
5503 break;
5504
5505 case DW_CFA_nop:
5506 if (! do_debug_frames_interp)
5507 printf (" DW_CFA_nop\n");
5508 break;
5509
5510 case DW_CFA_def_cfa_expression:
5511 ul = LEB ();
5512 if (! do_debug_frames_interp)
5513 {
5514 printf (" DW_CFA_def_cfa_expression (");
5515 decode_location_expression (start, eh_addr_size, 0, -1,
5516 ul, 0, section);
5517 printf (")\n");
5518 }
5519 fc->cfa_exp = 1;
5520 start += ul;
5521 break;
5522
5523 case DW_CFA_expression:
5524 reg = LEB ();
5525 ul = LEB ();
5526 if (reg >= (unsigned int) fc->ncols)
5527 reg_prefix = bad_reg;
5528 if (! do_debug_frames_interp || *reg_prefix != '\0')
5529 {
5530 printf (" DW_CFA_expression: %s%s (",
5531 reg_prefix, regname (reg, 0));
5532 decode_location_expression (start, eh_addr_size, 0, -1,
5533 ul, 0, section);
5534 printf (")\n");
5535 }
5536 if (*reg_prefix == '\0')
5537 fc->col_type[reg] = DW_CFA_expression;
5538 start += ul;
5539 break;
5540
5541 case DW_CFA_val_expression:
5542 reg = LEB ();
5543 ul = LEB ();
5544 if (reg >= (unsigned int) fc->ncols)
5545 reg_prefix = bad_reg;
5546 if (! do_debug_frames_interp || *reg_prefix != '\0')
5547 {
5548 printf (" DW_CFA_val_expression: %s%s (",
5549 reg_prefix, regname (reg, 0));
5550 decode_location_expression (start, eh_addr_size, 0, -1,
5551 ul, 0, section);
5552 printf (")\n");
5553 }
5554 if (*reg_prefix == '\0')
5555 fc->col_type[reg] = DW_CFA_val_expression;
5556 start += ul;
5557 break;
5558
5559 case DW_CFA_offset_extended_sf:
5560 reg = LEB ();
5561 l = SLEB ();
5562 if (frame_need_space (fc, reg) < 0)
5563 reg_prefix = bad_reg;
5564 if (! do_debug_frames_interp || *reg_prefix != '\0')
5565 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
5566 reg_prefix, regname (reg, 0),
5567 l * fc->data_factor);
5568 if (*reg_prefix == '\0')
5569 {
5570 fc->col_type[reg] = DW_CFA_offset;
5571 fc->col_offset[reg] = l * fc->data_factor;
5572 }
5573 break;
5574
5575 case DW_CFA_val_offset_sf:
5576 reg = LEB ();
5577 l = SLEB ();
5578 if (frame_need_space (fc, reg) < 0)
5579 reg_prefix = bad_reg;
5580 if (! do_debug_frames_interp || *reg_prefix != '\0')
5581 printf (" DW_CFA_val_offset_sf: %s%s at cfa%+ld\n",
5582 reg_prefix, regname (reg, 0),
5583 l * fc->data_factor);
5584 if (*reg_prefix == '\0')
5585 {
5586 fc->col_type[reg] = DW_CFA_val_offset;
5587 fc->col_offset[reg] = l * fc->data_factor;
5588 }
5589 break;
5590
5591 case DW_CFA_def_cfa_sf:
5592 fc->cfa_reg = LEB ();
5593 fc->cfa_offset = SLEB ();
5594 fc->cfa_offset = fc->cfa_offset * fc->data_factor;
5595 fc->cfa_exp = 0;
5596 if (! do_debug_frames_interp)
5597 printf (" DW_CFA_def_cfa_sf: %s ofs %d\n",
5598 regname (fc->cfa_reg, 0), fc->cfa_offset);
5599 break;
5600
5601 case DW_CFA_def_cfa_offset_sf:
5602 fc->cfa_offset = SLEB ();
5603 fc->cfa_offset = fc->cfa_offset * fc->data_factor;
5604 if (! do_debug_frames_interp)
5605 printf (" DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
5606 break;
5607
5608 case DW_CFA_MIPS_advance_loc8:
5609 ofs = byte_get (start, 8); start += 8;
5610 if (do_debug_frames_interp)
5611 frame_display_row (fc, &need_col_headers, &max_regs);
5612 else
5613 printf (" DW_CFA_MIPS_advance_loc8: %ld to %08lx\n",
5614 ofs * fc->code_factor,
5615 fc->pc_begin + ofs * fc->code_factor);
5616 fc->pc_begin += ofs * fc->code_factor;
5617 break;
5618
5619 case DW_CFA_GNU_window_save:
5620 if (! do_debug_frames_interp)
5621 printf (" DW_CFA_GNU_window_save\n");
5622 break;
5623
5624 case DW_CFA_GNU_args_size:
5625 ul = LEB ();
5626 if (! do_debug_frames_interp)
5627 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
5628 break;
5629
5630 case DW_CFA_GNU_negative_offset_extended:
5631 reg = LEB ();
5632 l = - LEB ();
5633 if (frame_need_space (fc, reg) < 0)
5634 reg_prefix = bad_reg;
5635 if (! do_debug_frames_interp || *reg_prefix != '\0')
5636 printf (" DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
5637 reg_prefix, regname (reg, 0),
5638 l * fc->data_factor);
5639 if (*reg_prefix == '\0')
5640 {
5641 fc->col_type[reg] = DW_CFA_offset;
5642 fc->col_offset[reg] = l * fc->data_factor;
5643 }
5644 break;
5645
5646 default:
5647 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
5648 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
5649 else
5650 warn (_("unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
5651 start = block_end;
5652 }
5653 }
5654
5655 if (do_debug_frames_interp)
5656 frame_display_row (fc, &need_col_headers, &max_regs);
5657
5658 start = block_end;
5659 eh_addr_size = saved_eh_addr_size;
5660 }
5661
5662 printf ("\n");
5663
5664 return 1;
5665 }
5666
5667 #undef GET
5668 #undef LEB
5669 #undef SLEB
5670
5671 static int
5672 display_gdb_index (struct dwarf_section *section,
5673 void *file ATTRIBUTE_UNUSED)
5674 {
5675 unsigned char *start = section->start;
5676 uint32_t version;
5677 uint32_t cu_list_offset, tu_list_offset;
5678 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
5679 unsigned int cu_list_elements, tu_list_elements;
5680 unsigned int address_table_size, symbol_table_slots;
5681 unsigned char *cu_list, *tu_list;
5682 unsigned char *address_table, *symbol_table, *constant_pool;
5683 unsigned int i;
5684
5685 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
5686
5687 printf (_("Contents of the %s section:\n"), section->name);
5688
5689 if (section->size < 6 * sizeof (uint32_t))
5690 {
5691 warn (_("Truncated header in the %s section.\n"), section->name);
5692 return 0;
5693 }
5694
5695 version = byte_get_little_endian (start, 4);
5696 printf (_("Version %ld\n"), (long) version);
5697
5698 /* Prior versions are obsolete, and future versions may not be
5699 backwards compatible. */
5700 if (version < 3 || version > 7)
5701 {
5702 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
5703 return 0;
5704 }
5705 if (version < 4)
5706 warn (_("The address table data in version 3 may be wrong.\n"));
5707 if (version < 5)
5708 warn (_("Version 4 does not support case insensitive lookups.\n"));
5709 if (version < 6)
5710 warn (_("Version 5 does not include inlined functions.\n"));
5711 if (version < 7)
5712 warn (_("Version 6 does not include symbol attributes.\n"));
5713
5714 cu_list_offset = byte_get_little_endian (start + 4, 4);
5715 tu_list_offset = byte_get_little_endian (start + 8, 4);
5716 address_table_offset = byte_get_little_endian (start + 12, 4);
5717 symbol_table_offset = byte_get_little_endian (start + 16, 4);
5718 constant_pool_offset = byte_get_little_endian (start + 20, 4);
5719
5720 if (cu_list_offset > section->size
5721 || tu_list_offset > section->size
5722 || address_table_offset > section->size
5723 || symbol_table_offset > section->size
5724 || constant_pool_offset > section->size)
5725 {
5726 warn (_("Corrupt header in the %s section.\n"), section->name);
5727 return 0;
5728 }
5729
5730 cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
5731 tu_list_elements = (address_table_offset - tu_list_offset) / 8;
5732 address_table_size = symbol_table_offset - address_table_offset;
5733 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
5734
5735 cu_list = start + cu_list_offset;
5736 tu_list = start + tu_list_offset;
5737 address_table = start + address_table_offset;
5738 symbol_table = start + symbol_table_offset;
5739 constant_pool = start + constant_pool_offset;
5740
5741 printf (_("\nCU table:\n"));
5742 for (i = 0; i < cu_list_elements; i += 2)
5743 {
5744 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
5745 uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
5746
5747 printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
5748 (unsigned long) cu_offset,
5749 (unsigned long) (cu_offset + cu_length - 1));
5750 }
5751
5752 printf (_("\nTU table:\n"));
5753 for (i = 0; i < tu_list_elements; i += 3)
5754 {
5755 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
5756 uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
5757 uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
5758
5759 printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
5760 (unsigned long) tu_offset,
5761 (unsigned long) type_offset);
5762 print_dwarf_vma (signature, 8);
5763 printf ("\n");
5764 }
5765
5766 printf (_("\nAddress table:\n"));
5767 for (i = 0; i < address_table_size; i += 2 * 8 + 4)
5768 {
5769 uint64_t low = byte_get_little_endian (address_table + i, 8);
5770 uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
5771 uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
5772
5773 print_dwarf_vma (low, 8);
5774 print_dwarf_vma (high, 8);
5775 printf (_("%lu\n"), (unsigned long) cu_index);
5776 }
5777
5778 printf (_("\nSymbol table:\n"));
5779 for (i = 0; i < symbol_table_slots; ++i)
5780 {
5781 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
5782 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
5783 uint32_t num_cus, cu;
5784
5785 if (name_offset != 0
5786 || cu_vector_offset != 0)
5787 {
5788 unsigned int j;
5789
5790 printf ("[%3u] %s:", i, constant_pool + name_offset);
5791 num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
5792 if (num_cus > 1)
5793 printf ("\n");
5794 for (j = 0; j < num_cus; ++j)
5795 {
5796 gdb_index_symbol_kind kind;
5797
5798 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
5799 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
5800 cu = GDB_INDEX_CU_VALUE (cu);
5801 /* Convert to TU number if it's for a type unit. */
5802 if (cu >= cu_list_elements / 2)
5803 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
5804 (unsigned long) (cu - cu_list_elements / 2));
5805 else
5806 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
5807
5808 switch (kind)
5809 {
5810 case GDB_INDEX_SYMBOL_KIND_NONE:
5811 printf (_(" [no symbol information]"));
5812 break;
5813 case GDB_INDEX_SYMBOL_KIND_TYPE:
5814 printf (_(" [type]"));
5815 break;
5816 case GDB_INDEX_SYMBOL_KIND_VARIABLE:
5817 printf (_(" [variable]"));
5818 break;
5819 case GDB_INDEX_SYMBOL_KIND_FUNCTION:
5820 printf (_(" [function]"));
5821 break;
5822 case GDB_INDEX_SYMBOL_KIND_OTHER:
5823 printf (_(" [other]"));
5824 break;
5825 default:
5826 printf (_(" [unknown: %d]"), kind);
5827 break;
5828 }
5829 if (num_cus > 1)
5830 printf ("\n");
5831 }
5832 if (num_cus <= 1)
5833 printf ("\n");
5834 }
5835 }
5836
5837 return 1;
5838 }
5839
5840 static int
5841 display_debug_not_supported (struct dwarf_section *section,
5842 void *file ATTRIBUTE_UNUSED)
5843 {
5844 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
5845 section->name);
5846
5847 return 1;
5848 }
5849
5850 void *
5851 cmalloc (size_t nmemb, size_t size)
5852 {
5853 /* Check for overflow. */
5854 if (nmemb >= ~(size_t) 0 / size)
5855 return NULL;
5856 else
5857 return malloc (nmemb * size);
5858 }
5859
5860 void *
5861 xcmalloc (size_t nmemb, size_t size)
5862 {
5863 /* Check for overflow. */
5864 if (nmemb >= ~(size_t) 0 / size)
5865 return NULL;
5866 else
5867 return xmalloc (nmemb * size);
5868 }
5869
5870 void *
5871 xcrealloc (void *ptr, size_t nmemb, size_t size)
5872 {
5873 /* Check for overflow. */
5874 if (nmemb >= ~(size_t) 0 / size)
5875 return NULL;
5876 else
5877 return xrealloc (ptr, nmemb * size);
5878 }
5879
5880 void
5881 free_debug_memory (void)
5882 {
5883 unsigned int i;
5884
5885 free_abbrevs ();
5886
5887 for (i = 0; i < max; i++)
5888 free_debug_section ((enum dwarf_section_display_enum) i);
5889
5890 if (debug_information != NULL)
5891 {
5892 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE)
5893 {
5894 for (i = 0; i < num_debug_info_entries; i++)
5895 {
5896 if (!debug_information [i].max_loc_offsets)
5897 {
5898 free (debug_information [i].loc_offsets);
5899 free (debug_information [i].have_frame_base);
5900 }
5901 if (!debug_information [i].max_range_lists)
5902 free (debug_information [i].range_lists);
5903 }
5904 }
5905
5906 free (debug_information);
5907 debug_information = NULL;
5908 num_debug_info_entries = 0;
5909 }
5910 }
5911
5912 void
5913 dwarf_select_sections_by_names (const char *names)
5914 {
5915 typedef struct
5916 {
5917 const char * option;
5918 int * variable;
5919 int val;
5920 }
5921 debug_dump_long_opts;
5922
5923 static const debug_dump_long_opts opts_table [] =
5924 {
5925 /* Please keep this table alpha- sorted. */
5926 { "Ranges", & do_debug_ranges, 1 },
5927 { "abbrev", & do_debug_abbrevs, 1 },
5928 { "aranges", & do_debug_aranges, 1 },
5929 { "frames", & do_debug_frames, 1 },
5930 { "frames-interp", & do_debug_frames_interp, 1 },
5931 { "info", & do_debug_info, 1 },
5932 { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility. */
5933 { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
5934 { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
5935 { "loc", & do_debug_loc, 1 },
5936 { "macro", & do_debug_macinfo, 1 },
5937 { "pubnames", & do_debug_pubnames, 1 },
5938 { "pubtypes", & do_debug_pubtypes, 1 },
5939 /* This entry is for compatability
5940 with earlier versions of readelf. */
5941 { "ranges", & do_debug_aranges, 1 },
5942 { "str", & do_debug_str, 1 },
5943 /* The special .gdb_index section. */
5944 { "gdb_index", & do_gdb_index, 1 },
5945 /* These trace_* sections are used by Itanium VMS. */
5946 { "trace_abbrev", & do_trace_abbrevs, 1 },
5947 { "trace_aranges", & do_trace_aranges, 1 },
5948 { "trace_info", & do_trace_info, 1 },
5949 { NULL, NULL, 0 }
5950 };
5951
5952 const char *p;
5953
5954 p = names;
5955 while (*p)
5956 {
5957 const debug_dump_long_opts * entry;
5958
5959 for (entry = opts_table; entry->option; entry++)
5960 {
5961 size_t len = strlen (entry->option);
5962
5963 if (strncmp (p, entry->option, len) == 0
5964 && (p[len] == ',' || p[len] == '\0'))
5965 {
5966 * entry->variable |= entry->val;
5967
5968 /* The --debug-dump=frames-interp option also
5969 enables the --debug-dump=frames option. */
5970 if (do_debug_frames_interp)
5971 do_debug_frames = 1;
5972
5973 p += len;
5974 break;
5975 }
5976 }
5977
5978 if (entry->option == NULL)
5979 {
5980 warn (_("Unrecognized debug option '%s'\n"), p);
5981 p = strchr (p, ',');
5982 if (p == NULL)
5983 break;
5984 }
5985
5986 if (*p == ',')
5987 p++;
5988 }
5989 }
5990
5991 void
5992 dwarf_select_sections_by_letters (const char *letters)
5993 {
5994 unsigned int lindex = 0;
5995
5996 while (letters[lindex])
5997 switch (letters[lindex++])
5998 {
5999 case 'i':
6000 do_debug_info = 1;
6001 break;
6002
6003 case 'a':
6004 do_debug_abbrevs = 1;
6005 break;
6006
6007 case 'l':
6008 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
6009 break;
6010
6011 case 'L':
6012 do_debug_lines |= FLAG_DEBUG_LINES_DECODED;
6013 break;
6014
6015 case 'p':
6016 do_debug_pubnames = 1;
6017 break;
6018
6019 case 't':
6020 do_debug_pubtypes = 1;
6021 break;
6022
6023 case 'r':
6024 do_debug_aranges = 1;
6025 break;
6026
6027 case 'R':
6028 do_debug_ranges = 1;
6029 break;
6030
6031 case 'F':
6032 do_debug_frames_interp = 1;
6033 case 'f':
6034 do_debug_frames = 1;
6035 break;
6036
6037 case 'm':
6038 do_debug_macinfo = 1;
6039 break;
6040
6041 case 's':
6042 do_debug_str = 1;
6043 break;
6044
6045 case 'o':
6046 do_debug_loc = 1;
6047 break;
6048
6049 default:
6050 warn (_("Unrecognized debug option '%s'\n"), optarg);
6051 break;
6052 }
6053 }
6054
6055 void
6056 dwarf_select_sections_all (void)
6057 {
6058 do_debug_info = 1;
6059 do_debug_abbrevs = 1;
6060 do_debug_lines = FLAG_DEBUG_LINES_RAW;
6061 do_debug_pubnames = 1;
6062 do_debug_pubtypes = 1;
6063 do_debug_aranges = 1;
6064 do_debug_ranges = 1;
6065 do_debug_frames = 1;
6066 do_debug_macinfo = 1;
6067 do_debug_str = 1;
6068 do_debug_loc = 1;
6069 do_gdb_index = 1;
6070 do_trace_info = 1;
6071 do_trace_abbrevs = 1;
6072 do_trace_aranges = 1;
6073 }
6074
6075 struct dwarf_section_display debug_displays[] =
6076 {
6077 { { ".debug_abbrev", ".zdebug_abbrev", NULL, NULL, 0, 0, abbrev },
6078 display_debug_abbrev, &do_debug_abbrevs, 0 },
6079 { { ".debug_aranges", ".zdebug_aranges", NULL, NULL, 0, 0, abbrev },
6080 display_debug_aranges, &do_debug_aranges, 1 },
6081 { { ".debug_frame", ".zdebug_frame", NULL, NULL, 0, 0, abbrev },
6082 display_debug_frames, &do_debug_frames, 1 },
6083 { { ".debug_info", ".zdebug_info", NULL, NULL, 0, 0, abbrev },
6084 display_debug_info, &do_debug_info, 1 },
6085 { { ".debug_line", ".zdebug_line", NULL, NULL, 0, 0, abbrev },
6086 display_debug_lines, &do_debug_lines, 1 },
6087 { { ".debug_pubnames", ".zdebug_pubnames", NULL, NULL, 0, 0, abbrev },
6088 display_debug_pubnames, &do_debug_pubnames, 0 },
6089 { { ".eh_frame", "", NULL, NULL, 0, 0, abbrev },
6090 display_debug_frames, &do_debug_frames, 1 },
6091 { { ".debug_macinfo", ".zdebug_macinfo", NULL, NULL, 0, 0, abbrev },
6092 display_debug_macinfo, &do_debug_macinfo, 0 },
6093 { { ".debug_macro", ".zdebug_macro", NULL, NULL, 0, 0, abbrev },
6094 display_debug_macro, &do_debug_macinfo, 1 },
6095 { { ".debug_str", ".zdebug_str", NULL, NULL, 0, 0, abbrev },
6096 display_debug_str, &do_debug_str, 0 },
6097 { { ".debug_loc", ".zdebug_loc", NULL, NULL, 0, 0, abbrev },
6098 display_debug_loc, &do_debug_loc, 1 },
6099 { { ".debug_pubtypes", ".zdebug_pubtypes", NULL, NULL, 0, 0, abbrev },
6100 display_debug_pubnames, &do_debug_pubtypes, 0 },
6101 { { ".debug_ranges", ".zdebug_ranges", NULL, NULL, 0, 0, abbrev },
6102 display_debug_ranges, &do_debug_ranges, 1 },
6103 { { ".debug_static_func", ".zdebug_static_func", NULL, NULL, 0, 0, abbrev },
6104 display_debug_not_supported, NULL, 0 },
6105 { { ".debug_static_vars", ".zdebug_static_vars", NULL, NULL, 0, 0, abbrev },
6106 display_debug_not_supported, NULL, 0 },
6107 { { ".debug_types", ".zdebug_types", NULL, NULL, 0, 0, abbrev },
6108 display_debug_types, &do_debug_info, 1 },
6109 { { ".debug_weaknames", ".zdebug_weaknames", NULL, NULL, 0, 0, abbrev },
6110 display_debug_not_supported, NULL, 0 },
6111 { { ".gdb_index", "", NULL, NULL, 0, 0, abbrev },
6112 display_gdb_index, &do_gdb_index, 0 },
6113 { { ".trace_info", "", NULL, NULL, 0, 0, trace_abbrev },
6114 display_trace_info, &do_trace_info, 1 },
6115 { { ".trace_abbrev", "", NULL, NULL, 0, 0, abbrev },
6116 display_debug_abbrev, &do_trace_abbrevs, 0 },
6117 { { ".trace_aranges", "", NULL, NULL, 0, 0, abbrev },
6118 display_debug_aranges, &do_trace_aranges, 0 },
6119 { { ".debug_info.dwo", ".zdebug_info.dwo", NULL, NULL, 0, 0, abbrev_dwo },
6120 display_debug_info, &do_debug_info, 1 },
6121 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", NULL, NULL, 0, 0, abbrev_dwo },
6122 display_debug_abbrev, &do_debug_abbrevs, 0 },
6123 { { ".debug_types.dwo", ".zdebug_types.dwo", NULL, NULL, 0, 0, abbrev_dwo },
6124 display_debug_types, &do_debug_info, 1 },
6125 { { ".debug_line.dwo", ".zdebug_line.dwo", NULL, NULL, 0, 0, abbrev_dwo },
6126 display_debug_lines, &do_debug_lines, 1 },
6127 { { ".debug_loc.dwo", ".zdebug_loc.dwo", NULL, NULL, 0, 0, abbrev_dwo },
6128 display_debug_loc, &do_debug_loc, 1 },
6129 { { ".debug_macro.dwo", ".zdebug_macro.dwo",NULL, NULL, 0, 0, abbrev },
6130 display_debug_macro, &do_debug_macinfo, 1 },
6131 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo",NULL, NULL, 0, 0, abbrev },
6132 display_debug_macinfo, &do_debug_macinfo, 0 },
6133 { { ".debug_str.dwo", ".zdebug_str.dwo", NULL, NULL, 0, 0, str_dwo },
6134 display_debug_str, &do_debug_str, 1 },
6135 { { ".debug_str_offsets",".zdebug_str_offsets", NULL, NULL, 0, 0, abbrev },
6136 display_debug_str_offsets, NULL, 0 },
6137 { { ".debug_str_offsets.dwo",".zdebug_str_offsets.dwo", NULL, NULL, 0, 0,
6138 abbrev },
6139 display_debug_str_offsets, NULL, 0 },
6140 { { ".debug_addr",".zdebug_addr", NULL, NULL, 0, 0, debug_addr },
6141 display_debug_addr, NULL, 1 },
6142 };
This page took 0.181642 seconds and 4 git commands to generate.