Fix: ld: Test case pr28158 fails on x86_64-linux-musl when index is > 19
[binutils-gdb.git] / binutils / dwarf.c
blob7a350cae50b21d8095ea2522c4ed73a26fb8b35e
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2 Copyright (C) 2005-2023 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
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.
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.
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. */
21 #include "sysdep.h"
22 #include "libiberty.h"
23 #include "bfd.h"
24 #include <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 #include "filenames.h"
32 #include "safe-ctype.h"
33 #include <assert.h>
35 #ifdef HAVE_LIBDEBUGINFOD
36 #include <elfutils/debuginfod.h>
37 #endif
39 #include <limits.h>
40 #ifndef CHAR_BIT
41 #define CHAR_BIT 8
42 #endif
44 #ifndef ENABLE_CHECKING
45 #define ENABLE_CHECKING 0
46 #endif
48 #undef MAX
49 #undef MIN
50 #define MAX(a, b) ((a) > (b) ? (a) : (b))
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
53 static const char *regname (unsigned int regno, int row);
54 static const char *regname_internal_by_table_only (unsigned int regno);
56 static int have_frame_base;
57 static int need_base_address;
59 static unsigned int num_debug_info_entries = 0;
60 static unsigned int alloc_num_debug_info_entries = 0;
61 static debug_info *debug_information = NULL;
62 /* Special value for num_debug_info_entries to indicate
63 that the .debug_info section could not be loaded/parsed. */
64 #define DEBUG_INFO_UNAVAILABLE (unsigned int) -1
66 /* A .debug_info section can contain multiple links to separate
67 DWO object files. We use these structures to record these links. */
68 typedef enum dwo_type
70 DWO_NAME,
71 DWO_DIR,
72 DWO_ID
73 } dwo_type;
75 typedef struct dwo_info
77 dwo_type type;
78 const char * value;
79 uint64_t cu_offset;
80 struct dwo_info * next;
81 } dwo_info;
83 static dwo_info *first_dwo_info = NULL;
84 static bool need_dwo_info;
86 separate_info * first_separate_info = NULL;
88 unsigned int eh_addr_size;
90 int do_debug_info;
91 int do_debug_abbrevs;
92 int do_debug_lines;
93 int do_debug_pubnames;
94 int do_debug_pubtypes;
95 int do_debug_aranges;
96 int do_debug_ranges;
97 int do_debug_frames;
98 int do_debug_frames_interp;
99 int do_debug_macinfo;
100 int do_debug_str;
101 int do_debug_str_offsets;
102 int do_debug_loc;
103 int do_gdb_index;
104 int do_trace_info;
105 int do_trace_abbrevs;
106 int do_trace_aranges;
107 int do_debug_addr;
108 int do_debug_cu_index;
109 int do_wide;
110 int do_debug_links;
111 int do_follow_links = DEFAULT_FOR_FOLLOW_LINKS;
112 #ifdef HAVE_LIBDEBUGINFOD
113 int use_debuginfod = 1;
114 #endif
115 bool do_checks;
117 int dwarf_cutoff_level = -1;
118 unsigned long dwarf_start_die;
120 int dwarf_check = 0;
122 /* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
123 sections. For version 1 package files, each set is stored in SHNDX_POOL
124 as a zero-terminated list of section indexes comprising one set of debug
125 sections from a .dwo file. */
127 static unsigned int *shndx_pool = NULL;
128 static unsigned int shndx_pool_size = 0;
129 static unsigned int shndx_pool_used = 0;
131 /* For version 2 package files, each set contains an array of section offsets
132 and an array of section sizes, giving the offset and size of the
133 contribution from a CU or TU within one of the debug sections.
134 When displaying debug info from a package file, we need to use these
135 tables to locate the corresponding contributions to each section. */
137 struct cu_tu_set
139 uint64_t signature;
140 uint64_t section_offsets[DW_SECT_MAX];
141 size_t section_sizes[DW_SECT_MAX];
144 static int cu_count = 0;
145 static int tu_count = 0;
146 static struct cu_tu_set *cu_sets = NULL;
147 static struct cu_tu_set *tu_sets = NULL;
149 static bool load_cu_tu_indexes (void *);
151 /* An array that indicates for a given level of CU nesting whether
152 the latest DW_AT_type seen for that level was a signed type or
153 an unsigned type. */
154 #define MAX_CU_NESTING (1 << 8)
155 static bool level_type_signed[MAX_CU_NESTING];
157 /* Values for do_debug_lines. */
158 #define FLAG_DEBUG_LINES_RAW 1
159 #define FLAG_DEBUG_LINES_DECODED 2
161 static unsigned int
162 size_of_encoded_value (int encoding)
164 switch (encoding & 0x7)
166 default: /* ??? */
167 case 0: return eh_addr_size;
168 case 2: return 2;
169 case 3: return 4;
170 case 4: return 8;
174 static uint64_t
175 get_encoded_value (unsigned char **pdata,
176 int encoding,
177 struct dwarf_section *section,
178 unsigned char * end)
180 unsigned char * data = * pdata;
181 unsigned int size = size_of_encoded_value (encoding);
182 uint64_t val;
184 if (data >= end || size > (size_t) (end - data))
186 warn (_("Encoded value extends past end of section\n"));
187 * pdata = end;
188 return 0;
191 /* PR 17512: file: 002-829853-0.004. */
192 if (size > 8)
194 warn (_("Encoded size of %d is too large to read\n"), size);
195 * pdata = end;
196 return 0;
199 /* PR 17512: file: 1085-5603-0.004. */
200 if (size == 0)
202 warn (_("Encoded size of 0 is too small to read\n"));
203 * pdata = end;
204 return 0;
207 if (encoding & DW_EH_PE_signed)
208 val = byte_get_signed (data, size);
209 else
210 val = byte_get (data, size);
212 if ((encoding & 0x70) == DW_EH_PE_pcrel)
213 val += section->address + (data - section->start);
215 * pdata = data + size;
216 return val;
219 /* Print a uint64_t value (typically an address, offset or length) in
220 hexadecimal format, followed by a space. The precision displayed is
221 determined by the NUM_BYTES parameter. */
223 static void
224 print_hex (uint64_t value, unsigned num_bytes)
226 if (num_bytes == 0)
227 num_bytes = 2;
229 printf ("%0*" PRIx64 " ", num_bytes * 2,
230 value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
233 /* Like print_hex, but no trailing space. */
235 static void
236 print_hex_ns (uint64_t value, unsigned num_bytes)
238 if (num_bytes == 0)
239 num_bytes = 2;
241 printf ("%0*" PRIx64, num_bytes * 2,
242 value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
245 /* Print a view number in hexadecimal value, with the same width as
246 print_hex would have printed it. */
248 static void
249 print_view (uint64_t value, unsigned num_bytes)
251 if (num_bytes == 0)
252 num_bytes = 2;
254 printf ("v%0*" PRIx64 " ", num_bytes * 2 - 1,
255 value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4));
258 static const char *
259 null_name (const char *p)
261 if (p == NULL)
262 p = _("unknown");
263 return p;
266 /* Read in a LEB128 encoded value starting at address DATA.
267 If SIGN is true, return a signed LEB128 value.
268 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
269 If STATUS_RETURN is not NULL, return with bit 0 (LSB) set if the
270 terminating byte was not found and with bit 1 set if the value
271 overflows a uint64_t.
272 No bytes will be read at address END or beyond. */
274 uint64_t
275 read_leb128 (unsigned char *data,
276 const unsigned char *const end,
277 bool sign,
278 unsigned int *length_return,
279 int *status_return)
281 uint64_t result = 0;
282 unsigned int num_read = 0;
283 unsigned int shift = 0;
284 int status = 1;
286 while (data < end)
288 unsigned char byte = *data++;
289 unsigned char lost, mask;
291 num_read++;
293 if (shift < CHAR_BIT * sizeof (result))
295 result |= ((uint64_t) (byte & 0x7f)) << shift;
296 /* These bits overflowed. */
297 lost = byte ^ (result >> shift);
298 /* And this is the mask of possible overflow bits. */
299 mask = 0x7f ^ ((uint64_t) 0x7f << shift >> shift);
300 shift += 7;
302 else
304 lost = byte;
305 mask = 0x7f;
307 if ((lost & mask) != (sign && (int64_t) result < 0 ? mask : 0))
308 status |= 2;
310 if ((byte & 0x80) == 0)
312 status &= ~1;
313 if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
314 result |= -((uint64_t) 1 << shift);
315 break;
319 if (length_return != NULL)
320 *length_return = num_read;
321 if (status_return != NULL)
322 *status_return = status;
324 return result;
327 /* Read AMOUNT bytes from PTR and store them in VAL.
328 Checks to make sure that the read will not reach or pass END.
329 FUNC chooses whether the value read is unsigned or signed, and may
330 be either byte_get or byte_get_signed. If INC is true, PTR is
331 incremented after reading the value.
332 This macro cannot protect against PTR values derived from user input.
333 The C standard sections 6.5.6 and 6.5.8 say attempts to do so using
334 pointers is undefined behaviour. */
335 #define SAFE_BYTE_GET_INTERNAL(VAL, PTR, AMOUNT, END, FUNC, INC) \
336 do \
338 size_t amount = (AMOUNT); \
339 if (sizeof (VAL) < amount) \
341 error (ngettext ("internal error: attempt to read %d byte " \
342 "of data in to %d sized variable", \
343 "internal error: attempt to read %d bytes " \
344 "of data in to %d sized variable", \
345 amount), \
346 (int) amount, (int) sizeof (VAL)); \
347 amount = sizeof (VAL); \
349 if (ENABLE_CHECKING) \
350 assert ((PTR) <= (END)); \
351 size_t avail = (END) - (PTR); \
352 if ((PTR) > (END)) \
353 avail = 0; \
354 if (amount > avail) \
355 amount = avail; \
356 if (amount == 0) \
357 (VAL) = 0; \
358 else \
359 (VAL) = (FUNC) ((PTR), amount); \
360 if (INC) \
361 (PTR) += amount; \
363 while (0)
365 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END) \
366 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, false)
368 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
369 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, true)
371 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END) \
372 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, false)
374 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
375 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, true)
377 typedef struct State_Machine_Registers
379 uint64_t address;
380 unsigned int view;
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;
393 static SMR state_machine_regs;
395 static void
396 reset_state_machine (int is_stmt)
398 state_machine_regs.address = 0;
399 state_machine_regs.view = 0;
400 state_machine_regs.op_index = 0;
401 state_machine_regs.file = 1;
402 state_machine_regs.line = 1;
403 state_machine_regs.column = 0;
404 state_machine_regs.is_stmt = is_stmt;
405 state_machine_regs.basic_block = 0;
406 state_machine_regs.end_sequence = 0;
407 state_machine_regs.last_file_entry = 0;
410 /* Handled an extend line op.
411 Returns the number of bytes read. */
413 static size_t
414 process_extended_line_op (unsigned char * data,
415 int is_stmt,
416 unsigned char * end)
418 unsigned char op_code;
419 size_t len, header_len;
420 unsigned char *name;
421 unsigned char *orig_data = data;
422 uint64_t adr, val;
424 READ_ULEB (len, data, end);
425 header_len = data - orig_data;
427 if (len == 0 || data >= end || len > (size_t) (end - data))
429 warn (_("Badly formed extended line op encountered!\n"));
430 return header_len;
433 op_code = *data++;
435 printf (_(" Extended opcode %d: "), op_code);
437 switch (op_code)
439 case DW_LNE_end_sequence:
440 printf (_("End of Sequence\n\n"));
441 reset_state_machine (is_stmt);
442 break;
444 case DW_LNE_set_address:
445 /* PR 17512: file: 002-100480-0.004. */
446 if (len - 1 > 8)
448 warn (_("Length (%zu) of DW_LNE_set_address op is too long\n"),
449 len - 1);
450 adr = 0;
452 else
453 SAFE_BYTE_GET (adr, data, len - 1, end);
454 printf (_("set Address to %#" PRIx64 "\n"), adr);
455 state_machine_regs.address = adr;
456 state_machine_regs.view = 0;
457 state_machine_regs.op_index = 0;
458 break;
460 case DW_LNE_define_file:
461 printf (_("define new File Table entry\n"));
462 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
463 printf (" %d\t", ++state_machine_regs.last_file_entry);
466 size_t l;
468 name = data;
469 l = strnlen ((char *) data, end - data);
470 data += l;
471 if (data < end)
472 data++;
473 READ_ULEB (val, data, end);
474 printf ("%" PRIu64 "\t", val);
475 READ_ULEB (val, data, end);
476 printf ("%" PRIu64 "\t", val);
477 READ_ULEB (val, data, end);
478 printf ("%" PRIu64 "\t", val);
479 printf ("%.*s\n\n", (int) l, name);
482 if (((size_t) (data - orig_data) != len + header_len) || data >= end)
483 warn (_("DW_LNE_define_file: Bad opcode length\n"));
484 break;
486 case DW_LNE_set_discriminator:
487 READ_ULEB (val, data, end);
488 printf (_("set Discriminator to %" PRIu64 "\n"), val);
489 break;
491 /* HP extensions. */
492 case DW_LNE_HP_negate_is_UV_update:
493 printf ("DW_LNE_HP_negate_is_UV_update\n");
494 break;
495 case DW_LNE_HP_push_context:
496 printf ("DW_LNE_HP_push_context\n");
497 break;
498 case DW_LNE_HP_pop_context:
499 printf ("DW_LNE_HP_pop_context\n");
500 break;
501 case DW_LNE_HP_set_file_line_column:
502 printf ("DW_LNE_HP_set_file_line_column\n");
503 break;
504 case DW_LNE_HP_set_routine_name:
505 printf ("DW_LNE_HP_set_routine_name\n");
506 break;
507 case DW_LNE_HP_set_sequence:
508 printf ("DW_LNE_HP_set_sequence\n");
509 break;
510 case DW_LNE_HP_negate_post_semantics:
511 printf ("DW_LNE_HP_negate_post_semantics\n");
512 break;
513 case DW_LNE_HP_negate_function_exit:
514 printf ("DW_LNE_HP_negate_function_exit\n");
515 break;
516 case DW_LNE_HP_negate_front_end_logical:
517 printf ("DW_LNE_HP_negate_front_end_logical\n");
518 break;
519 case DW_LNE_HP_define_proc:
520 printf ("DW_LNE_HP_define_proc\n");
521 break;
522 case DW_LNE_HP_source_file_correlation:
524 unsigned char *edata = data + len - 1;
526 printf ("DW_LNE_HP_source_file_correlation\n");
528 while (data < edata)
530 unsigned int opc;
532 READ_ULEB (opc, data, edata);
534 switch (opc)
536 case DW_LNE_HP_SFC_formfeed:
537 printf (" DW_LNE_HP_SFC_formfeed\n");
538 break;
539 case DW_LNE_HP_SFC_set_listing_line:
540 READ_ULEB (val, data, edata);
541 printf (" DW_LNE_HP_SFC_set_listing_line (%" PRIu64 ")\n",
542 val);
543 break;
544 case DW_LNE_HP_SFC_associate:
545 printf (" DW_LNE_HP_SFC_associate ");
546 READ_ULEB (val, data, edata);
547 printf ("(%" PRIu64 , val);
548 READ_ULEB (val, data, edata);
549 printf (",%" PRIu64, val);
550 READ_ULEB (val, data, edata);
551 printf (",%" PRIu64 ")\n", val);
552 break;
553 default:
554 printf (_(" UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
555 data = edata;
556 break;
560 break;
562 default:
564 unsigned int rlen = len - 1;
566 if (op_code >= DW_LNE_lo_user
567 /* The test against DW_LNW_hi_user is redundant due to
568 the limited range of the unsigned char data type used
569 for op_code. */
570 /*&& op_code <= DW_LNE_hi_user*/)
571 printf (_("user defined: "));
572 else
573 printf (_("UNKNOWN: "));
574 printf (_("length %d ["), rlen);
575 for (; rlen; rlen--)
576 printf (" %02x", *data++);
577 printf ("]\n");
579 break;
582 return len + header_len;
585 static const unsigned char *
586 fetch_indirect_string (uint64_t offset)
588 struct dwarf_section *section = &debug_displays [str].section;
589 const unsigned char * ret;
591 if (section->start == NULL)
592 return (const unsigned char *) _("<no .debug_str section>");
594 if (offset >= section->size)
596 warn (_("DW_FORM_strp offset too big: %#" PRIx64 "\n"), offset);
597 return (const unsigned char *) _("<offset is too big>");
600 ret = section->start + offset;
601 /* Unfortunately we cannot rely upon the .debug_str section ending with a
602 NUL byte. Since our caller is expecting to receive a well formed C
603 string we test for the lack of a terminating byte here. */
604 if (strnlen ((const char *) ret, section->size - offset)
605 == section->size - offset)
606 ret = (const unsigned char *)
607 _("<no NUL byte at end of .debug_str section>");
609 return ret;
612 static const unsigned char *
613 fetch_indirect_line_string (uint64_t offset)
615 struct dwarf_section *section = &debug_displays [line_str].section;
616 const unsigned char * ret;
618 if (section->start == NULL)
619 return (const unsigned char *) _("<no .debug_line_str section>");
621 if (offset >= section->size)
623 warn (_("DW_FORM_line_strp offset too big: %#" PRIx64 "\n"), offset);
624 return (const unsigned char *) _("<offset is too big>");
627 ret = section->start + offset;
628 /* Unfortunately we cannot rely upon the .debug_line_str section ending
629 with a NUL byte. Since our caller is expecting to receive a well formed
630 C string we test for the lack of a terminating byte here. */
631 if (strnlen ((const char *) ret, section->size - offset)
632 == section->size - offset)
633 ret = (const unsigned char *)
634 _("<no NUL byte at end of .debug_line_str section>");
636 return ret;
639 static const char *
640 fetch_indexed_string (uint64_t idx,
641 struct cu_tu_set *this_set,
642 uint64_t offset_size,
643 bool dwo,
644 uint64_t str_offsets_base)
646 enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
647 enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
648 struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
649 struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
650 uint64_t index_offset;
651 uint64_t str_offset;
652 const char * ret;
654 if (index_section->start == NULL)
655 return (dwo ? _("<no .debug_str_offsets.dwo section>")
656 : _("<no .debug_str_offsets section>"));
658 if (str_section->start == NULL)
659 return (dwo ? _("<no .debug_str.dwo section>")
660 : _("<no .debug_str section>"));
662 if (_mul_overflow (idx, offset_size, &index_offset)
663 || (this_set != NULL
664 && ((index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS])
665 < this_set->section_offsets [DW_SECT_STR_OFFSETS]))
666 || (index_offset += str_offsets_base) < str_offsets_base
667 || index_offset + offset_size < offset_size
668 || index_offset + offset_size > index_section->size)
670 warn (_("string index of %" PRIu64 " converts to an offset of %#" PRIx64
671 " which is too big for section %s"),
672 idx, index_offset, str_section->name);
674 return _("<string index too big>");
677 str_offset = byte_get (index_section->start + index_offset, offset_size);
679 str_offset -= str_section->address;
680 if (str_offset >= str_section->size)
682 warn (_("indirect offset too big: %#" PRIx64 "\n"), str_offset);
683 return _("<indirect index offset is too big>");
686 ret = (const char *) str_section->start + str_offset;
688 /* Unfortunately we cannot rely upon str_section ending with a NUL byte.
689 Since our caller is expecting to receive a well formed C string we test
690 for the lack of a terminating byte here. */
691 if (strnlen (ret, str_section->size - str_offset)
692 == str_section->size - str_offset)
693 return _("<no NUL byte at end of section>");
695 return ret;
698 static uint64_t
699 fetch_indexed_addr (uint64_t offset, uint32_t num_bytes)
701 struct dwarf_section *section = &debug_displays [debug_addr].section;
703 if (section->start == NULL)
705 warn (_("Cannot fetch indexed address: the .debug_addr section is missing\n"));
706 return 0;
709 if (offset + num_bytes > section->size)
711 warn (_("Offset into section %s too big: %#" PRIx64 "\n"),
712 section->name, offset);
713 return 0;
716 return byte_get (section->start + offset, num_bytes);
719 /* This is for resolving DW_FORM_rnglistx and DW_FORM_loclistx.
721 The memory layout is: base_address (taken from the CU's top DIE) points at a table of offsets,
722 relative to the section start.
723 The table of offsets contains the offsets of objects of interest relative to the table of offsets.
724 IDX is the index of the desired object in said table of offsets.
726 This returns the offset of the desired object relative to the section start or -1 upon failure. */
728 static uint64_t
729 fetch_indexed_offset (uint64_t idx,
730 enum dwarf_section_display_enum sec_enum,
731 uint64_t base_address,
732 uint64_t offset_size)
734 uint64_t offset_of_offset = base_address + idx * offset_size;
735 struct dwarf_section *section = &debug_displays [sec_enum].section;
737 if (section->start == NULL)
739 warn (_("Unable to locate %s section\n"), section->uncompressed_name);
740 return -1;
743 if (section->size < 4)
745 warn (_("Section %s is too small to contain an value indexed from another section!\n"),
746 section->name);
747 return -1;
750 if (offset_of_offset + offset_size >= section->size)
752 warn (_("Offset of %#" PRIx64 " is too big for section %s\n"),
753 offset_of_offset, section->name);
754 return -1;
757 return base_address + byte_get (section->start + offset_of_offset, offset_size);
760 /* FIXME: There are better and more efficient ways to handle
761 these structures. For now though, I just want something that
762 is simple to implement. */
763 /* Records a single attribute in an abbrev. */
764 typedef struct abbrev_attr
766 unsigned long attribute;
767 unsigned long form;
768 int64_t implicit_const;
769 struct abbrev_attr *next;
771 abbrev_attr;
773 /* Records a single abbrev. */
774 typedef struct abbrev_entry
776 unsigned long number;
777 unsigned long tag;
778 int children;
779 struct abbrev_attr * first_attr;
780 struct abbrev_attr * last_attr;
781 struct abbrev_entry * next;
783 abbrev_entry;
785 /* Records a set of abbreviations. */
786 typedef struct abbrev_list
788 abbrev_entry * first_abbrev;
789 abbrev_entry * last_abbrev;
790 unsigned char * raw;
791 struct abbrev_list * next;
792 unsigned char * start_of_next_abbrevs;
794 abbrev_list;
796 /* Records all the abbrevs found so far. */
797 static struct abbrev_list * abbrev_lists = NULL;
799 typedef struct abbrev_map
801 uint64_t start;
802 uint64_t end;
803 abbrev_list *list;
804 } abbrev_map;
806 /* Maps between CU offsets and abbrev sets. */
807 static abbrev_map * cu_abbrev_map = NULL;
808 static unsigned long num_abbrev_map_entries = 0;
809 static unsigned long next_free_abbrev_map_entry = 0;
811 #define INITIAL_NUM_ABBREV_MAP_ENTRIES 8
812 #define ABBREV_MAP_ENTRIES_INCREMENT 8
814 static void
815 record_abbrev_list_for_cu (uint64_t start, uint64_t end,
816 abbrev_list *list, abbrev_list *free_list)
818 if (free_list != NULL)
820 list->next = abbrev_lists;
821 abbrev_lists = list;
824 if (cu_abbrev_map == NULL)
826 num_abbrev_map_entries = INITIAL_NUM_ABBREV_MAP_ENTRIES;
827 cu_abbrev_map = xmalloc (num_abbrev_map_entries * sizeof (* cu_abbrev_map));
829 else if (next_free_abbrev_map_entry == num_abbrev_map_entries)
831 num_abbrev_map_entries += ABBREV_MAP_ENTRIES_INCREMENT;
832 cu_abbrev_map = xrealloc (cu_abbrev_map, num_abbrev_map_entries * sizeof (* cu_abbrev_map));
835 cu_abbrev_map[next_free_abbrev_map_entry].start = start;
836 cu_abbrev_map[next_free_abbrev_map_entry].end = end;
837 cu_abbrev_map[next_free_abbrev_map_entry].list = list;
838 next_free_abbrev_map_entry ++;
841 static abbrev_list *
842 free_abbrev_list (abbrev_list *list)
844 abbrev_entry *abbrv = list->first_abbrev;
846 while (abbrv)
848 abbrev_attr *attr = abbrv->first_attr;
850 while (attr)
852 abbrev_attr *next_attr = attr->next;
853 free (attr);
854 attr = next_attr;
857 abbrev_entry *next_abbrev = abbrv->next;
858 free (abbrv);
859 abbrv = next_abbrev;
862 abbrev_list *next = list->next;
863 free (list);
864 return next;
867 static void
868 free_all_abbrevs (void)
870 while (abbrev_lists)
871 abbrev_lists = free_abbrev_list (abbrev_lists);
873 free (cu_abbrev_map);
874 cu_abbrev_map = NULL;
875 next_free_abbrev_map_entry = 0;
878 static abbrev_list *
879 find_abbrev_list_by_raw_abbrev (unsigned char *raw)
881 abbrev_list * list;
883 for (list = abbrev_lists; list != NULL; list = list->next)
884 if (list->raw == raw)
885 return list;
887 return NULL;
890 /* Find the abbreviation map for the CU that includes OFFSET.
891 OFFSET is an absolute offset from the start of the .debug_info section. */
892 /* FIXME: This function is going to slow down readelf & objdump.
893 Not caching abbrevs is likely the answer. */
895 static abbrev_map *
896 find_abbrev_map_by_offset (uint64_t offset)
898 unsigned long i;
900 for (i = 0; i < next_free_abbrev_map_entry; i++)
901 if (cu_abbrev_map[i].start <= offset
902 && cu_abbrev_map[i].end > offset)
903 return cu_abbrev_map + i;
905 return NULL;
908 static void
909 add_abbrev (unsigned long number,
910 unsigned long tag,
911 int children,
912 abbrev_list * list)
914 abbrev_entry * entry;
916 entry = (abbrev_entry *) xmalloc (sizeof (*entry));
918 entry->number = number;
919 entry->tag = tag;
920 entry->children = children;
921 entry->first_attr = NULL;
922 entry->last_attr = NULL;
923 entry->next = NULL;
925 assert (list != NULL);
927 if (list->first_abbrev == NULL)
928 list->first_abbrev = entry;
929 else
930 list->last_abbrev->next = entry;
932 list->last_abbrev = entry;
935 static void
936 add_abbrev_attr (unsigned long attribute,
937 unsigned long form,
938 int64_t implicit_const,
939 abbrev_list *list)
941 abbrev_attr *attr;
943 attr = (abbrev_attr *) xmalloc (sizeof (*attr));
945 attr->attribute = attribute;
946 attr->form = form;
947 attr->implicit_const = implicit_const;
948 attr->next = NULL;
950 assert (list != NULL && list->last_abbrev != NULL);
952 if (list->last_abbrev->first_attr == NULL)
953 list->last_abbrev->first_attr = attr;
954 else
955 list->last_abbrev->last_attr->next = attr;
957 list->last_abbrev->last_attr = attr;
960 /* Return processed (partial) contents of a .debug_abbrev section.
961 Returns NULL on errors. */
963 static abbrev_list *
964 process_abbrev_set (struct dwarf_section *section,
965 unsigned char *start,
966 unsigned char *end)
968 abbrev_list *list = xmalloc (sizeof (*list));
969 list->first_abbrev = NULL;
970 list->last_abbrev = NULL;
971 list->raw = start;
972 list->next = NULL;
974 while (start < end)
976 unsigned long entry;
977 unsigned long tag;
978 unsigned long attribute;
979 int children;
981 READ_ULEB (entry, start, end);
983 /* A single zero is supposed to end the set according
984 to the standard. If there's more, then signal that to
985 the caller. */
986 if (start == end || entry == 0)
988 list->start_of_next_abbrevs = start != end ? start : NULL;
989 return list;
992 READ_ULEB (tag, start, end);
993 if (start == end)
994 return free_abbrev_list (list);
996 children = *start++;
998 add_abbrev (entry, tag, children, list);
1002 unsigned long form;
1003 /* Initialize it due to a false compiler warning. */
1004 int64_t implicit_const = -1;
1006 READ_ULEB (attribute, start, end);
1007 if (start == end)
1008 break;
1010 READ_ULEB (form, start, end);
1011 if (start == end)
1012 break;
1014 if (form == DW_FORM_implicit_const)
1016 READ_SLEB (implicit_const, start, end);
1017 if (start == end)
1018 break;
1021 add_abbrev_attr (attribute, form, implicit_const, list);
1023 while (attribute != 0);
1026 /* Report the missing single zero which ends the section. */
1027 error (_("%s section not zero terminated\n"), section->name);
1029 return free_abbrev_list (list);
1032 /* Return a sequence of abbrevs in SECTION starting at ABBREV_BASE
1033 plus ABBREV_OFFSET and finishing at ABBREV_BASE + ABBREV_SIZE.
1034 If FREE_LIST is non-NULL search the already decoded abbrevs on
1035 abbrev_lists first and if found set *FREE_LIST to NULL. If
1036 searching doesn't find a matching abbrev, set *FREE_LIST to the
1037 newly allocated list. If FREE_LIST is NULL, no search is done and
1038 the returned abbrev_list is always newly allocated. */
1040 static abbrev_list *
1041 find_and_process_abbrev_set (struct dwarf_section *section,
1042 uint64_t abbrev_base,
1043 uint64_t abbrev_size,
1044 uint64_t abbrev_offset,
1045 abbrev_list **free_list)
1047 if (free_list)
1048 *free_list = NULL;
1050 if (abbrev_base >= section->size
1051 || abbrev_size > section->size - abbrev_base)
1053 /* PR 17531: file:4bcd9ce9. */
1054 warn (_("Debug info is corrupted, abbrev size (%#" PRIx64 ")"
1055 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1056 abbrev_base + abbrev_size, section->size);
1057 return NULL;
1059 if (abbrev_offset >= abbrev_size)
1061 warn (_("Debug info is corrupted, abbrev offset (%#" PRIx64 ")"
1062 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1063 abbrev_offset, abbrev_size);
1064 return NULL;
1067 unsigned char *start = section->start + abbrev_base + abbrev_offset;
1068 unsigned char *end = section->start + abbrev_base + abbrev_size;
1069 abbrev_list *list = NULL;
1070 if (free_list)
1071 list = find_abbrev_list_by_raw_abbrev (start);
1072 if (list == NULL)
1074 list = process_abbrev_set (section, start, end);
1075 if (free_list)
1076 *free_list = list;
1078 return list;
1081 static const char *
1082 get_TAG_name (uint64_t tag)
1084 const char *name = NULL;
1086 if ((unsigned int) tag == tag)
1087 name = get_DW_TAG_name ((unsigned int) tag);
1088 if (name == NULL)
1090 static char buffer[100];
1092 if (tag >= DW_TAG_lo_user && tag <= DW_TAG_hi_user)
1093 snprintf (buffer, sizeof (buffer),
1094 _("User TAG value: %#" PRIx64), tag);
1095 else
1096 snprintf (buffer, sizeof (buffer),
1097 _("Unknown TAG value: %#" PRIx64), tag);
1098 return buffer;
1101 return name;
1104 static const char *
1105 get_FORM_name (unsigned long form)
1107 const char *name = NULL;
1109 if (form == 0)
1110 return "DW_FORM value: 0";
1112 if ((unsigned int) form == form)
1113 name = get_DW_FORM_name ((unsigned int) form);
1114 if (name == NULL)
1116 static char buffer[100];
1118 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
1119 return buffer;
1122 return name;
1125 static const char *
1126 get_IDX_name (unsigned long idx)
1128 const char *name = NULL;
1130 if ((unsigned int) idx == idx)
1131 name = get_DW_IDX_name ((unsigned int) idx);
1132 if (name == NULL)
1134 static char buffer[100];
1136 snprintf (buffer, sizeof (buffer), _("Unknown IDX value: %lx"), idx);
1137 return buffer;
1140 return name;
1143 static unsigned char *
1144 display_block (unsigned char *data,
1145 uint64_t length,
1146 const unsigned char * const end, char delimiter)
1148 size_t maxlen;
1150 printf (_("%c%" PRIu64 " byte block: "), delimiter, length);
1151 if (data > end)
1152 return (unsigned char *) end;
1154 maxlen = end - data;
1155 length = length > maxlen ? maxlen : length;
1157 while (length --)
1158 printf ("%" PRIx64 " ", byte_get (data++, 1));
1160 return data;
1163 static int
1164 decode_location_expression (unsigned char * data,
1165 unsigned int pointer_size,
1166 unsigned int offset_size,
1167 int dwarf_version,
1168 uint64_t length,
1169 uint64_t cu_offset,
1170 struct dwarf_section * section)
1172 unsigned op;
1173 uint64_t uvalue;
1174 int64_t svalue;
1175 unsigned char *end = data + length;
1176 int need_frame_base = 0;
1178 while (data < end)
1180 op = *data++;
1182 switch (op)
1184 case DW_OP_addr:
1185 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1186 printf ("DW_OP_addr: %" PRIx64, uvalue);
1187 break;
1188 case DW_OP_deref:
1189 printf ("DW_OP_deref");
1190 break;
1191 case DW_OP_const1u:
1192 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1193 printf ("DW_OP_const1u: %" PRIu64, uvalue);
1194 break;
1195 case DW_OP_const1s:
1196 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
1197 printf ("DW_OP_const1s: %" PRId64, svalue);
1198 break;
1199 case DW_OP_const2u:
1200 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1201 printf ("DW_OP_const2u: %" PRIu64, uvalue);
1202 break;
1203 case DW_OP_const2s:
1204 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1205 printf ("DW_OP_const2s: %" PRId64, svalue);
1206 break;
1207 case DW_OP_const4u:
1208 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1209 printf ("DW_OP_const4u: %" PRIu64, uvalue);
1210 break;
1211 case DW_OP_const4s:
1212 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1213 printf ("DW_OP_const4s: %" PRId64, svalue);
1214 break;
1215 case DW_OP_const8u:
1216 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1217 printf ("DW_OP_const8u: %" PRIu64, uvalue);
1218 break;
1219 case DW_OP_const8s:
1220 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 8, end);
1221 printf ("DW_OP_const8s: %" PRId64, svalue);
1222 break;
1223 case DW_OP_constu:
1224 READ_ULEB (uvalue, data, end);
1225 printf ("DW_OP_constu: %" PRIu64, uvalue);
1226 break;
1227 case DW_OP_consts:
1228 READ_SLEB (svalue, data, end);
1229 printf ("DW_OP_consts: %" PRId64, svalue);
1230 break;
1231 case DW_OP_dup:
1232 printf ("DW_OP_dup");
1233 break;
1234 case DW_OP_drop:
1235 printf ("DW_OP_drop");
1236 break;
1237 case DW_OP_over:
1238 printf ("DW_OP_over");
1239 break;
1240 case DW_OP_pick:
1241 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1242 printf ("DW_OP_pick: %" PRIu64, uvalue);
1243 break;
1244 case DW_OP_swap:
1245 printf ("DW_OP_swap");
1246 break;
1247 case DW_OP_rot:
1248 printf ("DW_OP_rot");
1249 break;
1250 case DW_OP_xderef:
1251 printf ("DW_OP_xderef");
1252 break;
1253 case DW_OP_abs:
1254 printf ("DW_OP_abs");
1255 break;
1256 case DW_OP_and:
1257 printf ("DW_OP_and");
1258 break;
1259 case DW_OP_div:
1260 printf ("DW_OP_div");
1261 break;
1262 case DW_OP_minus:
1263 printf ("DW_OP_minus");
1264 break;
1265 case DW_OP_mod:
1266 printf ("DW_OP_mod");
1267 break;
1268 case DW_OP_mul:
1269 printf ("DW_OP_mul");
1270 break;
1271 case DW_OP_neg:
1272 printf ("DW_OP_neg");
1273 break;
1274 case DW_OP_not:
1275 printf ("DW_OP_not");
1276 break;
1277 case DW_OP_or:
1278 printf ("DW_OP_or");
1279 break;
1280 case DW_OP_plus:
1281 printf ("DW_OP_plus");
1282 break;
1283 case DW_OP_plus_uconst:
1284 READ_ULEB (uvalue, data, end);
1285 printf ("DW_OP_plus_uconst: %" PRIu64, uvalue);
1286 break;
1287 case DW_OP_shl:
1288 printf ("DW_OP_shl");
1289 break;
1290 case DW_OP_shr:
1291 printf ("DW_OP_shr");
1292 break;
1293 case DW_OP_shra:
1294 printf ("DW_OP_shra");
1295 break;
1296 case DW_OP_xor:
1297 printf ("DW_OP_xor");
1298 break;
1299 case DW_OP_bra:
1300 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1301 printf ("DW_OP_bra: %" PRId64, svalue);
1302 break;
1303 case DW_OP_eq:
1304 printf ("DW_OP_eq");
1305 break;
1306 case DW_OP_ge:
1307 printf ("DW_OP_ge");
1308 break;
1309 case DW_OP_gt:
1310 printf ("DW_OP_gt");
1311 break;
1312 case DW_OP_le:
1313 printf ("DW_OP_le");
1314 break;
1315 case DW_OP_lt:
1316 printf ("DW_OP_lt");
1317 break;
1318 case DW_OP_ne:
1319 printf ("DW_OP_ne");
1320 break;
1321 case DW_OP_skip:
1322 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1323 printf ("DW_OP_skip: %" PRId64, svalue);
1324 break;
1326 case DW_OP_lit0:
1327 case DW_OP_lit1:
1328 case DW_OP_lit2:
1329 case DW_OP_lit3:
1330 case DW_OP_lit4:
1331 case DW_OP_lit5:
1332 case DW_OP_lit6:
1333 case DW_OP_lit7:
1334 case DW_OP_lit8:
1335 case DW_OP_lit9:
1336 case DW_OP_lit10:
1337 case DW_OP_lit11:
1338 case DW_OP_lit12:
1339 case DW_OP_lit13:
1340 case DW_OP_lit14:
1341 case DW_OP_lit15:
1342 case DW_OP_lit16:
1343 case DW_OP_lit17:
1344 case DW_OP_lit18:
1345 case DW_OP_lit19:
1346 case DW_OP_lit20:
1347 case DW_OP_lit21:
1348 case DW_OP_lit22:
1349 case DW_OP_lit23:
1350 case DW_OP_lit24:
1351 case DW_OP_lit25:
1352 case DW_OP_lit26:
1353 case DW_OP_lit27:
1354 case DW_OP_lit28:
1355 case DW_OP_lit29:
1356 case DW_OP_lit30:
1357 case DW_OP_lit31:
1358 printf ("DW_OP_lit%d", op - DW_OP_lit0);
1359 break;
1361 case DW_OP_reg0:
1362 case DW_OP_reg1:
1363 case DW_OP_reg2:
1364 case DW_OP_reg3:
1365 case DW_OP_reg4:
1366 case DW_OP_reg5:
1367 case DW_OP_reg6:
1368 case DW_OP_reg7:
1369 case DW_OP_reg8:
1370 case DW_OP_reg9:
1371 case DW_OP_reg10:
1372 case DW_OP_reg11:
1373 case DW_OP_reg12:
1374 case DW_OP_reg13:
1375 case DW_OP_reg14:
1376 case DW_OP_reg15:
1377 case DW_OP_reg16:
1378 case DW_OP_reg17:
1379 case DW_OP_reg18:
1380 case DW_OP_reg19:
1381 case DW_OP_reg20:
1382 case DW_OP_reg21:
1383 case DW_OP_reg22:
1384 case DW_OP_reg23:
1385 case DW_OP_reg24:
1386 case DW_OP_reg25:
1387 case DW_OP_reg26:
1388 case DW_OP_reg27:
1389 case DW_OP_reg28:
1390 case DW_OP_reg29:
1391 case DW_OP_reg30:
1392 case DW_OP_reg31:
1393 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1394 regname (op - DW_OP_reg0, 1));
1395 break;
1397 case DW_OP_breg0:
1398 case DW_OP_breg1:
1399 case DW_OP_breg2:
1400 case DW_OP_breg3:
1401 case DW_OP_breg4:
1402 case DW_OP_breg5:
1403 case DW_OP_breg6:
1404 case DW_OP_breg7:
1405 case DW_OP_breg8:
1406 case DW_OP_breg9:
1407 case DW_OP_breg10:
1408 case DW_OP_breg11:
1409 case DW_OP_breg12:
1410 case DW_OP_breg13:
1411 case DW_OP_breg14:
1412 case DW_OP_breg15:
1413 case DW_OP_breg16:
1414 case DW_OP_breg17:
1415 case DW_OP_breg18:
1416 case DW_OP_breg19:
1417 case DW_OP_breg20:
1418 case DW_OP_breg21:
1419 case DW_OP_breg22:
1420 case DW_OP_breg23:
1421 case DW_OP_breg24:
1422 case DW_OP_breg25:
1423 case DW_OP_breg26:
1424 case DW_OP_breg27:
1425 case DW_OP_breg28:
1426 case DW_OP_breg29:
1427 case DW_OP_breg30:
1428 case DW_OP_breg31:
1429 READ_SLEB (svalue, data, end);
1430 printf ("DW_OP_breg%d (%s): %" PRId64,
1431 op - DW_OP_breg0, regname (op - DW_OP_breg0, 1), svalue);
1432 break;
1434 case DW_OP_regx:
1435 READ_ULEB (uvalue, data, end);
1436 printf ("DW_OP_regx: %" PRIu64 " (%s)",
1437 uvalue, regname (uvalue, 1));
1438 break;
1439 case DW_OP_fbreg:
1440 need_frame_base = 1;
1441 READ_SLEB (svalue, data, end);
1442 printf ("DW_OP_fbreg: %" PRId64, svalue);
1443 break;
1444 case DW_OP_bregx:
1445 READ_ULEB (uvalue, data, end);
1446 READ_SLEB (svalue, data, end);
1447 printf ("DW_OP_bregx: %" PRIu64 " (%s) %" PRId64,
1448 uvalue, regname (uvalue, 1), svalue);
1449 break;
1450 case DW_OP_piece:
1451 READ_ULEB (uvalue, data, end);
1452 printf ("DW_OP_piece: %" PRIu64, uvalue);
1453 break;
1454 case DW_OP_deref_size:
1455 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1456 printf ("DW_OP_deref_size: %" PRIu64, uvalue);
1457 break;
1458 case DW_OP_xderef_size:
1459 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1460 printf ("DW_OP_xderef_size: %" PRIu64, uvalue);
1461 break;
1462 case DW_OP_nop:
1463 printf ("DW_OP_nop");
1464 break;
1466 /* DWARF 3 extensions. */
1467 case DW_OP_push_object_address:
1468 printf ("DW_OP_push_object_address");
1469 break;
1470 case DW_OP_call2:
1471 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1472 this ought to be an 8-byte wide computation. */
1473 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1474 printf ("DW_OP_call2: <%#" PRIx64 ">", svalue + cu_offset);
1475 break;
1476 case DW_OP_call4:
1477 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1478 this ought to be an 8-byte wide computation. */
1479 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1480 printf ("DW_OP_call4: <%#" PRIx64 ">", svalue + cu_offset);
1481 break;
1482 case DW_OP_call_ref:
1483 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1484 this ought to be an 8-byte wide computation. */
1485 if (dwarf_version == -1)
1487 printf (_("(DW_OP_call_ref in frame info)"));
1488 /* No way to tell where the next op is, so just bail. */
1489 return need_frame_base;
1491 if (dwarf_version == 2)
1493 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1495 else
1497 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1499 printf ("DW_OP_call_ref: <%#" PRIx64 ">", uvalue);
1500 break;
1501 case DW_OP_form_tls_address:
1502 printf ("DW_OP_form_tls_address");
1503 break;
1504 case DW_OP_call_frame_cfa:
1505 printf ("DW_OP_call_frame_cfa");
1506 break;
1507 case DW_OP_bit_piece:
1508 printf ("DW_OP_bit_piece: ");
1509 READ_ULEB (uvalue, data, end);
1510 printf (_("size: %" PRIu64 " "), uvalue);
1511 READ_ULEB (uvalue, data, end);
1512 printf (_("offset: %" PRIu64 " "), uvalue);
1513 break;
1515 /* DWARF 4 extensions. */
1516 case DW_OP_stack_value:
1517 printf ("DW_OP_stack_value");
1518 break;
1520 case DW_OP_implicit_value:
1521 printf ("DW_OP_implicit_value");
1522 READ_ULEB (uvalue, data, end);
1523 data = display_block (data, uvalue, end, ' ');
1524 break;
1526 /* GNU extensions. */
1527 case DW_OP_GNU_push_tls_address:
1528 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1529 break;
1530 case DW_OP_GNU_uninit:
1531 printf ("DW_OP_GNU_uninit");
1532 /* FIXME: Is there data associated with this OP ? */
1533 break;
1534 case DW_OP_GNU_encoded_addr:
1536 int encoding = 0;
1537 uint64_t addr;
1539 if (data < end)
1540 encoding = *data++;
1541 addr = get_encoded_value (&data, encoding, section, end);
1543 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1544 print_hex_ns (addr, pointer_size);
1546 break;
1547 case DW_OP_implicit_pointer:
1548 case DW_OP_GNU_implicit_pointer:
1549 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1550 this ought to be an 8-byte wide computation. */
1551 if (dwarf_version == -1)
1553 printf (_("(%s in frame info)"),
1554 (op == DW_OP_implicit_pointer
1555 ? "DW_OP_implicit_pointer"
1556 : "DW_OP_GNU_implicit_pointer"));
1557 /* No way to tell where the next op is, so just bail. */
1558 return need_frame_base;
1560 if (dwarf_version == 2)
1562 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1564 else
1566 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1568 READ_SLEB (svalue, data, end);
1569 printf ("%s: <%#" PRIx64 "> %" PRId64,
1570 (op == DW_OP_implicit_pointer
1571 ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
1572 uvalue, svalue);
1573 break;
1574 case DW_OP_entry_value:
1575 case DW_OP_GNU_entry_value:
1576 READ_ULEB (uvalue, data, end);
1577 /* PR 17531: file: 0cc9cd00. */
1578 if (uvalue > (size_t) (end - data))
1579 uvalue = end - data;
1580 printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1581 : "DW_OP_GNU_entry_value"));
1582 if (decode_location_expression (data, pointer_size, offset_size,
1583 dwarf_version, uvalue,
1584 cu_offset, section))
1585 need_frame_base = 1;
1586 putchar (')');
1587 data += uvalue;
1588 break;
1589 case DW_OP_const_type:
1590 case DW_OP_GNU_const_type:
1591 READ_ULEB (uvalue, data, end);
1592 printf ("%s: <%#" PRIx64 "> ",
1593 (op == DW_OP_const_type ? "DW_OP_const_type"
1594 : "DW_OP_GNU_const_type"),
1595 cu_offset + uvalue);
1596 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1597 data = display_block (data, uvalue, end, ' ');
1598 break;
1599 case DW_OP_regval_type:
1600 case DW_OP_GNU_regval_type:
1601 READ_ULEB (uvalue, data, end);
1602 printf ("%s: %" PRIu64 " (%s)",
1603 (op == DW_OP_regval_type ? "DW_OP_regval_type"
1604 : "DW_OP_GNU_regval_type"),
1605 uvalue, regname (uvalue, 1));
1606 READ_ULEB (uvalue, data, end);
1607 printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1608 break;
1609 case DW_OP_deref_type:
1610 case DW_OP_GNU_deref_type:
1611 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1612 printf ("%s: %" PRId64,
1613 (op == DW_OP_deref_type ? "DW_OP_deref_type"
1614 : "DW_OP_GNU_deref_type"),
1615 uvalue);
1616 READ_ULEB (uvalue, data, end);
1617 printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1618 break;
1619 case DW_OP_convert:
1620 case DW_OP_GNU_convert:
1621 READ_ULEB (uvalue, data, end);
1622 printf ("%s <%#" PRIx64 ">",
1623 (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
1624 uvalue ? cu_offset + uvalue : uvalue);
1625 break;
1626 case DW_OP_reinterpret:
1627 case DW_OP_GNU_reinterpret:
1628 READ_ULEB (uvalue, data, end);
1629 printf ("%s <%#" PRIx64 ">",
1630 (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1631 : "DW_OP_GNU_reinterpret"),
1632 uvalue ? cu_offset + uvalue : uvalue);
1633 break;
1634 case DW_OP_GNU_parameter_ref:
1635 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1636 printf ("DW_OP_GNU_parameter_ref: <%#" PRIx64 ">",
1637 cu_offset + uvalue);
1638 break;
1639 case DW_OP_addrx:
1640 READ_ULEB (uvalue, data, end);
1641 printf ("DW_OP_addrx <%#" PRIx64 ">", uvalue);
1642 break;
1643 case DW_OP_GNU_addr_index:
1644 READ_ULEB (uvalue, data, end);
1645 printf ("DW_OP_GNU_addr_index <%#" PRIx64 ">", uvalue);
1646 break;
1647 case DW_OP_GNU_const_index:
1648 READ_ULEB (uvalue, data, end);
1649 printf ("DW_OP_GNU_const_index <%#" PRIx64 ">", uvalue);
1650 break;
1651 case DW_OP_GNU_variable_value:
1652 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1653 this ought to be an 8-byte wide computation. */
1654 if (dwarf_version == -1)
1656 printf (_("(DW_OP_GNU_variable_value in frame info)"));
1657 /* No way to tell where the next op is, so just bail. */
1658 return need_frame_base;
1660 if (dwarf_version == 2)
1662 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1664 else
1666 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1668 printf ("DW_OP_GNU_variable_value: <%#" PRIx64 ">", uvalue);
1669 break;
1671 /* HP extensions. */
1672 case DW_OP_HP_is_value:
1673 printf ("DW_OP_HP_is_value");
1674 /* FIXME: Is there data associated with this OP ? */
1675 break;
1676 case DW_OP_HP_fltconst4:
1677 printf ("DW_OP_HP_fltconst4");
1678 /* FIXME: Is there data associated with this OP ? */
1679 break;
1680 case DW_OP_HP_fltconst8:
1681 printf ("DW_OP_HP_fltconst8");
1682 /* FIXME: Is there data associated with this OP ? */
1683 break;
1684 case DW_OP_HP_mod_range:
1685 printf ("DW_OP_HP_mod_range");
1686 /* FIXME: Is there data associated with this OP ? */
1687 break;
1688 case DW_OP_HP_unmod_range:
1689 printf ("DW_OP_HP_unmod_range");
1690 /* FIXME: Is there data associated with this OP ? */
1691 break;
1692 case DW_OP_HP_tls:
1693 printf ("DW_OP_HP_tls");
1694 /* FIXME: Is there data associated with this OP ? */
1695 break;
1697 /* PGI (STMicroelectronics) extensions. */
1698 case DW_OP_PGI_omp_thread_num:
1699 /* Pushes the thread number for the current thread as it would be
1700 returned by the standard OpenMP library function:
1701 omp_get_thread_num(). The "current thread" is the thread for
1702 which the expression is being evaluated. */
1703 printf ("DW_OP_PGI_omp_thread_num");
1704 break;
1706 default:
1707 if (op >= DW_OP_lo_user
1708 && op <= DW_OP_hi_user)
1709 printf (_("(User defined location op %#x)"), op);
1710 else
1711 printf (_("(Unknown location op %#x)"), op);
1712 /* No way to tell where the next op is, so just bail. */
1713 return need_frame_base;
1716 /* Separate the ops. */
1717 if (data < end)
1718 printf ("; ");
1721 return need_frame_base;
1724 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1725 This is used for DWARF package files. */
1727 static struct cu_tu_set *
1728 find_cu_tu_set_v2 (uint64_t cu_offset, int do_types)
1730 struct cu_tu_set *p;
1731 unsigned int nsets;
1732 unsigned int dw_sect;
1734 if (do_types)
1736 p = tu_sets;
1737 nsets = tu_count;
1738 dw_sect = DW_SECT_TYPES;
1740 else
1742 p = cu_sets;
1743 nsets = cu_count;
1744 dw_sect = DW_SECT_INFO;
1746 while (nsets > 0)
1748 if (p->section_offsets [dw_sect] == cu_offset)
1749 return p;
1750 p++;
1751 nsets--;
1753 return NULL;
1756 static const char *
1757 fetch_alt_indirect_string (uint64_t offset)
1759 separate_info * i;
1761 if (! do_follow_links)
1762 return "";
1764 if (first_separate_info == NULL)
1765 return _("<no links available>");
1767 for (i = first_separate_info; i != NULL; i = i->next)
1769 struct dwarf_section * section;
1770 const char * ret;
1772 if (! load_debug_section (separate_debug_str, i->handle))
1773 continue;
1775 section = &debug_displays [separate_debug_str].section;
1777 if (section->start == NULL)
1778 continue;
1780 if (offset >= section->size)
1781 continue;
1783 ret = (const char *) (section->start + offset);
1784 /* Unfortunately we cannot rely upon the .debug_str section ending with a
1785 NUL byte. Since our caller is expecting to receive a well formed C
1786 string we test for the lack of a terminating byte here. */
1787 if (strnlen ((const char *) ret, section->size - offset)
1788 == section->size - offset)
1789 return _("<no NUL byte at end of alt .debug_str section>");
1791 return ret;
1794 warn (_("DW_FORM_GNU_strp_alt offset (%#" PRIx64 ")"
1795 " too big or no string sections available\n"), offset);
1796 return _("<offset is too big>");
1799 static const char *
1800 get_AT_name (unsigned long attribute)
1802 const char *name;
1804 if (attribute == 0)
1805 return "DW_AT value: 0";
1807 /* One value is shared by the MIPS and HP extensions: */
1808 if (attribute == DW_AT_MIPS_fde)
1809 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1811 name = get_DW_AT_name (attribute);
1813 if (name == NULL)
1815 static char buffer[100];
1817 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1818 attribute);
1819 return buffer;
1822 return name;
1825 static void
1826 add_dwo_info (const char * value, uint64_t cu_offset, dwo_type type)
1828 dwo_info * dwinfo = xmalloc (sizeof * dwinfo);
1830 dwinfo->type = type;
1831 dwinfo->value = value;
1832 dwinfo->cu_offset = cu_offset;
1833 dwinfo->next = first_dwo_info;
1834 first_dwo_info = dwinfo;
1837 static void
1838 add_dwo_name (const char * name, uint64_t cu_offset)
1840 add_dwo_info (name, cu_offset, DWO_NAME);
1843 static void
1844 add_dwo_dir (const char * dir, uint64_t cu_offset)
1846 add_dwo_info (dir, cu_offset, DWO_DIR);
1849 static void
1850 add_dwo_id (const char * id, uint64_t cu_offset)
1852 add_dwo_info (id, cu_offset, DWO_ID);
1855 static void
1856 free_dwo_info (void)
1858 dwo_info * dwinfo;
1859 dwo_info * next;
1861 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = next)
1863 next = dwinfo->next;
1864 free (dwinfo);
1866 first_dwo_info = NULL;
1869 /* Ensure that START + UVALUE is less than END.
1870 Return an adjusted UVALUE if necessary to ensure this relationship. */
1872 static inline uint64_t
1873 check_uvalue (const unsigned char *start,
1874 uint64_t uvalue,
1875 const unsigned char *end)
1877 uint64_t max_uvalue = end - start;
1879 /* See PR 17512: file: 008-103549-0.001:0.1.
1880 and PR 24829 for examples of where these tests are triggered. */
1881 if (uvalue > max_uvalue)
1883 warn (_("Corrupt attribute block length: %#" PRIx64 "\n"), uvalue);
1884 uvalue = max_uvalue;
1887 return uvalue;
1890 static unsigned char *
1891 skip_attr_bytes (unsigned long form,
1892 unsigned char *data,
1893 unsigned char *end,
1894 uint64_t pointer_size,
1895 uint64_t offset_size,
1896 int dwarf_version,
1897 uint64_t *value_return)
1899 int64_t svalue;
1900 uint64_t uvalue = 0;
1901 uint64_t inc = 0;
1903 * value_return = 0;
1905 switch (form)
1907 case DW_FORM_ref_addr:
1908 if (dwarf_version == 2)
1909 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1910 else if (dwarf_version > 2)
1911 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1912 else
1913 return NULL;
1914 break;
1916 case DW_FORM_addr:
1917 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1918 break;
1920 case DW_FORM_strp:
1921 case DW_FORM_line_strp:
1922 case DW_FORM_sec_offset:
1923 case DW_FORM_GNU_ref_alt:
1924 case DW_FORM_GNU_strp_alt:
1925 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1926 break;
1928 case DW_FORM_flag_present:
1929 uvalue = 1;
1930 break;
1932 case DW_FORM_ref1:
1933 case DW_FORM_flag:
1934 case DW_FORM_data1:
1935 case DW_FORM_strx1:
1936 case DW_FORM_addrx1:
1937 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1938 break;
1940 case DW_FORM_strx3:
1941 case DW_FORM_addrx3:
1942 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
1943 break;
1945 case DW_FORM_ref2:
1946 case DW_FORM_data2:
1947 case DW_FORM_strx2:
1948 case DW_FORM_addrx2:
1949 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1950 break;
1952 case DW_FORM_ref4:
1953 case DW_FORM_data4:
1954 case DW_FORM_strx4:
1955 case DW_FORM_addrx4:
1956 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1957 break;
1959 case DW_FORM_sdata:
1960 READ_SLEB (svalue, data, end);
1961 uvalue = svalue;
1962 break;
1964 case DW_FORM_ref_udata:
1965 case DW_FORM_udata:
1966 case DW_FORM_GNU_str_index:
1967 case DW_FORM_strx:
1968 case DW_FORM_GNU_addr_index:
1969 case DW_FORM_addrx:
1970 case DW_FORM_loclistx:
1971 case DW_FORM_rnglistx:
1972 READ_ULEB (uvalue, data, end);
1973 break;
1975 case DW_FORM_ref8:
1976 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1977 break;
1979 case DW_FORM_data8:
1980 case DW_FORM_ref_sig8:
1981 inc = 8;
1982 break;
1984 case DW_FORM_data16:
1985 inc = 16;
1986 break;
1988 case DW_FORM_string:
1989 inc = strnlen ((char *) data, end - data) + 1;
1990 break;
1992 case DW_FORM_block:
1993 case DW_FORM_exprloc:
1994 READ_ULEB (uvalue, data, end);
1995 inc = uvalue;
1996 break;
1998 case DW_FORM_block1:
1999 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2000 inc = uvalue;
2001 break;
2003 case DW_FORM_block2:
2004 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2005 inc = uvalue;
2006 break;
2008 case DW_FORM_block4:
2009 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2010 inc = uvalue;
2011 break;
2013 case DW_FORM_indirect:
2014 READ_ULEB (form, data, end);
2015 if (form == DW_FORM_implicit_const)
2016 SKIP_ULEB (data, end);
2017 return skip_attr_bytes (form, data, end, pointer_size, offset_size,
2018 dwarf_version, value_return);
2020 default:
2021 return NULL;
2024 * value_return = uvalue;
2025 if (inc <= (size_t) (end - data))
2026 data += inc;
2027 else
2028 data = end;
2029 return data;
2032 /* Given form FORM with value UVALUE, locate and return the abbreviation
2033 associated with it. */
2035 static abbrev_entry *
2036 get_type_abbrev_from_form (unsigned long form,
2037 unsigned long uvalue,
2038 uint64_t cu_offset,
2039 unsigned char *cu_end,
2040 const struct dwarf_section *section,
2041 unsigned long *abbrev_num_return,
2042 unsigned char **data_return,
2043 abbrev_map **map_return)
2045 unsigned long abbrev_number;
2046 abbrev_map * map;
2047 abbrev_entry * entry;
2048 unsigned char * data;
2050 if (abbrev_num_return != NULL)
2051 * abbrev_num_return = 0;
2052 if (data_return != NULL)
2053 * data_return = NULL;
2055 switch (form)
2057 case DW_FORM_GNU_ref_alt:
2058 case DW_FORM_ref_sig8:
2059 /* FIXME: We are unable to handle this form at the moment. */
2060 return NULL;
2062 case DW_FORM_ref_addr:
2063 if (uvalue >= section->size)
2065 warn (_("Unable to resolve ref_addr form: uvalue %lx "
2066 "> section size %" PRIx64 " (%s)\n"),
2067 uvalue, section->size, section->name);
2068 return NULL;
2070 break;
2072 case DW_FORM_ref_sup4:
2073 case DW_FORM_ref_sup8:
2074 break;
2076 case DW_FORM_ref1:
2077 case DW_FORM_ref2:
2078 case DW_FORM_ref4:
2079 case DW_FORM_ref8:
2080 case DW_FORM_ref_udata:
2081 if (uvalue + cu_offset < uvalue
2082 || uvalue + cu_offset > (size_t) (cu_end - section->start))
2084 warn (_("Unable to resolve ref form: uvalue %lx + cu_offset %" PRIx64
2085 " > CU size %tx\n"),
2086 uvalue, cu_offset, cu_end - section->start);
2087 return NULL;
2089 uvalue += cu_offset;
2090 break;
2092 /* FIXME: Are there other DW_FORMs that can be used by types ? */
2094 default:
2095 warn (_("Unexpected form %lx encountered whilst finding abbreviation for type\n"), form);
2096 return NULL;
2099 data = (unsigned char *) section->start + uvalue;
2100 map = find_abbrev_map_by_offset (uvalue);
2102 if (map == NULL)
2104 warn (_("Unable to find abbreviations for CU offset %#lx\n"), uvalue);
2105 return NULL;
2107 if (map->list == NULL)
2109 warn (_("Empty abbreviation list encountered for CU offset %lx\n"), uvalue);
2110 return NULL;
2113 if (map_return != NULL)
2115 if (form == DW_FORM_ref_addr)
2116 *map_return = map;
2117 else
2118 *map_return = NULL;
2121 READ_ULEB (abbrev_number, data, section->start + section->size);
2123 for (entry = map->list->first_abbrev; entry != NULL; entry = entry->next)
2124 if (entry->number == abbrev_number)
2125 break;
2127 if (abbrev_num_return != NULL)
2128 * abbrev_num_return = abbrev_number;
2130 if (data_return != NULL)
2131 * data_return = data;
2133 if (entry == NULL)
2134 warn (_("Unable to find entry for abbreviation %lu\n"), abbrev_number);
2136 return entry;
2139 /* Return IS_SIGNED set to TRUE if the type using abbreviation ENTRY
2140 can be determined to be a signed type. The data for ENTRY can be
2141 found starting at DATA. */
2143 static void
2144 get_type_signedness (abbrev_entry *entry,
2145 const struct dwarf_section *section,
2146 unsigned char *data,
2147 unsigned char *end,
2148 uint64_t cu_offset,
2149 uint64_t pointer_size,
2150 uint64_t offset_size,
2151 int dwarf_version,
2152 bool *is_signed,
2153 unsigned int nesting)
2155 abbrev_attr * attr;
2157 * is_signed = false;
2159 #define MAX_NESTING 20
2160 if (nesting > MAX_NESTING)
2162 /* FIXME: Warn - or is this expected ?
2163 NB/ We need to avoid infinite recursion. */
2164 return;
2167 for (attr = entry->first_attr;
2168 attr != NULL && attr->attribute;
2169 attr = attr->next)
2171 unsigned char * orig_data = data;
2172 uint64_t uvalue = 0;
2174 data = skip_attr_bytes (attr->form, data, end, pointer_size,
2175 offset_size, dwarf_version, & uvalue);
2176 if (data == NULL)
2177 return;
2179 switch (attr->attribute)
2181 case DW_AT_linkage_name:
2182 case DW_AT_name:
2183 if (do_wide)
2185 if (attr->form == DW_FORM_strp)
2186 printf (", %s", fetch_indirect_string (uvalue));
2187 else if (attr->form == DW_FORM_string)
2188 printf (", %.*s", (int) (end - orig_data), orig_data);
2190 break;
2192 case DW_AT_type:
2193 /* Recurse. */
2195 abbrev_entry *type_abbrev;
2196 unsigned char *type_data;
2197 abbrev_map *map;
2199 type_abbrev = get_type_abbrev_from_form (attr->form,
2200 uvalue,
2201 cu_offset,
2202 end,
2203 section,
2204 NULL /* abbrev num return */,
2205 &type_data,
2206 &map);
2207 if (type_abbrev == NULL)
2208 break;
2210 get_type_signedness (type_abbrev, section, type_data,
2211 map ? section->start + map->end : end,
2212 map ? map->start : cu_offset,
2213 pointer_size, offset_size, dwarf_version,
2214 is_signed, nesting + 1);
2216 break;
2218 case DW_AT_encoding:
2219 /* Determine signness. */
2220 switch (uvalue)
2222 case DW_ATE_address:
2223 /* FIXME - some architectures have signed addresses. */
2224 case DW_ATE_boolean:
2225 case DW_ATE_unsigned:
2226 case DW_ATE_unsigned_char:
2227 case DW_ATE_unsigned_fixed:
2228 * is_signed = false;
2229 break;
2231 default:
2232 case DW_ATE_complex_float:
2233 case DW_ATE_float:
2234 case DW_ATE_signed:
2235 case DW_ATE_signed_char:
2236 case DW_ATE_imaginary_float:
2237 case DW_ATE_decimal_float:
2238 case DW_ATE_signed_fixed:
2239 * is_signed = true;
2240 break;
2242 break;
2247 static void
2248 read_and_print_leb128 (unsigned char *data,
2249 unsigned int *bytes_read,
2250 unsigned const char *end,
2251 bool is_signed)
2253 int status;
2254 uint64_t val = read_leb128 (data, end, is_signed, bytes_read, &status);
2255 if (status != 0)
2256 report_leb_status (status);
2257 else if (is_signed)
2258 printf ("%" PRId64, val);
2259 else
2260 printf ("%" PRIu64, val);
2263 static void
2264 display_discr_list (unsigned long form,
2265 uint64_t uvalue,
2266 unsigned char *data,
2267 int level)
2269 unsigned char *end = data;
2271 if (uvalue == 0)
2273 printf ("[default]");
2274 return;
2277 switch (form)
2279 case DW_FORM_block:
2280 case DW_FORM_block1:
2281 case DW_FORM_block2:
2282 case DW_FORM_block4:
2283 /* Move data pointer back to the start of the byte array. */
2284 data -= uvalue;
2285 break;
2286 default:
2287 printf ("<corrupt>\n");
2288 warn (_("corrupt discr_list - not using a block form\n"));
2289 return;
2292 if (uvalue < 2)
2294 printf ("<corrupt>\n");
2295 warn (_("corrupt discr_list - block not long enough\n"));
2296 return;
2299 bool is_signed = (level > 0 && level <= MAX_CU_NESTING
2300 ? level_type_signed [level - 1] : false);
2302 printf ("(");
2303 while (data < end)
2305 unsigned char discriminant;
2306 unsigned int bytes_read;
2308 SAFE_BYTE_GET_AND_INC (discriminant, data, 1, end);
2310 switch (discriminant)
2312 case DW_DSC_label:
2313 printf ("label ");
2314 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2315 data += bytes_read;
2316 break;
2318 case DW_DSC_range:
2319 printf ("range ");
2320 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2321 data += bytes_read;
2323 printf ("..");
2324 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2325 data += bytes_read;
2326 break;
2328 default:
2329 printf ("<corrupt>\n");
2330 warn (_("corrupt discr_list - unrecognized discriminant byte %#x\n"),
2331 discriminant);
2332 return;
2335 if (data < end)
2336 printf (", ");
2339 if (is_signed)
2340 printf (")(signed)");
2341 else
2342 printf (")(unsigned)");
2345 static unsigned char *
2346 read_and_display_attr_value (unsigned long attribute,
2347 unsigned long form,
2348 int64_t implicit_const,
2349 unsigned char *start,
2350 unsigned char *data,
2351 unsigned char *end,
2352 uint64_t cu_offset,
2353 uint64_t pointer_size,
2354 uint64_t offset_size,
2355 int dwarf_version,
2356 debug_info *debug_info_p,
2357 int do_loc,
2358 struct dwarf_section *section,
2359 struct cu_tu_set *this_set,
2360 char delimiter,
2361 int level)
2363 int64_t svalue;
2364 uint64_t uvalue = 0;
2365 uint64_t uvalue_hi = 0;
2366 unsigned char *block_start = NULL;
2367 unsigned char *orig_data = data;
2369 if (data > end || (data == end && form != DW_FORM_flag_present))
2371 warn (_("Corrupt attribute\n"));
2372 return data;
2375 if (do_wide && ! do_loc)
2377 /* PR 26847: Display the name of the form. */
2378 const char * name = get_FORM_name (form);
2380 /* For convenience we skip the DW_FORM_ prefix to the name. */
2381 if (name[0] == 'D')
2382 name += 8; /* strlen ("DW_FORM_") */
2383 printf ("%c(%s)", delimiter, name);
2386 switch (form)
2388 case DW_FORM_ref_addr:
2389 if (dwarf_version == 2)
2390 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2391 else if (dwarf_version > 2)
2392 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2393 else
2394 error (_("Internal error: DW_FORM_ref_addr is not supported in DWARF version 1.\n"));
2395 break;
2397 case DW_FORM_addr:
2398 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2399 break;
2401 case DW_FORM_strp_sup:
2402 case DW_FORM_strp:
2403 case DW_FORM_line_strp:
2404 case DW_FORM_sec_offset:
2405 case DW_FORM_GNU_ref_alt:
2406 case DW_FORM_GNU_strp_alt:
2407 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2408 break;
2410 case DW_FORM_flag_present:
2411 uvalue = 1;
2412 break;
2414 case DW_FORM_ref1:
2415 case DW_FORM_flag:
2416 case DW_FORM_data1:
2417 case DW_FORM_strx1:
2418 case DW_FORM_addrx1:
2419 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2420 break;
2422 case DW_FORM_ref2:
2423 case DW_FORM_data2:
2424 case DW_FORM_strx2:
2425 case DW_FORM_addrx2:
2426 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2427 break;
2429 case DW_FORM_strx3:
2430 case DW_FORM_addrx3:
2431 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2432 break;
2434 case DW_FORM_ref_sup4:
2435 case DW_FORM_ref4:
2436 case DW_FORM_data4:
2437 case DW_FORM_strx4:
2438 case DW_FORM_addrx4:
2439 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2440 break;
2442 case DW_FORM_ref_sup8:
2443 case DW_FORM_ref8:
2444 case DW_FORM_data8:
2445 case DW_FORM_ref_sig8:
2446 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2447 break;
2449 case DW_FORM_data16:
2450 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2451 SAFE_BYTE_GET_AND_INC (uvalue_hi, data, 8, end);
2452 if (byte_get != byte_get_little_endian)
2454 uint64_t utmp = uvalue;
2455 uvalue = uvalue_hi;
2456 uvalue_hi = utmp;
2458 break;
2460 case DW_FORM_sdata:
2461 READ_SLEB (svalue, data, end);
2462 uvalue = svalue;
2463 break;
2465 case DW_FORM_GNU_str_index:
2466 case DW_FORM_strx:
2467 case DW_FORM_ref_udata:
2468 case DW_FORM_udata:
2469 case DW_FORM_GNU_addr_index:
2470 case DW_FORM_addrx:
2471 case DW_FORM_loclistx:
2472 case DW_FORM_rnglistx:
2473 READ_ULEB (uvalue, data, end);
2474 break;
2476 case DW_FORM_indirect:
2477 READ_ULEB (form, data, end);
2478 if (!do_loc)
2479 printf ("%c%s", delimiter, get_FORM_name (form));
2480 if (form == DW_FORM_implicit_const)
2481 READ_SLEB (implicit_const, data, end);
2482 return read_and_display_attr_value (attribute, form, implicit_const,
2483 start, data, end,
2484 cu_offset, pointer_size,
2485 offset_size, dwarf_version,
2486 debug_info_p, do_loc,
2487 section, this_set, delimiter, level);
2489 case DW_FORM_implicit_const:
2490 uvalue = implicit_const;
2491 break;
2493 default:
2494 break;
2497 switch (form)
2499 case DW_FORM_ref_addr:
2500 if (!do_loc)
2501 printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2502 break;
2504 case DW_FORM_GNU_ref_alt:
2505 if (!do_loc)
2507 if (do_wide)
2508 /* We have already printed the form name. */
2509 printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2510 else
2511 printf ("%c<alt %#" PRIx64 ">", delimiter, uvalue);
2513 /* FIXME: Follow the reference... */
2514 break;
2516 case DW_FORM_ref1:
2517 case DW_FORM_ref2:
2518 case DW_FORM_ref4:
2519 case DW_FORM_ref_sup4:
2520 case DW_FORM_ref_udata:
2521 if (!do_loc)
2522 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2523 break;
2525 case DW_FORM_data4:
2526 case DW_FORM_addr:
2527 case DW_FORM_sec_offset:
2528 if (!do_loc)
2529 printf ("%c%#" PRIx64, delimiter, uvalue);
2530 break;
2532 case DW_FORM_flag_present:
2533 case DW_FORM_flag:
2534 case DW_FORM_data1:
2535 case DW_FORM_data2:
2536 case DW_FORM_sdata:
2537 if (!do_loc)
2538 printf ("%c%" PRId64, delimiter, uvalue);
2539 break;
2541 case DW_FORM_udata:
2542 if (!do_loc)
2543 printf ("%c%" PRIu64, delimiter, uvalue);
2544 break;
2546 case DW_FORM_implicit_const:
2547 if (!do_loc)
2548 printf ("%c%" PRId64, delimiter, implicit_const);
2549 break;
2551 case DW_FORM_ref_sup8:
2552 case DW_FORM_ref8:
2553 case DW_FORM_data8:
2554 if (!do_loc)
2556 uint64_t utmp = uvalue;
2557 if (form == DW_FORM_ref8)
2558 utmp += cu_offset;
2559 printf ("%c%#" PRIx64, delimiter, utmp);
2561 break;
2563 case DW_FORM_data16:
2564 if (!do_loc)
2566 if (uvalue_hi == 0)
2567 printf (" %#" PRIx64, uvalue);
2568 else
2569 printf (" %#" PRIx64 "%016" PRIx64, uvalue_hi, uvalue);
2571 break;
2573 case DW_FORM_string:
2574 if (!do_loc)
2575 printf ("%c%.*s", delimiter, (int) (end - data), data);
2576 data += strnlen ((char *) data, end - data);
2577 if (data < end)
2578 data++;
2579 break;
2581 case DW_FORM_block:
2582 case DW_FORM_exprloc:
2583 READ_ULEB (uvalue, data, end);
2584 do_block:
2585 block_start = data;
2586 if (block_start >= end)
2588 warn (_("Block ends prematurely\n"));
2589 uvalue = 0;
2590 block_start = end;
2593 uvalue = check_uvalue (block_start, uvalue, end);
2595 data = block_start + uvalue;
2596 if (!do_loc)
2598 unsigned char op;
2600 SAFE_BYTE_GET (op, block_start, sizeof (op), end);
2601 if (op != DW_OP_addrx)
2602 data = display_block (block_start, uvalue, end, delimiter);
2604 break;
2606 case DW_FORM_block1:
2607 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2608 goto do_block;
2610 case DW_FORM_block2:
2611 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2612 goto do_block;
2614 case DW_FORM_block4:
2615 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2616 goto do_block;
2618 case DW_FORM_strp:
2619 if (!do_loc)
2621 if (do_wide)
2622 /* We have already displayed the form name. */
2623 printf (_("%c(offset: %#" PRIx64 "): %s"),
2624 delimiter, uvalue, fetch_indirect_string (uvalue));
2625 else
2626 printf (_("%c(indirect string, offset: %#" PRIx64 "): %s"),
2627 delimiter, uvalue, fetch_indirect_string (uvalue));
2629 break;
2631 case DW_FORM_line_strp:
2632 if (!do_loc)
2634 if (do_wide)
2635 /* We have already displayed the form name. */
2636 printf (_("%c(offset: %#" PRIx64 "): %s"),
2637 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2638 else
2639 printf (_("%c(indirect line string, offset: %#" PRIx64 "): %s"),
2640 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2642 break;
2644 case DW_FORM_GNU_str_index:
2645 case DW_FORM_strx:
2646 case DW_FORM_strx1:
2647 case DW_FORM_strx2:
2648 case DW_FORM_strx3:
2649 case DW_FORM_strx4:
2650 if (!do_loc)
2652 const char *suffix = section ? strrchr (section->name, '.') : NULL;
2653 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2654 const char *strng;
2656 strng = fetch_indexed_string (uvalue, this_set, offset_size, dwo,
2657 debug_info_p ? debug_info_p->str_offsets_base : 0);
2658 if (do_wide)
2659 /* We have already displayed the form name. */
2660 printf (_("%c(offset: %#" PRIx64 "): %s"),
2661 delimiter, uvalue, strng);
2662 else
2663 printf (_("%c(indexed string: %#" PRIx64 "): %s"),
2664 delimiter, uvalue, strng);
2666 break;
2668 case DW_FORM_GNU_strp_alt:
2669 if (!do_loc)
2671 if (do_wide)
2672 /* We have already displayed the form name. */
2673 printf (_("%c(offset: %#" PRIx64 ") %s"),
2674 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2675 else
2676 printf (_("%c(alt indirect string, offset: %#" PRIx64 ") %s"),
2677 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2679 break;
2681 case DW_FORM_indirect:
2682 /* Handled above. */
2683 break;
2685 case DW_FORM_ref_sig8:
2686 if (!do_loc)
2687 printf ("%c%s: %#" PRIx64, delimiter, do_wide ? "" : "signature",
2688 uvalue);
2689 break;
2691 case DW_FORM_GNU_addr_index:
2692 case DW_FORM_addrx:
2693 case DW_FORM_addrx1:
2694 case DW_FORM_addrx2:
2695 case DW_FORM_addrx3:
2696 case DW_FORM_addrx4:
2697 case DW_FORM_loclistx:
2698 case DW_FORM_rnglistx:
2699 if (!do_loc)
2701 uint64_t base, idx;
2702 const char *suffix = strrchr (section->name, '.');
2703 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2705 if (form == DW_FORM_loclistx)
2707 if (dwo)
2709 idx = fetch_indexed_offset (uvalue, loclists_dwo,
2710 debug_info_p->loclists_base,
2711 debug_info_p->offset_size);
2712 if (idx != (uint64_t) -1)
2713 idx += (offset_size == 8) ? 20 : 12;
2715 else if (debug_info_p == NULL || dwarf_version > 4)
2717 idx = fetch_indexed_offset (uvalue, loclists,
2718 debug_info_p->loclists_base,
2719 debug_info_p->offset_size);
2721 else
2723 /* We want to compute:
2724 idx = fetch_indexed_value (uvalue, loclists,
2725 debug_info_p->loclists_base);
2726 idx += debug_info_p->loclists_base;
2727 Fortunately we already have that sum cached in the
2728 loc_offsets array. */
2729 if (uvalue < debug_info_p->num_loc_offsets)
2730 idx = debug_info_p->loc_offsets [uvalue];
2731 else
2733 warn (_("loc_offset %" PRIu64 " too big\n"), uvalue);
2734 idx = -1;
2738 else if (form == DW_FORM_rnglistx)
2740 if (dwo)
2742 idx = fetch_indexed_offset (uvalue, rnglists,
2743 debug_info_p->rnglists_base,
2744 debug_info_p->offset_size);
2746 else
2748 if (debug_info_p == NULL)
2749 base = 0;
2750 else
2751 base = debug_info_p->rnglists_base;
2752 idx = fetch_indexed_offset (uvalue, rnglists, base,
2753 debug_info_p->offset_size);
2756 else
2758 if (debug_info_p == NULL)
2759 base = 0;
2760 else if (debug_info_p->addr_base == DEBUG_INFO_UNAVAILABLE)
2761 base = 0;
2762 else
2763 base = debug_info_p->addr_base;
2765 base += uvalue * pointer_size;
2766 idx = fetch_indexed_addr (base, pointer_size);
2769 /* We have already displayed the form name. */
2770 if (idx != (uint64_t) -1)
2771 printf (_("%c(index: %#" PRIx64 "): %#" PRIx64),
2772 delimiter, uvalue, idx);
2774 break;
2776 case DW_FORM_strp_sup:
2777 if (!do_loc)
2778 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2779 break;
2781 default:
2782 warn (_("Unrecognized form: %#lx"), form);
2783 /* What to do? Consume a byte maybe? */
2784 ++data;
2785 break;
2788 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
2789 && num_debug_info_entries == 0
2790 && debug_info_p != NULL)
2792 switch (attribute)
2794 case DW_AT_loclists_base:
2795 if (debug_info_p->loclists_base)
2796 warn (_("CU @ %#" PRIx64 " has multiple loclists_base values "
2797 "(%#" PRIx64 " and %#" PRIx64 ")"),
2798 debug_info_p->cu_offset,
2799 debug_info_p->loclists_base, uvalue);
2800 svalue = uvalue;
2801 if (svalue < 0)
2803 warn (_("CU @ %#" PRIx64 " has has a negative loclists_base "
2804 "value of %#" PRIx64 " - treating as zero"),
2805 debug_info_p->cu_offset, svalue);
2806 uvalue = 0;
2808 debug_info_p->loclists_base = uvalue;
2809 break;
2811 case DW_AT_rnglists_base:
2812 /* Assignment to debug_info_p->rnglists_base is now elsewhere. */
2813 break;
2815 case DW_AT_str_offsets_base:
2816 if (debug_info_p->str_offsets_base)
2817 warn (_("CU @ %#" PRIx64 " has multiple str_offsets_base values "
2818 "%#" PRIx64 " and %#" PRIx64 ")"),
2819 debug_info_p->cu_offset,
2820 debug_info_p->str_offsets_base, uvalue);
2821 svalue = uvalue;
2822 if (svalue < 0)
2824 warn (_("CU @ %#" PRIx64 " has has a negative stroffsets_base "
2825 "value of %#" PRIx64 " - treating as zero"),
2826 debug_info_p->cu_offset, svalue);
2827 uvalue = 0;
2829 debug_info_p->str_offsets_base = uvalue;
2830 break;
2832 case DW_AT_frame_base:
2833 have_frame_base = 1;
2834 /* Fall through. */
2835 case DW_AT_location:
2836 case DW_AT_GNU_locviews:
2837 case DW_AT_string_length:
2838 case DW_AT_return_addr:
2839 case DW_AT_data_member_location:
2840 case DW_AT_vtable_elem_location:
2841 case DW_AT_segment:
2842 case DW_AT_static_link:
2843 case DW_AT_use_location:
2844 case DW_AT_call_value:
2845 case DW_AT_GNU_call_site_value:
2846 case DW_AT_call_data_value:
2847 case DW_AT_GNU_call_site_data_value:
2848 case DW_AT_call_target:
2849 case DW_AT_GNU_call_site_target:
2850 case DW_AT_call_target_clobbered:
2851 case DW_AT_GNU_call_site_target_clobbered:
2852 if ((dwarf_version < 4
2853 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2854 || form == DW_FORM_sec_offset
2855 || form == DW_FORM_loclistx)
2857 /* Process location list. */
2858 unsigned int lmax = debug_info_p->max_loc_offsets;
2859 unsigned int num = debug_info_p->num_loc_offsets;
2861 if (lmax == 0 || num >= lmax)
2863 lmax += 1024;
2864 debug_info_p->loc_offsets = (uint64_t *)
2865 xcrealloc (debug_info_p->loc_offsets,
2866 lmax, sizeof (*debug_info_p->loc_offsets));
2867 debug_info_p->loc_views = (uint64_t *)
2868 xcrealloc (debug_info_p->loc_views,
2869 lmax, sizeof (*debug_info_p->loc_views));
2870 debug_info_p->have_frame_base = (int *)
2871 xcrealloc (debug_info_p->have_frame_base,
2872 lmax, sizeof (*debug_info_p->have_frame_base));
2873 debug_info_p->max_loc_offsets = lmax;
2875 if (form == DW_FORM_loclistx)
2876 uvalue = fetch_indexed_offset (num, loclists,
2877 debug_info_p->loclists_base,
2878 debug_info_p->offset_size);
2879 else if (this_set != NULL)
2880 uvalue += this_set->section_offsets [DW_SECT_LOC];
2882 debug_info_p->have_frame_base [num] = have_frame_base;
2883 if (attribute != DW_AT_GNU_locviews)
2885 uvalue += debug_info_p->loclists_base;
2887 /* Corrupt DWARF info can produce more offsets than views.
2888 See PR 23062 for an example. */
2889 if (debug_info_p->num_loc_offsets
2890 > debug_info_p->num_loc_views)
2891 warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
2892 else
2894 debug_info_p->loc_offsets [num] = uvalue;
2895 debug_info_p->num_loc_offsets++;
2898 else
2900 if (debug_info_p->num_loc_views > num)
2902 warn (_("The number of views (%u) is greater than the number of locations (%u)\n"),
2903 debug_info_p->num_loc_views, num);
2904 debug_info_p->num_loc_views = num;
2906 else
2907 num = debug_info_p->num_loc_views;
2908 if (num > debug_info_p->num_loc_offsets)
2909 warn (_("More DW_AT_GNU_locview attributes than location offset attributes\n"));
2910 else
2912 debug_info_p->loc_views [num] = uvalue;
2913 debug_info_p->num_loc_views++;
2917 break;
2919 case DW_AT_low_pc:
2920 if (need_base_address)
2922 if (form == DW_FORM_addrx)
2923 uvalue = fetch_indexed_addr (debug_info_p->addr_base
2924 + uvalue * pointer_size,
2925 pointer_size);
2927 debug_info_p->base_address = uvalue;
2929 break;
2931 case DW_AT_GNU_addr_base:
2932 case DW_AT_addr_base:
2933 debug_info_p->addr_base = uvalue;
2934 /* Retrieved elsewhere so that it is in
2935 place by the time we read low_pc. */
2936 break;
2938 case DW_AT_GNU_ranges_base:
2939 debug_info_p->ranges_base = uvalue;
2940 break;
2942 case DW_AT_ranges:
2943 if ((dwarf_version < 4
2944 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2945 || form == DW_FORM_sec_offset
2946 || form == DW_FORM_rnglistx)
2948 /* Process range list. */
2949 unsigned int lmax = debug_info_p->max_range_lists;
2950 unsigned int num = debug_info_p->num_range_lists;
2952 if (lmax == 0 || num >= lmax)
2954 lmax += 1024;
2955 debug_info_p->range_lists = (uint64_t *)
2956 xcrealloc (debug_info_p->range_lists,
2957 lmax, sizeof (*debug_info_p->range_lists));
2958 debug_info_p->max_range_lists = lmax;
2961 if (form == DW_FORM_rnglistx)
2962 uvalue = fetch_indexed_offset (uvalue, rnglists,
2963 debug_info_p->rnglists_base,
2964 debug_info_p->offset_size);
2966 debug_info_p->range_lists [num] = uvalue;
2967 debug_info_p->num_range_lists++;
2969 break;
2971 case DW_AT_GNU_dwo_name:
2972 case DW_AT_dwo_name:
2973 if (need_dwo_info)
2974 switch (form)
2976 case DW_FORM_strp:
2977 add_dwo_name ((const char *) fetch_indirect_string (uvalue),
2978 cu_offset);
2979 break;
2980 case DW_FORM_GNU_strp_alt:
2981 add_dwo_name ((const char *) fetch_alt_indirect_string (uvalue), cu_offset);
2982 break;
2983 case DW_FORM_GNU_str_index:
2984 case DW_FORM_strx:
2985 case DW_FORM_strx1:
2986 case DW_FORM_strx2:
2987 case DW_FORM_strx3:
2988 case DW_FORM_strx4:
2989 add_dwo_name (fetch_indexed_string (uvalue, this_set,
2990 offset_size, false,
2991 debug_info_p->str_offsets_base),
2992 cu_offset);
2993 break;
2994 case DW_FORM_string:
2995 add_dwo_name ((const char *) orig_data, cu_offset);
2996 break;
2997 default:
2998 warn (_("Unsupported form (%s) for attribute %s\n"),
2999 get_FORM_name (form), get_AT_name (attribute));
3000 break;
3002 break;
3004 case DW_AT_comp_dir:
3005 /* FIXME: Also extract a build-id in a CU/TU. */
3006 if (need_dwo_info)
3007 switch (form)
3009 case DW_FORM_strp:
3010 add_dwo_dir ((const char *) fetch_indirect_string (uvalue), cu_offset);
3011 break;
3012 case DW_FORM_GNU_strp_alt:
3013 add_dwo_dir (fetch_alt_indirect_string (uvalue), cu_offset);
3014 break;
3015 case DW_FORM_line_strp:
3016 add_dwo_dir ((const char *) fetch_indirect_line_string (uvalue), cu_offset);
3017 break;
3018 case DW_FORM_GNU_str_index:
3019 case DW_FORM_strx:
3020 case DW_FORM_strx1:
3021 case DW_FORM_strx2:
3022 case DW_FORM_strx3:
3023 case DW_FORM_strx4:
3024 add_dwo_dir (fetch_indexed_string (uvalue, this_set, offset_size, false,
3025 debug_info_p->str_offsets_base),
3026 cu_offset);
3027 break;
3028 case DW_FORM_string:
3029 add_dwo_dir ((const char *) orig_data, cu_offset);
3030 break;
3031 default:
3032 warn (_("Unsupported form (%s) for attribute %s\n"),
3033 get_FORM_name (form), get_AT_name (attribute));
3034 break;
3036 break;
3038 case DW_AT_GNU_dwo_id:
3039 if (need_dwo_info)
3040 switch (form)
3042 case DW_FORM_data8:
3043 /* FIXME: Record the length of the ID as well ? */
3044 add_dwo_id ((const char *) (data - 8), cu_offset);
3045 break;
3046 default:
3047 warn (_("Unsupported form (%s) for attribute %s\n"),
3048 get_FORM_name (form), get_AT_name (attribute));
3049 break;
3051 break;
3053 default:
3054 break;
3058 if (do_loc || attribute == 0)
3059 return data;
3061 /* For some attributes we can display further information. */
3062 switch (attribute)
3064 case DW_AT_type:
3065 if (level >= 0 && level < MAX_CU_NESTING
3066 && uvalue < (size_t) (end - start))
3068 bool is_signed = false;
3069 abbrev_entry *type_abbrev;
3070 unsigned char *type_data;
3071 abbrev_map *map;
3073 type_abbrev = get_type_abbrev_from_form (form, uvalue,
3074 cu_offset, end,
3075 section, NULL,
3076 &type_data, &map);
3077 if (type_abbrev != NULL)
3079 get_type_signedness (type_abbrev, section, type_data,
3080 map ? section->start + map->end : end,
3081 map ? map->start : cu_offset,
3082 pointer_size, offset_size, dwarf_version,
3083 & is_signed, 0);
3085 level_type_signed[level] = is_signed;
3087 break;
3089 case DW_AT_inline:
3090 printf ("\t");
3091 switch (uvalue)
3093 case DW_INL_not_inlined:
3094 printf (_("(not inlined)"));
3095 break;
3096 case DW_INL_inlined:
3097 printf (_("(inlined)"));
3098 break;
3099 case DW_INL_declared_not_inlined:
3100 printf (_("(declared as inline but ignored)"));
3101 break;
3102 case DW_INL_declared_inlined:
3103 printf (_("(declared as inline and inlined)"));
3104 break;
3105 default:
3106 printf (_(" (Unknown inline attribute value: %#" PRIx64 ")"),
3107 uvalue);
3108 break;
3110 break;
3112 case DW_AT_language:
3113 printf ("\t");
3114 switch (uvalue)
3116 /* Ordered by the numeric value of these constants. */
3117 case DW_LANG_C89: printf ("(ANSI C)"); break;
3118 case DW_LANG_C: printf ("(non-ANSI C)"); break;
3119 case DW_LANG_Ada83: printf ("(Ada)"); break;
3120 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
3121 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
3122 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
3123 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
3124 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
3125 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
3126 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
3127 /* DWARF 2.1 values. */
3128 case DW_LANG_Java: printf ("(Java)"); break;
3129 case DW_LANG_C99: printf ("(ANSI C99)"); break;
3130 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
3131 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
3132 /* DWARF 3 values. */
3133 case DW_LANG_PLI: printf ("(PLI)"); break;
3134 case DW_LANG_ObjC: printf ("(Objective C)"); break;
3135 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
3136 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
3137 case DW_LANG_D: printf ("(D)"); break;
3138 /* DWARF 4 values. */
3139 case DW_LANG_Python: printf ("(Python)"); break;
3140 /* DWARF 5 values. */
3141 case DW_LANG_OpenCL: printf ("(OpenCL)"); break;
3142 case DW_LANG_Go: printf ("(Go)"); break;
3143 case DW_LANG_Modula3: printf ("(Modula 3)"); break;
3144 case DW_LANG_Haskell: printf ("(Haskell)"); break;
3145 case DW_LANG_C_plus_plus_03: printf ("(C++03)"); break;
3146 case DW_LANG_C_plus_plus_11: printf ("(C++11)"); break;
3147 case DW_LANG_OCaml: printf ("(OCaml)"); break;
3148 case DW_LANG_Rust: printf ("(Rust)"); break;
3149 case DW_LANG_C11: printf ("(C11)"); break;
3150 case DW_LANG_Swift: printf ("(Swift)"); break;
3151 case DW_LANG_Julia: printf ("(Julia)"); break;
3152 case DW_LANG_Dylan: printf ("(Dylan)"); break;
3153 case DW_LANG_C_plus_plus_14: printf ("(C++14)"); break;
3154 case DW_LANG_Fortran03: printf ("(Fortran 03)"); break;
3155 case DW_LANG_Fortran08: printf ("(Fortran 08)"); break;
3156 case DW_LANG_RenderScript: printf ("(RenderScript)"); break;
3157 /* MIPS extension. */
3158 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
3159 /* UPC extension. */
3160 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
3161 default:
3162 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
3163 printf (_("(implementation defined: %#" PRIx64 ")"), uvalue);
3164 else
3165 printf (_("(unknown: %#" PRIx64 ")"), uvalue);
3166 break;
3168 break;
3170 case DW_AT_encoding:
3171 printf ("\t");
3172 switch (uvalue)
3174 case DW_ATE_void: printf ("(void)"); break;
3175 case DW_ATE_address: printf ("(machine address)"); break;
3176 case DW_ATE_boolean: printf ("(boolean)"); break;
3177 case DW_ATE_complex_float: printf ("(complex float)"); break;
3178 case DW_ATE_float: printf ("(float)"); break;
3179 case DW_ATE_signed: printf ("(signed)"); break;
3180 case DW_ATE_signed_char: printf ("(signed char)"); break;
3181 case DW_ATE_unsigned: printf ("(unsigned)"); break;
3182 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
3183 /* DWARF 2.1 values: */
3184 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
3185 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
3186 /* DWARF 3 values: */
3187 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
3188 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
3189 case DW_ATE_edited: printf ("(edited)"); break;
3190 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
3191 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
3192 /* DWARF 4 values: */
3193 case DW_ATE_UTF: printf ("(unicode string)"); break;
3194 /* DWARF 5 values: */
3195 case DW_ATE_UCS: printf ("(UCS)"); break;
3196 case DW_ATE_ASCII: printf ("(ASCII)"); break;
3198 /* HP extensions: */
3199 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
3200 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
3201 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
3202 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
3203 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
3204 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
3205 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
3207 default:
3208 if (uvalue >= DW_ATE_lo_user
3209 && uvalue <= DW_ATE_hi_user)
3210 printf (_("(user defined type)"));
3211 else
3212 printf (_("(unknown type)"));
3213 break;
3215 break;
3217 case DW_AT_accessibility:
3218 printf ("\t");
3219 switch (uvalue)
3221 case DW_ACCESS_public: printf ("(public)"); break;
3222 case DW_ACCESS_protected: printf ("(protected)"); break;
3223 case DW_ACCESS_private: printf ("(private)"); break;
3224 default:
3225 printf (_("(unknown accessibility)"));
3226 break;
3228 break;
3230 case DW_AT_visibility:
3231 printf ("\t");
3232 switch (uvalue)
3234 case DW_VIS_local: printf ("(local)"); break;
3235 case DW_VIS_exported: printf ("(exported)"); break;
3236 case DW_VIS_qualified: printf ("(qualified)"); break;
3237 default: printf (_("(unknown visibility)")); break;
3239 break;
3241 case DW_AT_endianity:
3242 printf ("\t");
3243 switch (uvalue)
3245 case DW_END_default: printf ("(default)"); break;
3246 case DW_END_big: printf ("(big)"); break;
3247 case DW_END_little: printf ("(little)"); break;
3248 default:
3249 if (uvalue >= DW_END_lo_user && uvalue <= DW_END_hi_user)
3250 printf (_("(user specified)"));
3251 else
3252 printf (_("(unknown endianity)"));
3253 break;
3255 break;
3257 case DW_AT_virtuality:
3258 printf ("\t");
3259 switch (uvalue)
3261 case DW_VIRTUALITY_none: printf ("(none)"); break;
3262 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
3263 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
3264 default: printf (_("(unknown virtuality)")); break;
3266 break;
3268 case DW_AT_identifier_case:
3269 printf ("\t");
3270 switch (uvalue)
3272 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
3273 case DW_ID_up_case: printf ("(up_case)"); break;
3274 case DW_ID_down_case: printf ("(down_case)"); break;
3275 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
3276 default: printf (_("(unknown case)")); break;
3278 break;
3280 case DW_AT_calling_convention:
3281 printf ("\t");
3282 switch (uvalue)
3284 case DW_CC_normal: printf ("(normal)"); break;
3285 case DW_CC_program: printf ("(program)"); break;
3286 case DW_CC_nocall: printf ("(nocall)"); break;
3287 case DW_CC_pass_by_reference: printf ("(pass by ref)"); break;
3288 case DW_CC_pass_by_value: printf ("(pass by value)"); break;
3289 case DW_CC_GNU_renesas_sh: printf ("(Rensas SH)"); break;
3290 case DW_CC_GNU_borland_fastcall_i386: printf ("(Borland fastcall i386)"); break;
3291 default:
3292 if (uvalue >= DW_CC_lo_user
3293 && uvalue <= DW_CC_hi_user)
3294 printf (_("(user defined)"));
3295 else
3296 printf (_("(unknown convention)"));
3298 break;
3300 case DW_AT_ordering:
3301 printf ("\t");
3302 switch (uvalue)
3304 case 255:
3305 case -1: printf (_("(undefined)")); break;
3306 case 0: printf ("(row major)"); break;
3307 case 1: printf ("(column major)"); break;
3309 break;
3311 case DW_AT_decimal_sign:
3312 printf ("\t");
3313 switch (uvalue)
3315 case DW_DS_unsigned: printf (_("(unsigned)")); break;
3316 case DW_DS_leading_overpunch: printf (_("(leading overpunch)")); break;
3317 case DW_DS_trailing_overpunch: printf (_("(trailing overpunch)")); break;
3318 case DW_DS_leading_separate: printf (_("(leading separate)")); break;
3319 case DW_DS_trailing_separate: printf (_("(trailing separate)")); break;
3320 default: printf (_("(unrecognised)")); break;
3322 break;
3324 case DW_AT_defaulted:
3325 printf ("\t");
3326 switch (uvalue)
3328 case DW_DEFAULTED_no: printf (_("(no)")); break;
3329 case DW_DEFAULTED_in_class: printf (_("(in class)")); break;
3330 case DW_DEFAULTED_out_of_class: printf (_("(out of class)")); break;
3331 default: printf (_("(unrecognised)")); break;
3333 break;
3335 case DW_AT_discr_list:
3336 printf ("\t");
3337 display_discr_list (form, uvalue, data, level);
3338 break;
3340 case DW_AT_frame_base:
3341 have_frame_base = 1;
3342 /* Fall through. */
3343 case DW_AT_location:
3344 case DW_AT_loclists_base:
3345 case DW_AT_rnglists_base:
3346 case DW_AT_str_offsets_base:
3347 case DW_AT_string_length:
3348 case DW_AT_return_addr:
3349 case DW_AT_data_member_location:
3350 case DW_AT_vtable_elem_location:
3351 case DW_AT_segment:
3352 case DW_AT_static_link:
3353 case DW_AT_use_location:
3354 case DW_AT_call_value:
3355 case DW_AT_GNU_call_site_value:
3356 case DW_AT_call_data_value:
3357 case DW_AT_GNU_call_site_data_value:
3358 case DW_AT_call_target:
3359 case DW_AT_GNU_call_site_target:
3360 case DW_AT_call_target_clobbered:
3361 case DW_AT_GNU_call_site_target_clobbered:
3362 if ((dwarf_version < 4
3363 && (form == DW_FORM_data4 || form == DW_FORM_data8))
3364 || form == DW_FORM_sec_offset
3365 || form == DW_FORM_loclistx)
3367 if (attribute != DW_AT_rnglists_base
3368 && attribute != DW_AT_str_offsets_base)
3369 printf (_(" (location list)"));
3371 /* Fall through. */
3372 case DW_AT_allocated:
3373 case DW_AT_associated:
3374 case DW_AT_data_location:
3375 case DW_AT_stride:
3376 case DW_AT_upper_bound:
3377 case DW_AT_lower_bound:
3378 case DW_AT_rank:
3379 if (block_start)
3381 int need_frame_base;
3383 printf ("\t(");
3384 need_frame_base = decode_location_expression (block_start,
3385 pointer_size,
3386 offset_size,
3387 dwarf_version,
3388 uvalue,
3389 cu_offset, section);
3390 printf (")");
3391 if (need_frame_base && !have_frame_base)
3392 printf (_(" [without DW_AT_frame_base]"));
3394 break;
3396 case DW_AT_data_bit_offset:
3397 case DW_AT_byte_size:
3398 case DW_AT_bit_size:
3399 case DW_AT_string_length_byte_size:
3400 case DW_AT_string_length_bit_size:
3401 case DW_AT_bit_stride:
3402 if (form == DW_FORM_exprloc)
3404 printf ("\t(");
3405 (void) decode_location_expression (block_start, pointer_size,
3406 offset_size, dwarf_version,
3407 uvalue, cu_offset, section);
3408 printf (")");
3410 break;
3412 case DW_AT_import:
3414 unsigned long abbrev_number;
3415 abbrev_entry *entry;
3417 entry = get_type_abbrev_from_form (form, uvalue, cu_offset, end,
3418 section, & abbrev_number, NULL, NULL);
3419 if (entry == NULL)
3421 if (form != DW_FORM_GNU_ref_alt)
3422 warn (_("Offset %#" PRIx64 " used as value for DW_AT_import attribute of DIE at offset %#tx is too big.\n"),
3423 uvalue,
3424 orig_data - section->start);
3426 else
3428 printf (_("\t[Abbrev Number: %ld"), abbrev_number);
3429 printf (" (%s)", get_TAG_name (entry->tag));
3430 printf ("]");
3433 break;
3435 default:
3436 break;
3439 return data;
3442 static unsigned char *
3443 read_and_display_attr (unsigned long attribute,
3444 unsigned long form,
3445 int64_t implicit_const,
3446 unsigned char *start,
3447 unsigned char *data,
3448 unsigned char *end,
3449 uint64_t cu_offset,
3450 uint64_t pointer_size,
3451 uint64_t offset_size,
3452 int dwarf_version,
3453 debug_info *debug_info_p,
3454 int do_loc,
3455 struct dwarf_section *section,
3456 struct cu_tu_set *this_set,
3457 int level)
3459 if (!do_loc)
3460 printf (" %-18s:", get_AT_name (attribute));
3461 data = read_and_display_attr_value (attribute, form, implicit_const,
3462 start, data, end,
3463 cu_offset, pointer_size, offset_size,
3464 dwarf_version, debug_info_p,
3465 do_loc, section, this_set, ' ', level);
3466 if (!do_loc)
3467 printf ("\n");
3468 return data;
3471 /* Like load_debug_section, but if the ordinary call fails, and we are
3472 following debug links, then attempt to load the requested section
3473 from one of the separate debug info files. */
3475 static bool
3476 load_debug_section_with_follow (enum dwarf_section_display_enum sec_enum,
3477 void * handle)
3479 if (load_debug_section (sec_enum, handle))
3481 if (debug_displays[sec_enum].section.filename == NULL)
3483 /* See if we can associate a filename with this section. */
3484 separate_info * i;
3486 for (i = first_separate_info; i != NULL; i = i->next)
3487 if (i->handle == handle)
3489 debug_displays[sec_enum].section.filename = i->filename;
3490 break;
3494 return true;
3497 if (do_follow_links)
3499 separate_info * i;
3501 for (i = first_separate_info; i != NULL; i = i->next)
3503 if (load_debug_section (sec_enum, i->handle))
3505 debug_displays[sec_enum].section.filename = i->filename;
3507 /* FIXME: We should check to see if any of the remaining debug info
3508 files also contain this section, and, umm, do something about it. */
3509 return true;
3514 return false;
3517 static void
3518 introduce (struct dwarf_section * section, bool raw)
3520 if (raw)
3522 if (do_follow_links && section->filename)
3523 printf (_("Raw dump of debug contents of section %s (loaded from %s):\n\n"),
3524 section->name, section->filename);
3525 else
3526 printf (_("Raw dump of debug contents of section %s:\n\n"), section->name);
3528 else
3530 if (do_follow_links && section->filename)
3531 printf (_("Contents of the %s section (loaded from %s):\n\n"),
3532 section->name, section->filename);
3533 else
3534 printf (_("Contents of the %s section:\n\n"), section->name);
3538 /* Free memory allocated for one unit in debug_information. */
3540 static void
3541 free_debug_information (debug_info *ent)
3543 if (ent->max_loc_offsets)
3545 free (ent->loc_offsets);
3546 free (ent->loc_views);
3547 free (ent->have_frame_base);
3549 if (ent->max_range_lists)
3551 free (ent->range_lists);
3555 /* For look-ahead in attributes. When you want to scan a DIE for one specific
3556 attribute and ignore the rest. */
3558 static unsigned char *
3559 skip_attribute (unsigned long form,
3560 unsigned char * data,
3561 unsigned char * end,
3562 uint64_t pointer_size,
3563 uint64_t offset_size,
3564 int dwarf_version)
3566 uint64_t temp;
3567 int64_t stemp;
3569 switch (form)
3571 case DW_FORM_ref_addr:
3572 data += dwarf_version == 2 ? pointer_size : offset_size;
3573 break;
3574 case DW_FORM_addr:
3575 data += pointer_size;
3576 break;
3577 case DW_FORM_strp_sup:
3578 case DW_FORM_strp:
3579 case DW_FORM_line_strp:
3580 case DW_FORM_sec_offset:
3581 case DW_FORM_GNU_ref_alt:
3582 case DW_FORM_GNU_strp_alt:
3583 data += offset_size;
3584 break;
3585 case DW_FORM_ref1:
3586 case DW_FORM_flag:
3587 case DW_FORM_data1:
3588 case DW_FORM_strx1:
3589 case DW_FORM_addrx1:
3590 data += 1;
3591 break;
3592 case DW_FORM_ref2:
3593 case DW_FORM_data2:
3594 case DW_FORM_strx2:
3595 case DW_FORM_addrx2:
3596 data += 2;
3597 break;
3598 case DW_FORM_strx3:
3599 case DW_FORM_addrx3:
3600 data += 3;
3601 break;
3602 case DW_FORM_ref_sup4:
3603 case DW_FORM_ref4:
3604 case DW_FORM_data4:
3605 case DW_FORM_strx4:
3606 case DW_FORM_addrx4:
3607 data += 4;
3608 break;
3609 case DW_FORM_ref_sup8:
3610 case DW_FORM_ref8:
3611 case DW_FORM_data8:
3612 case DW_FORM_ref_sig8:
3613 data += 8;
3614 break;
3615 case DW_FORM_data16:
3616 data += 16;
3617 break;
3618 case DW_FORM_sdata:
3619 READ_SLEB (stemp, data, end);
3620 break;
3621 case DW_FORM_GNU_str_index:
3622 case DW_FORM_strx:
3623 case DW_FORM_ref_udata:
3624 case DW_FORM_udata:
3625 case DW_FORM_GNU_addr_index:
3626 case DW_FORM_addrx:
3627 case DW_FORM_loclistx:
3628 case DW_FORM_rnglistx:
3629 READ_ULEB (temp, data, end);
3630 break;
3632 case DW_FORM_indirect:
3633 while (form == DW_FORM_indirect)
3634 READ_ULEB (form, data, end);
3635 return skip_attribute (form, data, end, pointer_size, offset_size, dwarf_version);
3637 case DW_FORM_string:
3638 data += strnlen ((char *) data, end - data);
3639 break;
3640 case DW_FORM_block:
3641 case DW_FORM_exprloc:
3642 READ_ULEB (temp, data, end);
3643 data += temp;
3644 break;
3645 case DW_FORM_block1:
3646 SAFE_BYTE_GET_AND_INC (temp, data, 1, end);
3647 data += temp;
3648 break;
3649 case DW_FORM_block2:
3650 SAFE_BYTE_GET_AND_INC (temp, data, 2, end);
3651 data += temp;
3652 break;
3653 case DW_FORM_block4:
3654 SAFE_BYTE_GET_AND_INC (temp, data, 4, end);
3655 data += temp;
3656 break;
3657 case DW_FORM_implicit_const:
3658 case DW_FORM_flag_present:
3659 break;
3660 default:
3661 warn (_("Unexpected form in top DIE\n"));
3662 break;
3664 return data;
3667 static void
3668 read_bases (abbrev_entry * entry,
3669 unsigned char * data,
3670 unsigned char * end,
3671 int64_t pointer_size,
3672 uint64_t offset_size,
3673 int dwarf_version,
3674 debug_info * debug_info_p)
3676 abbrev_attr *attr;
3678 for (attr = entry->first_attr;
3679 attr && attr->attribute;
3680 attr = attr->next)
3682 uint64_t uvalue;
3684 if (attr->attribute == DW_AT_rnglists_base)
3686 if (attr->form == DW_FORM_sec_offset)
3688 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
3689 debug_info_p->rnglists_base = uvalue;
3691 else
3692 warn (_("Unexpected form of DW_AT_rnglists_base in the top DIE\n"));
3694 else if(attr->attribute == DW_AT_addr_base || attr->attribute == DW_AT_GNU_addr_base)
3696 if (attr->form == DW_FORM_sec_offset)
3698 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
3699 debug_info_p->addr_base = uvalue;
3701 else
3702 warn (_("Unexpected form of DW_AT_addr_base in the top DIE\n"));
3704 else
3705 data = skip_attribute(attr->form, data, end, pointer_size, offset_size, dwarf_version);
3709 /* Process the contents of a .debug_info section.
3710 If do_loc is TRUE then we are scanning for location lists and dwo tags
3711 and we do not want to display anything to the user.
3712 If do_types is TRUE, we are processing a .debug_types section instead of
3713 a .debug_info section.
3714 The information displayed is restricted by the values in DWARF_START_DIE
3715 and DWARF_CUTOFF_LEVEL.
3716 Returns TRUE upon success. Otherwise an error or warning message is
3717 printed and FALSE is returned. */
3719 static bool
3720 process_debug_info (struct dwarf_section * section,
3721 void *file,
3722 enum dwarf_section_display_enum abbrev_sec,
3723 bool do_loc,
3724 bool do_types)
3726 unsigned char *start = section->start;
3727 unsigned char *end = start + section->size;
3728 unsigned char *section_begin;
3729 unsigned int unit;
3730 unsigned int num_units = 0;
3732 /* First scan the section to get the number of comp units.
3733 Length sanity checks are done here. */
3734 for (section_begin = start, num_units = 0; section_begin < end;
3735 num_units ++)
3737 uint64_t length;
3739 /* Read the first 4 bytes. For a 32-bit DWARF section, this
3740 will be the length. For a 64-bit DWARF section, it'll be
3741 the escape code 0xffffffff followed by an 8 byte length. */
3742 SAFE_BYTE_GET_AND_INC (length, section_begin, 4, end);
3744 if (length == 0xffffffff)
3745 SAFE_BYTE_GET_AND_INC (length, section_begin, 8, end);
3746 else if (length >= 0xfffffff0 && length < 0xffffffff)
3748 warn (_("Reserved length value (%#" PRIx64 ") found in section %s\n"),
3749 length, section->name);
3750 return false;
3753 /* Negative values are illegal, they may even cause infinite
3754 looping. This can happen if we can't accurately apply
3755 relocations to an object file, or if the file is corrupt. */
3756 if (length > (size_t) (end - section_begin))
3758 warn (_("Corrupt unit length (got %#" PRIx64
3759 " expected at most %#tx) in section %s\n"),
3760 length, end - section_begin, section->name);
3761 return false;
3763 section_begin += length;
3766 if (num_units == 0)
3768 error (_("No comp units in %s section ?\n"), section->name);
3769 return false;
3772 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
3773 && num_debug_info_entries == 0
3774 && ! do_types)
3777 /* Then allocate an array to hold the information. */
3778 debug_information = (debug_info *) cmalloc (num_units,
3779 sizeof (* debug_information));
3780 if (debug_information == NULL)
3782 error (_("Not enough memory for a debug info array of %u entries\n"),
3783 num_units);
3784 alloc_num_debug_info_entries = num_debug_info_entries = 0;
3785 return false;
3788 /* PR 17531: file: 92ca3797.
3789 We cannot rely upon the debug_information array being initialised
3790 before it is used. A corrupt file could easily contain references
3791 to a unit for which information has not been made available. So
3792 we ensure that the array is zeroed here. */
3793 memset (debug_information, 0, num_units * sizeof (*debug_information));
3795 alloc_num_debug_info_entries = num_units;
3798 if (!do_loc)
3800 load_debug_section_with_follow (str, file);
3801 load_debug_section_with_follow (line_str, file);
3802 load_debug_section_with_follow (str_dwo, file);
3803 load_debug_section_with_follow (str_index, file);
3804 load_debug_section_with_follow (str_index_dwo, file);
3805 load_debug_section_with_follow (debug_addr, file);
3808 load_debug_section_with_follow (abbrev_sec, file);
3809 load_debug_section_with_follow (loclists, file);
3810 load_debug_section_with_follow (rnglists, file);
3811 load_debug_section_with_follow (loclists_dwo, file);
3812 load_debug_section_with_follow (rnglists_dwo, file);
3814 if (debug_displays [abbrev_sec].section.start == NULL)
3816 warn (_("Unable to locate %s section!\n"),
3817 debug_displays [abbrev_sec].section.uncompressed_name);
3818 return false;
3821 if (!do_loc && dwarf_start_die == 0)
3822 introduce (section, false);
3824 free_all_abbrevs ();
3826 /* In order to be able to resolve DW_FORM_ref_addr forms we need
3827 to load *all* of the abbrevs for all CUs in this .debug_info
3828 section. This does effectively mean that we (partially) read
3829 every CU header twice. */
3830 for (section_begin = start; start < end;)
3832 DWARF2_Internal_CompUnit compunit;
3833 unsigned char *hdrptr;
3834 uint64_t abbrev_base;
3835 size_t abbrev_size;
3836 uint64_t cu_offset;
3837 unsigned int offset_size;
3838 struct cu_tu_set *this_set;
3839 unsigned char *end_cu;
3841 hdrptr = start;
3842 cu_offset = start - section_begin;
3844 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3846 if (compunit.cu_length == 0xffffffff)
3848 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3849 offset_size = 8;
3851 else
3852 offset_size = 4;
3853 end_cu = hdrptr + compunit.cu_length;
3855 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3857 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3859 if (compunit.cu_version < 5)
3861 compunit.cu_unit_type = DW_UT_compile;
3862 /* Initialize it due to a false compiler warning. */
3863 compunit.cu_pointer_size = -1;
3865 else
3867 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3868 do_types = (compunit.cu_unit_type == DW_UT_type);
3870 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3873 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size,
3874 end_cu);
3876 if (compunit.cu_unit_type == DW_UT_split_compile
3877 || compunit.cu_unit_type == DW_UT_skeleton)
3879 uint64_t dwo_id;
3880 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3883 if (this_set == NULL)
3885 abbrev_base = 0;
3886 abbrev_size = debug_displays [abbrev_sec].section.size;
3888 else
3890 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3891 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3894 abbrev_list *list;
3895 abbrev_list *free_list;
3896 list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
3897 abbrev_base, abbrev_size,
3898 compunit.cu_abbrev_offset,
3899 &free_list);
3900 start = end_cu;
3901 if (list != NULL && list->first_abbrev != NULL)
3902 record_abbrev_list_for_cu (cu_offset, start - section_begin,
3903 list, free_list);
3904 else if (free_list != NULL)
3905 free_abbrev_list (free_list);
3908 for (start = section_begin, unit = 0; start < end; unit++)
3910 DWARF2_Internal_CompUnit compunit;
3911 unsigned char *hdrptr;
3912 unsigned char *tags;
3913 int level, last_level, saved_level;
3914 uint64_t cu_offset;
3915 unsigned int offset_size;
3916 uint64_t signature = 0;
3917 uint64_t type_offset = 0;
3918 struct cu_tu_set *this_set;
3919 uint64_t abbrev_base;
3920 size_t abbrev_size;
3921 unsigned char *end_cu;
3923 hdrptr = start;
3924 cu_offset = start - section_begin;
3926 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3928 if (compunit.cu_length == 0xffffffff)
3930 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3931 offset_size = 8;
3933 else
3934 offset_size = 4;
3935 end_cu = hdrptr + compunit.cu_length;
3937 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3939 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3941 if (compunit.cu_version < 5)
3943 compunit.cu_unit_type = DW_UT_compile;
3944 /* Initialize it due to a false compiler warning. */
3945 compunit.cu_pointer_size = -1;
3947 else
3949 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3950 do_types = (compunit.cu_unit_type == DW_UT_type);
3952 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3955 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end_cu);
3957 if (this_set == NULL)
3959 abbrev_base = 0;
3960 abbrev_size = debug_displays [abbrev_sec].section.size;
3962 else
3964 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3965 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3968 if (compunit.cu_version < 5)
3969 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3971 bool do_dwo_id = false;
3972 uint64_t dwo_id = 0;
3973 if (compunit.cu_unit_type == DW_UT_split_compile
3974 || compunit.cu_unit_type == DW_UT_skeleton)
3976 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3977 do_dwo_id = true;
3980 /* PR 17512: file: 001-108546-0.001:0.1. */
3981 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3983 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3984 compunit.cu_pointer_size, offset_size);
3985 compunit.cu_pointer_size = offset_size;
3988 if (do_types)
3990 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, end_cu);
3991 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end_cu);
3994 if (dwarf_start_die >= (size_t) (end_cu - section_begin))
3996 start = end_cu;
3997 continue;
4000 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
4001 && num_debug_info_entries == 0
4002 && alloc_num_debug_info_entries > unit
4003 && ! do_types)
4005 free_debug_information (&debug_information[unit]);
4006 memset (&debug_information[unit], 0, sizeof (*debug_information));
4007 debug_information[unit].pointer_size = compunit.cu_pointer_size;
4008 debug_information[unit].offset_size = offset_size;
4009 debug_information[unit].dwarf_version = compunit.cu_version;
4010 debug_information[unit].cu_offset = cu_offset;
4011 debug_information[unit].addr_base = DEBUG_INFO_UNAVAILABLE;
4012 debug_information[unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
4015 if (!do_loc && dwarf_start_die == 0)
4017 printf (_(" Compilation Unit @ offset %#" PRIx64 ":\n"),
4018 cu_offset);
4019 printf (_(" Length: %#" PRIx64 " (%s)\n"),
4020 compunit.cu_length,
4021 offset_size == 8 ? "64-bit" : "32-bit");
4022 printf (_(" Version: %d\n"), compunit.cu_version);
4023 if (compunit.cu_version >= 5)
4025 const char *name = get_DW_UT_name (compunit.cu_unit_type);
4027 printf (_(" Unit Type: %s (%x)\n"),
4028 null_name (name),
4029 compunit.cu_unit_type);
4031 printf (_(" Abbrev Offset: %#" PRIx64 "\n"),
4032 compunit.cu_abbrev_offset);
4033 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
4034 if (do_types)
4036 printf (_(" Signature: %#" PRIx64 "\n"), signature);
4037 printf (_(" Type Offset: %#" PRIx64 "\n"), type_offset);
4039 if (do_dwo_id)
4040 printf (_(" DWO ID: %#" PRIx64 "\n"), dwo_id);
4041 if (this_set != NULL)
4043 uint64_t *offsets = this_set->section_offsets;
4044 size_t *sizes = this_set->section_sizes;
4046 printf (_(" Section contributions:\n"));
4047 printf (_(" .debug_abbrev.dwo: %#" PRIx64 " %#zx\n"),
4048 offsets[DW_SECT_ABBREV], sizes[DW_SECT_ABBREV]);
4049 printf (_(" .debug_line.dwo: %#" PRIx64 " %#zx\n"),
4050 offsets[DW_SECT_LINE], sizes[DW_SECT_LINE]);
4051 printf (_(" .debug_loc.dwo: %#" PRIx64 " %#zx\n"),
4052 offsets[DW_SECT_LOC], sizes[DW_SECT_LOC]);
4053 printf (_(" .debug_str_offsets.dwo: %#" PRIx64 " %#zx\n"),
4054 offsets[DW_SECT_STR_OFFSETS], sizes[DW_SECT_STR_OFFSETS]);
4058 tags = hdrptr;
4059 start = end_cu;
4061 if (compunit.cu_version < 2 || compunit.cu_version > 5)
4063 warn (_("CU at offset %#" PRIx64 " contains corrupt or "
4064 "unsupported version number: %d.\n"),
4065 cu_offset, compunit.cu_version);
4066 continue;
4069 if (compunit.cu_unit_type != DW_UT_compile
4070 && compunit.cu_unit_type != DW_UT_partial
4071 && compunit.cu_unit_type != DW_UT_type
4072 && compunit.cu_unit_type != DW_UT_split_compile
4073 && compunit.cu_unit_type != DW_UT_skeleton)
4075 warn (_("CU at offset %#" PRIx64 " contains corrupt or "
4076 "unsupported unit type: %d.\n"),
4077 cu_offset, compunit.cu_unit_type);
4078 continue;
4081 /* Process the abbrevs used by this compilation unit. */
4082 abbrev_list *list;
4083 list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
4084 abbrev_base, abbrev_size,
4085 compunit.cu_abbrev_offset, NULL);
4086 level = 0;
4087 last_level = level;
4088 saved_level = -1;
4089 while (tags < start)
4091 unsigned long abbrev_number;
4092 unsigned long die_offset;
4093 abbrev_entry *entry;
4094 abbrev_attr *attr;
4095 int do_printing = 1;
4097 die_offset = tags - section_begin;
4099 READ_ULEB (abbrev_number, tags, start);
4101 /* A null DIE marks the end of a list of siblings or it may also be
4102 a section padding. */
4103 if (abbrev_number == 0)
4105 /* Check if it can be a section padding for the last CU. */
4106 if (level == 0 && start == end)
4108 unsigned char *chk;
4110 for (chk = tags; chk < start; chk++)
4111 if (*chk != 0)
4112 break;
4113 if (chk == start)
4114 break;
4117 if (!do_loc && die_offset >= dwarf_start_die
4118 && (dwarf_cutoff_level == -1
4119 || level < dwarf_cutoff_level))
4120 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
4121 level, die_offset);
4123 --level;
4124 if (level < 0)
4126 static unsigned num_bogus_warns = 0;
4128 if (num_bogus_warns < 3)
4130 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
4131 die_offset, section->name);
4132 num_bogus_warns ++;
4133 if (num_bogus_warns == 3)
4134 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
4137 if (dwarf_start_die != 0 && level < saved_level)
4139 if (list != NULL)
4140 free_abbrev_list (list);
4141 return true;
4143 continue;
4146 if (!do_loc)
4148 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
4149 do_printing = 0;
4150 else
4152 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
4153 saved_level = level;
4154 do_printing = (dwarf_cutoff_level == -1
4155 || level < dwarf_cutoff_level);
4156 if (do_printing)
4157 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
4158 level, die_offset, abbrev_number);
4159 else if (dwarf_cutoff_level == -1
4160 || last_level < dwarf_cutoff_level)
4161 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
4162 last_level = level;
4166 /* Scan through the abbreviation list until we reach the
4167 correct entry. */
4168 entry = NULL;
4169 if (list != NULL)
4170 for (entry = list->first_abbrev; entry != NULL; entry = entry->next)
4171 if (entry->number == abbrev_number)
4172 break;
4174 if (entry == NULL)
4176 if (!do_loc && do_printing)
4178 printf ("\n");
4179 fflush (stdout);
4181 warn (_("DIE at offset %#lx refers to abbreviation number %lu which does not exist\n"),
4182 die_offset, abbrev_number);
4183 if (list != NULL)
4184 free_abbrev_list (list);
4185 return false;
4188 if (!do_loc && do_printing)
4189 printf (" (%s)\n", get_TAG_name (entry->tag));
4191 switch (entry->tag)
4193 default:
4194 need_base_address = 0;
4195 break;
4196 case DW_TAG_compile_unit:
4197 case DW_TAG_skeleton_unit:
4198 need_base_address = 1;
4199 need_dwo_info = do_loc;
4200 break;
4201 case DW_TAG_entry_point:
4202 case DW_TAG_subprogram:
4203 need_base_address = 0;
4204 /* Assuming that there is no DW_AT_frame_base. */
4205 have_frame_base = 0;
4206 break;
4209 debug_info *debug_info_p =
4210 (debug_information && unit < alloc_num_debug_info_entries)
4211 ? debug_information + unit : NULL;
4213 assert (!debug_info_p
4214 || (debug_info_p->num_loc_offsets
4215 == debug_info_p->num_loc_views));
4217 /* Look ahead so that the values of DW_AT_rnglists_base, DW_AT_[GNU_]addr_base
4218 are available before attributes that reference them are parsed in the same DIE.
4219 Only needed for the top DIE on DWARFv5+.
4220 No simiar treatment for loclists_base because there should be no loclist
4221 attributes in top DIE. */
4222 if (compunit.cu_version >= 5 && level == 0)
4224 int64_t stemp;
4226 read_bases (entry,
4227 tags,
4228 start,
4229 compunit.cu_pointer_size,
4230 offset_size,
4231 compunit.cu_version,
4232 debug_info_p);
4234 /* This check was in place before, keep it. */
4235 stemp = debug_info_p->rnglists_base;
4236 if (stemp < 0)
4238 warn (_("CU @ %#" PRIx64 " has has a negative rnglists_base "
4239 "value of %#" PRIx64 " - treating as zero"),
4240 debug_info_p->cu_offset, stemp);
4241 debug_info_p->rnglists_base = 0;
4245 for (attr = entry->first_attr;
4246 attr && attr->attribute;
4247 attr = attr->next)
4249 if (! do_loc && do_printing)
4250 /* Show the offset from where the tag was extracted. */
4251 printf (" <%tx>", tags - section_begin);
4252 tags = read_and_display_attr (attr->attribute,
4253 attr->form,
4254 attr->implicit_const,
4255 section_begin,
4256 tags,
4257 start,
4258 cu_offset,
4259 compunit.cu_pointer_size,
4260 offset_size,
4261 compunit.cu_version,
4262 debug_info_p,
4263 do_loc || ! do_printing,
4264 section,
4265 this_set,
4266 level);
4269 /* If a locview attribute appears before a location one,
4270 make sure we don't associate it with an earlier
4271 loclist. */
4272 if (debug_info_p)
4273 switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
4275 case 1:
4276 debug_info_p->loc_views [debug_info_p->num_loc_views] = -1;
4277 debug_info_p->num_loc_views++;
4278 assert (debug_info_p->num_loc_views
4279 == debug_info_p->num_loc_offsets);
4280 break;
4282 case 0:
4283 break;
4285 case -1:
4286 warn(_("DIE has locviews without loclist\n"));
4287 debug_info_p->num_loc_views--;
4288 break;
4290 default:
4291 assert (0);
4294 if (entry->children)
4295 ++level;
4297 if (list != NULL)
4298 free_abbrev_list (list);
4301 /* Set num_debug_info_entries here so that it can be used to check if
4302 we need to process .debug_loc and .debug_ranges sections. */
4303 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
4304 && num_debug_info_entries == 0
4305 && ! do_types)
4307 if (num_units > alloc_num_debug_info_entries)
4308 num_debug_info_entries = alloc_num_debug_info_entries;
4309 else
4310 num_debug_info_entries = num_units;
4313 if (!do_loc)
4314 printf ("\n");
4316 return true;
4319 /* Locate and scan the .debug_info section in the file and record the pointer
4320 sizes and offsets for the compilation units in it. Usually an executable
4321 will have just one pointer size, but this is not guaranteed, and so we try
4322 not to make any assumptions. Returns zero upon failure, or the number of
4323 compilation units upon success. */
4325 static unsigned int
4326 load_debug_info (void * file)
4328 /* If we have already tried and failed to load the .debug_info
4329 section then do not bother to repeat the task. */
4330 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4331 return 0;
4333 /* If we already have the information there is nothing else to do. */
4334 if (num_debug_info_entries > 0)
4335 return num_debug_info_entries;
4337 /* If this is a DWARF package file, load the CU and TU indexes. */
4338 (void) load_cu_tu_indexes (file);
4340 if (load_debug_section_with_follow (info, file)
4341 && process_debug_info (&debug_displays [info].section, file, abbrev, true, false))
4342 return num_debug_info_entries;
4344 if (load_debug_section_with_follow (info_dwo, file)
4345 && process_debug_info (&debug_displays [info_dwo].section, file,
4346 abbrev_dwo, true, false))
4347 return num_debug_info_entries;
4349 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
4350 return 0;
4353 /* Read a DWARF .debug_line section header starting at DATA.
4354 Upon success returns an updated DATA pointer and the LINFO
4355 structure and the END_OF_SEQUENCE pointer will be filled in.
4356 Otherwise returns NULL. */
4358 static unsigned char *
4359 read_debug_line_header (struct dwarf_section * section,
4360 unsigned char * data,
4361 unsigned char * end,
4362 DWARF2_Internal_LineInfo * linfo,
4363 unsigned char ** end_of_sequence)
4365 unsigned char *hdrptr;
4367 /* Extract information from the Line Number Program Header.
4368 (section 6.2.4 in the Dwarf3 doc). */
4369 hdrptr = data;
4371 /* Get and check the length of the block. */
4372 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
4374 if (linfo->li_length == 0xffffffff)
4376 /* This section is 64-bit DWARF 3. */
4377 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
4378 linfo->li_offset_size = 8;
4380 else
4381 linfo->li_offset_size = 4;
4383 if (linfo->li_length > (size_t) (end - hdrptr))
4385 /* If the length field has a relocation against it, then we should
4386 not complain if it is inaccurate (and probably negative). This
4387 happens in object files when the .debug_line section is actually
4388 comprised of several different .debug_line.* sections, (some of
4389 which may be removed by linker garbage collection), and a relocation
4390 is used to compute the correct length once that is done. */
4391 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
4393 linfo->li_length = end - hdrptr;
4395 else
4397 warn (_("The length field (%#" PRIx64 ")"
4398 " in the debug_line header is wrong"
4399 " - the section is too small\n"),
4400 linfo->li_length);
4401 return NULL;
4404 end = hdrptr + linfo->li_length;
4406 /* Get and check the version number. */
4407 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
4409 if (linfo->li_version != 2
4410 && linfo->li_version != 3
4411 && linfo->li_version != 4
4412 && linfo->li_version != 5)
4414 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
4415 "is currently supported.\n"));
4416 return NULL;
4419 if (linfo->li_version >= 5)
4421 SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
4423 SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
4424 if (linfo->li_segment_size != 0)
4426 warn (_("The %s section contains "
4427 "unsupported segment selector size: %d.\n"),
4428 section->name, linfo->li_segment_size);
4429 return NULL;
4433 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
4434 linfo->li_offset_size, end);
4435 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
4437 if (linfo->li_version >= 4)
4439 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
4441 if (linfo->li_max_ops_per_insn == 0)
4443 warn (_("Invalid maximum operations per insn.\n"));
4444 return NULL;
4447 else
4448 linfo->li_max_ops_per_insn = 1;
4450 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
4451 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
4452 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
4453 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
4455 *end_of_sequence = end;
4456 return hdrptr;
4459 static unsigned char *
4460 display_formatted_table (unsigned char *data,
4461 unsigned char *start,
4462 unsigned char *end,
4463 const DWARF2_Internal_LineInfo *linfo,
4464 struct dwarf_section *section,
4465 bool is_dir)
4467 unsigned char *format_start, format_count, *format, formati;
4468 uint64_t data_count, datai;
4469 unsigned int namepass, last_entry = 0;
4470 const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
4472 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4473 if (do_checks && format_count > 5)
4474 warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
4475 table_name, format_count);
4477 format_start = data;
4478 for (formati = 0; formati < format_count; formati++)
4480 SKIP_ULEB (data, end);
4481 SKIP_ULEB (data, end);
4482 if (data >= end)
4484 warn (_("%s: Corrupt format description entry\n"), table_name);
4485 return data;
4489 READ_ULEB (data_count, data, end);
4490 if (data_count == 0)
4492 printf (_("\n The %s is empty.\n"), table_name);
4493 return data;
4495 else if (data >= end
4496 || data_count > (size_t) (end - data))
4498 warn (_("%s: Corrupt entry count %#" PRIx64 "\n"), table_name, data_count);
4499 return data;
4502 else if (format_count == 0)
4504 warn (_("%s: format count is zero, but the table is not empty\n"),
4505 table_name);
4506 return end;
4509 printf (_("\n The %s (offset %#tx, lines %" PRIu64 ", columns %u):\n"),
4510 table_name, data - start, data_count, format_count);
4512 printf (_(" Entry"));
4513 /* Delay displaying name as the last entry for better screen layout. */
4514 for (namepass = 0; namepass < 2; namepass++)
4516 format = format_start;
4517 for (formati = 0; formati < format_count; formati++)
4519 uint64_t content_type;
4521 READ_ULEB (content_type, format, end);
4522 if ((content_type == DW_LNCT_path) == (namepass == 1))
4523 switch (content_type)
4525 case DW_LNCT_path:
4526 printf (_("\tName"));
4527 break;
4528 case DW_LNCT_directory_index:
4529 printf (_("\tDir"));
4530 break;
4531 case DW_LNCT_timestamp:
4532 printf (_("\tTime"));
4533 break;
4534 case DW_LNCT_size:
4535 printf (_("\tSize"));
4536 break;
4537 case DW_LNCT_MD5:
4538 printf (_("\tMD5\t\t\t"));
4539 break;
4540 default:
4541 printf (_("\t(Unknown format content type %" PRIu64 ")"),
4542 content_type);
4544 SKIP_ULEB (format, end);
4547 putchar ('\n');
4549 for (datai = 0; datai < data_count; datai++)
4551 unsigned char *datapass = data;
4553 printf (" %d", last_entry++);
4554 /* Delay displaying name as the last entry for better screen layout. */
4555 for (namepass = 0; namepass < 2; namepass++)
4557 format = format_start;
4558 data = datapass;
4559 for (formati = 0; formati < format_count; formati++)
4561 uint64_t content_type, form;
4563 READ_ULEB (content_type, format, end);
4564 READ_ULEB (form, format, end);
4565 data = read_and_display_attr_value (0, form, 0, start, data, end,
4566 0, 0, linfo->li_offset_size,
4567 linfo->li_version, NULL,
4568 ((content_type == DW_LNCT_path) != (namepass == 1)),
4569 section, NULL, '\t', -1);
4573 if (data >= end && (datai < data_count - 1))
4575 warn (_("\n%s: Corrupt entries list\n"), table_name);
4576 return data;
4578 putchar ('\n');
4580 return data;
4583 static int
4584 display_debug_sup (struct dwarf_section * section,
4585 void * file ATTRIBUTE_UNUSED)
4587 unsigned char * start = section->start;
4588 unsigned char * end = section->start + section->size;
4589 unsigned int version;
4590 char is_supplementary;
4591 const unsigned char * sup_filename;
4592 size_t sup_filename_len;
4593 unsigned int num_read;
4594 int status;
4595 uint64_t checksum_len;
4598 introduce (section, true);
4599 if (section->size < 4)
4601 error (_("corrupt .debug_sup section: size is too small\n"));
4602 return 0;
4605 /* Read the data. */
4606 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
4607 if (version < 5)
4608 warn (_("corrupt .debug_sup section: version < 5"));
4610 SAFE_BYTE_GET_AND_INC (is_supplementary, start, 1, end);
4611 if (is_supplementary != 0 && is_supplementary != 1)
4612 warn (_("corrupt .debug_sup section: is_supplementary not 0 or 1\n"));
4614 sup_filename = start;
4615 if (is_supplementary && sup_filename[0] != 0)
4616 warn (_("corrupt .debug_sup section: filename not empty in supplementary section\n"));
4618 sup_filename_len = strnlen ((const char *) start, end - start);
4619 if (sup_filename_len == (size_t) (end - start))
4621 error (_("corrupt .debug_sup section: filename is not NUL terminated\n"));
4622 return 0;
4624 start += sup_filename_len + 1;
4626 checksum_len = read_leb128 (start, end, false /* unsigned */, & num_read, & status);
4627 if (status)
4629 error (_("corrupt .debug_sup section: bad LEB128 field for checksum length\n"));
4630 checksum_len = 0;
4632 start += num_read;
4633 if (checksum_len > (size_t) (end - start))
4635 error (_("corrupt .debug_sup section: checksum length is longer than the remaining section length\n"));
4636 checksum_len = end - start;
4638 else if (checksum_len < (size_t) (end - start))
4640 warn (_("corrupt .debug_sup section: there are %#" PRIx64
4641 " extra, unused bytes at the end of the section\n"),
4642 (end - start) - checksum_len);
4645 printf (_(" Version: %u\n"), version);
4646 printf (_(" Is Supp: %u\n"), is_supplementary);
4647 printf (_(" Filename: %s\n"), sup_filename);
4648 printf (_(" Checksum Len: %" PRIu64 "\n"), checksum_len);
4649 if (checksum_len > 0)
4651 printf (_(" Checksum: "));
4652 while (checksum_len--)
4653 printf ("0x%x ", * start++ );
4654 printf ("\n");
4656 return 1;
4659 static int
4660 display_debug_lines_raw (struct dwarf_section * section,
4661 unsigned char * data,
4662 unsigned char * end,
4663 void * file)
4665 unsigned char *start = section->start;
4666 int verbose_view = 0;
4668 introduce (section, true);
4670 while (data < end)
4672 static DWARF2_Internal_LineInfo saved_linfo;
4673 DWARF2_Internal_LineInfo linfo;
4674 unsigned char *standard_opcodes;
4675 unsigned char *end_of_sequence;
4676 int i;
4678 if (startswith (section->name, ".debug_line.")
4679 /* Note: the following does not apply to .debug_line.dwo sections.
4680 These are full debug_line sections. */
4681 && strcmp (section->name, ".debug_line.dwo") != 0)
4683 /* Sections named .debug_line.<foo> are fragments of a .debug_line
4684 section containing just the Line Number Statements. They are
4685 created by the assembler and intended to be used alongside gcc's
4686 -ffunction-sections command line option. When the linker's
4687 garbage collection decides to discard a .text.<foo> section it
4688 can then also discard the line number information in .debug_line.<foo>.
4690 Since the section is a fragment it does not have the details
4691 needed to fill out a LineInfo structure, so instead we use the
4692 details from the last full debug_line section that we processed. */
4693 end_of_sequence = end;
4694 standard_opcodes = NULL;
4695 linfo = saved_linfo;
4696 /* PR 17531: file: 0522b371. */
4697 if (linfo.li_line_range == 0)
4699 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4700 return 0;
4702 reset_state_machine (linfo.li_default_is_stmt);
4704 else
4706 unsigned char * hdrptr;
4708 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4709 & end_of_sequence)) == NULL)
4710 return 0;
4712 printf (_(" Offset: %#tx\n"), data - start);
4713 printf (_(" Length: %" PRId64 "\n"), linfo.li_length);
4714 printf (_(" DWARF Version: %d\n"), linfo.li_version);
4715 if (linfo.li_version >= 5)
4717 printf (_(" Address size (bytes): %d\n"), linfo.li_address_size);
4718 printf (_(" Segment selector (bytes): %d\n"), linfo.li_segment_size);
4720 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
4721 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
4722 if (linfo.li_version >= 4)
4723 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
4724 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
4725 printf (_(" Line Base: %d\n"), linfo.li_line_base);
4726 printf (_(" Line Range: %d\n"), linfo.li_line_range);
4727 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
4729 /* PR 17512: file: 1665-6428-0.004. */
4730 if (linfo.li_line_range == 0)
4732 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4733 linfo.li_line_range = 1;
4736 reset_state_machine (linfo.li_default_is_stmt);
4738 /* Display the contents of the Opcodes table. */
4739 standard_opcodes = hdrptr;
4741 /* PR 17512: file: 002-417945-0.004. */
4742 if (standard_opcodes + linfo.li_opcode_base >= end)
4744 warn (_("Line Base extends beyond end of section\n"));
4745 return 0;
4748 printf (_("\n Opcodes:\n"));
4750 for (i = 1; i < linfo.li_opcode_base; i++)
4751 printf (ngettext (" Opcode %d has %d arg\n",
4752 " Opcode %d has %d args\n",
4753 standard_opcodes[i - 1]),
4754 i, standard_opcodes[i - 1]);
4756 /* Display the contents of the Directory table. */
4757 data = standard_opcodes + linfo.li_opcode_base - 1;
4759 if (linfo.li_version >= 5)
4761 load_debug_section_with_follow (line_str, file);
4763 data = display_formatted_table (data, start, end, &linfo, section,
4764 true);
4765 data = display_formatted_table (data, start, end, &linfo, section,
4766 false);
4768 else
4770 if (*data == 0)
4771 printf (_("\n The Directory Table is empty.\n"));
4772 else
4774 unsigned int last_dir_entry = 0;
4776 printf (_("\n The Directory Table (offset %#tx):\n"),
4777 data - start);
4779 while (data < end && *data != 0)
4781 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
4783 data += strnlen ((char *) data, end - data);
4784 if (data < end)
4785 data++;
4788 /* PR 17512: file: 002-132094-0.004. */
4789 if (data >= end - 1)
4790 break;
4793 /* Skip the NUL at the end of the table. */
4794 if (data < end)
4795 data++;
4797 /* Display the contents of the File Name table. */
4798 if (data >= end || *data == 0)
4799 printf (_("\n The File Name Table is empty.\n"));
4800 else
4802 printf (_("\n The File Name Table (offset %#tx):\n"),
4803 data - start);
4804 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
4806 while (data < end && *data != 0)
4808 unsigned char *name;
4809 uint64_t val;
4811 printf (" %d\t", ++state_machine_regs.last_file_entry);
4812 name = data;
4813 data += strnlen ((char *) data, end - data);
4814 if (data < end)
4815 data++;
4817 READ_ULEB (val, data, end);
4818 printf ("%" PRIu64 "\t", val);
4819 READ_ULEB (val, data, end);
4820 printf ("%" PRIu64 "\t", val);
4821 READ_ULEB (val, data, end);
4822 printf ("%" PRIu64 "\t", val);
4823 printf ("%.*s\n", (int)(end - name), name);
4825 if (data >= end)
4827 warn (_("Corrupt file name table entry\n"));
4828 break;
4833 /* Skip the NUL at the end of the table. */
4834 if (data < end)
4835 data++;
4838 putchar ('\n');
4839 saved_linfo = linfo;
4842 /* Now display the statements. */
4843 if (data >= end_of_sequence)
4844 printf (_(" No Line Number Statements.\n"));
4845 else
4847 printf (_(" Line Number Statements:\n"));
4849 while (data < end_of_sequence)
4851 unsigned char op_code;
4852 int adv;
4853 uint64_t uladv;
4855 printf (" [0x%08tx]", data - start);
4857 op_code = *data++;
4859 if (op_code >= linfo.li_opcode_base)
4861 op_code -= linfo.li_opcode_base;
4862 uladv = (op_code / linfo.li_line_range);
4863 if (linfo.li_max_ops_per_insn == 1)
4865 uladv *= linfo.li_min_insn_length;
4866 state_machine_regs.address += uladv;
4867 if (uladv)
4868 state_machine_regs.view = 0;
4869 printf (_(" Special opcode %d: "
4870 "advance Address by %" PRIu64
4871 " to %#" PRIx64 "%s"),
4872 op_code, uladv, state_machine_regs.address,
4873 verbose_view && uladv
4874 ? _(" (reset view)") : "");
4876 else
4878 unsigned addrdelta
4879 = ((state_machine_regs.op_index + uladv)
4880 / linfo.li_max_ops_per_insn)
4881 * linfo.li_min_insn_length;
4883 state_machine_regs.address += addrdelta;
4884 state_machine_regs.op_index
4885 = (state_machine_regs.op_index + uladv)
4886 % linfo.li_max_ops_per_insn;
4887 if (addrdelta)
4888 state_machine_regs.view = 0;
4889 printf (_(" Special opcode %d: "
4890 "advance Address by %" PRIu64
4891 " to %#" PRIx64 "[%d]%s"),
4892 op_code, uladv, state_machine_regs.address,
4893 state_machine_regs.op_index,
4894 verbose_view && addrdelta
4895 ? _(" (reset view)") : "");
4897 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4898 state_machine_regs.line += adv;
4899 printf (_(" and Line by %d to %d"),
4900 adv, state_machine_regs.line);
4901 if (verbose_view || state_machine_regs.view)
4902 printf (_(" (view %u)\n"), state_machine_regs.view);
4903 else
4904 putchar ('\n');
4905 state_machine_regs.view++;
4907 else
4908 switch (op_code)
4910 case DW_LNS_extended_op:
4911 data += process_extended_line_op (data,
4912 linfo.li_default_is_stmt,
4913 end);
4914 break;
4916 case DW_LNS_copy:
4917 printf (_(" Copy"));
4918 if (verbose_view || state_machine_regs.view)
4919 printf (_(" (view %u)\n"), state_machine_regs.view);
4920 else
4921 putchar ('\n');
4922 state_machine_regs.view++;
4923 break;
4925 case DW_LNS_advance_pc:
4926 READ_ULEB (uladv, data, end);
4927 if (linfo.li_max_ops_per_insn == 1)
4929 uladv *= linfo.li_min_insn_length;
4930 state_machine_regs.address += uladv;
4931 if (uladv)
4932 state_machine_regs.view = 0;
4933 printf (_(" Advance PC by %" PRIu64
4934 " to %#" PRIx64 "%s\n"),
4935 uladv, state_machine_regs.address,
4936 verbose_view && uladv
4937 ? _(" (reset view)") : "");
4939 else
4941 unsigned addrdelta
4942 = ((state_machine_regs.op_index + uladv)
4943 / linfo.li_max_ops_per_insn)
4944 * linfo.li_min_insn_length;
4945 state_machine_regs.address
4946 += addrdelta;
4947 state_machine_regs.op_index
4948 = (state_machine_regs.op_index + uladv)
4949 % linfo.li_max_ops_per_insn;
4950 if (addrdelta)
4951 state_machine_regs.view = 0;
4952 printf (_(" Advance PC by %" PRIu64
4953 " to %#" PRIx64 "[%d]%s\n"),
4954 uladv, state_machine_regs.address,
4955 state_machine_regs.op_index,
4956 verbose_view && addrdelta
4957 ? _(" (reset view)") : "");
4959 break;
4961 case DW_LNS_advance_line:
4962 READ_SLEB (adv, data, end);
4963 state_machine_regs.line += adv;
4964 printf (_(" Advance Line by %d to %d\n"),
4965 adv, state_machine_regs.line);
4966 break;
4968 case DW_LNS_set_file:
4969 READ_ULEB (uladv, data, end);
4970 printf (_(" Set File Name to entry %" PRIu64
4971 " in the File Name Table\n"), uladv);
4972 state_machine_regs.file = uladv;
4973 break;
4975 case DW_LNS_set_column:
4976 READ_ULEB (uladv, data, end);
4977 printf (_(" Set column to %" PRIu64 "\n"), uladv);
4978 state_machine_regs.column = uladv;
4979 break;
4981 case DW_LNS_negate_stmt:
4982 adv = state_machine_regs.is_stmt;
4983 adv = ! adv;
4984 printf (_(" Set is_stmt to %d\n"), adv);
4985 state_machine_regs.is_stmt = adv;
4986 break;
4988 case DW_LNS_set_basic_block:
4989 printf (_(" Set basic block\n"));
4990 state_machine_regs.basic_block = 1;
4991 break;
4993 case DW_LNS_const_add_pc:
4994 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4995 if (linfo.li_max_ops_per_insn)
4997 uladv *= linfo.li_min_insn_length;
4998 state_machine_regs.address += uladv;
4999 if (uladv)
5000 state_machine_regs.view = 0;
5001 printf (_(" Advance PC by constant %" PRIu64
5002 " to %#" PRIx64 "%s\n"),
5003 uladv, state_machine_regs.address,
5004 verbose_view && uladv
5005 ? _(" (reset view)") : "");
5007 else
5009 unsigned addrdelta
5010 = ((state_machine_regs.op_index + uladv)
5011 / linfo.li_max_ops_per_insn)
5012 * linfo.li_min_insn_length;
5013 state_machine_regs.address
5014 += addrdelta;
5015 state_machine_regs.op_index
5016 = (state_machine_regs.op_index + uladv)
5017 % linfo.li_max_ops_per_insn;
5018 if (addrdelta)
5019 state_machine_regs.view = 0;
5020 printf (_(" Advance PC by constant %" PRIu64
5021 " to %#" PRIx64 "[%d]%s\n"),
5022 uladv, state_machine_regs.address,
5023 state_machine_regs.op_index,
5024 verbose_view && addrdelta
5025 ? _(" (reset view)") : "");
5027 break;
5029 case DW_LNS_fixed_advance_pc:
5030 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5031 state_machine_regs.address += uladv;
5032 state_machine_regs.op_index = 0;
5033 printf (_(" Advance PC by fixed size amount %" PRIu64
5034 " to %#" PRIx64 "\n"),
5035 uladv, state_machine_regs.address);
5036 /* Do NOT reset view. */
5037 break;
5039 case DW_LNS_set_prologue_end:
5040 printf (_(" Set prologue_end to true\n"));
5041 break;
5043 case DW_LNS_set_epilogue_begin:
5044 printf (_(" Set epilogue_begin to true\n"));
5045 break;
5047 case DW_LNS_set_isa:
5048 READ_ULEB (uladv, data, end);
5049 printf (_(" Set ISA to %" PRIu64 "\n"), uladv);
5050 break;
5052 default:
5053 printf (_(" Unknown opcode %d with operands: "), op_code);
5055 if (standard_opcodes != NULL)
5056 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5058 READ_ULEB (uladv, data, end);
5059 printf ("%#" PRIx64 "%s", uladv, i == 1 ? "" : ", ");
5061 putchar ('\n');
5062 break;
5065 putchar ('\n');
5069 return 1;
5072 typedef struct
5074 char *name;
5075 unsigned int directory_index;
5076 unsigned int modification_date;
5077 unsigned int length;
5078 } File_Entry;
5080 /* Output a decoded representation of the .debug_line section. */
5082 static int
5083 display_debug_lines_decoded (struct dwarf_section * section,
5084 unsigned char * start,
5085 unsigned char * data,
5086 unsigned char * end,
5087 void * fileptr)
5089 static DWARF2_Internal_LineInfo saved_linfo;
5091 introduce (section, false);
5093 while (data < end)
5095 /* This loop amounts to one iteration per compilation unit. */
5096 DWARF2_Internal_LineInfo linfo;
5097 unsigned char *standard_opcodes;
5098 unsigned char *end_of_sequence;
5099 int i;
5100 File_Entry *file_table = NULL;
5101 unsigned int n_files = 0;
5102 char **directory_table = NULL;
5103 unsigned int n_directories = 0;
5105 if (startswith (section->name, ".debug_line.")
5106 /* Note: the following does not apply to .debug_line.dwo sections.
5107 These are full debug_line sections. */
5108 && strcmp (section->name, ".debug_line.dwo") != 0)
5110 /* See comment in display_debug_lines_raw(). */
5111 end_of_sequence = end;
5112 standard_opcodes = NULL;
5113 linfo = saved_linfo;
5114 /* PR 17531: file: 0522b371. */
5115 if (linfo.li_line_range == 0)
5117 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
5118 return 0;
5120 reset_state_machine (linfo.li_default_is_stmt);
5122 else
5124 unsigned char *hdrptr;
5126 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
5127 & end_of_sequence)) == NULL)
5128 return 0;
5130 /* PR 17531: file: 0522b371. */
5131 if (linfo.li_line_range == 0)
5133 warn (_("Line range of 0 is invalid, using 1 instead\n"));
5134 linfo.li_line_range = 1;
5136 reset_state_machine (linfo.li_default_is_stmt);
5138 /* Save a pointer to the contents of the Opcodes table. */
5139 standard_opcodes = hdrptr;
5141 /* Traverse the Directory table just to count entries. */
5142 data = standard_opcodes + linfo.li_opcode_base - 1;
5143 /* PR 20440 */
5144 if (data >= end)
5146 warn (_("opcode base of %d extends beyond end of section\n"),
5147 linfo.li_opcode_base);
5148 return 0;
5151 if (linfo.li_version >= 5)
5153 unsigned char *format_start, *format;
5154 unsigned int format_count, formati, entryi;
5156 load_debug_section_with_follow (line_str, fileptr);
5158 /* Skip directories format. */
5159 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5160 if (do_checks && format_count > 1)
5161 warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
5162 format_count);
5163 format_start = data;
5164 for (formati = 0; formati < format_count; formati++)
5166 SKIP_ULEB (data, end);
5167 SKIP_ULEB (data, end);
5170 READ_ULEB (n_directories, data, end);
5171 if (data >= end)
5173 warn (_("Corrupt directories list\n"));
5174 break;
5177 if (n_directories == 0)
5178 directory_table = NULL;
5179 else if (n_directories > section->size)
5181 warn (_("number of directories (0x%x) exceeds size of section %s\n"),
5182 n_directories, section->name);
5183 return 0;
5185 else
5186 directory_table = (char **)
5187 xcalloc (n_directories, sizeof (unsigned char *));
5189 for (entryi = 0; entryi < n_directories; entryi++)
5191 char **pathp = &directory_table[entryi];
5193 format = format_start;
5194 for (formati = 0; formati < format_count; formati++)
5196 uint64_t content_type, form;
5197 uint64_t uvalue;
5199 READ_ULEB (content_type, format, end);
5200 READ_ULEB (form, format, end);
5201 if (data >= end)
5203 warn (_("Corrupt directories list\n"));
5204 break;
5206 switch (content_type)
5208 case DW_LNCT_path:
5209 switch (form)
5211 case DW_FORM_string:
5212 *pathp = (char *) data;
5213 break;
5214 case DW_FORM_line_strp:
5215 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5216 end);
5217 /* Remove const by the cast. */
5218 *pathp = (char *)
5219 fetch_indirect_line_string (uvalue);
5220 break;
5222 break;
5224 data = read_and_display_attr_value (0, form, 0, start,
5225 data, end, 0, 0,
5226 linfo.li_offset_size,
5227 linfo.li_version,
5228 NULL, 1, section,
5229 NULL, '\t', -1);
5231 if (data >= end)
5233 warn (_("Corrupt directories list\n"));
5234 break;
5238 /* Skip files format. */
5239 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5240 if (do_checks && format_count > 5)
5241 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
5242 format_count);
5244 format_start = data;
5245 for (formati = 0; formati < format_count; formati++)
5247 SKIP_ULEB (data, end);
5248 SKIP_ULEB (data, end);
5251 READ_ULEB (n_files, data, end);
5252 if (data >= end && n_files > 0)
5254 warn (_("Corrupt file name list\n"));
5255 break;
5258 if (n_files == 0)
5259 file_table = NULL;
5260 else if (n_files > section->size)
5262 warn (_("number of files (0x%x) exceeds size of section %s\n"),
5263 n_files, section->name);
5264 return 0;
5266 else
5267 file_table = (File_Entry *) xcalloc (n_files,
5268 sizeof (File_Entry));
5270 for (entryi = 0; entryi < n_files; entryi++)
5272 File_Entry *file = &file_table[entryi];
5274 format = format_start;
5275 for (formati = 0; formati < format_count; formati++)
5277 uint64_t content_type, form;
5278 uint64_t uvalue;
5279 unsigned char *tmp;
5281 READ_ULEB (content_type, format, end);
5282 READ_ULEB (form, format, end);
5283 if (data >= end)
5285 warn (_("Corrupt file name list\n"));
5286 break;
5288 switch (content_type)
5290 case DW_LNCT_path:
5291 switch (form)
5293 case DW_FORM_string:
5294 file->name = (char *) data;
5295 break;
5296 case DW_FORM_line_strp:
5297 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5298 end);
5299 /* Remove const by the cast. */
5300 file->name = (char *)
5301 fetch_indirect_line_string (uvalue);
5302 break;
5304 break;
5305 case DW_LNCT_directory_index:
5306 switch (form)
5308 case DW_FORM_data1:
5309 SAFE_BYTE_GET (file->directory_index, data, 1,
5310 end);
5311 break;
5312 case DW_FORM_data2:
5313 SAFE_BYTE_GET (file->directory_index, data, 2,
5314 end);
5315 break;
5316 case DW_FORM_udata:
5317 tmp = data;
5318 READ_ULEB (file->directory_index, tmp, end);
5319 break;
5321 break;
5323 data = read_and_display_attr_value (0, form, 0, start,
5324 data, end, 0, 0,
5325 linfo.li_offset_size,
5326 linfo.li_version,
5327 NULL, 1, section,
5328 NULL, '\t', -1);
5330 if (data >= end)
5332 warn (_("Corrupt file name list\n"));
5333 break;
5337 else
5339 if (*data != 0)
5341 char *ptr_directory_table = (char *) data;
5343 while (data < end && *data != 0)
5345 data += strnlen ((char *) data, end - data);
5346 if (data < end)
5347 data++;
5348 n_directories++;
5351 /* PR 20440 */
5352 if (data >= end)
5354 warn (_("directory table ends unexpectedly\n"));
5355 n_directories = 0;
5356 break;
5359 /* Go through the directory table again to save the directories. */
5360 directory_table = (char **)
5361 xmalloc (n_directories * sizeof (unsigned char *));
5363 i = 0;
5364 while (*ptr_directory_table != 0)
5366 directory_table[i] = ptr_directory_table;
5367 ptr_directory_table += strlen (ptr_directory_table) + 1;
5368 i++;
5371 /* Skip the NUL at the end of the table. */
5372 data++;
5374 /* Traverse the File Name table just to count the entries. */
5375 if (data < end && *data != 0)
5377 unsigned char *ptr_file_name_table = data;
5379 while (data < end && *data != 0)
5381 /* Skip Name, directory index, last modification
5382 time and length of file. */
5383 data += strnlen ((char *) data, end - data);
5384 if (data < end)
5385 data++;
5386 SKIP_ULEB (data, end);
5387 SKIP_ULEB (data, end);
5388 SKIP_ULEB (data, end);
5389 n_files++;
5392 if (data >= end)
5394 warn (_("file table ends unexpectedly\n"));
5395 n_files = 0;
5396 break;
5399 /* Go through the file table again to save the strings. */
5400 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
5402 i = 0;
5403 while (*ptr_file_name_table != 0)
5405 file_table[i].name = (char *) ptr_file_name_table;
5406 ptr_file_name_table
5407 += strlen ((char *) ptr_file_name_table) + 1;
5409 /* We are not interested in directory, time or size. */
5410 READ_ULEB (file_table[i].directory_index,
5411 ptr_file_name_table, end);
5412 READ_ULEB (file_table[i].modification_date,
5413 ptr_file_name_table, end);
5414 READ_ULEB (file_table[i].length,
5415 ptr_file_name_table, end);
5416 i++;
5418 i = 0;
5421 /* Skip the NUL at the end of the table. */
5422 data++;
5425 /* Print the Compilation Unit's name and a header. */
5426 if (file_table == NULL)
5427 printf (_("CU: No directory table\n"));
5428 else if (directory_table == NULL)
5429 printf (_("CU: %s:\n"), null_name (file_table[0].name));
5430 else
5432 unsigned int ix = file_table[0].directory_index;
5433 const char *directory;
5435 if (ix == 0 && linfo.li_version < 5)
5436 directory = ".";
5437 /* PR 20439 */
5438 else if (n_directories == 0)
5439 directory = _("<unknown>");
5440 else
5442 if (linfo.li_version < 5)
5443 --ix;
5444 if (ix >= n_directories)
5446 warn (_("directory index %u "
5447 ">= number of directories %u\n"),
5448 ix, n_directories);
5449 directory = _("<corrupt>");
5451 else
5452 directory = directory_table[ix];
5454 if (do_wide)
5455 printf (_("CU: %s/%s:\n"),
5456 null_name (directory),
5457 null_name (file_table[0].name));
5458 else
5459 printf ("%s:\n", null_name (file_table[0].name));
5462 if (n_files > 0)
5464 if (do_wide)
5465 printf (_("File name Line number Starting address View Stmt\n"));
5466 else
5467 printf (_("File name Line number Starting address View Stmt\n"));
5469 else
5470 printf (_("CU: Empty file name table\n"));
5471 saved_linfo = linfo;
5474 /* This loop iterates through the Dwarf Line Number Program. */
5475 while (data < end_of_sequence)
5477 unsigned char op_code;
5478 int xop;
5479 int adv;
5480 unsigned long int uladv;
5481 int is_special_opcode = 0;
5483 op_code = *data++;
5484 xop = op_code;
5486 if (op_code >= linfo.li_opcode_base)
5488 op_code -= linfo.li_opcode_base;
5489 uladv = (op_code / linfo.li_line_range);
5490 if (linfo.li_max_ops_per_insn == 1)
5492 uladv *= linfo.li_min_insn_length;
5493 state_machine_regs.address += uladv;
5494 if (uladv)
5495 state_machine_regs.view = 0;
5497 else
5499 unsigned addrdelta
5500 = ((state_machine_regs.op_index + uladv)
5501 / linfo.li_max_ops_per_insn)
5502 * linfo.li_min_insn_length;
5503 state_machine_regs.address
5504 += addrdelta;
5505 state_machine_regs.op_index
5506 = (state_machine_regs.op_index + uladv)
5507 % linfo.li_max_ops_per_insn;
5508 if (addrdelta)
5509 state_machine_regs.view = 0;
5512 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5513 state_machine_regs.line += adv;
5514 is_special_opcode = 1;
5515 /* Increment view after printing this row. */
5517 else
5518 switch (op_code)
5520 case DW_LNS_extended_op:
5522 unsigned int ext_op_code_len;
5523 unsigned char ext_op_code;
5524 unsigned char *op_code_end;
5525 unsigned char *op_code_data = data;
5527 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5528 op_code_end = op_code_data + ext_op_code_len;
5529 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5531 warn (_("Badly formed extended line op encountered!\n"));
5532 break;
5534 ext_op_code = *op_code_data++;
5535 xop = ext_op_code;
5536 xop = -xop;
5538 switch (ext_op_code)
5540 case DW_LNE_end_sequence:
5541 /* Reset stuff after printing this row. */
5542 break;
5543 case DW_LNE_set_address:
5544 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5545 op_code_data,
5546 op_code_end - op_code_data,
5547 op_code_end);
5548 state_machine_regs.op_index = 0;
5549 state_machine_regs.view = 0;
5550 break;
5551 case DW_LNE_define_file:
5552 file_table = (File_Entry *) xrealloc
5553 (file_table, (n_files + 1) * sizeof (File_Entry));
5555 ++state_machine_regs.last_file_entry;
5556 /* Source file name. */
5557 file_table[n_files].name = (char *) op_code_data;
5558 op_code_data += strlen ((char *) op_code_data) + 1;
5559 /* Directory index. */
5560 READ_ULEB (file_table[n_files].directory_index,
5561 op_code_data, op_code_end);
5562 /* Last modification time. */
5563 READ_ULEB (file_table[n_files].modification_date,
5564 op_code_data, op_code_end);
5565 /* File length. */
5566 READ_ULEB (file_table[n_files].length,
5567 op_code_data, op_code_end);
5568 n_files++;
5569 break;
5571 case DW_LNE_set_discriminator:
5572 case DW_LNE_HP_set_sequence:
5573 /* Simply ignored. */
5574 break;
5576 default:
5577 printf (_("UNKNOWN (%u): length %ld\n"),
5578 ext_op_code, (long int) (op_code_data - data));
5579 break;
5581 data = op_code_end;
5582 break;
5584 case DW_LNS_copy:
5585 /* Increment view after printing this row. */
5586 break;
5588 case DW_LNS_advance_pc:
5589 READ_ULEB (uladv, data, end);
5590 if (linfo.li_max_ops_per_insn == 1)
5592 uladv *= linfo.li_min_insn_length;
5593 state_machine_regs.address += uladv;
5594 if (uladv)
5595 state_machine_regs.view = 0;
5597 else
5599 unsigned addrdelta
5600 = ((state_machine_regs.op_index + uladv)
5601 / linfo.li_max_ops_per_insn)
5602 * linfo.li_min_insn_length;
5603 state_machine_regs.address
5604 += addrdelta;
5605 state_machine_regs.op_index
5606 = (state_machine_regs.op_index + uladv)
5607 % linfo.li_max_ops_per_insn;
5608 if (addrdelta)
5609 state_machine_regs.view = 0;
5611 break;
5613 case DW_LNS_advance_line:
5614 READ_SLEB (adv, data, end);
5615 state_machine_regs.line += adv;
5616 break;
5618 case DW_LNS_set_file:
5619 READ_ULEB (uladv, data, end);
5620 state_machine_regs.file = uladv;
5622 unsigned file = state_machine_regs.file;
5623 if (linfo.li_version < 5)
5624 --file;
5626 if (file_table == NULL || n_files == 0)
5627 printf (_("\n [Use file table entry %d]\n"), file);
5628 /* PR 20439 */
5629 else if (file >= n_files)
5631 warn (_("file index %u >= number of files %u\n"),
5632 file, n_files);
5633 printf (_("\n <over large file table index %u>"), file);
5635 else
5637 unsigned dir = file_table[file].directory_index;
5638 if (dir == 0 && linfo.li_version < 5)
5639 /* If directory index is 0, that means compilation
5640 current directory. bfd/dwarf2.c shows
5641 DW_AT_comp_dir here but in keeping with the
5642 readelf practice of minimal interpretation of
5643 file data, we show "./". */
5644 printf ("\n./%s:[++]\n",
5645 null_name (file_table[file].name));
5646 else if (directory_table == NULL || n_directories == 0)
5647 printf (_("\n [Use file %s "
5648 "in directory table entry %d]\n"),
5649 null_name (file_table[file].name), dir);
5650 else
5652 if (linfo.li_version < 5)
5653 --dir;
5654 /* PR 20439 */
5655 if (dir >= n_directories)
5657 warn (_("directory index %u "
5658 ">= number of directories %u\n"),
5659 dir, n_directories);
5660 printf (_("\n <over large directory table entry "
5661 "%u>\n"), dir);
5663 else
5664 printf ("\n%s/%s:\n",
5665 null_name (directory_table[dir]),
5666 null_name (file_table[file].name));
5669 break;
5671 case DW_LNS_set_column:
5672 READ_ULEB (uladv, data, end);
5673 state_machine_regs.column = uladv;
5674 break;
5676 case DW_LNS_negate_stmt:
5677 adv = state_machine_regs.is_stmt;
5678 adv = ! adv;
5679 state_machine_regs.is_stmt = adv;
5680 break;
5682 case DW_LNS_set_basic_block:
5683 state_machine_regs.basic_block = 1;
5684 break;
5686 case DW_LNS_const_add_pc:
5687 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5688 if (linfo.li_max_ops_per_insn == 1)
5690 uladv *= linfo.li_min_insn_length;
5691 state_machine_regs.address += uladv;
5692 if (uladv)
5693 state_machine_regs.view = 0;
5695 else
5697 unsigned addrdelta
5698 = ((state_machine_regs.op_index + uladv)
5699 / linfo.li_max_ops_per_insn)
5700 * linfo.li_min_insn_length;
5701 state_machine_regs.address
5702 += addrdelta;
5703 state_machine_regs.op_index
5704 = (state_machine_regs.op_index + uladv)
5705 % linfo.li_max_ops_per_insn;
5706 if (addrdelta)
5707 state_machine_regs.view = 0;
5709 break;
5711 case DW_LNS_fixed_advance_pc:
5712 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5713 state_machine_regs.address += uladv;
5714 state_machine_regs.op_index = 0;
5715 /* Do NOT reset view. */
5716 break;
5718 case DW_LNS_set_prologue_end:
5719 break;
5721 case DW_LNS_set_epilogue_begin:
5722 break;
5724 case DW_LNS_set_isa:
5725 READ_ULEB (uladv, data, end);
5726 printf (_(" Set ISA to %lu\n"), uladv);
5727 break;
5729 default:
5730 printf (_(" Unknown opcode %d with operands: "), op_code);
5732 if (standard_opcodes != NULL)
5733 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5735 uint64_t val;
5737 READ_ULEB (val, data, end);
5738 printf ("%#" PRIx64 "%s", val, i == 1 ? "" : ", ");
5740 putchar ('\n');
5741 break;
5744 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5745 to the DWARF address/line matrix. */
5746 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5747 || (xop == DW_LNS_copy))
5749 const unsigned int MAX_FILENAME_LENGTH = 35;
5750 char *fileName = NULL;
5751 char *newFileName = NULL;
5752 size_t fileNameLength;
5754 if (file_table)
5756 unsigned indx = state_machine_regs.file;
5758 if (linfo.li_version < 5)
5759 --indx;
5760 /* PR 20439 */
5761 if (indx >= n_files)
5763 warn (_("file index %u >= number of files %u\n"),
5764 indx, n_files);
5765 fileName = _("<corrupt>");
5767 else
5768 fileName = (char *) file_table[indx].name;
5770 if (!fileName)
5771 fileName = _("<unknown>");
5773 fileNameLength = strlen (fileName);
5774 newFileName = fileName;
5775 if (fileNameLength > MAX_FILENAME_LENGTH && !do_wide)
5777 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5778 /* Truncate file name */
5779 memcpy (newFileName,
5780 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5781 MAX_FILENAME_LENGTH);
5782 newFileName[MAX_FILENAME_LENGTH] = 0;
5785 /* A row with end_seq set to true has a meaningful address, but
5786 the other information in the same row is not significant.
5787 In such a row, print line as "-", and don't print
5788 view/is_stmt. */
5789 if (!do_wide || fileNameLength <= MAX_FILENAME_LENGTH)
5791 if (linfo.li_max_ops_per_insn == 1)
5793 if (xop == -DW_LNE_end_sequence)
5794 printf ("%-31s %11s %#18" PRIx64,
5795 newFileName, "-",
5796 state_machine_regs.address);
5797 else
5798 printf ("%-31s %11d %#18" PRIx64,
5799 newFileName, state_machine_regs.line,
5800 state_machine_regs.address);
5802 else
5804 if (xop == -DW_LNE_end_sequence)
5805 printf ("%-31s %11s %#18" PRIx64 "[%d]",
5806 newFileName, "-",
5807 state_machine_regs.address,
5808 state_machine_regs.op_index);
5809 else
5810 printf ("%-31s %11d %#18" PRIx64 "[%d]",
5811 newFileName, state_machine_regs.line,
5812 state_machine_regs.address,
5813 state_machine_regs.op_index);
5816 else
5818 if (linfo.li_max_ops_per_insn == 1)
5820 if (xop == -DW_LNE_end_sequence)
5821 printf ("%s %11s %#18" PRIx64,
5822 newFileName, "-",
5823 state_machine_regs.address);
5824 else
5825 printf ("%s %11d %#18" PRIx64,
5826 newFileName, state_machine_regs.line,
5827 state_machine_regs.address);
5829 else
5831 if (xop == -DW_LNE_end_sequence)
5832 printf ("%s %11s %#18" PRIx64 "[%d]",
5833 newFileName, "-",
5834 state_machine_regs.address,
5835 state_machine_regs.op_index);
5836 else
5837 printf ("%s %11d %#18" PRIx64 "[%d]",
5838 newFileName, state_machine_regs.line,
5839 state_machine_regs.address,
5840 state_machine_regs.op_index);
5844 if (xop != -DW_LNE_end_sequence)
5846 if (state_machine_regs.view)
5847 printf (" %6u", state_machine_regs.view);
5848 else
5849 printf (" ");
5851 if (state_machine_regs.is_stmt)
5852 printf (" x");
5855 putchar ('\n');
5856 state_machine_regs.view++;
5858 if (xop == -DW_LNE_end_sequence)
5860 reset_state_machine (linfo.li_default_is_stmt);
5861 putchar ('\n');
5864 if (newFileName != fileName)
5865 free (newFileName);
5869 if (file_table)
5871 free (file_table);
5872 file_table = NULL;
5873 n_files = 0;
5876 if (directory_table)
5878 free (directory_table);
5879 directory_table = NULL;
5880 n_directories = 0;
5883 putchar ('\n');
5886 return 1;
5889 static int
5890 display_debug_lines (struct dwarf_section *section, void *file)
5892 unsigned char *data = section->start;
5893 unsigned char *end = data + section->size;
5894 int retValRaw = 1;
5895 int retValDecoded = 1;
5897 if (do_debug_lines == 0)
5898 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5900 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
5901 retValRaw = display_debug_lines_raw (section, data, end, file);
5903 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
5904 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
5906 if (!retValRaw || !retValDecoded)
5907 return 0;
5909 return 1;
5912 static debug_info *
5913 find_debug_info_for_offset (uint64_t offset)
5915 unsigned int i;
5917 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5918 return NULL;
5920 for (i = 0; i < num_debug_info_entries; i++)
5921 if (debug_information[i].cu_offset == offset)
5922 return debug_information + i;
5924 return NULL;
5927 static const char *
5928 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5930 /* See gdb/gdb-index.h. */
5931 static const char * const kinds[] =
5933 N_ ("no info"),
5934 N_ ("type"),
5935 N_ ("variable"),
5936 N_ ("function"),
5937 N_ ("other"),
5938 N_ ("unused5"),
5939 N_ ("unused6"),
5940 N_ ("unused7")
5943 return _ (kinds[kind]);
5946 static int
5947 display_debug_pubnames_worker (struct dwarf_section *section,
5948 void *file ATTRIBUTE_UNUSED,
5949 int is_gnu)
5951 DWARF2_Internal_PubNames names;
5952 unsigned char *start = section->start;
5953 unsigned char *end = start + section->size;
5955 /* It does not matter if this load fails,
5956 we test for that later on. */
5957 load_debug_info (file);
5959 introduce (section, false);
5961 while (start < end)
5963 unsigned char *data;
5964 unsigned long sec_off = start - section->start;
5965 unsigned int offset_size;
5967 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
5968 if (names.pn_length == 0xffffffff)
5970 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
5971 offset_size = 8;
5973 else
5974 offset_size = 4;
5976 if (names.pn_length > (size_t) (end - start))
5978 warn (_("Debug info is corrupted, "
5979 "%s header at %#lx has length %#" PRIx64 "\n"),
5980 section->name, sec_off, names.pn_length);
5981 break;
5984 data = start;
5985 start += names.pn_length;
5987 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, start);
5988 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, start);
5990 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5991 && num_debug_info_entries > 0
5992 && find_debug_info_for_offset (names.pn_offset) == NULL)
5993 warn (_(".debug_info offset of %#" PRIx64
5994 " in %s section does not point to a CU header.\n"),
5995 names.pn_offset, section->name);
5997 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, start);
5999 printf (_(" Length: %" PRId64 "\n"),
6000 names.pn_length);
6001 printf (_(" Version: %d\n"),
6002 names.pn_version);
6003 printf (_(" Offset into .debug_info section: %#" PRIx64 "\n"),
6004 names.pn_offset);
6005 printf (_(" Size of area in .debug_info section: %" PRId64 "\n"),
6006 names.pn_size);
6008 if (names.pn_version != 2 && names.pn_version != 3)
6010 static int warned = 0;
6012 if (! warned)
6014 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
6015 warned = 1;
6018 continue;
6021 if (is_gnu)
6022 printf (_("\n Offset Kind Name\n"));
6023 else
6024 printf (_("\n Offset\tName\n"));
6026 while (1)
6028 size_t maxprint;
6029 uint64_t offset;
6031 SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start);
6033 if (offset == 0)
6034 break;
6036 if (data >= start)
6037 break;
6038 maxprint = (start - data) - 1;
6040 if (is_gnu)
6042 unsigned int kind_data;
6043 gdb_index_symbol_kind kind;
6044 const char *kind_name;
6045 int is_static;
6047 SAFE_BYTE_GET_AND_INC (kind_data, data, 1, start);
6048 maxprint --;
6049 /* GCC computes the kind as the upper byte in the CU index
6050 word, and then right shifts it by the CU index size.
6051 Left shift KIND to where the gdb-index.h accessor macros
6052 can use it. */
6053 kind_data <<= GDB_INDEX_CU_BITSIZE;
6054 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
6055 kind_name = get_gdb_index_symbol_kind_name (kind);
6056 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
6057 printf (" %-6" PRIx64 " %s,%-10s %.*s\n",
6058 offset, is_static ? _("s") : _("g"),
6059 kind_name, (int) maxprint, data);
6061 else
6062 printf (" %-6" PRIx64 "\t%.*s\n",
6063 offset, (int) maxprint, data);
6065 data += strnlen ((char *) data, maxprint);
6066 if (data < start)
6067 data++;
6068 if (data >= start)
6069 break;
6073 printf ("\n");
6074 return 1;
6077 static int
6078 display_debug_pubnames (struct dwarf_section *section, void *file)
6080 return display_debug_pubnames_worker (section, file, 0);
6083 static int
6084 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
6086 return display_debug_pubnames_worker (section, file, 1);
6089 static int
6090 display_debug_macinfo (struct dwarf_section *section,
6091 void *file ATTRIBUTE_UNUSED)
6093 unsigned char *start = section->start;
6094 unsigned char *end = start + section->size;
6095 unsigned char *curr = start;
6096 enum dwarf_macinfo_record_type op;
6098 introduce (section, false);
6100 while (curr < end)
6102 unsigned int lineno;
6103 const unsigned char *string;
6105 op = (enum dwarf_macinfo_record_type) *curr;
6106 curr++;
6108 switch (op)
6110 case DW_MACINFO_start_file:
6112 unsigned int filenum;
6114 READ_ULEB (lineno, curr, end);
6115 READ_ULEB (filenum, curr, end);
6116 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
6117 lineno, filenum);
6119 break;
6121 case DW_MACINFO_end_file:
6122 printf (_(" DW_MACINFO_end_file\n"));
6123 break;
6125 case DW_MACINFO_define:
6126 READ_ULEB (lineno, curr, end);
6127 string = curr;
6128 curr += strnlen ((char *) string, end - string);
6129 printf (_(" DW_MACINFO_define - lineno : %d macro : %*s\n"),
6130 lineno, (int) (curr - string), string);
6131 if (curr < end)
6132 curr++;
6133 break;
6135 case DW_MACINFO_undef:
6136 READ_ULEB (lineno, curr, end);
6137 string = curr;
6138 curr += strnlen ((char *) string, end - string);
6139 printf (_(" DW_MACINFO_undef - lineno : %d macro : %*s\n"),
6140 lineno, (int) (curr - string), string);
6141 if (curr < end)
6142 curr++;
6143 break;
6145 case DW_MACINFO_vendor_ext:
6147 unsigned int constant;
6149 READ_ULEB (constant, curr, end);
6150 string = curr;
6151 curr += strnlen ((char *) string, end - string);
6152 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %*s\n"),
6153 constant, (int) (curr - string), string);
6154 if (curr < end)
6155 curr++;
6157 break;
6161 return 1;
6164 /* Given LINE_OFFSET into the .debug_line section, attempt to return
6165 filename and dirname corresponding to file name table entry with index
6166 FILEIDX. Return NULL on failure. */
6168 static unsigned char *
6169 get_line_filename_and_dirname (uint64_t line_offset,
6170 uint64_t fileidx,
6171 unsigned char **dir_name)
6173 struct dwarf_section *section = &debug_displays [line].section;
6174 unsigned char *hdrptr, *dirtable, *file_name;
6175 unsigned int offset_size;
6176 unsigned int version, opcode_base;
6177 uint64_t length, diridx;
6178 const unsigned char * end;
6180 *dir_name = NULL;
6181 if (section->start == NULL
6182 || line_offset >= section->size
6183 || fileidx == 0)
6184 return NULL;
6186 hdrptr = section->start + line_offset;
6187 end = section->start + section->size;
6189 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
6190 if (length == 0xffffffff)
6192 /* This section is 64-bit DWARF 3. */
6193 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
6194 offset_size = 8;
6196 else
6197 offset_size = 4;
6199 if (length > (size_t) (end - hdrptr)
6200 || length < 2 + offset_size + 1 + 3 + 1)
6201 return NULL;
6202 end = hdrptr + length;
6204 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
6205 if (version != 2 && version != 3 && version != 4)
6206 return NULL;
6207 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
6208 if (version >= 4)
6209 hdrptr++; /* Skip max_ops_per_insn. */
6210 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
6212 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
6213 if (opcode_base == 0
6214 || opcode_base - 1 >= (size_t) (end - hdrptr))
6215 return NULL;
6217 hdrptr += opcode_base - 1;
6219 dirtable = hdrptr;
6220 /* Skip over dirname table. */
6221 while (*hdrptr != '\0')
6223 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6224 if (hdrptr < end)
6225 hdrptr++;
6226 if (hdrptr >= end)
6227 return NULL;
6229 hdrptr++; /* Skip the NUL at the end of the table. */
6231 /* Now skip over preceding filename table entries. */
6232 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
6234 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6235 if (hdrptr < end)
6236 hdrptr++;
6237 SKIP_ULEB (hdrptr, end);
6238 SKIP_ULEB (hdrptr, end);
6239 SKIP_ULEB (hdrptr, end);
6241 if (hdrptr >= end || *hdrptr == '\0')
6242 return NULL;
6244 file_name = hdrptr;
6245 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6246 if (hdrptr < end)
6247 hdrptr++;
6248 if (hdrptr >= end)
6249 return NULL;
6250 READ_ULEB (diridx, hdrptr, end);
6251 if (diridx == 0)
6252 return file_name;
6253 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
6255 dirtable += strnlen ((char *) dirtable, end - dirtable);
6256 if (dirtable < end)
6257 dirtable++;
6259 if (dirtable >= end || *dirtable == '\0')
6260 return NULL;
6261 *dir_name = dirtable;
6262 return file_name;
6265 static int
6266 display_debug_macro (struct dwarf_section *section,
6267 void *file)
6269 unsigned char *start = section->start;
6270 unsigned char *end = start + section->size;
6271 unsigned char *curr = start;
6272 unsigned char *extended_op_buf[256];
6273 bool is_dwo = false;
6274 const char *suffix = strrchr (section->name, '.');
6276 if (suffix && strcmp (suffix, ".dwo") == 0)
6277 is_dwo = true;
6279 load_debug_section_with_follow (str, file);
6280 load_debug_section_with_follow (line, file);
6281 load_debug_section_with_follow (str_index, file);
6283 introduce (section, false);
6285 while (curr < end)
6287 unsigned int lineno, version, flags;
6288 unsigned int offset_size;
6289 const unsigned char *string;
6290 uint64_t line_offset = 0, sec_offset = curr - start, offset;
6291 unsigned char **extended_ops = NULL;
6293 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
6294 if (version != 4 && version != 5)
6296 error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
6297 section->name, version);
6298 return 0;
6301 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
6302 offset_size = (flags & 1) ? 8 : 4;
6303 printf (_(" Offset: %#" PRIx64 "\n"), sec_offset);
6304 printf (_(" Version: %d\n"), version);
6305 printf (_(" Offset size: %d\n"), offset_size);
6306 if (flags & 2)
6308 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
6309 printf (_(" Offset into .debug_line: %#" PRIx64 "\n"),
6310 line_offset);
6312 if (flags & 4)
6314 unsigned int i, count, op;
6315 uint64_t nargs, n;
6317 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
6319 memset (extended_op_buf, 0, sizeof (extended_op_buf));
6320 extended_ops = extended_op_buf;
6321 if (count)
6323 printf (_(" Extension opcode arguments:\n"));
6324 for (i = 0; i < count; i++)
6326 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6327 extended_ops[op] = curr;
6328 READ_ULEB (nargs, curr, end);
6329 if (nargs == 0)
6330 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
6331 else
6333 printf (_(" DW_MACRO_%02x arguments: "), op);
6334 for (n = 0; n < nargs; n++)
6336 unsigned int form;
6338 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
6339 printf ("%s%s", get_FORM_name (form),
6340 n == nargs - 1 ? "\n" : ", ");
6341 switch (form)
6343 case DW_FORM_data1:
6344 case DW_FORM_data2:
6345 case DW_FORM_data4:
6346 case DW_FORM_data8:
6347 case DW_FORM_sdata:
6348 case DW_FORM_udata:
6349 case DW_FORM_block:
6350 case DW_FORM_block1:
6351 case DW_FORM_block2:
6352 case DW_FORM_block4:
6353 case DW_FORM_flag:
6354 case DW_FORM_string:
6355 case DW_FORM_strp:
6356 case DW_FORM_sec_offset:
6357 break;
6358 default:
6359 error (_("Invalid extension opcode form %s\n"),
6360 get_FORM_name (form));
6361 return 0;
6368 printf ("\n");
6370 while (1)
6372 unsigned int op;
6374 if (curr >= end)
6376 error (_(".debug_macro section not zero terminated\n"));
6377 return 0;
6380 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6381 if (op == 0)
6382 break;
6384 switch (op)
6386 case DW_MACRO_define:
6387 READ_ULEB (lineno, curr, end);
6388 string = curr;
6389 curr += strnlen ((char *) string, end - string);
6390 printf (_(" DW_MACRO_define - lineno : %d macro : %*s\n"),
6391 lineno, (int) (curr - string), string);
6392 if (curr < end)
6393 curr++;
6394 break;
6396 case DW_MACRO_undef:
6397 READ_ULEB (lineno, curr, end);
6398 string = curr;
6399 curr += strnlen ((char *) string, end - string);
6400 printf (_(" DW_MACRO_undef - lineno : %d macro : %*s\n"),
6401 lineno, (int) (curr - string), string);
6402 if (curr < end)
6403 curr++;
6404 break;
6406 case DW_MACRO_start_file:
6408 unsigned int filenum;
6409 unsigned char *file_name = NULL, *dir_name = NULL;
6411 READ_ULEB (lineno, curr, end);
6412 READ_ULEB (filenum, curr, end);
6414 if ((flags & 2) == 0)
6415 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
6416 else
6417 file_name
6418 = get_line_filename_and_dirname (line_offset, filenum,
6419 &dir_name);
6420 if (file_name == NULL)
6421 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
6422 lineno, filenum);
6423 else
6424 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
6425 lineno, filenum,
6426 dir_name != NULL ? (const char *) dir_name : "",
6427 dir_name != NULL ? "/" : "", file_name);
6429 break;
6431 case DW_MACRO_end_file:
6432 printf (_(" DW_MACRO_end_file\n"));
6433 break;
6435 case DW_MACRO_define_strp:
6436 READ_ULEB (lineno, curr, end);
6437 if (version == 4 && is_dwo)
6438 READ_ULEB (offset, curr, end);
6439 else
6440 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6441 string = fetch_indirect_string (offset);
6442 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
6443 lineno, string);
6444 break;
6446 case DW_MACRO_undef_strp:
6447 READ_ULEB (lineno, curr, end);
6448 if (version == 4 && is_dwo)
6449 READ_ULEB (offset, curr, end);
6450 else
6451 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6452 string = fetch_indirect_string (offset);
6453 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
6454 lineno, string);
6455 break;
6457 case DW_MACRO_import:
6458 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6459 printf (_(" DW_MACRO_import - offset : %#" PRIx64 "\n"),
6460 offset);
6461 break;
6463 case DW_MACRO_define_sup:
6464 READ_ULEB (lineno, curr, end);
6465 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6466 printf (_(" DW_MACRO_define_sup - lineno : %d"
6467 " macro offset : %#" PRIx64 "\n"),
6468 lineno, offset);
6469 break;
6471 case DW_MACRO_undef_sup:
6472 READ_ULEB (lineno, curr, end);
6473 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6474 printf (_(" DW_MACRO_undef_sup - lineno : %d"
6475 " macro offset : %#" PRIx64 "\n"),
6476 lineno, offset);
6477 break;
6479 case DW_MACRO_import_sup:
6480 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6481 printf (_(" DW_MACRO_import_sup - offset : %#" PRIx64 "\n"),
6482 offset);
6483 break;
6485 case DW_MACRO_define_strx:
6486 case DW_MACRO_undef_strx:
6487 READ_ULEB (lineno, curr, end);
6488 READ_ULEB (offset, curr, end);
6489 string = (const unsigned char *)
6490 fetch_indexed_string (offset, NULL, offset_size, false, 0);
6491 if (op == DW_MACRO_define_strx)
6492 printf (" DW_MACRO_define_strx ");
6493 else
6494 printf (" DW_MACRO_undef_strx ");
6495 if (do_wide)
6496 printf (_("(with offset %#" PRIx64 ") "), offset);
6497 printf (_("lineno : %d macro : %s\n"),
6498 lineno, string);
6499 break;
6501 default:
6502 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6504 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6505 break;
6508 if (extended_ops == NULL || extended_ops[op] == NULL)
6510 error (_(" Unknown macro opcode %02x seen\n"), op);
6511 return 0;
6513 else
6515 /* Skip over unhandled opcodes. */
6516 uint64_t nargs, n;
6517 unsigned char *desc = extended_ops[op];
6518 READ_ULEB (nargs, desc, end);
6519 if (nargs == 0)
6521 printf (_(" DW_MACRO_%02x\n"), op);
6522 break;
6524 printf (_(" DW_MACRO_%02x -"), op);
6525 for (n = 0; n < nargs; n++)
6527 int val;
6529 /* DW_FORM_implicit_const is not expected here. */
6530 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
6531 curr
6532 = read_and_display_attr_value (0, val, 0,
6533 start, curr, end, 0, 0,
6534 offset_size, version,
6535 NULL, 0, section,
6536 NULL, ' ', -1);
6537 if (n != nargs - 1)
6538 printf (",");
6540 printf ("\n");
6542 break;
6546 printf ("\n");
6549 return 1;
6552 static int
6553 display_debug_abbrev (struct dwarf_section *section,
6554 void *file ATTRIBUTE_UNUSED)
6556 abbrev_entry *entry;
6557 unsigned char *start = section->start;
6559 introduce (section, false);
6563 uint64_t offset = start - section->start;
6564 abbrev_list *list = find_and_process_abbrev_set (section, 0,
6565 section->size, offset,
6566 NULL);
6567 if (list == NULL)
6568 break;
6570 if (list->first_abbrev)
6571 printf (_(" Number TAG (%#" PRIx64 ")\n"), offset);
6573 for (entry = list->first_abbrev; entry; entry = entry->next)
6575 abbrev_attr *attr;
6577 printf (" %ld %s [%s]\n",
6578 entry->number,
6579 get_TAG_name (entry->tag),
6580 entry->children ? _("has children") : _("no children"));
6582 for (attr = entry->first_attr; attr; attr = attr->next)
6584 printf (" %-18s %s",
6585 get_AT_name (attr->attribute),
6586 get_FORM_name (attr->form));
6587 if (attr->form == DW_FORM_implicit_const)
6588 printf (": %" PRId64, attr->implicit_const);
6589 putchar ('\n');
6592 start = list->start_of_next_abbrevs;
6593 free_abbrev_list (list);
6595 while (start);
6597 printf ("\n");
6599 return 1;
6602 /* Return true when ADDR is the maximum address, when addresses are
6603 POINTER_SIZE bytes long. */
6605 static bool
6606 is_max_address (uint64_t addr, unsigned int pointer_size)
6608 uint64_t mask = ~(~(uint64_t) 0 << 1 << (pointer_size * 8 - 1));
6609 return ((addr & mask) == mask);
6612 /* Display a view pair list starting at *VSTART_PTR and ending at
6613 VLISTEND within SECTION. */
6615 static void
6616 display_view_pair_list (struct dwarf_section *section,
6617 unsigned char **vstart_ptr,
6618 unsigned int debug_info_entry,
6619 unsigned char *vlistend)
6621 unsigned char *vstart = *vstart_ptr;
6622 unsigned char *section_end = section->start + section->size;
6623 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6625 if (vlistend < section_end)
6626 section_end = vlistend;
6628 putchar ('\n');
6630 while (vstart < section_end)
6632 uint64_t off = vstart - section->start;
6633 uint64_t vbegin, vend;
6635 READ_ULEB (vbegin, vstart, section_end);
6636 if (vstart == section_end)
6637 break;
6639 READ_ULEB (vend, vstart, section_end);
6640 printf (" %8.8" PRIx64 " ", off);
6642 print_view (vbegin, pointer_size);
6643 print_view (vend, pointer_size);
6644 printf (_("location view pair\n"));
6647 putchar ('\n');
6648 *vstart_ptr = vstart;
6651 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
6653 static void
6654 display_loc_list (struct dwarf_section *section,
6655 unsigned char **start_ptr,
6656 unsigned int debug_info_entry,
6657 uint64_t offset,
6658 uint64_t base_address,
6659 unsigned char **vstart_ptr,
6660 int has_frame_base)
6662 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6663 unsigned char *section_end = section->start + section->size;
6664 uint64_t cu_offset;
6665 unsigned int pointer_size;
6666 unsigned int offset_size;
6667 int dwarf_version;
6668 uint64_t begin;
6669 uint64_t end;
6670 unsigned short length;
6671 int need_frame_base;
6673 if (debug_info_entry >= num_debug_info_entries)
6675 warn (_("No debug information available for loc lists of entry: %u\n"),
6676 debug_info_entry);
6677 return;
6680 cu_offset = debug_information [debug_info_entry].cu_offset;
6681 pointer_size = debug_information [debug_info_entry].pointer_size;
6682 offset_size = debug_information [debug_info_entry].offset_size;
6683 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6685 if (pointer_size < 2 || pointer_size > 8)
6687 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6688 pointer_size, debug_info_entry);
6689 return;
6692 while (1)
6694 uint64_t off = offset + (start - *start_ptr);
6695 uint64_t vbegin = -1, vend = -1;
6697 if (2 * pointer_size > (size_t) (section_end - start))
6699 warn (_("Location list starting at offset %#" PRIx64
6700 " is not terminated.\n"), offset);
6701 break;
6704 printf (" ");
6705 print_hex (off, 4);
6707 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6708 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6710 if (begin == 0 && end == 0)
6712 /* PR 18374: In a object file we can have a location list that
6713 starts with a begin and end of 0 because there are relocations
6714 that need to be applied to the addresses. Actually applying
6715 the relocations now does not help as they will probably resolve
6716 to 0, since the object file has not been fully linked. Real
6717 end of list markers will not have any relocations against them. */
6718 if (! reloc_at (section, off)
6719 && ! reloc_at (section, off + pointer_size))
6721 printf (_("<End of list>\n"));
6722 break;
6726 /* Check base address specifiers. */
6727 if (is_max_address (begin, pointer_size)
6728 && !is_max_address (end, pointer_size))
6730 base_address = end;
6731 print_hex (begin, pointer_size);
6732 print_hex (end, pointer_size);
6733 printf (_("(base address)\n"));
6734 continue;
6737 if (vstart)
6739 off = offset + (vstart - *start_ptr);
6741 READ_ULEB (vbegin, vstart, section_end);
6742 print_view (vbegin, pointer_size);
6744 READ_ULEB (vend, vstart, section_end);
6745 print_view (vend, pointer_size);
6747 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6750 if (2 > (size_t) (section_end - start))
6752 warn (_("Location list starting at offset %#" PRIx64
6753 " is not terminated.\n"), offset);
6754 break;
6757 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6759 if (length > (size_t) (section_end - start))
6761 warn (_("Location list starting at offset %#" PRIx64
6762 " is not terminated.\n"), offset);
6763 break;
6766 print_hex (begin + base_address, pointer_size);
6767 print_hex (end + base_address, pointer_size);
6769 putchar ('(');
6770 need_frame_base = decode_location_expression (start,
6771 pointer_size,
6772 offset_size,
6773 dwarf_version,
6774 length,
6775 cu_offset, section);
6776 putchar (')');
6778 if (need_frame_base && !has_frame_base)
6779 printf (_(" [without DW_AT_frame_base]"));
6781 if (begin == end && vbegin == vend)
6782 fputs (_(" (start == end)"), stdout);
6783 else if (begin > end || (begin == end && vbegin > vend))
6784 fputs (_(" (start > end)"), stdout);
6786 putchar ('\n');
6788 start += length;
6791 *start_ptr = start;
6792 *vstart_ptr = vstart;
6795 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
6797 static void
6798 display_loclists_list (struct dwarf_section * section,
6799 unsigned char ** start_ptr,
6800 unsigned int debug_info_entry,
6801 uint64_t offset,
6802 uint64_t base_address,
6803 unsigned char ** vstart_ptr,
6804 int has_frame_base)
6806 unsigned char *start = *start_ptr;
6807 unsigned char *vstart = *vstart_ptr;
6808 unsigned char *section_end = section->start + section->size;
6809 uint64_t cu_offset;
6810 unsigned int pointer_size;
6811 unsigned int offset_size;
6812 unsigned int dwarf_version;
6814 /* Initialize it due to a false compiler warning. */
6815 uint64_t begin = -1, vbegin = -1;
6816 uint64_t end = -1, vend = -1;
6817 uint64_t length;
6818 int need_frame_base;
6820 if (debug_info_entry >= num_debug_info_entries)
6822 warn (_("No debug information available for "
6823 "loclists lists of entry: %u\n"),
6824 debug_info_entry);
6825 return;
6828 cu_offset = debug_information [debug_info_entry].cu_offset;
6829 pointer_size = debug_information [debug_info_entry].pointer_size;
6830 offset_size = debug_information [debug_info_entry].offset_size;
6831 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6833 if (pointer_size < 2 || pointer_size > 8)
6835 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6836 pointer_size, debug_info_entry);
6837 return;
6840 while (1)
6842 uint64_t off = offset + (start - *start_ptr);
6843 enum dwarf_location_list_entry_type llet;
6845 if (start + 1 > section_end)
6847 warn (_("Location list starting at offset %#" PRIx64
6848 " is not terminated.\n"), offset);
6849 break;
6852 printf (" ");
6853 print_hex (off, 4);
6855 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6857 if (vstart && (llet == DW_LLE_offset_pair
6858 || llet == DW_LLE_start_end
6859 || llet == DW_LLE_start_length))
6861 off = offset + (vstart - *start_ptr);
6863 READ_ULEB (vbegin, vstart, section_end);
6864 print_view (vbegin, pointer_size);
6866 READ_ULEB (vend, vstart, section_end);
6867 print_view (vend, pointer_size);
6869 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6872 switch (llet)
6874 case DW_LLE_end_of_list:
6875 printf (_("<End of list>\n"));
6876 break;
6878 case DW_LLE_base_addressx:
6879 READ_ULEB (base_address, start, section_end);
6880 print_hex (base_address, pointer_size);
6881 printf (_("(index into .debug_addr) "));
6882 base_address = fetch_indexed_addr (base_address, pointer_size);
6883 print_hex (base_address, pointer_size);
6884 printf (_("(base address)\n"));
6885 break;
6887 case DW_LLE_startx_endx:
6888 READ_ULEB (begin, start, section_end);
6889 begin = fetch_indexed_addr (begin, pointer_size);
6890 READ_ULEB (end, start, section_end);
6891 end = fetch_indexed_addr (end, pointer_size);
6892 break;
6894 case DW_LLE_startx_length:
6895 READ_ULEB (begin, start, section_end);
6896 begin = fetch_indexed_addr (begin, pointer_size);
6897 READ_ULEB (end, start, section_end);
6898 end += begin;
6899 break;
6901 case DW_LLE_default_location:
6902 begin = end = 0;
6903 break;
6905 case DW_LLE_offset_pair:
6906 READ_ULEB (begin, start, section_end);
6907 begin += base_address;
6908 READ_ULEB (end, start, section_end);
6909 end += base_address;
6910 break;
6912 case DW_LLE_base_address:
6913 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6914 section_end);
6915 print_hex (base_address, pointer_size);
6916 printf (_("(base address)\n"));
6917 break;
6919 case DW_LLE_start_end:
6920 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6921 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6922 break;
6924 case DW_LLE_start_length:
6925 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6926 READ_ULEB (end, start, section_end);
6927 end += begin;
6928 break;
6930 #ifdef DW_LLE_view_pair
6931 case DW_LLE_view_pair:
6932 if (vstart)
6933 printf (_("View pair entry in loclist with locviews attribute\n"));
6934 READ_ULEB (vbegin, start, section_end);
6935 print_view (vbegin, pointer_size);
6937 READ_ULEB (vend, start, section_end);
6938 print_view (vend, pointer_size);
6940 printf (_("views for:\n"));
6941 continue;
6942 #endif
6944 default:
6945 error (_("Invalid location list entry type %d\n"), llet);
6946 return;
6949 if (llet == DW_LLE_end_of_list)
6950 break;
6952 if (llet == DW_LLE_base_address
6953 || llet == DW_LLE_base_addressx)
6954 continue;
6956 if (start == section_end)
6958 warn (_("Location list starting at offset %#" PRIx64
6959 " is not terminated.\n"), offset);
6960 break;
6962 READ_ULEB (length, start, section_end);
6964 if (length > (size_t) (section_end - start))
6966 warn (_("Location list starting at offset %#" PRIx64
6967 " is not terminated.\n"), offset);
6968 break;
6971 print_hex (begin, pointer_size);
6972 print_hex (end, pointer_size);
6974 putchar ('(');
6975 need_frame_base = decode_location_expression (start,
6976 pointer_size,
6977 offset_size,
6978 dwarf_version,
6979 length,
6980 cu_offset, section);
6981 putchar (')');
6983 if (need_frame_base && !has_frame_base)
6984 printf (_(" [without DW_AT_frame_base]"));
6986 if (begin == end && vbegin == vend)
6987 fputs (_(" (start == end)"), stdout);
6988 else if (begin > end || (begin == end && vbegin > vend))
6989 fputs (_(" (start > end)"), stdout);
6991 putchar ('\n');
6993 start += length;
6994 vbegin = vend = -1;
6997 if (vbegin != (uint64_t) -1 || vend != (uint64_t) -1)
6998 printf (_("Trailing view pair not used in a range"));
7000 *start_ptr = start;
7001 *vstart_ptr = vstart;
7004 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
7005 right-adjusted in a field of length LEN, and followed by a space. */
7007 static void
7008 print_addr_index (unsigned int idx, unsigned int len)
7010 static char buf[15];
7011 snprintf (buf, sizeof (buf), "[%d]", idx);
7012 printf ("%*s ", len, buf);
7015 /* Display a location list from a .dwo section. It uses address indexes rather
7016 than embedded addresses. This code closely follows display_loc_list, but the
7017 two are sufficiently different that combining things is very ugly. */
7019 static void
7020 display_loc_list_dwo (struct dwarf_section *section,
7021 unsigned char **start_ptr,
7022 unsigned int debug_info_entry,
7023 uint64_t offset,
7024 unsigned char **vstart_ptr,
7025 int has_frame_base)
7027 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
7028 unsigned char *section_end = section->start + section->size;
7029 uint64_t cu_offset;
7030 unsigned int pointer_size;
7031 unsigned int offset_size;
7032 int dwarf_version;
7033 int entry_type;
7034 unsigned short length;
7035 int need_frame_base;
7036 unsigned int idx;
7038 if (debug_info_entry >= num_debug_info_entries)
7040 warn (_("No debug information for loc lists of entry: %u\n"),
7041 debug_info_entry);
7042 return;
7045 cu_offset = debug_information [debug_info_entry].cu_offset;
7046 pointer_size = debug_information [debug_info_entry].pointer_size;
7047 offset_size = debug_information [debug_info_entry].offset_size;
7048 dwarf_version = debug_information [debug_info_entry].dwarf_version;
7050 if (pointer_size < 2 || pointer_size > 8)
7052 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
7053 pointer_size, debug_info_entry);
7054 return;
7057 while (1)
7059 printf (" ");
7060 print_hex (offset + (start - *start_ptr), 4);
7062 if (start >= section_end)
7064 warn (_("Location list starting at offset %#" PRIx64
7065 " is not terminated.\n"), offset);
7066 break;
7069 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
7071 if (vstart)
7072 switch (entry_type)
7074 default:
7075 break;
7077 case 2:
7078 case 3:
7079 case 4:
7081 uint64_t view;
7082 uint64_t off = offset + (vstart - *start_ptr);
7084 READ_ULEB (view, vstart, section_end);
7085 print_view (view, 8);
7087 READ_ULEB (view, vstart, section_end);
7088 print_view (view, 8);
7090 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
7093 break;
7096 switch (entry_type)
7098 case 0: /* A terminating entry. */
7099 *start_ptr = start;
7100 *vstart_ptr = vstart;
7101 printf (_("<End of list>\n"));
7102 return;
7103 case 1: /* A base-address entry. */
7104 READ_ULEB (idx, start, section_end);
7105 print_addr_index (idx, 8);
7106 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
7107 printf (_("(base address selection entry)\n"));
7108 continue;
7109 case 2: /* A start/end entry. */
7110 READ_ULEB (idx, start, section_end);
7111 print_addr_index (idx, 8);
7112 READ_ULEB (idx, start, section_end);
7113 print_addr_index (idx, 8);
7114 break;
7115 case 3: /* A start/length entry. */
7116 READ_ULEB (idx, start, section_end);
7117 print_addr_index (idx, 8);
7118 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
7119 printf ("%08x ", idx);
7120 break;
7121 case 4: /* An offset pair entry. */
7122 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
7123 printf ("%08x ", idx);
7124 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
7125 printf ("%08x ", idx);
7126 break;
7127 default:
7128 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
7129 *start_ptr = start;
7130 *vstart_ptr = vstart;
7131 return;
7134 if (2 > (size_t) (section_end - start))
7136 warn (_("Location list starting at offset %#" PRIx64
7137 " is not terminated.\n"), offset);
7138 break;
7141 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
7142 if (length > (size_t) (section_end - start))
7144 warn (_("Location list starting at offset %#" PRIx64
7145 " is not terminated.\n"), offset);
7146 break;
7149 putchar ('(');
7150 need_frame_base = decode_location_expression (start,
7151 pointer_size,
7152 offset_size,
7153 dwarf_version,
7154 length,
7155 cu_offset, section);
7156 putchar (')');
7158 if (need_frame_base && !has_frame_base)
7159 printf (_(" [without DW_AT_frame_base]"));
7161 putchar ('\n');
7163 start += length;
7166 *start_ptr = start;
7167 *vstart_ptr = vstart;
7170 /* Sort array of indexes in ascending order of loc_offsets[idx] and
7171 loc_views. */
7173 static uint64_t *loc_offsets, *loc_views;
7175 static int
7176 loc_offsets_compar (const void *ap, const void *bp)
7178 uint64_t a = loc_offsets[*(const unsigned int *) ap];
7179 uint64_t b = loc_offsets[*(const unsigned int *) bp];
7181 int ret = (a > b) - (b > a);
7182 if (ret)
7183 return ret;
7185 a = loc_views[*(const unsigned int *) ap];
7186 b = loc_views[*(const unsigned int *) bp];
7188 ret = (a > b) - (b > a);
7190 return ret;
7193 static int
7194 display_offset_entry_loclists (struct dwarf_section *section)
7196 unsigned char * start = section->start;
7197 unsigned char * const end = start + section->size;
7199 introduce (section, false);
7203 uint64_t length;
7204 unsigned short version;
7205 unsigned char address_size;
7206 unsigned char segment_selector_size;
7207 uint32_t offset_entry_count;
7208 uint32_t i;
7209 bool is_64bit;
7211 printf (_("Table at Offset %#tx\n"), start - section->start);
7213 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
7214 if (length == 0xffffffff)
7216 is_64bit = true;
7217 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
7219 else
7220 is_64bit = false;
7222 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
7223 SAFE_BYTE_GET_AND_INC (address_size, start, 1, end);
7224 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, end);
7225 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, end);
7227 printf (_(" Length: %#" PRIx64 "\n"), length);
7228 printf (_(" DWARF version: %u\n"), version);
7229 printf (_(" Address size: %u\n"), address_size);
7230 printf (_(" Segment size: %u\n"), segment_selector_size);
7231 printf (_(" Offset entries: %u\n"), offset_entry_count);
7233 if (version < 5)
7235 warn (_("The %s section contains a corrupt or "
7236 "unsupported version number: %d.\n"),
7237 section->name, version);
7238 return 0;
7241 if (segment_selector_size != 0)
7243 warn (_("The %s section contains an "
7244 "unsupported segment selector size: %d.\n"),
7245 section->name, segment_selector_size);
7246 return 0;
7249 if (offset_entry_count == 0)
7251 warn (_("The %s section contains a table without offset\n"),
7252 section->name);
7253 return 0;
7256 printf (_("\n Offset Entries starting at %#tx:\n"),
7257 start - section->start);
7259 for (i = 0; i < offset_entry_count; i++)
7261 uint64_t entry;
7263 SAFE_BYTE_GET_AND_INC (entry, start, is_64bit ? 8 : 4, end);
7264 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
7267 putchar ('\n');
7269 uint32_t j;
7271 for (j = 1, i = 0; i < offset_entry_count;)
7273 unsigned char lle;
7274 uint64_t base_address = 0;
7275 uint64_t begin;
7276 uint64_t finish;
7277 uint64_t off = start - section->start;
7279 if (j != i)
7281 printf (_(" Offset Entry %u\n"), i);
7282 j = i;
7285 printf (" ");
7286 print_hex (off, 4);
7288 SAFE_BYTE_GET_AND_INC (lle, start, 1, end);
7290 switch (lle)
7292 case DW_LLE_end_of_list:
7293 printf (_("<End of list>\n\n"));
7294 i ++;
7295 continue;
7297 case DW_LLE_base_addressx:
7298 READ_ULEB (base_address, start, end);
7299 print_hex (base_address, address_size);
7300 printf (_("(index into .debug_addr) "));
7301 base_address = fetch_indexed_addr (base_address, address_size);
7302 print_hex (base_address, address_size);
7303 printf (_("(base address)\n"));
7304 continue;
7306 case DW_LLE_startx_endx:
7307 READ_ULEB (begin, start, end);
7308 begin = fetch_indexed_addr (begin, address_size);
7309 READ_ULEB (finish, start, end);
7310 finish = fetch_indexed_addr (finish, address_size);
7311 break;
7313 case DW_LLE_startx_length:
7314 READ_ULEB (begin, start, end);
7315 begin = fetch_indexed_addr (begin, address_size);
7316 READ_ULEB (finish, start, end);
7317 finish += begin;
7318 break;
7320 case DW_LLE_offset_pair:
7321 READ_ULEB (begin, start, end);
7322 begin += base_address;
7323 READ_ULEB (finish, start, end);
7324 finish += base_address;
7325 break;
7327 case DW_LLE_default_location:
7328 begin = finish = 0;
7329 break;
7331 case DW_LLE_base_address:
7332 SAFE_BYTE_GET_AND_INC (base_address, start, address_size, end);
7333 print_hex (base_address, address_size);
7334 printf (_("(base address)\n"));
7335 continue;
7337 case DW_LLE_start_end:
7338 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7339 SAFE_BYTE_GET_AND_INC (finish, start, address_size, end);
7340 break;
7342 case DW_LLE_start_length:
7343 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7344 READ_ULEB (finish, start, end);
7345 finish += begin;
7346 break;
7348 default:
7349 error (_("Invalid location list entry type %d\n"), lle);
7350 return 0;
7353 if (start == end)
7355 warn (_("Location list starting at offset %#" PRIx64
7356 " is not terminated.\n"), off);
7357 break;
7360 print_hex (begin, address_size);
7361 print_hex (finish, address_size);
7363 if (begin == finish)
7364 fputs (_("(start == end)"), stdout);
7365 else if (begin > finish)
7366 fputs (_("(start > end)"), stdout);
7368 /* Read the counted location descriptions. */
7369 READ_ULEB (length, start, end);
7371 if (length > (size_t) (end - start))
7373 warn (_("Location list starting at offset %#" PRIx64
7374 " is not terminated.\n"), off);
7375 break;
7378 (void) decode_location_expression (start, address_size, address_size,
7379 version, length, 0, section);
7380 start += length;
7381 putchar ('\n');
7384 putchar ('\n');
7386 while (start < end);
7388 return 1;
7391 static int
7392 display_debug_loc (struct dwarf_section *section, void *file)
7394 unsigned char *start = section->start, *vstart = NULL;
7395 uint64_t bytes;
7396 unsigned char *section_begin = start;
7397 unsigned int num_loc_list = 0;
7398 uint64_t last_offset = 0;
7399 uint64_t last_view = 0;
7400 unsigned int first = 0;
7401 unsigned int i;
7402 unsigned int j;
7403 int seen_first_offset = 0;
7404 int locs_sorted = 1;
7405 unsigned char *next = start, *vnext = vstart;
7406 unsigned int *array = NULL;
7407 const char *suffix = strrchr (section->name, '.');
7408 bool is_dwo = false;
7409 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
7410 uint64_t header_size = 0;
7412 if (suffix && strcmp (suffix, ".dwo") == 0)
7413 is_dwo = true;
7415 bytes = section->size;
7417 if (bytes == 0)
7419 printf (_("\nThe %s section is empty.\n"), section->name);
7420 return 0;
7423 if (is_loclists)
7425 unsigned char *hdrptr = section_begin;
7426 uint64_t ll_length;
7427 unsigned short ll_version;
7428 unsigned char *end = section_begin + section->size;
7429 unsigned char address_size, segment_selector_size;
7430 uint32_t offset_entry_count;
7432 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
7433 if (ll_length == 0xffffffff)
7434 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
7436 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
7437 if (ll_version != 5)
7439 warn (_("The %s section contains corrupt or "
7440 "unsupported version number: %d.\n"),
7441 section->name, ll_version);
7442 return 0;
7445 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
7447 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
7448 if (segment_selector_size != 0)
7450 warn (_("The %s section contains "
7451 "unsupported segment selector size: %d.\n"),
7452 section->name, segment_selector_size);
7453 return 0;
7456 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
7458 if (offset_entry_count != 0)
7459 return display_offset_entry_loclists (section);
7461 header_size = hdrptr - section_begin;
7464 if (load_debug_info (file) == 0)
7466 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7467 section->name);
7468 return 0;
7471 /* Check the order of location list in .debug_info section. If
7472 offsets of location lists are in the ascending order, we can
7473 use `debug_information' directly. */
7474 for (i = 0; i < num_debug_info_entries; i++)
7476 unsigned int num;
7478 num = debug_information [i].num_loc_offsets;
7479 if (num > num_loc_list)
7480 num_loc_list = num;
7482 /* Check if we can use `debug_information' directly. */
7483 if (locs_sorted && num != 0)
7485 if (!seen_first_offset)
7487 /* This is the first location list. */
7488 last_offset = debug_information [i].loc_offsets [0];
7489 last_view = debug_information [i].loc_views [0];
7490 first = i;
7491 seen_first_offset = 1;
7492 j = 1;
7494 else
7495 j = 0;
7497 for (; j < num; j++)
7499 if (last_offset >
7500 debug_information [i].loc_offsets [j]
7501 || (last_offset == debug_information [i].loc_offsets [j]
7502 && last_view > debug_information [i].loc_views [j]))
7504 locs_sorted = 0;
7505 break;
7507 last_offset = debug_information [i].loc_offsets [j];
7508 last_view = debug_information [i].loc_views [j];
7513 if (!seen_first_offset)
7514 error (_("No location lists in .debug_info section!\n"));
7516 if (debug_information [first].num_loc_offsets > 0
7517 && debug_information [first].loc_offsets [0] != header_size
7518 && debug_information [first].loc_views [0] != header_size)
7519 warn (_("Location lists in %s section start at %#" PRIx64
7520 " rather than %#" PRIx64 "\n"),
7521 section->name, debug_information [first].loc_offsets [0],
7522 header_size);
7524 if (!locs_sorted)
7525 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
7527 introduce (section, false);
7529 if (reloc_at (section, 0))
7530 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
7532 printf (_(" Offset Begin End Expression\n"));
7534 for (i = first; i < num_debug_info_entries; i++)
7536 uint64_t offset = 0, voffset = 0;
7537 uint64_t base_address;
7538 unsigned int k;
7539 int has_frame_base;
7541 if (!locs_sorted)
7543 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7544 array[k] = k;
7545 loc_offsets = debug_information [i].loc_offsets;
7546 loc_views = debug_information [i].loc_views;
7547 qsort (array, debug_information [i].num_loc_offsets,
7548 sizeof (*array), loc_offsets_compar);
7551 /* .debug_loclists has a per-unit header.
7552 Update start if we are detecting it. */
7553 if (debug_information [i].dwarf_version == 5)
7555 j = locs_sorted ? 0 : array [0];
7557 if (debug_information [i].num_loc_offsets)
7558 offset = debug_information [i].loc_offsets [j];
7560 if (debug_information [i].num_loc_views)
7561 voffset = debug_information [i].loc_views [j];
7563 /* Assume that the size of the header is constant across CUs. */
7564 if (((start - section_begin) + header_size == offset)
7565 || ((start -section_begin) + header_size == voffset))
7566 start += header_size;
7569 int adjacent_view_loclists = 1;
7570 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7572 j = locs_sorted ? k : array[k];
7573 if (k
7574 && (debug_information [i].loc_offsets [locs_sorted
7575 ? k - 1 : array [k - 1]]
7576 == debug_information [i].loc_offsets [j])
7577 && (debug_information [i].loc_views [locs_sorted
7578 ? k - 1 : array [k - 1]]
7579 == debug_information [i].loc_views [j]))
7580 continue;
7581 has_frame_base = debug_information [i].have_frame_base [j];
7582 offset = debug_information [i].loc_offsets [j];
7583 next = section_begin + offset;
7584 voffset = debug_information [i].loc_views [j];
7585 if (voffset != (uint64_t) -1)
7586 vnext = section_begin + voffset;
7587 else
7588 vnext = NULL;
7589 base_address = debug_information [i].base_address;
7591 if (vnext && vnext < next)
7593 vstart = vnext;
7594 display_view_pair_list (section, &vstart, i, next);
7595 if (start == vnext)
7596 start = vstart;
7599 if (start < next)
7601 if (vnext && vnext < next)
7602 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7603 " in %s section.\n"),
7604 start - section_begin, voffset, section->name);
7605 else
7606 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7607 " in %s section.\n"),
7608 start - section_begin, offset, section->name);
7610 else if (start > next)
7611 warn (_("There is an overlap [%#tx - %#" PRIx64 "]"
7612 " in %s section.\n"),
7613 start - section_begin, offset, section->name);
7614 start = next;
7615 vstart = vnext;
7617 if (offset >= bytes)
7619 warn (_("Offset %#" PRIx64 " is bigger than %s section size.\n"),
7620 offset, section->name);
7621 continue;
7624 if (vnext && voffset >= bytes)
7626 warn (_("View Offset %#" PRIx64 " is bigger than %s section size.\n"),
7627 voffset, section->name);
7628 continue;
7631 if (!is_loclists)
7633 if (is_dwo)
7634 display_loc_list_dwo (section, &start, i, offset,
7635 &vstart, has_frame_base);
7636 else
7637 display_loc_list (section, &start, i, offset, base_address,
7638 &vstart, has_frame_base);
7640 else
7642 if (is_dwo)
7643 warn (_("DWO is not yet supported.\n"));
7644 else
7645 display_loclists_list (section, &start, i, offset, base_address,
7646 &vstart, has_frame_base);
7649 /* FIXME: this arrangement is quite simplistic. Nothing
7650 requires locview lists to be adjacent to corresponding
7651 loclists, and a single loclist could be augmented by
7652 different locview lists, and vice-versa, unlikely as it
7653 is that it would make sense to do so. Hopefully we'll
7654 have view pair support built into loclists before we ever
7655 need to address all these possibilities. */
7656 if (adjacent_view_loclists && vnext
7657 && vnext != start && vstart != next)
7659 adjacent_view_loclists = 0;
7660 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
7663 if (vnext && vnext == start)
7664 display_view_pair_list (section, &start, i, vstart);
7668 if (start < section->start + section->size)
7669 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7670 "There are %ld unused bytes at the end of section %s\n",
7671 (long) (section->start + section->size - start)),
7672 (long) (section->start + section->size - start), section->name);
7673 putchar ('\n');
7674 free (array);
7675 return 1;
7678 static int
7679 display_debug_str (struct dwarf_section *section,
7680 void *file ATTRIBUTE_UNUSED)
7682 unsigned char *start = section->start;
7683 uint64_t bytes = section->size;
7684 uint64_t addr = section->address;
7686 if (bytes == 0)
7688 printf (_("\nThe %s section is empty.\n"), section->name);
7689 return 0;
7692 introduce (section, false);
7694 while (bytes)
7696 int j;
7697 int k;
7698 int lbytes;
7700 lbytes = (bytes > 16 ? 16 : bytes);
7702 printf (" 0x%8.8" PRIx64 " ", addr);
7704 for (j = 0; j < 16; j++)
7706 if (j < lbytes)
7707 printf ("%2.2x", start[j]);
7708 else
7709 printf (" ");
7711 if ((j & 3) == 3)
7712 printf (" ");
7715 for (j = 0; j < lbytes; j++)
7717 k = start[j];
7718 if (k >= ' ' && k < 0x80)
7719 printf ("%c", k);
7720 else
7721 printf (".");
7724 putchar ('\n');
7726 start += lbytes;
7727 addr += lbytes;
7728 bytes -= lbytes;
7731 putchar ('\n');
7733 return 1;
7736 static int
7737 display_debug_info (struct dwarf_section *section, void *file)
7739 return process_debug_info (section, file, section->abbrev_sec, false, false);
7742 static int
7743 display_debug_types (struct dwarf_section *section, void *file)
7745 return process_debug_info (section, file, section->abbrev_sec, false, true);
7748 static int
7749 display_trace_info (struct dwarf_section *section, void *file)
7751 return process_debug_info (section, file, section->abbrev_sec, false, true);
7754 static int
7755 display_debug_aranges (struct dwarf_section *section,
7756 void *file ATTRIBUTE_UNUSED)
7758 unsigned char *start = section->start;
7759 unsigned char *end = start + section->size;
7761 introduce (section, false);
7763 /* It does not matter if this load fails,
7764 we test for that later on. */
7765 load_debug_info (file);
7767 while (start < end)
7769 unsigned char *hdrptr;
7770 DWARF2_Internal_ARange arange;
7771 unsigned char *addr_ranges;
7772 uint64_t length;
7773 uint64_t address;
7774 uint64_t sec_off;
7775 unsigned char address_size;
7776 unsigned int offset_size;
7777 unsigned char *end_ranges;
7779 hdrptr = start;
7780 sec_off = hdrptr - section->start;
7782 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
7783 if (arange.ar_length == 0xffffffff)
7785 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
7786 offset_size = 8;
7788 else
7789 offset_size = 4;
7791 if (arange.ar_length > (size_t) (end - hdrptr))
7793 warn (_("Debug info is corrupted, %s header at %#" PRIx64
7794 " has length %#" PRIx64 "\n"),
7795 section->name, sec_off, arange.ar_length);
7796 break;
7798 end_ranges = hdrptr + arange.ar_length;
7800 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end_ranges);
7801 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size,
7802 end_ranges);
7804 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7805 && num_debug_info_entries > 0
7806 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7807 warn (_(".debug_info offset of %#" PRIx64
7808 " in %s section does not point to a CU header.\n"),
7809 arange.ar_info_offset, section->name);
7811 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end_ranges);
7812 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end_ranges);
7814 if (arange.ar_version != 2 && arange.ar_version != 3)
7816 /* PR 19872: A version number of 0 probably means that there is
7817 padding at the end of the .debug_aranges section. Gold puts
7818 it there when performing an incremental link, for example.
7819 So do not generate a warning in this case. */
7820 if (arange.ar_version)
7821 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
7822 break;
7825 printf (_(" Length: %" PRId64 "\n"), arange.ar_length);
7826 printf (_(" Version: %d\n"), arange.ar_version);
7827 printf (_(" Offset into .debug_info: %#" PRIx64 "\n"),
7828 arange.ar_info_offset);
7829 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
7830 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
7832 address_size = arange.ar_pointer_size + arange.ar_segment_size;
7834 /* PR 17512: file: 001-108546-0.001:0.1. */
7835 if (address_size == 0 || address_size > 8)
7837 error (_("Invalid address size in %s section!\n"),
7838 section->name);
7839 break;
7842 /* The DWARF spec does not require that the address size be a power
7843 of two, but we do. This will have to change if we ever encounter
7844 an uneven architecture. */
7845 if ((address_size & (address_size - 1)) != 0)
7847 warn (_("Pointer size + Segment size is not a power of two.\n"));
7848 break;
7851 if (address_size > 4)
7852 printf (_("\n Address Length\n"));
7853 else
7854 printf (_("\n Address Length\n"));
7856 addr_ranges = hdrptr;
7858 /* Must pad to an alignment boundary that is twice the address size. */
7859 addr_ranges += (2 * address_size - 1
7860 - (hdrptr - start - 1) % (2 * address_size));
7862 while (2 * address_size <= end_ranges - addr_ranges)
7864 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size,
7865 end_ranges);
7866 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size,
7867 end_ranges);
7868 printf (" ");
7869 print_hex (address, address_size);
7870 print_hex_ns (length, address_size);
7871 putchar ('\n');
7874 start = end_ranges;
7877 printf ("\n");
7879 return 1;
7882 /* Comparison function for qsort. */
7883 static int
7884 comp_addr_base (const void * v0, const void * v1)
7886 debug_info *info0 = *(debug_info **) v0;
7887 debug_info *info1 = *(debug_info **) v1;
7888 return info0->addr_base - info1->addr_base;
7891 /* Display the debug_addr section. */
7892 static int
7893 display_debug_addr (struct dwarf_section *section,
7894 void *file)
7896 debug_info **debug_addr_info;
7897 unsigned char *entry;
7898 unsigned char *end;
7899 unsigned int i;
7900 unsigned int count;
7901 unsigned char * header;
7903 if (section->size == 0)
7905 printf (_("\nThe %s section is empty.\n"), section->name);
7906 return 0;
7909 if (load_debug_info (file) == 0)
7911 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7912 section->name);
7913 return 0;
7916 introduce (section, false);
7918 /* PR 17531: file: cf38d01b.
7919 We use xcalloc because a corrupt file may not have initialised all of the
7920 fields in the debug_info structure, which means that the sort below might
7921 try to move uninitialised data. */
7922 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
7923 sizeof (debug_info *));
7925 count = 0;
7926 for (i = 0; i < num_debug_info_entries; i++)
7927 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
7929 /* PR 17531: file: cf38d01b. */
7930 if (debug_information[i].addr_base >= section->size)
7931 warn (_("Corrupt address base (%#" PRIx64 ")"
7932 " found in debug section %u\n"),
7933 debug_information[i].addr_base, i);
7934 else
7935 debug_addr_info [count++] = debug_information + i;
7938 /* Add a sentinel to make iteration convenient. */
7939 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7940 debug_addr_info [count]->addr_base = section->size;
7941 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
7943 header = section->start;
7944 for (i = 0; i < count; i++)
7946 unsigned int idx;
7947 unsigned int address_size = debug_addr_info [i]->pointer_size;
7949 printf (_(" For compilation unit at offset %#" PRIx64 ":\n"),
7950 debug_addr_info [i]->cu_offset);
7952 printf (_("\tIndex\tAddress\n"));
7953 entry = section->start + debug_addr_info [i]->addr_base;
7954 if (debug_addr_info [i]->dwarf_version >= 5)
7956 size_t header_size = entry - header;
7957 unsigned char *curr_header = header;
7958 uint64_t length;
7959 int version;
7960 int segment_selector_size;
7962 if (header_size != 8 && header_size != 16)
7964 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %zd instead\n"),
7965 section->name, header_size);
7966 return 0;
7969 SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7970 if (length == 0xffffffff)
7971 SAFE_BYTE_GET_AND_INC (length, curr_header, 8, entry);
7972 if (length > (size_t) (section->start + section->size - curr_header)
7973 || length < (size_t) (entry - curr_header))
7975 warn (_("Corrupt %s section: unit_length field of %#" PRIx64
7976 " is invalid\n"), section->name, length);
7977 return 0;
7979 end = curr_header + length;
7980 SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7981 if (version != 5)
7982 warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7983 section->name, version);
7985 SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7986 SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7987 address_size += segment_selector_size;
7989 else
7990 end = section->start + debug_addr_info [i + 1]->addr_base;
7992 header = end;
7993 idx = 0;
7995 if (address_size < 1 || address_size > sizeof (uint64_t))
7997 warn (_("Corrupt %s section: address size (%x) is wrong"),
7998 section->name, address_size);
7999 return 0;
8002 while ((size_t) (end - entry) >= address_size)
8004 uint64_t base = byte_get (entry, address_size);
8005 printf (_("\t%d:\t"), idx);
8006 print_hex_ns (base, address_size);
8007 printf ("\n");
8008 entry += address_size;
8009 idx++;
8012 printf ("\n");
8014 free (debug_addr_info);
8015 return 1;
8018 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
8020 static int
8021 display_debug_str_offsets (struct dwarf_section *section,
8022 void *file ATTRIBUTE_UNUSED)
8024 unsigned long idx;
8026 if (section->size == 0)
8028 printf (_("\nThe %s section is empty.\n"), section->name);
8029 return 0;
8032 unsigned char *start = section->start;
8033 unsigned char *end = start + section->size;
8034 unsigned char *curr = start;
8035 uint64_t debug_str_offsets_hdr_len;
8037 const char *suffix = strrchr (section->name, '.');
8038 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
8040 if (dwo)
8041 load_debug_section_with_follow (str_dwo, file);
8042 else
8043 load_debug_section_with_follow (str, file);
8045 introduce (section, false);
8047 while (curr < end)
8049 uint64_t length;
8050 uint64_t entry_length;
8052 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
8053 /* FIXME: We assume that this means 64-bit DWARF is being used. */
8054 if (length == 0xffffffff)
8056 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
8057 entry_length = 8;
8058 debug_str_offsets_hdr_len = 16;
8060 else
8062 entry_length = 4;
8063 debug_str_offsets_hdr_len = 8;
8066 unsigned char *entries_end;
8067 if (length == 0)
8069 /* This is probably an old style .debug_str_offset section which
8070 just contains offsets and no header (and the first offset is 0). */
8071 length = section->size;
8072 curr = section->start;
8073 entries_end = end;
8075 printf (_(" Length: %#" PRIx64 "\n"), length);
8076 printf (_(" Index Offset [String]\n"));
8078 else
8080 if (length <= (size_t) (end - curr))
8081 entries_end = curr + length;
8082 else
8084 warn (_("Section %s is too small %#" PRIx64 "\n"),
8085 section->name, section->size);
8086 entries_end = end;
8089 int version;
8090 SAFE_BYTE_GET_AND_INC (version, curr, 2, entries_end);
8091 if (version != 5)
8092 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
8094 int padding;
8095 SAFE_BYTE_GET_AND_INC (padding, curr, 2, entries_end);
8096 if (padding != 0)
8097 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
8099 printf (_(" Length: %#" PRIx64 "\n"), length);
8100 printf (_(" Version: %#x\n"), version);
8101 printf (_(" Index Offset [String]\n"));
8104 for (idx = 0; curr < entries_end; idx++)
8106 uint64_t offset;
8107 const unsigned char * string;
8109 if ((size_t) (entries_end - curr) < entry_length)
8110 /* Not enough space to read one entry_length, give up. */
8111 return 0;
8113 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, entries_end);
8114 if (dwo)
8115 string = (const unsigned char *)
8116 fetch_indexed_string (idx, NULL, entry_length, dwo, debug_str_offsets_hdr_len);
8117 else
8118 string = fetch_indirect_string (offset);
8120 printf (" %8lu ", idx);
8121 print_hex (offset, entry_length);
8122 printf (" %s\n", string);
8126 return 1;
8129 /* Each debug_information[x].range_lists[y] gets this representation for
8130 sorting purposes. */
8132 struct range_entry
8134 /* The debug_information[x].range_lists[y] value. */
8135 uint64_t ranges_offset;
8137 /* Original debug_information to find parameters of the data. */
8138 debug_info *debug_info_p;
8141 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
8143 static int
8144 range_entry_compar (const void *ap, const void *bp)
8146 const struct range_entry *a_re = (const struct range_entry *) ap;
8147 const struct range_entry *b_re = (const struct range_entry *) bp;
8148 const uint64_t a = a_re->ranges_offset;
8149 const uint64_t b = b_re->ranges_offset;
8151 return (a > b) - (b > a);
8154 static void
8155 display_debug_ranges_list (unsigned char * start,
8156 unsigned char * finish,
8157 unsigned int pointer_size,
8158 uint64_t offset,
8159 uint64_t base_address)
8161 while (start < finish)
8163 uint64_t begin;
8164 uint64_t end;
8166 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8167 if (start >= finish)
8168 break;
8169 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8171 printf (" ");
8172 print_hex (offset, 4);
8174 if (begin == 0 && end == 0)
8176 printf (_("<End of list>\n"));
8177 break;
8180 /* Check base address specifiers. */
8181 if (is_max_address (begin, pointer_size)
8182 && !is_max_address (end, pointer_size))
8184 base_address = end;
8185 print_hex (begin, pointer_size);
8186 print_hex (end, pointer_size);
8187 printf ("(base address)\n");
8188 continue;
8191 print_hex (begin + base_address, pointer_size);
8192 print_hex_ns (end + base_address, pointer_size);
8194 if (begin == end)
8195 fputs (_(" (start == end)"), stdout);
8196 else if (begin > end)
8197 fputs (_(" (start > end)"), stdout);
8199 putchar ('\n');
8203 static unsigned char *
8204 display_debug_rnglists_list (unsigned char * start,
8205 unsigned char * finish,
8206 unsigned int pointer_size,
8207 uint64_t offset,
8208 uint64_t base_address,
8209 uint64_t addr_base)
8211 unsigned char *next = start;
8213 while (1)
8215 uint64_t off = offset + (start - next);
8216 enum dwarf_range_list_entry rlet;
8217 /* Initialize it due to a false compiler warning. */
8218 uint64_t begin = -1, length, end = -1;
8220 if (start >= finish)
8222 warn (_("Range list starting at offset %#" PRIx64
8223 " is not terminated.\n"), offset);
8224 break;
8227 printf (" ");
8228 print_hex (off, 4);
8230 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
8232 switch (rlet)
8234 case DW_RLE_end_of_list:
8235 printf (_("<End of list>\n"));
8236 break;
8237 case DW_RLE_base_addressx:
8238 READ_ULEB (base_address, start, finish);
8239 print_hex (base_address, pointer_size);
8240 printf (_("(base address index) "));
8241 base_address = fetch_indexed_addr ((base_address * pointer_size) + addr_base,
8242 pointer_size);
8243 print_hex (base_address, pointer_size);
8244 printf (_("(base address)\n"));
8245 break;
8246 case DW_RLE_startx_endx:
8247 READ_ULEB (begin, start, finish);
8248 READ_ULEB (end, start, finish);
8249 begin = fetch_indexed_addr ((begin * pointer_size) + addr_base,
8250 pointer_size);
8251 end = fetch_indexed_addr ((begin * pointer_size) + addr_base,
8252 pointer_size);
8253 break;
8254 case DW_RLE_startx_length:
8255 READ_ULEB (begin, start, finish);
8256 READ_ULEB (length, start, finish);
8257 begin = fetch_indexed_addr ((begin * pointer_size) + addr_base,
8258 pointer_size);
8259 end = begin + length;
8260 break;
8261 case DW_RLE_offset_pair:
8262 READ_ULEB (begin, start, finish);
8263 READ_ULEB (end, start, finish);
8264 break;
8265 case DW_RLE_base_address:
8266 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
8267 print_hex (base_address, pointer_size);
8268 printf (_("(base address)\n"));
8269 break;
8270 case DW_RLE_start_end:
8271 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8272 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8273 break;
8274 case DW_RLE_start_length:
8275 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8276 READ_ULEB (length, start, finish);
8277 end = begin + length;
8278 break;
8279 default:
8280 error (_("Invalid range list entry type %d\n"), rlet);
8281 rlet = DW_RLE_end_of_list;
8282 break;
8285 if (rlet == DW_RLE_end_of_list)
8286 break;
8287 if (rlet == DW_RLE_base_address || rlet == DW_RLE_base_addressx)
8288 continue;
8290 /* Only a DW_RLE_offset_pair needs the base address added. */
8291 if (rlet == DW_RLE_offset_pair)
8293 begin += base_address;
8294 end += base_address;
8297 print_hex (begin, pointer_size);
8298 print_hex (end, pointer_size);
8300 if (begin == end)
8301 fputs (_(" (start == end)"), stdout);
8302 else if (begin > end)
8303 fputs (_(" (start > end)"), stdout);
8305 putchar ('\n');
8308 return start;
8311 static int
8312 display_debug_rnglists_unit_header (struct dwarf_section * section,
8313 uint64_t * unit_offset,
8314 unsigned char * poffset_size)
8316 uint64_t start_offset = *unit_offset;
8317 unsigned char * p = section->start + start_offset;
8318 unsigned char * finish = section->start + section->size;
8319 uint64_t initial_length;
8320 unsigned char segment_selector_size;
8321 unsigned int offset_entry_count;
8322 unsigned int i;
8323 unsigned short version;
8324 unsigned char address_size = 0;
8325 unsigned char offset_size;
8327 /* Get and check the length of the block. */
8328 SAFE_BYTE_GET_AND_INC (initial_length, p, 4, finish);
8330 if (initial_length == 0xffffffff)
8332 /* This section is 64-bit DWARF 3. */
8333 SAFE_BYTE_GET_AND_INC (initial_length, p, 8, finish);
8334 *poffset_size = offset_size = 8;
8336 else
8337 *poffset_size = offset_size = 4;
8339 if (initial_length > (size_t) (finish - p))
8341 /* If the length field has a relocation against it, then we should
8342 not complain if it is inaccurate (and probably negative).
8343 It is copied from .debug_line handling code. */
8344 if (reloc_at (section, (p - section->start) - offset_size))
8345 initial_length = finish - p;
8346 else
8348 warn (_("The length field (%#" PRIx64
8349 ") in the debug_rnglists header is wrong"
8350 " - the section is too small\n"),
8351 initial_length);
8352 return 0;
8356 /* Report the next unit offset to the caller. */
8357 *unit_offset = (p - section->start) + initial_length;
8359 /* Get the other fields in the header. */
8360 SAFE_BYTE_GET_AND_INC (version, p, 2, finish);
8361 SAFE_BYTE_GET_AND_INC (address_size, p, 1, finish);
8362 SAFE_BYTE_GET_AND_INC (segment_selector_size, p, 1, finish);
8363 SAFE_BYTE_GET_AND_INC (offset_entry_count, p, 4, finish);
8365 printf (_(" Table at Offset: %#" PRIx64 ":\n"), start_offset);
8366 printf (_(" Length: %#" PRIx64 "\n"), initial_length);
8367 printf (_(" DWARF version: %u\n"), version);
8368 printf (_(" Address size: %u\n"), address_size);
8369 printf (_(" Segment size: %u\n"), segment_selector_size);
8370 printf (_(" Offset entries: %u\n"), offset_entry_count);
8372 /* Check the fields. */
8373 if (segment_selector_size != 0)
8375 warn (_("The %s section contains "
8376 "unsupported segment selector size: %d.\n"),
8377 section->name, segment_selector_size);
8378 return 0;
8381 if (version < 5)
8383 warn (_("Only DWARF version 5+ debug_rnglists info "
8384 "is currently supported.\n"));
8385 return 0;
8388 if (offset_entry_count != 0)
8390 printf (_("\n Offsets starting at %#tx:\n"), p - section->start);
8392 for (i = 0; i < offset_entry_count; i++)
8394 uint64_t entry;
8396 SAFE_BYTE_GET_AND_INC (entry, p, offset_size, finish);
8397 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
8401 return 1;
8404 static bool
8405 is_range_list_for_this_section (bool is_rnglists, unsigned int version)
8407 if (is_rnglists && version > 4)
8408 return true;
8410 if (! is_rnglists && version < 5)
8411 return true;
8413 return false;
8416 static int
8417 display_debug_ranges (struct dwarf_section *section,
8418 void *file ATTRIBUTE_UNUSED)
8420 unsigned char *start = section->start;
8421 unsigned char *last_start = start;
8422 uint64_t bytes = section->size;
8423 unsigned char *section_begin = start;
8424 unsigned char *finish = start + bytes;
8425 unsigned int num_range_list, i;
8426 struct range_entry *range_entries;
8427 struct range_entry *range_entry_fill;
8428 bool is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
8429 uint64_t last_offset = 0;
8430 uint64_t next_rnglists_cu_offset = 0;
8431 unsigned char offset_size;
8433 if (bytes == 0)
8435 printf (_("\nThe %s section is empty.\n"), section->name);
8436 return 0;
8439 introduce (section, false);
8441 if (load_debug_info (file) == 0)
8443 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
8444 section->name);
8445 return 0;
8448 num_range_list = 0;
8449 for (i = 0; i < num_debug_info_entries; i++)
8450 if (is_range_list_for_this_section (is_rnglists, debug_information [i].dwarf_version))
8451 num_range_list += debug_information [i].num_range_lists;
8453 if (num_range_list == 0)
8455 /* This can happen when the file was compiled with -gsplit-debug
8456 which removes references to range lists from the primary .o file. */
8457 printf (_("No range lists referenced by .debug_info section.\n"));
8458 return 1;
8461 range_entry_fill = range_entries = XNEWVEC (struct range_entry, num_range_list);
8463 for (i = 0; i < num_debug_info_entries; i++)
8465 debug_info *debug_info_p = &debug_information[i];
8466 unsigned int j;
8468 for (j = 0; j < debug_info_p->num_range_lists; j++)
8470 if (is_range_list_for_this_section (is_rnglists, debug_info_p->dwarf_version))
8472 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
8473 range_entry_fill->debug_info_p = debug_info_p;
8474 range_entry_fill++;
8479 assert (range_entry_fill >= range_entries);
8480 assert (num_range_list >= (unsigned int)(range_entry_fill - range_entries));
8481 num_range_list = range_entry_fill - range_entries;
8482 qsort (range_entries, num_range_list, sizeof (*range_entries),
8483 range_entry_compar);
8485 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
8486 warn (_("Range lists in %s section start at %#" PRIx64 "\n"),
8487 section->name, range_entries[0].ranges_offset);
8489 putchar ('\n');
8490 if (!is_rnglists)
8491 printf (_(" Offset Begin End\n"));
8493 for (i = 0; i < num_range_list; i++)
8495 struct range_entry *range_entry = &range_entries[i];
8496 debug_info *debug_info_p = range_entry->debug_info_p;
8497 unsigned int pointer_size;
8498 uint64_t offset;
8499 unsigned char *next;
8500 uint64_t base_address;
8502 pointer_size = debug_info_p->pointer_size;
8503 offset = range_entry->ranges_offset;
8504 base_address = debug_info_p->base_address;
8506 /* PR 17512: file: 001-101485-0.001:0.1. */
8507 if (pointer_size < 2 || pointer_size > 8)
8509 warn (_("Corrupt pointer size (%d) in debug entry at offset %#" PRIx64 "\n"),
8510 pointer_size, offset);
8511 continue;
8514 if (offset > (size_t) (finish - section_begin))
8516 warn (_("Corrupt offset (%#" PRIx64 ") in range entry %u\n"),
8517 offset, i);
8518 continue;
8521 /* If we've moved on to the next compile unit in the rnglists section - dump the unit header(s). */
8522 if (is_rnglists && next_rnglists_cu_offset < offset)
8524 while (next_rnglists_cu_offset < offset)
8525 display_debug_rnglists_unit_header (section, &next_rnglists_cu_offset, &offset_size);
8526 printf (_(" Offset Begin End\n"));
8529 next = section_begin + offset; /* Offset is from the section start, the base has already been added. */
8531 /* If multiple DWARF entities reference the same range then we will
8532 have multiple entries in the `range_entries' list for the same
8533 offset. Thanks to the sort above these will all be consecutive in
8534 the `range_entries' list, so we can easily ignore duplicates
8535 here. */
8536 if (i > 0 && last_offset == offset)
8537 continue;
8538 last_offset = offset;
8540 if (dwarf_check != 0 && i > 0)
8542 if (start < next)
8543 warn (_("There is a hole [%#tx - %#tx] in %s section.\n"),
8544 start - section_begin, next - section_begin, section->name);
8545 else if (start > next)
8547 if (next == last_start)
8548 continue;
8549 warn (_("There is an overlap [%#tx - %#tx] in %s section.\n"),
8550 start - section_begin, next - section_begin, section->name);
8554 start = next;
8555 last_start = next;
8557 if (is_rnglists)
8558 display_debug_rnglists_list
8559 (start, finish, pointer_size, offset, base_address, debug_info_p->addr_base);
8560 else
8561 display_debug_ranges_list
8562 (start, finish, pointer_size, offset, base_address);
8565 /* Display trailing empty (or unreferenced) compile units, if any. */
8566 if (is_rnglists)
8567 while (next_rnglists_cu_offset < section->size)
8568 display_debug_rnglists_unit_header (section, &next_rnglists_cu_offset, &offset_size);
8570 putchar ('\n');
8572 free (range_entries);
8574 return 1;
8577 typedef struct Frame_Chunk
8579 struct Frame_Chunk *next;
8580 unsigned char *chunk_start;
8581 unsigned int ncols;
8582 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8583 short int *col_type;
8584 int64_t *col_offset;
8585 char *augmentation;
8586 unsigned int code_factor;
8587 int data_factor;
8588 uint64_t pc_begin;
8589 uint64_t pc_range;
8590 unsigned int cfa_reg;
8591 uint64_t cfa_offset;
8592 unsigned int ra;
8593 unsigned char fde_encoding;
8594 unsigned char cfa_exp;
8595 unsigned char ptr_size;
8596 unsigned char segment_size;
8598 Frame_Chunk;
8600 typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
8601 static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
8602 static const char *const *dwarf_regnames;
8603 static unsigned int dwarf_regnames_count;
8604 static bool is_aarch64;
8606 /* A marker for a col_type that means this column was never referenced
8607 in the frame info. */
8608 #define DW_CFA_unreferenced (-1)
8610 /* Return 0 if no more space is needed, 1 if more space is needed,
8611 -1 for invalid reg. */
8613 static int
8614 frame_need_space (Frame_Chunk *fc, unsigned int reg)
8616 unsigned int prev = fc->ncols;
8618 if (reg < (unsigned int) fc->ncols)
8619 return 0;
8621 if (dwarf_regnames_count > 0
8622 && reg > dwarf_regnames_count)
8623 return -1;
8625 fc->ncols = reg + 1;
8626 /* PR 17512: file: 10450-2643-0.004.
8627 If reg == -1 then this can happen... */
8628 if (fc->ncols == 0)
8629 return -1;
8631 /* PR 17512: file: 2844a11d. */
8632 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
8634 error (_("Unfeasibly large register number: %u\n"), reg);
8635 fc->ncols = 0;
8636 /* FIXME: 1024 is an arbitrary limit. Increase it if
8637 we ever encounter a valid binary that exceeds it. */
8638 return -1;
8641 fc->col_type = xcrealloc (fc->col_type, fc->ncols,
8642 sizeof (*fc->col_type));
8643 fc->col_offset = xcrealloc (fc->col_offset, fc->ncols,
8644 sizeof (*fc->col_offset));
8645 /* PR 17512: file:002-10025-0.005. */
8646 if (fc->col_type == NULL || fc->col_offset == NULL)
8648 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
8649 fc->ncols);
8650 fc->ncols = 0;
8651 return -1;
8654 while (prev < fc->ncols)
8656 fc->col_type[prev] = DW_CFA_unreferenced;
8657 fc->col_offset[prev] = 0;
8658 prev++;
8660 return 1;
8663 static const char *const dwarf_regnames_i386[] =
8665 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8666 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8667 "eip", "eflags", NULL, /* 8 - 10 */
8668 "st0", "st1", "st2", "st3", /* 11 - 14 */
8669 "st4", "st5", "st6", "st7", /* 15 - 18 */
8670 NULL, NULL, /* 19 - 20 */
8671 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
8672 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
8673 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
8674 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
8675 "fcw", "fsw", "mxcsr", /* 37 - 39 */
8676 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8677 "tr", "ldtr", /* 48 - 49 */
8678 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8679 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8680 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8681 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8682 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8683 NULL, NULL, NULL, /* 90 - 92 */
8684 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
8687 static const char *const dwarf_regnames_iamcu[] =
8689 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8690 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8691 "eip", "eflags", NULL, /* 8 - 10 */
8692 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
8693 NULL, NULL, /* 19 - 20 */
8694 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
8695 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
8696 NULL, NULL, NULL, /* 37 - 39 */
8697 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8698 "tr", "ldtr", /* 48 - 49 */
8699 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8700 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8701 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8702 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8703 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8704 NULL, NULL, NULL, /* 90 - 92 */
8705 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
8708 static void
8709 init_dwarf_regnames_i386 (void)
8711 dwarf_regnames = dwarf_regnames_i386;
8712 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
8713 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8716 static void
8717 init_dwarf_regnames_iamcu (void)
8719 dwarf_regnames = dwarf_regnames_iamcu;
8720 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
8721 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8724 static const char *const DW_CFA_GNU_window_save_name[] =
8726 "DW_CFA_GNU_window_save",
8727 "DW_CFA_AARCH64_negate_ra_state"
8730 static const char *const dwarf_regnames_x86_64[] =
8732 "rax", "rdx", "rcx", "rbx",
8733 "rsi", "rdi", "rbp", "rsp",
8734 "r8", "r9", "r10", "r11",
8735 "r12", "r13", "r14", "r15",
8736 "rip",
8737 "xmm0", "xmm1", "xmm2", "xmm3",
8738 "xmm4", "xmm5", "xmm6", "xmm7",
8739 "xmm8", "xmm9", "xmm10", "xmm11",
8740 "xmm12", "xmm13", "xmm14", "xmm15",
8741 "st0", "st1", "st2", "st3",
8742 "st4", "st5", "st6", "st7",
8743 "mm0", "mm1", "mm2", "mm3",
8744 "mm4", "mm5", "mm6", "mm7",
8745 "rflags",
8746 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8747 "fs.base", "gs.base", NULL, NULL,
8748 "tr", "ldtr",
8749 "mxcsr", "fcw", "fsw",
8750 "xmm16", "xmm17", "xmm18", "xmm19",
8751 "xmm20", "xmm21", "xmm22", "xmm23",
8752 "xmm24", "xmm25", "xmm26", "xmm27",
8753 "xmm28", "xmm29", "xmm30", "xmm31",
8754 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
8755 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
8756 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
8757 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
8758 NULL, NULL, NULL, /* 115 - 117 */
8759 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
8762 static void
8763 init_dwarf_regnames_x86_64 (void)
8765 dwarf_regnames = dwarf_regnames_x86_64;
8766 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
8767 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8770 static const char *const dwarf_regnames_aarch64[] =
8772 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
8773 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
8774 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8775 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8776 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
8777 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
8778 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
8779 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
8780 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
8781 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
8782 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8783 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
8784 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
8785 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8786 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8787 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
8790 static void
8791 init_dwarf_regnames_aarch64 (void)
8793 dwarf_regnames = dwarf_regnames_aarch64;
8794 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
8795 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8796 is_aarch64 = true;
8799 static const char *const dwarf_regnames_s390[] =
8801 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
8802 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8803 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8804 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
8805 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
8806 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8807 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8808 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
8809 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
8810 "pswm", "pswa",
8811 NULL, NULL,
8812 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8813 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8816 static void
8817 init_dwarf_regnames_s390 (void)
8819 dwarf_regnames = dwarf_regnames_s390;
8820 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
8821 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8824 static const char *const dwarf_regnames_riscv[] =
8826 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
8827 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
8828 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
8829 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
8830 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
8831 "fs0", "fs1", /* 40 - 41 */
8832 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
8833 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
8834 "fs10", "fs11", /* 58 - 59 */
8835 "ft8", "ft9", "ft10", "ft11", /* 60 - 63 */
8836 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 64 - 71 */
8837 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 72 - 79 */
8838 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 80 - 87 */
8839 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 88 - 95 */
8840 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", /* 96 - 103 */
8841 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", /* 104 - 111 */
8842 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", /* 112 - 119 */
8843 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", /* 120 - 127 */
8846 /* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8847 the large number of CSRs. */
8849 static const char *
8850 regname_internal_riscv (unsigned int regno)
8852 const char *name = NULL;
8854 /* Lookup in the table first, this covers GPR and FPR. */
8855 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8856 name = dwarf_regnames_riscv [regno];
8857 else if (regno >= 4096 && regno <= 8191)
8859 /* This might be a CSR, these live in a sparse number space from 4096
8860 to 8191 These numbers are defined in the RISC-V ELF ABI
8861 document. */
8862 switch (regno)
8864 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8865 case VALUE + 4096: name = #NAME; break;
8866 #include "opcode/riscv-opc.h"
8867 #undef DECLARE_CSR
8869 default:
8871 static char csr_name[10];
8872 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8873 name = csr_name;
8875 break;
8879 return name;
8882 static void
8883 init_dwarf_regnames_riscv (void)
8885 dwarf_regnames = NULL;
8886 dwarf_regnames_count = 8192;
8887 dwarf_regnames_lookup_func = regname_internal_riscv;
8890 void
8891 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
8893 dwarf_regnames_lookup_func = NULL;
8894 is_aarch64 = false;
8896 switch (e_machine)
8898 case EM_386:
8899 init_dwarf_regnames_i386 ();
8900 break;
8902 case EM_IAMCU:
8903 init_dwarf_regnames_iamcu ();
8904 break;
8906 case EM_X86_64:
8907 case EM_L1OM:
8908 case EM_K1OM:
8909 init_dwarf_regnames_x86_64 ();
8910 break;
8912 case EM_AARCH64:
8913 init_dwarf_regnames_aarch64 ();
8914 break;
8916 case EM_S390:
8917 init_dwarf_regnames_s390 ();
8918 break;
8920 case EM_RISCV:
8921 init_dwarf_regnames_riscv ();
8922 break;
8924 default:
8925 break;
8929 /* Initialize the DWARF register name lookup state based on the
8930 architecture and specific machine type of a BFD. */
8932 void
8933 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8934 unsigned long mach)
8936 dwarf_regnames_lookup_func = NULL;
8937 is_aarch64 = false;
8939 switch (arch)
8941 case bfd_arch_i386:
8942 switch (mach)
8944 case bfd_mach_x86_64:
8945 case bfd_mach_x86_64_intel_syntax:
8946 case bfd_mach_x64_32:
8947 case bfd_mach_x64_32_intel_syntax:
8948 init_dwarf_regnames_x86_64 ();
8949 break;
8951 default:
8952 init_dwarf_regnames_i386 ();
8953 break;
8955 break;
8957 case bfd_arch_iamcu:
8958 init_dwarf_regnames_iamcu ();
8959 break;
8961 case bfd_arch_aarch64:
8962 init_dwarf_regnames_aarch64();
8963 break;
8965 case bfd_arch_s390:
8966 init_dwarf_regnames_s390 ();
8967 break;
8969 case bfd_arch_riscv:
8970 init_dwarf_regnames_riscv ();
8971 break;
8973 default:
8974 break;
8978 static const char *
8979 regname_internal_by_table_only (unsigned int regno)
8981 if (dwarf_regnames != NULL
8982 && regno < dwarf_regnames_count
8983 && dwarf_regnames [regno] != NULL)
8984 return dwarf_regnames [regno];
8986 return NULL;
8989 static const char *
8990 regname (unsigned int regno, int name_only_p)
8992 static char reg[64];
8994 const char *name = NULL;
8996 if (dwarf_regnames_lookup_func != NULL)
8997 name = dwarf_regnames_lookup_func (regno);
8999 if (name != NULL)
9001 if (name_only_p)
9002 return name;
9003 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
9005 else
9006 snprintf (reg, sizeof (reg), "r%d", regno);
9007 return reg;
9010 static void
9011 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
9013 unsigned int r;
9014 char tmp[100];
9016 if (*max_regs != fc->ncols)
9017 *max_regs = fc->ncols;
9019 if (*need_col_headers)
9021 *need_col_headers = 0;
9023 printf ("%-*s CFA ", eh_addr_size * 2, " LOC");
9025 for (r = 0; r < *max_regs; r++)
9026 if (fc->col_type[r] != DW_CFA_unreferenced)
9028 if (r == fc->ra)
9029 printf ("ra ");
9030 else
9031 printf ("%-5s ", regname (r, 1));
9034 printf ("\n");
9037 print_hex (fc->pc_begin, eh_addr_size);
9038 if (fc->cfa_exp)
9039 strcpy (tmp, "exp");
9040 else
9041 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
9042 printf ("%-8s ", tmp);
9044 for (r = 0; r < fc->ncols; r++)
9046 if (fc->col_type[r] != DW_CFA_unreferenced)
9048 switch (fc->col_type[r])
9050 case DW_CFA_undefined:
9051 strcpy (tmp, "u");
9052 break;
9053 case DW_CFA_same_value:
9054 strcpy (tmp, "s");
9055 break;
9056 case DW_CFA_offset:
9057 sprintf (tmp, "c%+" PRId64, fc->col_offset[r]);
9058 break;
9059 case DW_CFA_val_offset:
9060 sprintf (tmp, "v%+" PRId64, fc->col_offset[r]);
9061 break;
9062 case DW_CFA_register:
9063 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
9064 break;
9065 case DW_CFA_expression:
9066 strcpy (tmp, "exp");
9067 break;
9068 case DW_CFA_val_expression:
9069 strcpy (tmp, "vexp");
9070 break;
9071 default:
9072 strcpy (tmp, "n/a");
9073 break;
9075 printf ("%-5s ", tmp);
9078 printf ("\n");
9081 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
9083 static unsigned char *
9084 read_cie (unsigned char *start, unsigned char *end,
9085 Frame_Chunk **p_cie, int *p_version,
9086 uint64_t *p_aug_len, unsigned char **p_aug)
9088 int version;
9089 Frame_Chunk *fc;
9090 unsigned char *augmentation_data = NULL;
9091 uint64_t augmentation_data_len = 0;
9093 * p_cie = NULL;
9094 /* PR 17512: file: 001-228113-0.004. */
9095 if (start >= end)
9096 return end;
9098 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9099 memset (fc, 0, sizeof (Frame_Chunk));
9101 fc->col_type = xmalloc (sizeof (*fc->col_type));
9102 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
9104 version = *start++;
9106 fc->augmentation = (char *) start;
9107 /* PR 17512: file: 001-228113-0.004.
9108 Skip past augmentation name, but avoid running off the end of the data. */
9109 while (start < end)
9110 if (* start ++ == '\0')
9111 break;
9112 if (start == end)
9114 warn (_("No terminator for augmentation name\n"));
9115 goto fail;
9118 if (strcmp (fc->augmentation, "eh") == 0)
9120 if (eh_addr_size > (size_t) (end - start))
9121 goto fail;
9122 start += eh_addr_size;
9125 if (version >= 4)
9127 if (2 > (size_t) (end - start))
9128 goto fail;
9129 GET (fc->ptr_size, 1);
9130 if (fc->ptr_size < 1 || fc->ptr_size > 8)
9132 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
9133 goto fail;
9136 GET (fc->segment_size, 1);
9137 /* PR 17512: file: e99d2804. */
9138 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
9140 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
9141 goto fail;
9144 eh_addr_size = fc->ptr_size;
9146 else
9148 fc->ptr_size = eh_addr_size;
9149 fc->segment_size = 0;
9152 READ_ULEB (fc->code_factor, start, end);
9153 READ_SLEB (fc->data_factor, start, end);
9155 if (start >= end)
9156 goto fail;
9158 if (version == 1)
9160 GET (fc->ra, 1);
9162 else
9164 READ_ULEB (fc->ra, start, end);
9167 if (fc->augmentation[0] == 'z')
9169 if (start >= end)
9170 goto fail;
9171 READ_ULEB (augmentation_data_len, start, end);
9172 augmentation_data = start;
9173 /* PR 17512: file: 11042-2589-0.004. */
9174 if (augmentation_data_len > (size_t) (end - start))
9176 warn (_("Augmentation data too long: %#" PRIx64
9177 ", expected at most %#tx\n"),
9178 augmentation_data_len, end - start);
9179 goto fail;
9181 start += augmentation_data_len;
9184 if (augmentation_data_len)
9186 unsigned char *p;
9187 unsigned char *q;
9188 unsigned char *qend;
9190 p = (unsigned char *) fc->augmentation + 1;
9191 q = augmentation_data;
9192 qend = q + augmentation_data_len;
9194 while (p < end && q < qend)
9196 if (*p == 'L')
9197 q++;
9198 else if (*p == 'P')
9199 q += 1 + size_of_encoded_value (*q);
9200 else if (*p == 'R')
9201 fc->fde_encoding = *q++;
9202 else if (*p == 'S')
9204 else if (*p == 'B')
9206 else
9207 break;
9208 p++;
9210 /* Note - it is OK if this loop terminates with q < qend.
9211 Padding may have been inserted to align the end of the CIE. */
9214 *p_cie = fc;
9215 if (p_version)
9216 *p_version = version;
9217 if (p_aug_len)
9219 *p_aug_len = augmentation_data_len;
9220 *p_aug = augmentation_data;
9222 return start;
9224 fail:
9225 free (fc->col_offset);
9226 free (fc->col_type);
9227 free (fc);
9228 return end;
9231 /* Prints out the contents on the DATA array formatted as unsigned bytes.
9232 If do_wide is not enabled, then formats the output to fit into 80 columns.
9233 PRINTED contains the number of characters already written to the current
9234 output line. */
9236 static void
9237 display_data (size_t printed, const unsigned char *data, size_t len)
9239 if (do_wide || len < ((80 - printed) / 3))
9240 for (printed = 0; printed < len; ++printed)
9241 printf (" %02x", data[printed]);
9242 else
9244 for (printed = 0; printed < len; ++printed)
9246 if (printed % (80 / 3) == 0)
9247 putchar ('\n');
9248 printf (" %02x", data[printed]);
9253 /* Prints out the contents on the augmentation data array.
9254 If do_wide is not enabled, then formats the output to fit into 80 columns. */
9256 static void
9257 display_augmentation_data (const unsigned char * data, uint64_t len)
9259 size_t i;
9261 i = printf (_(" Augmentation data: "));
9262 display_data (i, data, len);
9265 static int
9266 display_debug_frames (struct dwarf_section *section,
9267 void *file ATTRIBUTE_UNUSED)
9269 unsigned char *start = section->start;
9270 unsigned char *end = start + section->size;
9271 unsigned char *section_start = start;
9272 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
9273 Frame_Chunk *remembered_state = NULL;
9274 Frame_Chunk *rs;
9275 bool is_eh = strcmp (section->name, ".eh_frame") == 0;
9276 unsigned int max_regs = 0;
9277 const char *bad_reg = _("bad register: ");
9278 unsigned int saved_eh_addr_size = eh_addr_size;
9280 introduce (section, false);
9282 while (start < end)
9284 unsigned char *saved_start;
9285 unsigned char *block_end;
9286 uint64_t length;
9287 uint64_t cie_id;
9288 Frame_Chunk *fc;
9289 Frame_Chunk *cie;
9290 int need_col_headers = 1;
9291 unsigned char *augmentation_data = NULL;
9292 uint64_t augmentation_data_len = 0;
9293 unsigned int encoded_ptr_size = saved_eh_addr_size;
9294 unsigned int offset_size;
9295 bool all_nops;
9296 static Frame_Chunk fde_fc;
9298 saved_start = start;
9300 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
9302 if (length == 0)
9304 printf ("\n%08tx ZERO terminator\n\n",
9305 saved_start - section_start);
9306 /* Skip any zero terminators that directly follow.
9307 A corrupt section size could have loaded a whole
9308 slew of zero filled memory bytes. eg
9309 PR 17512: file: 070-19381-0.004. */
9310 while (start < end && * start == 0)
9311 ++ start;
9312 continue;
9315 if (length == 0xffffffff)
9317 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
9318 offset_size = 8;
9320 else
9321 offset_size = 4;
9323 if (length > (size_t) (end - start))
9325 warn ("Invalid length %#" PRIx64 " in FDE at %#tx\n",
9326 length, saved_start - section_start);
9327 block_end = end;
9329 else
9330 block_end = start + length;
9332 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, block_end);
9334 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
9335 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
9337 int version;
9338 unsigned int mreg;
9340 start = read_cie (start, block_end, &cie, &version,
9341 &augmentation_data_len, &augmentation_data);
9342 /* PR 17512: file: 027-135133-0.005. */
9343 if (cie == NULL)
9344 break;
9346 fc = cie;
9347 fc->next = chunks;
9348 chunks = fc;
9349 fc->chunk_start = saved_start;
9350 mreg = max_regs > 0 ? max_regs - 1 : 0;
9351 if (mreg < fc->ra)
9352 mreg = fc->ra;
9353 if (frame_need_space (fc, mreg) < 0)
9354 break;
9355 if (fc->fde_encoding)
9356 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9358 printf ("\n%08tx ", saved_start - section_start);
9359 print_hex (length, fc->ptr_size);
9360 print_hex (cie_id, offset_size);
9362 if (do_debug_frames_interp)
9364 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
9365 fc->code_factor, fc->data_factor, fc->ra);
9367 else
9369 printf ("CIE\n");
9370 printf (" Version: %d\n", version);
9371 printf (" Augmentation: \"%s\"\n", fc->augmentation);
9372 if (version >= 4)
9374 printf (" Pointer Size: %u\n", fc->ptr_size);
9375 printf (" Segment Size: %u\n", fc->segment_size);
9377 printf (" Code alignment factor: %u\n", fc->code_factor);
9378 printf (" Data alignment factor: %d\n", fc->data_factor);
9379 printf (" Return address column: %d\n", fc->ra);
9381 if (augmentation_data_len)
9382 display_augmentation_data (augmentation_data, augmentation_data_len);
9384 putchar ('\n');
9387 else
9389 unsigned char *look_for;
9390 unsigned long segment_selector;
9391 uint64_t cie_off;
9393 cie_off = cie_id;
9394 if (is_eh)
9396 uint64_t sign = (uint64_t) 1 << (offset_size * 8 - 1);
9397 cie_off = (cie_off ^ sign) - sign;
9398 cie_off = start - 4 - section_start - cie_off;
9401 look_for = section_start + cie_off;
9402 if (cie_off <= (size_t) (saved_start - section_start))
9404 for (cie = chunks; cie ; cie = cie->next)
9405 if (cie->chunk_start == look_for)
9406 break;
9408 else if (cie_off >= section->size)
9409 cie = NULL;
9410 else
9412 for (cie = forward_refs; cie ; cie = cie->next)
9413 if (cie->chunk_start == look_for)
9414 break;
9415 if (!cie)
9417 unsigned int off_size;
9418 unsigned char *cie_scan;
9420 cie_scan = look_for;
9421 off_size = 4;
9422 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
9423 if (length == 0xffffffff)
9425 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
9426 off_size = 8;
9428 if (length != 0 && length <= (size_t) (end - cie_scan))
9430 uint64_t c_id;
9431 unsigned char *cie_end = cie_scan + length;
9433 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size,
9434 cie_end);
9435 if (is_eh
9436 ? c_id == 0
9437 : ((off_size == 4 && c_id == DW_CIE_ID)
9438 || (off_size == 8 && c_id == DW64_CIE_ID)))
9440 int version;
9441 unsigned int mreg;
9443 read_cie (cie_scan, cie_end, &cie, &version,
9444 &augmentation_data_len, &augmentation_data);
9445 /* PR 17512: file: 3450-2098-0.004. */
9446 if (cie == NULL)
9448 warn (_("Failed to read CIE information\n"));
9449 break;
9451 cie->next = forward_refs;
9452 forward_refs = cie;
9453 cie->chunk_start = look_for;
9454 mreg = max_regs > 0 ? max_regs - 1 : 0;
9455 if (mreg < cie->ra)
9456 mreg = cie->ra;
9457 if (frame_need_space (cie, mreg) < 0)
9459 warn (_("Invalid max register\n"));
9460 break;
9462 if (cie->fde_encoding)
9463 encoded_ptr_size
9464 = size_of_encoded_value (cie->fde_encoding);
9470 fc = &fde_fc;
9471 memset (fc, 0, sizeof (Frame_Chunk));
9473 if (!cie)
9475 fc->ncols = 0;
9476 fc->col_type = xmalloc (sizeof (*fc->col_type));
9477 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
9478 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
9480 warn (_("Invalid max register\n"));
9481 break;
9483 cie = fc;
9484 fc->augmentation = "";
9485 fc->fde_encoding = 0;
9486 fc->ptr_size = eh_addr_size;
9487 fc->segment_size = 0;
9489 else
9491 fc->ncols = cie->ncols;
9492 fc->col_type = xcmalloc (fc->ncols, sizeof (*fc->col_type));
9493 fc->col_offset = xcmalloc (fc->ncols, sizeof (*fc->col_offset));
9494 memcpy (fc->col_type, cie->col_type,
9495 fc->ncols * sizeof (*fc->col_type));
9496 memcpy (fc->col_offset, cie->col_offset,
9497 fc->ncols * sizeof (*fc->col_offset));
9498 fc->augmentation = cie->augmentation;
9499 fc->ptr_size = cie->ptr_size;
9500 eh_addr_size = cie->ptr_size;
9501 fc->segment_size = cie->segment_size;
9502 fc->code_factor = cie->code_factor;
9503 fc->data_factor = cie->data_factor;
9504 fc->cfa_reg = cie->cfa_reg;
9505 fc->cfa_offset = cie->cfa_offset;
9506 fc->ra = cie->ra;
9507 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
9509 warn (_("Invalid max register\n"));
9510 break;
9512 fc->fde_encoding = cie->fde_encoding;
9515 if (fc->fde_encoding)
9516 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9518 segment_selector = 0;
9519 if (fc->segment_size)
9521 if (fc->segment_size > sizeof (segment_selector))
9523 /* PR 17512: file: 9e196b3e. */
9524 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
9525 fc->segment_size = 4;
9527 SAFE_BYTE_GET_AND_INC (segment_selector, start,
9528 fc->segment_size, block_end);
9531 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section,
9532 block_end);
9534 /* FIXME: It appears that sometimes the final pc_range value is
9535 encoded in less than encoded_ptr_size bytes. See the x86_64
9536 run of the "objcopy on compressed debug sections" test for an
9537 example of this. */
9538 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size,
9539 block_end);
9541 if (cie->augmentation[0] == 'z')
9543 READ_ULEB (augmentation_data_len, start, block_end);
9544 augmentation_data = start;
9545 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
9546 if (augmentation_data_len > (size_t) (block_end - start))
9548 warn (_("Augmentation data too long: %#" PRIx64 ", "
9549 "expected at most %#tx\n"),
9550 augmentation_data_len, block_end - start);
9551 start = block_end;
9552 augmentation_data = NULL;
9553 augmentation_data_len = 0;
9555 start += augmentation_data_len;
9558 printf ("\n%08tx ", saved_start - section_start);
9559 print_hex (length, fc->ptr_size);
9560 print_hex (cie_id, offset_size);
9561 printf ("FDE ");
9563 if (cie->chunk_start)
9564 printf ("cie=%08tx", cie->chunk_start - section_start);
9565 else
9566 /* Ideally translate "invalid " to 8 chars, trailing space
9567 is optional. */
9568 printf (_("cie=invalid "));
9570 printf (" pc=");
9571 if (fc->segment_size)
9572 printf ("%04lx:", segment_selector);
9574 print_hex_ns (fc->pc_begin, fc->ptr_size);
9575 printf ("..");
9576 print_hex_ns (fc->pc_begin + fc->pc_range, fc->ptr_size);
9577 printf ("\n");
9579 if (! do_debug_frames_interp && augmentation_data_len)
9581 display_augmentation_data (augmentation_data, augmentation_data_len);
9582 putchar ('\n');
9586 /* At this point, fc is the current chunk, cie (if any) is set, and
9587 we're about to interpret instructions for the chunk. */
9588 /* ??? At present we need to do this always, since this sizes the
9589 fc->col_type and fc->col_offset arrays, which we write into always.
9590 We should probably split the interpreted and non-interpreted bits
9591 into two different routines, since there's so much that doesn't
9592 really overlap between them. */
9593 if (1 || do_debug_frames_interp)
9595 /* Start by making a pass over the chunk, allocating storage
9596 and taking note of what registers are used. */
9597 unsigned char *tmp = start;
9599 while (start < block_end)
9601 unsigned int reg, op, opa;
9602 unsigned long temp;
9604 op = *start++;
9605 opa = op & 0x3f;
9606 if (op & 0xc0)
9607 op &= 0xc0;
9609 /* Warning: if you add any more cases to this switch, be
9610 sure to add them to the corresponding switch below. */
9611 reg = -1u;
9612 switch (op)
9614 case DW_CFA_advance_loc:
9615 break;
9616 case DW_CFA_offset:
9617 SKIP_ULEB (start, block_end);
9618 reg = opa;
9619 break;
9620 case DW_CFA_restore:
9621 reg = opa;
9622 break;
9623 case DW_CFA_set_loc:
9624 if ((size_t) (block_end - start) < encoded_ptr_size)
9625 start = block_end;
9626 else
9627 start += encoded_ptr_size;
9628 break;
9629 case DW_CFA_advance_loc1:
9630 if ((size_t) (block_end - start) < 1)
9631 start = block_end;
9632 else
9633 start += 1;
9634 break;
9635 case DW_CFA_advance_loc2:
9636 if ((size_t) (block_end - start) < 2)
9637 start = block_end;
9638 else
9639 start += 2;
9640 break;
9641 case DW_CFA_advance_loc4:
9642 if ((size_t) (block_end - start) < 4)
9643 start = block_end;
9644 else
9645 start += 4;
9646 break;
9647 case DW_CFA_offset_extended:
9648 case DW_CFA_val_offset:
9649 READ_ULEB (reg, start, block_end);
9650 SKIP_ULEB (start, block_end);
9651 break;
9652 case DW_CFA_restore_extended:
9653 READ_ULEB (reg, start, block_end);
9654 break;
9655 case DW_CFA_undefined:
9656 READ_ULEB (reg, start, block_end);
9657 break;
9658 case DW_CFA_same_value:
9659 READ_ULEB (reg, start, block_end);
9660 break;
9661 case DW_CFA_register:
9662 READ_ULEB (reg, start, block_end);
9663 SKIP_ULEB (start, block_end);
9664 break;
9665 case DW_CFA_def_cfa:
9666 SKIP_ULEB (start, block_end);
9667 SKIP_ULEB (start, block_end);
9668 break;
9669 case DW_CFA_def_cfa_register:
9670 SKIP_ULEB (start, block_end);
9671 break;
9672 case DW_CFA_def_cfa_offset:
9673 SKIP_ULEB (start, block_end);
9674 break;
9675 case DW_CFA_def_cfa_expression:
9676 READ_ULEB (temp, start, block_end);
9677 if ((size_t) (block_end - start) < temp)
9678 start = block_end;
9679 else
9680 start += temp;
9681 break;
9682 case DW_CFA_expression:
9683 case DW_CFA_val_expression:
9684 READ_ULEB (reg, start, block_end);
9685 READ_ULEB (temp, start, block_end);
9686 if ((size_t) (block_end - start) < temp)
9687 start = block_end;
9688 else
9689 start += temp;
9690 break;
9691 case DW_CFA_offset_extended_sf:
9692 case DW_CFA_val_offset_sf:
9693 READ_ULEB (reg, start, block_end);
9694 SKIP_SLEB (start, block_end);
9695 break;
9696 case DW_CFA_def_cfa_sf:
9697 SKIP_ULEB (start, block_end);
9698 SKIP_SLEB (start, block_end);
9699 break;
9700 case DW_CFA_def_cfa_offset_sf:
9701 SKIP_SLEB (start, block_end);
9702 break;
9703 case DW_CFA_MIPS_advance_loc8:
9704 if ((size_t) (block_end - start) < 8)
9705 start = block_end;
9706 else
9707 start += 8;
9708 break;
9709 case DW_CFA_GNU_args_size:
9710 SKIP_ULEB (start, block_end);
9711 break;
9712 case DW_CFA_GNU_negative_offset_extended:
9713 READ_ULEB (reg, start, block_end);
9714 SKIP_ULEB (start, block_end);
9715 break;
9716 default:
9717 break;
9719 if (reg != -1u && frame_need_space (fc, reg) >= 0)
9721 /* Don't leave any reg as DW_CFA_unreferenced so
9722 that frame_display_row prints name of regs in
9723 header, and all referenced regs in each line. */
9724 if (reg >= cie->ncols
9725 || cie->col_type[reg] == DW_CFA_unreferenced)
9726 fc->col_type[reg] = DW_CFA_undefined;
9727 else
9728 fc->col_type[reg] = cie->col_type[reg];
9731 start = tmp;
9734 all_nops = true;
9736 /* Now we know what registers are used, make a second pass over
9737 the chunk, this time actually printing out the info. */
9739 while (start < block_end)
9741 unsigned op, opa;
9742 /* Note: It is tempting to use an unsigned long for 'reg' but there
9743 are various functions, notably frame_space_needed() that assume that
9744 reg is an unsigned int. */
9745 unsigned int reg;
9746 int64_t sofs;
9747 uint64_t ofs;
9748 const char *reg_prefix = "";
9750 op = *start++;
9751 opa = op & 0x3f;
9752 if (op & 0xc0)
9753 op &= 0xc0;
9755 /* Make a note if something other than DW_CFA_nop happens. */
9756 if (op != DW_CFA_nop)
9757 all_nops = false;
9759 /* Warning: if you add any more cases to this switch, be
9760 sure to add them to the corresponding switch above. */
9761 switch (op)
9763 case DW_CFA_advance_loc:
9764 opa *= fc->code_factor;
9765 if (do_debug_frames_interp)
9766 frame_display_row (fc, &need_col_headers, &max_regs);
9767 else
9769 printf (" DW_CFA_advance_loc: %d to ", opa);
9770 print_hex_ns (fc->pc_begin + opa, fc->ptr_size);
9771 printf ("\n");
9773 fc->pc_begin += opa;
9774 break;
9776 case DW_CFA_offset:
9777 READ_ULEB (ofs, start, block_end);
9778 ofs *= fc->data_factor;
9779 if (opa >= fc->ncols)
9780 reg_prefix = bad_reg;
9781 if (! do_debug_frames_interp || *reg_prefix != '\0')
9782 printf (" DW_CFA_offset: %s%s at cfa%+" PRId64 "\n",
9783 reg_prefix, regname (opa, 0), ofs);
9784 if (*reg_prefix == '\0')
9786 fc->col_type[opa] = DW_CFA_offset;
9787 fc->col_offset[opa] = ofs;
9789 break;
9791 case DW_CFA_restore:
9792 if (opa >= fc->ncols)
9793 reg_prefix = bad_reg;
9794 if (! do_debug_frames_interp || *reg_prefix != '\0')
9795 printf (" DW_CFA_restore: %s%s\n",
9796 reg_prefix, regname (opa, 0));
9797 if (*reg_prefix != '\0')
9798 break;
9800 if (opa >= cie->ncols
9801 || cie->col_type[opa] == DW_CFA_unreferenced)
9803 fc->col_type[opa] = DW_CFA_undefined;
9804 fc->col_offset[opa] = 0;
9806 else
9808 fc->col_type[opa] = cie->col_type[opa];
9809 fc->col_offset[opa] = cie->col_offset[opa];
9811 break;
9813 case DW_CFA_set_loc:
9814 ofs = get_encoded_value (&start, fc->fde_encoding, section,
9815 block_end);
9816 if (do_debug_frames_interp)
9817 frame_display_row (fc, &need_col_headers, &max_regs);
9818 else
9820 printf (" DW_CFA_set_loc: ");
9821 print_hex_ns (ofs, fc->ptr_size);
9822 printf ("\n");
9824 fc->pc_begin = ofs;
9825 break;
9827 case DW_CFA_advance_loc1:
9828 SAFE_BYTE_GET_AND_INC (ofs, start, 1, block_end);
9829 ofs *= fc->code_factor;
9830 if (do_debug_frames_interp)
9831 frame_display_row (fc, &need_col_headers, &max_regs);
9832 else
9834 printf (" DW_CFA_advance_loc1: %" PRId64 " to ", ofs);
9835 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9836 printf ("\n");
9838 fc->pc_begin += ofs;
9839 break;
9841 case DW_CFA_advance_loc2:
9842 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
9843 ofs *= fc->code_factor;
9844 if (do_debug_frames_interp)
9845 frame_display_row (fc, &need_col_headers, &max_regs);
9846 else
9848 printf (" DW_CFA_advance_loc2: %" PRId64 " to ", ofs);
9849 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9850 printf ("\n");
9852 fc->pc_begin += ofs;
9853 break;
9855 case DW_CFA_advance_loc4:
9856 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
9857 ofs *= fc->code_factor;
9858 if (do_debug_frames_interp)
9859 frame_display_row (fc, &need_col_headers, &max_regs);
9860 else
9862 printf (" DW_CFA_advance_loc4: %" PRId64 " to ", ofs);
9863 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9864 printf ("\n");
9866 fc->pc_begin += ofs;
9867 break;
9869 case DW_CFA_offset_extended:
9870 READ_ULEB (reg, start, block_end);
9871 READ_ULEB (ofs, start, block_end);
9872 ofs *= fc->data_factor;
9873 if (reg >= fc->ncols)
9874 reg_prefix = bad_reg;
9875 if (! do_debug_frames_interp || *reg_prefix != '\0')
9876 printf (" DW_CFA_offset_extended: %s%s at cfa%+" PRId64 "\n",
9877 reg_prefix, regname (reg, 0), ofs);
9878 if (*reg_prefix == '\0')
9880 fc->col_type[reg] = DW_CFA_offset;
9881 fc->col_offset[reg] = ofs;
9883 break;
9885 case DW_CFA_val_offset:
9886 READ_ULEB (reg, start, block_end);
9887 READ_ULEB (ofs, start, block_end);
9888 ofs *= fc->data_factor;
9889 if (reg >= fc->ncols)
9890 reg_prefix = bad_reg;
9891 if (! do_debug_frames_interp || *reg_prefix != '\0')
9892 printf (" DW_CFA_val_offset: %s%s is cfa%+" PRId64 "\n",
9893 reg_prefix, regname (reg, 0), ofs);
9894 if (*reg_prefix == '\0')
9896 fc->col_type[reg] = DW_CFA_val_offset;
9897 fc->col_offset[reg] = ofs;
9899 break;
9901 case DW_CFA_restore_extended:
9902 READ_ULEB (reg, start, block_end);
9903 if (reg >= fc->ncols)
9904 reg_prefix = bad_reg;
9905 if (! do_debug_frames_interp || *reg_prefix != '\0')
9906 printf (" DW_CFA_restore_extended: %s%s\n",
9907 reg_prefix, regname (reg, 0));
9908 if (*reg_prefix != '\0')
9909 break;
9911 if (reg >= cie->ncols
9912 || cie->col_type[reg] == DW_CFA_unreferenced)
9914 fc->col_type[reg] = DW_CFA_undefined;
9915 fc->col_offset[reg] = 0;
9917 else
9919 fc->col_type[reg] = cie->col_type[reg];
9920 fc->col_offset[reg] = cie->col_offset[reg];
9922 break;
9924 case DW_CFA_undefined:
9925 READ_ULEB (reg, start, block_end);
9926 if (reg >= fc->ncols)
9927 reg_prefix = bad_reg;
9928 if (! do_debug_frames_interp || *reg_prefix != '\0')
9929 printf (" DW_CFA_undefined: %s%s\n",
9930 reg_prefix, regname (reg, 0));
9931 if (*reg_prefix == '\0')
9933 fc->col_type[reg] = DW_CFA_undefined;
9934 fc->col_offset[reg] = 0;
9936 break;
9938 case DW_CFA_same_value:
9939 READ_ULEB (reg, start, block_end);
9940 if (reg >= fc->ncols)
9941 reg_prefix = bad_reg;
9942 if (! do_debug_frames_interp || *reg_prefix != '\0')
9943 printf (" DW_CFA_same_value: %s%s\n",
9944 reg_prefix, regname (reg, 0));
9945 if (*reg_prefix == '\0')
9947 fc->col_type[reg] = DW_CFA_same_value;
9948 fc->col_offset[reg] = 0;
9950 break;
9952 case DW_CFA_register:
9953 READ_ULEB (reg, start, block_end);
9954 READ_ULEB (ofs, start, block_end);
9955 if (reg >= fc->ncols)
9956 reg_prefix = bad_reg;
9957 if (! do_debug_frames_interp || *reg_prefix != '\0')
9959 printf (" DW_CFA_register: %s%s in ",
9960 reg_prefix, regname (reg, 0));
9961 puts (regname (ofs, 0));
9963 if (*reg_prefix == '\0')
9965 fc->col_type[reg] = DW_CFA_register;
9966 fc->col_offset[reg] = ofs;
9968 break;
9970 case DW_CFA_remember_state:
9971 if (! do_debug_frames_interp)
9972 printf (" DW_CFA_remember_state\n");
9973 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9974 rs->cfa_offset = fc->cfa_offset;
9975 rs->cfa_reg = fc->cfa_reg;
9976 rs->ra = fc->ra;
9977 rs->cfa_exp = fc->cfa_exp;
9978 rs->ncols = fc->ncols;
9979 rs->col_type = xcmalloc (rs->ncols, sizeof (*rs->col_type));
9980 rs->col_offset = xcmalloc (rs->ncols, sizeof (*rs->col_offset));
9981 memcpy (rs->col_type, fc->col_type,
9982 rs->ncols * sizeof (*fc->col_type));
9983 memcpy (rs->col_offset, fc->col_offset,
9984 rs->ncols * sizeof (*fc->col_offset));
9985 rs->next = remembered_state;
9986 remembered_state = rs;
9987 break;
9989 case DW_CFA_restore_state:
9990 if (! do_debug_frames_interp)
9991 printf (" DW_CFA_restore_state\n");
9992 rs = remembered_state;
9993 if (rs)
9995 remembered_state = rs->next;
9996 fc->cfa_offset = rs->cfa_offset;
9997 fc->cfa_reg = rs->cfa_reg;
9998 fc->ra = rs->ra;
9999 fc->cfa_exp = rs->cfa_exp;
10000 if (frame_need_space (fc, rs->ncols - 1) < 0)
10002 warn (_("Invalid column number in saved frame state\n"));
10003 fc->ncols = 0;
10005 else
10007 memcpy (fc->col_type, rs->col_type,
10008 rs->ncols * sizeof (*rs->col_type));
10009 memcpy (fc->col_offset, rs->col_offset,
10010 rs->ncols * sizeof (*rs->col_offset));
10012 free (rs->col_type);
10013 free (rs->col_offset);
10014 free (rs);
10016 else if (do_debug_frames_interp)
10017 printf ("Mismatched DW_CFA_restore_state\n");
10018 break;
10020 case DW_CFA_def_cfa:
10021 READ_ULEB (fc->cfa_reg, start, block_end);
10022 READ_ULEB (fc->cfa_offset, start, block_end);
10023 fc->cfa_exp = 0;
10024 if (! do_debug_frames_interp)
10025 printf (" DW_CFA_def_cfa: %s ofs %d\n",
10026 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
10027 break;
10029 case DW_CFA_def_cfa_register:
10030 READ_ULEB (fc->cfa_reg, start, block_end);
10031 fc->cfa_exp = 0;
10032 if (! do_debug_frames_interp)
10033 printf (" DW_CFA_def_cfa_register: %s\n",
10034 regname (fc->cfa_reg, 0));
10035 break;
10037 case DW_CFA_def_cfa_offset:
10038 READ_ULEB (fc->cfa_offset, start, block_end);
10039 if (! do_debug_frames_interp)
10040 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
10041 break;
10043 case DW_CFA_nop:
10044 if (! do_debug_frames_interp)
10045 printf (" DW_CFA_nop\n");
10046 break;
10048 case DW_CFA_def_cfa_expression:
10049 READ_ULEB (ofs, start, block_end);
10050 if (ofs > (size_t) (block_end - start))
10052 printf (_(" %s: <corrupt len %" PRIu64 ">\n"),
10053 "DW_CFA_def_cfa_expression", ofs);
10054 break;
10056 if (! do_debug_frames_interp)
10058 printf (" DW_CFA_def_cfa_expression (");
10059 decode_location_expression (start, eh_addr_size, 0, -1,
10060 ofs, 0, section);
10061 printf (")\n");
10063 fc->cfa_exp = 1;
10064 start += ofs;
10065 break;
10067 case DW_CFA_expression:
10068 READ_ULEB (reg, start, block_end);
10069 READ_ULEB (ofs, start, block_end);
10070 if (reg >= fc->ncols)
10071 reg_prefix = bad_reg;
10072 /* PR 17512: file: 069-133014-0.006. */
10073 /* PR 17512: file: 98c02eb4. */
10074 if (ofs > (size_t) (block_end - start))
10076 printf (_(" %s: <corrupt len %" PRIu64 ">\n"),
10077 "DW_CFA_expression", ofs);
10078 break;
10080 if (! do_debug_frames_interp || *reg_prefix != '\0')
10082 printf (" DW_CFA_expression: %s%s (",
10083 reg_prefix, regname (reg, 0));
10084 decode_location_expression (start, eh_addr_size, 0, -1,
10085 ofs, 0, section);
10086 printf (")\n");
10088 if (*reg_prefix == '\0')
10089 fc->col_type[reg] = DW_CFA_expression;
10090 start += ofs;
10091 break;
10093 case DW_CFA_val_expression:
10094 READ_ULEB (reg, start, block_end);
10095 READ_ULEB (ofs, start, block_end);
10096 if (reg >= fc->ncols)
10097 reg_prefix = bad_reg;
10098 if (ofs > (size_t) (block_end - start))
10100 printf (" %s: <corrupt len %" PRIu64 ">\n",
10101 "DW_CFA_val_expression", ofs);
10102 break;
10104 if (! do_debug_frames_interp || *reg_prefix != '\0')
10106 printf (" DW_CFA_val_expression: %s%s (",
10107 reg_prefix, regname (reg, 0));
10108 decode_location_expression (start, eh_addr_size, 0, -1,
10109 ofs, 0, section);
10110 printf (")\n");
10112 if (*reg_prefix == '\0')
10113 fc->col_type[reg] = DW_CFA_val_expression;
10114 start += ofs;
10115 break;
10117 case DW_CFA_offset_extended_sf:
10118 READ_ULEB (reg, start, block_end);
10119 READ_SLEB (sofs, start, block_end);
10120 /* data_factor multiplicaton done here as unsigned to
10121 avoid integer overflow warnings from asan on fuzzed
10122 objects. */
10123 ofs = sofs;
10124 ofs *= fc->data_factor;
10125 if (reg >= fc->ncols)
10126 reg_prefix = bad_reg;
10127 if (! do_debug_frames_interp || *reg_prefix != '\0')
10128 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+" PRId64 "\n",
10129 reg_prefix, regname (reg, 0), ofs);
10130 if (*reg_prefix == '\0')
10132 fc->col_type[reg] = DW_CFA_offset;
10133 fc->col_offset[reg] = ofs;
10135 break;
10137 case DW_CFA_val_offset_sf:
10138 READ_ULEB (reg, start, block_end);
10139 READ_SLEB (sofs, start, block_end);
10140 ofs = sofs;
10141 ofs *= fc->data_factor;
10142 if (reg >= fc->ncols)
10143 reg_prefix = bad_reg;
10144 if (! do_debug_frames_interp || *reg_prefix != '\0')
10145 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+" PRId64 "\n",
10146 reg_prefix, regname (reg, 0), ofs);
10147 if (*reg_prefix == '\0')
10149 fc->col_type[reg] = DW_CFA_val_offset;
10150 fc->col_offset[reg] = ofs;
10152 break;
10154 case DW_CFA_def_cfa_sf:
10155 READ_ULEB (fc->cfa_reg, start, block_end);
10156 READ_SLEB (sofs, start, block_end);
10157 ofs = sofs;
10158 ofs *= fc->data_factor;
10159 fc->cfa_offset = ofs;
10160 fc->cfa_exp = 0;
10161 if (! do_debug_frames_interp)
10162 printf (" DW_CFA_def_cfa_sf: %s ofs %" PRId64 "\n",
10163 regname (fc->cfa_reg, 0), ofs);
10164 break;
10166 case DW_CFA_def_cfa_offset_sf:
10167 READ_SLEB (sofs, start, block_end);
10168 ofs = sofs;
10169 ofs *= fc->data_factor;
10170 fc->cfa_offset = ofs;
10171 if (! do_debug_frames_interp)
10172 printf (" DW_CFA_def_cfa_offset_sf: %" PRId64 "\n", ofs);
10173 break;
10175 case DW_CFA_MIPS_advance_loc8:
10176 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
10177 ofs *= fc->code_factor;
10178 if (do_debug_frames_interp)
10179 frame_display_row (fc, &need_col_headers, &max_regs);
10180 else
10182 printf (" DW_CFA_MIPS_advance_loc8: %" PRId64 " to ", ofs);
10183 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
10184 printf ("\n");
10186 fc->pc_begin += ofs;
10187 break;
10189 case DW_CFA_GNU_window_save:
10190 if (! do_debug_frames_interp)
10191 printf (" %s\n", DW_CFA_GNU_window_save_name[is_aarch64]);
10192 break;
10194 case DW_CFA_GNU_args_size:
10195 READ_ULEB (ofs, start, block_end);
10196 if (! do_debug_frames_interp)
10197 printf (" DW_CFA_GNU_args_size: %" PRIu64 "\n", ofs);
10198 break;
10200 case DW_CFA_GNU_negative_offset_extended:
10201 READ_ULEB (reg, start, block_end);
10202 READ_SLEB (sofs, start, block_end);
10203 ofs = sofs;
10204 ofs = -ofs * fc->data_factor;
10205 if (reg >= fc->ncols)
10206 reg_prefix = bad_reg;
10207 if (! do_debug_frames_interp || *reg_prefix != '\0')
10208 printf (" DW_CFA_GNU_negative_offset_extended: %s%s "
10209 "at cfa%+" PRId64 "\n",
10210 reg_prefix, regname (reg, 0), ofs);
10211 if (*reg_prefix == '\0')
10213 fc->col_type[reg] = DW_CFA_offset;
10214 fc->col_offset[reg] = ofs;
10216 break;
10218 default:
10219 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
10220 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
10221 else
10222 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
10223 start = block_end;
10227 /* Interpret the CFA - as long as it is not completely full of NOPs. */
10228 if (do_debug_frames_interp && ! all_nops)
10229 frame_display_row (fc, &need_col_headers, &max_regs);
10231 if (fde_fc.col_type != NULL)
10233 free (fde_fc.col_type);
10234 fde_fc.col_type = NULL;
10236 if (fde_fc.col_offset != NULL)
10238 free (fde_fc.col_offset);
10239 fde_fc.col_offset = NULL;
10242 start = block_end;
10243 eh_addr_size = saved_eh_addr_size;
10246 printf ("\n");
10248 while (remembered_state != NULL)
10250 rs = remembered_state;
10251 remembered_state = rs->next;
10252 free (rs->col_type);
10253 free (rs->col_offset);
10254 rs->next = NULL; /* Paranoia. */
10255 free (rs);
10258 while (chunks != NULL)
10260 rs = chunks;
10261 chunks = rs->next;
10262 free (rs->col_type);
10263 free (rs->col_offset);
10264 rs->next = NULL; /* Paranoia. */
10265 free (rs);
10268 while (forward_refs != NULL)
10270 rs = forward_refs;
10271 forward_refs = rs->next;
10272 free (rs->col_type);
10273 free (rs->col_offset);
10274 rs->next = NULL; /* Paranoia. */
10275 free (rs);
10278 return 1;
10281 #undef GET
10283 static int
10284 display_debug_names (struct dwarf_section *section, void *file)
10286 unsigned char *hdrptr = section->start;
10287 uint64_t unit_length;
10288 unsigned char *unit_start;
10289 const unsigned char *const section_end = section->start + section->size;
10290 unsigned char *unit_end;
10292 introduce (section, false);
10294 load_debug_section_with_follow (str, file);
10296 for (; hdrptr < section_end; hdrptr = unit_end)
10298 unsigned int offset_size;
10299 uint16_t dwarf_version, padding;
10300 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
10301 uint64_t bucket_count, name_count, abbrev_table_size;
10302 uint32_t augmentation_string_size;
10303 unsigned int i;
10304 bool augmentation_printable;
10305 const char *augmentation_string;
10306 size_t total;
10308 unit_start = hdrptr;
10310 /* Get and check the length of the block. */
10311 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
10313 if (unit_length == 0xffffffff)
10315 /* This section is 64-bit DWARF. */
10316 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
10317 offset_size = 8;
10319 else
10320 offset_size = 4;
10322 if (unit_length > (size_t) (section_end - hdrptr)
10323 || unit_length < 2 + 2 + 4 * 7)
10325 too_short:
10326 warn (_("Debug info is corrupted, %s header at %#tx"
10327 " has length %#" PRIx64 "\n"),
10328 section->name, unit_start - section->start, unit_length);
10329 return 0;
10331 unit_end = hdrptr + unit_length;
10333 /* Get and check the version number. */
10334 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
10335 printf (_("Version %d\n"), (int) dwarf_version);
10337 /* Prior versions did not exist, and future versions may not be
10338 backwards compatible. */
10339 if (dwarf_version != 5)
10341 warn (_("Only DWARF version 5 .debug_names "
10342 "is currently supported.\n"));
10343 return 0;
10346 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
10347 if (padding != 0)
10348 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
10349 padding);
10351 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
10352 if (comp_unit_count == 0)
10353 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
10355 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
10356 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
10357 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
10358 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
10359 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
10361 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
10362 if (augmentation_string_size % 4 != 0)
10364 warn (_("Augmentation string length %u must be rounded up "
10365 "to a multiple of 4 in .debug_names.\n"),
10366 augmentation_string_size);
10367 augmentation_string_size += (-augmentation_string_size) & 3;
10369 if (augmentation_string_size > (size_t) (unit_end - hdrptr))
10370 goto too_short;
10372 printf (_("Augmentation string:"));
10374 augmentation_printable = true;
10375 augmentation_string = (const char *) hdrptr;
10377 for (i = 0; i < augmentation_string_size; i++)
10379 unsigned char uc;
10381 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
10382 printf (" %02x", uc);
10384 if (uc != 0 && !ISPRINT (uc))
10385 augmentation_printable = false;
10388 if (augmentation_printable)
10390 printf (" (\"");
10391 for (i = 0;
10392 i < augmentation_string_size && augmentation_string[i];
10393 ++i)
10394 putchar (augmentation_string[i]);
10395 printf ("\")");
10397 putchar ('\n');
10399 printf (_("CU table:\n"));
10400 if (_mul_overflow (comp_unit_count, offset_size, &total)
10401 || total > (size_t) (unit_end - hdrptr))
10402 goto too_short;
10403 for (i = 0; i < comp_unit_count; i++)
10405 uint64_t cu_offset;
10407 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
10408 printf ("[%3u] %#" PRIx64 "\n", i, cu_offset);
10410 putchar ('\n');
10412 printf (_("TU table:\n"));
10413 if (_mul_overflow (local_type_unit_count, offset_size, &total)
10414 || total > (size_t) (unit_end - hdrptr))
10415 goto too_short;
10416 for (i = 0; i < local_type_unit_count; i++)
10418 uint64_t tu_offset;
10420 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
10421 printf ("[%3u] %#" PRIx64 "\n", i, tu_offset);
10423 putchar ('\n');
10425 printf (_("Foreign TU table:\n"));
10426 if (_mul_overflow (foreign_type_unit_count, 8, &total)
10427 || total > (size_t) (unit_end - hdrptr))
10428 goto too_short;
10429 for (i = 0; i < foreign_type_unit_count; i++)
10431 uint64_t signature;
10433 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
10434 printf (_("[%3u] "), i);
10435 print_hex_ns (signature, 8);
10436 putchar ('\n');
10438 putchar ('\n');
10440 uint64_t xtra = (bucket_count * sizeof (uint32_t)
10441 + name_count * (sizeof (uint32_t) + 2 * offset_size)
10442 + abbrev_table_size);
10443 if (xtra > (size_t) (unit_end - hdrptr))
10445 warn (_("Entry pool offset (%#" PRIx64 ") exceeds unit size %#tx "
10446 "for unit %#tx in the debug_names\n"),
10447 xtra, unit_end - unit_start, unit_start - section->start);
10448 return 0;
10450 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
10451 hdrptr += bucket_count * sizeof (uint32_t);
10452 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
10453 hdrptr += name_count * sizeof (uint32_t);
10454 unsigned char *const name_table_string_offsets = hdrptr;
10455 hdrptr += name_count * offset_size;
10456 unsigned char *const name_table_entry_offsets = hdrptr;
10457 hdrptr += name_count * offset_size;
10458 unsigned char *const abbrev_table = hdrptr;
10459 hdrptr += abbrev_table_size;
10460 const unsigned char *const abbrev_table_end = hdrptr;
10461 unsigned char *const entry_pool = hdrptr;
10463 size_t buckets_filled = 0;
10464 size_t bucketi;
10465 for (bucketi = 0; bucketi < bucket_count; bucketi++)
10467 const uint32_t bucket = hash_table_buckets[bucketi];
10469 if (bucket != 0)
10470 ++buckets_filled;
10472 printf (ngettext ("Used %zu of %lu bucket.\n",
10473 "Used %zu of %lu buckets.\n",
10474 (unsigned long) bucket_count),
10475 buckets_filled, (unsigned long) bucket_count);
10477 if (bucket_count != 0)
10479 uint32_t hash_prev = 0;
10480 size_t hash_clash_count = 0;
10481 size_t longest_clash = 0;
10482 size_t this_length = 0;
10483 size_t hashi;
10484 for (hashi = 0; hashi < name_count; hashi++)
10486 const uint32_t hash_this = hash_table_hashes[hashi];
10488 if (hashi > 0)
10490 if (hash_prev % bucket_count == hash_this % bucket_count)
10492 ++hash_clash_count;
10493 ++this_length;
10494 longest_clash = MAX (longest_clash, this_length);
10496 else
10497 this_length = 0;
10499 hash_prev = hash_this;
10501 printf (_("Out of %" PRIu64 " items there are %zu bucket clashes"
10502 " (longest of %zu entries).\n"),
10503 name_count, hash_clash_count, longest_clash);
10505 if (name_count != buckets_filled + hash_clash_count)
10506 warn (_("The name_count (%" PRIu64 ")"
10507 " is not the same as the used bucket_count"
10508 " (%zu) + the hash clash count (%zu)"),
10509 name_count, buckets_filled, hash_clash_count);
10512 struct abbrev_lookup_entry
10514 uint64_t abbrev_tag;
10515 unsigned char *abbrev_lookup_ptr;
10517 struct abbrev_lookup_entry *abbrev_lookup = NULL;
10518 size_t abbrev_lookup_used = 0;
10519 size_t abbrev_lookup_allocated = 0;
10521 unsigned char *abbrevptr = abbrev_table;
10522 for (;;)
10524 uint64_t abbrev_tag;
10526 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
10527 if (abbrev_tag == 0)
10528 break;
10529 if (abbrev_lookup_used == abbrev_lookup_allocated)
10531 abbrev_lookup_allocated = MAX (0x100,
10532 abbrev_lookup_allocated * 2);
10533 abbrev_lookup = xrealloc (abbrev_lookup,
10534 (abbrev_lookup_allocated
10535 * sizeof (*abbrev_lookup)));
10537 assert (abbrev_lookup_used < abbrev_lookup_allocated);
10538 struct abbrev_lookup_entry *entry;
10539 for (entry = abbrev_lookup;
10540 entry < abbrev_lookup + abbrev_lookup_used;
10541 entry++)
10542 if (entry->abbrev_tag == abbrev_tag)
10544 warn (_("Duplicate abbreviation tag %" PRIu64
10545 " in unit %#tx in the debug_names section\n"),
10546 abbrev_tag, unit_start - section->start);
10547 break;
10549 entry = &abbrev_lookup[abbrev_lookup_used++];
10550 entry->abbrev_tag = abbrev_tag;
10551 entry->abbrev_lookup_ptr = abbrevptr;
10553 /* Skip DWARF tag. */
10554 SKIP_ULEB (abbrevptr, abbrev_table_end);
10555 for (;;)
10557 uint64_t xindex, form;
10559 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10560 READ_ULEB (form, abbrevptr, abbrev_table_end);
10561 if (xindex == 0 && form == 0)
10562 break;
10566 printf (_("\nSymbol table:\n"));
10567 uint32_t namei;
10568 for (namei = 0; namei < name_count; ++namei)
10570 uint64_t string_offset, entry_offset;
10571 unsigned char *p;
10573 p = name_table_string_offsets + namei * offset_size;
10574 SAFE_BYTE_GET (string_offset, p, offset_size, unit_end);
10575 p = name_table_entry_offsets + namei * offset_size;
10576 SAFE_BYTE_GET (entry_offset, p, offset_size, unit_end);
10578 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
10579 fetch_indirect_string (string_offset));
10581 unsigned char *entryptr = entry_pool + entry_offset;
10583 /* We need to scan first whether there is a single or multiple
10584 entries. TAGNO is -2 for the first entry, it is -1 for the
10585 initial tag read of the second entry, then it becomes 0 for the
10586 first entry for real printing etc. */
10587 int tagno = -2;
10588 /* Initialize it due to a false compiler warning. */
10589 uint64_t second_abbrev_tag = -1;
10590 for (;;)
10592 uint64_t abbrev_tag;
10593 uint64_t dwarf_tag;
10594 const struct abbrev_lookup_entry *entry;
10596 READ_ULEB (abbrev_tag, entryptr, unit_end);
10597 if (tagno == -1)
10599 second_abbrev_tag = abbrev_tag;
10600 tagno = 0;
10601 entryptr = entry_pool + entry_offset;
10602 continue;
10604 if (abbrev_tag == 0)
10605 break;
10606 if (tagno >= 0)
10607 printf ("%s<%" PRIu64 ">",
10608 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
10609 abbrev_tag);
10611 for (entry = abbrev_lookup;
10612 entry < abbrev_lookup + abbrev_lookup_used;
10613 entry++)
10614 if (entry->abbrev_tag == abbrev_tag)
10615 break;
10616 if (entry >= abbrev_lookup + abbrev_lookup_used)
10618 warn (_("Undefined abbreviation tag %" PRId64
10619 " in unit %#tx in the debug_names section\n"),
10620 abbrev_tag,
10621 unit_start - section->start);
10622 break;
10624 abbrevptr = entry->abbrev_lookup_ptr;
10625 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
10626 if (tagno >= 0)
10627 printf (" %s", get_TAG_name (dwarf_tag));
10628 for (;;)
10630 uint64_t xindex, form;
10632 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10633 READ_ULEB (form, abbrevptr, abbrev_table_end);
10634 if (xindex == 0 && form == 0)
10635 break;
10637 if (tagno >= 0)
10638 printf (" %s", get_IDX_name (xindex));
10639 entryptr = read_and_display_attr_value (0, form, 0,
10640 unit_start, entryptr, unit_end,
10641 0, 0, offset_size,
10642 dwarf_version, NULL,
10643 (tagno < 0), section,
10644 NULL, '=', -1);
10646 ++tagno;
10648 if (tagno <= 0)
10649 printf (_(" <no entries>"));
10650 putchar ('\n');
10653 free (abbrev_lookup);
10656 return 1;
10659 static int
10660 display_debug_links (struct dwarf_section * section,
10661 void * file ATTRIBUTE_UNUSED)
10663 const unsigned char * filename;
10664 unsigned int filelen;
10666 introduce (section, false);
10668 /* The .gnu_debuglink section is formatted as:
10669 (c-string) Filename.
10670 (padding) If needed to reach a 4 byte boundary.
10671 (uint32_t) CRC32 value.
10673 The .gun_debugaltlink section is formatted as:
10674 (c-string) Filename.
10675 (binary) Build-ID. */
10677 filename = section->start;
10678 filelen = strnlen ((const char *) filename, section->size);
10679 if (filelen == section->size)
10681 warn (_("The debuglink filename is corrupt/missing\n"));
10682 return 0;
10685 printf (_(" Separate debug info file: %s\n"), filename);
10687 if (startswith (section->name, ".gnu_debuglink"))
10689 unsigned int crc32;
10690 unsigned int crc_offset;
10692 crc_offset = filelen + 1;
10693 crc_offset = (crc_offset + 3) & ~3;
10694 if (crc_offset + 4 > section->size)
10696 warn (_("CRC offset missing/truncated\n"));
10697 return 0;
10700 crc32 = byte_get (filename + crc_offset, 4);
10702 printf (_(" CRC value: %#x\n"), crc32);
10704 if (crc_offset + 4 < section->size)
10706 warn (_("There are %#" PRIx64
10707 " extraneous bytes at the end of the section\n"),
10708 section->size - (crc_offset + 4));
10709 return 0;
10712 else /* startswith (section->name, ".gnu_debugaltlink") */
10714 const unsigned char *build_id = section->start + filelen + 1;
10715 size_t build_id_len = section->size - (filelen + 1);
10716 size_t printed;
10718 /* FIXME: Should we support smaller build-id notes ? */
10719 if (build_id_len < 0x14)
10721 warn (_("Build-ID is too short (%#zx bytes)\n"), build_id_len);
10722 return 0;
10725 printed = printf (_(" Build-ID (%#zx bytes):"), build_id_len);
10726 display_data (printed, build_id, build_id_len);
10727 putchar ('\n');
10730 putchar ('\n');
10731 return 1;
10734 static int
10735 display_gdb_index (struct dwarf_section *section,
10736 void *file ATTRIBUTE_UNUSED)
10738 unsigned char *start = section->start;
10739 uint32_t version;
10740 uint32_t cu_list_offset, tu_list_offset;
10741 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
10742 unsigned int cu_list_elements, tu_list_elements;
10743 unsigned int address_table_elements, symbol_table_slots;
10744 unsigned char *cu_list, *tu_list;
10745 unsigned char *address_table, *symbol_table, *constant_pool;
10746 unsigned int i;
10748 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
10750 introduce (section, false);
10752 if (section->size < 6 * sizeof (uint32_t))
10754 warn (_("Truncated header in the %s section.\n"), section->name);
10755 return 0;
10758 version = byte_get_little_endian (start, 4);
10759 printf (_("Version %lu\n"), (unsigned long) version);
10761 /* Prior versions are obsolete, and future versions may not be
10762 backwards compatible. */
10763 if (version < 3 || version > 8)
10765 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
10766 return 0;
10768 if (version < 4)
10769 warn (_("The address table data in version 3 may be wrong.\n"));
10770 if (version < 5)
10771 warn (_("Version 4 does not support case insensitive lookups.\n"));
10772 if (version < 6)
10773 warn (_("Version 5 does not include inlined functions.\n"));
10774 if (version < 7)
10775 warn (_("Version 6 does not include symbol attributes.\n"));
10776 /* Version 7 indices generated by Gold have bad type unit references,
10777 PR binutils/15021. But we don't know if the index was generated by
10778 Gold or not, so to avoid worrying users with gdb-generated indices
10779 we say nothing for version 7 here. */
10781 cu_list_offset = byte_get_little_endian (start + 4, 4);
10782 tu_list_offset = byte_get_little_endian (start + 8, 4);
10783 address_table_offset = byte_get_little_endian (start + 12, 4);
10784 symbol_table_offset = byte_get_little_endian (start + 16, 4);
10785 constant_pool_offset = byte_get_little_endian (start + 20, 4);
10787 if (cu_list_offset > section->size
10788 || tu_list_offset > section->size
10789 || address_table_offset > section->size
10790 || symbol_table_offset > section->size
10791 || constant_pool_offset > section->size
10792 || tu_list_offset < cu_list_offset
10793 || address_table_offset < tu_list_offset
10794 || symbol_table_offset < address_table_offset
10795 || constant_pool_offset < symbol_table_offset)
10797 warn (_("Corrupt header in the %s section.\n"), section->name);
10798 return 0;
10801 cu_list_elements = (tu_list_offset - cu_list_offset) / 16;
10802 tu_list_elements = (address_table_offset - tu_list_offset) / 24;
10803 address_table_elements = (symbol_table_offset - address_table_offset) / 20;
10804 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
10806 cu_list = start + cu_list_offset;
10807 tu_list = start + tu_list_offset;
10808 address_table = start + address_table_offset;
10809 symbol_table = start + symbol_table_offset;
10810 constant_pool = start + constant_pool_offset;
10812 printf (_("\nCU table:\n"));
10813 for (i = 0; i < cu_list_elements; i++)
10815 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 16, 8);
10816 uint64_t cu_length = byte_get_little_endian (cu_list + i * 16 + 8, 8);
10818 printf ("[%3u] %#" PRIx64 " - %#" PRIx64 "\n",
10819 i, cu_offset, cu_offset + cu_length - 1);
10822 printf (_("\nTU table:\n"));
10823 for (i = 0; i < tu_list_elements; i++)
10825 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 24, 8);
10826 uint64_t type_offset = byte_get_little_endian (tu_list + i * 24 + 8, 8);
10827 uint64_t signature = byte_get_little_endian (tu_list + i * 24 + 16, 8);
10829 printf ("[%3u] %#" PRIx64 " %#" PRIx64 " ",
10830 i, tu_offset, type_offset);
10831 print_hex_ns (signature, 8);
10832 printf ("\n");
10835 printf (_("\nAddress table:\n"));
10836 for (i = 0; i < address_table_elements; i++)
10838 uint64_t low = byte_get_little_endian (address_table + i * 20, 8);
10839 uint64_t high = byte_get_little_endian (address_table + i * 20 + 8, 8);
10840 uint32_t cu_index = byte_get_little_endian (address_table + i * 20 + 16, 4);
10842 print_hex (low, 8);
10843 print_hex (high, 8);
10844 printf ("%" PRIu32 "\n", cu_index);
10847 printf (_("\nSymbol table:\n"));
10848 for (i = 0; i < symbol_table_slots; ++i)
10850 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10851 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10852 uint32_t num_cus, cu;
10854 if (name_offset != 0
10855 || cu_vector_offset != 0)
10857 unsigned int j;
10859 /* PR 17531: file: 5b7b07ad. */
10860 if (name_offset >= section->size - constant_pool_offset)
10862 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10863 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10864 name_offset, i);
10866 else
10867 printf ("[%3u] %.*s:", i,
10868 (int) (section->size - (constant_pool_offset + name_offset)),
10869 constant_pool + name_offset);
10871 if (section->size - constant_pool_offset < 4
10872 || cu_vector_offset > section->size - constant_pool_offset - 4)
10874 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10875 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10876 cu_vector_offset, i);
10877 continue;
10880 num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
10882 if ((uint64_t) num_cus * 4 > section->size - (constant_pool_offset
10883 + cu_vector_offset + 4))
10885 printf ("<invalid number of CUs: %d>\n", num_cus);
10886 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
10887 num_cus, i);
10888 continue;
10891 if (num_cus > 1)
10892 printf ("\n");
10894 for (j = 0; j < num_cus; ++j)
10896 int is_static;
10897 gdb_index_symbol_kind kind;
10899 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
10900 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
10901 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10902 cu = GDB_INDEX_CU_VALUE (cu);
10903 /* Convert to TU number if it's for a type unit. */
10904 if (cu >= cu_list_elements)
10905 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10906 (unsigned long) cu - cu_list_elements);
10907 else
10908 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10910 printf (" [%s, %s]",
10911 is_static ? _("static") : _("global"),
10912 get_gdb_index_symbol_kind_name (kind));
10913 if (num_cus > 1)
10914 printf ("\n");
10916 if (num_cus <= 1)
10917 printf ("\n");
10921 return 1;
10924 /* Pre-allocate enough space for the CU/TU sets needed. */
10926 static void
10927 prealloc_cu_tu_list (unsigned int nshndx)
10929 if (nshndx == 0)
10930 /* Always allocate at least one entry for the end-marker. */
10931 nshndx = 1;
10933 if (shndx_pool == NULL)
10935 shndx_pool_size = nshndx;
10936 shndx_pool_used = 0;
10937 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10938 sizeof (unsigned int));
10940 else
10942 shndx_pool_size = shndx_pool_used + nshndx;
10943 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10944 sizeof (unsigned int));
10948 static void
10949 add_shndx_to_cu_tu_entry (unsigned int shndx)
10951 shndx_pool [shndx_pool_used++] = shndx;
10954 static void
10955 end_cu_tu_entry (void)
10957 shndx_pool [shndx_pool_used++] = 0;
10960 /* Return the short name of a DWARF section given by a DW_SECT enumerator. */
10962 static const char *
10963 get_DW_SECT_short_name (unsigned int dw_sect)
10965 static char buf[16];
10967 switch (dw_sect)
10969 case DW_SECT_INFO:
10970 return "info";
10971 case DW_SECT_TYPES:
10972 return "types";
10973 case DW_SECT_ABBREV:
10974 return "abbrev";
10975 case DW_SECT_LINE:
10976 return "line";
10977 case DW_SECT_LOC:
10978 return "loc";
10979 case DW_SECT_STR_OFFSETS:
10980 return "str_off";
10981 case DW_SECT_MACINFO:
10982 return "macinfo";
10983 case DW_SECT_MACRO:
10984 return "macro";
10985 default:
10986 break;
10989 snprintf (buf, sizeof (buf), "%d", dw_sect);
10990 return buf;
10993 /* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
10994 These sections are extensions for Fission.
10995 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
10997 static bool
10998 process_cu_tu_index (struct dwarf_section *section, int do_display)
11000 unsigned char *phdr = section->start;
11001 unsigned char *limit = phdr + section->size;
11002 unsigned char *phash;
11003 unsigned char *pindex;
11004 unsigned char *ppool;
11005 unsigned int version;
11006 unsigned int ncols = 0;
11007 unsigned int nused;
11008 unsigned int nslots;
11009 unsigned int i;
11010 unsigned int j;
11011 uint64_t signature;
11012 size_t total;
11014 /* PR 17512: file: 002-168123-0.004. */
11015 if (phdr == NULL)
11017 warn (_("Section %s is empty\n"), section->name);
11018 return false;
11020 /* PR 17512: file: 002-376-0.004. */
11021 if (section->size < 24)
11023 warn (_("Section %s is too small to contain a CU/TU header\n"),
11024 section->name);
11025 return false;
11028 phash = phdr;
11029 SAFE_BYTE_GET_AND_INC (version, phash, 4, limit);
11030 if (version >= 2)
11031 SAFE_BYTE_GET_AND_INC (ncols, phash, 4, limit);
11032 SAFE_BYTE_GET_AND_INC (nused, phash, 4, limit);
11033 SAFE_BYTE_GET_AND_INC (nslots, phash, 4, limit);
11035 pindex = phash + (size_t) nslots * 8;
11036 ppool = pindex + (size_t) nslots * 4;
11038 if (do_display)
11040 introduce (section, false);
11042 printf (_(" Version: %u\n"), version);
11043 if (version >= 2)
11044 printf (_(" Number of columns: %u\n"), ncols);
11045 printf (_(" Number of used entries: %u\n"), nused);
11046 printf (_(" Number of slots: %u\n\n"), nslots);
11049 /* PR 17531: file: 45d69832. */
11050 if (_mul_overflow ((size_t) nslots, 12, &total)
11051 || total > (size_t) (limit - phash))
11053 warn (ngettext ("Section %s is too small for %u slot\n",
11054 "Section %s is too small for %u slots\n",
11055 nslots),
11056 section->name, nslots);
11057 return false;
11060 if (version == 1)
11062 unsigned char *shndx_list;
11063 unsigned int shndx;
11065 if (!do_display)
11067 prealloc_cu_tu_list ((limit - ppool) / 4);
11068 for (shndx_list = ppool + 4; shndx_list <= limit - 4; shndx_list += 4)
11070 shndx = byte_get (shndx_list, 4);
11071 add_shndx_to_cu_tu_entry (shndx);
11073 end_cu_tu_entry ();
11075 else
11076 for (i = 0; i < nslots; i++)
11078 SAFE_BYTE_GET (signature, phash, 8, limit);
11079 if (signature != 0)
11081 SAFE_BYTE_GET (j, pindex, 4, limit);
11082 shndx_list = ppool + j * 4;
11083 /* PR 17531: file: 705e010d. */
11084 if (shndx_list < ppool)
11086 warn (_("Section index pool located before start of section\n"));
11087 return false;
11090 printf (_(" [%3d] Signature: %#" PRIx64 " Sections: "),
11091 i, signature);
11092 for (;;)
11094 if (shndx_list >= limit)
11096 warn (_("Section %s too small for shndx pool\n"),
11097 section->name);
11098 return false;
11100 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
11101 if (shndx == 0)
11102 break;
11103 printf (" %d", shndx);
11104 shndx_list += 4;
11106 printf ("\n");
11108 phash += 8;
11109 pindex += 4;
11112 else if (version == 2)
11114 unsigned int val;
11115 unsigned int dw_sect;
11116 unsigned char *ph = phash;
11117 unsigned char *pi = pindex;
11118 unsigned char *poffsets = ppool + (size_t) ncols * 4;
11119 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
11120 bool is_tu_index;
11121 struct cu_tu_set *this_set = NULL;
11122 unsigned int row;
11123 unsigned char *prow;
11124 size_t temp;
11126 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
11128 /* PR 17531: file: 0dd159bf.
11129 Check for integer overflow (can occur when size_t is 32-bit)
11130 with overlarge ncols or nused values. */
11131 if (nused == -1u
11132 || _mul_overflow ((size_t) ncols, 4, &temp)
11133 || _mul_overflow ((size_t) nused + 1, temp, &total)
11134 || total > (size_t) (limit - ppool)
11135 /* PR 30227: ncols could be 0. */
11136 || _mul_overflow ((size_t) nused + 1, 4, &total)
11137 || total > (size_t) (limit - ppool))
11139 warn (_("Section %s too small for offset and size tables\n"),
11140 section->name);
11141 return false;
11144 if (do_display)
11146 printf (_(" Offset table\n"));
11147 printf (" slot %-16s ",
11148 is_tu_index ? _("signature") : _("dwo_id"));
11150 else
11152 if (is_tu_index)
11154 tu_count = nused;
11155 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
11156 this_set = tu_sets;
11158 else
11160 cu_count = nused;
11161 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
11162 this_set = cu_sets;
11166 if (do_display)
11168 for (j = 0; j < ncols; j++)
11170 unsigned char *p = ppool + j * 4;
11171 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11172 printf (" %8s", get_DW_SECT_short_name (dw_sect));
11174 printf ("\n");
11177 for (i = 0; i < nslots; i++)
11179 SAFE_BYTE_GET (signature, ph, 8, limit);
11181 SAFE_BYTE_GET (row, pi, 4, limit);
11182 if (row != 0)
11184 /* PR 17531: file: a05f6ab3. */
11185 if (row > nused)
11187 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
11188 row, nused);
11189 return false;
11192 if (!do_display)
11194 size_t num_copy = sizeof (uint64_t);
11196 memcpy (&this_set[row - 1].signature, ph, num_copy);
11199 prow = poffsets + (row - 1) * ncols * 4;
11200 if (do_display)
11201 printf (" [%3d] %#" PRIx64, i, signature);
11202 for (j = 0; j < ncols; j++)
11204 unsigned char *p = prow + j * 4;
11205 SAFE_BYTE_GET (val, p, 4, limit);
11206 if (do_display)
11207 printf (" %8d", val);
11208 else
11210 p = ppool + j * 4;
11211 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11213 /* PR 17531: file: 10796eb3. */
11214 if (dw_sect >= DW_SECT_MAX)
11215 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11216 else
11217 this_set [row - 1].section_offsets [dw_sect] = val;
11221 if (do_display)
11222 printf ("\n");
11224 ph += 8;
11225 pi += 4;
11228 ph = phash;
11229 pi = pindex;
11230 if (do_display)
11232 printf ("\n");
11233 printf (_(" Size table\n"));
11234 printf (" slot %-16s ",
11235 is_tu_index ? _("signature") : _("dwo_id"));
11238 for (j = 0; j < ncols; j++)
11240 unsigned char *p = ppool + j * 4;
11241 SAFE_BYTE_GET (val, p, 4, limit);
11242 if (do_display)
11243 printf (" %8s", get_DW_SECT_short_name (val));
11246 if (do_display)
11247 printf ("\n");
11249 for (i = 0; i < nslots; i++)
11251 SAFE_BYTE_GET (signature, ph, 8, limit);
11253 SAFE_BYTE_GET (row, pi, 4, limit);
11254 if (row != 0)
11256 prow = psizes + (row - 1) * ncols * 4;
11258 if (do_display)
11259 printf (" [%3d] %#" PRIx64, i, signature);
11261 for (j = 0; j < ncols; j++)
11263 unsigned char *p = prow + j * 4;
11265 /* PR 28645: Check for overflow. Since we do not know how
11266 many populated rows there will be, we cannot just
11267 perform a single check at the start of this function. */
11268 if (p > (limit - 4))
11270 if (do_display)
11271 printf ("\n");
11272 warn (_("Too many rows/columns in DWARF index section %s\n"),
11273 section->name);
11274 return false;
11277 SAFE_BYTE_GET (val, p, 4, limit);
11279 if (do_display)
11280 printf (" %8d", val);
11281 else
11283 p = ppool + j * 4;
11284 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11285 if (dw_sect >= DW_SECT_MAX)
11286 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11287 else
11288 this_set [row - 1].section_sizes [dw_sect] = val;
11292 if (do_display)
11293 printf ("\n");
11296 ph += 8;
11297 pi += 4;
11300 else if (do_display)
11301 printf (_(" Unsupported version (%d)\n"), version);
11303 if (do_display)
11304 printf ("\n");
11306 return true;
11309 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
11311 /* Load the CU and TU indexes if present. This will build a list of
11312 section sets that we can use to associate a .debug_info.dwo section
11313 with its associated .debug_abbrev.dwo section in a .dwp file. */
11315 static bool
11316 load_cu_tu_indexes (void *file)
11318 /* If we have already loaded (or tried to load) the CU and TU indexes
11319 then do not bother to repeat the task. */
11320 if (cu_tu_indexes_read == -1)
11322 cu_tu_indexes_read = true;
11324 if (load_debug_section_with_follow (dwp_cu_index, file))
11325 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
11326 cu_tu_indexes_read = false;
11328 if (load_debug_section_with_follow (dwp_tu_index, file))
11329 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
11330 cu_tu_indexes_read = false;
11333 return (bool) cu_tu_indexes_read;
11336 /* Find the set of sections that includes section SHNDX. */
11338 unsigned int *
11339 find_cu_tu_set (void *file, unsigned int shndx)
11341 unsigned int i;
11343 if (! load_cu_tu_indexes (file))
11344 return NULL;
11346 /* Find SHNDX in the shndx pool. */
11347 for (i = 0; i < shndx_pool_used; i++)
11348 if (shndx_pool [i] == shndx)
11349 break;
11351 if (i >= shndx_pool_used)
11352 return NULL;
11354 /* Now backup to find the first entry in the set. */
11355 while (i > 0 && shndx_pool [i - 1] != 0)
11356 i--;
11358 return shndx_pool + i;
11361 /* Display a .debug_cu_index or .debug_tu_index section. */
11363 static int
11364 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
11366 return process_cu_tu_index (section, 1);
11369 static int
11370 display_debug_not_supported (struct dwarf_section *section,
11371 void *file ATTRIBUTE_UNUSED)
11373 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
11374 section->name);
11376 return 1;
11379 /* Like malloc, but takes two parameters like calloc.
11380 Verifies that the first parameter is not too large.
11381 Note: does *not* initialise the allocated memory to zero. */
11383 void *
11384 cmalloc (uint64_t nmemb, size_t size)
11386 /* Check for overflow. */
11387 if (nmemb >= ~(size_t) 0 / size)
11388 return NULL;
11390 return xmalloc (nmemb * size);
11393 /* Like xmalloc, but takes two parameters like calloc.
11394 Verifies that the first parameter is not too large.
11395 Note: does *not* initialise the allocated memory to zero. */
11397 void *
11398 xcmalloc (uint64_t nmemb, size_t size)
11400 /* Check for overflow. */
11401 if (nmemb >= ~(size_t) 0 / size)
11403 fprintf (stderr,
11404 _("Attempt to allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11405 nmemb);
11406 xexit (1);
11409 return xmalloc (nmemb * size);
11412 /* Like xrealloc, but takes three parameters.
11413 Verifies that the second parameter is not too large.
11414 Note: does *not* initialise any new memory to zero. */
11416 void *
11417 xcrealloc (void *ptr, uint64_t nmemb, size_t size)
11419 /* Check for overflow. */
11420 if (nmemb >= ~(size_t) 0 / size)
11422 error (_("Attempt to re-allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11423 nmemb);
11424 xexit (1);
11427 return xrealloc (ptr, nmemb * size);
11430 /* Like xcalloc, but verifies that the first parameter is not too large. */
11432 void *
11433 xcalloc2 (uint64_t nmemb, size_t size)
11435 /* Check for overflow. */
11436 if (nmemb >= ~(size_t) 0 / size)
11438 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: %#" PRIx64 "\n"),
11439 nmemb);
11440 xexit (1);
11443 return xcalloc (nmemb, size);
11446 static unsigned long
11447 calc_gnu_debuglink_crc32 (unsigned long crc,
11448 const unsigned char *buf,
11449 size_t len)
11451 static const unsigned long crc32_table[256] =
11453 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
11454 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
11455 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
11456 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
11457 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
11458 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
11459 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
11460 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
11461 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
11462 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
11463 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
11464 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
11465 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
11466 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
11467 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
11468 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
11469 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
11470 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
11471 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
11472 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
11473 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
11474 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
11475 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
11476 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
11477 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
11478 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
11479 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
11480 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
11481 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
11482 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
11483 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
11484 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
11485 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
11486 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
11487 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
11488 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
11489 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
11490 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
11491 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
11492 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
11493 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
11494 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
11495 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
11496 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
11497 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
11498 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
11499 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
11500 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
11501 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
11502 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
11503 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
11504 0x2d02ef8d
11506 const unsigned char *end;
11508 crc = ~crc & 0xffffffff;
11509 for (end = buf + len; buf < end; ++ buf)
11510 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
11511 return ~crc & 0xffffffff;
11514 typedef bool (*check_func_type) (const char *, void *);
11515 typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
11517 static bool
11518 check_gnu_debuglink (const char * pathname, void * crc_pointer)
11520 static unsigned char buffer[8 * 1024];
11521 FILE *f;
11522 size_t count;
11523 unsigned long crc = 0;
11524 void *sep_data;
11526 sep_data = open_debug_file (pathname);
11527 if (sep_data == NULL)
11528 return false;
11530 /* Yes - we are opening the file twice... */
11531 f = fopen (pathname, "rb");
11532 if (f == NULL)
11534 /* Paranoia: This should never happen. */
11535 close_debug_file (sep_data);
11536 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
11537 return false;
11540 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
11541 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
11543 fclose (f);
11545 if (crc != * (unsigned long *) crc_pointer)
11547 close_debug_file (sep_data);
11548 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
11549 pathname);
11550 return false;
11553 return true;
11556 static const char *
11557 parse_gnu_debuglink (struct dwarf_section * section, void * data)
11559 const char * name;
11560 unsigned int crc_offset;
11561 unsigned long * crc32 = (unsigned long *) data;
11563 /* The name is first.
11564 The CRC value is stored after the filename, aligned up to 4 bytes. */
11565 name = (const char *) section->start;
11567 crc_offset = strnlen (name, section->size) + 1;
11568 if (crc_offset == 1)
11569 return NULL;
11570 crc_offset = (crc_offset + 3) & ~3;
11571 if (crc_offset + 4 > section->size)
11572 return NULL;
11574 * crc32 = byte_get (section->start + crc_offset, 4);
11575 return name;
11578 static bool
11579 check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
11581 void * sep_data = open_debug_file (filename);
11583 if (sep_data == NULL)
11584 return false;
11586 /* FIXME: We should now extract the build-id in the separate file
11587 and check it... */
11589 return true;
11592 typedef struct build_id_data
11594 size_t len;
11595 const unsigned char *data;
11596 } Build_id_data;
11598 static const char *
11599 parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
11601 const char *name;
11602 size_t namelen;
11603 size_t id_len;
11604 Build_id_data *build_id_data;
11606 /* The name is first.
11607 The build-id follows immediately, with no padding, up to the section's end. */
11609 name = (const char *) section->start;
11610 namelen = strnlen (name, section->size) + 1;
11611 if (namelen == 1)
11612 return NULL;
11613 if (namelen >= section->size)
11614 return NULL;
11616 id_len = section->size - namelen;
11617 if (id_len < 0x14)
11618 return NULL;
11620 build_id_data = (Build_id_data *) data;
11621 build_id_data->len = id_len;
11622 build_id_data->data = section->start + namelen;
11624 return name;
11627 static void
11628 add_separate_debug_file (const char * filename, void * handle)
11630 separate_info * i = xmalloc (sizeof * i);
11632 i->filename = filename;
11633 i->handle = handle;
11634 i->next = first_separate_info;
11635 first_separate_info = i;
11638 #if HAVE_LIBDEBUGINFOD
11639 /* Query debuginfod servers for the target debuglink or debugaltlink
11640 file. If successful, store the path of the file in filename and
11641 return TRUE, otherwise return FALSE. */
11643 static bool
11644 debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
11645 char ** filename,
11646 void * file)
11648 size_t build_id_len;
11649 unsigned char * build_id;
11651 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
11653 /* Get the build-id of file. */
11654 build_id = get_build_id (file);
11655 build_id_len = 0;
11657 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
11659 /* Get the build-id of the debugaltlink file. */
11660 unsigned int filelen;
11662 filelen = strnlen ((const char *)section->start, section->size);
11663 if (filelen == section->size)
11664 /* Corrupt debugaltlink. */
11665 return false;
11667 build_id = section->start + filelen + 1;
11668 build_id_len = section->size - (filelen + 1);
11670 if (build_id_len == 0)
11671 return false;
11673 else
11674 return false;
11676 if (build_id)
11678 int fd;
11679 debuginfod_client * client;
11681 client = debuginfod_begin ();
11682 if (client == NULL)
11683 return false;
11685 /* Query debuginfod servers for the target file. If found its path
11686 will be stored in filename. */
11687 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11688 debuginfod_end (client);
11690 /* Only free build_id if we allocated space for a hex string
11691 in get_build_id (). */
11692 if (build_id_len == 0)
11693 free (build_id);
11695 if (fd >= 0)
11697 /* File successfully retrieved. Close fd since we want to
11698 use open_debug_file () on filename instead. */
11699 close (fd);
11700 return true;
11704 return false;
11706 #endif /* HAVE_LIBDEBUGINFOD */
11708 static void *
11709 load_separate_debug_info (const char * main_filename,
11710 struct dwarf_section * xlink,
11711 parse_func_type parse_func,
11712 check_func_type check_func,
11713 void * func_data,
11714 void * file ATTRIBUTE_UNUSED)
11716 const char * separate_filename;
11717 char * debug_filename;
11718 char * canon_dir;
11719 size_t canon_dirlen;
11720 size_t dirlen;
11721 char * canon_filename;
11722 char * canon_debug_filename;
11723 bool self;
11725 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
11727 warn (_("Corrupt debuglink section: %s\n"),
11728 xlink->name ? xlink->name : xlink->uncompressed_name);
11729 return NULL;
11732 /* Attempt to locate the separate file.
11733 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
11735 canon_filename = lrealpath (main_filename);
11736 canon_dir = xstrdup (canon_filename);
11738 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11739 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11740 break;
11741 canon_dir[canon_dirlen] = '\0';
11743 #ifndef DEBUGDIR
11744 #define DEBUGDIR "/lib/debug"
11745 #endif
11746 #ifndef EXTRA_DEBUG_ROOT1
11747 #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11748 #endif
11749 #ifndef EXTRA_DEBUG_ROOT2
11750 #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11751 #endif
11753 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11754 + canon_dirlen
11755 + strlen (".debug/")
11756 #ifdef EXTRA_DEBUG_ROOT1
11757 + strlen (EXTRA_DEBUG_ROOT1)
11758 #endif
11759 #ifdef EXTRA_DEBUG_ROOT2
11760 + strlen (EXTRA_DEBUG_ROOT2)
11761 #endif
11762 + strlen (separate_filename)
11763 + 1);
11764 if (debug_filename == NULL)
11766 warn (_("Out of memory"));
11767 free (canon_dir);
11768 free (canon_filename);
11769 return NULL;
11772 /* First try in the current directory. */
11773 sprintf (debug_filename, "%s", separate_filename);
11774 if (check_func (debug_filename, func_data))
11775 goto found;
11777 /* Then try in a subdirectory called .debug. */
11778 sprintf (debug_filename, ".debug/%s", separate_filename);
11779 if (check_func (debug_filename, func_data))
11780 goto found;
11782 /* Then try in the same directory as the original file. */
11783 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11784 if (check_func (debug_filename, func_data))
11785 goto found;
11787 /* And the .debug subdirectory of that directory. */
11788 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11789 if (check_func (debug_filename, func_data))
11790 goto found;
11792 #ifdef EXTRA_DEBUG_ROOT1
11793 /* Try the first extra debug file root. */
11794 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11795 if (check_func (debug_filename, func_data))
11796 goto found;
11798 /* Try the first extra debug file root. */
11799 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11800 if (check_func (debug_filename, func_data))
11801 goto found;
11802 #endif
11804 #ifdef EXTRA_DEBUG_ROOT2
11805 /* Try the second extra debug file root. */
11806 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11807 if (check_func (debug_filename, func_data))
11808 goto found;
11809 #endif
11811 /* Then try in the global debug_filename directory. */
11812 strcpy (debug_filename, DEBUGDIR);
11813 dirlen = strlen (DEBUGDIR) - 1;
11814 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
11815 strcat (debug_filename, "/");
11816 strcat (debug_filename, (const char *) separate_filename);
11818 if (check_func (debug_filename, func_data))
11819 goto found;
11821 #if HAVE_LIBDEBUGINFOD
11823 char * tmp_filename;
11825 if (use_debuginfod
11826 && debuginfod_fetch_separate_debug_info (xlink,
11827 & tmp_filename,
11828 file))
11830 /* File successfully downloaded from server, replace
11831 debug_filename with the file's path. */
11832 free (debug_filename);
11833 debug_filename = tmp_filename;
11834 goto found;
11837 #endif
11839 if (do_debug_links)
11841 /* Failed to find the file. */
11842 warn (_("could not find separate debug file '%s'\n"),
11843 separate_filename);
11844 warn (_("tried: %s\n"), debug_filename);
11846 #ifdef EXTRA_DEBUG_ROOT2
11847 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11848 separate_filename);
11849 warn (_("tried: %s\n"), debug_filename);
11850 #endif
11852 #ifdef EXTRA_DEBUG_ROOT1
11853 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11854 canon_dir, separate_filename);
11855 warn (_("tried: %s\n"), debug_filename);
11857 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11858 separate_filename);
11859 warn (_("tried: %s\n"), debug_filename);
11860 #endif
11862 sprintf (debug_filename, "%s.debug/%s", canon_dir,
11863 separate_filename);
11864 warn (_("tried: %s\n"), debug_filename);
11866 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11867 warn (_("tried: %s\n"), debug_filename);
11869 sprintf (debug_filename, ".debug/%s", separate_filename);
11870 warn (_("tried: %s\n"), debug_filename);
11872 sprintf (debug_filename, "%s", separate_filename);
11873 warn (_("tried: %s\n"), debug_filename);
11875 #if HAVE_LIBDEBUGINFOD
11876 if (use_debuginfod)
11878 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11880 if (urls == NULL)
11881 urls = "";
11883 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11885 #endif
11888 free (canon_dir);
11889 free (debug_filename);
11890 free (canon_filename);
11891 return NULL;
11893 found:
11894 free (canon_dir);
11896 canon_debug_filename = lrealpath (debug_filename);
11897 self = strcmp (canon_debug_filename, canon_filename) == 0;
11898 free (canon_filename);
11899 free (canon_debug_filename);
11900 if (self)
11902 free (debug_filename);
11903 return NULL;
11906 void * debug_handle;
11908 /* Now open the file.... */
11909 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
11911 warn (_("failed to open separate debug file: %s\n"), debug_filename);
11912 free (debug_filename);
11913 return NULL;
11916 /* FIXME: We do not check to see if there are any other separate debug info
11917 files that would also match. */
11919 if (do_debug_links)
11920 printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
11921 add_separate_debug_file (debug_filename, debug_handle);
11923 /* Do not free debug_filename - it might be referenced inside
11924 the structure returned by open_debug_file(). */
11925 return debug_handle;
11928 /* Attempt to load a separate dwarf object file. */
11930 static void *
11931 load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
11933 char * separate_filename;
11934 void * separate_handle;
11936 if (IS_ABSOLUTE_PATH (name))
11937 separate_filename = strdup (name);
11938 else
11939 /* FIXME: Skip adding / if dwo_dir ends in /. */
11940 separate_filename = concat (dir, "/", name, NULL);
11941 if (separate_filename == NULL)
11943 warn (_("Out of memory allocating dwo filename\n"));
11944 return NULL;
11947 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
11949 warn (_("Unable to load dwo file: %s\n"), separate_filename);
11950 free (separate_filename);
11951 return NULL;
11954 /* FIXME: We should check the dwo_id. */
11956 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
11958 add_separate_debug_file (separate_filename, separate_handle);
11959 /* Note - separate_filename will be freed in free_debug_memory(). */
11960 return separate_handle;
11963 static void *
11964 try_build_id_prefix (const char * prefix, char * filename, const unsigned char * data, unsigned long id_len)
11966 char * f = filename;
11968 f += sprintf (f, "%s.build-id/%02x/", prefix, (unsigned) *data++);
11969 id_len --;
11970 while (id_len --)
11971 f += sprintf (f, "%02x", (unsigned) *data++);
11972 strcpy (f, ".debug");
11974 return open_debug_file (filename);
11977 /* Try to load a debug file based upon the build-id held in the .note.gnu.build-id section. */
11979 static void
11980 load_build_id_debug_file (const char * main_filename ATTRIBUTE_UNUSED, void * main_file)
11982 if (! load_debug_section (note_gnu_build_id, main_file))
11983 return; /* No .note.gnu.build-id section. */
11985 struct dwarf_section * section = & debug_displays [note_gnu_build_id].section;
11986 if (section == NULL)
11988 warn (_("Unable to load the .note.gnu.build-id section\n"));
11989 return;
11992 if (section->start == NULL || section->size < 0x18)
11994 warn (_(".note.gnu.build-id section is corrupt/empty\n"));
11995 return;
11998 /* In theory we should extract the contents of the section into
11999 a note structure and then check the fields. For now though
12000 just use hard coded offsets instead:
12002 Field Bytes Contents
12003 NSize 0...3 4
12004 DSize 4...7 8+
12005 Type 8..11 3 (NT_GNU_BUILD_ID)
12006 Name 12.15 GNU\0
12007 Data 16.... */
12009 /* FIXME: Check the name size, name and type fields. */
12011 unsigned long build_id_size;
12012 build_id_size = byte_get (section->start + 4, 4);
12013 if (build_id_size < 8)
12015 warn (_(".note.gnu.build-id data size is too small\n"));
12016 return;
12019 if (build_id_size > (section->size - 16))
12021 warn (_(".note.gnu.build-id data size is too big\n"));
12022 return;
12025 char * filename;
12026 filename = xmalloc (strlen (".build-id/")
12027 + build_id_size * 2 + 2
12028 + strlen (".debug")
12029 /* The next string should be the same as the longest
12030 name found in the prefixes[] array below. */
12031 + strlen ("/usrlib64/debug/usr")
12032 + 1);
12033 void * handle;
12035 static const char * prefixes[] =
12038 ".debug/",
12039 "/usr/lib/debug/",
12040 "/usr/lib/debug/usr/",
12041 "/usr/lib64/debug/",
12042 "/usr/lib64/debug/usr"
12044 long unsigned int i;
12046 for (i = 0; i < ARRAY_SIZE (prefixes); i++)
12048 handle = try_build_id_prefix (prefixes[i], filename,
12049 section->start + 16, build_id_size);
12050 if (handle != NULL)
12051 break;
12053 /* FIXME: TYhe BFD library also tries a global debugfile directory prefix. */
12054 if (handle == NULL)
12056 /* Failed to find a debug file associated with the build-id.
12057 This is not an error however, rather it just means that
12058 the debug info has probably not been loaded on the system,
12059 or that another method is being used to link to the debug
12060 info. */
12061 free (filename);
12062 return;
12065 add_separate_debug_file (filename, handle);
12068 /* Try to load a debug file pointed to by the .debug_sup section. */
12070 static void
12071 load_debug_sup_file (const char * main_filename, void * file)
12073 if (! load_debug_section (debug_sup, file))
12074 return; /* No .debug_sup section. */
12076 struct dwarf_section * section;
12077 section = & debug_displays [debug_sup].section;
12078 assert (section != NULL);
12080 if (section->start == NULL || section->size < 5)
12082 warn (_(".debug_sup section is corrupt/empty\n"));
12083 return;
12086 if (section->start[2] != 0)
12087 return; /* This is a supplementary file. */
12089 const char * filename = (const char *) section->start + 3;
12090 if (strnlen (filename, section->size - 3) == section->size - 3)
12092 warn (_("filename in .debug_sup section is corrupt\n"));
12093 return;
12096 if (filename[0] != '/' && strchr (main_filename, '/'))
12098 char * new_name;
12099 int new_len;
12101 new_len = asprintf (& new_name, "%.*s/%s",
12102 (int) (strrchr (main_filename, '/') - main_filename),
12103 main_filename,
12104 filename);
12105 if (new_len < 3)
12107 warn (_("unable to construct path for supplementary debug file"));
12108 if (new_len > -1)
12109 free (new_name);
12110 return;
12112 filename = new_name;
12114 else
12116 /* PR 27796: Make sure that we pass a filename that can be free'd to
12117 add_separate_debug_file(). */
12118 filename = strdup (filename);
12119 if (filename == NULL)
12121 warn (_("out of memory constructing filename for .debug_sup link\n"));
12122 return;
12126 void * handle = open_debug_file (filename);
12127 if (handle == NULL)
12129 warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
12130 free ((void *) filename);
12131 return;
12134 printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
12136 /* FIXME: Compare the checksums, if present. */
12137 add_separate_debug_file (filename, handle);
12140 /* Load a debuglink section and/or a debugaltlink section, if either are present.
12141 Recursively check the loaded files for more of these sections.
12142 Also follow any links in .debug_sup sections.
12143 FIXME: Should also check for DWO_* entries in the newly loaded files. */
12145 static void
12146 check_for_and_load_links (void * file, const char * filename)
12148 void * handle = NULL;
12150 if (load_debug_section (gnu_debugaltlink, file))
12152 Build_id_data build_id_data;
12154 handle = load_separate_debug_info (filename,
12155 & debug_displays[gnu_debugaltlink].section,
12156 parse_gnu_debugaltlink,
12157 check_gnu_debugaltlink,
12158 & build_id_data,
12159 file);
12160 if (handle)
12162 assert (handle == first_separate_info->handle);
12163 check_for_and_load_links (first_separate_info->handle,
12164 first_separate_info->filename);
12168 if (load_debug_section (gnu_debuglink, file))
12170 unsigned long crc32;
12172 handle = load_separate_debug_info (filename,
12173 & debug_displays[gnu_debuglink].section,
12174 parse_gnu_debuglink,
12175 check_gnu_debuglink,
12176 & crc32,
12177 file);
12178 if (handle)
12180 assert (handle == first_separate_info->handle);
12181 check_for_and_load_links (first_separate_info->handle,
12182 first_separate_info->filename);
12186 load_debug_sup_file (filename, file);
12188 load_build_id_debug_file (filename, file);
12191 /* Load the separate debug info file(s) attached to FILE, if any exist.
12192 Returns TRUE if any were found, FALSE otherwise.
12193 If TRUE is returned then the linked list starting at first_separate_info
12194 will be populated with open file handles. */
12196 bool
12197 load_separate_debug_files (void * file, const char * filename)
12199 /* Skip this operation if we are not interested in debug links. */
12200 if (! do_follow_links && ! do_debug_links)
12201 return false;
12203 /* See if there are any dwo links. */
12204 if (load_debug_section (str, file)
12205 && load_debug_section (abbrev, file)
12206 && load_debug_section (info, file))
12208 /* Load the .debug_addr section, if it exists. */
12209 load_debug_section (debug_addr, file);
12210 /* Load the .debug_str_offsets section, if it exists. */
12211 load_debug_section (str_index, file);
12212 /* Load the .debug_loclists section, if it exists. */
12213 load_debug_section (loclists, file);
12214 /* Load the .debug_rnglists section, if it exists. */
12215 load_debug_section (rnglists, file);
12217 free_dwo_info ();
12219 if (process_debug_info (& debug_displays[info].section, file, abbrev,
12220 true, false))
12222 bool introduced = false;
12223 dwo_info *dwinfo;
12224 const char *dir = NULL;
12225 const char *id = NULL;
12226 const char *name = NULL;
12228 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
12230 /* Accumulate NAME, DIR and ID fields. */
12231 switch (dwinfo->type)
12233 case DWO_NAME:
12234 if (name != NULL)
12235 warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
12236 name = dwinfo->value;
12237 break;
12239 case DWO_DIR:
12240 /* There can be multiple DW_AT_comp_dir entries in a CU,
12241 so do not complain. */
12242 dir = dwinfo->value;
12243 break;
12245 case DWO_ID:
12246 if (id != NULL)
12247 warn (_("multiple DWO_IDs encountered for the same CU\n"));
12248 id = dwinfo->value;
12249 break;
12251 default:
12252 error (_("Unexpected DWO INFO type"));
12253 break;
12256 /* If we have reached the end of our list, or we are changing
12257 CUs, then display the information that we have accumulated
12258 so far. */
12259 if (name != NULL
12260 && (dwinfo->next == NULL
12261 || dwinfo->next->cu_offset != dwinfo->cu_offset))
12263 if (do_debug_links)
12265 if (! introduced)
12267 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
12268 debug_displays [info].section.uncompressed_name);
12269 introduced = true;
12272 printf (_(" Name: %s\n"), name);
12273 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
12274 if (id != NULL)
12275 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
12276 else if (debug_information[0].dwarf_version != 5)
12277 printf (_(" ID: <not specified>\n"));
12278 printf ("\n\n");
12281 if (do_follow_links)
12282 load_dwo_file (filename, name, dir, id);
12284 name = dir = id = NULL;
12290 if (! do_follow_links)
12291 /* The other debug links will be displayed by display_debug_links()
12292 so we do not need to do any further processing here. */
12293 return false;
12295 /* FIXME: We do not check for the presence of both link sections in the same file. */
12296 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
12297 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
12299 check_for_and_load_links (file, filename);
12300 if (first_separate_info != NULL)
12301 return true;
12303 do_follow_links = 0;
12304 return false;
12307 void
12308 free_debug_memory (void)
12310 unsigned int i;
12312 free_all_abbrevs ();
12314 free (shndx_pool);
12315 shndx_pool = NULL;
12316 shndx_pool_size = 0;
12317 shndx_pool_used = 0;
12318 free (cu_sets);
12319 cu_sets = NULL;
12320 cu_count = 0;
12321 free (tu_sets);
12322 tu_sets = NULL;
12323 tu_count = 0;
12325 memset (level_type_signed, 0, sizeof level_type_signed);
12326 cu_tu_indexes_read = -1;
12328 for (i = 0; i < max; i++)
12329 free_debug_section ((enum dwarf_section_display_enum) i);
12331 if (debug_information != NULL)
12333 for (i = 0; i < alloc_num_debug_info_entries; i++)
12334 free_debug_information (&debug_information[i]);
12335 free (debug_information);
12336 debug_information = NULL;
12337 alloc_num_debug_info_entries = num_debug_info_entries = 0;
12340 separate_info * d;
12341 separate_info * next;
12343 for (d = first_separate_info; d != NULL; d = next)
12345 close_debug_file (d->handle);
12346 free ((void *) d->filename);
12347 next = d->next;
12348 free ((void *) d);
12350 first_separate_info = NULL;
12352 free_dwo_info ();
12355 typedef struct
12357 const char letter;
12358 const char *option;
12359 int *variable;
12360 int val;
12361 } debug_dump_long_opts;
12363 static const debug_dump_long_opts debug_option_table[] =
12365 { 'A', "addr", &do_debug_addr, 1 },
12366 { 'a', "abbrev", &do_debug_abbrevs, 1 },
12367 { 'c', "cu_index", &do_debug_cu_index, 1 },
12368 #ifdef HAVE_LIBDEBUGINFOD
12369 { 'D', "use-debuginfod", &use_debuginfod, 1 },
12370 { 'E', "do-not-use-debuginfod", &use_debuginfod, 0 },
12371 #endif
12372 { 'F', "frames-interp", &do_debug_frames_interp, 1 },
12373 { 'f', "frames", &do_debug_frames, 1 },
12374 { 'g', "gdb_index", &do_gdb_index, 1 },
12375 { 'i', "info", &do_debug_info, 1 },
12376 { 'K', "follow-links", &do_follow_links, 1 },
12377 { 'k', "links", &do_debug_links, 1 },
12378 { 'L', "decodedline", &do_debug_lines, FLAG_DEBUG_LINES_DECODED },
12379 { 'l', "rawline", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12380 /* For compatibility with earlier versions of readelf. */
12381 { 'l', "line", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12382 { 'm', "macro", &do_debug_macinfo, 1 },
12383 { 'N', "no-follow-links", &do_follow_links, 0 },
12384 { 'O', "str-offsets", &do_debug_str_offsets, 1 },
12385 { 'o', "loc", &do_debug_loc, 1 },
12386 { 'p', "pubnames", &do_debug_pubnames, 1 },
12387 { 'R', "Ranges", &do_debug_ranges, 1 },
12388 { 'r', "aranges", &do_debug_aranges, 1 },
12389 /* For compatibility with earlier versions of readelf. */
12390 { 'r', "ranges", &do_debug_aranges, 1 },
12391 { 's', "str", &do_debug_str, 1 },
12392 { 'T', "trace_aranges", &do_trace_aranges, 1 },
12393 { 't', "pubtypes", &do_debug_pubtypes, 1 },
12394 { 'U', "trace_info", &do_trace_info, 1 },
12395 { 'u', "trace_abbrev", &do_trace_abbrevs, 1 },
12396 { 0, NULL, NULL, 0 }
12399 /* Enable display of specific DWARF sections as determined by the comma
12400 separated strings in NAMES. Returns non-zero if any displaying was
12401 enabled. */
12404 dwarf_select_sections_by_names (const char *names)
12406 const char *p;
12407 int result = 0;
12409 p = names;
12410 while (*p)
12412 const debug_dump_long_opts *entry;
12414 for (entry = debug_option_table; entry->option; entry++)
12416 size_t len = strlen (entry->option);
12418 if (strncmp (p, entry->option, len) == 0
12419 && (p[len] == ',' || p[len] == '\0'))
12421 if (entry->val == 0)
12422 * entry->variable = 0;
12423 else
12424 * entry->variable = entry->val;
12425 result |= entry->val;
12427 p += len;
12428 break;
12432 if (entry->option == NULL)
12434 warn (_("Unrecognized debug option '%s'\n"), p);
12435 p = strchr (p, ',');
12436 if (p == NULL)
12437 break;
12440 if (*p == ',')
12441 p++;
12444 /* The --debug-dump=frames-interp option also enables the
12445 --debug-dump=frames option. */
12446 if (do_debug_frames_interp)
12447 do_debug_frames = 1;
12449 return result;
12452 /* Enable display of specific DWARF sections as determined by the characters
12453 in LETTERS. Returns non-zero if any displaying was enabled. */
12456 dwarf_select_sections_by_letters (const char *letters)
12458 int result = 0;
12460 while (* letters)
12462 const debug_dump_long_opts *entry;
12464 for (entry = debug_option_table; entry->letter; entry++)
12466 if (entry->letter == * letters)
12468 if (entry->val == 0)
12469 * entry->variable = 0;
12470 else
12471 * entry->variable |= entry->val;
12472 result |= entry->val;
12473 break;
12477 if (entry->letter == 0)
12478 warn (_("Unrecognized debug letter option '%c'\n"), * letters);
12480 letters ++;
12483 /* The --debug-dump=frames-interp option also enables the
12484 --debug-dump=frames option. */
12485 if (do_debug_frames_interp)
12486 do_debug_frames = 1;
12488 return result;
12491 void
12492 dwarf_select_sections_all (void)
12494 do_debug_info = 1;
12495 do_debug_abbrevs = 1;
12496 do_debug_lines = FLAG_DEBUG_LINES_RAW;
12497 do_debug_pubnames = 1;
12498 do_debug_pubtypes = 1;
12499 do_debug_aranges = 1;
12500 do_debug_ranges = 1;
12501 do_debug_frames = 1;
12502 do_debug_macinfo = 1;
12503 do_debug_str = 1;
12504 do_debug_loc = 1;
12505 do_gdb_index = 1;
12506 do_trace_info = 1;
12507 do_trace_abbrevs = 1;
12508 do_trace_aranges = 1;
12509 do_debug_addr = 1;
12510 do_debug_cu_index = 1;
12511 do_follow_links = 1;
12512 do_debug_links = 1;
12513 do_debug_str_offsets = 1;
12516 #define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0
12517 #define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0
12519 /* N.B. The order here must match the order in section_display_enum. */
12521 struct dwarf_section_display debug_displays[] =
12523 { { ".debug_abbrev", ".zdebug_abbrev", ".dwabrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12524 { { ".debug_aranges", ".zdebug_aranges", ".dwarnge", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, true },
12525 { { ".debug_frame", ".zdebug_frame", ".dwframe", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12526 { { ".debug_info", ".zdebug_info", ".dwinfo", ABBREV (abbrev)}, display_debug_info, &do_debug_info, true },
12527 { { ".debug_line", ".zdebug_line", ".dwline", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12528 { { ".debug_pubnames", ".zdebug_pubnames", ".dwpbnms", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, false },
12529 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, false },
12530 { { ".eh_frame", "", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12531 { { ".debug_macinfo", ".zdebug_macinfo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12532 { { ".debug_macro", ".zdebug_macro", ".dwmac", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12533 { { ".debug_str", ".zdebug_str", ".dwstr", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12534 { { ".debug_line_str", ".zdebug_line_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12535 { { ".debug_loc", ".zdebug_loc", ".dwloc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12536 { { ".debug_loclists", ".zdebug_loclists", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12537 { { ".debug_loclists.dwo", ".zdebug_loclists.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12538 { { ".debug_pubtypes", ".zdebug_pubtypes", ".dwpbtyp", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, false },
12539 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, false },
12540 { { ".debug_ranges", ".zdebug_ranges", ".dwrnges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12541 { { ".debug_rnglists", ".zdebug_rnglists", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12542 { { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12543 { { ".debug_static_func", ".zdebug_static_func", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12544 { { ".debug_static_vars", ".zdebug_static_vars", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12545 { { ".debug_types", ".zdebug_types", "", ABBREV (abbrev) }, display_debug_types, &do_debug_info, true },
12546 { { ".debug_weaknames", ".zdebug_weaknames", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12547 { { ".gdb_index", "", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false },
12548 { { ".debug_names", "", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false },
12549 { { ".trace_info", "", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true },
12550 { { ".trace_abbrev", "", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false },
12551 { { ".trace_aranges", "", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false },
12552 { { ".debug_info.dwo", ".zdebug_info.dwo", "", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
12553 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", "", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12554 { { ".debug_types.dwo", ".zdebug_types.dwo", "", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, true },
12555 { { ".debug_line.dwo", ".zdebug_line.dwo", "", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12556 { { ".debug_loc.dwo", ".zdebug_loc.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12557 { { ".debug_macro.dwo", ".zdebug_macro.dwo", "", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12558 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12559 { { ".debug_str.dwo", ".zdebug_str.dwo", "", NO_ABBREVS }, display_debug_str, &do_debug_str, true },
12560 { { ".debug_str_offsets", ".zdebug_str_offsets", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12561 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12562 { { ".debug_addr", ".zdebug_addr", "", NO_ABBREVS }, display_debug_addr, &do_debug_addr, true },
12563 { { ".debug_cu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12564 { { ".debug_tu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12565 { { ".gnu_debuglink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12566 { { ".gnu_debugaltlink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12567 { { ".debug_sup", "", "", NO_ABBREVS }, display_debug_sup, &do_debug_links, false },
12568 /* Separate debug info files can containt their own .debug_str section,
12569 and this might be in *addition* to a .debug_str section already present
12570 in the main file. Hence we need to have two entries for .debug_str. */
12571 { { ".debug_str", ".zdebug_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12572 { { ".note.gnu.build-id", "", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12575 /* A static assertion. */
12576 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];