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