DIGEST: Documentation
[binutils-gdb.git] / binutils / dwarf.c
blob281a055a6d586181bbec7e5bed0b8bfacdb248ff
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 index_offset = idx * offset_size;
664 if (this_set != NULL)
665 index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
667 index_offset += str_offsets_base;
669 if (index_offset + offset_size > index_section->size)
671 warn (_("string index of %" PRIu64 " converts to an offset of %#" PRIx64
672 " which is too big for section %s"),
673 idx, index_offset, str_section->name);
675 return _("<string index too big>");
678 /* FIXME: If we are being paranoid then we should also check to see if
679 IDX references an entry beyond the end of the string table pointed to
680 by STR_OFFSETS_BASE. (Since there can be more than one string table
681 in a DWARF string section). */
683 str_offset = byte_get (index_section->start + index_offset, offset_size);
685 str_offset -= str_section->address;
686 if (str_offset >= str_section->size)
688 warn (_("indirect offset too big: %#" PRIx64 "\n"), str_offset);
689 return _("<indirect index offset is too big>");
692 ret = (const char *) str_section->start + str_offset;
694 /* Unfortunately we cannot rely upon str_section ending with a NUL byte.
695 Since our caller is expecting to receive a well formed C string we test
696 for the lack of a terminating byte here. */
697 if (strnlen (ret, str_section->size - str_offset)
698 == str_section->size - str_offset)
699 return _("<no NUL byte at end of section>");
701 return ret;
704 static uint64_t
705 fetch_indexed_addr (uint64_t offset, uint32_t num_bytes)
707 struct dwarf_section *section = &debug_displays [debug_addr].section;
709 if (section->start == NULL)
711 warn (_("Cannot fetch indexed address: the .debug_addr section is missing\n"));
712 return 0;
715 if (offset + num_bytes > section->size)
717 warn (_("Offset into section %s too big: %#" PRIx64 "\n"),
718 section->name, offset);
719 return 0;
722 return byte_get (section->start + offset, num_bytes);
725 /* Fetch a value from a debug section that has been indexed by
726 something in another section (eg DW_FORM_loclistx or DW_FORM_rnglistx).
727 Returns -1 if the value could not be found. */
729 static uint64_t
730 fetch_indexed_value (uint64_t idx,
731 enum dwarf_section_display_enum sec_enum,
732 uint64_t base_address)
734 struct dwarf_section *section = &debug_displays [sec_enum].section;
736 if (section->start == NULL)
738 warn (_("Unable to locate %s section\n"), section->uncompressed_name);
739 return -1;
742 if (section->size < 4)
744 warn (_("Section %s is too small to contain an value indexed from another section!\n"),
745 section->name);
746 return -1;
749 uint32_t pointer_size, bias;
751 if (byte_get (section->start, 4) == 0xffffffff)
753 pointer_size = 8;
754 bias = 20;
756 else
758 pointer_size = 4;
759 bias = 12;
762 uint64_t offset = idx * pointer_size;
764 /* Offsets are biased by the size of the section header
765 or base address. */
766 if (base_address)
767 offset += base_address;
768 else
769 offset += bias;
771 if (offset + pointer_size > section->size)
773 warn (_("Offset into section %s too big: %#" PRIx64 "\n"),
774 section->name, offset);
775 return -1;
778 return byte_get (section->start + offset, pointer_size);
781 /* FIXME: There are better and more efficient ways to handle
782 these structures. For now though, I just want something that
783 is simple to implement. */
784 /* Records a single attribute in an abbrev. */
785 typedef struct abbrev_attr
787 unsigned long attribute;
788 unsigned long form;
789 int64_t implicit_const;
790 struct abbrev_attr *next;
792 abbrev_attr;
794 /* Records a single abbrev. */
795 typedef struct abbrev_entry
797 unsigned long number;
798 unsigned long tag;
799 int children;
800 struct abbrev_attr * first_attr;
801 struct abbrev_attr * last_attr;
802 struct abbrev_entry * next;
804 abbrev_entry;
806 /* Records a set of abbreviations. */
807 typedef struct abbrev_list
809 abbrev_entry * first_abbrev;
810 abbrev_entry * last_abbrev;
811 unsigned char * raw;
812 struct abbrev_list * next;
813 unsigned char * start_of_next_abbrevs;
815 abbrev_list;
817 /* Records all the abbrevs found so far. */
818 static struct abbrev_list * abbrev_lists = NULL;
820 typedef struct abbrev_map
822 uint64_t start;
823 uint64_t end;
824 abbrev_list *list;
825 } abbrev_map;
827 /* Maps between CU offsets and abbrev sets. */
828 static abbrev_map * cu_abbrev_map = NULL;
829 static unsigned long num_abbrev_map_entries = 0;
830 static unsigned long next_free_abbrev_map_entry = 0;
832 #define INITIAL_NUM_ABBREV_MAP_ENTRIES 8
833 #define ABBREV_MAP_ENTRIES_INCREMENT 8
835 static void
836 record_abbrev_list_for_cu (uint64_t start, uint64_t end,
837 abbrev_list *list, abbrev_list *free_list)
839 if (free_list != NULL)
841 list->next = abbrev_lists;
842 abbrev_lists = list;
845 if (cu_abbrev_map == NULL)
847 num_abbrev_map_entries = INITIAL_NUM_ABBREV_MAP_ENTRIES;
848 cu_abbrev_map = xmalloc (num_abbrev_map_entries * sizeof (* cu_abbrev_map));
850 else if (next_free_abbrev_map_entry == num_abbrev_map_entries)
852 num_abbrev_map_entries += ABBREV_MAP_ENTRIES_INCREMENT;
853 cu_abbrev_map = xrealloc (cu_abbrev_map, num_abbrev_map_entries * sizeof (* cu_abbrev_map));
856 cu_abbrev_map[next_free_abbrev_map_entry].start = start;
857 cu_abbrev_map[next_free_abbrev_map_entry].end = end;
858 cu_abbrev_map[next_free_abbrev_map_entry].list = list;
859 next_free_abbrev_map_entry ++;
862 static abbrev_list *
863 free_abbrev_list (abbrev_list *list)
865 abbrev_entry *abbrv = list->first_abbrev;
867 while (abbrv)
869 abbrev_attr *attr = abbrv->first_attr;
871 while (attr)
873 abbrev_attr *next_attr = attr->next;
874 free (attr);
875 attr = next_attr;
878 abbrev_entry *next_abbrev = abbrv->next;
879 free (abbrv);
880 abbrv = next_abbrev;
883 abbrev_list *next = list->next;
884 free (list);
885 return next;
888 static void
889 free_all_abbrevs (void)
891 while (abbrev_lists)
892 abbrev_lists = free_abbrev_list (abbrev_lists);
894 free (cu_abbrev_map);
895 cu_abbrev_map = NULL;
896 next_free_abbrev_map_entry = 0;
899 static abbrev_list *
900 find_abbrev_list_by_raw_abbrev (unsigned char *raw)
902 abbrev_list * list;
904 for (list = abbrev_lists; list != NULL; list = list->next)
905 if (list->raw == raw)
906 return list;
908 return NULL;
911 /* Find the abbreviation map for the CU that includes OFFSET.
912 OFFSET is an absolute offset from the start of the .debug_info section. */
913 /* FIXME: This function is going to slow down readelf & objdump.
914 Not caching abbrevs is likely the answer. */
916 static abbrev_map *
917 find_abbrev_map_by_offset (uint64_t offset)
919 unsigned long i;
921 for (i = 0; i < next_free_abbrev_map_entry; i++)
922 if (cu_abbrev_map[i].start <= offset
923 && cu_abbrev_map[i].end > offset)
924 return cu_abbrev_map + i;
926 return NULL;
929 static void
930 add_abbrev (unsigned long number,
931 unsigned long tag,
932 int children,
933 abbrev_list * list)
935 abbrev_entry * entry;
937 entry = (abbrev_entry *) xmalloc (sizeof (*entry));
939 entry->number = number;
940 entry->tag = tag;
941 entry->children = children;
942 entry->first_attr = NULL;
943 entry->last_attr = NULL;
944 entry->next = NULL;
946 assert (list != NULL);
948 if (list->first_abbrev == NULL)
949 list->first_abbrev = entry;
950 else
951 list->last_abbrev->next = entry;
953 list->last_abbrev = entry;
956 static void
957 add_abbrev_attr (unsigned long attribute,
958 unsigned long form,
959 int64_t implicit_const,
960 abbrev_list *list)
962 abbrev_attr *attr;
964 attr = (abbrev_attr *) xmalloc (sizeof (*attr));
966 attr->attribute = attribute;
967 attr->form = form;
968 attr->implicit_const = implicit_const;
969 attr->next = NULL;
971 assert (list != NULL && list->last_abbrev != NULL);
973 if (list->last_abbrev->first_attr == NULL)
974 list->last_abbrev->first_attr = attr;
975 else
976 list->last_abbrev->last_attr->next = attr;
978 list->last_abbrev->last_attr = attr;
981 /* Return processed (partial) contents of a .debug_abbrev section.
982 Returns NULL on errors. */
984 static abbrev_list *
985 process_abbrev_set (struct dwarf_section *section,
986 unsigned char *start,
987 unsigned char *end)
989 abbrev_list *list = xmalloc (sizeof (*list));
990 list->first_abbrev = NULL;
991 list->last_abbrev = NULL;
992 list->raw = start;
994 while (start < end)
996 unsigned long entry;
997 unsigned long tag;
998 unsigned long attribute;
999 int children;
1001 READ_ULEB (entry, start, end);
1003 /* A single zero is supposed to end the set according
1004 to the standard. If there's more, then signal that to
1005 the caller. */
1006 if (start == end || entry == 0)
1008 list->next = NULL;
1009 list->start_of_next_abbrevs = start != end ? start : NULL;
1010 return list;
1013 READ_ULEB (tag, start, end);
1014 if (start == end)
1016 free (list);
1017 return NULL;
1020 children = *start++;
1022 add_abbrev (entry, tag, children, list);
1026 unsigned long form;
1027 /* Initialize it due to a false compiler warning. */
1028 int64_t implicit_const = -1;
1030 READ_ULEB (attribute, start, end);
1031 if (start == end)
1032 break;
1034 READ_ULEB (form, start, end);
1035 if (start == end)
1036 break;
1038 if (form == DW_FORM_implicit_const)
1040 READ_SLEB (implicit_const, start, end);
1041 if (start == end)
1042 break;
1045 add_abbrev_attr (attribute, form, implicit_const, list);
1047 while (attribute != 0);
1050 /* Report the missing single zero which ends the section. */
1051 error (_("%s section not zero terminated\n"), section->name);
1053 free (list);
1054 return NULL;
1057 /* Return a sequence of abbrevs in SECTION starting at ABBREV_BASE
1058 plus ABBREV_OFFSET and finishing at ABBREV_BASE + ABBREV_SIZE.
1059 If FREE_LIST is non-NULL search the already decoded abbrevs on
1060 abbrev_lists first and if found set *FREE_LIST to NULL. If
1061 searching doesn't find a matching abbrev, set *FREE_LIST to the
1062 newly allocated list. If FREE_LIST is NULL, no search is done and
1063 the returned abbrev_list is always newly allocated. */
1065 static abbrev_list *
1066 find_and_process_abbrev_set (struct dwarf_section *section,
1067 uint64_t abbrev_base,
1068 uint64_t abbrev_size,
1069 uint64_t abbrev_offset,
1070 abbrev_list **free_list)
1072 if (free_list)
1073 *free_list = NULL;
1075 if (abbrev_base >= section->size
1076 || abbrev_size > section->size - abbrev_base)
1078 /* PR 17531: file:4bcd9ce9. */
1079 warn (_("Debug info is corrupted, abbrev size (%#" PRIx64 ")"
1080 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1081 abbrev_base + abbrev_size, section->size);
1082 return NULL;
1084 if (abbrev_offset >= abbrev_size)
1086 warn (_("Debug info is corrupted, abbrev offset (%#" PRIx64 ")"
1087 " is larger than abbrev section size (%#" PRIx64 ")\n"),
1088 abbrev_offset, abbrev_size);
1089 return NULL;
1092 unsigned char *start = section->start + abbrev_base + abbrev_offset;
1093 unsigned char *end = section->start + abbrev_base + abbrev_size;
1094 abbrev_list *list = NULL;
1095 if (free_list)
1096 list = find_abbrev_list_by_raw_abbrev (start);
1097 if (list == NULL)
1099 list = process_abbrev_set (section, start, end);
1100 if (free_list)
1101 *free_list = list;
1103 return list;
1106 static const char *
1107 get_TAG_name (uint64_t tag)
1109 const char *name = NULL;
1111 if ((unsigned int) tag == tag)
1112 name = get_DW_TAG_name ((unsigned int) tag);
1113 if (name == NULL)
1115 static char buffer[100];
1117 if (tag >= DW_TAG_lo_user && tag <= DW_TAG_hi_user)
1118 snprintf (buffer, sizeof (buffer),
1119 _("User TAG value: %#" PRIx64), tag);
1120 else
1121 snprintf (buffer, sizeof (buffer),
1122 _("Unknown TAG value: %#" PRIx64), tag);
1123 return buffer;
1126 return name;
1129 static const char *
1130 get_FORM_name (unsigned long form)
1132 const char *name = NULL;
1134 if (form == 0)
1135 return "DW_FORM value: 0";
1137 if ((unsigned int) form == form)
1138 name = get_DW_FORM_name ((unsigned int) form);
1139 if (name == NULL)
1141 static char buffer[100];
1143 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
1144 return buffer;
1147 return name;
1150 static const char *
1151 get_IDX_name (unsigned long idx)
1153 const char *name = NULL;
1155 if ((unsigned int) idx == idx)
1156 name = get_DW_IDX_name ((unsigned int) idx);
1157 if (name == NULL)
1159 static char buffer[100];
1161 snprintf (buffer, sizeof (buffer), _("Unknown IDX value: %lx"), idx);
1162 return buffer;
1165 return name;
1168 static unsigned char *
1169 display_block (unsigned char *data,
1170 uint64_t length,
1171 const unsigned char * const end, char delimiter)
1173 size_t maxlen;
1175 printf (_("%c%" PRIu64 " byte block: "), delimiter, length);
1176 if (data > end)
1177 return (unsigned char *) end;
1179 maxlen = end - data;
1180 length = length > maxlen ? maxlen : length;
1182 while (length --)
1183 printf ("%" PRIx64 " ", byte_get (data++, 1));
1185 return data;
1188 static int
1189 decode_location_expression (unsigned char * data,
1190 unsigned int pointer_size,
1191 unsigned int offset_size,
1192 int dwarf_version,
1193 uint64_t length,
1194 uint64_t cu_offset,
1195 struct dwarf_section * section)
1197 unsigned op;
1198 uint64_t uvalue;
1199 int64_t svalue;
1200 unsigned char *end = data + length;
1201 int need_frame_base = 0;
1203 while (data < end)
1205 op = *data++;
1207 switch (op)
1209 case DW_OP_addr:
1210 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1211 printf ("DW_OP_addr: %" PRIx64, uvalue);
1212 break;
1213 case DW_OP_deref:
1214 printf ("DW_OP_deref");
1215 break;
1216 case DW_OP_const1u:
1217 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1218 printf ("DW_OP_const1u: %" PRIu64, uvalue);
1219 break;
1220 case DW_OP_const1s:
1221 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
1222 printf ("DW_OP_const1s: %" PRId64, svalue);
1223 break;
1224 case DW_OP_const2u:
1225 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1226 printf ("DW_OP_const2u: %" PRIu64, uvalue);
1227 break;
1228 case DW_OP_const2s:
1229 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1230 printf ("DW_OP_const2s: %" PRId64, svalue);
1231 break;
1232 case DW_OP_const4u:
1233 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1234 printf ("DW_OP_const4u: %" PRIu64, uvalue);
1235 break;
1236 case DW_OP_const4s:
1237 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1238 printf ("DW_OP_const4s: %" PRId64, svalue);
1239 break;
1240 case DW_OP_const8u:
1241 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
1242 printf ("DW_OP_const8u: %" PRIu64, uvalue);
1243 break;
1244 case DW_OP_const8s:
1245 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 8, end);
1246 printf ("DW_OP_const8s: %" PRId64, svalue);
1247 break;
1248 case DW_OP_constu:
1249 READ_ULEB (uvalue, data, end);
1250 printf ("DW_OP_constu: %" PRIu64, uvalue);
1251 break;
1252 case DW_OP_consts:
1253 READ_SLEB (svalue, data, end);
1254 printf ("DW_OP_consts: %" PRId64, svalue);
1255 break;
1256 case DW_OP_dup:
1257 printf ("DW_OP_dup");
1258 break;
1259 case DW_OP_drop:
1260 printf ("DW_OP_drop");
1261 break;
1262 case DW_OP_over:
1263 printf ("DW_OP_over");
1264 break;
1265 case DW_OP_pick:
1266 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1267 printf ("DW_OP_pick: %" PRIu64, uvalue);
1268 break;
1269 case DW_OP_swap:
1270 printf ("DW_OP_swap");
1271 break;
1272 case DW_OP_rot:
1273 printf ("DW_OP_rot");
1274 break;
1275 case DW_OP_xderef:
1276 printf ("DW_OP_xderef");
1277 break;
1278 case DW_OP_abs:
1279 printf ("DW_OP_abs");
1280 break;
1281 case DW_OP_and:
1282 printf ("DW_OP_and");
1283 break;
1284 case DW_OP_div:
1285 printf ("DW_OP_div");
1286 break;
1287 case DW_OP_minus:
1288 printf ("DW_OP_minus");
1289 break;
1290 case DW_OP_mod:
1291 printf ("DW_OP_mod");
1292 break;
1293 case DW_OP_mul:
1294 printf ("DW_OP_mul");
1295 break;
1296 case DW_OP_neg:
1297 printf ("DW_OP_neg");
1298 break;
1299 case DW_OP_not:
1300 printf ("DW_OP_not");
1301 break;
1302 case DW_OP_or:
1303 printf ("DW_OP_or");
1304 break;
1305 case DW_OP_plus:
1306 printf ("DW_OP_plus");
1307 break;
1308 case DW_OP_plus_uconst:
1309 READ_ULEB (uvalue, data, end);
1310 printf ("DW_OP_plus_uconst: %" PRIu64, uvalue);
1311 break;
1312 case DW_OP_shl:
1313 printf ("DW_OP_shl");
1314 break;
1315 case DW_OP_shr:
1316 printf ("DW_OP_shr");
1317 break;
1318 case DW_OP_shra:
1319 printf ("DW_OP_shra");
1320 break;
1321 case DW_OP_xor:
1322 printf ("DW_OP_xor");
1323 break;
1324 case DW_OP_bra:
1325 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1326 printf ("DW_OP_bra: %" PRId64, svalue);
1327 break;
1328 case DW_OP_eq:
1329 printf ("DW_OP_eq");
1330 break;
1331 case DW_OP_ge:
1332 printf ("DW_OP_ge");
1333 break;
1334 case DW_OP_gt:
1335 printf ("DW_OP_gt");
1336 break;
1337 case DW_OP_le:
1338 printf ("DW_OP_le");
1339 break;
1340 case DW_OP_lt:
1341 printf ("DW_OP_lt");
1342 break;
1343 case DW_OP_ne:
1344 printf ("DW_OP_ne");
1345 break;
1346 case DW_OP_skip:
1347 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1348 printf ("DW_OP_skip: %" PRId64, svalue);
1349 break;
1351 case DW_OP_lit0:
1352 case DW_OP_lit1:
1353 case DW_OP_lit2:
1354 case DW_OP_lit3:
1355 case DW_OP_lit4:
1356 case DW_OP_lit5:
1357 case DW_OP_lit6:
1358 case DW_OP_lit7:
1359 case DW_OP_lit8:
1360 case DW_OP_lit9:
1361 case DW_OP_lit10:
1362 case DW_OP_lit11:
1363 case DW_OP_lit12:
1364 case DW_OP_lit13:
1365 case DW_OP_lit14:
1366 case DW_OP_lit15:
1367 case DW_OP_lit16:
1368 case DW_OP_lit17:
1369 case DW_OP_lit18:
1370 case DW_OP_lit19:
1371 case DW_OP_lit20:
1372 case DW_OP_lit21:
1373 case DW_OP_lit22:
1374 case DW_OP_lit23:
1375 case DW_OP_lit24:
1376 case DW_OP_lit25:
1377 case DW_OP_lit26:
1378 case DW_OP_lit27:
1379 case DW_OP_lit28:
1380 case DW_OP_lit29:
1381 case DW_OP_lit30:
1382 case DW_OP_lit31:
1383 printf ("DW_OP_lit%d", op - DW_OP_lit0);
1384 break;
1386 case DW_OP_reg0:
1387 case DW_OP_reg1:
1388 case DW_OP_reg2:
1389 case DW_OP_reg3:
1390 case DW_OP_reg4:
1391 case DW_OP_reg5:
1392 case DW_OP_reg6:
1393 case DW_OP_reg7:
1394 case DW_OP_reg8:
1395 case DW_OP_reg9:
1396 case DW_OP_reg10:
1397 case DW_OP_reg11:
1398 case DW_OP_reg12:
1399 case DW_OP_reg13:
1400 case DW_OP_reg14:
1401 case DW_OP_reg15:
1402 case DW_OP_reg16:
1403 case DW_OP_reg17:
1404 case DW_OP_reg18:
1405 case DW_OP_reg19:
1406 case DW_OP_reg20:
1407 case DW_OP_reg21:
1408 case DW_OP_reg22:
1409 case DW_OP_reg23:
1410 case DW_OP_reg24:
1411 case DW_OP_reg25:
1412 case DW_OP_reg26:
1413 case DW_OP_reg27:
1414 case DW_OP_reg28:
1415 case DW_OP_reg29:
1416 case DW_OP_reg30:
1417 case DW_OP_reg31:
1418 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1419 regname (op - DW_OP_reg0, 1));
1420 break;
1422 case DW_OP_breg0:
1423 case DW_OP_breg1:
1424 case DW_OP_breg2:
1425 case DW_OP_breg3:
1426 case DW_OP_breg4:
1427 case DW_OP_breg5:
1428 case DW_OP_breg6:
1429 case DW_OP_breg7:
1430 case DW_OP_breg8:
1431 case DW_OP_breg9:
1432 case DW_OP_breg10:
1433 case DW_OP_breg11:
1434 case DW_OP_breg12:
1435 case DW_OP_breg13:
1436 case DW_OP_breg14:
1437 case DW_OP_breg15:
1438 case DW_OP_breg16:
1439 case DW_OP_breg17:
1440 case DW_OP_breg18:
1441 case DW_OP_breg19:
1442 case DW_OP_breg20:
1443 case DW_OP_breg21:
1444 case DW_OP_breg22:
1445 case DW_OP_breg23:
1446 case DW_OP_breg24:
1447 case DW_OP_breg25:
1448 case DW_OP_breg26:
1449 case DW_OP_breg27:
1450 case DW_OP_breg28:
1451 case DW_OP_breg29:
1452 case DW_OP_breg30:
1453 case DW_OP_breg31:
1454 READ_SLEB (svalue, data, end);
1455 printf ("DW_OP_breg%d (%s): %" PRId64,
1456 op - DW_OP_breg0, regname (op - DW_OP_breg0, 1), svalue);
1457 break;
1459 case DW_OP_regx:
1460 READ_ULEB (uvalue, data, end);
1461 printf ("DW_OP_regx: %" PRIu64 " (%s)",
1462 uvalue, regname (uvalue, 1));
1463 break;
1464 case DW_OP_fbreg:
1465 need_frame_base = 1;
1466 READ_SLEB (svalue, data, end);
1467 printf ("DW_OP_fbreg: %" PRId64, svalue);
1468 break;
1469 case DW_OP_bregx:
1470 READ_ULEB (uvalue, data, end);
1471 READ_SLEB (svalue, data, end);
1472 printf ("DW_OP_bregx: %" PRIu64 " (%s) %" PRId64,
1473 uvalue, regname (uvalue, 1), svalue);
1474 break;
1475 case DW_OP_piece:
1476 READ_ULEB (uvalue, data, end);
1477 printf ("DW_OP_piece: %" PRIu64, uvalue);
1478 break;
1479 case DW_OP_deref_size:
1480 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1481 printf ("DW_OP_deref_size: %" PRIu64, uvalue);
1482 break;
1483 case DW_OP_xderef_size:
1484 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1485 printf ("DW_OP_xderef_size: %" PRIu64, uvalue);
1486 break;
1487 case DW_OP_nop:
1488 printf ("DW_OP_nop");
1489 break;
1491 /* DWARF 3 extensions. */
1492 case DW_OP_push_object_address:
1493 printf ("DW_OP_push_object_address");
1494 break;
1495 case DW_OP_call2:
1496 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1497 this ought to be an 8-byte wide computation. */
1498 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1499 printf ("DW_OP_call2: <%#" PRIx64 ">", svalue + cu_offset);
1500 break;
1501 case DW_OP_call4:
1502 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1503 this ought to be an 8-byte wide computation. */
1504 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1505 printf ("DW_OP_call4: <%#" PRIx64 ">", svalue + cu_offset);
1506 break;
1507 case DW_OP_call_ref:
1508 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1509 this ought to be an 8-byte wide computation. */
1510 if (dwarf_version == -1)
1512 printf (_("(DW_OP_call_ref in frame info)"));
1513 /* No way to tell where the next op is, so just bail. */
1514 return need_frame_base;
1516 if (dwarf_version == 2)
1518 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1520 else
1522 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1524 printf ("DW_OP_call_ref: <%#" PRIx64 ">", uvalue);
1525 break;
1526 case DW_OP_form_tls_address:
1527 printf ("DW_OP_form_tls_address");
1528 break;
1529 case DW_OP_call_frame_cfa:
1530 printf ("DW_OP_call_frame_cfa");
1531 break;
1532 case DW_OP_bit_piece:
1533 printf ("DW_OP_bit_piece: ");
1534 READ_ULEB (uvalue, data, end);
1535 printf (_("size: %" PRIu64 " "), uvalue);
1536 READ_ULEB (uvalue, data, end);
1537 printf (_("offset: %" PRIu64 " "), uvalue);
1538 break;
1540 /* DWARF 4 extensions. */
1541 case DW_OP_stack_value:
1542 printf ("DW_OP_stack_value");
1543 break;
1545 case DW_OP_implicit_value:
1546 printf ("DW_OP_implicit_value");
1547 READ_ULEB (uvalue, data, end);
1548 data = display_block (data, uvalue, end, ' ');
1549 break;
1551 /* GNU extensions. */
1552 case DW_OP_GNU_push_tls_address:
1553 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1554 break;
1555 case DW_OP_GNU_uninit:
1556 printf ("DW_OP_GNU_uninit");
1557 /* FIXME: Is there data associated with this OP ? */
1558 break;
1559 case DW_OP_GNU_encoded_addr:
1561 int encoding = 0;
1562 uint64_t addr;
1564 if (data < end)
1565 encoding = *data++;
1566 addr = get_encoded_value (&data, encoding, section, end);
1568 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1569 print_hex_ns (addr, pointer_size);
1571 break;
1572 case DW_OP_implicit_pointer:
1573 case DW_OP_GNU_implicit_pointer:
1574 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1575 this ought to be an 8-byte wide computation. */
1576 if (dwarf_version == -1)
1578 printf (_("(%s in frame info)"),
1579 (op == DW_OP_implicit_pointer
1580 ? "DW_OP_implicit_pointer"
1581 : "DW_OP_GNU_implicit_pointer"));
1582 /* No way to tell where the next op is, so just bail. */
1583 return need_frame_base;
1585 if (dwarf_version == 2)
1587 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1589 else
1591 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1593 READ_SLEB (svalue, data, end);
1594 printf ("%s: <%#" PRIx64 "> %" PRId64,
1595 (op == DW_OP_implicit_pointer
1596 ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
1597 uvalue, svalue);
1598 break;
1599 case DW_OP_entry_value:
1600 case DW_OP_GNU_entry_value:
1601 READ_ULEB (uvalue, data, end);
1602 /* PR 17531: file: 0cc9cd00. */
1603 if (uvalue > (size_t) (end - data))
1604 uvalue = end - data;
1605 printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1606 : "DW_OP_GNU_entry_value"));
1607 if (decode_location_expression (data, pointer_size, offset_size,
1608 dwarf_version, uvalue,
1609 cu_offset, section))
1610 need_frame_base = 1;
1611 putchar (')');
1612 data += uvalue;
1613 break;
1614 case DW_OP_const_type:
1615 case DW_OP_GNU_const_type:
1616 READ_ULEB (uvalue, data, end);
1617 printf ("%s: <%#" PRIx64 "> ",
1618 (op == DW_OP_const_type ? "DW_OP_const_type"
1619 : "DW_OP_GNU_const_type"),
1620 cu_offset + uvalue);
1621 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1622 data = display_block (data, uvalue, end, ' ');
1623 break;
1624 case DW_OP_regval_type:
1625 case DW_OP_GNU_regval_type:
1626 READ_ULEB (uvalue, data, end);
1627 printf ("%s: %" PRIu64 " (%s)",
1628 (op == DW_OP_regval_type ? "DW_OP_regval_type"
1629 : "DW_OP_GNU_regval_type"),
1630 uvalue, regname (uvalue, 1));
1631 READ_ULEB (uvalue, data, end);
1632 printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1633 break;
1634 case DW_OP_deref_type:
1635 case DW_OP_GNU_deref_type:
1636 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1637 printf ("%s: %" PRId64,
1638 (op == DW_OP_deref_type ? "DW_OP_deref_type"
1639 : "DW_OP_GNU_deref_type"),
1640 uvalue);
1641 READ_ULEB (uvalue, data, end);
1642 printf (" <%#" PRIx64 ">", cu_offset + uvalue);
1643 break;
1644 case DW_OP_convert:
1645 case DW_OP_GNU_convert:
1646 READ_ULEB (uvalue, data, end);
1647 printf ("%s <%#" PRIx64 ">",
1648 (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
1649 uvalue ? cu_offset + uvalue : uvalue);
1650 break;
1651 case DW_OP_reinterpret:
1652 case DW_OP_GNU_reinterpret:
1653 READ_ULEB (uvalue, data, end);
1654 printf ("%s <%#" PRIx64 ">",
1655 (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1656 : "DW_OP_GNU_reinterpret"),
1657 uvalue ? cu_offset + uvalue : uvalue);
1658 break;
1659 case DW_OP_GNU_parameter_ref:
1660 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1661 printf ("DW_OP_GNU_parameter_ref: <%#" PRIx64 ">",
1662 cu_offset + uvalue);
1663 break;
1664 case DW_OP_addrx:
1665 READ_ULEB (uvalue, data, end);
1666 printf ("DW_OP_addrx <%#" PRIx64 ">", uvalue);
1667 break;
1668 case DW_OP_GNU_addr_index:
1669 READ_ULEB (uvalue, data, end);
1670 printf ("DW_OP_GNU_addr_index <%#" PRIx64 ">", uvalue);
1671 break;
1672 case DW_OP_GNU_const_index:
1673 READ_ULEB (uvalue, data, end);
1674 printf ("DW_OP_GNU_const_index <%#" PRIx64 ">", uvalue);
1675 break;
1676 case DW_OP_GNU_variable_value:
1677 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1678 this ought to be an 8-byte wide computation. */
1679 if (dwarf_version == -1)
1681 printf (_("(DW_OP_GNU_variable_value in frame info)"));
1682 /* No way to tell where the next op is, so just bail. */
1683 return need_frame_base;
1685 if (dwarf_version == 2)
1687 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1689 else
1691 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1693 printf ("DW_OP_GNU_variable_value: <%#" PRIx64 ">", uvalue);
1694 break;
1696 /* HP extensions. */
1697 case DW_OP_HP_is_value:
1698 printf ("DW_OP_HP_is_value");
1699 /* FIXME: Is there data associated with this OP ? */
1700 break;
1701 case DW_OP_HP_fltconst4:
1702 printf ("DW_OP_HP_fltconst4");
1703 /* FIXME: Is there data associated with this OP ? */
1704 break;
1705 case DW_OP_HP_fltconst8:
1706 printf ("DW_OP_HP_fltconst8");
1707 /* FIXME: Is there data associated with this OP ? */
1708 break;
1709 case DW_OP_HP_mod_range:
1710 printf ("DW_OP_HP_mod_range");
1711 /* FIXME: Is there data associated with this OP ? */
1712 break;
1713 case DW_OP_HP_unmod_range:
1714 printf ("DW_OP_HP_unmod_range");
1715 /* FIXME: Is there data associated with this OP ? */
1716 break;
1717 case DW_OP_HP_tls:
1718 printf ("DW_OP_HP_tls");
1719 /* FIXME: Is there data associated with this OP ? */
1720 break;
1722 /* PGI (STMicroelectronics) extensions. */
1723 case DW_OP_PGI_omp_thread_num:
1724 /* Pushes the thread number for the current thread as it would be
1725 returned by the standard OpenMP library function:
1726 omp_get_thread_num(). The "current thread" is the thread for
1727 which the expression is being evaluated. */
1728 printf ("DW_OP_PGI_omp_thread_num");
1729 break;
1731 default:
1732 if (op >= DW_OP_lo_user
1733 && op <= DW_OP_hi_user)
1734 printf (_("(User defined location op %#x)"), op);
1735 else
1736 printf (_("(Unknown location op %#x)"), op);
1737 /* No way to tell where the next op is, so just bail. */
1738 return need_frame_base;
1741 /* Separate the ops. */
1742 if (data < end)
1743 printf ("; ");
1746 return need_frame_base;
1749 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1750 This is used for DWARF package files. */
1752 static struct cu_tu_set *
1753 find_cu_tu_set_v2 (uint64_t cu_offset, int do_types)
1755 struct cu_tu_set *p;
1756 unsigned int nsets;
1757 unsigned int dw_sect;
1759 if (do_types)
1761 p = tu_sets;
1762 nsets = tu_count;
1763 dw_sect = DW_SECT_TYPES;
1765 else
1767 p = cu_sets;
1768 nsets = cu_count;
1769 dw_sect = DW_SECT_INFO;
1771 while (nsets > 0)
1773 if (p->section_offsets [dw_sect] == cu_offset)
1774 return p;
1775 p++;
1776 nsets--;
1778 return NULL;
1781 static const char *
1782 fetch_alt_indirect_string (uint64_t offset)
1784 separate_info * i;
1786 if (! do_follow_links)
1787 return "";
1789 if (first_separate_info == NULL)
1790 return _("<no links available>");
1792 for (i = first_separate_info; i != NULL; i = i->next)
1794 struct dwarf_section * section;
1795 const char * ret;
1797 if (! load_debug_section (separate_debug_str, i->handle))
1798 continue;
1800 section = &debug_displays [separate_debug_str].section;
1802 if (section->start == NULL)
1803 continue;
1805 if (offset >= section->size)
1806 continue;
1808 ret = (const char *) (section->start + offset);
1809 /* Unfortunately we cannot rely upon the .debug_str section ending with a
1810 NUL byte. Since our caller is expecting to receive a well formed C
1811 string we test for the lack of a terminating byte here. */
1812 if (strnlen ((const char *) ret, section->size - offset)
1813 == section->size - offset)
1814 return _("<no NUL byte at end of alt .debug_str section>");
1816 return ret;
1819 warn (_("DW_FORM_GNU_strp_alt offset (%#" PRIx64 ")"
1820 " too big or no string sections available\n"), offset);
1821 return _("<offset is too big>");
1824 static const char *
1825 get_AT_name (unsigned long attribute)
1827 const char *name;
1829 if (attribute == 0)
1830 return "DW_AT value: 0";
1832 /* One value is shared by the MIPS and HP extensions: */
1833 if (attribute == DW_AT_MIPS_fde)
1834 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1836 name = get_DW_AT_name (attribute);
1838 if (name == NULL)
1840 static char buffer[100];
1842 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1843 attribute);
1844 return buffer;
1847 return name;
1850 static void
1851 add_dwo_info (const char * value, uint64_t cu_offset, dwo_type type)
1853 dwo_info * dwinfo = xmalloc (sizeof * dwinfo);
1855 dwinfo->type = type;
1856 dwinfo->value = value;
1857 dwinfo->cu_offset = cu_offset;
1858 dwinfo->next = first_dwo_info;
1859 first_dwo_info = dwinfo;
1862 static void
1863 add_dwo_name (const char * name, uint64_t cu_offset)
1865 add_dwo_info (name, cu_offset, DWO_NAME);
1868 static void
1869 add_dwo_dir (const char * dir, uint64_t cu_offset)
1871 add_dwo_info (dir, cu_offset, DWO_DIR);
1874 static void
1875 add_dwo_id (const char * id, uint64_t cu_offset)
1877 add_dwo_info (id, cu_offset, DWO_ID);
1880 static void
1881 free_dwo_info (void)
1883 dwo_info * dwinfo;
1884 dwo_info * next;
1886 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = next)
1888 next = dwinfo->next;
1889 free (dwinfo);
1891 first_dwo_info = NULL;
1894 /* Ensure that START + UVALUE is less than END.
1895 Return an adjusted UVALUE if necessary to ensure this relationship. */
1897 static inline uint64_t
1898 check_uvalue (const unsigned char *start,
1899 uint64_t uvalue,
1900 const unsigned char *end)
1902 uint64_t max_uvalue = end - start;
1904 /* See PR 17512: file: 008-103549-0.001:0.1.
1905 and PR 24829 for examples of where these tests are triggered. */
1906 if (uvalue > max_uvalue)
1908 warn (_("Corrupt attribute block length: %#" PRIx64 "\n"), uvalue);
1909 uvalue = max_uvalue;
1912 return uvalue;
1915 static unsigned char *
1916 skip_attr_bytes (unsigned long form,
1917 unsigned char *data,
1918 unsigned char *end,
1919 uint64_t pointer_size,
1920 uint64_t offset_size,
1921 int dwarf_version,
1922 uint64_t *value_return)
1924 int64_t svalue;
1925 uint64_t uvalue = 0;
1926 uint64_t inc = 0;
1928 * value_return = 0;
1930 switch (form)
1932 case DW_FORM_ref_addr:
1933 if (dwarf_version == 2)
1934 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1935 else if (dwarf_version > 2)
1936 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1937 else
1938 return NULL;
1939 break;
1941 case DW_FORM_addr:
1942 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1943 break;
1945 case DW_FORM_strp:
1946 case DW_FORM_line_strp:
1947 case DW_FORM_sec_offset:
1948 case DW_FORM_GNU_ref_alt:
1949 case DW_FORM_GNU_strp_alt:
1950 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1951 break;
1953 case DW_FORM_flag_present:
1954 uvalue = 1;
1955 break;
1957 case DW_FORM_ref1:
1958 case DW_FORM_flag:
1959 case DW_FORM_data1:
1960 case DW_FORM_strx1:
1961 case DW_FORM_addrx1:
1962 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1963 break;
1965 case DW_FORM_strx3:
1966 case DW_FORM_addrx3:
1967 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
1968 break;
1970 case DW_FORM_ref2:
1971 case DW_FORM_data2:
1972 case DW_FORM_strx2:
1973 case DW_FORM_addrx2:
1974 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1975 break;
1977 case DW_FORM_ref4:
1978 case DW_FORM_data4:
1979 case DW_FORM_strx4:
1980 case DW_FORM_addrx4:
1981 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1982 break;
1984 case DW_FORM_sdata:
1985 READ_SLEB (svalue, data, end);
1986 uvalue = svalue;
1987 break;
1989 case DW_FORM_ref_udata:
1990 case DW_FORM_udata:
1991 case DW_FORM_GNU_str_index:
1992 case DW_FORM_strx:
1993 case DW_FORM_GNU_addr_index:
1994 case DW_FORM_addrx:
1995 case DW_FORM_loclistx:
1996 case DW_FORM_rnglistx:
1997 READ_ULEB (uvalue, data, end);
1998 break;
2000 case DW_FORM_ref8:
2001 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2002 break;
2004 case DW_FORM_data8:
2005 case DW_FORM_ref_sig8:
2006 inc = 8;
2007 break;
2009 case DW_FORM_data16:
2010 inc = 16;
2011 break;
2013 case DW_FORM_string:
2014 inc = strnlen ((char *) data, end - data) + 1;
2015 break;
2017 case DW_FORM_block:
2018 case DW_FORM_exprloc:
2019 READ_ULEB (uvalue, data, end);
2020 inc = uvalue;
2021 break;
2023 case DW_FORM_block1:
2024 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2025 inc = uvalue;
2026 break;
2028 case DW_FORM_block2:
2029 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2030 inc = uvalue;
2031 break;
2033 case DW_FORM_block4:
2034 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2035 inc = uvalue;
2036 break;
2038 case DW_FORM_indirect:
2039 READ_ULEB (form, data, end);
2040 if (form == DW_FORM_implicit_const)
2041 SKIP_ULEB (data, end);
2042 return skip_attr_bytes (form, data, end, pointer_size, offset_size,
2043 dwarf_version, value_return);
2045 default:
2046 return NULL;
2049 * value_return = uvalue;
2050 if (inc <= (size_t) (end - data))
2051 data += inc;
2052 else
2053 data = end;
2054 return data;
2057 /* Given form FORM with value UVALUE, locate and return the abbreviation
2058 associated with it. */
2060 static abbrev_entry *
2061 get_type_abbrev_from_form (unsigned long form,
2062 unsigned long uvalue,
2063 uint64_t cu_offset,
2064 unsigned char *cu_end,
2065 const struct dwarf_section *section,
2066 unsigned long *abbrev_num_return,
2067 unsigned char **data_return,
2068 abbrev_map **map_return)
2070 unsigned long abbrev_number;
2071 abbrev_map * map;
2072 abbrev_entry * entry;
2073 unsigned char * data;
2075 if (abbrev_num_return != NULL)
2076 * abbrev_num_return = 0;
2077 if (data_return != NULL)
2078 * data_return = NULL;
2080 switch (form)
2082 case DW_FORM_GNU_ref_alt:
2083 case DW_FORM_ref_sig8:
2084 /* FIXME: We are unable to handle this form at the moment. */
2085 return NULL;
2087 case DW_FORM_ref_addr:
2088 if (uvalue >= section->size)
2090 warn (_("Unable to resolve ref_addr form: uvalue %lx "
2091 "> section size %" PRIx64 " (%s)\n"),
2092 uvalue, section->size, section->name);
2093 return NULL;
2095 break;
2097 case DW_FORM_ref_sup4:
2098 case DW_FORM_ref_sup8:
2099 break;
2101 case DW_FORM_ref1:
2102 case DW_FORM_ref2:
2103 case DW_FORM_ref4:
2104 case DW_FORM_ref8:
2105 case DW_FORM_ref_udata:
2106 if (uvalue + cu_offset < uvalue
2107 || uvalue + cu_offset > (size_t) (cu_end - section->start))
2109 warn (_("Unable to resolve ref form: uvalue %lx + cu_offset %" PRIx64
2110 " > CU size %tx\n"),
2111 uvalue, cu_offset, cu_end - section->start);
2112 return NULL;
2114 uvalue += cu_offset;
2115 break;
2117 /* FIXME: Are there other DW_FORMs that can be used by types ? */
2119 default:
2120 warn (_("Unexpected form %lx encountered whilst finding abbreviation for type\n"), form);
2121 return NULL;
2124 data = (unsigned char *) section->start + uvalue;
2125 map = find_abbrev_map_by_offset (uvalue);
2127 if (map == NULL)
2129 warn (_("Unable to find abbreviations for CU offset %#lx\n"), uvalue);
2130 return NULL;
2132 if (map->list == NULL)
2134 warn (_("Empty abbreviation list encountered for CU offset %lx\n"), uvalue);
2135 return NULL;
2138 if (map_return != NULL)
2140 if (form == DW_FORM_ref_addr)
2141 *map_return = map;
2142 else
2143 *map_return = NULL;
2146 READ_ULEB (abbrev_number, data, section->start + section->size);
2148 for (entry = map->list->first_abbrev; entry != NULL; entry = entry->next)
2149 if (entry->number == abbrev_number)
2150 break;
2152 if (abbrev_num_return != NULL)
2153 * abbrev_num_return = abbrev_number;
2155 if (data_return != NULL)
2156 * data_return = data;
2158 if (entry == NULL)
2159 warn (_("Unable to find entry for abbreviation %lu\n"), abbrev_number);
2161 return entry;
2164 /* Return IS_SIGNED set to TRUE if the type using abbreviation ENTRY
2165 can be determined to be a signed type. The data for ENTRY can be
2166 found starting at DATA. */
2168 static void
2169 get_type_signedness (abbrev_entry *entry,
2170 const struct dwarf_section *section,
2171 unsigned char *data,
2172 unsigned char *end,
2173 uint64_t cu_offset,
2174 uint64_t pointer_size,
2175 uint64_t offset_size,
2176 int dwarf_version,
2177 bool *is_signed,
2178 unsigned int nesting)
2180 abbrev_attr * attr;
2182 * is_signed = false;
2184 #define MAX_NESTING 20
2185 if (nesting > MAX_NESTING)
2187 /* FIXME: Warn - or is this expected ?
2188 NB/ We need to avoid infinite recursion. */
2189 return;
2192 for (attr = entry->first_attr;
2193 attr != NULL && attr->attribute;
2194 attr = attr->next)
2196 unsigned char * orig_data = data;
2197 uint64_t uvalue = 0;
2199 data = skip_attr_bytes (attr->form, data, end, pointer_size,
2200 offset_size, dwarf_version, & uvalue);
2201 if (data == NULL)
2202 return;
2204 switch (attr->attribute)
2206 case DW_AT_linkage_name:
2207 case DW_AT_name:
2208 if (do_wide)
2210 if (attr->form == DW_FORM_strp)
2211 printf (", %s", fetch_indirect_string (uvalue));
2212 else if (attr->form == DW_FORM_string)
2213 printf (", %.*s", (int) (end - orig_data), orig_data);
2215 break;
2217 case DW_AT_type:
2218 /* Recurse. */
2220 abbrev_entry *type_abbrev;
2221 unsigned char *type_data;
2222 abbrev_map *map;
2224 type_abbrev = get_type_abbrev_from_form (attr->form,
2225 uvalue,
2226 cu_offset,
2227 end,
2228 section,
2229 NULL /* abbrev num return */,
2230 &type_data,
2231 &map);
2232 if (type_abbrev == NULL)
2233 break;
2235 get_type_signedness (type_abbrev, section, type_data,
2236 map ? section->start + map->end : end,
2237 map ? map->start : cu_offset,
2238 pointer_size, offset_size, dwarf_version,
2239 is_signed, nesting + 1);
2241 break;
2243 case DW_AT_encoding:
2244 /* Determine signness. */
2245 switch (uvalue)
2247 case DW_ATE_address:
2248 /* FIXME - some architectures have signed addresses. */
2249 case DW_ATE_boolean:
2250 case DW_ATE_unsigned:
2251 case DW_ATE_unsigned_char:
2252 case DW_ATE_unsigned_fixed:
2253 * is_signed = false;
2254 break;
2256 default:
2257 case DW_ATE_complex_float:
2258 case DW_ATE_float:
2259 case DW_ATE_signed:
2260 case DW_ATE_signed_char:
2261 case DW_ATE_imaginary_float:
2262 case DW_ATE_decimal_float:
2263 case DW_ATE_signed_fixed:
2264 * is_signed = true;
2265 break;
2267 break;
2272 static void
2273 read_and_print_leb128 (unsigned char *data,
2274 unsigned int *bytes_read,
2275 unsigned const char *end,
2276 bool is_signed)
2278 int status;
2279 uint64_t val = read_leb128 (data, end, is_signed, bytes_read, &status);
2280 if (status != 0)
2281 report_leb_status (status);
2282 else if (is_signed)
2283 printf ("%" PRId64, val);
2284 else
2285 printf ("%" PRIu64, val);
2288 static void
2289 display_discr_list (unsigned long form,
2290 uint64_t uvalue,
2291 unsigned char *data,
2292 int level)
2294 unsigned char *end = data;
2296 if (uvalue == 0)
2298 printf ("[default]");
2299 return;
2302 switch (form)
2304 case DW_FORM_block:
2305 case DW_FORM_block1:
2306 case DW_FORM_block2:
2307 case DW_FORM_block4:
2308 /* Move data pointer back to the start of the byte array. */
2309 data -= uvalue;
2310 break;
2311 default:
2312 printf ("<corrupt>\n");
2313 warn (_("corrupt discr_list - not using a block form\n"));
2314 return;
2317 if (uvalue < 2)
2319 printf ("<corrupt>\n");
2320 warn (_("corrupt discr_list - block not long enough\n"));
2321 return;
2324 bool is_signed = (level > 0 && level <= MAX_CU_NESTING
2325 ? level_type_signed [level - 1] : false);
2327 printf ("(");
2328 while (data < end)
2330 unsigned char discriminant;
2331 unsigned int bytes_read;
2333 SAFE_BYTE_GET_AND_INC (discriminant, data, 1, end);
2335 switch (discriminant)
2337 case DW_DSC_label:
2338 printf ("label ");
2339 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2340 data += bytes_read;
2341 break;
2343 case DW_DSC_range:
2344 printf ("range ");
2345 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2346 data += bytes_read;
2348 printf ("..");
2349 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2350 data += bytes_read;
2351 break;
2353 default:
2354 printf ("<corrupt>\n");
2355 warn (_("corrupt discr_list - unrecognized discriminant byte %#x\n"),
2356 discriminant);
2357 return;
2360 if (data < end)
2361 printf (", ");
2364 if (is_signed)
2365 printf (")(signed)");
2366 else
2367 printf (")(unsigned)");
2370 static unsigned char *
2371 read_and_display_attr_value (unsigned long attribute,
2372 unsigned long form,
2373 int64_t implicit_const,
2374 unsigned char *start,
2375 unsigned char *data,
2376 unsigned char *end,
2377 uint64_t cu_offset,
2378 uint64_t pointer_size,
2379 uint64_t offset_size,
2380 int dwarf_version,
2381 debug_info *debug_info_p,
2382 int do_loc,
2383 struct dwarf_section *section,
2384 struct cu_tu_set *this_set,
2385 char delimiter,
2386 int level)
2388 int64_t svalue;
2389 uint64_t uvalue = 0;
2390 uint64_t uvalue_hi = 0;
2391 unsigned char *block_start = NULL;
2392 unsigned char *orig_data = data;
2394 if (data > end || (data == end && form != DW_FORM_flag_present))
2396 warn (_("Corrupt attribute\n"));
2397 return data;
2400 if (do_wide && ! do_loc)
2402 /* PR 26847: Display the name of the form. */
2403 const char * name = get_FORM_name (form);
2405 /* For convenience we skip the DW_FORM_ prefix to the name. */
2406 if (name[0] == 'D')
2407 name += 8; /* strlen ("DW_FORM_") */
2408 printf ("%c(%s)", delimiter, name);
2411 switch (form)
2413 case DW_FORM_ref_addr:
2414 if (dwarf_version == 2)
2415 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2416 else if (dwarf_version > 2)
2417 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2418 else
2419 error (_("Internal error: DW_FORM_ref_addr is not supported in DWARF version 1.\n"));
2420 break;
2422 case DW_FORM_addr:
2423 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2424 break;
2426 case DW_FORM_strp_sup:
2427 case DW_FORM_strp:
2428 case DW_FORM_line_strp:
2429 case DW_FORM_sec_offset:
2430 case DW_FORM_GNU_ref_alt:
2431 case DW_FORM_GNU_strp_alt:
2432 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2433 break;
2435 case DW_FORM_flag_present:
2436 uvalue = 1;
2437 break;
2439 case DW_FORM_ref1:
2440 case DW_FORM_flag:
2441 case DW_FORM_data1:
2442 case DW_FORM_strx1:
2443 case DW_FORM_addrx1:
2444 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2445 break;
2447 case DW_FORM_ref2:
2448 case DW_FORM_data2:
2449 case DW_FORM_strx2:
2450 case DW_FORM_addrx2:
2451 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2452 break;
2454 case DW_FORM_strx3:
2455 case DW_FORM_addrx3:
2456 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2457 break;
2459 case DW_FORM_ref_sup4:
2460 case DW_FORM_ref4:
2461 case DW_FORM_data4:
2462 case DW_FORM_strx4:
2463 case DW_FORM_addrx4:
2464 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2465 break;
2467 case DW_FORM_ref_sup8:
2468 case DW_FORM_ref8:
2469 case DW_FORM_data8:
2470 case DW_FORM_ref_sig8:
2471 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2472 break;
2474 case DW_FORM_data16:
2475 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2476 SAFE_BYTE_GET_AND_INC (uvalue_hi, data, 8, end);
2477 if (byte_get != byte_get_little_endian)
2479 uint64_t utmp = uvalue;
2480 uvalue = uvalue_hi;
2481 uvalue_hi = utmp;
2483 break;
2485 case DW_FORM_sdata:
2486 READ_SLEB (svalue, data, end);
2487 uvalue = svalue;
2488 break;
2490 case DW_FORM_GNU_str_index:
2491 case DW_FORM_strx:
2492 case DW_FORM_ref_udata:
2493 case DW_FORM_udata:
2494 case DW_FORM_GNU_addr_index:
2495 case DW_FORM_addrx:
2496 case DW_FORM_loclistx:
2497 case DW_FORM_rnglistx:
2498 READ_ULEB (uvalue, data, end);
2499 break;
2501 case DW_FORM_indirect:
2502 READ_ULEB (form, data, end);
2503 if (!do_loc)
2504 printf ("%c%s", delimiter, get_FORM_name (form));
2505 if (form == DW_FORM_implicit_const)
2506 READ_SLEB (implicit_const, data, end);
2507 return read_and_display_attr_value (attribute, form, implicit_const,
2508 start, data, end,
2509 cu_offset, pointer_size,
2510 offset_size, dwarf_version,
2511 debug_info_p, do_loc,
2512 section, this_set, delimiter, level);
2514 case DW_FORM_implicit_const:
2515 uvalue = implicit_const;
2516 break;
2518 default:
2519 break;
2522 switch (form)
2524 case DW_FORM_ref_addr:
2525 if (!do_loc)
2526 printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2527 break;
2529 case DW_FORM_GNU_ref_alt:
2530 if (!do_loc)
2532 if (do_wide)
2533 /* We have already printed the form name. */
2534 printf ("%c<%#" PRIx64 ">", delimiter, uvalue);
2535 else
2536 printf ("%c<alt %#" PRIx64 ">", delimiter, uvalue);
2538 /* FIXME: Follow the reference... */
2539 break;
2541 case DW_FORM_ref1:
2542 case DW_FORM_ref2:
2543 case DW_FORM_ref4:
2544 case DW_FORM_ref_sup4:
2545 case DW_FORM_ref_udata:
2546 if (!do_loc)
2547 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2548 break;
2550 case DW_FORM_data4:
2551 case DW_FORM_addr:
2552 case DW_FORM_sec_offset:
2553 if (!do_loc)
2554 printf ("%c%#" PRIx64, delimiter, uvalue);
2555 break;
2557 case DW_FORM_flag_present:
2558 case DW_FORM_flag:
2559 case DW_FORM_data1:
2560 case DW_FORM_data2:
2561 case DW_FORM_sdata:
2562 if (!do_loc)
2563 printf ("%c%" PRId64, delimiter, uvalue);
2564 break;
2566 case DW_FORM_udata:
2567 if (!do_loc)
2568 printf ("%c%" PRIu64, delimiter, uvalue);
2569 break;
2571 case DW_FORM_implicit_const:
2572 if (!do_loc)
2573 printf ("%c%" PRId64, delimiter, implicit_const);
2574 break;
2576 case DW_FORM_ref_sup8:
2577 case DW_FORM_ref8:
2578 case DW_FORM_data8:
2579 if (!do_loc)
2581 uint64_t utmp = uvalue;
2582 if (form == DW_FORM_ref8)
2583 utmp += cu_offset;
2584 printf ("%c%#" PRIx64, delimiter, utmp);
2586 break;
2588 case DW_FORM_data16:
2589 if (!do_loc)
2591 if (uvalue_hi == 0)
2592 printf (" %#" PRIx64, uvalue);
2593 else
2594 printf (" %#" PRIx64 "%016" PRIx64, uvalue_hi, uvalue);
2596 break;
2598 case DW_FORM_string:
2599 if (!do_loc)
2600 printf ("%c%.*s", delimiter, (int) (end - data), data);
2601 data += strnlen ((char *) data, end - data);
2602 if (data < end)
2603 data++;
2604 break;
2606 case DW_FORM_block:
2607 case DW_FORM_exprloc:
2608 READ_ULEB (uvalue, data, end);
2609 do_block:
2610 block_start = data;
2611 if (block_start >= end)
2613 warn (_("Block ends prematurely\n"));
2614 uvalue = 0;
2615 block_start = end;
2618 uvalue = check_uvalue (block_start, uvalue, end);
2620 data = block_start + uvalue;
2621 if (!do_loc)
2623 unsigned char op;
2625 SAFE_BYTE_GET (op, block_start, sizeof (op), end);
2626 if (op != DW_OP_addrx)
2627 data = display_block (block_start, uvalue, end, delimiter);
2629 break;
2631 case DW_FORM_block1:
2632 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2633 goto do_block;
2635 case DW_FORM_block2:
2636 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2637 goto do_block;
2639 case DW_FORM_block4:
2640 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2641 goto do_block;
2643 case DW_FORM_strp:
2644 if (!do_loc)
2646 if (do_wide)
2647 /* We have already displayed the form name. */
2648 printf (_("%c(offset: %#" PRIx64 "): %s"),
2649 delimiter, uvalue, fetch_indirect_string (uvalue));
2650 else
2651 printf (_("%c(indirect string, offset: %#" PRIx64 "): %s"),
2652 delimiter, uvalue, fetch_indirect_string (uvalue));
2654 break;
2656 case DW_FORM_line_strp:
2657 if (!do_loc)
2659 if (do_wide)
2660 /* We have already displayed the form name. */
2661 printf (_("%c(offset: %#" PRIx64 "): %s"),
2662 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2663 else
2664 printf (_("%c(indirect line string, offset: %#" PRIx64 "): %s"),
2665 delimiter, uvalue, fetch_indirect_line_string (uvalue));
2667 break;
2669 case DW_FORM_GNU_str_index:
2670 case DW_FORM_strx:
2671 case DW_FORM_strx1:
2672 case DW_FORM_strx2:
2673 case DW_FORM_strx3:
2674 case DW_FORM_strx4:
2675 if (!do_loc)
2677 const char *suffix = section ? strrchr (section->name, '.') : NULL;
2678 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2679 const char *strng;
2681 strng = fetch_indexed_string (uvalue, this_set, offset_size, dwo,
2682 debug_info_p ? debug_info_p->str_offsets_base : 0);
2683 if (do_wide)
2684 /* We have already displayed the form name. */
2685 printf (_("%c(offset: %#" PRIx64 "): %s"),
2686 delimiter, uvalue, strng);
2687 else
2688 printf (_("%c(indexed string: %#" PRIx64 "): %s"),
2689 delimiter, uvalue, strng);
2691 break;
2693 case DW_FORM_GNU_strp_alt:
2694 if (!do_loc)
2696 if (do_wide)
2697 /* We have already displayed the form name. */
2698 printf (_("%c(offset: %#" PRIx64 ") %s"),
2699 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2700 else
2701 printf (_("%c(alt indirect string, offset: %#" PRIx64 ") %s"),
2702 delimiter, uvalue, fetch_alt_indirect_string (uvalue));
2704 break;
2706 case DW_FORM_indirect:
2707 /* Handled above. */
2708 break;
2710 case DW_FORM_ref_sig8:
2711 if (!do_loc)
2712 printf ("%c%s: %#" PRIx64, delimiter, do_wide ? "" : "signature",
2713 uvalue);
2714 break;
2716 case DW_FORM_GNU_addr_index:
2717 case DW_FORM_addrx:
2718 case DW_FORM_addrx1:
2719 case DW_FORM_addrx2:
2720 case DW_FORM_addrx3:
2721 case DW_FORM_addrx4:
2722 case DW_FORM_loclistx:
2723 case DW_FORM_rnglistx:
2724 if (!do_loc)
2726 uint64_t base, idx;
2727 const char *suffix = strrchr (section->name, '.');
2728 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2730 if (form == DW_FORM_loclistx)
2732 if (dwo)
2734 idx = fetch_indexed_value (uvalue, loclists_dwo, 0);
2735 if (idx != (uint64_t) -1)
2736 idx += (offset_size == 8) ? 20 : 12;
2738 else if (debug_info_p == NULL)
2740 idx = fetch_indexed_value (uvalue, loclists, 0);
2742 else
2744 /* We want to compute:
2745 idx = fetch_indexed_value (uvalue, loclists, debug_info_p->loclists_base);
2746 idx += debug_info_p->loclists_base;
2747 Fortunately we already have that sum cached in the
2748 loc_offsets array. */
2749 if (uvalue < debug_info_p->num_loc_offsets)
2750 idx = debug_info_p->loc_offsets [uvalue];
2751 else
2753 warn (_("loc_offset %" PRIu64 " too big\n"), uvalue);
2754 idx = -1;
2758 else if (form == DW_FORM_rnglistx)
2760 if (dwo)
2762 idx = fetch_indexed_value (uvalue, rnglists_dwo, 0);
2763 if (idx != (uint64_t) -1)
2764 idx += (offset_size == 8) ? 20 : 12;
2766 else
2768 if (debug_info_p == NULL)
2769 base = 0;
2770 else
2771 base = debug_info_p->rnglists_base;
2772 /* We do not have a cached value this time, so we perform the
2773 computation manually. */
2774 idx = fetch_indexed_value (uvalue, rnglists, base);
2775 if (idx != (uint64_t) -1)
2776 idx += base;
2779 else
2781 if (debug_info_p == NULL)
2782 base = 0;
2783 else if (debug_info_p->addr_base == DEBUG_INFO_UNAVAILABLE)
2784 base = 0;
2785 else
2786 base = debug_info_p->addr_base;
2788 base += uvalue * pointer_size;
2789 idx = fetch_indexed_addr (base, pointer_size);
2792 /* We have already displayed the form name. */
2793 if (idx != (uint64_t) -1)
2794 printf (_("%c(index: %#" PRIx64 "): %#" PRIx64),
2795 delimiter, uvalue, idx);
2797 break;
2799 case DW_FORM_strp_sup:
2800 if (!do_loc)
2801 printf ("%c<%#" PRIx64 ">", delimiter, uvalue + cu_offset);
2802 break;
2804 default:
2805 warn (_("Unrecognized form: %#lx\n"), form);
2806 /* What to do? Consume a byte maybe? */
2807 ++data;
2808 break;
2811 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
2812 && num_debug_info_entries == 0
2813 && debug_info_p != NULL)
2815 switch (attribute)
2817 case DW_AT_loclists_base:
2818 if (debug_info_p->loclists_base)
2819 warn (_("CU @ %#" PRIx64 " has multiple loclists_base values "
2820 "(%#" PRIx64 " and %#" PRIx64 ")"),
2821 debug_info_p->cu_offset,
2822 debug_info_p->loclists_base, uvalue);
2823 debug_info_p->loclists_base = uvalue;
2824 break;
2825 case DW_AT_rnglists_base:
2826 if (debug_info_p->rnglists_base)
2827 warn (_("CU @ %#" PRIx64 " has multiple rnglists_base values "
2828 "(%#" PRIx64 " and %#" PRIx64 ")"),
2829 debug_info_p->cu_offset,
2830 debug_info_p->rnglists_base, uvalue);
2831 debug_info_p->rnglists_base = uvalue;
2832 break;
2833 case DW_AT_str_offsets_base:
2834 if (debug_info_p->str_offsets_base)
2835 warn (_("CU @ %#" PRIx64 " has multiple str_offsets_base values "
2836 "%#" PRIx64 " and %#" PRIx64 ")"),
2837 debug_info_p->cu_offset,
2838 debug_info_p->str_offsets_base, uvalue);
2839 debug_info_p->str_offsets_base = uvalue;
2840 break;
2842 case DW_AT_frame_base:
2843 have_frame_base = 1;
2844 /* Fall through. */
2845 case DW_AT_location:
2846 case DW_AT_GNU_locviews:
2847 case DW_AT_string_length:
2848 case DW_AT_return_addr:
2849 case DW_AT_data_member_location:
2850 case DW_AT_vtable_elem_location:
2851 case DW_AT_segment:
2852 case DW_AT_static_link:
2853 case DW_AT_use_location:
2854 case DW_AT_call_value:
2855 case DW_AT_GNU_call_site_value:
2856 case DW_AT_call_data_value:
2857 case DW_AT_GNU_call_site_data_value:
2858 case DW_AT_call_target:
2859 case DW_AT_GNU_call_site_target:
2860 case DW_AT_call_target_clobbered:
2861 case DW_AT_GNU_call_site_target_clobbered:
2862 if ((dwarf_version < 4
2863 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2864 || form == DW_FORM_sec_offset
2865 || form == DW_FORM_loclistx)
2867 /* Process location list. */
2868 unsigned int lmax = debug_info_p->max_loc_offsets;
2869 unsigned int num = debug_info_p->num_loc_offsets;
2871 if (lmax == 0 || num >= lmax)
2873 lmax += 1024;
2874 debug_info_p->loc_offsets = (uint64_t *)
2875 xcrealloc (debug_info_p->loc_offsets,
2876 lmax, sizeof (*debug_info_p->loc_offsets));
2877 debug_info_p->loc_views = (uint64_t *)
2878 xcrealloc (debug_info_p->loc_views,
2879 lmax, sizeof (*debug_info_p->loc_views));
2880 debug_info_p->have_frame_base = (int *)
2881 xcrealloc (debug_info_p->have_frame_base,
2882 lmax, sizeof (*debug_info_p->have_frame_base));
2883 debug_info_p->max_loc_offsets = lmax;
2885 if (form == DW_FORM_loclistx)
2886 uvalue = fetch_indexed_value (num, loclists, debug_info_p->loclists_base);
2887 else if (this_set != NULL)
2888 uvalue += this_set->section_offsets [DW_SECT_LOC];
2890 debug_info_p->have_frame_base [num] = have_frame_base;
2891 if (attribute != DW_AT_GNU_locviews)
2893 uvalue += debug_info_p->loclists_base;
2895 /* Corrupt DWARF info can produce more offsets than views.
2896 See PR 23062 for an example. */
2897 if (debug_info_p->num_loc_offsets
2898 > debug_info_p->num_loc_views)
2899 warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
2900 else
2902 debug_info_p->loc_offsets [num] = uvalue;
2903 debug_info_p->num_loc_offsets++;
2906 else
2908 assert (debug_info_p->num_loc_views <= num);
2909 num = debug_info_p->num_loc_views;
2910 if (num > debug_info_p->num_loc_offsets)
2911 warn (_("More DW_AT_GNU_locview attributes than location offset attributes\n"));
2912 else
2914 debug_info_p->loc_views [num] = uvalue;
2915 debug_info_p->num_loc_views++;
2919 break;
2921 case DW_AT_low_pc:
2922 if (need_base_address)
2923 debug_info_p->base_address = uvalue;
2924 break;
2926 case DW_AT_GNU_addr_base:
2927 case DW_AT_addr_base:
2928 debug_info_p->addr_base = uvalue;
2929 break;
2931 case DW_AT_GNU_ranges_base:
2932 debug_info_p->ranges_base = uvalue;
2933 break;
2935 case DW_AT_ranges:
2936 if ((dwarf_version < 4
2937 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2938 || form == DW_FORM_sec_offset
2939 || form == DW_FORM_rnglistx)
2941 /* Process range list. */
2942 unsigned int lmax = debug_info_p->max_range_lists;
2943 unsigned int num = debug_info_p->num_range_lists;
2945 if (lmax == 0 || num >= lmax)
2947 lmax += 1024;
2948 debug_info_p->range_lists = (uint64_t *)
2949 xcrealloc (debug_info_p->range_lists,
2950 lmax, sizeof (*debug_info_p->range_lists));
2951 debug_info_p->max_range_lists = lmax;
2954 if (form == DW_FORM_rnglistx)
2955 uvalue = fetch_indexed_value (uvalue, rnglists, 0);
2957 debug_info_p->range_lists [num] = uvalue;
2958 debug_info_p->num_range_lists++;
2960 break;
2962 case DW_AT_GNU_dwo_name:
2963 case DW_AT_dwo_name:
2964 if (need_dwo_info)
2965 switch (form)
2967 case DW_FORM_strp:
2968 add_dwo_name ((const char *) fetch_indirect_string (uvalue), cu_offset);
2969 break;
2970 case DW_FORM_GNU_strp_alt:
2971 add_dwo_name ((const char *) fetch_alt_indirect_string (uvalue), cu_offset);
2972 break;
2973 case DW_FORM_GNU_str_index:
2974 case DW_FORM_strx:
2975 case DW_FORM_strx1:
2976 case DW_FORM_strx2:
2977 case DW_FORM_strx3:
2978 case DW_FORM_strx4:
2979 add_dwo_name (fetch_indexed_string (uvalue, this_set, offset_size, false,
2980 debug_info_p->str_offsets_base),
2981 cu_offset);
2982 break;
2983 case DW_FORM_string:
2984 add_dwo_name ((const char *) orig_data, cu_offset);
2985 break;
2986 default:
2987 warn (_("Unsupported form (%s) for attribute %s\n"),
2988 get_FORM_name (form), get_AT_name (attribute));
2989 break;
2991 break;
2993 case DW_AT_comp_dir:
2994 /* FIXME: Also extract a build-id in a CU/TU. */
2995 if (need_dwo_info)
2996 switch (form)
2998 case DW_FORM_strp:
2999 add_dwo_dir ((const char *) fetch_indirect_string (uvalue), cu_offset);
3000 break;
3001 case DW_FORM_GNU_strp_alt:
3002 add_dwo_dir (fetch_alt_indirect_string (uvalue), cu_offset);
3003 break;
3004 case DW_FORM_line_strp:
3005 add_dwo_dir ((const char *) fetch_indirect_line_string (uvalue), cu_offset);
3006 break;
3007 case DW_FORM_GNU_str_index:
3008 case DW_FORM_strx:
3009 case DW_FORM_strx1:
3010 case DW_FORM_strx2:
3011 case DW_FORM_strx3:
3012 case DW_FORM_strx4:
3013 add_dwo_dir (fetch_indexed_string (uvalue, this_set, offset_size, false,
3014 debug_info_p->str_offsets_base),
3015 cu_offset);
3016 break;
3017 case DW_FORM_string:
3018 add_dwo_dir ((const char *) orig_data, cu_offset);
3019 break;
3020 default:
3021 warn (_("Unsupported form (%s) for attribute %s\n"),
3022 get_FORM_name (form), get_AT_name (attribute));
3023 break;
3025 break;
3027 case DW_AT_GNU_dwo_id:
3028 if (need_dwo_info)
3029 switch (form)
3031 case DW_FORM_data8:
3032 /* FIXME: Record the length of the ID as well ? */
3033 add_dwo_id ((const char *) (data - 8), cu_offset);
3034 break;
3035 default:
3036 warn (_("Unsupported form (%s) for attribute %s\n"),
3037 get_FORM_name (form), get_AT_name (attribute));
3038 break;
3040 break;
3042 default:
3043 break;
3047 if (do_loc || attribute == 0)
3048 return data;
3050 /* For some attributes we can display further information. */
3051 switch (attribute)
3053 case DW_AT_type:
3054 if (level >= 0 && level < MAX_CU_NESTING
3055 && uvalue < (size_t) (end - start))
3057 bool is_signed = false;
3058 abbrev_entry *type_abbrev;
3059 unsigned char *type_data;
3060 abbrev_map *map;
3062 type_abbrev = get_type_abbrev_from_form (form, uvalue,
3063 cu_offset, end,
3064 section, NULL,
3065 &type_data, &map);
3066 if (type_abbrev != NULL)
3068 get_type_signedness (type_abbrev, section, type_data,
3069 map ? section->start + map->end : end,
3070 map ? map->start : cu_offset,
3071 pointer_size, offset_size, dwarf_version,
3072 & is_signed, 0);
3074 level_type_signed[level] = is_signed;
3076 break;
3078 case DW_AT_inline:
3079 printf ("\t");
3080 switch (uvalue)
3082 case DW_INL_not_inlined:
3083 printf (_("(not inlined)"));
3084 break;
3085 case DW_INL_inlined:
3086 printf (_("(inlined)"));
3087 break;
3088 case DW_INL_declared_not_inlined:
3089 printf (_("(declared as inline but ignored)"));
3090 break;
3091 case DW_INL_declared_inlined:
3092 printf (_("(declared as inline and inlined)"));
3093 break;
3094 default:
3095 printf (_(" (Unknown inline attribute value: %#" PRIx64 ")"),
3096 uvalue);
3097 break;
3099 break;
3101 case DW_AT_language:
3102 printf ("\t");
3103 switch (uvalue)
3105 /* Ordered by the numeric value of these constants. */
3106 case DW_LANG_C89: printf ("(ANSI C)"); break;
3107 case DW_LANG_C: printf ("(non-ANSI C)"); break;
3108 case DW_LANG_Ada83: printf ("(Ada)"); break;
3109 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
3110 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
3111 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
3112 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
3113 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
3114 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
3115 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
3116 /* DWARF 2.1 values. */
3117 case DW_LANG_Java: printf ("(Java)"); break;
3118 case DW_LANG_C99: printf ("(ANSI C99)"); break;
3119 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
3120 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
3121 /* DWARF 3 values. */
3122 case DW_LANG_PLI: printf ("(PLI)"); break;
3123 case DW_LANG_ObjC: printf ("(Objective C)"); break;
3124 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
3125 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
3126 case DW_LANG_D: printf ("(D)"); break;
3127 /* DWARF 4 values. */
3128 case DW_LANG_Python: printf ("(Python)"); break;
3129 /* DWARF 5 values. */
3130 case DW_LANG_OpenCL: printf ("(OpenCL)"); break;
3131 case DW_LANG_Go: printf ("(Go)"); break;
3132 case DW_LANG_Modula3: printf ("(Modula 3)"); break;
3133 case DW_LANG_Haskell: printf ("(Haskell)"); break;
3134 case DW_LANG_C_plus_plus_03: printf ("(C++03)"); break;
3135 case DW_LANG_C_plus_plus_11: printf ("(C++11)"); break;
3136 case DW_LANG_OCaml: printf ("(OCaml)"); break;
3137 case DW_LANG_Rust: printf ("(Rust)"); break;
3138 case DW_LANG_C11: printf ("(C11)"); break;
3139 case DW_LANG_Swift: printf ("(Swift)"); break;
3140 case DW_LANG_Julia: printf ("(Julia)"); break;
3141 case DW_LANG_Dylan: printf ("(Dylan)"); break;
3142 case DW_LANG_C_plus_plus_14: printf ("(C++14)"); break;
3143 case DW_LANG_Fortran03: printf ("(Fortran 03)"); break;
3144 case DW_LANG_Fortran08: printf ("(Fortran 08)"); break;
3145 case DW_LANG_RenderScript: printf ("(RenderScript)"); break;
3146 /* MIPS extension. */
3147 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
3148 /* UPC extension. */
3149 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
3150 default:
3151 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
3152 printf (_("(implementation defined: %#" PRIx64 ")"), uvalue);
3153 else
3154 printf (_("(unknown: %#" PRIx64 ")"), uvalue);
3155 break;
3157 break;
3159 case DW_AT_encoding:
3160 printf ("\t");
3161 switch (uvalue)
3163 case DW_ATE_void: printf ("(void)"); break;
3164 case DW_ATE_address: printf ("(machine address)"); break;
3165 case DW_ATE_boolean: printf ("(boolean)"); break;
3166 case DW_ATE_complex_float: printf ("(complex float)"); break;
3167 case DW_ATE_float: printf ("(float)"); break;
3168 case DW_ATE_signed: printf ("(signed)"); break;
3169 case DW_ATE_signed_char: printf ("(signed char)"); break;
3170 case DW_ATE_unsigned: printf ("(unsigned)"); break;
3171 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
3172 /* DWARF 2.1 values: */
3173 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
3174 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
3175 /* DWARF 3 values: */
3176 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
3177 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
3178 case DW_ATE_edited: printf ("(edited)"); break;
3179 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
3180 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
3181 /* DWARF 4 values: */
3182 case DW_ATE_UTF: printf ("(unicode string)"); break;
3183 /* DWARF 5 values: */
3184 case DW_ATE_UCS: printf ("(UCS)"); break;
3185 case DW_ATE_ASCII: printf ("(ASCII)"); break;
3187 /* HP extensions: */
3188 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
3189 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
3190 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
3191 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
3192 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
3193 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
3194 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
3196 default:
3197 if (uvalue >= DW_ATE_lo_user
3198 && uvalue <= DW_ATE_hi_user)
3199 printf (_("(user defined type)"));
3200 else
3201 printf (_("(unknown type)"));
3202 break;
3204 break;
3206 case DW_AT_accessibility:
3207 printf ("\t");
3208 switch (uvalue)
3210 case DW_ACCESS_public: printf ("(public)"); break;
3211 case DW_ACCESS_protected: printf ("(protected)"); break;
3212 case DW_ACCESS_private: printf ("(private)"); break;
3213 default:
3214 printf (_("(unknown accessibility)"));
3215 break;
3217 break;
3219 case DW_AT_visibility:
3220 printf ("\t");
3221 switch (uvalue)
3223 case DW_VIS_local: printf ("(local)"); break;
3224 case DW_VIS_exported: printf ("(exported)"); break;
3225 case DW_VIS_qualified: printf ("(qualified)"); break;
3226 default: printf (_("(unknown visibility)")); break;
3228 break;
3230 case DW_AT_endianity:
3231 printf ("\t");
3232 switch (uvalue)
3234 case DW_END_default: printf ("(default)"); break;
3235 case DW_END_big: printf ("(big)"); break;
3236 case DW_END_little: printf ("(little)"); break;
3237 default:
3238 if (uvalue >= DW_END_lo_user && uvalue <= DW_END_hi_user)
3239 printf (_("(user specified)"));
3240 else
3241 printf (_("(unknown endianity)"));
3242 break;
3244 break;
3246 case DW_AT_virtuality:
3247 printf ("\t");
3248 switch (uvalue)
3250 case DW_VIRTUALITY_none: printf ("(none)"); break;
3251 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
3252 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
3253 default: printf (_("(unknown virtuality)")); break;
3255 break;
3257 case DW_AT_identifier_case:
3258 printf ("\t");
3259 switch (uvalue)
3261 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
3262 case DW_ID_up_case: printf ("(up_case)"); break;
3263 case DW_ID_down_case: printf ("(down_case)"); break;
3264 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
3265 default: printf (_("(unknown case)")); break;
3267 break;
3269 case DW_AT_calling_convention:
3270 printf ("\t");
3271 switch (uvalue)
3273 case DW_CC_normal: printf ("(normal)"); break;
3274 case DW_CC_program: printf ("(program)"); break;
3275 case DW_CC_nocall: printf ("(nocall)"); break;
3276 case DW_CC_pass_by_reference: printf ("(pass by ref)"); break;
3277 case DW_CC_pass_by_value: printf ("(pass by value)"); break;
3278 case DW_CC_GNU_renesas_sh: printf ("(Rensas SH)"); break;
3279 case DW_CC_GNU_borland_fastcall_i386: printf ("(Borland fastcall i386)"); break;
3280 default:
3281 if (uvalue >= DW_CC_lo_user
3282 && uvalue <= DW_CC_hi_user)
3283 printf (_("(user defined)"));
3284 else
3285 printf (_("(unknown convention)"));
3287 break;
3289 case DW_AT_ordering:
3290 printf ("\t");
3291 switch (uvalue)
3293 case 255:
3294 case -1: printf (_("(undefined)")); break;
3295 case 0: printf ("(row major)"); break;
3296 case 1: printf ("(column major)"); break;
3298 break;
3300 case DW_AT_decimal_sign:
3301 printf ("\t");
3302 switch (uvalue)
3304 case DW_DS_unsigned: printf (_("(unsigned)")); break;
3305 case DW_DS_leading_overpunch: printf (_("(leading overpunch)")); break;
3306 case DW_DS_trailing_overpunch: printf (_("(trailing overpunch)")); break;
3307 case DW_DS_leading_separate: printf (_("(leading separate)")); break;
3308 case DW_DS_trailing_separate: printf (_("(trailing separate)")); break;
3309 default: printf (_("(unrecognised)")); break;
3311 break;
3313 case DW_AT_defaulted:
3314 printf ("\t");
3315 switch (uvalue)
3317 case DW_DEFAULTED_no: printf (_("(no)")); break;
3318 case DW_DEFAULTED_in_class: printf (_("(in class)")); break;
3319 case DW_DEFAULTED_out_of_class: printf (_("(out of class)")); break;
3320 default: printf (_("(unrecognised)")); break;
3322 break;
3324 case DW_AT_discr_list:
3325 printf ("\t");
3326 display_discr_list (form, uvalue, data, level);
3327 break;
3329 case DW_AT_frame_base:
3330 have_frame_base = 1;
3331 /* Fall through. */
3332 case DW_AT_location:
3333 case DW_AT_loclists_base:
3334 case DW_AT_rnglists_base:
3335 case DW_AT_str_offsets_base:
3336 case DW_AT_string_length:
3337 case DW_AT_return_addr:
3338 case DW_AT_data_member_location:
3339 case DW_AT_vtable_elem_location:
3340 case DW_AT_segment:
3341 case DW_AT_static_link:
3342 case DW_AT_use_location:
3343 case DW_AT_call_value:
3344 case DW_AT_GNU_call_site_value:
3345 case DW_AT_call_data_value:
3346 case DW_AT_GNU_call_site_data_value:
3347 case DW_AT_call_target:
3348 case DW_AT_GNU_call_site_target:
3349 case DW_AT_call_target_clobbered:
3350 case DW_AT_GNU_call_site_target_clobbered:
3351 if ((dwarf_version < 4
3352 && (form == DW_FORM_data4 || form == DW_FORM_data8))
3353 || form == DW_FORM_sec_offset
3354 || form == DW_FORM_loclistx)
3356 if (attribute != DW_AT_rnglists_base
3357 && attribute != DW_AT_str_offsets_base)
3358 printf (_(" (location list)"));
3360 /* Fall through. */
3361 case DW_AT_allocated:
3362 case DW_AT_associated:
3363 case DW_AT_data_location:
3364 case DW_AT_stride:
3365 case DW_AT_upper_bound:
3366 case DW_AT_lower_bound:
3367 case DW_AT_rank:
3368 if (block_start)
3370 int need_frame_base;
3372 printf ("\t(");
3373 need_frame_base = decode_location_expression (block_start,
3374 pointer_size,
3375 offset_size,
3376 dwarf_version,
3377 uvalue,
3378 cu_offset, section);
3379 printf (")");
3380 if (need_frame_base && !have_frame_base)
3381 printf (_(" [without DW_AT_frame_base]"));
3383 break;
3385 case DW_AT_data_bit_offset:
3386 case DW_AT_byte_size:
3387 case DW_AT_bit_size:
3388 case DW_AT_string_length_byte_size:
3389 case DW_AT_string_length_bit_size:
3390 case DW_AT_bit_stride:
3391 if (form == DW_FORM_exprloc)
3393 printf ("\t(");
3394 (void) decode_location_expression (block_start, pointer_size,
3395 offset_size, dwarf_version,
3396 uvalue, cu_offset, section);
3397 printf (")");
3399 break;
3401 case DW_AT_import:
3403 unsigned long abbrev_number;
3404 abbrev_entry *entry;
3406 entry = get_type_abbrev_from_form (form, uvalue, cu_offset, end,
3407 section, & abbrev_number, NULL, NULL);
3408 if (entry == NULL)
3410 if (form != DW_FORM_GNU_ref_alt)
3411 warn (_("Offset %#" PRIx64 " used as value for DW_AT_import attribute of DIE at offset %#tx is too big.\n"),
3412 uvalue,
3413 orig_data - section->start);
3415 else
3417 printf (_("\t[Abbrev Number: %ld"), abbrev_number);
3418 printf (" (%s)", get_TAG_name (entry->tag));
3419 printf ("]");
3422 break;
3424 default:
3425 break;
3428 return data;
3431 static unsigned char *
3432 read_and_display_attr (unsigned long attribute,
3433 unsigned long form,
3434 int64_t implicit_const,
3435 unsigned char *start,
3436 unsigned char *data,
3437 unsigned char *end,
3438 uint64_t cu_offset,
3439 uint64_t pointer_size,
3440 uint64_t offset_size,
3441 int dwarf_version,
3442 debug_info *debug_info_p,
3443 int do_loc,
3444 struct dwarf_section *section,
3445 struct cu_tu_set *this_set,
3446 int level)
3448 if (!do_loc)
3449 printf (" %-18s:", get_AT_name (attribute));
3450 data = read_and_display_attr_value (attribute, form, implicit_const,
3451 start, data, end,
3452 cu_offset, pointer_size, offset_size,
3453 dwarf_version, debug_info_p,
3454 do_loc, section, this_set, ' ', level);
3455 if (!do_loc)
3456 printf ("\n");
3457 return data;
3460 /* Like load_debug_section, but if the ordinary call fails, and we are
3461 following debug links, then attempt to load the requested section
3462 from one of the separate debug info files. */
3464 static bool
3465 load_debug_section_with_follow (enum dwarf_section_display_enum sec_enum,
3466 void * handle)
3468 if (load_debug_section (sec_enum, handle))
3470 if (debug_displays[sec_enum].section.filename == NULL)
3472 /* See if we can associate a filename with this section. */
3473 separate_info * i;
3475 for (i = first_separate_info; i != NULL; i = i->next)
3476 if (i->handle == handle)
3478 debug_displays[sec_enum].section.filename = i->filename;
3479 break;
3483 return true;
3486 if (do_follow_links)
3488 separate_info * i;
3490 for (i = first_separate_info; i != NULL; i = i->next)
3492 if (load_debug_section (sec_enum, i->handle))
3494 debug_displays[sec_enum].section.filename = i->filename;
3496 /* FIXME: We should check to see if any of the remaining debug info
3497 files also contain this section, and, umm, do something about it. */
3498 return true;
3503 return false;
3506 static void
3507 introduce (struct dwarf_section * section, bool raw)
3509 if (raw)
3511 if (do_follow_links && section->filename)
3512 printf (_("Raw dump of debug contents of section %s (loaded from %s):\n\n"),
3513 section->name, section->filename);
3514 else
3515 printf (_("Raw dump of debug contents of section %s:\n\n"), section->name);
3517 else
3519 if (do_follow_links && section->filename)
3520 printf (_("Contents of the %s section (loaded from %s):\n\n"),
3521 section->name, section->filename);
3522 else
3523 printf (_("Contents of the %s section:\n\n"), section->name);
3527 /* Free memory allocated for one unit in debug_information. */
3529 static void
3530 free_debug_information (debug_info *ent)
3532 if (ent->max_loc_offsets)
3534 free (ent->loc_offsets);
3535 free (ent->loc_views);
3536 free (ent->have_frame_base);
3538 if (ent->max_range_lists)
3539 free (ent->range_lists);
3542 /* Process the contents of a .debug_info section.
3543 If do_loc is TRUE then we are scanning for location lists and dwo tags
3544 and we do not want to display anything to the user.
3545 If do_types is TRUE, we are processing a .debug_types section instead of
3546 a .debug_info section.
3547 The information displayed is restricted by the values in DWARF_START_DIE
3548 and DWARF_CUTOFF_LEVEL.
3549 Returns TRUE upon success. Otherwise an error or warning message is
3550 printed and FALSE is returned. */
3552 static bool
3553 process_debug_info (struct dwarf_section * section,
3554 void *file,
3555 enum dwarf_section_display_enum abbrev_sec,
3556 bool do_loc,
3557 bool do_types)
3559 unsigned char *start = section->start;
3560 unsigned char *end = start + section->size;
3561 unsigned char *section_begin;
3562 unsigned int unit;
3563 unsigned int num_units = 0;
3565 /* First scan the section to get the number of comp units.
3566 Length sanity checks are done here. */
3567 for (section_begin = start, num_units = 0; section_begin < end;
3568 num_units ++)
3570 uint64_t length;
3572 /* Read the first 4 bytes. For a 32-bit DWARF section, this
3573 will be the length. For a 64-bit DWARF section, it'll be
3574 the escape code 0xffffffff followed by an 8 byte length. */
3575 SAFE_BYTE_GET_AND_INC (length, section_begin, 4, end);
3577 if (length == 0xffffffff)
3578 SAFE_BYTE_GET_AND_INC (length, section_begin, 8, end);
3579 else if (length >= 0xfffffff0 && length < 0xffffffff)
3581 warn (_("Reserved length value (%#" PRIx64 ") found in section %s\n"),
3582 length, section->name);
3583 return false;
3586 /* Negative values are illegal, they may even cause infinite
3587 looping. This can happen if we can't accurately apply
3588 relocations to an object file, or if the file is corrupt. */
3589 if (length > (size_t) (end - section_begin))
3591 warn (_("Corrupt unit length (got %#" PRIx64
3592 " expected at most %#tx) in section %s\n"),
3593 length, end - section_begin, section->name);
3594 return false;
3596 section_begin += length;
3599 if (num_units == 0)
3601 error (_("No comp units in %s section ?\n"), section->name);
3602 return false;
3605 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
3606 && num_debug_info_entries == 0
3607 && ! do_types)
3610 /* Then allocate an array to hold the information. */
3611 debug_information = (debug_info *) cmalloc (num_units,
3612 sizeof (* debug_information));
3613 if (debug_information == NULL)
3615 error (_("Not enough memory for a debug info array of %u entries\n"),
3616 num_units);
3617 alloc_num_debug_info_entries = num_debug_info_entries = 0;
3618 return false;
3621 /* PR 17531: file: 92ca3797.
3622 We cannot rely upon the debug_information array being initialised
3623 before it is used. A corrupt file could easily contain references
3624 to a unit for which information has not been made available. So
3625 we ensure that the array is zeroed here. */
3626 memset (debug_information, 0, num_units * sizeof (*debug_information));
3628 alloc_num_debug_info_entries = num_units;
3631 if (!do_loc)
3633 load_debug_section_with_follow (str, file);
3634 load_debug_section_with_follow (line_str, file);
3635 load_debug_section_with_follow (str_dwo, file);
3636 load_debug_section_with_follow (str_index, file);
3637 load_debug_section_with_follow (str_index_dwo, file);
3638 load_debug_section_with_follow (debug_addr, file);
3641 load_debug_section_with_follow (abbrev_sec, file);
3642 load_debug_section_with_follow (loclists, file);
3643 load_debug_section_with_follow (rnglists, file);
3644 load_debug_section_with_follow (loclists_dwo, file);
3645 load_debug_section_with_follow (rnglists_dwo, file);
3647 if (debug_displays [abbrev_sec].section.start == NULL)
3649 warn (_("Unable to locate %s section!\n"),
3650 debug_displays [abbrev_sec].section.uncompressed_name);
3651 return false;
3654 if (!do_loc && dwarf_start_die == 0)
3655 introduce (section, false);
3657 free_all_abbrevs ();
3659 /* In order to be able to resolve DW_FORM_ref_addr forms we need
3660 to load *all* of the abbrevs for all CUs in this .debug_info
3661 section. This does effectively mean that we (partially) read
3662 every CU header twice. */
3663 for (section_begin = start; start < end;)
3665 DWARF2_Internal_CompUnit compunit;
3666 unsigned char *hdrptr;
3667 uint64_t abbrev_base;
3668 size_t abbrev_size;
3669 uint64_t cu_offset;
3670 unsigned int offset_size;
3671 struct cu_tu_set *this_set;
3672 unsigned char *end_cu;
3674 hdrptr = start;
3675 cu_offset = start - section_begin;
3677 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3679 if (compunit.cu_length == 0xffffffff)
3681 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3682 offset_size = 8;
3684 else
3685 offset_size = 4;
3686 end_cu = hdrptr + compunit.cu_length;
3688 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3690 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3692 if (compunit.cu_version < 5)
3694 compunit.cu_unit_type = DW_UT_compile;
3695 /* Initialize it due to a false compiler warning. */
3696 compunit.cu_pointer_size = -1;
3698 else
3700 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3701 do_types = (compunit.cu_unit_type == DW_UT_type);
3703 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3706 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size,
3707 end_cu);
3709 if (compunit.cu_unit_type == DW_UT_split_compile
3710 || compunit.cu_unit_type == DW_UT_skeleton)
3712 uint64_t dwo_id;
3713 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3716 if (this_set == NULL)
3718 abbrev_base = 0;
3719 abbrev_size = debug_displays [abbrev_sec].section.size;
3721 else
3723 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3724 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3727 abbrev_list *list;
3728 abbrev_list *free_list;
3729 list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
3730 abbrev_base, abbrev_size,
3731 compunit.cu_abbrev_offset,
3732 &free_list);
3733 start = end_cu;
3734 if (list != NULL && list->first_abbrev != NULL)
3735 record_abbrev_list_for_cu (cu_offset, start - section_begin,
3736 list, free_list);
3737 else if (free_list != NULL)
3738 free_abbrev_list (free_list);
3741 for (start = section_begin, unit = 0; start < end; unit++)
3743 DWARF2_Internal_CompUnit compunit;
3744 unsigned char *hdrptr;
3745 unsigned char *tags;
3746 int level, last_level, saved_level;
3747 uint64_t cu_offset;
3748 unsigned int offset_size;
3749 uint64_t signature = 0;
3750 uint64_t type_offset = 0;
3751 struct cu_tu_set *this_set;
3752 uint64_t abbrev_base;
3753 size_t abbrev_size;
3754 unsigned char *end_cu;
3756 hdrptr = start;
3757 cu_offset = start - section_begin;
3759 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3761 if (compunit.cu_length == 0xffffffff)
3763 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3764 offset_size = 8;
3766 else
3767 offset_size = 4;
3768 end_cu = hdrptr + compunit.cu_length;
3770 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3772 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3774 if (compunit.cu_version < 5)
3776 compunit.cu_unit_type = DW_UT_compile;
3777 /* Initialize it due to a false compiler warning. */
3778 compunit.cu_pointer_size = -1;
3780 else
3782 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3783 do_types = (compunit.cu_unit_type == DW_UT_type);
3785 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3788 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end_cu);
3790 if (this_set == NULL)
3792 abbrev_base = 0;
3793 abbrev_size = debug_displays [abbrev_sec].section.size;
3795 else
3797 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3798 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3801 if (compunit.cu_version < 5)
3802 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3804 bool do_dwo_id = false;
3805 uint64_t dwo_id = 0;
3806 if (compunit.cu_unit_type == DW_UT_split_compile
3807 || compunit.cu_unit_type == DW_UT_skeleton)
3809 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3810 do_dwo_id = true;
3813 /* PR 17512: file: 001-108546-0.001:0.1. */
3814 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3816 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3817 compunit.cu_pointer_size, offset_size);
3818 compunit.cu_pointer_size = offset_size;
3821 if (do_types)
3823 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, end_cu);
3824 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end_cu);
3827 if (dwarf_start_die >= (size_t) (end_cu - section_begin))
3829 start = end_cu;
3830 continue;
3833 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
3834 && num_debug_info_entries == 0
3835 && alloc_num_debug_info_entries > unit
3836 && ! do_types)
3838 free_debug_information (&debug_information[unit]);
3839 memset (&debug_information[unit], 0, sizeof (*debug_information));
3840 debug_information[unit].pointer_size = compunit.cu_pointer_size;
3841 debug_information[unit].offset_size = offset_size;
3842 debug_information[unit].dwarf_version = compunit.cu_version;
3843 debug_information[unit].cu_offset = cu_offset;
3844 debug_information[unit].addr_base = DEBUG_INFO_UNAVAILABLE;
3845 debug_information[unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
3848 if (!do_loc && dwarf_start_die == 0)
3850 printf (_(" Compilation Unit @ offset %#" PRIx64 ":\n"),
3851 cu_offset);
3852 printf (_(" Length: %#" PRIx64 " (%s)\n"),
3853 compunit.cu_length,
3854 offset_size == 8 ? "64-bit" : "32-bit");
3855 printf (_(" Version: %d\n"), compunit.cu_version);
3856 if (compunit.cu_version >= 5)
3858 const char *name = get_DW_UT_name (compunit.cu_unit_type);
3860 printf (_(" Unit Type: %s (%x)\n"),
3861 null_name (name),
3862 compunit.cu_unit_type);
3864 printf (_(" Abbrev Offset: %#" PRIx64 "\n"),
3865 compunit.cu_abbrev_offset);
3866 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
3867 if (do_types)
3869 printf (_(" Signature: %#" PRIx64 "\n"), signature);
3870 printf (_(" Type Offset: %#" PRIx64 "\n"), type_offset);
3872 if (do_dwo_id)
3873 printf (_(" DWO ID: %#" PRIx64 "\n"), dwo_id);
3874 if (this_set != NULL)
3876 uint64_t *offsets = this_set->section_offsets;
3877 size_t *sizes = this_set->section_sizes;
3879 printf (_(" Section contributions:\n"));
3880 printf (_(" .debug_abbrev.dwo: %#" PRIx64 " %#zx\n"),
3881 offsets[DW_SECT_ABBREV], sizes[DW_SECT_ABBREV]);
3882 printf (_(" .debug_line.dwo: %#" PRIx64 " %#zx\n"),
3883 offsets[DW_SECT_LINE], sizes[DW_SECT_LINE]);
3884 printf (_(" .debug_loc.dwo: %#" PRIx64 " %#zx\n"),
3885 offsets[DW_SECT_LOC], sizes[DW_SECT_LOC]);
3886 printf (_(" .debug_str_offsets.dwo: %#" PRIx64 " %#zx\n"),
3887 offsets[DW_SECT_STR_OFFSETS], sizes[DW_SECT_STR_OFFSETS]);
3891 tags = hdrptr;
3892 start = end_cu;
3894 if (compunit.cu_version < 2 || compunit.cu_version > 5)
3896 warn (_("CU at offset %#" PRIx64 " contains corrupt or "
3897 "unsupported version number: %d.\n"),
3898 cu_offset, compunit.cu_version);
3899 continue;
3902 if (compunit.cu_unit_type != DW_UT_compile
3903 && compunit.cu_unit_type != DW_UT_partial
3904 && compunit.cu_unit_type != DW_UT_type
3905 && compunit.cu_unit_type != DW_UT_split_compile
3906 && compunit.cu_unit_type != DW_UT_skeleton)
3908 warn (_("CU at offset %#" PRIx64 " contains corrupt or "
3909 "unsupported unit type: %d.\n"),
3910 cu_offset, compunit.cu_unit_type);
3911 continue;
3914 /* Process the abbrevs used by this compilation unit. */
3915 abbrev_list *list;
3916 list = find_and_process_abbrev_set (&debug_displays[abbrev_sec].section,
3917 abbrev_base, abbrev_size,
3918 compunit.cu_abbrev_offset, NULL);
3919 level = 0;
3920 last_level = level;
3921 saved_level = -1;
3922 while (tags < start)
3924 unsigned long abbrev_number;
3925 unsigned long die_offset;
3926 abbrev_entry *entry;
3927 abbrev_attr *attr;
3928 int do_printing = 1;
3930 die_offset = tags - section_begin;
3932 READ_ULEB (abbrev_number, tags, start);
3934 /* A null DIE marks the end of a list of siblings or it may also be
3935 a section padding. */
3936 if (abbrev_number == 0)
3938 /* Check if it can be a section padding for the last CU. */
3939 if (level == 0 && start == end)
3941 unsigned char *chk;
3943 for (chk = tags; chk < start; chk++)
3944 if (*chk != 0)
3945 break;
3946 if (chk == start)
3947 break;
3950 if (!do_loc && die_offset >= dwarf_start_die
3951 && (dwarf_cutoff_level == -1
3952 || level < dwarf_cutoff_level))
3953 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
3954 level, die_offset);
3956 --level;
3957 if (level < 0)
3959 static unsigned num_bogus_warns = 0;
3961 if (num_bogus_warns < 3)
3963 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
3964 die_offset, section->name);
3965 num_bogus_warns ++;
3966 if (num_bogus_warns == 3)
3967 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
3970 if (dwarf_start_die != 0 && level < saved_level)
3971 return true;
3972 continue;
3975 if (!do_loc)
3977 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
3978 do_printing = 0;
3979 else
3981 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
3982 saved_level = level;
3983 do_printing = (dwarf_cutoff_level == -1
3984 || level < dwarf_cutoff_level);
3985 if (do_printing)
3986 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
3987 level, die_offset, abbrev_number);
3988 else if (dwarf_cutoff_level == -1
3989 || last_level < dwarf_cutoff_level)
3990 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
3991 last_level = level;
3995 /* Scan through the abbreviation list until we reach the
3996 correct entry. */
3997 entry = NULL;
3998 if (list != NULL)
3999 for (entry = list->first_abbrev; entry != NULL; entry = entry->next)
4000 if (entry->number == abbrev_number)
4001 break;
4003 if (entry == NULL)
4005 if (!do_loc && do_printing)
4007 printf ("\n");
4008 fflush (stdout);
4010 warn (_("DIE at offset %#lx refers to abbreviation number %lu which does not exist\n"),
4011 die_offset, abbrev_number);
4012 return false;
4015 if (!do_loc && do_printing)
4016 printf (" (%s)\n", get_TAG_name (entry->tag));
4018 switch (entry->tag)
4020 default:
4021 need_base_address = 0;
4022 break;
4023 case DW_TAG_compile_unit:
4024 case DW_TAG_skeleton_unit:
4025 need_base_address = 1;
4026 need_dwo_info = do_loc;
4027 break;
4028 case DW_TAG_entry_point:
4029 case DW_TAG_subprogram:
4030 need_base_address = 0;
4031 /* Assuming that there is no DW_AT_frame_base. */
4032 have_frame_base = 0;
4033 break;
4036 debug_info *debug_info_p =
4037 (debug_information && unit < alloc_num_debug_info_entries)
4038 ? debug_information + unit : NULL;
4040 assert (!debug_info_p
4041 || (debug_info_p->num_loc_offsets
4042 == debug_info_p->num_loc_views));
4044 for (attr = entry->first_attr;
4045 attr && attr->attribute;
4046 attr = attr->next)
4048 if (! do_loc && do_printing)
4049 /* Show the offset from where the tag was extracted. */
4050 printf (" <%tx>", tags - section_begin);
4051 tags = read_and_display_attr (attr->attribute,
4052 attr->form,
4053 attr->implicit_const,
4054 section_begin,
4055 tags,
4056 start,
4057 cu_offset,
4058 compunit.cu_pointer_size,
4059 offset_size,
4060 compunit.cu_version,
4061 debug_info_p,
4062 do_loc || ! do_printing,
4063 section,
4064 this_set,
4065 level);
4068 /* If a locview attribute appears before a location one,
4069 make sure we don't associate it with an earlier
4070 loclist. */
4071 if (debug_info_p)
4072 switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
4074 case 1:
4075 debug_info_p->loc_views [debug_info_p->num_loc_views] = -1;
4076 debug_info_p->num_loc_views++;
4077 assert (debug_info_p->num_loc_views
4078 == debug_info_p->num_loc_offsets);
4079 break;
4081 case 0:
4082 break;
4084 case -1:
4085 warn(_("DIE has locviews without loclist\n"));
4086 debug_info_p->num_loc_views--;
4087 break;
4089 default:
4090 assert (0);
4093 if (entry->children)
4094 ++level;
4096 if (list != NULL)
4097 free_abbrev_list (list);
4100 /* Set num_debug_info_entries here so that it can be used to check if
4101 we need to process .debug_loc and .debug_ranges sections. */
4102 if ((do_loc || do_debug_loc || do_debug_ranges || do_debug_info)
4103 && num_debug_info_entries == 0
4104 && ! do_types)
4106 if (num_units > alloc_num_debug_info_entries)
4107 num_debug_info_entries = alloc_num_debug_info_entries;
4108 else
4109 num_debug_info_entries = num_units;
4112 if (!do_loc)
4113 printf ("\n");
4115 return true;
4118 /* Locate and scan the .debug_info section in the file and record the pointer
4119 sizes and offsets for the compilation units in it. Usually an executable
4120 will have just one pointer size, but this is not guaranteed, and so we try
4121 not to make any assumptions. Returns zero upon failure, or the number of
4122 compilation units upon success. */
4124 static unsigned int
4125 load_debug_info (void * file)
4127 /* If we have already tried and failed to load the .debug_info
4128 section then do not bother to repeat the task. */
4129 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4130 return 0;
4132 /* If we already have the information there is nothing else to do. */
4133 if (num_debug_info_entries > 0)
4134 return num_debug_info_entries;
4136 /* If this is a DWARF package file, load the CU and TU indexes. */
4137 (void) load_cu_tu_indexes (file);
4139 if (load_debug_section_with_follow (info, file)
4140 && process_debug_info (&debug_displays [info].section, file, abbrev, true, false))
4141 return num_debug_info_entries;
4143 if (load_debug_section_with_follow (info_dwo, file)
4144 && process_debug_info (&debug_displays [info_dwo].section, file,
4145 abbrev_dwo, true, false))
4146 return num_debug_info_entries;
4148 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
4149 return 0;
4152 /* Read a DWARF .debug_line section header starting at DATA.
4153 Upon success returns an updated DATA pointer and the LINFO
4154 structure and the END_OF_SEQUENCE pointer will be filled in.
4155 Otherwise returns NULL. */
4157 static unsigned char *
4158 read_debug_line_header (struct dwarf_section * section,
4159 unsigned char * data,
4160 unsigned char * end,
4161 DWARF2_Internal_LineInfo * linfo,
4162 unsigned char ** end_of_sequence)
4164 unsigned char *hdrptr;
4166 /* Extract information from the Line Number Program Header.
4167 (section 6.2.4 in the Dwarf3 doc). */
4168 hdrptr = data;
4170 /* Get and check the length of the block. */
4171 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
4173 if (linfo->li_length == 0xffffffff)
4175 /* This section is 64-bit DWARF 3. */
4176 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
4177 linfo->li_offset_size = 8;
4179 else
4180 linfo->li_offset_size = 4;
4182 if (linfo->li_length > (size_t) (end - hdrptr))
4184 /* If the length field has a relocation against it, then we should
4185 not complain if it is inaccurate (and probably negative). This
4186 happens in object files when the .debug_line section is actually
4187 comprised of several different .debug_line.* sections, (some of
4188 which may be removed by linker garbage collection), and a relocation
4189 is used to compute the correct length once that is done. */
4190 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
4192 linfo->li_length = end - hdrptr;
4194 else
4196 warn (_("The length field (%#" PRIx64 ")"
4197 " in the debug_line header is wrong"
4198 " - the section is too small\n"),
4199 linfo->li_length);
4200 return NULL;
4203 end = hdrptr + linfo->li_length;
4205 /* Get and check the version number. */
4206 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
4208 if (linfo->li_version != 2
4209 && linfo->li_version != 3
4210 && linfo->li_version != 4
4211 && linfo->li_version != 5)
4213 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
4214 "is currently supported.\n"));
4215 return NULL;
4218 if (linfo->li_version >= 5)
4220 SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
4222 SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
4223 if (linfo->li_segment_size != 0)
4225 warn (_("The %s section contains "
4226 "unsupported segment selector size: %d.\n"),
4227 section->name, linfo->li_segment_size);
4228 return NULL;
4232 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
4233 linfo->li_offset_size, end);
4234 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
4236 if (linfo->li_version >= 4)
4238 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
4240 if (linfo->li_max_ops_per_insn == 0)
4242 warn (_("Invalid maximum operations per insn.\n"));
4243 return NULL;
4246 else
4247 linfo->li_max_ops_per_insn = 1;
4249 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
4250 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
4251 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
4252 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
4254 *end_of_sequence = end;
4255 return hdrptr;
4258 static unsigned char *
4259 display_formatted_table (unsigned char *data,
4260 unsigned char *start,
4261 unsigned char *end,
4262 const DWARF2_Internal_LineInfo *linfo,
4263 struct dwarf_section *section,
4264 bool is_dir)
4266 unsigned char *format_start, format_count, *format, formati;
4267 uint64_t data_count, datai;
4268 unsigned int namepass, last_entry = 0;
4269 const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
4271 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4272 if (do_checks && format_count > 5)
4273 warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
4274 table_name, format_count);
4276 format_start = data;
4277 for (formati = 0; formati < format_count; formati++)
4279 SKIP_ULEB (data, end);
4280 SKIP_ULEB (data, end);
4281 if (data >= end)
4283 warn (_("%s: Corrupt format description entry\n"), table_name);
4284 return data;
4288 READ_ULEB (data_count, data, end);
4289 if (data_count == 0)
4291 printf (_("\n The %s is empty.\n"), table_name);
4292 return data;
4294 else if (data >= end)
4296 warn (_("%s: Corrupt entry count - expected %#" PRIx64
4297 " but none found\n"), table_name, data_count);
4298 return data;
4301 else if (format_count == 0)
4303 warn (_("%s: format count is zero, but the table is not empty\n"),
4304 table_name);
4305 return end;
4308 printf (_("\n The %s (offset %#tx, lines %" PRIu64 ", columns %u):\n"),
4309 table_name, data - start, data_count, format_count);
4311 printf (_(" Entry"));
4312 /* Delay displaying name as the last entry for better screen layout. */
4313 for (namepass = 0; namepass < 2; namepass++)
4315 format = format_start;
4316 for (formati = 0; formati < format_count; formati++)
4318 uint64_t content_type;
4320 READ_ULEB (content_type, format, end);
4321 if ((content_type == DW_LNCT_path) == (namepass == 1))
4322 switch (content_type)
4324 case DW_LNCT_path:
4325 printf (_("\tName"));
4326 break;
4327 case DW_LNCT_directory_index:
4328 printf (_("\tDir"));
4329 break;
4330 case DW_LNCT_timestamp:
4331 printf (_("\tTime"));
4332 break;
4333 case DW_LNCT_size:
4334 printf (_("\tSize"));
4335 break;
4336 case DW_LNCT_MD5:
4337 printf (_("\tMD5\t\t\t"));
4338 break;
4339 default:
4340 printf (_("\t(Unknown format content type %" PRIu64 ")"),
4341 content_type);
4343 SKIP_ULEB (format, end);
4346 putchar ('\n');
4348 for (datai = 0; datai < data_count; datai++)
4350 unsigned char *datapass = data;
4352 printf (" %d", last_entry++);
4353 /* Delay displaying name as the last entry for better screen layout. */
4354 for (namepass = 0; namepass < 2; namepass++)
4356 format = format_start;
4357 data = datapass;
4358 for (formati = 0; formati < format_count; formati++)
4360 uint64_t content_type, form;
4362 READ_ULEB (content_type, format, end);
4363 READ_ULEB (form, format, end);
4364 data = read_and_display_attr_value (0, form, 0, start, data, end,
4365 0, 0, linfo->li_offset_size,
4366 linfo->li_version, NULL,
4367 ((content_type == DW_LNCT_path) != (namepass == 1)),
4368 section, NULL, '\t', -1);
4372 if (data >= end && (datai < data_count - 1))
4374 warn (_("\n%s: Corrupt entries list\n"), table_name);
4375 return data;
4377 putchar ('\n');
4379 return data;
4382 static int
4383 display_debug_sup (struct dwarf_section * section,
4384 void * file ATTRIBUTE_UNUSED)
4386 unsigned char * start = section->start;
4387 unsigned char * end = section->start + section->size;
4388 unsigned int version;
4389 char is_supplementary;
4390 const unsigned char * sup_filename;
4391 size_t sup_filename_len;
4392 unsigned int num_read;
4393 int status;
4394 uint64_t checksum_len;
4397 introduce (section, true);
4398 if (section->size < 4)
4400 error (_("corrupt .debug_sup section: size is too small\n"));
4401 return 0;
4404 /* Read the data. */
4405 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
4406 if (version < 5)
4407 warn (_("corrupt .debug_sup section: version < 5"));
4409 SAFE_BYTE_GET_AND_INC (is_supplementary, start, 1, end);
4410 if (is_supplementary != 0 && is_supplementary != 1)
4411 warn (_("corrupt .debug_sup section: is_supplementary not 0 or 1\n"));
4413 sup_filename = start;
4414 if (is_supplementary && sup_filename[0] != 0)
4415 warn (_("corrupt .debug_sup section: filename not empty in supplementary section\n"));
4417 sup_filename_len = strnlen ((const char *) start, end - start);
4418 if (sup_filename_len == (size_t) (end - start))
4420 error (_("corrupt .debug_sup section: filename is not NUL terminated\n"));
4421 return 0;
4423 start += sup_filename_len + 1;
4425 checksum_len = read_leb128 (start, end, false /* unsigned */, & num_read, & status);
4426 if (status)
4428 error (_("corrupt .debug_sup section: bad LEB128 field for checksum length\n"));
4429 checksum_len = 0;
4431 start += num_read;
4432 if (checksum_len > (size_t) (end - start))
4434 error (_("corrupt .debug_sup section: checksum length is longer than the remaining section length\n"));
4435 checksum_len = end - start;
4437 else if (checksum_len < (size_t) (end - start))
4439 warn (_("corrupt .debug_sup section: there are %#" PRIx64
4440 " extra, unused bytes at the end of the section\n"),
4441 (end - start) - checksum_len);
4444 printf (_(" Version: %u\n"), version);
4445 printf (_(" Is Supp: %u\n"), is_supplementary);
4446 printf (_(" Filename: %s\n"), sup_filename);
4447 printf (_(" Checksum Len: %" PRIu64 "\n"), checksum_len);
4448 if (checksum_len > 0)
4450 printf (_(" Checksum: "));
4451 while (checksum_len--)
4452 printf ("0x%x ", * start++ );
4453 printf ("\n");
4455 return 1;
4458 static int
4459 display_debug_lines_raw (struct dwarf_section * section,
4460 unsigned char * data,
4461 unsigned char * end,
4462 void * file)
4464 unsigned char *start = section->start;
4465 int verbose_view = 0;
4467 introduce (section, true);
4469 while (data < end)
4471 static DWARF2_Internal_LineInfo saved_linfo;
4472 DWARF2_Internal_LineInfo linfo;
4473 unsigned char *standard_opcodes;
4474 unsigned char *end_of_sequence;
4475 int i;
4477 if (startswith (section->name, ".debug_line.")
4478 /* Note: the following does not apply to .debug_line.dwo sections.
4479 These are full debug_line sections. */
4480 && strcmp (section->name, ".debug_line.dwo") != 0)
4482 /* Sections named .debug_line.<foo> are fragments of a .debug_line
4483 section containing just the Line Number Statements. They are
4484 created by the assembler and intended to be used alongside gcc's
4485 -ffunction-sections command line option. When the linker's
4486 garbage collection decides to discard a .text.<foo> section it
4487 can then also discard the line number information in .debug_line.<foo>.
4489 Since the section is a fragment it does not have the details
4490 needed to fill out a LineInfo structure, so instead we use the
4491 details from the last full debug_line section that we processed. */
4492 end_of_sequence = end;
4493 standard_opcodes = NULL;
4494 linfo = saved_linfo;
4495 /* PR 17531: file: 0522b371. */
4496 if (linfo.li_line_range == 0)
4498 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4499 return 0;
4501 reset_state_machine (linfo.li_default_is_stmt);
4503 else
4505 unsigned char * hdrptr;
4507 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4508 & end_of_sequence)) == NULL)
4509 return 0;
4511 printf (_(" Offset: %#tx\n"), data - start);
4512 printf (_(" Length: %" PRId64 "\n"), linfo.li_length);
4513 printf (_(" DWARF Version: %d\n"), linfo.li_version);
4514 if (linfo.li_version >= 5)
4516 printf (_(" Address size (bytes): %d\n"), linfo.li_address_size);
4517 printf (_(" Segment selector (bytes): %d\n"), linfo.li_segment_size);
4519 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
4520 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
4521 if (linfo.li_version >= 4)
4522 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
4523 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
4524 printf (_(" Line Base: %d\n"), linfo.li_line_base);
4525 printf (_(" Line Range: %d\n"), linfo.li_line_range);
4526 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
4528 /* PR 17512: file: 1665-6428-0.004. */
4529 if (linfo.li_line_range == 0)
4531 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4532 linfo.li_line_range = 1;
4535 reset_state_machine (linfo.li_default_is_stmt);
4537 /* Display the contents of the Opcodes table. */
4538 standard_opcodes = hdrptr;
4540 /* PR 17512: file: 002-417945-0.004. */
4541 if (standard_opcodes + linfo.li_opcode_base >= end)
4543 warn (_("Line Base extends beyond end of section\n"));
4544 return 0;
4547 printf (_("\n Opcodes:\n"));
4549 for (i = 1; i < linfo.li_opcode_base; i++)
4550 printf (ngettext (" Opcode %d has %d arg\n",
4551 " Opcode %d has %d args\n",
4552 standard_opcodes[i - 1]),
4553 i, standard_opcodes[i - 1]);
4555 /* Display the contents of the Directory table. */
4556 data = standard_opcodes + linfo.li_opcode_base - 1;
4558 if (linfo.li_version >= 5)
4560 load_debug_section_with_follow (line_str, file);
4562 data = display_formatted_table (data, start, end, &linfo, section,
4563 true);
4564 data = display_formatted_table (data, start, end, &linfo, section,
4565 false);
4567 else
4569 if (*data == 0)
4570 printf (_("\n The Directory Table is empty.\n"));
4571 else
4573 unsigned int last_dir_entry = 0;
4575 printf (_("\n The Directory Table (offset %#tx):\n"),
4576 data - start);
4578 while (data < end && *data != 0)
4580 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
4582 data += strnlen ((char *) data, end - data);
4583 if (data < end)
4584 data++;
4587 /* PR 17512: file: 002-132094-0.004. */
4588 if (data >= end - 1)
4589 break;
4592 /* Skip the NUL at the end of the table. */
4593 if (data < end)
4594 data++;
4596 /* Display the contents of the File Name table. */
4597 if (data >= end || *data == 0)
4598 printf (_("\n The File Name Table is empty.\n"));
4599 else
4601 printf (_("\n The File Name Table (offset %#tx):\n"),
4602 data - start);
4603 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
4605 while (data < end && *data != 0)
4607 unsigned char *name;
4608 uint64_t val;
4610 printf (" %d\t", ++state_machine_regs.last_file_entry);
4611 name = data;
4612 data += strnlen ((char *) data, end - data);
4613 if (data < end)
4614 data++;
4616 READ_ULEB (val, data, end);
4617 printf ("%" PRIu64 "\t", val);
4618 READ_ULEB (val, data, end);
4619 printf ("%" PRIu64 "\t", val);
4620 READ_ULEB (val, data, end);
4621 printf ("%" PRIu64 "\t", val);
4622 printf ("%.*s\n", (int)(end - name), name);
4624 if (data >= end)
4626 warn (_("Corrupt file name table entry\n"));
4627 break;
4632 /* Skip the NUL at the end of the table. */
4633 if (data < end)
4634 data++;
4637 putchar ('\n');
4638 saved_linfo = linfo;
4641 /* Now display the statements. */
4642 if (data >= end_of_sequence)
4643 printf (_(" No Line Number Statements.\n"));
4644 else
4646 printf (_(" Line Number Statements:\n"));
4648 while (data < end_of_sequence)
4650 unsigned char op_code;
4651 int64_t adv;
4652 uint64_t uladv;
4654 printf (" [0x%08tx]", data - start);
4656 op_code = *data++;
4658 if (op_code >= linfo.li_opcode_base)
4660 op_code -= linfo.li_opcode_base;
4661 uladv = (op_code / linfo.li_line_range);
4662 if (linfo.li_max_ops_per_insn == 1)
4664 uladv *= linfo.li_min_insn_length;
4665 state_machine_regs.address += uladv;
4666 if (uladv)
4667 state_machine_regs.view = 0;
4668 printf (_(" Special opcode %d: "
4669 "advance Address by %" PRIu64
4670 " to %#" PRIx64 "%s"),
4671 op_code, uladv, state_machine_regs.address,
4672 verbose_view && uladv
4673 ? _(" (reset view)") : "");
4675 else
4677 unsigned addrdelta
4678 = ((state_machine_regs.op_index + uladv)
4679 / linfo.li_max_ops_per_insn)
4680 * linfo.li_min_insn_length;
4682 state_machine_regs.address += addrdelta;
4683 state_machine_regs.op_index
4684 = (state_machine_regs.op_index + uladv)
4685 % linfo.li_max_ops_per_insn;
4686 if (addrdelta)
4687 state_machine_regs.view = 0;
4688 printf (_(" Special opcode %d: "
4689 "advance Address by %" PRIu64
4690 " to %#" PRIx64 "[%d]%s"),
4691 op_code, uladv, state_machine_regs.address,
4692 state_machine_regs.op_index,
4693 verbose_view && addrdelta
4694 ? _(" (reset view)") : "");
4696 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4697 state_machine_regs.line += adv;
4698 printf (_(" and Line by %" PRId64 " to %d"),
4699 adv, state_machine_regs.line);
4700 if (verbose_view || state_machine_regs.view)
4701 printf (_(" (view %u)\n"), state_machine_regs.view);
4702 else
4703 putchar ('\n');
4704 state_machine_regs.view++;
4706 else
4707 switch (op_code)
4709 case DW_LNS_extended_op:
4710 data += process_extended_line_op (data,
4711 linfo.li_default_is_stmt,
4712 end);
4713 break;
4715 case DW_LNS_copy:
4716 printf (_(" Copy"));
4717 if (verbose_view || state_machine_regs.view)
4718 printf (_(" (view %u)\n"), state_machine_regs.view);
4719 else
4720 putchar ('\n');
4721 state_machine_regs.view++;
4722 break;
4724 case DW_LNS_advance_pc:
4725 READ_ULEB (uladv, data, end);
4726 if (linfo.li_max_ops_per_insn == 1)
4728 uladv *= linfo.li_min_insn_length;
4729 state_machine_regs.address += uladv;
4730 if (uladv)
4731 state_machine_regs.view = 0;
4732 printf (_(" Advance PC by %" PRIu64
4733 " to %#" PRIx64 "%s\n"),
4734 uladv, state_machine_regs.address,
4735 verbose_view && uladv
4736 ? _(" (reset view)") : "");
4738 else
4740 unsigned addrdelta
4741 = ((state_machine_regs.op_index + uladv)
4742 / linfo.li_max_ops_per_insn)
4743 * linfo.li_min_insn_length;
4744 state_machine_regs.address
4745 += addrdelta;
4746 state_machine_regs.op_index
4747 = (state_machine_regs.op_index + uladv)
4748 % linfo.li_max_ops_per_insn;
4749 if (addrdelta)
4750 state_machine_regs.view = 0;
4751 printf (_(" Advance PC by %" PRIu64
4752 " to %#" PRIx64 "[%d]%s\n"),
4753 uladv, state_machine_regs.address,
4754 state_machine_regs.op_index,
4755 verbose_view && addrdelta
4756 ? _(" (reset view)") : "");
4758 break;
4760 case DW_LNS_advance_line:
4761 READ_SLEB (adv, data, end);
4762 state_machine_regs.line += adv;
4763 printf (_(" Advance Line by %" PRId64 " to %d\n"),
4764 adv, state_machine_regs.line);
4765 break;
4767 case DW_LNS_set_file:
4768 READ_ULEB (uladv, data, end);
4769 printf (_(" Set File Name to entry %" PRIu64
4770 " in the File Name Table\n"), uladv);
4771 state_machine_regs.file = uladv;
4772 break;
4774 case DW_LNS_set_column:
4775 READ_ULEB (uladv, data, end);
4776 printf (_(" Set column to %" PRIu64 "\n"), uladv);
4777 state_machine_regs.column = uladv;
4778 break;
4780 case DW_LNS_negate_stmt:
4781 adv = state_machine_regs.is_stmt;
4782 adv = ! adv;
4783 printf (_(" Set is_stmt to %" PRId64 "\n"), adv);
4784 state_machine_regs.is_stmt = adv;
4785 break;
4787 case DW_LNS_set_basic_block:
4788 printf (_(" Set basic block\n"));
4789 state_machine_regs.basic_block = 1;
4790 break;
4792 case DW_LNS_const_add_pc:
4793 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4794 if (linfo.li_max_ops_per_insn)
4796 uladv *= linfo.li_min_insn_length;
4797 state_machine_regs.address += uladv;
4798 if (uladv)
4799 state_machine_regs.view = 0;
4800 printf (_(" Advance PC by constant %" PRIu64
4801 " to %#" PRIx64 "%s\n"),
4802 uladv, state_machine_regs.address,
4803 verbose_view && uladv
4804 ? _(" (reset view)") : "");
4806 else
4808 unsigned addrdelta
4809 = ((state_machine_regs.op_index + uladv)
4810 / linfo.li_max_ops_per_insn)
4811 * linfo.li_min_insn_length;
4812 state_machine_regs.address
4813 += addrdelta;
4814 state_machine_regs.op_index
4815 = (state_machine_regs.op_index + uladv)
4816 % linfo.li_max_ops_per_insn;
4817 if (addrdelta)
4818 state_machine_regs.view = 0;
4819 printf (_(" Advance PC by constant %" PRIu64
4820 " to %#" PRIx64 "[%d]%s\n"),
4821 uladv, state_machine_regs.address,
4822 state_machine_regs.op_index,
4823 verbose_view && addrdelta
4824 ? _(" (reset view)") : "");
4826 break;
4828 case DW_LNS_fixed_advance_pc:
4829 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4830 state_machine_regs.address += uladv;
4831 state_machine_regs.op_index = 0;
4832 printf (_(" Advance PC by fixed size amount %" PRIu64
4833 " to %#" PRIx64 "\n"),
4834 uladv, state_machine_regs.address);
4835 /* Do NOT reset view. */
4836 break;
4838 case DW_LNS_set_prologue_end:
4839 printf (_(" Set prologue_end to true\n"));
4840 break;
4842 case DW_LNS_set_epilogue_begin:
4843 printf (_(" Set epilogue_begin to true\n"));
4844 break;
4846 case DW_LNS_set_isa:
4847 READ_ULEB (uladv, data, end);
4848 printf (_(" Set ISA to %" PRIu64 "\n"), uladv);
4849 break;
4851 default:
4852 printf (_(" Unknown opcode %d with operands: "), op_code);
4854 if (standard_opcodes != NULL)
4855 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4857 READ_ULEB (uladv, data, end);
4858 printf ("%#" PRIx64 "%s", uladv, i == 1 ? "" : ", ");
4860 putchar ('\n');
4861 break;
4864 putchar ('\n');
4868 return 1;
4871 typedef struct
4873 char *name;
4874 unsigned int directory_index;
4875 unsigned int modification_date;
4876 unsigned int length;
4877 } File_Entry;
4879 /* Output a decoded representation of the .debug_line section. */
4881 static int
4882 display_debug_lines_decoded (struct dwarf_section * section,
4883 unsigned char * start,
4884 unsigned char * data,
4885 unsigned char * end,
4886 void * fileptr)
4888 static DWARF2_Internal_LineInfo saved_linfo;
4890 introduce (section, false);
4892 while (data < end)
4894 /* This loop amounts to one iteration per compilation unit. */
4895 DWARF2_Internal_LineInfo linfo;
4896 unsigned char *standard_opcodes;
4897 unsigned char *end_of_sequence;
4898 int i;
4899 File_Entry *file_table = NULL;
4900 unsigned int n_files = 0;
4901 char **directory_table = NULL;
4902 unsigned int n_directories = 0;
4904 if (startswith (section->name, ".debug_line.")
4905 /* Note: the following does not apply to .debug_line.dwo sections.
4906 These are full debug_line sections. */
4907 && strcmp (section->name, ".debug_line.dwo") != 0)
4909 /* See comment in display_debug_lines_raw(). */
4910 end_of_sequence = end;
4911 standard_opcodes = NULL;
4912 linfo = saved_linfo;
4913 /* PR 17531: file: 0522b371. */
4914 if (linfo.li_line_range == 0)
4916 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4917 return 0;
4919 reset_state_machine (linfo.li_default_is_stmt);
4921 else
4923 unsigned char *hdrptr;
4925 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4926 & end_of_sequence)) == NULL)
4927 return 0;
4929 /* PR 17531: file: 0522b371. */
4930 if (linfo.li_line_range == 0)
4932 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4933 linfo.li_line_range = 1;
4935 reset_state_machine (linfo.li_default_is_stmt);
4937 /* Save a pointer to the contents of the Opcodes table. */
4938 standard_opcodes = hdrptr;
4940 /* Traverse the Directory table just to count entries. */
4941 data = standard_opcodes + linfo.li_opcode_base - 1;
4942 /* PR 20440 */
4943 if (data >= end)
4945 warn (_("opcode base of %d extends beyond end of section\n"),
4946 linfo.li_opcode_base);
4947 return 0;
4950 if (linfo.li_version >= 5)
4952 unsigned char *format_start, *format;
4953 unsigned int format_count, formati, entryi;
4955 load_debug_section_with_follow (line_str, fileptr);
4957 /* Skip directories format. */
4958 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4959 if (do_checks && format_count > 1)
4960 warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
4961 format_count);
4962 format_start = data;
4963 for (formati = 0; formati < format_count; formati++)
4965 SKIP_ULEB (data, end);
4966 SKIP_ULEB (data, end);
4969 READ_ULEB (n_directories, data, end);
4970 if (data >= end)
4972 warn (_("Corrupt directories list\n"));
4973 break;
4976 if (n_directories == 0)
4977 directory_table = NULL;
4978 else
4979 directory_table = (char **)
4980 xcalloc (n_directories, sizeof (unsigned char *));
4982 for (entryi = 0; entryi < n_directories; entryi++)
4984 char **pathp = &directory_table[entryi];
4986 format = format_start;
4987 for (formati = 0; formati < format_count; formati++)
4989 uint64_t content_type, form;
4990 uint64_t uvalue;
4992 READ_ULEB (content_type, format, end);
4993 READ_ULEB (form, format, end);
4994 if (data >= end)
4996 warn (_("Corrupt directories list\n"));
4997 break;
4999 switch (content_type)
5001 case DW_LNCT_path:
5002 switch (form)
5004 case DW_FORM_string:
5005 *pathp = (char *) data;
5006 break;
5007 case DW_FORM_line_strp:
5008 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5009 end);
5010 /* Remove const by the cast. */
5011 *pathp = (char *)
5012 fetch_indirect_line_string (uvalue);
5013 break;
5015 break;
5017 data = read_and_display_attr_value (0, form, 0, start,
5018 data, end, 0, 0,
5019 linfo.li_offset_size,
5020 linfo.li_version,
5021 NULL, 1, section,
5022 NULL, '\t', -1);
5024 if (data >= end)
5026 warn (_("Corrupt directories list\n"));
5027 break;
5031 /* Skip files format. */
5032 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5033 if (do_checks && format_count > 5)
5034 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
5035 format_count);
5036 format_start = data;
5037 for (formati = 0; formati < format_count; formati++)
5039 SKIP_ULEB (data, end);
5040 SKIP_ULEB (data, end);
5043 READ_ULEB (n_files, data, end);
5044 if (data >= end && n_files > 0)
5046 warn (_("Corrupt file name list\n"));
5047 break;
5050 if (n_files == 0)
5051 file_table = NULL;
5052 else
5053 file_table = (File_Entry *) xcalloc (n_files,
5054 sizeof (File_Entry));
5056 for (entryi = 0; entryi < n_files; entryi++)
5058 File_Entry *file = &file_table[entryi];
5060 format = format_start;
5061 for (formati = 0; formati < format_count; formati++)
5063 uint64_t content_type, form;
5064 uint64_t uvalue;
5065 unsigned char *tmp;
5067 READ_ULEB (content_type, format, end);
5068 READ_ULEB (form, format, end);
5069 if (data >= end)
5071 warn (_("Corrupt file name list\n"));
5072 break;
5074 switch (content_type)
5076 case DW_LNCT_path:
5077 switch (form)
5079 case DW_FORM_string:
5080 file->name = (char *) data;
5081 break;
5082 case DW_FORM_line_strp:
5083 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5084 end);
5085 /* Remove const by the cast. */
5086 file->name = (char *)
5087 fetch_indirect_line_string (uvalue);
5088 break;
5090 break;
5091 case DW_LNCT_directory_index:
5092 switch (form)
5094 case DW_FORM_data1:
5095 SAFE_BYTE_GET (file->directory_index, data, 1,
5096 end);
5097 break;
5098 case DW_FORM_data2:
5099 SAFE_BYTE_GET (file->directory_index, data, 2,
5100 end);
5101 break;
5102 case DW_FORM_udata:
5103 tmp = data;
5104 READ_ULEB (file->directory_index, tmp, end);
5105 break;
5107 break;
5109 data = read_and_display_attr_value (0, form, 0, start,
5110 data, end, 0, 0,
5111 linfo.li_offset_size,
5112 linfo.li_version,
5113 NULL, 1, section,
5114 NULL, '\t', -1);
5116 if (data >= end)
5118 warn (_("Corrupt file name list\n"));
5119 break;
5123 else
5125 if (*data != 0)
5127 char *ptr_directory_table = (char *) data;
5129 while (data < end && *data != 0)
5131 data += strnlen ((char *) data, end - data);
5132 if (data < end)
5133 data++;
5134 n_directories++;
5137 /* PR 20440 */
5138 if (data >= end)
5140 warn (_("directory table ends unexpectedly\n"));
5141 n_directories = 0;
5142 break;
5145 /* Go through the directory table again to save the directories. */
5146 directory_table = (char **)
5147 xmalloc (n_directories * sizeof (unsigned char *));
5149 i = 0;
5150 while (*ptr_directory_table != 0)
5152 directory_table[i] = ptr_directory_table;
5153 ptr_directory_table += strlen (ptr_directory_table) + 1;
5154 i++;
5157 /* Skip the NUL at the end of the table. */
5158 data++;
5160 /* Traverse the File Name table just to count the entries. */
5161 if (data < end && *data != 0)
5163 unsigned char *ptr_file_name_table = data;
5165 while (data < end && *data != 0)
5167 /* Skip Name, directory index, last modification
5168 time and length of file. */
5169 data += strnlen ((char *) data, end - data);
5170 if (data < end)
5171 data++;
5172 SKIP_ULEB (data, end);
5173 SKIP_ULEB (data, end);
5174 SKIP_ULEB (data, end);
5175 n_files++;
5178 if (data >= end)
5180 warn (_("file table ends unexpectedly\n"));
5181 n_files = 0;
5182 break;
5185 /* Go through the file table again to save the strings. */
5186 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
5188 i = 0;
5189 while (*ptr_file_name_table != 0)
5191 file_table[i].name = (char *) ptr_file_name_table;
5192 ptr_file_name_table
5193 += strlen ((char *) ptr_file_name_table) + 1;
5195 /* We are not interested in directory, time or size. */
5196 READ_ULEB (file_table[i].directory_index,
5197 ptr_file_name_table, end);
5198 READ_ULEB (file_table[i].modification_date,
5199 ptr_file_name_table, end);
5200 READ_ULEB (file_table[i].length,
5201 ptr_file_name_table, end);
5202 i++;
5204 i = 0;
5207 /* Skip the NUL at the end of the table. */
5208 data++;
5211 /* Print the Compilation Unit's name and a header. */
5212 if (file_table == NULL)
5213 printf (_("CU: No directory table\n"));
5214 else if (directory_table == NULL)
5215 printf (_("CU: %s:\n"), null_name (file_table[0].name));
5216 else
5218 unsigned int ix = file_table[0].directory_index;
5219 const char *directory;
5221 if (ix == 0 && linfo.li_version < 5)
5222 directory = ".";
5223 /* PR 20439 */
5224 else if (n_directories == 0)
5225 directory = _("<unknown>");
5226 else
5228 if (linfo.li_version < 5)
5229 --ix;
5230 if (ix >= n_directories)
5232 warn (_("directory index %u "
5233 ">= number of directories %u\n"),
5234 ix, n_directories);
5235 directory = _("<corrupt>");
5237 else
5238 directory = directory_table[ix];
5240 if (do_wide)
5241 printf (_("CU: %s/%s:\n"),
5242 null_name (directory),
5243 null_name (file_table[0].name));
5244 else
5245 printf ("%s:\n", null_name (file_table[0].name));
5248 if (n_files > 0)
5249 printf (_("File name Line number Starting address View Stmt\n"));
5250 else
5251 printf (_("CU: Empty file name table\n"));
5252 saved_linfo = linfo;
5255 /* This loop iterates through the Dwarf Line Number Program. */
5256 while (data < end_of_sequence)
5258 unsigned char op_code;
5259 int xop;
5260 int adv;
5261 unsigned long int uladv;
5262 int is_special_opcode = 0;
5264 op_code = *data++;
5265 xop = op_code;
5267 if (op_code >= linfo.li_opcode_base)
5269 op_code -= linfo.li_opcode_base;
5270 uladv = (op_code / linfo.li_line_range);
5271 if (linfo.li_max_ops_per_insn == 1)
5273 uladv *= linfo.li_min_insn_length;
5274 state_machine_regs.address += uladv;
5275 if (uladv)
5276 state_machine_regs.view = 0;
5278 else
5280 unsigned addrdelta
5281 = ((state_machine_regs.op_index + uladv)
5282 / linfo.li_max_ops_per_insn)
5283 * linfo.li_min_insn_length;
5284 state_machine_regs.address
5285 += addrdelta;
5286 state_machine_regs.op_index
5287 = (state_machine_regs.op_index + uladv)
5288 % linfo.li_max_ops_per_insn;
5289 if (addrdelta)
5290 state_machine_regs.view = 0;
5293 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5294 state_machine_regs.line += adv;
5295 is_special_opcode = 1;
5296 /* Increment view after printing this row. */
5298 else
5299 switch (op_code)
5301 case DW_LNS_extended_op:
5303 unsigned int ext_op_code_len;
5304 unsigned char ext_op_code;
5305 unsigned char *op_code_end;
5306 unsigned char *op_code_data = data;
5308 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5309 op_code_end = op_code_data + ext_op_code_len;
5310 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5312 warn (_("Badly formed extended line op encountered!\n"));
5313 break;
5315 ext_op_code = *op_code_data++;
5316 xop = ext_op_code;
5317 xop = -xop;
5319 switch (ext_op_code)
5321 case DW_LNE_end_sequence:
5322 /* Reset stuff after printing this row. */
5323 break;
5324 case DW_LNE_set_address:
5325 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5326 op_code_data,
5327 op_code_end - op_code_data,
5328 op_code_end);
5329 state_machine_regs.op_index = 0;
5330 state_machine_regs.view = 0;
5331 break;
5332 case DW_LNE_define_file:
5333 file_table = (File_Entry *) xrealloc
5334 (file_table, (n_files + 1) * sizeof (File_Entry));
5336 ++state_machine_regs.last_file_entry;
5337 /* Source file name. */
5338 file_table[n_files].name = (char *) op_code_data;
5339 op_code_data += strlen ((char *) op_code_data) + 1;
5340 /* Directory index. */
5341 READ_ULEB (file_table[n_files].directory_index,
5342 op_code_data, op_code_end);
5343 /* Last modification time. */
5344 READ_ULEB (file_table[n_files].modification_date,
5345 op_code_data, op_code_end);
5346 /* File length. */
5347 READ_ULEB (file_table[n_files].length,
5348 op_code_data, op_code_end);
5349 n_files++;
5350 break;
5352 case DW_LNE_set_discriminator:
5353 case DW_LNE_HP_set_sequence:
5354 /* Simply ignored. */
5355 break;
5357 default:
5358 printf (_("UNKNOWN (%u): length %ld\n"),
5359 ext_op_code, (long int) (op_code_data - data));
5360 break;
5362 data = op_code_end;
5363 break;
5365 case DW_LNS_copy:
5366 /* Increment view after printing this row. */
5367 break;
5369 case DW_LNS_advance_pc:
5370 READ_ULEB (uladv, data, end);
5371 if (linfo.li_max_ops_per_insn == 1)
5373 uladv *= linfo.li_min_insn_length;
5374 state_machine_regs.address += uladv;
5375 if (uladv)
5376 state_machine_regs.view = 0;
5378 else
5380 unsigned addrdelta
5381 = ((state_machine_regs.op_index + uladv)
5382 / linfo.li_max_ops_per_insn)
5383 * linfo.li_min_insn_length;
5384 state_machine_regs.address
5385 += addrdelta;
5386 state_machine_regs.op_index
5387 = (state_machine_regs.op_index + uladv)
5388 % linfo.li_max_ops_per_insn;
5389 if (addrdelta)
5390 state_machine_regs.view = 0;
5392 break;
5394 case DW_LNS_advance_line:
5395 READ_SLEB (adv, data, end);
5396 state_machine_regs.line += adv;
5397 break;
5399 case DW_LNS_set_file:
5400 READ_ULEB (uladv, data, end);
5401 state_machine_regs.file = uladv;
5403 unsigned file = state_machine_regs.file;
5404 if (linfo.li_version < 5)
5405 --file;
5407 if (file_table == NULL || n_files == 0)
5408 printf (_("\n [Use file table entry %d]\n"), file);
5409 /* PR 20439 */
5410 else if (file >= n_files)
5412 warn (_("file index %u >= number of files %u\n"),
5413 file, n_files);
5414 printf (_("\n <over large file table index %u>"), file);
5416 else
5418 unsigned dir = file_table[file].directory_index;
5419 if (dir == 0 && linfo.li_version < 5)
5420 /* If directory index is 0, that means compilation
5421 current directory. bfd/dwarf2.c shows
5422 DW_AT_comp_dir here but in keeping with the
5423 readelf practice of minimal interpretation of
5424 file data, we show "./". */
5425 printf ("\n./%s:[++]\n",
5426 null_name (file_table[file].name));
5427 else if (directory_table == NULL || n_directories == 0)
5428 printf (_("\n [Use file %s "
5429 "in directory table entry %d]\n"),
5430 null_name (file_table[file].name), dir);
5431 else
5433 if (linfo.li_version < 5)
5434 --dir;
5435 /* PR 20439 */
5436 if (dir >= n_directories)
5438 warn (_("directory index %u "
5439 ">= number of directories %u\n"),
5440 dir, n_directories);
5441 printf (_("\n <over large directory table entry "
5442 "%u>\n"), dir);
5444 else
5445 printf ("\n%s/%s:\n",
5446 null_name (directory_table[dir]),
5447 null_name (file_table[file].name));
5450 break;
5452 case DW_LNS_set_column:
5453 READ_ULEB (uladv, data, end);
5454 state_machine_regs.column = uladv;
5455 break;
5457 case DW_LNS_negate_stmt:
5458 adv = state_machine_regs.is_stmt;
5459 adv = ! adv;
5460 state_machine_regs.is_stmt = adv;
5461 break;
5463 case DW_LNS_set_basic_block:
5464 state_machine_regs.basic_block = 1;
5465 break;
5467 case DW_LNS_const_add_pc:
5468 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5469 if (linfo.li_max_ops_per_insn == 1)
5471 uladv *= linfo.li_min_insn_length;
5472 state_machine_regs.address += uladv;
5473 if (uladv)
5474 state_machine_regs.view = 0;
5476 else
5478 unsigned addrdelta
5479 = ((state_machine_regs.op_index + uladv)
5480 / linfo.li_max_ops_per_insn)
5481 * linfo.li_min_insn_length;
5482 state_machine_regs.address
5483 += addrdelta;
5484 state_machine_regs.op_index
5485 = (state_machine_regs.op_index + uladv)
5486 % linfo.li_max_ops_per_insn;
5487 if (addrdelta)
5488 state_machine_regs.view = 0;
5490 break;
5492 case DW_LNS_fixed_advance_pc:
5493 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5494 state_machine_regs.address += uladv;
5495 state_machine_regs.op_index = 0;
5496 /* Do NOT reset view. */
5497 break;
5499 case DW_LNS_set_prologue_end:
5500 break;
5502 case DW_LNS_set_epilogue_begin:
5503 break;
5505 case DW_LNS_set_isa:
5506 READ_ULEB (uladv, data, end);
5507 printf (_(" Set ISA to %lu\n"), uladv);
5508 break;
5510 default:
5511 printf (_(" Unknown opcode %d with operands: "), op_code);
5513 if (standard_opcodes != NULL)
5514 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5516 uint64_t val;
5518 READ_ULEB (val, data, end);
5519 printf ("%#" PRIx64 "%s", val, i == 1 ? "" : ", ");
5521 putchar ('\n');
5522 break;
5525 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5526 to the DWARF address/line matrix. */
5527 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5528 || (xop == DW_LNS_copy))
5530 const unsigned int MAX_FILENAME_LENGTH = 35;
5531 char *fileName = NULL;
5532 char *newFileName = NULL;
5533 size_t fileNameLength;
5535 if (file_table)
5537 unsigned indx = state_machine_regs.file;
5539 if (linfo.li_version < 5)
5540 --indx;
5541 /* PR 20439 */
5542 if (indx >= n_files)
5544 warn (_("file index %u >= number of files %u\n"),
5545 indx, n_files);
5546 fileName = _("<corrupt>");
5548 else
5549 fileName = (char *) file_table[indx].name;
5551 if (!fileName)
5552 fileName = _("<unknown>");
5554 fileNameLength = strlen (fileName);
5555 newFileName = fileName;
5556 if (fileNameLength > MAX_FILENAME_LENGTH && !do_wide)
5558 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5559 /* Truncate file name */
5560 memcpy (newFileName,
5561 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5562 MAX_FILENAME_LENGTH);
5563 newFileName[MAX_FILENAME_LENGTH] = 0;
5566 /* A row with end_seq set to true has a meaningful address, but
5567 the other information in the same row is not significant.
5568 In such a row, print line as "-", and don't print
5569 view/is_stmt. */
5570 if (!do_wide || fileNameLength <= MAX_FILENAME_LENGTH)
5572 if (linfo.li_max_ops_per_insn == 1)
5574 if (xop == -DW_LNE_end_sequence)
5575 printf ("%-35s %11s %#18" PRIx64,
5576 newFileName, "-",
5577 state_machine_regs.address);
5578 else
5579 printf ("%-35s %11d %#18" PRIx64,
5580 newFileName, state_machine_regs.line,
5581 state_machine_regs.address);
5583 else
5585 if (xop == -DW_LNE_end_sequence)
5586 printf ("%-35s %11s %#18" PRIx64 "[%d]",
5587 newFileName, "-",
5588 state_machine_regs.address,
5589 state_machine_regs.op_index);
5590 else
5591 printf ("%-35s %11d %#18" PRIx64 "[%d]",
5592 newFileName, state_machine_regs.line,
5593 state_machine_regs.address,
5594 state_machine_regs.op_index);
5597 else
5599 if (linfo.li_max_ops_per_insn == 1)
5601 if (xop == -DW_LNE_end_sequence)
5602 printf ("%s %11s %#18" PRIx64,
5603 newFileName, "-",
5604 state_machine_regs.address);
5605 else
5606 printf ("%s %11d %#18" PRIx64,
5607 newFileName, state_machine_regs.line,
5608 state_machine_regs.address);
5610 else
5612 if (xop == -DW_LNE_end_sequence)
5613 printf ("%s %11s %#18" PRIx64 "[%d]",
5614 newFileName, "-",
5615 state_machine_regs.address,
5616 state_machine_regs.op_index);
5617 else
5618 printf ("%s %11d %#18" PRIx64 "[%d]",
5619 newFileName, state_machine_regs.line,
5620 state_machine_regs.address,
5621 state_machine_regs.op_index);
5625 if (xop != -DW_LNE_end_sequence)
5627 if (state_machine_regs.view)
5628 printf (" %6u", state_machine_regs.view);
5629 else
5630 printf (" ");
5632 if (state_machine_regs.is_stmt)
5633 printf (" x");
5636 putchar ('\n');
5637 state_machine_regs.view++;
5639 if (xop == -DW_LNE_end_sequence)
5641 reset_state_machine (linfo.li_default_is_stmt);
5642 putchar ('\n');
5645 if (newFileName != fileName)
5646 free (newFileName);
5650 if (file_table)
5652 free (file_table);
5653 file_table = NULL;
5654 n_files = 0;
5657 if (directory_table)
5659 free (directory_table);
5660 directory_table = NULL;
5661 n_directories = 0;
5664 putchar ('\n');
5667 return 1;
5670 static int
5671 display_debug_lines (struct dwarf_section *section, void *file)
5673 unsigned char *data = section->start;
5674 unsigned char *end = data + section->size;
5675 int retValRaw = 1;
5676 int retValDecoded = 1;
5678 if (do_debug_lines == 0)
5679 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5681 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
5682 retValRaw = display_debug_lines_raw (section, data, end, file);
5684 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
5685 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
5687 if (!retValRaw || !retValDecoded)
5688 return 0;
5690 return 1;
5693 static debug_info *
5694 find_debug_info_for_offset (uint64_t offset)
5696 unsigned int i;
5698 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5699 return NULL;
5701 for (i = 0; i < num_debug_info_entries; i++)
5702 if (debug_information[i].cu_offset == offset)
5703 return debug_information + i;
5705 return NULL;
5708 static const char *
5709 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5711 /* See gdb/gdb-index.h. */
5712 static const char * const kinds[] =
5714 N_ ("no info"),
5715 N_ ("type"),
5716 N_ ("variable"),
5717 N_ ("function"),
5718 N_ ("other"),
5719 N_ ("unused5"),
5720 N_ ("unused6"),
5721 N_ ("unused7")
5724 return _ (kinds[kind]);
5727 static int
5728 display_debug_pubnames_worker (struct dwarf_section *section,
5729 void *file ATTRIBUTE_UNUSED,
5730 int is_gnu)
5732 DWARF2_Internal_PubNames names;
5733 unsigned char *start = section->start;
5734 unsigned char *end = start + section->size;
5736 /* It does not matter if this load fails,
5737 we test for that later on. */
5738 load_debug_info (file);
5740 introduce (section, false);
5742 while (start < end)
5744 unsigned char *data;
5745 unsigned long sec_off = start - section->start;
5746 unsigned int offset_size;
5748 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
5749 if (names.pn_length == 0xffffffff)
5751 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
5752 offset_size = 8;
5754 else
5755 offset_size = 4;
5757 if (names.pn_length > (size_t) (end - start))
5759 warn (_("Debug info is corrupted, "
5760 "%s header at %#lx has length %#" PRIx64 "\n"),
5761 section->name, sec_off, names.pn_length);
5762 break;
5765 data = start;
5766 start += names.pn_length;
5768 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, start);
5769 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, start);
5771 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5772 && num_debug_info_entries > 0
5773 && find_debug_info_for_offset (names.pn_offset) == NULL)
5774 warn (_(".debug_info offset of %#" PRIx64
5775 " in %s section does not point to a CU header.\n"),
5776 names.pn_offset, section->name);
5778 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, start);
5780 printf (_(" Length: %" PRId64 "\n"),
5781 names.pn_length);
5782 printf (_(" Version: %d\n"),
5783 names.pn_version);
5784 printf (_(" Offset into .debug_info section: %#" PRIx64 "\n"),
5785 names.pn_offset);
5786 printf (_(" Size of area in .debug_info section: %" PRId64 "\n"),
5787 names.pn_size);
5789 if (names.pn_version != 2 && names.pn_version != 3)
5791 static int warned = 0;
5793 if (! warned)
5795 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
5796 warned = 1;
5799 continue;
5802 if (is_gnu)
5803 printf (_("\n Offset Kind Name\n"));
5804 else
5805 printf (_("\n Offset\tName\n"));
5807 while (1)
5809 size_t maxprint;
5810 uint64_t offset;
5812 SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start);
5814 if (offset == 0)
5815 break;
5817 if (data >= start)
5818 break;
5819 maxprint = (start - data) - 1;
5821 if (is_gnu)
5823 unsigned int kind_data;
5824 gdb_index_symbol_kind kind;
5825 const char *kind_name;
5826 int is_static;
5828 SAFE_BYTE_GET_AND_INC (kind_data, data, 1, start);
5829 maxprint --;
5830 /* GCC computes the kind as the upper byte in the CU index
5831 word, and then right shifts it by the CU index size.
5832 Left shift KIND to where the gdb-index.h accessor macros
5833 can use it. */
5834 kind_data <<= GDB_INDEX_CU_BITSIZE;
5835 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
5836 kind_name = get_gdb_index_symbol_kind_name (kind);
5837 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
5838 printf (" %-6" PRIx64 " %s,%-10s %.*s\n",
5839 offset, is_static ? _("s") : _("g"),
5840 kind_name, (int) maxprint, data);
5842 else
5843 printf (" %-6" PRIx64 "\t%.*s\n",
5844 offset, (int) maxprint, data);
5846 data += strnlen ((char *) data, maxprint);
5847 if (data < start)
5848 data++;
5849 if (data >= start)
5850 break;
5854 printf ("\n");
5855 return 1;
5858 static int
5859 display_debug_pubnames (struct dwarf_section *section, void *file)
5861 return display_debug_pubnames_worker (section, file, 0);
5864 static int
5865 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
5867 return display_debug_pubnames_worker (section, file, 1);
5870 static int
5871 display_debug_macinfo (struct dwarf_section *section,
5872 void *file ATTRIBUTE_UNUSED)
5874 unsigned char *start = section->start;
5875 unsigned char *end = start + section->size;
5876 unsigned char *curr = start;
5877 enum dwarf_macinfo_record_type op;
5879 introduce (section, false);
5881 while (curr < end)
5883 unsigned int lineno;
5884 const unsigned char *string;
5886 op = (enum dwarf_macinfo_record_type) *curr;
5887 curr++;
5889 switch (op)
5891 case DW_MACINFO_start_file:
5893 unsigned int filenum;
5895 READ_ULEB (lineno, curr, end);
5896 READ_ULEB (filenum, curr, end);
5897 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
5898 lineno, filenum);
5900 break;
5902 case DW_MACINFO_end_file:
5903 printf (_(" DW_MACINFO_end_file\n"));
5904 break;
5906 case DW_MACINFO_define:
5907 READ_ULEB (lineno, curr, end);
5908 string = curr;
5909 curr += strnlen ((char *) string, end - string);
5910 printf (_(" DW_MACINFO_define - lineno : %d macro : %*s\n"),
5911 lineno, (int) (curr - string), string);
5912 if (curr < end)
5913 curr++;
5914 break;
5916 case DW_MACINFO_undef:
5917 READ_ULEB (lineno, curr, end);
5918 string = curr;
5919 curr += strnlen ((char *) string, end - string);
5920 printf (_(" DW_MACINFO_undef - lineno : %d macro : %*s\n"),
5921 lineno, (int) (curr - string), string);
5922 if (curr < end)
5923 curr++;
5924 break;
5926 case DW_MACINFO_vendor_ext:
5928 unsigned int constant;
5930 READ_ULEB (constant, curr, end);
5931 string = curr;
5932 curr += strnlen ((char *) string, end - string);
5933 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %*s\n"),
5934 constant, (int) (curr - string), string);
5935 if (curr < end)
5936 curr++;
5938 break;
5942 return 1;
5945 /* Given LINE_OFFSET into the .debug_line section, attempt to return
5946 filename and dirname corresponding to file name table entry with index
5947 FILEIDX. Return NULL on failure. */
5949 static unsigned char *
5950 get_line_filename_and_dirname (uint64_t line_offset,
5951 uint64_t fileidx,
5952 unsigned char **dir_name)
5954 struct dwarf_section *section = &debug_displays [line].section;
5955 unsigned char *hdrptr, *dirtable, *file_name;
5956 unsigned int offset_size;
5957 unsigned int version, opcode_base;
5958 uint64_t length, diridx;
5959 const unsigned char * end;
5961 *dir_name = NULL;
5962 if (section->start == NULL
5963 || line_offset >= section->size
5964 || fileidx == 0)
5965 return NULL;
5967 hdrptr = section->start + line_offset;
5968 end = section->start + section->size;
5970 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
5971 if (length == 0xffffffff)
5973 /* This section is 64-bit DWARF 3. */
5974 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
5975 offset_size = 8;
5977 else
5978 offset_size = 4;
5980 if (length > (size_t) (end - hdrptr)
5981 || length < 2 + offset_size + 1 + 3 + 1)
5982 return NULL;
5983 end = hdrptr + length;
5985 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
5986 if (version != 2 && version != 3 && version != 4)
5987 return NULL;
5988 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
5989 if (version >= 4)
5990 hdrptr++; /* Skip max_ops_per_insn. */
5991 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
5993 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
5994 if (opcode_base == 0
5995 || opcode_base - 1 >= (size_t) (end - hdrptr))
5996 return NULL;
5998 hdrptr += opcode_base - 1;
6000 dirtable = hdrptr;
6001 /* Skip over dirname table. */
6002 while (*hdrptr != '\0')
6004 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6005 if (hdrptr < end)
6006 hdrptr++;
6007 if (hdrptr >= end)
6008 return NULL;
6010 hdrptr++; /* Skip the NUL at the end of the table. */
6012 /* Now skip over preceding filename table entries. */
6013 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
6015 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6016 if (hdrptr < end)
6017 hdrptr++;
6018 SKIP_ULEB (hdrptr, end);
6019 SKIP_ULEB (hdrptr, end);
6020 SKIP_ULEB (hdrptr, end);
6022 if (hdrptr >= end || *hdrptr == '\0')
6023 return NULL;
6025 file_name = hdrptr;
6026 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
6027 if (hdrptr < end)
6028 hdrptr++;
6029 if (hdrptr >= end)
6030 return NULL;
6031 READ_ULEB (diridx, hdrptr, end);
6032 if (diridx == 0)
6033 return file_name;
6034 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
6036 dirtable += strnlen ((char *) dirtable, end - dirtable);
6037 if (dirtable < end)
6038 dirtable++;
6040 if (dirtable >= end || *dirtable == '\0')
6041 return NULL;
6042 *dir_name = dirtable;
6043 return file_name;
6046 static int
6047 display_debug_macro (struct dwarf_section *section,
6048 void *file)
6050 unsigned char *start = section->start;
6051 unsigned char *end = start + section->size;
6052 unsigned char *curr = start;
6053 unsigned char *extended_op_buf[256];
6054 bool is_dwo = false;
6055 const char *suffix = strrchr (section->name, '.');
6057 if (suffix && strcmp (suffix, ".dwo") == 0)
6058 is_dwo = true;
6060 load_debug_section_with_follow (str, file);
6061 load_debug_section_with_follow (line, file);
6062 load_debug_section_with_follow (str_index, file);
6064 introduce (section, false);
6066 while (curr < end)
6068 unsigned int lineno, version, flags;
6069 unsigned int offset_size;
6070 const unsigned char *string;
6071 uint64_t line_offset = 0, sec_offset = curr - start, offset;
6072 unsigned char **extended_ops = NULL;
6074 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
6075 if (version != 4 && version != 5)
6077 error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
6078 section->name, version);
6079 return 0;
6082 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
6083 offset_size = (flags & 1) ? 8 : 4;
6084 printf (_(" Offset: %#" PRIx64 "\n"), sec_offset);
6085 printf (_(" Version: %d\n"), version);
6086 printf (_(" Offset size: %d\n"), offset_size);
6087 if (flags & 2)
6089 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
6090 printf (_(" Offset into .debug_line: %#" PRIx64 "\n"),
6091 line_offset);
6093 if (flags & 4)
6095 unsigned int i, count, op;
6096 uint64_t nargs, n;
6098 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
6100 memset (extended_op_buf, 0, sizeof (extended_op_buf));
6101 extended_ops = extended_op_buf;
6102 if (count)
6104 printf (_(" Extension opcode arguments:\n"));
6105 for (i = 0; i < count; i++)
6107 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6108 extended_ops[op] = curr;
6109 READ_ULEB (nargs, curr, end);
6110 if (nargs == 0)
6111 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
6112 else
6114 printf (_(" DW_MACRO_%02x arguments: "), op);
6115 for (n = 0; n < nargs; n++)
6117 unsigned int form;
6119 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
6120 printf ("%s%s", get_FORM_name (form),
6121 n == nargs - 1 ? "\n" : ", ");
6122 switch (form)
6124 case DW_FORM_data1:
6125 case DW_FORM_data2:
6126 case DW_FORM_data4:
6127 case DW_FORM_data8:
6128 case DW_FORM_sdata:
6129 case DW_FORM_udata:
6130 case DW_FORM_block:
6131 case DW_FORM_block1:
6132 case DW_FORM_block2:
6133 case DW_FORM_block4:
6134 case DW_FORM_flag:
6135 case DW_FORM_string:
6136 case DW_FORM_strp:
6137 case DW_FORM_sec_offset:
6138 break;
6139 default:
6140 error (_("Invalid extension opcode form %s\n"),
6141 get_FORM_name (form));
6142 return 0;
6149 printf ("\n");
6151 while (1)
6153 unsigned int op;
6155 if (curr >= end)
6157 error (_(".debug_macro section not zero terminated\n"));
6158 return 0;
6161 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6162 if (op == 0)
6163 break;
6165 switch (op)
6167 case DW_MACRO_define:
6168 READ_ULEB (lineno, curr, end);
6169 string = curr;
6170 curr += strnlen ((char *) string, end - string);
6171 printf (_(" DW_MACRO_define - lineno : %d macro : %*s\n"),
6172 lineno, (int) (curr - string), string);
6173 if (curr < end)
6174 curr++;
6175 break;
6177 case DW_MACRO_undef:
6178 READ_ULEB (lineno, curr, end);
6179 string = curr;
6180 curr += strnlen ((char *) string, end - string);
6181 printf (_(" DW_MACRO_undef - lineno : %d macro : %*s\n"),
6182 lineno, (int) (curr - string), string);
6183 if (curr < end)
6184 curr++;
6185 break;
6187 case DW_MACRO_start_file:
6189 unsigned int filenum;
6190 unsigned char *file_name = NULL, *dir_name = NULL;
6192 READ_ULEB (lineno, curr, end);
6193 READ_ULEB (filenum, curr, end);
6195 if ((flags & 2) == 0)
6196 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
6197 else
6198 file_name
6199 = get_line_filename_and_dirname (line_offset, filenum,
6200 &dir_name);
6201 if (file_name == NULL)
6202 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
6203 lineno, filenum);
6204 else
6205 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
6206 lineno, filenum,
6207 dir_name != NULL ? (const char *) dir_name : "",
6208 dir_name != NULL ? "/" : "", file_name);
6210 break;
6212 case DW_MACRO_end_file:
6213 printf (_(" DW_MACRO_end_file\n"));
6214 break;
6216 case DW_MACRO_define_strp:
6217 READ_ULEB (lineno, curr, end);
6218 if (version == 4 && is_dwo)
6219 READ_ULEB (offset, curr, end);
6220 else
6221 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6222 string = fetch_indirect_string (offset);
6223 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
6224 lineno, string);
6225 break;
6227 case DW_MACRO_undef_strp:
6228 READ_ULEB (lineno, curr, end);
6229 if (version == 4 && is_dwo)
6230 READ_ULEB (offset, curr, end);
6231 else
6232 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6233 string = fetch_indirect_string (offset);
6234 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
6235 lineno, string);
6236 break;
6238 case DW_MACRO_import:
6239 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6240 printf (_(" DW_MACRO_import - offset : %#" PRIx64 "\n"),
6241 offset);
6242 break;
6244 case DW_MACRO_define_sup:
6245 READ_ULEB (lineno, curr, end);
6246 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6247 printf (_(" DW_MACRO_define_sup - lineno : %d"
6248 " macro offset : %#" PRIx64 "\n"),
6249 lineno, offset);
6250 break;
6252 case DW_MACRO_undef_sup:
6253 READ_ULEB (lineno, curr, end);
6254 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6255 printf (_(" DW_MACRO_undef_sup - lineno : %d"
6256 " macro offset : %#" PRIx64 "\n"),
6257 lineno, offset);
6258 break;
6260 case DW_MACRO_import_sup:
6261 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6262 printf (_(" DW_MACRO_import_sup - offset : %#" PRIx64 "\n"),
6263 offset);
6264 break;
6266 case DW_MACRO_define_strx:
6267 case DW_MACRO_undef_strx:
6268 READ_ULEB (lineno, curr, end);
6269 READ_ULEB (offset, curr, end);
6270 string = (const unsigned char *)
6271 fetch_indexed_string (offset, NULL, offset_size, false, 0);
6272 if (op == DW_MACRO_define_strx)
6273 printf (" DW_MACRO_define_strx ");
6274 else
6275 printf (" DW_MACRO_undef_strx ");
6276 if (do_wide)
6277 printf (_("(with offset %#" PRIx64 ") "), offset);
6278 printf (_("lineno : %d macro : %s\n"),
6279 lineno, string);
6280 break;
6282 default:
6283 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6285 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6286 break;
6289 if (extended_ops == NULL || extended_ops[op] == NULL)
6291 error (_(" Unknown macro opcode %02x seen\n"), op);
6292 return 0;
6294 else
6296 /* Skip over unhandled opcodes. */
6297 uint64_t nargs, n;
6298 unsigned char *desc = extended_ops[op];
6299 READ_ULEB (nargs, desc, end);
6300 if (nargs == 0)
6302 printf (_(" DW_MACRO_%02x\n"), op);
6303 break;
6305 printf (_(" DW_MACRO_%02x -"), op);
6306 for (n = 0; n < nargs; n++)
6308 int val;
6310 /* DW_FORM_implicit_const is not expected here. */
6311 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
6312 curr
6313 = read_and_display_attr_value (0, val, 0,
6314 start, curr, end, 0, 0,
6315 offset_size, version,
6316 NULL, 0, section,
6317 NULL, ' ', -1);
6318 if (n != nargs - 1)
6319 printf (",");
6321 printf ("\n");
6323 break;
6327 printf ("\n");
6330 return 1;
6333 static int
6334 display_debug_abbrev (struct dwarf_section *section,
6335 void *file ATTRIBUTE_UNUSED)
6337 abbrev_entry *entry;
6338 unsigned char *start = section->start;
6340 introduce (section, false);
6344 uint64_t offset = start - section->start;
6345 abbrev_list *list = find_and_process_abbrev_set (section, 0,
6346 section->size, offset,
6347 NULL);
6348 if (list == NULL)
6349 break;
6351 if (list->first_abbrev)
6352 printf (_(" Number TAG (%#" PRIx64 ")\n"), offset);
6354 for (entry = list->first_abbrev; entry; entry = entry->next)
6356 abbrev_attr *attr;
6358 printf (" %ld %s [%s]\n",
6359 entry->number,
6360 get_TAG_name (entry->tag),
6361 entry->children ? _("has children") : _("no children"));
6363 for (attr = entry->first_attr; attr; attr = attr->next)
6365 printf (" %-18s %s",
6366 get_AT_name (attr->attribute),
6367 get_FORM_name (attr->form));
6368 if (attr->form == DW_FORM_implicit_const)
6369 printf (": %" PRId64, attr->implicit_const);
6370 putchar ('\n');
6373 start = list->start_of_next_abbrevs;
6374 free_abbrev_list (list);
6376 while (start);
6378 printf ("\n");
6380 return 1;
6383 /* Return true when ADDR is the maximum address, when addresses are
6384 POINTER_SIZE bytes long. */
6386 static bool
6387 is_max_address (uint64_t addr, unsigned int pointer_size)
6389 uint64_t mask = ~(~(uint64_t) 0 << 1 << (pointer_size * 8 - 1));
6390 return ((addr & mask) == mask);
6393 /* Display a view pair list starting at *VSTART_PTR and ending at
6394 VLISTEND within SECTION. */
6396 static void
6397 display_view_pair_list (struct dwarf_section *section,
6398 unsigned char **vstart_ptr,
6399 unsigned int debug_info_entry,
6400 unsigned char *vlistend)
6402 unsigned char *vstart = *vstart_ptr;
6403 unsigned char *section_end = section->start + section->size;
6404 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6406 if (vlistend < section_end)
6407 section_end = vlistend;
6409 putchar ('\n');
6411 while (vstart < section_end)
6413 uint64_t off = vstart - section->start;
6414 uint64_t vbegin, vend;
6416 READ_ULEB (vbegin, vstart, section_end);
6417 if (vstart == section_end)
6418 break;
6420 READ_ULEB (vend, vstart, section_end);
6421 printf (" %8.8" PRIx64 " ", off);
6423 print_view (vbegin, pointer_size);
6424 print_view (vend, pointer_size);
6425 printf (_("location view pair\n"));
6428 putchar ('\n');
6429 *vstart_ptr = vstart;
6432 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
6434 static void
6435 display_loc_list (struct dwarf_section *section,
6436 unsigned char **start_ptr,
6437 unsigned int debug_info_entry,
6438 uint64_t offset,
6439 uint64_t base_address,
6440 unsigned char **vstart_ptr,
6441 int has_frame_base)
6443 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6444 unsigned char *section_end = section->start + section->size;
6445 uint64_t cu_offset;
6446 unsigned int pointer_size;
6447 unsigned int offset_size;
6448 int dwarf_version;
6449 uint64_t begin;
6450 uint64_t end;
6451 unsigned short length;
6452 int need_frame_base;
6454 if (debug_info_entry >= num_debug_info_entries)
6456 warn (_("No debug information available for loc lists of entry: %u\n"),
6457 debug_info_entry);
6458 return;
6461 cu_offset = debug_information [debug_info_entry].cu_offset;
6462 pointer_size = debug_information [debug_info_entry].pointer_size;
6463 offset_size = debug_information [debug_info_entry].offset_size;
6464 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6466 if (pointer_size < 2 || pointer_size > 8)
6468 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6469 pointer_size, debug_info_entry);
6470 return;
6473 while (1)
6475 uint64_t off = offset + (start - *start_ptr);
6476 uint64_t vbegin = -1, vend = -1;
6478 if (2 * pointer_size > (size_t) (section_end - start))
6480 warn (_("Location list starting at offset %#" PRIx64
6481 " is not terminated.\n"), offset);
6482 break;
6485 printf (" ");
6486 print_hex (off, 4);
6488 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6489 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6491 if (begin == 0 && end == 0)
6493 /* PR 18374: In a object file we can have a location list that
6494 starts with a begin and end of 0 because there are relocations
6495 that need to be applied to the addresses. Actually applying
6496 the relocations now does not help as they will probably resolve
6497 to 0, since the object file has not been fully linked. Real
6498 end of list markers will not have any relocations against them. */
6499 if (! reloc_at (section, off)
6500 && ! reloc_at (section, off + pointer_size))
6502 printf (_("<End of list>\n"));
6503 break;
6507 /* Check base address specifiers. */
6508 if (is_max_address (begin, pointer_size)
6509 && !is_max_address (end, pointer_size))
6511 base_address = end;
6512 print_hex (begin, pointer_size);
6513 print_hex (end, pointer_size);
6514 printf (_("(base address)\n"));
6515 continue;
6518 if (vstart)
6520 off = offset + (vstart - *start_ptr);
6522 READ_ULEB (vbegin, vstart, section_end);
6523 print_view (vbegin, pointer_size);
6525 READ_ULEB (vend, vstart, section_end);
6526 print_view (vend, pointer_size);
6528 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6531 if (2 > (size_t) (section_end - start))
6533 warn (_("Location list starting at offset %#" PRIx64
6534 " is not terminated.\n"), offset);
6535 break;
6538 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6540 if (length > (size_t) (section_end - start))
6542 warn (_("Location list starting at offset %#" PRIx64
6543 " is not terminated.\n"), offset);
6544 break;
6547 print_hex (begin + base_address, pointer_size);
6548 print_hex (end + base_address, pointer_size);
6550 putchar ('(');
6551 need_frame_base = decode_location_expression (start,
6552 pointer_size,
6553 offset_size,
6554 dwarf_version,
6555 length,
6556 cu_offset, section);
6557 putchar (')');
6559 if (need_frame_base && !has_frame_base)
6560 printf (_(" [without DW_AT_frame_base]"));
6562 if (begin == end && vbegin == vend)
6563 fputs (_(" (start == end)"), stdout);
6564 else if (begin > end || (begin == end && vbegin > vend))
6565 fputs (_(" (start > end)"), stdout);
6567 putchar ('\n');
6569 start += length;
6572 *start_ptr = start;
6573 *vstart_ptr = vstart;
6576 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
6578 static void
6579 display_loclists_list (struct dwarf_section * section,
6580 unsigned char ** start_ptr,
6581 unsigned int debug_info_entry,
6582 uint64_t offset,
6583 uint64_t base_address,
6584 unsigned char ** vstart_ptr,
6585 int has_frame_base)
6587 unsigned char *start = *start_ptr;
6588 unsigned char *vstart = *vstart_ptr;
6589 unsigned char *section_end = section->start + section->size;
6590 uint64_t cu_offset;
6591 unsigned int pointer_size;
6592 unsigned int offset_size;
6593 unsigned int dwarf_version;
6595 /* Initialize it due to a false compiler warning. */
6596 uint64_t begin = -1, vbegin = -1;
6597 uint64_t end = -1, vend = -1;
6598 uint64_t length;
6599 int need_frame_base;
6601 if (debug_info_entry >= num_debug_info_entries)
6603 warn (_("No debug information available for "
6604 "loclists lists of entry: %u\n"),
6605 debug_info_entry);
6606 return;
6609 cu_offset = debug_information [debug_info_entry].cu_offset;
6610 pointer_size = debug_information [debug_info_entry].pointer_size;
6611 offset_size = debug_information [debug_info_entry].offset_size;
6612 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6614 if (pointer_size < 2 || pointer_size > 8)
6616 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6617 pointer_size, debug_info_entry);
6618 return;
6621 while (1)
6623 uint64_t off = offset + (start - *start_ptr);
6624 enum dwarf_location_list_entry_type llet;
6626 if (start + 1 > section_end)
6628 warn (_("Location list starting at offset %#" PRIx64
6629 " is not terminated.\n"), offset);
6630 break;
6633 printf (" ");
6634 print_hex (off, 4);
6636 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6638 if (vstart && (llet == DW_LLE_offset_pair
6639 || llet == DW_LLE_start_end
6640 || llet == DW_LLE_start_length))
6642 off = offset + (vstart - *start_ptr);
6644 READ_ULEB (vbegin, vstart, section_end);
6645 print_view (vbegin, pointer_size);
6647 READ_ULEB (vend, vstart, section_end);
6648 print_view (vend, pointer_size);
6650 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6653 switch (llet)
6655 case DW_LLE_end_of_list:
6656 printf (_("<End of list>\n"));
6657 break;
6659 case DW_LLE_base_addressx:
6660 READ_ULEB (base_address, start, section_end);
6661 print_hex (base_address, pointer_size);
6662 printf (_("(index into .debug_addr) "));
6663 base_address = fetch_indexed_addr (base_address, pointer_size);
6664 print_hex (base_address, pointer_size);
6665 printf (_("(base address)\n"));
6666 break;
6668 case DW_LLE_startx_endx:
6669 READ_ULEB (begin, start, section_end);
6670 begin = fetch_indexed_addr (begin, pointer_size);
6671 READ_ULEB (end, start, section_end);
6672 end = fetch_indexed_addr (end, pointer_size);
6673 break;
6675 case DW_LLE_startx_length:
6676 READ_ULEB (begin, start, section_end);
6677 begin = fetch_indexed_addr (begin, pointer_size);
6678 READ_ULEB (end, start, section_end);
6679 end += begin;
6680 break;
6682 case DW_LLE_default_location:
6683 begin = end = 0;
6684 break;
6686 case DW_LLE_offset_pair:
6687 READ_ULEB (begin, start, section_end);
6688 begin += base_address;
6689 READ_ULEB (end, start, section_end);
6690 end += base_address;
6691 break;
6693 case DW_LLE_base_address:
6694 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6695 section_end);
6696 print_hex (base_address, pointer_size);
6697 printf (_("(base address)\n"));
6698 break;
6700 case DW_LLE_start_end:
6701 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6702 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6703 break;
6705 case DW_LLE_start_length:
6706 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6707 READ_ULEB (end, start, section_end);
6708 end += begin;
6709 break;
6711 #ifdef DW_LLE_view_pair
6712 case DW_LLE_view_pair:
6713 if (vstart)
6714 printf (_("View pair entry in loclist with locviews attribute\n"));
6715 READ_ULEB (vbegin, start, section_end);
6716 print_view (vbegin, pointer_size);
6718 READ_ULEB (vend, start, section_end);
6719 print_view (vend, pointer_size);
6721 printf (_("views for:\n"));
6722 continue;
6723 #endif
6725 default:
6726 error (_("Invalid location list entry type %d\n"), llet);
6727 return;
6730 if (llet == DW_LLE_end_of_list)
6731 break;
6733 if (llet == DW_LLE_base_address
6734 || llet == DW_LLE_base_addressx)
6735 continue;
6737 if (start == section_end)
6739 warn (_("Location list starting at offset %#" PRIx64
6740 " is not terminated.\n"), offset);
6741 break;
6743 READ_ULEB (length, start, section_end);
6745 if (length > (size_t) (section_end - start))
6747 warn (_("Location list starting at offset %#" PRIx64
6748 " is not terminated.\n"), offset);
6749 break;
6752 print_hex (begin, pointer_size);
6753 print_hex (end, pointer_size);
6755 putchar ('(');
6756 need_frame_base = decode_location_expression (start,
6757 pointer_size,
6758 offset_size,
6759 dwarf_version,
6760 length,
6761 cu_offset, section);
6762 putchar (')');
6764 if (need_frame_base && !has_frame_base)
6765 printf (_(" [without DW_AT_frame_base]"));
6767 if (begin == end && vbegin == vend)
6768 fputs (_(" (start == end)"), stdout);
6769 else if (begin > end || (begin == end && vbegin > vend))
6770 fputs (_(" (start > end)"), stdout);
6772 putchar ('\n');
6774 start += length;
6775 vbegin = vend = -1;
6778 if (vbegin != (uint64_t) -1 || vend != (uint64_t) -1)
6779 printf (_("Trailing view pair not used in a range"));
6781 *start_ptr = start;
6782 *vstart_ptr = vstart;
6785 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
6786 right-adjusted in a field of length LEN, and followed by a space. */
6788 static void
6789 print_addr_index (unsigned int idx, unsigned int len)
6791 static char buf[15];
6792 snprintf (buf, sizeof (buf), "[%d]", idx);
6793 printf ("%*s ", len, buf);
6796 /* Display a location list from a .dwo section. It uses address indexes rather
6797 than embedded addresses. This code closely follows display_loc_list, but the
6798 two are sufficiently different that combining things is very ugly. */
6800 static void
6801 display_loc_list_dwo (struct dwarf_section *section,
6802 unsigned char **start_ptr,
6803 unsigned int debug_info_entry,
6804 uint64_t offset,
6805 unsigned char **vstart_ptr,
6806 int has_frame_base)
6808 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6809 unsigned char *section_end = section->start + section->size;
6810 uint64_t cu_offset;
6811 unsigned int pointer_size;
6812 unsigned int offset_size;
6813 int dwarf_version;
6814 int entry_type;
6815 unsigned short length;
6816 int need_frame_base;
6817 unsigned int idx;
6819 if (debug_info_entry >= num_debug_info_entries)
6821 warn (_("No debug information for loc lists of entry: %u\n"),
6822 debug_info_entry);
6823 return;
6826 cu_offset = debug_information [debug_info_entry].cu_offset;
6827 pointer_size = debug_information [debug_info_entry].pointer_size;
6828 offset_size = debug_information [debug_info_entry].offset_size;
6829 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6831 if (pointer_size < 2 || pointer_size > 8)
6833 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6834 pointer_size, debug_info_entry);
6835 return;
6838 while (1)
6840 printf (" ");
6841 print_hex (offset + (start - *start_ptr), 4);
6843 if (start >= section_end)
6845 warn (_("Location list starting at offset %#" PRIx64
6846 " is not terminated.\n"), offset);
6847 break;
6850 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
6852 if (vstart)
6853 switch (entry_type)
6855 default:
6856 break;
6858 case 2:
6859 case 3:
6860 case 4:
6862 uint64_t view;
6863 uint64_t off = offset + (vstart - *start_ptr);
6865 READ_ULEB (view, vstart, section_end);
6866 print_view (view, 8);
6868 READ_ULEB (view, vstart, section_end);
6869 print_view (view, 8);
6871 printf (_("views at %8.8" PRIx64 " for:\n %*s "), off, 8, "");
6874 break;
6877 switch (entry_type)
6879 case 0: /* A terminating entry. */
6880 *start_ptr = start;
6881 *vstart_ptr = vstart;
6882 printf (_("<End of list>\n"));
6883 return;
6884 case 1: /* A base-address entry. */
6885 READ_ULEB (idx, start, section_end);
6886 print_addr_index (idx, 8);
6887 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
6888 printf (_("(base address selection entry)\n"));
6889 continue;
6890 case 2: /* A start/end entry. */
6891 READ_ULEB (idx, start, section_end);
6892 print_addr_index (idx, 8);
6893 READ_ULEB (idx, start, section_end);
6894 print_addr_index (idx, 8);
6895 break;
6896 case 3: /* A start/length entry. */
6897 READ_ULEB (idx, start, section_end);
6898 print_addr_index (idx, 8);
6899 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6900 printf ("%08x ", idx);
6901 break;
6902 case 4: /* An offset pair entry. */
6903 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6904 printf ("%08x ", idx);
6905 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6906 printf ("%08x ", idx);
6907 break;
6908 default:
6909 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
6910 *start_ptr = start;
6911 *vstart_ptr = vstart;
6912 return;
6915 if (2 > (size_t) (section_end - start))
6917 warn (_("Location list starting at offset %#" PRIx64
6918 " is not terminated.\n"), offset);
6919 break;
6922 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6923 if (length > (size_t) (section_end - start))
6925 warn (_("Location list starting at offset %#" PRIx64
6926 " is not terminated.\n"), offset);
6927 break;
6930 putchar ('(');
6931 need_frame_base = decode_location_expression (start,
6932 pointer_size,
6933 offset_size,
6934 dwarf_version,
6935 length,
6936 cu_offset, section);
6937 putchar (')');
6939 if (need_frame_base && !has_frame_base)
6940 printf (_(" [without DW_AT_frame_base]"));
6942 putchar ('\n');
6944 start += length;
6947 *start_ptr = start;
6948 *vstart_ptr = vstart;
6951 /* Sort array of indexes in ascending order of loc_offsets[idx] and
6952 loc_views. */
6954 static uint64_t *loc_offsets, *loc_views;
6956 static int
6957 loc_offsets_compar (const void *ap, const void *bp)
6959 uint64_t a = loc_offsets[*(const unsigned int *) ap];
6960 uint64_t b = loc_offsets[*(const unsigned int *) bp];
6962 int ret = (a > b) - (b > a);
6963 if (ret)
6964 return ret;
6966 a = loc_views[*(const unsigned int *) ap];
6967 b = loc_views[*(const unsigned int *) bp];
6969 ret = (a > b) - (b > a);
6971 return ret;
6974 static int
6975 display_offset_entry_loclists (struct dwarf_section *section)
6977 unsigned char * start = section->start;
6978 unsigned char * const end = start + section->size;
6980 introduce (section, false);
6984 uint64_t length;
6985 unsigned short version;
6986 unsigned char address_size;
6987 unsigned char segment_selector_size;
6988 uint32_t offset_entry_count;
6989 uint32_t i;
6990 bool is_64bit;
6992 printf (_("Table at Offset %#tx\n"), start - section->start);
6994 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
6995 if (length == 0xffffffff)
6997 is_64bit = true;
6998 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
7000 else
7001 is_64bit = false;
7003 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
7004 SAFE_BYTE_GET_AND_INC (address_size, start, 1, end);
7005 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, end);
7006 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, end);
7008 printf (_(" Length: %#" PRIx64 "\n"), length);
7009 printf (_(" DWARF version: %u\n"), version);
7010 printf (_(" Address size: %u\n"), address_size);
7011 printf (_(" Segment size: %u\n"), segment_selector_size);
7012 printf (_(" Offset entries: %u\n"), offset_entry_count);
7014 if (version < 5)
7016 warn (_("The %s section contains a corrupt or "
7017 "unsupported version number: %d.\n"),
7018 section->name, version);
7019 return 0;
7022 if (segment_selector_size != 0)
7024 warn (_("The %s section contains an "
7025 "unsupported segment selector size: %d.\n"),
7026 section->name, segment_selector_size);
7027 return 0;
7030 if (offset_entry_count == 0)
7032 warn (_("The %s section contains a table without offset\n"),
7033 section->name);
7034 return 0;
7037 printf (_("\n Offset Entries starting at %#tx:\n"),
7038 start - section->start);
7040 for (i = 0; i < offset_entry_count; i++)
7042 uint64_t entry;
7044 SAFE_BYTE_GET_AND_INC (entry, start, is_64bit ? 8 : 4, end);
7045 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
7048 putchar ('\n');
7050 uint32_t j;
7052 for (j = 1, i = 0; i < offset_entry_count;)
7054 unsigned char lle;
7055 uint64_t base_address = 0;
7056 uint64_t begin;
7057 uint64_t finish;
7058 uint64_t off = start - section->start;
7060 if (j != i)
7062 printf (_(" Offset Entry %u\n"), i);
7063 j = i;
7066 printf (" ");
7067 print_hex (off, 4);
7069 SAFE_BYTE_GET_AND_INC (lle, start, 1, end);
7071 switch (lle)
7073 case DW_LLE_end_of_list:
7074 printf (_("<End of list>\n\n"));
7075 i ++;
7076 continue;
7078 case DW_LLE_base_addressx:
7079 READ_ULEB (base_address, start, end);
7080 print_hex (base_address, address_size);
7081 printf (_("(index into .debug_addr) "));
7082 base_address = fetch_indexed_addr (base_address, address_size);
7083 print_hex (base_address, address_size);
7084 printf (_("(base address)\n"));
7085 continue;
7087 case DW_LLE_startx_endx:
7088 READ_ULEB (begin, start, end);
7089 begin = fetch_indexed_addr (begin, address_size);
7090 READ_ULEB (finish, start, end);
7091 finish = fetch_indexed_addr (finish, address_size);
7092 break;
7094 case DW_LLE_startx_length:
7095 READ_ULEB (begin, start, end);
7096 begin = fetch_indexed_addr (begin, address_size);
7097 READ_ULEB (finish, start, end);
7098 finish += begin;
7099 break;
7101 case DW_LLE_offset_pair:
7102 READ_ULEB (begin, start, end);
7103 begin += base_address;
7104 READ_ULEB (finish, start, end);
7105 finish += base_address;
7106 break;
7108 case DW_LLE_default_location:
7109 begin = finish = 0;
7110 break;
7112 case DW_LLE_base_address:
7113 SAFE_BYTE_GET_AND_INC (base_address, start, address_size, end);
7114 print_hex (base_address, address_size);
7115 printf (_("(base address)\n"));
7116 continue;
7118 case DW_LLE_start_end:
7119 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7120 SAFE_BYTE_GET_AND_INC (finish, start, address_size, end);
7121 break;
7123 case DW_LLE_start_length:
7124 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7125 READ_ULEB (finish, start, end);
7126 finish += begin;
7127 break;
7129 default:
7130 error (_("Invalid location list entry type %d\n"), lle);
7131 return 0;
7134 if (start == end)
7136 warn (_("Location list starting at offset %#" PRIx64
7137 " is not terminated.\n"), off);
7138 break;
7141 print_hex (begin, address_size);
7142 print_hex (finish, address_size);
7144 if (begin == finish)
7145 fputs (_("(start == end)"), stdout);
7146 else if (begin > finish)
7147 fputs (_("(start > end)"), stdout);
7149 /* Read the counted location descriptions. */
7150 READ_ULEB (length, start, end);
7152 if (length > (size_t) (end - start))
7154 warn (_("Location list starting at offset %#" PRIx64
7155 " is not terminated.\n"), off);
7156 break;
7159 (void) decode_location_expression (start, address_size, address_size,
7160 version, length, 0, section);
7161 start += length;
7162 putchar ('\n');
7165 putchar ('\n');
7167 while (start < end);
7169 return 1;
7172 static int
7173 display_debug_loc (struct dwarf_section *section, void *file)
7175 unsigned char *start = section->start, *vstart = NULL;
7176 uint64_t bytes;
7177 unsigned char *section_begin = start;
7178 unsigned int num_loc_list = 0;
7179 uint64_t last_offset = 0;
7180 uint64_t last_view = 0;
7181 unsigned int first = 0;
7182 unsigned int i;
7183 unsigned int j;
7184 int seen_first_offset = 0;
7185 int locs_sorted = 1;
7186 unsigned char *next = start, *vnext = vstart;
7187 unsigned int *array = NULL;
7188 const char *suffix = strrchr (section->name, '.');
7189 bool is_dwo = false;
7190 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
7191 uint64_t header_size = 0;
7193 if (suffix && strcmp (suffix, ".dwo") == 0)
7194 is_dwo = true;
7196 bytes = section->size;
7198 if (bytes == 0)
7200 printf (_("\nThe %s section is empty.\n"), section->name);
7201 return 0;
7204 if (is_loclists)
7206 unsigned char *hdrptr = section_begin;
7207 uint64_t ll_length;
7208 unsigned short ll_version;
7209 unsigned char *end = section_begin + section->size;
7210 unsigned char address_size, segment_selector_size;
7211 uint32_t offset_entry_count;
7213 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
7214 if (ll_length == 0xffffffff)
7215 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
7217 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
7218 if (ll_version != 5)
7220 warn (_("The %s section contains corrupt or "
7221 "unsupported version number: %d.\n"),
7222 section->name, ll_version);
7223 return 0;
7226 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
7228 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
7229 if (segment_selector_size != 0)
7231 warn (_("The %s section contains "
7232 "unsupported segment selector size: %d.\n"),
7233 section->name, segment_selector_size);
7234 return 0;
7237 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
7239 if (offset_entry_count != 0)
7240 return display_offset_entry_loclists (section);
7242 header_size = hdrptr - section_begin;
7245 if (load_debug_info (file) == 0)
7247 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7248 section->name);
7249 return 0;
7252 /* Check the order of location list in .debug_info section. If
7253 offsets of location lists are in the ascending order, we can
7254 use `debug_information' directly. */
7255 for (i = 0; i < num_debug_info_entries; i++)
7257 unsigned int num;
7259 num = debug_information [i].num_loc_offsets;
7260 if (num > num_loc_list)
7261 num_loc_list = num;
7263 /* Check if we can use `debug_information' directly. */
7264 if (locs_sorted && num != 0)
7266 if (!seen_first_offset)
7268 /* This is the first location list. */
7269 last_offset = debug_information [i].loc_offsets [0];
7270 last_view = debug_information [i].loc_views [0];
7271 first = i;
7272 seen_first_offset = 1;
7273 j = 1;
7275 else
7276 j = 0;
7278 for (; j < num; j++)
7280 if (last_offset >
7281 debug_information [i].loc_offsets [j]
7282 || (last_offset == debug_information [i].loc_offsets [j]
7283 && last_view > debug_information [i].loc_views [j]))
7285 locs_sorted = 0;
7286 break;
7288 last_offset = debug_information [i].loc_offsets [j];
7289 last_view = debug_information [i].loc_views [j];
7294 if (!seen_first_offset)
7295 error (_("No location lists in .debug_info section!\n"));
7297 if (debug_information [first].num_loc_offsets > 0
7298 && debug_information [first].loc_offsets [0] != header_size
7299 && debug_information [first].loc_views [0] != header_size)
7300 warn (_("Location lists in %s section start at %#" PRIx64
7301 " rather than %#" PRIx64 "\n"),
7302 section->name, debug_information [first].loc_offsets [0],
7303 header_size);
7305 if (!locs_sorted)
7306 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
7308 introduce (section, false);
7310 if (reloc_at (section, 0))
7311 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
7313 printf (_(" Offset Begin End Expression\n"));
7315 for (i = first; i < num_debug_info_entries; i++)
7317 uint64_t offset = 0, voffset = 0;
7318 uint64_t base_address;
7319 unsigned int k;
7320 int has_frame_base;
7322 if (!locs_sorted)
7324 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7325 array[k] = k;
7326 loc_offsets = debug_information [i].loc_offsets;
7327 loc_views = debug_information [i].loc_views;
7328 qsort (array, debug_information [i].num_loc_offsets,
7329 sizeof (*array), loc_offsets_compar);
7332 /* .debug_loclists has a per-unit header.
7333 Update start if we are detecting it. */
7334 if (debug_information [i].dwarf_version == 5)
7336 j = locs_sorted ? 0 : array [0];
7338 if (debug_information [i].num_loc_offsets)
7339 offset = debug_information [i].loc_offsets [j];
7341 if (debug_information [i].num_loc_views)
7342 voffset = debug_information [i].loc_views [j];
7344 /* Assume that the size of the header is constant across CUs. */
7345 if (((start - section_begin) + header_size == offset)
7346 || ((start -section_begin) + header_size == voffset))
7347 start += header_size;
7350 int adjacent_view_loclists = 1;
7351 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7353 j = locs_sorted ? k : array[k];
7354 if (k
7355 && (debug_information [i].loc_offsets [locs_sorted
7356 ? k - 1 : array [k - 1]]
7357 == debug_information [i].loc_offsets [j])
7358 && (debug_information [i].loc_views [locs_sorted
7359 ? k - 1 : array [k - 1]]
7360 == debug_information [i].loc_views [j]))
7361 continue;
7362 has_frame_base = debug_information [i].have_frame_base [j];
7363 offset = debug_information [i].loc_offsets [j];
7364 next = section_begin + offset;
7365 voffset = debug_information [i].loc_views [j];
7366 if (voffset != (uint64_t) -1)
7367 vnext = section_begin + voffset;
7368 else
7369 vnext = NULL;
7370 base_address = debug_information [i].base_address;
7372 if (vnext && vnext < next)
7374 vstart = vnext;
7375 display_view_pair_list (section, &vstart, i, next);
7376 if (start == vnext)
7377 start = vstart;
7380 if (start < next)
7382 if (vnext && vnext < next)
7383 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7384 " in %s section.\n"),
7385 start - section_begin, voffset, section->name);
7386 else
7387 warn (_("There is a hole [%#tx - %#" PRIx64 "]"
7388 " in %s section.\n"),
7389 start - section_begin, offset, section->name);
7391 else if (start > next)
7392 warn (_("There is an overlap [%#tx - %#" PRIx64 "]"
7393 " in %s section.\n"),
7394 start - section_begin, offset, section->name);
7395 start = next;
7396 vstart = vnext;
7398 if (offset >= bytes)
7400 warn (_("Offset %#" PRIx64 " is bigger than %s section size.\n"),
7401 offset, section->name);
7402 continue;
7405 if (vnext && voffset >= bytes)
7407 warn (_("View Offset %#" PRIx64 " is bigger than %s section size.\n"),
7408 voffset, section->name);
7409 continue;
7412 if (!is_loclists)
7414 if (is_dwo)
7415 display_loc_list_dwo (section, &start, i, offset,
7416 &vstart, has_frame_base);
7417 else
7418 display_loc_list (section, &start, i, offset, base_address,
7419 &vstart, has_frame_base);
7421 else
7423 if (is_dwo)
7424 warn (_("DWO is not yet supported.\n"));
7425 else
7426 display_loclists_list (section, &start, i, offset, base_address,
7427 &vstart, has_frame_base);
7430 /* FIXME: this arrangement is quite simplistic. Nothing
7431 requires locview lists to be adjacent to corresponding
7432 loclists, and a single loclist could be augmented by
7433 different locview lists, and vice-versa, unlikely as it
7434 is that it would make sense to do so. Hopefully we'll
7435 have view pair support built into loclists before we ever
7436 need to address all these possibilities. */
7437 if (adjacent_view_loclists && vnext
7438 && vnext != start && vstart != next)
7440 adjacent_view_loclists = 0;
7441 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
7444 if (vnext && vnext == start)
7445 display_view_pair_list (section, &start, i, vstart);
7449 if (start < section->start + section->size)
7450 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7451 "There are %ld unused bytes at the end of section %s\n",
7452 (long) (section->start + section->size - start)),
7453 (long) (section->start + section->size - start), section->name);
7454 putchar ('\n');
7455 free (array);
7456 return 1;
7459 static int
7460 display_debug_str (struct dwarf_section *section,
7461 void *file ATTRIBUTE_UNUSED)
7463 unsigned char *start = section->start;
7464 uint64_t bytes = section->size;
7465 uint64_t addr = section->address;
7467 if (bytes == 0)
7469 printf (_("\nThe %s section is empty.\n"), section->name);
7470 return 0;
7473 introduce (section, false);
7475 while (bytes)
7477 int j;
7478 int k;
7479 int lbytes;
7481 lbytes = (bytes > 16 ? 16 : bytes);
7483 printf (" 0x%8.8" PRIx64 " ", addr);
7485 for (j = 0; j < 16; j++)
7487 if (j < lbytes)
7488 printf ("%2.2x", start[j]);
7489 else
7490 printf (" ");
7492 if ((j & 3) == 3)
7493 printf (" ");
7496 for (j = 0; j < lbytes; j++)
7498 k = start[j];
7499 if (k >= ' ' && k < 0x80)
7500 printf ("%c", k);
7501 else
7502 printf (".");
7505 putchar ('\n');
7507 start += lbytes;
7508 addr += lbytes;
7509 bytes -= lbytes;
7512 putchar ('\n');
7514 return 1;
7517 static int
7518 display_debug_info (struct dwarf_section *section, void *file)
7520 return process_debug_info (section, file, section->abbrev_sec, false, false);
7523 static int
7524 display_debug_types (struct dwarf_section *section, void *file)
7526 return process_debug_info (section, file, section->abbrev_sec, false, true);
7529 static int
7530 display_trace_info (struct dwarf_section *section, void *file)
7532 return process_debug_info (section, file, section->abbrev_sec, false, true);
7535 static int
7536 display_debug_aranges (struct dwarf_section *section,
7537 void *file ATTRIBUTE_UNUSED)
7539 unsigned char *start = section->start;
7540 unsigned char *end = start + section->size;
7542 introduce (section, false);
7544 /* It does not matter if this load fails,
7545 we test for that later on. */
7546 load_debug_info (file);
7548 while (start < end)
7550 unsigned char *hdrptr;
7551 DWARF2_Internal_ARange arange;
7552 unsigned char *addr_ranges;
7553 uint64_t length;
7554 uint64_t address;
7555 uint64_t sec_off;
7556 unsigned char address_size;
7557 unsigned int offset_size;
7558 unsigned char *end_ranges;
7560 hdrptr = start;
7561 sec_off = hdrptr - section->start;
7563 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
7564 if (arange.ar_length == 0xffffffff)
7566 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
7567 offset_size = 8;
7569 else
7570 offset_size = 4;
7572 if (arange.ar_length > (size_t) (end - hdrptr))
7574 warn (_("Debug info is corrupted, %s header at %#" PRIx64
7575 " has length %#" PRIx64 "\n"),
7576 section->name, sec_off, arange.ar_length);
7577 break;
7579 end_ranges = hdrptr + arange.ar_length;
7581 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end_ranges);
7582 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size,
7583 end_ranges);
7585 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7586 && num_debug_info_entries > 0
7587 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7588 warn (_(".debug_info offset of %#" PRIx64
7589 " in %s section does not point to a CU header.\n"),
7590 arange.ar_info_offset, section->name);
7592 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end_ranges);
7593 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end_ranges);
7595 if (arange.ar_version != 2 && arange.ar_version != 3)
7597 /* PR 19872: A version number of 0 probably means that there is
7598 padding at the end of the .debug_aranges section. Gold puts
7599 it there when performing an incremental link, for example.
7600 So do not generate a warning in this case. */
7601 if (arange.ar_version)
7602 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
7603 break;
7606 printf (_(" Length: %" PRId64 "\n"), arange.ar_length);
7607 printf (_(" Version: %d\n"), arange.ar_version);
7608 printf (_(" Offset into .debug_info: %#" PRIx64 "\n"),
7609 arange.ar_info_offset);
7610 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
7611 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
7613 address_size = arange.ar_pointer_size + arange.ar_segment_size;
7615 /* PR 17512: file: 001-108546-0.001:0.1. */
7616 if (address_size == 0 || address_size > 8)
7618 error (_("Invalid address size in %s section!\n"),
7619 section->name);
7620 break;
7623 /* The DWARF spec does not require that the address size be a power
7624 of two, but we do. This will have to change if we ever encounter
7625 an uneven architecture. */
7626 if ((address_size & (address_size - 1)) != 0)
7628 warn (_("Pointer size + Segment size is not a power of two.\n"));
7629 break;
7632 if (address_size > 4)
7633 printf (_("\n Address Length\n"));
7634 else
7635 printf (_("\n Address Length\n"));
7637 addr_ranges = hdrptr;
7639 /* Must pad to an alignment boundary that is twice the address size. */
7640 addr_ranges += (2 * address_size - 1
7641 - (hdrptr - start - 1) % (2 * address_size));
7643 while (2 * address_size <= end_ranges - addr_ranges)
7645 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size,
7646 end_ranges);
7647 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size,
7648 end_ranges);
7649 printf (" ");
7650 print_hex (address, address_size);
7651 print_hex_ns (length, address_size);
7652 putchar ('\n');
7655 start = end_ranges;
7658 printf ("\n");
7660 return 1;
7663 /* Comparison function for qsort. */
7664 static int
7665 comp_addr_base (const void * v0, const void * v1)
7667 debug_info *info0 = *(debug_info **) v0;
7668 debug_info *info1 = *(debug_info **) v1;
7669 return info0->addr_base - info1->addr_base;
7672 /* Display the debug_addr section. */
7673 static int
7674 display_debug_addr (struct dwarf_section *section,
7675 void *file)
7677 debug_info **debug_addr_info;
7678 unsigned char *entry;
7679 unsigned char *end;
7680 unsigned int i;
7681 unsigned int count;
7682 unsigned char * header;
7684 if (section->size == 0)
7686 printf (_("\nThe %s section is empty.\n"), section->name);
7687 return 0;
7690 if (load_debug_info (file) == 0)
7692 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7693 section->name);
7694 return 0;
7697 introduce (section, false);
7699 /* PR 17531: file: cf38d01b.
7700 We use xcalloc because a corrupt file may not have initialised all of the
7701 fields in the debug_info structure, which means that the sort below might
7702 try to move uninitialised data. */
7703 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
7704 sizeof (debug_info *));
7706 count = 0;
7707 for (i = 0; i < num_debug_info_entries; i++)
7708 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
7710 /* PR 17531: file: cf38d01b. */
7711 if (debug_information[i].addr_base >= section->size)
7712 warn (_("Corrupt address base (%#" PRIx64 ")"
7713 " found in debug section %u\n"),
7714 debug_information[i].addr_base, i);
7715 else
7716 debug_addr_info [count++] = debug_information + i;
7719 /* Add a sentinel to make iteration convenient. */
7720 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7721 debug_addr_info [count]->addr_base = section->size;
7722 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
7724 header = section->start;
7725 for (i = 0; i < count; i++)
7727 unsigned int idx;
7728 unsigned int address_size = debug_addr_info [i]->pointer_size;
7730 printf (_(" For compilation unit at offset %#" PRIx64 ":\n"),
7731 debug_addr_info [i]->cu_offset);
7733 printf (_("\tIndex\tAddress\n"));
7734 entry = section->start + debug_addr_info [i]->addr_base;
7735 if (debug_addr_info [i]->dwarf_version >= 5)
7737 size_t header_size = entry - header;
7738 unsigned char *curr_header = header;
7739 uint64_t length;
7740 int version;
7741 int segment_selector_size;
7743 if (header_size != 8 && header_size != 16)
7745 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %zd instead\n"),
7746 section->name, header_size);
7747 return 0;
7750 SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7751 if (length == 0xffffffff)
7752 SAFE_BYTE_GET_AND_INC (length, curr_header, 8, entry);
7753 if (length > (size_t) (section->start + section->size - curr_header)
7754 || length < (size_t) (entry - curr_header))
7756 warn (_("Corrupt %s section: unit_length field of %#" PRIx64
7757 " is invalid\n"), section->name, length);
7758 return 0;
7760 end = curr_header + length;
7761 SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7762 if (version != 5)
7763 warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7764 section->name, version);
7766 SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7767 SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7768 address_size += segment_selector_size;
7770 else
7771 end = section->start + debug_addr_info [i + 1]->addr_base;
7773 header = end;
7774 idx = 0;
7776 if (address_size < 1 || address_size > sizeof (uint64_t))
7778 warn (_("Corrupt %s section: address size (%x) is wrong"),
7779 section->name, address_size);
7780 return 0;
7783 while ((size_t) (end - entry) >= address_size)
7785 uint64_t base = byte_get (entry, address_size);
7786 printf (_("\t%d:\t"), idx);
7787 print_hex_ns (base, address_size);
7788 printf ("\n");
7789 entry += address_size;
7790 idx++;
7793 printf ("\n");
7795 free (debug_addr_info);
7796 return 1;
7799 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
7801 static int
7802 display_debug_str_offsets (struct dwarf_section *section,
7803 void *file ATTRIBUTE_UNUSED)
7805 unsigned long idx;
7807 if (section->size == 0)
7809 printf (_("\nThe %s section is empty.\n"), section->name);
7810 return 0;
7813 unsigned char *start = section->start;
7814 unsigned char *end = start + section->size;
7815 unsigned char *curr = start;
7816 uint64_t debug_str_offsets_hdr_len;
7818 const char *suffix = strrchr (section->name, '.');
7819 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
7821 if (dwo)
7822 load_debug_section_with_follow (str_dwo, file);
7823 else
7824 load_debug_section_with_follow (str, file);
7826 introduce (section, false);
7828 while (curr < end)
7830 uint64_t length;
7831 uint64_t entry_length;
7833 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
7834 /* FIXME: We assume that this means 64-bit DWARF is being used. */
7835 if (length == 0xffffffff)
7837 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
7838 entry_length = 8;
7839 debug_str_offsets_hdr_len = 16;
7841 else
7843 entry_length = 4;
7844 debug_str_offsets_hdr_len = 8;
7847 unsigned char *entries_end;
7848 if (length == 0)
7850 /* This is probably an old style .debug_str_offset section which
7851 just contains offsets and no header (and the first offset is 0). */
7852 length = section->size;
7853 curr = section->start;
7854 entries_end = end;
7856 printf (_(" Length: %#" PRIx64 "\n"), length);
7857 printf (_(" Index Offset [String]\n"));
7859 else
7861 if (length <= (size_t) (end - curr))
7862 entries_end = curr + length;
7863 else
7865 warn (_("Section %s is too small %#" PRIx64 "\n"),
7866 section->name, section->size);
7867 entries_end = end;
7870 int version;
7871 SAFE_BYTE_GET_AND_INC (version, curr, 2, entries_end);
7872 if (version != 5)
7873 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
7875 int padding;
7876 SAFE_BYTE_GET_AND_INC (padding, curr, 2, entries_end);
7877 if (padding != 0)
7878 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
7880 printf (_(" Length: %#" PRIx64 "\n"), length);
7881 printf (_(" Version: %#x\n"), version);
7882 printf (_(" Index Offset [String]\n"));
7885 for (idx = 0; curr < entries_end; idx++)
7887 uint64_t offset;
7888 const unsigned char * string;
7890 if ((size_t) (entries_end - curr) < entry_length)
7891 /* Not enough space to read one entry_length, give up. */
7892 return 0;
7894 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, entries_end);
7895 if (dwo)
7896 string = (const unsigned char *)
7897 fetch_indexed_string (idx, NULL, entry_length, dwo, debug_str_offsets_hdr_len);
7898 else
7899 string = fetch_indirect_string (offset);
7901 printf (" %8lu ", idx);
7902 print_hex (offset, entry_length);
7903 printf (" %s\n", string);
7907 return 1;
7910 /* Each debug_information[x].range_lists[y] gets this representation for
7911 sorting purposes. */
7913 struct range_entry
7915 /* The debug_information[x].range_lists[y] value. */
7916 uint64_t ranges_offset;
7918 /* Original debug_information to find parameters of the data. */
7919 debug_info *debug_info_p;
7922 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
7924 static int
7925 range_entry_compar (const void *ap, const void *bp)
7927 const struct range_entry *a_re = (const struct range_entry *) ap;
7928 const struct range_entry *b_re = (const struct range_entry *) bp;
7929 const uint64_t a = a_re->ranges_offset;
7930 const uint64_t b = b_re->ranges_offset;
7932 return (a > b) - (b > a);
7935 static void
7936 display_debug_ranges_list (unsigned char * start,
7937 unsigned char * finish,
7938 unsigned int pointer_size,
7939 uint64_t offset,
7940 uint64_t base_address)
7942 while (start < finish)
7944 uint64_t begin;
7945 uint64_t end;
7947 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7948 if (start >= finish)
7949 break;
7950 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7952 printf (" ");
7953 print_hex (offset, 4);
7955 if (begin == 0 && end == 0)
7957 printf (_("<End of list>\n"));
7958 break;
7961 /* Check base address specifiers. */
7962 if (is_max_address (begin, pointer_size)
7963 && !is_max_address (end, pointer_size))
7965 base_address = end;
7966 print_hex (begin, pointer_size);
7967 print_hex (end, pointer_size);
7968 printf ("(base address)\n");
7969 continue;
7972 print_hex (begin + base_address, pointer_size);
7973 print_hex_ns (end + base_address, pointer_size);
7975 if (begin == end)
7976 fputs (_(" (start == end)"), stdout);
7977 else if (begin > end)
7978 fputs (_(" (start > end)"), stdout);
7980 putchar ('\n');
7984 static unsigned char *
7985 display_debug_rnglists_list (unsigned char * start,
7986 unsigned char * finish,
7987 unsigned int pointer_size,
7988 uint64_t offset,
7989 uint64_t base_address,
7990 unsigned int offset_size)
7992 unsigned char *next = start;
7993 unsigned int debug_addr_section_hdr_len;
7995 if (offset_size == 4)
7996 debug_addr_section_hdr_len = 8;
7997 else
7998 debug_addr_section_hdr_len = 16;
8000 while (1)
8002 uint64_t off = offset + (start - next);
8003 enum dwarf_range_list_entry rlet;
8004 /* Initialize it due to a false compiler warning. */
8005 uint64_t begin = -1, length, end = -1;
8007 if (start >= finish)
8009 warn (_("Range list starting at offset %#" PRIx64
8010 " is not terminated.\n"), offset);
8011 break;
8014 printf (" ");
8015 print_hex (off, 4);
8017 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
8019 switch (rlet)
8021 case DW_RLE_end_of_list:
8022 printf (_("<End of list>\n"));
8023 break;
8024 case DW_RLE_base_addressx:
8025 READ_ULEB (base_address, start, finish);
8026 print_hex (base_address, pointer_size);
8027 printf (_("(base address index) "));
8028 base_address = fetch_indexed_addr ((base_address * pointer_size)
8029 + debug_addr_section_hdr_len, pointer_size);
8030 print_hex (base_address, pointer_size);
8031 printf (_("(base address)\n"));
8032 break;
8033 case DW_RLE_startx_endx:
8034 READ_ULEB (begin, start, finish);
8035 READ_ULEB (end, start, finish);
8036 begin = fetch_indexed_addr ((begin * pointer_size)
8037 + debug_addr_section_hdr_len, pointer_size);
8038 end = fetch_indexed_addr ((begin * pointer_size)
8039 + debug_addr_section_hdr_len, pointer_size);
8040 break;
8041 case DW_RLE_startx_length:
8042 READ_ULEB (begin, start, finish);
8043 READ_ULEB (length, start, finish);
8044 begin = fetch_indexed_addr ((begin * pointer_size)
8045 + debug_addr_section_hdr_len, pointer_size);
8046 end = begin + length;
8047 break;
8048 case DW_RLE_offset_pair:
8049 READ_ULEB (begin, start, finish);
8050 READ_ULEB (end, start, finish);
8051 break;
8052 case DW_RLE_base_address:
8053 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
8054 print_hex (base_address, pointer_size);
8055 printf (_("(base address)\n"));
8056 break;
8057 case DW_RLE_start_end:
8058 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8059 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
8060 break;
8061 case DW_RLE_start_length:
8062 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8063 READ_ULEB (length, start, finish);
8064 end = begin + length;
8065 break;
8066 default:
8067 error (_("Invalid range list entry type %d\n"), rlet);
8068 rlet = DW_RLE_end_of_list;
8069 break;
8072 if (rlet == DW_RLE_end_of_list)
8073 break;
8074 if (rlet == DW_RLE_base_address || rlet == DW_RLE_base_addressx)
8075 continue;
8077 /* Only a DW_RLE_offset_pair needs the base address added. */
8078 if (rlet == DW_RLE_offset_pair)
8080 begin += base_address;
8081 end += base_address;
8084 print_hex (begin, pointer_size);
8085 print_hex (end, pointer_size);
8087 if (begin == end)
8088 fputs (_(" (start == end)"), stdout);
8089 else if (begin > end)
8090 fputs (_(" (start > end)"), stdout);
8092 putchar ('\n');
8095 return start;
8098 static int
8099 display_debug_rnglists (struct dwarf_section *section)
8101 unsigned char *start = section->start;
8102 unsigned char *finish = start + section->size;
8104 while (start < finish)
8106 unsigned char *table_start;
8107 uint64_t offset = start - section->start;
8108 unsigned char *end;
8109 uint64_t initial_length;
8110 unsigned char segment_selector_size;
8111 unsigned int offset_entry_count;
8112 unsigned int i;
8113 unsigned short version;
8114 unsigned char address_size = 0;
8115 unsigned char offset_size;
8117 /* Get and check the length of the block. */
8118 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
8120 if (initial_length == 0xffffffff)
8122 /* This section is 64-bit DWARF 3. */
8123 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
8124 offset_size = 8;
8126 else
8127 offset_size = 4;
8129 if (initial_length > (size_t) (finish - start))
8131 /* If the length field has a relocation against it, then we should
8132 not complain if it is inaccurate (and probably negative).
8133 It is copied from .debug_line handling code. */
8134 if (reloc_at (section, (start - section->start) - offset_size))
8135 initial_length = finish - start;
8136 else
8138 warn (_("The length field (%#" PRIx64
8139 ") in the debug_rnglists header is wrong"
8140 " - the section is too small\n"),
8141 initial_length);
8142 return 0;
8146 end = start + initial_length;
8148 /* Get the other fields in the header. */
8149 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
8150 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
8151 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
8152 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
8154 printf (_(" Table at Offset: %#" PRIx64 ":\n"), offset);
8155 printf (_(" Length: %#" PRIx64 "\n"), initial_length);
8156 printf (_(" DWARF version: %u\n"), version);
8157 printf (_(" Address size: %u\n"), address_size);
8158 printf (_(" Segment size: %u\n"), segment_selector_size);
8159 printf (_(" Offset entries: %u\n"), offset_entry_count);
8161 /* Check the fields. */
8162 if (segment_selector_size != 0)
8164 warn (_("The %s section contains "
8165 "unsupported segment selector size: %d.\n"),
8166 section->name, segment_selector_size);
8167 return 0;
8170 if (version < 5)
8172 warn (_("Only DWARF version 5+ debug_rnglists info "
8173 "is currently supported.\n"));
8174 return 0;
8177 table_start = start;
8179 if (offset_entry_count != 0)
8181 printf (_("\n Offsets starting at %#tx:\n"),
8182 start - section->start);
8184 for (i = 0; i < offset_entry_count; i++)
8186 uint64_t entry;
8188 SAFE_BYTE_GET_AND_INC (entry, start, offset_size, finish);
8189 printf (_(" [%6u] %#" PRIx64 "\n"), i, entry);
8192 else
8193 offset_entry_count = 1;
8195 for (i = 0; i < offset_entry_count; i++)
8197 uint64_t indx = start - table_start;
8199 offset = start - section->start;
8200 printf (_("\n Offset: %#" PRIx64 ", Index: %#" PRIx64 "\n"),
8201 offset, indx);
8202 printf (_(" Offset Begin End\n"));
8203 start = display_debug_rnglists_list
8204 (start, end, address_size, offset, 0, offset_size);
8205 if (start >= end)
8206 break;
8209 start = end;
8211 if (start < finish)
8212 putchar ('\n');
8215 putchar ('\n');
8216 return 1;
8219 static int
8220 display_debug_ranges (struct dwarf_section *section,
8221 void *file ATTRIBUTE_UNUSED)
8223 unsigned char *start = section->start;
8224 unsigned char *last_start = start;
8225 uint64_t bytes = section->size;
8226 unsigned char *section_begin = start;
8227 unsigned char *finish = start + bytes;
8228 unsigned int num_range_list, i;
8229 struct range_entry *range_entries;
8230 struct range_entry *range_entry_fill;
8231 int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
8232 /* Initialize it due to a false compiler warning. */
8233 unsigned char address_size = 0;
8234 uint64_t last_offset = 0;
8236 if (bytes == 0)
8238 printf (_("\nThe %s section is empty.\n"), section->name);
8239 return 0;
8242 introduce (section, false);
8244 if (is_rnglists)
8245 return display_debug_rnglists (section);
8247 if (load_debug_info (file) == 0)
8249 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
8250 section->name);
8251 return 0;
8254 num_range_list = 0;
8255 for (i = 0; i < num_debug_info_entries; i++)
8256 num_range_list += debug_information [i].num_range_lists;
8258 if (num_range_list == 0)
8260 /* This can happen when the file was compiled with -gsplit-debug
8261 which removes references to range lists from the primary .o file. */
8262 printf (_("No range lists in .debug_info section.\n"));
8263 return 1;
8266 range_entries = (struct range_entry *)
8267 xmalloc (sizeof (*range_entries) * num_range_list);
8268 range_entry_fill = range_entries;
8270 for (i = 0; i < num_debug_info_entries; i++)
8272 debug_info *debug_info_p = &debug_information[i];
8273 unsigned int j;
8275 for (j = 0; j < debug_info_p->num_range_lists; j++)
8277 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
8278 range_entry_fill->debug_info_p = debug_info_p;
8279 range_entry_fill++;
8283 qsort (range_entries, num_range_list, sizeof (*range_entries),
8284 range_entry_compar);
8286 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
8287 warn (_("Range lists in %s section start at %#" PRIx64 "\n"),
8288 section->name, range_entries[0].ranges_offset);
8290 putchar ('\n');
8291 printf (_(" Offset Begin End\n"));
8293 for (i = 0; i < num_range_list; i++)
8295 struct range_entry *range_entry = &range_entries[i];
8296 debug_info *debug_info_p = range_entry->debug_info_p;
8297 unsigned int pointer_size;
8298 uint64_t offset;
8299 unsigned char *next;
8300 uint64_t base_address;
8302 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
8303 offset = range_entry->ranges_offset;
8304 base_address = debug_info_p->base_address;
8306 /* PR 17512: file: 001-101485-0.001:0.1. */
8307 if (pointer_size < 2 || pointer_size > 8)
8309 warn (_("Corrupt pointer size (%d) in debug entry at offset %#" PRIx64 "\n"),
8310 pointer_size, offset);
8311 continue;
8314 if (offset > (size_t) (finish - section_begin))
8316 warn (_("Corrupt offset (%#" PRIx64 ") in range entry %u\n"),
8317 offset, i);
8318 continue;
8321 next = section_begin + offset + debug_info_p->rnglists_base;
8323 /* If multiple DWARF entities reference the same range then we will
8324 have multiple entries in the `range_entries' list for the same
8325 offset. Thanks to the sort above these will all be consecutive in
8326 the `range_entries' list, so we can easily ignore duplicates
8327 here. */
8328 if (i > 0 && last_offset == offset)
8329 continue;
8330 last_offset = offset;
8332 if (dwarf_check != 0 && i > 0)
8334 if (start < next)
8335 warn (_("There is a hole [%#tx - %#tx] in %s section.\n"),
8336 start - section_begin, next - section_begin, section->name);
8337 else if (start > next)
8339 if (next == last_start)
8340 continue;
8341 warn (_("There is an overlap [%#tx - %#tx] in %s section.\n"),
8342 start - section_begin, next - section_begin, section->name);
8346 start = next;
8347 last_start = next;
8349 display_debug_ranges_list
8350 (start, finish, pointer_size, offset, base_address);
8352 putchar ('\n');
8354 free (range_entries);
8356 return 1;
8359 typedef struct Frame_Chunk
8361 struct Frame_Chunk *next;
8362 unsigned char *chunk_start;
8363 unsigned int ncols;
8364 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8365 short int *col_type;
8366 int64_t *col_offset;
8367 char *augmentation;
8368 unsigned int code_factor;
8369 int data_factor;
8370 uint64_t pc_begin;
8371 uint64_t pc_range;
8372 unsigned int cfa_reg;
8373 uint64_t cfa_offset;
8374 unsigned int ra;
8375 unsigned char fde_encoding;
8376 unsigned char cfa_exp;
8377 unsigned char ptr_size;
8378 unsigned char segment_size;
8380 Frame_Chunk;
8382 typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
8383 static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
8384 static const char *const *dwarf_regnames;
8385 static unsigned int dwarf_regnames_count;
8386 static bool is_aarch64;
8388 /* A marker for a col_type that means this column was never referenced
8389 in the frame info. */
8390 #define DW_CFA_unreferenced (-1)
8392 /* Return 0 if no more space is needed, 1 if more space is needed,
8393 -1 for invalid reg. */
8395 static int
8396 frame_need_space (Frame_Chunk *fc, unsigned int reg)
8398 unsigned int prev = fc->ncols;
8400 if (reg < (unsigned int) fc->ncols)
8401 return 0;
8403 if (dwarf_regnames_count > 0
8404 && reg > dwarf_regnames_count)
8405 return -1;
8407 fc->ncols = reg + 1;
8408 /* PR 17512: file: 10450-2643-0.004.
8409 If reg == -1 then this can happen... */
8410 if (fc->ncols == 0)
8411 return -1;
8413 /* PR 17512: file: 2844a11d. */
8414 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
8416 error (_("Unfeasibly large register number: %u\n"), reg);
8417 fc->ncols = 0;
8418 /* FIXME: 1024 is an arbitrary limit. Increase it if
8419 we ever encounter a valid binary that exceeds it. */
8420 return -1;
8423 fc->col_type = xcrealloc (fc->col_type, fc->ncols,
8424 sizeof (*fc->col_type));
8425 fc->col_offset = xcrealloc (fc->col_offset, fc->ncols,
8426 sizeof (*fc->col_offset));
8427 /* PR 17512: file:002-10025-0.005. */
8428 if (fc->col_type == NULL || fc->col_offset == NULL)
8430 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
8431 fc->ncols);
8432 fc->ncols = 0;
8433 return -1;
8436 while (prev < fc->ncols)
8438 fc->col_type[prev] = DW_CFA_unreferenced;
8439 fc->col_offset[prev] = 0;
8440 prev++;
8442 return 1;
8445 static const char *const dwarf_regnames_i386[] =
8447 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8448 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8449 "eip", "eflags", NULL, /* 8 - 10 */
8450 "st0", "st1", "st2", "st3", /* 11 - 14 */
8451 "st4", "st5", "st6", "st7", /* 15 - 18 */
8452 NULL, NULL, /* 19 - 20 */
8453 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
8454 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
8455 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
8456 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
8457 "fcw", "fsw", "mxcsr", /* 37 - 39 */
8458 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8459 "tr", "ldtr", /* 48 - 49 */
8460 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8461 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8462 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8463 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8464 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8465 NULL, NULL, NULL, /* 90 - 92 */
8466 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
8469 static const char *const dwarf_regnames_iamcu[] =
8471 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8472 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8473 "eip", "eflags", NULL, /* 8 - 10 */
8474 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
8475 NULL, NULL, /* 19 - 20 */
8476 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
8477 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
8478 NULL, NULL, NULL, /* 37 - 39 */
8479 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8480 "tr", "ldtr", /* 48 - 49 */
8481 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8482 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8483 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8484 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8485 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8486 NULL, NULL, NULL, /* 90 - 92 */
8487 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
8490 static void
8491 init_dwarf_regnames_i386 (void)
8493 dwarf_regnames = dwarf_regnames_i386;
8494 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
8495 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8498 static void
8499 init_dwarf_regnames_iamcu (void)
8501 dwarf_regnames = dwarf_regnames_iamcu;
8502 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
8503 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8506 static const char *const DW_CFA_GNU_window_save_name[] =
8508 "DW_CFA_GNU_window_save",
8509 "DW_CFA_AARCH64_negate_ra_state"
8512 static const char *const dwarf_regnames_x86_64[] =
8514 "rax", "rdx", "rcx", "rbx",
8515 "rsi", "rdi", "rbp", "rsp",
8516 "r8", "r9", "r10", "r11",
8517 "r12", "r13", "r14", "r15",
8518 "rip",
8519 "xmm0", "xmm1", "xmm2", "xmm3",
8520 "xmm4", "xmm5", "xmm6", "xmm7",
8521 "xmm8", "xmm9", "xmm10", "xmm11",
8522 "xmm12", "xmm13", "xmm14", "xmm15",
8523 "st0", "st1", "st2", "st3",
8524 "st4", "st5", "st6", "st7",
8525 "mm0", "mm1", "mm2", "mm3",
8526 "mm4", "mm5", "mm6", "mm7",
8527 "rflags",
8528 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8529 "fs.base", "gs.base", NULL, NULL,
8530 "tr", "ldtr",
8531 "mxcsr", "fcw", "fsw",
8532 "xmm16", "xmm17", "xmm18", "xmm19",
8533 "xmm20", "xmm21", "xmm22", "xmm23",
8534 "xmm24", "xmm25", "xmm26", "xmm27",
8535 "xmm28", "xmm29", "xmm30", "xmm31",
8536 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
8537 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
8538 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
8539 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
8540 NULL, NULL, NULL, /* 115 - 117 */
8541 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
8544 static void
8545 init_dwarf_regnames_x86_64 (void)
8547 dwarf_regnames = dwarf_regnames_x86_64;
8548 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
8549 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8552 static const char *const dwarf_regnames_aarch64[] =
8554 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
8555 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
8556 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8557 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8558 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
8559 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
8560 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
8561 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
8562 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
8563 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
8564 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8565 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
8566 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
8567 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8568 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8569 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
8572 static void
8573 init_dwarf_regnames_aarch64 (void)
8575 dwarf_regnames = dwarf_regnames_aarch64;
8576 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
8577 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8578 is_aarch64 = true;
8581 static const char *const dwarf_regnames_s390[] =
8583 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
8584 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8585 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8586 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
8587 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
8588 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8589 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8590 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
8591 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
8592 "pswm", "pswa",
8593 NULL, NULL,
8594 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8595 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8598 static void
8599 init_dwarf_regnames_s390 (void)
8601 dwarf_regnames = dwarf_regnames_s390;
8602 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
8603 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8606 static const char *const dwarf_regnames_riscv[] =
8608 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
8609 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
8610 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
8611 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
8612 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
8613 "fs0", "fs1", /* 40 - 41 */
8614 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
8615 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
8616 "fs10", "fs11", /* 58 - 59 */
8617 "ft8", "ft9", "ft10", "ft11", /* 60 - 63 */
8618 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 64 - 71 */
8619 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 72 - 79 */
8620 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 80 - 87 */
8621 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 88 - 95 */
8622 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", /* 96 - 103 */
8623 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", /* 104 - 111 */
8624 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", /* 112 - 119 */
8625 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31", /* 120 - 127 */
8628 /* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8629 the large number of CSRs. */
8631 static const char *
8632 regname_internal_riscv (unsigned int regno)
8634 const char *name = NULL;
8636 /* Lookup in the table first, this covers GPR and FPR. */
8637 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8638 name = dwarf_regnames_riscv [regno];
8639 else if (regno >= 4096 && regno <= 8191)
8641 /* This might be a CSR, these live in a sparse number space from 4096
8642 to 8191 These numbers are defined in the RISC-V ELF ABI
8643 document. */
8644 switch (regno)
8646 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8647 case VALUE + 4096: name = #NAME; break;
8648 #include "opcode/riscv-opc.h"
8649 #undef DECLARE_CSR
8651 default:
8653 static char csr_name[10];
8654 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8655 name = csr_name;
8657 break;
8661 return name;
8664 static void
8665 init_dwarf_regnames_riscv (void)
8667 dwarf_regnames = NULL;
8668 dwarf_regnames_count = 8192;
8669 dwarf_regnames_lookup_func = regname_internal_riscv;
8672 void
8673 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
8675 dwarf_regnames_lookup_func = NULL;
8676 is_aarch64 = false;
8678 switch (e_machine)
8680 case EM_386:
8681 init_dwarf_regnames_i386 ();
8682 break;
8684 case EM_IAMCU:
8685 init_dwarf_regnames_iamcu ();
8686 break;
8688 case EM_X86_64:
8689 case EM_L1OM:
8690 case EM_K1OM:
8691 init_dwarf_regnames_x86_64 ();
8692 break;
8694 case EM_AARCH64:
8695 init_dwarf_regnames_aarch64 ();
8696 break;
8698 case EM_S390:
8699 init_dwarf_regnames_s390 ();
8700 break;
8702 case EM_RISCV:
8703 init_dwarf_regnames_riscv ();
8704 break;
8706 default:
8707 break;
8711 /* Initialize the DWARF register name lookup state based on the
8712 architecture and specific machine type of a BFD. */
8714 void
8715 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8716 unsigned long mach)
8718 dwarf_regnames_lookup_func = NULL;
8719 is_aarch64 = false;
8721 switch (arch)
8723 case bfd_arch_i386:
8724 switch (mach)
8726 case bfd_mach_x86_64:
8727 case bfd_mach_x86_64_intel_syntax:
8728 case bfd_mach_x64_32:
8729 case bfd_mach_x64_32_intel_syntax:
8730 init_dwarf_regnames_x86_64 ();
8731 break;
8733 default:
8734 init_dwarf_regnames_i386 ();
8735 break;
8737 break;
8739 case bfd_arch_iamcu:
8740 init_dwarf_regnames_iamcu ();
8741 break;
8743 case bfd_arch_aarch64:
8744 init_dwarf_regnames_aarch64();
8745 break;
8747 case bfd_arch_s390:
8748 init_dwarf_regnames_s390 ();
8749 break;
8751 case bfd_arch_riscv:
8752 init_dwarf_regnames_riscv ();
8753 break;
8755 default:
8756 break;
8760 static const char *
8761 regname_internal_by_table_only (unsigned int regno)
8763 if (dwarf_regnames != NULL
8764 && regno < dwarf_regnames_count
8765 && dwarf_regnames [regno] != NULL)
8766 return dwarf_regnames [regno];
8768 return NULL;
8771 static const char *
8772 regname (unsigned int regno, int name_only_p)
8774 static char reg[64];
8776 const char *name = NULL;
8778 if (dwarf_regnames_lookup_func != NULL)
8779 name = dwarf_regnames_lookup_func (regno);
8781 if (name != NULL)
8783 if (name_only_p)
8784 return name;
8785 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
8787 else
8788 snprintf (reg, sizeof (reg), "r%d", regno);
8789 return reg;
8792 static void
8793 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
8795 unsigned int r;
8796 char tmp[100];
8798 if (*max_regs != fc->ncols)
8799 *max_regs = fc->ncols;
8801 if (*need_col_headers)
8803 *need_col_headers = 0;
8805 printf ("%-*s CFA ", eh_addr_size * 2, " LOC");
8807 for (r = 0; r < *max_regs; r++)
8808 if (fc->col_type[r] != DW_CFA_unreferenced)
8810 if (r == fc->ra)
8811 printf ("ra ");
8812 else
8813 printf ("%-5s ", regname (r, 1));
8816 printf ("\n");
8819 print_hex (fc->pc_begin, eh_addr_size);
8820 if (fc->cfa_exp)
8821 strcpy (tmp, "exp");
8822 else
8823 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
8824 printf ("%-8s ", tmp);
8826 for (r = 0; r < fc->ncols; r++)
8828 if (fc->col_type[r] != DW_CFA_unreferenced)
8830 switch (fc->col_type[r])
8832 case DW_CFA_undefined:
8833 strcpy (tmp, "u");
8834 break;
8835 case DW_CFA_same_value:
8836 strcpy (tmp, "s");
8837 break;
8838 case DW_CFA_offset:
8839 sprintf (tmp, "c%+" PRId64, fc->col_offset[r]);
8840 break;
8841 case DW_CFA_val_offset:
8842 sprintf (tmp, "v%+" PRId64, fc->col_offset[r]);
8843 break;
8844 case DW_CFA_register:
8845 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
8846 break;
8847 case DW_CFA_expression:
8848 strcpy (tmp, "exp");
8849 break;
8850 case DW_CFA_val_expression:
8851 strcpy (tmp, "vexp");
8852 break;
8853 default:
8854 strcpy (tmp, "n/a");
8855 break;
8857 printf ("%-5s ", tmp);
8860 printf ("\n");
8863 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
8865 static unsigned char *
8866 read_cie (unsigned char *start, unsigned char *end,
8867 Frame_Chunk **p_cie, int *p_version,
8868 uint64_t *p_aug_len, unsigned char **p_aug)
8870 int version;
8871 Frame_Chunk *fc;
8872 unsigned char *augmentation_data = NULL;
8873 uint64_t augmentation_data_len = 0;
8875 * p_cie = NULL;
8876 /* PR 17512: file: 001-228113-0.004. */
8877 if (start >= end)
8878 return end;
8880 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8881 memset (fc, 0, sizeof (Frame_Chunk));
8883 fc->col_type = xmalloc (sizeof (*fc->col_type));
8884 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
8886 version = *start++;
8888 fc->augmentation = (char *) start;
8889 /* PR 17512: file: 001-228113-0.004.
8890 Skip past augmentation name, but avoid running off the end of the data. */
8891 while (start < end)
8892 if (* start ++ == '\0')
8893 break;
8894 if (start == end)
8896 warn (_("No terminator for augmentation name\n"));
8897 goto fail;
8900 if (strcmp (fc->augmentation, "eh") == 0)
8902 if (eh_addr_size > (size_t) (end - start))
8903 goto fail;
8904 start += eh_addr_size;
8907 if (version >= 4)
8909 if (2 > (size_t) (end - start))
8910 goto fail;
8911 GET (fc->ptr_size, 1);
8912 if (fc->ptr_size < 1 || fc->ptr_size > 8)
8914 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
8915 goto fail;
8918 GET (fc->segment_size, 1);
8919 /* PR 17512: file: e99d2804. */
8920 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
8922 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
8923 goto fail;
8926 eh_addr_size = fc->ptr_size;
8928 else
8930 fc->ptr_size = eh_addr_size;
8931 fc->segment_size = 0;
8934 READ_ULEB (fc->code_factor, start, end);
8935 READ_SLEB (fc->data_factor, start, end);
8937 if (start >= end)
8938 goto fail;
8940 if (version == 1)
8942 GET (fc->ra, 1);
8944 else
8946 READ_ULEB (fc->ra, start, end);
8949 if (fc->augmentation[0] == 'z')
8951 if (start >= end)
8952 goto fail;
8953 READ_ULEB (augmentation_data_len, start, end);
8954 augmentation_data = start;
8955 /* PR 17512: file: 11042-2589-0.004. */
8956 if (augmentation_data_len > (size_t) (end - start))
8958 warn (_("Augmentation data too long: %#" PRIx64
8959 ", expected at most %#tx\n"),
8960 augmentation_data_len, end - start);
8961 goto fail;
8963 start += augmentation_data_len;
8966 if (augmentation_data_len)
8968 unsigned char *p;
8969 unsigned char *q;
8970 unsigned char *qend;
8972 p = (unsigned char *) fc->augmentation + 1;
8973 q = augmentation_data;
8974 qend = q + augmentation_data_len;
8976 while (p < end && q < qend)
8978 if (*p == 'L')
8979 q++;
8980 else if (*p == 'P')
8981 q += 1 + size_of_encoded_value (*q);
8982 else if (*p == 'R')
8983 fc->fde_encoding = *q++;
8984 else if (*p == 'S')
8986 else if (*p == 'B')
8988 else
8989 break;
8990 p++;
8992 /* Note - it is OK if this loop terminates with q < qend.
8993 Padding may have been inserted to align the end of the CIE. */
8996 *p_cie = fc;
8997 if (p_version)
8998 *p_version = version;
8999 if (p_aug_len)
9001 *p_aug_len = augmentation_data_len;
9002 *p_aug = augmentation_data;
9004 return start;
9006 fail:
9007 free (fc->col_offset);
9008 free (fc->col_type);
9009 free (fc);
9010 return end;
9013 /* Prints out the contents on the DATA array formatted as unsigned bytes.
9014 If do_wide is not enabled, then formats the output to fit into 80 columns.
9015 PRINTED contains the number of characters already written to the current
9016 output line. */
9018 static void
9019 display_data (size_t printed, const unsigned char *data, size_t len)
9021 if (do_wide || len < ((80 - printed) / 3))
9022 for (printed = 0; printed < len; ++printed)
9023 printf (" %02x", data[printed]);
9024 else
9026 for (printed = 0; printed < len; ++printed)
9028 if (printed % (80 / 3) == 0)
9029 putchar ('\n');
9030 printf (" %02x", data[printed]);
9035 /* Prints out the contents on the augmentation data array.
9036 If do_wide is not enabled, then formats the output to fit into 80 columns. */
9038 static void
9039 display_augmentation_data (const unsigned char * data, uint64_t len)
9041 size_t i;
9043 i = printf (_(" Augmentation data: "));
9044 display_data (i, data, len);
9047 static int
9048 display_debug_frames (struct dwarf_section *section,
9049 void *file ATTRIBUTE_UNUSED)
9051 unsigned char *start = section->start;
9052 unsigned char *end = start + section->size;
9053 unsigned char *section_start = start;
9054 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
9055 Frame_Chunk *remembered_state = NULL;
9056 Frame_Chunk *rs;
9057 bool is_eh = strcmp (section->name, ".eh_frame") == 0;
9058 unsigned int max_regs = 0;
9059 const char *bad_reg = _("bad register: ");
9060 unsigned int saved_eh_addr_size = eh_addr_size;
9062 introduce (section, false);
9064 while (start < end)
9066 unsigned char *saved_start;
9067 unsigned char *block_end;
9068 uint64_t length;
9069 uint64_t cie_id;
9070 Frame_Chunk *fc;
9071 Frame_Chunk *cie;
9072 int need_col_headers = 1;
9073 unsigned char *augmentation_data = NULL;
9074 uint64_t augmentation_data_len = 0;
9075 unsigned int encoded_ptr_size = saved_eh_addr_size;
9076 unsigned int offset_size;
9077 bool all_nops;
9078 static Frame_Chunk fde_fc;
9080 saved_start = start;
9082 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
9084 if (length == 0)
9086 printf ("\n%08tx ZERO terminator\n\n",
9087 saved_start - section_start);
9088 /* Skip any zero terminators that directly follow.
9089 A corrupt section size could have loaded a whole
9090 slew of zero filled memory bytes. eg
9091 PR 17512: file: 070-19381-0.004. */
9092 while (start < end && * start == 0)
9093 ++ start;
9094 continue;
9097 if (length == 0xffffffff)
9099 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
9100 offset_size = 8;
9102 else
9103 offset_size = 4;
9105 if (length > (size_t) (end - start))
9107 warn ("Invalid length %#" PRIx64 " in FDE at %#tx\n",
9108 length, saved_start - section_start);
9109 block_end = end;
9111 else
9112 block_end = start + length;
9114 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, block_end);
9116 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
9117 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
9119 int version;
9120 unsigned int mreg;
9122 start = read_cie (start, block_end, &cie, &version,
9123 &augmentation_data_len, &augmentation_data);
9124 /* PR 17512: file: 027-135133-0.005. */
9125 if (cie == NULL)
9126 break;
9128 fc = cie;
9129 fc->next = chunks;
9130 chunks = fc;
9131 fc->chunk_start = saved_start;
9132 mreg = max_regs > 0 ? max_regs - 1 : 0;
9133 if (mreg < fc->ra)
9134 mreg = fc->ra;
9135 if (frame_need_space (fc, mreg) < 0)
9136 break;
9137 if (fc->fde_encoding)
9138 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9140 printf ("\n%08tx ", saved_start - section_start);
9141 print_hex (length, fc->ptr_size);
9142 print_hex (cie_id, offset_size);
9144 if (do_debug_frames_interp)
9146 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
9147 fc->code_factor, fc->data_factor, fc->ra);
9149 else
9151 printf ("CIE\n");
9152 printf (" Version: %d\n", version);
9153 printf (" Augmentation: \"%s\"\n", fc->augmentation);
9154 if (version >= 4)
9156 printf (" Pointer Size: %u\n", fc->ptr_size);
9157 printf (" Segment Size: %u\n", fc->segment_size);
9159 printf (" Code alignment factor: %u\n", fc->code_factor);
9160 printf (" Data alignment factor: %d\n", fc->data_factor);
9161 printf (" Return address column: %d\n", fc->ra);
9163 if (augmentation_data_len)
9164 display_augmentation_data (augmentation_data, augmentation_data_len);
9166 putchar ('\n');
9169 else
9171 unsigned char *look_for;
9172 unsigned long segment_selector;
9173 uint64_t cie_off;
9175 cie_off = cie_id;
9176 if (is_eh)
9178 uint64_t sign = (uint64_t) 1 << (offset_size * 8 - 1);
9179 cie_off = (cie_off ^ sign) - sign;
9180 cie_off = start - 4 - section_start - cie_off;
9183 look_for = section_start + cie_off;
9184 if (cie_off <= (size_t) (saved_start - section_start))
9186 for (cie = chunks; cie ; cie = cie->next)
9187 if (cie->chunk_start == look_for)
9188 break;
9190 else if (cie_off >= section->size)
9191 cie = NULL;
9192 else
9194 for (cie = forward_refs; cie ; cie = cie->next)
9195 if (cie->chunk_start == look_for)
9196 break;
9197 if (!cie)
9199 unsigned int off_size;
9200 unsigned char *cie_scan;
9202 cie_scan = look_for;
9203 off_size = 4;
9204 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
9205 if (length == 0xffffffff)
9207 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
9208 off_size = 8;
9210 if (length != 0 && length <= (size_t) (end - cie_scan))
9212 uint64_t c_id;
9213 unsigned char *cie_end = cie_scan + length;
9215 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size,
9216 cie_end);
9217 if (is_eh
9218 ? c_id == 0
9219 : ((off_size == 4 && c_id == DW_CIE_ID)
9220 || (off_size == 8 && c_id == DW64_CIE_ID)))
9222 int version;
9223 unsigned int mreg;
9225 read_cie (cie_scan, cie_end, &cie, &version,
9226 &augmentation_data_len, &augmentation_data);
9227 /* PR 17512: file: 3450-2098-0.004. */
9228 if (cie == NULL)
9230 warn (_("Failed to read CIE information\n"));
9231 break;
9233 cie->next = forward_refs;
9234 forward_refs = cie;
9235 cie->chunk_start = look_for;
9236 mreg = max_regs > 0 ? max_regs - 1 : 0;
9237 if (mreg < cie->ra)
9238 mreg = cie->ra;
9239 if (frame_need_space (cie, mreg) < 0)
9241 warn (_("Invalid max register\n"));
9242 break;
9244 if (cie->fde_encoding)
9245 encoded_ptr_size
9246 = size_of_encoded_value (cie->fde_encoding);
9252 fc = &fde_fc;
9253 memset (fc, 0, sizeof (Frame_Chunk));
9255 if (!cie)
9257 fc->ncols = 0;
9258 fc->col_type = xmalloc (sizeof (*fc->col_type));
9259 fc->col_offset = xmalloc (sizeof (*fc->col_offset));
9260 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
9262 warn (_("Invalid max register\n"));
9263 break;
9265 cie = fc;
9266 fc->augmentation = "";
9267 fc->fde_encoding = 0;
9268 fc->ptr_size = eh_addr_size;
9269 fc->segment_size = 0;
9271 else
9273 fc->ncols = cie->ncols;
9274 fc->col_type = xcmalloc (fc->ncols, sizeof (*fc->col_type));
9275 fc->col_offset = xcmalloc (fc->ncols, sizeof (*fc->col_offset));
9276 memcpy (fc->col_type, cie->col_type,
9277 fc->ncols * sizeof (*fc->col_type));
9278 memcpy (fc->col_offset, cie->col_offset,
9279 fc->ncols * sizeof (*fc->col_offset));
9280 fc->augmentation = cie->augmentation;
9281 fc->ptr_size = cie->ptr_size;
9282 eh_addr_size = cie->ptr_size;
9283 fc->segment_size = cie->segment_size;
9284 fc->code_factor = cie->code_factor;
9285 fc->data_factor = cie->data_factor;
9286 fc->cfa_reg = cie->cfa_reg;
9287 fc->cfa_offset = cie->cfa_offset;
9288 fc->ra = cie->ra;
9289 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
9291 warn (_("Invalid max register\n"));
9292 break;
9294 fc->fde_encoding = cie->fde_encoding;
9297 if (fc->fde_encoding)
9298 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9300 segment_selector = 0;
9301 if (fc->segment_size)
9303 if (fc->segment_size > sizeof (segment_selector))
9305 /* PR 17512: file: 9e196b3e. */
9306 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
9307 fc->segment_size = 4;
9309 SAFE_BYTE_GET_AND_INC (segment_selector, start,
9310 fc->segment_size, block_end);
9313 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section,
9314 block_end);
9316 /* FIXME: It appears that sometimes the final pc_range value is
9317 encoded in less than encoded_ptr_size bytes. See the x86_64
9318 run of the "objcopy on compressed debug sections" test for an
9319 example of this. */
9320 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size,
9321 block_end);
9323 if (cie->augmentation[0] == 'z')
9325 READ_ULEB (augmentation_data_len, start, block_end);
9326 augmentation_data = start;
9327 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
9328 if (augmentation_data_len > (size_t) (block_end - start))
9330 warn (_("Augmentation data too long: %#" PRIx64 ", "
9331 "expected at most %#tx\n"),
9332 augmentation_data_len, block_end - start);
9333 start = block_end;
9334 augmentation_data = NULL;
9335 augmentation_data_len = 0;
9337 start += augmentation_data_len;
9340 printf ("\n%08tx ", saved_start - section_start);
9341 print_hex (length, fc->ptr_size);
9342 print_hex (cie_id, offset_size);
9343 printf ("FDE ");
9345 if (cie->chunk_start)
9346 printf ("cie=%08tx", cie->chunk_start - section_start);
9347 else
9348 /* Ideally translate "invalid " to 8 chars, trailing space
9349 is optional. */
9350 printf (_("cie=invalid "));
9352 printf (" pc=");
9353 if (fc->segment_size)
9354 printf ("%04lx:", segment_selector);
9356 print_hex_ns (fc->pc_begin, fc->ptr_size);
9357 printf ("..");
9358 print_hex_ns (fc->pc_begin + fc->pc_range, fc->ptr_size);
9359 printf ("\n");
9361 if (! do_debug_frames_interp && augmentation_data_len)
9363 display_augmentation_data (augmentation_data, augmentation_data_len);
9364 putchar ('\n');
9368 /* At this point, fc is the current chunk, cie (if any) is set, and
9369 we're about to interpret instructions for the chunk. */
9370 /* ??? At present we need to do this always, since this sizes the
9371 fc->col_type and fc->col_offset arrays, which we write into always.
9372 We should probably split the interpreted and non-interpreted bits
9373 into two different routines, since there's so much that doesn't
9374 really overlap between them. */
9375 if (1 || do_debug_frames_interp)
9377 /* Start by making a pass over the chunk, allocating storage
9378 and taking note of what registers are used. */
9379 unsigned char *tmp = start;
9381 while (start < block_end)
9383 unsigned int reg, op, opa;
9384 unsigned long temp;
9386 op = *start++;
9387 opa = op & 0x3f;
9388 if (op & 0xc0)
9389 op &= 0xc0;
9391 /* Warning: if you add any more cases to this switch, be
9392 sure to add them to the corresponding switch below. */
9393 reg = -1u;
9394 switch (op)
9396 case DW_CFA_advance_loc:
9397 break;
9398 case DW_CFA_offset:
9399 SKIP_ULEB (start, block_end);
9400 reg = opa;
9401 break;
9402 case DW_CFA_restore:
9403 reg = opa;
9404 break;
9405 case DW_CFA_set_loc:
9406 if ((size_t) (block_end - start) < encoded_ptr_size)
9407 start = block_end;
9408 else
9409 start += encoded_ptr_size;
9410 break;
9411 case DW_CFA_advance_loc1:
9412 if ((size_t) (block_end - start) < 1)
9413 start = block_end;
9414 else
9415 start += 1;
9416 break;
9417 case DW_CFA_advance_loc2:
9418 if ((size_t) (block_end - start) < 2)
9419 start = block_end;
9420 else
9421 start += 2;
9422 break;
9423 case DW_CFA_advance_loc4:
9424 if ((size_t) (block_end - start) < 4)
9425 start = block_end;
9426 else
9427 start += 4;
9428 break;
9429 case DW_CFA_offset_extended:
9430 case DW_CFA_val_offset:
9431 READ_ULEB (reg, start, block_end);
9432 SKIP_ULEB (start, block_end);
9433 break;
9434 case DW_CFA_restore_extended:
9435 READ_ULEB (reg, start, block_end);
9436 break;
9437 case DW_CFA_undefined:
9438 READ_ULEB (reg, start, block_end);
9439 break;
9440 case DW_CFA_same_value:
9441 READ_ULEB (reg, start, block_end);
9442 break;
9443 case DW_CFA_register:
9444 READ_ULEB (reg, start, block_end);
9445 SKIP_ULEB (start, block_end);
9446 break;
9447 case DW_CFA_def_cfa:
9448 SKIP_ULEB (start, block_end);
9449 SKIP_ULEB (start, block_end);
9450 break;
9451 case DW_CFA_def_cfa_register:
9452 SKIP_ULEB (start, block_end);
9453 break;
9454 case DW_CFA_def_cfa_offset:
9455 SKIP_ULEB (start, block_end);
9456 break;
9457 case DW_CFA_def_cfa_expression:
9458 READ_ULEB (temp, start, block_end);
9459 if ((size_t) (block_end - start) < temp)
9460 start = block_end;
9461 else
9462 start += temp;
9463 break;
9464 case DW_CFA_expression:
9465 case DW_CFA_val_expression:
9466 READ_ULEB (reg, start, block_end);
9467 READ_ULEB (temp, start, block_end);
9468 if ((size_t) (block_end - start) < temp)
9469 start = block_end;
9470 else
9471 start += temp;
9472 break;
9473 case DW_CFA_offset_extended_sf:
9474 case DW_CFA_val_offset_sf:
9475 READ_ULEB (reg, start, block_end);
9476 SKIP_SLEB (start, block_end);
9477 break;
9478 case DW_CFA_def_cfa_sf:
9479 SKIP_ULEB (start, block_end);
9480 SKIP_SLEB (start, block_end);
9481 break;
9482 case DW_CFA_def_cfa_offset_sf:
9483 SKIP_SLEB (start, block_end);
9484 break;
9485 case DW_CFA_MIPS_advance_loc8:
9486 if ((size_t) (block_end - start) < 8)
9487 start = block_end;
9488 else
9489 start += 8;
9490 break;
9491 case DW_CFA_GNU_args_size:
9492 SKIP_ULEB (start, block_end);
9493 break;
9494 case DW_CFA_GNU_negative_offset_extended:
9495 READ_ULEB (reg, start, block_end);
9496 SKIP_ULEB (start, block_end);
9497 break;
9498 default:
9499 break;
9501 if (reg != -1u && frame_need_space (fc, reg) >= 0)
9503 /* Don't leave any reg as DW_CFA_unreferenced so
9504 that frame_display_row prints name of regs in
9505 header, and all referenced regs in each line. */
9506 if (reg >= cie->ncols
9507 || cie->col_type[reg] == DW_CFA_unreferenced)
9508 fc->col_type[reg] = DW_CFA_undefined;
9509 else
9510 fc->col_type[reg] = cie->col_type[reg];
9513 start = tmp;
9516 all_nops = true;
9518 /* Now we know what registers are used, make a second pass over
9519 the chunk, this time actually printing out the info. */
9521 while (start < block_end)
9523 unsigned op, opa;
9524 /* Note: It is tempting to use an unsigned long for 'reg' but there
9525 are various functions, notably frame_space_needed() that assume that
9526 reg is an unsigned int. */
9527 unsigned int reg;
9528 int64_t sofs;
9529 uint64_t ofs;
9530 const char *reg_prefix = "";
9532 op = *start++;
9533 opa = op & 0x3f;
9534 if (op & 0xc0)
9535 op &= 0xc0;
9537 /* Make a note if something other than DW_CFA_nop happens. */
9538 if (op != DW_CFA_nop)
9539 all_nops = false;
9541 /* Warning: if you add any more cases to this switch, be
9542 sure to add them to the corresponding switch above. */
9543 switch (op)
9545 case DW_CFA_advance_loc:
9546 opa *= fc->code_factor;
9547 if (do_debug_frames_interp)
9548 frame_display_row (fc, &need_col_headers, &max_regs);
9549 else
9551 printf (" DW_CFA_advance_loc: %d to ", opa);
9552 print_hex_ns (fc->pc_begin + opa, fc->ptr_size);
9553 printf ("\n");
9555 fc->pc_begin += opa;
9556 break;
9558 case DW_CFA_offset:
9559 READ_ULEB (ofs, start, block_end);
9560 ofs *= fc->data_factor;
9561 if (opa >= fc->ncols)
9562 reg_prefix = bad_reg;
9563 if (! do_debug_frames_interp || *reg_prefix != '\0')
9564 printf (" DW_CFA_offset: %s%s at cfa%+" PRId64 "\n",
9565 reg_prefix, regname (opa, 0), ofs);
9566 if (*reg_prefix == '\0')
9568 fc->col_type[opa] = DW_CFA_offset;
9569 fc->col_offset[opa] = ofs;
9571 break;
9573 case DW_CFA_restore:
9574 if (opa >= fc->ncols)
9575 reg_prefix = bad_reg;
9576 if (! do_debug_frames_interp || *reg_prefix != '\0')
9577 printf (" DW_CFA_restore: %s%s\n",
9578 reg_prefix, regname (opa, 0));
9579 if (*reg_prefix != '\0')
9580 break;
9582 if (opa >= cie->ncols
9583 || cie->col_type[opa] == DW_CFA_unreferenced)
9585 fc->col_type[opa] = DW_CFA_undefined;
9586 fc->col_offset[opa] = 0;
9588 else
9590 fc->col_type[opa] = cie->col_type[opa];
9591 fc->col_offset[opa] = cie->col_offset[opa];
9593 break;
9595 case DW_CFA_set_loc:
9596 ofs = get_encoded_value (&start, fc->fde_encoding, section,
9597 block_end);
9598 if (do_debug_frames_interp)
9599 frame_display_row (fc, &need_col_headers, &max_regs);
9600 else
9602 printf (" DW_CFA_set_loc: ");
9603 print_hex_ns (ofs, fc->ptr_size);
9604 printf ("\n");
9606 fc->pc_begin = ofs;
9607 break;
9609 case DW_CFA_advance_loc1:
9610 SAFE_BYTE_GET_AND_INC (ofs, start, 1, block_end);
9611 ofs *= fc->code_factor;
9612 if (do_debug_frames_interp)
9613 frame_display_row (fc, &need_col_headers, &max_regs);
9614 else
9616 printf (" DW_CFA_advance_loc1: %" PRId64 " to ", ofs);
9617 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9618 printf ("\n");
9620 fc->pc_begin += ofs;
9621 break;
9623 case DW_CFA_advance_loc2:
9624 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
9625 ofs *= fc->code_factor;
9626 if (do_debug_frames_interp)
9627 frame_display_row (fc, &need_col_headers, &max_regs);
9628 else
9630 printf (" DW_CFA_advance_loc2: %" PRId64 " to ", ofs);
9631 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9632 printf ("\n");
9634 fc->pc_begin += ofs;
9635 break;
9637 case DW_CFA_advance_loc4:
9638 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
9639 ofs *= fc->code_factor;
9640 if (do_debug_frames_interp)
9641 frame_display_row (fc, &need_col_headers, &max_regs);
9642 else
9644 printf (" DW_CFA_advance_loc4: %" PRId64 " to ", ofs);
9645 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9646 printf ("\n");
9648 fc->pc_begin += ofs;
9649 break;
9651 case DW_CFA_offset_extended:
9652 READ_ULEB (reg, start, block_end);
9653 READ_ULEB (ofs, start, block_end);
9654 ofs *= fc->data_factor;
9655 if (reg >= fc->ncols)
9656 reg_prefix = bad_reg;
9657 if (! do_debug_frames_interp || *reg_prefix != '\0')
9658 printf (" DW_CFA_offset_extended: %s%s at cfa%+" PRId64 "\n",
9659 reg_prefix, regname (reg, 0), ofs);
9660 if (*reg_prefix == '\0')
9662 fc->col_type[reg] = DW_CFA_offset;
9663 fc->col_offset[reg] = ofs;
9665 break;
9667 case DW_CFA_val_offset:
9668 READ_ULEB (reg, start, block_end);
9669 READ_ULEB (ofs, start, block_end);
9670 ofs *= fc->data_factor;
9671 if (reg >= fc->ncols)
9672 reg_prefix = bad_reg;
9673 if (! do_debug_frames_interp || *reg_prefix != '\0')
9674 printf (" DW_CFA_val_offset: %s%s is cfa%+" PRId64 "\n",
9675 reg_prefix, regname (reg, 0), ofs);
9676 if (*reg_prefix == '\0')
9678 fc->col_type[reg] = DW_CFA_val_offset;
9679 fc->col_offset[reg] = ofs;
9681 break;
9683 case DW_CFA_restore_extended:
9684 READ_ULEB (reg, start, block_end);
9685 if (reg >= fc->ncols)
9686 reg_prefix = bad_reg;
9687 if (! do_debug_frames_interp || *reg_prefix != '\0')
9688 printf (" DW_CFA_restore_extended: %s%s\n",
9689 reg_prefix, regname (reg, 0));
9690 if (*reg_prefix != '\0')
9691 break;
9693 if (reg >= cie->ncols
9694 || cie->col_type[reg] == DW_CFA_unreferenced)
9696 fc->col_type[reg] = DW_CFA_undefined;
9697 fc->col_offset[reg] = 0;
9699 else
9701 fc->col_type[reg] = cie->col_type[reg];
9702 fc->col_offset[reg] = cie->col_offset[reg];
9704 break;
9706 case DW_CFA_undefined:
9707 READ_ULEB (reg, start, block_end);
9708 if (reg >= fc->ncols)
9709 reg_prefix = bad_reg;
9710 if (! do_debug_frames_interp || *reg_prefix != '\0')
9711 printf (" DW_CFA_undefined: %s%s\n",
9712 reg_prefix, regname (reg, 0));
9713 if (*reg_prefix == '\0')
9715 fc->col_type[reg] = DW_CFA_undefined;
9716 fc->col_offset[reg] = 0;
9718 break;
9720 case DW_CFA_same_value:
9721 READ_ULEB (reg, start, block_end);
9722 if (reg >= fc->ncols)
9723 reg_prefix = bad_reg;
9724 if (! do_debug_frames_interp || *reg_prefix != '\0')
9725 printf (" DW_CFA_same_value: %s%s\n",
9726 reg_prefix, regname (reg, 0));
9727 if (*reg_prefix == '\0')
9729 fc->col_type[reg] = DW_CFA_same_value;
9730 fc->col_offset[reg] = 0;
9732 break;
9734 case DW_CFA_register:
9735 READ_ULEB (reg, start, block_end);
9736 READ_ULEB (ofs, start, block_end);
9737 if (reg >= fc->ncols)
9738 reg_prefix = bad_reg;
9739 if (! do_debug_frames_interp || *reg_prefix != '\0')
9741 printf (" DW_CFA_register: %s%s in ",
9742 reg_prefix, regname (reg, 0));
9743 puts (regname (ofs, 0));
9745 if (*reg_prefix == '\0')
9747 fc->col_type[reg] = DW_CFA_register;
9748 fc->col_offset[reg] = ofs;
9750 break;
9752 case DW_CFA_remember_state:
9753 if (! do_debug_frames_interp)
9754 printf (" DW_CFA_remember_state\n");
9755 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9756 rs->cfa_offset = fc->cfa_offset;
9757 rs->cfa_reg = fc->cfa_reg;
9758 rs->ra = fc->ra;
9759 rs->cfa_exp = fc->cfa_exp;
9760 rs->ncols = fc->ncols;
9761 rs->col_type = xcmalloc (rs->ncols, sizeof (*rs->col_type));
9762 rs->col_offset = xcmalloc (rs->ncols, sizeof (*rs->col_offset));
9763 memcpy (rs->col_type, fc->col_type,
9764 rs->ncols * sizeof (*fc->col_type));
9765 memcpy (rs->col_offset, fc->col_offset,
9766 rs->ncols * sizeof (*fc->col_offset));
9767 rs->next = remembered_state;
9768 remembered_state = rs;
9769 break;
9771 case DW_CFA_restore_state:
9772 if (! do_debug_frames_interp)
9773 printf (" DW_CFA_restore_state\n");
9774 rs = remembered_state;
9775 if (rs)
9777 remembered_state = rs->next;
9778 fc->cfa_offset = rs->cfa_offset;
9779 fc->cfa_reg = rs->cfa_reg;
9780 fc->ra = rs->ra;
9781 fc->cfa_exp = rs->cfa_exp;
9782 if (frame_need_space (fc, rs->ncols - 1) < 0)
9784 warn (_("Invalid column number in saved frame state\n"));
9785 fc->ncols = 0;
9786 break;
9788 memcpy (fc->col_type, rs->col_type,
9789 rs->ncols * sizeof (*rs->col_type));
9790 memcpy (fc->col_offset, rs->col_offset,
9791 rs->ncols * sizeof (*rs->col_offset));
9792 free (rs->col_type);
9793 free (rs->col_offset);
9794 free (rs);
9796 else if (do_debug_frames_interp)
9797 printf ("Mismatched DW_CFA_restore_state\n");
9798 break;
9800 case DW_CFA_def_cfa:
9801 READ_ULEB (fc->cfa_reg, start, block_end);
9802 READ_ULEB (fc->cfa_offset, start, block_end);
9803 fc->cfa_exp = 0;
9804 if (! do_debug_frames_interp)
9805 printf (" DW_CFA_def_cfa: %s ofs %d\n",
9806 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
9807 break;
9809 case DW_CFA_def_cfa_register:
9810 READ_ULEB (fc->cfa_reg, start, block_end);
9811 fc->cfa_exp = 0;
9812 if (! do_debug_frames_interp)
9813 printf (" DW_CFA_def_cfa_register: %s\n",
9814 regname (fc->cfa_reg, 0));
9815 break;
9817 case DW_CFA_def_cfa_offset:
9818 READ_ULEB (fc->cfa_offset, start, block_end);
9819 if (! do_debug_frames_interp)
9820 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
9821 break;
9823 case DW_CFA_nop:
9824 if (! do_debug_frames_interp)
9825 printf (" DW_CFA_nop\n");
9826 break;
9828 case DW_CFA_def_cfa_expression:
9829 READ_ULEB (ofs, start, block_end);
9830 if (ofs > (size_t) (block_end - start))
9832 printf (_(" %s: <corrupt len %" PRIu64 ">\n"),
9833 "DW_CFA_def_cfa_expression", ofs);
9834 break;
9836 if (! do_debug_frames_interp)
9838 printf (" DW_CFA_def_cfa_expression (");
9839 decode_location_expression (start, eh_addr_size, 0, -1,
9840 ofs, 0, section);
9841 printf (")\n");
9843 fc->cfa_exp = 1;
9844 start += ofs;
9845 break;
9847 case DW_CFA_expression:
9848 READ_ULEB (reg, start, block_end);
9849 READ_ULEB (ofs, start, block_end);
9850 if (reg >= fc->ncols)
9851 reg_prefix = bad_reg;
9852 /* PR 17512: file: 069-133014-0.006. */
9853 /* PR 17512: file: 98c02eb4. */
9854 if (ofs > (size_t) (block_end - start))
9856 printf (_(" %s: <corrupt len %" PRIu64 ">\n"),
9857 "DW_CFA_expression", ofs);
9858 break;
9860 if (! do_debug_frames_interp || *reg_prefix != '\0')
9862 printf (" DW_CFA_expression: %s%s (",
9863 reg_prefix, regname (reg, 0));
9864 decode_location_expression (start, eh_addr_size, 0, -1,
9865 ofs, 0, section);
9866 printf (")\n");
9868 if (*reg_prefix == '\0')
9869 fc->col_type[reg] = DW_CFA_expression;
9870 start += ofs;
9871 break;
9873 case DW_CFA_val_expression:
9874 READ_ULEB (reg, start, block_end);
9875 READ_ULEB (ofs, start, block_end);
9876 if (reg >= fc->ncols)
9877 reg_prefix = bad_reg;
9878 if (ofs > (size_t) (block_end - start))
9880 printf (" %s: <corrupt len %" PRIu64 ">\n",
9881 "DW_CFA_val_expression", ofs);
9882 break;
9884 if (! do_debug_frames_interp || *reg_prefix != '\0')
9886 printf (" DW_CFA_val_expression: %s%s (",
9887 reg_prefix, regname (reg, 0));
9888 decode_location_expression (start, eh_addr_size, 0, -1,
9889 ofs, 0, section);
9890 printf (")\n");
9892 if (*reg_prefix == '\0')
9893 fc->col_type[reg] = DW_CFA_val_expression;
9894 start += ofs;
9895 break;
9897 case DW_CFA_offset_extended_sf:
9898 READ_ULEB (reg, start, block_end);
9899 READ_SLEB (sofs, start, block_end);
9900 /* data_factor multiplicaton done here as unsigned to
9901 avoid integer overflow warnings from asan on fuzzed
9902 objects. */
9903 ofs = sofs;
9904 ofs *= fc->data_factor;
9905 if (reg >= fc->ncols)
9906 reg_prefix = bad_reg;
9907 if (! do_debug_frames_interp || *reg_prefix != '\0')
9908 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+" PRId64 "\n",
9909 reg_prefix, regname (reg, 0), ofs);
9910 if (*reg_prefix == '\0')
9912 fc->col_type[reg] = DW_CFA_offset;
9913 fc->col_offset[reg] = ofs;
9915 break;
9917 case DW_CFA_val_offset_sf:
9918 READ_ULEB (reg, start, block_end);
9919 READ_SLEB (sofs, start, block_end);
9920 ofs = sofs;
9921 ofs *= fc->data_factor;
9922 if (reg >= fc->ncols)
9923 reg_prefix = bad_reg;
9924 if (! do_debug_frames_interp || *reg_prefix != '\0')
9925 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+" PRId64 "\n",
9926 reg_prefix, regname (reg, 0), ofs);
9927 if (*reg_prefix == '\0')
9929 fc->col_type[reg] = DW_CFA_val_offset;
9930 fc->col_offset[reg] = ofs;
9932 break;
9934 case DW_CFA_def_cfa_sf:
9935 READ_ULEB (fc->cfa_reg, start, block_end);
9936 READ_SLEB (sofs, start, block_end);
9937 ofs = sofs;
9938 ofs *= fc->data_factor;
9939 fc->cfa_offset = ofs;
9940 fc->cfa_exp = 0;
9941 if (! do_debug_frames_interp)
9942 printf (" DW_CFA_def_cfa_sf: %s ofs %" PRId64 "\n",
9943 regname (fc->cfa_reg, 0), ofs);
9944 break;
9946 case DW_CFA_def_cfa_offset_sf:
9947 READ_SLEB (sofs, start, block_end);
9948 ofs = sofs;
9949 ofs *= fc->data_factor;
9950 fc->cfa_offset = ofs;
9951 if (! do_debug_frames_interp)
9952 printf (" DW_CFA_def_cfa_offset_sf: %" PRId64 "\n", ofs);
9953 break;
9955 case DW_CFA_MIPS_advance_loc8:
9956 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
9957 ofs *= fc->code_factor;
9958 if (do_debug_frames_interp)
9959 frame_display_row (fc, &need_col_headers, &max_regs);
9960 else
9962 printf (" DW_CFA_MIPS_advance_loc8: %" PRId64 " to ", ofs);
9963 print_hex_ns (fc->pc_begin + ofs, fc->ptr_size);
9964 printf ("\n");
9966 fc->pc_begin += ofs;
9967 break;
9969 case DW_CFA_GNU_window_save:
9970 if (! do_debug_frames_interp)
9971 printf (" %s\n", DW_CFA_GNU_window_save_name[is_aarch64]);
9972 break;
9974 case DW_CFA_GNU_args_size:
9975 READ_ULEB (ofs, start, block_end);
9976 if (! do_debug_frames_interp)
9977 printf (" DW_CFA_GNU_args_size: %" PRIu64 "\n", ofs);
9978 break;
9980 case DW_CFA_GNU_negative_offset_extended:
9981 READ_ULEB (reg, start, block_end);
9982 READ_SLEB (sofs, start, block_end);
9983 ofs = sofs;
9984 ofs = -ofs * fc->data_factor;
9985 if (reg >= fc->ncols)
9986 reg_prefix = bad_reg;
9987 if (! do_debug_frames_interp || *reg_prefix != '\0')
9988 printf (" DW_CFA_GNU_negative_offset_extended: %s%s "
9989 "at cfa%+" PRId64 "\n",
9990 reg_prefix, regname (reg, 0), ofs);
9991 if (*reg_prefix == '\0')
9993 fc->col_type[reg] = DW_CFA_offset;
9994 fc->col_offset[reg] = ofs;
9996 break;
9998 default:
9999 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
10000 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
10001 else
10002 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
10003 start = block_end;
10007 /* Interpret the CFA - as long as it is not completely full of NOPs. */
10008 if (do_debug_frames_interp && ! all_nops)
10009 frame_display_row (fc, &need_col_headers, &max_regs);
10011 if (fde_fc.col_type != NULL)
10013 free (fde_fc.col_type);
10014 fde_fc.col_type = NULL;
10016 if (fde_fc.col_offset != NULL)
10018 free (fde_fc.col_offset);
10019 fde_fc.col_offset = NULL;
10022 start = block_end;
10023 eh_addr_size = saved_eh_addr_size;
10026 printf ("\n");
10028 while (remembered_state != NULL)
10030 rs = remembered_state;
10031 remembered_state = rs->next;
10032 free (rs->col_type);
10033 free (rs->col_offset);
10034 rs->next = NULL; /* Paranoia. */
10035 free (rs);
10038 while (chunks != NULL)
10040 rs = chunks;
10041 chunks = rs->next;
10042 free (rs->col_type);
10043 free (rs->col_offset);
10044 rs->next = NULL; /* Paranoia. */
10045 free (rs);
10048 while (forward_refs != NULL)
10050 rs = forward_refs;
10051 forward_refs = rs->next;
10052 free (rs->col_type);
10053 free (rs->col_offset);
10054 rs->next = NULL; /* Paranoia. */
10055 free (rs);
10058 return 1;
10061 #undef GET
10063 static int
10064 display_debug_names (struct dwarf_section *section, void *file)
10066 unsigned char *hdrptr = section->start;
10067 uint64_t unit_length;
10068 unsigned char *unit_start;
10069 const unsigned char *const section_end = section->start + section->size;
10070 unsigned char *unit_end;
10072 introduce (section, false);
10074 load_debug_section_with_follow (str, file);
10076 for (; hdrptr < section_end; hdrptr = unit_end)
10078 unsigned int offset_size;
10079 uint16_t dwarf_version, padding;
10080 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
10081 uint64_t bucket_count, name_count, abbrev_table_size;
10082 uint32_t augmentation_string_size;
10083 unsigned int i;
10084 bool augmentation_printable;
10085 const char *augmentation_string;
10086 size_t total;
10088 unit_start = hdrptr;
10090 /* Get and check the length of the block. */
10091 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
10093 if (unit_length == 0xffffffff)
10095 /* This section is 64-bit DWARF. */
10096 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
10097 offset_size = 8;
10099 else
10100 offset_size = 4;
10102 if (unit_length > (size_t) (section_end - hdrptr)
10103 || unit_length < 2 + 2 + 4 * 7)
10105 too_short:
10106 warn (_("Debug info is corrupted, %s header at %#tx"
10107 " has length %#" PRIx64 "\n"),
10108 section->name, unit_start - section->start, unit_length);
10109 return 0;
10111 unit_end = hdrptr + unit_length;
10113 /* Get and check the version number. */
10114 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
10115 printf (_("Version %d\n"), (int) dwarf_version);
10117 /* Prior versions did not exist, and future versions may not be
10118 backwards compatible. */
10119 if (dwarf_version != 5)
10121 warn (_("Only DWARF version 5 .debug_names "
10122 "is currently supported.\n"));
10123 return 0;
10126 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
10127 if (padding != 0)
10128 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
10129 padding);
10131 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
10132 if (comp_unit_count == 0)
10133 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
10135 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
10136 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
10137 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
10138 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
10139 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
10141 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
10142 if (augmentation_string_size % 4 != 0)
10144 warn (_("Augmentation string length %u must be rounded up "
10145 "to a multiple of 4 in .debug_names.\n"),
10146 augmentation_string_size);
10147 augmentation_string_size += (-augmentation_string_size) & 3;
10149 if (augmentation_string_size > (size_t) (unit_end - hdrptr))
10150 goto too_short;
10152 printf (_("Augmentation string:"));
10154 augmentation_printable = true;
10155 augmentation_string = (const char *) hdrptr;
10157 for (i = 0; i < augmentation_string_size; i++)
10159 unsigned char uc;
10161 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
10162 printf (" %02x", uc);
10164 if (uc != 0 && !ISPRINT (uc))
10165 augmentation_printable = false;
10168 if (augmentation_printable)
10170 printf (" (\"");
10171 for (i = 0;
10172 i < augmentation_string_size && augmentation_string[i];
10173 ++i)
10174 putchar (augmentation_string[i]);
10175 printf ("\")");
10177 putchar ('\n');
10179 printf (_("CU table:\n"));
10180 if (_mul_overflow (comp_unit_count, offset_size, &total)
10181 || total > (size_t) (unit_end - hdrptr))
10182 goto too_short;
10183 for (i = 0; i < comp_unit_count; i++)
10185 uint64_t cu_offset;
10187 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
10188 printf ("[%3u] %#" PRIx64 "\n", i, cu_offset);
10190 putchar ('\n');
10192 printf (_("TU table:\n"));
10193 if (_mul_overflow (local_type_unit_count, offset_size, &total)
10194 || total > (size_t) (unit_end - hdrptr))
10195 goto too_short;
10196 for (i = 0; i < local_type_unit_count; i++)
10198 uint64_t tu_offset;
10200 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
10201 printf ("[%3u] %#" PRIx64 "\n", i, tu_offset);
10203 putchar ('\n');
10205 printf (_("Foreign TU table:\n"));
10206 if (_mul_overflow (foreign_type_unit_count, 8, &total)
10207 || total > (size_t) (unit_end - hdrptr))
10208 goto too_short;
10209 for (i = 0; i < foreign_type_unit_count; i++)
10211 uint64_t signature;
10213 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
10214 printf (_("[%3u] "), i);
10215 print_hex_ns (signature, 8);
10216 putchar ('\n');
10218 putchar ('\n');
10220 uint64_t xtra = (bucket_count * sizeof (uint32_t)
10221 + name_count * (sizeof (uint32_t) + 2 * offset_size)
10222 + abbrev_table_size);
10223 if (xtra > (size_t) (unit_end - hdrptr))
10225 warn (_("Entry pool offset (%#" PRIx64 ") exceeds unit size %#tx "
10226 "for unit %#tx in the debug_names\n"),
10227 xtra, unit_end - unit_start, unit_start - section->start);
10228 return 0;
10230 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
10231 hdrptr += bucket_count * sizeof (uint32_t);
10232 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
10233 hdrptr += name_count * sizeof (uint32_t);
10234 unsigned char *const name_table_string_offsets = hdrptr;
10235 hdrptr += name_count * offset_size;
10236 unsigned char *const name_table_entry_offsets = hdrptr;
10237 hdrptr += name_count * offset_size;
10238 unsigned char *const abbrev_table = hdrptr;
10239 hdrptr += abbrev_table_size;
10240 const unsigned char *const abbrev_table_end = hdrptr;
10241 unsigned char *const entry_pool = hdrptr;
10243 size_t buckets_filled = 0;
10244 size_t bucketi;
10245 for (bucketi = 0; bucketi < bucket_count; bucketi++)
10247 const uint32_t bucket = hash_table_buckets[bucketi];
10249 if (bucket != 0)
10250 ++buckets_filled;
10252 printf (ngettext ("Used %zu of %lu bucket.\n",
10253 "Used %zu of %lu buckets.\n",
10254 (unsigned long) bucket_count),
10255 buckets_filled, (unsigned long) bucket_count);
10257 if (bucket_count != 0)
10259 uint32_t hash_prev = 0;
10260 size_t hash_clash_count = 0;
10261 size_t longest_clash = 0;
10262 size_t this_length = 0;
10263 size_t hashi;
10264 for (hashi = 0; hashi < name_count; hashi++)
10266 const uint32_t hash_this = hash_table_hashes[hashi];
10268 if (hashi > 0)
10270 if (hash_prev % bucket_count == hash_this % bucket_count)
10272 ++hash_clash_count;
10273 ++this_length;
10274 longest_clash = MAX (longest_clash, this_length);
10276 else
10277 this_length = 0;
10279 hash_prev = hash_this;
10281 printf (_("Out of %" PRIu64 " items there are %zu bucket clashes"
10282 " (longest of %zu entries).\n"),
10283 name_count, hash_clash_count, longest_clash);
10285 if (name_count != buckets_filled + hash_clash_count)
10286 warn (_("The name_count (%" PRIu64 ")"
10287 " is not the same as the used bucket_count"
10288 " (%zu) + the hash clash count (%zu)"),
10289 name_count, buckets_filled, hash_clash_count);
10292 struct abbrev_lookup_entry
10294 uint64_t abbrev_tag;
10295 unsigned char *abbrev_lookup_ptr;
10297 struct abbrev_lookup_entry *abbrev_lookup = NULL;
10298 size_t abbrev_lookup_used = 0;
10299 size_t abbrev_lookup_allocated = 0;
10301 unsigned char *abbrevptr = abbrev_table;
10302 for (;;)
10304 uint64_t abbrev_tag;
10306 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
10307 if (abbrev_tag == 0)
10308 break;
10309 if (abbrev_lookup_used == abbrev_lookup_allocated)
10311 abbrev_lookup_allocated = MAX (0x100,
10312 abbrev_lookup_allocated * 2);
10313 abbrev_lookup = xrealloc (abbrev_lookup,
10314 (abbrev_lookup_allocated
10315 * sizeof (*abbrev_lookup)));
10317 assert (abbrev_lookup_used < abbrev_lookup_allocated);
10318 struct abbrev_lookup_entry *entry;
10319 for (entry = abbrev_lookup;
10320 entry < abbrev_lookup + abbrev_lookup_used;
10321 entry++)
10322 if (entry->abbrev_tag == abbrev_tag)
10324 warn (_("Duplicate abbreviation tag %" PRIu64
10325 " in unit %#tx in the debug_names section\n"),
10326 abbrev_tag, unit_start - section->start);
10327 break;
10329 entry = &abbrev_lookup[abbrev_lookup_used++];
10330 entry->abbrev_tag = abbrev_tag;
10331 entry->abbrev_lookup_ptr = abbrevptr;
10333 /* Skip DWARF tag. */
10334 SKIP_ULEB (abbrevptr, abbrev_table_end);
10335 for (;;)
10337 uint64_t xindex, form;
10339 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10340 READ_ULEB (form, abbrevptr, abbrev_table_end);
10341 if (xindex == 0 && form == 0)
10342 break;
10346 printf (_("\nSymbol table:\n"));
10347 uint32_t namei;
10348 for (namei = 0; namei < name_count; ++namei)
10350 uint64_t string_offset, entry_offset;
10351 unsigned char *p;
10353 p = name_table_string_offsets + namei * offset_size;
10354 SAFE_BYTE_GET (string_offset, p, offset_size, unit_end);
10355 p = name_table_entry_offsets + namei * offset_size;
10356 SAFE_BYTE_GET (entry_offset, p, offset_size, unit_end);
10358 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
10359 fetch_indirect_string (string_offset));
10361 unsigned char *entryptr = entry_pool + entry_offset;
10363 /* We need to scan first whether there is a single or multiple
10364 entries. TAGNO is -2 for the first entry, it is -1 for the
10365 initial tag read of the second entry, then it becomes 0 for the
10366 first entry for real printing etc. */
10367 int tagno = -2;
10368 /* Initialize it due to a false compiler warning. */
10369 uint64_t second_abbrev_tag = -1;
10370 for (;;)
10372 uint64_t abbrev_tag;
10373 uint64_t dwarf_tag;
10374 const struct abbrev_lookup_entry *entry;
10376 READ_ULEB (abbrev_tag, entryptr, unit_end);
10377 if (tagno == -1)
10379 second_abbrev_tag = abbrev_tag;
10380 tagno = 0;
10381 entryptr = entry_pool + entry_offset;
10382 continue;
10384 if (abbrev_tag == 0)
10385 break;
10386 if (tagno >= 0)
10387 printf ("%s<%" PRIu64 ">",
10388 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
10389 abbrev_tag);
10391 for (entry = abbrev_lookup;
10392 entry < abbrev_lookup + abbrev_lookup_used;
10393 entry++)
10394 if (entry->abbrev_tag == abbrev_tag)
10395 break;
10396 if (entry >= abbrev_lookup + abbrev_lookup_used)
10398 warn (_("Undefined abbreviation tag %" PRId64
10399 " in unit %#tx in the debug_names section\n"),
10400 abbrev_tag,
10401 unit_start - section->start);
10402 break;
10404 abbrevptr = entry->abbrev_lookup_ptr;
10405 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
10406 if (tagno >= 0)
10407 printf (" %s", get_TAG_name (dwarf_tag));
10408 for (;;)
10410 uint64_t xindex, form;
10412 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10413 READ_ULEB (form, abbrevptr, abbrev_table_end);
10414 if (xindex == 0 && form == 0)
10415 break;
10417 if (tagno >= 0)
10418 printf (" %s", get_IDX_name (xindex));
10419 entryptr = read_and_display_attr_value (0, form, 0,
10420 unit_start, entryptr, unit_end,
10421 0, 0, offset_size,
10422 dwarf_version, NULL,
10423 (tagno < 0), section,
10424 NULL, '=', -1);
10426 ++tagno;
10428 if (tagno <= 0)
10429 printf (_(" <no entries>"));
10430 putchar ('\n');
10433 free (abbrev_lookup);
10436 return 1;
10439 static int
10440 display_debug_links (struct dwarf_section * section,
10441 void * file ATTRIBUTE_UNUSED)
10443 const unsigned char * filename;
10444 unsigned int filelen;
10446 introduce (section, false);
10448 /* The .gnu_debuglink section is formatted as:
10449 (c-string) Filename.
10450 (padding) If needed to reach a 4 byte boundary.
10451 (uint32_t) CRC32 value.
10453 The .gun_debugaltlink section is formatted as:
10454 (c-string) Filename.
10455 (binary) Build-ID. */
10457 filename = section->start;
10458 filelen = strnlen ((const char *) filename, section->size);
10459 if (filelen == section->size)
10461 warn (_("The debuglink filename is corrupt/missing\n"));
10462 return 0;
10465 printf (_(" Separate debug info file: %s\n"), filename);
10467 if (startswith (section->name, ".gnu_debuglink"))
10469 unsigned int crc32;
10470 unsigned int crc_offset;
10472 crc_offset = filelen + 1;
10473 crc_offset = (crc_offset + 3) & ~3;
10474 if (crc_offset + 4 > section->size)
10476 warn (_("CRC offset missing/truncated\n"));
10477 return 0;
10480 crc32 = byte_get (filename + crc_offset, 4);
10482 printf (_(" CRC value: %#x\n"), crc32);
10484 if (crc_offset + 4 < section->size)
10486 warn (_("There are %#" PRIx64
10487 " extraneous bytes at the end of the section\n"),
10488 section->size - (crc_offset + 4));
10489 return 0;
10492 else /* startswith (section->name, ".gnu_debugaltlink") */
10494 const unsigned char *build_id = section->start + filelen + 1;
10495 size_t build_id_len = section->size - (filelen + 1);
10496 size_t printed;
10498 /* FIXME: Should we support smaller build-id notes ? */
10499 if (build_id_len < 0x14)
10501 warn (_("Build-ID is too short (%#zx bytes)\n"), build_id_len);
10502 return 0;
10505 printed = printf (_(" Build-ID (%#zx bytes):"), build_id_len);
10506 display_data (printed, build_id, build_id_len);
10507 putchar ('\n');
10510 putchar ('\n');
10511 return 1;
10514 static int
10515 display_gdb_index (struct dwarf_section *section,
10516 void *file ATTRIBUTE_UNUSED)
10518 unsigned char *start = section->start;
10519 uint32_t version;
10520 uint32_t cu_list_offset, tu_list_offset;
10521 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
10522 unsigned int cu_list_elements, tu_list_elements;
10523 unsigned int address_table_elements, symbol_table_slots;
10524 unsigned char *cu_list, *tu_list;
10525 unsigned char *address_table, *symbol_table, *constant_pool;
10526 unsigned int i;
10528 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
10530 introduce (section, false);
10532 if (section->size < 6 * sizeof (uint32_t))
10534 warn (_("Truncated header in the %s section.\n"), section->name);
10535 return 0;
10538 version = byte_get_little_endian (start, 4);
10539 printf (_("Version %lu\n"), (unsigned long) version);
10541 /* Prior versions are obsolete, and future versions may not be
10542 backwards compatible. */
10543 if (version < 3 || version > 8)
10545 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
10546 return 0;
10548 if (version < 4)
10549 warn (_("The address table data in version 3 may be wrong.\n"));
10550 if (version < 5)
10551 warn (_("Version 4 does not support case insensitive lookups.\n"));
10552 if (version < 6)
10553 warn (_("Version 5 does not include inlined functions.\n"));
10554 if (version < 7)
10555 warn (_("Version 6 does not include symbol attributes.\n"));
10556 /* Version 7 indices generated by Gold have bad type unit references,
10557 PR binutils/15021. But we don't know if the index was generated by
10558 Gold or not, so to avoid worrying users with gdb-generated indices
10559 we say nothing for version 7 here. */
10561 cu_list_offset = byte_get_little_endian (start + 4, 4);
10562 tu_list_offset = byte_get_little_endian (start + 8, 4);
10563 address_table_offset = byte_get_little_endian (start + 12, 4);
10564 symbol_table_offset = byte_get_little_endian (start + 16, 4);
10565 constant_pool_offset = byte_get_little_endian (start + 20, 4);
10567 if (cu_list_offset > section->size
10568 || tu_list_offset > section->size
10569 || address_table_offset > section->size
10570 || symbol_table_offset > section->size
10571 || constant_pool_offset > section->size
10572 || tu_list_offset < cu_list_offset
10573 || address_table_offset < tu_list_offset
10574 || symbol_table_offset < address_table_offset
10575 || constant_pool_offset < symbol_table_offset)
10577 warn (_("Corrupt header in the %s section.\n"), section->name);
10578 return 0;
10581 cu_list_elements = (tu_list_offset - cu_list_offset) / 16;
10582 tu_list_elements = (address_table_offset - tu_list_offset) / 24;
10583 address_table_elements = (symbol_table_offset - address_table_offset) / 20;
10584 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
10586 cu_list = start + cu_list_offset;
10587 tu_list = start + tu_list_offset;
10588 address_table = start + address_table_offset;
10589 symbol_table = start + symbol_table_offset;
10590 constant_pool = start + constant_pool_offset;
10592 printf (_("\nCU table:\n"));
10593 for (i = 0; i < cu_list_elements; i++)
10595 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 16, 8);
10596 uint64_t cu_length = byte_get_little_endian (cu_list + i * 16 + 8, 8);
10598 printf ("[%3u] %#" PRIx64 " - %#" PRIx64 "\n",
10599 i, cu_offset, cu_offset + cu_length - 1);
10602 printf (_("\nTU table:\n"));
10603 for (i = 0; i < tu_list_elements; i++)
10605 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 24, 8);
10606 uint64_t type_offset = byte_get_little_endian (tu_list + i * 24 + 8, 8);
10607 uint64_t signature = byte_get_little_endian (tu_list + i * 24 + 16, 8);
10609 printf ("[%3u] %#" PRIx64 " %#" PRIx64 " ",
10610 i, tu_offset, type_offset);
10611 print_hex_ns (signature, 8);
10612 printf ("\n");
10615 printf (_("\nAddress table:\n"));
10616 for (i = 0; i < address_table_elements; i++)
10618 uint64_t low = byte_get_little_endian (address_table + i * 20, 8);
10619 uint64_t high = byte_get_little_endian (address_table + i * 20 + 8, 8);
10620 uint32_t cu_index = byte_get_little_endian (address_table + i * 20 + 16, 4);
10622 print_hex (low, 8);
10623 print_hex (high, 8);
10624 printf ("%" PRIu32 "\n", cu_index);
10627 printf (_("\nSymbol table:\n"));
10628 for (i = 0; i < symbol_table_slots; ++i)
10630 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10631 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10632 uint32_t num_cus, cu;
10634 if (name_offset != 0
10635 || cu_vector_offset != 0)
10637 unsigned int j;
10639 /* PR 17531: file: 5b7b07ad. */
10640 if (name_offset >= section->size - constant_pool_offset)
10642 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10643 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10644 name_offset, i);
10646 else
10647 printf ("[%3u] %.*s:", i,
10648 (int) (section->size - (constant_pool_offset + name_offset)),
10649 constant_pool + name_offset);
10651 if (section->size - constant_pool_offset < 4
10652 || cu_vector_offset > section->size - constant_pool_offset - 4)
10654 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10655 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10656 cu_vector_offset, i);
10657 continue;
10660 num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
10662 if ((uint64_t) num_cus * 4 > section->size - (constant_pool_offset
10663 + cu_vector_offset + 4))
10665 printf ("<invalid number of CUs: %d>\n", num_cus);
10666 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
10667 num_cus, i);
10668 continue;
10671 if (num_cus > 1)
10672 printf ("\n");
10674 for (j = 0; j < num_cus; ++j)
10676 int is_static;
10677 gdb_index_symbol_kind kind;
10679 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
10680 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
10681 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10682 cu = GDB_INDEX_CU_VALUE (cu);
10683 /* Convert to TU number if it's for a type unit. */
10684 if (cu >= cu_list_elements)
10685 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10686 (unsigned long) cu - cu_list_elements);
10687 else
10688 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10690 printf (" [%s, %s]",
10691 is_static ? _("static") : _("global"),
10692 get_gdb_index_symbol_kind_name (kind));
10693 if (num_cus > 1)
10694 printf ("\n");
10696 if (num_cus <= 1)
10697 printf ("\n");
10701 return 1;
10704 /* Pre-allocate enough space for the CU/TU sets needed. */
10706 static void
10707 prealloc_cu_tu_list (unsigned int nshndx)
10709 if (shndx_pool == NULL)
10711 shndx_pool_size = nshndx;
10712 shndx_pool_used = 0;
10713 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10714 sizeof (unsigned int));
10716 else
10718 shndx_pool_size = shndx_pool_used + nshndx;
10719 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10720 sizeof (unsigned int));
10724 static void
10725 add_shndx_to_cu_tu_entry (unsigned int shndx)
10727 shndx_pool [shndx_pool_used++] = shndx;
10730 static void
10731 end_cu_tu_entry (void)
10733 shndx_pool [shndx_pool_used++] = 0;
10736 /* Return the short name of a DWARF section given by a DW_SECT enumerator. */
10738 static const char *
10739 get_DW_SECT_short_name (unsigned int dw_sect)
10741 static char buf[16];
10743 switch (dw_sect)
10745 case DW_SECT_INFO:
10746 return "info";
10747 case DW_SECT_TYPES:
10748 return "types";
10749 case DW_SECT_ABBREV:
10750 return "abbrev";
10751 case DW_SECT_LINE:
10752 return "line";
10753 case DW_SECT_LOC:
10754 return "loc";
10755 case DW_SECT_STR_OFFSETS:
10756 return "str_off";
10757 case DW_SECT_MACINFO:
10758 return "macinfo";
10759 case DW_SECT_MACRO:
10760 return "macro";
10761 default:
10762 break;
10765 snprintf (buf, sizeof (buf), "%d", dw_sect);
10766 return buf;
10769 /* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
10770 These sections are extensions for Fission.
10771 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
10773 static int
10774 process_cu_tu_index (struct dwarf_section *section, int do_display)
10776 unsigned char *phdr = section->start;
10777 unsigned char *limit = phdr + section->size;
10778 unsigned char *phash;
10779 unsigned char *pindex;
10780 unsigned char *ppool;
10781 unsigned int version;
10782 unsigned int ncols = 0;
10783 unsigned int nused;
10784 unsigned int nslots;
10785 unsigned int i;
10786 unsigned int j;
10787 uint64_t signature;
10788 size_t total;
10790 /* PR 17512: file: 002-168123-0.004. */
10791 if (phdr == NULL)
10793 warn (_("Section %s is empty\n"), section->name);
10794 return 0;
10796 /* PR 17512: file: 002-376-0.004. */
10797 if (section->size < 24)
10799 warn (_("Section %s is too small to contain a CU/TU header\n"),
10800 section->name);
10801 return 0;
10804 phash = phdr;
10805 SAFE_BYTE_GET_AND_INC (version, phash, 4, limit);
10806 if (version >= 2)
10807 SAFE_BYTE_GET_AND_INC (ncols, phash, 4, limit);
10808 SAFE_BYTE_GET_AND_INC (nused, phash, 4, limit);
10809 SAFE_BYTE_GET_AND_INC (nslots, phash, 4, limit);
10811 pindex = phash + (size_t) nslots * 8;
10812 ppool = pindex + (size_t) nslots * 4;
10814 if (do_display)
10816 introduce (section, false);
10818 printf (_(" Version: %u\n"), version);
10819 if (version >= 2)
10820 printf (_(" Number of columns: %u\n"), ncols);
10821 printf (_(" Number of used entries: %u\n"), nused);
10822 printf (_(" Number of slots: %u\n\n"), nslots);
10825 /* PR 17531: file: 45d69832. */
10826 if (_mul_overflow ((size_t) nslots, 12, &total)
10827 || total > (size_t) (limit - phash))
10829 warn (ngettext ("Section %s is too small for %u slot\n",
10830 "Section %s is too small for %u slots\n",
10831 nslots),
10832 section->name, nslots);
10833 return 0;
10836 if (version == 1)
10838 unsigned char *shndx_list;
10839 unsigned int shndx;
10841 if (!do_display)
10843 prealloc_cu_tu_list ((limit - ppool) / 4);
10844 for (shndx_list = ppool + 4; shndx_list <= limit - 4; shndx_list += 4)
10846 shndx = byte_get (shndx_list, 4);
10847 add_shndx_to_cu_tu_entry (shndx);
10849 end_cu_tu_entry ();
10851 else
10852 for (i = 0; i < nslots; i++)
10854 SAFE_BYTE_GET (signature, phash, 8, limit);
10855 if (signature != 0)
10857 SAFE_BYTE_GET (j, pindex, 4, limit);
10858 shndx_list = ppool + j * 4;
10859 /* PR 17531: file: 705e010d. */
10860 if (shndx_list < ppool)
10862 warn (_("Section index pool located before start of section\n"));
10863 return 0;
10866 printf (_(" [%3d] Signature: %#" PRIx64 " Sections: "),
10867 i, signature);
10868 for (;;)
10870 if (shndx_list >= limit)
10872 warn (_("Section %s too small for shndx pool\n"),
10873 section->name);
10874 return 0;
10876 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
10877 if (shndx == 0)
10878 break;
10879 printf (" %d", shndx);
10880 shndx_list += 4;
10882 printf ("\n");
10884 phash += 8;
10885 pindex += 4;
10888 else if (version == 2)
10890 unsigned int val;
10891 unsigned int dw_sect;
10892 unsigned char *ph = phash;
10893 unsigned char *pi = pindex;
10894 unsigned char *poffsets = ppool + (size_t) ncols * 4;
10895 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
10896 bool is_tu_index;
10897 struct cu_tu_set *this_set = NULL;
10898 unsigned int row;
10899 unsigned char *prow;
10900 size_t temp;
10902 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
10904 /* PR 17531: file: 0dd159bf.
10905 Check for integer overflow (can occur when size_t is 32-bit)
10906 with overlarge ncols or nused values. */
10907 if (nused == -1u
10908 || _mul_overflow ((size_t) ncols, 4, &temp)
10909 || _mul_overflow ((size_t) nused + 1, temp, &total)
10910 || total > (size_t) (limit - ppool))
10912 warn (_("Section %s too small for offset and size tables\n"),
10913 section->name);
10914 return 0;
10917 if (do_display)
10919 printf (_(" Offset table\n"));
10920 printf (" slot %-16s ",
10921 is_tu_index ? _("signature") : _("dwo_id"));
10923 else
10925 if (is_tu_index)
10927 tu_count = nused;
10928 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10929 this_set = tu_sets;
10931 else
10933 cu_count = nused;
10934 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10935 this_set = cu_sets;
10939 if (do_display)
10941 for (j = 0; j < ncols; j++)
10943 unsigned char *p = ppool + j * 4;
10944 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10945 printf (" %8s", get_DW_SECT_short_name (dw_sect));
10947 printf ("\n");
10950 for (i = 0; i < nslots; i++)
10952 SAFE_BYTE_GET (signature, ph, 8, limit);
10954 SAFE_BYTE_GET (row, pi, 4, limit);
10955 if (row != 0)
10957 /* PR 17531: file: a05f6ab3. */
10958 if (row > nused)
10960 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
10961 row, nused);
10962 return 0;
10965 if (!do_display)
10967 size_t num_copy = sizeof (uint64_t);
10969 memcpy (&this_set[row - 1].signature, ph, num_copy);
10972 prow = poffsets + (row - 1) * ncols * 4;
10973 if (do_display)
10974 printf (" [%3d] %#" PRIx64, i, signature);
10975 for (j = 0; j < ncols; j++)
10977 unsigned char *p = prow + j * 4;
10978 SAFE_BYTE_GET (val, p, 4, limit);
10979 if (do_display)
10980 printf (" %8d", val);
10981 else
10983 p = ppool + j * 4;
10984 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10986 /* PR 17531: file: 10796eb3. */
10987 if (dw_sect >= DW_SECT_MAX)
10988 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10989 else
10990 this_set [row - 1].section_offsets [dw_sect] = val;
10994 if (do_display)
10995 printf ("\n");
10997 ph += 8;
10998 pi += 4;
11001 ph = phash;
11002 pi = pindex;
11003 if (do_display)
11005 printf ("\n");
11006 printf (_(" Size table\n"));
11007 printf (" slot %-16s ",
11008 is_tu_index ? _("signature") : _("dwo_id"));
11011 for (j = 0; j < ncols; j++)
11013 unsigned char *p = ppool + j * 4;
11014 SAFE_BYTE_GET (val, p, 4, limit);
11015 if (do_display)
11016 printf (" %8s", get_DW_SECT_short_name (val));
11019 if (do_display)
11020 printf ("\n");
11022 for (i = 0; i < nslots; i++)
11024 SAFE_BYTE_GET (signature, ph, 8, limit);
11026 SAFE_BYTE_GET (row, pi, 4, limit);
11027 if (row != 0)
11029 prow = psizes + (row - 1) * ncols * 4;
11031 if (do_display)
11032 printf (" [%3d] %#" PRIx64, i, signature);
11034 for (j = 0; j < ncols; j++)
11036 unsigned char *p = prow + j * 4;
11038 /* PR 28645: Check for overflow. Since we do not know how
11039 many populated rows there will be, we cannot just
11040 perform a single check at the start of this function. */
11041 if (p > (limit - 4))
11043 if (do_display)
11044 printf ("\n");
11045 warn (_("Too many rows/columns in DWARF index section %s\n"),
11046 section->name);
11047 return 0;
11050 SAFE_BYTE_GET (val, p, 4, limit);
11052 if (do_display)
11053 printf (" %8d", val);
11054 else
11056 p = ppool + j * 4;
11057 SAFE_BYTE_GET (dw_sect, p, 4, limit);
11058 if (dw_sect >= DW_SECT_MAX)
11059 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
11060 else
11061 this_set [row - 1].section_sizes [dw_sect] = val;
11065 if (do_display)
11066 printf ("\n");
11069 ph += 8;
11070 pi += 4;
11073 else if (do_display)
11074 printf (_(" Unsupported version (%d)\n"), version);
11076 if (do_display)
11077 printf ("\n");
11079 return 1;
11082 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
11084 /* Load the CU and TU indexes if present. This will build a list of
11085 section sets that we can use to associate a .debug_info.dwo section
11086 with its associated .debug_abbrev.dwo section in a .dwp file. */
11088 static bool
11089 load_cu_tu_indexes (void *file)
11091 /* If we have already loaded (or tried to load) the CU and TU indexes
11092 then do not bother to repeat the task. */
11093 if (cu_tu_indexes_read == -1)
11095 cu_tu_indexes_read = true;
11097 if (load_debug_section_with_follow (dwp_cu_index, file))
11098 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
11099 cu_tu_indexes_read = false;
11101 if (load_debug_section_with_follow (dwp_tu_index, file))
11102 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
11103 cu_tu_indexes_read = false;
11106 return (bool) cu_tu_indexes_read;
11109 /* Find the set of sections that includes section SHNDX. */
11111 unsigned int *
11112 find_cu_tu_set (void *file, unsigned int shndx)
11114 unsigned int i;
11116 if (! load_cu_tu_indexes (file))
11117 return NULL;
11119 /* Find SHNDX in the shndx pool. */
11120 for (i = 0; i < shndx_pool_used; i++)
11121 if (shndx_pool [i] == shndx)
11122 break;
11124 if (i >= shndx_pool_used)
11125 return NULL;
11127 /* Now backup to find the first entry in the set. */
11128 while (i > 0 && shndx_pool [i - 1] != 0)
11129 i--;
11131 return shndx_pool + i;
11134 /* Display a .debug_cu_index or .debug_tu_index section. */
11136 static int
11137 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
11139 return process_cu_tu_index (section, 1);
11142 static int
11143 display_debug_not_supported (struct dwarf_section *section,
11144 void *file ATTRIBUTE_UNUSED)
11146 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
11147 section->name);
11149 return 1;
11152 /* Like malloc, but takes two parameters like calloc.
11153 Verifies that the first parameter is not too large.
11154 Note: does *not* initialise the allocated memory to zero. */
11156 void *
11157 cmalloc (uint64_t nmemb, size_t size)
11159 /* Check for overflow. */
11160 if (nmemb >= ~(size_t) 0 / size)
11161 return NULL;
11163 return xmalloc (nmemb * size);
11166 /* Like xmalloc, but takes two parameters like calloc.
11167 Verifies that the first parameter is not too large.
11168 Note: does *not* initialise the allocated memory to zero. */
11170 void *
11171 xcmalloc (uint64_t nmemb, size_t size)
11173 /* Check for overflow. */
11174 if (nmemb >= ~(size_t) 0 / size)
11176 fprintf (stderr,
11177 _("Attempt to allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11178 nmemb);
11179 xexit (1);
11182 return xmalloc (nmemb * size);
11185 /* Like xrealloc, but takes three parameters.
11186 Verifies that the second parameter is not too large.
11187 Note: does *not* initialise any new memory to zero. */
11189 void *
11190 xcrealloc (void *ptr, uint64_t nmemb, size_t size)
11192 /* Check for overflow. */
11193 if (nmemb >= ~(size_t) 0 / size)
11195 error (_("Attempt to re-allocate an array with an excessive number of elements: %#" PRIx64 "\n"),
11196 nmemb);
11197 xexit (1);
11200 return xrealloc (ptr, nmemb * size);
11203 /* Like xcalloc, but verifies that the first parameter is not too large. */
11205 void *
11206 xcalloc2 (uint64_t nmemb, size_t size)
11208 /* Check for overflow. */
11209 if (nmemb >= ~(size_t) 0 / size)
11211 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: %#" PRIx64 "\n"),
11212 nmemb);
11213 xexit (1);
11216 return xcalloc (nmemb, size);
11219 static unsigned long
11220 calc_gnu_debuglink_crc32 (unsigned long crc,
11221 const unsigned char *buf,
11222 size_t len)
11224 static const unsigned long crc32_table[256] =
11226 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
11227 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
11228 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
11229 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
11230 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
11231 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
11232 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
11233 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
11234 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
11235 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
11236 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
11237 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
11238 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
11239 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
11240 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
11241 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
11242 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
11243 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
11244 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
11245 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
11246 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
11247 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
11248 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
11249 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
11250 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
11251 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
11252 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
11253 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
11254 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
11255 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
11256 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
11257 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
11258 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
11259 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
11260 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
11261 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
11262 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
11263 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
11264 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
11265 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
11266 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
11267 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
11268 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
11269 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
11270 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
11271 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
11272 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
11273 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
11274 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
11275 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
11276 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
11277 0x2d02ef8d
11279 const unsigned char *end;
11281 crc = ~crc & 0xffffffff;
11282 for (end = buf + len; buf < end; ++ buf)
11283 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
11284 return ~crc & 0xffffffff;
11287 typedef bool (*check_func_type) (const char *, void *);
11288 typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
11290 static bool
11291 check_gnu_debuglink (const char * pathname, void * crc_pointer)
11293 static unsigned char buffer[8 * 1024];
11294 FILE *f;
11295 size_t count;
11296 unsigned long crc = 0;
11297 void *sep_data;
11299 sep_data = open_debug_file (pathname);
11300 if (sep_data == NULL)
11301 return false;
11303 /* Yes - we are opening the file twice... */
11304 f = fopen (pathname, "rb");
11305 if (f == NULL)
11307 /* Paranoia: This should never happen. */
11308 close_debug_file (sep_data);
11309 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
11310 return false;
11313 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
11314 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
11316 fclose (f);
11318 if (crc != * (unsigned long *) crc_pointer)
11320 close_debug_file (sep_data);
11321 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
11322 pathname);
11323 return false;
11326 return true;
11329 static const char *
11330 parse_gnu_debuglink (struct dwarf_section * section, void * data)
11332 const char * name;
11333 unsigned int crc_offset;
11334 unsigned long * crc32 = (unsigned long *) data;
11336 /* The name is first.
11337 The CRC value is stored after the filename, aligned up to 4 bytes. */
11338 name = (const char *) section->start;
11340 crc_offset = strnlen (name, section->size) + 1;
11341 if (crc_offset == 1)
11342 return NULL;
11343 crc_offset = (crc_offset + 3) & ~3;
11344 if (crc_offset + 4 > section->size)
11345 return NULL;
11347 * crc32 = byte_get (section->start + crc_offset, 4);
11348 return name;
11351 static bool
11352 check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
11354 void * sep_data = open_debug_file (filename);
11356 if (sep_data == NULL)
11357 return false;
11359 /* FIXME: We should now extract the build-id in the separate file
11360 and check it... */
11362 return true;
11365 typedef struct build_id_data
11367 size_t len;
11368 const unsigned char *data;
11369 } Build_id_data;
11371 static const char *
11372 parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
11374 const char *name;
11375 size_t namelen;
11376 size_t id_len;
11377 Build_id_data *build_id_data;
11379 /* The name is first.
11380 The build-id follows immediately, with no padding, up to the section's end. */
11382 name = (const char *) section->start;
11383 namelen = strnlen (name, section->size) + 1;
11384 if (namelen == 1)
11385 return NULL;
11386 if (namelen >= section->size)
11387 return NULL;
11389 id_len = section->size - namelen;
11390 if (id_len < 0x14)
11391 return NULL;
11393 build_id_data = (Build_id_data *) data;
11394 build_id_data->len = id_len;
11395 build_id_data->data = section->start + namelen;
11397 return name;
11400 static void
11401 add_separate_debug_file (const char * filename, void * handle)
11403 separate_info * i = xmalloc (sizeof * i);
11405 i->filename = filename;
11406 i->handle = handle;
11407 i->next = first_separate_info;
11408 first_separate_info = i;
11411 #if HAVE_LIBDEBUGINFOD
11412 /* Query debuginfod servers for the target debuglink or debugaltlink
11413 file. If successful, store the path of the file in filename and
11414 return TRUE, otherwise return FALSE. */
11416 static bool
11417 debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
11418 char ** filename,
11419 void * file)
11421 size_t build_id_len;
11422 unsigned char * build_id;
11424 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
11426 /* Get the build-id of file. */
11427 build_id = get_build_id (file);
11428 build_id_len = 0;
11430 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
11432 /* Get the build-id of the debugaltlink file. */
11433 unsigned int filelen;
11435 filelen = strnlen ((const char *)section->start, section->size);
11436 if (filelen == section->size)
11437 /* Corrupt debugaltlink. */
11438 return false;
11440 build_id = section->start + filelen + 1;
11441 build_id_len = section->size - (filelen + 1);
11443 if (build_id_len == 0)
11444 return false;
11446 else
11447 return false;
11449 if (build_id)
11451 int fd;
11452 debuginfod_client * client;
11454 client = debuginfod_begin ();
11455 if (client == NULL)
11456 return false;
11458 /* Query debuginfod servers for the target file. If found its path
11459 will be stored in filename. */
11460 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11461 debuginfod_end (client);
11463 /* Only free build_id if we allocated space for a hex string
11464 in get_build_id (). */
11465 if (build_id_len == 0)
11466 free (build_id);
11468 if (fd >= 0)
11470 /* File successfully retrieved. Close fd since we want to
11471 use open_debug_file () on filename instead. */
11472 close (fd);
11473 return true;
11477 return false;
11479 #endif /* HAVE_LIBDEBUGINFOD */
11481 static void *
11482 load_separate_debug_info (const char * main_filename,
11483 struct dwarf_section * xlink,
11484 parse_func_type parse_func,
11485 check_func_type check_func,
11486 void * func_data,
11487 void * file ATTRIBUTE_UNUSED)
11489 const char * separate_filename;
11490 char * debug_filename;
11491 char * canon_dir;
11492 size_t canon_dirlen;
11493 size_t dirlen;
11494 char * canon_filename;
11495 char * canon_debug_filename;
11496 bool self;
11498 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
11500 warn (_("Corrupt debuglink section: %s\n"),
11501 xlink->name ? xlink->name : xlink->uncompressed_name);
11502 return NULL;
11505 /* Attempt to locate the separate file.
11506 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
11508 canon_filename = lrealpath (main_filename);
11509 canon_dir = xstrdup (canon_filename);
11511 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11512 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11513 break;
11514 canon_dir[canon_dirlen] = '\0';
11516 #ifndef DEBUGDIR
11517 #define DEBUGDIR "/lib/debug"
11518 #endif
11519 #ifndef EXTRA_DEBUG_ROOT1
11520 #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11521 #endif
11522 #ifndef EXTRA_DEBUG_ROOT2
11523 #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11524 #endif
11526 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11527 + canon_dirlen
11528 + strlen (".debug/")
11529 #ifdef EXTRA_DEBUG_ROOT1
11530 + strlen (EXTRA_DEBUG_ROOT1)
11531 #endif
11532 #ifdef EXTRA_DEBUG_ROOT2
11533 + strlen (EXTRA_DEBUG_ROOT2)
11534 #endif
11535 + strlen (separate_filename)
11536 + 1);
11537 if (debug_filename == NULL)
11539 warn (_("Out of memory"));
11540 free (canon_dir);
11541 free (canon_filename);
11542 return NULL;
11545 /* First try in the current directory. */
11546 sprintf (debug_filename, "%s", separate_filename);
11547 if (check_func (debug_filename, func_data))
11548 goto found;
11550 /* Then try in a subdirectory called .debug. */
11551 sprintf (debug_filename, ".debug/%s", separate_filename);
11552 if (check_func (debug_filename, func_data))
11553 goto found;
11555 /* Then try in the same directory as the original file. */
11556 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11557 if (check_func (debug_filename, func_data))
11558 goto found;
11560 /* And the .debug subdirectory of that directory. */
11561 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11562 if (check_func (debug_filename, func_data))
11563 goto found;
11565 #ifdef EXTRA_DEBUG_ROOT1
11566 /* Try the first extra debug file root. */
11567 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11568 if (check_func (debug_filename, func_data))
11569 goto found;
11571 /* Try the first extra debug file root. */
11572 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11573 if (check_func (debug_filename, func_data))
11574 goto found;
11575 #endif
11577 #ifdef EXTRA_DEBUG_ROOT2
11578 /* Try the second extra debug file root. */
11579 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11580 if (check_func (debug_filename, func_data))
11581 goto found;
11582 #endif
11584 /* Then try in the global debug_filename directory. */
11585 strcpy (debug_filename, DEBUGDIR);
11586 dirlen = strlen (DEBUGDIR) - 1;
11587 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
11588 strcat (debug_filename, "/");
11589 strcat (debug_filename, (const char *) separate_filename);
11591 if (check_func (debug_filename, func_data))
11592 goto found;
11594 #if HAVE_LIBDEBUGINFOD
11596 char * tmp_filename;
11598 if (use_debuginfod
11599 && debuginfod_fetch_separate_debug_info (xlink,
11600 & tmp_filename,
11601 file))
11603 /* File successfully downloaded from server, replace
11604 debug_filename with the file's path. */
11605 free (debug_filename);
11606 debug_filename = tmp_filename;
11607 goto found;
11610 #endif
11612 if (do_debug_links)
11614 /* Failed to find the file. */
11615 warn (_("could not find separate debug file '%s'\n"),
11616 separate_filename);
11617 warn (_("tried: %s\n"), debug_filename);
11619 #ifdef EXTRA_DEBUG_ROOT2
11620 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11621 separate_filename);
11622 warn (_("tried: %s\n"), debug_filename);
11623 #endif
11625 #ifdef EXTRA_DEBUG_ROOT1
11626 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11627 canon_dir, separate_filename);
11628 warn (_("tried: %s\n"), debug_filename);
11630 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11631 separate_filename);
11632 warn (_("tried: %s\n"), debug_filename);
11633 #endif
11635 sprintf (debug_filename, "%s.debug/%s", canon_dir,
11636 separate_filename);
11637 warn (_("tried: %s\n"), debug_filename);
11639 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11640 warn (_("tried: %s\n"), debug_filename);
11642 sprintf (debug_filename, ".debug/%s", separate_filename);
11643 warn (_("tried: %s\n"), debug_filename);
11645 sprintf (debug_filename, "%s", separate_filename);
11646 warn (_("tried: %s\n"), debug_filename);
11648 #if HAVE_LIBDEBUGINFOD
11649 if (use_debuginfod)
11651 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11653 if (urls == NULL)
11654 urls = "";
11656 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11658 #endif
11661 free (canon_dir);
11662 free (debug_filename);
11663 free (canon_filename);
11664 return NULL;
11666 found:
11667 free (canon_dir);
11669 canon_debug_filename = lrealpath (debug_filename);
11670 self = strcmp (canon_debug_filename, canon_filename) == 0;
11671 free (canon_filename);
11672 free (canon_debug_filename);
11673 if (self)
11675 free (debug_filename);
11676 return NULL;
11679 void * debug_handle;
11681 /* Now open the file.... */
11682 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
11684 warn (_("failed to open separate debug file: %s\n"), debug_filename);
11685 free (debug_filename);
11686 return NULL;
11689 /* FIXME: We do not check to see if there are any other separate debug info
11690 files that would also match. */
11692 if (do_debug_links)
11693 printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
11694 add_separate_debug_file (debug_filename, debug_handle);
11696 /* Do not free debug_filename - it might be referenced inside
11697 the structure returned by open_debug_file(). */
11698 return debug_handle;
11701 /* Attempt to load a separate dwarf object file. */
11703 static void *
11704 load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
11706 char * separate_filename;
11707 void * separate_handle;
11709 if (IS_ABSOLUTE_PATH (name))
11710 separate_filename = strdup (name);
11711 else
11712 /* FIXME: Skip adding / if dwo_dir ends in /. */
11713 separate_filename = concat (dir, "/", name, NULL);
11714 if (separate_filename == NULL)
11716 warn (_("Out of memory allocating dwo filename\n"));
11717 return NULL;
11720 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
11722 warn (_("Unable to load dwo file: %s\n"), separate_filename);
11723 free (separate_filename);
11724 return NULL;
11727 /* FIXME: We should check the dwo_id. */
11729 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
11731 add_separate_debug_file (separate_filename, separate_handle);
11732 /* Note - separate_filename will be freed in free_debug_memory(). */
11733 return separate_handle;
11736 static void *
11737 try_build_id_prefix (const char * prefix, char * filename, const unsigned char * data, unsigned long id_len)
11739 char * f = filename;
11741 f += sprintf (f, "%s.build-id/%02x/", prefix, (unsigned) *data++);
11742 id_len --;
11743 while (id_len --)
11744 f += sprintf (f, "%02x", (unsigned) *data++);
11745 strcpy (f, ".debug");
11747 return open_debug_file (filename);
11750 /* Try to load a debug file based upon the build-id held in the .note.gnu.build-id section. */
11752 static void
11753 load_build_id_debug_file (const char * main_filename ATTRIBUTE_UNUSED, void * main_file)
11755 if (! load_debug_section (note_gnu_build_id, main_file))
11756 return; /* No .note.gnu.build-id section. */
11758 struct dwarf_section * section = & debug_displays [note_gnu_build_id].section;
11759 if (section == NULL)
11761 warn (_("Unable to load the .note.gnu.build-id section\n"));
11762 return;
11765 if (section->start == NULL || section->size < 0x18)
11767 warn (_(".note.gnu.build-id section is corrupt/empty\n"));
11768 return;
11771 /* In theory we should extract the contents of the section into
11772 a note structure and then check the fields. For now though
11773 just use hard coded offsets instead:
11775 Field Bytes Contents
11776 NSize 0...3 4
11777 DSize 4...7 8+
11778 Type 8..11 3 (NT_GNU_BUILD_ID)
11779 Name 12.15 GNU\0
11780 Data 16.... */
11782 /* FIXME: Check the name size, name and type fields. */
11784 unsigned long build_id_size;
11785 build_id_size = byte_get (section->start + 4, 4);
11786 if (build_id_size < 8)
11788 warn (_(".note.gnu.build-id data size is too small\n"));
11789 return;
11792 if (build_id_size > (section->size - 16))
11794 warn (_(".note.gnu.build-id data size is too big\n"));
11795 return;
11798 char * filename;
11799 filename = xmalloc (strlen (".build-id/")
11800 + build_id_size * 2 + 2
11801 + strlen (".debug")
11802 /* The next string should be the same as the longest
11803 name found in the prefixes[] array below. */
11804 + strlen ("/usrlib64/debug/usr")
11805 + 1);
11806 void * handle;
11808 static const char * prefixes[] =
11811 ".debug/",
11812 "/usr/lib/debug/",
11813 "/usr/lib/debug/usr/",
11814 "/usr/lib64/debug/",
11815 "/usr/lib64/debug/usr"
11817 long unsigned int i;
11819 for (i = 0; i < ARRAY_SIZE (prefixes); i++)
11821 handle = try_build_id_prefix (prefixes[i], filename,
11822 section->start + 16, build_id_size);
11823 if (handle != NULL)
11824 break;
11826 /* FIXME: TYhe BFD library also tries a global debugfile directory prefix. */
11827 if (handle == NULL)
11829 /* Failed to find a debug file associated with the build-id.
11830 This is not an error however, rather it just means that
11831 the debug info has probably not been loaded on the system,
11832 or that another method is being used to link to the debug
11833 info. */
11834 free (filename);
11835 return;
11838 add_separate_debug_file (filename, handle);
11841 /* Try to load a debug file pointed to by the .debug_sup section. */
11843 static void
11844 load_debug_sup_file (const char * main_filename, void * file)
11846 if (! load_debug_section (debug_sup, file))
11847 return; /* No .debug_sup section. */
11849 struct dwarf_section * section;
11850 section = & debug_displays [debug_sup].section;
11851 assert (section != NULL);
11853 if (section->start == NULL || section->size < 5)
11855 warn (_(".debug_sup section is corrupt/empty\n"));
11856 return;
11859 if (section->start[2] != 0)
11860 return; /* This is a supplementary file. */
11862 const char * filename = (const char *) section->start + 3;
11863 if (strnlen (filename, section->size - 3) == section->size - 3)
11865 warn (_("filename in .debug_sup section is corrupt\n"));
11866 return;
11869 if (filename[0] != '/' && strchr (main_filename, '/'))
11871 char * new_name;
11872 int new_len;
11874 new_len = asprintf (& new_name, "%.*s/%s",
11875 (int) (strrchr (main_filename, '/') - main_filename),
11876 main_filename,
11877 filename);
11878 if (new_len < 3)
11880 warn (_("unable to construct path for supplementary debug file"));
11881 if (new_len > -1)
11882 free (new_name);
11883 return;
11885 filename = new_name;
11887 else
11889 /* PR 27796: Make sure that we pass a filename that can be free'd to
11890 add_separate_debug_file(). */
11891 filename = strdup (filename);
11892 if (filename == NULL)
11894 warn (_("out of memory constructing filename for .debug_sup link\n"));
11895 return;
11899 void * handle = open_debug_file (filename);
11900 if (handle == NULL)
11902 warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
11903 free ((void *) filename);
11904 return;
11907 printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
11909 /* FIXME: Compare the checksums, if present. */
11910 add_separate_debug_file (filename, handle);
11913 /* Load a debuglink section and/or a debugaltlink section, if either are present.
11914 Recursively check the loaded files for more of these sections.
11915 Also follow any links in .debug_sup sections.
11916 FIXME: Should also check for DWO_* entries in the newly loaded files. */
11918 static void
11919 check_for_and_load_links (void * file, const char * filename)
11921 void * handle = NULL;
11923 if (load_debug_section (gnu_debugaltlink, file))
11925 Build_id_data build_id_data;
11927 handle = load_separate_debug_info (filename,
11928 & debug_displays[gnu_debugaltlink].section,
11929 parse_gnu_debugaltlink,
11930 check_gnu_debugaltlink,
11931 & build_id_data,
11932 file);
11933 if (handle)
11935 assert (handle == first_separate_info->handle);
11936 check_for_and_load_links (first_separate_info->handle,
11937 first_separate_info->filename);
11941 if (load_debug_section (gnu_debuglink, file))
11943 unsigned long crc32;
11945 handle = load_separate_debug_info (filename,
11946 & debug_displays[gnu_debuglink].section,
11947 parse_gnu_debuglink,
11948 check_gnu_debuglink,
11949 & crc32,
11950 file);
11951 if (handle)
11953 assert (handle == first_separate_info->handle);
11954 check_for_and_load_links (first_separate_info->handle,
11955 first_separate_info->filename);
11959 load_debug_sup_file (filename, file);
11961 load_build_id_debug_file (filename, file);
11964 /* Load the separate debug info file(s) attached to FILE, if any exist.
11965 Returns TRUE if any were found, FALSE otherwise.
11966 If TRUE is returned then the linked list starting at first_separate_info
11967 will be populated with open file handles. */
11969 bool
11970 load_separate_debug_files (void * file, const char * filename)
11972 /* Skip this operation if we are not interested in debug links. */
11973 if (! do_follow_links && ! do_debug_links)
11974 return false;
11976 /* See if there are any dwo links. */
11977 if (load_debug_section (str, file)
11978 && load_debug_section (abbrev, file)
11979 && load_debug_section (info, file))
11981 /* Load the .debug_addr section, if it exists. */
11982 load_debug_section (debug_addr, file);
11983 /* Load the .debug_str_offsets section, if it exists. */
11984 load_debug_section (str_index, file);
11985 /* Load the .debug_loclists section, if it exists. */
11986 load_debug_section (loclists, file);
11987 /* Load the .debug_rnglists section, if it exists. */
11988 load_debug_section (rnglists, file);
11990 free_dwo_info ();
11992 if (process_debug_info (& debug_displays[info].section, file, abbrev,
11993 true, false))
11995 bool introduced = false;
11996 dwo_info *dwinfo;
11997 const char *dir = NULL;
11998 const char *id = NULL;
11999 const char *name = NULL;
12001 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
12003 /* Accumulate NAME, DIR and ID fields. */
12004 switch (dwinfo->type)
12006 case DWO_NAME:
12007 if (name != NULL)
12008 warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
12009 name = dwinfo->value;
12010 break;
12012 case DWO_DIR:
12013 /* There can be multiple DW_AT_comp_dir entries in a CU,
12014 so do not complain. */
12015 dir = dwinfo->value;
12016 break;
12018 case DWO_ID:
12019 if (id != NULL)
12020 warn (_("multiple DWO_IDs encountered for the same CU\n"));
12021 id = dwinfo->value;
12022 break;
12024 default:
12025 error (_("Unexpected DWO INFO type"));
12026 break;
12029 /* If we have reached the end of our list, or we are changing
12030 CUs, then display the information that we have accumulated
12031 so far. */
12032 if (name != NULL
12033 && (dwinfo->next == NULL
12034 || dwinfo->next->cu_offset != dwinfo->cu_offset))
12036 if (do_debug_links)
12038 if (! introduced)
12040 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
12041 debug_displays [info].section.uncompressed_name);
12042 introduced = true;
12045 printf (_(" Name: %s\n"), name);
12046 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
12047 if (id != NULL)
12048 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
12049 else if (debug_information[0].dwarf_version != 5)
12050 printf (_(" ID: <not specified>\n"));
12051 printf ("\n\n");
12054 if (do_follow_links)
12055 load_dwo_file (filename, name, dir, id);
12057 name = dir = id = NULL;
12063 if (! do_follow_links)
12064 /* The other debug links will be displayed by display_debug_links()
12065 so we do not need to do any further processing here. */
12066 return false;
12068 /* FIXME: We do not check for the presence of both link sections in the same file. */
12069 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
12070 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
12072 check_for_and_load_links (file, filename);
12073 if (first_separate_info != NULL)
12074 return true;
12076 do_follow_links = 0;
12077 return false;
12080 void
12081 free_debug_memory (void)
12083 unsigned int i;
12085 free_all_abbrevs ();
12087 free (shndx_pool);
12088 shndx_pool = NULL;
12089 shndx_pool_size = 0;
12090 shndx_pool_used = 0;
12091 free (cu_sets);
12092 cu_sets = NULL;
12093 cu_count = 0;
12094 free (tu_sets);
12095 tu_sets = NULL;
12096 tu_count = 0;
12098 memset (level_type_signed, 0, sizeof level_type_signed);
12099 cu_tu_indexes_read = -1;
12101 for (i = 0; i < max; i++)
12102 free_debug_section ((enum dwarf_section_display_enum) i);
12104 if (debug_information != NULL)
12106 for (i = 0; i < alloc_num_debug_info_entries; i++)
12107 free_debug_information (&debug_information[i]);
12108 free (debug_information);
12109 debug_information = NULL;
12110 alloc_num_debug_info_entries = num_debug_info_entries = 0;
12113 separate_info * d;
12114 separate_info * next;
12116 for (d = first_separate_info; d != NULL; d = next)
12118 close_debug_file (d->handle);
12119 free ((void *) d->filename);
12120 next = d->next;
12121 free ((void *) d);
12123 first_separate_info = NULL;
12125 free_dwo_info ();
12128 typedef struct
12130 const char letter;
12131 const char *option;
12132 int *variable;
12133 int val;
12134 } debug_dump_long_opts;
12136 static const debug_dump_long_opts debug_option_table[] =
12138 { 'A', "addr", &do_debug_addr, 1 },
12139 { 'a', "abbrev", &do_debug_abbrevs, 1 },
12140 { 'c', "cu_index", &do_debug_cu_index, 1 },
12141 #ifdef HAVE_LIBDEBUGINFOD
12142 { 'D', "use-debuginfod", &use_debuginfod, 1 },
12143 { 'E', "do-not-use-debuginfod", &use_debuginfod, 0 },
12144 #endif
12145 { 'F', "frames-interp", &do_debug_frames_interp, 1 },
12146 { 'f', "frames", &do_debug_frames, 1 },
12147 { 'g', "gdb_index", &do_gdb_index, 1 },
12148 { 'i', "info", &do_debug_info, 1 },
12149 { 'K', "follow-links", &do_follow_links, 1 },
12150 { 'k', "links", &do_debug_links, 1 },
12151 { 'L', "decodedline", &do_debug_lines, FLAG_DEBUG_LINES_DECODED },
12152 { 'l', "rawline", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12153 /* For compatibility with earlier versions of readelf. */
12154 { 'l', "line", &do_debug_lines, FLAG_DEBUG_LINES_RAW },
12155 { 'm', "macro", &do_debug_macinfo, 1 },
12156 { 'N', "no-follow-links", &do_follow_links, 0 },
12157 { 'O', "str-offsets", &do_debug_str_offsets, 1 },
12158 { 'o', "loc", &do_debug_loc, 1 },
12159 { 'p', "pubnames", &do_debug_pubnames, 1 },
12160 { 'R', "Ranges", &do_debug_ranges, 1 },
12161 { 'r', "aranges", &do_debug_aranges, 1 },
12162 /* For compatibility with earlier versions of readelf. */
12163 { 'r', "ranges", &do_debug_aranges, 1 },
12164 { 's', "str", &do_debug_str, 1 },
12165 { 'T', "trace_aranges", &do_trace_aranges, 1 },
12166 { 't', "pubtypes", &do_debug_pubtypes, 1 },
12167 { 'U', "trace_info", &do_trace_info, 1 },
12168 { 'u', "trace_abbrev", &do_trace_abbrevs, 1 },
12169 { 0, NULL, NULL, 0 }
12172 /* Enable display of specific DWARF sections as determined by the comma
12173 separated strings in NAMES. Returns non-zero if any displaying was
12174 enabled. */
12177 dwarf_select_sections_by_names (const char *names)
12179 const char *p;
12180 int result = 0;
12182 p = names;
12183 while (*p)
12185 const debug_dump_long_opts *entry;
12187 for (entry = debug_option_table; entry->option; entry++)
12189 size_t len = strlen (entry->option);
12191 if (strncmp (p, entry->option, len) == 0
12192 && (p[len] == ',' || p[len] == '\0'))
12194 if (entry->val == 0)
12195 * entry->variable = 0;
12196 else
12197 * entry->variable = entry->val;
12198 result |= entry->val;
12200 p += len;
12201 break;
12205 if (entry->option == NULL)
12207 warn (_("Unrecognized debug option '%s'\n"), p);
12208 p = strchr (p, ',');
12209 if (p == NULL)
12210 break;
12213 if (*p == ',')
12214 p++;
12217 /* The --debug-dump=frames-interp option also enables the
12218 --debug-dump=frames option. */
12219 if (do_debug_frames_interp)
12220 do_debug_frames = 1;
12222 return result;
12225 /* Enable display of specific DWARF sections as determined by the characters
12226 in LETTERS. Returns non-zero if any displaying was enabled. */
12229 dwarf_select_sections_by_letters (const char *letters)
12231 int result = 0;
12233 while (* letters)
12235 const debug_dump_long_opts *entry;
12237 for (entry = debug_option_table; entry->letter; entry++)
12239 if (entry->letter == * letters)
12241 if (entry->val == 0)
12242 * entry->variable = 0;
12243 else
12244 * entry->variable |= entry->val;
12245 result |= entry->val;
12246 break;
12250 if (entry->letter == 0)
12251 warn (_("Unrecognized debug letter option '%c'\n"), * letters);
12253 letters ++;
12256 /* The --debug-dump=frames-interp option also enables the
12257 --debug-dump=frames option. */
12258 if (do_debug_frames_interp)
12259 do_debug_frames = 1;
12261 return result;
12264 void
12265 dwarf_select_sections_all (void)
12267 do_debug_info = 1;
12268 do_debug_abbrevs = 1;
12269 do_debug_lines = FLAG_DEBUG_LINES_RAW;
12270 do_debug_pubnames = 1;
12271 do_debug_pubtypes = 1;
12272 do_debug_aranges = 1;
12273 do_debug_ranges = 1;
12274 do_debug_frames = 1;
12275 do_debug_macinfo = 1;
12276 do_debug_str = 1;
12277 do_debug_loc = 1;
12278 do_gdb_index = 1;
12279 do_trace_info = 1;
12280 do_trace_abbrevs = 1;
12281 do_trace_aranges = 1;
12282 do_debug_addr = 1;
12283 do_debug_cu_index = 1;
12284 do_follow_links = 1;
12285 do_debug_links = 1;
12286 do_debug_str_offsets = 1;
12289 #define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0
12290 #define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0
12292 /* N.B. The order here must match the order in section_display_enum. */
12294 struct dwarf_section_display debug_displays[] =
12296 { { ".debug_abbrev", ".zdebug_abbrev", ".dwabrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12297 { { ".debug_aranges", ".zdebug_aranges", ".dwarnge", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, true },
12298 { { ".debug_frame", ".zdebug_frame", ".dwframe", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12299 { { ".debug_info", ".zdebug_info", ".dwinfo", ABBREV (abbrev)}, display_debug_info, &do_debug_info, true },
12300 { { ".debug_line", ".zdebug_line", ".dwline", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12301 { { ".debug_pubnames", ".zdebug_pubnames", ".dwpbnms", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, false },
12302 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, false },
12303 { { ".eh_frame", "", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12304 { { ".debug_macinfo", ".zdebug_macinfo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12305 { { ".debug_macro", ".zdebug_macro", ".dwmac", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12306 { { ".debug_str", ".zdebug_str", ".dwstr", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12307 { { ".debug_line_str", ".zdebug_line_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12308 { { ".debug_loc", ".zdebug_loc", ".dwloc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12309 { { ".debug_loclists", ".zdebug_loclists", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12310 { { ".debug_loclists.dwo", ".zdebug_loclists.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12311 { { ".debug_pubtypes", ".zdebug_pubtypes", ".dwpbtyp", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, false },
12312 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, false },
12313 { { ".debug_ranges", ".zdebug_ranges", ".dwrnges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12314 { { ".debug_rnglists", ".zdebug_rnglists", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12315 { { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12316 { { ".debug_static_func", ".zdebug_static_func", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12317 { { ".debug_static_vars", ".zdebug_static_vars", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12318 { { ".debug_types", ".zdebug_types", "", ABBREV (abbrev) }, display_debug_types, &do_debug_info, true },
12319 { { ".debug_weaknames", ".zdebug_weaknames", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12320 { { ".gdb_index", "", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false },
12321 { { ".debug_names", "", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false },
12322 { { ".trace_info", "", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true },
12323 { { ".trace_abbrev", "", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false },
12324 { { ".trace_aranges", "", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false },
12325 { { ".debug_info.dwo", ".zdebug_info.dwo", "", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
12326 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", "", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12327 { { ".debug_types.dwo", ".zdebug_types.dwo", "", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, true },
12328 { { ".debug_line.dwo", ".zdebug_line.dwo", "", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12329 { { ".debug_loc.dwo", ".zdebug_loc.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12330 { { ".debug_macro.dwo", ".zdebug_macro.dwo", "", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12331 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12332 { { ".debug_str.dwo", ".zdebug_str.dwo", "", NO_ABBREVS }, display_debug_str, &do_debug_str, true },
12333 { { ".debug_str_offsets", ".zdebug_str_offsets", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12334 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12335 { { ".debug_addr", ".zdebug_addr", "", NO_ABBREVS }, display_debug_addr, &do_debug_addr, true },
12336 { { ".debug_cu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12337 { { ".debug_tu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12338 { { ".gnu_debuglink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12339 { { ".gnu_debugaltlink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12340 { { ".debug_sup", "", "", NO_ABBREVS }, display_debug_sup, &do_debug_links, false },
12341 /* Separate debug info files can containt their own .debug_str section,
12342 and this might be in *addition* to a .debug_str section already present
12343 in the main file. Hence we need to have two entries for .debug_str. */
12344 { { ".debug_str", ".zdebug_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12345 { { ".note.gnu.build-id", "", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12348 /* A static assertion. */
12349 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];