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