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