Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / sdbinutils / binutils / dwarf.c
blobbc33e9f0814383d4fe6299549bc303cbcf480659
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 bool do_checks;
114 int dwarf_cutoff_level = -1;
115 unsigned long dwarf_start_die;
117 int dwarf_check = 0;
119 /* Convenient constant, to avoid having to cast -1 to dwarf_vma when
120 testing whether e.g. a locview list is present. */
121 static const dwarf_vma vm1 = -1;
123 /* Collection of CU/TU section sets from .debug_cu_index and .debug_tu_index
124 sections. For version 1 package files, each set is stored in SHNDX_POOL
125 as a zero-terminated list of section indexes comprising one set of debug
126 sections from a .dwo file. */
128 static unsigned int *shndx_pool = NULL;
129 static unsigned int shndx_pool_size = 0;
130 static unsigned int shndx_pool_used = 0;
132 /* For version 2 package files, each set contains an array of section offsets
133 and an array of section sizes, giving the offset and size of the
134 contribution from a CU or TU within one of the debug sections.
135 When displaying debug info from a package file, we need to use these
136 tables to locate the corresponding contributions to each section. */
138 struct cu_tu_set
140 uint64_t signature;
141 dwarf_vma section_offsets[DW_SECT_MAX];
142 size_t section_sizes[DW_SECT_MAX];
145 static int cu_count = 0;
146 static int tu_count = 0;
147 static struct cu_tu_set *cu_sets = NULL;
148 static struct cu_tu_set *tu_sets = NULL;
150 static bool load_cu_tu_indexes (void *);
152 /* An array that indicates for a given level of CU nesting whether
153 the latest DW_AT_type seen for that level was a signed type or
154 an unsigned type. */
155 #define MAX_CU_NESTING (1 << 8)
156 static bool level_type_signed[MAX_CU_NESTING];
158 /* Values for do_debug_lines. */
159 #define FLAG_DEBUG_LINES_RAW 1
160 #define FLAG_DEBUG_LINES_DECODED 2
162 static unsigned int
163 size_of_encoded_value (int encoding)
165 switch (encoding & 0x7)
167 default: /* ??? */
168 case 0: return eh_addr_size;
169 case 2: return 2;
170 case 3: return 4;
171 case 4: return 8;
175 static dwarf_vma
176 get_encoded_value (unsigned char **pdata,
177 int encoding,
178 struct dwarf_section *section,
179 unsigned char * end)
181 unsigned char * data = * pdata;
182 unsigned int size = size_of_encoded_value (encoding);
183 dwarf_vma val;
185 if (data >= end || size > (size_t) (end - data))
187 warn (_("Encoded value extends past end of section\n"));
188 * pdata = end;
189 return 0;
192 /* PR 17512: file: 002-829853-0.004. */
193 if (size > 8)
195 warn (_("Encoded size of %d is too large to read\n"), size);
196 * pdata = end;
197 return 0;
200 /* PR 17512: file: 1085-5603-0.004. */
201 if (size == 0)
203 warn (_("Encoded size of 0 is too small to read\n"));
204 * pdata = end;
205 return 0;
208 if (encoding & DW_EH_PE_signed)
209 val = byte_get_signed (data, size);
210 else
211 val = byte_get (data, size);
213 if ((encoding & 0x70) == DW_EH_PE_pcrel)
214 val += section->address + (data - section->start);
216 * pdata = data + size;
217 return val;
220 #if SIZEOF_LONG_LONG > SIZEOF_LONG
221 # ifndef __MINGW32__
222 # define DWARF_VMA_FMT "ll"
223 # define DWARF_VMA_FMT_LONG "%16.16llx"
224 # else
225 # define DWARF_VMA_FMT "I64"
226 # define DWARF_VMA_FMT_LONG "%016I64x"
227 # endif
228 #else
229 # define DWARF_VMA_FMT "l"
230 # define DWARF_VMA_FMT_LONG "%16.16lx"
231 #endif
233 /* Convert a dwarf vma value into a string. Returns a pointer to a static
234 buffer containing the converted VALUE. The value is converted according
235 to the printf formating character FMTCH. If NUM_BYTES is non-zero then
236 it specifies the maximum number of bytes to be displayed in the converted
237 value and FMTCH is ignored - hex is always used. */
239 static const char *
240 dwarf_vmatoa_1 (const char *fmtch, dwarf_vma value, unsigned num_bytes)
242 /* As dwarf_vmatoa is used more then once in a printf call
243 for output, we are cycling through an fixed array of pointers
244 for return address. */
245 static int buf_pos = 0;
246 static struct dwarf_vmatoa_buf
248 char place[64];
249 } buf[16];
250 char *ret;
252 ret = buf[buf_pos++].place;
253 buf_pos %= ARRAY_SIZE (buf);
255 if (num_bytes)
257 /* Printf does not have a way of specifying a maximum field width for an
258 integer value, so we print the full value into a buffer and then select
259 the precision we need. */
260 snprintf (ret, sizeof (buf[0].place), DWARF_VMA_FMT_LONG, value);
261 if (num_bytes > 8)
262 num_bytes = 8;
263 return ret + (16 - 2 * num_bytes);
265 else
267 char fmt[32];
269 if (fmtch)
270 sprintf (fmt, "%%%s%s", DWARF_VMA_FMT, fmtch);
271 else
272 sprintf (fmt, "%%%s", DWARF_VMA_FMT);
273 snprintf (ret, sizeof (buf[0].place), fmt, value);
274 return ret;
278 static inline const char *
279 dwarf_vmatoa (const char * fmtch, dwarf_vma value)
281 return dwarf_vmatoa_1 (fmtch, value, 0);
284 /* Print a dwarf_vma value (typically an address, offset or length) in
285 hexadecimal format, followed by a space. The length of the VALUE (and
286 hence the precision displayed) is determined by the NUM_BYTES parameter. */
288 static void
289 print_dwarf_vma (dwarf_vma value, unsigned num_bytes)
291 printf ("%s ", dwarf_vmatoa_1 (NULL, value, num_bytes));
294 /* Print a view number in hexadecimal value, with the same width
295 print_dwarf_vma would have printed it with the same num_bytes.
296 Print blanks for zero view, unless force is nonzero. */
298 static void
299 print_dwarf_view (dwarf_vma value, unsigned num_bytes, int force)
301 int len;
302 if (!num_bytes)
303 len = 4;
304 else
305 len = num_bytes * 2;
307 assert (value == (unsigned long) value);
308 if (value || force)
309 printf ("v%0*lx ", len - 1, (unsigned long) value);
310 else
311 printf ("%*s", len + 1, "");
314 /* Read in a LEB128 encoded value starting at address DATA.
315 If SIGN is true, return a signed LEB128 value.
316 If LENGTH_RETURN is not NULL, return in it the number of bytes read.
317 If STATUS_RETURN is not NULL, return with bit 0 (LSB) set if the
318 terminating byte was not found and with bit 1 set if the value
319 overflows a dwarf_vma.
320 No bytes will be read at address END or beyond. */
322 dwarf_vma
323 read_leb128 (unsigned char *data,
324 const unsigned char *const end,
325 bool sign,
326 unsigned int *length_return,
327 int *status_return)
329 dwarf_vma result = 0;
330 unsigned int num_read = 0;
331 unsigned int shift = 0;
332 int status = 1;
334 while (data < end)
336 unsigned char byte = *data++;
337 unsigned char lost, mask;
339 num_read++;
341 if (shift < CHAR_BIT * sizeof (result))
343 result |= ((dwarf_vma) (byte & 0x7f)) << shift;
344 /* These bits overflowed. */
345 lost = byte ^ (result >> shift);
346 /* And this is the mask of possible overflow bits. */
347 mask = 0x7f ^ ((dwarf_vma) 0x7f << shift >> shift);
348 shift += 7;
350 else
352 lost = byte;
353 mask = 0x7f;
355 if ((lost & mask) != (sign && (dwarf_signed_vma) result < 0 ? mask : 0))
356 status |= 2;
358 if ((byte & 0x80) == 0)
360 status &= ~1;
361 if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
362 result |= -((dwarf_vma) 1 << shift);
363 break;
367 if (length_return != NULL)
368 *length_return = num_read;
369 if (status_return != NULL)
370 *status_return = status;
372 return result;
375 /* Read AMOUNT bytes from PTR and store them in VAL.
376 Checks to make sure that the read will not reach or pass END.
377 FUNC chooses whether the value read is unsigned or signed, and may
378 be either byte_get or byte_get_signed. If INC is true, PTR is
379 incremented after reading the value.
380 This macro cannot protect against PTR values derived from user input.
381 The C standard sections 6.5.6 and 6.5.8 say attempts to do so using
382 pointers is undefined behaviour. */
383 #define SAFE_BYTE_GET_INTERNAL(VAL, PTR, AMOUNT, END, FUNC, INC) \
384 do \
386 size_t amount = (AMOUNT); \
387 if (sizeof (VAL) < amount) \
389 error (ngettext ("internal error: attempt to read %d byte " \
390 "of data in to %d sized variable", \
391 "internal error: attempt to read %d bytes " \
392 "of data in to %d sized variable", \
393 amount), \
394 (int) amount, (int) sizeof (VAL)); \
395 amount = sizeof (VAL); \
397 if (ENABLE_CHECKING) \
398 assert ((PTR) <= (END)); \
399 size_t avail = (END) - (PTR); \
400 if ((PTR) > (END)) \
401 avail = 0; \
402 if (amount > avail) \
403 amount = avail; \
404 if (amount == 0) \
405 (VAL) = 0; \
406 else \
407 (VAL) = (FUNC) ((PTR), amount); \
408 if (INC) \
409 (PTR) += amount; \
411 while (0)
413 #define SAFE_BYTE_GET(VAL, PTR, AMOUNT, END) \
414 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, false)
416 #define SAFE_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
417 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get, true)
419 #define SAFE_SIGNED_BYTE_GET(VAL, PTR, AMOUNT, END) \
420 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, false)
422 #define SAFE_SIGNED_BYTE_GET_AND_INC(VAL, PTR, AMOUNT, END) \
423 SAFE_BYTE_GET_INTERNAL (VAL, PTR, AMOUNT, END, byte_get_signed, true)
425 typedef struct State_Machine_Registers
427 dwarf_vma address;
428 unsigned int view;
429 unsigned int file;
430 unsigned int line;
431 unsigned int column;
432 int is_stmt;
433 int basic_block;
434 unsigned char op_index;
435 unsigned char end_sequence;
436 /* This variable hold the number of the last entry seen
437 in the File Table. */
438 unsigned int last_file_entry;
439 } SMR;
441 static SMR state_machine_regs;
443 static void
444 reset_state_machine (int is_stmt)
446 state_machine_regs.address = 0;
447 state_machine_regs.view = 0;
448 state_machine_regs.op_index = 0;
449 state_machine_regs.file = 1;
450 state_machine_regs.line = 1;
451 state_machine_regs.column = 0;
452 state_machine_regs.is_stmt = is_stmt;
453 state_machine_regs.basic_block = 0;
454 state_machine_regs.end_sequence = 0;
455 state_machine_regs.last_file_entry = 0;
458 /* Handled an extend line op.
459 Returns the number of bytes read. */
461 static size_t
462 process_extended_line_op (unsigned char * data,
463 int is_stmt,
464 unsigned char * end)
466 unsigned char op_code;
467 size_t len, header_len;
468 unsigned char *name;
469 unsigned char *orig_data = data;
470 dwarf_vma adr, val;
472 READ_ULEB (len, data, end);
473 header_len = data - orig_data;
475 if (len == 0 || data >= end || len > (size_t) (end - data))
477 warn (_("Badly formed extended line op encountered!\n"));
478 return header_len;
481 op_code = *data++;
483 printf (_(" Extended opcode %d: "), op_code);
485 switch (op_code)
487 case DW_LNE_end_sequence:
488 printf (_("End of Sequence\n\n"));
489 reset_state_machine (is_stmt);
490 break;
492 case DW_LNE_set_address:
493 /* PR 17512: file: 002-100480-0.004. */
494 if (len - 1 > 8)
496 warn (_("Length (%lu) of DW_LNE_set_address op is too long\n"),
497 (unsigned long) len - 1);
498 adr = 0;
500 else
501 SAFE_BYTE_GET (adr, data, len - 1, end);
502 printf (_("set Address to 0x%s\n"), dwarf_vmatoa ("x", adr));
503 state_machine_regs.address = adr;
504 state_machine_regs.view = 0;
505 state_machine_regs.op_index = 0;
506 break;
508 case DW_LNE_define_file:
509 printf (_("define new File Table entry\n"));
510 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
511 printf (" %d\t", ++state_machine_regs.last_file_entry);
514 size_t l;
516 name = data;
517 l = strnlen ((char *) data, end - data);
518 data += l;
519 if (data < end)
520 data++;
521 READ_ULEB (val, data, end);
522 printf ("%s\t", dwarf_vmatoa ("u", val));
523 READ_ULEB (val, data, end);
524 printf ("%s\t", dwarf_vmatoa ("u", val));
525 READ_ULEB (val, data, end);
526 printf ("%s\t", dwarf_vmatoa ("u", val));
527 printf ("%.*s\n\n", (int) l, name);
530 if (((size_t) (data - orig_data) != len + header_len) || data >= end)
531 warn (_("DW_LNE_define_file: Bad opcode length\n"));
532 break;
534 case DW_LNE_set_discriminator:
535 READ_ULEB (val, data, end);
536 printf (_("set Discriminator to %s\n"), dwarf_vmatoa ("u", val));
537 break;
539 /* HP extensions. */
540 case DW_LNE_HP_negate_is_UV_update:
541 printf ("DW_LNE_HP_negate_is_UV_update\n");
542 break;
543 case DW_LNE_HP_push_context:
544 printf ("DW_LNE_HP_push_context\n");
545 break;
546 case DW_LNE_HP_pop_context:
547 printf ("DW_LNE_HP_pop_context\n");
548 break;
549 case DW_LNE_HP_set_file_line_column:
550 printf ("DW_LNE_HP_set_file_line_column\n");
551 break;
552 case DW_LNE_HP_set_routine_name:
553 printf ("DW_LNE_HP_set_routine_name\n");
554 break;
555 case DW_LNE_HP_set_sequence:
556 printf ("DW_LNE_HP_set_sequence\n");
557 break;
558 case DW_LNE_HP_negate_post_semantics:
559 printf ("DW_LNE_HP_negate_post_semantics\n");
560 break;
561 case DW_LNE_HP_negate_function_exit:
562 printf ("DW_LNE_HP_negate_function_exit\n");
563 break;
564 case DW_LNE_HP_negate_front_end_logical:
565 printf ("DW_LNE_HP_negate_front_end_logical\n");
566 break;
567 case DW_LNE_HP_define_proc:
568 printf ("DW_LNE_HP_define_proc\n");
569 break;
570 case DW_LNE_HP_source_file_correlation:
572 unsigned char *edata = data + len - 1;
574 printf ("DW_LNE_HP_source_file_correlation\n");
576 while (data < edata)
578 unsigned int opc;
580 READ_ULEB (opc, data, edata);
582 switch (opc)
584 case DW_LNE_HP_SFC_formfeed:
585 printf (" DW_LNE_HP_SFC_formfeed\n");
586 break;
587 case DW_LNE_HP_SFC_set_listing_line:
588 READ_ULEB (val, data, edata);
589 printf (" DW_LNE_HP_SFC_set_listing_line (%s)\n",
590 dwarf_vmatoa ("u", val));
591 break;
592 case DW_LNE_HP_SFC_associate:
593 printf (" DW_LNE_HP_SFC_associate ");
594 READ_ULEB (val, data, edata);
595 printf ("(%s", dwarf_vmatoa ("u", val));
596 READ_ULEB (val, data, edata);
597 printf (",%s", dwarf_vmatoa ("u", val));
598 READ_ULEB (val, data, edata);
599 printf (",%s)\n", dwarf_vmatoa ("u", val));
600 break;
601 default:
602 printf (_(" UNKNOWN DW_LNE_HP_SFC opcode (%u)\n"), opc);
603 data = edata;
604 break;
608 break;
610 default:
612 unsigned int rlen = len - 1;
614 if (op_code >= DW_LNE_lo_user
615 /* The test against DW_LNW_hi_user is redundant due to
616 the limited range of the unsigned char data type used
617 for op_code. */
618 /*&& op_code <= DW_LNE_hi_user*/)
619 printf (_("user defined: "));
620 else
621 printf (_("UNKNOWN: "));
622 printf (_("length %d ["), rlen);
623 for (; rlen; rlen--)
624 printf (" %02x", *data++);
625 printf ("]\n");
627 break;
630 return len + header_len;
633 static const unsigned char *
634 fetch_indirect_string (dwarf_vma offset)
636 struct dwarf_section *section = &debug_displays [str].section;
637 const unsigned char * ret;
639 if (section->start == NULL)
640 return (const unsigned char *) _("<no .debug_str section>");
642 if (offset >= section->size)
644 warn (_("DW_FORM_strp offset too big: 0x%s\n"),
645 dwarf_vmatoa ("x", offset));
646 return (const unsigned char *) _("<offset is too big>");
649 ret = section->start + offset;
650 /* Unfortunately we cannot rely upon the .debug_str section ending with a
651 NUL byte. Since our caller is expecting to receive a well formed C
652 string we test for the lack of a terminating byte here. */
653 if (strnlen ((const char *) ret, section->size - offset)
654 == section->size - offset)
655 ret = (const unsigned char *)
656 _("<no NUL byte at end of .debug_str section>");
658 return ret;
661 static const unsigned char *
662 fetch_indirect_line_string (dwarf_vma offset)
664 struct dwarf_section *section = &debug_displays [line_str].section;
665 const unsigned char * ret;
667 if (section->start == NULL)
668 return (const unsigned char *) _("<no .debug_line_str section>");
670 if (offset >= section->size)
672 warn (_("DW_FORM_line_strp offset too big: 0x%s\n"),
673 dwarf_vmatoa ("x", offset));
674 return (const unsigned char *) _("<offset is too big>");
677 ret = section->start + offset;
678 /* Unfortunately we cannot rely upon the .debug_line_str section ending
679 with a NUL byte. Since our caller is expecting to receive a well formed
680 C string we test for the lack of a terminating byte here. */
681 if (strnlen ((const char *) ret, section->size - offset)
682 == section->size - offset)
683 ret = (const unsigned char *)
684 _("<no NUL byte at end of .debug_line_str section>");
686 return ret;
689 static const char *
690 fetch_indexed_string (dwarf_vma idx, struct cu_tu_set *this_set,
691 dwarf_vma offset_size, bool dwo)
693 enum dwarf_section_display_enum str_sec_idx = dwo ? str_dwo : str;
694 enum dwarf_section_display_enum idx_sec_idx = dwo ? str_index_dwo : str_index;
695 struct dwarf_section *index_section = &debug_displays [idx_sec_idx].section;
696 struct dwarf_section *str_section = &debug_displays [str_sec_idx].section;
697 dwarf_vma index_offset;
698 dwarf_vma str_offset;
699 const char * ret;
700 unsigned char *curr = index_section->start;
701 unsigned char *end = curr + index_section->size;
702 dwarf_vma length;
704 if (index_section->start == NULL)
705 return (dwo ? _("<no .debug_str_offsets.dwo section>")
706 : _("<no .debug_str_offsets section>"));
708 if (str_section->start == NULL)
709 return (dwo ? _("<no .debug_str.dwo section>")
710 : _("<no .debug_str section>"));
712 /* FIXME: We should cache the length... */
713 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
714 if (length == 0xffffffff)
716 if (offset_size != 8)
717 warn (_("Expected offset size of 8 but given %s"), dwarf_vmatoa ("x", offset_size));
718 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
720 else if (offset_size != 4)
722 warn (_("Expected offset size of 4 but given %s"), dwarf_vmatoa ("x", offset_size));
725 if (length == 0)
727 /* This is probably an old style .debug_str_offset section which
728 just contains offsets and no header (and the first offset is 0). */
729 curr = index_section->start;
730 length = index_section->size;
732 else
734 /* Skip the version and padding bytes.
735 We assume that they are correct. */
736 if (end - curr >= 4)
737 curr += 4;
738 else
739 curr = end;
740 if (length >= 4)
741 length -= 4;
742 else
743 length = 0;
745 if (this_set != NULL
746 && this_set->section_sizes[DW_SECT_STR_OFFSETS] < length)
747 length = this_set->section_sizes[DW_SECT_STR_OFFSETS];
749 if (length > (dwarf_vma) (end - curr))
751 warn (_("index table size too large for section %s vs %s\n"),
752 dwarf_vmatoa ("x", length),
753 dwarf_vmatoa ("x", index_section->size));
754 length = end - curr;
757 if (length < offset_size)
759 warn (_("index table size %s is too small\n"),
760 dwarf_vmatoa ("x", length));
761 return _("<table too small>");
765 index_offset = idx * offset_size;
767 if (this_set != NULL)
768 index_offset += this_set->section_offsets [DW_SECT_STR_OFFSETS];
770 if (index_offset >= length
771 || length - index_offset < offset_size)
773 warn (_("DW_FORM_GNU_str_index offset too big: 0x%s vs 0x%s\n"),
774 dwarf_vmatoa ("x", index_offset),
775 dwarf_vmatoa ("x", length));
776 return _("<index offset is too big>");
779 str_offset = byte_get (curr + index_offset, offset_size);
780 str_offset -= str_section->address;
781 if (str_offset >= str_section->size)
783 warn (_("DW_FORM_GNU_str_index indirect offset too big: 0x%s\n"),
784 dwarf_vmatoa ("x", str_offset));
785 return _("<indirect index offset is too big>");
788 ret = (const char *) str_section->start + str_offset;
789 /* Unfortunately we cannot rely upon str_section ending with a NUL byte.
790 Since our caller is expecting to receive a well formed C string we test
791 for the lack of a terminating byte here. */
792 if (strnlen (ret, str_section->size - str_offset)
793 == str_section->size - str_offset)
794 ret = (const char *) _("<no NUL byte at end of section>");
796 return ret;
799 static const char *
800 fetch_indexed_value (dwarf_vma offset, dwarf_vma bytes)
802 struct dwarf_section *section = &debug_displays [debug_addr].section;
804 if (section->start == NULL)
805 return (_("<no .debug_addr section>"));
807 if (offset + bytes > section->size)
809 warn (_("Offset into section %s too big: 0x%s\n"),
810 section->name, dwarf_vmatoa ("x", offset));
811 return "<offset too big>";
814 return dwarf_vmatoa ("x", byte_get (section->start + offset, bytes));
818 /* FIXME: There are better and more efficient ways to handle
819 these structures. For now though, I just want something that
820 is simple to implement. */
821 /* Records a single attribute in an abbrev. */
822 typedef struct abbrev_attr
824 unsigned long attribute;
825 unsigned long form;
826 dwarf_signed_vma implicit_const;
827 struct abbrev_attr * next;
829 abbrev_attr;
831 /* Records a single abbrev. */
832 typedef struct abbrev_entry
834 unsigned long number;
835 unsigned long tag;
836 int children;
837 struct abbrev_attr * first_attr;
838 struct abbrev_attr * last_attr;
839 struct abbrev_entry * next;
841 abbrev_entry;
843 /* Records a set of abbreviations. */
844 typedef struct abbrev_list
846 abbrev_entry * first_abbrev;
847 abbrev_entry * last_abbrev;
848 dwarf_vma abbrev_base;
849 dwarf_vma abbrev_offset;
850 struct abbrev_list * next;
851 unsigned char * start_of_next_abbrevs;
853 abbrev_list;
855 /* Records all the abbrevs found so far. */
856 static struct abbrev_list * abbrev_lists = NULL;
858 typedef struct abbrev_map
860 dwarf_vma start;
861 dwarf_vma end;
862 abbrev_list * list;
863 } abbrev_map;
865 /* Maps between CU offsets and abbrev sets. */
866 static abbrev_map * cu_abbrev_map = NULL;
867 static unsigned long num_abbrev_map_entries = 0;
868 static unsigned long next_free_abbrev_map_entry = 0;
870 #define INITIAL_NUM_ABBREV_MAP_ENTRIES 8
871 #define ABBREV_MAP_ENTRIES_INCREMENT 8
873 static void
874 record_abbrev_list_for_cu (dwarf_vma start, dwarf_vma end, abbrev_list * list)
876 if (cu_abbrev_map == NULL)
878 num_abbrev_map_entries = INITIAL_NUM_ABBREV_MAP_ENTRIES;
879 cu_abbrev_map = xmalloc (num_abbrev_map_entries * sizeof (* cu_abbrev_map));
881 else if (next_free_abbrev_map_entry == num_abbrev_map_entries)
883 num_abbrev_map_entries += ABBREV_MAP_ENTRIES_INCREMENT;
884 cu_abbrev_map = xrealloc (cu_abbrev_map, num_abbrev_map_entries * sizeof (* cu_abbrev_map));
887 cu_abbrev_map[next_free_abbrev_map_entry].start = start;
888 cu_abbrev_map[next_free_abbrev_map_entry].end = end;
889 cu_abbrev_map[next_free_abbrev_map_entry].list = list;
890 next_free_abbrev_map_entry ++;
893 static void
894 free_all_abbrevs (void)
896 abbrev_list * list;
898 for (list = abbrev_lists; list != NULL;)
900 abbrev_list * next = list->next;
901 abbrev_entry * abbrv;
903 for (abbrv = list->first_abbrev; abbrv != NULL;)
905 abbrev_entry * next_abbrev = abbrv->next;
906 abbrev_attr * attr;
908 for (attr = abbrv->first_attr; attr;)
910 abbrev_attr *next_attr = attr->next;
912 free (attr);
913 attr = next_attr;
916 free (abbrv);
917 abbrv = next_abbrev;
920 free (list);
921 list = next;
924 abbrev_lists = NULL;
927 static abbrev_list *
928 new_abbrev_list (dwarf_vma abbrev_base, dwarf_vma abbrev_offset)
930 abbrev_list * list = (abbrev_list *) xcalloc (sizeof * list, 1);
932 list->abbrev_base = abbrev_base;
933 list->abbrev_offset = abbrev_offset;
935 list->next = abbrev_lists;
936 abbrev_lists = list;
938 return list;
941 static abbrev_list *
942 find_abbrev_list_by_abbrev_offset (dwarf_vma abbrev_base,
943 dwarf_vma abbrev_offset)
945 abbrev_list * list;
947 for (list = abbrev_lists; list != NULL; list = list->next)
948 if (list->abbrev_base == abbrev_base
949 && list->abbrev_offset == abbrev_offset)
950 return list;
952 return NULL;
955 /* Find the abbreviation map for the CU that includes OFFSET.
956 OFFSET is an absolute offset from the start of the .debug_info section. */
957 /* FIXME: This function is going to slow down readelf & objdump.
958 Consider using a better algorithm to mitigate this effect. */
960 static abbrev_map *
961 find_abbrev_map_by_offset (dwarf_vma offset)
963 unsigned long i;
965 for (i = 0; i < next_free_abbrev_map_entry; i++)
966 if (cu_abbrev_map[i].start <= offset
967 && cu_abbrev_map[i].end > offset)
968 return cu_abbrev_map + i;
970 return NULL;
973 static void
974 add_abbrev (unsigned long number,
975 unsigned long tag,
976 int children,
977 abbrev_list * list)
979 abbrev_entry * entry;
981 entry = (abbrev_entry *) xmalloc (sizeof (*entry));
983 entry->number = number;
984 entry->tag = tag;
985 entry->children = children;
986 entry->first_attr = NULL;
987 entry->last_attr = NULL;
988 entry->next = NULL;
990 assert (list != NULL);
992 if (list->first_abbrev == NULL)
993 list->first_abbrev = entry;
994 else
995 list->last_abbrev->next = entry;
997 list->last_abbrev = entry;
1000 static void
1001 add_abbrev_attr (unsigned long attribute,
1002 unsigned long form,
1003 dwarf_signed_vma implicit_const,
1004 abbrev_list * list)
1006 abbrev_attr *attr;
1008 attr = (abbrev_attr *) xmalloc (sizeof (*attr));
1010 attr->attribute = attribute;
1011 attr->form = form;
1012 attr->implicit_const = implicit_const;
1013 attr->next = NULL;
1015 assert (list != NULL && list->last_abbrev != NULL);
1017 if (list->last_abbrev->first_attr == NULL)
1018 list->last_abbrev->first_attr = attr;
1019 else
1020 list->last_abbrev->last_attr->next = attr;
1022 list->last_abbrev->last_attr = attr;
1025 /* Processes the (partial) contents of a .debug_abbrev section.
1026 Returns NULL if the end of the section was encountered.
1027 Returns the address after the last byte read if the end of
1028 an abbreviation set was found. */
1030 static unsigned char *
1031 process_abbrev_set (struct dwarf_section *section,
1032 dwarf_vma abbrev_base,
1033 dwarf_vma abbrev_size,
1034 dwarf_vma abbrev_offset,
1035 abbrev_list *list)
1037 if (abbrev_base >= section->size
1038 || abbrev_size > section->size - abbrev_base)
1040 /* PR 17531: file:4bcd9ce9. */
1041 warn (_("Debug info is corrupted, abbrev size (%lx) is larger than "
1042 "abbrev section size (%lx)\n"),
1043 (unsigned long) (abbrev_base + abbrev_size),
1044 (unsigned long) section->size);
1045 return NULL;
1047 if (abbrev_offset >= abbrev_size)
1049 warn (_("Debug info is corrupted, abbrev offset (%lx) is larger than "
1050 "abbrev section size (%lx)\n"),
1051 (unsigned long) abbrev_offset,
1052 (unsigned long) abbrev_size);
1053 return NULL;
1056 unsigned char *start = section->start + abbrev_base;
1057 unsigned char *end = start + abbrev_size;
1058 start += abbrev_offset;
1059 while (start < end)
1061 unsigned long entry;
1062 unsigned long tag;
1063 unsigned long attribute;
1064 int children;
1066 READ_ULEB (entry, start, end);
1068 /* A single zero is supposed to end the set according
1069 to the standard. If there's more, then signal that to
1070 the caller. */
1071 if (start == end)
1072 return NULL;
1073 if (entry == 0)
1074 return start;
1076 READ_ULEB (tag, start, end);
1077 if (start == end)
1078 return NULL;
1080 children = *start++;
1082 add_abbrev (entry, tag, children, list);
1086 unsigned long form;
1087 /* Initialize it due to a false compiler warning. */
1088 dwarf_signed_vma implicit_const = -1;
1090 READ_ULEB (attribute, start, end);
1091 if (start == end)
1092 break;
1094 READ_ULEB (form, start, end);
1095 if (start == end)
1096 break;
1098 if (form == DW_FORM_implicit_const)
1100 READ_SLEB (implicit_const, start, end);
1101 if (start == end)
1102 break;
1105 add_abbrev_attr (attribute, form, implicit_const, list);
1107 while (attribute != 0);
1110 /* Report the missing single zero which ends the section. */
1111 error (_(".debug_abbrev section not zero terminated\n"));
1113 return NULL;
1116 static const char *
1117 get_TAG_name (unsigned long tag)
1119 const char *name = get_DW_TAG_name ((unsigned int) tag);
1121 if (name == NULL)
1123 static char buffer[100];
1125 if (tag >= DW_TAG_lo_user && tag <= DW_TAG_hi_user)
1126 snprintf (buffer, sizeof (buffer), _("User TAG value: %#lx"), tag);
1127 else
1128 snprintf (buffer, sizeof (buffer), _("Unknown TAG value: %#lx"), tag);
1129 return buffer;
1132 return name;
1135 static const char *
1136 get_FORM_name (unsigned long form)
1138 const char *name;
1140 if (form == 0)
1141 return "DW_FORM value: 0";
1143 name = get_DW_FORM_name (form);
1144 if (name == NULL)
1146 static char buffer[100];
1148 snprintf (buffer, sizeof (buffer), _("Unknown FORM value: %lx"), form);
1149 return buffer;
1152 return name;
1155 static const char *
1156 get_IDX_name (unsigned long idx)
1158 const char *name = get_DW_IDX_name ((unsigned int) idx);
1160 if (name == NULL)
1162 static char buffer[100];
1164 snprintf (buffer, sizeof (buffer), _("Unknown IDX value: %lx"), idx);
1165 return buffer;
1168 return name;
1171 static unsigned char *
1172 display_block (unsigned char *data,
1173 dwarf_vma length,
1174 const unsigned char * const end, char delimiter)
1176 dwarf_vma maxlen;
1178 printf (_("%c%s byte block: "), delimiter, dwarf_vmatoa ("u", length));
1179 if (data > end)
1180 return (unsigned char *) end;
1182 maxlen = (dwarf_vma) (end - data);
1183 length = length > maxlen ? maxlen : length;
1185 while (length --)
1186 printf ("%lx ", (unsigned long) byte_get (data++, 1));
1188 return data;
1191 static int
1192 decode_location_expression (unsigned char * data,
1193 unsigned int pointer_size,
1194 unsigned int offset_size,
1195 int dwarf_version,
1196 dwarf_vma length,
1197 dwarf_vma cu_offset,
1198 struct dwarf_section * section)
1200 unsigned op;
1201 dwarf_vma uvalue;
1202 dwarf_signed_vma svalue;
1203 unsigned char *end = data + length;
1204 int need_frame_base = 0;
1206 while (data < end)
1208 op = *data++;
1210 switch (op)
1212 case DW_OP_addr:
1213 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1214 printf ("DW_OP_addr: %s", dwarf_vmatoa ("x", uvalue));
1215 break;
1216 case DW_OP_deref:
1217 printf ("DW_OP_deref");
1218 break;
1219 case DW_OP_const1u:
1220 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1221 printf ("DW_OP_const1u: %lu", (unsigned long) uvalue);
1222 break;
1223 case DW_OP_const1s:
1224 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 1, end);
1225 printf ("DW_OP_const1s: %ld", (long) svalue);
1226 break;
1227 case DW_OP_const2u:
1228 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1229 printf ("DW_OP_const2u: %lu", (unsigned long) uvalue);
1230 break;
1231 case DW_OP_const2s:
1232 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1233 printf ("DW_OP_const2s: %ld", (long) svalue);
1234 break;
1235 case DW_OP_const4u:
1236 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1237 printf ("DW_OP_const4u: %lu", (unsigned long) uvalue);
1238 break;
1239 case DW_OP_const4s:
1240 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1241 printf ("DW_OP_const4s: %ld", (long) svalue);
1242 break;
1243 case DW_OP_const8u:
1244 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1245 printf ("DW_OP_const8u: %lu ", (unsigned long) uvalue);
1246 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1247 printf ("%lu", (unsigned long) uvalue);
1248 break;
1249 case DW_OP_const8s:
1250 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1251 printf ("DW_OP_const8s: %ld ", (long) svalue);
1252 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1253 printf ("%ld", (long) svalue);
1254 break;
1255 case DW_OP_constu:
1256 READ_ULEB (uvalue, data, end);
1257 printf ("DW_OP_constu: %s", dwarf_vmatoa ("u", uvalue));
1258 break;
1259 case DW_OP_consts:
1260 READ_SLEB (svalue, data, end);
1261 printf ("DW_OP_consts: %s", dwarf_vmatoa ("d", svalue));
1262 break;
1263 case DW_OP_dup:
1264 printf ("DW_OP_dup");
1265 break;
1266 case DW_OP_drop:
1267 printf ("DW_OP_drop");
1268 break;
1269 case DW_OP_over:
1270 printf ("DW_OP_over");
1271 break;
1272 case DW_OP_pick:
1273 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1274 printf ("DW_OP_pick: %ld", (unsigned long) uvalue);
1275 break;
1276 case DW_OP_swap:
1277 printf ("DW_OP_swap");
1278 break;
1279 case DW_OP_rot:
1280 printf ("DW_OP_rot");
1281 break;
1282 case DW_OP_xderef:
1283 printf ("DW_OP_xderef");
1284 break;
1285 case DW_OP_abs:
1286 printf ("DW_OP_abs");
1287 break;
1288 case DW_OP_and:
1289 printf ("DW_OP_and");
1290 break;
1291 case DW_OP_div:
1292 printf ("DW_OP_div");
1293 break;
1294 case DW_OP_minus:
1295 printf ("DW_OP_minus");
1296 break;
1297 case DW_OP_mod:
1298 printf ("DW_OP_mod");
1299 break;
1300 case DW_OP_mul:
1301 printf ("DW_OP_mul");
1302 break;
1303 case DW_OP_neg:
1304 printf ("DW_OP_neg");
1305 break;
1306 case DW_OP_not:
1307 printf ("DW_OP_not");
1308 break;
1309 case DW_OP_or:
1310 printf ("DW_OP_or");
1311 break;
1312 case DW_OP_plus:
1313 printf ("DW_OP_plus");
1314 break;
1315 case DW_OP_plus_uconst:
1316 READ_ULEB (uvalue, data, end);
1317 printf ("DW_OP_plus_uconst: %s", dwarf_vmatoa ("u", uvalue));
1318 break;
1319 case DW_OP_shl:
1320 printf ("DW_OP_shl");
1321 break;
1322 case DW_OP_shr:
1323 printf ("DW_OP_shr");
1324 break;
1325 case DW_OP_shra:
1326 printf ("DW_OP_shra");
1327 break;
1328 case DW_OP_xor:
1329 printf ("DW_OP_xor");
1330 break;
1331 case DW_OP_bra:
1332 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1333 printf ("DW_OP_bra: %ld", (long) svalue);
1334 break;
1335 case DW_OP_eq:
1336 printf ("DW_OP_eq");
1337 break;
1338 case DW_OP_ge:
1339 printf ("DW_OP_ge");
1340 break;
1341 case DW_OP_gt:
1342 printf ("DW_OP_gt");
1343 break;
1344 case DW_OP_le:
1345 printf ("DW_OP_le");
1346 break;
1347 case DW_OP_lt:
1348 printf ("DW_OP_lt");
1349 break;
1350 case DW_OP_ne:
1351 printf ("DW_OP_ne");
1352 break;
1353 case DW_OP_skip:
1354 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1355 printf ("DW_OP_skip: %ld", (long) svalue);
1356 break;
1358 case DW_OP_lit0:
1359 case DW_OP_lit1:
1360 case DW_OP_lit2:
1361 case DW_OP_lit3:
1362 case DW_OP_lit4:
1363 case DW_OP_lit5:
1364 case DW_OP_lit6:
1365 case DW_OP_lit7:
1366 case DW_OP_lit8:
1367 case DW_OP_lit9:
1368 case DW_OP_lit10:
1369 case DW_OP_lit11:
1370 case DW_OP_lit12:
1371 case DW_OP_lit13:
1372 case DW_OP_lit14:
1373 case DW_OP_lit15:
1374 case DW_OP_lit16:
1375 case DW_OP_lit17:
1376 case DW_OP_lit18:
1377 case DW_OP_lit19:
1378 case DW_OP_lit20:
1379 case DW_OP_lit21:
1380 case DW_OP_lit22:
1381 case DW_OP_lit23:
1382 case DW_OP_lit24:
1383 case DW_OP_lit25:
1384 case DW_OP_lit26:
1385 case DW_OP_lit27:
1386 case DW_OP_lit28:
1387 case DW_OP_lit29:
1388 case DW_OP_lit30:
1389 case DW_OP_lit31:
1390 printf ("DW_OP_lit%d", op - DW_OP_lit0);
1391 break;
1393 case DW_OP_reg0:
1394 case DW_OP_reg1:
1395 case DW_OP_reg2:
1396 case DW_OP_reg3:
1397 case DW_OP_reg4:
1398 case DW_OP_reg5:
1399 case DW_OP_reg6:
1400 case DW_OP_reg7:
1401 case DW_OP_reg8:
1402 case DW_OP_reg9:
1403 case DW_OP_reg10:
1404 case DW_OP_reg11:
1405 case DW_OP_reg12:
1406 case DW_OP_reg13:
1407 case DW_OP_reg14:
1408 case DW_OP_reg15:
1409 case DW_OP_reg16:
1410 case DW_OP_reg17:
1411 case DW_OP_reg18:
1412 case DW_OP_reg19:
1413 case DW_OP_reg20:
1414 case DW_OP_reg21:
1415 case DW_OP_reg22:
1416 case DW_OP_reg23:
1417 case DW_OP_reg24:
1418 case DW_OP_reg25:
1419 case DW_OP_reg26:
1420 case DW_OP_reg27:
1421 case DW_OP_reg28:
1422 case DW_OP_reg29:
1423 case DW_OP_reg30:
1424 case DW_OP_reg31:
1425 printf ("DW_OP_reg%d (%s)", op - DW_OP_reg0,
1426 regname (op - DW_OP_reg0, 1));
1427 break;
1429 case DW_OP_breg0:
1430 case DW_OP_breg1:
1431 case DW_OP_breg2:
1432 case DW_OP_breg3:
1433 case DW_OP_breg4:
1434 case DW_OP_breg5:
1435 case DW_OP_breg6:
1436 case DW_OP_breg7:
1437 case DW_OP_breg8:
1438 case DW_OP_breg9:
1439 case DW_OP_breg10:
1440 case DW_OP_breg11:
1441 case DW_OP_breg12:
1442 case DW_OP_breg13:
1443 case DW_OP_breg14:
1444 case DW_OP_breg15:
1445 case DW_OP_breg16:
1446 case DW_OP_breg17:
1447 case DW_OP_breg18:
1448 case DW_OP_breg19:
1449 case DW_OP_breg20:
1450 case DW_OP_breg21:
1451 case DW_OP_breg22:
1452 case DW_OP_breg23:
1453 case DW_OP_breg24:
1454 case DW_OP_breg25:
1455 case DW_OP_breg26:
1456 case DW_OP_breg27:
1457 case DW_OP_breg28:
1458 case DW_OP_breg29:
1459 case DW_OP_breg30:
1460 case DW_OP_breg31:
1461 READ_SLEB (svalue, data, end);
1462 printf ("DW_OP_breg%d (%s): %s", op - DW_OP_breg0,
1463 regname (op - DW_OP_breg0, 1), dwarf_vmatoa ("d", svalue));
1464 break;
1466 case DW_OP_regx:
1467 READ_ULEB (uvalue, data, end);
1468 printf ("DW_OP_regx: %s (%s)",
1469 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1470 break;
1471 case DW_OP_fbreg:
1472 need_frame_base = 1;
1473 READ_SLEB (svalue, data, end);
1474 printf ("DW_OP_fbreg: %s", dwarf_vmatoa ("d", svalue));
1475 break;
1476 case DW_OP_bregx:
1477 READ_ULEB (uvalue, data, end);
1478 READ_SLEB (svalue, data, end);
1479 printf ("DW_OP_bregx: %s (%s) %s",
1480 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1),
1481 dwarf_vmatoa ("d", svalue));
1482 break;
1483 case DW_OP_piece:
1484 READ_ULEB (uvalue, data, end);
1485 printf ("DW_OP_piece: %s", dwarf_vmatoa ("u", uvalue));
1486 break;
1487 case DW_OP_deref_size:
1488 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1489 printf ("DW_OP_deref_size: %ld", (long) uvalue);
1490 break;
1491 case DW_OP_xderef_size:
1492 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1493 printf ("DW_OP_xderef_size: %ld", (long) uvalue);
1494 break;
1495 case DW_OP_nop:
1496 printf ("DW_OP_nop");
1497 break;
1499 /* DWARF 3 extensions. */
1500 case DW_OP_push_object_address:
1501 printf ("DW_OP_push_object_address");
1502 break;
1503 case DW_OP_call2:
1504 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1505 this ought to be an 8-byte wide computation. */
1506 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 2, end);
1507 printf ("DW_OP_call2: <0x%s>",
1508 dwarf_vmatoa ("x", svalue + cu_offset));
1509 break;
1510 case DW_OP_call4:
1511 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1512 this ought to be an 8-byte wide computation. */
1513 SAFE_SIGNED_BYTE_GET_AND_INC (svalue, data, 4, end);
1514 printf ("DW_OP_call4: <0x%s>",
1515 dwarf_vmatoa ("x", svalue + cu_offset));
1516 break;
1517 case DW_OP_call_ref:
1518 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1519 this ought to be an 8-byte wide computation. */
1520 if (dwarf_version == -1)
1522 printf (_("(DW_OP_call_ref in frame info)"));
1523 /* No way to tell where the next op is, so just bail. */
1524 return need_frame_base;
1526 if (dwarf_version == 2)
1528 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1530 else
1532 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1534 printf ("DW_OP_call_ref: <0x%s>", dwarf_vmatoa ("x", uvalue));
1535 break;
1536 case DW_OP_form_tls_address:
1537 printf ("DW_OP_form_tls_address");
1538 break;
1539 case DW_OP_call_frame_cfa:
1540 printf ("DW_OP_call_frame_cfa");
1541 break;
1542 case DW_OP_bit_piece:
1543 printf ("DW_OP_bit_piece: ");
1544 READ_ULEB (uvalue, data, end);
1545 printf (_("size: %s "), dwarf_vmatoa ("u", uvalue));
1546 READ_ULEB (uvalue, data, end);
1547 printf (_("offset: %s "), dwarf_vmatoa ("u", uvalue));
1548 break;
1550 /* DWARF 4 extensions. */
1551 case DW_OP_stack_value:
1552 printf ("DW_OP_stack_value");
1553 break;
1555 case DW_OP_implicit_value:
1556 printf ("DW_OP_implicit_value");
1557 READ_ULEB (uvalue, data, end);
1558 data = display_block (data, uvalue, end, ' ');
1559 break;
1561 /* GNU extensions. */
1562 case DW_OP_GNU_push_tls_address:
1563 printf (_("DW_OP_GNU_push_tls_address or DW_OP_HP_unknown"));
1564 break;
1565 case DW_OP_GNU_uninit:
1566 printf ("DW_OP_GNU_uninit");
1567 /* FIXME: Is there data associated with this OP ? */
1568 break;
1569 case DW_OP_GNU_encoded_addr:
1571 int encoding = 0;
1572 dwarf_vma addr;
1574 if (data < end)
1575 encoding = *data++;
1576 addr = get_encoded_value (&data, encoding, section, end);
1578 printf ("DW_OP_GNU_encoded_addr: fmt:%02x addr:", encoding);
1579 print_dwarf_vma (addr, pointer_size);
1581 break;
1582 case DW_OP_implicit_pointer:
1583 case DW_OP_GNU_implicit_pointer:
1584 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1585 this ought to be an 8-byte wide computation. */
1586 if (dwarf_version == -1)
1588 printf (_("(%s in frame info)"),
1589 (op == DW_OP_implicit_pointer
1590 ? "DW_OP_implicit_pointer"
1591 : "DW_OP_GNU_implicit_pointer"));
1592 /* No way to tell where the next op is, so just bail. */
1593 return need_frame_base;
1595 if (dwarf_version == 2)
1597 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1599 else
1601 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1603 READ_SLEB (svalue, data, end);
1604 printf ("%s: <0x%s> %s",
1605 (op == DW_OP_implicit_pointer
1606 ? "DW_OP_implicit_pointer" : "DW_OP_GNU_implicit_pointer"),
1607 dwarf_vmatoa ("x", uvalue),
1608 dwarf_vmatoa ("d", svalue));
1609 break;
1610 case DW_OP_entry_value:
1611 case DW_OP_GNU_entry_value:
1612 READ_ULEB (uvalue, data, end);
1613 /* PR 17531: file: 0cc9cd00. */
1614 if (uvalue > (dwarf_vma) (end - data))
1615 uvalue = end - data;
1616 printf ("%s: (", (op == DW_OP_entry_value ? "DW_OP_entry_value"
1617 : "DW_OP_GNU_entry_value"));
1618 if (decode_location_expression (data, pointer_size, offset_size,
1619 dwarf_version, uvalue,
1620 cu_offset, section))
1621 need_frame_base = 1;
1622 putchar (')');
1623 data += uvalue;
1624 break;
1625 case DW_OP_const_type:
1626 case DW_OP_GNU_const_type:
1627 READ_ULEB (uvalue, data, end);
1628 printf ("%s: <0x%s> ",
1629 (op == DW_OP_const_type ? "DW_OP_const_type"
1630 : "DW_OP_GNU_const_type"),
1631 dwarf_vmatoa ("x", cu_offset + uvalue));
1632 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1633 data = display_block (data, uvalue, end, ' ');
1634 break;
1635 case DW_OP_regval_type:
1636 case DW_OP_GNU_regval_type:
1637 READ_ULEB (uvalue, data, end);
1638 printf ("%s: %s (%s)",
1639 (op == DW_OP_regval_type ? "DW_OP_regval_type"
1640 : "DW_OP_GNU_regval_type"),
1641 dwarf_vmatoa ("u", uvalue), regname (uvalue, 1));
1642 READ_ULEB (uvalue, data, end);
1643 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1644 break;
1645 case DW_OP_deref_type:
1646 case DW_OP_GNU_deref_type:
1647 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1648 printf ("%s: %ld",
1649 (op == DW_OP_deref_type ? "DW_OP_deref_type"
1650 : "DW_OP_GNU_deref_type"),
1651 (long) uvalue);
1652 READ_ULEB (uvalue, data, end);
1653 printf (" <0x%s>", dwarf_vmatoa ("x", cu_offset + uvalue));
1654 break;
1655 case DW_OP_convert:
1656 case DW_OP_GNU_convert:
1657 READ_ULEB (uvalue, data, end);
1658 printf ("%s <0x%s>",
1659 (op == DW_OP_convert ? "DW_OP_convert" : "DW_OP_GNU_convert"),
1660 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1661 break;
1662 case DW_OP_reinterpret:
1663 case DW_OP_GNU_reinterpret:
1664 READ_ULEB (uvalue, data, end);
1665 printf ("%s <0x%s>",
1666 (op == DW_OP_reinterpret ? "DW_OP_reinterpret"
1667 : "DW_OP_GNU_reinterpret"),
1668 dwarf_vmatoa ("x", uvalue ? cu_offset + uvalue : 0));
1669 break;
1670 case DW_OP_GNU_parameter_ref:
1671 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1672 printf ("DW_OP_GNU_parameter_ref: <0x%s>",
1673 dwarf_vmatoa ("x", cu_offset + uvalue));
1674 break;
1675 case DW_OP_GNU_addr_index:
1676 READ_ULEB (uvalue, data, end);
1677 printf ("DW_OP_GNU_addr_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1678 break;
1679 case DW_OP_GNU_const_index:
1680 READ_ULEB (uvalue, data, end);
1681 printf ("DW_OP_GNU_const_index <0x%s>", dwarf_vmatoa ("x", uvalue));
1682 break;
1683 case DW_OP_GNU_variable_value:
1684 /* FIXME: Strictly speaking for 64-bit DWARF3 files
1685 this ought to be an 8-byte wide computation. */
1686 if (dwarf_version == -1)
1688 printf (_("(DW_OP_GNU_variable_value in frame info)"));
1689 /* No way to tell where the next op is, so just bail. */
1690 return need_frame_base;
1692 if (dwarf_version == 2)
1694 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1696 else
1698 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1700 printf ("DW_OP_GNU_variable_value: <0x%s>", dwarf_vmatoa ("x", uvalue));
1701 break;
1703 /* HP extensions. */
1704 case DW_OP_HP_is_value:
1705 printf ("DW_OP_HP_is_value");
1706 /* FIXME: Is there data associated with this OP ? */
1707 break;
1708 case DW_OP_HP_fltconst4:
1709 printf ("DW_OP_HP_fltconst4");
1710 /* FIXME: Is there data associated with this OP ? */
1711 break;
1712 case DW_OP_HP_fltconst8:
1713 printf ("DW_OP_HP_fltconst8");
1714 /* FIXME: Is there data associated with this OP ? */
1715 break;
1716 case DW_OP_HP_mod_range:
1717 printf ("DW_OP_HP_mod_range");
1718 /* FIXME: Is there data associated with this OP ? */
1719 break;
1720 case DW_OP_HP_unmod_range:
1721 printf ("DW_OP_HP_unmod_range");
1722 /* FIXME: Is there data associated with this OP ? */
1723 break;
1724 case DW_OP_HP_tls:
1725 printf ("DW_OP_HP_tls");
1726 /* FIXME: Is there data associated with this OP ? */
1727 break;
1729 /* PGI (STMicroelectronics) extensions. */
1730 case DW_OP_PGI_omp_thread_num:
1731 /* Pushes the thread number for the current thread as it would be
1732 returned by the standard OpenMP library function:
1733 omp_get_thread_num(). The "current thread" is the thread for
1734 which the expression is being evaluated. */
1735 printf ("DW_OP_PGI_omp_thread_num");
1736 break;
1738 default:
1739 if (op >= DW_OP_lo_user
1740 && op <= DW_OP_hi_user)
1741 printf (_("(User defined location op 0x%x)"), op);
1742 else
1743 printf (_("(Unknown location op 0x%x)"), op);
1744 /* No way to tell where the next op is, so just bail. */
1745 return need_frame_base;
1748 /* Separate the ops. */
1749 if (data < end)
1750 printf ("; ");
1753 return need_frame_base;
1756 /* Find the CU or TU set corresponding to the given CU_OFFSET.
1757 This is used for DWARF package files. */
1759 static struct cu_tu_set *
1760 find_cu_tu_set_v2 (dwarf_vma cu_offset, int do_types)
1762 struct cu_tu_set *p;
1763 unsigned int nsets;
1764 unsigned int dw_sect;
1766 if (do_types)
1768 p = tu_sets;
1769 nsets = tu_count;
1770 dw_sect = DW_SECT_TYPES;
1772 else
1774 p = cu_sets;
1775 nsets = cu_count;
1776 dw_sect = DW_SECT_INFO;
1778 while (nsets > 0)
1780 if (p->section_offsets [dw_sect] == cu_offset)
1781 return p;
1782 p++;
1783 nsets--;
1785 return NULL;
1788 static const char *
1789 fetch_alt_indirect_string (dwarf_vma offset)
1791 separate_info * i;
1793 if (! do_follow_links)
1794 return "";
1796 if (first_separate_info == NULL)
1797 return _("<no links available>");
1799 for (i = first_separate_info; i != NULL; i = i->next)
1801 struct dwarf_section * section;
1802 const char * ret;
1804 if (! load_debug_section (separate_debug_str, i->handle))
1805 continue;
1807 section = &debug_displays [separate_debug_str].section;
1809 if (section->start == NULL)
1810 continue;
1812 if (offset >= section->size)
1813 continue;
1815 ret = (const char *) (section->start + offset);
1816 /* Unfortunately we cannot rely upon the .debug_str section ending with a
1817 NUL byte. Since our caller is expecting to receive a well formed C
1818 string we test for the lack of a terminating byte here. */
1819 if (strnlen ((const char *) ret, section->size - offset)
1820 == section->size - offset)
1821 return _("<no NUL byte at end of alt .debug_str section>");
1823 return ret;
1826 warn (_("DW_FORM_GNU_strp_alt offset (%s) too big or no string sections available\n"),
1827 dwarf_vmatoa ("x", offset));
1828 return _("<offset is too big>");
1831 static const char *
1832 get_AT_name (unsigned long attribute)
1834 const char *name;
1836 if (attribute == 0)
1837 return "DW_AT value: 0";
1839 /* One value is shared by the MIPS and HP extensions: */
1840 if (attribute == DW_AT_MIPS_fde)
1841 return "DW_AT_MIPS_fde or DW_AT_HP_unmodifiable";
1843 name = get_DW_AT_name (attribute);
1845 if (name == NULL)
1847 static char buffer[100];
1849 snprintf (buffer, sizeof (buffer), _("Unknown AT value: %lx"),
1850 attribute);
1851 return buffer;
1854 return name;
1857 static void
1858 add_dwo_info (const char * value, dwarf_vma cu_offset, dwo_type type)
1860 dwo_info * dwinfo = xmalloc (sizeof * dwinfo);
1862 dwinfo->type = type;
1863 dwinfo->value = value;
1864 dwinfo->cu_offset = cu_offset;
1865 dwinfo->next = first_dwo_info;
1866 first_dwo_info = dwinfo;
1869 static void
1870 add_dwo_name (const char * name, dwarf_vma cu_offset)
1872 add_dwo_info (name, cu_offset, DWO_NAME);
1875 static void
1876 add_dwo_dir (const char * dir, dwarf_vma cu_offset)
1878 add_dwo_info (dir, cu_offset, DWO_DIR);
1881 static void
1882 add_dwo_id (const char * id, dwarf_vma cu_offset)
1884 add_dwo_info (id, cu_offset, DWO_ID);
1887 static void
1888 free_dwo_info (void)
1890 dwo_info * dwinfo;
1891 dwo_info * next;
1893 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = next)
1895 next = dwinfo->next;
1896 free (dwinfo);
1898 first_dwo_info = NULL;
1901 /* Ensure that START + UVALUE is less than END.
1902 Return an adjusted UVALUE if necessary to ensure this relationship. */
1904 static inline dwarf_vma
1905 check_uvalue (const unsigned char * start,
1906 dwarf_vma uvalue,
1907 const unsigned char * end)
1909 dwarf_vma max_uvalue = end - start;
1911 /* See PR 17512: file: 008-103549-0.001:0.1.
1912 and PR 24829 for examples of where these tests are triggered. */
1913 if (uvalue > max_uvalue)
1915 warn (_("Corrupt attribute block length: %lx\n"), (long) uvalue);
1916 uvalue = max_uvalue;
1919 return uvalue;
1922 static unsigned char *
1923 skip_attr_bytes (unsigned long form,
1924 unsigned char *data,
1925 unsigned char *end,
1926 dwarf_vma pointer_size,
1927 dwarf_vma offset_size,
1928 int dwarf_version,
1929 dwarf_vma *value_return)
1931 dwarf_signed_vma svalue;
1932 dwarf_vma uvalue = 0;
1933 dwarf_vma inc = 0;
1935 * value_return = 0;
1937 switch (form)
1939 case DW_FORM_ref_addr:
1940 if (dwarf_version == 2)
1941 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1942 else if (dwarf_version > 2)
1943 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1944 else
1945 return NULL;
1946 break;
1948 case DW_FORM_addr:
1949 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
1950 break;
1952 case DW_FORM_strp:
1953 case DW_FORM_line_strp:
1954 case DW_FORM_sec_offset:
1955 case DW_FORM_GNU_ref_alt:
1956 case DW_FORM_GNU_strp_alt:
1957 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
1958 break;
1960 case DW_FORM_flag_present:
1961 uvalue = 1;
1962 break;
1964 case DW_FORM_ref1:
1965 case DW_FORM_flag:
1966 case DW_FORM_data1:
1967 case DW_FORM_strx1:
1968 case DW_FORM_addrx1:
1969 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
1970 break;
1972 case DW_FORM_strx3:
1973 case DW_FORM_addrx3:
1974 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
1975 break;
1977 case DW_FORM_ref2:
1978 case DW_FORM_data2:
1979 case DW_FORM_strx2:
1980 case DW_FORM_addrx2:
1981 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
1982 break;
1984 case DW_FORM_ref4:
1985 case DW_FORM_data4:
1986 case DW_FORM_strx4:
1987 case DW_FORM_addrx4:
1988 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
1989 break;
1991 case DW_FORM_sdata:
1992 READ_SLEB (svalue, data, end);
1993 uvalue = svalue;
1994 break;
1996 case DW_FORM_ref_udata:
1997 case DW_FORM_udata:
1998 case DW_FORM_GNU_str_index:
1999 case DW_FORM_strx:
2000 case DW_FORM_GNU_addr_index:
2001 case DW_FORM_addrx:
2002 READ_ULEB (uvalue, data, end);
2003 break;
2005 case DW_FORM_ref8:
2006 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2007 break;
2009 case DW_FORM_data8:
2010 case DW_FORM_ref_sig8:
2011 inc = 8;
2012 break;
2014 case DW_FORM_data16:
2015 inc = 16;
2016 break;
2018 case DW_FORM_string:
2019 inc = strnlen ((char *) data, end - data) + 1;
2020 break;
2022 case DW_FORM_block:
2023 case DW_FORM_exprloc:
2024 READ_ULEB (uvalue, data, end);
2025 inc = uvalue;
2026 break;
2028 case DW_FORM_block1:
2029 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2030 inc = uvalue;
2031 break;
2033 case DW_FORM_block2:
2034 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2035 inc = uvalue;
2036 break;
2038 case DW_FORM_block4:
2039 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2040 inc = uvalue;
2041 break;
2043 case DW_FORM_indirect:
2044 READ_ULEB (form, data, end);
2045 if (form == DW_FORM_implicit_const)
2046 SKIP_ULEB (data, end);
2047 return skip_attr_bytes (form, data, end, pointer_size, offset_size,
2048 dwarf_version, value_return);
2050 default:
2051 return NULL;
2054 * value_return = uvalue;
2055 if (inc <= (dwarf_vma) (end - data))
2056 data += inc;
2057 else
2058 data = end;
2059 return data;
2062 /* Given form FORM with value UVALUE, locate and return the abbreviation
2063 associated with it. */
2065 static abbrev_entry *
2066 get_type_abbrev_from_form (unsigned long form,
2067 unsigned long uvalue,
2068 dwarf_vma cu_offset,
2069 unsigned char *cu_end,
2070 const struct dwarf_section *section,
2071 unsigned long *abbrev_num_return,
2072 unsigned char **data_return,
2073 abbrev_map **map_return)
2075 unsigned long abbrev_number;
2076 abbrev_map * map;
2077 abbrev_entry * entry;
2078 unsigned char * data;
2080 if (abbrev_num_return != NULL)
2081 * abbrev_num_return = 0;
2082 if (data_return != NULL)
2083 * data_return = NULL;
2085 switch (form)
2087 case DW_FORM_GNU_ref_alt:
2088 case DW_FORM_ref_sig8:
2089 /* FIXME: We are unable to handle this form at the moment. */
2090 return NULL;
2092 case DW_FORM_ref_addr:
2093 if (uvalue >= section->size)
2095 warn (_("Unable to resolve ref_addr form: uvalue %lx > section size %lx (%s)\n"),
2096 uvalue, (long) section->size, section->name);
2097 return NULL;
2099 break;
2101 case DW_FORM_ref_sup4:
2102 case DW_FORM_ref_sup8:
2103 break;
2105 case DW_FORM_ref1:
2106 case DW_FORM_ref2:
2107 case DW_FORM_ref4:
2108 case DW_FORM_ref8:
2109 case DW_FORM_ref_udata:
2110 if (uvalue + cu_offset > (size_t) (cu_end - section->start))
2112 warn (_("Unable to resolve ref form: uvalue %lx + cu_offset %lx > CU size %lx\n"),
2113 uvalue, (long) cu_offset, (long) (cu_end - section->start));
2114 return NULL;
2116 uvalue += cu_offset;
2117 break;
2119 /* FIXME: Are there other DW_FORMs that can be used by types ? */
2121 default:
2122 warn (_("Unexpected form %lx encountered whilst finding abbreviation for type\n"), form);
2123 return NULL;
2126 data = (unsigned char *) section->start + uvalue;
2127 map = find_abbrev_map_by_offset (uvalue);
2129 if (map == NULL)
2131 warn (_("Unable to find abbreviations for CU offset %#lx\n"), uvalue);
2132 return NULL;
2134 if (map->list == NULL)
2136 warn (_("Empty abbreviation list encountered for CU offset %lx\n"), uvalue);
2137 return NULL;
2140 if (map_return != NULL)
2142 if (form == DW_FORM_ref_addr)
2143 *map_return = map;
2144 else
2145 *map_return = NULL;
2148 READ_ULEB (abbrev_number, data, section->start + section->size);
2150 for (entry = map->list->first_abbrev; entry != NULL; entry = entry->next)
2151 if (entry->number == abbrev_number)
2152 break;
2154 if (abbrev_num_return != NULL)
2155 * abbrev_num_return = abbrev_number;
2157 if (data_return != NULL)
2158 * data_return = data;
2160 if (entry == NULL)
2161 warn (_("Unable to find entry for abbreviation %lu\n"), abbrev_number);
2163 return entry;
2166 /* Return IS_SIGNED set to TRUE if the type using abbreviation ENTRY
2167 can be determined to be a signed type. The data for ENTRY can be
2168 found starting at DATA. */
2170 static void
2171 get_type_signedness (abbrev_entry *entry,
2172 const struct dwarf_section *section,
2173 unsigned char *data,
2174 unsigned char *end,
2175 dwarf_vma cu_offset,
2176 dwarf_vma pointer_size,
2177 dwarf_vma offset_size,
2178 int dwarf_version,
2179 bool *is_signed,
2180 unsigned int nesting)
2182 abbrev_attr * attr;
2184 * is_signed = false;
2186 #define MAX_NESTING 20
2187 if (nesting > MAX_NESTING)
2189 /* FIXME: Warn - or is this expected ?
2190 NB/ We need to avoid infinite recursion. */
2191 return;
2194 for (attr = entry->first_attr;
2195 attr != NULL && attr->attribute;
2196 attr = attr->next)
2198 unsigned char * orig_data = data;
2199 dwarf_vma uvalue = 0;
2201 data = skip_attr_bytes (attr->form, data, end, pointer_size,
2202 offset_size, dwarf_version, & uvalue);
2203 if (data == NULL)
2204 return;
2206 switch (attr->attribute)
2208 case DW_AT_linkage_name:
2209 case DW_AT_name:
2210 if (do_wide)
2212 if (attr->form == DW_FORM_strp)
2213 printf (", %s", fetch_indirect_string (uvalue));
2214 else if (attr->form == DW_FORM_string)
2215 printf (", %.*s", (int) (end - orig_data), orig_data);
2217 break;
2219 case DW_AT_type:
2220 /* Recurse. */
2222 abbrev_entry *type_abbrev;
2223 unsigned char *type_data;
2224 abbrev_map *map;
2226 type_abbrev = get_type_abbrev_from_form (attr->form,
2227 uvalue,
2228 cu_offset,
2229 end,
2230 section,
2231 NULL /* abbrev num return */,
2232 &type_data,
2233 &map);
2234 if (type_abbrev == NULL)
2235 break;
2237 get_type_signedness (type_abbrev, section, type_data,
2238 map ? section->start + map->end : end,
2239 map ? map->start : cu_offset,
2240 pointer_size, offset_size, dwarf_version,
2241 is_signed, nesting + 1);
2243 break;
2245 case DW_AT_encoding:
2246 /* Determine signness. */
2247 switch (uvalue)
2249 case DW_ATE_address:
2250 /* FIXME - some architectures have signed addresses. */
2251 case DW_ATE_boolean:
2252 case DW_ATE_unsigned:
2253 case DW_ATE_unsigned_char:
2254 case DW_ATE_unsigned_fixed:
2255 * is_signed = false;
2256 break;
2258 default:
2259 case DW_ATE_complex_float:
2260 case DW_ATE_float:
2261 case DW_ATE_signed:
2262 case DW_ATE_signed_char:
2263 case DW_ATE_imaginary_float:
2264 case DW_ATE_decimal_float:
2265 case DW_ATE_signed_fixed:
2266 * is_signed = true;
2267 break;
2269 break;
2274 static void
2275 read_and_print_leb128 (unsigned char *data,
2276 unsigned int *bytes_read,
2277 unsigned const char *end,
2278 bool is_signed)
2280 int status;
2281 dwarf_vma val = read_leb128 (data, end, is_signed, bytes_read, &status);
2282 if (status != 0)
2283 report_leb_status (status);
2284 else
2285 printf ("%s", dwarf_vmatoa (is_signed ? "d" : "u", val));
2288 static void
2289 display_discr_list (unsigned long form,
2290 dwarf_vma uvalue,
2291 unsigned char * data,
2292 int level)
2294 unsigned char *end = data;
2296 if (uvalue == 0)
2298 printf ("[default]");
2299 return;
2302 switch (form)
2304 case DW_FORM_block:
2305 case DW_FORM_block1:
2306 case DW_FORM_block2:
2307 case DW_FORM_block4:
2308 /* Move data pointer back to the start of the byte array. */
2309 data -= uvalue;
2310 break;
2311 default:
2312 printf ("<corrupt>\n");
2313 warn (_("corrupt discr_list - not using a block form\n"));
2314 return;
2317 if (uvalue < 2)
2319 printf ("<corrupt>\n");
2320 warn (_("corrupt discr_list - block not long enough\n"));
2321 return;
2324 bool is_signed = (level > 0 && level <= MAX_CU_NESTING
2325 ? level_type_signed [level - 1] : false);
2327 printf ("(");
2328 while (data < end)
2330 unsigned char discriminant;
2331 unsigned int bytes_read;
2333 SAFE_BYTE_GET_AND_INC (discriminant, data, 1, end);
2335 switch (discriminant)
2337 case DW_DSC_label:
2338 printf ("label ");
2339 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2340 data += bytes_read;
2341 break;
2343 case DW_DSC_range:
2344 printf ("range ");
2345 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2346 data += bytes_read;
2348 printf ("..");
2349 read_and_print_leb128 (data, & bytes_read, end, is_signed);
2350 data += bytes_read;
2351 break;
2353 default:
2354 printf ("<corrupt>\n");
2355 warn (_("corrupt discr_list - unrecognized discriminant byte %#x\n"),
2356 discriminant);
2357 return;
2360 if (data < end)
2361 printf (", ");
2364 if (is_signed)
2365 printf (")(signed)");
2366 else
2367 printf (")(unsigned)");
2370 static unsigned char *
2371 read_and_display_attr_value (unsigned long attribute,
2372 unsigned long form,
2373 dwarf_signed_vma implicit_const,
2374 unsigned char * start,
2375 unsigned char * data,
2376 unsigned char * end,
2377 dwarf_vma cu_offset,
2378 dwarf_vma pointer_size,
2379 dwarf_vma offset_size,
2380 int dwarf_version,
2381 debug_info * debug_info_p,
2382 int do_loc,
2383 struct dwarf_section * section,
2384 struct cu_tu_set * this_set,
2385 char delimiter,
2386 int level)
2388 dwarf_signed_vma svalue;
2389 dwarf_vma uvalue = 0;
2390 dwarf_vma uvalue_hi = 0;
2391 unsigned char *block_start = NULL;
2392 unsigned char *orig_data = data;
2394 if (data > end || (data == end && form != DW_FORM_flag_present))
2396 warn (_("Corrupt attribute\n"));
2397 return data;
2400 if (do_wide && ! do_loc)
2402 /* PR 26847: Display the name of the form. */
2403 const char * name = get_FORM_name (form);
2405 /* For convenience we skip the DW_FORM_ prefix to the name. */
2406 if (name[0] == 'D')
2407 name += 8; /* strlen ("DW_FORM_") */
2408 printf ("%c(%s)", delimiter, name);
2411 switch (form)
2413 default:
2414 break;
2416 case DW_FORM_ref_addr:
2417 if (dwarf_version == 2)
2418 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2419 else if (dwarf_version > 2)
2420 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2421 else
2422 error (_("Internal error: DW_FORM_ref_addr is not supported in DWARF version 1.\n"));
2423 break;
2425 case DW_FORM_addr:
2426 SAFE_BYTE_GET_AND_INC (uvalue, data, pointer_size, end);
2427 break;
2429 case DW_FORM_strp_sup:
2430 case DW_FORM_strp:
2431 case DW_FORM_line_strp:
2432 case DW_FORM_sec_offset:
2433 case DW_FORM_GNU_ref_alt:
2434 case DW_FORM_GNU_strp_alt:
2435 SAFE_BYTE_GET_AND_INC (uvalue, data, offset_size, end);
2436 break;
2438 case DW_FORM_flag_present:
2439 uvalue = 1;
2440 break;
2442 case DW_FORM_ref1:
2443 case DW_FORM_flag:
2444 case DW_FORM_data1:
2445 case DW_FORM_strx1:
2446 case DW_FORM_addrx1:
2447 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2448 break;
2450 case DW_FORM_ref2:
2451 case DW_FORM_data2:
2452 case DW_FORM_strx2:
2453 case DW_FORM_addrx2:
2454 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2455 break;
2457 case DW_FORM_strx3:
2458 case DW_FORM_addrx3:
2459 SAFE_BYTE_GET_AND_INC (uvalue, data, 3, end);
2460 break;
2462 case DW_FORM_ref_sup4:
2463 case DW_FORM_ref4:
2464 case DW_FORM_data4:
2465 case DW_FORM_strx4:
2466 case DW_FORM_addrx4:
2467 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2468 break;
2470 case DW_FORM_ref_sup8:
2471 case DW_FORM_ref8:
2472 case DW_FORM_data8:
2473 case DW_FORM_ref_sig8:
2474 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2475 break;
2477 case DW_FORM_data16:
2478 SAFE_BYTE_GET_AND_INC (uvalue, data, 8, end);
2479 SAFE_BYTE_GET_AND_INC (uvalue_hi, data, 8, end);
2480 if (byte_get != byte_get_little_endian)
2482 dwarf_vma utmp = uvalue;
2483 uvalue = uvalue_hi;
2484 uvalue_hi = utmp;
2486 break;
2488 case DW_FORM_sdata:
2489 READ_SLEB (svalue, data, end);
2490 uvalue = svalue;
2491 break;
2493 case DW_FORM_GNU_str_index:
2494 case DW_FORM_strx:
2495 case DW_FORM_ref_udata:
2496 case DW_FORM_udata:
2497 case DW_FORM_GNU_addr_index:
2498 case DW_FORM_addrx:
2499 READ_ULEB (uvalue, data, end);
2500 break;
2502 case DW_FORM_indirect:
2503 READ_ULEB (form, data, end);
2504 if (!do_loc)
2505 printf ("%c%s", delimiter, get_FORM_name (form));
2506 if (form == DW_FORM_implicit_const)
2507 READ_SLEB (implicit_const, data, end);
2508 return read_and_display_attr_value (attribute, form, implicit_const,
2509 start, data, end,
2510 cu_offset, pointer_size,
2511 offset_size, dwarf_version,
2512 debug_info_p, do_loc,
2513 section, this_set, delimiter, level);
2515 case DW_FORM_implicit_const:
2516 uvalue = implicit_const;
2517 break;
2520 switch (form)
2522 case DW_FORM_ref_addr:
2523 if (!do_loc)
2524 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
2525 break;
2527 case DW_FORM_GNU_ref_alt:
2528 if (!do_loc)
2530 if (do_wide)
2531 /* We have already printed the form name. */
2532 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
2533 else
2534 printf ("%c<alt 0x%s>", delimiter, dwarf_vmatoa ("x", uvalue));
2536 /* FIXME: Follow the reference... */
2537 break;
2539 case DW_FORM_ref1:
2540 case DW_FORM_ref2:
2541 case DW_FORM_ref4:
2542 case DW_FORM_ref_sup4:
2543 case DW_FORM_ref_udata:
2544 if (!do_loc)
2545 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue + cu_offset));
2546 break;
2548 case DW_FORM_data4:
2549 case DW_FORM_addr:
2550 case DW_FORM_sec_offset:
2551 if (!do_loc)
2552 printf ("%c0x%s", delimiter, dwarf_vmatoa ("x", uvalue));
2553 break;
2555 case DW_FORM_flag_present:
2556 case DW_FORM_flag:
2557 case DW_FORM_data1:
2558 case DW_FORM_data2:
2559 case DW_FORM_sdata:
2560 case DW_FORM_udata:
2561 if (!do_loc)
2562 printf ("%c%s", delimiter, dwarf_vmatoa ("d", uvalue));
2563 break;
2565 case DW_FORM_implicit_const:
2566 if (!do_loc)
2567 printf ("%c%s", delimiter, dwarf_vmatoa ("d", implicit_const));
2568 break;
2570 case DW_FORM_ref_sup8:
2571 case DW_FORM_ref8:
2572 case DW_FORM_data8:
2573 if (!do_loc)
2575 dwarf_vma utmp = uvalue;
2576 if (form == DW_FORM_ref8)
2577 utmp += cu_offset;
2578 printf ("%c0x%s", delimiter, dwarf_vmatoa ("x", utmp));
2580 break;
2582 case DW_FORM_data16:
2583 if (!do_loc)
2584 printf (" 0x%s%s",
2585 uvalue_hi == 0 ? "" : dwarf_vmatoa ("x", uvalue_hi),
2586 dwarf_vmatoa_1 ("x", uvalue, uvalue_hi == 0 ? 0 : 8));
2587 break;
2589 case DW_FORM_string:
2590 if (!do_loc)
2591 printf ("%c%.*s", delimiter, (int) (end - data), data);
2592 data += strnlen ((char *) data, end - data);
2593 if (data < end)
2594 data++;
2595 break;
2597 case DW_FORM_block:
2598 case DW_FORM_exprloc:
2599 READ_ULEB (uvalue, data, end);
2600 do_block:
2601 block_start = data;
2602 if (block_start >= end)
2604 warn (_("Block ends prematurely\n"));
2605 uvalue = 0;
2606 block_start = end;
2609 uvalue = check_uvalue (block_start, uvalue, end);
2611 if (do_loc)
2612 data = block_start + uvalue;
2613 else
2614 data = display_block (block_start, uvalue, end, delimiter);
2615 break;
2617 case DW_FORM_block1:
2618 SAFE_BYTE_GET_AND_INC (uvalue, data, 1, end);
2619 goto do_block;
2621 case DW_FORM_block2:
2622 SAFE_BYTE_GET_AND_INC (uvalue, data, 2, end);
2623 goto do_block;
2625 case DW_FORM_block4:
2626 SAFE_BYTE_GET_AND_INC (uvalue, data, 4, end);
2627 goto do_block;
2629 case DW_FORM_strp:
2630 if (!do_loc)
2632 if (do_wide)
2633 /* We have already displayed the form name. */
2634 printf (_("%c(offset: 0x%s): %s"), delimiter,
2635 dwarf_vmatoa ("x", uvalue),
2636 fetch_indirect_string (uvalue));
2637 else
2638 printf (_("%c(indirect string, offset: 0x%s): %s"), delimiter,
2639 dwarf_vmatoa ("x", uvalue),
2640 fetch_indirect_string (uvalue));
2642 break;
2644 case DW_FORM_line_strp:
2645 if (!do_loc)
2647 if (do_wide)
2648 /* We have already displayed the form name. */
2649 printf (_("%c(offset: 0x%s): %s"), delimiter,
2650 dwarf_vmatoa ("x", uvalue),
2651 fetch_indirect_line_string (uvalue));
2652 else
2653 printf (_("%c(indirect line string, offset: 0x%s): %s"), delimiter,
2654 dwarf_vmatoa ("x", uvalue),
2655 fetch_indirect_line_string (uvalue));
2657 break;
2659 case DW_FORM_GNU_str_index:
2660 case DW_FORM_strx:
2661 case DW_FORM_strx1:
2662 case DW_FORM_strx2:
2663 case DW_FORM_strx3:
2664 case DW_FORM_strx4:
2665 if (!do_loc)
2667 const char *suffix = strrchr (section->name, '.');
2668 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
2670 if (do_wide)
2671 /* We have already displayed the form name. */
2672 printf (_("%c(offset: 0x%s): %s"), delimiter,
2673 dwarf_vmatoa ("x", uvalue),
2674 fetch_indexed_string (uvalue, this_set, offset_size, dwo));
2675 else
2676 printf (_("%c(indexed string: 0x%s): %s"), delimiter,
2677 dwarf_vmatoa ("x", uvalue),
2678 fetch_indexed_string (uvalue, this_set, offset_size, dwo));
2680 break;
2682 case DW_FORM_GNU_strp_alt:
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_alt_indirect_string (uvalue));
2690 else
2691 printf (_("%c(alt indirect string, offset: 0x%s) %s"), delimiter,
2692 dwarf_vmatoa ("x", uvalue),
2693 fetch_alt_indirect_string (uvalue));
2695 break;
2697 case DW_FORM_indirect:
2698 /* Handled above. */
2699 break;
2701 case DW_FORM_ref_sig8:
2702 if (!do_loc)
2703 printf ("%c%s: 0x%s", delimiter, do_wide ? "" : "signature",
2704 dwarf_vmatoa ("x", uvalue));
2705 break;
2707 case DW_FORM_GNU_addr_index:
2708 case DW_FORM_addrx:
2709 case DW_FORM_addrx1:
2710 case DW_FORM_addrx2:
2711 case DW_FORM_addrx3:
2712 case DW_FORM_addrx4:
2713 if (!do_loc)
2715 dwarf_vma base;
2716 dwarf_vma offset;
2718 if (debug_info_p == NULL)
2719 base = 0;
2720 else if (debug_info_p->addr_base == DEBUG_INFO_UNAVAILABLE)
2721 base = 0;
2722 else
2723 base = debug_info_p->addr_base;
2725 offset = base + uvalue * pointer_size;
2727 if (do_wide)
2728 /* We have already displayed the form name. */
2729 printf (_("%c(index: 0x%s): %s"), delimiter,
2730 dwarf_vmatoa ("x", uvalue),
2731 fetch_indexed_value (offset, pointer_size));
2732 else
2733 printf (_("%c(addr_index: 0x%s): %s"), delimiter,
2734 dwarf_vmatoa ("x", uvalue),
2735 fetch_indexed_value (offset, pointer_size));
2737 break;
2739 case DW_FORM_strp_sup:
2740 if (!do_loc)
2741 printf ("%c<0x%s>", delimiter, dwarf_vmatoa ("x", uvalue + cu_offset));
2742 break;
2744 default:
2745 warn (_("Unrecognized form: 0x%lx\n"), form);
2746 /* What to do? Consume a byte maybe? */
2747 ++data;
2748 break;
2751 if ((do_loc || do_debug_loc || do_debug_ranges)
2752 && num_debug_info_entries == 0
2753 && debug_info_p != NULL)
2755 switch (attribute)
2757 case DW_AT_frame_base:
2758 have_frame_base = 1;
2759 /* Fall through. */
2760 case DW_AT_location:
2761 case DW_AT_GNU_locviews:
2762 case DW_AT_string_length:
2763 case DW_AT_return_addr:
2764 case DW_AT_data_member_location:
2765 case DW_AT_vtable_elem_location:
2766 case DW_AT_segment:
2767 case DW_AT_static_link:
2768 case DW_AT_use_location:
2769 case DW_AT_call_value:
2770 case DW_AT_GNU_call_site_value:
2771 case DW_AT_call_data_value:
2772 case DW_AT_GNU_call_site_data_value:
2773 case DW_AT_call_target:
2774 case DW_AT_GNU_call_site_target:
2775 case DW_AT_call_target_clobbered:
2776 case DW_AT_GNU_call_site_target_clobbered:
2777 if ((dwarf_version < 4
2778 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2779 || form == DW_FORM_sec_offset)
2781 /* Process location list. */
2782 unsigned int lmax = debug_info_p->max_loc_offsets;
2783 unsigned int num = debug_info_p->num_loc_offsets;
2785 if (lmax == 0 || num >= lmax)
2787 lmax += 1024;
2788 debug_info_p->loc_offsets = (dwarf_vma *)
2789 xcrealloc (debug_info_p->loc_offsets,
2790 lmax, sizeof (*debug_info_p->loc_offsets));
2791 debug_info_p->loc_views = (dwarf_vma *)
2792 xcrealloc (debug_info_p->loc_views,
2793 lmax, sizeof (*debug_info_p->loc_views));
2794 debug_info_p->have_frame_base = (int *)
2795 xcrealloc (debug_info_p->have_frame_base,
2796 lmax, sizeof (*debug_info_p->have_frame_base));
2797 debug_info_p->max_loc_offsets = lmax;
2799 if (this_set != NULL)
2800 uvalue += this_set->section_offsets [DW_SECT_LOC];
2801 debug_info_p->have_frame_base [num] = have_frame_base;
2802 if (attribute != DW_AT_GNU_locviews)
2804 /* Corrupt DWARF info can produce more offsets than views.
2805 See PR 23062 for an example. */
2806 if (debug_info_p->num_loc_offsets
2807 > debug_info_p->num_loc_views)
2808 warn (_("More location offset attributes than DW_AT_GNU_locview attributes\n"));
2809 else
2811 debug_info_p->loc_offsets [num] = uvalue;
2812 debug_info_p->num_loc_offsets++;
2815 else
2817 assert (debug_info_p->num_loc_views <= num);
2818 num = debug_info_p->num_loc_views;
2819 if (num > debug_info_p->num_loc_offsets)
2820 warn (_("More DW_AT_GNU_locview attributes than location offset attributes\n"));
2821 else
2823 debug_info_p->loc_views [num] = uvalue;
2824 debug_info_p->num_loc_views++;
2828 break;
2830 case DW_AT_low_pc:
2831 if (need_base_address)
2832 debug_info_p->base_address = uvalue;
2833 break;
2835 case DW_AT_GNU_addr_base:
2836 case DW_AT_addr_base:
2837 debug_info_p->addr_base = uvalue;
2838 break;
2840 case DW_AT_GNU_ranges_base:
2841 debug_info_p->ranges_base = uvalue;
2842 break;
2844 case DW_AT_ranges:
2845 if ((dwarf_version < 4
2846 && (form == DW_FORM_data4 || form == DW_FORM_data8))
2847 || form == DW_FORM_sec_offset)
2849 /* Process range list. */
2850 unsigned int lmax = debug_info_p->max_range_lists;
2851 unsigned int num = debug_info_p->num_range_lists;
2853 if (lmax == 0 || num >= lmax)
2855 lmax += 1024;
2856 debug_info_p->range_lists = (dwarf_vma *)
2857 xcrealloc (debug_info_p->range_lists,
2858 lmax, sizeof (*debug_info_p->range_lists));
2859 debug_info_p->max_range_lists = lmax;
2861 debug_info_p->range_lists [num] = uvalue;
2862 debug_info_p->num_range_lists++;
2864 break;
2866 case DW_AT_GNU_dwo_name:
2867 case DW_AT_dwo_name:
2868 if (need_dwo_info)
2869 switch (form)
2871 case DW_FORM_strp:
2872 add_dwo_name ((const char *) fetch_indirect_string (uvalue), cu_offset);
2873 break;
2874 case DW_FORM_GNU_strp_alt:
2875 add_dwo_name ((const char *) fetch_alt_indirect_string (uvalue), cu_offset);
2876 break;
2877 case DW_FORM_GNU_str_index:
2878 case DW_FORM_strx:
2879 case DW_FORM_strx1:
2880 case DW_FORM_strx2:
2881 case DW_FORM_strx3:
2882 case DW_FORM_strx4:
2883 add_dwo_name (fetch_indexed_string (uvalue, this_set, offset_size, false), cu_offset);
2884 break;
2885 case DW_FORM_string:
2886 add_dwo_name ((const char *) orig_data, cu_offset);
2887 break;
2888 default:
2889 warn (_("Unsupported form (%s) for attribute %s\n"),
2890 get_FORM_name (form), get_AT_name (attribute));
2891 break;
2893 break;
2895 case DW_AT_comp_dir:
2896 /* FIXME: Also extract a build-id in a CU/TU. */
2897 if (need_dwo_info)
2898 switch (form)
2900 case DW_FORM_strp:
2901 add_dwo_dir ((const char *) fetch_indirect_string (uvalue), cu_offset);
2902 break;
2903 case DW_FORM_GNU_strp_alt:
2904 add_dwo_dir (fetch_alt_indirect_string (uvalue), cu_offset);
2905 break;
2906 case DW_FORM_line_strp:
2907 add_dwo_dir ((const char *) fetch_indirect_line_string (uvalue), cu_offset);
2908 break;
2909 case DW_FORM_GNU_str_index:
2910 case DW_FORM_strx:
2911 case DW_FORM_strx1:
2912 case DW_FORM_strx2:
2913 case DW_FORM_strx3:
2914 case DW_FORM_strx4:
2915 add_dwo_dir (fetch_indexed_string (uvalue, this_set, offset_size, false), cu_offset);
2916 break;
2917 case DW_FORM_string:
2918 add_dwo_dir ((const char *) orig_data, cu_offset);
2919 break;
2920 default:
2921 warn (_("Unsupported form (%s) for attribute %s\n"),
2922 get_FORM_name (form), get_AT_name (attribute));
2923 break;
2925 break;
2927 case DW_AT_GNU_dwo_id:
2928 if (need_dwo_info)
2929 switch (form)
2931 case DW_FORM_data8:
2932 /* FIXME: Record the length of the ID as well ? */
2933 add_dwo_id ((const char *) (data - 8), cu_offset);
2934 break;
2935 default:
2936 warn (_("Unsupported form (%s) for attribute %s\n"),
2937 get_FORM_name (form), get_AT_name (attribute));
2938 break;
2940 break;
2942 default:
2943 break;
2947 if (do_loc || attribute == 0)
2948 return data;
2950 /* For some attributes we can display further information. */
2951 switch (attribute)
2953 case DW_AT_type:
2954 if (level >= 0 && level < MAX_CU_NESTING
2955 && uvalue < (size_t) (end - start))
2957 bool is_signed = false;
2958 abbrev_entry *type_abbrev;
2959 unsigned char *type_data;
2960 abbrev_map *map;
2962 type_abbrev = get_type_abbrev_from_form (form, uvalue,
2963 cu_offset, end,
2964 section, NULL,
2965 &type_data, &map);
2966 if (type_abbrev != NULL)
2968 get_type_signedness (type_abbrev, section, type_data,
2969 map ? section->start + map->end : end,
2970 map ? map->start : cu_offset,
2971 pointer_size, offset_size, dwarf_version,
2972 & is_signed, 0);
2974 level_type_signed[level] = is_signed;
2976 break;
2978 case DW_AT_inline:
2979 printf ("\t");
2980 switch (uvalue)
2982 case DW_INL_not_inlined:
2983 printf (_("(not inlined)"));
2984 break;
2985 case DW_INL_inlined:
2986 printf (_("(inlined)"));
2987 break;
2988 case DW_INL_declared_not_inlined:
2989 printf (_("(declared as inline but ignored)"));
2990 break;
2991 case DW_INL_declared_inlined:
2992 printf (_("(declared as inline and inlined)"));
2993 break;
2994 default:
2995 printf (_(" (Unknown inline attribute value: %s)"),
2996 dwarf_vmatoa ("x", uvalue));
2997 break;
2999 break;
3001 case DW_AT_language:
3002 printf ("\t");
3003 switch (uvalue)
3005 /* Ordered by the numeric value of these constants. */
3006 case DW_LANG_C89: printf ("(ANSI C)"); break;
3007 case DW_LANG_C: printf ("(non-ANSI C)"); break;
3008 case DW_LANG_Ada83: printf ("(Ada)"); break;
3009 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
3010 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
3011 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
3012 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
3013 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
3014 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
3015 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
3016 /* DWARF 2.1 values. */
3017 case DW_LANG_Java: printf ("(Java)"); break;
3018 case DW_LANG_C99: printf ("(ANSI C99)"); break;
3019 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
3020 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
3021 /* DWARF 3 values. */
3022 case DW_LANG_PLI: printf ("(PLI)"); break;
3023 case DW_LANG_ObjC: printf ("(Objective C)"); break;
3024 case DW_LANG_ObjC_plus_plus: printf ("(Objective C++)"); break;
3025 case DW_LANG_UPC: printf ("(Unified Parallel C)"); break;
3026 case DW_LANG_D: printf ("(D)"); break;
3027 /* DWARF 4 values. */
3028 case DW_LANG_Python: printf ("(Python)"); break;
3029 /* DWARF 5 values. */
3030 case DW_LANG_OpenCL: printf ("(OpenCL)"); break;
3031 case DW_LANG_Go: printf ("(Go)"); break;
3032 case DW_LANG_Modula3: printf ("(Modula 3)"); break;
3033 case DW_LANG_Haskell: printf ("(Haskell)"); break;
3034 case DW_LANG_C_plus_plus_03: printf ("(C++03)"); break;
3035 case DW_LANG_C_plus_plus_11: printf ("(C++11)"); break;
3036 case DW_LANG_OCaml: printf ("(OCaml)"); break;
3037 case DW_LANG_Rust: printf ("(Rust)"); break;
3038 case DW_LANG_C11: printf ("(C11)"); break;
3039 case DW_LANG_Swift: printf ("(Swift)"); break;
3040 case DW_LANG_Julia: printf ("(Julia)"); break;
3041 case DW_LANG_Dylan: printf ("(Dylan)"); break;
3042 case DW_LANG_C_plus_plus_14: printf ("(C++14)"); break;
3043 case DW_LANG_Fortran03: printf ("(Fortran 03)"); break;
3044 case DW_LANG_Fortran08: printf ("(Fortran 08)"); break;
3045 case DW_LANG_RenderScript: printf ("(RenderScript)"); break;
3046 /* MIPS extension. */
3047 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
3048 /* UPC extension. */
3049 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
3050 default:
3051 if (uvalue >= DW_LANG_lo_user && uvalue <= DW_LANG_hi_user)
3052 printf (_("(implementation defined: %s)"),
3053 dwarf_vmatoa ("x", uvalue));
3054 else
3055 printf (_("(Unknown: %s)"), dwarf_vmatoa ("x", uvalue));
3056 break;
3058 break;
3060 case DW_AT_encoding:
3061 printf ("\t");
3062 switch (uvalue)
3064 case DW_ATE_void: printf ("(void)"); break;
3065 case DW_ATE_address: printf ("(machine address)"); break;
3066 case DW_ATE_boolean: printf ("(boolean)"); break;
3067 case DW_ATE_complex_float: printf ("(complex float)"); break;
3068 case DW_ATE_float: printf ("(float)"); break;
3069 case DW_ATE_signed: printf ("(signed)"); break;
3070 case DW_ATE_signed_char: printf ("(signed char)"); break;
3071 case DW_ATE_unsigned: printf ("(unsigned)"); break;
3072 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
3073 /* DWARF 2.1 values: */
3074 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
3075 case DW_ATE_decimal_float: printf ("(decimal float)"); break;
3076 /* DWARF 3 values: */
3077 case DW_ATE_packed_decimal: printf ("(packed_decimal)"); break;
3078 case DW_ATE_numeric_string: printf ("(numeric_string)"); break;
3079 case DW_ATE_edited: printf ("(edited)"); break;
3080 case DW_ATE_signed_fixed: printf ("(signed_fixed)"); break;
3081 case DW_ATE_unsigned_fixed: printf ("(unsigned_fixed)"); break;
3082 /* DWARF 4 values: */
3083 case DW_ATE_UTF: printf ("(unicode string)"); break;
3084 /* DWARF 5 values: */
3085 case DW_ATE_UCS: printf ("(UCS)"); break;
3086 case DW_ATE_ASCII: printf ("(ASCII)"); break;
3088 /* HP extensions: */
3089 case DW_ATE_HP_float80: printf ("(HP_float80)"); break;
3090 case DW_ATE_HP_complex_float80: printf ("(HP_complex_float80)"); break;
3091 case DW_ATE_HP_float128: printf ("(HP_float128)"); break;
3092 case DW_ATE_HP_complex_float128:printf ("(HP_complex_float128)"); break;
3093 case DW_ATE_HP_floathpintel: printf ("(HP_floathpintel)"); break;
3094 case DW_ATE_HP_imaginary_float80: printf ("(HP_imaginary_float80)"); break;
3095 case DW_ATE_HP_imaginary_float128: printf ("(HP_imaginary_float128)"); break;
3097 default:
3098 if (uvalue >= DW_ATE_lo_user
3099 && uvalue <= DW_ATE_hi_user)
3100 printf (_("(user defined type)"));
3101 else
3102 printf (_("(unknown type)"));
3103 break;
3105 break;
3107 case DW_AT_accessibility:
3108 printf ("\t");
3109 switch (uvalue)
3111 case DW_ACCESS_public: printf ("(public)"); break;
3112 case DW_ACCESS_protected: printf ("(protected)"); break;
3113 case DW_ACCESS_private: printf ("(private)"); break;
3114 default:
3115 printf (_("(unknown accessibility)"));
3116 break;
3118 break;
3120 case DW_AT_visibility:
3121 printf ("\t");
3122 switch (uvalue)
3124 case DW_VIS_local: printf ("(local)"); break;
3125 case DW_VIS_exported: printf ("(exported)"); break;
3126 case DW_VIS_qualified: printf ("(qualified)"); break;
3127 default: printf (_("(unknown visibility)")); break;
3129 break;
3131 case DW_AT_endianity:
3132 printf ("\t");
3133 switch (uvalue)
3135 case DW_END_default: printf ("(default)"); break;
3136 case DW_END_big: printf ("(big)"); break;
3137 case DW_END_little: printf ("(little)"); break;
3138 default:
3139 if (uvalue >= DW_END_lo_user && uvalue <= DW_END_hi_user)
3140 printf (_("(user specified)"));
3141 else
3142 printf (_("(unknown endianity)"));
3143 break;
3145 break;
3147 case DW_AT_virtuality:
3148 printf ("\t");
3149 switch (uvalue)
3151 case DW_VIRTUALITY_none: printf ("(none)"); break;
3152 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
3153 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
3154 default: printf (_("(unknown virtuality)")); break;
3156 break;
3158 case DW_AT_identifier_case:
3159 printf ("\t");
3160 switch (uvalue)
3162 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
3163 case DW_ID_up_case: printf ("(up_case)"); break;
3164 case DW_ID_down_case: printf ("(down_case)"); break;
3165 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
3166 default: printf (_("(unknown case)")); break;
3168 break;
3170 case DW_AT_calling_convention:
3171 printf ("\t");
3172 switch (uvalue)
3174 case DW_CC_normal: printf ("(normal)"); break;
3175 case DW_CC_program: printf ("(program)"); break;
3176 case DW_CC_nocall: printf ("(nocall)"); break;
3177 case DW_CC_pass_by_reference: printf ("(pass by ref)"); break;
3178 case DW_CC_pass_by_value: printf ("(pass by value)"); break;
3179 case DW_CC_GNU_renesas_sh: printf ("(Rensas SH)"); break;
3180 case DW_CC_GNU_borland_fastcall_i386: printf ("(Borland fastcall i386)"); break;
3181 default:
3182 if (uvalue >= DW_CC_lo_user
3183 && uvalue <= DW_CC_hi_user)
3184 printf (_("(user defined)"));
3185 else
3186 printf (_("(unknown convention)"));
3188 break;
3190 case DW_AT_ordering:
3191 printf ("\t");
3192 switch (uvalue)
3194 case 255:
3195 case -1: printf (_("(undefined)")); break;
3196 case 0: printf ("(row major)"); break;
3197 case 1: printf ("(column major)"); break;
3199 break;
3201 case DW_AT_decimal_sign:
3202 printf ("\t");
3203 switch (uvalue)
3205 case DW_DS_unsigned: printf (_("(unsigned)")); break;
3206 case DW_DS_leading_overpunch: printf (_("(leading overpunch)")); break;
3207 case DW_DS_trailing_overpunch: printf (_("(trailing overpunch)")); break;
3208 case DW_DS_leading_separate: printf (_("(leading separate)")); break;
3209 case DW_DS_trailing_separate: printf (_("(trailing separate)")); break;
3210 default: printf (_("(unrecognised)")); break;
3212 break;
3214 case DW_AT_defaulted:
3215 printf ("\t");
3216 switch (uvalue)
3218 case DW_DEFAULTED_no: printf (_("(no)")); break;
3219 case DW_DEFAULTED_in_class: printf (_("(in class)")); break;
3220 case DW_DEFAULTED_out_of_class: printf (_("(out of class)")); break;
3221 default: printf (_("(unrecognised)")); break;
3223 break;
3225 case DW_AT_discr_list:
3226 printf ("\t");
3227 display_discr_list (form, uvalue, data, level);
3228 break;
3230 case DW_AT_frame_base:
3231 have_frame_base = 1;
3232 /* Fall through. */
3233 case DW_AT_location:
3234 case DW_AT_string_length:
3235 case DW_AT_return_addr:
3236 case DW_AT_data_member_location:
3237 case DW_AT_vtable_elem_location:
3238 case DW_AT_segment:
3239 case DW_AT_static_link:
3240 case DW_AT_use_location:
3241 case DW_AT_call_value:
3242 case DW_AT_GNU_call_site_value:
3243 case DW_AT_call_data_value:
3244 case DW_AT_GNU_call_site_data_value:
3245 case DW_AT_call_target:
3246 case DW_AT_GNU_call_site_target:
3247 case DW_AT_call_target_clobbered:
3248 case DW_AT_GNU_call_site_target_clobbered:
3249 if ((dwarf_version < 4
3250 && (form == DW_FORM_data4 || form == DW_FORM_data8))
3251 || form == DW_FORM_sec_offset)
3252 printf (_(" (location list)"));
3253 /* Fall through. */
3254 case DW_AT_allocated:
3255 case DW_AT_associated:
3256 case DW_AT_data_location:
3257 case DW_AT_stride:
3258 case DW_AT_upper_bound:
3259 case DW_AT_lower_bound:
3260 if (block_start)
3262 int need_frame_base;
3264 printf ("\t(");
3265 need_frame_base = decode_location_expression (block_start,
3266 pointer_size,
3267 offset_size,
3268 dwarf_version,
3269 uvalue,
3270 cu_offset, section);
3271 printf (")");
3272 if (need_frame_base && !have_frame_base)
3273 printf (_(" [without DW_AT_frame_base]"));
3275 break;
3277 case DW_AT_data_bit_offset:
3278 case DW_AT_byte_size:
3279 case DW_AT_bit_size:
3280 case DW_AT_string_length_byte_size:
3281 case DW_AT_string_length_bit_size:
3282 case DW_AT_bit_stride:
3283 if (form == DW_FORM_exprloc)
3285 printf ("\t(");
3286 (void) decode_location_expression (block_start, pointer_size,
3287 offset_size, dwarf_version,
3288 uvalue, cu_offset, section);
3289 printf (")");
3291 break;
3293 case DW_AT_import:
3295 unsigned long abbrev_number;
3296 abbrev_entry *entry;
3298 entry = get_type_abbrev_from_form (form, uvalue, cu_offset, end,
3299 section, & abbrev_number, NULL, NULL);
3300 if (entry == NULL)
3302 if (form != DW_FORM_GNU_ref_alt)
3303 warn (_("Offset %s used as value for DW_AT_import attribute of DIE at offset 0x%lx is too big.\n"),
3304 dwarf_vmatoa ("x", uvalue),
3305 (unsigned long) (orig_data - section->start));
3307 else
3309 printf (_("\t[Abbrev Number: %ld"), abbrev_number);
3310 printf (" (%s)", get_TAG_name (entry->tag));
3311 printf ("]");
3314 break;
3316 default:
3317 break;
3320 return data;
3323 static unsigned char *
3324 read_and_display_attr (unsigned long attribute,
3325 unsigned long form,
3326 dwarf_signed_vma implicit_const,
3327 unsigned char * start,
3328 unsigned char * data,
3329 unsigned char * end,
3330 dwarf_vma cu_offset,
3331 dwarf_vma pointer_size,
3332 dwarf_vma offset_size,
3333 int dwarf_version,
3334 debug_info * debug_info_p,
3335 int do_loc,
3336 struct dwarf_section * section,
3337 struct cu_tu_set * this_set,
3338 int level)
3340 if (!do_loc)
3341 printf (" %-18s:", get_AT_name (attribute));
3342 data = read_and_display_attr_value (attribute, form, implicit_const,
3343 start, data, end,
3344 cu_offset, pointer_size, offset_size,
3345 dwarf_version, debug_info_p,
3346 do_loc, section, this_set, ' ', level);
3347 if (!do_loc)
3348 printf ("\n");
3349 return data;
3352 /* Like load_debug_section, but if the ordinary call fails, and we are
3353 following debug links, then attempt to load the requested section
3354 from one of the separate debug info files. */
3356 static bool
3357 load_debug_section_with_follow (enum dwarf_section_display_enum sec_enum,
3358 void * handle)
3360 if (load_debug_section (sec_enum, handle))
3362 if (debug_displays[sec_enum].section.filename == NULL)
3364 /* See if we can associate a filename with this section. */
3365 separate_info * i;
3367 for (i = first_separate_info; i != NULL; i = i->next)
3368 if (i->handle == handle)
3370 debug_displays[sec_enum].section.filename = i->filename;
3371 break;
3375 return true;
3378 if (do_follow_links)
3380 separate_info * i;
3382 for (i = first_separate_info; i != NULL; i = i->next)
3384 if (load_debug_section (sec_enum, i->handle))
3386 debug_displays[sec_enum].section.filename = i->filename;
3388 /* FIXME: We should check to see if any of the remaining debug info
3389 files also contain this section, and, umm, do something about it. */
3390 return true;
3395 return false;
3398 static void
3399 introduce (struct dwarf_section * section, bool raw)
3401 if (raw)
3403 if (do_follow_links && section->filename)
3404 printf (_("Raw dump of debug contents of section %s (loaded from %s):\n\n"),
3405 section->name, section->filename);
3406 else
3407 printf (_("Raw dump of debug contents of section %s:\n\n"), section->name);
3409 else
3411 if (do_follow_links && section->filename)
3412 printf (_("Contents of the %s section (loaded from %s):\n\n"),
3413 section->name, section->filename);
3414 else
3415 printf (_("Contents of the %s section:\n\n"), section->name);
3419 /* Process the contents of a .debug_info section.
3420 If do_loc is TRUE then we are scanning for location lists and dwo tags
3421 and we do not want to display anything to the user.
3422 If do_types is TRUE, we are processing a .debug_types section instead of
3423 a .debug_info section.
3424 The information displayed is restricted by the values in DWARF_START_DIE
3425 and DWARF_CUTOFF_LEVEL.
3426 Returns TRUE upon success. Otherwise an error or warning message is
3427 printed and FALSE is returned. */
3429 static bool
3430 process_debug_info (struct dwarf_section * section,
3431 void *file,
3432 enum dwarf_section_display_enum abbrev_sec,
3433 bool do_loc,
3434 bool do_types)
3436 unsigned char *start = section->start;
3437 unsigned char *end = start + section->size;
3438 unsigned char *section_begin;
3439 unsigned int unit;
3440 unsigned int num_units = 0;
3442 /* First scan the section to get the number of comp units.
3443 Length sanity checks are done here. */
3444 for (section_begin = start, num_units = 0; section_begin < end;
3445 num_units ++)
3447 dwarf_vma length;
3449 /* Read the first 4 bytes. For a 32-bit DWARF section, this
3450 will be the length. For a 64-bit DWARF section, it'll be
3451 the escape code 0xffffffff followed by an 8 byte length. */
3452 SAFE_BYTE_GET_AND_INC (length, section_begin, 4, end);
3454 if (length == 0xffffffff)
3455 SAFE_BYTE_GET_AND_INC (length, section_begin, 8, end);
3456 else if (length >= 0xfffffff0 && length < 0xffffffff)
3458 warn (_("Reserved length value (0x%s) found in section %s\n"),
3459 dwarf_vmatoa ("x", length), section->name);
3460 return false;
3463 /* Negative values are illegal, they may even cause infinite
3464 looping. This can happen if we can't accurately apply
3465 relocations to an object file, or if the file is corrupt. */
3466 if (length > (size_t) (end - section_begin))
3468 warn (_("Corrupt unit length (got 0x%s expected at most 0x%s) in section %s\n"),
3469 dwarf_vmatoa ("x", length),
3470 dwarf_vmatoa ("x", end - section_begin),
3471 section->name);
3472 return false;
3474 section_begin += length;
3477 if (num_units == 0)
3479 error (_("No comp units in %s section ?\n"), section->name);
3480 return false;
3483 if ((do_loc || do_debug_loc || do_debug_ranges)
3484 && num_debug_info_entries == 0
3485 && ! do_types)
3488 /* Then allocate an array to hold the information. */
3489 debug_information = (debug_info *) cmalloc (num_units,
3490 sizeof (* debug_information));
3491 if (debug_information == NULL)
3493 error (_("Not enough memory for a debug info array of %u entries\n"),
3494 num_units);
3495 alloc_num_debug_info_entries = num_debug_info_entries = 0;
3496 return false;
3499 /* PR 17531: file: 92ca3797.
3500 We cannot rely upon the debug_information array being initialised
3501 before it is used. A corrupt file could easily contain references
3502 to a unit for which information has not been made available. So
3503 we ensure that the array is zeroed here. */
3504 memset (debug_information, 0, num_units * sizeof (*debug_information));
3506 alloc_num_debug_info_entries = num_units;
3509 if (!do_loc)
3511 load_debug_section_with_follow (str, file);
3512 load_debug_section_with_follow (line_str, file);
3513 load_debug_section_with_follow (str_dwo, file);
3514 load_debug_section_with_follow (str_index, file);
3515 load_debug_section_with_follow (str_index_dwo, file);
3516 load_debug_section_with_follow (debug_addr, file);
3519 load_debug_section_with_follow (abbrev_sec, file);
3520 if (debug_displays [abbrev_sec].section.start == NULL)
3522 warn (_("Unable to locate %s section!\n"),
3523 debug_displays [abbrev_sec].section.uncompressed_name);
3524 return false;
3527 if (!do_loc && dwarf_start_die == 0)
3528 introduce (section, false);
3530 free_all_abbrevs ();
3531 free (cu_abbrev_map);
3532 cu_abbrev_map = NULL;
3533 next_free_abbrev_map_entry = 0;
3535 /* In order to be able to resolve DW_FORM_ref_attr forms we need
3536 to load *all* of the abbrevs for all CUs in this .debug_info
3537 section. This does effectively mean that we (partially) read
3538 every CU header twice. */
3539 for (section_begin = start; start < end;)
3541 DWARF2_Internal_CompUnit compunit;
3542 unsigned char * hdrptr;
3543 dwarf_vma abbrev_base;
3544 size_t abbrev_size;
3545 dwarf_vma cu_offset;
3546 unsigned int offset_size;
3547 struct cu_tu_set * this_set;
3548 abbrev_list * list;
3549 unsigned char *end_cu;
3551 hdrptr = start;
3552 cu_offset = start - section_begin;
3554 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3556 if (compunit.cu_length == 0xffffffff)
3558 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3559 offset_size = 8;
3561 else
3562 offset_size = 4;
3563 end_cu = hdrptr + compunit.cu_length;
3565 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3567 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3569 if (compunit.cu_version < 5)
3571 compunit.cu_unit_type = DW_UT_compile;
3572 /* Initialize it due to a false compiler warning. */
3573 compunit.cu_pointer_size = -1;
3575 else
3577 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3578 do_types = (compunit.cu_unit_type == DW_UT_type);
3580 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3583 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size,
3584 end_cu);
3586 if (compunit.cu_unit_type == DW_UT_split_compile
3587 || compunit.cu_unit_type == DW_UT_skeleton)
3589 uint64_t dwo_id;
3590 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3593 if (this_set == NULL)
3595 abbrev_base = 0;
3596 abbrev_size = debug_displays [abbrev_sec].section.size;
3598 else
3600 abbrev_base = this_set->section_offsets [DW_SECT_ABBREV];
3601 abbrev_size = this_set->section_sizes [DW_SECT_ABBREV];
3604 list = find_abbrev_list_by_abbrev_offset (abbrev_base,
3605 compunit.cu_abbrev_offset);
3606 if (list == NULL)
3608 unsigned char * next;
3610 list = new_abbrev_list (abbrev_base,
3611 compunit.cu_abbrev_offset);
3612 next = process_abbrev_set (&debug_displays[abbrev_sec].section,
3613 abbrev_base, abbrev_size,
3614 compunit.cu_abbrev_offset, list);
3615 list->start_of_next_abbrevs = next;
3618 start = end_cu;
3619 record_abbrev_list_for_cu (cu_offset, start - section_begin, list);
3622 for (start = section_begin, unit = 0; start < end; unit++)
3624 DWARF2_Internal_CompUnit compunit;
3625 unsigned char *hdrptr;
3626 unsigned char *tags;
3627 int level, last_level, saved_level;
3628 dwarf_vma cu_offset;
3629 unsigned int offset_size;
3630 dwarf_vma signature = 0;
3631 dwarf_vma type_offset = 0;
3632 struct cu_tu_set *this_set;
3633 dwarf_vma abbrev_base;
3634 size_t abbrev_size;
3635 abbrev_list * list = NULL;
3636 unsigned char *end_cu;
3638 hdrptr = start;
3639 cu_offset = start - section_begin;
3641 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 4, end);
3643 if (compunit.cu_length == 0xffffffff)
3645 SAFE_BYTE_GET_AND_INC (compunit.cu_length, hdrptr, 8, end);
3646 offset_size = 8;
3648 else
3649 offset_size = 4;
3650 end_cu = hdrptr + compunit.cu_length;
3652 SAFE_BYTE_GET_AND_INC (compunit.cu_version, hdrptr, 2, end_cu);
3654 this_set = find_cu_tu_set_v2 (cu_offset, do_types);
3656 if (compunit.cu_version < 5)
3658 compunit.cu_unit_type = DW_UT_compile;
3659 /* Initialize it due to a false compiler warning. */
3660 compunit.cu_pointer_size = -1;
3662 else
3664 SAFE_BYTE_GET_AND_INC (compunit.cu_unit_type, hdrptr, 1, end_cu);
3665 do_types = (compunit.cu_unit_type == DW_UT_type);
3667 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3670 SAFE_BYTE_GET_AND_INC (compunit.cu_abbrev_offset, hdrptr, offset_size, 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 if (compunit.cu_version < 5)
3684 SAFE_BYTE_GET_AND_INC (compunit.cu_pointer_size, hdrptr, 1, end_cu);
3686 bool do_dwo_id = false;
3687 uint64_t dwo_id = 0;
3688 if (compunit.cu_unit_type == DW_UT_split_compile
3689 || compunit.cu_unit_type == DW_UT_skeleton)
3691 SAFE_BYTE_GET_AND_INC (dwo_id, hdrptr, 8, end_cu);
3692 do_dwo_id = true;
3695 /* PR 17512: file: 001-108546-0.001:0.1. */
3696 if (compunit.cu_pointer_size < 2 || compunit.cu_pointer_size > 8)
3698 warn (_("Invalid pointer size (%d) in compunit header, using %d instead\n"),
3699 compunit.cu_pointer_size, offset_size);
3700 compunit.cu_pointer_size = offset_size;
3703 if (do_types)
3705 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, end_cu);
3706 SAFE_BYTE_GET_AND_INC (type_offset, hdrptr, offset_size, end_cu);
3709 if (dwarf_start_die >= (size_t) (end_cu - section_begin))
3711 start = end_cu;
3712 continue;
3715 if ((do_loc || do_debug_loc || do_debug_ranges)
3716 && num_debug_info_entries == 0
3717 && alloc_num_debug_info_entries > unit
3718 && ! do_types)
3720 debug_information [unit].cu_offset = cu_offset;
3721 debug_information [unit].pointer_size
3722 = compunit.cu_pointer_size;
3723 debug_information [unit].offset_size = offset_size;
3724 debug_information [unit].dwarf_version = compunit.cu_version;
3725 debug_information [unit].base_address = 0;
3726 debug_information [unit].addr_base = DEBUG_INFO_UNAVAILABLE;
3727 debug_information [unit].ranges_base = DEBUG_INFO_UNAVAILABLE;
3728 debug_information [unit].loc_offsets = NULL;
3729 debug_information [unit].have_frame_base = NULL;
3730 debug_information [unit].max_loc_offsets = 0;
3731 debug_information [unit].num_loc_offsets = 0;
3732 debug_information [unit].range_lists = NULL;
3733 debug_information [unit].max_range_lists= 0;
3734 debug_information [unit].num_range_lists = 0;
3737 if (!do_loc && dwarf_start_die == 0)
3739 printf (_(" Compilation Unit @ offset 0x%s:\n"),
3740 dwarf_vmatoa ("x", cu_offset));
3741 printf (_(" Length: 0x%s (%s)\n"),
3742 dwarf_vmatoa ("x", compunit.cu_length),
3743 offset_size == 8 ? "64-bit" : "32-bit");
3744 printf (_(" Version: %d\n"), compunit.cu_version);
3745 if (compunit.cu_version >= 5)
3747 const char *name = get_DW_UT_name (compunit.cu_unit_type);
3749 printf (_(" Unit Type: %s (%x)\n"),
3750 name ? name : "???",
3751 compunit.cu_unit_type);
3753 printf (_(" Abbrev Offset: 0x%s\n"),
3754 dwarf_vmatoa ("x", compunit.cu_abbrev_offset));
3755 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
3756 if (do_types)
3758 printf (_(" Signature: 0x%s\n"),
3759 dwarf_vmatoa ("x", signature));
3760 printf (_(" Type Offset: 0x%s\n"),
3761 dwarf_vmatoa ("x", type_offset));
3763 if (do_dwo_id)
3764 printf (_(" DWO ID: 0x%s\n"), dwarf_vmatoa ("x", dwo_id));
3765 if (this_set != NULL)
3767 dwarf_vma *offsets = this_set->section_offsets;
3768 size_t *sizes = this_set->section_sizes;
3770 printf (_(" Section contributions:\n"));
3771 printf (_(" .debug_abbrev.dwo: 0x%s 0x%s\n"),
3772 dwarf_vmatoa ("x", offsets [DW_SECT_ABBREV]),
3773 dwarf_vmatoa ("x", sizes [DW_SECT_ABBREV]));
3774 printf (_(" .debug_line.dwo: 0x%s 0x%s\n"),
3775 dwarf_vmatoa ("x", offsets [DW_SECT_LINE]),
3776 dwarf_vmatoa ("x", sizes [DW_SECT_LINE]));
3777 printf (_(" .debug_loc.dwo: 0x%s 0x%s\n"),
3778 dwarf_vmatoa ("x", offsets [DW_SECT_LOC]),
3779 dwarf_vmatoa ("x", sizes [DW_SECT_LOC]));
3780 printf (_(" .debug_str_offsets.dwo: 0x%s 0x%s\n"),
3781 dwarf_vmatoa ("x", offsets [DW_SECT_STR_OFFSETS]),
3782 dwarf_vmatoa ("x", sizes [DW_SECT_STR_OFFSETS]));
3786 tags = hdrptr;
3787 start = end_cu;
3789 if (compunit.cu_version < 2 || compunit.cu_version > 5)
3791 warn (_("CU at offset %s contains corrupt or "
3792 "unsupported version number: %d.\n"),
3793 dwarf_vmatoa ("x", cu_offset), compunit.cu_version);
3794 continue;
3797 if (compunit.cu_unit_type != DW_UT_compile
3798 && compunit.cu_unit_type != DW_UT_partial
3799 && compunit.cu_unit_type != DW_UT_type
3800 && compunit.cu_unit_type != DW_UT_split_compile
3801 && compunit.cu_unit_type != DW_UT_skeleton)
3803 warn (_("CU at offset %s contains corrupt or "
3804 "unsupported unit type: %d.\n"),
3805 dwarf_vmatoa ("x", cu_offset), compunit.cu_unit_type);
3806 continue;
3809 /* Process the abbrevs used by this compilation unit. */
3810 list = find_abbrev_list_by_abbrev_offset (abbrev_base,
3811 compunit.cu_abbrev_offset);
3812 if (list == NULL)
3814 unsigned char *next;
3816 list = new_abbrev_list (abbrev_base,
3817 compunit.cu_abbrev_offset);
3818 next = process_abbrev_set (&debug_displays[abbrev_sec].section,
3819 abbrev_base, abbrev_size,
3820 compunit.cu_abbrev_offset, list);
3821 list->start_of_next_abbrevs = next;
3824 level = 0;
3825 last_level = level;
3826 saved_level = -1;
3827 while (tags < start)
3829 unsigned long abbrev_number;
3830 unsigned long die_offset;
3831 abbrev_entry *entry;
3832 abbrev_attr *attr;
3833 int do_printing = 1;
3835 die_offset = tags - section_begin;
3837 READ_ULEB (abbrev_number, tags, start);
3839 /* A null DIE marks the end of a list of siblings or it may also be
3840 a section padding. */
3841 if (abbrev_number == 0)
3843 /* Check if it can be a section padding for the last CU. */
3844 if (level == 0 && start == end)
3846 unsigned char *chk;
3848 for (chk = tags; chk < start; chk++)
3849 if (*chk != 0)
3850 break;
3851 if (chk == start)
3852 break;
3855 if (!do_loc && die_offset >= dwarf_start_die
3856 && (dwarf_cutoff_level == -1
3857 || level < dwarf_cutoff_level))
3858 printf (_(" <%d><%lx>: Abbrev Number: 0\n"),
3859 level, die_offset);
3861 --level;
3862 if (level < 0)
3864 static unsigned num_bogus_warns = 0;
3866 if (num_bogus_warns < 3)
3868 warn (_("Bogus end-of-siblings marker detected at offset %lx in %s section\n"),
3869 die_offset, section->name);
3870 num_bogus_warns ++;
3871 if (num_bogus_warns == 3)
3872 warn (_("Further warnings about bogus end-of-sibling markers suppressed\n"));
3875 if (dwarf_start_die != 0 && level < saved_level)
3876 return true;
3877 continue;
3880 if (!do_loc)
3882 if (dwarf_start_die != 0 && die_offset < dwarf_start_die)
3883 do_printing = 0;
3884 else
3886 if (dwarf_start_die != 0 && die_offset == dwarf_start_die)
3887 saved_level = level;
3888 do_printing = (dwarf_cutoff_level == -1
3889 || level < dwarf_cutoff_level);
3890 if (do_printing)
3891 printf (_(" <%d><%lx>: Abbrev Number: %lu"),
3892 level, die_offset, abbrev_number);
3893 else if (dwarf_cutoff_level == -1
3894 || last_level < dwarf_cutoff_level)
3895 printf (_(" <%d><%lx>: ...\n"), level, die_offset);
3896 last_level = level;
3900 /* Scan through the abbreviation list until we reach the
3901 correct entry. */
3902 if (list == NULL)
3903 continue;
3905 for (entry = list->first_abbrev; entry != NULL; entry = entry->next)
3906 if (entry->number == abbrev_number)
3907 break;
3909 if (entry == NULL)
3911 if (!do_loc && do_printing)
3913 printf ("\n");
3914 fflush (stdout);
3916 warn (_("DIE at offset 0x%lx refers to abbreviation number %lu which does not exist\n"),
3917 die_offset, abbrev_number);
3918 return false;
3921 if (!do_loc && do_printing)
3922 printf (" (%s)\n", get_TAG_name (entry->tag));
3924 switch (entry->tag)
3926 default:
3927 need_base_address = 0;
3928 break;
3929 case DW_TAG_compile_unit:
3930 need_base_address = 1;
3931 need_dwo_info = do_loc;
3932 break;
3933 case DW_TAG_entry_point:
3934 case DW_TAG_subprogram:
3935 need_base_address = 0;
3936 /* Assuming that there is no DW_AT_frame_base. */
3937 have_frame_base = 0;
3938 break;
3941 debug_info *debug_info_p =
3942 (debug_information && unit < alloc_num_debug_info_entries)
3943 ? debug_information + unit : NULL;
3945 assert (!debug_info_p
3946 || (debug_info_p->num_loc_offsets
3947 == debug_info_p->num_loc_views));
3949 for (attr = entry->first_attr;
3950 attr && attr->attribute;
3951 attr = attr->next)
3953 if (! do_loc && do_printing)
3954 /* Show the offset from where the tag was extracted. */
3955 printf (" <%lx>", (unsigned long)(tags - section_begin));
3956 tags = read_and_display_attr (attr->attribute,
3957 attr->form,
3958 attr->implicit_const,
3959 section_begin,
3960 tags,
3961 start,
3962 cu_offset,
3963 compunit.cu_pointer_size,
3964 offset_size,
3965 compunit.cu_version,
3966 debug_info_p,
3967 do_loc || ! do_printing,
3968 section,
3969 this_set,
3970 level);
3973 /* If a locview attribute appears before a location one,
3974 make sure we don't associate it with an earlier
3975 loclist. */
3976 if (debug_info_p)
3977 switch (debug_info_p->num_loc_offsets - debug_info_p->num_loc_views)
3979 case 1:
3980 debug_info_p->loc_views [debug_info_p->num_loc_views] = vm1;
3981 debug_info_p->num_loc_views++;
3982 assert (debug_info_p->num_loc_views
3983 == debug_info_p->num_loc_offsets);
3984 break;
3986 case 0:
3987 break;
3989 case -1:
3990 warn(_("DIE has locviews without loclist\n"));
3991 debug_info_p->num_loc_views--;
3992 break;
3994 default:
3995 assert (0);
3998 if (entry->children)
3999 ++level;
4003 /* Set num_debug_info_entries here so that it can be used to check if
4004 we need to process .debug_loc and .debug_ranges sections. */
4005 if ((do_loc || do_debug_loc || do_debug_ranges)
4006 && num_debug_info_entries == 0
4007 && ! do_types)
4009 if (num_units > alloc_num_debug_info_entries)
4010 num_debug_info_entries = alloc_num_debug_info_entries;
4011 else
4012 num_debug_info_entries = num_units;
4015 if (!do_loc)
4016 printf ("\n");
4018 return true;
4021 /* Locate and scan the .debug_info section in the file and record the pointer
4022 sizes and offsets for the compilation units in it. Usually an executable
4023 will have just one pointer size, but this is not guaranteed, and so we try
4024 not to make any assumptions. Returns zero upon failure, or the number of
4025 compilation units upon success. */
4027 static unsigned int
4028 load_debug_info (void * file)
4030 /* If we have already tried and failed to load the .debug_info
4031 section then do not bother to repeat the task. */
4032 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
4033 return 0;
4035 /* If we already have the information there is nothing else to do. */
4036 if (num_debug_info_entries > 0)
4037 return num_debug_info_entries;
4039 /* If this is a DWARF package file, load the CU and TU indexes. */
4040 (void) load_cu_tu_indexes (file);
4042 if (load_debug_section_with_follow (info, file)
4043 && process_debug_info (&debug_displays [info].section, file, abbrev, true, false))
4044 return num_debug_info_entries;
4046 if (load_debug_section_with_follow (info_dwo, file)
4047 && process_debug_info (&debug_displays [info_dwo].section, file,
4048 abbrev_dwo, true, false))
4049 return num_debug_info_entries;
4051 num_debug_info_entries = DEBUG_INFO_UNAVAILABLE;
4052 return 0;
4055 /* Read a DWARF .debug_line section header starting at DATA.
4056 Upon success returns an updated DATA pointer and the LINFO
4057 structure and the END_OF_SEQUENCE pointer will be filled in.
4058 Otherwise returns NULL. */
4060 static unsigned char *
4061 read_debug_line_header (struct dwarf_section * section,
4062 unsigned char * data,
4063 unsigned char * end,
4064 DWARF2_Internal_LineInfo * linfo,
4065 unsigned char ** end_of_sequence)
4067 unsigned char *hdrptr;
4069 /* Extract information from the Line Number Program Header.
4070 (section 6.2.4 in the Dwarf3 doc). */
4071 hdrptr = data;
4073 /* Get and check the length of the block. */
4074 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 4, end);
4076 if (linfo->li_length == 0xffffffff)
4078 /* This section is 64-bit DWARF 3. */
4079 SAFE_BYTE_GET_AND_INC (linfo->li_length, hdrptr, 8, end);
4080 linfo->li_offset_size = 8;
4082 else
4083 linfo->li_offset_size = 4;
4085 if (linfo->li_length > (size_t) (end - hdrptr))
4087 /* If the length field has a relocation against it, then we should
4088 not complain if it is inaccurate (and probably negative). This
4089 happens in object files when the .debug_line section is actually
4090 comprised of several different .debug_line.* sections, (some of
4091 which may be removed by linker garbage collection), and a relocation
4092 is used to compute the correct length once that is done. */
4093 if (reloc_at (section, (hdrptr - section->start) - linfo->li_offset_size))
4095 linfo->li_length = end - hdrptr;
4097 else
4099 warn (_("The length field (0x%lx) in the debug_line header is wrong - the section is too small\n"),
4100 (long) linfo->li_length);
4101 return NULL;
4104 end = hdrptr + linfo->li_length;
4106 /* Get and check the version number. */
4107 SAFE_BYTE_GET_AND_INC (linfo->li_version, hdrptr, 2, end);
4109 if (linfo->li_version != 2
4110 && linfo->li_version != 3
4111 && linfo->li_version != 4
4112 && linfo->li_version != 5)
4114 warn (_("Only DWARF version 2, 3, 4 and 5 line info "
4115 "is currently supported.\n"));
4116 return NULL;
4119 if (linfo->li_version >= 5)
4121 SAFE_BYTE_GET_AND_INC (linfo->li_address_size, hdrptr, 1, end);
4123 SAFE_BYTE_GET_AND_INC (linfo->li_segment_size, hdrptr, 1, end);
4124 if (linfo->li_segment_size != 0)
4126 warn (_("The %s section contains "
4127 "unsupported segment selector size: %d.\n"),
4128 section->name, linfo->li_segment_size);
4129 return NULL;
4133 SAFE_BYTE_GET_AND_INC (linfo->li_prologue_length, hdrptr,
4134 linfo->li_offset_size, end);
4135 SAFE_BYTE_GET_AND_INC (linfo->li_min_insn_length, hdrptr, 1, end);
4137 if (linfo->li_version >= 4)
4139 SAFE_BYTE_GET_AND_INC (linfo->li_max_ops_per_insn, hdrptr, 1, end);
4141 if (linfo->li_max_ops_per_insn == 0)
4143 warn (_("Invalid maximum operations per insn.\n"));
4144 return NULL;
4147 else
4148 linfo->li_max_ops_per_insn = 1;
4150 SAFE_BYTE_GET_AND_INC (linfo->li_default_is_stmt, hdrptr, 1, end);
4151 SAFE_SIGNED_BYTE_GET_AND_INC (linfo->li_line_base, hdrptr, 1, end);
4152 SAFE_BYTE_GET_AND_INC (linfo->li_line_range, hdrptr, 1, end);
4153 SAFE_BYTE_GET_AND_INC (linfo->li_opcode_base, hdrptr, 1, end);
4155 *end_of_sequence = end;
4156 return hdrptr;
4159 static unsigned char *
4160 display_formatted_table (unsigned char *data,
4161 unsigned char *start,
4162 unsigned char *end,
4163 const DWARF2_Internal_LineInfo *linfo,
4164 struct dwarf_section *section,
4165 bool is_dir)
4167 unsigned char *format_start, format_count, *format, formati;
4168 dwarf_vma data_count, datai;
4169 unsigned int namepass, last_entry = 0;
4170 const char * table_name = is_dir ? N_("Directory Table") : N_("File Name Table");
4172 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4173 if (do_checks && format_count > 5)
4174 warn (_("Unexpectedly large number of columns in the %s (%u)\n"),
4175 table_name, format_count);
4177 format_start = data;
4178 for (formati = 0; formati < format_count; formati++)
4180 SKIP_ULEB (data, end);
4181 SKIP_ULEB (data, end);
4182 if (data >= end)
4184 warn (_("%s: Corrupt format description entry\n"), table_name);
4185 return data;
4189 READ_ULEB (data_count, data, end);
4190 if (data_count == 0)
4192 printf (_("\n The %s is empty.\n"), table_name);
4193 return data;
4195 else if (data >= end)
4197 warn (_("%s: Corrupt entry count - expected %s but none found\n"),
4198 table_name, dwarf_vmatoa ("x", data_count));
4199 return data;
4202 else if (format_count == 0)
4204 warn (_("%s: format count is zero, but the table is not empty\n"),
4205 table_name);
4206 return end;
4209 printf (_("\n The %s (offset 0x%lx, lines %s, columns %u):\n"),
4210 table_name, (long) (data - start), dwarf_vmatoa ("u", data_count),
4211 format_count);
4213 printf (_(" Entry"));
4214 /* Delay displaying name as the last entry for better screen layout. */
4215 for (namepass = 0; namepass < 2; namepass++)
4217 format = format_start;
4218 for (formati = 0; formati < format_count; formati++)
4220 dwarf_vma content_type;
4222 READ_ULEB (content_type, format, end);
4223 if ((content_type == DW_LNCT_path) == (namepass == 1))
4224 switch (content_type)
4226 case DW_LNCT_path:
4227 printf (_("\tName"));
4228 break;
4229 case DW_LNCT_directory_index:
4230 printf (_("\tDir"));
4231 break;
4232 case DW_LNCT_timestamp:
4233 printf (_("\tTime"));
4234 break;
4235 case DW_LNCT_size:
4236 printf (_("\tSize"));
4237 break;
4238 case DW_LNCT_MD5:
4239 printf (_("\tMD5\t\t\t"));
4240 break;
4241 default:
4242 printf (_("\t(Unknown format content type %s)"),
4243 dwarf_vmatoa ("u", content_type));
4245 SKIP_ULEB (format, end);
4248 putchar ('\n');
4250 for (datai = 0; datai < data_count; datai++)
4252 unsigned char *datapass = data;
4254 printf (" %d", last_entry++);
4255 /* Delay displaying name as the last entry for better screen layout. */
4256 for (namepass = 0; namepass < 2; namepass++)
4258 format = format_start;
4259 data = datapass;
4260 for (formati = 0; formati < format_count; formati++)
4262 dwarf_vma content_type, form;
4264 READ_ULEB (content_type, format, end);
4265 READ_ULEB (form, format, end);
4266 data = read_and_display_attr_value (0, form, 0, start, data, end,
4267 0, 0, linfo->li_offset_size,
4268 linfo->li_version, NULL,
4269 ((content_type == DW_LNCT_path) != (namepass == 1)),
4270 section, NULL, '\t', -1);
4274 if (data >= end && (datai < data_count - 1))
4276 warn (_("\n%s: Corrupt entries list\n"), table_name);
4277 return data;
4279 putchar ('\n');
4281 return data;
4284 static int
4285 display_debug_sup (struct dwarf_section * section,
4286 void * file ATTRIBUTE_UNUSED)
4288 unsigned char * start = section->start;
4289 unsigned char * end = section->start + section->size;
4290 unsigned int version;
4291 char is_supplementary;
4292 const unsigned char * sup_filename;
4293 size_t sup_filename_len;
4294 unsigned int num_read;
4295 int status;
4296 dwarf_vma checksum_len;
4299 introduce (section, true);
4300 if (section->size < 4)
4302 error (_("corrupt .debug_sup section: size is too small\n"));
4303 return 0;
4306 /* Read the data. */
4307 SAFE_BYTE_GET_AND_INC (version, start, 2, end);
4308 if (version < 5)
4309 warn (_("corrupt .debug_sup section: version < 5"));
4311 SAFE_BYTE_GET_AND_INC (is_supplementary, start, 1, end);
4312 if (is_supplementary != 0 && is_supplementary != 1)
4313 warn (_("corrupt .debug_sup section: is_supplementary not 0 or 1\n"));
4315 sup_filename = start;
4316 if (is_supplementary && sup_filename[0] != 0)
4317 warn (_("corrupt .debug_sup section: filename not empty in supplementary section\n"));
4319 sup_filename_len = strnlen ((const char *) start, end - start);
4320 if (sup_filename_len == (size_t) (end - start))
4322 error (_("corrupt .debug_sup section: filename is not NUL terminated\n"));
4323 return 0;
4325 start += sup_filename_len + 1;
4327 checksum_len = read_leb128 (start, end, false /* unsigned */, & num_read, & status);
4328 if (status)
4330 error (_("corrupt .debug_sup section: bad LEB128 field for checksum length\n"));
4331 checksum_len = 0;
4333 start += num_read;
4334 if (checksum_len > (dwarf_vma) (end - start))
4336 error (_("corrupt .debug_sup section: checksum length is longer than the remaining section length\n"));
4337 checksum_len = end - start;
4339 else if (checksum_len < (dwarf_vma) (end - start))
4341 warn (_("corrupt .debug_sup section: there are 0x%lx extra, unused bytes at the end of the section\n"),
4342 (long) ((end - start) - checksum_len));
4345 printf (_(" Version: %u\n"), version);
4346 printf (_(" Is Supp: %u\n"), is_supplementary);
4347 printf (_(" Filename: %s\n"), sup_filename);
4348 printf (_(" Checksum Len: %lu\n"), (long) checksum_len);
4349 if (checksum_len > 0)
4351 printf (_(" Checksum: "));
4352 while (checksum_len--)
4353 printf ("0x%x ", * start++ );
4354 printf ("\n");
4356 return 1;
4359 static int
4360 display_debug_lines_raw (struct dwarf_section * section,
4361 unsigned char * data,
4362 unsigned char * end,
4363 void * file)
4365 unsigned char *start = section->start;
4366 int verbose_view = 0;
4368 introduce (section, true);
4370 while (data < end)
4372 static DWARF2_Internal_LineInfo saved_linfo;
4373 DWARF2_Internal_LineInfo linfo;
4374 unsigned char *standard_opcodes;
4375 unsigned char *end_of_sequence;
4376 int i;
4378 if (startswith (section->name, ".debug_line.")
4379 /* Note: the following does not apply to .debug_line.dwo sections.
4380 These are full debug_line sections. */
4381 && strcmp (section->name, ".debug_line.dwo") != 0)
4383 /* Sections named .debug_line.<foo> are fragments of a .debug_line
4384 section containing just the Line Number Statements. They are
4385 created by the assembler and intended to be used alongside gcc's
4386 -ffunction-sections command line option. When the linker's
4387 garbage collection decides to discard a .text.<foo> section it
4388 can then also discard the line number information in .debug_line.<foo>.
4390 Since the section is a fragment it does not have the details
4391 needed to fill out a LineInfo structure, so instead we use the
4392 details from the last full debug_line section that we processed. */
4393 end_of_sequence = end;
4394 standard_opcodes = NULL;
4395 linfo = saved_linfo;
4396 /* PR 17531: file: 0522b371. */
4397 if (linfo.li_line_range == 0)
4399 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4400 return 0;
4402 reset_state_machine (linfo.li_default_is_stmt);
4404 else
4406 unsigned char * hdrptr;
4408 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4409 & end_of_sequence)) == NULL)
4410 return 0;
4412 printf (_(" Offset: 0x%lx\n"), (long)(data - start));
4413 printf (_(" Length: %ld\n"), (long) linfo.li_length);
4414 printf (_(" DWARF Version: %d\n"), linfo.li_version);
4415 if (linfo.li_version >= 5)
4417 printf (_(" Address size (bytes): %d\n"), linfo.li_address_size);
4418 printf (_(" Segment selector (bytes): %d\n"), linfo.li_segment_size);
4420 printf (_(" Prologue Length: %d\n"), (int) linfo.li_prologue_length);
4421 printf (_(" Minimum Instruction Length: %d\n"), linfo.li_min_insn_length);
4422 if (linfo.li_version >= 4)
4423 printf (_(" Maximum Ops per Instruction: %d\n"), linfo.li_max_ops_per_insn);
4424 printf (_(" Initial value of 'is_stmt': %d\n"), linfo.li_default_is_stmt);
4425 printf (_(" Line Base: %d\n"), linfo.li_line_base);
4426 printf (_(" Line Range: %d\n"), linfo.li_line_range);
4427 printf (_(" Opcode Base: %d\n"), linfo.li_opcode_base);
4429 /* PR 17512: file: 1665-6428-0.004. */
4430 if (linfo.li_line_range == 0)
4432 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4433 linfo.li_line_range = 1;
4436 reset_state_machine (linfo.li_default_is_stmt);
4438 /* Display the contents of the Opcodes table. */
4439 standard_opcodes = hdrptr;
4441 /* PR 17512: file: 002-417945-0.004. */
4442 if (standard_opcodes + linfo.li_opcode_base >= end)
4444 warn (_("Line Base extends beyond end of section\n"));
4445 return 0;
4448 printf (_("\n Opcodes:\n"));
4450 for (i = 1; i < linfo.li_opcode_base; i++)
4451 printf (ngettext (" Opcode %d has %d arg\n",
4452 " Opcode %d has %d args\n",
4453 standard_opcodes[i - 1]),
4454 i, standard_opcodes[i - 1]);
4456 /* Display the contents of the Directory table. */
4457 data = standard_opcodes + linfo.li_opcode_base - 1;
4459 if (linfo.li_version >= 5)
4461 load_debug_section_with_follow (line_str, file);
4463 data = display_formatted_table (data, start, end, &linfo, section,
4464 true);
4465 data = display_formatted_table (data, start, end, &linfo, section,
4466 false);
4468 else
4470 if (*data == 0)
4471 printf (_("\n The Directory Table is empty.\n"));
4472 else
4474 unsigned int last_dir_entry = 0;
4476 printf (_("\n The Directory Table (offset 0x%lx):\n"),
4477 (long)(data - start));
4479 while (data < end && *data != 0)
4481 printf (" %d\t%.*s\n", ++last_dir_entry, (int) (end - data), data);
4483 data += strnlen ((char *) data, end - data);
4484 if (data < end)
4485 data++;
4488 /* PR 17512: file: 002-132094-0.004. */
4489 if (data >= end - 1)
4490 break;
4493 /* Skip the NUL at the end of the table. */
4494 if (data < end)
4495 data++;
4497 /* Display the contents of the File Name table. */
4498 if (data >= end || *data == 0)
4499 printf (_("\n The File Name Table is empty.\n"));
4500 else
4502 printf (_("\n The File Name Table (offset 0x%lx):\n"),
4503 (long)(data - start));
4504 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
4506 while (data < end && *data != 0)
4508 unsigned char *name;
4509 dwarf_vma val;
4511 printf (" %d\t", ++state_machine_regs.last_file_entry);
4512 name = data;
4513 data += strnlen ((char *) data, end - data);
4514 if (data < end)
4515 data++;
4517 READ_ULEB (val, data, end);
4518 printf ("%s\t", dwarf_vmatoa ("u", val));
4519 READ_ULEB (val, data, end);
4520 printf ("%s\t", dwarf_vmatoa ("u", val));
4521 READ_ULEB (val, data, end);
4522 printf ("%s\t", dwarf_vmatoa ("u", val));
4523 printf ("%.*s\n", (int)(end - name), name);
4525 if (data >= end)
4527 warn (_("Corrupt file name table entry\n"));
4528 break;
4533 /* Skip the NUL at the end of the table. */
4534 if (data < end)
4535 data++;
4538 putchar ('\n');
4539 saved_linfo = linfo;
4542 /* Now display the statements. */
4543 if (data >= end_of_sequence)
4544 printf (_(" No Line Number Statements.\n"));
4545 else
4547 printf (_(" Line Number Statements:\n"));
4549 while (data < end_of_sequence)
4551 unsigned char op_code;
4552 dwarf_signed_vma adv;
4553 dwarf_vma uladv;
4555 printf (" [0x%08lx]", (long)(data - start));
4557 op_code = *data++;
4559 if (op_code >= linfo.li_opcode_base)
4561 op_code -= linfo.li_opcode_base;
4562 uladv = (op_code / linfo.li_line_range);
4563 if (linfo.li_max_ops_per_insn == 1)
4565 uladv *= linfo.li_min_insn_length;
4566 state_machine_regs.address += uladv;
4567 if (uladv)
4568 state_machine_regs.view = 0;
4569 printf (_(" Special opcode %d: "
4570 "advance Address by %s to 0x%s%s"),
4571 op_code, dwarf_vmatoa ("u", uladv),
4572 dwarf_vmatoa ("x", state_machine_regs.address),
4573 verbose_view && uladv
4574 ? _(" (reset view)") : "");
4576 else
4578 unsigned addrdelta
4579 = ((state_machine_regs.op_index + uladv)
4580 / linfo.li_max_ops_per_insn)
4581 * linfo.li_min_insn_length;
4583 state_machine_regs.address += addrdelta;
4584 state_machine_regs.op_index
4585 = (state_machine_regs.op_index + uladv)
4586 % linfo.li_max_ops_per_insn;
4587 if (addrdelta)
4588 state_machine_regs.view = 0;
4589 printf (_(" Special opcode %d: "
4590 "advance Address by %s to 0x%s[%d]%s"),
4591 op_code, dwarf_vmatoa ("u", uladv),
4592 dwarf_vmatoa ("x", state_machine_regs.address),
4593 state_machine_regs.op_index,
4594 verbose_view && addrdelta
4595 ? _(" (reset view)") : "");
4597 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
4598 state_machine_regs.line += adv;
4599 printf (_(" and Line by %s to %d"),
4600 dwarf_vmatoa ("d", adv), state_machine_regs.line);
4601 if (verbose_view || state_machine_regs.view)
4602 printf (_(" (view %u)\n"), state_machine_regs.view);
4603 else
4604 putchar ('\n');
4605 state_machine_regs.view++;
4607 else
4608 switch (op_code)
4610 case DW_LNS_extended_op:
4611 data += process_extended_line_op (data,
4612 linfo.li_default_is_stmt,
4613 end);
4614 break;
4616 case DW_LNS_copy:
4617 printf (_(" Copy"));
4618 if (verbose_view || state_machine_regs.view)
4619 printf (_(" (view %u)\n"), state_machine_regs.view);
4620 else
4621 putchar ('\n');
4622 state_machine_regs.view++;
4623 break;
4625 case DW_LNS_advance_pc:
4626 READ_ULEB (uladv, data, end);
4627 if (linfo.li_max_ops_per_insn == 1)
4629 uladv *= linfo.li_min_insn_length;
4630 state_machine_regs.address += uladv;
4631 if (uladv)
4632 state_machine_regs.view = 0;
4633 printf (_(" Advance PC by %s to 0x%s%s\n"),
4634 dwarf_vmatoa ("u", uladv),
4635 dwarf_vmatoa ("x", state_machine_regs.address),
4636 verbose_view && uladv
4637 ? _(" (reset view)") : "");
4639 else
4641 unsigned addrdelta
4642 = ((state_machine_regs.op_index + uladv)
4643 / linfo.li_max_ops_per_insn)
4644 * linfo.li_min_insn_length;
4645 state_machine_regs.address
4646 += addrdelta;
4647 state_machine_regs.op_index
4648 = (state_machine_regs.op_index + uladv)
4649 % linfo.li_max_ops_per_insn;
4650 if (addrdelta)
4651 state_machine_regs.view = 0;
4652 printf (_(" Advance PC by %s to 0x%s[%d]%s\n"),
4653 dwarf_vmatoa ("u", uladv),
4654 dwarf_vmatoa ("x", state_machine_regs.address),
4655 state_machine_regs.op_index,
4656 verbose_view && addrdelta
4657 ? _(" (reset view)") : "");
4659 break;
4661 case DW_LNS_advance_line:
4662 READ_SLEB (adv, data, end);
4663 state_machine_regs.line += adv;
4664 printf (_(" Advance Line by %s to %d\n"),
4665 dwarf_vmatoa ("d", adv),
4666 state_machine_regs.line);
4667 break;
4669 case DW_LNS_set_file:
4670 READ_ULEB (uladv, data, end);
4671 printf (_(" Set File Name to entry %s in the File Name Table\n"),
4672 dwarf_vmatoa ("u", uladv));
4673 state_machine_regs.file = uladv;
4674 break;
4676 case DW_LNS_set_column:
4677 READ_ULEB (uladv, data, end);
4678 printf (_(" Set column to %s\n"),
4679 dwarf_vmatoa ("u", uladv));
4680 state_machine_regs.column = uladv;
4681 break;
4683 case DW_LNS_negate_stmt:
4684 adv = state_machine_regs.is_stmt;
4685 adv = ! adv;
4686 printf (_(" Set is_stmt to %s\n"), dwarf_vmatoa ("d", adv));
4687 state_machine_regs.is_stmt = adv;
4688 break;
4690 case DW_LNS_set_basic_block:
4691 printf (_(" Set basic block\n"));
4692 state_machine_regs.basic_block = 1;
4693 break;
4695 case DW_LNS_const_add_pc:
4696 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
4697 if (linfo.li_max_ops_per_insn)
4699 uladv *= linfo.li_min_insn_length;
4700 state_machine_regs.address += uladv;
4701 if (uladv)
4702 state_machine_regs.view = 0;
4703 printf (_(" Advance PC by constant %s to 0x%s%s\n"),
4704 dwarf_vmatoa ("u", uladv),
4705 dwarf_vmatoa ("x", state_machine_regs.address),
4706 verbose_view && uladv
4707 ? _(" (reset view)") : "");
4709 else
4711 unsigned addrdelta
4712 = ((state_machine_regs.op_index + uladv)
4713 / linfo.li_max_ops_per_insn)
4714 * linfo.li_min_insn_length;
4715 state_machine_regs.address
4716 += addrdelta;
4717 state_machine_regs.op_index
4718 = (state_machine_regs.op_index + uladv)
4719 % linfo.li_max_ops_per_insn;
4720 if (addrdelta)
4721 state_machine_regs.view = 0;
4722 printf (_(" Advance PC by constant %s to 0x%s[%d]%s\n"),
4723 dwarf_vmatoa ("u", uladv),
4724 dwarf_vmatoa ("x", state_machine_regs.address),
4725 state_machine_regs.op_index,
4726 verbose_view && addrdelta
4727 ? _(" (reset view)") : "");
4729 break;
4731 case DW_LNS_fixed_advance_pc:
4732 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
4733 state_machine_regs.address += uladv;
4734 state_machine_regs.op_index = 0;
4735 printf (_(" Advance PC by fixed size amount %s to 0x%s\n"),
4736 dwarf_vmatoa ("u", uladv),
4737 dwarf_vmatoa ("x", state_machine_regs.address));
4738 /* Do NOT reset view. */
4739 break;
4741 case DW_LNS_set_prologue_end:
4742 printf (_(" Set prologue_end to true\n"));
4743 break;
4745 case DW_LNS_set_epilogue_begin:
4746 printf (_(" Set epilogue_begin to true\n"));
4747 break;
4749 case DW_LNS_set_isa:
4750 READ_ULEB (uladv, data, end);
4751 printf (_(" Set ISA to %s\n"), dwarf_vmatoa ("u", uladv));
4752 break;
4754 default:
4755 printf (_(" Unknown opcode %d with operands: "), op_code);
4757 if (standard_opcodes != NULL)
4758 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
4760 READ_ULEB (uladv, data, end);
4761 printf ("0x%s%s", dwarf_vmatoa ("x", uladv),
4762 i == 1 ? "" : ", ");
4764 putchar ('\n');
4765 break;
4768 putchar ('\n');
4772 return 1;
4775 typedef struct
4777 unsigned char *name;
4778 unsigned int directory_index;
4779 unsigned int modification_date;
4780 unsigned int length;
4781 } File_Entry;
4783 /* Output a decoded representation of the .debug_line section. */
4785 static int
4786 display_debug_lines_decoded (struct dwarf_section * section,
4787 unsigned char * start,
4788 unsigned char * data,
4789 unsigned char * end,
4790 void * fileptr)
4792 static DWARF2_Internal_LineInfo saved_linfo;
4794 introduce (section, false);
4796 while (data < end)
4798 /* This loop amounts to one iteration per compilation unit. */
4799 DWARF2_Internal_LineInfo linfo;
4800 unsigned char *standard_opcodes;
4801 unsigned char *end_of_sequence;
4802 int i;
4803 File_Entry *file_table = NULL;
4804 unsigned int n_files = 0;
4805 unsigned char **directory_table = NULL;
4806 dwarf_vma n_directories = 0;
4808 if (startswith (section->name, ".debug_line.")
4809 /* Note: the following does not apply to .debug_line.dwo sections.
4810 These are full debug_line sections. */
4811 && strcmp (section->name, ".debug_line.dwo") != 0)
4813 /* See comment in display_debug_lines_raw(). */
4814 end_of_sequence = end;
4815 standard_opcodes = NULL;
4816 linfo = saved_linfo;
4817 /* PR 17531: file: 0522b371. */
4818 if (linfo.li_line_range == 0)
4820 warn (_("Partial .debug_line. section encountered without a prior full .debug_line section\n"));
4821 return 0;
4823 reset_state_machine (linfo.li_default_is_stmt);
4825 else
4827 unsigned char *hdrptr;
4829 if ((hdrptr = read_debug_line_header (section, data, end, & linfo,
4830 & end_of_sequence)) == NULL)
4831 return 0;
4833 /* PR 17531: file: 0522b371. */
4834 if (linfo.li_line_range == 0)
4836 warn (_("Line range of 0 is invalid, using 1 instead\n"));
4837 linfo.li_line_range = 1;
4839 reset_state_machine (linfo.li_default_is_stmt);
4841 /* Save a pointer to the contents of the Opcodes table. */
4842 standard_opcodes = hdrptr;
4844 /* Traverse the Directory table just to count entries. */
4845 data = standard_opcodes + linfo.li_opcode_base - 1;
4846 /* PR 20440 */
4847 if (data >= end)
4849 warn (_("opcode base of %d extends beyond end of section\n"),
4850 linfo.li_opcode_base);
4851 return 0;
4854 if (linfo.li_version >= 5)
4856 unsigned char *format_start, format_count, *format;
4857 dwarf_vma formati, entryi;
4859 load_debug_section_with_follow (line_str, fileptr);
4861 /* Skip directories format. */
4862 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4863 if (do_checks && format_count > 1)
4864 warn (_("Unexpectedly large number of columns in the directory name table (%u)\n"),
4865 format_count);
4866 format_start = data;
4867 for (formati = 0; formati < format_count; formati++)
4869 SKIP_ULEB (data, end);
4870 SKIP_ULEB (data, end);
4873 READ_ULEB (n_directories, data, end);
4874 if (data >= end)
4876 warn (_("Corrupt directories list\n"));
4877 break;
4880 if (n_directories == 0)
4881 directory_table = NULL;
4882 else
4883 directory_table = (unsigned char **)
4884 xmalloc (n_directories * sizeof (unsigned char *));
4886 for (entryi = 0; entryi < n_directories; entryi++)
4888 unsigned char **pathp = &directory_table[entryi];
4890 format = format_start;
4891 for (formati = 0; formati < format_count; formati++)
4893 dwarf_vma content_type, form;
4894 dwarf_vma uvalue;
4896 READ_ULEB (content_type, format, end);
4897 READ_ULEB (form, format, end);
4898 if (data >= end)
4900 warn (_("Corrupt directories list\n"));
4901 break;
4903 switch (content_type)
4905 case DW_LNCT_path:
4906 switch (form)
4908 case DW_FORM_string:
4909 *pathp = data;
4910 break;
4911 case DW_FORM_line_strp:
4912 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
4913 end);
4914 /* Remove const by the cast. */
4915 *pathp = (unsigned char *)
4916 fetch_indirect_line_string (uvalue);
4917 break;
4919 break;
4921 data = read_and_display_attr_value (0, form, 0, start,
4922 data, end, 0, 0,
4923 linfo.li_offset_size,
4924 linfo.li_version,
4925 NULL, 1, section,
4926 NULL, '\t', -1);
4928 if (data >= end)
4930 warn (_("Corrupt directories list\n"));
4931 break;
4935 /* Skip files format. */
4936 SAFE_BYTE_GET_AND_INC (format_count, data, 1, end);
4937 if (do_checks && format_count > 5)
4938 warn (_("Unexpectedly large number of columns in the file name table (%u)\n"),
4939 format_count);
4940 format_start = data;
4941 for (formati = 0; formati < format_count; formati++)
4943 SKIP_ULEB (data, end);
4944 SKIP_ULEB (data, end);
4947 READ_ULEB (n_files, data, end);
4948 if (data >= end && n_files > 0)
4950 warn (_("Corrupt file name list\n"));
4951 break;
4954 if (n_files == 0)
4955 file_table = NULL;
4956 else
4957 file_table = (File_Entry *) xcalloc (1, n_files
4958 * sizeof (File_Entry));
4960 for (entryi = 0; entryi < n_files; entryi++)
4962 File_Entry *file = &file_table[entryi];
4964 format = format_start;
4965 for (formati = 0; formati < format_count; formati++)
4967 dwarf_vma content_type, form;
4968 dwarf_vma uvalue;
4969 unsigned char *tmp;
4971 READ_ULEB (content_type, format, end);
4972 READ_ULEB (form, format, end);
4973 if (data >= end)
4975 warn (_("Corrupt file name list\n"));
4976 break;
4978 switch (content_type)
4980 case DW_LNCT_path:
4981 switch (form)
4983 case DW_FORM_string:
4984 file->name = data;
4985 break;
4986 case DW_FORM_line_strp:
4987 SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size,
4988 end);
4989 /* Remove const by the cast. */
4990 file->name = (unsigned char *)
4991 fetch_indirect_line_string (uvalue);
4992 break;
4994 break;
4995 case DW_LNCT_directory_index:
4996 switch (form)
4998 case DW_FORM_data1:
4999 SAFE_BYTE_GET (file->directory_index, data, 1,
5000 end);
5001 break;
5002 case DW_FORM_data2:
5003 SAFE_BYTE_GET (file->directory_index, data, 2,
5004 end);
5005 break;
5006 case DW_FORM_udata:
5007 tmp = data;
5008 READ_ULEB (file->directory_index, tmp, end);
5009 break;
5011 break;
5013 data = read_and_display_attr_value (0, form, 0, start,
5014 data, end, 0, 0,
5015 linfo.li_offset_size,
5016 linfo.li_version,
5017 NULL, 1, section,
5018 NULL, '\t', -1);
5020 if (data >= end)
5022 warn (_("Corrupt file name list\n"));
5023 break;
5027 else
5029 if (*data != 0)
5031 unsigned char *ptr_directory_table = data;
5033 while (data < end && *data != 0)
5035 data += strnlen ((char *) data, end - data);
5036 if (data < end)
5037 data++;
5038 n_directories++;
5041 /* PR 20440 */
5042 if (data >= end)
5044 warn (_("directory table ends unexpectedly\n"));
5045 n_directories = 0;
5046 break;
5049 /* Go through the directory table again to save the directories. */
5050 directory_table = (unsigned char **)
5051 xmalloc (n_directories * sizeof (unsigned char *));
5053 i = 0;
5054 while (*ptr_directory_table != 0)
5056 directory_table[i] = ptr_directory_table;
5057 ptr_directory_table
5058 += strlen ((char *) ptr_directory_table) + 1;
5059 i++;
5062 /* Skip the NUL at the end of the table. */
5063 data++;
5065 /* Traverse the File Name table just to count the entries. */
5066 if (data < end && *data != 0)
5068 unsigned char *ptr_file_name_table = data;
5070 while (data < end && *data != 0)
5072 /* Skip Name, directory index, last modification
5073 time and length of file. */
5074 data += strnlen ((char *) data, end - data);
5075 if (data < end)
5076 data++;
5077 SKIP_ULEB (data, end);
5078 SKIP_ULEB (data, end);
5079 SKIP_ULEB (data, end);
5080 n_files++;
5083 if (data >= end)
5085 warn (_("file table ends unexpectedly\n"));
5086 n_files = 0;
5087 break;
5090 /* Go through the file table again to save the strings. */
5091 file_table = (File_Entry *) xmalloc (n_files * sizeof (File_Entry));
5093 i = 0;
5094 while (*ptr_file_name_table != 0)
5096 file_table[i].name = ptr_file_name_table;
5097 ptr_file_name_table
5098 += strlen ((char *) ptr_file_name_table) + 1;
5100 /* We are not interested in directory, time or size. */
5101 READ_ULEB (file_table[i].directory_index,
5102 ptr_file_name_table, end);
5103 READ_ULEB (file_table[i].modification_date,
5104 ptr_file_name_table, end);
5105 READ_ULEB (file_table[i].length,
5106 ptr_file_name_table, end);
5107 i++;
5109 i = 0;
5112 /* Skip the NUL at the end of the table. */
5113 data++;
5116 /* Print the Compilation Unit's name and a header. */
5117 if (file_table == NULL)
5118 printf (_("CU: No directory table\n"));
5119 else if (directory_table == NULL)
5120 printf (_("CU: %s:\n"), file_table[0].name);
5121 else
5123 unsigned int ix = file_table[0].directory_index;
5124 const char *directory;
5126 if (ix == 0)
5127 directory = ".";
5128 /* PR 20439 */
5129 else if (n_directories == 0)
5130 directory = _("<unknown>");
5131 else if (ix > n_directories)
5133 warn (_("directory index %u > number of directories %s\n"),
5134 ix, dwarf_vmatoa ("u", n_directories));
5135 directory = _("<corrupt>");
5137 else
5138 directory = (char *) directory_table[ix - 1];
5140 if (do_wide)
5141 printf (_("CU: %s/%s:\n"), directory, file_table[0].name);
5142 else
5143 printf ("%s:\n", file_table[0].name);
5146 if (n_files > 0)
5147 printf (_("File name Line number Starting address View Stmt\n"));
5148 else
5149 printf (_("CU: Empty file name table\n"));
5150 saved_linfo = linfo;
5153 /* This loop iterates through the Dwarf Line Number Program. */
5154 while (data < end_of_sequence)
5156 unsigned char op_code;
5157 int xop;
5158 int adv;
5159 unsigned long int uladv;
5160 int is_special_opcode = 0;
5162 op_code = *data++;
5163 xop = op_code;
5165 if (op_code >= linfo.li_opcode_base)
5167 op_code -= linfo.li_opcode_base;
5168 uladv = (op_code / linfo.li_line_range);
5169 if (linfo.li_max_ops_per_insn == 1)
5171 uladv *= linfo.li_min_insn_length;
5172 state_machine_regs.address += uladv;
5173 if (uladv)
5174 state_machine_regs.view = 0;
5176 else
5178 unsigned addrdelta
5179 = ((state_machine_regs.op_index + uladv)
5180 / linfo.li_max_ops_per_insn)
5181 * linfo.li_min_insn_length;
5182 state_machine_regs.address
5183 += addrdelta;
5184 state_machine_regs.op_index
5185 = (state_machine_regs.op_index + uladv)
5186 % linfo.li_max_ops_per_insn;
5187 if (addrdelta)
5188 state_machine_regs.view = 0;
5191 adv = (op_code % linfo.li_line_range) + linfo.li_line_base;
5192 state_machine_regs.line += adv;
5193 is_special_opcode = 1;
5194 /* Increment view after printing this row. */
5196 else
5197 switch (op_code)
5199 case DW_LNS_extended_op:
5201 unsigned int ext_op_code_len;
5202 unsigned char ext_op_code;
5203 unsigned char *op_code_end;
5204 unsigned char *op_code_data = data;
5206 READ_ULEB (ext_op_code_len, op_code_data, end_of_sequence);
5207 op_code_end = op_code_data + ext_op_code_len;
5208 if (ext_op_code_len == 0 || op_code_end > end_of_sequence)
5210 warn (_("Badly formed extended line op encountered!\n"));
5211 break;
5213 ext_op_code = *op_code_data++;
5214 xop = ext_op_code;
5215 xop = -xop;
5217 switch (ext_op_code)
5219 case DW_LNE_end_sequence:
5220 /* Reset stuff after printing this row. */
5221 break;
5222 case DW_LNE_set_address:
5223 SAFE_BYTE_GET_AND_INC (state_machine_regs.address,
5224 op_code_data,
5225 op_code_end - op_code_data,
5226 op_code_end);
5227 state_machine_regs.op_index = 0;
5228 state_machine_regs.view = 0;
5229 break;
5230 case DW_LNE_define_file:
5231 file_table = (File_Entry *) xrealloc
5232 (file_table, (n_files + 1) * sizeof (File_Entry));
5234 ++state_machine_regs.last_file_entry;
5235 /* Source file name. */
5236 file_table[n_files].name = op_code_data;
5237 op_code_data += strlen ((char *) op_code_data) + 1;
5238 /* Directory index. */
5239 READ_ULEB (file_table[n_files].directory_index,
5240 op_code_data, op_code_end);
5241 /* Last modification time. */
5242 READ_ULEB (file_table[n_files].modification_date,
5243 op_code_data, op_code_end);
5244 /* File length. */
5245 READ_ULEB (file_table[n_files].length,
5246 op_code_data, op_code_end);
5247 n_files++;
5248 break;
5250 case DW_LNE_set_discriminator:
5251 case DW_LNE_HP_set_sequence:
5252 /* Simply ignored. */
5253 break;
5255 default:
5256 printf (_("UNKNOWN (%u): length %ld\n"),
5257 ext_op_code, (long int) (op_code_data - data));
5258 break;
5260 data = op_code_end;
5261 break;
5263 case DW_LNS_copy:
5264 /* Increment view after printing this row. */
5265 break;
5267 case DW_LNS_advance_pc:
5268 READ_ULEB (uladv, data, end);
5269 if (linfo.li_max_ops_per_insn == 1)
5271 uladv *= linfo.li_min_insn_length;
5272 state_machine_regs.address += uladv;
5273 if (uladv)
5274 state_machine_regs.view = 0;
5276 else
5278 unsigned addrdelta
5279 = ((state_machine_regs.op_index + uladv)
5280 / linfo.li_max_ops_per_insn)
5281 * linfo.li_min_insn_length;
5282 state_machine_regs.address
5283 += addrdelta;
5284 state_machine_regs.op_index
5285 = (state_machine_regs.op_index + uladv)
5286 % linfo.li_max_ops_per_insn;
5287 if (addrdelta)
5288 state_machine_regs.view = 0;
5290 break;
5292 case DW_LNS_advance_line:
5293 READ_SLEB (adv, data, end);
5294 state_machine_regs.line += adv;
5295 break;
5297 case DW_LNS_set_file:
5298 READ_ULEB (uladv, data, end);
5299 state_machine_regs.file = uladv;
5302 unsigned file = state_machine_regs.file;
5303 unsigned dir;
5305 if (linfo.li_version < 5)
5306 --file;
5307 if (file_table == NULL || n_files == 0)
5308 printf (_("\n [Use file table entry %d]\n"), file);
5309 /* PR 20439 */
5310 else if (file >= n_files)
5312 warn (_("file index %u > number of files %u\n"), file, n_files);
5313 printf (_("\n <over large file table index %u>"), file);
5315 else if ((dir = file_table[file].directory_index) == 0)
5316 /* If directory index is 0, that means current directory. */
5317 printf ("\n./%s:[++]\n", file_table[file].name);
5318 else if (directory_table == NULL || n_directories == 0)
5319 printf (_("\n [Use file %s in directory table entry %d]\n"),
5320 file_table[file].name, dir);
5321 /* PR 20439 */
5322 else if (dir > n_directories)
5324 warn (_("directory index %u > number of directories %s\n"),
5325 dir, dwarf_vmatoa ("u", n_directories));
5326 printf (_("\n <over large directory table entry %u>\n"), dir);
5328 else
5329 printf ("\n%s/%s:\n",
5330 /* The directory index starts counting at 1. */
5331 directory_table[dir - 1], file_table[file].name);
5333 break;
5335 case DW_LNS_set_column:
5336 READ_ULEB (uladv, data, end);
5337 state_machine_regs.column = uladv;
5338 break;
5340 case DW_LNS_negate_stmt:
5341 adv = state_machine_regs.is_stmt;
5342 adv = ! adv;
5343 state_machine_regs.is_stmt = adv;
5344 break;
5346 case DW_LNS_set_basic_block:
5347 state_machine_regs.basic_block = 1;
5348 break;
5350 case DW_LNS_const_add_pc:
5351 uladv = ((255 - linfo.li_opcode_base) / linfo.li_line_range);
5352 if (linfo.li_max_ops_per_insn == 1)
5354 uladv *= linfo.li_min_insn_length;
5355 state_machine_regs.address += uladv;
5356 if (uladv)
5357 state_machine_regs.view = 0;
5359 else
5361 unsigned addrdelta
5362 = ((state_machine_regs.op_index + uladv)
5363 / linfo.li_max_ops_per_insn)
5364 * linfo.li_min_insn_length;
5365 state_machine_regs.address
5366 += addrdelta;
5367 state_machine_regs.op_index
5368 = (state_machine_regs.op_index + uladv)
5369 % linfo.li_max_ops_per_insn;
5370 if (addrdelta)
5371 state_machine_regs.view = 0;
5373 break;
5375 case DW_LNS_fixed_advance_pc:
5376 SAFE_BYTE_GET_AND_INC (uladv, data, 2, end);
5377 state_machine_regs.address += uladv;
5378 state_machine_regs.op_index = 0;
5379 /* Do NOT reset view. */
5380 break;
5382 case DW_LNS_set_prologue_end:
5383 break;
5385 case DW_LNS_set_epilogue_begin:
5386 break;
5388 case DW_LNS_set_isa:
5389 READ_ULEB (uladv, data, end);
5390 printf (_(" Set ISA to %lu\n"), uladv);
5391 break;
5393 default:
5394 printf (_(" Unknown opcode %d with operands: "), op_code);
5396 if (standard_opcodes != NULL)
5397 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
5399 dwarf_vma val;
5401 READ_ULEB (val, data, end);
5402 printf ("0x%s%s", dwarf_vmatoa ("x", val),
5403 i == 1 ? "" : ", ");
5405 putchar ('\n');
5406 break;
5409 /* Only Special opcodes, DW_LNS_copy and DW_LNE_end_sequence adds a row
5410 to the DWARF address/line matrix. */
5411 if ((is_special_opcode) || (xop == -DW_LNE_end_sequence)
5412 || (xop == DW_LNS_copy))
5414 const unsigned int MAX_FILENAME_LENGTH = 35;
5415 char *fileName;
5416 char *newFileName = NULL;
5417 size_t fileNameLength;
5419 if (file_table)
5421 unsigned indx = state_machine_regs.file;
5423 if (linfo.li_version < 5)
5424 --indx;
5425 /* PR 20439 */
5426 if (indx >= n_files)
5428 warn (_("corrupt file index %u encountered\n"), indx);
5429 fileName = _("<corrupt>");
5431 else
5432 fileName = (char *) file_table[indx].name;
5434 else
5435 fileName = _("<unknown>");
5437 fileNameLength = strlen (fileName);
5438 newFileName = fileName;
5439 if (fileNameLength > MAX_FILENAME_LENGTH && !do_wide)
5441 newFileName = (char *) xmalloc (MAX_FILENAME_LENGTH + 1);
5442 /* Truncate file name */
5443 memcpy (newFileName,
5444 fileName + fileNameLength - MAX_FILENAME_LENGTH,
5445 MAX_FILENAME_LENGTH);
5446 newFileName[MAX_FILENAME_LENGTH] = 0;
5449 /* A row with end_seq set to true has a meaningful address, but
5450 the other information in the same row is not significant.
5451 In such a row, print line as "-", and don't print
5452 view/is_stmt. */
5453 if (!do_wide || fileNameLength <= MAX_FILENAME_LENGTH)
5455 if (linfo.li_max_ops_per_insn == 1)
5457 if (xop == -DW_LNE_end_sequence)
5458 printf ("%-35s %11s %#18" DWARF_VMA_FMT "x",
5459 newFileName, "-",
5460 state_machine_regs.address);
5461 else
5462 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x",
5463 newFileName, state_machine_regs.line,
5464 state_machine_regs.address);
5466 else
5468 if (xop == -DW_LNE_end_sequence)
5469 printf ("%-35s %11s %#18" DWARF_VMA_FMT "x[%d]",
5470 newFileName, "-",
5471 state_machine_regs.address,
5472 state_machine_regs.op_index);
5473 else
5474 printf ("%-35s %11d %#18" DWARF_VMA_FMT "x[%d]",
5475 newFileName, state_machine_regs.line,
5476 state_machine_regs.address,
5477 state_machine_regs.op_index);
5480 else
5482 if (linfo.li_max_ops_per_insn == 1)
5484 if (xop == -DW_LNE_end_sequence)
5485 printf ("%s %11s %#18" DWARF_VMA_FMT "x",
5486 newFileName, "-",
5487 state_machine_regs.address);
5488 else
5489 printf ("%s %11d %#18" DWARF_VMA_FMT "x",
5490 newFileName, state_machine_regs.line,
5491 state_machine_regs.address);
5493 else
5495 if (xop == -DW_LNE_end_sequence)
5496 printf ("%s %11s %#18" DWARF_VMA_FMT "x[%d]",
5497 newFileName, "-",
5498 state_machine_regs.address,
5499 state_machine_regs.op_index);
5500 else
5501 printf ("%s %11d %#18" DWARF_VMA_FMT "x[%d]",
5502 newFileName, state_machine_regs.line,
5503 state_machine_regs.address,
5504 state_machine_regs.op_index);
5508 if (xop != -DW_LNE_end_sequence)
5510 if (state_machine_regs.view)
5511 printf (" %6u", state_machine_regs.view);
5512 else
5513 printf (" ");
5515 if (state_machine_regs.is_stmt)
5516 printf (" x");
5519 putchar ('\n');
5520 state_machine_regs.view++;
5522 if (xop == -DW_LNE_end_sequence)
5524 reset_state_machine (linfo.li_default_is_stmt);
5525 putchar ('\n');
5528 if (newFileName != fileName)
5529 free (newFileName);
5533 if (file_table)
5535 free (file_table);
5536 file_table = NULL;
5537 n_files = 0;
5540 if (directory_table)
5542 free (directory_table);
5543 directory_table = NULL;
5544 n_directories = 0;
5547 putchar ('\n');
5550 return 1;
5553 static int
5554 display_debug_lines (struct dwarf_section *section, void *file)
5556 unsigned char *data = section->start;
5557 unsigned char *end = data + section->size;
5558 int retValRaw = 1;
5559 int retValDecoded = 1;
5561 if (do_debug_lines == 0)
5562 do_debug_lines |= FLAG_DEBUG_LINES_RAW;
5564 if (do_debug_lines & FLAG_DEBUG_LINES_RAW)
5565 retValRaw = display_debug_lines_raw (section, data, end, file);
5567 if (do_debug_lines & FLAG_DEBUG_LINES_DECODED)
5568 retValDecoded = display_debug_lines_decoded (section, data, data, end, file);
5570 if (!retValRaw || !retValDecoded)
5571 return 0;
5573 return 1;
5576 static debug_info *
5577 find_debug_info_for_offset (dwarf_vma offset)
5579 unsigned int i;
5581 if (num_debug_info_entries == DEBUG_INFO_UNAVAILABLE)
5582 return NULL;
5584 for (i = 0; i < num_debug_info_entries; i++)
5585 if (debug_information[i].cu_offset == offset)
5586 return debug_information + i;
5588 return NULL;
5591 static const char *
5592 get_gdb_index_symbol_kind_name (gdb_index_symbol_kind kind)
5594 /* See gdb/gdb-index.h. */
5595 static const char * const kinds[] =
5597 N_ ("no info"),
5598 N_ ("type"),
5599 N_ ("variable"),
5600 N_ ("function"),
5601 N_ ("other"),
5602 N_ ("unused5"),
5603 N_ ("unused6"),
5604 N_ ("unused7")
5607 return _ (kinds[kind]);
5610 static int
5611 display_debug_pubnames_worker (struct dwarf_section *section,
5612 void *file ATTRIBUTE_UNUSED,
5613 int is_gnu)
5615 DWARF2_Internal_PubNames names;
5616 unsigned char *start = section->start;
5617 unsigned char *end = start + section->size;
5619 /* It does not matter if this load fails,
5620 we test for that later on. */
5621 load_debug_info (file);
5623 introduce (section, false);
5625 while (start < end)
5627 unsigned char *data;
5628 unsigned long sec_off = start - section->start;
5629 unsigned int offset_size;
5631 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 4, end);
5632 if (names.pn_length == 0xffffffff)
5634 SAFE_BYTE_GET_AND_INC (names.pn_length, start, 8, end);
5635 offset_size = 8;
5637 else
5638 offset_size = 4;
5640 if (names.pn_length > (size_t) (end - start))
5642 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
5643 section->name,
5644 sec_off,
5645 dwarf_vmatoa ("x", names.pn_length));
5646 break;
5649 data = start;
5650 start += names.pn_length;
5652 SAFE_BYTE_GET_AND_INC (names.pn_version, data, 2, start);
5653 SAFE_BYTE_GET_AND_INC (names.pn_offset, data, offset_size, start);
5655 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
5656 && num_debug_info_entries > 0
5657 && find_debug_info_for_offset (names.pn_offset) == NULL)
5658 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
5659 (unsigned long) names.pn_offset, section->name);
5661 SAFE_BYTE_GET_AND_INC (names.pn_size, data, offset_size, start);
5663 printf (_(" Length: %ld\n"),
5664 (long) names.pn_length);
5665 printf (_(" Version: %d\n"),
5666 names.pn_version);
5667 printf (_(" Offset into .debug_info section: 0x%lx\n"),
5668 (unsigned long) names.pn_offset);
5669 printf (_(" Size of area in .debug_info section: %ld\n"),
5670 (long) names.pn_size);
5672 if (names.pn_version != 2 && names.pn_version != 3)
5674 static int warned = 0;
5676 if (! warned)
5678 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
5679 warned = 1;
5682 continue;
5685 if (is_gnu)
5686 printf (_("\n Offset Kind Name\n"));
5687 else
5688 printf (_("\n Offset\tName\n"));
5690 while (1)
5692 bfd_size_type maxprint;
5693 dwarf_vma offset;
5695 SAFE_BYTE_GET_AND_INC (offset, data, offset_size, start);
5697 if (offset == 0)
5698 break;
5700 if (data >= start)
5701 break;
5702 maxprint = (start - data) - 1;
5704 if (is_gnu)
5706 unsigned int kind_data;
5707 gdb_index_symbol_kind kind;
5708 const char *kind_name;
5709 int is_static;
5711 SAFE_BYTE_GET_AND_INC (kind_data, data, 1, start);
5712 maxprint --;
5713 /* GCC computes the kind as the upper byte in the CU index
5714 word, and then right shifts it by the CU index size.
5715 Left shift KIND to where the gdb-index.h accessor macros
5716 can use it. */
5717 kind_data <<= GDB_INDEX_CU_BITSIZE;
5718 kind = GDB_INDEX_SYMBOL_KIND_VALUE (kind_data);
5719 kind_name = get_gdb_index_symbol_kind_name (kind);
5720 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (kind_data);
5721 printf (" %-6lx %s,%-10s %.*s\n",
5722 (unsigned long) offset, is_static ? _("s") : _("g"),
5723 kind_name, (int) maxprint, data);
5725 else
5726 printf (" %-6lx\t%.*s\n",
5727 (unsigned long) offset, (int) maxprint, data);
5729 data += strnlen ((char *) data, maxprint);
5730 if (data < start)
5731 data++;
5732 if (data >= start)
5733 break;
5737 printf ("\n");
5738 return 1;
5741 static int
5742 display_debug_pubnames (struct dwarf_section *section, void *file)
5744 return display_debug_pubnames_worker (section, file, 0);
5747 static int
5748 display_debug_gnu_pubnames (struct dwarf_section *section, void *file)
5750 return display_debug_pubnames_worker (section, file, 1);
5753 static int
5754 display_debug_macinfo (struct dwarf_section *section,
5755 void *file ATTRIBUTE_UNUSED)
5757 unsigned char *start = section->start;
5758 unsigned char *end = start + section->size;
5759 unsigned char *curr = start;
5760 enum dwarf_macinfo_record_type op;
5762 introduce (section, false);
5764 while (curr < end)
5766 unsigned int lineno;
5767 const unsigned char *string;
5769 op = (enum dwarf_macinfo_record_type) *curr;
5770 curr++;
5772 switch (op)
5774 case DW_MACINFO_start_file:
5776 unsigned int filenum;
5778 READ_ULEB (lineno, curr, end);
5779 READ_ULEB (filenum, curr, end);
5780 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"),
5781 lineno, filenum);
5783 break;
5785 case DW_MACINFO_end_file:
5786 printf (_(" DW_MACINFO_end_file\n"));
5787 break;
5789 case DW_MACINFO_define:
5790 READ_ULEB (lineno, curr, end);
5791 string = curr;
5792 curr += strnlen ((char *) string, end - string);
5793 printf (_(" DW_MACINFO_define - lineno : %d macro : %*s\n"),
5794 lineno, (int) (curr - string), string);
5795 if (curr < end)
5796 curr++;
5797 break;
5799 case DW_MACINFO_undef:
5800 READ_ULEB (lineno, curr, end);
5801 string = curr;
5802 curr += strnlen ((char *) string, end - string);
5803 printf (_(" DW_MACINFO_undef - lineno : %d macro : %*s\n"),
5804 lineno, (int) (curr - string), string);
5805 if (curr < end)
5806 curr++;
5807 break;
5809 case DW_MACINFO_vendor_ext:
5811 unsigned int constant;
5813 READ_ULEB (constant, curr, end);
5814 string = curr;
5815 curr += strnlen ((char *) string, end - string);
5816 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %*s\n"),
5817 constant, (int) (curr - string), string);
5818 if (curr < end)
5819 curr++;
5821 break;
5825 return 1;
5828 /* Given LINE_OFFSET into the .debug_line section, attempt to return
5829 filename and dirname corresponding to file name table entry with index
5830 FILEIDX. Return NULL on failure. */
5832 static unsigned char *
5833 get_line_filename_and_dirname (dwarf_vma line_offset,
5834 dwarf_vma fileidx,
5835 unsigned char **dir_name)
5837 struct dwarf_section *section = &debug_displays [line].section;
5838 unsigned char *hdrptr, *dirtable, *file_name;
5839 unsigned int offset_size;
5840 unsigned int version, opcode_base;
5841 dwarf_vma length, diridx;
5842 const unsigned char * end;
5844 *dir_name = NULL;
5845 if (section->start == NULL
5846 || line_offset >= section->size
5847 || fileidx == 0)
5848 return NULL;
5850 hdrptr = section->start + line_offset;
5851 end = section->start + section->size;
5853 SAFE_BYTE_GET_AND_INC (length, hdrptr, 4, end);
5854 if (length == 0xffffffff)
5856 /* This section is 64-bit DWARF 3. */
5857 SAFE_BYTE_GET_AND_INC (length, hdrptr, 8, end);
5858 offset_size = 8;
5860 else
5861 offset_size = 4;
5863 if (length > (size_t) (end - hdrptr)
5864 || length < 2 + offset_size + 1 + 3 + 1)
5865 return NULL;
5866 end = hdrptr + length;
5868 SAFE_BYTE_GET_AND_INC (version, hdrptr, 2, end);
5869 if (version != 2 && version != 3 && version != 4)
5870 return NULL;
5871 hdrptr += offset_size + 1;/* Skip prologue_length and min_insn_length. */
5872 if (version >= 4)
5873 hdrptr++; /* Skip max_ops_per_insn. */
5874 hdrptr += 3; /* Skip default_is_stmt, line_base, line_range. */
5876 SAFE_BYTE_GET_AND_INC (opcode_base, hdrptr, 1, end);
5877 if (opcode_base == 0
5878 || opcode_base - 1 >= (size_t) (end - hdrptr))
5879 return NULL;
5881 hdrptr += opcode_base - 1;
5883 dirtable = hdrptr;
5884 /* Skip over dirname table. */
5885 while (*hdrptr != '\0')
5887 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5888 if (hdrptr < end)
5889 hdrptr++;
5890 if (hdrptr >= end)
5891 return NULL;
5893 hdrptr++; /* Skip the NUL at the end of the table. */
5895 /* Now skip over preceding filename table entries. */
5896 for (; hdrptr < end && *hdrptr != '\0' && fileidx > 1; fileidx--)
5898 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5899 if (hdrptr < end)
5900 hdrptr++;
5901 SKIP_ULEB (hdrptr, end);
5902 SKIP_ULEB (hdrptr, end);
5903 SKIP_ULEB (hdrptr, end);
5905 if (hdrptr >= end || *hdrptr == '\0')
5906 return NULL;
5908 file_name = hdrptr;
5909 hdrptr += strnlen ((char *) hdrptr, end - hdrptr);
5910 if (hdrptr < end)
5911 hdrptr++;
5912 if (hdrptr >= end)
5913 return NULL;
5914 READ_ULEB (diridx, hdrptr, end);
5915 if (diridx == 0)
5916 return file_name;
5917 for (; dirtable < end && *dirtable != '\0' && diridx > 1; diridx--)
5919 dirtable += strnlen ((char *) dirtable, end - dirtable);
5920 if (dirtable < end)
5921 dirtable++;
5923 if (dirtable >= end || *dirtable == '\0')
5924 return NULL;
5925 *dir_name = dirtable;
5926 return file_name;
5929 static int
5930 display_debug_macro (struct dwarf_section *section,
5931 void *file)
5933 unsigned char *start = section->start;
5934 unsigned char *end = start + section->size;
5935 unsigned char *curr = start;
5936 unsigned char *extended_op_buf[256];
5937 bool is_dwo = false;
5938 const char *suffix = strrchr (section->name, '.');
5940 if (suffix && strcmp (suffix, ".dwo") == 0)
5941 is_dwo = true;
5943 load_debug_section_with_follow (str, file);
5944 load_debug_section_with_follow (line, file);
5945 load_debug_section_with_follow (str_index, file);
5947 introduce (section, false);
5949 while (curr < end)
5951 unsigned int lineno, version, flags;
5952 unsigned int offset_size;
5953 const unsigned char *string;
5954 dwarf_vma line_offset = 0, sec_offset = curr - start, offset;
5955 unsigned char **extended_ops = NULL;
5957 SAFE_BYTE_GET_AND_INC (version, curr, 2, end);
5958 if (version != 4 && version != 5)
5960 error (_("Expected to find a version number of 4 or 5 in section %s but found %d instead\n"),
5961 section->name, version);
5962 return 0;
5965 SAFE_BYTE_GET_AND_INC (flags, curr, 1, end);
5966 offset_size = (flags & 1) ? 8 : 4;
5967 printf (_(" Offset: 0x%lx\n"),
5968 (unsigned long) sec_offset);
5969 printf (_(" Version: %d\n"), version);
5970 printf (_(" Offset size: %d\n"), offset_size);
5971 if (flags & 2)
5973 SAFE_BYTE_GET_AND_INC (line_offset, curr, offset_size, end);
5974 printf (_(" Offset into .debug_line: 0x%lx\n"),
5975 (unsigned long) line_offset);
5977 if (flags & 4)
5979 unsigned int i, count, op;
5980 dwarf_vma nargs, n;
5982 SAFE_BYTE_GET_AND_INC (count, curr, 1, end);
5984 memset (extended_op_buf, 0, sizeof (extended_op_buf));
5985 extended_ops = extended_op_buf;
5986 if (count)
5988 printf (_(" Extension opcode arguments:\n"));
5989 for (i = 0; i < count; i++)
5991 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
5992 extended_ops[op] = curr;
5993 READ_ULEB (nargs, curr, end);
5994 if (nargs == 0)
5995 printf (_(" DW_MACRO_%02x has no arguments\n"), op);
5996 else
5998 printf (_(" DW_MACRO_%02x arguments: "), op);
5999 for (n = 0; n < nargs; n++)
6001 unsigned int form;
6003 SAFE_BYTE_GET_AND_INC (form, curr, 1, end);
6004 printf ("%s%s", get_FORM_name (form),
6005 n == nargs - 1 ? "\n" : ", ");
6006 switch (form)
6008 case DW_FORM_data1:
6009 case DW_FORM_data2:
6010 case DW_FORM_data4:
6011 case DW_FORM_data8:
6012 case DW_FORM_sdata:
6013 case DW_FORM_udata:
6014 case DW_FORM_block:
6015 case DW_FORM_block1:
6016 case DW_FORM_block2:
6017 case DW_FORM_block4:
6018 case DW_FORM_flag:
6019 case DW_FORM_string:
6020 case DW_FORM_strp:
6021 case DW_FORM_sec_offset:
6022 break;
6023 default:
6024 error (_("Invalid extension opcode form %s\n"),
6025 get_FORM_name (form));
6026 return 0;
6033 printf ("\n");
6035 while (1)
6037 unsigned int op;
6039 if (curr >= end)
6041 error (_(".debug_macro section not zero terminated\n"));
6042 return 0;
6045 SAFE_BYTE_GET_AND_INC (op, curr, 1, end);
6046 if (op == 0)
6047 break;
6049 switch (op)
6051 case DW_MACRO_define:
6052 READ_ULEB (lineno, curr, end);
6053 string = curr;
6054 curr += strnlen ((char *) string, end - string);
6055 printf (_(" DW_MACRO_define - lineno : %d macro : %*s\n"),
6056 lineno, (int) (curr - string), string);
6057 if (curr < end)
6058 curr++;
6059 break;
6061 case DW_MACRO_undef:
6062 READ_ULEB (lineno, curr, end);
6063 string = curr;
6064 curr += strnlen ((char *) string, end - string);
6065 printf (_(" DW_MACRO_undef - lineno : %d macro : %*s\n"),
6066 lineno, (int) (curr - string), string);
6067 if (curr < end)
6068 curr++;
6069 break;
6071 case DW_MACRO_start_file:
6073 unsigned int filenum;
6074 unsigned char *file_name = NULL, *dir_name = NULL;
6076 READ_ULEB (lineno, curr, end);
6077 READ_ULEB (filenum, curr, end);
6079 if ((flags & 2) == 0)
6080 error (_("DW_MACRO_start_file used, but no .debug_line offset provided.\n"));
6081 else
6082 file_name
6083 = get_line_filename_and_dirname (line_offset, filenum,
6084 &dir_name);
6085 if (file_name == NULL)
6086 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d\n"),
6087 lineno, filenum);
6088 else
6089 printf (_(" DW_MACRO_start_file - lineno: %d filenum: %d filename: %s%s%s\n"),
6090 lineno, filenum,
6091 dir_name != NULL ? (const char *) dir_name : "",
6092 dir_name != NULL ? "/" : "", file_name);
6094 break;
6096 case DW_MACRO_end_file:
6097 printf (_(" DW_MACRO_end_file\n"));
6098 break;
6100 case DW_MACRO_define_strp:
6101 READ_ULEB (lineno, curr, end);
6102 if (version == 4 && is_dwo)
6103 READ_ULEB (offset, curr, end);
6104 else
6105 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6106 string = fetch_indirect_string (offset);
6107 printf (_(" DW_MACRO_define_strp - lineno : %d macro : %s\n"),
6108 lineno, string);
6109 break;
6111 case DW_MACRO_undef_strp:
6112 READ_ULEB (lineno, curr, end);
6113 if (version == 4 && is_dwo)
6114 READ_ULEB (offset, curr, end);
6115 else
6116 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6117 string = fetch_indirect_string (offset);
6118 printf (_(" DW_MACRO_undef_strp - lineno : %d macro : %s\n"),
6119 lineno, string);
6120 break;
6122 case DW_MACRO_import:
6123 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6124 printf (_(" DW_MACRO_import - offset : 0x%lx\n"),
6125 (unsigned long) offset);
6126 break;
6128 case DW_MACRO_define_sup:
6129 READ_ULEB (lineno, curr, end);
6130 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6131 printf (_(" DW_MACRO_define_sup - lineno : %d macro offset : 0x%lx\n"),
6132 lineno, (unsigned long) offset);
6133 break;
6135 case DW_MACRO_undef_sup:
6136 READ_ULEB (lineno, curr, end);
6137 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6138 printf (_(" DW_MACRO_undef_sup - lineno : %d macro offset : 0x%lx\n"),
6139 lineno, (unsigned long) offset);
6140 break;
6142 case DW_MACRO_import_sup:
6143 SAFE_BYTE_GET_AND_INC (offset, curr, offset_size, end);
6144 printf (_(" DW_MACRO_import_sup - offset : 0x%lx\n"),
6145 (unsigned long) offset);
6146 break;
6148 case DW_MACRO_define_strx:
6149 case DW_MACRO_undef_strx:
6150 READ_ULEB (lineno, curr, end);
6151 READ_ULEB (offset, curr, end);
6152 string = (const unsigned char *)
6153 fetch_indexed_string (offset, NULL, offset_size, false);
6154 if (op == DW_MACRO_define_strx)
6155 printf (" DW_MACRO_define_strx ");
6156 else
6157 printf (" DW_MACRO_undef_strx ");
6158 if (do_wide)
6159 printf (_("(with offset %s) "), dwarf_vmatoa ("x", offset));
6160 printf (_("lineno : %d macro : %s\n"),
6161 lineno, string);
6162 break;
6164 default:
6165 if (op >= DW_MACRO_lo_user && op <= DW_MACRO_hi_user)
6167 printf (_(" <Target Specific macro op: %#x - UNHANDLED"), op);
6168 break;
6171 if (extended_ops == NULL || extended_ops[op] == NULL)
6173 error (_(" Unknown macro opcode %02x seen\n"), op);
6174 return 0;
6176 else
6178 /* Skip over unhandled opcodes. */
6179 dwarf_vma nargs, n;
6180 unsigned char *desc = extended_ops[op];
6181 READ_ULEB (nargs, desc, end);
6182 if (nargs == 0)
6184 printf (_(" DW_MACRO_%02x\n"), op);
6185 break;
6187 printf (_(" DW_MACRO_%02x -"), op);
6188 for (n = 0; n < nargs; n++)
6190 int val;
6192 /* DW_FORM_implicit_const is not expected here. */
6193 SAFE_BYTE_GET_AND_INC (val, desc, 1, end);
6194 curr
6195 = read_and_display_attr_value (0, val, 0,
6196 start, curr, end, 0, 0, offset_size,
6197 version, NULL, 0, NULL,
6198 NULL, ' ', -1);
6199 if (n != nargs - 1)
6200 printf (",");
6202 printf ("\n");
6204 break;
6208 printf ("\n");
6211 return 1;
6214 static int
6215 display_debug_abbrev (struct dwarf_section *section,
6216 void *file ATTRIBUTE_UNUSED)
6218 abbrev_entry *entry;
6219 unsigned char *start = section->start;
6221 introduce (section, false);
6225 abbrev_list * list;
6226 dwarf_vma offset;
6228 offset = start - section->start;
6229 list = find_abbrev_list_by_abbrev_offset (0, offset);
6230 if (list == NULL)
6232 list = new_abbrev_list (0, offset);
6233 start = process_abbrev_set (section, 0, section->size, offset, list);
6234 list->start_of_next_abbrevs = start;
6236 else
6237 start = list->start_of_next_abbrevs;
6239 if (list->first_abbrev == NULL)
6240 continue;
6242 printf (_(" Number TAG (0x%lx)\n"), (long) offset);
6244 for (entry = list->first_abbrev; entry; entry = entry->next)
6246 abbrev_attr *attr;
6248 printf (" %ld %s [%s]\n",
6249 entry->number,
6250 get_TAG_name (entry->tag),
6251 entry->children ? _("has children") : _("no children"));
6253 for (attr = entry->first_attr; attr; attr = attr->next)
6255 printf (" %-18s %s",
6256 get_AT_name (attr->attribute),
6257 get_FORM_name (attr->form));
6258 if (attr->form == DW_FORM_implicit_const)
6259 printf (": %s", dwarf_vmatoa ("d", attr->implicit_const));
6260 putchar ('\n');
6264 while (start);
6266 printf ("\n");
6268 return 1;
6271 /* Return true when ADDR is the maximum address, when addresses are
6272 POINTER_SIZE bytes long. */
6274 static bool
6275 is_max_address (dwarf_vma addr, unsigned int pointer_size)
6277 dwarf_vma mask = ~(~(dwarf_vma) 1 << (pointer_size * 8 - 1));
6278 return ((addr & mask) == mask);
6281 /* Display a view pair list starting at *VSTART_PTR and ending at
6282 VLISTEND within SECTION. */
6284 static void
6285 display_view_pair_list (struct dwarf_section *section,
6286 unsigned char **vstart_ptr,
6287 unsigned int debug_info_entry,
6288 unsigned char *vlistend)
6290 unsigned char *vstart = *vstart_ptr;
6291 unsigned char *section_end = section->start + section->size;
6292 unsigned int pointer_size = debug_information [debug_info_entry].pointer_size;
6294 if (vlistend < section_end)
6295 section_end = vlistend;
6297 putchar ('\n');
6299 while (vstart < section_end)
6301 dwarf_vma off = vstart - section->start;
6302 dwarf_vma vbegin, vend;
6304 READ_ULEB (vbegin, vstart, section_end);
6305 if (vstart == section_end)
6306 break;
6308 READ_ULEB (vend, vstart, section_end);
6309 printf (" %8.8lx ", (unsigned long) off);
6311 print_dwarf_view (vbegin, pointer_size, 1);
6312 print_dwarf_view (vend, pointer_size, 1);
6313 printf (_("location view pair\n"));
6316 putchar ('\n');
6317 *vstart_ptr = vstart;
6320 /* Display a location list from a normal (ie, non-dwo) .debug_loc section. */
6322 static void
6323 display_loc_list (struct dwarf_section *section,
6324 unsigned char **start_ptr,
6325 unsigned int debug_info_entry,
6326 dwarf_vma offset,
6327 dwarf_vma base_address,
6328 unsigned char **vstart_ptr,
6329 int has_frame_base)
6331 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6332 unsigned char *section_end = section->start + section->size;
6333 dwarf_vma cu_offset;
6334 unsigned int pointer_size;
6335 unsigned int offset_size;
6336 int dwarf_version;
6337 dwarf_vma begin;
6338 dwarf_vma end;
6339 unsigned short length;
6340 int need_frame_base;
6342 if (debug_info_entry >= num_debug_info_entries)
6344 warn (_("No debug information available for loc lists of entry: %u\n"),
6345 debug_info_entry);
6346 return;
6349 cu_offset = debug_information [debug_info_entry].cu_offset;
6350 pointer_size = debug_information [debug_info_entry].pointer_size;
6351 offset_size = debug_information [debug_info_entry].offset_size;
6352 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6354 if (pointer_size < 2 || pointer_size > 8)
6356 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6357 pointer_size, debug_info_entry);
6358 return;
6361 while (1)
6363 dwarf_vma off = offset + (start - *start_ptr);
6364 dwarf_vma vbegin = vm1, vend = vm1;
6366 if (2 * pointer_size > (size_t) (section_end - start))
6368 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6369 (unsigned long) offset);
6370 break;
6373 printf (" ");
6374 print_dwarf_vma (off, 4);
6376 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6377 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6379 if (begin == 0 && end == 0)
6381 /* PR 18374: In a object file we can have a location list that
6382 starts with a begin and end of 0 because there are relocations
6383 that need to be applied to the addresses. Actually applying
6384 the relocations now does not help as they will probably resolve
6385 to 0, since the object file has not been fully linked. Real
6386 end of list markers will not have any relocations against them. */
6387 if (! reloc_at (section, off)
6388 && ! reloc_at (section, off + pointer_size))
6390 printf (_("<End of list>\n"));
6391 break;
6395 /* Check base address specifiers. */
6396 if (is_max_address (begin, pointer_size)
6397 && !is_max_address (end, pointer_size))
6399 base_address = end;
6400 print_dwarf_vma (begin, pointer_size);
6401 print_dwarf_vma (end, pointer_size);
6402 printf (_("(base address)\n"));
6403 continue;
6406 if (vstart)
6408 off = offset + (vstart - *start_ptr);
6410 READ_ULEB (vbegin, vstart, section_end);
6411 print_dwarf_view (vbegin, pointer_size, 1);
6413 READ_ULEB (vend, vstart, section_end);
6414 print_dwarf_view (vend, pointer_size, 1);
6416 printf (_("views at %8.8lx for:\n %*s "),
6417 (unsigned long) off, 8, "");
6420 if (2 > (size_t) (section_end - start))
6422 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6423 (unsigned long) offset);
6424 break;
6427 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6429 if (length > (size_t) (section_end - start))
6431 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6432 (unsigned long) offset);
6433 break;
6436 print_dwarf_vma (begin + base_address, pointer_size);
6437 print_dwarf_vma (end + base_address, pointer_size);
6439 putchar ('(');
6440 need_frame_base = decode_location_expression (start,
6441 pointer_size,
6442 offset_size,
6443 dwarf_version,
6444 length,
6445 cu_offset, section);
6446 putchar (')');
6448 if (need_frame_base && !has_frame_base)
6449 printf (_(" [without DW_AT_frame_base]"));
6451 if (begin == end && vbegin == vend)
6452 fputs (_(" (start == end)"), stdout);
6453 else if (begin > end || (begin == end && vbegin > vend))
6454 fputs (_(" (start > end)"), stdout);
6456 putchar ('\n');
6458 start += length;
6461 *start_ptr = start;
6462 *vstart_ptr = vstart;
6465 /* Display a location list from a normal (ie, non-dwo) .debug_loclists section. */
6467 static void
6468 display_loclists_list (struct dwarf_section *section,
6469 unsigned char **start_ptr,
6470 unsigned int debug_info_entry,
6471 dwarf_vma offset,
6472 dwarf_vma base_address,
6473 unsigned char **vstart_ptr,
6474 int has_frame_base)
6476 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6477 unsigned char *section_end = section->start + section->size;
6478 dwarf_vma cu_offset;
6479 unsigned int pointer_size;
6480 unsigned int offset_size;
6481 int dwarf_version;
6483 /* Initialize it due to a false compiler warning. */
6484 dwarf_vma begin = -1, vbegin = -1;
6485 dwarf_vma end = -1, vend = -1;
6486 dwarf_vma length;
6487 int need_frame_base;
6489 if (debug_info_entry >= num_debug_info_entries)
6491 warn (_("No debug information available for "
6492 "loclists lists of entry: %u\n"),
6493 debug_info_entry);
6494 return;
6497 cu_offset = debug_information [debug_info_entry].cu_offset;
6498 pointer_size = debug_information [debug_info_entry].pointer_size;
6499 offset_size = debug_information [debug_info_entry].offset_size;
6500 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6502 if (pointer_size < 2 || pointer_size > 8)
6504 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6505 pointer_size, debug_info_entry);
6506 return;
6509 while (1)
6511 dwarf_vma off = offset + (start - *start_ptr);
6512 enum dwarf_location_list_entry_type llet;
6514 if (start + 1 > section_end)
6516 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6517 (unsigned long) offset);
6518 break;
6521 printf (" ");
6522 print_dwarf_vma (off, 4);
6524 SAFE_BYTE_GET_AND_INC (llet, start, 1, section_end);
6526 if (vstart && (llet == DW_LLE_offset_pair
6527 || llet == DW_LLE_start_end
6528 || llet == DW_LLE_start_length))
6530 off = offset + (vstart - *start_ptr);
6532 READ_ULEB (vbegin, vstart, section_end);
6533 print_dwarf_view (vbegin, pointer_size, 1);
6535 READ_ULEB (vend, vstart, section_end);
6536 print_dwarf_view (vend, pointer_size, 1);
6538 printf (_("views at %8.8lx for:\n %*s "),
6539 (unsigned long) off, 8, "");
6542 switch (llet)
6544 case DW_LLE_end_of_list:
6545 printf (_("<End of list>\n"));
6546 break;
6547 case DW_LLE_offset_pair:
6548 READ_ULEB (begin, start, section_end);
6549 begin += base_address;
6550 READ_ULEB (end, start, section_end);
6551 end += base_address;
6552 break;
6553 case DW_LLE_start_end:
6554 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6555 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, section_end);
6556 break;
6557 case DW_LLE_start_length:
6558 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, section_end);
6559 READ_ULEB (end, start, section_end);
6560 end += begin;
6561 break;
6562 case DW_LLE_base_address:
6563 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size,
6564 section_end);
6565 print_dwarf_vma (base_address, pointer_size);
6566 printf (_("(base address)\n"));
6567 break;
6568 #ifdef DW_LLE_view_pair
6569 case DW_LLE_view_pair:
6570 if (vstart)
6571 printf (_("View pair entry in loclist with locviews attribute\n"));
6572 READ_ULEB (vbegin, start, section_end);
6573 print_dwarf_view (vbegin, pointer_size, 1);
6575 READ_ULEB (vend, start, section_end);
6576 print_dwarf_view (vend, pointer_size, 1);
6578 printf (_("views for:\n"));
6579 continue;
6580 #endif
6581 default:
6582 error (_("Invalid location list entry type %d\n"), llet);
6583 return;
6585 if (llet == DW_LLE_end_of_list)
6586 break;
6587 if (llet != DW_LLE_offset_pair
6588 && llet != DW_LLE_start_end
6589 && llet != DW_LLE_start_length)
6590 continue;
6592 if (start == section_end)
6594 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6595 (unsigned long) offset);
6596 break;
6598 READ_ULEB (length, start, section_end);
6600 if (length > (size_t) (section_end - start))
6602 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6603 (unsigned long) offset);
6604 break;
6607 print_dwarf_vma (begin, pointer_size);
6608 print_dwarf_vma (end, pointer_size);
6610 putchar ('(');
6611 need_frame_base = decode_location_expression (start,
6612 pointer_size,
6613 offset_size,
6614 dwarf_version,
6615 length,
6616 cu_offset, section);
6617 putchar (')');
6619 if (need_frame_base && !has_frame_base)
6620 printf (_(" [without DW_AT_frame_base]"));
6622 if (begin == end && vbegin == vend)
6623 fputs (_(" (start == end)"), stdout);
6624 else if (begin > end || (begin == end && vbegin > vend))
6625 fputs (_(" (start > end)"), stdout);
6627 putchar ('\n');
6629 start += length;
6630 vbegin = vend = -1;
6633 if (vbegin != vm1 || vend != vm1)
6634 printf (_("Trailing view pair not used in a range"));
6636 *start_ptr = start;
6637 *vstart_ptr = vstart;
6640 /* Print a .debug_addr table index in decimal, surrounded by square brackets,
6641 right-adjusted in a field of length LEN, and followed by a space. */
6643 static void
6644 print_addr_index (unsigned int idx, unsigned int len)
6646 static char buf[15];
6647 snprintf (buf, sizeof (buf), "[%d]", idx);
6648 printf ("%*s ", len, buf);
6651 /* Display a location list from a .dwo section. It uses address indexes rather
6652 than embedded addresses. This code closely follows display_loc_list, but the
6653 two are sufficiently different that combining things is very ugly. */
6655 static void
6656 display_loc_list_dwo (struct dwarf_section *section,
6657 unsigned char **start_ptr,
6658 unsigned int debug_info_entry,
6659 dwarf_vma offset,
6660 unsigned char **vstart_ptr,
6661 int has_frame_base)
6663 unsigned char *start = *start_ptr, *vstart = *vstart_ptr;
6664 unsigned char *section_end = section->start + section->size;
6665 dwarf_vma cu_offset;
6666 unsigned int pointer_size;
6667 unsigned int offset_size;
6668 int dwarf_version;
6669 int entry_type;
6670 unsigned short length;
6671 int need_frame_base;
6672 unsigned int idx;
6674 if (debug_info_entry >= num_debug_info_entries)
6676 warn (_("No debug information for loc lists of entry: %u\n"),
6677 debug_info_entry);
6678 return;
6681 cu_offset = debug_information [debug_info_entry].cu_offset;
6682 pointer_size = debug_information [debug_info_entry].pointer_size;
6683 offset_size = debug_information [debug_info_entry].offset_size;
6684 dwarf_version = debug_information [debug_info_entry].dwarf_version;
6686 if (pointer_size < 2 || pointer_size > 8)
6688 warn (_("Invalid pointer size (%d) in debug info for entry %d\n"),
6689 pointer_size, debug_info_entry);
6690 return;
6693 while (1)
6695 printf (" ");
6696 print_dwarf_vma (offset + (start - *start_ptr), 4);
6698 if (start >= section_end)
6700 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6701 (unsigned long) offset);
6702 break;
6705 SAFE_BYTE_GET_AND_INC (entry_type, start, 1, section_end);
6707 if (vstart)
6708 switch (entry_type)
6710 default:
6711 break;
6713 case 2:
6714 case 3:
6715 case 4:
6717 dwarf_vma view;
6718 dwarf_vma off = offset + (vstart - *start_ptr);
6720 READ_ULEB (view, vstart, section_end);
6721 print_dwarf_view (view, 8, 1);
6723 READ_ULEB (view, vstart, section_end);
6724 print_dwarf_view (view, 8, 1);
6726 printf (_("views at %8.8lx for:\n %*s "),
6727 (unsigned long) off, 8, "");
6730 break;
6733 switch (entry_type)
6735 case 0: /* A terminating entry. */
6736 *start_ptr = start;
6737 *vstart_ptr = vstart;
6738 printf (_("<End of list>\n"));
6739 return;
6740 case 1: /* A base-address entry. */
6741 READ_ULEB (idx, start, section_end);
6742 print_addr_index (idx, 8);
6743 printf ("%*s", 9 + (vstart ? 2 * 6 : 0), "");
6744 printf (_("(base address selection entry)\n"));
6745 continue;
6746 case 2: /* A start/end entry. */
6747 READ_ULEB (idx, start, section_end);
6748 print_addr_index (idx, 8);
6749 READ_ULEB (idx, start, section_end);
6750 print_addr_index (idx, 8);
6751 break;
6752 case 3: /* A start/length entry. */
6753 READ_ULEB (idx, start, section_end);
6754 print_addr_index (idx, 8);
6755 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6756 printf ("%08x ", idx);
6757 break;
6758 case 4: /* An offset pair entry. */
6759 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6760 printf ("%08x ", idx);
6761 SAFE_BYTE_GET_AND_INC (idx, start, 4, section_end);
6762 printf ("%08x ", idx);
6763 break;
6764 default:
6765 warn (_("Unknown location list entry type 0x%x.\n"), entry_type);
6766 *start_ptr = start;
6767 *vstart_ptr = vstart;
6768 return;
6771 if (2 > (size_t) (section_end - start))
6773 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6774 (unsigned long) offset);
6775 break;
6778 SAFE_BYTE_GET_AND_INC (length, start, 2, section_end);
6779 if (length > (size_t) (section_end - start))
6781 warn (_("Location list starting at offset 0x%lx is not terminated.\n"),
6782 (unsigned long) offset);
6783 break;
6786 putchar ('(');
6787 need_frame_base = decode_location_expression (start,
6788 pointer_size,
6789 offset_size,
6790 dwarf_version,
6791 length,
6792 cu_offset, section);
6793 putchar (')');
6795 if (need_frame_base && !has_frame_base)
6796 printf (_(" [without DW_AT_frame_base]"));
6798 putchar ('\n');
6800 start += length;
6803 *start_ptr = start;
6804 *vstart_ptr = vstart;
6807 /* Sort array of indexes in ascending order of loc_offsets[idx] and
6808 loc_views. */
6810 static dwarf_vma *loc_offsets, *loc_views;
6812 static int
6813 loc_offsets_compar (const void *ap, const void *bp)
6815 dwarf_vma a = loc_offsets[*(const unsigned int *) ap];
6816 dwarf_vma b = loc_offsets[*(const unsigned int *) bp];
6818 int ret = (a > b) - (b > a);
6819 if (ret)
6820 return ret;
6822 a = loc_views[*(const unsigned int *) ap];
6823 b = loc_views[*(const unsigned int *) bp];
6825 ret = (a > b) - (b > a);
6827 return ret;
6830 static int
6831 display_debug_loc (struct dwarf_section *section, void *file)
6833 unsigned char *start = section->start, *vstart = NULL;
6834 dwarf_vma bytes;
6835 unsigned char *section_begin = start;
6836 unsigned int num_loc_list = 0;
6837 dwarf_vma last_offset = 0;
6838 dwarf_vma last_view = 0;
6839 unsigned int first = 0;
6840 unsigned int i;
6841 unsigned int j;
6842 int seen_first_offset = 0;
6843 int locs_sorted = 1;
6844 unsigned char *next = start, *vnext = vstart;
6845 unsigned int *array = NULL;
6846 const char *suffix = strrchr (section->name, '.');
6847 bool is_dwo = false;
6848 int is_loclists = strstr (section->name, "debug_loclists") != NULL;
6849 dwarf_vma expected_start = 0;
6851 if (suffix && strcmp (suffix, ".dwo") == 0)
6852 is_dwo = true;
6854 bytes = section->size;
6856 if (bytes == 0)
6858 printf (_("\nThe %s section is empty.\n"), section->name);
6859 return 0;
6862 if (is_loclists)
6864 unsigned char *hdrptr = section_begin;
6865 dwarf_vma ll_length;
6866 unsigned short ll_version;
6867 unsigned char *end = section_begin + section->size;
6868 unsigned char address_size, segment_selector_size;
6869 uint32_t offset_entry_count;
6871 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 4, end);
6872 if (ll_length == 0xffffffff)
6873 SAFE_BYTE_GET_AND_INC (ll_length, hdrptr, 8, end);
6875 SAFE_BYTE_GET_AND_INC (ll_version, hdrptr, 2, end);
6876 if (ll_version != 5)
6878 warn (_("The %s section contains corrupt or "
6879 "unsupported version number: %d.\n"),
6880 section->name, ll_version);
6881 return 0;
6884 SAFE_BYTE_GET_AND_INC (address_size, hdrptr, 1, end);
6886 SAFE_BYTE_GET_AND_INC (segment_selector_size, hdrptr, 1, end);
6887 if (segment_selector_size != 0)
6889 warn (_("The %s section contains "
6890 "unsupported segment selector size: %d.\n"),
6891 section->name, segment_selector_size);
6892 return 0;
6895 SAFE_BYTE_GET_AND_INC (offset_entry_count, hdrptr, 4, end);
6896 if (offset_entry_count != 0)
6898 warn (_("The %s section contains "
6899 "unsupported offset entry count: %d.\n"),
6900 section->name, offset_entry_count);
6901 return 0;
6904 expected_start = hdrptr - section_begin;
6907 if (load_debug_info (file) == 0)
6909 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
6910 section->name);
6911 return 0;
6914 /* Check the order of location list in .debug_info section. If
6915 offsets of location lists are in the ascending order, we can
6916 use `debug_information' directly. */
6917 for (i = 0; i < num_debug_info_entries; i++)
6919 unsigned int num;
6921 num = debug_information [i].num_loc_offsets;
6922 if (num > num_loc_list)
6923 num_loc_list = num;
6925 /* Check if we can use `debug_information' directly. */
6926 if (locs_sorted && num != 0)
6928 if (!seen_first_offset)
6930 /* This is the first location list. */
6931 last_offset = debug_information [i].loc_offsets [0];
6932 last_view = debug_information [i].loc_views [0];
6933 first = i;
6934 seen_first_offset = 1;
6935 j = 1;
6937 else
6938 j = 0;
6940 for (; j < num; j++)
6942 if (last_offset >
6943 debug_information [i].loc_offsets [j]
6944 || (last_offset == debug_information [i].loc_offsets [j]
6945 && last_view > debug_information [i].loc_views [j]))
6947 locs_sorted = 0;
6948 break;
6950 last_offset = debug_information [i].loc_offsets [j];
6951 last_view = debug_information [i].loc_views [j];
6956 if (!seen_first_offset)
6957 error (_("No location lists in .debug_info section!\n"));
6959 if (debug_information [first].num_loc_offsets > 0
6960 && debug_information [first].loc_offsets [0] != expected_start
6961 && debug_information [first].loc_views [0] != expected_start)
6962 warn (_("Location lists in %s section start at 0x%s\n"),
6963 section->name,
6964 dwarf_vmatoa ("x", debug_information [first].loc_offsets [0]));
6966 if (!locs_sorted)
6967 array = (unsigned int *) xcmalloc (num_loc_list, sizeof (unsigned int));
6969 introduce (section, false);
6971 if (reloc_at (section, 0))
6972 printf (_(" Warning: This section has relocations - addresses seen here may not be accurate.\n\n"));
6974 printf (_(" Offset Begin End Expression\n"));
6976 seen_first_offset = 0;
6977 for (i = first; i < num_debug_info_entries; i++)
6979 dwarf_vma offset, voffset;
6980 dwarf_vma base_address;
6981 unsigned int k;
6982 int has_frame_base;
6984 if (!locs_sorted)
6986 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
6987 array[k] = k;
6988 loc_offsets = debug_information [i].loc_offsets;
6989 loc_views = debug_information [i].loc_views;
6990 qsort (array, debug_information [i].num_loc_offsets,
6991 sizeof (*array), loc_offsets_compar);
6994 int adjacent_view_loclists = 1;
6995 for (k = 0; k < debug_information [i].num_loc_offsets; k++)
6997 j = locs_sorted ? k : array[k];
6998 if (k
6999 && (debug_information [i].loc_offsets [locs_sorted
7000 ? k - 1 : array [k - 1]]
7001 == debug_information [i].loc_offsets [j])
7002 && (debug_information [i].loc_views [locs_sorted
7003 ? k - 1 : array [k - 1]]
7004 == debug_information [i].loc_views [j]))
7005 continue;
7006 has_frame_base = debug_information [i].have_frame_base [j];
7007 offset = debug_information [i].loc_offsets [j];
7008 next = section_begin + offset;
7009 voffset = debug_information [i].loc_views [j];
7010 if (voffset != vm1)
7011 vnext = section_begin + voffset;
7012 else
7013 vnext = NULL;
7014 base_address = debug_information [i].base_address;
7016 if (vnext && vnext < next)
7018 vstart = vnext;
7019 display_view_pair_list (section, &vstart, i, next);
7020 if (start == vnext)
7021 start = vstart;
7024 if (!seen_first_offset || !adjacent_view_loclists)
7025 seen_first_offset = 1;
7026 else
7028 if (start < next)
7029 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
7030 (unsigned long) (start - section_begin),
7031 (unsigned long) offset,
7032 section->name);
7033 else if (start > next)
7034 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
7035 (unsigned long) (start - section_begin),
7036 (unsigned long) offset,
7037 section->name);
7039 start = next;
7040 vstart = vnext;
7042 if (offset >= bytes)
7044 warn (_("Offset 0x%lx is bigger than %s section size.\n"),
7045 (unsigned long) offset,
7046 section->name);
7047 continue;
7050 if (vnext && voffset >= bytes)
7052 warn (_("View Offset 0x%lx is bigger than %s section size.\n"),
7053 (unsigned long) voffset,
7054 section->name);
7055 continue;
7058 if (!is_loclists)
7060 if (is_dwo)
7061 display_loc_list_dwo (section, &start, i, offset,
7062 &vstart, has_frame_base);
7063 else
7064 display_loc_list (section, &start, i, offset, base_address,
7065 &vstart, has_frame_base);
7067 else
7069 if (is_dwo)
7070 warn (_("DWO is not yet supported.\n"));
7071 else
7072 display_loclists_list (section, &start, i, offset, base_address,
7073 &vstart, has_frame_base);
7076 /* FIXME: this arrangement is quite simplistic. Nothing
7077 requires locview lists to be adjacent to corresponding
7078 loclists, and a single loclist could be augmented by
7079 different locview lists, and vice-versa, unlikely as it
7080 is that it would make sense to do so. Hopefully we'll
7081 have view pair support built into loclists before we ever
7082 need to address all these possibilities. */
7083 if (adjacent_view_loclists && vnext
7084 && vnext != start && vstart != next)
7086 adjacent_view_loclists = 0;
7087 warn (_("Hole and overlap detection requires adjacent view lists and loclists.\n"));
7090 if (vnext && vnext == start)
7091 display_view_pair_list (section, &start, i, vstart);
7095 if (start < section->start + section->size)
7096 warn (ngettext ("There is %ld unused byte at the end of section %s\n",
7097 "There are %ld unused bytes at the end of section %s\n",
7098 (long) (section->start + section->size - start)),
7099 (long) (section->start + section->size - start), section->name);
7100 putchar ('\n');
7101 free (array);
7102 return 1;
7105 static int
7106 display_debug_str (struct dwarf_section *section,
7107 void *file ATTRIBUTE_UNUSED)
7109 unsigned char *start = section->start;
7110 dwarf_vma bytes = section->size;
7111 dwarf_vma addr = section->address;
7113 if (bytes == 0)
7115 printf (_("\nThe %s section is empty.\n"), section->name);
7116 return 0;
7119 introduce (section, false);
7121 while (bytes)
7123 int j;
7124 int k;
7125 int lbytes;
7127 lbytes = (bytes > 16 ? 16 : bytes);
7129 printf (" 0x%8.8lx ", (unsigned long) addr);
7131 for (j = 0; j < 16; j++)
7133 if (j < lbytes)
7134 printf ("%2.2x", start[j]);
7135 else
7136 printf (" ");
7138 if ((j & 3) == 3)
7139 printf (" ");
7142 for (j = 0; j < lbytes; j++)
7144 k = start[j];
7145 if (k >= ' ' && k < 0x80)
7146 printf ("%c", k);
7147 else
7148 printf (".");
7151 putchar ('\n');
7153 start += lbytes;
7154 addr += lbytes;
7155 bytes -= lbytes;
7158 putchar ('\n');
7160 return 1;
7163 static int
7164 display_debug_info (struct dwarf_section *section, void *file)
7166 return process_debug_info (section, file, section->abbrev_sec, false, false);
7169 static int
7170 display_debug_types (struct dwarf_section *section, void *file)
7172 return process_debug_info (section, file, section->abbrev_sec, false, true);
7175 static int
7176 display_trace_info (struct dwarf_section *section, void *file)
7178 return process_debug_info (section, file, section->abbrev_sec, false, true);
7181 static int
7182 display_debug_aranges (struct dwarf_section *section,
7183 void *file ATTRIBUTE_UNUSED)
7185 unsigned char *start = section->start;
7186 unsigned char *end = start + section->size;
7188 introduce (section, false);
7190 /* It does not matter if this load fails,
7191 we test for that later on. */
7192 load_debug_info (file);
7194 while (start < end)
7196 unsigned char *hdrptr;
7197 DWARF2_Internal_ARange arange;
7198 unsigned char *addr_ranges;
7199 dwarf_vma length;
7200 dwarf_vma address;
7201 dwarf_vma sec_off;
7202 unsigned char address_size;
7203 unsigned int offset_size;
7204 unsigned char *end_ranges;
7206 hdrptr = start;
7207 sec_off = hdrptr - section->start;
7209 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 4, end);
7210 if (arange.ar_length == 0xffffffff)
7212 SAFE_BYTE_GET_AND_INC (arange.ar_length, hdrptr, 8, end);
7213 offset_size = 8;
7215 else
7216 offset_size = 4;
7218 if (arange.ar_length > (size_t) (end - hdrptr))
7220 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
7221 section->name,
7222 (unsigned long) sec_off,
7223 dwarf_vmatoa ("x", arange.ar_length));
7224 break;
7226 end_ranges = hdrptr + arange.ar_length;
7228 SAFE_BYTE_GET_AND_INC (arange.ar_version, hdrptr, 2, end_ranges);
7229 SAFE_BYTE_GET_AND_INC (arange.ar_info_offset, hdrptr, offset_size,
7230 end_ranges);
7232 if (num_debug_info_entries != DEBUG_INFO_UNAVAILABLE
7233 && num_debug_info_entries > 0
7234 && find_debug_info_for_offset (arange.ar_info_offset) == NULL)
7235 warn (_(".debug_info offset of 0x%lx in %s section does not point to a CU header.\n"),
7236 (unsigned long) arange.ar_info_offset, section->name);
7238 SAFE_BYTE_GET_AND_INC (arange.ar_pointer_size, hdrptr, 1, end_ranges);
7239 SAFE_BYTE_GET_AND_INC (arange.ar_segment_size, hdrptr, 1, end_ranges);
7241 if (arange.ar_version != 2 && arange.ar_version != 3)
7243 /* PR 19872: A version number of 0 probably means that there is
7244 padding at the end of the .debug_aranges section. Gold puts
7245 it there when performing an incremental link, for example.
7246 So do not generate a warning in this case. */
7247 if (arange.ar_version)
7248 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
7249 break;
7252 printf (_(" Length: %ld\n"),
7253 (long) arange.ar_length);
7254 printf (_(" Version: %d\n"), arange.ar_version);
7255 printf (_(" Offset into .debug_info: 0x%lx\n"),
7256 (unsigned long) arange.ar_info_offset);
7257 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
7258 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
7260 address_size = arange.ar_pointer_size + arange.ar_segment_size;
7262 /* PR 17512: file: 001-108546-0.001:0.1. */
7263 if (address_size == 0 || address_size > 8)
7265 error (_("Invalid address size in %s section!\n"),
7266 section->name);
7267 break;
7270 /* The DWARF spec does not require that the address size be a power
7271 of two, but we do. This will have to change if we ever encounter
7272 an uneven architecture. */
7273 if ((address_size & (address_size - 1)) != 0)
7275 warn (_("Pointer size + Segment size is not a power of two.\n"));
7276 break;
7279 if (address_size > 4)
7280 printf (_("\n Address Length\n"));
7281 else
7282 printf (_("\n Address Length\n"));
7284 addr_ranges = hdrptr;
7286 /* Must pad to an alignment boundary that is twice the address size. */
7287 addr_ranges += (2 * address_size - 1
7288 - (hdrptr - start - 1) % (2 * address_size));
7290 while (2 * address_size <= end_ranges - addr_ranges)
7292 SAFE_BYTE_GET_AND_INC (address, addr_ranges, address_size,
7293 end_ranges);
7294 SAFE_BYTE_GET_AND_INC (length, addr_ranges, address_size,
7295 end_ranges);
7296 printf (" ");
7297 print_dwarf_vma (address, address_size);
7298 print_dwarf_vma (length, address_size);
7299 putchar ('\n');
7302 start = end_ranges;
7305 printf ("\n");
7307 return 1;
7310 /* Comparison function for qsort. */
7311 static int
7312 comp_addr_base (const void * v0, const void * v1)
7314 debug_info *info0 = *(debug_info **) v0;
7315 debug_info *info1 = *(debug_info **) v1;
7316 return info0->addr_base - info1->addr_base;
7319 /* Display the debug_addr section. */
7320 static int
7321 display_debug_addr (struct dwarf_section *section,
7322 void *file)
7324 debug_info **debug_addr_info;
7325 unsigned char *entry;
7326 unsigned char *end;
7327 unsigned int i;
7328 unsigned int count;
7329 unsigned char * header;
7331 if (section->size == 0)
7333 printf (_("\nThe %s section is empty.\n"), section->name);
7334 return 0;
7337 if (load_debug_info (file) == 0)
7339 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7340 section->name);
7341 return 0;
7344 introduce (section, false);
7346 /* PR 17531: file: cf38d01b.
7347 We use xcalloc because a corrupt file may not have initialised all of the
7348 fields in the debug_info structure, which means that the sort below might
7349 try to move uninitialised data. */
7350 debug_addr_info = (debug_info **) xcalloc ((num_debug_info_entries + 1),
7351 sizeof (debug_info *));
7353 count = 0;
7354 for (i = 0; i < num_debug_info_entries; i++)
7355 if (debug_information [i].addr_base != DEBUG_INFO_UNAVAILABLE)
7357 /* PR 17531: file: cf38d01b. */
7358 if (debug_information[i].addr_base >= section->size)
7359 warn (_("Corrupt address base (%lx) found in debug section %u\n"),
7360 (unsigned long) debug_information[i].addr_base, i);
7361 else
7362 debug_addr_info [count++] = debug_information + i;
7365 /* Add a sentinel to make iteration convenient. */
7366 debug_addr_info [count] = (debug_info *) xmalloc (sizeof (debug_info));
7367 debug_addr_info [count]->addr_base = section->size;
7368 qsort (debug_addr_info, count, sizeof (debug_info *), comp_addr_base);
7370 header = section->start;
7371 for (i = 0; i < count; i++)
7373 unsigned int idx;
7374 unsigned int address_size = debug_addr_info [i]->pointer_size;
7376 printf (_(" For compilation unit at offset 0x%s:\n"),
7377 dwarf_vmatoa ("x", debug_addr_info [i]->cu_offset));
7379 printf (_("\tIndex\tAddress\n"));
7380 entry = section->start + debug_addr_info [i]->addr_base;
7381 if (debug_addr_info [i]->dwarf_version >= 5)
7383 size_t header_size = entry - header;
7384 unsigned char * curr_header = header;
7385 dwarf_vma length;
7386 int version;
7387 int segment_selector_size;
7389 if (header_size != 8 && header_size != 16)
7391 warn (_("Corrupt %s section: expecting header size of 8 or 16, but found %ld instead\n"),
7392 section->name, (long) header_size);
7393 return 0;
7396 SAFE_BYTE_GET_AND_INC (length, curr_header, 4, entry);
7397 if (length == 0xffffffff)
7398 SAFE_BYTE_GET_AND_INC (length, curr_header, 8, entry);
7399 end = curr_header + length;
7401 SAFE_BYTE_GET_AND_INC (version, curr_header, 2, entry);
7402 if (version != 5)
7403 warn (_("Corrupt %s section: expecting version number 5 in header but found %d instead\n"),
7404 section->name, version);
7406 SAFE_BYTE_GET_AND_INC (address_size, curr_header, 1, entry);
7407 SAFE_BYTE_GET_AND_INC (segment_selector_size, curr_header, 1, entry);
7408 address_size += segment_selector_size;
7410 else
7411 end = section->start + debug_addr_info [i + 1]->addr_base;
7412 header = end;
7413 idx = 0;
7414 while (entry < end)
7416 dwarf_vma base = byte_get (entry, address_size);
7417 printf (_("\t%d:\t"), idx);
7418 print_dwarf_vma (base, address_size);
7419 printf ("\n");
7420 entry += address_size;
7421 idx++;
7424 printf ("\n");
7426 free (debug_addr_info);
7427 return 1;
7430 /* Display the .debug_str_offsets and .debug_str_offsets.dwo sections. */
7432 static int
7433 display_debug_str_offsets (struct dwarf_section *section,
7434 void *file ATTRIBUTE_UNUSED)
7436 unsigned long idx;
7438 if (section->size == 0)
7440 printf (_("\nThe %s section is empty.\n"), section->name);
7441 return 0;
7444 unsigned char *start = section->start;
7445 unsigned char *end = start + section->size;
7446 unsigned char *curr = start;
7448 const char *suffix = strrchr (section->name, '.');
7449 bool dwo = suffix && strcmp (suffix, ".dwo") == 0;
7451 if (dwo)
7452 load_debug_section_with_follow (str_dwo, file);
7453 else
7454 load_debug_section_with_follow (str, file);
7456 introduce (section, false);
7458 while (curr < end)
7460 dwarf_vma length;
7461 dwarf_vma entry_length;
7463 SAFE_BYTE_GET_AND_INC (length, curr, 4, end);
7464 /* FIXME: We assume that this means 64-bit DWARF is being used. */
7465 if (length == 0xffffffff)
7467 SAFE_BYTE_GET_AND_INC (length, curr, 8, end);
7468 entry_length = 8;
7470 else
7471 entry_length = 4;
7473 unsigned char *entries_end;
7474 if (length == 0)
7476 /* This is probably an old style .debug_str_offset section which
7477 just contains offsets and no header (and the first offset is 0). */
7478 length = section->size;
7479 curr = section->start;
7480 entries_end = end;
7482 printf (_(" Length: %#lx\n"), (unsigned long) length);
7483 printf (_(" Index Offset [String]\n"));
7485 else
7487 if (length <= (dwarf_vma) (end - curr))
7488 entries_end = curr + length;
7489 else
7491 warn (_("Section %s is too small %#lx\n"),
7492 section->name, (unsigned long) section->size);
7493 entries_end = end;
7496 int version;
7497 SAFE_BYTE_GET_AND_INC (version, curr, 2, entries_end);
7498 if (version != 5)
7499 warn (_("Unexpected version number in str_offset header: %#x\n"), version);
7501 int padding;
7502 SAFE_BYTE_GET_AND_INC (padding, curr, 2, entries_end);
7503 if (padding != 0)
7504 warn (_("Unexpected value in str_offset header's padding field: %#x\n"), padding);
7506 printf (_(" Length: %#lx\n"), (unsigned long) length);
7507 printf (_(" Version: %#lx\n"), (unsigned long) version);
7508 printf (_(" Index Offset [String]\n"));
7511 for (idx = 0; curr < entries_end; idx++)
7513 dwarf_vma offset;
7514 const unsigned char * string;
7516 if ((dwarf_vma) (entries_end - curr) < entry_length)
7517 /* Not enough space to read one entry_length, give up. */
7518 return 0;
7520 SAFE_BYTE_GET_AND_INC (offset, curr, entry_length, entries_end);
7521 if (dwo)
7522 string = (const unsigned char *)
7523 fetch_indexed_string (idx, NULL, entry_length, dwo);
7524 else
7525 string = fetch_indirect_string (offset);
7527 printf (" %8lu %8s %s\n", idx, dwarf_vmatoa ("x", offset),
7528 string);
7532 return 1;
7535 /* Each debug_information[x].range_lists[y] gets this representation for
7536 sorting purposes. */
7538 struct range_entry
7540 /* The debug_information[x].range_lists[y] value. */
7541 dwarf_vma ranges_offset;
7543 /* Original debug_information to find parameters of the data. */
7544 debug_info *debug_info_p;
7547 /* Sort struct range_entry in ascending order of its RANGES_OFFSET. */
7549 static int
7550 range_entry_compar (const void *ap, const void *bp)
7552 const struct range_entry *a_re = (const struct range_entry *) ap;
7553 const struct range_entry *b_re = (const struct range_entry *) bp;
7554 const dwarf_vma a = a_re->ranges_offset;
7555 const dwarf_vma b = b_re->ranges_offset;
7557 return (a > b) - (b > a);
7560 static void
7561 display_debug_ranges_list (unsigned char * start,
7562 unsigned char * finish,
7563 unsigned int pointer_size,
7564 dwarf_vma offset,
7565 dwarf_vma base_address)
7567 while (start < finish)
7569 dwarf_vma begin;
7570 dwarf_vma end;
7572 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7573 if (start >= finish)
7574 break;
7575 SAFE_SIGNED_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7577 printf (" ");
7578 print_dwarf_vma (offset, 4);
7580 if (begin == 0 && end == 0)
7582 printf (_("<End of list>\n"));
7583 break;
7586 /* Check base address specifiers. */
7587 if (is_max_address (begin, pointer_size)
7588 && !is_max_address (end, pointer_size))
7590 base_address = end;
7591 print_dwarf_vma (begin, pointer_size);
7592 print_dwarf_vma (end, pointer_size);
7593 printf ("(base address)\n");
7594 continue;
7597 print_dwarf_vma (begin + base_address, pointer_size);
7598 print_dwarf_vma (end + base_address, pointer_size);
7600 if (begin == end)
7601 fputs (_("(start == end)"), stdout);
7602 else if (begin > end)
7603 fputs (_("(start > end)"), stdout);
7605 putchar ('\n');
7609 static void
7610 display_debug_rnglists_list (unsigned char * start,
7611 unsigned char * finish,
7612 unsigned int pointer_size,
7613 dwarf_vma offset,
7614 dwarf_vma base_address)
7616 unsigned char *next = start;
7618 while (1)
7620 dwarf_vma off = offset + (start - next);
7621 enum dwarf_range_list_entry rlet;
7622 /* Initialize it due to a false compiler warning. */
7623 dwarf_vma begin = -1, length, end = -1;
7625 if (start >= finish)
7627 warn (_("Range list starting at offset 0x%s is not terminated.\n"),
7628 dwarf_vmatoa ("x", offset));
7629 break;
7632 printf (" ");
7633 print_dwarf_vma (off, 4);
7635 SAFE_BYTE_GET_AND_INC (rlet, start, 1, finish);
7637 switch (rlet)
7639 case DW_RLE_end_of_list:
7640 printf (_("<End of list>\n"));
7641 break;
7642 case DW_RLE_base_address:
7643 SAFE_BYTE_GET_AND_INC (base_address, start, pointer_size, finish);
7644 print_dwarf_vma (base_address, pointer_size);
7645 printf (_("(base address)\n"));
7646 break;
7647 case DW_RLE_start_length:
7648 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7649 READ_ULEB (length, start, finish);
7650 end = begin + length;
7651 break;
7652 case DW_RLE_offset_pair:
7653 READ_ULEB (begin, start, finish);
7654 READ_ULEB (end, start, finish);
7655 break;
7656 case DW_RLE_start_end:
7657 SAFE_BYTE_GET_AND_INC (begin, start, pointer_size, finish);
7658 SAFE_BYTE_GET_AND_INC (end, start, pointer_size, finish);
7659 break;
7660 default:
7661 error (_("Invalid range list entry type %d\n"), rlet);
7662 rlet = DW_RLE_end_of_list;
7663 break;
7665 if (rlet == DW_RLE_end_of_list)
7666 break;
7667 if (rlet == DW_RLE_base_address)
7668 continue;
7670 /* Only a DW_RLE_offset_pair needs the base address added. */
7671 if (rlet == DW_RLE_offset_pair)
7673 begin += base_address;
7674 end += base_address;
7677 print_dwarf_vma (begin, pointer_size);
7678 print_dwarf_vma (end, pointer_size);
7680 if (begin == end)
7681 fputs (_("(start == end)"), stdout);
7682 else if (begin > end)
7683 fputs (_("(start > end)"), stdout);
7685 putchar ('\n');
7689 static int
7690 display_debug_ranges (struct dwarf_section *section,
7691 void *file ATTRIBUTE_UNUSED)
7693 unsigned char * start = section->start;
7694 unsigned char * last_start = start;
7695 dwarf_vma bytes = section->size;
7696 unsigned char * section_begin = start;
7697 unsigned char * finish = start + bytes;
7698 unsigned int num_range_list, i;
7699 struct range_entry * range_entries;
7700 struct range_entry * range_entry_fill;
7701 int is_rnglists = strstr (section->name, "debug_rnglists") != NULL;
7702 /* Initialize it due to a false compiler warning. */
7703 unsigned char address_size = 0;
7704 dwarf_vma last_offset = 0;
7706 if (bytes == 0)
7708 printf (_("\nThe %s section is empty.\n"), section->name);
7709 return 0;
7712 if (is_rnglists)
7714 dwarf_vma initial_length;
7715 unsigned char segment_selector_size;
7716 unsigned int offset_size, offset_entry_count;
7717 unsigned short version;
7719 /* Get and check the length of the block. */
7720 SAFE_BYTE_GET_AND_INC (initial_length, start, 4, finish);
7722 if (initial_length == 0xffffffff)
7724 /* This section is 64-bit DWARF 3. */
7725 SAFE_BYTE_GET_AND_INC (initial_length, start, 8, finish);
7726 offset_size = 8;
7728 else
7729 offset_size = 4;
7731 if (initial_length > (size_t) (finish - start))
7733 /* If the length field has a relocation against it, then we should
7734 not complain if it is inaccurate (and probably negative).
7735 It is copied from .debug_line handling code. */
7736 if (reloc_at (section, (start - section->start) - offset_size))
7738 initial_length = finish - start;
7740 else
7742 warn (_("The length field (0x%lx) in the debug_rnglists header is wrong - the section is too small\n"),
7743 (long) initial_length);
7744 return 0;
7748 /* Get and check the version number. */
7749 SAFE_BYTE_GET_AND_INC (version, start, 2, finish);
7751 if (version != 5)
7753 warn (_("Only DWARF version 5 debug_rnglists info "
7754 "is currently supported.\n"));
7755 return 0;
7758 SAFE_BYTE_GET_AND_INC (address_size, start, 1, finish);
7760 SAFE_BYTE_GET_AND_INC (segment_selector_size, start, 1, finish);
7761 if (segment_selector_size != 0)
7763 warn (_("The %s section contains "
7764 "unsupported segment selector size: %d.\n"),
7765 section->name, segment_selector_size);
7766 return 0;
7769 SAFE_BYTE_GET_AND_INC (offset_entry_count, start, 4, finish);
7770 if (offset_entry_count != 0)
7772 warn (_("The %s section contains "
7773 "unsupported offset entry count: %u.\n"),
7774 section->name, offset_entry_count);
7775 return 0;
7779 if (load_debug_info (file) == 0)
7781 warn (_("Unable to load/parse the .debug_info section, so cannot interpret the %s section.\n"),
7782 section->name);
7783 return 0;
7786 num_range_list = 0;
7787 for (i = 0; i < num_debug_info_entries; i++)
7789 if (debug_information [i].dwarf_version < 5 && is_rnglists)
7790 /* Skip .debug_rnglists reference. */
7791 continue;
7792 if (debug_information [i].dwarf_version >= 5 && !is_rnglists)
7793 /* Skip .debug_range reference. */
7794 continue;
7795 num_range_list += debug_information [i].num_range_lists;
7798 if (num_range_list == 0)
7800 /* This can happen when the file was compiled with -gsplit-debug
7801 which removes references to range lists from the primary .o file. */
7802 printf (_("No range lists in .debug_info section.\n"));
7803 return 1;
7806 range_entries = (struct range_entry *)
7807 xmalloc (sizeof (*range_entries) * num_range_list);
7808 range_entry_fill = range_entries;
7810 for (i = 0; i < num_debug_info_entries; i++)
7812 debug_info *debug_info_p = &debug_information[i];
7813 unsigned int j;
7815 if (debug_information [i].dwarf_version < 5 && is_rnglists)
7816 /* Skip .debug_rnglists reference. */
7817 continue;
7818 if (debug_information [i].dwarf_version >= 5 && !is_rnglists)
7819 /* Skip .debug_range reference. */
7820 continue;
7822 for (j = 0; j < debug_info_p->num_range_lists; j++)
7824 range_entry_fill->ranges_offset = debug_info_p->range_lists[j];
7825 range_entry_fill->debug_info_p = debug_info_p;
7826 range_entry_fill++;
7830 qsort (range_entries, num_range_list, sizeof (*range_entries),
7831 range_entry_compar);
7833 if (dwarf_check != 0 && range_entries[0].ranges_offset != 0)
7834 warn (_("Range lists in %s section start at 0x%lx\n"),
7835 section->name, (unsigned long) range_entries[0].ranges_offset);
7837 introduce (section, false);
7839 printf (_(" Offset Begin End\n"));
7841 for (i = 0; i < num_range_list; i++)
7843 struct range_entry *range_entry = &range_entries[i];
7844 debug_info *debug_info_p = range_entry->debug_info_p;
7845 unsigned int pointer_size;
7846 dwarf_vma offset;
7847 unsigned char *next;
7848 dwarf_vma base_address;
7850 pointer_size = (is_rnglists ? address_size : debug_info_p->pointer_size);
7851 offset = range_entry->ranges_offset;
7852 base_address = debug_info_p->base_address;
7854 /* PR 17512: file: 001-101485-0.001:0.1. */
7855 if (pointer_size < 2 || pointer_size > 8)
7857 warn (_("Corrupt pointer size (%d) in debug entry at offset %8.8lx\n"),
7858 pointer_size, (unsigned long) offset);
7859 continue;
7862 if (offset > (size_t) (finish - section_begin))
7864 warn (_("Corrupt offset (%#8.8lx) in range entry %u\n"),
7865 (unsigned long) offset, i);
7866 continue;
7868 next = section_begin + offset;
7870 /* If multiple DWARF entities reference the same range then we will
7871 have multiple entries in the `range_entries' list for the same
7872 offset. Thanks to the sort above these will all be consecutive in
7873 the `range_entries' list, so we can easily ignore duplicates
7874 here. */
7875 if (i > 0 && last_offset == offset)
7876 continue;
7877 last_offset = offset;
7879 if (dwarf_check != 0 && i > 0)
7881 if (start < next)
7882 warn (_("There is a hole [0x%lx - 0x%lx] in %s section.\n"),
7883 (unsigned long) (start - section_begin),
7884 (unsigned long) (next - section_begin), section->name);
7885 else if (start > next)
7887 if (next == last_start)
7888 continue;
7889 warn (_("There is an overlap [0x%lx - 0x%lx] in %s section.\n"),
7890 (unsigned long) (start - section_begin),
7891 (unsigned long) (next - section_begin), section->name);
7895 start = next;
7896 last_start = next;
7898 (is_rnglists ? display_debug_rnglists_list : display_debug_ranges_list)
7899 (start, finish, pointer_size, offset, base_address);
7901 putchar ('\n');
7903 free (range_entries);
7905 return 1;
7908 typedef struct Frame_Chunk
7910 struct Frame_Chunk *next;
7911 unsigned char *chunk_start;
7912 unsigned int ncols;
7913 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
7914 short int *col_type;
7915 int *col_offset;
7916 char *augmentation;
7917 unsigned int code_factor;
7918 int data_factor;
7919 dwarf_vma pc_begin;
7920 dwarf_vma pc_range;
7921 unsigned int cfa_reg;
7922 dwarf_vma cfa_offset;
7923 unsigned int ra;
7924 unsigned char fde_encoding;
7925 unsigned char cfa_exp;
7926 unsigned char ptr_size;
7927 unsigned char segment_size;
7929 Frame_Chunk;
7931 typedef const char *(*dwarf_regname_lookup_ftype) (unsigned int);
7932 static dwarf_regname_lookup_ftype dwarf_regnames_lookup_func;
7933 static const char *const *dwarf_regnames;
7934 static unsigned int dwarf_regnames_count;
7937 /* A marker for a col_type that means this column was never referenced
7938 in the frame info. */
7939 #define DW_CFA_unreferenced (-1)
7941 /* Return 0 if no more space is needed, 1 if more space is needed,
7942 -1 for invalid reg. */
7944 static int
7945 frame_need_space (Frame_Chunk *fc, unsigned int reg)
7947 unsigned int prev = fc->ncols;
7949 if (reg < (unsigned int) fc->ncols)
7950 return 0;
7952 if (dwarf_regnames_count > 0
7953 && reg > dwarf_regnames_count)
7954 return -1;
7956 fc->ncols = reg + 1;
7957 /* PR 17512: file: 10450-2643-0.004.
7958 If reg == -1 then this can happen... */
7959 if (fc->ncols == 0)
7960 return -1;
7962 /* PR 17512: file: 2844a11d. */
7963 if (fc->ncols > 1024 && dwarf_regnames_count == 0)
7965 error (_("Unfeasibly large register number: %u\n"), reg);
7966 fc->ncols = 0;
7967 /* FIXME: 1024 is an arbitrary limit. Increase it if
7968 we ever encounter a valid binary that exceeds it. */
7969 return -1;
7972 fc->col_type = (short int *) xcrealloc (fc->col_type, fc->ncols,
7973 sizeof (short int));
7974 fc->col_offset = (int *) xcrealloc (fc->col_offset, fc->ncols, sizeof (int));
7975 /* PR 17512: file:002-10025-0.005. */
7976 if (fc->col_type == NULL || fc->col_offset == NULL)
7978 error (_("Out of memory allocating %u columns in dwarf frame arrays\n"),
7979 fc->ncols);
7980 fc->ncols = 0;
7981 return -1;
7984 while (prev < fc->ncols)
7986 fc->col_type[prev] = DW_CFA_unreferenced;
7987 fc->col_offset[prev] = 0;
7988 prev++;
7990 return 1;
7993 static const char *const dwarf_regnames_i386[] =
7995 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
7996 "esp", "ebp", "esi", "edi", /* 4 - 7 */
7997 "eip", "eflags", NULL, /* 8 - 10 */
7998 "st0", "st1", "st2", "st3", /* 11 - 14 */
7999 "st4", "st5", "st6", "st7", /* 15 - 18 */
8000 NULL, NULL, /* 19 - 20 */
8001 "xmm0", "xmm1", "xmm2", "xmm3", /* 21 - 24 */
8002 "xmm4", "xmm5", "xmm6", "xmm7", /* 25 - 28 */
8003 "mm0", "mm1", "mm2", "mm3", /* 29 - 32 */
8004 "mm4", "mm5", "mm6", "mm7", /* 33 - 36 */
8005 "fcw", "fsw", "mxcsr", /* 37 - 39 */
8006 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8007 "tr", "ldtr", /* 48 - 49 */
8008 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8009 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8010 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8011 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8012 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8013 NULL, NULL, NULL, /* 90 - 92 */
8014 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7" /* 93 - 100 */
8017 static const char *const dwarf_regnames_iamcu[] =
8019 "eax", "ecx", "edx", "ebx", /* 0 - 3 */
8020 "esp", "ebp", "esi", "edi", /* 4 - 7 */
8021 "eip", "eflags", NULL, /* 8 - 10 */
8022 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 11 - 18 */
8023 NULL, NULL, /* 19 - 20 */
8024 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 21 - 28 */
8025 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 29 - 36 */
8026 NULL, NULL, NULL, /* 37 - 39 */
8027 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL, /* 40 - 47 */
8028 "tr", "ldtr", /* 48 - 49 */
8029 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 50 - 57 */
8030 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 58 - 65 */
8031 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 66 - 73 */
8032 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 74 - 81 */
8033 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 82 - 89 */
8034 NULL, NULL, NULL, /* 90 - 92 */
8035 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL /* 93 - 100 */
8038 static void
8039 init_dwarf_regnames_i386 (void)
8041 dwarf_regnames = dwarf_regnames_i386;
8042 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_i386);
8043 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8046 static void
8047 init_dwarf_regnames_iamcu (void)
8049 dwarf_regnames = dwarf_regnames_iamcu;
8050 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_iamcu);
8051 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8054 static const char *const dwarf_regnames_x86_64[] =
8056 "rax", "rdx", "rcx", "rbx",
8057 "rsi", "rdi", "rbp", "rsp",
8058 "r8", "r9", "r10", "r11",
8059 "r12", "r13", "r14", "r15",
8060 "rip",
8061 "xmm0", "xmm1", "xmm2", "xmm3",
8062 "xmm4", "xmm5", "xmm6", "xmm7",
8063 "xmm8", "xmm9", "xmm10", "xmm11",
8064 "xmm12", "xmm13", "xmm14", "xmm15",
8065 "st0", "st1", "st2", "st3",
8066 "st4", "st5", "st6", "st7",
8067 "mm0", "mm1", "mm2", "mm3",
8068 "mm4", "mm5", "mm6", "mm7",
8069 "rflags",
8070 "es", "cs", "ss", "ds", "fs", "gs", NULL, NULL,
8071 "fs.base", "gs.base", NULL, NULL,
8072 "tr", "ldtr",
8073 "mxcsr", "fcw", "fsw",
8074 "xmm16", "xmm17", "xmm18", "xmm19",
8075 "xmm20", "xmm21", "xmm22", "xmm23",
8076 "xmm24", "xmm25", "xmm26", "xmm27",
8077 "xmm28", "xmm29", "xmm30", "xmm31",
8078 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 83 - 90 */
8079 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 91 - 98 */
8080 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 99 - 106 */
8081 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, /* 107 - 114 */
8082 NULL, NULL, NULL, /* 115 - 117 */
8083 "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7"
8086 static void
8087 init_dwarf_regnames_x86_64 (void)
8089 dwarf_regnames = dwarf_regnames_x86_64;
8090 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_x86_64);
8091 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8094 static const char *const dwarf_regnames_aarch64[] =
8096 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
8097 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
8098 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
8099 "x24", "x25", "x26", "x27", "x28", "x29", "x30", "sp",
8100 NULL, "elr", NULL, NULL, NULL, NULL, NULL, NULL,
8101 NULL, NULL, NULL, NULL, NULL, NULL, "vg", "ffr",
8102 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
8103 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
8104 "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7",
8105 "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
8106 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23",
8107 "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31",
8108 "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z7",
8109 "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15",
8110 "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23",
8111 "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31",
8114 static void
8115 init_dwarf_regnames_aarch64 (void)
8117 dwarf_regnames = dwarf_regnames_aarch64;
8118 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_aarch64);
8119 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8122 static const char *const dwarf_regnames_s390[] =
8124 /* Avoid saying "r5 (r5)", so omit the names of r0-r15. */
8125 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8126 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
8127 "f0", "f2", "f4", "f6", "f1", "f3", "f5", "f7",
8128 "f8", "f10", "f12", "f14", "f9", "f11", "f13", "f15",
8129 "cr0", "cr1", "cr2", "cr3", "cr4", "cr5", "cr6", "cr7",
8130 "cr8", "cr9", "cr10", "cr11", "cr12", "cr13", "cr14", "cr15",
8131 "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
8132 "a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15",
8133 "pswm", "pswa",
8134 NULL, NULL,
8135 "v16", "v18", "v20", "v22", "v17", "v19", "v21", "v23",
8136 "v24", "v26", "v28", "v30", "v25", "v27", "v29", "v31",
8139 static void
8140 init_dwarf_regnames_s390 (void)
8142 dwarf_regnames = dwarf_regnames_s390;
8143 dwarf_regnames_count = ARRAY_SIZE (dwarf_regnames_s390);
8144 dwarf_regnames_lookup_func = regname_internal_by_table_only;
8147 static const char *const dwarf_regnames_riscv[] =
8149 "zero", "ra", "sp", "gp", "tp", "t0", "t1", "t2", /* 0 - 7 */
8150 "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", /* 8 - 15 */
8151 "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", /* 16 - 23 */
8152 "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6", /* 24 - 31 */
8153 "ft0", "ft1", "ft2", "ft3", "ft4", "ft5", "ft6", "ft7", /* 32 - 39 */
8154 "fs0", "fs1", /* 40 - 41 */
8155 "fa0", "fa1", "fa2", "fa3", "fa4", "fa5", "fa6", "fa7", /* 42 - 49 */
8156 "fs2", "fs3", "fs4", "fs5", "fs6", "fs7", "fs8", "fs9", /* 50 - 57 */
8157 "fs10", "fs11", /* 58 - 59 */
8158 "ft8", "ft9", "ft10", "ft11" /* 60 - 63 */
8161 /* A RISC-V replacement for REGNAME_INTERNAL_BY_TABLE_ONLY which handles
8162 the large number of CSRs. */
8164 static const char *
8165 regname_internal_riscv (unsigned int regno)
8167 const char *name = NULL;
8169 /* Lookup in the table first, this covers GPR and FPR. */
8170 if (regno < ARRAY_SIZE (dwarf_regnames_riscv))
8171 name = dwarf_regnames_riscv [regno];
8172 else if (regno >= 4096 && regno <= 8191)
8174 /* This might be a CSR, these live in a sparse number space from 4096
8175 to 8191 These numbers are defined in the RISC-V ELF ABI
8176 document. */
8177 switch (regno)
8179 #define DECLARE_CSR(NAME,VALUE,CLASS,DEFINE_VER,ABORT_VER) \
8180 case VALUE + 4096: name = #NAME; break;
8181 #include "opcode/riscv-opc.h"
8182 #undef DECLARE_CSR
8184 default:
8186 static char csr_name[10];
8187 snprintf (csr_name, sizeof (csr_name), "csr%d", (regno - 4096));
8188 name = csr_name;
8190 break;
8194 return name;
8197 static void
8198 init_dwarf_regnames_riscv (void)
8200 dwarf_regnames = NULL;
8201 dwarf_regnames_count = 8192;
8202 dwarf_regnames_lookup_func = regname_internal_riscv;
8205 void
8206 init_dwarf_regnames_by_elf_machine_code (unsigned int e_machine)
8208 dwarf_regnames_lookup_func = NULL;
8210 switch (e_machine)
8212 case EM_386:
8213 init_dwarf_regnames_i386 ();
8214 break;
8216 case EM_IAMCU:
8217 init_dwarf_regnames_iamcu ();
8218 break;
8220 case EM_X86_64:
8221 case EM_L1OM:
8222 case EM_K1OM:
8223 init_dwarf_regnames_x86_64 ();
8224 break;
8226 case EM_AARCH64:
8227 init_dwarf_regnames_aarch64 ();
8228 break;
8230 case EM_S390:
8231 init_dwarf_regnames_s390 ();
8232 break;
8234 case EM_RISCV:
8235 init_dwarf_regnames_riscv ();
8236 break;
8238 default:
8239 break;
8243 /* Initialize the DWARF register name lookup state based on the
8244 architecture and specific machine type of a BFD. */
8246 void
8247 init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
8248 unsigned long mach)
8250 dwarf_regnames_lookup_func = NULL;
8252 switch (arch)
8254 case bfd_arch_i386:
8255 switch (mach)
8257 case bfd_mach_x86_64:
8258 case bfd_mach_x86_64_intel_syntax:
8259 case bfd_mach_x64_32:
8260 case bfd_mach_x64_32_intel_syntax:
8261 init_dwarf_regnames_x86_64 ();
8262 break;
8264 default:
8265 init_dwarf_regnames_i386 ();
8266 break;
8268 break;
8270 case bfd_arch_iamcu:
8271 init_dwarf_regnames_iamcu ();
8272 break;
8274 case bfd_arch_aarch64:
8275 init_dwarf_regnames_aarch64();
8276 break;
8278 case bfd_arch_s390:
8279 init_dwarf_regnames_s390 ();
8280 break;
8282 case bfd_arch_riscv:
8283 init_dwarf_regnames_riscv ();
8284 break;
8286 default:
8287 break;
8291 static const char *
8292 regname_internal_by_table_only (unsigned int regno)
8294 if (dwarf_regnames != NULL
8295 && regno < dwarf_regnames_count
8296 && dwarf_regnames [regno] != NULL)
8297 return dwarf_regnames [regno];
8299 return NULL;
8302 static const char *
8303 regname (unsigned int regno, int name_only_p)
8305 static char reg[64];
8307 const char *name = NULL;
8309 if (dwarf_regnames_lookup_func != NULL)
8310 name = dwarf_regnames_lookup_func (regno);
8312 if (name != NULL)
8314 if (name_only_p)
8315 return name;
8316 snprintf (reg, sizeof (reg), "r%d (%s)", regno, name);
8318 else
8319 snprintf (reg, sizeof (reg), "r%d", regno);
8320 return reg;
8323 static void
8324 frame_display_row (Frame_Chunk *fc, int *need_col_headers, unsigned int *max_regs)
8326 unsigned int r;
8327 char tmp[100];
8329 if (*max_regs != fc->ncols)
8330 *max_regs = fc->ncols;
8332 if (*need_col_headers)
8334 *need_col_headers = 0;
8336 printf ("%-*s CFA ", eh_addr_size * 2, " LOC");
8338 for (r = 0; r < *max_regs; r++)
8339 if (fc->col_type[r] != DW_CFA_unreferenced)
8341 if (r == fc->ra)
8342 printf ("ra ");
8343 else
8344 printf ("%-5s ", regname (r, 1));
8347 printf ("\n");
8350 print_dwarf_vma (fc->pc_begin, eh_addr_size);
8351 if (fc->cfa_exp)
8352 strcpy (tmp, "exp");
8353 else
8354 sprintf (tmp, "%s%+d", regname (fc->cfa_reg, 1), (int) fc->cfa_offset);
8355 printf ("%-8s ", tmp);
8357 for (r = 0; r < fc->ncols; r++)
8359 if (fc->col_type[r] != DW_CFA_unreferenced)
8361 switch (fc->col_type[r])
8363 case DW_CFA_undefined:
8364 strcpy (tmp, "u");
8365 break;
8366 case DW_CFA_same_value:
8367 strcpy (tmp, "s");
8368 break;
8369 case DW_CFA_offset:
8370 sprintf (tmp, "c%+d", fc->col_offset[r]);
8371 break;
8372 case DW_CFA_val_offset:
8373 sprintf (tmp, "v%+d", fc->col_offset[r]);
8374 break;
8375 case DW_CFA_register:
8376 sprintf (tmp, "%s", regname (fc->col_offset[r], 0));
8377 break;
8378 case DW_CFA_expression:
8379 strcpy (tmp, "exp");
8380 break;
8381 case DW_CFA_val_expression:
8382 strcpy (tmp, "vexp");
8383 break;
8384 default:
8385 strcpy (tmp, "n/a");
8386 break;
8388 printf ("%-5s ", tmp);
8391 printf ("\n");
8394 #define GET(VAR, N) SAFE_BYTE_GET_AND_INC (VAR, start, N, end)
8396 static unsigned char *
8397 read_cie (unsigned char *start, unsigned char *end,
8398 Frame_Chunk **p_cie, int *p_version,
8399 bfd_size_type *p_aug_len, unsigned char **p_aug)
8401 int version;
8402 Frame_Chunk *fc;
8403 unsigned char *augmentation_data = NULL;
8404 bfd_size_type augmentation_data_len = 0;
8406 * p_cie = NULL;
8407 /* PR 17512: file: 001-228113-0.004. */
8408 if (start >= end)
8409 return end;
8411 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8412 memset (fc, 0, sizeof (Frame_Chunk));
8414 fc->col_type = (short int *) xmalloc (sizeof (short int));
8415 fc->col_offset = (int *) xmalloc (sizeof (int));
8417 version = *start++;
8419 fc->augmentation = (char *) start;
8420 /* PR 17512: file: 001-228113-0.004.
8421 Skip past augmentation name, but avoid running off the end of the data. */
8422 while (start < end)
8423 if (* start ++ == '\0')
8424 break;
8425 if (start == end)
8427 warn (_("No terminator for augmentation name\n"));
8428 goto fail;
8431 if (strcmp (fc->augmentation, "eh") == 0)
8433 if (eh_addr_size > (size_t) (end - start))
8434 goto fail;
8435 start += eh_addr_size;
8438 if (version >= 4)
8440 if (2 > (size_t) (end - start))
8441 goto fail;
8442 GET (fc->ptr_size, 1);
8443 if (fc->ptr_size < 1 || fc->ptr_size > 8)
8445 warn (_("Invalid pointer size (%d) in CIE data\n"), fc->ptr_size);
8446 goto fail;
8449 GET (fc->segment_size, 1);
8450 /* PR 17512: file: e99d2804. */
8451 if (fc->segment_size > 8 || fc->segment_size + fc->ptr_size > 8)
8453 warn (_("Invalid segment size (%d) in CIE data\n"), fc->segment_size);
8454 goto fail;
8457 eh_addr_size = fc->ptr_size;
8459 else
8461 fc->ptr_size = eh_addr_size;
8462 fc->segment_size = 0;
8465 READ_ULEB (fc->code_factor, start, end);
8466 READ_SLEB (fc->data_factor, start, end);
8468 if (start >= end)
8469 goto fail;
8471 if (version == 1)
8473 GET (fc->ra, 1);
8475 else
8477 READ_ULEB (fc->ra, start, end);
8480 if (fc->augmentation[0] == 'z')
8482 if (start >= end)
8483 goto fail;
8484 READ_ULEB (augmentation_data_len, start, end);
8485 augmentation_data = start;
8486 /* PR 17512: file: 11042-2589-0.004. */
8487 if (augmentation_data_len > (bfd_size_type) (end - start))
8489 warn (_("Augmentation data too long: 0x%s, expected at most %#lx\n"),
8490 dwarf_vmatoa ("x", augmentation_data_len),
8491 (unsigned long) (end - start));
8492 goto fail;
8494 start += augmentation_data_len;
8497 if (augmentation_data_len)
8499 unsigned char *p;
8500 unsigned char *q;
8501 unsigned char *qend;
8503 p = (unsigned char *) fc->augmentation + 1;
8504 q = augmentation_data;
8505 qend = q + augmentation_data_len;
8507 while (p < end && q < qend)
8509 if (*p == 'L')
8510 q++;
8511 else if (*p == 'P')
8512 q += 1 + size_of_encoded_value (*q);
8513 else if (*p == 'R')
8514 fc->fde_encoding = *q++;
8515 else if (*p == 'S')
8517 else if (*p == 'B')
8519 else
8520 break;
8521 p++;
8523 /* Note - it is OK if this loop terminates with q < qend.
8524 Padding may have been inserted to align the end of the CIE. */
8527 *p_cie = fc;
8528 if (p_version)
8529 *p_version = version;
8530 if (p_aug_len)
8532 *p_aug_len = augmentation_data_len;
8533 *p_aug = augmentation_data;
8535 return start;
8537 fail:
8538 free (fc->col_offset);
8539 free (fc->col_type);
8540 free (fc);
8541 return end;
8544 /* Prints out the contents on the DATA array formatted as unsigned bytes.
8545 If do_wide is not enabled, then formats the output to fit into 80 columns.
8546 PRINTED contains the number of characters already written to the current
8547 output line. */
8549 static void
8550 display_data (bfd_size_type printed,
8551 const unsigned char * data,
8552 const bfd_size_type len)
8554 if (do_wide || len < ((80 - printed) / 3))
8555 for (printed = 0; printed < len; ++printed)
8556 printf (" %02x", data[printed]);
8557 else
8559 for (printed = 0; printed < len; ++printed)
8561 if (printed % (80 / 3) == 0)
8562 putchar ('\n');
8563 printf (" %02x", data[printed]);
8568 /* Prints out the contents on the augmentation data array.
8569 If do_wide is not enabled, then formats the output to fit into 80 columns. */
8571 static void
8572 display_augmentation_data (const unsigned char * data, const bfd_size_type len)
8574 bfd_size_type i;
8576 i = printf (_(" Augmentation data: "));
8577 display_data (i, data, len);
8580 static int
8581 display_debug_frames (struct dwarf_section *section,
8582 void *file ATTRIBUTE_UNUSED)
8584 unsigned char *start = section->start;
8585 unsigned char *end = start + section->size;
8586 unsigned char *section_start = start;
8587 Frame_Chunk *chunks = NULL, *forward_refs = NULL;
8588 Frame_Chunk *remembered_state = NULL;
8589 Frame_Chunk *rs;
8590 bool is_eh = strcmp (section->name, ".eh_frame") == 0;
8591 unsigned int max_regs = 0;
8592 const char *bad_reg = _("bad register: ");
8593 unsigned int saved_eh_addr_size = eh_addr_size;
8595 introduce (section, false);
8597 while (start < end)
8599 unsigned char *saved_start;
8600 unsigned char *block_end;
8601 dwarf_vma length;
8602 dwarf_vma cie_id;
8603 Frame_Chunk *fc;
8604 Frame_Chunk *cie;
8605 int need_col_headers = 1;
8606 unsigned char *augmentation_data = NULL;
8607 bfd_size_type augmentation_data_len = 0;
8608 unsigned int encoded_ptr_size = saved_eh_addr_size;
8609 unsigned int offset_size;
8610 bool all_nops;
8611 static Frame_Chunk fde_fc;
8613 saved_start = start;
8615 SAFE_BYTE_GET_AND_INC (length, start, 4, end);
8617 if (length == 0)
8619 printf ("\n%08lx ZERO terminator\n\n",
8620 (unsigned long)(saved_start - section_start));
8621 /* Skip any zero terminators that directly follow.
8622 A corrupt section size could have loaded a whole
8623 slew of zero filled memory bytes. eg
8624 PR 17512: file: 070-19381-0.004. */
8625 while (start < end && * start == 0)
8626 ++ start;
8627 continue;
8630 if (length == 0xffffffff)
8632 SAFE_BYTE_GET_AND_INC (length, start, 8, end);
8633 offset_size = 8;
8635 else
8636 offset_size = 4;
8638 if (length > (size_t) (end - start))
8640 warn ("Invalid length 0x%s in FDE at %#08lx\n",
8641 dwarf_vmatoa_1 (NULL, length, offset_size),
8642 (unsigned long) (saved_start - section_start));
8643 block_end = end;
8645 else
8646 block_end = start + length;
8648 SAFE_BYTE_GET_AND_INC (cie_id, start, offset_size, block_end);
8650 if (is_eh ? (cie_id == 0) : ((offset_size == 4 && cie_id == DW_CIE_ID)
8651 || (offset_size == 8 && cie_id == DW64_CIE_ID)))
8653 int version;
8654 unsigned int mreg;
8656 start = read_cie (start, block_end, &cie, &version,
8657 &augmentation_data_len, &augmentation_data);
8658 /* PR 17512: file: 027-135133-0.005. */
8659 if (cie == NULL)
8660 break;
8662 fc = cie;
8663 fc->next = chunks;
8664 chunks = fc;
8665 fc->chunk_start = saved_start;
8666 mreg = max_regs > 0 ? max_regs - 1 : 0;
8667 if (mreg < fc->ra)
8668 mreg = fc->ra;
8669 if (frame_need_space (fc, mreg) < 0)
8670 break;
8671 if (fc->fde_encoding)
8672 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
8674 printf ("\n%08lx ", (unsigned long) (saved_start - section_start));
8675 print_dwarf_vma (length, fc->ptr_size);
8676 print_dwarf_vma (cie_id, offset_size);
8678 if (do_debug_frames_interp)
8680 printf ("CIE \"%s\" cf=%d df=%d ra=%d\n", fc->augmentation,
8681 fc->code_factor, fc->data_factor, fc->ra);
8683 else
8685 printf ("CIE\n");
8686 printf (" Version: %d\n", version);
8687 printf (" Augmentation: \"%s\"\n", fc->augmentation);
8688 if (version >= 4)
8690 printf (" Pointer Size: %u\n", fc->ptr_size);
8691 printf (" Segment Size: %u\n", fc->segment_size);
8693 printf (" Code alignment factor: %u\n", fc->code_factor);
8694 printf (" Data alignment factor: %d\n", fc->data_factor);
8695 printf (" Return address column: %d\n", fc->ra);
8697 if (augmentation_data_len)
8698 display_augmentation_data (augmentation_data, augmentation_data_len);
8700 putchar ('\n');
8703 else
8705 unsigned char *look_for;
8706 unsigned long segment_selector;
8707 dwarf_vma cie_off;
8709 cie_off = cie_id;
8710 if (is_eh)
8712 dwarf_vma sign = (dwarf_vma) 1 << (offset_size * 8 - 1);
8713 cie_off = (cie_off ^ sign) - sign;
8714 cie_off = start - 4 - section_start - cie_off;
8717 look_for = section_start + cie_off;
8718 if (cie_off <= (dwarf_vma) (saved_start - section_start))
8720 for (cie = chunks; cie ; cie = cie->next)
8721 if (cie->chunk_start == look_for)
8722 break;
8724 else if (cie_off >= section->size)
8725 cie = NULL;
8726 else
8728 for (cie = forward_refs; cie ; cie = cie->next)
8729 if (cie->chunk_start == look_for)
8730 break;
8731 if (!cie)
8733 unsigned int off_size;
8734 unsigned char *cie_scan;
8736 cie_scan = look_for;
8737 off_size = 4;
8738 SAFE_BYTE_GET_AND_INC (length, cie_scan, 4, end);
8739 if (length == 0xffffffff)
8741 SAFE_BYTE_GET_AND_INC (length, cie_scan, 8, end);
8742 off_size = 8;
8744 if (length != 0 && length <= (size_t) (end - cie_scan))
8746 dwarf_vma c_id;
8747 unsigned char *cie_end = cie_scan + length;
8749 SAFE_BYTE_GET_AND_INC (c_id, cie_scan, off_size,
8750 cie_end);
8751 if (is_eh
8752 ? c_id == 0
8753 : ((off_size == 4 && c_id == DW_CIE_ID)
8754 || (off_size == 8 && c_id == DW64_CIE_ID)))
8756 int version;
8757 unsigned int mreg;
8759 read_cie (cie_scan, cie_end, &cie, &version,
8760 &augmentation_data_len, &augmentation_data);
8761 /* PR 17512: file: 3450-2098-0.004. */
8762 if (cie == NULL)
8764 warn (_("Failed to read CIE information\n"));
8765 break;
8767 cie->next = forward_refs;
8768 forward_refs = cie;
8769 cie->chunk_start = look_for;
8770 mreg = max_regs > 0 ? max_regs - 1 : 0;
8771 if (mreg < cie->ra)
8772 mreg = cie->ra;
8773 if (frame_need_space (cie, mreg) < 0)
8775 warn (_("Invalid max register\n"));
8776 break;
8778 if (cie->fde_encoding)
8779 encoded_ptr_size
8780 = size_of_encoded_value (cie->fde_encoding);
8786 fc = &fde_fc;
8787 memset (fc, 0, sizeof (Frame_Chunk));
8789 if (!cie)
8791 fc->ncols = 0;
8792 fc->col_type = (short int *) xmalloc (sizeof (short int));
8793 fc->col_offset = (int *) xmalloc (sizeof (int));
8794 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1 : 0) < 0)
8796 warn (_("Invalid max register\n"));
8797 break;
8799 cie = fc;
8800 fc->augmentation = "";
8801 fc->fde_encoding = 0;
8802 fc->ptr_size = eh_addr_size;
8803 fc->segment_size = 0;
8805 else
8807 fc->ncols = cie->ncols;
8808 fc->col_type = (short int *) xcmalloc (fc->ncols, sizeof (short int));
8809 fc->col_offset = (int *) xcmalloc (fc->ncols, sizeof (int));
8810 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
8811 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
8812 fc->augmentation = cie->augmentation;
8813 fc->ptr_size = cie->ptr_size;
8814 eh_addr_size = cie->ptr_size;
8815 fc->segment_size = cie->segment_size;
8816 fc->code_factor = cie->code_factor;
8817 fc->data_factor = cie->data_factor;
8818 fc->cfa_reg = cie->cfa_reg;
8819 fc->cfa_offset = cie->cfa_offset;
8820 fc->ra = cie->ra;
8821 if (frame_need_space (fc, max_regs > 0 ? max_regs - 1: 0) < 0)
8823 warn (_("Invalid max register\n"));
8824 break;
8826 fc->fde_encoding = cie->fde_encoding;
8829 if (fc->fde_encoding)
8830 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
8832 segment_selector = 0;
8833 if (fc->segment_size)
8835 if (fc->segment_size > sizeof (segment_selector))
8837 /* PR 17512: file: 9e196b3e. */
8838 warn (_("Probably corrupt segment size: %d - using 4 instead\n"), fc->segment_size);
8839 fc->segment_size = 4;
8841 SAFE_BYTE_GET_AND_INC (segment_selector, start,
8842 fc->segment_size, block_end);
8845 fc->pc_begin = get_encoded_value (&start, fc->fde_encoding, section,
8846 block_end);
8848 /* FIXME: It appears that sometimes the final pc_range value is
8849 encoded in less than encoded_ptr_size bytes. See the x86_64
8850 run of the "objcopy on compressed debug sections" test for an
8851 example of this. */
8852 SAFE_BYTE_GET_AND_INC (fc->pc_range, start, encoded_ptr_size,
8853 block_end);
8855 if (cie->augmentation[0] == 'z')
8857 READ_ULEB (augmentation_data_len, start, block_end);
8858 augmentation_data = start;
8859 /* PR 17512 file: 722-8446-0.004 and PR 22386. */
8860 if (augmentation_data_len > (bfd_size_type) (block_end - start))
8862 warn (_("Augmentation data too long: 0x%s, "
8863 "expected at most %#lx\n"),
8864 dwarf_vmatoa ("x", augmentation_data_len),
8865 (unsigned long) (block_end - start));
8866 start = block_end;
8867 augmentation_data = NULL;
8868 augmentation_data_len = 0;
8870 start += augmentation_data_len;
8873 printf ("\n%08lx %s %s FDE ",
8874 (unsigned long)(saved_start - section_start),
8875 dwarf_vmatoa_1 (NULL, length, fc->ptr_size),
8876 dwarf_vmatoa_1 (NULL, cie_id, offset_size));
8878 if (cie->chunk_start)
8879 printf ("cie=%08lx",
8880 (unsigned long) (cie->chunk_start - section_start));
8881 else
8882 /* Ideally translate "invalid " to 8 chars, trailing space
8883 is optional. */
8884 printf (_("cie=invalid "));
8886 printf (" pc=");
8887 if (fc->segment_size)
8888 printf ("%04lx:", segment_selector);
8890 printf ("%s..%s\n",
8891 dwarf_vmatoa_1 (NULL, fc->pc_begin, fc->ptr_size),
8892 dwarf_vmatoa_1 (NULL, fc->pc_begin + fc->pc_range, fc->ptr_size));
8894 if (! do_debug_frames_interp && augmentation_data_len)
8896 display_augmentation_data (augmentation_data, augmentation_data_len);
8897 putchar ('\n');
8901 /* At this point, fc is the current chunk, cie (if any) is set, and
8902 we're about to interpret instructions for the chunk. */
8903 /* ??? At present we need to do this always, since this sizes the
8904 fc->col_type and fc->col_offset arrays, which we write into always.
8905 We should probably split the interpreted and non-interpreted bits
8906 into two different routines, since there's so much that doesn't
8907 really overlap between them. */
8908 if (1 || do_debug_frames_interp)
8910 /* Start by making a pass over the chunk, allocating storage
8911 and taking note of what registers are used. */
8912 unsigned char *tmp = start;
8914 while (start < block_end)
8916 unsigned int reg, op, opa;
8917 unsigned long temp;
8919 op = *start++;
8920 opa = op & 0x3f;
8921 if (op & 0xc0)
8922 op &= 0xc0;
8924 /* Warning: if you add any more cases to this switch, be
8925 sure to add them to the corresponding switch below. */
8926 switch (op)
8928 case DW_CFA_advance_loc:
8929 break;
8930 case DW_CFA_offset:
8931 SKIP_ULEB (start, block_end);
8932 if (frame_need_space (fc, opa) >= 0)
8933 fc->col_type[opa] = DW_CFA_undefined;
8934 break;
8935 case DW_CFA_restore:
8936 if (frame_need_space (fc, opa) >= 0)
8937 fc->col_type[opa] = DW_CFA_undefined;
8938 break;
8939 case DW_CFA_set_loc:
8940 if ((size_t) (block_end - start) < encoded_ptr_size)
8941 start = block_end;
8942 else
8943 start += encoded_ptr_size;
8944 break;
8945 case DW_CFA_advance_loc1:
8946 if ((size_t) (block_end - start) < 1)
8947 start = block_end;
8948 else
8949 start += 1;
8950 break;
8951 case DW_CFA_advance_loc2:
8952 if ((size_t) (block_end - start) < 2)
8953 start = block_end;
8954 else
8955 start += 2;
8956 break;
8957 case DW_CFA_advance_loc4:
8958 if ((size_t) (block_end - start) < 4)
8959 start = block_end;
8960 else
8961 start += 4;
8962 break;
8963 case DW_CFA_offset_extended:
8964 case DW_CFA_val_offset:
8965 READ_ULEB (reg, start, block_end);
8966 SKIP_ULEB (start, block_end);
8967 if (frame_need_space (fc, reg) >= 0)
8968 fc->col_type[reg] = DW_CFA_undefined;
8969 break;
8970 case DW_CFA_restore_extended:
8971 READ_ULEB (reg, start, block_end);
8972 if (frame_need_space (fc, reg) >= 0)
8973 fc->col_type[reg] = DW_CFA_undefined;
8974 break;
8975 case DW_CFA_undefined:
8976 READ_ULEB (reg, start, block_end);
8977 if (frame_need_space (fc, reg) >= 0)
8978 fc->col_type[reg] = DW_CFA_undefined;
8979 break;
8980 case DW_CFA_same_value:
8981 READ_ULEB (reg, start, block_end);
8982 if (frame_need_space (fc, reg) >= 0)
8983 fc->col_type[reg] = DW_CFA_undefined;
8984 break;
8985 case DW_CFA_register:
8986 READ_ULEB (reg, start, block_end);
8987 SKIP_ULEB (start, block_end);
8988 if (frame_need_space (fc, reg) >= 0)
8989 fc->col_type[reg] = DW_CFA_undefined;
8990 break;
8991 case DW_CFA_def_cfa:
8992 SKIP_ULEB (start, block_end);
8993 SKIP_ULEB (start, block_end);
8994 break;
8995 case DW_CFA_def_cfa_register:
8996 SKIP_ULEB (start, block_end);
8997 break;
8998 case DW_CFA_def_cfa_offset:
8999 SKIP_ULEB (start, block_end);
9000 break;
9001 case DW_CFA_def_cfa_expression:
9002 READ_ULEB (temp, start, block_end);
9003 if ((size_t) (block_end - start) < temp)
9004 start = block_end;
9005 else
9006 start += temp;
9007 break;
9008 case DW_CFA_expression:
9009 case DW_CFA_val_expression:
9010 READ_ULEB (reg, start, block_end);
9011 READ_ULEB (temp, start, block_end);
9012 if ((size_t) (block_end - start) < temp)
9013 start = block_end;
9014 else
9015 start += temp;
9016 if (frame_need_space (fc, reg) >= 0)
9017 fc->col_type[reg] = DW_CFA_undefined;
9018 break;
9019 case DW_CFA_offset_extended_sf:
9020 case DW_CFA_val_offset_sf:
9021 READ_ULEB (reg, start, block_end);
9022 SKIP_SLEB (start, block_end);
9023 if (frame_need_space (fc, reg) >= 0)
9024 fc->col_type[reg] = DW_CFA_undefined;
9025 break;
9026 case DW_CFA_def_cfa_sf:
9027 SKIP_ULEB (start, block_end);
9028 SKIP_SLEB (start, block_end);
9029 break;
9030 case DW_CFA_def_cfa_offset_sf:
9031 SKIP_SLEB (start, block_end);
9032 break;
9033 case DW_CFA_MIPS_advance_loc8:
9034 if ((size_t) (block_end - start) < 8)
9035 start = block_end;
9036 else
9037 start += 8;
9038 break;
9039 case DW_CFA_GNU_args_size:
9040 SKIP_ULEB (start, block_end);
9041 break;
9042 case DW_CFA_GNU_negative_offset_extended:
9043 READ_ULEB (reg, start, block_end);
9044 SKIP_ULEB (start, block_end);
9045 if (frame_need_space (fc, reg) >= 0)
9046 fc->col_type[reg] = DW_CFA_undefined;
9047 break;
9048 default:
9049 break;
9052 start = tmp;
9055 all_nops = true;
9057 /* Now we know what registers are used, make a second pass over
9058 the chunk, this time actually printing out the info. */
9060 while (start < block_end)
9062 unsigned op, opa;
9063 unsigned long ul, roffs;
9064 /* Note: It is tempting to use an unsigned long for 'reg' but there
9065 are various functions, notably frame_space_needed() that assume that
9066 reg is an unsigned int. */
9067 unsigned int reg;
9068 dwarf_signed_vma l;
9069 dwarf_vma ofs;
9070 dwarf_vma vma;
9071 const char *reg_prefix = "";
9073 op = *start++;
9074 opa = op & 0x3f;
9075 if (op & 0xc0)
9076 op &= 0xc0;
9078 /* Make a note if something other than DW_CFA_nop happens. */
9079 if (op != DW_CFA_nop)
9080 all_nops = false;
9082 /* Warning: if you add any more cases to this switch, be
9083 sure to add them to the corresponding switch above. */
9084 switch (op)
9086 case DW_CFA_advance_loc:
9087 if (do_debug_frames_interp)
9088 frame_display_row (fc, &need_col_headers, &max_regs);
9089 else
9090 printf (" DW_CFA_advance_loc: %d to %s\n",
9091 opa * fc->code_factor,
9092 dwarf_vmatoa_1 (NULL,
9093 fc->pc_begin + opa * fc->code_factor,
9094 fc->ptr_size));
9095 fc->pc_begin += opa * fc->code_factor;
9096 break;
9098 case DW_CFA_offset:
9099 READ_ULEB (roffs, start, block_end);
9100 if (opa >= (unsigned int) fc->ncols)
9101 reg_prefix = bad_reg;
9102 if (! do_debug_frames_interp || *reg_prefix != '\0')
9103 printf (" DW_CFA_offset: %s%s at cfa%+ld\n",
9104 reg_prefix, regname (opa, 0),
9105 roffs * fc->data_factor);
9106 if (*reg_prefix == '\0')
9108 fc->col_type[opa] = DW_CFA_offset;
9109 fc->col_offset[opa] = roffs * fc->data_factor;
9111 break;
9113 case DW_CFA_restore:
9114 if (opa >= (unsigned int) fc->ncols)
9115 reg_prefix = bad_reg;
9116 if (! do_debug_frames_interp || *reg_prefix != '\0')
9117 printf (" DW_CFA_restore: %s%s\n",
9118 reg_prefix, regname (opa, 0));
9119 if (*reg_prefix != '\0')
9120 break;
9122 if (opa >= (unsigned int) cie->ncols
9123 || (do_debug_frames_interp
9124 && cie->col_type[opa] == DW_CFA_unreferenced))
9126 fc->col_type[opa] = DW_CFA_undefined;
9127 fc->col_offset[opa] = 0;
9129 else
9131 fc->col_type[opa] = cie->col_type[opa];
9132 fc->col_offset[opa] = cie->col_offset[opa];
9134 break;
9136 case DW_CFA_set_loc:
9137 vma = get_encoded_value (&start, fc->fde_encoding, section,
9138 block_end);
9139 if (do_debug_frames_interp)
9140 frame_display_row (fc, &need_col_headers, &max_regs);
9141 else
9142 printf (" DW_CFA_set_loc: %s\n",
9143 dwarf_vmatoa_1 (NULL, vma, fc->ptr_size));
9144 fc->pc_begin = vma;
9145 break;
9147 case DW_CFA_advance_loc1:
9148 SAFE_BYTE_GET_AND_INC (ofs, start, 1, block_end);
9149 if (do_debug_frames_interp)
9150 frame_display_row (fc, &need_col_headers, &max_regs);
9151 else
9152 printf (" DW_CFA_advance_loc1: %ld to %s\n",
9153 (unsigned long) (ofs * fc->code_factor),
9154 dwarf_vmatoa_1 (NULL,
9155 fc->pc_begin + ofs * fc->code_factor,
9156 fc->ptr_size));
9157 fc->pc_begin += ofs * fc->code_factor;
9158 break;
9160 case DW_CFA_advance_loc2:
9161 SAFE_BYTE_GET_AND_INC (ofs, start, 2, block_end);
9162 if (do_debug_frames_interp)
9163 frame_display_row (fc, &need_col_headers, &max_regs);
9164 else
9165 printf (" DW_CFA_advance_loc2: %ld to %s\n",
9166 (unsigned long) (ofs * fc->code_factor),
9167 dwarf_vmatoa_1 (NULL,
9168 fc->pc_begin + ofs * fc->code_factor,
9169 fc->ptr_size));
9170 fc->pc_begin += ofs * fc->code_factor;
9171 break;
9173 case DW_CFA_advance_loc4:
9174 SAFE_BYTE_GET_AND_INC (ofs, start, 4, block_end);
9175 if (do_debug_frames_interp)
9176 frame_display_row (fc, &need_col_headers, &max_regs);
9177 else
9178 printf (" DW_CFA_advance_loc4: %ld to %s\n",
9179 (unsigned long) (ofs * fc->code_factor),
9180 dwarf_vmatoa_1 (NULL,
9181 fc->pc_begin + ofs * fc->code_factor,
9182 fc->ptr_size));
9183 fc->pc_begin += ofs * fc->code_factor;
9184 break;
9186 case DW_CFA_offset_extended:
9187 READ_ULEB (reg, start, block_end);
9188 READ_ULEB (roffs, start, block_end);
9189 if (reg >= (unsigned int) fc->ncols)
9190 reg_prefix = bad_reg;
9191 if (! do_debug_frames_interp || *reg_prefix != '\0')
9192 printf (" DW_CFA_offset_extended: %s%s at cfa%+ld\n",
9193 reg_prefix, regname (reg, 0),
9194 roffs * fc->data_factor);
9195 if (*reg_prefix == '\0')
9197 fc->col_type[reg] = DW_CFA_offset;
9198 fc->col_offset[reg] = roffs * fc->data_factor;
9200 break;
9202 case DW_CFA_val_offset:
9203 READ_ULEB (reg, start, block_end);
9204 READ_ULEB (roffs, start, block_end);
9205 if (reg >= (unsigned int) fc->ncols)
9206 reg_prefix = bad_reg;
9207 if (! do_debug_frames_interp || *reg_prefix != '\0')
9208 printf (" DW_CFA_val_offset: %s%s is cfa%+ld\n",
9209 reg_prefix, regname (reg, 0),
9210 roffs * fc->data_factor);
9211 if (*reg_prefix == '\0')
9213 fc->col_type[reg] = DW_CFA_val_offset;
9214 fc->col_offset[reg] = roffs * fc->data_factor;
9216 break;
9218 case DW_CFA_restore_extended:
9219 READ_ULEB (reg, start, block_end);
9220 if (reg >= (unsigned int) fc->ncols)
9221 reg_prefix = bad_reg;
9222 if (! do_debug_frames_interp || *reg_prefix != '\0')
9223 printf (" DW_CFA_restore_extended: %s%s\n",
9224 reg_prefix, regname (reg, 0));
9225 if (*reg_prefix != '\0')
9226 break;
9228 if (reg >= (unsigned int) cie->ncols)
9230 fc->col_type[reg] = DW_CFA_undefined;
9231 fc->col_offset[reg] = 0;
9233 else
9235 fc->col_type[reg] = cie->col_type[reg];
9236 fc->col_offset[reg] = cie->col_offset[reg];
9238 break;
9240 case DW_CFA_undefined:
9241 READ_ULEB (reg, start, block_end);
9242 if (reg >= (unsigned int) fc->ncols)
9243 reg_prefix = bad_reg;
9244 if (! do_debug_frames_interp || *reg_prefix != '\0')
9245 printf (" DW_CFA_undefined: %s%s\n",
9246 reg_prefix, regname (reg, 0));
9247 if (*reg_prefix == '\0')
9249 fc->col_type[reg] = DW_CFA_undefined;
9250 fc->col_offset[reg] = 0;
9252 break;
9254 case DW_CFA_same_value:
9255 READ_ULEB (reg, start, block_end);
9256 if (reg >= (unsigned int) fc->ncols)
9257 reg_prefix = bad_reg;
9258 if (! do_debug_frames_interp || *reg_prefix != '\0')
9259 printf (" DW_CFA_same_value: %s%s\n",
9260 reg_prefix, regname (reg, 0));
9261 if (*reg_prefix == '\0')
9263 fc->col_type[reg] = DW_CFA_same_value;
9264 fc->col_offset[reg] = 0;
9266 break;
9268 case DW_CFA_register:
9269 READ_ULEB (reg, start, block_end);
9270 READ_ULEB (roffs, start, block_end);
9271 if (reg >= (unsigned int) fc->ncols)
9272 reg_prefix = bad_reg;
9273 if (! do_debug_frames_interp || *reg_prefix != '\0')
9275 printf (" DW_CFA_register: %s%s in ",
9276 reg_prefix, regname (reg, 0));
9277 puts (regname (roffs, 0));
9279 if (*reg_prefix == '\0')
9281 fc->col_type[reg] = DW_CFA_register;
9282 fc->col_offset[reg] = roffs;
9284 break;
9286 case DW_CFA_remember_state:
9287 if (! do_debug_frames_interp)
9288 printf (" DW_CFA_remember_state\n");
9289 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9290 rs->cfa_offset = fc->cfa_offset;
9291 rs->cfa_reg = fc->cfa_reg;
9292 rs->ra = fc->ra;
9293 rs->cfa_exp = fc->cfa_exp;
9294 rs->ncols = fc->ncols;
9295 rs->col_type = (short int *) xcmalloc (rs->ncols,
9296 sizeof (* rs->col_type));
9297 rs->col_offset = (int *) xcmalloc (rs->ncols, sizeof (* rs->col_offset));
9298 memcpy (rs->col_type, fc->col_type, rs->ncols * sizeof (* fc->col_type));
9299 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (* fc->col_offset));
9300 rs->next = remembered_state;
9301 remembered_state = rs;
9302 break;
9304 case DW_CFA_restore_state:
9305 if (! do_debug_frames_interp)
9306 printf (" DW_CFA_restore_state\n");
9307 rs = remembered_state;
9308 if (rs)
9310 remembered_state = rs->next;
9311 fc->cfa_offset = rs->cfa_offset;
9312 fc->cfa_reg = rs->cfa_reg;
9313 fc->ra = rs->ra;
9314 fc->cfa_exp = rs->cfa_exp;
9315 if (frame_need_space (fc, rs->ncols - 1) < 0)
9317 warn (_("Invalid column number in saved frame state\n"));
9318 fc->ncols = 0;
9319 break;
9321 memcpy (fc->col_type, rs->col_type, rs->ncols * sizeof (* rs->col_type));
9322 memcpy (fc->col_offset, rs->col_offset,
9323 rs->ncols * sizeof (* rs->col_offset));
9324 free (rs->col_type);
9325 free (rs->col_offset);
9326 free (rs);
9328 else if (do_debug_frames_interp)
9329 printf ("Mismatched DW_CFA_restore_state\n");
9330 break;
9332 case DW_CFA_def_cfa:
9333 READ_ULEB (fc->cfa_reg, start, block_end);
9334 READ_ULEB (fc->cfa_offset, start, block_end);
9335 fc->cfa_exp = 0;
9336 if (! do_debug_frames_interp)
9337 printf (" DW_CFA_def_cfa: %s ofs %d\n",
9338 regname (fc->cfa_reg, 0), (int) fc->cfa_offset);
9339 break;
9341 case DW_CFA_def_cfa_register:
9342 READ_ULEB (fc->cfa_reg, start, block_end);
9343 fc->cfa_exp = 0;
9344 if (! do_debug_frames_interp)
9345 printf (" DW_CFA_def_cfa_register: %s\n",
9346 regname (fc->cfa_reg, 0));
9347 break;
9349 case DW_CFA_def_cfa_offset:
9350 READ_ULEB (fc->cfa_offset, start, block_end);
9351 if (! do_debug_frames_interp)
9352 printf (" DW_CFA_def_cfa_offset: %d\n", (int) fc->cfa_offset);
9353 break;
9355 case DW_CFA_nop:
9356 if (! do_debug_frames_interp)
9357 printf (" DW_CFA_nop\n");
9358 break;
9360 case DW_CFA_def_cfa_expression:
9361 READ_ULEB (ul, start, block_end);
9362 if (ul > (size_t) (block_end - start))
9364 printf (_(" DW_CFA_def_cfa_expression: <corrupt len %lu>\n"), ul);
9365 break;
9367 if (! do_debug_frames_interp)
9369 printf (" DW_CFA_def_cfa_expression (");
9370 decode_location_expression (start, eh_addr_size, 0, -1,
9371 ul, 0, section);
9372 printf (")\n");
9374 fc->cfa_exp = 1;
9375 start += ul;
9376 break;
9378 case DW_CFA_expression:
9379 READ_ULEB (reg, start, block_end);
9380 READ_ULEB (ul, start, block_end);
9381 if (reg >= (unsigned int) fc->ncols)
9382 reg_prefix = bad_reg;
9383 /* PR 17512: file: 069-133014-0.006. */
9384 /* PR 17512: file: 98c02eb4. */
9385 if (ul > (size_t) (block_end - start))
9387 printf (_(" DW_CFA_expression: <corrupt len %lu>\n"), ul);
9388 break;
9390 if (! do_debug_frames_interp || *reg_prefix != '\0')
9392 printf (" DW_CFA_expression: %s%s (",
9393 reg_prefix, regname (reg, 0));
9394 decode_location_expression (start, eh_addr_size, 0, -1,
9395 ul, 0, section);
9396 printf (")\n");
9398 if (*reg_prefix == '\0')
9399 fc->col_type[reg] = DW_CFA_expression;
9400 start += ul;
9401 break;
9403 case DW_CFA_val_expression:
9404 READ_ULEB (reg, start, block_end);
9405 READ_ULEB (ul, start, block_end);
9406 if (reg >= (unsigned int) fc->ncols)
9407 reg_prefix = bad_reg;
9408 if (ul > (size_t) (block_end - start))
9410 printf (" DW_CFA_val_expression: <corrupt len %lu>\n", ul);
9411 break;
9413 if (! do_debug_frames_interp || *reg_prefix != '\0')
9415 printf (" DW_CFA_val_expression: %s%s (",
9416 reg_prefix, regname (reg, 0));
9417 decode_location_expression (start, eh_addr_size, 0, -1,
9418 ul, 0, section);
9419 printf (")\n");
9421 if (*reg_prefix == '\0')
9422 fc->col_type[reg] = DW_CFA_val_expression;
9423 start += ul;
9424 break;
9426 case DW_CFA_offset_extended_sf:
9427 READ_ULEB (reg, start, block_end);
9428 READ_SLEB (l, start, block_end);
9429 if (frame_need_space (fc, reg) < 0)
9430 reg_prefix = bad_reg;
9431 if (! do_debug_frames_interp || *reg_prefix != '\0')
9432 printf (" DW_CFA_offset_extended_sf: %s%s at cfa%+ld\n",
9433 reg_prefix, regname (reg, 0),
9434 (long)(l * fc->data_factor));
9435 if (*reg_prefix == '\0')
9437 fc->col_type[reg] = DW_CFA_offset;
9438 fc->col_offset[reg] = l * fc->data_factor;
9440 break;
9442 case DW_CFA_val_offset_sf:
9443 READ_ULEB (reg, start, block_end);
9444 READ_SLEB (l, start, block_end);
9445 if (frame_need_space (fc, reg) < 0)
9446 reg_prefix = bad_reg;
9447 if (! do_debug_frames_interp || *reg_prefix != '\0')
9448 printf (" DW_CFA_val_offset_sf: %s%s is cfa%+ld\n",
9449 reg_prefix, regname (reg, 0),
9450 (long)(l * fc->data_factor));
9451 if (*reg_prefix == '\0')
9453 fc->col_type[reg] = DW_CFA_val_offset;
9454 fc->col_offset[reg] = l * fc->data_factor;
9456 break;
9458 case DW_CFA_def_cfa_sf:
9459 READ_ULEB (fc->cfa_reg, start, block_end);
9460 READ_SLEB (l, start, block_end);
9461 l *= fc->data_factor;
9462 fc->cfa_offset = l;
9463 fc->cfa_exp = 0;
9464 if (! do_debug_frames_interp)
9465 printf (" DW_CFA_def_cfa_sf: %s ofs %ld\n",
9466 regname (fc->cfa_reg, 0), (long) l);
9467 break;
9469 case DW_CFA_def_cfa_offset_sf:
9470 READ_SLEB (l, start, block_end);
9471 l *= fc->data_factor;
9472 fc->cfa_offset = l;
9473 if (! do_debug_frames_interp)
9474 printf (" DW_CFA_def_cfa_offset_sf: %ld\n", (long) l);
9475 break;
9477 case DW_CFA_MIPS_advance_loc8:
9478 SAFE_BYTE_GET_AND_INC (ofs, start, 8, block_end);
9479 if (do_debug_frames_interp)
9480 frame_display_row (fc, &need_col_headers, &max_regs);
9481 else
9482 printf (" DW_CFA_MIPS_advance_loc8: %ld to %s\n",
9483 (unsigned long) (ofs * fc->code_factor),
9484 dwarf_vmatoa_1 (NULL,
9485 fc->pc_begin + ofs * fc->code_factor,
9486 fc->ptr_size));
9487 fc->pc_begin += ofs * fc->code_factor;
9488 break;
9490 case DW_CFA_GNU_window_save:
9491 if (! do_debug_frames_interp)
9492 printf (" DW_CFA_GNU_window_save\n");
9493 break;
9495 case DW_CFA_GNU_args_size:
9496 READ_ULEB (ul, start, block_end);
9497 if (! do_debug_frames_interp)
9498 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
9499 break;
9501 case DW_CFA_GNU_negative_offset_extended:
9502 READ_ULEB (reg, start, block_end);
9503 READ_SLEB (l, start, block_end);
9504 l = - l;
9505 if (frame_need_space (fc, reg) < 0)
9506 reg_prefix = bad_reg;
9507 if (! do_debug_frames_interp || *reg_prefix != '\0')
9508 printf (" DW_CFA_GNU_negative_offset_extended: %s%s at cfa%+ld\n",
9509 reg_prefix, regname (reg, 0),
9510 (long)(l * fc->data_factor));
9511 if (*reg_prefix == '\0')
9513 fc->col_type[reg] = DW_CFA_offset;
9514 fc->col_offset[reg] = l * fc->data_factor;
9516 break;
9518 default:
9519 if (op >= DW_CFA_lo_user && op <= DW_CFA_hi_user)
9520 printf (_(" DW_CFA_??? (User defined call frame op: %#x)\n"), op);
9521 else
9522 warn (_("Unsupported or unknown Dwarf Call Frame Instruction number: %#x\n"), op);
9523 start = block_end;
9527 /* Interpret the CFA - as long as it is not completely full of NOPs. */
9528 if (do_debug_frames_interp && ! all_nops)
9529 frame_display_row (fc, &need_col_headers, &max_regs);
9531 if (fde_fc.col_type != NULL)
9533 free (fde_fc.col_type);
9534 fde_fc.col_type = NULL;
9536 if (fde_fc.col_offset != NULL)
9538 free (fde_fc.col_offset);
9539 fde_fc.col_offset = NULL;
9542 start = block_end;
9543 eh_addr_size = saved_eh_addr_size;
9546 printf ("\n");
9548 while (remembered_state != NULL)
9550 rs = remembered_state;
9551 remembered_state = rs->next;
9552 free (rs->col_type);
9553 free (rs->col_offset);
9554 rs->next = NULL; /* Paranoia. */
9555 free (rs);
9558 while (chunks != NULL)
9560 rs = chunks;
9561 chunks = rs->next;
9562 free (rs->col_type);
9563 free (rs->col_offset);
9564 rs->next = NULL; /* Paranoia. */
9565 free (rs);
9568 while (forward_refs != NULL)
9570 rs = forward_refs;
9571 forward_refs = rs->next;
9572 free (rs->col_type);
9573 free (rs->col_offset);
9574 rs->next = NULL; /* Paranoia. */
9575 free (rs);
9578 return 1;
9581 #undef GET
9583 static int
9584 display_debug_names (struct dwarf_section *section, void *file)
9586 unsigned char *hdrptr = section->start;
9587 dwarf_vma unit_length;
9588 unsigned char *unit_start;
9589 const unsigned char *const section_end = section->start + section->size;
9590 unsigned char *unit_end;
9592 introduce (section, false);
9594 load_debug_section_with_follow (str, file);
9596 for (; hdrptr < section_end; hdrptr = unit_end)
9598 unsigned int offset_size;
9599 uint16_t dwarf_version, padding;
9600 uint32_t comp_unit_count, local_type_unit_count, foreign_type_unit_count;
9601 uint64_t bucket_count, name_count, abbrev_table_size;
9602 uint32_t augmentation_string_size;
9603 unsigned int i;
9604 bool augmentation_printable;
9605 const char *augmentation_string;
9606 size_t total;
9608 unit_start = hdrptr;
9610 /* Get and check the length of the block. */
9611 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 4, section_end);
9613 if (unit_length == 0xffffffff)
9615 /* This section is 64-bit DWARF. */
9616 SAFE_BYTE_GET_AND_INC (unit_length, hdrptr, 8, section_end);
9617 offset_size = 8;
9619 else
9620 offset_size = 4;
9622 if (unit_length > (size_t) (section_end - hdrptr)
9623 || unit_length < 2 + 2 + 4 * 7)
9625 too_short:
9626 warn (_("Debug info is corrupted, %s header at %#lx has length %s\n"),
9627 section->name,
9628 (unsigned long) (unit_start - section->start),
9629 dwarf_vmatoa ("x", unit_length));
9630 return 0;
9632 unit_end = hdrptr + unit_length;
9634 /* Get and check the version number. */
9635 SAFE_BYTE_GET_AND_INC (dwarf_version, hdrptr, 2, unit_end);
9636 printf (_("Version %ld\n"), (long) dwarf_version);
9638 /* Prior versions did not exist, and future versions may not be
9639 backwards compatible. */
9640 if (dwarf_version != 5)
9642 warn (_("Only DWARF version 5 .debug_names "
9643 "is currently supported.\n"));
9644 return 0;
9647 SAFE_BYTE_GET_AND_INC (padding, hdrptr, 2, unit_end);
9648 if (padding != 0)
9649 warn (_("Padding field of .debug_names must be 0 (found 0x%x)\n"),
9650 padding);
9652 SAFE_BYTE_GET_AND_INC (comp_unit_count, hdrptr, 4, unit_end);
9653 if (comp_unit_count == 0)
9654 warn (_("Compilation unit count must be >= 1 in .debug_names\n"));
9656 SAFE_BYTE_GET_AND_INC (local_type_unit_count, hdrptr, 4, unit_end);
9657 SAFE_BYTE_GET_AND_INC (foreign_type_unit_count, hdrptr, 4, unit_end);
9658 SAFE_BYTE_GET_AND_INC (bucket_count, hdrptr, 4, unit_end);
9659 SAFE_BYTE_GET_AND_INC (name_count, hdrptr, 4, unit_end);
9660 SAFE_BYTE_GET_AND_INC (abbrev_table_size, hdrptr, 4, unit_end);
9662 SAFE_BYTE_GET_AND_INC (augmentation_string_size, hdrptr, 4, unit_end);
9663 if (augmentation_string_size % 4 != 0)
9665 warn (_("Augmentation string length %u must be rounded up "
9666 "to a multiple of 4 in .debug_names.\n"),
9667 augmentation_string_size);
9668 augmentation_string_size += (-augmentation_string_size) & 3;
9670 if (augmentation_string_size > (size_t) (unit_end - hdrptr))
9671 goto too_short;
9673 printf (_("Augmentation string:"));
9675 augmentation_printable = true;
9676 augmentation_string = (const char *) hdrptr;
9678 for (i = 0; i < augmentation_string_size; i++)
9680 unsigned char uc;
9682 SAFE_BYTE_GET_AND_INC (uc, hdrptr, 1, unit_end);
9683 printf (" %02x", uc);
9685 if (uc != 0 && !ISPRINT (uc))
9686 augmentation_printable = false;
9689 if (augmentation_printable)
9691 printf (" (\"");
9692 for (i = 0;
9693 i < augmentation_string_size && augmentation_string[i];
9694 ++i)
9695 putchar (augmentation_string[i]);
9696 printf ("\")");
9698 putchar ('\n');
9700 printf (_("CU table:\n"));
9701 if (_mul_overflow (comp_unit_count, offset_size, &total)
9702 || total > (size_t) (unit_end - hdrptr))
9703 goto too_short;
9704 for (i = 0; i < comp_unit_count; i++)
9706 uint64_t cu_offset;
9708 SAFE_BYTE_GET_AND_INC (cu_offset, hdrptr, offset_size, unit_end);
9709 printf (_("[%3u] 0x%lx\n"), i, (unsigned long) cu_offset);
9711 putchar ('\n');
9713 printf (_("TU table:\n"));
9714 if (_mul_overflow (local_type_unit_count, offset_size, &total)
9715 || total > (size_t) (unit_end - hdrptr))
9716 goto too_short;
9717 for (i = 0; i < local_type_unit_count; i++)
9719 uint64_t tu_offset;
9721 SAFE_BYTE_GET_AND_INC (tu_offset, hdrptr, offset_size, unit_end);
9722 printf (_("[%3u] 0x%lx\n"), i, (unsigned long) tu_offset);
9724 putchar ('\n');
9726 printf (_("Foreign TU table:\n"));
9727 if (_mul_overflow (foreign_type_unit_count, 8, &total)
9728 || total > (size_t) (unit_end - hdrptr))
9729 goto too_short;
9730 for (i = 0; i < foreign_type_unit_count; i++)
9732 uint64_t signature;
9734 SAFE_BYTE_GET_AND_INC (signature, hdrptr, 8, unit_end);
9735 printf (_("[%3u] "), i);
9736 print_dwarf_vma (signature, 8);
9737 putchar ('\n');
9739 putchar ('\n');
9741 uint64_t xtra = (bucket_count * sizeof (uint32_t)
9742 + name_count * (sizeof (uint32_t) + 2 * offset_size)
9743 + abbrev_table_size);
9744 if (xtra > (size_t) (unit_end - hdrptr))
9746 warn (_("Entry pool offset (0x%lx) exceeds unit size 0x%lx "
9747 "for unit 0x%lx in the debug_names\n"),
9748 (long) xtra,
9749 (long) (unit_end - unit_start),
9750 (long) (unit_start - section->start));
9751 return 0;
9753 const uint32_t *const hash_table_buckets = (uint32_t *) hdrptr;
9754 hdrptr += bucket_count * sizeof (uint32_t);
9755 const uint32_t *const hash_table_hashes = (uint32_t *) hdrptr;
9756 hdrptr += name_count * sizeof (uint32_t);
9757 unsigned char *const name_table_string_offsets = hdrptr;
9758 hdrptr += name_count * offset_size;
9759 unsigned char *const name_table_entry_offsets = hdrptr;
9760 hdrptr += name_count * offset_size;
9761 unsigned char *const abbrev_table = hdrptr;
9762 hdrptr += abbrev_table_size;
9763 const unsigned char *const abbrev_table_end = hdrptr;
9764 unsigned char *const entry_pool = hdrptr;
9766 size_t buckets_filled = 0;
9767 size_t bucketi;
9768 for (bucketi = 0; bucketi < bucket_count; bucketi++)
9770 const uint32_t bucket = hash_table_buckets[bucketi];
9772 if (bucket != 0)
9773 ++buckets_filled;
9775 printf (ngettext ("Used %zu of %lu bucket.\n",
9776 "Used %zu of %lu buckets.\n",
9777 bucket_count),
9778 buckets_filled, (unsigned long) bucket_count);
9780 uint32_t hash_prev = 0;
9781 size_t hash_clash_count = 0;
9782 size_t longest_clash = 0;
9783 size_t this_length = 0;
9784 size_t hashi;
9785 for (hashi = 0; hashi < name_count; hashi++)
9787 const uint32_t hash_this = hash_table_hashes[hashi];
9789 if (hashi > 0)
9791 if (hash_prev % bucket_count == hash_this % bucket_count)
9793 ++hash_clash_count;
9794 ++this_length;
9795 longest_clash = MAX (longest_clash, this_length);
9797 else
9798 this_length = 0;
9800 hash_prev = hash_this;
9802 printf (_("Out of %lu items there are %zu bucket clashes"
9803 " (longest of %zu entries).\n"),
9804 (unsigned long) name_count, hash_clash_count, longest_clash);
9805 assert (name_count == buckets_filled + hash_clash_count);
9807 struct abbrev_lookup_entry
9809 dwarf_vma abbrev_tag;
9810 unsigned char *abbrev_lookup_ptr;
9812 struct abbrev_lookup_entry *abbrev_lookup = NULL;
9813 size_t abbrev_lookup_used = 0;
9814 size_t abbrev_lookup_allocated = 0;
9816 unsigned char *abbrevptr = abbrev_table;
9817 for (;;)
9819 dwarf_vma abbrev_tag;
9821 READ_ULEB (abbrev_tag, abbrevptr, abbrev_table_end);
9822 if (abbrev_tag == 0)
9823 break;
9824 if (abbrev_lookup_used == abbrev_lookup_allocated)
9826 abbrev_lookup_allocated = MAX (0x100,
9827 abbrev_lookup_allocated * 2);
9828 abbrev_lookup = xrealloc (abbrev_lookup,
9829 (abbrev_lookup_allocated
9830 * sizeof (*abbrev_lookup)));
9832 assert (abbrev_lookup_used < abbrev_lookup_allocated);
9833 struct abbrev_lookup_entry *entry;
9834 for (entry = abbrev_lookup;
9835 entry < abbrev_lookup + abbrev_lookup_used;
9836 entry++)
9837 if (entry->abbrev_tag == abbrev_tag)
9839 warn (_("Duplicate abbreviation tag %lu "
9840 "in unit 0x%lx in the debug_names\n"),
9841 (long) abbrev_tag, (long) (unit_start - section->start));
9842 break;
9844 entry = &abbrev_lookup[abbrev_lookup_used++];
9845 entry->abbrev_tag = abbrev_tag;
9846 entry->abbrev_lookup_ptr = abbrevptr;
9848 /* Skip DWARF tag. */
9849 SKIP_ULEB (abbrevptr, abbrev_table_end);
9850 for (;;)
9852 dwarf_vma xindex, form;
9854 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
9855 READ_ULEB (form, abbrevptr, abbrev_table_end);
9856 if (xindex == 0 && form == 0)
9857 break;
9861 printf (_("\nSymbol table:\n"));
9862 uint32_t namei;
9863 for (namei = 0; namei < name_count; ++namei)
9865 uint64_t string_offset, entry_offset;
9866 unsigned char *p;
9868 p = name_table_string_offsets + namei * offset_size;
9869 SAFE_BYTE_GET (string_offset, p, offset_size, unit_end);
9870 p = name_table_entry_offsets + namei * offset_size;
9871 SAFE_BYTE_GET (entry_offset, p, offset_size, unit_end);
9873 printf ("[%3u] #%08x %s:", namei, hash_table_hashes[namei],
9874 fetch_indirect_string (string_offset));
9876 unsigned char *entryptr = entry_pool + entry_offset;
9878 /* We need to scan first whether there is a single or multiple
9879 entries. TAGNO is -2 for the first entry, it is -1 for the
9880 initial tag read of the second entry, then it becomes 0 for the
9881 first entry for real printing etc. */
9882 int tagno = -2;
9883 /* Initialize it due to a false compiler warning. */
9884 dwarf_vma second_abbrev_tag = -1;
9885 for (;;)
9887 dwarf_vma abbrev_tag;
9888 dwarf_vma dwarf_tag;
9889 const struct abbrev_lookup_entry *entry;
9891 READ_ULEB (abbrev_tag, entryptr, unit_end);
9892 if (tagno == -1)
9894 second_abbrev_tag = abbrev_tag;
9895 tagno = 0;
9896 entryptr = entry_pool + entry_offset;
9897 continue;
9899 if (abbrev_tag == 0)
9900 break;
9901 if (tagno >= 0)
9902 printf ("%s<%lu>",
9903 (tagno == 0 && second_abbrev_tag == 0 ? " " : "\n\t"),
9904 (unsigned long) abbrev_tag);
9906 for (entry = abbrev_lookup;
9907 entry < abbrev_lookup + abbrev_lookup_used;
9908 entry++)
9909 if (entry->abbrev_tag == abbrev_tag)
9910 break;
9911 if (entry >= abbrev_lookup + abbrev_lookup_used)
9913 warn (_("Undefined abbreviation tag %lu "
9914 "in unit 0x%lx in the debug_names\n"),
9915 (long) abbrev_tag,
9916 (long) (unit_start - section->start));
9917 break;
9919 abbrevptr = entry->abbrev_lookup_ptr;
9920 READ_ULEB (dwarf_tag, abbrevptr, abbrev_table_end);
9921 if (tagno >= 0)
9922 printf (" %s", get_TAG_name (dwarf_tag));
9923 for (;;)
9925 dwarf_vma xindex, form;
9927 READ_ULEB (xindex, abbrevptr, abbrev_table_end);
9928 READ_ULEB (form, abbrevptr, abbrev_table_end);
9929 if (xindex == 0 && form == 0)
9930 break;
9932 if (tagno >= 0)
9933 printf (" %s", get_IDX_name (xindex));
9934 entryptr = read_and_display_attr_value (0, form, 0,
9935 unit_start, entryptr, unit_end,
9936 0, 0, offset_size,
9937 dwarf_version, NULL,
9938 (tagno < 0), NULL,
9939 NULL, '=', -1);
9941 ++tagno;
9943 if (tagno <= 0)
9944 printf (_(" <no entries>"));
9945 putchar ('\n');
9948 free (abbrev_lookup);
9951 return 1;
9954 static int
9955 display_debug_links (struct dwarf_section * section,
9956 void * file ATTRIBUTE_UNUSED)
9958 const unsigned char * filename;
9959 unsigned int filelen;
9961 introduce (section, false);
9963 /* The .gnu_debuglink section is formatted as:
9964 (c-string) Filename.
9965 (padding) If needed to reach a 4 byte boundary.
9966 (uint32_t) CRC32 value.
9968 The .gun_debugaltlink section is formatted as:
9969 (c-string) Filename.
9970 (binary) Build-ID. */
9972 filename = section->start;
9973 filelen = strnlen ((const char *) filename, section->size);
9974 if (filelen == section->size)
9976 warn (_("The debuglink filename is corrupt/missing\n"));
9977 return 0;
9980 printf (_(" Separate debug info file: %s\n"), filename);
9982 if (startswith (section->name, ".gnu_debuglink"))
9984 unsigned int crc32;
9985 unsigned int crc_offset;
9987 crc_offset = filelen + 1;
9988 crc_offset = (crc_offset + 3) & ~3;
9989 if (crc_offset + 4 > section->size)
9991 warn (_("CRC offset missing/truncated\n"));
9992 return 0;
9995 crc32 = byte_get (filename + crc_offset, 4);
9997 printf (_(" CRC value: %#x\n"), crc32);
9999 if (crc_offset + 4 < section->size)
10001 warn (_("There are %#lx extraneous bytes at the end of the section\n"),
10002 (long)(section->size - (crc_offset + 4)));
10003 return 0;
10006 else /* startswith (section->name, ".gnu_debugaltlink") */
10008 const unsigned char * build_id = section->start + filelen + 1;
10009 bfd_size_type build_id_len = section->size - (filelen + 1);
10010 bfd_size_type printed;
10012 /* FIXME: Should we support smaller build-id notes ? */
10013 if (build_id_len < 0x14)
10015 warn (_("Build-ID is too short (%#lx bytes)\n"), (long) build_id_len);
10016 return 0;
10019 printed = printf (_(" Build-ID (%#lx bytes):"), (long) build_id_len);
10020 display_data (printed, build_id, build_id_len);
10021 putchar ('\n');
10024 putchar ('\n');
10025 return 1;
10028 static int
10029 display_gdb_index (struct dwarf_section *section,
10030 void *file ATTRIBUTE_UNUSED)
10032 unsigned char *start = section->start;
10033 uint32_t version;
10034 uint32_t cu_list_offset, tu_list_offset;
10035 uint32_t address_table_offset, symbol_table_offset, constant_pool_offset;
10036 unsigned int cu_list_elements, tu_list_elements;
10037 unsigned int address_table_size, symbol_table_slots;
10038 unsigned char *cu_list, *tu_list;
10039 unsigned char *address_table, *symbol_table, *constant_pool;
10040 unsigned int i;
10042 /* The documentation for the format of this file is in gdb/dwarf2read.c. */
10044 introduce (section, false);
10046 if (section->size < 6 * sizeof (uint32_t))
10048 warn (_("Truncated header in the %s section.\n"), section->name);
10049 return 0;
10052 version = byte_get_little_endian (start, 4);
10053 printf (_("Version %ld\n"), (long) version);
10055 /* Prior versions are obsolete, and future versions may not be
10056 backwards compatible. */
10057 if (version < 3 || version > 8)
10059 warn (_("Unsupported version %lu.\n"), (unsigned long) version);
10060 return 0;
10062 if (version < 4)
10063 warn (_("The address table data in version 3 may be wrong.\n"));
10064 if (version < 5)
10065 warn (_("Version 4 does not support case insensitive lookups.\n"));
10066 if (version < 6)
10067 warn (_("Version 5 does not include inlined functions.\n"));
10068 if (version < 7)
10069 warn (_("Version 6 does not include symbol attributes.\n"));
10070 /* Version 7 indices generated by Gold have bad type unit references,
10071 PR binutils/15021. But we don't know if the index was generated by
10072 Gold or not, so to avoid worrying users with gdb-generated indices
10073 we say nothing for version 7 here. */
10075 cu_list_offset = byte_get_little_endian (start + 4, 4);
10076 tu_list_offset = byte_get_little_endian (start + 8, 4);
10077 address_table_offset = byte_get_little_endian (start + 12, 4);
10078 symbol_table_offset = byte_get_little_endian (start + 16, 4);
10079 constant_pool_offset = byte_get_little_endian (start + 20, 4);
10081 if (cu_list_offset > section->size
10082 || tu_list_offset > section->size
10083 || address_table_offset > section->size
10084 || symbol_table_offset > section->size
10085 || constant_pool_offset > section->size)
10087 warn (_("Corrupt header in the %s section.\n"), section->name);
10088 return 0;
10091 /* PR 17531: file: 418d0a8a. */
10092 if (tu_list_offset < cu_list_offset)
10094 warn (_("TU offset (%x) is less than CU offset (%x)\n"),
10095 tu_list_offset, cu_list_offset);
10096 return 0;
10099 cu_list_elements = (tu_list_offset - cu_list_offset) / 8;
10101 if (address_table_offset < tu_list_offset)
10103 warn (_("Address table offset (%x) is less than TU offset (%x)\n"),
10104 address_table_offset, tu_list_offset);
10105 return 0;
10108 tu_list_elements = (address_table_offset - tu_list_offset) / 8;
10110 /* PR 17531: file: 18a47d3d. */
10111 if (symbol_table_offset < address_table_offset)
10113 warn (_("Symbol table offset (%x) is less then Address table offset (%x)\n"),
10114 symbol_table_offset, address_table_offset);
10115 return 0;
10118 address_table_size = symbol_table_offset - address_table_offset;
10120 if (constant_pool_offset < symbol_table_offset)
10122 warn (_("Constant pool offset (%x) is less than symbol table offset (%x)\n"),
10123 constant_pool_offset, symbol_table_offset);
10124 return 0;
10127 symbol_table_slots = (constant_pool_offset - symbol_table_offset) / 8;
10129 cu_list = start + cu_list_offset;
10130 tu_list = start + tu_list_offset;
10131 address_table = start + address_table_offset;
10132 symbol_table = start + symbol_table_offset;
10133 constant_pool = start + constant_pool_offset;
10135 if (address_table_offset + address_table_size > section->size)
10137 warn (_("Address table extends beyond end of section.\n"));
10138 return 0;
10141 printf (_("\nCU table:\n"));
10142 for (i = 0; i < cu_list_elements; i += 2)
10144 uint64_t cu_offset = byte_get_little_endian (cu_list + i * 8, 8);
10145 uint64_t cu_length = byte_get_little_endian (cu_list + i * 8 + 8, 8);
10147 printf (_("[%3u] 0x%lx - 0x%lx\n"), i / 2,
10148 (unsigned long) cu_offset,
10149 (unsigned long) (cu_offset + cu_length - 1));
10152 printf (_("\nTU table:\n"));
10153 for (i = 0; i < tu_list_elements; i += 3)
10155 uint64_t tu_offset = byte_get_little_endian (tu_list + i * 8, 8);
10156 uint64_t type_offset = byte_get_little_endian (tu_list + i * 8 + 8, 8);
10157 uint64_t signature = byte_get_little_endian (tu_list + i * 8 + 16, 8);
10159 printf (_("[%3u] 0x%lx 0x%lx "), i / 3,
10160 (unsigned long) tu_offset,
10161 (unsigned long) type_offset);
10162 print_dwarf_vma (signature, 8);
10163 printf ("\n");
10166 printf (_("\nAddress table:\n"));
10167 for (i = 0; i < address_table_size && i <= address_table_size - (2 * 8 + 4);
10168 i += 2 * 8 + 4)
10170 uint64_t low = byte_get_little_endian (address_table + i, 8);
10171 uint64_t high = byte_get_little_endian (address_table + i + 8, 8);
10172 uint32_t cu_index = byte_get_little_endian (address_table + i + 16, 4);
10174 print_dwarf_vma (low, 8);
10175 print_dwarf_vma (high, 8);
10176 printf (_("%lu\n"), (unsigned long) cu_index);
10179 printf (_("\nSymbol table:\n"));
10180 for (i = 0; i < symbol_table_slots; ++i)
10182 uint32_t name_offset = byte_get_little_endian (symbol_table + i * 8, 4);
10183 uint32_t cu_vector_offset = byte_get_little_endian (symbol_table + i * 8 + 4, 4);
10184 uint32_t num_cus, cu;
10186 if (name_offset != 0
10187 || cu_vector_offset != 0)
10189 unsigned int j;
10191 /* PR 17531: file: 5b7b07ad. */
10192 if (name_offset >= section->size - constant_pool_offset)
10194 printf (_("[%3u] <corrupt offset: %x>"), i, name_offset);
10195 warn (_("Corrupt name offset of 0x%x found for symbol table slot %d\n"),
10196 name_offset, i);
10198 else
10199 printf ("[%3u] %.*s:", i,
10200 (int) (section->size - (constant_pool_offset + name_offset)),
10201 constant_pool + name_offset);
10203 if (section->size - constant_pool_offset < 4
10204 || cu_vector_offset > section->size - constant_pool_offset - 4)
10206 printf (_("<invalid CU vector offset: %x>\n"), cu_vector_offset);
10207 warn (_("Corrupt CU vector offset of 0x%x found for symbol table slot %d\n"),
10208 cu_vector_offset, i);
10209 continue;
10212 num_cus = byte_get_little_endian (constant_pool + cu_vector_offset, 4);
10214 if ((uint64_t) num_cus * 4 > section->size - (constant_pool_offset
10215 + cu_vector_offset + 4))
10217 printf ("<invalid number of CUs: %d>\n", num_cus);
10218 warn (_("Invalid number of CUs (0x%x) for symbol table slot %d\n"),
10219 num_cus, i);
10220 continue;
10223 if (num_cus > 1)
10224 printf ("\n");
10226 for (j = 0; j < num_cus; ++j)
10228 int is_static;
10229 gdb_index_symbol_kind kind;
10231 cu = byte_get_little_endian (constant_pool + cu_vector_offset + 4 + j * 4, 4);
10232 is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu);
10233 kind = GDB_INDEX_SYMBOL_KIND_VALUE (cu);
10234 cu = GDB_INDEX_CU_VALUE (cu);
10235 /* Convert to TU number if it's for a type unit. */
10236 if (cu >= cu_list_elements / 2)
10237 printf ("%cT%lu", num_cus > 1 ? '\t' : ' ',
10238 (unsigned long) (cu - cu_list_elements / 2));
10239 else
10240 printf ("%c%lu", num_cus > 1 ? '\t' : ' ', (unsigned long) cu);
10242 printf (" [%s, %s]",
10243 is_static ? _("static") : _("global"),
10244 get_gdb_index_symbol_kind_name (kind));
10245 if (num_cus > 1)
10246 printf ("\n");
10248 if (num_cus <= 1)
10249 printf ("\n");
10253 return 1;
10256 /* Pre-allocate enough space for the CU/TU sets needed. */
10258 static void
10259 prealloc_cu_tu_list (unsigned int nshndx)
10261 if (shndx_pool == NULL)
10263 shndx_pool_size = nshndx;
10264 shndx_pool_used = 0;
10265 shndx_pool = (unsigned int *) xcmalloc (shndx_pool_size,
10266 sizeof (unsigned int));
10268 else
10270 shndx_pool_size = shndx_pool_used + nshndx;
10271 shndx_pool = (unsigned int *) xcrealloc (shndx_pool, shndx_pool_size,
10272 sizeof (unsigned int));
10276 static void
10277 add_shndx_to_cu_tu_entry (unsigned int shndx)
10279 if (shndx_pool_used >= shndx_pool_size)
10281 error (_("Internal error: out of space in the shndx pool.\n"));
10282 return;
10284 shndx_pool [shndx_pool_used++] = shndx;
10287 static void
10288 end_cu_tu_entry (void)
10290 if (shndx_pool_used >= shndx_pool_size)
10292 error (_("Internal error: out of space in the shndx pool.\n"));
10293 return;
10295 shndx_pool [shndx_pool_used++] = 0;
10298 /* Return the short name of a DWARF section given by a DW_SECT enumerator. */
10300 static const char *
10301 get_DW_SECT_short_name (unsigned int dw_sect)
10303 static char buf[16];
10305 switch (dw_sect)
10307 case DW_SECT_INFO:
10308 return "info";
10309 case DW_SECT_TYPES:
10310 return "types";
10311 case DW_SECT_ABBREV:
10312 return "abbrev";
10313 case DW_SECT_LINE:
10314 return "line";
10315 case DW_SECT_LOC:
10316 return "loc";
10317 case DW_SECT_STR_OFFSETS:
10318 return "str_off";
10319 case DW_SECT_MACINFO:
10320 return "macinfo";
10321 case DW_SECT_MACRO:
10322 return "macro";
10323 default:
10324 break;
10327 snprintf (buf, sizeof (buf), "%d", dw_sect);
10328 return buf;
10331 /* Process a CU or TU index. If DO_DISPLAY is true, print the contents.
10332 These sections are extensions for Fission.
10333 See http://gcc.gnu.org/wiki/DebugFissionDWP. */
10335 static int
10336 process_cu_tu_index (struct dwarf_section *section, int do_display)
10338 unsigned char *phdr = section->start;
10339 unsigned char *limit = phdr + section->size;
10340 unsigned char *phash;
10341 unsigned char *pindex;
10342 unsigned char *ppool;
10343 unsigned int version;
10344 unsigned int ncols = 0;
10345 unsigned int nused;
10346 unsigned int nslots;
10347 unsigned int i;
10348 unsigned int j;
10349 dwarf_vma signature;
10350 size_t total;
10352 /* PR 17512: file: 002-168123-0.004. */
10353 if (phdr == NULL)
10355 warn (_("Section %s is empty\n"), section->name);
10356 return 0;
10358 /* PR 17512: file: 002-376-0.004. */
10359 if (section->size < 24)
10361 warn (_("Section %s is too small to contain a CU/TU header\n"),
10362 section->name);
10363 return 0;
10366 phash = phdr;
10367 SAFE_BYTE_GET_AND_INC (version, phash, 4, limit);
10368 if (version >= 2)
10369 SAFE_BYTE_GET_AND_INC (ncols, phash, 4, limit);
10370 SAFE_BYTE_GET_AND_INC (nused, phash, 4, limit);
10371 SAFE_BYTE_GET_AND_INC (nslots, phash, 4, limit);
10373 pindex = phash + (size_t) nslots * 8;
10374 ppool = pindex + (size_t) nslots * 4;
10376 if (do_display)
10378 introduce (section, false);
10380 printf (_(" Version: %u\n"), version);
10381 if (version >= 2)
10382 printf (_(" Number of columns: %u\n"), ncols);
10383 printf (_(" Number of used entries: %u\n"), nused);
10384 printf (_(" Number of slots: %u\n\n"), nslots);
10387 /* PR 17531: file: 45d69832. */
10388 if (_mul_overflow ((size_t) nslots, 12, &total)
10389 || total > (size_t) (limit - phash))
10391 warn (ngettext ("Section %s is too small for %u slot\n",
10392 "Section %s is too small for %u slots\n",
10393 nslots),
10394 section->name, nslots);
10395 return 0;
10398 if (version == 1)
10400 if (!do_display)
10401 prealloc_cu_tu_list ((limit - ppool) / 4);
10402 for (i = 0; i < nslots; i++)
10404 unsigned char *shndx_list;
10405 unsigned int shndx;
10407 SAFE_BYTE_GET (signature, phash, 8, limit);
10408 if (signature != 0)
10410 SAFE_BYTE_GET (j, pindex, 4, limit);
10411 shndx_list = ppool + j * 4;
10412 /* PR 17531: file: 705e010d. */
10413 if (shndx_list < ppool)
10415 warn (_("Section index pool located before start of section\n"));
10416 return 0;
10419 if (do_display)
10420 printf (_(" [%3d] Signature: 0x%s Sections: "),
10421 i, dwarf_vmatoa ("x", signature));
10422 for (;;)
10424 if (shndx_list >= limit)
10426 warn (_("Section %s too small for shndx pool\n"),
10427 section->name);
10428 return 0;
10430 SAFE_BYTE_GET (shndx, shndx_list, 4, limit);
10431 if (shndx == 0)
10432 break;
10433 if (do_display)
10434 printf (" %d", shndx);
10435 else
10436 add_shndx_to_cu_tu_entry (shndx);
10437 shndx_list += 4;
10439 if (do_display)
10440 printf ("\n");
10441 else
10442 end_cu_tu_entry ();
10444 phash += 8;
10445 pindex += 4;
10448 else if (version == 2)
10450 unsigned int val;
10451 unsigned int dw_sect;
10452 unsigned char *ph = phash;
10453 unsigned char *pi = pindex;
10454 unsigned char *poffsets = ppool + (size_t) ncols * 4;
10455 unsigned char *psizes = poffsets + (size_t) nused * ncols * 4;
10456 bool is_tu_index;
10457 struct cu_tu_set *this_set = NULL;
10458 unsigned int row;
10459 unsigned char *prow;
10460 size_t temp;
10462 is_tu_index = strcmp (section->name, ".debug_tu_index") == 0;
10464 /* PR 17531: file: 0dd159bf.
10465 Check for integer overflow (can occur when size_t is 32-bit)
10466 with overlarge ncols or nused values. */
10467 if (nused == -1u
10468 || _mul_overflow ((size_t) ncols, 4, &temp)
10469 || _mul_overflow ((size_t) nused + 1, temp, &total)
10470 || total > (size_t) (limit - ppool))
10472 warn (_("Section %s too small for offset and size tables\n"),
10473 section->name);
10474 return 0;
10477 if (do_display)
10479 printf (_(" Offset table\n"));
10480 printf (" slot %-16s ",
10481 is_tu_index ? _("signature") : _("dwo_id"));
10483 else
10485 if (is_tu_index)
10487 tu_count = nused;
10488 tu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10489 this_set = tu_sets;
10491 else
10493 cu_count = nused;
10494 cu_sets = xcalloc2 (nused, sizeof (struct cu_tu_set));
10495 this_set = cu_sets;
10499 if (do_display)
10501 for (j = 0; j < ncols; j++)
10503 unsigned char *p = ppool + j * 4;
10504 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10505 printf (" %8s", get_DW_SECT_short_name (dw_sect));
10507 printf ("\n");
10510 for (i = 0; i < nslots; i++)
10512 SAFE_BYTE_GET (signature, ph, 8, limit);
10514 SAFE_BYTE_GET (row, pi, 4, limit);
10515 if (row != 0)
10517 /* PR 17531: file: a05f6ab3. */
10518 if (row > nused)
10520 warn (_("Row index (%u) is larger than number of used entries (%u)\n"),
10521 row, nused);
10522 return 0;
10525 if (!do_display)
10527 size_t num_copy = sizeof (uint64_t);
10529 memcpy (&this_set[row - 1].signature, ph, num_copy);
10532 prow = poffsets + (row - 1) * ncols * 4;
10533 if (do_display)
10534 printf (_(" [%3d] 0x%s"),
10535 i, dwarf_vmatoa ("x", signature));
10536 for (j = 0; j < ncols; j++)
10538 unsigned char *p = prow + j * 4;
10539 SAFE_BYTE_GET (val, p, 4, limit);
10540 if (do_display)
10541 printf (" %8d", val);
10542 else
10544 p = ppool + j * 4;
10545 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10547 /* PR 17531: file: 10796eb3. */
10548 if (dw_sect >= DW_SECT_MAX)
10549 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10550 else
10551 this_set [row - 1].section_offsets [dw_sect] = val;
10555 if (do_display)
10556 printf ("\n");
10558 ph += 8;
10559 pi += 4;
10562 ph = phash;
10563 pi = pindex;
10564 if (do_display)
10566 printf ("\n");
10567 printf (_(" Size table\n"));
10568 printf (" slot %-16s ",
10569 is_tu_index ? _("signature") : _("dwo_id"));
10572 for (j = 0; j < ncols; j++)
10574 unsigned char *p = ppool + j * 4;
10575 SAFE_BYTE_GET (val, p, 4, limit);
10576 if (do_display)
10577 printf (" %8s", get_DW_SECT_short_name (val));
10580 if (do_display)
10581 printf ("\n");
10583 for (i = 0; i < nslots; i++)
10585 SAFE_BYTE_GET (signature, ph, 8, limit);
10587 SAFE_BYTE_GET (row, pi, 4, limit);
10588 if (row != 0)
10590 prow = psizes + (row - 1) * ncols * 4;
10592 if (do_display)
10593 printf (_(" [%3d] 0x%s"),
10594 i, dwarf_vmatoa ("x", signature));
10596 for (j = 0; j < ncols; j++)
10598 unsigned char *p = prow + j * 4;
10600 /* PR 28645: Check for overflow. Since we do not know how
10601 many populated rows there will be, we cannot just
10602 perform a single check at the start of this function. */
10603 if (p > (limit - 4))
10605 if (do_display)
10606 printf ("\n");
10607 warn (_("Too many rows/columns in DWARF index section %s\n"),
10608 section->name);
10609 return 0;
10612 SAFE_BYTE_GET (val, p, 4, limit);
10614 if (do_display)
10615 printf (" %8d", val);
10616 else
10618 p = ppool + j * 4;
10619 SAFE_BYTE_GET (dw_sect, p, 4, limit);
10620 if (dw_sect >= DW_SECT_MAX)
10621 warn (_("Overlarge Dwarf section index detected: %u\n"), dw_sect);
10622 else
10623 this_set [row - 1].section_sizes [dw_sect] = val;
10627 if (do_display)
10628 printf ("\n");
10631 ph += 8;
10632 pi += 4;
10635 else if (do_display)
10636 printf (_(" Unsupported version (%d)\n"), version);
10638 if (do_display)
10639 printf ("\n");
10641 return 1;
10644 static int cu_tu_indexes_read = -1; /* Tri-state variable. */
10646 /* Load the CU and TU indexes if present. This will build a list of
10647 section sets that we can use to associate a .debug_info.dwo section
10648 with its associated .debug_abbrev.dwo section in a .dwp file. */
10650 static bool
10651 load_cu_tu_indexes (void *file)
10653 /* If we have already loaded (or tried to load) the CU and TU indexes
10654 then do not bother to repeat the task. */
10655 if (cu_tu_indexes_read == -1)
10657 cu_tu_indexes_read = true;
10659 if (load_debug_section_with_follow (dwp_cu_index, file))
10660 if (! process_cu_tu_index (&debug_displays [dwp_cu_index].section, 0))
10661 cu_tu_indexes_read = false;
10663 if (load_debug_section_with_follow (dwp_tu_index, file))
10664 if (! process_cu_tu_index (&debug_displays [dwp_tu_index].section, 0))
10665 cu_tu_indexes_read = false;
10668 return (bool) cu_tu_indexes_read;
10671 /* Find the set of sections that includes section SHNDX. */
10673 unsigned int *
10674 find_cu_tu_set (void *file, unsigned int shndx)
10676 unsigned int i;
10678 if (! load_cu_tu_indexes (file))
10679 return NULL;
10681 /* Find SHNDX in the shndx pool. */
10682 for (i = 0; i < shndx_pool_used; i++)
10683 if (shndx_pool [i] == shndx)
10684 break;
10686 if (i >= shndx_pool_used)
10687 return NULL;
10689 /* Now backup to find the first entry in the set. */
10690 while (i > 0 && shndx_pool [i - 1] != 0)
10691 i--;
10693 return shndx_pool + i;
10696 /* Display a .debug_cu_index or .debug_tu_index section. */
10698 static int
10699 display_cu_index (struct dwarf_section *section, void *file ATTRIBUTE_UNUSED)
10701 return process_cu_tu_index (section, 1);
10704 static int
10705 display_debug_not_supported (struct dwarf_section *section,
10706 void *file ATTRIBUTE_UNUSED)
10708 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
10709 section->name);
10711 return 1;
10714 /* Like malloc, but takes two parameters like calloc.
10715 Verifies that the first parameter is not too large.
10716 Note: does *not* initialise the allocated memory to zero. */
10718 void *
10719 cmalloc (size_t nmemb, size_t size)
10721 /* Check for overflow. */
10722 if (nmemb >= ~(size_t) 0 / size)
10723 return NULL;
10725 return xmalloc (nmemb * size);
10728 /* Like xmalloc, but takes two parameters like calloc.
10729 Verifies that the first parameter is not too large.
10730 Note: does *not* initialise the allocated memory to zero. */
10732 void *
10733 xcmalloc (size_t nmemb, size_t size)
10735 /* Check for overflow. */
10736 if (nmemb >= ~(size_t) 0 / size)
10738 fprintf (stderr,
10739 _("Attempt to allocate an array with an excessive number of elements: 0x%lx\n"),
10740 (long) nmemb);
10741 xexit (1);
10744 return xmalloc (nmemb * size);
10747 /* Like xrealloc, but takes three parameters.
10748 Verifies that the second parameter is not too large.
10749 Note: does *not* initialise any new memory to zero. */
10751 void *
10752 xcrealloc (void *ptr, size_t nmemb, size_t size)
10754 /* Check for overflow. */
10755 if (nmemb >= ~(size_t) 0 / size)
10757 error (_("Attempt to re-allocate an array with an excessive number of elements: 0x%lx\n"),
10758 (long) nmemb);
10759 xexit (1);
10762 return xrealloc (ptr, nmemb * size);
10765 /* Like xcalloc, but verifies that the first parameter is not too large. */
10767 void *
10768 xcalloc2 (size_t nmemb, size_t size)
10770 /* Check for overflow. */
10771 if (nmemb >= ~(size_t) 0 / size)
10773 error (_("Attempt to allocate a zero'ed array with an excessive number of elements: 0x%lx\n"),
10774 (long) nmemb);
10775 xexit (1);
10778 return xcalloc (nmemb, size);
10781 static unsigned long
10782 calc_gnu_debuglink_crc32 (unsigned long crc,
10783 const unsigned char * buf,
10784 bfd_size_type len)
10786 static const unsigned long crc32_table[256] =
10788 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
10789 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
10790 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
10791 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
10792 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
10793 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
10794 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
10795 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
10796 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
10797 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
10798 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
10799 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
10800 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
10801 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
10802 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
10803 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
10804 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
10805 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
10806 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
10807 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
10808 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
10809 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
10810 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
10811 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
10812 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
10813 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
10814 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
10815 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
10816 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
10817 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
10818 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
10819 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
10820 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
10821 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
10822 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
10823 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
10824 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
10825 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
10826 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
10827 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
10828 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
10829 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
10830 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
10831 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
10832 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
10833 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
10834 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
10835 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
10836 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
10837 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
10838 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
10839 0x2d02ef8d
10841 const unsigned char *end;
10843 crc = ~crc & 0xffffffff;
10844 for (end = buf + len; buf < end; ++ buf)
10845 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
10846 return ~crc & 0xffffffff;
10849 typedef bool (*check_func_type) (const char *, void *);
10850 typedef const char *(* parse_func_type) (struct dwarf_section *, void *);
10852 static bool
10853 check_gnu_debuglink (const char * pathname, void * crc_pointer)
10855 static unsigned char buffer [8 * 1024];
10856 FILE * f;
10857 bfd_size_type count;
10858 unsigned long crc = 0;
10859 void * sep_data;
10861 sep_data = open_debug_file (pathname);
10862 if (sep_data == NULL)
10863 return false;
10865 /* Yes - we are opening the file twice... */
10866 f = fopen (pathname, "rb");
10867 if (f == NULL)
10869 /* Paranoia: This should never happen. */
10870 close_debug_file (sep_data);
10871 warn (_("Unable to reopen separate debug info file: %s\n"), pathname);
10872 return false;
10875 while ((count = fread (buffer, 1, sizeof (buffer), f)) > 0)
10876 crc = calc_gnu_debuglink_crc32 (crc, buffer, count);
10878 fclose (f);
10880 if (crc != * (unsigned long *) crc_pointer)
10882 close_debug_file (sep_data);
10883 warn (_("Separate debug info file %s found, but CRC does not match - ignoring\n"),
10884 pathname);
10885 return false;
10888 return true;
10891 static const char *
10892 parse_gnu_debuglink (struct dwarf_section * section, void * data)
10894 const char * name;
10895 unsigned int crc_offset;
10896 unsigned long * crc32 = (unsigned long *) data;
10898 /* The name is first.
10899 The CRC value is stored after the filename, aligned up to 4 bytes. */
10900 name = (const char *) section->start;
10902 crc_offset = strnlen (name, section->size) + 1;
10903 if (crc_offset == 1)
10904 return NULL;
10905 crc_offset = (crc_offset + 3) & ~3;
10906 if (crc_offset + 4 > section->size)
10907 return NULL;
10909 * crc32 = byte_get (section->start + crc_offset, 4);
10910 return name;
10913 static bool
10914 check_gnu_debugaltlink (const char * filename, void * data ATTRIBUTE_UNUSED)
10916 void * sep_data = open_debug_file (filename);
10918 if (sep_data == NULL)
10919 return false;
10921 /* FIXME: We should now extract the build-id in the separate file
10922 and check it... */
10924 return true;
10927 typedef struct build_id_data
10929 bfd_size_type len;
10930 const unsigned char * data;
10931 } Build_id_data;
10933 static const char *
10934 parse_gnu_debugaltlink (struct dwarf_section * section, void * data)
10936 const char * name;
10937 bfd_size_type namelen;
10938 bfd_size_type id_len;
10939 Build_id_data * build_id_data;
10941 /* The name is first.
10942 The build-id follows immediately, with no padding, up to the section's end. */
10944 name = (const char *) section->start;
10945 namelen = strnlen (name, section->size) + 1;
10946 if (namelen == 1)
10947 return NULL;
10948 if (namelen >= section->size)
10949 return NULL;
10951 id_len = section->size - namelen;
10952 if (id_len < 0x14)
10953 return NULL;
10955 build_id_data = (Build_id_data *) data;
10956 build_id_data->len = id_len;
10957 build_id_data->data = section->start + namelen;
10959 return name;
10962 static void
10963 add_separate_debug_file (const char * filename, void * handle)
10965 separate_info * i = xmalloc (sizeof * i);
10967 i->filename = filename;
10968 i->handle = handle;
10969 i->next = first_separate_info;
10970 first_separate_info = i;
10973 #if HAVE_LIBDEBUGINFOD
10974 /* Query debuginfod servers for the target debuglink or debugaltlink
10975 file. If successful, store the path of the file in filename and
10976 return TRUE, otherwise return FALSE. */
10978 static bool
10979 debuginfod_fetch_separate_debug_info (struct dwarf_section * section,
10980 char ** filename,
10981 void * file)
10983 size_t build_id_len;
10984 unsigned char * build_id;
10986 if (strcmp (section->uncompressed_name, ".gnu_debuglink") == 0)
10988 /* Get the build-id of file. */
10989 build_id = get_build_id (file);
10990 build_id_len = 0;
10992 else if (strcmp (section->uncompressed_name, ".gnu_debugaltlink") == 0)
10994 /* Get the build-id of the debugaltlink file. */
10995 unsigned int filelen;
10997 filelen = strnlen ((const char *)section->start, section->size);
10998 if (filelen == section->size)
10999 /* Corrupt debugaltlink. */
11000 return false;
11002 build_id = section->start + filelen + 1;
11003 build_id_len = section->size - (filelen + 1);
11005 if (build_id_len == 0)
11006 return false;
11008 else
11009 return false;
11011 if (build_id)
11013 int fd;
11014 debuginfod_client * client;
11016 client = debuginfod_begin ();
11017 if (client == NULL)
11018 return false;
11020 /* Query debuginfod servers for the target file. If found its path
11021 will be stored in filename. */
11022 fd = debuginfod_find_debuginfo (client, build_id, build_id_len, filename);
11023 debuginfod_end (client);
11025 /* Only free build_id if we allocated space for a hex string
11026 in get_build_id (). */
11027 if (build_id_len == 0)
11028 free (build_id);
11030 if (fd >= 0)
11032 /* File successfully retrieved. Close fd since we want to
11033 use open_debug_file () on filename instead. */
11034 close (fd);
11035 return true;
11039 return false;
11041 #endif
11043 static void *
11044 load_separate_debug_info (const char * main_filename,
11045 struct dwarf_section * xlink,
11046 parse_func_type parse_func,
11047 check_func_type check_func,
11048 void * func_data,
11049 void * file ATTRIBUTE_UNUSED)
11051 const char * separate_filename;
11052 char * debug_filename;
11053 char * canon_dir;
11054 size_t canon_dirlen;
11055 size_t dirlen;
11056 char * canon_filename;
11057 char * canon_debug_filename;
11058 bool self;
11060 if ((separate_filename = parse_func (xlink, func_data)) == NULL)
11062 warn (_("Corrupt debuglink section: %s\n"),
11063 xlink->name ? xlink->name : xlink->uncompressed_name);
11064 return NULL;
11067 /* Attempt to locate the separate file.
11068 This should duplicate the logic in bfd/opncls.c:find_separate_debug_file(). */
11070 canon_filename = lrealpath (main_filename);
11071 canon_dir = xstrdup (canon_filename);
11073 for (canon_dirlen = strlen (canon_dir); canon_dirlen > 0; canon_dirlen--)
11074 if (IS_DIR_SEPARATOR (canon_dir[canon_dirlen - 1]))
11075 break;
11076 canon_dir[canon_dirlen] = '\0';
11078 #ifndef DEBUGDIR
11079 #define DEBUGDIR "/lib/debug"
11080 #endif
11081 #ifndef EXTRA_DEBUG_ROOT1
11082 #define EXTRA_DEBUG_ROOT1 "/usr/lib/debug"
11083 #endif
11084 #ifndef EXTRA_DEBUG_ROOT2
11085 #define EXTRA_DEBUG_ROOT2 "/usr/lib/debug/usr"
11086 #endif
11088 debug_filename = (char *) malloc (strlen (DEBUGDIR) + 1
11089 + canon_dirlen
11090 + strlen (".debug/")
11091 #ifdef EXTRA_DEBUG_ROOT1
11092 + strlen (EXTRA_DEBUG_ROOT1)
11093 #endif
11094 #ifdef EXTRA_DEBUG_ROOT2
11095 + strlen (EXTRA_DEBUG_ROOT2)
11096 #endif
11097 + strlen (separate_filename)
11098 + 1);
11099 if (debug_filename == NULL)
11101 warn (_("Out of memory"));
11102 free (canon_dir);
11103 free (canon_filename);
11104 return NULL;
11107 /* First try in the current directory. */
11108 sprintf (debug_filename, "%s", separate_filename);
11109 if (check_func (debug_filename, func_data))
11110 goto found;
11112 /* Then try in a subdirectory called .debug. */
11113 sprintf (debug_filename, ".debug/%s", separate_filename);
11114 if (check_func (debug_filename, func_data))
11115 goto found;
11117 /* Then try in the same directory as the original file. */
11118 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11119 if (check_func (debug_filename, func_data))
11120 goto found;
11122 /* And the .debug subdirectory of that directory. */
11123 sprintf (debug_filename, "%s.debug/%s", canon_dir, separate_filename);
11124 if (check_func (debug_filename, func_data))
11125 goto found;
11127 #ifdef EXTRA_DEBUG_ROOT1
11128 /* Try the first extra debug file root. */
11129 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1, separate_filename);
11130 if (check_func (debug_filename, func_data))
11131 goto found;
11133 /* Try the first extra debug file root. */
11134 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1, canon_dir, separate_filename);
11135 if (check_func (debug_filename, func_data))
11136 goto found;
11137 #endif
11139 #ifdef EXTRA_DEBUG_ROOT2
11140 /* Try the second extra debug file root. */
11141 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2, separate_filename);
11142 if (check_func (debug_filename, func_data))
11143 goto found;
11144 #endif
11146 /* Then try in the global debug_filename directory. */
11147 strcpy (debug_filename, DEBUGDIR);
11148 dirlen = strlen (DEBUGDIR) - 1;
11149 if (dirlen > 0 && DEBUGDIR[dirlen] != '/')
11150 strcat (debug_filename, "/");
11151 strcat (debug_filename, (const char *) separate_filename);
11153 if (check_func (debug_filename, func_data))
11154 goto found;
11156 #if HAVE_LIBDEBUGINFOD
11158 char * tmp_filename;
11160 if (debuginfod_fetch_separate_debug_info (xlink,
11161 & tmp_filename,
11162 file))
11164 /* File successfully downloaded from server, replace
11165 debug_filename with the file's path. */
11166 free (debug_filename);
11167 debug_filename = tmp_filename;
11168 goto found;
11171 #endif
11173 if (do_debug_links)
11175 /* Failed to find the file. */
11176 warn (_("could not find separate debug file '%s'\n"),
11177 separate_filename);
11178 warn (_("tried: %s\n"), debug_filename);
11180 #ifdef EXTRA_DEBUG_ROOT2
11181 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT2,
11182 separate_filename);
11183 warn (_("tried: %s\n"), debug_filename);
11184 #endif
11186 #ifdef EXTRA_DEBUG_ROOT1
11187 sprintf (debug_filename, "%s/%s/%s", EXTRA_DEBUG_ROOT1,
11188 canon_dir, separate_filename);
11189 warn (_("tried: %s\n"), debug_filename);
11191 sprintf (debug_filename, "%s/%s", EXTRA_DEBUG_ROOT1,
11192 separate_filename);
11193 warn (_("tried: %s\n"), debug_filename);
11194 #endif
11196 sprintf (debug_filename, "%s.debug/%s", canon_dir,
11197 separate_filename);
11198 warn (_("tried: %s\n"), debug_filename);
11200 sprintf (debug_filename, "%s%s", canon_dir, separate_filename);
11201 warn (_("tried: %s\n"), debug_filename);
11203 sprintf (debug_filename, ".debug/%s", separate_filename);
11204 warn (_("tried: %s\n"), debug_filename);
11206 sprintf (debug_filename, "%s", separate_filename);
11207 warn (_("tried: %s\n"), debug_filename);
11209 #if HAVE_LIBDEBUGINFOD
11211 char *urls = getenv (DEBUGINFOD_URLS_ENV_VAR);
11212 if (urls == NULL)
11213 urls = "";
11215 warn (_("tried: DEBUGINFOD_URLS=%s\n"), urls);
11217 #endif
11220 free (canon_dir);
11221 free (debug_filename);
11222 free (canon_filename);
11223 return NULL;
11225 found:
11226 free (canon_dir);
11228 canon_debug_filename = lrealpath (debug_filename);
11229 self = strcmp (canon_debug_filename, canon_filename) == 0;
11230 free (canon_filename);
11231 free (canon_debug_filename);
11232 if (self)
11234 free (debug_filename);
11235 return NULL;
11238 void * debug_handle;
11240 /* Now open the file.... */
11241 if ((debug_handle = open_debug_file (debug_filename)) == NULL)
11243 warn (_("failed to open separate debug file: %s\n"), debug_filename);
11244 free (debug_filename);
11245 return NULL;
11248 /* FIXME: We do not check to see if there are any other separate debug info
11249 files that would also match. */
11251 if (do_debug_links)
11252 printf (_("\n%s: Found separate debug info file: %s\n"), main_filename, debug_filename);
11253 add_separate_debug_file (debug_filename, debug_handle);
11255 /* Do not free debug_filename - it might be referenced inside
11256 the structure returned by open_debug_file(). */
11257 return debug_handle;
11260 /* Attempt to load a separate dwarf object file. */
11262 static void *
11263 load_dwo_file (const char * main_filename, const char * name, const char * dir, const char * id ATTRIBUTE_UNUSED)
11265 char * separate_filename;
11266 void * separate_handle;
11268 if (IS_ABSOLUTE_PATH (name))
11269 separate_filename = strdup (name);
11270 else
11271 /* FIXME: Skip adding / if dwo_dir ends in /. */
11272 separate_filename = concat (dir, "/", name, NULL);
11273 if (separate_filename == NULL)
11275 warn (_("Out of memory allocating dwo filename\n"));
11276 return NULL;
11279 if ((separate_handle = open_debug_file (separate_filename)) == NULL)
11281 warn (_("Unable to load dwo file: %s\n"), separate_filename);
11282 free (separate_filename);
11283 return NULL;
11286 /* FIXME: We should check the dwo_id. */
11288 printf (_("%s: Found separate debug object file: %s\n\n"), main_filename, separate_filename);
11290 add_separate_debug_file (separate_filename, separate_handle);
11291 /* Note - separate_filename will be freed in free_debug_memory(). */
11292 return separate_handle;
11295 static void *
11296 try_build_id_prefix (const char * prefix, char * filename, const unsigned char * data, unsigned long id_len)
11298 char * f = filename;
11300 f += sprintf (f, "%s.build-id/%02x/", prefix, (unsigned) *data++);
11301 id_len --;
11302 while (id_len --)
11303 f += sprintf (f, "%02x", (unsigned) *data++);
11304 strcpy (f, ".debug");
11306 return open_debug_file (filename);
11309 /* Try to load a debug file based upon the build-id held in the .note.gnu.build-id section. */
11311 static void
11312 load_build_id_debug_file (const char * main_filename ATTRIBUTE_UNUSED, void * main_file)
11314 if (! load_debug_section (note_gnu_build_id, main_file))
11315 return; /* No .note.gnu.build-id section. */
11317 struct dwarf_section * section = & debug_displays [note_gnu_build_id].section;
11318 if (section == NULL)
11320 warn (_("Unable to load the .note.gnu.build-id section\n"));
11321 return;
11324 if (section->start == NULL || section->size < 0x18)
11326 warn (_(".note.gnu.build-id section is corrupt/empty\n"));
11327 return;
11330 /* In theory we should extract the contents of the section into
11331 a note structure and then check the fields. For now though
11332 just use hard coded offsets instead:
11334 Field Bytes Contents
11335 NSize 0...3 4
11336 DSize 4...7 8+
11337 Type 8..11 3 (NT_GNU_BUILD_ID)
11338 Name 12.15 GNU\0
11339 Data 16.... */
11341 /* FIXME: Check the name size, name and type fields. */
11343 unsigned long build_id_size;
11344 build_id_size = byte_get (section->start + 4, 4);
11345 if (build_id_size < 8)
11347 warn (_(".note.gnu.build-id data size is too small\n"));
11348 return;
11351 if (build_id_size > (section->size - 16))
11353 warn (_(".note.gnu.build-id data size is too bug\n"));
11354 return;
11357 char * filename;
11358 filename = xmalloc (strlen (".build-id/")
11359 + build_id_size * 2 + 2
11360 + strlen (".debug")
11361 /* The next string should be the same as the longest
11362 name found in the prefixes[] array below. */
11363 + strlen ("/usrlib64/debug/usr")
11364 + 1);
11365 void * handle;
11367 static const char * prefixes[] =
11370 ".debug/",
11371 "/usr/lib/debug/",
11372 "/usr/lib/debug/usr/",
11373 "/usr/lib64/debug/",
11374 "/usr/lib64/debug/usr"
11376 long unsigned int i;
11378 for (i = 0; i < ARRAY_SIZE (prefixes); i++)
11380 handle = try_build_id_prefix (prefixes[i], filename,
11381 section->start + 16, build_id_size);
11382 if (handle != NULL)
11383 break;
11385 /* FIXME: TYhe BFD library also tries a global debugfile directory prefix. */
11386 if (handle == NULL)
11388 /* Failed to find a debug file associated with the build-id.
11389 This is not an error however, rather it just means that
11390 the debug info has probably not been loaded on the system,
11391 or that another method is being used to link to the debug
11392 info. */
11393 free (filename);
11394 return;
11397 add_separate_debug_file (filename, handle);
11400 /* Try to load a debug file pointed to by the .debug_sup section. */
11402 static void
11403 load_debug_sup_file (const char * main_filename, void * file)
11405 if (! load_debug_section (debug_sup, file))
11406 return; /* No .debug_sup section. */
11408 struct dwarf_section * section;
11409 section = & debug_displays [debug_sup].section;
11410 assert (section != NULL);
11412 if (section->start == NULL || section->size < 5)
11414 warn (_(".debug_sup section is corrupt/empty\n"));
11415 return;
11418 if (section->start[2] != 0)
11419 return; /* This is a supplementary file. */
11421 const char * filename = (const char *) section->start + 3;
11422 if (strnlen (filename, section->size - 3) == section->size - 3)
11424 warn (_("filename in .debug_sup section is corrupt\n"));
11425 return;
11428 if (filename[0] != '/' && strchr (main_filename, '/'))
11430 char * new_name;
11431 int new_len;
11433 new_len = asprintf (& new_name, "%.*s/%s",
11434 (int) (strrchr (main_filename, '/') - main_filename),
11435 main_filename,
11436 filename);
11437 if (new_len < 3)
11439 warn (_("unable to construct path for supplementary debug file"));
11440 if (new_len > -1)
11441 free (new_name);
11442 return;
11444 filename = new_name;
11446 else
11448 /* PR 27796: Make sure that we pass a filename that can be free'd to
11449 add_separate_debug_file(). */
11450 filename = strdup (filename);
11451 if (filename == NULL)
11453 warn (_("out of memory constructing filename for .debug_sup link\n"));
11454 return;
11458 void * handle = open_debug_file (filename);
11459 if (handle == NULL)
11461 warn (_("unable to open file '%s' referenced from .debug_sup section\n"), filename);
11462 free ((void *) filename);
11463 return;
11466 printf (_("%s: Found supplementary debug file: %s\n\n"), main_filename, filename);
11468 /* FIXME: Compare the checksums, if present. */
11469 add_separate_debug_file (filename, handle);
11472 /* Load a debuglink section and/or a debugaltlink section, if either are present.
11473 Recursively check the loaded files for more of these sections.
11474 Also follow any links in .debug_sup sections.
11475 FIXME: Should also check for DWO_* entries in the newly loaded files. */
11477 static void
11478 check_for_and_load_links (void * file, const char * filename)
11480 void * handle = NULL;
11482 if (load_debug_section (gnu_debugaltlink, file))
11484 Build_id_data build_id_data;
11486 handle = load_separate_debug_info (filename,
11487 & debug_displays[gnu_debugaltlink].section,
11488 parse_gnu_debugaltlink,
11489 check_gnu_debugaltlink,
11490 & build_id_data,
11491 file);
11492 if (handle)
11494 assert (handle == first_separate_info->handle);
11495 check_for_and_load_links (first_separate_info->handle,
11496 first_separate_info->filename);
11500 if (load_debug_section (gnu_debuglink, file))
11502 unsigned long crc32;
11504 handle = load_separate_debug_info (filename,
11505 & debug_displays[gnu_debuglink].section,
11506 parse_gnu_debuglink,
11507 check_gnu_debuglink,
11508 & crc32,
11509 file);
11510 if (handle)
11512 assert (handle == first_separate_info->handle);
11513 check_for_and_load_links (first_separate_info->handle,
11514 first_separate_info->filename);
11518 load_debug_sup_file (filename, file);
11520 load_build_id_debug_file (filename, file);
11523 /* Load the separate debug info file(s) attached to FILE, if any exist.
11524 Returns TRUE if any were found, FALSE otherwise.
11525 If TRUE is returned then the linked list starting at first_separate_info
11526 will be populated with open file handles. */
11528 bool
11529 load_separate_debug_files (void * file, const char * filename)
11531 /* Skip this operation if we are not interested in debug links. */
11532 if (! do_follow_links && ! do_debug_links)
11533 return false;
11535 /* See if there are any dwo links. */
11536 if (load_debug_section (str, file)
11537 && load_debug_section (abbrev, file)
11538 && load_debug_section (info, file))
11540 free_dwo_info ();
11542 if (process_debug_info (& debug_displays[info].section, file, abbrev,
11543 true, false))
11545 bool introduced = false;
11546 dwo_info *dwinfo;
11547 const char *dir = NULL;
11548 const char *id = NULL;
11549 const char *name = NULL;
11551 for (dwinfo = first_dwo_info; dwinfo != NULL; dwinfo = dwinfo->next)
11553 /* Accumulate NAME, DIR and ID fields. */
11554 switch (dwinfo->type)
11556 case DWO_NAME:
11557 if (name != NULL)
11558 warn (_("Multiple DWO_NAMEs encountered for the same CU\n"));
11559 name = dwinfo->value;
11560 break;
11562 case DWO_DIR:
11563 /* There can be multiple DW_AT_comp_dir entries in a CU,
11564 so do not complain. */
11565 dir = dwinfo->value;
11566 break;
11568 case DWO_ID:
11569 if (id != NULL)
11570 warn (_("multiple DWO_IDs encountered for the same CU\n"));
11571 id = dwinfo->value;
11572 break;
11574 default:
11575 error (_("Unexpected DWO INFO type"));
11576 break;
11579 /* If we have reached the end of our list, or we are changing
11580 CUs, then display the information that we have accumulated
11581 so far. */
11582 if (name != NULL
11583 && (dwinfo->next == NULL
11584 || dwinfo->next->cu_offset != dwinfo->cu_offset))
11586 if (do_debug_links)
11588 if (! introduced)
11590 printf (_("The %s section contains link(s) to dwo file(s):\n\n"),
11591 debug_displays [info].section.uncompressed_name);
11592 introduced = true;
11595 printf (_(" Name: %s\n"), name);
11596 printf (_(" Directory: %s\n"), dir ? dir : _("<not-found>"));
11597 if (id != NULL)
11598 display_data (printf (_(" ID: ")), (unsigned char *) id, 8);
11599 else
11600 printf (_(" ID: <not specified>\n"));
11601 printf ("\n\n");
11604 if (do_follow_links)
11605 load_dwo_file (filename, name, dir, id);
11607 name = dir = id = NULL;
11613 if (! do_follow_links)
11614 /* The other debug links will be displayed by display_debug_links()
11615 so we do not need to do any further processing here. */
11616 return false;
11618 /* FIXME: We do not check for the presence of both link sections in the same file. */
11619 /* FIXME: We do not check for the presence of multiple, same-name debuglink sections. */
11620 /* FIXME: We do not check for the presence of a dwo link as well as a debuglink. */
11622 check_for_and_load_links (file, filename);
11623 if (first_separate_info != NULL)
11624 return true;
11626 do_follow_links = 0;
11627 return false;
11630 void
11631 free_debug_memory (void)
11633 unsigned int i;
11635 free_all_abbrevs ();
11637 free (cu_abbrev_map);
11638 cu_abbrev_map = NULL;
11639 next_free_abbrev_map_entry = 0;
11641 free (shndx_pool);
11642 shndx_pool = NULL;
11643 shndx_pool_size = 0;
11644 shndx_pool_used = 0;
11645 free (cu_sets);
11646 cu_sets = NULL;
11647 cu_count = 0;
11648 free (tu_sets);
11649 tu_sets = NULL;
11650 tu_count = 0;
11652 memset (level_type_signed, 0, sizeof level_type_signed);
11653 cu_tu_indexes_read = -1;
11655 for (i = 0; i < max; i++)
11656 free_debug_section ((enum dwarf_section_display_enum) i);
11658 if (debug_information != NULL)
11660 for (i = 0; i < alloc_num_debug_info_entries; i++)
11662 if (debug_information [i].max_loc_offsets)
11664 free (debug_information [i].loc_offsets);
11665 free (debug_information [i].have_frame_base);
11667 if (debug_information [i].max_range_lists)
11668 free (debug_information [i].range_lists);
11670 free (debug_information);
11671 debug_information = NULL;
11672 alloc_num_debug_info_entries = num_debug_info_entries = 0;
11675 separate_info * d;
11676 separate_info * next;
11678 for (d = first_separate_info; d != NULL; d = next)
11680 close_debug_file (d->handle);
11681 free ((void *) d->filename);
11682 next = d->next;
11683 free ((void *) d);
11685 first_separate_info = NULL;
11687 free_dwo_info ();
11690 void
11691 dwarf_select_sections_by_names (const char *names)
11693 typedef struct
11695 const char * option;
11696 int * variable;
11697 int val;
11699 debug_dump_long_opts;
11701 static const debug_dump_long_opts opts_table [] =
11703 /* Please keep this table alpha- sorted. */
11704 { "Ranges", & do_debug_ranges, 1 },
11705 { "abbrev", & do_debug_abbrevs, 1 },
11706 { "addr", & do_debug_addr, 1 },
11707 { "aranges", & do_debug_aranges, 1 },
11708 { "cu_index", & do_debug_cu_index, 1 },
11709 { "decodedline", & do_debug_lines, FLAG_DEBUG_LINES_DECODED },
11710 { "follow-links", & do_follow_links, 1 },
11711 { "frames", & do_debug_frames, 1 },
11712 { "frames-interp", & do_debug_frames_interp, 1 },
11713 /* The special .gdb_index section. */
11714 { "gdb_index", & do_gdb_index, 1 },
11715 { "info", & do_debug_info, 1 },
11716 { "line", & do_debug_lines, FLAG_DEBUG_LINES_RAW }, /* For backwards compatibility. */
11717 { "links", & do_debug_links, 1 },
11718 { "loc", & do_debug_loc, 1 },
11719 { "macro", & do_debug_macinfo, 1 },
11720 { "no-follow-links", & do_follow_links, 0 },
11721 { "pubnames", & do_debug_pubnames, 1 },
11722 { "pubtypes", & do_debug_pubtypes, 1 },
11723 /* This entry is for compatibility
11724 with earlier versions of readelf. */
11725 { "ranges", & do_debug_aranges, 1 },
11726 { "rawline", & do_debug_lines, FLAG_DEBUG_LINES_RAW },
11727 { "str", & do_debug_str, 1 },
11728 { "str-offsets", & do_debug_str_offsets, 1 },
11729 /* These trace_* sections are used by Itanium VMS. */
11730 { "trace_abbrev", & do_trace_abbrevs, 1 },
11731 { "trace_aranges", & do_trace_aranges, 1 },
11732 { "trace_info", & do_trace_info, 1 },
11733 { NULL, NULL, 0 }
11736 const char *p;
11738 p = names;
11739 while (*p)
11741 const debug_dump_long_opts * entry;
11743 for (entry = opts_table; entry->option; entry++)
11745 size_t len = strlen (entry->option);
11747 if (strncmp (p, entry->option, len) == 0
11748 && (p[len] == ',' || p[len] == '\0'))
11750 * entry->variable = entry->val;
11752 /* The --debug-dump=frames-interp option also
11753 enables the --debug-dump=frames option. */
11754 if (do_debug_frames_interp)
11755 do_debug_frames = 1;
11757 p += len;
11758 break;
11762 if (entry->option == NULL)
11764 warn (_("Unrecognized debug option '%s'\n"), p);
11765 p = strchr (p, ',');
11766 if (p == NULL)
11767 break;
11770 if (*p == ',')
11771 p++;
11775 void
11776 dwarf_select_sections_by_letters (const char *letters)
11778 unsigned int lindex = 0;
11780 while (letters[lindex])
11781 switch (letters[lindex++])
11783 case 'A': do_debug_addr = 1; break;
11784 case 'a': do_debug_abbrevs = 1; break;
11785 case 'c': do_debug_cu_index = 1; break;
11786 case 'F': do_debug_frames_interp = 1; /* Fall through. */
11787 case 'f': do_debug_frames = 1; break;
11788 case 'g': do_gdb_index = 1; break;
11789 case 'i': do_debug_info = 1; break;
11790 case 'K': do_follow_links = 1; break;
11791 case 'N': do_follow_links = 0; break;
11792 case 'k': do_debug_links = 1; break;
11793 case 'l': do_debug_lines |= FLAG_DEBUG_LINES_RAW; break;
11794 case 'L': do_debug_lines |= FLAG_DEBUG_LINES_DECODED; break;
11795 case 'm': do_debug_macinfo = 1; break;
11796 case 'O': do_debug_str_offsets = 1; break;
11797 case 'o': do_debug_loc = 1; break;
11798 case 'p': do_debug_pubnames = 1; break;
11799 case 'R': do_debug_ranges = 1; break;
11800 case 'r': do_debug_aranges = 1; break;
11801 case 's': do_debug_str = 1; break;
11802 case 'T': do_trace_aranges = 1; break;
11803 case 't': do_debug_pubtypes = 1; break;
11804 case 'U': do_trace_info = 1; break;
11805 case 'u': do_trace_abbrevs = 1; break;
11807 default:
11808 warn (_("Unrecognized debug option '%s'\n"), letters);
11809 break;
11813 void
11814 dwarf_select_sections_all (void)
11816 do_debug_info = 1;
11817 do_debug_abbrevs = 1;
11818 do_debug_lines = FLAG_DEBUG_LINES_RAW;
11819 do_debug_pubnames = 1;
11820 do_debug_pubtypes = 1;
11821 do_debug_aranges = 1;
11822 do_debug_ranges = 1;
11823 do_debug_frames = 1;
11824 do_debug_macinfo = 1;
11825 do_debug_str = 1;
11826 do_debug_loc = 1;
11827 do_gdb_index = 1;
11828 do_trace_info = 1;
11829 do_trace_abbrevs = 1;
11830 do_trace_aranges = 1;
11831 do_debug_addr = 1;
11832 do_debug_cu_index = 1;
11833 do_follow_links = 1;
11834 do_debug_links = 1;
11835 do_debug_str_offsets = 1;
11838 #define NO_ABBREVS NULL, NULL, NULL, 0, 0, 0, NULL, 0
11839 #define ABBREV(N) NULL, NULL, NULL, 0, 0, N, NULL, 0
11841 /* N.B. The order here must match the order in section_display_enum. */
11843 struct dwarf_section_display debug_displays[] =
11845 { { ".debug_abbrev", ".zdebug_abbrev", ".dwabrev", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
11846 { { ".debug_aranges", ".zdebug_aranges", ".dwarnge", NO_ABBREVS }, display_debug_aranges, &do_debug_aranges, true },
11847 { { ".debug_frame", ".zdebug_frame", ".dwframe", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
11848 { { ".debug_info", ".zdebug_info", ".dwinfo", ABBREV (abbrev)}, display_debug_info, &do_debug_info, true },
11849 { { ".debug_line", ".zdebug_line", ".dwline", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
11850 { { ".debug_pubnames", ".zdebug_pubnames", ".dwpbnms", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubnames, false },
11851 { { ".debug_gnu_pubnames", ".zdebug_gnu_pubnames", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubnames, false },
11852 { { ".eh_frame", "", "", NO_ABBREVS }, display_debug_frames, &do_debug_frames, true },
11853 { { ".debug_macinfo", ".zdebug_macinfo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
11854 { { ".debug_macro", ".zdebug_macro", ".dwmac", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
11855 { { ".debug_str", ".zdebug_str", ".dwstr", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
11856 { { ".debug_line_str", ".zdebug_line_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
11857 { { ".debug_loc", ".zdebug_loc", ".dwloc", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
11858 { { ".debug_loclists", ".zdebug_loclists", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
11859 { { ".debug_pubtypes", ".zdebug_pubtypes", ".dwpbtyp", NO_ABBREVS }, display_debug_pubnames, &do_debug_pubtypes, false },
11860 { { ".debug_gnu_pubtypes", ".zdebug_gnu_pubtypes", "", NO_ABBREVS }, display_debug_gnu_pubnames, &do_debug_pubtypes, false },
11861 { { ".debug_ranges", ".zdebug_ranges", ".dwrnges", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
11862 { { ".debug_rnglists", ".zdebug_rnglists", "", NO_ABBREVS }, display_debug_ranges, &do_debug_ranges, true },
11863 { { ".debug_static_func", ".zdebug_static_func", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
11864 { { ".debug_static_vars", ".zdebug_static_vars", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
11865 { { ".debug_types", ".zdebug_types", "", ABBREV (abbrev) }, display_debug_types, &do_debug_info, true },
11866 { { ".debug_weaknames", ".zdebug_weaknames", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
11867 { { ".gdb_index", "", "", NO_ABBREVS }, display_gdb_index, &do_gdb_index, false },
11868 { { ".debug_names", "", "", NO_ABBREVS }, display_debug_names, &do_gdb_index, false },
11869 { { ".trace_info", "", "", ABBREV (trace_abbrev) }, display_trace_info, &do_trace_info, true },
11870 { { ".trace_abbrev", "", "", NO_ABBREVS }, display_debug_abbrev, &do_trace_abbrevs, false },
11871 { { ".trace_aranges", "", "", NO_ABBREVS }, display_debug_aranges, &do_trace_aranges, false },
11872 { { ".debug_info.dwo", ".zdebug_info.dwo", "", ABBREV (abbrev_dwo) }, display_debug_info, &do_debug_info, true },
11873 { { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo", "", NO_ABBREVS }, display_debug_abbrev, &do_debug_abbrevs, false },
11874 { { ".debug_types.dwo", ".zdebug_types.dwo", "", ABBREV (abbrev_dwo) }, display_debug_types, &do_debug_info, true },
11875 { { ".debug_line.dwo", ".zdebug_line.dwo", "", NO_ABBREVS }, display_debug_lines, &do_debug_lines, true },
11876 { { ".debug_loc.dwo", ".zdebug_loc.dwo", "", NO_ABBREVS }, display_debug_loc, &do_debug_loc, true },
11877 { { ".debug_macro.dwo", ".zdebug_macro.dwo", "", NO_ABBREVS }, display_debug_macro, &do_debug_macinfo, true },
11878 { { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo", "", NO_ABBREVS }, display_debug_macinfo, &do_debug_macinfo, false },
11879 { { ".debug_str.dwo", ".zdebug_str.dwo", "", NO_ABBREVS }, display_debug_str, &do_debug_str, true },
11880 { { ".debug_str_offsets", ".zdebug_str_offsets", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
11881 { { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo", "", NO_ABBREVS }, display_debug_str_offsets, &do_debug_str_offsets, true },
11882 { { ".debug_addr", ".zdebug_addr", "", NO_ABBREVS }, display_debug_addr, &do_debug_addr, true },
11883 { { ".debug_cu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
11884 { { ".debug_tu_index", "", "", NO_ABBREVS }, display_cu_index, &do_debug_cu_index, false },
11885 { { ".gnu_debuglink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
11886 { { ".gnu_debugaltlink", "", "", NO_ABBREVS }, display_debug_links, &do_debug_links, false },
11887 { { ".debug_sup", "", "", NO_ABBREVS }, display_debug_sup, &do_debug_links, false },
11888 /* Separate debug info files can containt their own .debug_str section,
11889 and this might be in *addition* to a .debug_str section already present
11890 in the main file. Hence we need to have two entries for .debug_str. */
11891 { { ".debug_str", ".zdebug_str", "", NO_ABBREVS }, display_debug_str, &do_debug_str, false },
11892 { { ".note.gnu.build-id", "", "", NO_ABBREVS }, display_debug_not_supported, NULL, false },
11895 /* A static assertion. */
11896 extern int debug_displays_assert[ARRAY_SIZE (debug_displays) == max ? 1 : -1];