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