gold: don't invoke IA32 syscall in x86_64 assembly testcase
[binutils-gdb.git] / binutils / dwarf.c
blobe61c63a06012715208d80889927ee3b9144a0fab
1 /* dwarf.c -- display DWARF contents of a BFD binary file
2 Copyright (C) 2005-2022 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 dwarf_vma 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 /* Convenient constant, to avoid having to cast -1 to dwarf_vma when
123 testing whether e.g. a locview list is present. */
124 static const dwarf_vma vm1 = -1;
126 /* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
127 sections. For version 1 package files, each set is stored in SHNDX_POOL
128 as a zero-terminated list of section indexes comprising one set of debug
129 sections from a .dwo file. */
131 static unsigned int *shndx_pool = NULL;
132 static unsigned int shndx_pool_size = 0;
133 static unsigned int shndx_pool_used = 0;
135 /* For version 2 package files, each set contains an array of section offsets
136 and an array of section sizes, giving the offset and size of the
137 contribution from a CU or TU within one of the debug sections.
138 When displaying debug info from a package file, we need to use these
139 tables to locate the corresponding contributions to each section. */
141 struct cu_tu_set
143 uint64_t signature;
144 dwarf_vma section_offsets[DW_SECT_MAX];
145 size_t section_sizes[DW_SECT_MAX];
148 static int cu_count = 0;
149 static int tu_count = 0;
150 static struct cu_tu_set *cu_sets = NULL;
151 static struct cu_tu_set *tu_sets = NULL;
153 static bool load_cu_tu_indexes (void *);
155 /* An array that indicates for a given level of CU nesting whether
156 the latest DW_AT_type seen for that level was a signed type or
157 an unsigned type. */
158 #define MAX_CU_NESTING (1 << 8)
159 static bool level_type_signed[MAX_CU_NESTING];
161 /* Values for do_debug_lines. */
162 #define FLAG_DEBUG_LINES_RAW 1
163 #define FLAG_DEBUG_LINES_DECODED 2
165 static unsigned int
166 size_of_encoded_value (int encoding)
168 switch (encoding & 0x7)
170 default: /* ??? */
171 case 0: return eh_addr_size;
172 case 2: return 2;
173 case 3: return 4;
174 case 4: return 8;
178 static dwarf_vma
179 get_encoded_value (unsigned char **pdata,
180 int encoding,
181 struct dwarf_section *section,
182 unsigned char * end)
184 unsigned char * data = * pdata;
185 unsigned int size = size_of_encoded_value (encoding);
186 dwarf_vma val;
188 if (data >= end || size > (size_t) (end - data))
190 warn (_("Encoded value extends past end of section\n"));
191 * pdata = end;
192 return 0;
195 /* PR 17512: file: 002-829853-0.004. */
196 if (size > 8)
198 warn (_("Encoded size of %d is too large to read\n"), size);
199 * pdata = end;
200 return 0;
203 /* PR 17512: file: 1085-5603-0.004. */
204 if (size == 0)
206 warn (_("Encoded size of 0 is too small to read\n"));
207 * pdata = end;
208 return 0;
211 if (encoding & DW_EH_PE_signed)
212 val = byte_get_signed (data, size);
213 else
214 val = byte_get (data, size);
216 if ((encoding & 0x70) == DW_EH_PE_pcrel)
217 val += section->address + (data - section->start);
219 * pdata = data + size;
220 return val;
223 #if SIZEOF_LONG_LONG > SIZEOF_LONG
224 # ifndef __MINGW32__
225 # define DWARF_VMA_FMT "ll"
226 # define DWARF_VMA_FMT_LONG "%16.16llx"
227 # else
228 # define DWARF_VMA_FMT "I64"
229 # define DWARF_VMA_FMT_LONG "%016I64x"
230 # endif
231 #else
232 # define DWARF_VMA_FMT "l"
233 # define DWARF_VMA_FMT_LONG "%16.16lx"
234 #endif
236 /* Convert a dwarf vma value into a string. Returns a pointer to a static
237 buffer containing the converted VALUE. The value is converted according
238 to the printf formating character FMTCH. If NUM_BYTES is non-zero then
239 it specifies the maximum number of bytes to be displayed in the converted
240 value and FMTCH is ignored - hex is always used. */
242 static const char *
243 dwarf_vmatoa_1 (const char *fmtch, dwarf_vma value, unsigned num_bytes)
245 /* As dwarf_vmatoa is used more then once in a printf call
246 for output, we are cycling through a fixed array of pointers
247 for return address. */
248 static int buf_pos = 0;
249 static struct dwarf_vmatoa_buf
251 char place[64];
252 } buf[16];
253 char *ret;
255 ret = buf[buf_pos++].place;
256 buf_pos %= ARRAY_SIZE (buf);
258 if (num_bytes)
260 /* Printf does not have a way of specifying a maximum field width for an
261 integer value, so we print the full value into a buffer and then select
262 the precision we need. */
263 snprintf (ret, sizeof (buf[0].place), DWARF_VMA_FMT_LONG, value);
264 if (num_bytes > 8)
265 num_bytes = 8;
266 return ret + (16 - 2 * num_bytes);
268 else
270 char fmt[32];
272 if (fmtch)
273 sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
274 else
275 sprintf (fmt, "%%%s", DWARF_VMA_FMT);
276 snprintf (ret, sizeof (buf[0].place), fmt, value);
277 return ret;
281 static inline const char *
282 dwarf_vmatoa (const char * fmtch, dwarf_vma value)
284 return dwarf_vmatoa_1 (fmtch, value, 0);
287 /* Print a dwarf_vma value (typically an address, offset or length) in
288 hexadecimal format, followed by a space. The length of the VALUE (and
289 hence the precision displayed) is determined by the NUM_BYTES parameter. */
291 static void
292 print_dwarf_vma (dwarf_vma value, unsigned num_bytes)
294 printf ("%s ", dwarf_vmatoa_1 (NULL, value, num_bytes));
297 /* Print a view number in hexadecimal value, with the same width
298 print_dwarf_vma would have printed it with the same num_bytes.
299 Print blanks for zero view, unless force is nonzero. */
301 static void
302 print_dwarf_view (dwarf_vma value, unsigned num_bytes, int force)
304 int len;
305 if (!num_bytes)
306 len = 4;
307 else
308 len = num_bytes * 2;
310 assert (value == (unsigned long) value);
311 if (value || force)
312 printf ("v%0*lx ", len - 1, (unsigned long) value);
313 else
314 printf ("%*s", len + 1, "");
317 /* Read in a LEB128 encoded value starting at address DATA.
318 If SIGN is true, return a signed LEB128 value.
319 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
320 If STATUS_RETURN is not NULL, return with bit 0 (LSB) set if the
321 terminating byte was not found and with bit 1 set if the value
322 overflows a dwarf_vma.
323 No bytes will be read at address END or beyond. */
325 dwarf_vma
326 read_leb128 (unsigned char *data,
327 const unsigned char *const end,
328 bool sign,
329 unsigned int *length_return,
330 int *status_return)
332 dwarf_vma result = 0;
333 unsigned int num_read = 0;
334 unsigned int shift = 0;
335 int status = 1;
337 while (data < end)
339 unsigned char byte = *data++;
340 unsigned char lost, mask;
342 num_read++;
344 if (shift < CHAR_BIT * sizeof (result))
346 result |= ((dwarf_vma) (byte & 0x7f)) << shift;
347 /* These bits overflowed. */
348 lost = byte ^ (result >> shift);
349 /* And this is the mask of possible overflow bits. */
350 mask = 0x7f ^ ((dwarf_vma) 0x7f << shift >> shift);
351 shift += 7;
353 else
355 lost = byte;
356 mask = 0x7f;
358 if ((lost & mask) != (sign && (dwarf_signed_vma) result < 0 ? mask : 0))
359 status |= 2;
361 if ((byte & 0x80) == 0)
363 status &= ~1;
364 if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
365 result |= -((dwarf_vma) 1 << shift);
366 break;
370 if (length_return != NULL)
371 *length_return = num_read;
372 if (status_return != NULL)
373 *status_return = status;
375 return result;
378 /* Read AMOUNT bytes from PTR and store them in VAL.
379 Checks to make sure that the read will not reach or pass END.
380 FUNC chooses whether the value read is unsigned or signed, and may
381 be either byte_get or byte_get_signed. If INC is true, PTR is
382 incremented after reading the value.
383 This macro cannot protect against PTR values derived from user input.
384 The C standard sections 6.5.6 and 6.5.8 say attempts to do so using
385 pointers is undefined behaviour. */
386 #define SAFE_BYTE_GET_INTERNAL(VAL, PTR, AMOUNT, END, FUNC, INC) \
387 do \
389 size_t amount = (AMOUNT); \
390 if (sizeof (VAL) < amount) \
392 error (ngettext ("internal error: attempt to read %d byte " \
393 "of data in to %d sized variable", \
394 "internal error: attempt to read %d bytes " \
395 "of data in to %d sized variable", \
396 amount), \
397 (int) amount, (int) sizeof (VAL)); \
398 amount = sizeof (VAL); \
400 if (ENABLE_CHECKING) \
401 assert ((PTR) <= (END)); \
402 size_t avail = (END) - (PTR); \
403 if ((PTR) > (END)) \
404 avail = 0; \
405 if (amount > avail) \
406 amount = avail; \
407 if (amount == 0) \
408 (VAL) = 0; \
409 else \
410 (VAL) = (FUNC) ((PTR), amount); \
411 if (INC) \
412 (PTR) += amount; \
414 while (0)
416 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END) \
417 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, false)
419 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
420 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, true)
422 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END) \
423 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, false)
425 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
426 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, true)
428 typedef struct State_Machine_Registers
430 dwarf_vma address;
431 unsigned int view;
432 unsigned int file;
433 unsigned int line;
434 unsigned int column;
435 int is_stmt;
436 int basic_block;
437 unsigned char op_index;
438 unsigned char end_sequence;
439 /* This variable hold the number of the last entry seen
440 in the File Table. */
441 unsigned int last_file_entry;
442 } SMR;
444 static SMR state_machine_regs;
446 static void
447 reset_state_machine (int is_stmt)
449 state_machine_regs.address = 0;
450 state_machine_regs.view = 0;
451 state_machine_regs.op_index = 0;
452 state_machine_regs.file = 1;
453 state_machine_regs.line = 1;
454 state_machine_regs.column = 0;
455 state_machine_regs.is_stmt = is_stmt;
456 state_machine_regs.basic_block = 0;
457 state_machine_regs.end_sequence = 0;
458 state_machine_regs.last_file_entry = 0;
461 /* Handled an extend line op.
462 Returns the number of bytes read. */
464 static size_t
465 process_extended_line_op (unsigned char * data,
466 int is_stmt,
467 unsigned char * end)
469 unsigned char op_code;
470 size_t len, header_len;
471 unsigned char *name;
472 unsigned char *orig_data = data;
473 dwarf_vma adr, val;
475 READ_ULEB (len, data, end);
476 header_len = data - orig_data;
478 if (len == 0 || data >= end || len > (size_t) (end - data))
480 warn (_("Badly formed extended line op encountered!\n"));
481 return header_len;
484 op_code = *data++;
486 printf (_(" Extended opcode %d: "), op_code);
488 switch (op_code)
490 case DW_LNE_end_sequence:
491 printf (_("End of Sequence\n\n"));
492 reset_state_machine (is_stmt);
493 break;
495 case DW_LNE_set_address:
496 /* PR 17512: file: 002-100480-0.004. */
497 if (len - 1 > 8)
499 warn (_("Length (%lu) of DW_LNE_set_address op is too long\n"),
500 (unsigned long) len - 1);
501 adr = 0;
503 else
504 SAFE_BYTE_GET (adr, data, len - 1, end);
505 printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
506 state_machine_regs.address = adr;
507 state_machine_regs.view = 0;
508 state_machine_regs.op_index = 0;
509 break;
511 case DW_LNE_define_file:
512 printf (_("define new File Table entry\n"));
513 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
514 printf (" %d\t", ++state_machine_regs.last_file_entry);
517 size_t l;
519 name = data;
520 l = strnlen ((char *) data, end - data);
521 data += l;
522 if (data < end)
523 data++;
524 READ_ULEB (val, data, end);
525 printf ("%s\t", dwarf_vmatoa ("u", val));
526 READ_ULEB (val, data, end);
527 printf ("%s\t", dwarf_vmatoa ("u", val));
528 READ_ULEB (val, data, end);
529 printf ("%s\t", dwarf_vmatoa ("u", val));
530 printf ("%.*s\n\n", (int) l, name);
533 if (((size_t) (data - orig_data) != len + header_len) || data >= end)
534 warn (_("DW_LNE_define_file: Bad opcode length\n"));
535 break;
537 case DW_LNE_set_discriminator:
538 READ_ULEB (val, data, end);
539 printf (_("set Discriminator to %s\n"), dwarf_vmatoa ("u", val));
540 break;
542 /* HP extensions. */
543 case DW_LNE_HP_negate_is_UV_update:
544 printf ("DW_LNE_HP_negate_is_UV_update\n");
545 break;
546 case DW_LNE_HP_push_context:
547 printf ("DW_LNE_HP_push_context\n");
548 break;
549 case DW_LNE_HP_pop_context:
550 printf ("DW_LNE_HP_pop_context\n");
551 break;
552 case DW_LNE_HP_set_file_line_column:
553 printf ("DW_LNE_HP_set_file_line_column\n");
554 break;
555 case DW_LNE_HP_set_routine_name:
556 printf ("DW_LNE_HP_set_routine_name\n");
557 break;
558 case DW_LNE_HP_set_sequence:
559 printf ("DW_LNE_HP_set_sequence\n");
560 break;
561 case DW_LNE_HP_negate_post_semantics:
562 printf ("DW_LNE_HP_negate_post_semantics\n");
563 break;
564 case DW_LNE_HP_negate_function_exit:
565 printf ("DW_LNE_HP_negate_function_exit\n");
566 break;
567 case DW_LNE_HP_negate_front_end_logical:
568 printf ("DW_LNE_HP_negate_front_end_logical\n");
569 break;
570 case DW_LNE_HP_define_proc:
571 printf ("DW_LNE_HP_define_proc\n");
572 break;
573 case DW_LNE_HP_source_file_correlation:
575 unsigned char *edata = data + len - 1;
577 printf ("DW_LNE_HP_source_file_correlation\n");
579 while (data < edata)
581 unsigned int opc;
583 READ_ULEB (opc, data, edata);
585 switch (opc)
587 case DW_LNE_HP_SFC_formfeed:
588 printf (" DW_LNE_HP_SFC_formfeed\n");
589 break;
590 case DW_LNE_HP_SFC_set_listing_line:
591 READ_ULEB (val, data, edata);
592 printf (" DW_LNE_HP_SFC_set_listing_line (%s)\n",
593 dwarf_vmatoa ("u", val));
594 break;
595 case DW_LNE_HP_SFC_associate:
596 printf (" DW_LNE_HP_SFC_associate ");
597 READ_ULEB (val, data, edata);
598 printf ("(%s", dwarf_vmatoa ("u", val));
599 READ_ULEB (val, data, edata);
600 printf (",%s", dwarf_vmatoa ("u", val));
601 READ_ULEB (val, data, edata);
602 printf (",%s)\n", dwarf_vmatoa ("u", val));
603 break;
604 default:
605 printf (_(" UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
606 data = edata;
607 break;
611 break;
613 default:
615 unsigned int rlen = len - 1;
617 if (op_code >= DW_LNE_lo_user
618 /* The test against DW_LNW_hi_user is redundant due to
619 the limited range of the unsigned char data type used
620 for op_code. */
621 /*&& op_code <= DW_LNE_hi_user*/)
622 printf (_("user defined: "));
623 else
624 printf (_("UNKNOWN: "));
625 printf (_("length %d ["), rlen);
626 for (; rlen; rlen--)
627 printf (" %02x", *data++);
628 printf ("]\n");
630 break;
633 return len + header_len;
636 static const unsigned char *
637 fetch_indirect_string (dwarf_vma offset)
639 struct dwarf_section *section = &debug_displays [str].section;
640 const unsigned char * ret;
642 if (section->start == NULL)
643 return (const unsigned char *) _("<no .debug_str section>");
645 if (offset >= section->size)
647 warn (_("DW_FORM_strp offset too big: 0x%s\n"),
648 dwarf_vmatoa ("x", offset));
649 return (const unsigned char *) _("<offset is too big>");
652 ret = section->start + offset;
653 /* Unfortunately we cannot rely upon the .debug_str section ending with a
654 NUL byte. Since our caller is expecting to receive a well formed C
655 string we test for the lack of a terminating byte here. */
656 if (strnlen ((const char *) ret, section->size - offset)
657 == section->size - offset)
658 ret = (const unsigned char *)
659 _("<no NUL byte at end of .debug_str section>");
661 return ret;
664 static const unsigned char *
665 fetch_indirect_line_string (dwarf_vma offset)
667 struct dwarf_section *section = &debug_displays [line_str].section;
668 const unsigned char * ret;
670 if (section->start == NULL)
671 return (const unsigned char *) _("<no .debug_line_str section>");
673 if (offset >= section->size)
675 warn (_("DW_FORM_line_strp offset too big: 0x%s\n"),
676 dwarf_vmatoa ("x", offset));
677 return (const unsigned char *) _("<offset is too big>");
680 ret = section->start + offset;
681 /* Unfortunately we cannot rely upon the .debug_line_str section ending
682 with a NUL byte. Since our caller is expecting to receive a well formed
683 C string we test for the lack of a terminating byte here. */
684 if (strnlen ((const char *) ret, section->size - offset)
685 == section->size - offset)
686 ret = (const unsigned char *)
687 _("<no NUL byte at end of .debug_line_str section>");
689 return ret;
692 static const char *
693 fetch_indexed_string (dwarf_vma idx, struct cu_tu_set *this_set,
694 dwarf_vma offset_size, bool dwo)
696 enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
697 enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
698 struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
699 struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
700 dwarf_vma index_offset;
701 dwarf_vma str_offset;
702 const char * ret;
703 unsigned char *curr = index_section->start;
704 unsigned char *end = curr + index_section->size;
705 dwarf_vma length;
707 if (index_section->start == NULL)
708 return (dwo ? _("<no .debug_str_offsets.dwo section>")
709 : _("<no .debug_str_offsets section>"));
711 if (str_section->start == NULL)
712 return (dwo ? _("<no .debug_str.dwo section>")
713 : _("<no .debug_str section>"));
715 /* FIXME: We should cache the length... */
716 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
717 if (length == 0xffffffff)
719 if (offset_size != 8)
720 warn (_("Expected offset size of 8 but given %s"), dwarf_vmatoa ("x", offset_size));
721 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
723 else if (offset_size != 4)
725 warn (_("Expected offset size of 4 but given %s"), dwarf_vmatoa ("x", offset_size));
728 if (length == 0)
730 /* This is probably an old style .debug_str_offset section which
731 just contains offsets and no header (and the first offset is 0). */
732 curr = index_section->start;
733 length = index_section->size;
735 else
737 /* Skip the version and padding bytes.
738 We assume that they are correct. */
739 if (end - curr >= 4)
740 curr += 4;
741 else
742 curr = end;
743 if (length >= 4)
744 length -= 4;
745 else
746 length = 0;
748 if (this_set != NULL
749 && this_set->section_sizes[DW_SECT_STR_OFFSETS] < length)
750 length = this_set->section_sizes[DW_SECT_STR_OFFSETS];
752 if (length > (dwarf_vma) (end - curr))
754 warn (_("index table size too large for section %s vs %s\n"),
755 dwarf_vmatoa ("x", length),
756 dwarf_vmatoa ("x", index_section->size));
757 length = end - curr;
760 if (length < offset_size)
762 warn (_("index table size %s is too small\n"),
763 dwarf_vmatoa ("x", length));
764 return _("<table too small>");
768 index_offset = idx * offset_size;
770 if (this_set != NULL)
771 index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
773 if (index_offset >= length
774 || length - index_offset < offset_size)
776 warn (_("DW_FORM_GNU_str_index offset too big: 0x%s vs 0x%s\n"),
777 dwarf_vmatoa ("x", index_offset),
778 dwarf_vmatoa ("x", length));
779 return _("<index offset is too big>");
782 str_offset = byte_get (curr + index_offset, offset_size);
783 str_offset -= str_section->address;
784 if (str_offset >= str_section->size)
786 warn (_("DW_FORM_GNU_str_index indirect offset too big: 0x%s\n"),
787 dwarf_vmatoa ("x", str_offset));
788 return _("<indirect index offset is too big>");
791 ret = (const char *) str_section->start + str_offset;
792 /* Unfortunately we cannot rely upon str_section ending with a NUL byte.
793 Since our caller is expecting to receive a well formed C string we test
794 for the lack of a terminating byte here. */
795 if (strnlen (ret, str_section->size - str_offset)
796 == str_section->size - str_offset)
797 ret = (const char *) _("<no NUL byte at end of section>");
799 return ret;
802 static dwarf_vma
803 fetch_indexed_addr (dwarf_vma offset, uint32_t num_bytes)
805 struct dwarf_section *section = &debug_displays [debug_addr].section;
807 if (section->start == NULL)
809 warn (_("<no .debug_addr section>"));
810 return 0;
813 if (offset + num_bytes > section->size)
815 warn (_("Offset into section %s too big: 0x%s\n"),
816 section->name, dwarf_vmatoa ("x", offset));
817 return 0;
820 return byte_get (section->start + offset, num_bytes);
823 /* Fetch a value from a debug section that has been indexed by
824 something in another section (eg DW_FORM_loclistx).
825 Returns 0 if the value could not be found. */
827 static dwarf_vma
828 fetch_indexed_value (dwarf_vma idx,
829 enum dwarf_section_display_enum sec_enum)
831 struct dwarf_section *section = &debug_displays [sec_enum].section;
833 if (section->start == NULL)
835 warn (_("Unable to locate %s section\n"), section->uncompressed_name);
836 return 0;
839 uint32_t pointer_size, bias;
841 if (byte_get (section->start, 4) == 0xffffffff)
843 pointer_size = 8;
844 bias = 20;
846 else
848 pointer_size = 4;
849 bias = 12;
852 dwarf_vma offset = idx * pointer_size;
854 /* Offsets are biased by the size of the section header. */
855 offset += bias;
857 if (offset + pointer_size > section->size)
859 warn (_("Offset into section %s too big: 0x%s\n"),
860 section->name, dwarf_vmatoa ("x", offset));
861 return 0;
864 return byte_get (section->start + offset, pointer_size);
867 /* FIXME: There are better and more efficient ways to handle
868 these structures. For now though, I just want something that
869 is simple to implement. */
870 /* Records a single attribute in an abbrev. */
871 typedef struct abbrev_attr
873 unsigned long attribute;
874 unsigned long form;
875 dwarf_signed_vma implicit_const;
876 struct abbrev_attr * next;
878 abbrev_attr;
880 /* Records a single abbrev. */
881 typedef struct abbrev_entry
883 unsigned long number;
884 unsigned long tag;
885 int children;
886 struct abbrev_attr * first_attr;
887 struct abbrev_attr * last_attr;
888 struct abbrev_entry * next;
890 abbrev_entry;
892 /* Records a set of abbreviations. */
893 typedef struct abbrev_list
895 abbrev_entry * first_abbrev;
896 abbrev_entry * last_abbrev;
897 dwarf_vma abbrev_base;
898 dwarf_vma abbrev_offset;
899 struct abbrev_list * next;
900 unsigned char * start_of_next_abbrevs;
902 abbrev_list;
904 /* Records all the abbrevs found so far. */
905 static struct abbrev_list * abbrev_lists = NULL;
907 typedef struct abbrev_map
909 dwarf_vma start;
910 dwarf_vma end;
911 abbrev_list * list;
912 } abbrev_map;
914 /* Maps between CU offsets and abbrev sets. */
915 static abbrev_map * cu_abbrev_map = NULL;
916 static unsigned long num_abbrev_map_entries = 0;
917 static unsigned long next_free_abbrev_map_entry = 0;
919 #define INITIAL_NUM_ABBREV_MAP_ENTRIES 8
920 #define ABBREV_MAP_ENTRIES_INCREMENT 8
922 static void
923 record_abbrev_list_for_cu (dwarf_vma start, dwarf_vma end, abbrev_list * list)
925 if (cu_abbrev_map == NULL)
927 num_abbrev_map_entries = INITIAL_NUM_ABBREV_MAP_ENTRIES;
928 cu_abbrev_map = xmalloc (num_abbrev_map_entries * sizeof (* cu_abbrev_map));
930 else if (next_free_abbrev_map_entry == num_abbrev_map_entries)
932 num_abbrev_map_entries += ABBREV_MAP_ENTRIES_INCREMENT;
933 cu_abbrev_map = xrealloc (cu_abbrev_map, num_abbrev_map_entries * sizeof (* cu_abbrev_map));
936 cu_abbrev_map[next_free_abbrev_map_entry].start = start;
937 cu_abbrev_map[next_free_abbrev_map_entry].end = end;
938 cu_abbrev_map[next_free_abbrev_map_entry].list = list;
939 next_free_abbrev_map_entry ++;
942 static void
943 free_all_abbrevs (void)
945 abbrev_list * list;
947 for (list = abbrev_lists; list != NULL;)
949 abbrev_list * next = list->next;
950 abbrev_entry * abbrv;
952 for (abbrv = list->first_abbrev; abbrv != NULL;)
954 abbrev_entry * next_abbrev = abbrv->next;
955 abbrev_attr * attr;
957 for (attr = abbrv->first_attr; attr;)
959 abbrev_attr *next_attr = attr->next;
961 free (attr);
962 attr = next_attr;
965 free (abbrv);
966 abbrv = next_abbrev;
969 free (list);
970 list = next;
973 abbrev_lists = NULL;
976 static abbrev_list *
977 new_abbrev_list (dwarf_vma abbrev_base, dwarf_vma abbrev_offset)
979 abbrev_list * list = (abbrev_list *) xcalloc (sizeof * list, 1);
981 list->abbrev_base = abbrev_base;
982 list->abbrev_offset = abbrev_offset;
984 list->next = abbrev_lists;
985 abbrev_lists = list;
987 return list;
990 static abbrev_list *
991 find_abbrev_list_by_abbrev_offset (dwarf_vma abbrev_base,
992 dwarf_vma abbrev_offset)
994 abbrev_list * list;
996 for (list = abbrev_lists; list != NULL; list = list->next)
997 if (list->abbrev_base == abbrev_base
998 && list->abbrev_offset == abbrev_offset)
999 return list;
1001 return NULL;
1004 /* Find the abbreviation map for the CU that includes OFFSET.
1005 OFFSET is an absolute offset from the start of the .debug_info section. */
1006 /* FIXME: This function is going to slow down readelf & objdump.
1007 Consider using a better algorithm to mitigate this effect. */
1009 static abbrev_map *
1010 find_abbrev_map_by_offset (dwarf_vma offset)
1012 unsigned long i;
1014 for (i = 0; i < next_free_abbrev_map_entry; i++)
1015 if (cu_abbrev_map[i].start <= offset
1016 && cu_abbrev_map[i].end > offset)
1017 return cu_abbrev_map + i;
1019 return NULL;
1022 static void
1023 add_abbrev (unsigned long number,
1024 unsigned long tag,
1025 int children,
1026 abbrev_list * list)
1028 abbrev_entry * entry;
1030 entry = (abbrev_entry *) xmalloc (sizeof (*entry));
1032 entry->number = number;
1033 entry->tag = tag;
1034 entry->children = children;
1035 entry->first_attr = NULL;
1036 entry->last_attr = NULL;
1037 entry->next = NULL;
1039 assert (list != NULL);
1041 if (list->first_abbrev == NULL)
1042 list->first_abbrev = entry;
1043 else
1044 list->last_abbrev->next = entry;
1046 list->last_abbrev = entry;
1049 static void
1050 add_abbrev_attr (unsigned long attribute,
1051 unsigned long form,
1052 dwarf_signed_vma implicit_const,
1053 abbrev_list * list)
1055 abbrev_attr *attr;
1057 attr = (abbrev_attr *) xmalloc (sizeof (*attr));
1059 attr->attribute = attribute;
1060 attr->form = form;
1061 attr->implicit_const = implicit_const;
1062 attr->next = NULL;
1064 assert (list != NULL && list->last_abbrev != NULL);
1066 if (list->last_abbrev->first_attr == NULL)
1067 list->last_abbrev->first_attr = attr;
1068 else
1069 list->last_abbrev->last_attr->next = attr;
1071 list->last_abbrev->last_attr = attr;
1074 /* Processes the (partial) contents of a .debug_abbrev section.
1075 Returns NULL if the end of the section was encountered.
1076 Returns the address after the last byte read if the end of
1077 an abbreviation set was found. */
1079 static unsigned char *
1080 process_abbrev_set (struct dwarf_section *section,
1081 dwarf_vma abbrev_base,
1082 dwarf_vma abbrev_size,
1083 dwarf_vma abbrev_offset,
1084 abbrev_list *list)
1086 if (abbrev_base >= section->size
1087 || abbrev_size > section->size - abbrev_base)
1089 /* PR 17531: file:4bcd9ce9. */
1090 warn (_("Debug info is corrupted, abbrev size (%lx) is larger than "
1091 "abbrev section size (%lx)\n"),
1092 (unsigned long) (abbrev_base + abbrev_size),
1093 (unsigned long) section->size);
1094 return NULL;
1096 if (abbrev_offset >= abbrev_size)
1098 warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than "
1099 "abbrev section size (%lx)\n"),
1100 (unsigned long) abbrev_offset,
1101 (unsigned long) abbrev_size);
1102 return NULL;
1105 unsigned char *start = section->start + abbrev_base;
1106 unsigned char *end = start + abbrev_size;
1107 start += abbrev_offset;
1108 while (start < end)
1110 unsigned long entry;
1111 unsigned long tag;
1112 unsigned long attribute;
1113 int children;
1115 READ_ULEB (entry, start, end);
1117 /* A single zero is supposed to end the set according
1118 to the standard. If there's more, then signal that to
1119 the caller. */
1120 if (start == end)
1121 return NULL;
1122 if (entry == 0)
1123 return start;
1125 READ_ULEB (tag, start, end);
1126 if (start == end)
1127 return NULL;
1129 children = *start++;
1131 add_abbrev (entry, tag, children, list);
1135 unsigned long form;
1136 /* Initialize it due to a false compiler warning. */
1137 dwarf_signed_vma implicit_const = -1;
1139 READ_ULEB (attribute, start, end);
1140 if (start == end)
1141 break;
1143 READ_ULEB (form, start, end);
1144 if (start == end)
1145 break;
1147 if (form == DW_FORM_implicit_const)
1149 READ_SLEB (implicit_const, start, end);
1150 if (start == end)
1151 break;
1154 add_abbrev_attr (attribute, form, implicit_const, list);
1156 while (attribute != 0);
1159 /* Report the missing single zero which ends the section. */
1160 error (_(".debug_abbrev section not zero terminated\n"));
1162 return NULL;
1165 static const char *
1166 get_TAG_name (unsigned long tag)
1168 const char *name = get_DW_TAG_name ((unsigned int) tag);
1170 if (name == NULL)
1172 static char buffer[100];
1174 if (tag >= DW_TAG_lo_user && tag <= DW_TAG_hi_user)
1175 snprintf (buffer, sizeof (buffer), _("User TAG value: %#lx"), tag);
1176 else
1177 snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %#lx"), tag);
1178 return buffer;
1181 return name;
1184 static const char *
1185 get_FORM_name (unsigned long form)
1187 const char *name;
1189 if (form == 0)
1190 return "DW_FORM value: 0";
1192 name = get_DW_FORM_name (form);
1193 if (name == NULL)
1195 static char buffer[100];
1197 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
1198 return buffer;
1201 return name;
1204 static const char *
1205 get_IDX_name (unsigned long idx)
1207 const char *name = get_DW_IDX_name ((unsigned int) idx);
1209 if (name == NULL)
1211 static char buffer[100];
1213 snprintf (buffer, sizeof (buffer), _("Unknown IDX value: %lx"), idx);
1214 return buffer;
1217 return name;
1220 static unsigned char *
1221 display_block (unsigned char *data,
1222 dwarf_vma length,
1223 const unsigned char * const end, char delimiter)
1225 dwarf_vma maxlen;
1227 printf (_("%c%s byte block: "), delimiter, dwarf_vmatoa ("u", length));
1228 if (data > end)
1229 return (unsigned char *) end;
1231 maxlen = (dwarf_vma) (end - data);
1232 length = length > maxlen ? maxlen : length;
1234 while (length --)
1235 printf ("%lx ", (unsigned long) byte_get (data++, 1));
1237 return data;
1240 static int
1241 decode_location_expression (unsigned char * data,
1242 unsigned int pointer_size,
1243 unsigned int offset_size,
1244 int dwarf_version,
1245 dwarf_vma length,
1246 dwarf_vma cu_offset,
1247 struct dwarf_section * section)
1249 unsigned op;
1250 dwarf_vma uvalue;
1251 dwarf_signed_vma svalue;
1252 unsigned char *end = data + length;
1253 int need_frame_base = 0;
1255 while (data < end)
1257 op = *data++;
1259 switch (op)
1261 case DW_OP_addr:
1262 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1263 printf ("DW_OP_addr: %s", dwarf_vmatoa ("x", uvalue));
1264 break;
1265 case DW_OP_deref:
1266 printf ("DW_OP_deref");
1267 break;
1268 case DW_OP_const1u:
1269 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1270 printf ("DW_OP_const1u: %lu", (unsigned long) uvalue);
1271 break;
1272 case DW_OP_const1s:
1273 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
1274 printf ("DW_OP_const1s: %ld", (long) svalue);
1275 break;
1276 case DW_OP_const2u:
1277 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1278 printf ("DW_OP_const2u: %lu", (unsigned long) uvalue);
1279 break;
1280 case DW_OP_const2s:
1281 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1282 printf ("DW_OP_const2s: %ld", (long) svalue);
1283 break;
1284 case DW_OP_const4u:
1285 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1286 printf ("DW_OP_const4u: %lu", (unsigned long) uvalue);
1287 break;
1288 case DW_OP_const4s:
1289 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1290 printf ("DW_OP_const4s: %ld", (long) svalue);
1291 break;
1292 case DW_OP_const8u:
1293 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1294 printf ("DW_OP_const8u: %lu ", (unsigned long) uvalue);
1295 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1296 printf ("%lu", (unsigned long) uvalue);
1297 break;
1298 case DW_OP_const8s:
1299 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1300 printf ("DW_OP_const8s: %ld ", (long) svalue);
1301 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1302 printf ("%ld", (long) svalue);
1303 break;
1304 case DW_OP_constu:
1305 READ_ULEB (uvalue, data, end);
1306 printf ("DW_OP_constu: %s", dwarf_vmatoa ("u", uvalue));
1307 break;
1308 case DW_OP_consts:
1309 READ_SLEB (svalue, data, end);
1310 printf ("DW_OP_consts: %s", dwarf_vmatoa ("d", svalue));
1311 break;
1312 case DW_OP_dup:
1313 printf ("DW_OP_dup");
1314 break;
1315 case DW_OP_drop:
1316 printf ("DW_OP_drop");
1317 break;
1318 case DW_OP_over:
1319 printf ("DW_OP_over");
1320 break;
1321 case DW_OP_pick:
1322 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1323 printf ("DW_OP_pick: %ld", (unsigned long) uvalue);
1324 break;
1325 case DW_OP_swap:
1326 printf ("DW_OP_swap");
1327 break;
1328 case DW_OP_rot:
1329 printf ("DW_OP_rot");
1330 break;
1331 case DW_OP_xderef:
1332 printf ("DW_OP_xderef");
1333 break;
1334 case DW_OP_abs:
1335 printf ("DW_OP_abs");
1336 break;
1337 case DW_OP_and:
1338 printf ("DW_OP_and");
1339 break;
1340 case DW_OP_div:
1341 printf ("DW_OP_div");
1342 break;
1343 case DW_OP_minus:
1344 printf ("DW_OP_minus");
1345 break;
1346 case DW_OP_mod:
1347 printf ("DW_OP_mod");
1348 break;
1349 case DW_OP_mul:
1350 printf ("DW_OP_mul");
1351 break;
1352 case DW_OP_neg:
1353 printf ("DW_OP_neg");
1354 break;
1355 case DW_OP_not:
1356 printf ("DW_OP_not");
1357 break;
1358 case DW_OP_or:
1359 printf ("DW_OP_or");
1360 break;
1361 case DW_OP_plus:
1362 printf ("DW_OP_plus");
1363 break;
1364 case DW_OP_plus_uconst:
1365 READ_ULEB (uvalue, data, end);
1366 printf ("DW_OP_plus_uconst: %s", dwarf_vmatoa ("u", uvalue));
1367 break;
1368 case DW_OP_shl:
1369 printf ("DW_OP_shl");
1370 break;
1371 case DW_OP_shr:
1372 printf ("DW_OP_shr");
1373 break;
1374 case DW_OP_shra:
1375 printf ("DW_OP_shra");
1376 break;
1377 case DW_OP_xor:
1378 printf ("DW_OP_xor");
1379 break;
1380 case DW_OP_bra:
1381 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1382 printf ("DW_OP_bra: %ld", (long) svalue);
1383 break;
1384 case DW_OP_eq:
1385 printf ("DW_OP_eq");
1386 break;
1387 case DW_OP_ge:
1388 printf ("DW_OP_ge");
1389 break;
1390 case DW_OP_gt:
1391 printf ("DW_OP_gt");
1392 break;
1393 case DW_OP_le:
1394 printf ("DW_OP_le");
1395 break;
1396 case DW_OP_lt:
1397 printf ("DW_OP_lt");
1398 break;
1399 case DW_OP_ne:
1400 printf ("DW_OP_ne");
1401 break;
1402 case DW_OP_skip:
1403 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1404 printf ("DW_OP_skip: %ld", (long) svalue);
1405 break;
1407 case DW_OP_lit0:
1408 case DW_OP_lit1:
1409 case DW_OP_lit2:
1410 case DW_OP_lit3:
1411 case DW_OP_lit4:
1412 case DW_OP_lit5:
1413 case DW_OP_lit6:
1414 case DW_OP_lit7:
1415 case DW_OP_lit8:
1416 case DW_OP_lit9:
1417 case DW_OP_lit10:
1418 case DW_OP_lit11:
1419 case DW_OP_lit12:
1420 case DW_OP_lit13:
1421 case DW_OP_lit14:
1422 case DW_OP_lit15:
1423 case DW_OP_lit16:
1424 case DW_OP_lit17:
1425 case DW_OP_lit18:
1426 case DW_OP_lit19:
1427 case DW_OP_lit20:
1428 case DW_OP_lit21:
1429 case DW_OP_lit22:
1430 case DW_OP_lit23:
1431 case DW_OP_lit24:
1432 case DW_OP_lit25:
1433 case DW_OP_lit26:
1434 case DW_OP_lit27:
1435 case DW_OP_lit28:
1436 case DW_OP_lit29:
1437 case DW_OP_lit30:
1438 case DW_OP_lit31:
1439 printf ("DW_OP_lit%d", op - DW_OP_lit0);
1440 break;
1442 case DW_OP_reg0:
1443 case DW_OP_reg1:
1444 case DW_OP_reg2:
1445 case DW_OP_reg3:
1446 case DW_OP_reg4:
1447 case DW_OP_reg5:
1448 case DW_OP_reg6:
1449 case DW_OP_reg7:
1450 case DW_OP_reg8:
1451 case DW_OP_reg9:
1452 case DW_OP_reg10:
1453 case DW_OP_reg11:
1454 case DW_OP_reg12:
1455 case DW_OP_reg13:
1456 case DW_OP_reg14:
1457 case DW_OP_reg15:
1458 case DW_OP_reg16:
1459 case DW_OP_reg17:
1460 case DW_OP_reg18:
1461 case DW_OP_reg19:
1462 case DW_OP_reg20:
1463 case DW_OP_reg21:
1464 case DW_OP_reg22:
1465 case DW_OP_reg23:
1466 case DW_OP_reg24:
1467 case DW_OP_reg25:
1468 case DW_OP_reg26:
1469 case DW_OP_reg27:
1470 case DW_OP_reg28:
1471 case DW_OP_reg29:
1472 case DW_OP_reg30:
1473 case DW_OP_reg31:
1474 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1475 regname (op - DW_OP_reg0, 1));
1476 break;
1478 case DW_OP_breg0:
1479 case DW_OP_breg1:
1480 case DW_OP_breg2:
1481 case DW_OP_breg3:
1482 case DW_OP_breg4:
1483 case DW_OP_breg5:
1484 case DW_OP_breg6:
1485 case DW_OP_breg7:
1486 case DW_OP_breg8:
1487 case DW_OP_breg9:
1488 case DW_OP_breg10:
1489 case DW_OP_breg11:
1490 case DW_OP_breg12:
1491 case DW_OP_breg13:
1492 case DW_OP_breg14:
1493 case DW_OP_breg15:
1494 case DW_OP_breg16:
1495 case DW_OP_breg17:
1496 case DW_OP_breg18:
1497 case DW_OP_breg19:
1498 case DW_OP_breg20:
1499 case DW_OP_breg21:
1500 case DW_OP_breg22:
1501 case DW_OP_breg23:
1502 case DW_OP_breg24:
1503 case DW_OP_breg25:
1504 case DW_OP_breg26:
1505 case DW_OP_breg27:
1506 case DW_OP_breg28:
1507 case DW_OP_breg29:
1508 case DW_OP_breg30:
1509 case DW_OP_breg31:
1510 READ_SLEB (svalue, data, end);
1511 printf ("DW_OP_breg%d (%s): %s", op - DW_OP_breg0,
1512 regname (op - DW_OP_breg0, 1), dwarf_vmatoa ("d", svalue));
1513 break;
1515 case DW_OP_regx:
1516 READ_ULEB (uvalue, data, end);
1517 printf ("DW_OP_regx: %s (%s)",
1518 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1519 break;
1520 case DW_OP_fbreg:
1521 need_frame_base = 1;
1522 READ_SLEB (svalue, data, end);
1523 printf ("DW_OP_fbreg: %s", dwarf_vmatoa ("d", svalue));
1524 break;
1525 case DW_OP_bregx:
1526 READ_ULEB (uvalue, data, end);
1527 READ_SLEB (svalue, data, end);
1528 printf ("DW_OP_bregx: %s (%s) %s",
1529 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
1530 dwarf_vmatoa ("d", svalue));
1531 break;
1532 case DW_OP_piece:
1533 READ_ULEB (uvalue, data, end);
1534 printf ("DW_OP_piece: %s", dwarf_vmatoa ("u", uvalue));
1535 break;
1536 case DW_OP_deref_size:
1537 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1538 printf ("DW_OP_deref_size: %ld", (long) uvalue);
1539 break;
1540 case DW_OP_xderef_size:
1541 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1542 printf ("DW_OP_xderef_size: %ld", (long) uvalue);
1543 break;
1544 case DW_OP_nop:
1545 printf ("DW_OP_nop");
1546 break;
1548 /* DWARF 3 extensions. */
1549 case DW_OP_push_object_address:
1550 printf ("DW_OP_push_object_address");
1551 break;
1552 case DW_OP_call2:
1553 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1554 this ought to be an 8-byte wide computation. */
1555 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1556 printf ("DW_OP_call2: <0x%s>",
1557 dwarf_vmatoa ("x", svalue + cu_offset));
1558 break;
1559 case DW_OP_call4:
1560 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1561 this ought to be an 8-byte wide computation. */
1562 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1563 printf ("DW_OP_call4: <0x%s>",
1564 dwarf_vmatoa ("x", svalue + cu_offset));
1565 break;
1566 case DW_OP_call_ref:
1567 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1568 this ought to be an 8-byte wide computation. */
1569 if (dwarf_version == -1)
1571 printf (_("(DW_OP_call_ref in frame info)"));
1572 /* No way to tell where the next op is, so just bail. */
1573 return need_frame_base;
1575 if (dwarf_version == 2)
1577 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1579 else
1581 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1583 printf ("DW_OP_call_ref: <0x%s>", dwarf_vmatoa ("x", uvalue));
1584 break;
1585 case DW_OP_form_tls_address:
1586 printf ("DW_OP_form_tls_address");
1587 break;
1588 case DW_OP_call_frame_cfa:
1589 printf ("DW_OP_call_frame_cfa");
1590 break;
1591 case DW_OP_bit_piece:
1592 printf ("DW_OP_bit_piece: ");
1593 READ_ULEB (uvalue, data, end);
1594 printf (_("size: %s "), dwarf_vmatoa ("u", uvalue));
1595 READ_ULEB (uvalue, data, end);
1596 printf (_("offset: %s "), dwarf_vmatoa ("u", uvalue));
1597 break;
1599 /* DWARF 4 extensions. */
1600 case DW_OP_stack_value:
1601 printf ("DW_OP_stack_value");
1602 break;
1604 case DW_OP_implicit_value:
1605 printf ("DW_OP_implicit_value");
1606 READ_ULEB (uvalue, data, end);
1607 data = display_block (data, uvalue, end, ' ');
1608 break;
1610 /* GNU extensions. */
1611 case DW_OP_GNU_push_tls_address:
1612 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1613 break;
1614 case DW_OP_GNU_uninit:
1615 printf ("DW_OP_GNU_uninit");
1616 /* FIXME: Is there data associated with this OP ? */
1617 break;
1618 case DW_OP_GNU_encoded_addr:
1620 int encoding = 0;
1621 dwarf_vma addr;
1623 if (data < end)
1624 encoding = *data++;
1625 addr = get_encoded_value (&data, encoding, section, end);
1627 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1628 print_dwarf_vma (addr, pointer_size);
1630 break;
1631 case DW_OP_implicit_pointer:
1632 case DW_OP_GNU_implicit_pointer:
1633 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1634 this ought to be an 8-byte wide computation. */
1635 if (dwarf_version == -1)
1637 printf (_("(%s in frame info)"),
1638 (op == DW_OP_implicit_pointer
1639 ? "DW_OP_implicit_pointer"
1640 : "DW_OP_GNU_implicit_pointer"));
1641 /* No way to tell where the next op is, so just bail. */
1642 return need_frame_base;
1644 if (dwarf_version == 2)
1646 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1648 else
1650 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1652 READ_SLEB (svalue, data, end);
1653 printf ("%s: <0x%s> %s",
1654 (op == DW_OP_implicit_pointer
1655 ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
1656 dwarf_vmatoa ("x", uvalue),
1657 dwarf_vmatoa ("d", svalue));
1658 break;
1659 case DW_OP_entry_value:
1660 case DW_OP_GNU_entry_value:
1661 READ_ULEB (uvalue, data, end);
1662 /* PR 17531: file: 0cc9cd00. */
1663 if (uvalue > (dwarf_vma) (end - data))
1664 uvalue = end - data;
1665 printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1666 : "DW_OP_GNU_entry_value"));
1667 if (decode_location_expression (data, pointer_size, offset_size,
1668 dwarf_version, uvalue,
1669 cu_offset, section))
1670 need_frame_base = 1;
1671 putchar (')');
1672 data += uvalue;
1673 break;
1674 case DW_OP_const_type:
1675 case DW_OP_GNU_const_type:
1676 READ_ULEB (uvalue, data, end);
1677 printf ("%s: <0x%s> ",
1678 (op == DW_OP_const_type ? "DW_OP_const_type"
1679 : "DW_OP_GNU_const_type"),
1680 dwarf_vmatoa ("x", cu_offset + uvalue));
1681 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1682 data = display_block (data, uvalue, end, ' ');
1683 break;
1684 case DW_OP_regval_type:
1685 case DW_OP_GNU_regval_type:
1686 READ_ULEB (uvalue, data, end);
1687 printf ("%s: %s (%s)",
1688 (op == DW_OP_regval_type ? "DW_OP_regval_type"
1689 : "DW_OP_GNU_regval_type"),
1690 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1691 READ_ULEB (uvalue, data, end);
1692 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1693 break;
1694 case DW_OP_deref_type:
1695 case DW_OP_GNU_deref_type:
1696 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1697 printf ("%s: %ld",
1698 (op == DW_OP_deref_type ? "DW_OP_deref_type"
1699 : "DW_OP_GNU_deref_type"),
1700 (long) uvalue);
1701 READ_ULEB (uvalue, data, end);
1702 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1703 break;
1704 case DW_OP_convert:
1705 case DW_OP_GNU_convert:
1706 READ_ULEB (uvalue, data, end);
1707 printf ("%s <0x%s>",
1708 (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
1709 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1710 break;
1711 case DW_OP_reinterpret:
1712 case DW_OP_GNU_reinterpret:
1713 READ_ULEB (uvalue, data, end);
1714 printf ("%s <0x%s>",
1715 (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1716 : "DW_OP_GNU_reinterpret"),
1717 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1718 break;
1719 case DW_OP_GNU_parameter_ref:
1720 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1721 printf ("DW_OP_GNU_parameter_ref: <0x%s>",
1722 dwarf_vmatoa ("x", cu_offset + uvalue));
1723 break;
1724 case DW_OP_GNU_addr_index:
1725 READ_ULEB (uvalue, data, end);
1726 printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1727 break;
1728 case DW_OP_GNU_const_index:
1729 READ_ULEB (uvalue, data, end);
1730 printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1731 break;
1732 case DW_OP_GNU_variable_value:
1733 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1734 this ought to be an 8-byte wide computation. */
1735 if (dwarf_version == -1)
1737 printf (_("(DW_OP_GNU_variable_value in frame info)"));
1738 /* No way to tell where the next op is, so just bail. */
1739 return need_frame_base;
1741 if (dwarf_version == 2)
1743 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1745 else
1747 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1749 printf ("DW_OP_GNU_variable_value: <0x%s>", dwarf_vmatoa ("x", uvalue));
1750 break;
1752 /* HP extensions. */
1753 case DW_OP_HP_is_value:
1754 printf ("DW_OP_HP_is_value");
1755 /* FIXME: Is there data associated with this OP ? */
1756 break;
1757 case DW_OP_HP_fltconst4:
1758 printf ("DW_OP_HP_fltconst4");
1759 /* FIXME: Is there data associated with this OP ? */
1760 break;
1761 case DW_OP_HP_fltconst8:
1762 printf ("DW_OP_HP_fltconst8");
1763 /* FIXME: Is there data associated with this OP ? */
1764 break;
1765 case DW_OP_HP_mod_range:
1766 printf ("DW_OP_HP_mod_range");
1767 /* FIXME: Is there data associated with this OP ? */
1768 break;
1769 case DW_OP_HP_unmod_range:
1770 printf ("DW_OP_HP_unmod_range");
1771 /* FIXME: Is there data associated with this OP ? */
1772 break;
1773 case DW_OP_HP_tls:
1774 printf ("DW_OP_HP_tls");
1775 /* FIXME: Is there data associated with this OP ? */
1776 break;
1778 /* PGI (STMicroelectronics) extensions. */
1779 case DW_OP_PGI_omp_thread_num:
1780 /* Pushes the thread number for the current thread as it would be
1781 returned by the standard OpenMP library function:
1782 omp_get_thread_num(). The "current thread" is the thread for
1783 which the expression is being evaluated. */
1784 printf ("DW_OP_PGI_omp_thread_num");
1785 break;
1787 default:
1788 if (op >= DW_OP_lo_user
1789 && op <= DW_OP_hi_user)
1790 printf (_("(User defined location op 0x%x)"), op);
1791 else
1792 printf (_("(Unknown location op 0x%x)"), op);
1793 /* No way to tell where the next op is, so just bail. */
1794 return need_frame_base;
1797 /* Separate the ops. */
1798 if (data < end)
1799 printf ("; ");
1802 return need_frame_base;
1805 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1806 This is used for DWARF package files. */
1808 static struct cu_tu_set *
1809 find_cu_tu_set_v2 (dwarf_vma cu_offset, int do_types)
1811 struct cu_tu_set *p;
1812 unsigned int nsets;
1813 unsigned int dw_sect;
1815 if (do_types)
1817 p = tu_sets;
1818 nsets = tu_count;
1819 dw_sect = DW_SECT_TYPES;
1821 else
1823 p = cu_sets;
1824 nsets = cu_count;
1825 dw_sect = DW_SECT_INFO;
1827 while (nsets > 0)
1829 if (p->section_offsets [dw_sect] == cu_offset)
1830 return p;
1831 p++;
1832 nsets--;
1834 return NULL;
1837 static const char *
1838 fetch_alt_indirect_string (dwarf_vma offset)
1840 separate_info * i;
1842 if (! do_follow_links)
1843 return "";
1845 if (first_separate_info == NULL)
1846 return _("<no links available>");
1848 for (i = first_separate_info; i != NULL; i = i->next)
1850 struct dwarf_section * section;
1851 const char * ret;
1853 if (! load_debug_section (separate_debug_str, i->handle))
1854 continue;
1856 section = &debug_displays [separate_debug_str].section;
1858 if (section->start == NULL)
1859 continue;
1861 if (offset >= section->size)
1862 continue;
1864 ret = (const char *) (section->start + offset);
1865 /* Unfortunately we cannot rely upon the .debug_str section ending with a
1866 NUL byte. Since our caller is expecting to receive a well formed C
1867 string we test for the lack of a terminating byte here. */
1868 if (strnlen ((const char *) ret, section->size - offset)
1869 == section->size - offset)
1870 return _("<no NUL byte at end of alt .debug_str section>");
1872 return ret;
1875 warn (_("DW_FORM_GNU_strp_alt offset (%s) too big or no string sections available\n"),
1876 dwarf_vmatoa ("x", offset));
1877 return _("<offset is too big>");
1880 static const char *
1881 get_AT_name (unsigned long attribute)
1883 const char *name;
1885 if (attribute == 0)
1886 return "DW_AT value: 0";
1888 /* One value is shared by the MIPS and HP extensions: */
1889 if (attribute == DW_AT_MIPS_fde)
1890 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1892 name = get_DW_AT_name (attribute);
1894 if (name == NULL)
1896 static char buffer[100];
1898 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1899 attribute);
1900 return buffer;
1903 return name;
1906 static void
1907 add_dwo_info (const char * value, dwarf_vma cu_offset, dwo_type type)
1909 dwo_info * dwinfo = xmalloc (sizeof * dwinfo);
1911 dwinfo->type = type;
1912 dwinfo->value = value;
1913 dwinfo->cu_offset = cu_offset;
1914 dwinfo->next = first_dwo_info;
1915 first_dwo_info = dwinfo;
1918 static void
1919 add_dwo_name (const char * name, dwarf_vma cu_offset)
1921 add_dwo_info (name, cu_offset, DWO_NAME);
1924 static void
1925 add_dwo_dir (const char * dir, dwarf_vma cu_offset)
1927 add_dwo_info (dir, cu_offset, DWO_DIR);
1930 static void
1931 add_dwo_id (const char * id, dwarf_vma cu_offset)
1933 add_dwo_info (id, cu_offset, DWO_ID);
1936 static void
1937 free_dwo_info (void)
1939 dwo_info * dwinfo;
1940 dwo_info * next;
1942 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = next)
1944 next = dwinfo->next;
1945 free (dwinfo);
1947 first_dwo_info = NULL;
1950 /* Ensure that START + UVALUE is less than END.
1951 Return an adjusted UVALUE if necessary to ensure this relationship. */
1953 static inline dwarf_vma
1954 check_uvalue (const unsigned char * start,
1955 dwarf_vma uvalue,
1956 const unsigned char * end)
1958 dwarf_vma max_uvalue = end - start;
1960 /* See PR 17512: file: 008-103549-0.001:0.1.
1961 and PR 24829 for examples of where these tests are triggered. */
1962 if (uvalue > max_uvalue)
1964 warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1965 uvalue = max_uvalue;
1968 return uvalue;
1971 static unsigned char *
1972 skip_attr_bytes (unsigned long form,
1973 unsigned char *data,
1974 unsigned char *end,
1975 dwarf_vma pointer_size,
1976 dwarf_vma offset_size,
1977 int dwarf_version,
1978 dwarf_vma *value_return)
1980 dwarf_signed_vma svalue;
1981 dwarf_vma uvalue = 0;
1982 dwarf_vma inc = 0;
1984 * value_return = 0;
1986 switch (form)
1988 case DW_FORM_ref_addr:
1989 if (dwarf_version == 2)
1990 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1991 else if (dwarf_version > 2)
1992 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1993 else
1994 return NULL;
1995 break;
1997 case DW_FORM_addr:
1998 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1999 break;
2001 case DW_FORM_strp:
2002 case DW_FORM_line_strp:
2003 case DW_FORM_sec_offset:
2004 case DW_FORM_GNU_ref_alt:
2005 case DW_FORM_GNU_strp_alt:
2006 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2007 break;
2009 case DW_FORM_flag_present:
2010 uvalue = 1;
2011 break;
2013 case DW_FORM_ref1:
2014 case DW_FORM_flag:
2015 case DW_FORM_data1:
2016 case DW_FORM_strx1:
2017 case DW_FORM_addrx1:
2018 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2019 break;
2021 case DW_FORM_strx3:
2022 case DW_FORM_addrx3:
2023 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2024 break;
2026 case DW_FORM_ref2:
2027 case DW_FORM_data2:
2028 case DW_FORM_strx2:
2029 case DW_FORM_addrx2:
2030 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2031 break;
2033 case DW_FORM_ref4:
2034 case DW_FORM_data4:
2035 case DW_FORM_strx4:
2036 case DW_FORM_addrx4:
2037 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2038 break;
2040 case DW_FORM_sdata:
2041 READ_SLEB (svalue, data, end);
2042 uvalue = svalue;
2043 break;
2045 case DW_FORM_ref_udata:
2046 case DW_FORM_udata:
2047 case DW_FORM_GNU_str_index:
2048 case DW_FORM_strx:
2049 case DW_FORM_GNU_addr_index:
2050 case DW_FORM_addrx:
2051 case DW_FORM_loclistx:
2052 case DW_FORM_rnglistx:
2053 READ_ULEB (uvalue, data, end);
2054 break;
2056 case DW_FORM_ref8:
2057 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2058 break;
2060 case DW_FORM_data8:
2061 case DW_FORM_ref_sig8:
2062 inc = 8;
2063 break;
2065 case DW_FORM_data16:
2066 inc = 16;
2067 break;
2069 case DW_FORM_string:
2070 inc = strnlen ((char *) data, end - data) + 1;
2071 break;
2073 case DW_FORM_block:
2074 case DW_FORM_exprloc:
2075 READ_ULEB (uvalue, data, end);
2076 inc = uvalue;
2077 break;
2079 case DW_FORM_block1:
2080 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2081 inc = uvalue;
2082 break;
2084 case DW_FORM_block2:
2085 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2086 inc = uvalue;
2087 break;
2089 case DW_FORM_block4:
2090 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2091 inc = uvalue;
2092 break;
2094 case DW_FORM_indirect:
2095 READ_ULEB (form, data, end);
2096 if (form == DW_FORM_implicit_const)
2097 SKIP_ULEB (data, end);
2098 return skip_attr_bytes (form, data, end, pointer_size, offset_size,
2099 dwarf_version, value_return);
2101 default:
2102 return NULL;
2105 * value_return = uvalue;
2106 if (inc <= (dwarf_vma) (end - data))
2107 data += inc;
2108 else
2109 data = end;
2110 return data;
2113 /* Given form FORM with value UVALUE, locate and return the abbreviation
2114 associated with it. */
2116 static abbrev_entry *
2117 get_type_abbrev_from_form (unsigned long form,
2118 unsigned long uvalue,
2119 dwarf_vma cu_offset,
2120 unsigned char *cu_end,
2121 const struct dwarf_section *section,
2122 unsigned long *abbrev_num_return,
2123 unsigned char **data_return,
2124 abbrev_map **map_return)
2126 unsigned long abbrev_number;
2127 abbrev_map * map;
2128 abbrev_entry * entry;
2129 unsigned char * data;
2131 if (abbrev_num_return != NULL)
2132 * abbrev_num_return = 0;
2133 if (data_return != NULL)
2134 * data_return = NULL;
2136 switch (form)
2138 case DW_FORM_GNU_ref_alt:
2139 case DW_FORM_ref_sig8:
2140 /* FIXME: We are unable to handle this form at the moment. */
2141 return NULL;
2143 case DW_FORM_ref_addr:
2144 if (uvalue >= section->size)
2146 warn (_("Unable to resolve ref_addr form: uvalue %lx > section size %lx (%s)\n"),
2147 uvalue, (long) section->size, section->name);
2148 return NULL;
2150 break;
2152 case DW_FORM_ref_sup4:
2153 case DW_FORM_ref_sup8:
2154 break;
2156 case DW_FORM_ref1:
2157 case DW_FORM_ref2:
2158 case DW_FORM_ref4:
2159 case DW_FORM_ref8:
2160 case DW_FORM_ref_udata:
2161 if (uvalue + cu_offset > (size_t) (cu_end - section->start))
2163 warn (_("Unable to resolve ref form: uvalue %lx + cu_offset %lx > CU size %lx\n"),
2164 uvalue, (long) cu_offset, (long) (cu_end - section->start));
2165 return NULL;
2167 uvalue += cu_offset;
2168 break;
2170 /* FIXME: Are there other DW_FORMs that can be used by types ? */
2172 default:
2173 warn (_("Unexpected form %lx encountered whilst finding abbreviation for type\n"), form);
2174 return NULL;
2177 data = (unsigned char *) section->start + uvalue;
2178 map = find_abbrev_map_by_offset (uvalue);
2180 if (map == NULL)
2182 warn (_("Unable to find abbreviations for CU offset %#lx\n"), uvalue);
2183 return NULL;
2185 if (map->list == NULL)
2187 warn (_("Empty abbreviation list encountered for CU offset %lx\n"), uvalue);
2188 return NULL;
2191 if (map_return != NULL)
2193 if (form == DW_FORM_ref_addr)
2194 *map_return = map;
2195 else
2196 *map_return = NULL;
2199 READ_ULEB (abbrev_number, data, section->start + section->size);
2201 for (entry = map->list->first_abbrev; entry != NULL; entry = entry->next)
2202 if (entry->number == abbrev_number)
2203 break;
2205 if (abbrev_num_return != NULL)
2206 * abbrev_num_return = abbrev_number;
2208 if (data_return != NULL)
2209 * data_return = data;
2211 if (entry == NULL)
2212 warn (_("Unable to find entry for abbreviation %lu\n"), abbrev_number);
2214 return entry;
2217 /* Return IS_SIGNED set to TRUE if the type using abbreviation ENTRY
2218 can be determined to be a signed type. The data for ENTRY can be
2219 found starting at DATA. */
2221 static void
2222 get_type_signedness (abbrev_entry *entry,
2223 const struct dwarf_section *section,
2224 unsigned char *data,
2225 unsigned char *end,
2226 dwarf_vma cu_offset,
2227 dwarf_vma pointer_size,
2228 dwarf_vma offset_size,
2229 int dwarf_version,
2230 bool *is_signed,
2231 unsigned int nesting)
2233 abbrev_attr * attr;
2235 * is_signed = false;
2237 #define MAX_NESTING 20
2238 if (nesting > MAX_NESTING)
2240 /* FIXME: Warn - or is this expected ?
2241 NB/ We need to avoid infinite recursion. */
2242 return;
2245 for (attr = entry->first_attr;
2246 attr != NULL && attr->attribute;
2247 attr = attr->next)
2249 unsigned char * orig_data = data;
2250 dwarf_vma uvalue = 0;
2252 data = skip_attr_bytes (attr->form, data, end, pointer_size,
2253 offset_size, dwarf_version, & uvalue);
2254 if (data == NULL)
2255 return;
2257 switch (attr->attribute)
2259 case DW_AT_linkage_name:
2260 case DW_AT_name:
2261 if (do_wide)
2263 if (attr->form == DW_FORM_strp)
2264 printf (", %s", fetch_indirect_string (uvalue));
2265 else if (attr->form == DW_FORM_string)
2266 printf (", %.*s", (int) (end - orig_data), orig_data);
2268 break;
2270 case DW_AT_type:
2271 /* Recurse. */
2273 abbrev_entry *type_abbrev;
2274 unsigned char *type_data;
2275 abbrev_map *map;
2277 type_abbrev = get_type_abbrev_from_form (attr->form,
2278 uvalue,
2279 cu_offset,
2280 end,
2281 section,
2282 NULL /* abbrev num return */,
2283 &type_data,
2284 &map);
2285 if (type_abbrev == NULL)
2286 break;
2288 get_type_signedness (type_abbrev, section, type_data,
2289 map ? section->start + map->end : end,
2290 map ? map->start : cu_offset,
2291 pointer_size, offset_size, dwarf_version,
2292 is_signed, nesting + 1);
2294 break;
2296 case DW_AT_encoding:
2297 /* Determine signness. */
2298 switch (uvalue)
2300 case DW_ATE_address:
2301 /* FIXME - some architectures have signed addresses. */
2302 case DW_ATE_boolean:
2303 case DW_ATE_unsigned:
2304 case DW_ATE_unsigned_char:
2305 case DW_ATE_unsigned_fixed:
2306 * is_signed = false;
2307 break;
2309 default:
2310 case DW_ATE_complex_float:
2311 case DW_ATE_float:
2312 case DW_ATE_signed:
2313 case DW_ATE_signed_char:
2314 case DW_ATE_imaginary_float:
2315 case DW_ATE_decimal_float:
2316 case DW_ATE_signed_fixed:
2317 * is_signed = true;
2318 break;
2320 break;
2325 static void
2326 read_and_print_leb128 (unsigned char *data,
2327 unsigned int *bytes_read,
2328 unsigned const char *end,
2329 bool is_signed)
2331 int status;
2332 dwarf_vma val = read_leb128 (data, end, is_signed, bytes_read, &status);
2333 if (status != 0)
2334 report_leb_status (status);
2335 else
2336 printf ("%s", dwarf_vmatoa (is_signed ? "d" : "u", val));
2339 static void
2340 display_discr_list (unsigned long form,
2341 dwarf_vma uvalue,
2342 unsigned char * data,
2343 int level)
2345 unsigned char *end = data;
2347 if (uvalue == 0)
2349 printf ("[default]");
2350 return;
2353 switch (form)
2355 case DW_FORM_block:
2356 case DW_FORM_block1:
2357 case DW_FORM_block2:
2358 case DW_FORM_block4:
2359 /* Move data pointer back to the start of the byte array. */
2360 data -= uvalue;
2361 break;
2362 default:
2363 printf ("<corrupt>\n");
2364 warn (_("corrupt discr_list - not using a block form\n"));
2365 return;
2368 if (uvalue < 2)
2370 printf ("<corrupt>\n");
2371 warn (_("corrupt discr_list - block not long enough\n"));
2372 return;
2375 bool is_signed = (level > 0 && level <= MAX_CU_NESTING
2376 ? level_type_signed [level - 1] : false);
2378 printf ("(");
2379 while (data < end)
2381 unsigned char discriminant;
2382 unsigned int bytes_read;
2384 SAFE_BYTE_GET_AND_INC (discriminant, data, 1, end);
2386 switch (discriminant)
2388 case DW_DSC_label:
2389 printf ("label ");
2390 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2391 data += bytes_read;
2392 break;
2394 case DW_DSC_range:
2395 printf ("range ");
2396 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2397 data += bytes_read;
2399 printf ("..");
2400 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2401 data += bytes_read;
2402 break;
2404 default:
2405 printf ("<corrupt>\n");
2406 warn (_("corrupt discr_list - unrecognized discriminant byte %#x\n"),
2407 discriminant);
2408 return;
2411 if (data < end)
2412 printf (", ");
2415 if (is_signed)
2416 printf (")(signed)");
2417 else
2418 printf (")(unsigned)");
2421 static unsigned char *
2422 read_and_display_attr_value (unsigned long attribute,
2423 unsigned long form,
2424 dwarf_signed_vma implicit_const,
2425 unsigned char * start,
2426 unsigned char * data,
2427 unsigned char * end,
2428 dwarf_vma cu_offset,
2429 dwarf_vma pointer_size,
2430 dwarf_vma offset_size,
2431 int dwarf_version,
2432 debug_info * debug_info_p,
2433 int do_loc,
2434 struct dwarf_section * section,
2435 struct cu_tu_set * this_set,
2436 char delimiter,
2437 int level)
2439 dwarf_signed_vma svalue;
2440 dwarf_vma uvalue = 0;
2441 dwarf_vma uvalue_hi = 0;
2442 unsigned char *block_start = NULL;
2443 unsigned char *orig_data = data;
2445 if (data > end || (data == end && form != DW_FORM_flag_present))
2447 warn (_("Corrupt attribute\n"));
2448 return data;
2451 if (do_wide && ! do_loc)
2453 /* PR 26847: Display the name of the form. */
2454 const char * name = get_FORM_name (form);
2456 /* For convenience we skip the DW_FORM_ prefix to the name. */
2457 if (name[0] == 'D')
2458 name += 8; /* strlen ("DW_FORM_") */
2459 printf ("%c(%s)", delimiter, name);
2462 switch (form)
2464 case DW_FORM_ref_addr:
2465 if (dwarf_version == 2)
2466 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2467 else if (dwarf_version > 2)
2468 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2469 else
2470 error (_("Internal error: DW_FORM_ref_addr is not supported in DWARF version 1.\n"));
2471 break;
2473 case DW_FORM_addr:
2474 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2475 break;
2477 case DW_FORM_strp_sup:
2478 case DW_FORM_strp:
2479 case DW_FORM_line_strp:
2480 case DW_FORM_sec_offset:
2481 case DW_FORM_GNU_ref_alt:
2482 case DW_FORM_GNU_strp_alt:
2483 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2484 break;
2486 case DW_FORM_flag_present:
2487 uvalue = 1;
2488 break;
2490 case DW_FORM_ref1:
2491 case DW_FORM_flag:
2492 case DW_FORM_data1:
2493 case DW_FORM_strx1:
2494 case DW_FORM_addrx1:
2495 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2496 break;
2498 case DW_FORM_ref2:
2499 case DW_FORM_data2:
2500 case DW_FORM_strx2:
2501 case DW_FORM_addrx2:
2502 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2503 break;
2505 case DW_FORM_strx3:
2506 case DW_FORM_addrx3:
2507 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2508 break;
2510 case DW_FORM_ref_sup4:
2511 case DW_FORM_ref4:
2512 case DW_FORM_data4:
2513 case DW_FORM_strx4:
2514 case DW_FORM_addrx4:
2515 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2516 break;
2518 case DW_FORM_ref_sup8:
2519 case DW_FORM_ref8:
2520 case DW_FORM_data8:
2521 case DW_FORM_ref_sig8:
2522 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2523 break;
2525 case DW_FORM_data16:
2526 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2527 SAFE_BYTE_GET_AND_INC (uvalue_hi, data, 8, end);
2528 if (byte_get != byte_get_little_endian)
2530 dwarf_vma utmp = uvalue;
2531 uvalue = uvalue_hi;
2532 uvalue_hi = utmp;
2534 break;
2536 case DW_FORM_sdata:
2537 READ_SLEB (svalue, data, end);
2538 uvalue = svalue;
2539 break;
2541 case DW_FORM_GNU_str_index:
2542 case DW_FORM_strx:
2543 case DW_FORM_ref_udata:
2544 case DW_FORM_udata:
2545 case DW_FORM_GNU_addr_index:
2546 case DW_FORM_addrx:
2547 case DW_FORM_loclistx:
2548 case DW_FORM_rnglistx:
2549 READ_ULEB (uvalue, data, end);
2550 break;
2552 case DW_FORM_indirect:
2553 READ_ULEB (form, data, end);
2554 if (!do_loc)
2555 printf ("%c%s", delimiter, get_FORM_name (form));
2556 if (form == DW_FORM_implicit_const)
2557 READ_SLEB (implicit_const, data, end);
2558 return read_and_display_attr_value (attribute, form, implicit_const,
2559 start, data, end,
2560 cu_offset, pointer_size,
2561 offset_size, dwarf_version,
2562 debug_info_p, do_loc,
2563 section, this_set, delimiter, level);
2565 case DW_FORM_implicit_const:
2566 uvalue = implicit_const;
2567 break;
2569 default:
2570 break;
2573 switch (form)
2575 case DW_FORM_ref_addr:
2576 if (!do_loc)
2577 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
2578 break;
2580 case DW_FORM_GNU_ref_alt:
2581 if (!do_loc)
2583 if (do_wide)
2584 /* We have already printed the form name. */
2585 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
2586 else
2587 printf ("%c<alt 0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
2589 /* FIXME: Follow the reference... */
2590 break;
2592 case DW_FORM_ref1:
2593 case DW_FORM_ref2:
2594 case DW_FORM_ref4:
2595 case DW_FORM_ref_sup4:
2596 case DW_FORM_ref_udata:
2597 if (!do_loc)
2598 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue + cu_offset));
2599 break;
2601 case DW_FORM_data4:
2602 case DW_FORM_addr:
2603 case DW_FORM_sec_offset:
2604 if (!do_loc)
2605 printf ("%c0x%s", delimiter, dwarf_vmatoa ("x", uvalue));
2606 break;
2608 case DW_FORM_flag_present:
2609 case DW_FORM_flag:
2610 case DW_FORM_data1:
2611 case DW_FORM_data2:
2612 case DW_FORM_sdata:
2613 case DW_FORM_udata:
2614 if (!do_loc)
2615 printf ("%c%s", delimiter, dwarf_vmatoa ("d", uvalue));
2616 break;
2618 case DW_FORM_implicit_const:
2619 if (!do_loc)
2620 printf ("%c%s", delimiter, dwarf_vmatoa ("d", implicit_const));
2621 break;
2623 case DW_FORM_ref_sup8:
2624 case DW_FORM_ref8:
2625 case DW_FORM_data8:
2626 if (!do_loc)
2628 dwarf_vma utmp = uvalue;
2629 if (form == DW_FORM_ref8)
2630 utmp += cu_offset;
2631 printf ("%c0x%s", delimiter, dwarf_vmatoa ("x", utmp));
2633 break;
2635 case DW_FORM_data16:
2636 if (!do_loc)
2637 printf (" 0x%s%s",
2638 uvalue_hi == 0 ? "" : dwarf_vmatoa ("x", uvalue_hi),
2639 dwarf_vmatoa_1 ("x", uvalue, uvalue_hi == 0 ? 0 : 8));
2640 break;
2642 case DW_FORM_string:
2643 if (!do_loc)
2644 printf ("%c%.*s", delimiter, (int) (end - data), data);
2645 data += strnlen ((char *) data, end - data);
2646 if (data < end)
2647 data++;
2648 break;
2650 case DW_FORM_block:
2651 case DW_FORM_exprloc:
2652 READ_ULEB (uvalue, data, end);
2653 do_block:
2654 block_start = data;
2655 if (block_start >= end)
2657 warn (_("Block ends prematurely\n"));
2658 uvalue = 0;
2659 block_start = end;
2662 uvalue = check_uvalue (block_start, uvalue, end);
2664 if (do_loc)
2665 data = block_start + uvalue;
2666 else
2667 data = display_block (block_start, uvalue, end, delimiter);
2668 break;
2670 case DW_FORM_block1:
2671 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2672 goto do_block;
2674 case DW_FORM_block2:
2675 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2676 goto do_block;
2678 case DW_FORM_block4:
2679 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2680 goto do_block;
2682 case DW_FORM_strp:
2683 if (!do_loc)
2685 if (do_wide)
2686 /* We have already displayed the form name. */
2687 printf (_("%c(offset: 0x%s): %s"), delimiter,
2688 dwarf_vmatoa ("x", uvalue),
2689 fetch_indirect_string (uvalue));
2690 else
2691 printf (_("%c(indirect string, offset: 0x%s): %s"), delimiter,
2692 dwarf_vmatoa ("x", uvalue),
2693 fetch_indirect_string (uvalue));
2695 break;
2697 case DW_FORM_line_strp:
2698 if (!do_loc)
2700 if (do_wide)
2701 /* We have already displayed the form name. */
2702 printf (_("%c(offset: 0x%s): %s"), delimiter,
2703 dwarf_vmatoa ("x", uvalue),
2704 fetch_indirect_line_string (uvalue));
2705 else
2706 printf (_("%c(indirect line string, offset: 0x%s): %s"), delimiter,
2707 dwarf_vmatoa ("x", uvalue),
2708 fetch_indirect_line_string (uvalue));
2710 break;
2712 case DW_FORM_GNU_str_index:
2713 case DW_FORM_strx:
2714 case DW_FORM_strx1:
2715 case DW_FORM_strx2:
2716 case DW_FORM_strx3:
2717 case DW_FORM_strx4:
2718 if (!do_loc)
2720 const char *suffix = strrchr (section->name, '.');
2721 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2723 if (do_wide)
2724 /* We have already displayed the form name. */
2725 printf (_("%c(offset: 0x%s): %s"), delimiter,
2726 dwarf_vmatoa ("x", uvalue),
2727 fetch_indexed_string (uvalue, this_set, offset_size, dwo));
2728 else
2729 printf (_("%c(indexed string: 0x%s): %s"), delimiter,
2730 dwarf_vmatoa ("x", uvalue),
2731 fetch_indexed_string (uvalue, this_set, offset_size, dwo));
2733 break;
2735 case DW_FORM_GNU_strp_alt:
2736 if (!do_loc)
2738 if (do_wide)
2739 /* We have already displayed the form name. */
2740 printf (_("%c(offset: 0x%s) %s"), delimiter,
2741 dwarf_vmatoa ("x", uvalue),
2742 fetch_alt_indirect_string (uvalue));
2743 else
2744 printf (_("%c(alt indirect string, offset: 0x%s) %s"), delimiter,
2745 dwarf_vmatoa ("x", uvalue),
2746 fetch_alt_indirect_string (uvalue));
2748 break;
2750 case DW_FORM_indirect:
2751 /* Handled above. */
2752 break;
2754 case DW_FORM_ref_sig8:
2755 if (!do_loc)
2756 printf ("%c%s: 0x%s", delimiter, do_wide ? "" : "signature",
2757 dwarf_vmatoa ("x", uvalue));
2758 break;
2760 case DW_FORM_GNU_addr_index:
2761 case DW_FORM_addrx:
2762 case DW_FORM_addrx1:
2763 case DW_FORM_addrx2:
2764 case DW_FORM_addrx3:
2765 case DW_FORM_addrx4:
2766 case DW_FORM_loclistx:
2767 case DW_FORM_rnglistx:
2768 if (!do_loc)
2770 dwarf_vma base;
2771 dwarf_vma offset;
2773 if (debug_info_p == NULL)
2774 base = 0;
2775 else if (debug_info_p->addr_base == DEBUG_INFO_UNAVAILABLE)
2776 base = 0;
2777 else
2778 base = debug_info_p->addr_base;
2780 offset = base + uvalue * pointer_size;
2782 if (do_wide)
2783 /* We have already displayed the form name. */
2784 printf (_("%c(index: 0x%s): %s"), delimiter,
2785 dwarf_vmatoa ("x", uvalue),
2786 dwarf_vmatoa ("x", fetch_indexed_addr (offset, pointer_size)));
2787 else
2788 printf (_("%c(addr_index: 0x%s): %s"), delimiter,
2789 dwarf_vmatoa ("x", uvalue),
2790 dwarf_vmatoa ("x", fetch_indexed_addr (offset, pointer_size)));
2792 break;
2794 case DW_FORM_strp_sup:
2795 if (!do_loc)
2796 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue + cu_offset));
2797 break;
2799 default:
2800 warn (_("Unrecognized form: 0x%lx\n"), form);
2801 /* What to do? Consume a byte maybe? */
2802 ++data;
2803 break;
2806 if ((do_loc || do_debug_loc || do_debug_ranges)
2807 && num_debug_info_entries == 0
2808 && debug_info_p != NULL)
2810 switch (attribute)
2812 case DW_AT_loclists_base:
2813 if (debug_info_p->loclists_base)
2814 warn (_("CU @ 0x%s has multiple loclists_base values"),
2815 dwarf_vmatoa ("x", debug_info_p->cu_offset));
2816 debug_info_p->loclists_base = uvalue;
2817 break;
2819 case DW_AT_frame_base:
2820 have_frame_base = 1;
2821 /* Fall through. */
2822 case DW_AT_location:
2823 case DW_AT_GNU_locviews:
2824 case DW_AT_string_length:
2825 case DW_AT_return_addr:
2826 case DW_AT_data_member_location:
2827 case DW_AT_vtable_elem_location:
2828 case DW_AT_segment:
2829 case DW_AT_static_link:
2830 case DW_AT_use_location:
2831 case DW_AT_call_value:
2832 case DW_AT_GNU_call_site_value:
2833 case DW_AT_call_data_value:
2834 case DW_AT_GNU_call_site_data_value:
2835 case DW_AT_call_target:
2836 case DW_AT_GNU_call_site_target:
2837 case DW_AT_call_target_clobbered:
2838 case DW_AT_GNU_call_site_target_clobbered:
2839 if ((dwarf_version < 4
2840 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2841 || form == DW_FORM_sec_offset
2842 || form == DW_FORM_loclistx)
2844 /* Process location list. */
2845 unsigned int lmax = debug_info_p->max_loc_offsets;
2846 unsigned int num = debug_info_p->num_loc_offsets;
2848 if (lmax == 0 || num >= lmax)
2850 lmax += 1024;
2851 debug_info_p->loc_offsets = (dwarf_vma *)
2852 xcrealloc (debug_info_p->loc_offsets,
2853 lmax, sizeof (*debug_info_p->loc_offsets));
2854 debug_info_p->loc_views = (dwarf_vma *)
2855 xcrealloc (debug_info_p->loc_views,
2856 lmax, sizeof (*debug_info_p->loc_views));
2857 debug_info_p->have_frame_base = (int *)
2858 xcrealloc (debug_info_p->have_frame_base,
2859 lmax, sizeof (*debug_info_p->have_frame_base));
2860 debug_info_p->max_loc_offsets = lmax;
2863 if (form == DW_FORM_loclistx)
2864 uvalue = fetch_indexed_value (uvalue, loclists);
2865 else if (this_set != NULL)
2866 uvalue += this_set->section_offsets [DW_SECT_LOC];
2868 debug_info_p->have_frame_base [num] = have_frame_base;
2869 if (attribute != DW_AT_GNU_locviews)
2871 uvalue += debug_info_p->loclists_base;
2873 /* Corrupt DWARF info can produce more offsets than views.
2874 See PR 23062 for an example. */
2875 if (debug_info_p->num_loc_offsets
2876 > debug_info_p->num_loc_views)
2877 warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
2878 else
2880 debug_info_p->loc_offsets [num] = uvalue;
2881 debug_info_p->num_loc_offsets++;
2884 else
2886 assert (debug_info_p->num_loc_views <= num);
2887 num = debug_info_p->num_loc_views;
2888 if (num > debug_info_p->num_loc_offsets)
2889 warn (_("More DW_AT_GNU_locview attributes than location offset attributes\n"));
2890 else
2892 debug_info_p->loc_views [num] = uvalue;
2893 debug_info_p->num_loc_views++;
2897 break;
2899 case DW_AT_low_pc:
2900 if (need_base_address)
2901 debug_info_p->base_address = uvalue;
2902 break;
2904 case DW_AT_GNU_addr_base:
2905 case DW_AT_addr_base:
2906 debug_info_p->addr_base = uvalue;
2907 break;
2909 case DW_AT_GNU_ranges_base:
2910 debug_info_p->ranges_base = uvalue;
2911 break;
2913 case DW_AT_ranges:
2914 if ((dwarf_version < 4
2915 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2916 || form == DW_FORM_sec_offset
2917 || form == DW_FORM_rnglistx)
2919 /* Process range list. */
2920 unsigned int lmax = debug_info_p->max_range_lists;
2921 unsigned int num = debug_info_p->num_range_lists;
2923 if (lmax == 0 || num >= lmax)
2925 lmax += 1024;
2926 debug_info_p->range_lists = (dwarf_vma *)
2927 xcrealloc (debug_info_p->range_lists,
2928 lmax, sizeof (*debug_info_p->range_lists));
2929 debug_info_p->max_range_lists = lmax;
2932 if (form == DW_FORM_rnglistx)
2933 uvalue = fetch_indexed_value (uvalue, rnglists);
2935 debug_info_p->range_lists [num] = uvalue;
2936 debug_info_p->num_range_lists++;
2938 break;
2940 case DW_AT_GNU_dwo_name:
2941 case DW_AT_dwo_name:
2942 if (need_dwo_info)
2943 switch (form)
2945 case DW_FORM_strp:
2946 add_dwo_name ((const char *) fetch_indirect_string (uvalue), cu_offset);
2947 break;
2948 case DW_FORM_GNU_strp_alt:
2949 add_dwo_name ((const char *) fetch_alt_indirect_string (uvalue), cu_offset);
2950 break;
2951 case DW_FORM_GNU_str_index:
2952 case DW_FORM_strx:
2953 case DW_FORM_strx1:
2954 case DW_FORM_strx2:
2955 case DW_FORM_strx3:
2956 case DW_FORM_strx4:
2957 add_dwo_name (fetch_indexed_string (uvalue, this_set, offset_size, false), cu_offset);
2958 break;
2959 case DW_FORM_string:
2960 add_dwo_name ((const char *) orig_data, cu_offset);
2961 break;
2962 default:
2963 warn (_("Unsupported form (%s) for attribute %s\n"),
2964 get_FORM_name (form), get_AT_name (attribute));
2965 break;
2967 break;
2969 case DW_AT_comp_dir:
2970 /* FIXME: Also extract a build-id in a CU/TU. */
2971 if (need_dwo_info)
2972 switch (form)
2974 case DW_FORM_strp:
2975 add_dwo_dir ((const char *) fetch_indirect_string (uvalue), cu_offset);
2976 break;
2977 case DW_FORM_GNU_strp_alt:
2978 add_dwo_dir (fetch_alt_indirect_string (uvalue), cu_offset);
2979 break;
2980 case DW_FORM_line_strp:
2981 add_dwo_dir ((const char *) fetch_indirect_line_string (uvalue), cu_offset);
2982 break;
2983 case DW_FORM_GNU_str_index:
2984 case DW_FORM_strx:
2985 case DW_FORM_strx1:
2986 case DW_FORM_strx2:
2987 case DW_FORM_strx3:
2988 case DW_FORM_strx4:
2989 add_dwo_dir (fetch_indexed_string (uvalue, this_set, offset_size, false), cu_offset);
2990 break;
2991 case DW_FORM_string:
2992 add_dwo_dir ((const char *) orig_data, cu_offset);
2993 break;
2994 default:
2995 warn (_("Unsupported form (%s) for attribute %s\n"),
2996 get_FORM_name (form), get_AT_name (attribute));
2997 break;
2999 break;
3001 case DW_AT_GNU_dwo_id:
3002 if (need_dwo_info)
3003 switch (form)
3005 case DW_FORM_data8:
3006 /* FIXME: Record the length of the ID as well ? */
3007 add_dwo_id ((const char *) (data - 8), cu_offset);
3008 break;
3009 default:
3010 warn (_("Unsupported form (%s) for attribute %s\n"),
3011 get_FORM_name (form), get_AT_name (attribute));
3012 break;
3014 break;
3016 default:
3017 break;
3021 if (do_loc || attribute == 0)
3022 return data;
3024 /* For some attributes we can display further information. */
3025 switch (attribute)
3027 case DW_AT_type:
3028 if (level >= 0 && level < MAX_CU_NESTING
3029 && uvalue < (size_t) (end - start))
3031 bool is_signed = false;
3032 abbrev_entry *type_abbrev;
3033 unsigned char *type_data;
3034 abbrev_map *map;
3036 type_abbrev = get_type_abbrev_from_form (form, uvalue,
3037 cu_offset, end,
3038 section, NULL,
3039 &type_data, &map);
3040 if (type_abbrev != NULL)
3042 get_type_signedness (type_abbrev, section, type_data,
3043 map ? section->start + map->end : end,
3044 map ? map->start : cu_offset,
3045 pointer_size, offset_size, dwarf_version,
3046 & is_signed, 0);
3048 level_type_signed[level] = is_signed;
3050 break;
3052 case DW_AT_inline:
3053 printf ("\t");
3054 switch (uvalue)
3056 case DW_INL_not_inlined:
3057 printf (_("(not inlined)"));
3058 break;
3059 case DW_INL_inlined:
3060 printf (_("(inlined)"));
3061 break;
3062 case DW_INL_declared_not_inlined:
3063 printf (_("(declared as inline but ignored)"));
3064 break;
3065 case DW_INL_declared_inlined:
3066 printf (_("(declared as inline and inlined)"));
3067 break;
3068 default:
3069 printf (_(" (Unknown inline attribute value: %s)"),
3070 dwarf_vmatoa ("x", uvalue));
3071 break;
3073 break;
3075 case DW_AT_language:
3076 printf ("\t");
3077 switch (uvalue)
3079 /* Ordered by the numeric value of these constants. */
3080 case DW_LANG_C89: printf ("(ANSI C)"); break;
3081 case DW_LANG_C: printf ("(non-ANSI C)"); break;
3082 case DW_LANG_Ada83: printf ("(Ada)"); break;
3083 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
3084 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
3085 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
3086 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
3087 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
3088 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
3089 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
3090 /* DWARF 2.1 values. */
3091 case DW_LANG_Java: printf ("(Java)"); break;
3092 case DW_LANG_C99: printf ("(ANSI C99)"); break;
3093 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
3094 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
3095 /* DWARF 3 values. */
3096 case DW_LANG_PLI: printf ("(PLI)"); break;
3097 case DW_LANG_ObjC: printf ("(Objective C)"); break;
3098 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
3099 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
3100 case DW_LANG_D: printf ("(D)"); break;
3101 /* DWARF 4 values. */
3102 case DW_LANG_Python: printf ("(Python)"); break;
3103 /* DWARF 5 values. */
3104 case DW_LANG_OpenCL: printf ("(OpenCL)"); break;
3105 case DW_LANG_Go: printf ("(Go)"); break;
3106 case DW_LANG_Modula3: printf ("(Modula 3)"); break;
3107 case DW_LANG_Haskell: printf ("(Haskell)"); break;
3108 case DW_LANG_C_plus_plus_03: printf ("(C++03)"); break;
3109 case DW_LANG_C_plus_plus_11: printf ("(C++11)"); break;
3110 case DW_LANG_OCaml: printf ("(OCaml)"); break;
3111 case DW_LANG_Rust: printf ("(Rust)"); break;
3112 case DW_LANG_C11: printf ("(C11)"); break;
3113 case DW_LANG_Swift: printf ("(Swift)"); break;
3114 case DW_LANG_Julia: printf ("(Julia)"); break;
3115 case DW_LANG_Dylan: printf ("(Dylan)"); break;
3116 case DW_LANG_C_plus_plus_14: printf ("(C++14)"); break;
3117 case DW_LANG_Fortran03: printf ("(Fortran 03)"); break;
3118 case DW_LANG_Fortran08: printf ("(Fortran 08)"); break;
3119 case DW_LANG_RenderScript: printf ("(RenderScript)"); break;
3120 /* MIPS extension. */
3121 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
3122 /* UPC extension. */
3123 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
3124 default:
3125 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
3126 printf (_("(implementation defined: %s)"),
3127 dwarf_vmatoa ("x", uvalue));
3128 else
3129 printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
3130 break;
3132 break;
3134 case DW_AT_encoding:
3135 printf ("\t");
3136 switch (uvalue)
3138 case DW_ATE_void: printf ("(void)"); break;
3139 case DW_ATE_address: printf ("(machine address)"); break;
3140 case DW_ATE_boolean: printf ("(boolean)"); break;
3141 case DW_ATE_complex_float: printf ("(complex float)"); break;
3142 case DW_ATE_float: printf ("(float)"); break;
3143 case DW_ATE_signed: printf ("(signed)"); break;
3144 case DW_ATE_signed_char: printf ("(signed char)"); break;
3145 case DW_ATE_unsigned: printf ("(unsigned)"); break;
3146 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
3147 /* DWARF 2.1 values: */
3148 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
3149 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
3150 /* DWARF 3 values: */
3151 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
3152 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
3153 case DW_ATE_edited: printf ("(edited)"); break;
3154 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
3155 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
3156 /* DWARF 4 values: */
3157 case DW_ATE_UTF: printf ("(unicode string)"); break;
3158 /* DWARF 5 values: */
3159 case DW_ATE_UCS: printf ("(UCS)"); break;
3160 case DW_ATE_ASCII: printf ("(ASCII)"); break;
3162 /* HP extensions: */
3163 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
3164 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
3165 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
3166 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
3167 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
3168 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
3169 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
3171 default:
3172 if (uvalue >= DW_ATE_lo_user
3173 && uvalue <= DW_ATE_hi_user)
3174 printf (_("(user defined type)"));
3175 else
3176 printf (_("(unknown type)"));
3177 break;
3179 break;
3181 case DW_AT_accessibility:
3182 printf ("\t");
3183 switch (uvalue)
3185 case DW_ACCESS_public: printf ("(public)"); break;
3186 case DW_ACCESS_protected: printf ("(protected)"); break;
3187 case DW_ACCESS_private: printf ("(private)"); break;
3188 default:
3189 printf (_("(unknown accessibility)"));
3190 break;
3192 break;
3194 case DW_AT_visibility:
3195 printf ("\t");
3196 switch (uvalue)
3198 case DW_VIS_local: printf ("(local)"); break;
3199 case DW_VIS_exported: printf ("(exported)"); break;
3200 case DW_VIS_qualified: printf ("(qualified)"); break;
3201 default: printf (_("(unknown visibility)")); break;
3203 break;
3205 case DW_AT_endianity:
3206 printf ("\t");
3207 switch (uvalue)
3209 case DW_END_default: printf ("(default)"); break;
3210 case DW_END_big: printf ("(big)"); break;
3211 case DW_END_little: printf ("(little)"); break;
3212 default:
3213 if (uvalue >= DW_END_lo_user && uvalue <= DW_END_hi_user)
3214 printf (_("(user specified)"));
3215 else
3216 printf (_("(unknown endianity)"));
3217 break;
3219 break;
3221 case DW_AT_virtuality:
3222 printf ("\t");
3223 switch (uvalue)
3225 case DW_VIRTUALITY_none: printf ("(none)"); break;
3226 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
3227 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
3228 default: printf (_("(unknown virtuality)")); break;
3230 break;
3232 case DW_AT_identifier_case:
3233 printf ("\t");
3234 switch (uvalue)
3236 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
3237 case DW_ID_up_case: printf ("(up_case)"); break;
3238 case DW_ID_down_case: printf ("(down_case)"); break;
3239 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
3240 default: printf (_("(unknown case)")); break;
3242 break;
3244 case DW_AT_calling_convention:
3245 printf ("\t");
3246 switch (uvalue)
3248 case DW_CC_normal: printf ("(normal)"); break;
3249 case DW_CC_program: printf ("(program)"); break;
3250 case DW_CC_nocall: printf ("(nocall)"); break;
3251 case DW_CC_pass_by_reference: printf ("(pass by ref)"); break;
3252 case DW_CC_pass_by_value: printf ("(pass by value)"); break;
3253 case DW_CC_GNU_renesas_sh: printf ("(Rensas SH)"); break;
3254 case DW_CC_GNU_borland_fastcall_i386: printf ("(Borland fastcall i386)"); break;
3255 default:
3256 if (uvalue >= DW_CC_lo_user
3257 && uvalue <= DW_CC_hi_user)
3258 printf (_("(user defined)"));
3259 else
3260 printf (_("(unknown convention)"));
3262 break;
3264 case DW_AT_ordering:
3265 printf ("\t");
3266 switch (uvalue)
3268 case 255:
3269 case -1: printf (_("(undefined)")); break;
3270 case 0: printf ("(row major)"); break;
3271 case 1: printf ("(column major)"); break;
3273 break;
3275 case DW_AT_decimal_sign:
3276 printf ("\t");
3277 switch (uvalue)
3279 case DW_DS_unsigned: printf (_("(unsigned)")); break;
3280 case DW_DS_leading_overpunch: printf (_("(leading overpunch)")); break;
3281 case DW_DS_trailing_overpunch: printf (_("(trailing overpunch)")); break;
3282 case DW_DS_leading_separate: printf (_("(leading separate)")); break;
3283 case DW_DS_trailing_separate: printf (_("(trailing separate)")); break;
3284 default: printf (_("(unrecognised)")); break;
3286 break;
3288 case DW_AT_defaulted:
3289 printf ("\t");
3290 switch (uvalue)
3292 case DW_DEFAULTED_no: printf (_("(no)")); break;
3293 case DW_DEFAULTED_in_class: printf (_("(in class)")); break;
3294 case DW_DEFAULTED_out_of_class: printf (_("(out of class)")); break;
3295 default: printf (_("(unrecognised)")); break;
3297 break;
3299 case DW_AT_discr_list:
3300 printf ("\t");
3301 display_discr_list (form, uvalue, data, level);
3302 break;
3304 case DW_AT_frame_base:
3305 have_frame_base = 1;
3306 /* Fall through. */
3307 case DW_AT_location:
3308 case DW_AT_loclists_base:
3309 case DW_AT_string_length:
3310 case DW_AT_return_addr:
3311 case DW_AT_data_member_location:
3312 case DW_AT_vtable_elem_location:
3313 case DW_AT_segment:
3314 case DW_AT_static_link:
3315 case DW_AT_use_location:
3316 case DW_AT_call_value:
3317 case DW_AT_GNU_call_site_value:
3318 case DW_AT_call_data_value:
3319 case DW_AT_GNU_call_site_data_value:
3320 case DW_AT_call_target:
3321 case DW_AT_GNU_call_site_target:
3322 case DW_AT_call_target_clobbered:
3323 case DW_AT_GNU_call_site_target_clobbered:
3324 if ((dwarf_version < 4
3325 && (form == DW_FORM_data4 || form == DW_FORM_data8))
3326 || form == DW_FORM_sec_offset
3327 || form == DW_FORM_loclistx)
3328 printf (_(" (location list)"));
3329 /* Fall through. */
3330 case DW_AT_allocated:
3331 case DW_AT_associated:
3332 case DW_AT_data_location:
3333 case DW_AT_stride:
3334 case DW_AT_upper_bound:
3335 case DW_AT_lower_bound:
3336 if (block_start)
3338 int need_frame_base;
3340 printf ("\t(");
3341 need_frame_base = decode_location_expression (block_start,
3342 pointer_size,
3343 offset_size,
3344 dwarf_version,
3345 uvalue,
3346 cu_offset, section);
3347 printf (")");
3348 if (need_frame_base && !have_frame_base)
3349 printf (_(" [without DW_AT_frame_base]"));
3351 break;
3353 case DW_AT_data_bit_offset:
3354 case DW_AT_byte_size:
3355 case DW_AT_bit_size:
3356 case DW_AT_string_length_byte_size:
3357 case DW_AT_string_length_bit_size:
3358 case DW_AT_bit_stride:
3359 if (form == DW_FORM_exprloc)
3361 printf ("\t(");
3362 (void) decode_location_expression (block_start, pointer_size,
3363 offset_size, dwarf_version,
3364 uvalue, cu_offset, section);
3365 printf (")");
3367 break;
3369 case DW_AT_import:
3371 unsigned long abbrev_number;
3372 abbrev_entry *entry;
3374 entry = get_type_abbrev_from_form (form, uvalue, cu_offset, end,
3375 section, & abbrev_number, NULL, NULL);
3376 if (entry == NULL)
3378 if (form != DW_FORM_GNU_ref_alt)
3379 warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset 0x%lx is too big.\n"),
3380 dwarf_vmatoa ("x", uvalue),
3381 (unsigned long) (orig_data - section->start));
3383 else
3385 printf (_("\t[Abbrev Number: %ld"), abbrev_number);
3386 printf (" (%s)", get_TAG_name (entry->tag));
3387 printf ("]");
3390 break;
3392 default:
3393 break;
3396 return data;
3399 static unsigned char *
3400 read_and_display_attr (unsigned long attribute,
3401 unsigned long form,
3402 dwarf_signed_vma implicit_const,
3403 unsigned char * start,
3404 unsigned char * data,
3405 unsigned char * end,
3406 dwarf_vma cu_offset,
3407 dwarf_vma pointer_size,
3408 dwarf_vma offset_size,
3409 int dwarf_version,
3410 debug_info * debug_info_p,
3411 int do_loc,
3412 struct dwarf_section * section,
3413 struct cu_tu_set * this_set,
3414 int level)
3416 if (!do_loc)
3417 printf (" %-18s:", get_AT_name (attribute));
3418 data = read_and_display_attr_value (attribute, form, implicit_const,
3419 start, data, end,
3420 cu_offset, pointer_size, offset_size,
3421 dwarf_version, debug_info_p,
3422 do_loc, section, this_set, ' ', level);
3423 if (!do_loc)
3424 printf ("\n");
3425 return data;
3428 /* Like load_debug_section, but if the ordinary call fails, and we are
3429 following debug links, then attempt to load the requested section
3430 from one of the separate debug info files. */
3432 static bool
3433 load_debug_section_with_follow (enum dwarf_section_display_enum sec_enum,
3434 void * handle)
3436 if (load_debug_section (sec_enum, handle))
3438 if (debug_displays[sec_enum].section.filename == NULL)
3440 /* See if we can associate a filename with this section. */
3441 separate_info * i;
3443 for (i = first_separate_info; i != NULL; i = i->next)
3444 if (i->handle == handle)
3446 debug_displays[sec_enum].section.filename = i->filename;
3447 break;
3451 return true;
3454 if (do_follow_links)
3456 separate_info * i;
3458 for (i = first_separate_info; i != NULL; i = i->next)
3460 if (load_debug_section (sec_enum, i->handle))
3462 debug_displays[sec_enum].section.filename = i->filename;
3464 /* FIXME: We should check to see if any of the remaining debug info
3465 files also contain this section, and, umm, do something about it. */
3466 return true;
3471 return false;
3474 static void
3475 introduce (struct dwarf_section * section, bool raw)
3477 if (raw)
3479 if (do_follow_links && section->filename)
3480 printf (_("Raw dump of debug contents of section %s (loaded from %s):\n\n"),
3481 section->name, section->filename);
3482 else
3483 printf (_("Raw dump of debug contents of section %s:\n\n"), section->name);
3485 else
3487 if (do_follow_links && section->filename)
3488 printf (_("Contents of the %s section (loaded from %s):\n\n"),
3489 section->name, section->filename);
3490 else
3491 printf (_("Contents of the %s section:\n\n"), section->name);
3495 /* Process the contents of a .debug_info section.
3496 If do_loc is TRUE then we are scanning for location lists and dwo tags
3497 and we do not want to display anything to the user.
3498 If do_types is TRUE, we are processing a .debug_types section instead of
3499 a .debug_info section.
3500 The information displayed is restricted by the values in DWARF_START_DIE
3501 and DWARF_CUTOFF_LEVEL.
3502 Returns TRUE upon success. Otherwise an error or warning message is
3503 printed and FALSE is returned. */
3505 static bool
3506 process_debug_info (struct dwarf_section * section,
3507 void *file,
3508 enum dwarf_section_display_enum abbrev_sec,
3509 bool do_loc,
3510 bool do_types)
3512 unsigned char *start = section->start;
3513 unsigned char *end = start + section->size;
3514 unsigned char *section_begin;
3515 unsigned int unit;
3516 unsigned int num_units = 0;
3518 /* First scan the section to get the number of comp units.
3519 Length sanity checks are done here. */
3520 for (section_begin = start, num_units = 0; section_begin < end;
3521 num_units ++)
3523 dwarf_vma length;
3525 /* Read the first 4 bytes. For a 32-bit DWARF section, this
3526 will be the length. For a 64-bit DWARF section, it'll be
3527 the escape code 0xffffffff followed by an 8 byte length. */
3528 SAFE_BYTE_GET_AND_INC (length, section_begin, 4, end);
3530 if (length == 0xffffffff)
3531 SAFE_BYTE_GET_AND_INC (length, section_begin, 8, end);
3532 else if (length >= 0xfffffff0 && length < 0xffffffff)
3534 warn (_("Reserved length value (0x%s) found in section %s\n"),
3535 dwarf_vmatoa ("x", length), section->name);
3536 return false;
3539 /* Negative values are illegal, they may even cause infinite
3540 looping. This can happen if we can't accurately apply
3541 relocations to an object file, or if the file is corrupt. */
3542 if (length > (size_t) (end - section_begin))
3544 warn (_("Corrupt unit length (got 0x%s expected at most 0x%s) in section %s\n"),
3545 dwarf_vmatoa ("x", length),
3546 dwarf_vmatoa ("x", end - section_begin),
3547 section->name);
3548 return false;
3550 section_begin += length;
3553 if (num_units == 0)
3555 error (_("No comp units in %s section ?\n"), section->name);
3556 return false;
3559 if ((do_loc || do_debug_loc || do_debug_ranges)
3560 && num_debug_info_entries == 0
3561 && ! do_types)
3564 /* Then allocate an array to hold the information. */
3565 debug_information = (debug_info *) cmalloc (num_units,
3566 sizeof (* debug_information));
3567 if (debug_information == NULL)
3569 error (_("Not enough memory for a debug info array of %u entries\n"),
3570 num_units);
3571 alloc_num_debug_info_entries = num_debug_info_entries = 0;
3572 return false;
3575 /* PR 17531: file: 92ca3797.
3576 We cannot rely upon the debug_information array being initialised
3577 before it is used. A corrupt file could easily contain references
3578 to a unit for which information has not been made available. So
3579 we ensure that the array is zeroed here. */
3580 memset (debug_information, 0, num_units * sizeof (*debug_information));
3582 alloc_num_debug_info_entries = num_units;
3585 if (!do_loc)
3587 load_debug_section_with_follow (str, file);
3588 load_debug_section_with_follow (line_str, file);
3589 load_debug_section_with_follow (str_dwo, file);
3590 load_debug_section_with_follow (str_index, file);
3591 load_debug_section_with_follow (str_index_dwo, file);
3592 load_debug_section_with_follow (debug_addr, file);
3595 load_debug_section_with_follow (abbrev_sec, file);
3596 load_debug_section_with_follow (loclists, file);
3597 load_debug_section_with_follow (rnglists, file);
3599 if (debug_displays [abbrev_sec].section.start == NULL)
3601 warn (_("Unable to locate %s section!\n"),
3602 debug_displays [abbrev_sec].section.uncompressed_name);
3603 return false;
3606 if (!do_loc && dwarf_start_die == 0)
3607 introduce (section, false);
3609 free_all_abbrevs ();
3610 free (cu_abbrev_map);
3611 cu_abbrev_map = NULL;
3612 next_free_abbrev_map_entry = 0;
3614 /* In order to be able to resolve DW_FORM_ref_attr forms we need
3615 to load *all* of the abbrevs for all CUs in this .debug_info
3616 section. This does effectively mean that we (partially) read
3617 every CU header twice. */
3618 for (section_begin = start; start < end;)
3620 DWARF2_Internal_CompUnit compunit;
3621 unsigned char * hdrptr;
3622 dwarf_vma abbrev_base;
3623 size_t abbrev_size;
3624 dwarf_vma cu_offset;
3625 unsigned int offset_size;
3626 struct cu_tu_set * this_set;
3627 abbrev_list * list;
3628 unsigned char *end_cu;
3630 hdrptr = start;
3631 cu_offset = start - section_begin;
3633 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3635 if (compunit.cu_length == 0xffffffff)
3637 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3638 offset_size = 8;
3640 else
3641 offset_size = 4;
3642 end_cu = hdrptr + compunit.cu_length;
3644 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3646 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3648 if (compunit.cu_version < 5)
3650 compunit.cu_unit_type = DW_UT_compile;
3651 /* Initialize it due to a false compiler warning. */
3652 compunit.cu_pointer_size = -1;
3654 else
3656 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3657 do_types = (compunit.cu_unit_type == DW_UT_type);
3659 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3662 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size,
3663 end_cu);
3665 if (compunit.cu_unit_type == DW_UT_split_compile
3666 || compunit.cu_unit_type == DW_UT_skeleton)
3668 uint64_t dwo_id;
3669 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3672 if (this_set == NULL)
3674 abbrev_base = 0;
3675 abbrev_size = debug_displays [abbrev_sec].section.size;
3677 else
3679 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3680 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3683 list = find_abbrev_list_by_abbrev_offset (abbrev_base,
3684 compunit.cu_abbrev_offset);
3685 if (list == NULL)
3687 unsigned char * next;
3689 list = new_abbrev_list (abbrev_base,
3690 compunit.cu_abbrev_offset);
3691 next = process_abbrev_set (&debug_displays[abbrev_sec].section,
3692 abbrev_base, abbrev_size,
3693 compunit.cu_abbrev_offset, list);
3694 list->start_of_next_abbrevs = next;
3697 start = end_cu;
3698 record_abbrev_list_for_cu (cu_offset, start - section_begin, list);
3701 for (start = section_begin, unit = 0; start < end; unit++)
3703 DWARF2_Internal_CompUnit compunit;
3704 unsigned char *hdrptr;
3705 unsigned char *tags;
3706 int level, last_level, saved_level;
3707 dwarf_vma cu_offset;
3708 unsigned int offset_size;
3709 dwarf_vma signature = 0;
3710 dwarf_vma type_offset = 0;
3711 struct cu_tu_set *this_set;
3712 dwarf_vma abbrev_base;
3713 size_t abbrev_size;
3714 abbrev_list * list = NULL;
3715 unsigned char *end_cu;
3717 hdrptr = start;
3718 cu_offset = start - section_begin;
3720 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3722 if (compunit.cu_length == 0xffffffff)
3724 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3725 offset_size = 8;
3727 else
3728 offset_size = 4;
3729 end_cu = hdrptr + compunit.cu_length;
3731 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3733 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3735 if (compunit.cu_version < 5)
3737 compunit.cu_unit_type = DW_UT_compile;
3738 /* Initialize it due to a false compiler warning. */
3739 compunit.cu_pointer_size = -1;
3741 else
3743 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3744 do_types = (compunit.cu_unit_type == DW_UT_type);
3746 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3749 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, end_cu);
3751 if (this_set == NULL)
3753 abbrev_base = 0;
3754 abbrev_size = debug_displays [abbrev_sec].section.size;
3756 else
3758 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3759 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3762 if (compunit.cu_version < 5)
3763 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3765 bool do_dwo_id = false;
3766 uint64_t dwo_id = 0;
3767 if (compunit.cu_unit_type == DW_UT_split_compile
3768 || compunit.cu_unit_type == DW_UT_skeleton)
3770 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3771 do_dwo_id = true;
3774 /* PR 17512: file: 001-108546-0.001:0.1. */
3775 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3777 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3778 compunit.cu_pointer_size, offset_size);
3779 compunit.cu_pointer_size = offset_size;
3782 if (do_types)
3784 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, end_cu);
3785 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end_cu);
3788 if (dwarf_start_die >= (size_t) (end_cu - section_begin))
3790 start = end_cu;
3791 continue;
3794 if ((do_loc || do_debug_loc || do_debug_ranges)
3795 && num_debug_info_entries == 0
3796 && alloc_num_debug_info_entries > unit
3797 && ! do_types)
3799 debug_information [unit].cu_offset = cu_offset;
3800 debug_information [unit].pointer_size
3801 = compunit.cu_pointer_size;
3802 debug_information [unit].offset_size = offset_size;
3803 debug_information [unit].dwarf_version = compunit.cu_version;
3804 debug_information [unit].base_address = 0;
3805 debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
3806 debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
3807 debug_information [unit].loc_offsets = NULL;
3808 debug_information [unit].have_frame_base = NULL;
3809 debug_information [unit].max_loc_offsets = 0;
3810 debug_information [unit].num_loc_offsets = 0;
3811 debug_information [unit].loclists_base = 0;
3812 debug_information [unit].range_lists = NULL;
3813 debug_information [unit].max_range_lists= 0;
3814 debug_information [unit].num_range_lists = 0;
3817 if (!do_loc && dwarf_start_die == 0)
3819 printf (_(" Compilation Unit @ offset 0x%s:\n"),
3820 dwarf_vmatoa ("x", cu_offset));
3821 printf (_(" Length: 0x%s (%s)\n"),
3822 dwarf_vmatoa ("x", compunit.cu_length),
3823 offset_size == 8 ? "64-bit" : "32-bit");
3824 printf (_(" Version: %d\n"), compunit.cu_version);
3825 if (compunit.cu_version >= 5)
3827 const char *name = get_DW_UT_name (compunit.cu_unit_type);
3829 printf (_(" Unit Type: %s (%x)\n"),
3830 name ? name : "???",
3831 compunit.cu_unit_type);
3833 printf (_(" Abbrev Offset: 0x%s\n"),
3834 dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
3835 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
3836 if (do_types)
3838 printf (_(" Signature: 0x%s\n"),
3839 dwarf_vmatoa ("x", signature));
3840 printf (_(" Type Offset: 0x%s\n"),
3841 dwarf_vmatoa ("x", type_offset));
3843 if (do_dwo_id)
3844 printf (_(" DWO ID: 0x%s\n"), dwarf_vmatoa ("x", dwo_id));
3845 if (this_set != NULL)
3847 dwarf_vma *offsets = this_set->section_offsets;
3848 size_t *sizes = this_set->section_sizes;
3850 printf (_(" Section contributions:\n"));
3851 printf (_(" .debug_abbrev.dwo: 0x%s 0x%s\n"),
3852 dwarf_vmatoa ("x", offsets [DW_SECT_ABBREV]),
3853 dwarf_vmatoa ("x", sizes [DW_SECT_ABBREV]));
3854 printf (_(" .debug_line.dwo: 0x%s 0x%s\n"),
3855 dwarf_vmatoa ("x", offsets [DW_SECT_LINE]),
3856 dwarf_vmatoa ("x", sizes [DW_SECT_LINE]));
3857 printf (_(" .debug_loc.dwo: 0x%s 0x%s\n"),
3858 dwarf_vmatoa ("x", offsets [DW_SECT_LOC]),
3859 dwarf_vmatoa ("x", sizes [DW_SECT_LOC]));
3860 printf (_(" .debug_str_offsets.dwo: 0x%s 0x%s\n"),
3861 dwarf_vmatoa ("x", offsets [DW_SECT_STR_OFFSETS]),
3862 dwarf_vmatoa ("x", sizes [DW_SECT_STR_OFFSETS]));
3866 tags = hdrptr;
3867 start = end_cu;
3869 if (compunit.cu_version < 2 || compunit.cu_version > 5)
3871 warn (_("CU at offset %s contains corrupt or "
3872 "unsupported version number: %d.\n"),
3873 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
3874 continue;
3877 if (compunit.cu_unit_type != DW_UT_compile
3878 && compunit.cu_unit_type != DW_UT_partial
3879 && compunit.cu_unit_type != DW_UT_type
3880 && compunit.cu_unit_type != DW_UT_split_compile
3881 && compunit.cu_unit_type != DW_UT_skeleton)
3883 warn (_("CU at offset %s contains corrupt or "
3884 "unsupported unit type: %d.\n"),
3885 dwarf_vmatoa ("x", cu_offset), compunit.cu_unit_type);
3886 continue;
3889 /* Process the abbrevs used by this compilation unit. */
3890 list = find_abbrev_list_by_abbrev_offset (abbrev_base,
3891 compunit.cu_abbrev_offset);
3892 if (list == NULL)
3894 unsigned char *next;
3896 list = new_abbrev_list (abbrev_base,
3897 compunit.cu_abbrev_offset);
3898 next = process_abbrev_set (&debug_displays[abbrev_sec].section,
3899 abbrev_base, abbrev_size,
3900 compunit.cu_abbrev_offset, list);
3901 list->start_of_next_abbrevs = next;
3904 level = 0;
3905 last_level = level;
3906 saved_level = -1;
3907 while (tags < start)
3909 unsigned long abbrev_number;
3910 unsigned long die_offset;
3911 abbrev_entry *entry;
3912 abbrev_attr *attr;
3913 int do_printing = 1;
3915 die_offset = tags - section_begin;
3917 READ_ULEB (abbrev_number, tags, start);
3919 /* A null DIE marks the end of a list of siblings or it may also be
3920 a section padding. */
3921 if (abbrev_number == 0)
3923 /* Check if it can be a section padding for the last CU. */
3924 if (level == 0 && start == end)
3926 unsigned char *chk;
3928 for (chk = tags; chk < start; chk++)
3929 if (*chk != 0)
3930 break;
3931 if (chk == start)
3932 break;
3935 if (!do_loc && die_offset >= dwarf_start_die
3936 && (dwarf_cutoff_level == -1
3937 || level < dwarf_cutoff_level))
3938 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
3939 level, die_offset);
3941 --level;
3942 if (level < 0)
3944 static unsigned num_bogus_warns = 0;
3946 if (num_bogus_warns < 3)
3948 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
3949 die_offset, section->name);
3950 num_bogus_warns ++;
3951 if (num_bogus_warns == 3)
3952 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
3955 if (dwarf_start_die != 0 && level < saved_level)
3956 return true;
3957 continue;
3960 if (!do_loc)
3962 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
3963 do_printing = 0;
3964 else
3966 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
3967 saved_level = level;
3968 do_printing = (dwarf_cutoff_level == -1
3969 || level < dwarf_cutoff_level);
3970 if (do_printing)
3971 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
3972 level, die_offset, abbrev_number);
3973 else if (dwarf_cutoff_level == -1
3974 || last_level < dwarf_cutoff_level)
3975 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
3976 last_level = level;
3980 /* Scan through the abbreviation list until we reach the
3981 correct entry. */
3982 if (list == NULL)
3983 continue;
3985 for (entry = list->first_abbrev; entry != NULL; entry = entry->next)
3986 if (entry->number == abbrev_number)
3987 break;
3989 if (entry == NULL)
3991 if (!do_loc && do_printing)
3993 printf ("\n");
3994 fflush (stdout);
3996 warn (_("DIE at offset 0x%lx refers to abbreviation number %lu which does not exist\n"),
3997 die_offset, abbrev_number);
3998 return false;
4001 if (!do_loc && do_printing)
4002 printf (" (%s)\n", get_TAG_name (entry->tag));
4004 switch (entry->tag)
4006 default:
4007 need_base_address = 0;
4008 break;
4009 case DW_TAG_compile_unit:
4010 need_base_address = 1;
4011 need_dwo_info = do_loc;
4012 break;
4013 case DW_TAG_entry_point:
4014 case DW_TAG_subprogram:
4015 need_base_address = 0;
4016 /* Assuming that there is no DW_AT_frame_base. */
4017 have_frame_base = 0;
4018 break;
4021 debug_info *debug_info_p =
4022 (debug_information && unit < alloc_num_debug_info_entries)
4023 ? debug_information + unit : NULL;
4025 assert (!debug_info_p
4026 || (debug_info_p->num_loc_offsets
4027 == debug_info_p->num_loc_views));
4029 for (attr = entry->first_attr;
4030 attr && attr->attribute;
4031 attr = attr->next)
4033 if (! do_loc && do_printing)
4034 /* Show the offset from where the tag was extracted. */
4035 printf (" <%lx>", (unsigned long)(tags - section_begin));
4036 tags = read_and_display_attr (attr->attribute,
4037 attr->form,
4038 attr->implicit_const,
4039 section_begin,
4040 tags,
4041 start,
4042 cu_offset,
4043 compunit.cu_pointer_size,
4044 offset_size,
4045 compunit.cu_version,
4046 debug_info_p,
4047 do_loc || ! do_printing,
4048 section,
4049 this_set,
4050 level);
4053 /* If a locview attribute appears before a location one,
4054 make sure we don't associate it with an earlier
4055 loclist. */
4056 if (debug_info_p)
4057 switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
4059 case 1:
4060 debug_info_p->loc_views [debug_info_p->num_loc_views] = vm1;
4061 debug_info_p->num_loc_views++;
4062 assert (debug_info_p->num_loc_views
4063 == debug_info_p->num_loc_offsets);
4064 break;
4066 case 0:
4067 break;
4069 case -1:
4070 warn(_("DIE has locviews without loclist\n"));
4071 debug_info_p->num_loc_views--;
4072 break;
4074 default:
4075 assert (0);
4078 if (entry->children)
4079 ++level;
4083 /* Set num_debug_info_entries here so that it can be used to check if
4084 we need to process .debug_loc and .debug_ranges sections. */
4085 if ((do_loc || do_debug_loc || do_debug_ranges)
4086 && num_debug_info_entries == 0
4087 && ! do_types)
4089 if (num_units > alloc_num_debug_info_entries)
4090 num_debug_info_entries = alloc_num_debug_info_entries;
4091 else
4092 num_debug_info_entries = num_units;
4095 if (!do_loc)
4096 printf ("\n");
4098 return true;
4101 /* Locate and scan the .debug_info section in the file and record the pointer
4102 sizes and offsets for the compilation units in it. Usually an executable
4103 will have just one pointer size, but this is not guaranteed, and so we try
4104 not to make any assumptions. Returns zero upon failure, or the number of
4105 compilation units upon success. */
4107 static unsigned int
4108 load_debug_info (void * file)
4110 /* If we have already tried and failed to load the .debug_info
4111 section then do not bother to repeat the task. */
4112 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4113 return 0;
4115 /* If we already have the information there is nothing else to do. */
4116 if (num_debug_info_entries > 0)
4117 return num_debug_info_entries;
4119 /* If this is a DWARF package file, load the CU and TU indexes. */
4120 (void) load_cu_tu_indexes (file);
4122 if (load_debug_section_with_follow (info, file)
4123 && process_debug_info (&debug_displays [info].section, file, abbrev, true, false))
4124 return num_debug_info_entries;
4126 if (load_debug_section_with_follow (info_dwo, file)
4127 && process_debug_info (&debug_displays [info_dwo].section, file,
4128 abbrev_dwo, true, false))
4129 return num_debug_info_entries;
4131 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
4132 return 0;
4135 /* Read a DWARF .debug_line section header starting at DATA.
4136 Upon success returns an updated DATA pointer and the LINFO
4137 structure and the END_OF_SEQUENCE pointer will be filled in.
4138 Otherwise returns NULL. */
4140 static unsigned char *
4141 read_debug_line_header (struct dwarf_section * section,
4142 unsigned char * data,
4143 unsigned char * end,
4144 DWARF2_Internal_LineInfo * linfo,
4145 unsigned char ** end_of_sequence)
4147 unsigned char *hdrptr;
4149 /* Extract information from the Line Number Program Header.
4150 (section 6.2.4 in the Dwarf3 doc). */
4151 hdrptr = data;
4153 /* Get and check the length of the block. */
4154 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
4156 if (linfo->li_length == 0xffffffff)
4158 /* This section is 64-bit DWARF 3. */
4159 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
4160 linfo->li_offset_size = 8;
4162 else
4163 linfo->li_offset_size = 4;
4165 if (linfo->li_length > (size_t) (end - hdrptr))
4167 /* If the length field has a relocation against it, then we should
4168 not complain if it is inaccurate (and probably negative). This
4169 happens in object files when the .debug_line section is actually
4170 comprised of several different .debug_line.* sections, (some of
4171 which may be removed by linker garbage collection), and a relocation
4172 is used to compute the correct length once that is done. */
4173 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
4175 linfo->li_length = end - hdrptr;
4177 else
4179 warn (_("The length field (0x%lx) in the debug_line header is wrong - the section is too small\n"),
4180 (long) linfo->li_length);
4181 return NULL;
4184 end = hdrptr + linfo->li_length;
4186 /* Get and check the version number. */
4187 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
4189 if (linfo->li_version != 2
4190 && linfo->li_version != 3
4191 && linfo->li_version != 4
4192 && linfo->li_version != 5)
4194 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
4195 "is currently supported.\n"));
4196 return NULL;
4199 if (linfo->li_version >= 5)
4201 SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
4203 SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
4204 if (linfo->li_segment_size != 0)
4206 warn (_("The %s section contains "
4207 "unsupported segment selector size: %d.\n"),
4208 section->name, linfo->li_segment_size);
4209 return NULL;
4213 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
4214 linfo->li_offset_size, end);
4215 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
4217 if (linfo->li_version >= 4)
4219 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
4221 if (linfo->li_max_ops_per_insn == 0)
4223 warn (_("Invalid maximum operations per insn.\n"));
4224 return NULL;
4227 else
4228 linfo->li_max_ops_per_insn = 1;
4230 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
4231 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
4232 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
4233 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
4235 *end_of_sequence = end;
4236 return hdrptr;
4239 static unsigned char *
4240 display_formatted_table (unsigned char *data,
4241 unsigned char *start,
4242 unsigned char *end,
4243 const DWARF2_Internal_LineInfo *linfo,
4244 struct dwarf_section *section,
4245 bool is_dir)
4247 unsigned char *format_start, format_count, *format, formati;
4248 dwarf_vma data_count, datai;
4249 unsigned int namepass, last_entry = 0;
4250 const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
4252 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4253 if (do_checks && format_count > 5)
4254 warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
4255 table_name, format_count);
4257 format_start = data;
4258 for (formati = 0; formati < format_count; formati++)
4260 SKIP_ULEB (data, end);
4261 SKIP_ULEB (data, end);
4262 if (data >= end)
4264 warn (_("%s: Corrupt format description entry\n"), table_name);
4265 return data;
4269 READ_ULEB (data_count, data, end);
4270 if (data_count == 0)
4272 printf (_("\n The %s is empty.\n"), table_name);
4273 return data;
4275 else if (data >= end)
4277 warn (_("%s: Corrupt entry count - expected %s but none found\n"),
4278 table_name, dwarf_vmatoa ("x", data_count));
4279 return data;
4282 else if (format_count == 0)
4284 warn (_("%s: format count is zero, but the table is not empty\n"),
4285 table_name);
4286 return end;
4289 printf (_("\n The %s (offset 0x%lx, lines %s, columns %u):\n"),
4290 table_name, (long) (data - start), dwarf_vmatoa ("u", data_count),
4291 format_count);
4293 printf (_(" Entry"));
4294 /* Delay displaying name as the last entry for better screen layout. */
4295 for (namepass = 0; namepass < 2; namepass++)
4297 format = format_start;
4298 for (formati = 0; formati < format_count; formati++)
4300 dwarf_vma content_type;
4302 READ_ULEB (content_type, format, end);
4303 if ((content_type == DW_LNCT_path) == (namepass == 1))
4304 switch (content_type)
4306 case DW_LNCT_path:
4307 printf (_("\tName"));
4308 break;
4309 case DW_LNCT_directory_index:
4310 printf (_("\tDir"));
4311 break;
4312 case DW_LNCT_timestamp:
4313 printf (_("\tTime"));
4314 break;
4315 case DW_LNCT_size:
4316 printf (_("\tSize"));
4317 break;
4318 case DW_LNCT_MD5:
4319 printf (_("\tMD5\t\t\t"));
4320 break;
4321 default:
4322 printf (_("\t(Unknown format content type %s)"),
4323 dwarf_vmatoa ("u", content_type));
4325 SKIP_ULEB (format, end);
4328 putchar ('\n');
4330 for (datai = 0; datai < data_count; datai++)
4332 unsigned char *datapass = data;
4334 printf (" %d", last_entry++);
4335 /* Delay displaying name as the last entry for better screen layout. */
4336 for (namepass = 0; namepass < 2; namepass++)
4338 format = format_start;
4339 data = datapass;
4340 for (formati = 0; formati < format_count; formati++)
4342 dwarf_vma content_type, form;
4344 READ_ULEB (content_type, format, end);
4345 READ_ULEB (form, format, end);
4346 data = read_and_display_attr_value (0, form, 0, start, data, end,
4347 0, 0, linfo->li_offset_size,
4348 linfo->li_version, NULL,
4349 ((content_type == DW_LNCT_path) != (namepass == 1)),
4350 section, NULL, '\t', -1);
4354 if (data >= end && (datai < data_count - 1))
4356 warn (_("\n%s: Corrupt entries list\n"), table_name);
4357 return data;
4359 putchar ('\n');
4361 return data;
4364 static int
4365 display_debug_sup (struct dwarf_section * section,
4366 void * file ATTRIBUTE_UNUSED)
4368 unsigned char * start = section->start;
4369 unsigned char * end = section->start + section->size;
4370 unsigned int version;
4371 char is_supplementary;
4372 const unsigned char * sup_filename;
4373 size_t sup_filename_len;
4374 unsigned int num_read;
4375 int status;
4376 dwarf_vma checksum_len;
4379 introduce (section, true);
4380 if (section->size < 4)
4382 error (_("corrupt .debug_sup section: size is too small\n"));
4383 return 0;
4386 /* Read the data. */
4387 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
4388 if (version < 5)
4389 warn (_("corrupt .debug_sup section: version < 5"));
4391 SAFE_BYTE_GET_AND_INC (is_supplementary, start, 1, end);
4392 if (is_supplementary != 0 && is_supplementary != 1)
4393 warn (_("corrupt .debug_sup section: is_supplementary not 0 or 1\n"));
4395 sup_filename = start;
4396 if (is_supplementary && sup_filename[0] != 0)
4397 warn (_("corrupt .debug_sup section: filename not empty in supplementary section\n"));
4399 sup_filename_len = strnlen ((const char *) start, end - start);
4400 if (sup_filename_len == (size_t) (end - start))
4402 error (_("corrupt .debug_sup section: filename is not NUL terminated\n"));
4403 return 0;
4405 start += sup_filename_len + 1;
4407 checksum_len = read_leb128 (start, end, false /* unsigned */, & num_read, & status);
4408 if (status)
4410 error (_("corrupt .debug_sup section: bad LEB128 field for checksum length\n"));
4411 checksum_len = 0;
4413 start += num_read;
4414 if (checksum_len > (dwarf_vma) (end - start))
4416 error (_("corrupt .debug_sup section: checksum length is longer than the remaining section length\n"));
4417 checksum_len = end - start;
4419 else if (checksum_len < (dwarf_vma) (end - start))
4421 warn (_("corrupt .debug_sup section: there are 0x%lx extra, unused bytes at the end of the section\n"),
4422 (long) ((end - start) - checksum_len));
4425 printf (_(" Version: %u\n"), version);
4426 printf (_(" Is Supp: %u\n"), is_supplementary);
4427 printf (_(" Filename: %s\n"), sup_filename);
4428 printf (_(" Checksum Len: %lu\n"), (long) checksum_len);
4429 if (checksum_len > 0)
4431 printf (_(" Checksum: "));
4432 while (checksum_len--)
4433 printf ("0x%x ", * start++ );
4434 printf ("\n");
4436 return 1;
4439 static int
4440 display_debug_lines_raw (struct dwarf_section * section,
4441 unsigned char * data,
4442 unsigned char * end,
4443 void * file)
4445 unsigned char *start = section->start;
4446 int verbose_view = 0;
4448 introduce (section, true);
4450 while (data < end)
4452 static DWARF2_Internal_LineInfo saved_linfo;
4453 DWARF2_Internal_LineInfo linfo;
4454 unsigned char *standard_opcodes;
4455 unsigned char *end_of_sequence;
4456 int i;
4458 if (startswith (section->name, ".debug_line.")
4459 /* Note: the following does not apply to .debug_line.dwo sections.
4460 These are full debug_line sections. */
4461 && strcmp (section->name, ".debug_line.dwo") != 0)
4463 /* Sections named .debug_line.<foo> are fragments of a .debug_line
4464 section containing just the Line Number Statements. They are
4465 created by the assembler and intended to be used alongside gcc's
4466 -ffunction-sections command line option. When the linker's
4467 garbage collection decides to discard a .text.<foo> section it
4468 can then also discard the line number information in .debug_line.<foo>.
4470 Since the section is a fragment it does not have the details
4471 needed to fill out a LineInfo structure, so instead we use the
4472 details from the last full debug_line section that we processed. */
4473 end_of_sequence = end;
4474 standard_opcodes = NULL;
4475 linfo = saved_linfo;
4476 /* PR 17531: file: 0522b371. */
4477 if (linfo.li_line_range == 0)
4479 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4480 return 0;
4482 reset_state_machine (linfo.li_default_is_stmt);
4484 else
4486 unsigned char * hdrptr;
4488 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4489 & end_of_sequence)) == NULL)
4490 return 0;
4492 printf (_(" Offset: 0x%lx\n"), (long)(data - start));
4493 printf (_(" Length: %ld\n"), (long) linfo.li_length);
4494 printf (_(" DWARF Version: %d\n"), linfo.li_version);
4495 if (linfo.li_version >= 5)
4497 printf (_(" Address size (bytes): %d\n"), linfo.li_address_size);
4498 printf (_(" Segment selector (bytes): %d\n"), linfo.li_segment_size);
4500 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
4501 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
4502 if (linfo.li_version >= 4)
4503 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
4504 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
4505 printf (_(" Line Base: %d\n"), linfo.li_line_base);
4506 printf (_(" Line Range: %d\n"), linfo.li_line_range);
4507 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
4509 /* PR 17512: file: 1665-6428-0.004. */
4510 if (linfo.li_line_range == 0)
4512 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4513 linfo.li_line_range = 1;
4516 reset_state_machine (linfo.li_default_is_stmt);
4518 /* Display the contents of the Opcodes table. */
4519 standard_opcodes = hdrptr;
4521 /* PR 17512: file: 002-417945-0.004. */
4522 if (standard_opcodes + linfo.li_opcode_base >= end)
4524 warn (_("Line Base extends beyond end of section\n"));
4525 return 0;
4528 printf (_("\n Opcodes:\n"));
4530 for (i = 1; i < linfo.li_opcode_base; i++)
4531 printf (ngettext (" Opcode %d has %d arg\n",
4532 " Opcode %d has %d args\n",
4533 standard_opcodes[i - 1]),
4534 i, standard_opcodes[i - 1]);
4536 /* Display the contents of the Directory table. */
4537 data = standard_opcodes + linfo.li_opcode_base - 1;
4539 if (linfo.li_version >= 5)
4541 load_debug_section_with_follow (line_str, file);
4543 data = display_formatted_table (data, start, end, &linfo, section,
4544 true);
4545 data = display_formatted_table (data, start, end, &linfo, section,
4546 false);
4548 else
4550 if (*data == 0)
4551 printf (_("\n The Directory Table is empty.\n"));
4552 else
4554 unsigned int last_dir_entry = 0;
4556 printf (_("\n The Directory Table (offset 0x%lx):\n"),
4557 (long)(data - start));
4559 while (data < end && *data != 0)
4561 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
4563 data += strnlen ((char *) data, end - data);
4564 if (data < end)
4565 data++;
4568 /* PR 17512: file: 002-132094-0.004. */
4569 if (data >= end - 1)
4570 break;
4573 /* Skip the NUL at the end of the table. */
4574 if (data < end)
4575 data++;
4577 /* Display the contents of the File Name table. */
4578 if (data >= end || *data == 0)
4579 printf (_("\n The File Name Table is empty.\n"));
4580 else
4582 printf (_("\n The File Name Table (offset 0x%lx):\n"),
4583 (long)(data - start));
4584 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
4586 while (data < end && *data != 0)
4588 unsigned char *name;
4589 dwarf_vma val;
4591 printf (" %d\t", ++state_machine_regs.last_file_entry);
4592 name = data;
4593 data += strnlen ((char *) data, end - data);
4594 if (data < end)
4595 data++;
4597 READ_ULEB (val, data, end);
4598 printf ("%s\t", dwarf_vmatoa ("u", val));
4599 READ_ULEB (val, data, end);
4600 printf ("%s\t", dwarf_vmatoa ("u", val));
4601 READ_ULEB (val, data, end);
4602 printf ("%s\t", dwarf_vmatoa ("u", val));
4603 printf ("%.*s\n", (int)(end - name), name);
4605 if (data >= end)
4607 warn (_("Corrupt file name table entry\n"));
4608 break;
4613 /* Skip the NUL at the end of the table. */
4614 if (data < end)
4615 data++;
4618 putchar ('\n');
4619 saved_linfo = linfo;
4622 /* Now display the statements. */
4623 if (data >= end_of_sequence)
4624 printf (_(" No Line Number Statements.\n"));
4625 else
4627 printf (_(" Line Number Statements:\n"));
4629 while (data < end_of_sequence)
4631 unsigned char op_code;
4632 dwarf_signed_vma adv;
4633 dwarf_vma uladv;
4635 printf (" [0x%08lx]", (long)(data - start));
4637 op_code = *data++;
4639 if (op_code >= linfo.li_opcode_base)
4641 op_code -= linfo.li_opcode_base;
4642 uladv = (op_code / linfo.li_line_range);
4643 if (linfo.li_max_ops_per_insn == 1)
4645 uladv *= linfo.li_min_insn_length;
4646 state_machine_regs.address += uladv;
4647 if (uladv)
4648 state_machine_regs.view = 0;
4649 printf (_(" Special opcode %d: "
4650 "advance Address by %s to 0x%s%s"),
4651 op_code, dwarf_vmatoa ("u", uladv),
4652 dwarf_vmatoa ("x", state_machine_regs.address),
4653 verbose_view && uladv
4654 ? _(" (reset view)") : "");
4656 else
4658 unsigned addrdelta
4659 = ((state_machine_regs.op_index + uladv)
4660 / linfo.li_max_ops_per_insn)
4661 * linfo.li_min_insn_length;
4663 state_machine_regs.address += addrdelta;
4664 state_machine_regs.op_index
4665 = (state_machine_regs.op_index + uladv)
4666 % linfo.li_max_ops_per_insn;
4667 if (addrdelta)
4668 state_machine_regs.view = 0;
4669 printf (_(" Special opcode %d: "
4670 "advance Address by %s to 0x%s[%d]%s"),
4671 op_code, dwarf_vmatoa ("u", uladv),
4672 dwarf_vmatoa ("x", state_machine_regs.address),
4673 state_machine_regs.op_index,
4674 verbose_view && addrdelta
4675 ? _(" (reset view)") : "");
4677 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4678 state_machine_regs.line += adv;
4679 printf (_(" and Line by %s to %d"),
4680 dwarf_vmatoa ("d", adv), state_machine_regs.line);
4681 if (verbose_view || state_machine_regs.view)
4682 printf (_(" (view %u)\n"), state_machine_regs.view);
4683 else
4684 putchar ('\n');
4685 state_machine_regs.view++;
4687 else
4688 switch (op_code)
4690 case DW_LNS_extended_op:
4691 data += process_extended_line_op (data,
4692 linfo.li_default_is_stmt,
4693 end);
4694 break;
4696 case DW_LNS_copy:
4697 printf (_(" Copy"));
4698 if (verbose_view || state_machine_regs.view)
4699 printf (_(" (view %u)\n"), state_machine_regs.view);
4700 else
4701 putchar ('\n');
4702 state_machine_regs.view++;
4703 break;
4705 case DW_LNS_advance_pc:
4706 READ_ULEB (uladv, data, end);
4707 if (linfo.li_max_ops_per_insn == 1)
4709 uladv *= linfo.li_min_insn_length;
4710 state_machine_regs.address += uladv;
4711 if (uladv)
4712 state_machine_regs.view = 0;
4713 printf (_(" Advance PC by %s to 0x%s%s\n"),
4714 dwarf_vmatoa ("u", uladv),
4715 dwarf_vmatoa ("x", state_machine_regs.address),
4716 verbose_view && uladv
4717 ? _(" (reset view)") : "");
4719 else
4721 unsigned addrdelta
4722 = ((state_machine_regs.op_index + uladv)
4723 / linfo.li_max_ops_per_insn)
4724 * linfo.li_min_insn_length;
4725 state_machine_regs.address
4726 += addrdelta;
4727 state_machine_regs.op_index
4728 = (state_machine_regs.op_index + uladv)
4729 % linfo.li_max_ops_per_insn;
4730 if (addrdelta)
4731 state_machine_regs.view = 0;
4732 printf (_(" Advance PC by %s to 0x%s[%d]%s\n"),
4733 dwarf_vmatoa ("u", uladv),
4734 dwarf_vmatoa ("x", state_machine_regs.address),
4735 state_machine_regs.op_index,
4736 verbose_view && addrdelta
4737 ? _(" (reset view)") : "");
4739 break;
4741 case DW_LNS_advance_line:
4742 READ_SLEB (adv, data, end);
4743 state_machine_regs.line += adv;
4744 printf (_(" Advance Line by %s to %d\n"),
4745 dwarf_vmatoa ("d", adv),
4746 state_machine_regs.line);
4747 break;
4749 case DW_LNS_set_file:
4750 READ_ULEB (uladv, data, end);
4751 printf (_(" Set File Name to entry %s in the File Name Table\n"),
4752 dwarf_vmatoa ("u", uladv));
4753 state_machine_regs.file = uladv;
4754 break;
4756 case DW_LNS_set_column:
4757 READ_ULEB (uladv, data, end);
4758 printf (_(" Set column to %s\n"),
4759 dwarf_vmatoa ("u", uladv));
4760 state_machine_regs.column = uladv;
4761 break;
4763 case DW_LNS_negate_stmt:
4764 adv = state_machine_regs.is_stmt;
4765 adv = ! adv;
4766 printf (_(" Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
4767 state_machine_regs.is_stmt = adv;
4768 break;
4770 case DW_LNS_set_basic_block:
4771 printf (_(" Set basic block\n"));
4772 state_machine_regs.basic_block = 1;
4773 break;
4775 case DW_LNS_const_add_pc:
4776 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4777 if (linfo.li_max_ops_per_insn)
4779 uladv *= linfo.li_min_insn_length;
4780 state_machine_regs.address += uladv;
4781 if (uladv)
4782 state_machine_regs.view = 0;
4783 printf (_(" Advance PC by constant %s to 0x%s%s\n"),
4784 dwarf_vmatoa ("u", uladv),
4785 dwarf_vmatoa ("x", state_machine_regs.address),
4786 verbose_view && uladv
4787 ? _(" (reset view)") : "");
4789 else
4791 unsigned addrdelta
4792 = ((state_machine_regs.op_index + uladv)
4793 / linfo.li_max_ops_per_insn)
4794 * linfo.li_min_insn_length;
4795 state_machine_regs.address
4796 += addrdelta;
4797 state_machine_regs.op_index
4798 = (state_machine_regs.op_index + uladv)
4799 % linfo.li_max_ops_per_insn;
4800 if (addrdelta)
4801 state_machine_regs.view = 0;
4802 printf (_(" Advance PC by constant %s to 0x%s[%d]%s\n"),
4803 dwarf_vmatoa ("u", uladv),
4804 dwarf_vmatoa ("x", state_machine_regs.address),
4805 state_machine_regs.op_index,
4806 verbose_view && addrdelta
4807 ? _(" (reset view)") : "");
4809 break;
4811 case DW_LNS_fixed_advance_pc:
4812 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4813 state_machine_regs.address += uladv;
4814 state_machine_regs.op_index = 0;
4815 printf (_(" Advance PC by fixed size amount %s to 0x%s\n"),
4816 dwarf_vmatoa ("u", uladv),
4817 dwarf_vmatoa ("x", state_machine_regs.address));
4818 /* Do NOT reset view. */
4819 break;
4821 case DW_LNS_set_prologue_end:
4822 printf (_(" Set prologue_end to true\n"));
4823 break;
4825 case DW_LNS_set_epilogue_begin:
4826 printf (_(" Set epilogue_begin to true\n"));
4827 break;
4829 case DW_LNS_set_isa:
4830 READ_ULEB (uladv, data, end);
4831 printf (_(" Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
4832 break;
4834 default:
4835 printf (_(" Unknown opcode %d with operands: "), op_code);
4837 if (standard_opcodes != NULL)
4838 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4840 READ_ULEB (uladv, data, end);
4841 printf ("0x%s%s", dwarf_vmatoa ("x", uladv),
4842 i == 1 ? "" : ", ");
4844 putchar ('\n');
4845 break;
4848 putchar ('\n');
4852 return 1;
4855 typedef struct
4857 unsigned char *name;
4858 unsigned int directory_index;
4859 unsigned int modification_date;
4860 unsigned int length;
4861 } File_Entry;
4863 /* Output a decoded representation of the .debug_line section. */
4865 static int
4866 display_debug_lines_decoded (struct dwarf_section * section,
4867 unsigned char * start,
4868 unsigned char * data,
4869 unsigned char * end,
4870 void * fileptr)
4872 static DWARF2_Internal_LineInfo saved_linfo;
4874 introduce (section, false);
4876 while (data < end)
4878 /* This loop amounts to one iteration per compilation unit. */
4879 DWARF2_Internal_LineInfo linfo;
4880 unsigned char *standard_opcodes;
4881 unsigned char *end_of_sequence;
4882 int i;
4883 File_Entry *file_table = NULL;
4884 unsigned int n_files = 0;
4885 unsigned char **directory_table = NULL;
4886 dwarf_vma n_directories = 0;
4888 if (startswith (section->name, ".debug_line.")
4889 /* Note: the following does not apply to .debug_line.dwo sections.
4890 These are full debug_line sections. */
4891 && strcmp (section->name, ".debug_line.dwo") != 0)
4893 /* See comment in display_debug_lines_raw(). */
4894 end_of_sequence = end;
4895 standard_opcodes = NULL;
4896 linfo = saved_linfo;
4897 /* PR 17531: file: 0522b371. */
4898 if (linfo.li_line_range == 0)
4900 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4901 return 0;
4903 reset_state_machine (linfo.li_default_is_stmt);
4905 else
4907 unsigned char *hdrptr;
4909 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4910 & end_of_sequence)) == NULL)
4911 return 0;
4913 /* PR 17531: file: 0522b371. */
4914 if (linfo.li_line_range == 0)
4916 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4917 linfo.li_line_range = 1;
4919 reset_state_machine (linfo.li_default_is_stmt);
4921 /* Save a pointer to the contents of the Opcodes table. */
4922 standard_opcodes = hdrptr;
4924 /* Traverse the Directory table just to count entries. */
4925 data = standard_opcodes + linfo.li_opcode_base - 1;
4926 /* PR 20440 */
4927 if (data >= end)
4929 warn (_("opcode base of %d extends beyond end of section\n"),
4930 linfo.li_opcode_base);
4931 return 0;
4934 if (linfo.li_version >= 5)
4936 unsigned char *format_start, format_count, *format;
4937 dwarf_vma formati, entryi;
4939 load_debug_section_with_follow (line_str, fileptr);
4941 /* Skip directories format. */
4942 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4943 if (do_checks && format_count > 1)
4944 warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
4945 format_count);
4946 format_start = data;
4947 for (formati = 0; formati < format_count; formati++)
4949 SKIP_ULEB (data, end);
4950 SKIP_ULEB (data, end);
4953 READ_ULEB (n_directories, data, end);
4954 if (data >= end)
4956 warn (_("Corrupt directories list\n"));
4957 break;
4960 if (n_directories == 0)
4961 directory_table = NULL;
4962 else
4963 directory_table = (unsigned char **)
4964 xmalloc (n_directories * sizeof (unsigned char *));
4966 for (entryi = 0; entryi < n_directories; entryi++)
4968 unsigned char **pathp = &directory_table[entryi];
4970 format = format_start;
4971 for (formati = 0; formati < format_count; formati++)
4973 dwarf_vma content_type, form;
4974 dwarf_vma uvalue;
4976 READ_ULEB (content_type, format, end);
4977 READ_ULEB (form, format, end);
4978 if (data >= end)
4980 warn (_("Corrupt directories list\n"));
4981 break;
4983 switch (content_type)
4985 case DW_LNCT_path:
4986 switch (form)
4988 case DW_FORM_string:
4989 *pathp = data;
4990 break;
4991 case DW_FORM_line_strp:
4992 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
4993 end);
4994 /* Remove const by the cast. */
4995 *pathp = (unsigned char *)
4996 fetch_indirect_line_string (uvalue);
4997 break;
4999 break;
5001 data = read_and_display_attr_value (0, form, 0, start,
5002 data, end, 0, 0,
5003 linfo.li_offset_size,
5004 linfo.li_version,
5005 NULL, 1, section,
5006 NULL, '\t', -1);
5008 if (data >= end)
5010 warn (_("Corrupt directories list\n"));
5011 break;
5015 /* Skip files format. */
5016 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
5017 if (do_checks && format_count > 5)
5018 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
5019 format_count);
5020 format_start = data;
5021 for (formati = 0; formati < format_count; formati++)
5023 SKIP_ULEB (data, end);
5024 SKIP_ULEB (data, end);
5027 READ_ULEB (n_files, data, end);
5028 if (data >= end && n_files > 0)
5030 warn (_("Corrupt file name list\n"));
5031 break;
5034 if (n_files == 0)
5035 file_table = NULL;
5036 else
5037 file_table = (File_Entry *) xcalloc (1, n_files
5038 * sizeof (File_Entry));
5040 for (entryi = 0; entryi < n_files; entryi++)
5042 File_Entry *file = &file_table[entryi];
5044 format = format_start;
5045 for (formati = 0; formati < format_count; formati++)
5047 dwarf_vma content_type, form;
5048 dwarf_vma uvalue;
5049 unsigned char *tmp;
5051 READ_ULEB (content_type, format, end);
5052 READ_ULEB (form, format, end);
5053 if (data >= end)
5055 warn (_("Corrupt file name list\n"));
5056 break;
5058 switch (content_type)
5060 case DW_LNCT_path:
5061 switch (form)
5063 case DW_FORM_string:
5064 file->name = data;
5065 break;
5066 case DW_FORM_line_strp:
5067 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
5068 end);
5069 /* Remove const by the cast. */
5070 file->name = (unsigned char *)
5071 fetch_indirect_line_string (uvalue);
5072 break;
5074 break;
5075 case DW_LNCT_directory_index:
5076 switch (form)
5078 case DW_FORM_data1:
5079 SAFE_BYTE_GET (file->directory_index, data, 1,
5080 end);
5081 break;
5082 case DW_FORM_data2:
5083 SAFE_BYTE_GET (file->directory_index, data, 2,
5084 end);
5085 break;
5086 case DW_FORM_udata:
5087 tmp = data;
5088 READ_ULEB (file->directory_index, tmp, end);
5089 break;
5091 break;
5093 data = read_and_display_attr_value (0, form, 0, start,
5094 data, end, 0, 0,
5095 linfo.li_offset_size,
5096 linfo.li_version,
5097 NULL, 1, section,
5098 NULL, '\t', -1);
5100 if (data >= end)
5102 warn (_("Corrupt file name list\n"));
5103 break;
5107 else
5109 if (*data != 0)
5111 unsigned char *ptr_directory_table = data;
5113 while (data < end && *data != 0)
5115 data += strnlen ((char *) data, end - data);
5116 if (data < end)
5117 data++;
5118 n_directories++;
5121 /* PR 20440 */
5122 if (data >= end)
5124 warn (_("directory table ends unexpectedly\n"));
5125 n_directories = 0;
5126 break;
5129 /* Go through the directory table again to save the directories. */
5130 directory_table = (unsigned char **)
5131 xmalloc (n_directories * sizeof (unsigned char *));
5133 i = 0;
5134 while (*ptr_directory_table != 0)
5136 directory_table[i] = ptr_directory_table;
5137 ptr_directory_table
5138 += strlen ((char *) ptr_directory_table) + 1;
5139 i++;
5142 /* Skip the NUL at the end of the table. */
5143 data++;
5145 /* Traverse the File Name table just to count the entries. */
5146 if (data < end && *data != 0)
5148 unsigned char *ptr_file_name_table = data;
5150 while (data < end && *data != 0)
5152 /* Skip Name, directory index, last modification
5153 time and length of file. */
5154 data += strnlen ((char *) data, end - data);
5155 if (data < end)
5156 data++;
5157 SKIP_ULEB (data, end);
5158 SKIP_ULEB (data, end);
5159 SKIP_ULEB (data, end);
5160 n_files++;
5163 if (data >= end)
5165 warn (_("file table ends unexpectedly\n"));
5166 n_files = 0;
5167 break;
5170 /* Go through the file table again to save the strings. */
5171 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
5173 i = 0;
5174 while (*ptr_file_name_table != 0)
5176 file_table[i].name = ptr_file_name_table;
5177 ptr_file_name_table
5178 += strlen ((char *) ptr_file_name_table) + 1;
5180 /* We are not interested in directory, time or size. */
5181 READ_ULEB (file_table[i].directory_index,
5182 ptr_file_name_table, end);
5183 READ_ULEB (file_table[i].modification_date,
5184 ptr_file_name_table, end);
5185 READ_ULEB (file_table[i].length,
5186 ptr_file_name_table, end);
5187 i++;
5189 i = 0;
5192 /* Skip the NUL at the end of the table. */
5193 data++;
5196 /* Print the Compilation Unit's name and a header. */
5197 if (file_table == NULL)
5198 printf (_("CU: No directory table\n"));
5199 else if (directory_table == NULL)
5200 printf (_("CU: %s:\n"), file_table[0].name);
5201 else
5203 unsigned int ix = file_table[0].directory_index;
5204 const char *directory;
5206 if (ix == 0)
5207 directory = ".";
5208 /* PR 20439 */
5209 else if (n_directories == 0)
5210 directory = _("<unknown>");
5211 else if (ix > n_directories)
5213 warn (_("directory index %u > number of directories %s\n"),
5214 ix, dwarf_vmatoa ("u", n_directories));
5215 directory = _("<corrupt>");
5217 else
5218 directory = (char *) directory_table[ix - 1];
5220 if (do_wide)
5221 printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
5222 else
5223 printf ("%s:\n", file_table[0].name);
5226 if (n_files > 0)
5227 printf (_("File name Line number Starting address View Stmt\n"));
5228 else
5229 printf (_("CU: Empty file name table\n"));
5230 saved_linfo = linfo;
5233 /* This loop iterates through the Dwarf Line Number Program. */
5234 while (data < end_of_sequence)
5236 unsigned char op_code;
5237 int xop;
5238 int adv;
5239 unsigned long int uladv;
5240 int is_special_opcode = 0;
5242 op_code = *data++;
5243 xop = op_code;
5245 if (op_code >= linfo.li_opcode_base)
5247 op_code -= linfo.li_opcode_base;
5248 uladv = (op_code / linfo.li_line_range);
5249 if (linfo.li_max_ops_per_insn == 1)
5251 uladv *= linfo.li_min_insn_length;
5252 state_machine_regs.address += uladv;
5253 if (uladv)
5254 state_machine_regs.view = 0;
5256 else
5258 unsigned addrdelta
5259 = ((state_machine_regs.op_index + uladv)
5260 / linfo.li_max_ops_per_insn)
5261 * linfo.li_min_insn_length;
5262 state_machine_regs.address
5263 += addrdelta;
5264 state_machine_regs.op_index
5265 = (state_machine_regs.op_index + uladv)
5266 % linfo.li_max_ops_per_insn;
5267 if (addrdelta)
5268 state_machine_regs.view = 0;
5271 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5272 state_machine_regs.line += adv;
5273 is_special_opcode = 1;
5274 /* Increment view after printing this row. */
5276 else
5277 switch (op_code)
5279 case DW_LNS_extended_op:
5281 unsigned int ext_op_code_len;
5282 unsigned char ext_op_code;
5283 unsigned char *op_code_end;
5284 unsigned char *op_code_data = data;
5286 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5287 op_code_end = op_code_data + ext_op_code_len;
5288 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5290 warn (_("Badly formed extended line op encountered!\n"));
5291 break;
5293 ext_op_code = *op_code_data++;
5294 xop = ext_op_code;
5295 xop = -xop;
5297 switch (ext_op_code)
5299 case DW_LNE_end_sequence:
5300 /* Reset stuff after printing this row. */
5301 break;
5302 case DW_LNE_set_address:
5303 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5304 op_code_data,
5305 op_code_end - op_code_data,
5306 op_code_end);
5307 state_machine_regs.op_index = 0;
5308 state_machine_regs.view = 0;
5309 break;
5310 case DW_LNE_define_file:
5311 file_table = (File_Entry *) xrealloc
5312 (file_table, (n_files + 1) * sizeof (File_Entry));
5314 ++state_machine_regs.last_file_entry;
5315 /* Source file name. */
5316 file_table[n_files].name = op_code_data;
5317 op_code_data += strlen ((char *) op_code_data) + 1;
5318 /* Directory index. */
5319 READ_ULEB (file_table[n_files].directory_index,
5320 op_code_data, op_code_end);
5321 /* Last modification time. */
5322 READ_ULEB (file_table[n_files].modification_date,
5323 op_code_data, op_code_end);
5324 /* File length. */
5325 READ_ULEB (file_table[n_files].length,
5326 op_code_data, op_code_end);
5327 n_files++;
5328 break;
5330 case DW_LNE_set_discriminator:
5331 case DW_LNE_HP_set_sequence:
5332 /* Simply ignored. */
5333 break;
5335 default:
5336 printf (_("UNKNOWN (%u): length %ld\n"),
5337 ext_op_code, (long int) (op_code_data - data));
5338 break;
5340 data = op_code_end;
5341 break;
5343 case DW_LNS_copy:
5344 /* Increment view after printing this row. */
5345 break;
5347 case DW_LNS_advance_pc:
5348 READ_ULEB (uladv, data, end);
5349 if (linfo.li_max_ops_per_insn == 1)
5351 uladv *= linfo.li_min_insn_length;
5352 state_machine_regs.address += uladv;
5353 if (uladv)
5354 state_machine_regs.view = 0;
5356 else
5358 unsigned addrdelta
5359 = ((state_machine_regs.op_index + uladv)
5360 / linfo.li_max_ops_per_insn)
5361 * linfo.li_min_insn_length;
5362 state_machine_regs.address
5363 += addrdelta;
5364 state_machine_regs.op_index
5365 = (state_machine_regs.op_index + uladv)
5366 % linfo.li_max_ops_per_insn;
5367 if (addrdelta)
5368 state_machine_regs.view = 0;
5370 break;
5372 case DW_LNS_advance_line:
5373 READ_SLEB (adv, data, end);
5374 state_machine_regs.line += adv;
5375 break;
5377 case DW_LNS_set_file:
5378 READ_ULEB (uladv, data, end);
5379 state_machine_regs.file = uladv;
5382 unsigned file = state_machine_regs.file;
5383 unsigned dir;
5385 if (linfo.li_version < 5)
5386 --file;
5387 if (file_table == NULL || n_files == 0)
5388 printf (_("\n [Use file table entry %d]\n"), file);
5389 /* PR 20439 */
5390 else if (file >= n_files)
5392 warn (_("file index %u > number of files %u\n"), file, n_files);
5393 printf (_("\n <over large file table index %u>"), file);
5395 else if ((dir = file_table[file].directory_index) == 0)
5396 /* If directory index is 0, that means current directory. */
5397 printf ("\n./%s:[++]\n", file_table[file].name);
5398 else if (directory_table == NULL || n_directories == 0)
5399 printf (_("\n [Use file %s in directory table entry %d]\n"),
5400 file_table[file].name, dir);
5401 /* PR 20439 */
5402 else if (dir > n_directories)
5404 warn (_("directory index %u > number of directories %s\n"),
5405 dir, dwarf_vmatoa ("u", n_directories));
5406 printf (_("\n <over large directory table entry %u>\n"), dir);
5408 else
5409 printf ("\n%s/%s:\n",
5410 /* The directory index starts counting at 1. */
5411 directory_table[dir - 1], file_table[file].name);
5413 break;
5415 case DW_LNS_set_column:
5416 READ_ULEB (uladv, data, end);
5417 state_machine_regs.column = uladv;
5418 break;
5420 case DW_LNS_negate_stmt:
5421 adv = state_machine_regs.is_stmt;
5422 adv = ! adv;
5423 state_machine_regs.is_stmt = adv;
5424 break;
5426 case DW_LNS_set_basic_block:
5427 state_machine_regs.basic_block = 1;
5428 break;
5430 case DW_LNS_const_add_pc:
5431 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5432 if (linfo.li_max_ops_per_insn == 1)
5434 uladv *= linfo.li_min_insn_length;
5435 state_machine_regs.address += uladv;
5436 if (uladv)
5437 state_machine_regs.view = 0;
5439 else
5441 unsigned addrdelta
5442 = ((state_machine_regs.op_index + uladv)
5443 / linfo.li_max_ops_per_insn)
5444 * linfo.li_min_insn_length;
5445 state_machine_regs.address
5446 += addrdelta;
5447 state_machine_regs.op_index
5448 = (state_machine_regs.op_index + uladv)
5449 % linfo.li_max_ops_per_insn;
5450 if (addrdelta)
5451 state_machine_regs.view = 0;
5453 break;
5455 case DW_LNS_fixed_advance_pc:
5456 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5457 state_machine_regs.address += uladv;
5458 state_machine_regs.op_index = 0;
5459 /* Do NOT reset view. */
5460 break;
5462 case DW_LNS_set_prologue_end:
5463 break;
5465 case DW_LNS_set_epilogue_begin:
5466 break;
5468 case DW_LNS_set_isa:
5469 READ_ULEB (uladv, data, end);
5470 printf (_(" Set ISA to %lu\n"), uladv);
5471 break;
5473 default:
5474 printf (_(" Unknown opcode %d with operands: "), op_code);
5476 if (standard_opcodes != NULL)
5477 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5479 dwarf_vma val;
5481 READ_ULEB (val, data, end);
5482 printf ("0x%s%s", dwarf_vmatoa ("x", val),
5483 i == 1 ? "" : ", ");
5485 putchar ('\n');
5486 break;
5489 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5490 to the DWARF address/line matrix. */
5491 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5492 || (xop == DW_LNS_copy))
5494 const unsigned int MAX_FILENAME_LENGTH = 35;
5495 char *fileName;
5496 char *newFileName = NULL;
5497 size_t fileNameLength;
5499 if (file_table)
5501 unsigned indx = state_machine_regs.file;
5503 if (linfo.li_version < 5)
5504 --indx;
5505 /* PR 20439 */
5506 if (indx >= n_files)
5508 warn (_("corrupt file index %u encountered\n"), indx);
5509 fileName = _("<corrupt>");
5511 else
5512 fileName = (char *) file_table[indx].name;
5514 else
5515 fileName = _("<unknown>");
5517 fileNameLength = strlen (fileName);
5518 newFileName = fileName;
5519 if (fileNameLength > MAX_FILENAME_LENGTH && !do_wide)
5521 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5522 /* Truncate file name */
5523 memcpy (newFileName,
5524 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5525 MAX_FILENAME_LENGTH);
5526 newFileName[MAX_FILENAME_LENGTH] = 0;
5529 /* A row with end_seq set to true has a meaningful address, but
5530 the other information in the same row is not significant.
5531 In such a row, print line as "-", and don't print
5532 view/is_stmt. */
5533 if (!do_wide || fileNameLength <= MAX_FILENAME_LENGTH)
5535 if (linfo.li_max_ops_per_insn == 1)
5537 if (xop == -DW_LNE_end_sequence)
5538 printf ("%-35s %11s %#18" DWARF_VMA_FMT "x",
5539 newFileName, "-",
5540 state_machine_regs.address);
5541 else
5542 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x",
5543 newFileName, state_machine_regs.line,
5544 state_machine_regs.address);
5546 else
5548 if (xop == -DW_LNE_end_sequence)
5549 printf ("%-35s %11s %#18" DWARF_VMA_FMT "x[%d]",
5550 newFileName, "-",
5551 state_machine_regs.address,
5552 state_machine_regs.op_index);
5553 else
5554 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x[%d]",
5555 newFileName, state_machine_regs.line,
5556 state_machine_regs.address,
5557 state_machine_regs.op_index);
5560 else
5562 if (linfo.li_max_ops_per_insn == 1)
5564 if (xop == -DW_LNE_end_sequence)
5565 printf ("%s %11s %#18" DWARF_VMA_FMT "x",
5566 newFileName, "-",
5567 state_machine_regs.address);
5568 else
5569 printf ("%s %11d %#18" DWARF_VMA_FMT "x",
5570 newFileName, state_machine_regs.line,
5571 state_machine_regs.address);
5573 else
5575 if (xop == -DW_LNE_end_sequence)
5576 printf ("%s %11s %#18" DWARF_VMA_FMT "x[%d]",
5577 newFileName, "-",
5578 state_machine_regs.address,
5579 state_machine_regs.op_index);
5580 else
5581 printf ("%s %11d %#18" DWARF_VMA_FMT "x[%d]",
5582 newFileName, state_machine_regs.line,
5583 state_machine_regs.address,
5584 state_machine_regs.op_index);
5588 if (xop != -DW_LNE_end_sequence)
5590 if (state_machine_regs.view)
5591 printf (" %6u", state_machine_regs.view);
5592 else
5593 printf (" ");
5595 if (state_machine_regs.is_stmt)
5596 printf (" x");
5599 putchar ('\n');
5600 state_machine_regs.view++;
5602 if (xop == -DW_LNE_end_sequence)
5604 reset_state_machine (linfo.li_default_is_stmt);
5605 putchar ('\n');
5608 if (newFileName != fileName)
5609 free (newFileName);
5613 if (file_table)
5615 free (file_table);
5616 file_table = NULL;
5617 n_files = 0;
5620 if (directory_table)
5622 free (directory_table);
5623 directory_table = NULL;
5624 n_directories = 0;
5627 putchar ('\n');
5630 return 1;
5633 static int
5634 display_debug_lines (struct dwarf_section *section, void *file)
5636 unsigned char *data = section->start;
5637 unsigned char *end = data + section->size;
5638 int retValRaw = 1;
5639 int retValDecoded = 1;
5641 if (do_debug_lines == 0)
5642 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5644 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
5645 retValRaw = display_debug_lines_raw (section, data, end, file);
5647 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
5648 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
5650 if (!retValRaw || !retValDecoded)
5651 return 0;
5653 return 1;
5656 static debug_info *
5657 find_debug_info_for_offset (dwarf_vma offset)
5659 unsigned int i;
5661 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5662 return NULL;
5664 for (i = 0; i < num_debug_info_entries; i++)
5665 if (debug_information[i].cu_offset == offset)
5666 return debug_information + i;
5668 return NULL;
5671 static const char *
5672 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5674 /* See gdb/gdb-index.h. */
5675 static const char * const kinds[] =
5677 N_ ("no info"),
5678 N_ ("type"),
5679 N_ ("variable"),
5680 N_ ("function"),
5681 N_ ("other"),
5682 N_ ("unused5"),
5683 N_ ("unused6"),
5684 N_ ("unused7")
5687 return _ (kinds[kind]);
5690 static int
5691 display_debug_pubnames_worker (struct dwarf_section *section,
5692 void *file ATTRIBUTE_UNUSED,
5693 int is_gnu)
5695 DWARF2_Internal_PubNames names;
5696 unsigned char *start = section->start;
5697 unsigned char *end = start + section->size;
5699 /* It does not matter if this load fails,
5700 we test for that later on. */
5701 load_debug_info (file);
5703 introduce (section, false);
5705 while (start < end)
5707 unsigned char *data;
5708 unsigned long sec_off = start - section->start;
5709 unsigned int offset_size;
5711 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
5712 if (names.pn_length == 0xffffffff)
5714 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
5715 offset_size = 8;
5717 else
5718 offset_size = 4;
5720 if (names.pn_length > (size_t) (end - start))
5722 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
5723 section->name,
5724 sec_off,
5725 dwarf_vmatoa ("x", names.pn_length));
5726 break;
5729 data = start;
5730 start += names.pn_length;
5732 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, start);
5733 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, start);
5735 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5736 && num_debug_info_entries > 0
5737 && find_debug_info_for_offset (names.pn_offset) == NULL)
5738 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
5739 (unsigned long) names.pn_offset, section->name);
5741 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, start);
5743 printf (_(" Length: %ld\n"),
5744 (long) names.pn_length);
5745 printf (_(" Version: %d\n"),
5746 names.pn_version);
5747 printf (_(" Offset into .debug_info section: 0x%lx\n"),
5748 (unsigned long) names.pn_offset);
5749 printf (_(" Size of area in .debug_info section: %ld\n"),
5750 (long) names.pn_size);
5752 if (names.pn_version != 2 && names.pn_version != 3)
5754 static int warned = 0;
5756 if (! warned)
5758 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
5759 warned = 1;
5762 continue;
5765 if (is_gnu)
5766 printf (_("\n Offset Kind Name\n"));
5767 else
5768 printf (_("\n Offset\tName\n"));
5770 while (1)
5772 bfd_size_type maxprint;
5773 dwarf_vma offset;
5775 SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start);
5777 if (offset == 0)
5778 break;
5780 if (data >= start)
5781 break;
5782 maxprint = (start - data) - 1;
5784 if (is_gnu)
5786 unsigned int kind_data;
5787 gdb_index_symbol_kind kind;
5788 const char *kind_name;
5789 int is_static;
5791 SAFE_BYTE_GET_AND_INC (kind_data, data, 1, start);
5792 maxprint --;
5793 /* GCC computes the kind as the upper byte in the CU index
5794 word, and then right shifts it by the CU index size.
5795 Left shift KIND to where the gdb-index.h accessor macros
5796 can use it. */
5797 kind_data <<= GDB_INDEX_CU_BITSIZE;
5798 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
5799 kind_name = get_gdb_index_symbol_kind_name (kind);
5800 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
5801 printf (" %-6lx %s,%-10s %.*s\n",
5802 (unsigned long) offset, is_static ? _("s") : _("g"),
5803 kind_name, (int) maxprint, data);
5805 else
5806 printf (" %-6lx\t%.*s\n",
5807 (unsigned long) offset, (int) maxprint, data);
5809 data += strnlen ((char *) data, maxprint);
5810 if (data < start)
5811 data++;
5812 if (data >= start)
5813 break;
5817 printf ("\n");
5818 return 1;
5821 static int
5822 display_debug_pubnames (struct dwarf_section *section, void *file)
5824 return display_debug_pubnames_worker (section, file, 0);
5827 static int
5828 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
5830 return display_debug_pubnames_worker (section, file, 1);
5833 static int
5834 display_debug_macinfo (struct dwarf_section *section,
5835 void *file ATTRIBUTE_UNUSED)
5837 unsigned char *start = section->start;
5838 unsigned char *end = start + section->size;
5839 unsigned char *curr = start;
5840 enum dwarf_macinfo_record_type op;
5842 introduce (section, false);
5844 while (curr < end)
5846 unsigned int lineno;
5847 const unsigned char *string;
5849 op = (enum dwarf_macinfo_record_type) *curr;
5850 curr++;
5852 switch (op)
5854 case DW_MACINFO_start_file:
5856 unsigned int filenum;
5858 READ_ULEB (lineno, curr, end);
5859 READ_ULEB (filenum, curr, end);
5860 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
5861 lineno, filenum);
5863 break;
5865 case DW_MACINFO_end_file:
5866 printf (_(" DW_MACINFO_end_file\n"));
5867 break;
5869 case DW_MACINFO_define:
5870 READ_ULEB (lineno, curr, end);
5871 string = curr;
5872 curr += strnlen ((char *) string, end - string);
5873 printf (_(" DW_MACINFO_define - lineno : %d macro : %*s\n"),
5874 lineno, (int) (curr - string), string);
5875 if (curr < end)
5876 curr++;
5877 break;
5879 case DW_MACINFO_undef:
5880 READ_ULEB (lineno, curr, end);
5881 string = curr;
5882 curr += strnlen ((char *) string, end - string);
5883 printf (_(" DW_MACINFO_undef - lineno : %d macro : %*s\n"),
5884 lineno, (int) (curr - string), string);
5885 if (curr < end)
5886 curr++;
5887 break;
5889 case DW_MACINFO_vendor_ext:
5891 unsigned int constant;
5893 READ_ULEB (constant, curr, end);
5894 string = curr;
5895 curr += strnlen ((char *) string, end - string);
5896 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %*s\n"),
5897 constant, (int) (curr - string), string);
5898 if (curr < end)
5899 curr++;
5901 break;
5905 return 1;
5908 /* Given LINE_OFFSET into the .debug_line section, attempt to return
5909 filename and dirname corresponding to file name table entry with index
5910 FILEIDX. Return NULL on failure. */
5912 static unsigned char *
5913 get_line_filename_and_dirname (dwarf_vma line_offset,
5914 dwarf_vma fileidx,
5915 unsigned char **dir_name)
5917 struct dwarf_section *section = &debug_displays [line].section;
5918 unsigned char *hdrptr, *dirtable, *file_name;
5919 unsigned int offset_size;
5920 unsigned int version, opcode_base;
5921 dwarf_vma length, diridx;
5922 const unsigned char * end;
5924 *dir_name = NULL;
5925 if (section->start == NULL
5926 || line_offset >= section->size
5927 || fileidx == 0)
5928 return NULL;
5930 hdrptr = section->start + line_offset;
5931 end = section->start + section->size;
5933 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
5934 if (length == 0xffffffff)
5936 /* This section is 64-bit DWARF 3. */
5937 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
5938 offset_size = 8;
5940 else
5941 offset_size = 4;
5943 if (length > (size_t) (end - hdrptr)
5944 || length < 2 + offset_size + 1 + 3 + 1)
5945 return NULL;
5946 end = hdrptr + length;
5948 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
5949 if (version != 2 && version != 3 && version != 4)
5950 return NULL;
5951 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
5952 if (version >= 4)
5953 hdrptr++; /* Skip max_ops_per_insn. */
5954 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
5956 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
5957 if (opcode_base == 0
5958 || opcode_base - 1 >= (size_t) (end - hdrptr))
5959 return NULL;
5961 hdrptr += opcode_base - 1;
5963 dirtable = hdrptr;
5964 /* Skip over dirname table. */
5965 while (*hdrptr != '\0')
5967 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5968 if (hdrptr < end)
5969 hdrptr++;
5970 if (hdrptr >= end)
5971 return NULL;
5973 hdrptr++; /* Skip the NUL at the end of the table. */
5975 /* Now skip over preceding filename table entries. */
5976 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
5978 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5979 if (hdrptr < end)
5980 hdrptr++;
5981 SKIP_ULEB (hdrptr, end);
5982 SKIP_ULEB (hdrptr, end);
5983 SKIP_ULEB (hdrptr, end);
5985 if (hdrptr >= end || *hdrptr == '\0')
5986 return NULL;
5988 file_name = hdrptr;
5989 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5990 if (hdrptr < end)
5991 hdrptr++;
5992 if (hdrptr >= end)
5993 return NULL;
5994 READ_ULEB (diridx, hdrptr, end);
5995 if (diridx == 0)
5996 return file_name;
5997 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
5999 dirtable += strnlen ((char *) dirtable, end - dirtable);
6000 if (dirtable < end)
6001 dirtable++;
6003 if (dirtable >= end || *dirtable == '\0')
6004 return NULL;
6005 *dir_name = dirtable;
6006 return file_name;
6009 static int
6010 display_debug_macro (struct dwarf_section *section,
6011 void *file)
6013 unsigned char *start = section->start;
6014 unsigned char *end = start + section->size;
6015 unsigned char *curr = start;
6016 unsigned char *extended_op_buf[256];
6017 bool is_dwo = false;
6018 const char *suffix = strrchr (section->name, '.');
6020 if (suffix && strcmp (suffix, ".dwo") == 0)
6021 is_dwo = true;
6023 load_debug_section_with_follow (str, file);
6024 load_debug_section_with_follow (line, file);
6025 load_debug_section_with_follow (str_index, file);
6027 introduce (section, false);
6029 while (curr < end)
6031 unsigned int lineno, version, flags;
6032 unsigned int offset_size;
6033 const unsigned char *string;
6034 dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
6035 unsigned char **extended_ops = NULL;
6037 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
6038 if (version != 4 && version != 5)
6040 error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
6041 section->name, version);
6042 return 0;
6045 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
6046 offset_size = (flags & 1) ? 8 : 4;
6047 printf (_(" Offset: 0x%lx\n"),
6048 (unsigned long) sec_offset);
6049 printf (_(" Version: %d\n"), version);
6050 printf (_(" Offset size: %d\n"), offset_size);
6051 if (flags & 2)
6053 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
6054 printf (_(" Offset into .debug_line: 0x%lx\n"),
6055 (unsigned long) line_offset);
6057 if (flags & 4)
6059 unsigned int i, count, op;
6060 dwarf_vma nargs, n;
6062 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
6064 memset (extended_op_buf, 0, sizeof (extended_op_buf));
6065 extended_ops = extended_op_buf;
6066 if (count)
6068 printf (_(" Extension opcode arguments:\n"));
6069 for (i = 0; i < count; i++)
6071 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6072 extended_ops[op] = curr;
6073 READ_ULEB (nargs, curr, end);
6074 if (nargs == 0)
6075 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
6076 else
6078 printf (_(" DW_MACRO_%02x arguments: "), op);
6079 for (n = 0; n < nargs; n++)
6081 unsigned int form;
6083 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
6084 printf ("%s%s", get_FORM_name (form),
6085 n == nargs - 1 ? "\n" : ", ");
6086 switch (form)
6088 case DW_FORM_data1:
6089 case DW_FORM_data2:
6090 case DW_FORM_data4:
6091 case DW_FORM_data8:
6092 case DW_FORM_sdata:
6093 case DW_FORM_udata:
6094 case DW_FORM_block:
6095 case DW_FORM_block1:
6096 case DW_FORM_block2:
6097 case DW_FORM_block4:
6098 case DW_FORM_flag:
6099 case DW_FORM_string:
6100 case DW_FORM_strp:
6101 case DW_FORM_sec_offset:
6102 break;
6103 default:
6104 error (_("Invalid extension opcode form %s\n"),
6105 get_FORM_name (form));
6106 return 0;
6113 printf ("\n");
6115 while (1)
6117 unsigned int op;
6119 if (curr >= end)
6121 error (_(".debug_macro section not zero terminated\n"));
6122 return 0;
6125 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6126 if (op == 0)
6127 break;
6129 switch (op)
6131 case DW_MACRO_define:
6132 READ_ULEB (lineno, curr, end);
6133 string = curr;
6134 curr += strnlen ((char *) string, end - string);
6135 printf (_(" DW_MACRO_define - lineno : %d macro : %*s\n"),
6136 lineno, (int) (curr - string), string);
6137 if (curr < end)
6138 curr++;
6139 break;
6141 case DW_MACRO_undef:
6142 READ_ULEB (lineno, curr, end);
6143 string = curr;
6144 curr += strnlen ((char *) string, end - string);
6145 printf (_(" DW_MACRO_undef - lineno : %d macro : %*s\n"),
6146 lineno, (int) (curr - string), string);
6147 if (curr < end)
6148 curr++;
6149 break;
6151 case DW_MACRO_start_file:
6153 unsigned int filenum;
6154 unsigned char *file_name = NULL, *dir_name = NULL;
6156 READ_ULEB (lineno, curr, end);
6157 READ_ULEB (filenum, curr, end);
6159 if ((flags & 2) == 0)
6160 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
6161 else
6162 file_name
6163 = get_line_filename_and_dirname (line_offset, filenum,
6164 &dir_name);
6165 if (file_name == NULL)
6166 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
6167 lineno, filenum);
6168 else
6169 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
6170 lineno, filenum,
6171 dir_name != NULL ? (const char *) dir_name : "",
6172 dir_name != NULL ? "/" : "", file_name);
6174 break;
6176 case DW_MACRO_end_file:
6177 printf (_(" DW_MACRO_end_file\n"));
6178 break;
6180 case DW_MACRO_define_strp:
6181 READ_ULEB (lineno, curr, end);
6182 if (version == 4 && is_dwo)
6183 READ_ULEB (offset, curr, end);
6184 else
6185 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6186 string = fetch_indirect_string (offset);
6187 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
6188 lineno, string);
6189 break;
6191 case DW_MACRO_undef_strp:
6192 READ_ULEB (lineno, curr, end);
6193 if (version == 4 && is_dwo)
6194 READ_ULEB (offset, curr, end);
6195 else
6196 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6197 string = fetch_indirect_string (offset);
6198 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
6199 lineno, string);
6200 break;
6202 case DW_MACRO_import:
6203 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6204 printf (_(" DW_MACRO_import - offset : 0x%lx\n"),
6205 (unsigned long) offset);
6206 break;
6208 case DW_MACRO_define_sup:
6209 READ_ULEB (lineno, curr, end);
6210 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6211 printf (_(" DW_MACRO_define_sup - lineno : %d macro offset : 0x%lx\n"),
6212 lineno, (unsigned long) offset);
6213 break;
6215 case DW_MACRO_undef_sup:
6216 READ_ULEB (lineno, curr, end);
6217 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6218 printf (_(" DW_MACRO_undef_sup - lineno : %d macro offset : 0x%lx\n"),
6219 lineno, (unsigned long) offset);
6220 break;
6222 case DW_MACRO_import_sup:
6223 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6224 printf (_(" DW_MACRO_import_sup - offset : 0x%lx\n"),
6225 (unsigned long) offset);
6226 break;
6228 case DW_MACRO_define_strx:
6229 case DW_MACRO_undef_strx:
6230 READ_ULEB (lineno, curr, end);
6231 READ_ULEB (offset, curr, end);
6232 string = (const unsigned char *)
6233 fetch_indexed_string (offset, NULL, offset_size, false);
6234 if (op == DW_MACRO_define_strx)
6235 printf (" DW_MACRO_define_strx ");
6236 else
6237 printf (" DW_MACRO_undef_strx ");
6238 if (do_wide)
6239 printf (_("(with offset %s) "), dwarf_vmatoa ("x", offset));
6240 printf (_("lineno : %d macro : %s\n"),
6241 lineno, string);
6242 break;
6244 default:
6245 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6247 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6248 break;
6251 if (extended_ops == NULL || extended_ops[op] == NULL)
6253 error (_(" Unknown macro opcode %02x seen\n"), op);
6254 return 0;
6256 else
6258 /* Skip over unhandled opcodes. */
6259 dwarf_vma nargs, n;
6260 unsigned char *desc = extended_ops[op];
6261 READ_ULEB (nargs, desc, end);
6262 if (nargs == 0)
6264 printf (_(" DW_MACRO_%02x\n"), op);
6265 break;
6267 printf (_(" DW_MACRO_%02x -"), op);
6268 for (n = 0; n < nargs; n++)
6270 int val;
6272 /* DW_FORM_implicit_const is not expected here. */
6273 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
6274 curr
6275 = read_and_display_attr_value (0, val, 0,
6276 start, curr, end, 0, 0, offset_size,
6277 version, NULL, 0, NULL,
6278 NULL, ' ', -1);
6279 if (n != nargs - 1)
6280 printf (",");
6282 printf ("\n");
6284 break;
6288 printf ("\n");
6291 return 1;
6294 static int
6295 display_debug_abbrev (struct dwarf_section *section,
6296 void *file ATTRIBUTE_UNUSED)
6298 abbrev_entry *entry;
6299 unsigned char *start = section->start;
6301 introduce (section, false);
6305 abbrev_list * list;
6306 dwarf_vma offset;
6308 offset = start - section->start;
6309 list = find_abbrev_list_by_abbrev_offset (0, offset);
6310 if (list == NULL)
6312 list = new_abbrev_list (0, offset);
6313 start = process_abbrev_set (section, 0, section->size, offset, list);
6314 list->start_of_next_abbrevs = start;
6316 else
6317 start = list->start_of_next_abbrevs;
6319 if (list->first_abbrev == NULL)
6320 continue;
6322 printf (_(" Number TAG (0x%lx)\n"), (long) offset);
6324 for (entry = list->first_abbrev; entry; entry = entry->next)
6326 abbrev_attr *attr;
6328 printf (" %ld %s [%s]\n",
6329 entry->number,
6330 get_TAG_name (entry->tag),
6331 entry->children ? _("has children") : _("no children"));
6333 for (attr = entry->first_attr; attr; attr = attr->next)
6335 printf (" %-18s %s",
6336 get_AT_name (attr->attribute),
6337 get_FORM_name (attr->form));
6338 if (attr->form == DW_FORM_implicit_const)
6339 printf (": %s", dwarf_vmatoa ("d", attr->implicit_const));
6340 putchar ('\n');
6344 while (start);
6346 printf ("\n");
6348 return 1;
6351 /* Return true when ADDR is the maximum address, when addresses are
6352 POINTER_SIZE bytes long. */
6354 static bool
6355 is_max_address (dwarf_vma addr, unsigned int pointer_size)
6357 dwarf_vma mask = ~(~(dwarf_vma) 1 << (pointer_size * 8 - 1));
6358 return ((addr & mask) == mask);
6361 /* Display a view pair list starting at *VSTART_PTR and ending at
6362 VLISTEND within SECTION. */
6364 static void
6365 display_view_pair_list (struct dwarf_section *section,
6366 unsigned char **vstart_ptr,
6367 unsigned int debug_info_entry,
6368 unsigned char *vlistend)
6370 unsigned char *vstart = *vstart_ptr;
6371 unsigned char *section_end = section->start + section->size;
6372 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6374 if (vlistend < section_end)
6375 section_end = vlistend;
6377 putchar ('\n');
6379 while (vstart < section_end)
6381 dwarf_vma off = vstart - section->start;
6382 dwarf_vma vbegin, vend;
6384 READ_ULEB (vbegin, vstart, section_end);
6385 if (vstart == section_end)
6386 break;
6388 READ_ULEB (vend, vstart, section_end);
6389 printf (" %8.8lx ", (unsigned long) off);
6391 print_dwarf_view (vbegin, pointer_size, 1);
6392 print_dwarf_view (vend, pointer_size, 1);
6393 printf (_("location view pair\n"));
6396 putchar ('\n');
6397 *vstart_ptr = vstart;
6400 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
6402 static void
6403 display_loc_list (struct dwarf_section *section,
6404 unsigned char **start_ptr,
6405 unsigned int debug_info_entry,
6406 dwarf_vma offset,
6407 dwarf_vma base_address,
6408 unsigned char **vstart_ptr,
6409 int has_frame_base)
6411 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6412 unsigned char *section_end = section->start + section->size;
6413 dwarf_vma cu_offset;
6414 unsigned int pointer_size;
6415 unsigned int offset_size;
6416 int dwarf_version;
6417 dwarf_vma begin;
6418 dwarf_vma end;
6419 unsigned short length;
6420 int need_frame_base;
6422 if (debug_info_entry >= num_debug_info_entries)
6424 warn (_("No debug information available for loc lists of entry: %u\n"),
6425 debug_info_entry);
6426 return;
6429 cu_offset = debug_information [debug_info_entry].cu_offset;
6430 pointer_size = debug_information [debug_info_entry].pointer_size;
6431 offset_size = debug_information [debug_info_entry].offset_size;
6432 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6434 if (pointer_size < 2 || pointer_size > 8)
6436 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6437 pointer_size, debug_info_entry);
6438 return;
6441 while (1)
6443 dwarf_vma off = offset + (start - *start_ptr);
6444 dwarf_vma vbegin = vm1, vend = vm1;
6446 if (2 * pointer_size > (size_t) (section_end - start))
6448 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6449 (unsigned long) offset);
6450 break;
6453 printf (" ");
6454 print_dwarf_vma (off, 4);
6456 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6457 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6459 if (begin == 0 && end == 0)
6461 /* PR 18374: In a object file we can have a location list that
6462 starts with a begin and end of 0 because there are relocations
6463 that need to be applied to the addresses. Actually applying
6464 the relocations now does not help as they will probably resolve
6465 to 0, since the object file has not been fully linked. Real
6466 end of list markers will not have any relocations against them. */
6467 if (! reloc_at (section, off)
6468 && ! reloc_at (section, off + pointer_size))
6470 printf (_("<End of list>\n"));
6471 break;
6475 /* Check base address specifiers. */
6476 if (is_max_address (begin, pointer_size)
6477 && !is_max_address (end, pointer_size))
6479 base_address = end;
6480 print_dwarf_vma (begin, pointer_size);
6481 print_dwarf_vma (end, pointer_size);
6482 printf (_("(base address)\n"));
6483 continue;
6486 if (vstart)
6488 off = offset + (vstart - *start_ptr);
6490 READ_ULEB (vbegin, vstart, section_end);
6491 print_dwarf_view (vbegin, pointer_size, 1);
6493 READ_ULEB (vend, vstart, section_end);
6494 print_dwarf_view (vend, pointer_size, 1);
6496 printf (_("views at %8.8lx for:\n %*s "),
6497 (unsigned long) off, 8, "");
6500 if (2 > (size_t) (section_end - start))
6502 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6503 (unsigned long) offset);
6504 break;
6507 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6509 if (length > (size_t) (section_end - start))
6511 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6512 (unsigned long) offset);
6513 break;
6516 print_dwarf_vma (begin + base_address, pointer_size);
6517 print_dwarf_vma (end + base_address, pointer_size);
6519 putchar ('(');
6520 need_frame_base = decode_location_expression (start,
6521 pointer_size,
6522 offset_size,
6523 dwarf_version,
6524 length,
6525 cu_offset, section);
6526 putchar (')');
6528 if (need_frame_base && !has_frame_base)
6529 printf (_(" [without DW_AT_frame_base]"));
6531 if (begin == end && vbegin == vend)
6532 fputs (_(" (start == end)"), stdout);
6533 else if (begin > end || (begin == end && vbegin > vend))
6534 fputs (_(" (start > end)"), stdout);
6536 putchar ('\n');
6538 start += length;
6541 *start_ptr = start;
6542 *vstart_ptr = vstart;
6545 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
6547 static void
6548 display_loclists_list (struct dwarf_section * section,
6549 unsigned char ** start_ptr,
6550 unsigned int debug_info_entry,
6551 dwarf_vma offset,
6552 dwarf_vma base_address,
6553 unsigned char ** vstart_ptr,
6554 int has_frame_base)
6556 unsigned char * start = *start_ptr;
6557 unsigned char * vstart = *vstart_ptr;
6558 unsigned char * section_end = section->start + section->size;
6559 dwarf_vma cu_offset;
6560 unsigned int pointer_size;
6561 unsigned int offset_size;
6562 unsigned int dwarf_version;
6564 /* Initialize it due to a false compiler warning. */
6565 dwarf_vma begin = -1, vbegin = -1;
6566 dwarf_vma end = -1, vend = -1;
6567 dwarf_vma length;
6568 int need_frame_base;
6570 if (debug_info_entry >= num_debug_info_entries)
6572 warn (_("No debug information available for "
6573 "loclists lists of entry: %u\n"),
6574 debug_info_entry);
6575 return;
6578 cu_offset = debug_information [debug_info_entry].cu_offset;
6579 pointer_size = debug_information [debug_info_entry].pointer_size;
6580 offset_size = debug_information [debug_info_entry].offset_size;
6581 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6583 if (pointer_size < 2 || pointer_size > 8)
6585 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6586 pointer_size, debug_info_entry);
6587 return;
6590 while (1)
6592 dwarf_vma off = offset + (start - *start_ptr);
6593 enum dwarf_location_list_entry_type llet;
6595 if (start + 1 > section_end)
6597 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6598 (unsigned long) offset);
6599 break;
6602 printf (" ");
6603 print_dwarf_vma (off, 4);
6605 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6607 if (vstart && (llet == DW_LLE_offset_pair
6608 || llet == DW_LLE_start_end
6609 || llet == DW_LLE_start_length))
6611 off = offset + (vstart - *start_ptr);
6613 READ_ULEB (vbegin, vstart, section_end);
6614 print_dwarf_view (vbegin, pointer_size, 1);
6616 READ_ULEB (vend, vstart, section_end);
6617 print_dwarf_view (vend, pointer_size, 1);
6619 printf (_("views at %8.8lx for:\n %*s "),
6620 (unsigned long) off, 8, "");
6623 switch (llet)
6625 case DW_LLE_end_of_list:
6626 printf (_("<End of list>\n"));
6627 break;
6629 case DW_LLE_base_addressx:
6630 READ_ULEB (base_address, start, section_end);
6631 print_dwarf_vma (base_address, pointer_size);
6632 printf (_("(index into .debug_addr) "));
6633 base_address = fetch_indexed_addr (base_address, pointer_size);
6634 print_dwarf_vma (base_address, pointer_size);
6635 printf (_("(base address)\n"));
6636 break;
6638 case DW_LLE_startx_endx:
6639 READ_ULEB (begin, start, section_end);
6640 begin = fetch_indexed_addr (begin, pointer_size);
6641 READ_ULEB (end, start, section_end);
6642 end = fetch_indexed_addr (end, pointer_size);
6643 break;
6645 case DW_LLE_startx_length:
6646 READ_ULEB (begin, start, section_end);
6647 begin = fetch_indexed_addr (begin, pointer_size);
6648 READ_ULEB (end, start, section_end);
6649 end += begin;
6650 break;
6652 case DW_LLE_default_location:
6653 begin = end = 0;
6654 break;
6656 case DW_LLE_offset_pair:
6657 READ_ULEB (begin, start, section_end);
6658 begin += base_address;
6659 READ_ULEB (end, start, section_end);
6660 end += base_address;
6661 break;
6663 case DW_LLE_base_address:
6664 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6665 section_end);
6666 print_dwarf_vma (base_address, pointer_size);
6667 printf (_("(base address)\n"));
6668 break;
6670 case DW_LLE_start_end:
6671 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6672 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6673 break;
6675 case DW_LLE_start_length:
6676 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6677 READ_ULEB (end, start, section_end);
6678 end += begin;
6679 break;
6681 #ifdef DW_LLE_view_pair
6682 case DW_LLE_view_pair:
6683 if (vstart)
6684 printf (_("View pair entry in loclist with locviews attribute\n"));
6685 READ_ULEB (vbegin, start, section_end);
6686 print_dwarf_view (vbegin, pointer_size, 1);
6688 READ_ULEB (vend, start, section_end);
6689 print_dwarf_view (vend, pointer_size, 1);
6691 printf (_("views for:\n"));
6692 continue;
6693 #endif
6695 default:
6696 error (_("Invalid location list entry type %d\n"), llet);
6697 return;
6700 if (llet == DW_LLE_end_of_list)
6701 break;
6703 if (llet == DW_LLE_base_address
6704 || llet == DW_LLE_base_addressx)
6705 continue;
6707 if (start == section_end)
6709 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6710 (unsigned long) offset);
6711 break;
6713 READ_ULEB (length, start, section_end);
6715 if (length > (size_t) (section_end - start))
6717 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6718 (unsigned long) offset);
6719 break;
6722 print_dwarf_vma (begin, pointer_size);
6723 print_dwarf_vma (end, pointer_size);
6725 putchar ('(');
6726 need_frame_base = decode_location_expression (start,
6727 pointer_size,
6728 offset_size,
6729 dwarf_version,
6730 length,
6731 cu_offset, section);
6732 putchar (')');
6734 if (need_frame_base && !has_frame_base)
6735 printf (_(" [without DW_AT_frame_base]"));
6737 if (begin == end && vbegin == vend)
6738 fputs (_(" (start == end)"), stdout);
6739 else if (begin > end || (begin == end && vbegin > vend))
6740 fputs (_(" (start > end)"), stdout);
6742 putchar ('\n');
6744 start += length;
6745 vbegin = vend = -1;
6748 if (vbegin != vm1 || vend != vm1)
6749 printf (_("Trailing view pair not used in a range"));
6751 *start_ptr = start;
6752 *vstart_ptr = vstart;
6755 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
6756 right-adjusted in a field of length LEN, and followed by a space. */
6758 static void
6759 print_addr_index (unsigned int idx, unsigned int len)
6761 static char buf[15];
6762 snprintf (buf, sizeof (buf), "[%d]", idx);
6763 printf ("%*s ", len, buf);
6766 /* Display a location list from a .dwo section. It uses address indexes rather
6767 than embedded addresses. This code closely follows display_loc_list, but the
6768 two are sufficiently different that combining things is very ugly. */
6770 static void
6771 display_loc_list_dwo (struct dwarf_section *section,
6772 unsigned char **start_ptr,
6773 unsigned int debug_info_entry,
6774 dwarf_vma offset,
6775 unsigned char **vstart_ptr,
6776 int has_frame_base)
6778 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6779 unsigned char *section_end = section->start + section->size;
6780 dwarf_vma cu_offset;
6781 unsigned int pointer_size;
6782 unsigned int offset_size;
6783 int dwarf_version;
6784 int entry_type;
6785 unsigned short length;
6786 int need_frame_base;
6787 unsigned int idx;
6789 if (debug_info_entry >= num_debug_info_entries)
6791 warn (_("No debug information for loc lists of entry: %u\n"),
6792 debug_info_entry);
6793 return;
6796 cu_offset = debug_information [debug_info_entry].cu_offset;
6797 pointer_size = debug_information [debug_info_entry].pointer_size;
6798 offset_size = debug_information [debug_info_entry].offset_size;
6799 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6801 if (pointer_size < 2 || pointer_size > 8)
6803 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6804 pointer_size, debug_info_entry);
6805 return;
6808 while (1)
6810 printf (" ");
6811 print_dwarf_vma (offset + (start - *start_ptr), 4);
6813 if (start >= section_end)
6815 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6816 (unsigned long) offset);
6817 break;
6820 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
6822 if (vstart)
6823 switch (entry_type)
6825 default:
6826 break;
6828 case 2:
6829 case 3:
6830 case 4:
6832 dwarf_vma view;
6833 dwarf_vma off = offset + (vstart - *start_ptr);
6835 READ_ULEB (view, vstart, section_end);
6836 print_dwarf_view (view, 8, 1);
6838 READ_ULEB (view, vstart, section_end);
6839 print_dwarf_view (view, 8, 1);
6841 printf (_("views at %8.8lx for:\n %*s "),
6842 (unsigned long) off, 8, "");
6845 break;
6848 switch (entry_type)
6850 case 0: /* A terminating entry. */
6851 *start_ptr = start;
6852 *vstart_ptr = vstart;
6853 printf (_("<End of list>\n"));
6854 return;
6855 case 1: /* A base-address entry. */
6856 READ_ULEB (idx, start, section_end);
6857 print_addr_index (idx, 8);
6858 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
6859 printf (_("(base address selection entry)\n"));
6860 continue;
6861 case 2: /* A start/end entry. */
6862 READ_ULEB (idx, start, section_end);
6863 print_addr_index (idx, 8);
6864 READ_ULEB (idx, start, section_end);
6865 print_addr_index (idx, 8);
6866 break;
6867 case 3: /* A start/length entry. */
6868 READ_ULEB (idx, start, section_end);
6869 print_addr_index (idx, 8);
6870 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6871 printf ("%08x ", idx);
6872 break;
6873 case 4: /* An offset pair entry. */
6874 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6875 printf ("%08x ", idx);
6876 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6877 printf ("%08x ", idx);
6878 break;
6879 default:
6880 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
6881 *start_ptr = start;
6882 *vstart_ptr = vstart;
6883 return;
6886 if (2 > (size_t) (section_end - start))
6888 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6889 (unsigned long) offset);
6890 break;
6893 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6894 if (length > (size_t) (section_end - start))
6896 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6897 (unsigned long) offset);
6898 break;
6901 putchar ('(');
6902 need_frame_base = decode_location_expression (start,
6903 pointer_size,
6904 offset_size,
6905 dwarf_version,
6906 length,
6907 cu_offset, section);
6908 putchar (')');
6910 if (need_frame_base && !has_frame_base)
6911 printf (_(" [without DW_AT_frame_base]"));
6913 putchar ('\n');
6915 start += length;
6918 *start_ptr = start;
6919 *vstart_ptr = vstart;
6922 /* Sort array of indexes in ascending order of loc_offsets[idx] and
6923 loc_views. */
6925 static dwarf_vma *loc_offsets, *loc_views;
6927 static int
6928 loc_offsets_compar (const void *ap, const void *bp)
6930 dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
6931 dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
6933 int ret = (a > b) - (b > a);
6934 if (ret)
6935 return ret;
6937 a = loc_views[*(const unsigned int *) ap];
6938 b = loc_views[*(const unsigned int *) bp];
6940 ret = (a > b) - (b > a);
6942 return ret;
6945 static int
6946 display_offset_entry_loclists (struct dwarf_section *section)
6948 unsigned char * start = section->start;
6949 unsigned char * const end = start + section->size;
6951 introduce (section, false);
6955 dwarf_vma length;
6956 unsigned short version;
6957 unsigned char address_size;
6958 unsigned char segment_selector_size;
6959 uint32_t offset_entry_count;
6960 uint32_t i;
6961 bool is_64bit;
6963 printf (_("Table at Offset 0x%lx\n"), (long)(start - section->start));
6965 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
6966 if (length == 0xffffffff)
6968 is_64bit = true;
6969 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
6971 else
6972 is_64bit = false;
6974 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
6975 SAFE_BYTE_GET_AND_INC (address_size, start, 1, end);
6976 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, end);
6977 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, end);
6979 printf (_(" Length: 0x%s\n"), dwarf_vmatoa ("x", length));
6980 printf (_(" DWARF version: %u\n"), version);
6981 printf (_(" Address size: %u\n"), address_size);
6982 printf (_(" Segment size: %u\n"), segment_selector_size);
6983 printf (_(" Offset entries: %u\n"), offset_entry_count);
6985 if (version < 5)
6987 warn (_("The %s section contains a corrupt or "
6988 "unsupported version number: %d.\n"),
6989 section->name, version);
6990 return 0;
6993 if (segment_selector_size != 0)
6995 warn (_("The %s section contains an "
6996 "unsupported segment selector size: %d.\n"),
6997 section->name, segment_selector_size);
6998 return 0;
7001 if (offset_entry_count == 0)
7003 warn (_("The %s section contains a table without offset\n"),
7004 section->name);
7005 return 0;
7008 printf (_("\n Offset Entries starting at 0x%lx:\n"),
7009 (long)(start - section->start));
7011 if (is_64bit)
7013 for (i = 0; i < offset_entry_count; i++)
7015 dwarf_vma entry;
7017 SAFE_BYTE_GET_AND_INC (entry, start, 8, end);
7018 printf (_(" [%6u] 0x%s\n"), i, dwarf_vmatoa ("x", entry));
7021 else
7023 for (i = 0; i < offset_entry_count; i++)
7025 uint32_t entry;
7027 SAFE_BYTE_GET_AND_INC (entry, start, 4, end);
7028 printf (_(" [%6u] 0x%x\n"), i, entry);
7032 putchar ('\n');
7034 uint32_t j;
7036 for (j = 1, i = 0; i < offset_entry_count;)
7038 unsigned char lle;
7039 dwarf_vma base_address = 0;
7040 dwarf_vma begin;
7041 dwarf_vma finish;
7042 dwarf_vma off = start - section->start;
7044 if (j != i)
7046 printf (_(" Offset Entry %u\n"), i);
7047 j = i;
7050 printf (" ");
7051 print_dwarf_vma (off, 4);
7053 SAFE_BYTE_GET_AND_INC (lle, start, 1, end);
7055 switch (lle)
7057 case DW_LLE_end_of_list:
7058 printf (_("<End of list>\n\n"));
7059 i ++;
7060 continue;
7062 case DW_LLE_base_addressx:
7063 READ_ULEB (base_address, start, end);
7064 print_dwarf_vma (base_address, address_size);
7065 printf (_("(index into .debug_addr) "));
7066 base_address = fetch_indexed_addr (base_address, address_size);
7067 print_dwarf_vma (base_address, address_size);
7068 printf (_("(base address)\n"));
7069 continue;
7071 case DW_LLE_startx_endx:
7072 READ_ULEB (begin, start, end);
7073 begin = fetch_indexed_addr (begin, address_size);
7074 READ_ULEB (finish, start, end);
7075 finish = fetch_indexed_addr (finish, address_size);
7076 break;
7078 case DW_LLE_startx_length:
7079 READ_ULEB (begin, start, end);
7080 begin = fetch_indexed_addr (begin, address_size);
7081 READ_ULEB (finish, start, end);
7082 finish += begin;
7083 break;
7085 case DW_LLE_offset_pair:
7086 READ_ULEB (begin, start, end);
7087 begin += base_address;
7088 READ_ULEB (finish, start, end);
7089 finish += base_address;
7090 break;
7092 case DW_LLE_default_location:
7093 begin = finish = 0;
7094 break;
7096 case DW_LLE_base_address:
7097 SAFE_BYTE_GET_AND_INC (base_address, start, address_size, end);
7098 print_dwarf_vma (base_address, address_size);
7099 printf (_("(base address)\n"));
7100 continue;
7102 case DW_LLE_start_end:
7103 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7104 SAFE_BYTE_GET_AND_INC (finish, start, address_size, end);
7105 break;
7107 case DW_LLE_start_length:
7108 SAFE_BYTE_GET_AND_INC (begin, start, address_size, end);
7109 READ_ULEB (finish, start, end);
7110 finish += begin;
7111 break;
7113 default:
7114 error (_("Invalid location list entry type %d\n"), lle);
7115 return 0;
7118 if (start == end)
7120 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
7121 (unsigned long) off);
7122 break;
7125 print_dwarf_vma (begin, address_size);
7126 print_dwarf_vma (finish, address_size);
7128 if (begin == finish)
7129 fputs (_(" (start == end)"), stdout);
7130 else if (begin > finish)
7131 fputs (_(" (start > end)"), stdout);
7133 /* Read the counted location descriptions. */
7134 READ_ULEB (length, start, end);
7136 if (length > (size_t) (end - start))
7138 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
7139 (unsigned long) off);
7140 break;
7143 putchar (' ');
7144 (void) decode_location_expression (start, address_size, address_size,
7145 version, length, 0, section);
7146 start += length;
7147 putchar ('\n');
7150 putchar ('\n');
7152 while (start < end);
7154 return 1;
7157 static int
7158 display_debug_loc (struct dwarf_section *section, void *file)
7160 unsigned char *start = section->start, *vstart = NULL;
7161 dwarf_vma bytes;
7162 unsigned char *section_begin = start;
7163 unsigned int num_loc_list = 0;
7164 dwarf_vma last_offset = 0;
7165 dwarf_vma last_view = 0;
7166 unsigned int first = 0;
7167 unsigned int i;
7168 unsigned int j;
7169 int seen_first_offset = 0;
7170 int locs_sorted = 1;
7171 unsigned char *next = start, *vnext = vstart;
7172 unsigned int *array = NULL;
7173 const char *suffix = strrchr (section->name, '.');
7174 bool is_dwo = false;
7175 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
7176 dwarf_vma expected_start = 0;
7178 if (suffix && strcmp (suffix, ".dwo") == 0)
7179 is_dwo = true;
7181 bytes = section->size;
7183 if (bytes == 0)
7185 printf (_("\nThe %s section is empty.\n"), section->name);
7186 return 0;
7189 if (is_loclists)
7191 unsigned char *hdrptr = section_begin;
7192 dwarf_vma ll_length;
7193 unsigned short ll_version;
7194 unsigned char *end = section_begin + section->size;
7195 unsigned char address_size, segment_selector_size;
7196 uint32_t offset_entry_count;
7198 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
7199 if (ll_length == 0xffffffff)
7200 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
7202 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
7203 if (ll_version != 5)
7205 warn (_("The %s section contains corrupt or "
7206 "unsupported version number: %d.\n"),
7207 section->name, ll_version);
7208 return 0;
7211 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
7213 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
7214 if (segment_selector_size != 0)
7216 warn (_("The %s section contains "
7217 "unsupported segment selector size: %d.\n"),
7218 section->name, segment_selector_size);
7219 return 0;
7222 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
7224 if (offset_entry_count != 0)
7225 return display_offset_entry_loclists (section);
7227 expected_start = hdrptr - section_begin;
7230 if (load_debug_info (file) == 0)
7232 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7233 section->name);
7234 return 0;
7237 /* Check the order of location list in .debug_info section. If
7238 offsets of location lists are in the ascending order, we can
7239 use `debug_information' directly. */
7240 for (i = 0; i < num_debug_info_entries; i++)
7242 unsigned int num;
7244 num = debug_information [i].num_loc_offsets;
7245 if (num > num_loc_list)
7246 num_loc_list = num;
7248 /* Check if we can use `debug_information' directly. */
7249 if (locs_sorted && num != 0)
7251 if (!seen_first_offset)
7253 /* This is the first location list. */
7254 last_offset = debug_information [i].loc_offsets [0];
7255 last_view = debug_information [i].loc_views [0];
7256 first = i;
7257 seen_first_offset = 1;
7258 j = 1;
7260 else
7261 j = 0;
7263 for (; j < num; j++)
7265 if (last_offset >
7266 debug_information [i].loc_offsets [j]
7267 || (last_offset == debug_information [i].loc_offsets [j]
7268 && last_view > debug_information [i].loc_views [j]))
7270 locs_sorted = 0;
7271 break;
7273 last_offset = debug_information [i].loc_offsets [j];
7274 last_view = debug_information [i].loc_views [j];
7279 if (!seen_first_offset)
7280 error (_("No location lists in .debug_info section!\n"));
7282 if (debug_information [first].num_loc_offsets > 0
7283 && debug_information [first].loc_offsets [0] != expected_start
7284 && debug_information [first].loc_views [0] != expected_start)
7285 warn (_("Location lists in %s section start at 0x%s rather than 0x%s\n"),
7286 section->name,
7287 dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]),
7288 dwarf_vmatoa ("x", expected_start));
7290 if (!locs_sorted)
7291 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
7293 introduce (section, false);
7295 if (reloc_at (section, 0))
7296 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
7298 printf (_(" Offset Begin End Expression\n"));
7300 seen_first_offset = 0;
7301 for (i = first; i < num_debug_info_entries; i++)
7303 dwarf_vma offset, voffset;
7304 dwarf_vma base_address;
7305 unsigned int k;
7306 int has_frame_base;
7308 if (!locs_sorted)
7310 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7311 array[k] = k;
7312 loc_offsets = debug_information [i].loc_offsets;
7313 loc_views = debug_information [i].loc_views;
7314 qsort (array, debug_information [i].num_loc_offsets,
7315 sizeof (*array), loc_offsets_compar);
7318 int adjacent_view_loclists = 1;
7319 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
7321 j = locs_sorted ? k : array[k];
7322 if (k
7323 && (debug_information [i].loc_offsets [locs_sorted
7324 ? k - 1 : array [k - 1]]
7325 == debug_information [i].loc_offsets [j])
7326 && (debug_information [i].loc_views [locs_sorted
7327 ? k - 1 : array [k - 1]]
7328 == debug_information [i].loc_views [j]))
7329 continue;
7330 has_frame_base = debug_information [i].have_frame_base [j];
7331 offset = debug_information [i].loc_offsets [j];
7332 next = section_begin + offset;
7333 voffset = debug_information [i].loc_views [j];
7334 if (voffset != vm1)
7335 vnext = section_begin + voffset;
7336 else
7337 vnext = NULL;
7338 base_address = debug_information [i].base_address;
7340 if (vnext && vnext < next)
7342 vstart = vnext;
7343 display_view_pair_list (section, &vstart, i, next);
7344 if (start == vnext)
7345 start = vstart;
7348 if (!seen_first_offset || !adjacent_view_loclists)
7349 seen_first_offset = 1;
7350 else
7352 if (start < next)
7353 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
7354 (unsigned long) (start - section_begin),
7355 (unsigned long) offset,
7356 section->name);
7357 else if (start > next)
7358 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
7359 (unsigned long) (start - section_begin),
7360 (unsigned long) offset,
7361 section->name);
7363 start = next;
7364 vstart = vnext;
7366 if (offset >= bytes)
7368 warn (_("Offset 0x%lx is bigger than %s section size.\n"),
7369 (unsigned long) offset,
7370 section->name);
7371 continue;
7374 if (vnext && voffset >= bytes)
7376 warn (_("View Offset 0x%lx is bigger than %s section size.\n"),
7377 (unsigned long) voffset,
7378 section->name);
7379 continue;
7382 if (!is_loclists)
7384 if (is_dwo)
7385 display_loc_list_dwo (section, &start, i, offset,
7386 &vstart, has_frame_base);
7387 else
7388 display_loc_list (section, &start, i, offset, base_address,
7389 &vstart, has_frame_base);
7391 else
7393 if (is_dwo)
7394 warn (_("DWO is not yet supported.\n"));
7395 else
7396 display_loclists_list (section, &start, i, offset, base_address,
7397 &vstart, has_frame_base);
7400 /* FIXME: this arrangement is quite simplistic. Nothing
7401 requires locview lists to be adjacent to corresponding
7402 loclists, and a single loclist could be augmented by
7403 different locview lists, and vice-versa, unlikely as it
7404 is that it would make sense to do so. Hopefully we'll
7405 have view pair support built into loclists before we ever
7406 need to address all these possibilities. */
7407 if (adjacent_view_loclists && vnext
7408 && vnext != start && vstart != next)
7410 adjacent_view_loclists = 0;
7411 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
7414 if (vnext && vnext == start)
7415 display_view_pair_list (section, &start, i, vstart);
7419 if (start < section->start + section->size)
7420 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7421 "There are %ld unused bytes at the end of section %s\n",
7422 (long) (section->start + section->size - start)),
7423 (long) (section->start + section->size - start), section->name);
7424 putchar ('\n');
7425 free (array);
7426 return 1;
7429 static int
7430 display_debug_str (struct dwarf_section *section,
7431 void *file ATTRIBUTE_UNUSED)
7433 unsigned char *start = section->start;
7434 dwarf_vma bytes = section->size;
7435 dwarf_vma addr = section->address;
7437 if (bytes == 0)
7439 printf (_("\nThe %s section is empty.\n"), section->name);
7440 return 0;
7443 introduce (section, false);
7445 while (bytes)
7447 int j;
7448 int k;
7449 int lbytes;
7451 lbytes = (bytes > 16 ? 16 : bytes);
7453 printf (" 0x%8.8lx ", (unsigned long) addr);
7455 for (j = 0; j < 16; j++)
7457 if (j < lbytes)
7458 printf ("%2.2x", start[j]);
7459 else
7460 printf (" ");
7462 if ((j & 3) == 3)
7463 printf (" ");
7466 for (j = 0; j < lbytes; j++)
7468 k = start[j];
7469 if (k >= ' ' && k < 0x80)
7470 printf ("%c", k);
7471 else
7472 printf (".");
7475 putchar ('\n');
7477 start += lbytes;
7478 addr += lbytes;
7479 bytes -= lbytes;
7482 putchar ('\n');
7484 return 1;
7487 static int
7488 display_debug_info (struct dwarf_section *section, void *file)
7490 return process_debug_info (section, file, section->abbrev_sec, false, false);
7493 static int
7494 display_debug_types (struct dwarf_section *section, void *file)
7496 return process_debug_info (section, file, section->abbrev_sec, false, true);
7499 static int
7500 display_trace_info (struct dwarf_section *section, void *file)
7502 return process_debug_info (section, file, section->abbrev_sec, false, true);
7505 static int
7506 display_debug_aranges (struct dwarf_section *section,
7507 void *file ATTRIBUTE_UNUSED)
7509 unsigned char *start = section->start;
7510 unsigned char *end = start + section->size;
7512 introduce (section, false);
7514 /* It does not matter if this load fails,
7515 we test for that later on. */
7516 load_debug_info (file);
7518 while (start < end)
7520 unsigned char *hdrptr;
7521 DWARF2_Internal_ARange arange;
7522 unsigned char *addr_ranges;
7523 dwarf_vma length;
7524 dwarf_vma address;
7525 dwarf_vma sec_off;
7526 unsigned char address_size;
7527 unsigned int offset_size;
7528 unsigned char *end_ranges;
7530 hdrptr = start;
7531 sec_off = hdrptr - section->start;
7533 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
7534 if (arange.ar_length == 0xffffffff)
7536 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
7537 offset_size = 8;
7539 else
7540 offset_size = 4;
7542 if (arange.ar_length > (size_t) (end - hdrptr))
7544 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
7545 section->name,
7546 (unsigned long) sec_off,
7547 dwarf_vmatoa ("x", arange.ar_length));
7548 break;
7550 end_ranges = hdrptr + arange.ar_length;
7552 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end_ranges);
7553 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size,
7554 end_ranges);
7556 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7557 && num_debug_info_entries > 0
7558 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7559 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
7560 (unsigned long) arange.ar_info_offset, section->name);
7562 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end_ranges);
7563 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end_ranges);
7565 if (arange.ar_version != 2 && arange.ar_version != 3)
7567 /* PR 19872: A version number of 0 probably means that there is
7568 padding at the end of the .debug_aranges section. Gold puts
7569 it there when performing an incremental link, for example.
7570 So do not generate a warning in this case. */
7571 if (arange.ar_version)
7572 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
7573 break;
7576 printf (_(" Length: %ld\n"),
7577 (long) arange.ar_length);
7578 printf (_(" Version: %d\n"), arange.ar_version);
7579 printf (_(" Offset into .debug_info: 0x%lx\n"),
7580 (unsigned long) arange.ar_info_offset);
7581 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
7582 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
7584 address_size = arange.ar_pointer_size + arange.ar_segment_size;
7586 /* PR 17512: file: 001-108546-0.001:0.1. */
7587 if (address_size == 0 || address_size > 8)
7589 error (_("Invalid address size in %s section!\n"),
7590 section->name);
7591 break;
7594 /* The DWARF spec does not require that the address size be a power
7595 of two, but we do. This will have to change if we ever encounter
7596 an uneven architecture. */
7597 if ((address_size & (address_size - 1)) != 0)
7599 warn (_("Pointer size + Segment size is not a power of two.\n"));
7600 break;
7603 if (address_size > 4)
7604 printf (_("\n Address Length\n"));
7605 else
7606 printf (_("\n Address Length\n"));
7608 addr_ranges = hdrptr;
7610 /* Must pad to an alignment boundary that is twice the address size. */
7611 addr_ranges += (2 * address_size - 1
7612 - (hdrptr - start - 1) % (2 * address_size));
7614 while (2 * address_size <= end_ranges - addr_ranges)
7616 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size,
7617 end_ranges);
7618 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size,
7619 end_ranges);
7620 printf (" ");
7621 print_dwarf_vma (address, address_size);
7622 print_dwarf_vma (length, address_size);
7623 putchar ('\n');
7626 start = end_ranges;
7629 printf ("\n");
7631 return 1;
7634 /* Comparison function for qsort. */
7635 static int
7636 comp_addr_base (const void * v0, const void * v1)
7638 debug_info *info0 = *(debug_info **) v0;
7639 debug_info *info1 = *(debug_info **) v1;
7640 return info0->addr_base - info1->addr_base;
7643 /* Display the debug_addr section. */
7644 static int
7645 display_debug_addr (struct dwarf_section *section,
7646 void *file)
7648 debug_info **debug_addr_info;
7649 unsigned char *entry;
7650 unsigned char *end;
7651 unsigned int i;
7652 unsigned int count;
7653 unsigned char * header;
7655 if (section->size == 0)
7657 printf (_("\nThe %s section is empty.\n"), section->name);
7658 return 0;
7661 if (load_debug_info (file) == 0)
7663 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7664 section->name);
7665 return 0;
7668 introduce (section, false);
7670 /* PR 17531: file: cf38d01b.
7671 We use xcalloc because a corrupt file may not have initialised all of the
7672 fields in the debug_info structure, which means that the sort below might
7673 try to move uninitialised data. */
7674 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
7675 sizeof (debug_info *));
7677 count = 0;
7678 for (i = 0; i < num_debug_info_entries; i++)
7679 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
7681 /* PR 17531: file: cf38d01b. */
7682 if (debug_information[i].addr_base >= section->size)
7683 warn (_("Corrupt address base (%lx) found in debug section %u\n"),
7684 (unsigned long) debug_information[i].addr_base, i);
7685 else
7686 debug_addr_info [count++] = debug_information + i;
7689 /* Add a sentinel to make iteration convenient. */
7690 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7691 debug_addr_info [count]->addr_base = section->size;
7692 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
7694 header = section->start;
7695 for (i = 0; i < count; i++)
7697 unsigned int idx;
7698 unsigned int address_size = debug_addr_info [i]->pointer_size;
7700 printf (_(" For compilation unit at offset 0x%s:\n"),
7701 dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
7703 printf (_("\tIndex\tAddress\n"));
7704 entry = section->start + debug_addr_info [i]->addr_base;
7705 if (debug_addr_info [i]->dwarf_version >= 5)
7707 size_t header_size = entry - header;
7708 unsigned char * curr_header = header;
7709 dwarf_vma length;
7710 int version;
7711 int segment_selector_size;
7713 if (header_size != 8 && header_size != 16)
7715 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %ld instead\n"),
7716 section->name, (long) header_size);
7717 return 0;
7720 SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7721 if (length == 0xffffffff)
7722 SAFE_BYTE_GET_AND_INC (length, curr_header, 8, entry);
7723 end = curr_header + length;
7725 SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7726 if (version != 5)
7727 warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7728 section->name, version);
7730 SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7731 SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7732 address_size += segment_selector_size;
7734 else
7735 end = section->start + debug_addr_info [i + 1]->addr_base;
7736 header = end;
7737 idx = 0;
7738 while (entry < end)
7740 dwarf_vma base = byte_get (entry, address_size);
7741 printf (_("\t%d:\t"), idx);
7742 print_dwarf_vma (base, address_size);
7743 printf ("\n");
7744 entry += address_size;
7745 idx++;
7748 printf ("\n");
7750 free (debug_addr_info);
7751 return 1;
7754 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
7756 static int
7757 display_debug_str_offsets (struct dwarf_section *section,
7758 void *file ATTRIBUTE_UNUSED)
7760 unsigned long idx;
7762 if (section->size == 0)
7764 printf (_("\nThe %s section is empty.\n"), section->name);
7765 return 0;
7768 unsigned char *start = section->start;
7769 unsigned char *end = start + section->size;
7770 unsigned char *curr = start;
7772 const char *suffix = strrchr (section->name, '.');
7773 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
7775 if (dwo)
7776 load_debug_section_with_follow (str_dwo, file);
7777 else
7778 load_debug_section_with_follow (str, file);
7780 introduce (section, false);
7782 while (curr < end)
7784 dwarf_vma length;
7785 dwarf_vma entry_length;
7787 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
7788 /* FIXME: We assume that this means 64-bit DWARF is being used. */
7789 if (length == 0xffffffff)
7791 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
7792 entry_length = 8;
7794 else
7795 entry_length = 4;
7797 unsigned char *entries_end;
7798 if (length == 0)
7800 /* This is probably an old style .debug_str_offset section which
7801 just contains offsets and no header (and the first offset is 0). */
7802 length = section->size;
7803 curr = section->start;
7804 entries_end = end;
7806 printf (_(" Length: %#lx\n"), (unsigned long) length);
7807 printf (_(" Index Offset [String]\n"));
7809 else
7811 if (length <= (dwarf_vma) (end - curr))
7812 entries_end = curr + length;
7813 else
7815 warn (_("Section %s is too small %#lx\n"),
7816 section->name, (unsigned long) section->size);
7817 entries_end = end;
7820 int version;
7821 SAFE_BYTE_GET_AND_INC (version, curr, 2, entries_end);
7822 if (version != 5)
7823 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
7825 int padding;
7826 SAFE_BYTE_GET_AND_INC (padding, curr, 2, entries_end);
7827 if (padding != 0)
7828 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
7830 printf (_(" Length: %#lx\n"), (unsigned long) length);
7831 printf (_(" Version: %#lx\n"), (unsigned long) version);
7832 printf (_(" Index Offset [String]\n"));
7835 for (idx = 0; curr < entries_end; idx++)
7837 dwarf_vma offset;
7838 const unsigned char * string;
7840 if ((dwarf_vma) (entries_end - curr) < entry_length)
7841 /* Not enough space to read one entry_length, give up. */
7842 return 0;
7844 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, entries_end);
7845 if (dwo)
7846 string = (const unsigned char *)
7847 fetch_indexed_string (idx, NULL, entry_length, dwo);
7848 else
7849 string = fetch_indirect_string (offset);
7851 printf (" %8lu %8s %s\n", idx, dwarf_vmatoa ("x", offset),
7852 string);
7856 return 1;
7859 /* Each debug_information[x].range_lists[y] gets this representation for
7860 sorting purposes. */
7862 struct range_entry
7864 /* The debug_information[x].range_lists[y] value. */
7865 dwarf_vma ranges_offset;
7867 /* Original debug_information to find parameters of the data. */
7868 debug_info *debug_info_p;
7871 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
7873 static int
7874 range_entry_compar (const void *ap, const void *bp)
7876 const struct range_entry *a_re = (const struct range_entry *) ap;
7877 const struct range_entry *b_re = (const struct range_entry *) bp;
7878 const dwarf_vma a = a_re->ranges_offset;
7879 const dwarf_vma b = b_re->ranges_offset;
7881 return (a > b) - (b > a);
7884 static void
7885 display_debug_ranges_list (unsigned char * start,
7886 unsigned char * finish,
7887 unsigned int pointer_size,
7888 dwarf_vma offset,
7889 dwarf_vma base_address)
7891 while (start < finish)
7893 dwarf_vma begin;
7894 dwarf_vma end;
7896 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7897 if (start >= finish)
7898 break;
7899 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7901 printf (" ");
7902 print_dwarf_vma (offset, 4);
7904 if (begin == 0 && end == 0)
7906 printf (_("<End of list>\n"));
7907 break;
7910 /* Check base address specifiers. */
7911 if (is_max_address (begin, pointer_size)
7912 && !is_max_address (end, pointer_size))
7914 base_address = end;
7915 print_dwarf_vma (begin, pointer_size);
7916 print_dwarf_vma (end, pointer_size);
7917 printf ("(base address)\n");
7918 continue;
7921 print_dwarf_vma (begin + base_address, pointer_size);
7922 print_dwarf_vma (end + base_address, pointer_size);
7924 if (begin == end)
7925 fputs (_("(start == end)"), stdout);
7926 else if (begin > end)
7927 fputs (_("(start > end)"), stdout);
7929 putchar ('\n');
7933 static void
7934 display_debug_rnglists_list (unsigned char * start,
7935 unsigned char * finish,
7936 unsigned int pointer_size,
7937 dwarf_vma offset,
7938 dwarf_vma base_address)
7940 unsigned char *next = start;
7942 while (1)
7944 dwarf_vma off = offset + (start - next);
7945 enum dwarf_range_list_entry rlet;
7946 /* Initialize it due to a false compiler warning. */
7947 dwarf_vma begin = -1, length, end = -1;
7949 if (start >= finish)
7951 warn (_("Range list starting at offset 0x%s is not terminated.\n"),
7952 dwarf_vmatoa ("x", offset));
7953 break;
7956 printf (" ");
7957 print_dwarf_vma (off, 4);
7959 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
7961 switch (rlet)
7963 case DW_RLE_end_of_list:
7964 printf (_("<End of list>\n"));
7965 break;
7966 case DW_RLE_base_addressx:
7967 READ_ULEB (base_address, start, finish);
7968 print_dwarf_vma (base_address, pointer_size);
7969 printf (_("(base address index) "));
7970 base_address = fetch_indexed_addr (base_address, pointer_size);
7971 print_dwarf_vma (base_address, pointer_size);
7972 printf (_("(base address)\n"));
7973 break;
7974 case DW_RLE_startx_endx:
7975 READ_ULEB (begin, start, finish);
7976 READ_ULEB (end, start, finish);
7977 begin = fetch_indexed_addr (begin, pointer_size);
7978 end = fetch_indexed_addr (begin, pointer_size);
7979 break;
7980 case DW_RLE_startx_length:
7981 READ_ULEB (begin, start, finish);
7982 READ_ULEB (length, start, finish);
7983 begin = fetch_indexed_addr (begin, pointer_size);
7984 end = begin + length;
7985 break;
7986 case DW_RLE_offset_pair:
7987 READ_ULEB (begin, start, finish);
7988 READ_ULEB (end, start, finish);
7989 break;
7990 case DW_RLE_base_address:
7991 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
7992 print_dwarf_vma (base_address, pointer_size);
7993 printf (_("(base address)\n"));
7994 break;
7995 case DW_RLE_start_end:
7996 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7997 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7998 break;
7999 case DW_RLE_start_length:
8000 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
8001 READ_ULEB (length, start, finish);
8002 end = begin + length;
8003 break;
8004 default:
8005 error (_("Invalid range list entry type %d\n"), rlet);
8006 rlet = DW_RLE_end_of_list;
8007 break;
8009 if (rlet == DW_RLE_end_of_list)
8010 break;
8011 if (rlet == DW_RLE_base_address || rlet == DW_RLE_base_addressx)
8012 continue;
8014 /* Only a DW_RLE_offset_pair needs the base address added. */
8015 if (rlet == DW_RLE_offset_pair)
8017 begin += base_address;
8018 end += base_address;
8021 print_dwarf_vma (begin, pointer_size);
8022 print_dwarf_vma (end, pointer_size);
8024 if (begin == end)
8025 fputs (_("(start == end)"), stdout);
8026 else if (begin > end)
8027 fputs (_("(start > end)"), stdout);
8029 putchar ('\n');
8033 static int
8034 display_debug_ranges (struct dwarf_section *section,
8035 void *file ATTRIBUTE_UNUSED)
8037 unsigned char * start = section->start;
8038 unsigned char * last_start = start;
8039 dwarf_vma bytes = section->size;
8040 unsigned char * section_begin = start;
8041 unsigned char * finish = start + bytes;
8042 unsigned int num_range_list, i;
8043 struct range_entry * range_entries;
8044 struct range_entry * range_entry_fill;
8045 int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
8046 /* Initialize it due to a false compiler warning. */
8047 unsigned char address_size = 0;
8048 dwarf_vma last_offset = 0;
8050 if (bytes == 0)
8052 printf (_("\nThe %s section is empty.\n"), section->name);
8053 return 0;
8056 introduce (section, false);
8058 if (is_rnglists)
8060 dwarf_vma initial_length;
8061 unsigned char segment_selector_size;
8062 unsigned int offset_size, offset_entry_count;
8063 unsigned short version;
8065 /* Get and check the length of the block. */
8066 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
8068 if (initial_length == 0xffffffff)
8070 /* This section is 64-bit DWARF 3. */
8071 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
8072 offset_size = 8;
8074 else
8075 offset_size = 4;
8077 if (initial_length > (size_t) (finish - start))
8079 /* If the length field has a relocation against it, then we should
8080 not complain if it is inaccurate (and probably negative).
8081 It is copied from .debug_line handling code. */
8082 if (reloc_at (section, (start - section->start) - offset_size))
8084 initial_length = finish - start;
8086 else
8088 warn (_("The length field (0x%lx) in the debug_rnglists header is wrong - the section is too small\n"),
8089 (long) initial_length);
8090 return 0;
8094 /* Get the other fields in the header. */
8095 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
8096 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
8097 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
8098 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
8100 printf (_(" Length: 0x%s\n"), dwarf_vmatoa ("x", initial_length));
8101 printf (_(" DWARF version: %u\n"), version);
8102 printf (_(" Address size: %u\n"), address_size);
8103 printf (_(" Segment size: %u\n"), segment_selector_size);
8104 printf (_(" Offset entries: %u\n"), offset_entry_count);
8106 /* Check the fields. */
8107 if (segment_selector_size != 0)
8109 warn (_("The %s section contains "
8110 "unsupported segment selector size: %d.\n"),
8111 section->name, segment_selector_size);
8112 return 0;
8115 if (version < 5)
8117 warn (_("Only DWARF version 5+ debug_rnglists info "
8118 "is currently supported.\n"));
8119 return 0;
8122 if (offset_entry_count != 0)
8124 printf (_("\n Offsets starting at 0x%lx:\n"), (long)(start - section->start));
8125 if (offset_size == 8)
8127 for (i = 0; i < offset_entry_count; i++)
8129 dwarf_vma entry;
8131 SAFE_BYTE_GET_AND_INC (entry, start, 8, finish);
8132 printf (_(" [%6u] 0x%s\n"), i, dwarf_vmatoa ("x", entry));
8135 else
8137 for (i = 0; i < offset_entry_count; i++)
8139 uint32_t entry;
8141 SAFE_BYTE_GET_AND_INC (entry, start, 4, finish);
8142 printf (_(" [%6u] 0x%x\n"), i, entry);
8148 if (load_debug_info (file) == 0)
8150 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
8151 section->name);
8152 return 0;
8155 num_range_list = 0;
8156 for (i = 0; i < num_debug_info_entries; i++)
8158 if (debug_information [i].dwarf_version < 5 && is_rnglists)
8159 /* Skip .debug_rnglists reference. */
8160 continue;
8161 if (debug_information [i].dwarf_version >= 5 && !is_rnglists)
8162 /* Skip .debug_range reference. */
8163 continue;
8164 num_range_list += debug_information [i].num_range_lists;
8167 if (num_range_list == 0)
8169 /* This can happen when the file was compiled with -gsplit-debug
8170 which removes references to range lists from the primary .o file. */
8171 printf (_("No range lists in .debug_info section.\n"));
8172 return 1;
8175 range_entries = (struct range_entry *)
8176 xmalloc (sizeof (*range_entries) * num_range_list);
8177 range_entry_fill = range_entries;
8179 for (i = 0; i < num_debug_info_entries; i++)
8181 debug_info *debug_info_p = &debug_information[i];
8182 unsigned int j;
8184 if (debug_information [i].dwarf_version < 5 && is_rnglists)
8185 /* Skip .debug_rnglists reference. */
8186 continue;
8187 if (debug_information [i].dwarf_version >= 5 && !is_rnglists)
8188 /* Skip .debug_range reference. */
8189 continue;
8191 for (j = 0; j < debug_info_p->num_range_lists; j++)
8193 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
8194 range_entry_fill->debug_info_p = debug_info_p;
8195 range_entry_fill++;
8199 qsort (range_entries, num_range_list, sizeof (*range_entries),
8200 range_entry_compar);
8202 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
8203 warn (_("Range lists in %s section start at 0x%lx\n"),
8204 section->name, (unsigned long) range_entries[0].ranges_offset);
8206 putchar ('\n');
8207 printf (_(" Offset Begin End\n"));
8209 for (i = 0; i < num_range_list; i++)
8211 struct range_entry *range_entry = &range_entries[i];
8212 debug_info *debug_info_p = range_entry->debug_info_p;
8213 unsigned int pointer_size;
8214 dwarf_vma offset;
8215 unsigned char *next;
8216 dwarf_vma base_address;
8218 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
8219 offset = range_entry->ranges_offset;
8220 base_address = debug_info_p->base_address;
8222 /* PR 17512: file: 001-101485-0.001:0.1. */
8223 if (pointer_size < 2 || pointer_size > 8)
8225 warn (_("Corrupt pointer size (%d) in debug entry at offset %8.8lx\n"),
8226 pointer_size, (unsigned long) offset);
8227 continue;
8230 if (offset > (size_t) (finish - section_begin))
8232 warn (_("Corrupt offset (%#8.8lx) in range entry %u\n"),
8233 (unsigned long) offset, i);
8234 continue;
8236 next = section_begin + offset;
8238 /* If multiple DWARF entities reference the same range then we will
8239 have multiple entries in the `range_entries' list for the same
8240 offset. Thanks to the sort above these will all be consecutive in
8241 the `range_entries' list, so we can easily ignore duplicates
8242 here. */
8243 if (i > 0 && last_offset == offset)
8244 continue;
8245 last_offset = offset;
8247 if (dwarf_check != 0 && i > 0)
8249 if (start < next)
8250 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
8251 (unsigned long) (start - section_begin),
8252 (unsigned long) (next - section_begin), section->name);
8253 else if (start > next)
8255 if (next == last_start)
8256 continue;
8257 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
8258 (unsigned long) (start - section_begin),
8259 (unsigned long) (next - section_begin), section->name);
8263 start = next;
8264 last_start = next;
8266 if (is_rnglists)
8267 display_debug_rnglists_list
8268 (start, finish, pointer_size, offset, base_address);
8269 else
8270 display_debug_ranges_list
8271 (start, finish, pointer_size, offset, base_address);
8273 putchar ('\n');
8275 free (range_entries);
8277 return 1;
8280 typedef struct Frame_Chunk
8282 struct Frame_Chunk *next;
8283 unsigned char *chunk_start;
8284 unsigned int ncols;
8285 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8286 short int *col_type;
8287 int *col_offset;
8288 char *augmentation;
8289 unsigned int code_factor;
8290 int data_factor;
8291 dwarf_vma pc_begin;
8292 dwarf_vma pc_range;
8293 unsigned int cfa_reg;
8294 dwarf_vma cfa_offset;
8295 unsigned int ra;
8296 unsigned char fde_encoding;
8297 unsigned char cfa_exp;
8298 unsigned char ptr_size;
8299 unsigned char segment_size;
8301 Frame_Chunk;
8303 typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
8304 static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
8305 static const char *const *dwarf_regnames;
8306 static unsigned int dwarf_regnames_count;
8309 /* A marker for a col_type that means this column was never referenced
8310 in the frame info. */
8311 #define DW_CFA_unreferenced (-1)
8313 /* Return 0 if no more space is needed, 1 if more space is needed,
8314 -1 for invalid reg. */
8316 static int
8317 frame_need_space (Frame_Chunk *fc, unsigned int reg)
8319 unsigned int prev = fc->ncols;
8321 if (reg < (unsigned int) fc->ncols)
8322 return 0;
8324 if (dwarf_regnames_count > 0
8325 && reg > dwarf_regnames_count)
8326 return -1;
8328 fc->ncols = reg + 1;
8329 /* PR 17512: file: 10450-2643-0.004.
8330 If reg == -1 then this can happen... */
8331 if (fc->ncols == 0)
8332 return -1;
8334 /* PR 17512: file: 2844a11d. */
8335 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
8337 error (_("Unfeasibly large register number: %u\n"), reg);
8338 fc->ncols = 0;
8339 /* FIXME: 1024 is an arbitrary limit. Increase it if
8340 we ever encounter a valid binary that exceeds it. */
8341 return -1;
8344 fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
8345 sizeof (short int));
8346 fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
8347 /* PR 17512: file:002-10025-0.005. */
8348 if (fc->col_type == NULL || fc->col_offset == NULL)
8350 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
8351 fc->ncols);
8352 fc->ncols = 0;
8353 return -1;
8356 while (prev < fc->ncols)
8358 fc->col_type[prev] = DW_CFA_unreferenced;
8359 fc->col_offset[prev] = 0;
8360 prev++;
8362 return 1;
8365 static const char *const dwarf_regnames_i386[] =
8367 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8368 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8369 "eip", "eflags", NULL, /* 8 - 10 */
8370 "st0", "st1", "st2", "st3", /* 11 - 14 */
8371 "st4", "st5", "st6", "st7", /* 15 - 18 */
8372 NULL, NULL, /* 19 - 20 */
8373 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
8374 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
8375 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
8376 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
8377 "fcw", "fsw", "mxcsr", /* 37 - 39 */
8378 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8379 "tr", "ldtr", /* 48 - 49 */
8380 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8381 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8382 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8383 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8384 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8385 NULL, NULL, NULL, /* 90 - 92 */
8386 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
8389 static const char *const dwarf_regnames_iamcu[] =
8391 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8392 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8393 "eip", "eflags", NULL, /* 8 - 10 */
8394 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
8395 NULL, NULL, /* 19 - 20 */
8396 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
8397 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
8398 NULL, NULL, NULL, /* 37 - 39 */
8399 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8400 "tr", "ldtr", /* 48 - 49 */
8401 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8402 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8403 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8404 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8405 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8406 NULL, NULL, NULL, /* 90 - 92 */
8407 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
8410 static void
8411 init_dwarf_regnames_i386 (void)
8413 dwarf_regnames = dwarf_regnames_i386;
8414 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
8415 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8418 static void
8419 init_dwarf_regnames_iamcu (void)
8421 dwarf_regnames = dwarf_regnames_iamcu;
8422 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
8423 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8426 static const char *const dwarf_regnames_x86_64[] =
8428 "rax", "rdx", "rcx", "rbx",
8429 "rsi", "rdi", "rbp", "rsp",
8430 "r8", "r9", "r10", "r11",
8431 "r12", "r13", "r14", "r15",
8432 "rip",
8433 "xmm0", "xmm1", "xmm2", "xmm3",
8434 "xmm4", "xmm5", "xmm6", "xmm7",
8435 "xmm8", "xmm9", "xmm10", "xmm11",
8436 "xmm12", "xmm13", "xmm14", "xmm15",
8437 "st0", "st1", "st2", "st3",
8438 "st4", "st5", "st6", "st7",
8439 "mm0", "mm1", "mm2", "mm3",
8440 "mm4", "mm5", "mm6", "mm7",
8441 "rflags",
8442 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8443 "fs.base", "gs.base", NULL, NULL,
8444 "tr", "ldtr",
8445 "mxcsr", "fcw", "fsw",
8446 "xmm16", "xmm17", "xmm18", "xmm19",
8447 "xmm20", "xmm21", "xmm22", "xmm23",
8448 "xmm24", "xmm25", "xmm26", "xmm27",
8449 "xmm28", "xmm29", "xmm30", "xmm31",
8450 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
8451 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
8452 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
8453 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
8454 NULL, NULL, NULL, /* 115 - 117 */
8455 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
8458 static void
8459 init_dwarf_regnames_x86_64 (void)
8461 dwarf_regnames = dwarf_regnames_x86_64;
8462 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
8463 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8466 static const char *const dwarf_regnames_aarch64[] =
8468 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
8469 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
8470 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8471 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8472 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
8473 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
8474 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
8475 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
8476 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
8477 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
8478 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8479 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
8480 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
8481 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8482 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8483 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
8486 static void
8487 init_dwarf_regnames_aarch64 (void)
8489 dwarf_regnames = dwarf_regnames_aarch64;
8490 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
8491 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8494 static const char *const dwarf_regnames_s390[] =
8496 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
8497 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8498 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8499 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
8500 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
8501 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8502 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8503 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
8504 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
8505 "pswm", "pswa",
8506 NULL, NULL,
8507 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8508 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8511 static void
8512 init_dwarf_regnames_s390 (void)
8514 dwarf_regnames = dwarf_regnames_s390;
8515 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
8516 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8519 static const char *const dwarf_regnames_riscv[] =
8521 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
8522 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
8523 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
8524 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
8525 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
8526 "fs0", "fs1", /* 40 - 41 */
8527 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
8528 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
8529 "fs10", "fs11", /* 58 - 59 */
8530 "ft8", "ft9", "ft10", "ft11" /* 60 - 63 */
8533 /* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8534 the large number of CSRs. */
8536 static const char *
8537 regname_internal_riscv (unsigned int regno)
8539 const char *name = NULL;
8541 /* Lookup in the table first, this covers GPR and FPR. */
8542 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8543 name = dwarf_regnames_riscv [regno];
8544 else if (regno >= 4096 && regno <= 8191)
8546 /* This might be a CSR, these live in a sparse number space from 4096
8547 to 8191 These numbers are defined in the RISC-V ELF ABI
8548 document. */
8549 switch (regno)
8551 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8552 case VALUE + 4096: name = #NAME; break;
8553 #include "opcode/riscv-opc.h"
8554 #undef DECLARE_CSR
8556 default:
8558 static char csr_name[10];
8559 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8560 name = csr_name;
8562 break;
8566 return name;
8569 static void
8570 init_dwarf_regnames_riscv (void)
8572 dwarf_regnames = NULL;
8573 dwarf_regnames_count = 8192;
8574 dwarf_regnames_lookup_func = regname_internal_riscv;
8577 void
8578 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
8580 dwarf_regnames_lookup_func = NULL;
8582 switch (e_machine)
8584 case EM_386:
8585 init_dwarf_regnames_i386 ();
8586 break;
8588 case EM_IAMCU:
8589 init_dwarf_regnames_iamcu ();
8590 break;
8592 case EM_X86_64:
8593 case EM_L1OM:
8594 case EM_K1OM:
8595 init_dwarf_regnames_x86_64 ();
8596 break;
8598 case EM_AARCH64:
8599 init_dwarf_regnames_aarch64 ();
8600 break;
8602 case EM_S390:
8603 init_dwarf_regnames_s390 ();
8604 break;
8606 case EM_RISCV:
8607 init_dwarf_regnames_riscv ();
8608 break;
8610 default:
8611 break;
8615 /* Initialize the DWARF register name lookup state based on the
8616 architecture and specific machine type of a BFD. */
8618 void
8619 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8620 unsigned long mach)
8622 dwarf_regnames_lookup_func = NULL;
8624 switch (arch)
8626 case bfd_arch_i386:
8627 switch (mach)
8629 case bfd_mach_x86_64:
8630 case bfd_mach_x86_64_intel_syntax:
8631 case bfd_mach_x64_32:
8632 case bfd_mach_x64_32_intel_syntax:
8633 init_dwarf_regnames_x86_64 ();
8634 break;
8636 default:
8637 init_dwarf_regnames_i386 ();
8638 break;
8640 break;
8642 case bfd_arch_iamcu:
8643 init_dwarf_regnames_iamcu ();
8644 break;
8646 case bfd_arch_aarch64:
8647 init_dwarf_regnames_aarch64();
8648 break;
8650 case bfd_arch_s390:
8651 init_dwarf_regnames_s390 ();
8652 break;
8654 case bfd_arch_riscv:
8655 init_dwarf_regnames_riscv ();
8656 break;
8658 default:
8659 break;
8663 static const char *
8664 regname_internal_by_table_only (unsigned int regno)
8666 if (dwarf_regnames != NULL
8667 && regno < dwarf_regnames_count
8668 && dwarf_regnames [regno] != NULL)
8669 return dwarf_regnames [regno];
8671 return NULL;
8674 static const char *
8675 regname (unsigned int regno, int name_only_p)
8677 static char reg[64];
8679 const char *name = NULL;
8681 if (dwarf_regnames_lookup_func != NULL)
8682 name = dwarf_regnames_lookup_func (regno);
8684 if (name != NULL)
8686 if (name_only_p)
8687 return name;
8688 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
8690 else
8691 snprintf (reg, sizeof (reg), "r%d", regno);
8692 return reg;
8695 static void
8696 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
8698 unsigned int r;
8699 char tmp[100];
8701 if (*max_regs != fc->ncols)
8702 *max_regs = fc->ncols;
8704 if (*need_col_headers)
8706 *need_col_headers = 0;
8708 printf ("%-*s CFA ", eh_addr_size * 2, " LOC");
8710 for (r = 0; r < *max_regs; r++)
8711 if (fc->col_type[r] != DW_CFA_unreferenced)
8713 if (r == fc->ra)
8714 printf ("ra ");
8715 else
8716 printf ("%-5s ", regname (r, 1));
8719 printf ("\n");
8722 print_dwarf_vma (fc->pc_begin, eh_addr_size);
8723 if (fc->cfa_exp)
8724 strcpy (tmp, "exp");
8725 else
8726 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
8727 printf ("%-8s ", tmp);
8729 for (r = 0; r < fc->ncols; r++)
8731 if (fc->col_type[r] != DW_CFA_unreferenced)
8733 switch (fc->col_type[r])
8735 case DW_CFA_undefined:
8736 strcpy (tmp, "u");
8737 break;
8738 case DW_CFA_same_value:
8739 strcpy (tmp, "s");
8740 break;
8741 case DW_CFA_offset:
8742 sprintf (tmp, "c%+d", fc->col_offset[r]);
8743 break;
8744 case DW_CFA_val_offset:
8745 sprintf (tmp, "v%+d", fc->col_offset[r]);
8746 break;
8747 case DW_CFA_register:
8748 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
8749 break;
8750 case DW_CFA_expression:
8751 strcpy (tmp, "exp");
8752 break;
8753 case DW_CFA_val_expression:
8754 strcpy (tmp, "vexp");
8755 break;
8756 default:
8757 strcpy (tmp, "n/a");
8758 break;
8760 printf ("%-5s ", tmp);
8763 printf ("\n");
8766 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
8768 static unsigned char *
8769 read_cie (unsigned char *start, unsigned char *end,
8770 Frame_Chunk **p_cie, int *p_version,
8771 bfd_size_type *p_aug_len, unsigned char **p_aug)
8773 int version;
8774 Frame_Chunk *fc;
8775 unsigned char *augmentation_data = NULL;
8776 bfd_size_type augmentation_data_len = 0;
8778 * p_cie = NULL;
8779 /* PR 17512: file: 001-228113-0.004. */
8780 if (start >= end)
8781 return end;
8783 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8784 memset (fc, 0, sizeof (Frame_Chunk));
8786 fc->col_type = (short int *) xmalloc (sizeof (short int));
8787 fc->col_offset = (int *) xmalloc (sizeof (int));
8789 version = *start++;
8791 fc->augmentation = (char *) start;
8792 /* PR 17512: file: 001-228113-0.004.
8793 Skip past augmentation name, but avoid running off the end of the data. */
8794 while (start < end)
8795 if (* start ++ == '\0')
8796 break;
8797 if (start == end)
8799 warn (_("No terminator for augmentation name\n"));
8800 goto fail;
8803 if (strcmp (fc->augmentation, "eh") == 0)
8805 if (eh_addr_size > (size_t) (end - start))
8806 goto fail;
8807 start += eh_addr_size;
8810 if (version >= 4)
8812 if (2 > (size_t) (end - start))
8813 goto fail;
8814 GET (fc->ptr_size, 1);
8815 if (fc->ptr_size < 1 || fc->ptr_size > 8)
8817 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
8818 goto fail;
8821 GET (fc->segment_size, 1);
8822 /* PR 17512: file: e99d2804. */
8823 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
8825 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
8826 goto fail;
8829 eh_addr_size = fc->ptr_size;
8831 else
8833 fc->ptr_size = eh_addr_size;
8834 fc->segment_size = 0;
8837 READ_ULEB (fc->code_factor, start, end);
8838 READ_SLEB (fc->data_factor, start, end);
8840 if (start >= end)
8841 goto fail;
8843 if (version == 1)
8845 GET (fc->ra, 1);
8847 else
8849 READ_ULEB (fc->ra, start, end);
8852 if (fc->augmentation[0] == 'z')
8854 if (start >= end)
8855 goto fail;
8856 READ_ULEB (augmentation_data_len, start, end);
8857 augmentation_data = start;
8858 /* PR 17512: file: 11042-2589-0.004. */
8859 if (augmentation_data_len > (bfd_size_type) (end - start))
8861 warn (_("Augmentation data too long: 0x%s, expected at most %#lx\n"),
8862 dwarf_vmatoa ("x", augmentation_data_len),
8863 (unsigned long) (end - start));
8864 goto fail;
8866 start += augmentation_data_len;
8869 if (augmentation_data_len)
8871 unsigned char *p;
8872 unsigned char *q;
8873 unsigned char *qend;
8875 p = (unsigned char *) fc->augmentation + 1;
8876 q = augmentation_data;
8877 qend = q + augmentation_data_len;
8879 while (p < end && q < qend)
8881 if (*p == 'L')
8882 q++;
8883 else if (*p == 'P')
8884 q += 1 + size_of_encoded_value (*q);
8885 else if (*p == 'R')
8886 fc->fde_encoding = *q++;
8887 else if (*p == 'S')
8889 else if (*p == 'B')
8891 else
8892 break;
8893 p++;
8895 /* Note - it is OK if this loop terminates with q < qend.
8896 Padding may have been inserted to align the end of the CIE. */
8899 *p_cie = fc;
8900 if (p_version)
8901 *p_version = version;
8902 if (p_aug_len)
8904 *p_aug_len = augmentation_data_len;
8905 *p_aug = augmentation_data;
8907 return start;
8909 fail:
8910 free (fc->col_offset);
8911 free (fc->col_type);
8912 free (fc);
8913 return end;
8916 /* Prints out the contents on the DATA array formatted as unsigned bytes.
8917 If do_wide is not enabled, then formats the output to fit into 80 columns.
8918 PRINTED contains the number of characters already written to the current
8919 output line. */
8921 static void
8922 display_data (bfd_size_type printed,
8923 const unsigned char * data,
8924 const bfd_size_type len)
8926 if (do_wide || len < ((80 - printed) / 3))
8927 for (printed = 0; printed < len; ++printed)
8928 printf (" %02x", data[printed]);
8929 else
8931 for (printed = 0; printed < len; ++printed)
8933 if (printed % (80 / 3) == 0)
8934 putchar ('\n');
8935 printf (" %02x", data[printed]);
8940 /* Prints out the contents on the augmentation data array.
8941 If do_wide is not enabled, then formats the output to fit into 80 columns. */
8943 static void
8944 display_augmentation_data (const unsigned char * data, const bfd_size_type len)
8946 bfd_size_type i;
8948 i = printf (_(" Augmentation data: "));
8949 display_data (i, data, len);
8952 static int
8953 display_debug_frames (struct dwarf_section *section,
8954 void *file ATTRIBUTE_UNUSED)
8956 unsigned char *start = section->start;
8957 unsigned char *end = start + section->size;
8958 unsigned char *section_start = start;
8959 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
8960 Frame_Chunk *remembered_state = NULL;
8961 Frame_Chunk *rs;
8962 bool is_eh = strcmp (section->name, ".eh_frame") == 0;
8963 unsigned int max_regs = 0;
8964 const char *bad_reg = _("bad register: ");
8965 unsigned int saved_eh_addr_size = eh_addr_size;
8967 introduce (section, false);
8969 while (start < end)
8971 unsigned char *saved_start;
8972 unsigned char *block_end;
8973 dwarf_vma length;
8974 dwarf_vma cie_id;
8975 Frame_Chunk *fc;
8976 Frame_Chunk *cie;
8977 int need_col_headers = 1;
8978 unsigned char *augmentation_data = NULL;
8979 bfd_size_type augmentation_data_len = 0;
8980 unsigned int encoded_ptr_size = saved_eh_addr_size;
8981 unsigned int offset_size;
8982 bool all_nops;
8983 static Frame_Chunk fde_fc;
8985 saved_start = start;
8987 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
8989 if (length == 0)
8991 printf ("\n%08lx ZERO terminator\n\n",
8992 (unsigned long)(saved_start - section_start));
8993 /* Skip any zero terminators that directly follow.
8994 A corrupt section size could have loaded a whole
8995 slew of zero filled memory bytes. eg
8996 PR 17512: file: 070-19381-0.004. */
8997 while (start < end && * start == 0)
8998 ++ start;
8999 continue;
9002 if (length == 0xffffffff)
9004 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
9005 offset_size = 8;
9007 else
9008 offset_size = 4;
9010 if (length > (size_t) (end - start))
9012 warn ("Invalid length 0x%s in FDE at %#08lx\n",
9013 dwarf_vmatoa_1 (NULL, length, offset_size),
9014 (unsigned long) (saved_start - section_start));
9015 block_end = end;
9017 else
9018 block_end = start + length;
9020 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, block_end);
9022 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
9023 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
9025 int version;
9026 unsigned int mreg;
9028 start = read_cie (start, block_end, &cie, &version,
9029 &augmentation_data_len, &augmentation_data);
9030 /* PR 17512: file: 027-135133-0.005. */
9031 if (cie == NULL)
9032 break;
9034 fc = cie;
9035 fc->next = chunks;
9036 chunks = fc;
9037 fc->chunk_start = saved_start;
9038 mreg = max_regs > 0 ? max_regs - 1 : 0;
9039 if (mreg < fc->ra)
9040 mreg = fc->ra;
9041 if (frame_need_space (fc, mreg) < 0)
9042 break;
9043 if (fc->fde_encoding)
9044 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9046 printf ("\n%08lx ", (unsigned long) (saved_start - section_start));
9047 print_dwarf_vma (length, fc->ptr_size);
9048 print_dwarf_vma (cie_id, offset_size);
9050 if (do_debug_frames_interp)
9052 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
9053 fc->code_factor, fc->data_factor, fc->ra);
9055 else
9057 printf ("CIE\n");
9058 printf (" Version: %d\n", version);
9059 printf (" Augmentation: \"%s\"\n", fc->augmentation);
9060 if (version >= 4)
9062 printf (" Pointer Size: %u\n", fc->ptr_size);
9063 printf (" Segment Size: %u\n", fc->segment_size);
9065 printf (" Code alignment factor: %u\n", fc->code_factor);
9066 printf (" Data alignment factor: %d\n", fc->data_factor);
9067 printf (" Return address column: %d\n", fc->ra);
9069 if (augmentation_data_len)
9070 display_augmentation_data (augmentation_data, augmentation_data_len);
9072 putchar ('\n');
9075 else
9077 unsigned char *look_for;
9078 unsigned long segment_selector;
9079 dwarf_vma cie_off;
9081 cie_off = cie_id;
9082 if (is_eh)
9084 dwarf_vma sign = (dwarf_vma) 1 << (offset_size * 8 - 1);
9085 cie_off = (cie_off ^ sign) - sign;
9086 cie_off = start - 4 - section_start - cie_off;
9089 look_for = section_start + cie_off;
9090 if (cie_off <= (dwarf_vma) (saved_start - section_start))
9092 for (cie = chunks; cie ; cie = cie->next)
9093 if (cie->chunk_start == look_for)
9094 break;
9096 else if (cie_off >= section->size)
9097 cie = NULL;
9098 else
9100 for (cie = forward_refs; cie ; cie = cie->next)
9101 if (cie->chunk_start == look_for)
9102 break;
9103 if (!cie)
9105 unsigned int off_size;
9106 unsigned char *cie_scan;
9108 cie_scan = look_for;
9109 off_size = 4;
9110 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
9111 if (length == 0xffffffff)
9113 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
9114 off_size = 8;
9116 if (length != 0 && length <= (size_t) (end - cie_scan))
9118 dwarf_vma c_id;
9119 unsigned char *cie_end = cie_scan + length;
9121 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size,
9122 cie_end);
9123 if (is_eh
9124 ? c_id == 0
9125 : ((off_size == 4 && c_id == DW_CIE_ID)
9126 || (off_size == 8 && c_id == DW64_CIE_ID)))
9128 int version;
9129 unsigned int mreg;
9131 read_cie (cie_scan, cie_end, &cie, &version,
9132 &augmentation_data_len, &augmentation_data);
9133 /* PR 17512: file: 3450-2098-0.004. */
9134 if (cie == NULL)
9136 warn (_("Failed to read CIE information\n"));
9137 break;
9139 cie->next = forward_refs;
9140 forward_refs = cie;
9141 cie->chunk_start = look_for;
9142 mreg = max_regs > 0 ? max_regs - 1 : 0;
9143 if (mreg < cie->ra)
9144 mreg = cie->ra;
9145 if (frame_need_space (cie, mreg) < 0)
9147 warn (_("Invalid max register\n"));
9148 break;
9150 if (cie->fde_encoding)
9151 encoded_ptr_size
9152 = size_of_encoded_value (cie->fde_encoding);
9158 fc = &fde_fc;
9159 memset (fc, 0, sizeof (Frame_Chunk));
9161 if (!cie)
9163 fc->ncols = 0;
9164 fc->col_type = (short int *) xmalloc (sizeof (short int));
9165 fc->col_offset = (int *) xmalloc (sizeof (int));
9166 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
9168 warn (_("Invalid max register\n"));
9169 break;
9171 cie = fc;
9172 fc->augmentation = "";
9173 fc->fde_encoding = 0;
9174 fc->ptr_size = eh_addr_size;
9175 fc->segment_size = 0;
9177 else
9179 fc->ncols = cie->ncols;
9180 fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
9181 fc->col_offset = (int *) xcmalloc (fc->ncols, sizeof (int));
9182 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
9183 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
9184 fc->augmentation = cie->augmentation;
9185 fc->ptr_size = cie->ptr_size;
9186 eh_addr_size = cie->ptr_size;
9187 fc->segment_size = cie->segment_size;
9188 fc->code_factor = cie->code_factor;
9189 fc->data_factor = cie->data_factor;
9190 fc->cfa_reg = cie->cfa_reg;
9191 fc->cfa_offset = cie->cfa_offset;
9192 fc->ra = cie->ra;
9193 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
9195 warn (_("Invalid max register\n"));
9196 break;
9198 fc->fde_encoding = cie->fde_encoding;
9201 if (fc->fde_encoding)
9202 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
9204 segment_selector = 0;
9205 if (fc->segment_size)
9207 if (fc->segment_size > sizeof (segment_selector))
9209 /* PR 17512: file: 9e196b3e. */
9210 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
9211 fc->segment_size = 4;
9213 SAFE_BYTE_GET_AND_INC (segment_selector, start,
9214 fc->segment_size, block_end);
9217 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section,
9218 block_end);
9220 /* FIXME: It appears that sometimes the final pc_range value is
9221 encoded in less than encoded_ptr_size bytes. See the x86_64
9222 run of the "objcopy on compressed debug sections" test for an
9223 example of this. */
9224 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size,
9225 block_end);
9227 if (cie->augmentation[0] == 'z')
9229 READ_ULEB (augmentation_data_len, start, block_end);
9230 augmentation_data = start;
9231 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
9232 if (augmentation_data_len > (bfd_size_type) (block_end - start))
9234 warn (_("Augmentation data too long: 0x%s, "
9235 "expected at most %#lx\n"),
9236 dwarf_vmatoa ("x", augmentation_data_len),
9237 (unsigned long) (block_end - start));
9238 start = block_end;
9239 augmentation_data = NULL;
9240 augmentation_data_len = 0;
9242 start += augmentation_data_len;
9245 printf ("\n%08lx %s %s FDE ",
9246 (unsigned long)(saved_start - section_start),
9247 dwarf_vmatoa_1 (NULL, length, fc->ptr_size),
9248 dwarf_vmatoa_1 (NULL, cie_id, offset_size));
9250 if (cie->chunk_start)
9251 printf ("cie=%08lx",
9252 (unsigned long) (cie->chunk_start - section_start));
9253 else
9254 /* Ideally translate "invalid " to 8 chars, trailing space
9255 is optional. */
9256 printf (_("cie=invalid "));
9258 printf (" pc=");
9259 if (fc->segment_size)
9260 printf ("%04lx:", segment_selector);
9262 printf ("%s..%s\n",
9263 dwarf_vmatoa_1 (NULL, fc->pc_begin, fc->ptr_size),
9264 dwarf_vmatoa_1 (NULL, fc->pc_begin + fc->pc_range, fc->ptr_size));
9266 if (! do_debug_frames_interp && augmentation_data_len)
9268 display_augmentation_data (augmentation_data, augmentation_data_len);
9269 putchar ('\n');
9273 /* At this point, fc is the current chunk, cie (if any) is set, and
9274 we're about to interpret instructions for the chunk. */
9275 /* ??? At present we need to do this always, since this sizes the
9276 fc->col_type and fc->col_offset arrays, which we write into always.
9277 We should probably split the interpreted and non-interpreted bits
9278 into two different routines, since there's so much that doesn't
9279 really overlap between them. */
9280 if (1 || do_debug_frames_interp)
9282 /* Start by making a pass over the chunk, allocating storage
9283 and taking note of what registers are used. */
9284 unsigned char *tmp = start;
9286 while (start < block_end)
9288 unsigned int reg, op, opa;
9289 unsigned long temp;
9291 op = *start++;
9292 opa = op & 0x3f;
9293 if (op & 0xc0)
9294 op &= 0xc0;
9296 /* Warning: if you add any more cases to this switch, be
9297 sure to add them to the corresponding switch below. */
9298 switch (op)
9300 case DW_CFA_advance_loc:
9301 break;
9302 case DW_CFA_offset:
9303 SKIP_ULEB (start, block_end);
9304 if (frame_need_space (fc, opa) >= 0)
9305 fc->col_type[opa] = DW_CFA_undefined;
9306 break;
9307 case DW_CFA_restore:
9308 if (frame_need_space (fc, opa) >= 0)
9309 fc->col_type[opa] = DW_CFA_undefined;
9310 break;
9311 case DW_CFA_set_loc:
9312 if ((size_t) (block_end - start) < encoded_ptr_size)
9313 start = block_end;
9314 else
9315 start += encoded_ptr_size;
9316 break;
9317 case DW_CFA_advance_loc1:
9318 if ((size_t) (block_end - start) < 1)
9319 start = block_end;
9320 else
9321 start += 1;
9322 break;
9323 case DW_CFA_advance_loc2:
9324 if ((size_t) (block_end - start) < 2)
9325 start = block_end;
9326 else
9327 start += 2;
9328 break;
9329 case DW_CFA_advance_loc4:
9330 if ((size_t) (block_end - start) < 4)
9331 start = block_end;
9332 else
9333 start += 4;
9334 break;
9335 case DW_CFA_offset_extended:
9336 case DW_CFA_val_offset:
9337 READ_ULEB (reg, start, block_end);
9338 SKIP_ULEB (start, block_end);
9339 if (frame_need_space (fc, reg) >= 0)
9340 fc->col_type[reg] = DW_CFA_undefined;
9341 break;
9342 case DW_CFA_restore_extended:
9343 READ_ULEB (reg, start, block_end);
9344 if (frame_need_space (fc, reg) >= 0)
9345 fc->col_type[reg] = DW_CFA_undefined;
9346 break;
9347 case DW_CFA_undefined:
9348 READ_ULEB (reg, start, block_end);
9349 if (frame_need_space (fc, reg) >= 0)
9350 fc->col_type[reg] = DW_CFA_undefined;
9351 break;
9352 case DW_CFA_same_value:
9353 READ_ULEB (reg, start, block_end);
9354 if (frame_need_space (fc, reg) >= 0)
9355 fc->col_type[reg] = DW_CFA_undefined;
9356 break;
9357 case DW_CFA_register:
9358 READ_ULEB (reg, start, block_end);
9359 SKIP_ULEB (start, block_end);
9360 if (frame_need_space (fc, reg) >= 0)
9361 fc->col_type[reg] = DW_CFA_undefined;
9362 break;
9363 case DW_CFA_def_cfa:
9364 SKIP_ULEB (start, block_end);
9365 SKIP_ULEB (start, block_end);
9366 break;
9367 case DW_CFA_def_cfa_register:
9368 SKIP_ULEB (start, block_end);
9369 break;
9370 case DW_CFA_def_cfa_offset:
9371 SKIP_ULEB (start, block_end);
9372 break;
9373 case DW_CFA_def_cfa_expression:
9374 READ_ULEB (temp, start, block_end);
9375 if ((size_t) (block_end - start) < temp)
9376 start = block_end;
9377 else
9378 start += temp;
9379 break;
9380 case DW_CFA_expression:
9381 case DW_CFA_val_expression:
9382 READ_ULEB (reg, start, block_end);
9383 READ_ULEB (temp, start, block_end);
9384 if ((size_t) (block_end - start) < temp)
9385 start = block_end;
9386 else
9387 start += temp;
9388 if (frame_need_space (fc, reg) >= 0)
9389 fc->col_type[reg] = DW_CFA_undefined;
9390 break;
9391 case DW_CFA_offset_extended_sf:
9392 case DW_CFA_val_offset_sf:
9393 READ_ULEB (reg, start, block_end);
9394 SKIP_SLEB (start, block_end);
9395 if (frame_need_space (fc, reg) >= 0)
9396 fc->col_type[reg] = DW_CFA_undefined;
9397 break;
9398 case DW_CFA_def_cfa_sf:
9399 SKIP_ULEB (start, block_end);
9400 SKIP_SLEB (start, block_end);
9401 break;
9402 case DW_CFA_def_cfa_offset_sf:
9403 SKIP_SLEB (start, block_end);
9404 break;
9405 case DW_CFA_MIPS_advance_loc8:
9406 if ((size_t) (block_end - start) < 8)
9407 start = block_end;
9408 else
9409 start += 8;
9410 break;
9411 case DW_CFA_GNU_args_size:
9412 SKIP_ULEB (start, block_end);
9413 break;
9414 case DW_CFA_GNU_negative_offset_extended:
9415 READ_ULEB (reg, start, block_end);
9416 SKIP_ULEB (start, block_end);
9417 if (frame_need_space (fc, reg) >= 0)
9418 fc->col_type[reg] = DW_CFA_undefined;
9419 break;
9420 default:
9421 break;
9424 start = tmp;
9427 all_nops = true;
9429 /* Now we know what registers are used, make a second pass over
9430 the chunk, this time actually printing out the info. */
9432 while (start < block_end)
9434 unsigned op, opa;
9435 unsigned long ul, roffs;
9436 /* Note: It is tempting to use an unsigned long for 'reg' but there
9437 are various functions, notably frame_space_needed() that assume that
9438 reg is an unsigned int. */
9439 unsigned int reg;
9440 dwarf_signed_vma l;
9441 dwarf_vma ofs;
9442 dwarf_vma vma;
9443 const char *reg_prefix = "";
9445 op = *start++;
9446 opa = op & 0x3f;
9447 if (op & 0xc0)
9448 op &= 0xc0;
9450 /* Make a note if something other than DW_CFA_nop happens. */
9451 if (op != DW_CFA_nop)
9452 all_nops = false;
9454 /* Warning: if you add any more cases to this switch, be
9455 sure to add them to the corresponding switch above. */
9456 switch (op)
9458 case DW_CFA_advance_loc:
9459 if (do_debug_frames_interp)
9460 frame_display_row (fc, &need_col_headers, &max_regs);
9461 else
9462 printf (" DW_CFA_advance_loc: %d to %s\n",
9463 opa * fc->code_factor,
9464 dwarf_vmatoa_1 (NULL,
9465 fc->pc_begin + opa * fc->code_factor,
9466 fc->ptr_size));
9467 fc->pc_begin += opa * fc->code_factor;
9468 break;
9470 case DW_CFA_offset:
9471 READ_ULEB (roffs, start, block_end);
9472 if (opa >= (unsigned int) fc->ncols)
9473 reg_prefix = bad_reg;
9474 if (! do_debug_frames_interp || *reg_prefix != '\0')
9475 printf (" DW_CFA_offset: %s%s at cfa%+ld\n",
9476 reg_prefix, regname (opa, 0),
9477 roffs * fc->data_factor);
9478 if (*reg_prefix == '\0')
9480 fc->col_type[opa] = DW_CFA_offset;
9481 fc->col_offset[opa] = roffs * fc->data_factor;
9483 break;
9485 case DW_CFA_restore:
9486 if (opa >= (unsigned int) fc->ncols)
9487 reg_prefix = bad_reg;
9488 if (! do_debug_frames_interp || *reg_prefix != '\0')
9489 printf (" DW_CFA_restore: %s%s\n",
9490 reg_prefix, regname (opa, 0));
9491 if (*reg_prefix != '\0')
9492 break;
9494 if (opa >= (unsigned int) cie->ncols
9495 || (do_debug_frames_interp
9496 && cie->col_type[opa] == DW_CFA_unreferenced))
9498 fc->col_type[opa] = DW_CFA_undefined;
9499 fc->col_offset[opa] = 0;
9501 else
9503 fc->col_type[opa] = cie->col_type[opa];
9504 fc->col_offset[opa] = cie->col_offset[opa];
9506 break;
9508 case DW_CFA_set_loc:
9509 vma = get_encoded_value (&start, fc->fde_encoding, section,
9510 block_end);
9511 if (do_debug_frames_interp)
9512 frame_display_row (fc, &need_col_headers, &max_regs);
9513 else
9514 printf (" DW_CFA_set_loc: %s\n",
9515 dwarf_vmatoa_1 (NULL, vma, fc->ptr_size));
9516 fc->pc_begin = vma;
9517 break;
9519 case DW_CFA_advance_loc1:
9520 SAFE_BYTE_GET_AND_INC (ofs, start, 1, block_end);
9521 if (do_debug_frames_interp)
9522 frame_display_row (fc, &need_col_headers, &max_regs);
9523 else
9524 printf (" DW_CFA_advance_loc1: %ld to %s\n",
9525 (unsigned long) (ofs * fc->code_factor),
9526 dwarf_vmatoa_1 (NULL,
9527 fc->pc_begin + ofs * fc->code_factor,
9528 fc->ptr_size));
9529 fc->pc_begin += ofs * fc->code_factor;
9530 break;
9532 case DW_CFA_advance_loc2:
9533 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
9534 if (do_debug_frames_interp)
9535 frame_display_row (fc, &need_col_headers, &max_regs);
9536 else
9537 printf (" DW_CFA_advance_loc2: %ld to %s\n",
9538 (unsigned long) (ofs * fc->code_factor),
9539 dwarf_vmatoa_1 (NULL,
9540 fc->pc_begin + ofs * fc->code_factor,
9541 fc->ptr_size));
9542 fc->pc_begin += ofs * fc->code_factor;
9543 break;
9545 case DW_CFA_advance_loc4:
9546 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
9547 if (do_debug_frames_interp)
9548 frame_display_row (fc, &need_col_headers, &max_regs);
9549 else
9550 printf (" DW_CFA_advance_loc4: %ld to %s\n",
9551 (unsigned long) (ofs * fc->code_factor),
9552 dwarf_vmatoa_1 (NULL,
9553 fc->pc_begin + ofs * fc->code_factor,
9554 fc->ptr_size));
9555 fc->pc_begin += ofs * fc->code_factor;
9556 break;
9558 case DW_CFA_offset_extended:
9559 READ_ULEB (reg, start, block_end);
9560 READ_ULEB (roffs, start, block_end);
9561 if (reg >= (unsigned int) fc->ncols)
9562 reg_prefix = bad_reg;
9563 if (! do_debug_frames_interp || *reg_prefix != '\0')
9564 printf (" DW_CFA_offset_extended: %s%s at cfa%+ld\n",
9565 reg_prefix, regname (reg, 0),
9566 roffs * fc->data_factor);
9567 if (*reg_prefix == '\0')
9569 fc->col_type[reg] = DW_CFA_offset;
9570 fc->col_offset[reg] = roffs * fc->data_factor;
9572 break;
9574 case DW_CFA_val_offset:
9575 READ_ULEB (reg, start, block_end);
9576 READ_ULEB (roffs, start, block_end);
9577 if (reg >= (unsigned int) fc->ncols)
9578 reg_prefix = bad_reg;
9579 if (! do_debug_frames_interp || *reg_prefix != '\0')
9580 printf (" DW_CFA_val_offset: %s%s is cfa%+ld\n",
9581 reg_prefix, regname (reg, 0),
9582 roffs * fc->data_factor);
9583 if (*reg_prefix == '\0')
9585 fc->col_type[reg] = DW_CFA_val_offset;
9586 fc->col_offset[reg] = roffs * fc->data_factor;
9588 break;
9590 case DW_CFA_restore_extended:
9591 READ_ULEB (reg, start, block_end);
9592 if (reg >= (unsigned int) fc->ncols)
9593 reg_prefix = bad_reg;
9594 if (! do_debug_frames_interp || *reg_prefix != '\0')
9595 printf (" DW_CFA_restore_extended: %s%s\n",
9596 reg_prefix, regname (reg, 0));
9597 if (*reg_prefix != '\0')
9598 break;
9600 if (reg >= (unsigned int) cie->ncols)
9602 fc->col_type[reg] = DW_CFA_undefined;
9603 fc->col_offset[reg] = 0;
9605 else
9607 fc->col_type[reg] = cie->col_type[reg];
9608 fc->col_offset[reg] = cie->col_offset[reg];
9610 break;
9612 case DW_CFA_undefined:
9613 READ_ULEB (reg, start, block_end);
9614 if (reg >= (unsigned int) fc->ncols)
9615 reg_prefix = bad_reg;
9616 if (! do_debug_frames_interp || *reg_prefix != '\0')
9617 printf (" DW_CFA_undefined: %s%s\n",
9618 reg_prefix, regname (reg, 0));
9619 if (*reg_prefix == '\0')
9621 fc->col_type[reg] = DW_CFA_undefined;
9622 fc->col_offset[reg] = 0;
9624 break;
9626 case DW_CFA_same_value:
9627 READ_ULEB (reg, start, block_end);
9628 if (reg >= (unsigned int) fc->ncols)
9629 reg_prefix = bad_reg;
9630 if (! do_debug_frames_interp || *reg_prefix != '\0')
9631 printf (" DW_CFA_same_value: %s%s\n",
9632 reg_prefix, regname (reg, 0));
9633 if (*reg_prefix == '\0')
9635 fc->col_type[reg] = DW_CFA_same_value;
9636 fc->col_offset[reg] = 0;
9638 break;
9640 case DW_CFA_register:
9641 READ_ULEB (reg, start, block_end);
9642 READ_ULEB (roffs, start, block_end);
9643 if (reg >= (unsigned int) fc->ncols)
9644 reg_prefix = bad_reg;
9645 if (! do_debug_frames_interp || *reg_prefix != '\0')
9647 printf (" DW_CFA_register: %s%s in ",
9648 reg_prefix, regname (reg, 0));
9649 puts (regname (roffs, 0));
9651 if (*reg_prefix == '\0')
9653 fc->col_type[reg] = DW_CFA_register;
9654 fc->col_offset[reg] = roffs;
9656 break;
9658 case DW_CFA_remember_state:
9659 if (! do_debug_frames_interp)
9660 printf (" DW_CFA_remember_state\n");
9661 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9662 rs->cfa_offset = fc->cfa_offset;
9663 rs->cfa_reg = fc->cfa_reg;
9664 rs->ra = fc->ra;
9665 rs->cfa_exp = fc->cfa_exp;
9666 rs->ncols = fc->ncols;
9667 rs->col_type = (short int *) xcmalloc (rs->ncols,
9668 sizeof (* rs->col_type));
9669 rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (* rs->col_offset));
9670 memcpy (rs->col_type, fc->col_type, rs->ncols * sizeof (* fc->col_type));
9671 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (* fc->col_offset));
9672 rs->next = remembered_state;
9673 remembered_state = rs;
9674 break;
9676 case DW_CFA_restore_state:
9677 if (! do_debug_frames_interp)
9678 printf (" DW_CFA_restore_state\n");
9679 rs = remembered_state;
9680 if (rs)
9682 remembered_state = rs->next;
9683 fc->cfa_offset = rs->cfa_offset;
9684 fc->cfa_reg = rs->cfa_reg;
9685 fc->ra = rs->ra;
9686 fc->cfa_exp = rs->cfa_exp;
9687 if (frame_need_space (fc, rs->ncols - 1) < 0)
9689 warn (_("Invalid column number in saved frame state\n"));
9690 fc->ncols = 0;
9691 break;
9693 memcpy (fc->col_type, rs->col_type, rs->ncols * sizeof (* rs->col_type));
9694 memcpy (fc->col_offset, rs->col_offset,
9695 rs->ncols * sizeof (* rs->col_offset));
9696 free (rs->col_type);
9697 free (rs->col_offset);
9698 free (rs);
9700 else if (do_debug_frames_interp)
9701 printf ("Mismatched DW_CFA_restore_state\n");
9702 break;
9704 case DW_CFA_def_cfa:
9705 READ_ULEB (fc->cfa_reg, start, block_end);
9706 READ_ULEB (fc->cfa_offset, start, block_end);
9707 fc->cfa_exp = 0;
9708 if (! do_debug_frames_interp)
9709 printf (" DW_CFA_def_cfa: %s ofs %d\n",
9710 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
9711 break;
9713 case DW_CFA_def_cfa_register:
9714 READ_ULEB (fc->cfa_reg, start, block_end);
9715 fc->cfa_exp = 0;
9716 if (! do_debug_frames_interp)
9717 printf (" DW_CFA_def_cfa_register: %s\n",
9718 regname (fc->cfa_reg, 0));
9719 break;
9721 case DW_CFA_def_cfa_offset:
9722 READ_ULEB (fc->cfa_offset, start, block_end);
9723 if (! do_debug_frames_interp)
9724 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
9725 break;
9727 case DW_CFA_nop:
9728 if (! do_debug_frames_interp)
9729 printf (" DW_CFA_nop\n");
9730 break;
9732 case DW_CFA_def_cfa_expression:
9733 READ_ULEB (ul, start, block_end);
9734 if (ul > (size_t) (block_end - start))
9736 printf (_(" DW_CFA_def_cfa_expression: <corrupt len %lu>\n"), ul);
9737 break;
9739 if (! do_debug_frames_interp)
9741 printf (" DW_CFA_def_cfa_expression (");
9742 decode_location_expression (start, eh_addr_size, 0, -1,
9743 ul, 0, section);
9744 printf (")\n");
9746 fc->cfa_exp = 1;
9747 start += ul;
9748 break;
9750 case DW_CFA_expression:
9751 READ_ULEB (reg, start, block_end);
9752 READ_ULEB (ul, start, block_end);
9753 if (reg >= (unsigned int) fc->ncols)
9754 reg_prefix = bad_reg;
9755 /* PR 17512: file: 069-133014-0.006. */
9756 /* PR 17512: file: 98c02eb4. */
9757 if (ul > (size_t) (block_end - start))
9759 printf (_(" DW_CFA_expression: <corrupt len %lu>\n"), ul);
9760 break;
9762 if (! do_debug_frames_interp || *reg_prefix != '\0')
9764 printf (" DW_CFA_expression: %s%s (",
9765 reg_prefix, regname (reg, 0));
9766 decode_location_expression (start, eh_addr_size, 0, -1,
9767 ul, 0, section);
9768 printf (")\n");
9770 if (*reg_prefix == '\0')
9771 fc->col_type[reg] = DW_CFA_expression;
9772 start += ul;
9773 break;
9775 case DW_CFA_val_expression:
9776 READ_ULEB (reg, start, block_end);
9777 READ_ULEB (ul, start, block_end);
9778 if (reg >= (unsigned int) fc->ncols)
9779 reg_prefix = bad_reg;
9780 if (ul > (size_t) (block_end - start))
9782 printf (" DW_CFA_val_expression: <corrupt len %lu>\n", ul);
9783 break;
9785 if (! do_debug_frames_interp || *reg_prefix != '\0')
9787 printf (" DW_CFA_val_expression: %s%s (",
9788 reg_prefix, regname (reg, 0));
9789 decode_location_expression (start, eh_addr_size, 0, -1,
9790 ul, 0, section);
9791 printf (")\n");
9793 if (*reg_prefix == '\0')
9794 fc->col_type[reg] = DW_CFA_val_expression;
9795 start += ul;
9796 break;
9798 case DW_CFA_offset_extended_sf:
9799 READ_ULEB (reg, start, block_end);
9800 READ_SLEB (l, start, block_end);
9801 if (frame_need_space (fc, reg) < 0)
9802 reg_prefix = bad_reg;
9803 if (! do_debug_frames_interp || *reg_prefix != '\0')
9804 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
9805 reg_prefix, regname (reg, 0),
9806 (long)(l * fc->data_factor));
9807 if (*reg_prefix == '\0')
9809 fc->col_type[reg] = DW_CFA_offset;
9810 fc->col_offset[reg] = l * fc->data_factor;
9812 break;
9814 case DW_CFA_val_offset_sf:
9815 READ_ULEB (reg, start, block_end);
9816 READ_SLEB (l, start, block_end);
9817 if (frame_need_space (fc, reg) < 0)
9818 reg_prefix = bad_reg;
9819 if (! do_debug_frames_interp || *reg_prefix != '\0')
9820 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+ld\n",
9821 reg_prefix, regname (reg, 0),
9822 (long)(l * fc->data_factor));
9823 if (*reg_prefix == '\0')
9825 fc->col_type[reg] = DW_CFA_val_offset;
9826 fc->col_offset[reg] = l * fc->data_factor;
9828 break;
9830 case DW_CFA_def_cfa_sf:
9831 READ_ULEB (fc->cfa_reg, start, block_end);
9832 READ_SLEB (l, start, block_end);
9833 l *= fc->data_factor;
9834 fc->cfa_offset = l;
9835 fc->cfa_exp = 0;
9836 if (! do_debug_frames_interp)
9837 printf (" DW_CFA_def_cfa_sf: %s ofs %ld\n",
9838 regname (fc->cfa_reg, 0), (long) l);
9839 break;
9841 case DW_CFA_def_cfa_offset_sf:
9842 READ_SLEB (l, start, block_end);
9843 l *= fc->data_factor;
9844 fc->cfa_offset = l;
9845 if (! do_debug_frames_interp)
9846 printf (" DW_CFA_def_cfa_offset_sf: %ld\n", (long) l);
9847 break;
9849 case DW_CFA_MIPS_advance_loc8:
9850 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
9851 if (do_debug_frames_interp)
9852 frame_display_row (fc, &need_col_headers, &max_regs);
9853 else
9854 printf (" DW_CFA_MIPS_advance_loc8: %ld to %s\n",
9855 (unsigned long) (ofs * fc->code_factor),
9856 dwarf_vmatoa_1 (NULL,
9857 fc->pc_begin + ofs * fc->code_factor,
9858 fc->ptr_size));
9859 fc->pc_begin += ofs * fc->code_factor;
9860 break;
9862 case DW_CFA_GNU_window_save:
9863 if (! do_debug_frames_interp)
9864 printf (" DW_CFA_GNU_window_save\n");
9865 break;
9867 case DW_CFA_GNU_args_size:
9868 READ_ULEB (ul, start, block_end);
9869 if (! do_debug_frames_interp)
9870 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
9871 break;
9873 case DW_CFA_GNU_negative_offset_extended:
9874 READ_ULEB (reg, start, block_end);
9875 READ_SLEB (l, start, block_end);
9876 l = - l;
9877 if (frame_need_space (fc, reg) < 0)
9878 reg_prefix = bad_reg;
9879 if (! do_debug_frames_interp || *reg_prefix != '\0')
9880 printf (" DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
9881 reg_prefix, regname (reg, 0),
9882 (long)(l * fc->data_factor));
9883 if (*reg_prefix == '\0')
9885 fc->col_type[reg] = DW_CFA_offset;
9886 fc->col_offset[reg] = l * fc->data_factor;
9888 break;
9890 default:
9891 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
9892 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
9893 else
9894 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
9895 start = block_end;
9899 /* Interpret the CFA - as long as it is not completely full of NOPs. */
9900 if (do_debug_frames_interp && ! all_nops)
9901 frame_display_row (fc, &need_col_headers, &max_regs);
9903 if (fde_fc.col_type != NULL)
9905 free (fde_fc.col_type);
9906 fde_fc.col_type = NULL;
9908 if (fde_fc.col_offset != NULL)
9910 free (fde_fc.col_offset);
9911 fde_fc.col_offset = NULL;
9914 start = block_end;
9915 eh_addr_size = saved_eh_addr_size;
9918 printf ("\n");
9920 while (remembered_state != NULL)
9922 rs = remembered_state;
9923 remembered_state = rs->next;
9924 free (rs->col_type);
9925 free (rs->col_offset);
9926 rs->next = NULL; /* Paranoia. */
9927 free (rs);
9930 while (chunks != NULL)
9932 rs = chunks;
9933 chunks = rs->next;
9934 free (rs->col_type);
9935 free (rs->col_offset);
9936 rs->next = NULL; /* Paranoia. */
9937 free (rs);
9940 while (forward_refs != NULL)
9942 rs = forward_refs;
9943 forward_refs = rs->next;
9944 free (rs->col_type);
9945 free (rs->col_offset);
9946 rs->next = NULL; /* Paranoia. */
9947 free (rs);
9950 return 1;
9953 #undef GET
9955 static int
9956 display_debug_names (struct dwarf_section *section, void *file)
9958 unsigned char *hdrptr = section->start;
9959 dwarf_vma unit_length;
9960 unsigned char *unit_start;
9961 const unsigned char *const section_end = section->start + section->size;
9962 unsigned char *unit_end;
9964 introduce (section, false);
9966 load_debug_section_with_follow (str, file);
9968 for (; hdrptr < section_end; hdrptr = unit_end)
9970 unsigned int offset_size;
9971 uint16_t dwarf_version, padding;
9972 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
9973 uint64_t bucket_count, name_count, abbrev_table_size;
9974 uint32_t augmentation_string_size;
9975 unsigned int i;
9976 bool augmentation_printable;
9977 const char *augmentation_string;
9978 size_t total;
9980 unit_start = hdrptr;
9982 /* Get and check the length of the block. */
9983 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
9985 if (unit_length == 0xffffffff)
9987 /* This section is 64-bit DWARF. */
9988 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
9989 offset_size = 8;
9991 else
9992 offset_size = 4;
9994 if (unit_length > (size_t) (section_end - hdrptr)
9995 || unit_length < 2 + 2 + 4 * 7)
9997 too_short:
9998 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
9999 section->name,
10000 (unsigned long) (unit_start - section->start),
10001 dwarf_vmatoa ("x", unit_length));
10002 return 0;
10004 unit_end = hdrptr + unit_length;
10006 /* Get and check the version number. */
10007 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
10008 printf (_("Version %ld\n"), (long) dwarf_version);
10010 /* Prior versions did not exist, and future versions may not be
10011 backwards compatible. */
10012 if (dwarf_version != 5)
10014 warn (_("Only DWARF version 5 .debug_names "
10015 "is currently supported.\n"));
10016 return 0;
10019 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
10020 if (padding != 0)
10021 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
10022 padding);
10024 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
10025 if (comp_unit_count == 0)
10026 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
10028 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
10029 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
10030 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
10031 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
10032 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
10034 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
10035 if (augmentation_string_size % 4 != 0)
10037 warn (_("Augmentation string length %u must be rounded up "
10038 "to a multiple of 4 in .debug_names.\n"),
10039 augmentation_string_size);
10040 augmentation_string_size += (-augmentation_string_size) & 3;
10042 if (augmentation_string_size > (size_t) (unit_end - hdrptr))
10043 goto too_short;
10045 printf (_("Augmentation string:"));
10047 augmentation_printable = true;
10048 augmentation_string = (const char *) hdrptr;
10050 for (i = 0; i < augmentation_string_size; i++)
10052 unsigned char uc;
10054 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
10055 printf (" %02x", uc);
10057 if (uc != 0 && !ISPRINT (uc))
10058 augmentation_printable = false;
10061 if (augmentation_printable)
10063 printf (" (\"");
10064 for (i = 0;
10065 i < augmentation_string_size && augmentation_string[i];
10066 ++i)
10067 putchar (augmentation_string[i]);
10068 printf ("\")");
10070 putchar ('\n');
10072 printf (_("CU table:\n"));
10073 if (_mul_overflow (comp_unit_count, offset_size, &total)
10074 || total > (size_t) (unit_end - hdrptr))
10075 goto too_short;
10076 for (i = 0; i < comp_unit_count; i++)
10078 uint64_t cu_offset;
10080 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
10081 printf (_("[%3u] 0x%lx\n"), i, (unsigned long) cu_offset);
10083 putchar ('\n');
10085 printf (_("TU table:\n"));
10086 if (_mul_overflow (local_type_unit_count, offset_size, &total)
10087 || total > (size_t) (unit_end - hdrptr))
10088 goto too_short;
10089 for (i = 0; i < local_type_unit_count; i++)
10091 uint64_t tu_offset;
10093 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
10094 printf (_("[%3u] 0x%lx\n"), i, (unsigned long) tu_offset);
10096 putchar ('\n');
10098 printf (_("Foreign TU table:\n"));
10099 if (_mul_overflow (foreign_type_unit_count, 8, &total)
10100 || total > (size_t) (unit_end - hdrptr))
10101 goto too_short;
10102 for (i = 0; i < foreign_type_unit_count; i++)
10104 uint64_t signature;
10106 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
10107 printf (_("[%3u] "), i);
10108 print_dwarf_vma (signature, 8);
10109 putchar ('\n');
10111 putchar ('\n');
10113 uint64_t xtra = (bucket_count * sizeof (uint32_t)
10114 + name_count * (sizeof (uint32_t) + 2 * offset_size)
10115 + abbrev_table_size);
10116 if (xtra > (size_t) (unit_end - hdrptr))
10118 warn (_("Entry pool offset (0x%lx) exceeds unit size 0x%lx "
10119 "for unit 0x%lx in the debug_names\n"),
10120 (long) xtra,
10121 (long) (unit_end - unit_start),
10122 (long) (unit_start - section->start));
10123 return 0;
10125 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
10126 hdrptr += bucket_count * sizeof (uint32_t);
10127 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
10128 hdrptr += name_count * sizeof (uint32_t);
10129 unsigned char *const name_table_string_offsets = hdrptr;
10130 hdrptr += name_count * offset_size;
10131 unsigned char *const name_table_entry_offsets = hdrptr;
10132 hdrptr += name_count * offset_size;
10133 unsigned char *const abbrev_table = hdrptr;
10134 hdrptr += abbrev_table_size;
10135 const unsigned char *const abbrev_table_end = hdrptr;
10136 unsigned char *const entry_pool = hdrptr;
10138 size_t buckets_filled = 0;
10139 size_t bucketi;
10140 for (bucketi = 0; bucketi < bucket_count; bucketi++)
10142 const uint32_t bucket = hash_table_buckets[bucketi];
10144 if (bucket != 0)
10145 ++buckets_filled;
10147 printf (ngettext ("Used %zu of %lu bucket.\n",
10148 "Used %zu of %lu buckets.\n",
10149 bucket_count),
10150 buckets_filled, (unsigned long) bucket_count);
10152 uint32_t hash_prev = 0;
10153 size_t hash_clash_count = 0;
10154 size_t longest_clash = 0;
10155 size_t this_length = 0;
10156 size_t hashi;
10157 for (hashi = 0; hashi < name_count; hashi++)
10159 const uint32_t hash_this = hash_table_hashes[hashi];
10161 if (hashi > 0)
10163 if (hash_prev % bucket_count == hash_this % bucket_count)
10165 ++hash_clash_count;
10166 ++this_length;
10167 longest_clash = MAX (longest_clash, this_length);
10169 else
10170 this_length = 0;
10172 hash_prev = hash_this;
10174 printf (_("Out of %lu items there are %zu bucket clashes"
10175 " (longest of %zu entries).\n"),
10176 (unsigned long) name_count, hash_clash_count, longest_clash);
10177 assert (name_count == buckets_filled + hash_clash_count);
10179 struct abbrev_lookup_entry
10181 dwarf_vma abbrev_tag;
10182 unsigned char *abbrev_lookup_ptr;
10184 struct abbrev_lookup_entry *abbrev_lookup = NULL;
10185 size_t abbrev_lookup_used = 0;
10186 size_t abbrev_lookup_allocated = 0;
10188 unsigned char *abbrevptr = abbrev_table;
10189 for (;;)
10191 dwarf_vma abbrev_tag;
10193 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
10194 if (abbrev_tag == 0)
10195 break;
10196 if (abbrev_lookup_used == abbrev_lookup_allocated)
10198 abbrev_lookup_allocated = MAX (0x100,
10199 abbrev_lookup_allocated * 2);
10200 abbrev_lookup = xrealloc (abbrev_lookup,
10201 (abbrev_lookup_allocated
10202 * sizeof (*abbrev_lookup)));
10204 assert (abbrev_lookup_used < abbrev_lookup_allocated);
10205 struct abbrev_lookup_entry *entry;
10206 for (entry = abbrev_lookup;
10207 entry < abbrev_lookup + abbrev_lookup_used;
10208 entry++)
10209 if (entry->abbrev_tag == abbrev_tag)
10211 warn (_("Duplicate abbreviation tag %lu "
10212 "in unit 0x%lx in the debug_names\n"),
10213 (long) abbrev_tag, (long) (unit_start - section->start));
10214 break;
10216 entry = &abbrev_lookup[abbrev_lookup_used++];
10217 entry->abbrev_tag = abbrev_tag;
10218 entry->abbrev_lookup_ptr = abbrevptr;
10220 /* Skip DWARF tag. */
10221 SKIP_ULEB (abbrevptr, abbrev_table_end);
10222 for (;;)
10224 dwarf_vma xindex, form;
10226 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10227 READ_ULEB (form, abbrevptr, abbrev_table_end);
10228 if (xindex == 0 && form == 0)
10229 break;
10233 printf (_("\nSymbol table:\n"));
10234 uint32_t namei;
10235 for (namei = 0; namei < name_count; ++namei)
10237 uint64_t string_offset, entry_offset;
10238 unsigned char *p;
10240 p = name_table_string_offsets + namei * offset_size;
10241 SAFE_BYTE_GET (string_offset, p, offset_size, unit_end);
10242 p = name_table_entry_offsets + namei * offset_size;
10243 SAFE_BYTE_GET (entry_offset, p, offset_size, unit_end);
10245 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
10246 fetch_indirect_string (string_offset));
10248 unsigned char *entryptr = entry_pool + entry_offset;
10250 /* We need to scan first whether there is a single or multiple
10251 entries. TAGNO is -2 for the first entry, it is -1 for the
10252 initial tag read of the second entry, then it becomes 0 for the
10253 first entry for real printing etc. */
10254 int tagno = -2;
10255 /* Initialize it due to a false compiler warning. */
10256 dwarf_vma second_abbrev_tag = -1;
10257 for (;;)
10259 dwarf_vma abbrev_tag;
10260 dwarf_vma dwarf_tag;
10261 const struct abbrev_lookup_entry *entry;
10263 READ_ULEB (abbrev_tag, entryptr, unit_end);
10264 if (tagno == -1)
10266 second_abbrev_tag = abbrev_tag;
10267 tagno = 0;
10268 entryptr = entry_pool + entry_offset;
10269 continue;
10271 if (abbrev_tag == 0)
10272 break;
10273 if (tagno >= 0)
10274 printf ("%s<%lu>",
10275 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
10276 (unsigned long) abbrev_tag);
10278 for (entry = abbrev_lookup;
10279 entry < abbrev_lookup + abbrev_lookup_used;
10280 entry++)
10281 if (entry->abbrev_tag == abbrev_tag)
10282 break;
10283 if (entry >= abbrev_lookup + abbrev_lookup_used)
10285 warn (_("Undefined abbreviation tag %lu "
10286 "in unit 0x%lx in the debug_names\n"),
10287 (long) abbrev_tag,
10288 (long) (unit_start - section->start));
10289 break;
10291 abbrevptr = entry->abbrev_lookup_ptr;
10292 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
10293 if (tagno >= 0)
10294 printf (" %s", get_TAG_name (dwarf_tag));
10295 for (;;)
10297 dwarf_vma xindex, form;
10299 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
10300 READ_ULEB (form, abbrevptr, abbrev_table_end);
10301 if (xindex == 0 && form == 0)
10302 break;
10304 if (tagno >= 0)
10305 printf (" %s", get_IDX_name (xindex));
10306 entryptr = read_and_display_attr_value (0, form, 0,
10307 unit_start, entryptr, unit_end,
10308 0, 0, offset_size,
10309 dwarf_version, NULL,
10310 (tagno < 0), NULL,
10311 NULL, '=', -1);
10313 ++tagno;
10315 if (tagno <= 0)
10316 printf (_(" <no entries>"));
10317 putchar ('\n');
10320 free (abbrev_lookup);
10323 return 1;
10326 static int
10327 display_debug_links (struct dwarf_section * section,
10328 void * file ATTRIBUTE_UNUSED)
10330 const unsigned char * filename;
10331 unsigned int filelen;
10333 introduce (section, false);
10335 /* The .gnu_debuglink section is formatted as:
10336 (c-string) Filename.
10337 (padding) If needed to reach a 4 byte boundary.
10338 (uint32_t) CRC32 value.
10340 The .gun_debugaltlink section is formatted as:
10341 (c-string) Filename.
10342 (binary) Build-ID. */
10344 filename = section->start;
10345 filelen = strnlen ((const char *) filename, section->size);
10346 if (filelen == section->size)
10348 warn (_("The debuglink filename is corrupt/missing\n"));
10349 return 0;
10352 printf (_(" Separate debug info file: %s\n"), filename);
10354 if (startswith (section->name, ".gnu_debuglink"))
10356 unsigned int crc32;
10357 unsigned int crc_offset;
10359 crc_offset = filelen + 1;
10360 crc_offset = (crc_offset + 3) & ~3;
10361 if (crc_offset + 4 > section->size)
10363 warn (_("CRC offset missing/truncated\n"));
10364 return 0;
10367 crc32 = byte_get (filename + crc_offset, 4);
10369 printf (_(" CRC value: %#x\n"), crc32);
10371 if (crc_offset + 4 < section->size)
10373 warn (_("There are %#lx extraneous bytes at the end of the section\n"),
10374 (long)(section->size - (crc_offset + 4)));
10375 return 0;
10378 else /* startswith (section->name, ".gnu_debugaltlink") */
10380 const unsigned char * build_id = section->start + filelen + 1;
10381 bfd_size_type build_id_len = section->size - (filelen + 1);
10382 bfd_size_type printed;
10384 /* FIXME: Should we support smaller build-id notes ? */
10385 if (build_id_len < 0x14)
10387 warn (_("Build-ID is too short (%#lx bytes)\n"), (long) build_id_len);
10388 return 0;
10391 printed = printf (_(" Build-ID (%#lx bytes):"), (long) build_id_len);
10392 display_data (printed, build_id, build_id_len);
10393 putchar ('\n');
10396 putchar ('\n');
10397 return 1;
10400 static int
10401 display_gdb_index (struct dwarf_section *section,
10402 void *file ATTRIBUTE_UNUSED)
10404 unsigned char *start = section->start;
10405 uint32_t version;
10406 uint32_t cu_list_offset, tu_list_offset;
10407 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
10408 unsigned int cu_list_elements, tu_list_elements;
10409 unsigned int address_table_size, symbol_table_slots;
10410 unsigned char *cu_list, *tu_list;
10411 unsigned char *address_table, *symbol_table, *constant_pool;
10412 unsigned int i;
10414 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
10416 introduce (section, false);
10418 if (section->size < 6 * sizeof (uint32_t))
10420 warn (_("Truncated header in the %s section.\n"), section->name);
10421 return 0;
10424 version = byte_get_little_endian (start, 4);
10425 printf (_("Version %ld\n"), (long) version);
10427 /* Prior versions are obsolete, and future versions may not be
10428 backwards compatible. */
10429 if (version < 3 || version > 8)
10431 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
10432 return 0;
10434 if (version < 4)
10435 warn (_("The address table data in version 3 may be wrong.\n"));
10436 if (version < 5)
10437 warn (_("Version 4 does not support case insensitive lookups.\n"));
10438 if (version < 6)
10439 warn (_("Version 5 does not include inlined functions.\n"));
10440 if (version < 7)
10441 warn (_("Version 6 does not include symbol attributes.\n"));
10442 /* Version 7 indices generated by Gold have bad type unit references,
10443 PR binutils/15021. But we don't know if the index was generated by
10444 Gold or not, so to avoid worrying users with gdb-generated indices
10445 we say nothing for version 7 here. */
10447 cu_list_offset = byte_get_little_endian (start + 4, 4);
10448 tu_list_offset = byte_get_little_endian (start + 8, 4);
10449 address_table_offset = byte_get_little_endian (start + 12, 4);
10450 symbol_table_offset = byte_get_little_endian (start + 16, 4);
10451 constant_pool_offset = byte_get_little_endian (start + 20, 4);
10453 if (cu_list_offset > section->size
10454 || tu_list_offset > section->size
10455 || address_table_offset > section->size
10456 || symbol_table_offset > section->size
10457 || constant_pool_offset > section->size)
10459 warn (_("Corrupt header in the %s section.\n"), section->name);
10460 return 0;
10463 /* PR 17531: file: 418d0a8a. */
10464 if (tu_list_offset < cu_list_offset)
10466 warn (_("TU offset (%x) is less than CU offset (%x)\n"),
10467 tu_list_offset, cu_list_offset);
10468 return 0;
10471 cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
10473 if (address_table_offset < tu_list_offset)
10475 warn (_("Address table offset (%x) is less than TU offset (%x)\n"),
10476 address_table_offset, tu_list_offset);
10477 return 0;
10480 tu_list_elements = (address_table_offset - tu_list_offset) / 8;
10482 /* PR 17531: file: 18a47d3d. */
10483 if (symbol_table_offset < address_table_offset)
10485 warn (_("Symbol table offset (%x) is less then Address table offset (%x)\n"),
10486 symbol_table_offset, address_table_offset);
10487 return 0;
10490 address_table_size = symbol_table_offset - address_table_offset;
10492 if (constant_pool_offset < symbol_table_offset)
10494 warn (_("Constant pool offset (%x) is less than symbol table offset (%x)\n"),
10495 constant_pool_offset, symbol_table_offset);
10496 return 0;
10499 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
10501 cu_list = start + cu_list_offset;
10502 tu_list = start + tu_list_offset;
10503 address_table = start + address_table_offset;
10504 symbol_table = start + symbol_table_offset;
10505 constant_pool = start + constant_pool_offset;
10507 if (address_table_offset + address_table_size > section->size)
10509 warn (_("Address table extends beyond end of section.\n"));
10510 return 0;
10513 printf (_("\nCU table:\n"));
10514 for (i = 0; i < cu_list_elements; i += 2)
10516 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
10517 uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
10519 printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
10520 (unsigned long) cu_offset,
10521 (unsigned long) (cu_offset + cu_length - 1));
10524 printf (_("\nTU table:\n"));
10525 for (i = 0; i < tu_list_elements; i += 3)
10527 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
10528 uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
10529 uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
10531 printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
10532 (unsigned long) tu_offset,
10533 (unsigned long) type_offset);
10534 print_dwarf_vma (signature, 8);
10535 printf ("\n");
10538 printf (_("\nAddress table:\n"));
10539 for (i = 0; i < address_table_size && i <= address_table_size - (2 * 8 + 4);
10540 i += 2 * 8 + 4)
10542 uint64_t low = byte_get_little_endian (address_table + i, 8);
10543 uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
10544 uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
10546 print_dwarf_vma (low, 8);
10547 print_dwarf_vma (high, 8);
10548 printf (_("%lu\n"), (unsigned long) cu_index);
10551 printf (_("\nSymbol table:\n"));
10552 for (i = 0; i < symbol_table_slots; ++i)
10554 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10555 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10556 uint32_t num_cus, cu;
10558 if (name_offset != 0
10559 || cu_vector_offset != 0)
10561 unsigned int j;
10563 /* PR 17531: file: 5b7b07ad. */
10564 if (name_offset >= section->size - constant_pool_offset)
10566 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10567 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10568 name_offset, i);
10570 else
10571 printf ("[%3u] %.*s:", i,
10572 (int) (section->size - (constant_pool_offset + name_offset)),
10573 constant_pool + name_offset);
10575 if (section->size - constant_pool_offset < 4
10576 || cu_vector_offset > section->size - constant_pool_offset - 4)
10578 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10579 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10580 cu_vector_offset, i);
10581 continue;
10584 num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
10586 if ((uint64_t) num_cus * 4 > section->size - (constant_pool_offset
10587 + cu_vector_offset + 4))
10589 printf ("<invalid number of CUs: %d>\n", num_cus);
10590 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
10591 num_cus, i);
10592 continue;
10595 if (num_cus > 1)
10596 printf ("\n");
10598 for (j = 0; j < num_cus; ++j)
10600 int is_static;
10601 gdb_index_symbol_kind kind;
10603 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
10604 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
10605 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10606 cu = GDB_INDEX_CU_VALUE (cu);
10607 /* Convert to TU number if it's for a type unit. */
10608 if (cu >= cu_list_elements / 2)
10609 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10610 (unsigned long) (cu - cu_list_elements / 2));
10611 else
10612 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10614 printf (" [%s, %s]",
10615 is_static ? _("static") : _("global"),
10616 get_gdb_index_symbol_kind_name (kind));
10617 if (num_cus > 1)
10618 printf ("\n");
10620 if (num_cus <= 1)
10621 printf ("\n");
10625 return 1;
10628 /* Pre-allocate enough space for the CU/TU sets needed. */
10630 static void
10631 prealloc_cu_tu_list (unsigned int nshndx)
10633 if (shndx_pool == NULL)
10635 shndx_pool_size = nshndx;
10636 shndx_pool_used = 0;
10637 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10638 sizeof (unsigned int));
10640 else
10642 shndx_pool_size = shndx_pool_used + nshndx;
10643 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10644 sizeof (unsigned int));
10648 static void
10649 add_shndx_to_cu_tu_entry (unsigned int shndx)
10651 if (shndx_pool_used >= shndx_pool_size)
10653 error (_("Internal error: out of space in the shndx pool.\n"));
10654 return;
10656 shndx_pool [shndx_pool_used++] = shndx;
10659 static void
10660 end_cu_tu_entry (void)
10662 if (shndx_pool_used >= shndx_pool_size)
10664 error (_("Internal error: out of space in the shndx pool.\n"));
10665 return;
10667 shndx_pool [shndx_pool_used++] = 0;
10670 /* Return the short name of a DWARF section given by a DW_SECT enumerator. */
10672 static const char *
10673 get_DW_SECT_short_name (unsigned int dw_sect)
10675 static char buf[16];
10677 switch (dw_sect)
10679 case DW_SECT_INFO:
10680 return "info";
10681 case DW_SECT_TYPES:
10682 return "types";
10683 case DW_SECT_ABBREV:
10684 return "abbrev";
10685 case DW_SECT_LINE:
10686 return "line";
10687 case DW_SECT_LOC:
10688 return "loc";
10689 case DW_SECT_STR_OFFSETS:
10690 return "str_off";
10691 case DW_SECT_MACINFO:
10692 return "macinfo";
10693 case DW_SECT_MACRO:
10694 return "macro";
10695 default:
10696 break;
10699 snprintf (buf, sizeof (buf), "%d", dw_sect);
10700 return buf;
10703 /* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
10704 These sections are extensions for Fission.
10705 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
10707 static int
10708 process_cu_tu_index (struct dwarf_section *section, int do_display)
10710 unsigned char *phdr = section->start;
10711 unsigned char *limit = phdr + section->size;
10712 unsigned char *phash;
10713 unsigned char *pindex;
10714 unsigned char *ppool;
10715 unsigned int version;
10716 unsigned int ncols = 0;
10717 unsigned int nused;
10718 unsigned int nslots;
10719 unsigned int i;
10720 unsigned int j;
10721 dwarf_vma signature;
10722 size_t total;
10724 /* PR 17512: file: 002-168123-0.004. */
10725 if (phdr == NULL)
10727 warn (_("Section %s is empty\n"), section->name);
10728 return 0;
10730 /* PR 17512: file: 002-376-0.004. */
10731 if (section->size < 24)
10733 warn (_("Section %s is too small to contain a CU/TU header\n"),
10734 section->name);
10735 return 0;
10738 phash = phdr;
10739 SAFE_BYTE_GET_AND_INC (version, phash, 4, limit);
10740 if (version >= 2)
10741 SAFE_BYTE_GET_AND_INC (ncols, phash, 4, limit);
10742 SAFE_BYTE_GET_AND_INC (nused, phash, 4, limit);
10743 SAFE_BYTE_GET_AND_INC (nslots, phash, 4, limit);
10745 pindex = phash + (size_t) nslots * 8;
10746 ppool = pindex + (size_t) nslots * 4;
10748 if (do_display)
10750 introduce (section, false);
10752 printf (_(" Version: %u\n"), version);
10753 if (version >= 2)
10754 printf (_(" Number of columns: %u\n"), ncols);
10755 printf (_(" Number of used entries: %u\n"), nused);
10756 printf (_(" Number of slots: %u\n\n"), nslots);
10759 /* PR 17531: file: 45d69832. */
10760 if (_mul_overflow ((size_t) nslots, 12, &total)
10761 || total > (size_t) (limit - phash))
10763 warn (ngettext ("Section %s is too small for %u slot\n",
10764 "Section %s is too small for %u slots\n",
10765 nslots),
10766 section->name, nslots);
10767 return 0;
10770 if (version == 1)
10772 if (!do_display)
10773 prealloc_cu_tu_list ((limit - ppool) / 4);
10774 for (i = 0; i < nslots; i++)
10776 unsigned char *shndx_list;
10777 unsigned int shndx;
10779 SAFE_BYTE_GET (signature, phash, 8, limit);
10780 if (signature != 0)
10782 SAFE_BYTE_GET (j, pindex, 4, limit);
10783 shndx_list = ppool + j * 4;
10784 /* PR 17531: file: 705e010d. */
10785 if (shndx_list < ppool)
10787 warn (_("Section index pool located before start of section\n"));
10788 return 0;
10791 if (do_display)
10792 printf (_(" [%3d] Signature: 0x%s Sections: "),
10793 i, dwarf_vmatoa ("x", signature));
10794 for (;;)
10796 if (shndx_list >= limit)
10798 warn (_("Section %s too small for shndx pool\n"),
10799 section->name);
10800 return 0;
10802 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
10803 if (shndx == 0)
10804 break;
10805 if (do_display)
10806 printf (" %d", shndx);
10807 else
10808 add_shndx_to_cu_tu_entry (shndx);
10809 shndx_list += 4;
10811 if (do_display)
10812 printf ("\n");
10813 else
10814 end_cu_tu_entry ();
10816 phash += 8;
10817 pindex += 4;
10820 else if (version == 2)
10822 unsigned int val;
10823 unsigned int dw_sect;
10824 unsigned char *ph = phash;
10825 unsigned char *pi = pindex;
10826 unsigned char *poffsets = ppool + (size_t) ncols * 4;
10827 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
10828 bool is_tu_index;
10829 struct cu_tu_set *this_set = NULL;
10830 unsigned int row;
10831 unsigned char *prow;
10832 size_t temp;
10834 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
10836 /* PR 17531: file: 0dd159bf.
10837 Check for integer overflow (can occur when size_t is 32-bit)
10838 with overlarge ncols or nused values. */
10839 if (nused == -1u
10840 || _mul_overflow ((size_t) ncols, 4, &temp)
10841 || _mul_overflow ((size_t) nused + 1, temp, &total)
10842 || total > (size_t) (limit - ppool))
10844 warn (_("Section %s too small for offset and size tables\n"),
10845 section->name);
10846 return 0;
10849 if (do_display)
10851 printf (_(" Offset table\n"));
10852 printf (" slot %-16s ",
10853 is_tu_index ? _("signature") : _("dwo_id"));
10855 else
10857 if (is_tu_index)
10859 tu_count = nused;
10860 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10861 this_set = tu_sets;
10863 else
10865 cu_count = nused;
10866 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10867 this_set = cu_sets;
10871 if (do_display)
10873 for (j = 0; j < ncols; j++)
10875 unsigned char *p = ppool + j * 4;
10876 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10877 printf (" %8s", get_DW_SECT_short_name (dw_sect));
10879 printf ("\n");
10882 for (i = 0; i < nslots; i++)
10884 SAFE_BYTE_GET (signature, ph, 8, limit);
10886 SAFE_BYTE_GET (row, pi, 4, limit);
10887 if (row != 0)
10889 /* PR 17531: file: a05f6ab3. */
10890 if (row > nused)
10892 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
10893 row, nused);
10894 return 0;
10897 if (!do_display)
10899 size_t num_copy = sizeof (uint64_t);
10901 memcpy (&this_set[row - 1].signature, ph, num_copy);
10904 prow = poffsets + (row - 1) * ncols * 4;
10905 if (do_display)
10906 printf (_(" [%3d] 0x%s"),
10907 i, dwarf_vmatoa ("x", signature));
10908 for (j = 0; j < ncols; j++)
10910 unsigned char *p = prow + j * 4;
10911 SAFE_BYTE_GET (val, p, 4, limit);
10912 if (do_display)
10913 printf (" %8d", val);
10914 else
10916 p = ppool + j * 4;
10917 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10919 /* PR 17531: file: 10796eb3. */
10920 if (dw_sect >= DW_SECT_MAX)
10921 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10922 else
10923 this_set [row - 1].section_offsets [dw_sect] = val;
10927 if (do_display)
10928 printf ("\n");
10930 ph += 8;
10931 pi += 4;
10934 ph = phash;
10935 pi = pindex;
10936 if (do_display)
10938 printf ("\n");
10939 printf (_(" Size table\n"));
10940 printf (" slot %-16s ",
10941 is_tu_index ? _("signature") : _("dwo_id"));
10944 for (j = 0; j < ncols; j++)
10946 unsigned char *p = ppool + j * 4;
10947 SAFE_BYTE_GET (val, p, 4, limit);
10948 if (do_display)
10949 printf (" %8s", get_DW_SECT_short_name (val));
10952 if (do_display)
10953 printf ("\n");
10955 for (i = 0; i < nslots; i++)
10957 SAFE_BYTE_GET (signature, ph, 8, limit);
10959 SAFE_BYTE_GET (row, pi, 4, limit);
10960 if (row != 0)
10962 prow = psizes + (row - 1) * ncols * 4;
10964 if (do_display)
10965 printf (_(" [%3d] 0x%s"),
10966 i, dwarf_vmatoa ("x", signature));
10968 for (j = 0; j < ncols; j++)
10970 unsigned char *p = prow + j * 4;
10972 /* PR 28645: Check for overflow. Since we do not know how
10973 many populated rows there will be, we cannot just
10974 perform a single check at the start of this function. */
10975 if (p > (limit - 4))
10977 if (do_display)
10978 printf ("\n");
10979 warn (_("Too many rows/columns in DWARF index section %s\n"),
10980 section->name);
10981 return 0;
10984 SAFE_BYTE_GET (val, p, 4, limit);
10986 if (do_display)
10987 printf (" %8d", val);
10988 else
10990 p = ppool + j * 4;
10991 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10992 if (dw_sect >= DW_SECT_MAX)
10993 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10994 else
10995 this_set [row - 1].section_sizes [dw_sect] = val;
10999 if (do_display)
11000 printf ("\n");
11003 ph += 8;
11004 pi += 4;
11007 else if (do_display)
11008 printf (_(" Unsupported version (%d)\n"), version);
11010 if (do_display)
11011 printf ("\n");
11013 return 1;
11016 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
11018 /* Load the CU and TU indexes if present. This will build a list of
11019 section sets that we can use to associate a .debug_info.dwo section
11020 with its associated .debug_abbrev.dwo section in a .dwp file. */
11022 static bool
11023 load_cu_tu_indexes (void *file)
11025 /* If we have already loaded (or tried to load) the CU and TU indexes
11026 then do not bother to repeat the task. */
11027 if (cu_tu_indexes_read == -1)
11029 cu_tu_indexes_read = true;
11031 if (load_debug_section_with_follow (dwp_cu_index, file))
11032 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
11033 cu_tu_indexes_read = false;
11035 if (load_debug_section_with_follow (dwp_tu_index, file))
11036 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
11037 cu_tu_indexes_read = false;
11040 return (bool) cu_tu_indexes_read;
11043 /* Find the set of sections that includes section SHNDX. */
11045 unsigned int *
11046 find_cu_tu_set (void *file, unsigned int shndx)
11048 unsigned int i;
11050 if (! load_cu_tu_indexes (file))
11051 return NULL;
11053 /* Find SHNDX in the shndx pool. */
11054 for (i = 0; i < shndx_pool_used; i++)
11055 if (shndx_pool [i] == shndx)
11056 break;
11058 if (i >= shndx_pool_used)
11059 return NULL;
11061 /* Now backup to find the first entry in the set. */
11062 while (i > 0 && shndx_pool [i - 1] != 0)
11063 i--;
11065 return shndx_pool + i;
11068 /* Display a .debug_cu_index or .debug_tu_index section. */
11070 static int
11071 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
11073 return process_cu_tu_index (section, 1);
11076 static int
11077 display_debug_not_supported (struct dwarf_section *section,
11078 void *file ATTRIBUTE_UNUSED)
11080 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
11081 section->name);
11083 return 1;
11086 /* Like malloc, but takes two parameters like calloc.
11087 Verifies that the first parameter is not too large.
11088 Note: does *not* initialise the allocated memory to zero. */
11090 void *
11091 cmalloc (size_t nmemb, size_t size)
11093 /* Check for overflow. */
11094 if (nmemb >= ~(size_t) 0 / size)
11095 return NULL;
11097 return xmalloc (nmemb * size);
11100 /* Like xmalloc, but takes two parameters like calloc.
11101 Verifies that the first parameter is not too large.
11102 Note: does *not* initialise the allocated memory to zero. */
11104 void *
11105 xcmalloc (size_t nmemb, size_t size)
11107 /* Check for overflow. */
11108 if (nmemb >= ~(size_t) 0 / size)
11110 fprintf (stderr,
11111 _("Attempt to allocate an array with an excessive number of elements: 0x%lx\n"),
11112 (long) nmemb);
11113 xexit (1);
11116 return xmalloc (nmemb * size);
11119 /* Like xrealloc, but takes three parameters.
11120 Verifies that the second parameter is not too large.
11121 Note: does *not* initialise any new memory to zero. */
11123 void *
11124 xcrealloc (void *ptr, size_t nmemb, size_t size)
11126 /* Check for overflow. */
11127 if (nmemb >= ~(size_t) 0 / size)
11129 error (_("Attempt to re-allocate an array with an excessive number of elements: 0x%lx\n"),
11130 (long) nmemb);
11131 xexit (1);
11134 return xrealloc (ptr, nmemb * size);
11137 /* Like xcalloc, but verifies that the first parameter is not too large. */
11139 void *
11140 xcalloc2 (size_t nmemb, size_t size)
11142 /* Check for overflow. */
11143 if (nmemb >= ~(size_t) 0 / size)
11145 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: 0x%lx\n"),
11146 (long) nmemb);
11147 xexit (1);
11150 return xcalloc (nmemb, size);
11153 static unsigned long
11154 calc_gnu_debuglink_crc32 (unsigned long crc,
11155 const unsigned char * buf,
11156 bfd_size_type len)
11158 static const unsigned long crc32_table[256] =
11160 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
11161 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
11162 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
11163 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
11164 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
11165 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
11166 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
11167 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
11168 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
11169 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
11170 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
11171 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
11172 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
11173 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
11174 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
11175 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
11176 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
11177 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
11178 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
11179 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
11180 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
11181 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
11182 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
11183 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
11184 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
11185 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
11186 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
11187 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
11188 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
11189 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
11190 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
11191 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
11192 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
11193 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
11194 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
11195 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
11196 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
11197 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
11198 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
11199 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
11200 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
11201 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
11202 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
11203 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
11204 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
11205 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
11206 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
11207 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
11208 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
11209 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
11210 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
11211 0x2d02ef8d
11213 const unsigned char *end;
11215 crc = ~crc & 0xffffffff;
11216 for (end = buf + len; buf < end; ++ buf)
11217 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
11218 return ~crc & 0xffffffff;
11221 typedef bool (*check_func_type) (const char *, void *);
11222 typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
11224 static bool
11225 check_gnu_debuglink (const char * pathname, void * crc_pointer)
11227 static unsigned char buffer [8 * 1024];
11228 FILE * f;
11229 bfd_size_type count;
11230 unsigned long crc = 0;
11231 void * sep_data;
11233 sep_data = open_debug_file (pathname);
11234 if (sep_data == NULL)
11235 return false;
11237 /* Yes - we are opening the file twice... */
11238 f = fopen (pathname, "rb");
11239 if (f == NULL)
11241 /* Paranoia: This should never happen. */
11242 close_debug_file (sep_data);
11243 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
11244 return false;
11247 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
11248 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
11250 fclose (f);
11252 if (crc != * (unsigned long *) crc_pointer)
11254 close_debug_file (sep_data);
11255 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
11256 pathname);
11257 return false;
11260 return true;
11263 static const char *
11264 parse_gnu_debuglink (struct dwarf_section * section, void * data)
11266 const char * name;
11267 unsigned int crc_offset;
11268 unsigned long * crc32 = (unsigned long *) data;
11270 /* The name is first.
11271 The CRC value is stored after the filename, aligned up to 4 bytes. */
11272 name = (const char *) section->start;
11274 crc_offset = strnlen (name, section->size) + 1;
11275 if (crc_offset == 1)
11276 return NULL;
11277 crc_offset = (crc_offset + 3) & ~3;
11278 if (crc_offset + 4 > section->size)
11279 return NULL;
11281 * crc32 = byte_get (section->start + crc_offset, 4);
11282 return name;
11285 static bool
11286 check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
11288 void * sep_data = open_debug_file (filename);
11290 if (sep_data == NULL)
11291 return false;
11293 /* FIXME: We should now extract the build-id in the separate file
11294 and check it... */
11296 return true;
11299 typedef struct build_id_data
11301 bfd_size_type len;
11302 const unsigned char * data;
11303 } Build_id_data;
11305 static const char *
11306 parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
11308 const char * name;
11309 bfd_size_type namelen;
11310 bfd_size_type id_len;
11311 Build_id_data * build_id_data;
11313 /* The name is first.
11314 The build-id follows immediately, with no padding, up to the section's end. */
11316 name = (const char *) section->start;
11317 namelen = strnlen (name, section->size) + 1;
11318 if (namelen == 1)
11319 return NULL;
11320 if (namelen >= section->size)
11321 return NULL;
11323 id_len = section->size - namelen;
11324 if (id_len < 0x14)
11325 return NULL;
11327 build_id_data = (Build_id_data *) data;
11328 build_id_data->len = id_len;
11329 build_id_data->data = section->start + namelen;
11331 return name;
11334 static void
11335 add_separate_debug_file (const char * filename, void * handle)
11337 separate_info * i = xmalloc (sizeof * i);
11339 i->filename = filename;
11340 i->handle = handle;
11341 i->next = first_separate_info;
11342 first_separate_info = i;
11345 #if HAVE_LIBDEBUGINFOD
11346 /* Query debuginfod servers for the target debuglink or debugaltlink
11347 file. If successful, store the path of the file in filename and
11348 return TRUE, otherwise return FALSE. */
11350 static bool
11351 debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
11352 char ** filename,
11353 void * file)
11355 size_t build_id_len;
11356 unsigned char * build_id;
11358 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
11360 /* Get the build-id of file. */
11361 build_id = get_build_id (file);
11362 build_id_len = 0;
11364 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
11366 /* Get the build-id of the debugaltlink file. */
11367 unsigned int filelen;
11369 filelen = strnlen ((const char *)section->start, section->size);
11370 if (filelen == section->size)
11371 /* Corrupt debugaltlink. */
11372 return false;
11374 build_id = section->start + filelen + 1;
11375 build_id_len = section->size - (filelen + 1);
11377 if (build_id_len == 0)
11378 return false;
11380 else
11381 return false;
11383 if (build_id)
11385 int fd;
11386 debuginfod_client * client;
11388 client = debuginfod_begin ();
11389 if (client == NULL)
11390 return false;
11392 /* Query debuginfod servers for the target file. If found its path
11393 will be stored in filename. */
11394 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11395 debuginfod_end (client);
11397 /* Only free build_id if we allocated space for a hex string
11398 in get_build_id (). */
11399 if (build_id_len == 0)
11400 free (build_id);
11402 if (fd >= 0)
11404 /* File successfully retrieved. Close fd since we want to
11405 use open_debug_file () on filename instead. */
11406 close (fd);
11407 return true;
11411 return false;
11413 #endif /* HAVE_LIBDEBUGINFOD */
11415 static void *
11416 load_separate_debug_info (const char * main_filename,
11417 struct dwarf_section * xlink,
11418 parse_func_type parse_func,
11419 check_func_type check_func,
11420 void * func_data,
11421 void * file ATTRIBUTE_UNUSED)
11423 const char * separate_filename;
11424 char * debug_filename;
11425 char * canon_dir;
11426 size_t canon_dirlen;
11427 size_t dirlen;
11428 char * canon_filename;
11429 char * canon_debug_filename;
11430 bool self;
11432 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
11434 warn (_("Corrupt debuglink section: %s\n"),
11435 xlink->name ? xlink->name : xlink->uncompressed_name);
11436 return NULL;
11439 /* Attempt to locate the separate file.
11440 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
11442 canon_filename = lrealpath (main_filename);
11443 canon_dir = xstrdup (canon_filename);
11445 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11446 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11447 break;
11448 canon_dir[canon_dirlen] = '\0';
11450 #ifndef DEBUGDIR
11451 #define DEBUGDIR "/lib/debug"
11452 #endif
11453 #ifndef EXTRA_DEBUG_ROOT1
11454 #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11455 #endif
11456 #ifndef EXTRA_DEBUG_ROOT2
11457 #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11458 #endif
11460 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11461 + canon_dirlen
11462 + strlen (".debug/")
11463 #ifdef EXTRA_DEBUG_ROOT1
11464 + strlen (EXTRA_DEBUG_ROOT1)
11465 #endif
11466 #ifdef EXTRA_DEBUG_ROOT2
11467 + strlen (EXTRA_DEBUG_ROOT2)
11468 #endif
11469 + strlen (separate_filename)
11470 + 1);
11471 if (debug_filename == NULL)
11473 warn (_("Out of memory"));
11474 free (canon_dir);
11475 free (canon_filename);
11476 return NULL;
11479 /* First try in the current directory. */
11480 sprintf (debug_filename, "%s", separate_filename);
11481 if (check_func (debug_filename, func_data))
11482 goto found;
11484 /* Then try in a subdirectory called .debug. */
11485 sprintf (debug_filename, ".debug/%s", separate_filename);
11486 if (check_func (debug_filename, func_data))
11487 goto found;
11489 /* Then try in the same directory as the original file. */
11490 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11491 if (check_func (debug_filename, func_data))
11492 goto found;
11494 /* And the .debug subdirectory of that directory. */
11495 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11496 if (check_func (debug_filename, func_data))
11497 goto found;
11499 #ifdef EXTRA_DEBUG_ROOT1
11500 /* Try the first extra debug file root. */
11501 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11502 if (check_func (debug_filename, func_data))
11503 goto found;
11505 /* Try the first extra debug file root. */
11506 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11507 if (check_func (debug_filename, func_data))
11508 goto found;
11509 #endif
11511 #ifdef EXTRA_DEBUG_ROOT2
11512 /* Try the second extra debug file root. */
11513 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11514 if (check_func (debug_filename, func_data))
11515 goto found;
11516 #endif
11518 /* Then try in the global debug_filename directory. */
11519 strcpy (debug_filename, DEBUGDIR);
11520 dirlen = strlen (DEBUGDIR) - 1;
11521 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
11522 strcat (debug_filename, "/");
11523 strcat (debug_filename, (const char *) separate_filename);
11525 if (check_func (debug_filename, func_data))
11526 goto found;
11528 #if HAVE_LIBDEBUGINFOD
11530 char * tmp_filename;
11532 if (use_debuginfod
11533 && debuginfod_fetch_separate_debug_info (xlink,
11534 & tmp_filename,
11535 file))
11537 /* File successfully downloaded from server, replace
11538 debug_filename with the file's path. */
11539 free (debug_filename);
11540 debug_filename = tmp_filename;
11541 goto found;
11544 #endif
11546 if (do_debug_links)
11548 /* Failed to find the file. */
11549 warn (_("could not find separate debug file '%s'\n"),
11550 separate_filename);
11551 warn (_("tried: %s\n"), debug_filename);
11553 #ifdef EXTRA_DEBUG_ROOT2
11554 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11555 separate_filename);
11556 warn (_("tried: %s\n"), debug_filename);
11557 #endif
11559 #ifdef EXTRA_DEBUG_ROOT1
11560 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11561 canon_dir, separate_filename);
11562 warn (_("tried: %s\n"), debug_filename);
11564 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11565 separate_filename);
11566 warn (_("tried: %s\n"), debug_filename);
11567 #endif
11569 sprintf (debug_filename, "%s.debug/%s", canon_dir,
11570 separate_filename);
11571 warn (_("tried: %s\n"), debug_filename);
11573 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11574 warn (_("tried: %s\n"), debug_filename);
11576 sprintf (debug_filename, ".debug/%s", separate_filename);
11577 warn (_("tried: %s\n"), debug_filename);
11579 sprintf (debug_filename, "%s", separate_filename);
11580 warn (_("tried: %s\n"), debug_filename);
11582 #if HAVE_LIBDEBUGINFOD
11583 if (use_debuginfod)
11585 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11587 if (urls == NULL)
11588 urls = "";
11590 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11592 #endif
11595 free (canon_dir);
11596 free (debug_filename);
11597 free (canon_filename);
11598 return NULL;
11600 found:
11601 free (canon_dir);
11603 canon_debug_filename = lrealpath (debug_filename);
11604 self = strcmp (canon_debug_filename, canon_filename) == 0;
11605 free (canon_filename);
11606 free (canon_debug_filename);
11607 if (self)
11609 free (debug_filename);
11610 return NULL;
11613 void * debug_handle;
11615 /* Now open the file.... */
11616 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
11618 warn (_("failed to open separate debug file: %s\n"), debug_filename);
11619 free (debug_filename);
11620 return NULL;
11623 /* FIXME: We do not check to see if there are any other separate debug info
11624 files that would also match. */
11626 if (do_debug_links)
11627 printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
11628 add_separate_debug_file (debug_filename, debug_handle);
11630 /* Do not free debug_filename - it might be referenced inside
11631 the structure returned by open_debug_file(). */
11632 return debug_handle;
11635 /* Attempt to load a separate dwarf object file. */
11637 static void *
11638 load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
11640 char * separate_filename;
11641 void * separate_handle;
11643 if (IS_ABSOLUTE_PATH (name))
11644 separate_filename = strdup (name);
11645 else
11646 /* FIXME: Skip adding / if dwo_dir ends in /. */
11647 separate_filename = concat (dir, "/", name, NULL);
11648 if (separate_filename == NULL)
11650 warn (_("Out of memory allocating dwo filename\n"));
11651 return NULL;
11654 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
11656 warn (_("Unable to load dwo file: %s\n"), separate_filename);
11657 free (separate_filename);
11658 return NULL;
11661 /* FIXME: We should check the dwo_id. */
11663 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
11665 add_separate_debug_file (separate_filename, separate_handle);
11666 /* Note - separate_filename will be freed in free_debug_memory(). */
11667 return separate_handle;
11670 static void *
11671 try_build_id_prefix (const char * prefix, char * filename, const unsigned char * data, unsigned long id_len)
11673 char * f = filename;
11675 f += sprintf (f, "%s.build-id/%02x/", prefix, (unsigned) *data++);
11676 id_len --;
11677 while (id_len --)
11678 f += sprintf (f, "%02x", (unsigned) *data++);
11679 strcpy (f, ".debug");
11681 return open_debug_file (filename);
11684 /* Try to load a debug file based upon the build-id held in the .note.gnu.build-id section. */
11686 static void
11687 load_build_id_debug_file (const char * main_filename ATTRIBUTE_UNUSED, void * main_file)
11689 if (! load_debug_section (note_gnu_build_id, main_file))
11690 return; /* No .note.gnu.build-id section. */
11692 struct dwarf_section * section = & debug_displays [note_gnu_build_id].section;
11693 if (section == NULL)
11695 warn (_("Unable to load the .note.gnu.build-id section\n"));
11696 return;
11699 if (section->start == NULL || section->size < 0x18)
11701 warn (_(".note.gnu.build-id section is corrupt/empty\n"));
11702 return;
11705 /* In theory we should extract the contents of the section into
11706 a note structure and then check the fields. For now though
11707 just use hard coded offsets instead:
11709 Field Bytes Contents
11710 NSize 0...3 4
11711 DSize 4...7 8+
11712 Type 8..11 3 (NT_GNU_BUILD_ID)
11713 Name 12.15 GNU\0
11714 Data 16.... */
11716 /* FIXME: Check the name size, name and type fields. */
11718 unsigned long build_id_size;
11719 build_id_size = byte_get (section->start + 4, 4);
11720 if (build_id_size < 8)
11722 warn (_(".note.gnu.build-id data size is too small\n"));
11723 return;
11726 if (build_id_size > (section->size - 16))
11728 warn (_(".note.gnu.build-id data size is too bug\n"));
11729 return;
11732 char * filename;
11733 filename = xmalloc (strlen (".build-id/")
11734 + build_id_size * 2 + 2
11735 + strlen (".debug")
11736 /* The next string should be the same as the longest
11737 name found in the prefixes[] array below. */
11738 + strlen ("/usrlib64/debug/usr")
11739 + 1);
11740 void * handle;
11742 static const char * prefixes[] =
11745 ".debug/",
11746 "/usr/lib/debug/",
11747 "/usr/lib/debug/usr/",
11748 "/usr/lib64/debug/",
11749 "/usr/lib64/debug/usr"
11751 long unsigned int i;
11753 for (i = 0; i < ARRAY_SIZE (prefixes); i++)
11755 handle = try_build_id_prefix (prefixes[i], filename,
11756 section->start + 16, build_id_size);
11757 if (handle != NULL)
11758 break;
11760 /* FIXME: TYhe BFD library also tries a global debugfile directory prefix. */
11761 if (handle == NULL)
11763 /* Failed to find a debug file associated with the build-id.
11764 This is not an error however, rather it just means that
11765 the debug info has probably not been loaded on the system,
11766 or that another method is being used to link to the debug
11767 info. */
11768 free (filename);
11769 return;
11772 add_separate_debug_file (filename, handle);
11775 /* Try to load a debug file pointed to by the .debug_sup section. */
11777 static void
11778 load_debug_sup_file (const char * main_filename, void * file)
11780 if (! load_debug_section (debug_sup, file))
11781 return; /* No .debug_sup section. */
11783 struct dwarf_section * section;
11784 section = & debug_displays [debug_sup].section;
11785 assert (section != NULL);
11787 if (section->start == NULL || section->size < 5)
11789 warn (_(".debug_sup section is corrupt/empty\n"));
11790 return;
11793 if (section->start[2] != 0)
11794 return; /* This is a supplementary file. */
11796 const char * filename = (const char *) section->start + 3;
11797 if (strnlen (filename, section->size - 3) == section->size - 3)
11799 warn (_("filename in .debug_sup section is corrupt\n"));
11800 return;
11803 if (filename[0] != '/' && strchr (main_filename, '/'))
11805 char * new_name;
11806 int new_len;
11808 new_len = asprintf (& new_name, "%.*s/%s",
11809 (int) (strrchr (main_filename, '/') - main_filename),
11810 main_filename,
11811 filename);
11812 if (new_len < 3)
11814 warn (_("unable to construct path for supplementary debug file"));
11815 if (new_len > -1)
11816 free (new_name);
11817 return;
11819 filename = new_name;
11821 else
11823 /* PR 27796: Make sure that we pass a filename that can be free'd to
11824 add_separate_debug_file(). */
11825 filename = strdup (filename);
11826 if (filename == NULL)
11828 warn (_("out of memory constructing filename for .debug_sup link\n"));
11829 return;
11833 void * handle = open_debug_file (filename);
11834 if (handle == NULL)
11836 warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
11837 free ((void *) filename);
11838 return;
11841 printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
11843 /* FIXME: Compare the checksums, if present. */
11844 add_separate_debug_file (filename, handle);
11847 /* Load a debuglink section and/or a debugaltlink section, if either are present.
11848 Recursively check the loaded files for more of these sections.
11849 Also follow any links in .debug_sup sections.
11850 FIXME: Should also check for DWO_* entries in the newly loaded files. */
11852 static void
11853 check_for_and_load_links (void * file, const char * filename)
11855 void * handle = NULL;
11857 if (load_debug_section (gnu_debugaltlink, file))
11859 Build_id_data build_id_data;
11861 handle = load_separate_debug_info (filename,
11862 & debug_displays[gnu_debugaltlink].section,
11863 parse_gnu_debugaltlink,
11864 check_gnu_debugaltlink,
11865 & build_id_data,
11866 file);
11867 if (handle)
11869 assert (handle == first_separate_info->handle);
11870 check_for_and_load_links (first_separate_info->handle,
11871 first_separate_info->filename);
11875 if (load_debug_section (gnu_debuglink, file))
11877 unsigned long crc32;
11879 handle = load_separate_debug_info (filename,
11880 & debug_displays[gnu_debuglink].section,
11881 parse_gnu_debuglink,
11882 check_gnu_debuglink,
11883 & crc32,
11884 file);
11885 if (handle)
11887 assert (handle == first_separate_info->handle);
11888 check_for_and_load_links (first_separate_info->handle,
11889 first_separate_info->filename);
11893 load_debug_sup_file (filename, file);
11895 load_build_id_debug_file (filename, file);
11898 /* Load the separate debug info file(s) attached to FILE, if any exist.
11899 Returns TRUE if any were found, FALSE otherwise.
11900 If TRUE is returned then the linked list starting at first_separate_info
11901 will be populated with open file handles. */
11903 bool
11904 load_separate_debug_files (void * file, const char * filename)
11906 /* Skip this operation if we are not interested in debug links. */
11907 if (! do_follow_links && ! do_debug_links)
11908 return false;
11910 /* See if there are any dwo links. */
11911 if (load_debug_section (str, file)
11912 && load_debug_section (abbrev, file)
11913 && load_debug_section (info, file))
11915 free_dwo_info ();
11917 if (process_debug_info (& debug_displays[info].section, file, abbrev,
11918 true, false))
11920 bool introduced = false;
11921 dwo_info *dwinfo;
11922 const char *dir = NULL;
11923 const char *id = NULL;
11924 const char *name = NULL;
11926 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
11928 /* Accumulate NAME, DIR and ID fields. */
11929 switch (dwinfo->type)
11931 case DWO_NAME:
11932 if (name != NULL)
11933 warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
11934 name = dwinfo->value;
11935 break;
11937 case DWO_DIR:
11938 /* There can be multiple DW_AT_comp_dir entries in a CU,
11939 so do not complain. */
11940 dir = dwinfo->value;
11941 break;
11943 case DWO_ID:
11944 if (id != NULL)
11945 warn (_("multiple DWO_IDs encountered for the same CU\n"));
11946 id = dwinfo->value;
11947 break;
11949 default:
11950 error (_("Unexpected DWO INFO type"));
11951 break;
11954 /* If we have reached the end of our list, or we are changing
11955 CUs, then display the information that we have accumulated
11956 so far. */
11957 if (name != NULL
11958 && (dwinfo->next == NULL
11959 || dwinfo->next->cu_offset != dwinfo->cu_offset))
11961 if (do_debug_links)
11963 if (! introduced)
11965 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
11966 debug_displays [info].section.uncompressed_name);
11967 introduced = true;
11970 printf (_(" Name: %s\n"), name);
11971 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
11972 if (id != NULL)
11973 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
11974 else
11975 printf (_(" ID: <not specified>\n"));
11976 printf ("\n\n");
11979 if (do_follow_links)
11980 load_dwo_file (filename, name, dir, id);
11982 name = dir = id = NULL;
11988 if (! do_follow_links)
11989 /* The other debug links will be displayed by display_debug_links()
11990 so we do not need to do any further processing here. */
11991 return false;
11993 /* FIXME: We do not check for the presence of both link sections in the same file. */
11994 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
11995 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
11997 check_for_and_load_links (file, filename);
11998 if (first_separate_info != NULL)
11999 return true;
12001 do_follow_links = 0;
12002 return false;
12005 void
12006 free_debug_memory (void)
12008 unsigned int i;
12010 free_all_abbrevs ();
12012 free (cu_abbrev_map);
12013 cu_abbrev_map = NULL;
12014 next_free_abbrev_map_entry = 0;
12016 free (shndx_pool);
12017 shndx_pool = NULL;
12018 shndx_pool_size = 0;
12019 shndx_pool_used = 0;
12020 free (cu_sets);
12021 cu_sets = NULL;
12022 cu_count = 0;
12023 free (tu_sets);
12024 tu_sets = NULL;
12025 tu_count = 0;
12027 memset (level_type_signed, 0, sizeof level_type_signed);
12028 cu_tu_indexes_read = -1;
12030 for (i = 0; i < max; i++)
12031 free_debug_section ((enum dwarf_section_display_enum) i);
12033 if (debug_information != NULL)
12035 for (i = 0; i < alloc_num_debug_info_entries; i++)
12037 if (debug_information [i].max_loc_offsets)
12039 free (debug_information [i].loc_offsets);
12040 free (debug_information [i].have_frame_base);
12042 if (debug_information [i].max_range_lists)
12043 free (debug_information [i].range_lists);
12045 free (debug_information);
12046 debug_information = NULL;
12047 alloc_num_debug_info_entries = num_debug_info_entries = 0;
12050 separate_info * d;
12051 separate_info * next;
12053 for (d = first_separate_info; d != NULL; d = next)
12055 close_debug_file (d->handle);
12056 free ((void *) d->filename);
12057 next = d->next;
12058 free ((void *) d);
12060 first_separate_info = NULL;
12062 free_dwo_info ();
12065 void
12066 dwarf_select_sections_by_names (const char *names)
12068 typedef struct
12070 const char * option;
12071 int * variable;
12072 int val;
12074 debug_dump_long_opts;
12076 static const debug_dump_long_opts opts_table [] =
12078 /* Please keep this table alpha- sorted. */
12079 { "Ranges", & do_debug_ranges, 1 },
12080 { "abbrev", & do_debug_abbrevs, 1 },
12081 { "addr", & do_debug_addr, 1 },
12082 { "aranges", & do_debug_aranges, 1 },
12083 { "cu_index", & do_debug_cu_index, 1 },
12084 { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
12085 #ifdef HAVE_LIBDEBUGINFOD
12086 { "do-not-use-debuginfod", & use_debuginfod, 0 },
12087 #endif
12088 { "follow-links", & do_follow_links, 1 },
12089 { "frames", & do_debug_frames, 1 },
12090 { "frames-interp", & do_debug_frames_interp, 1 },
12091 /* The special .gdb_index section. */
12092 { "gdb_index", & do_gdb_index, 1 },
12093 { "info", & do_debug_info, 1 },
12094 { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility. */
12095 { "links", & do_debug_links, 1 },
12096 { "loc", & do_debug_loc, 1 },
12097 { "macro", & do_debug_macinfo, 1 },
12098 { "no-follow-links", & do_follow_links, 0 },
12099 { "pubnames", & do_debug_pubnames, 1 },
12100 { "pubtypes", & do_debug_pubtypes, 1 },
12101 /* This entry is for compatibility
12102 with earlier versions of readelf. */
12103 { "ranges", & do_debug_aranges, 1 },
12104 { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
12105 { "str", & do_debug_str, 1 },
12106 { "str-offsets", & do_debug_str_offsets, 1 },
12107 /* These trace_* sections are used by Itanium VMS. */
12108 { "trace_abbrev", & do_trace_abbrevs, 1 },
12109 { "trace_aranges", & do_trace_aranges, 1 },
12110 { "trace_info", & do_trace_info, 1 },
12111 #ifdef HAVE_LIBDEBUGINFOD
12112 { "use-debuginfod", & use_debuginfod, 1 },
12113 #endif
12114 { NULL, NULL, 0 }
12117 const char *p;
12119 p = names;
12120 while (*p)
12122 const debug_dump_long_opts * entry;
12124 for (entry = opts_table; entry->option; entry++)
12126 size_t len = strlen (entry->option);
12128 if (strncmp (p, entry->option, len) == 0
12129 && (p[len] == ',' || p[len] == '\0'))
12131 * entry->variable = entry->val;
12133 /* The --debug-dump=frames-interp option also
12134 enables the --debug-dump=frames option. */
12135 if (do_debug_frames_interp)
12136 do_debug_frames = 1;
12138 p += len;
12139 break;
12143 if (entry->option == NULL)
12145 warn (_("Unrecognized debug option '%s'\n"), p);
12146 p = strchr (p, ',');
12147 if (p == NULL)
12148 break;
12151 if (*p == ',')
12152 p++;
12156 void
12157 dwarf_select_sections_by_letters (const char *letters)
12159 unsigned int lindex = 0;
12161 while (letters[lindex])
12162 switch (letters[lindex++])
12164 case 'A': do_debug_addr = 1; break;
12165 case 'a': do_debug_abbrevs = 1; break;
12166 case 'c': do_debug_cu_index = 1; break;
12167 #ifdef HAVE_LIBDEBUGINFOD
12168 case 'D': use_debuginfod = 1; break;
12169 case 'E': use_debuginfod = 0; break;
12170 #endif
12171 case 'F': do_debug_frames_interp = 1; /* Fall through. */
12172 case 'f': do_debug_frames = 1; break;
12173 case 'g': do_gdb_index = 1; break;
12174 case 'i': do_debug_info = 1; break;
12175 case 'K': do_follow_links = 1; break;
12176 case 'N': do_follow_links = 0; break;
12177 case 'k': do_debug_links = 1; break;
12178 case 'l': do_debug_lines |= FLAG_DEBUG_LINES_RAW; break;
12179 case 'L': do_debug_lines |= FLAG_DEBUG_LINES_DECODED; break;
12180 case 'm': do_debug_macinfo = 1; break;
12181 case 'O': do_debug_str_offsets = 1; break;
12182 case 'o': do_debug_loc = 1; break;
12183 case 'p': do_debug_pubnames = 1; break;
12184 case 'R': do_debug_ranges = 1; break;
12185 case 'r': do_debug_aranges = 1; break;
12186 case 's': do_debug_str = 1; break;
12187 case 'T': do_trace_aranges = 1; break;
12188 case 't': do_debug_pubtypes = 1; break;
12189 case 'U': do_trace_info = 1; break;
12190 case 'u': do_trace_abbrevs = 1; break;
12192 default:
12193 warn (_("Unrecognized debug option '%s'\n"), letters);
12194 break;
12198 void
12199 dwarf_select_sections_all (void)
12201 do_debug_info = 1;
12202 do_debug_abbrevs = 1;
12203 do_debug_lines = FLAG_DEBUG_LINES_RAW;
12204 do_debug_pubnames = 1;
12205 do_debug_pubtypes = 1;
12206 do_debug_aranges = 1;
12207 do_debug_ranges = 1;
12208 do_debug_frames = 1;
12209 do_debug_macinfo = 1;
12210 do_debug_str = 1;
12211 do_debug_loc = 1;
12212 do_gdb_index = 1;
12213 do_trace_info = 1;
12214 do_trace_abbrevs = 1;
12215 do_trace_aranges = 1;
12216 do_debug_addr = 1;
12217 do_debug_cu_index = 1;
12218 do_follow_links = 1;
12219 do_debug_links = 1;
12220 do_debug_str_offsets = 1;
12223 #define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0
12224 #define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0
12226 /* N.B. The order here must match the order in section_display_enum. */
12228 struct dwarf_section_display debug_displays[] =
12230 { { ".debug_abbrev", ".zdebug_abbrev", ".dwabrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12231 { { ".debug_aranges", ".zdebug_aranges", ".dwarnge", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, true },
12232 { { ".debug_frame", ".zdebug_frame", ".dwframe", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12233 { { ".debug_info", ".zdebug_info", ".dwinfo", ABBREV (abbrev)}, display_debug_info, &do_debug_info, true },
12234 { { ".debug_line", ".zdebug_line", ".dwline", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12235 { { ".debug_pubnames", ".zdebug_pubnames", ".dwpbnms", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, false },
12236 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, false },
12237 { { ".eh_frame", "", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
12238 { { ".debug_macinfo", ".zdebug_macinfo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12239 { { ".debug_macro", ".zdebug_macro", ".dwmac", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12240 { { ".debug_str", ".zdebug_str", ".dwstr", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12241 { { ".debug_line_str", ".zdebug_line_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12242 { { ".debug_loc", ".zdebug_loc", ".dwloc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12243 { { ".debug_loclists", ".zdebug_loclists", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12244 { { ".debug_pubtypes", ".zdebug_pubtypes", ".dwpbtyp", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, false },
12245 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, false },
12246 { { ".debug_ranges", ".zdebug_ranges", ".dwrnges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12247 { { ".debug_rnglists", ".zdebug_rnglists", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
12248 { { ".debug_static_func", ".zdebug_static_func", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12249 { { ".debug_static_vars", ".zdebug_static_vars", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12250 { { ".debug_types", ".zdebug_types", "", ABBREV (abbrev) }, display_debug_types, &do_debug_info, true },
12251 { { ".debug_weaknames", ".zdebug_weaknames", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12252 { { ".gdb_index", "", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false },
12253 { { ".debug_names", "", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false },
12254 { { ".trace_info", "", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true },
12255 { { ".trace_abbrev", "", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false },
12256 { { ".trace_aranges", "", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false },
12257 { { ".debug_info.dwo", ".zdebug_info.dwo", "", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
12258 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", "", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
12259 { { ".debug_types.dwo", ".zdebug_types.dwo", "", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, true },
12260 { { ".debug_line.dwo", ".zdebug_line.dwo", "", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
12261 { { ".debug_loc.dwo", ".zdebug_loc.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
12262 { { ".debug_macro.dwo", ".zdebug_macro.dwo", "", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
12263 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
12264 { { ".debug_str.dwo", ".zdebug_str.dwo", "", NO_ABBREVS }, display_debug_str, &do_debug_str, true },
12265 { { ".debug_str_offsets", ".zdebug_str_offsets", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12266 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
12267 { { ".debug_addr", ".zdebug_addr", "", NO_ABBREVS }, display_debug_addr, &do_debug_addr, true },
12268 { { ".debug_cu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12269 { { ".debug_tu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
12270 { { ".gnu_debuglink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12271 { { ".gnu_debugaltlink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
12272 { { ".debug_sup", "", "", NO_ABBREVS }, display_debug_sup, &do_debug_links, false },
12273 /* Separate debug info files can containt their own .debug_str section,
12274 and this might be in *addition* to a .debug_str section already present
12275 in the main file. Hence we need to have two entries for .debug_str. */
12276 { { ".debug_str", ".zdebug_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
12277 { { ".note.gnu.build-id", "", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
12280 /* A static assertion. */
12281 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];